deluser: make it simpler, fix inability to delete user from group
[platform/upstream/busybox.git] / loginutils / deluser.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * deluser/delgroup implementation for busybox
4  *
5  * Copyright (C) 1999 by Lineo, inc. and John Beppu
6  * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7  * Copyright (C) 2007 by Tito Ragusa <farmatito@tiscali.it>
8  *
9  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
10  *
11  */
12 #include "libbb.h"
13
14 int deluser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15 int deluser_main(int argc, char **argv)
16 {
17         if (argc != 2
18          && (!ENABLE_FEATURE_DEL_USER_FROM_GROUP
19             || applet_name[3] != 'g'
20             || argc != 3)
21         ) {
22                 bb_show_usage();
23         }
24
25         if (geteuid())
26                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
27
28         if (ENABLE_DELUSER && applet_name[3] == 'u') {
29                 /* deluser USER */
30                 if (update_passwd(bb_path_passwd_file, argv[1], NULL, NULL) < 0)
31                         return EXIT_FAILURE;
32                 if (ENABLE_FEATURE_SHADOWPASSWDS)
33                         if (update_passwd(bb_path_shadow_file, argv[1], NULL, NULL) < 0)
34                                 return EXIT_FAILURE;
35         } else if (ENABLE_DELGROUP) {
36                 /* delgroup ... */
37                 if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || argc != 3) {
38                         /* delgroup GROUP */
39                         if (update_passwd(bb_path_group_file, argv[1], NULL, NULL) < 0)
40                                 return EXIT_FAILURE;
41                         if (ENABLE_FEATURE_SHADOWPASSWDS)
42                                 if (update_passwd(bb_path_gshadow_file, argv[1], NULL, NULL) < 0)
43                                         return EXIT_FAILURE;
44                 } else {
45                         /* delgroup USER GROUP */
46                         if (update_passwd(bb_path_group_file, argv[2], NULL, argv[1]) < 0)
47                                 return EXIT_FAILURE;
48                         if (ENABLE_FEATURE_SHADOWPASSWDS)
49                                 if (update_passwd(bb_path_gshadow_file, argv[2], NULL, argv[1]) < 0)
50                                         return EXIT_FAILURE;
51                 }
52         }
53         return EXIT_SUCCESS;
54 }