Hi,
this patch partially reduces size of deluser/delgroup and adds better
error checking and a few additional features:
Pros:
1) size increase vs functionality is not bad. With all options enabled:
scripts/bloat-o-meter busybox_old busybox_unstripped
function old new delta
deluser_main 189 214 +25
.rodata 134994 135016 +22
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 47/0) Total: 47 bytes
2) proper error checking and reporting if removing non existent user/group
3) proper error checking and reporting if removing primary group of a user
4) deluser also removes user's primary group if ENABLE_DELGROUP is set, so no
leftovers
5) maximum dead code optimization by the compiler with different options turned
off
Contra:
1) code is obfuscated, sorry can't do nothing about that it's my style.
2) Not known ....yet :-)
The patch is tested and seems to work well for me.
More testing by the list members is appreciated.
Hints, critics and improvements by the list members are welcome.
Please apply if you like it.
Attached you'll find also a drop in deluser,c file
for testing and review as the patch is rather big.
Ciao,
Tito
--- loginutils/deluser.c.orig 2010-09-22 22:05:25.000000000 +0200
+++ loginutils/deluser.c 2010-10-03 01:28:37.000000000 +0200
@@ -14,41 +14,47 @@
int deluser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int deluser_main(int argc, char **argv)
{
- if (argc != 2
- && (!ENABLE_FEATURE_DEL_USER_FROM_GROUP
- || applet_name[3] != 'g'
- || argc != 3)
- ) {
- bb_show_usage();
- }
-
- if (geteuid())
- bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
+ char *name = argv[1];
+ char *member = NULL;
+ const char *pfile = bb_path_passwd_file;
+ const char *sfile = NULL;
+ bool do_deluser = (ENABLE_DELUSER && (!ENABLE_DELGROUP || applet_name[3] == 'u'));
- if (ENABLE_DELUSER && applet_name[3] == 'u') {
- /* deluser USER */
- if (update_passwd(bb_path_passwd_file, argv[1], NULL, NULL) < 0)
- return EXIT_FAILURE;
- if (ENABLE_FEATURE_SHADOWPASSWDS)
- if (update_passwd(bb_path_shadow_file, argv[1], NULL, NULL) < 0)
- return EXIT_FAILURE;
- } else if (ENABLE_DELGROUP) {
- /* delgroup ... */
- if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || argc != 3) {
- /* delgroup GROUP */
- if (update_passwd(bb_path_group_file, argv[1], NULL, NULL) < 0)
- return EXIT_FAILURE;
- if (ENABLE_FEATURE_SHADOWPASSWDS)
- if (update_passwd(bb_path_gshadow_file, argv[1], NULL, NULL) < 0)
+ switch (argc) {
+ case 3:
+ if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || do_deluser)
+ break;
+ name = argv[2];
+ member = argv[1];
+ /* Fallthrough */
+ case 2:
+ if (geteuid())
+ bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
+ if (do_deluser) {
+ xgetpwnam(name);
+ if (ENABLE_FEATURE_SHADOWPASSWDS)
+ sfile = bb_path_shadow_file;
+ } else {
+ do_delgroup:
+ xgetgrnam(name);
+ if (!member && getpwnam(name))
+ bb_error_msg_and_die("is %s's primary group", name);
+ pfile = bb_path_group_file;
+ if (ENABLE_FEATURE_SHADOWPASSWDS)
+ sfile = bb_path_gshadow_file;
+ }
+ do {
+ if (update_passwd(pfile, name, NULL, member) == -1)
return EXIT_FAILURE;
- } else {
- /* delgroup USER GROUP */
- if (update_passwd(bb_path_group_file, argv[2], NULL, argv[1]) < 0)
- return EXIT_FAILURE;
- if (ENABLE_FEATURE_SHADOWPASSWDS)
- if (update_passwd(bb_path_gshadow_file, argv[2], NULL, argv[1]) < 0)
- return EXIT_FAILURE;
- }
+ pfile = sfile;
+ sfile = NULL;
+ } while (pfile);
+
+ if (ENABLE_DELGROUP && do_deluser) {
+ do_deluser = 0;
+ goto do_delgroup;
+ }
+ return EXIT_SUCCESS;
}
- return EXIT_SUCCESS;
+ bb_show_usage();
}
/* vi: set sw=4 ts=4: */
/*
* deluser/delgroup implementation for busybox
*
* Copyright (C) 1999 by Lineo, inc. and John Beppu
* Copyright (C) 1999,2000,2001 by John Beppu <[email protected]>
* Copyright (C) 2007 by Tito Ragusa <[email protected]>
*
* Licensed under GPL version 2, see file LICENSE in this tarball for details.
*
*/
#include "libbb.h"
int deluser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int deluser_main(int argc, char **argv)
{
char *name = argv[1];
char *member = NULL;
const char *pfile = bb_path_passwd_file;
const char *sfile = NULL;
bool do_deluser = (ENABLE_DELUSER && (!ENABLE_DELGROUP || applet_name[3] == 'u'));
switch (argc) {
case 3:
if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || do_deluser)
break;
name = argv[2];
member = argv[1];
/* Fallthrough */
case 2:
if (geteuid())
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
if (do_deluser) {
xgetpwnam(name);
if (ENABLE_FEATURE_SHADOWPASSWDS)
sfile = bb_path_shadow_file;
} else {
do_delgroup:
xgetgrnam(name);
if (!member && getpwnam(name))
bb_error_msg_and_die("is %s's primary group", name);
pfile = bb_path_group_file;
if (ENABLE_FEATURE_SHADOWPASSWDS)
sfile = bb_path_gshadow_file;
}
do {
if (update_passwd(pfile, name, NULL, member) == -1)
return EXIT_FAILURE;
pfile = sfile;
sfile = NULL;
} while (pfile);
if (ENABLE_DELGROUP && do_deluser) {
do_deluser = 0;
goto do_delgroup;
}
return EXIT_SUCCESS;
}
bb_show_usage();
}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox