getopt32: remove opt_complementary
[oweals/busybox.git] / loginutils / addgroup.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * addgroup - add groups to /etc/group and /etc/gshadow
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 GPLv2 or later, see file LICENSE in this source tree.
10  *
11  */
12 //config:config ADDGROUP
13 //config:       bool "addgroup (8.2 kb)"
14 //config:       default y
15 //config:       select LONG_OPTS
16 //config:       help
17 //config:       Utility for creating a new group account.
18 //config:
19 //config:config FEATURE_ADDUSER_TO_GROUP
20 //config:       bool "Support adding users to groups"
21 //config:       default y
22 //config:       depends on ADDGROUP
23 //config:       help
24 //config:       If called with two non-option arguments,
25 //config:       addgroup will add an existing user to an
26 //config:       existing group.
27
28 //applet:IF_ADDGROUP(APPLET_NOEXEC(addgroup, addgroup, BB_DIR_USR_SBIN, BB_SUID_DROP, addgroup))
29
30 //kbuild:lib-$(CONFIG_ADDGROUP) += addgroup.o
31
32 //usage:#define addgroup_trivial_usage
33 //usage:       "[-g GID] [-S] " IF_FEATURE_ADDUSER_TO_GROUP("[USER] ") "GROUP"
34 //usage:#define addgroup_full_usage "\n\n"
35 //usage:       "Add a group" IF_FEATURE_ADDUSER_TO_GROUP(" or add a user to a group") "\n"
36 //usage:     "\n        -g GID  Group id"
37 //usage:     "\n        -S      Create a system group"
38
39 #include "libbb.h"
40
41 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
42 #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
43 #endif
44 #if CONFIG_LAST_ID < CONFIG_LAST_SYSTEM_ID
45 #error Bad LAST_ID or LAST_SYSTEM_ID in .config
46 #endif
47
48 #define OPT_GID                       (1 << 0)
49 #define OPT_SYSTEM_ACCOUNT            (1 << 1)
50
51 static void xgroup_study(struct group *g)
52 {
53         unsigned max = CONFIG_LAST_ID;
54
55         /* Make sure gr_name is unused */
56         if (getgrnam(g->gr_name)) {
57                 bb_error_msg_and_die("%s '%s' in use", "group", g->gr_name);
58                 /* these format strings are reused in adduser and addgroup */
59         }
60
61         /* if a specific gid is requested, the --system switch and */
62         /* min and max values are overridden, and the range of valid */
63         /* gid values is set to [0, INT_MAX] */
64         if (!(option_mask32 & OPT_GID)) {
65                 if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
66                         g->gr_gid = CONFIG_FIRST_SYSTEM_ID;
67                         max = CONFIG_LAST_SYSTEM_ID;
68                 } else {
69                         g->gr_gid = CONFIG_LAST_SYSTEM_ID + 1;
70                 }
71         }
72         /* Check if the desired gid is free
73          * or find the first free one */
74         while (1) {
75                 if (!getgrgid(g->gr_gid)) {
76                         return; /* found free group: return */
77                 }
78                 if (option_mask32 & OPT_GID) {
79                         /* -g N, cannot pick gid other than N: error */
80                         bb_error_msg_and_die("%s '%s' in use", "gid", itoa(g->gr_gid));
81                         /* this format strings is reused in adduser and addgroup */
82                 }
83                 if (g->gr_gid == max) {
84                         /* overflowed: error */
85                         bb_error_msg_and_die("no %cids left", 'g');
86                         /* this format string is reused in adduser and addgroup */
87                 }
88                 g->gr_gid++;
89         }
90 }
91
92 /* append a new user to the passwd file */
93 static void new_group(char *group, gid_t gid)
94 {
95         struct group gr;
96         char *p;
97
98         /* make sure gid and group haven't already been allocated */
99         gr.gr_gid = gid;
100         gr.gr_name = group;
101         xgroup_study(&gr);
102
103         /* add entry to group */
104         p = xasprintf("x:%u:", (unsigned) gr.gr_gid);
105         if (update_passwd(bb_path_group_file, group, p, NULL) < 0)
106                 exit(EXIT_FAILURE);
107         if (ENABLE_FEATURE_CLEAN_UP)
108                 free(p);
109 #if ENABLE_FEATURE_SHADOWPASSWDS
110         /* /etc/gshadow fields:
111          * 1. Group name.
112          * 2. Encrypted password.
113          *    If set, non-members of the group can join the group
114          *    by typing the password for that group using the newgrp command.
115          *    If the value is of this field ! then no user is allowed
116          *    to access the group using the newgrp command. A value of !!
117          *    is treated the same as a value of ! only it indicates
118          *    that a password has never been set before. If the value is null,
119          *    only group members can log into the group.
120          * 3. Group administrators (comma delimited list).
121          *    Group members listed here can add or remove group members
122          *    using the gpasswd command.
123          * 4. Group members (comma delimited list).
124          */
125         /* Ignore errors: if file is missing we assume admin doesn't want it */
126         update_passwd(bb_path_gshadow_file, group, "!::", NULL);
127 #endif
128 }
129
130 //FIXME: upstream addgroup has no short options! NOT COMPATIBLE!
131 static const char addgroup_longopts[] ALIGN1 =
132                 "gid\0"                 Required_argument "g"
133                 "system\0"              No_argument       "S"
134                 ;
135
136 /*
137  * addgroup will take a login_name as its first parameter.
138  *
139  * gid can be customized via command-line parameters.
140  * If called with two non-option arguments, addgroup
141  * will add an existing user to an existing group.
142  */
143 int addgroup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
144 int addgroup_main(int argc UNUSED_PARAM, char **argv)
145 {
146         unsigned opts;
147         const char *gid = "0";
148
149         /* need to be root */
150         if (geteuid()) {
151                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
152         }
153         /* Syntax:
154          *  addgroup group
155          *  addgroup --gid num group
156          *  addgroup user group
157          * Check for min, max and missing args */
158         opts = getopt32long(argv, "^" "g:S" "\0" "-1:?2", addgroup_longopts,
159                                 &gid
160         );
161         /* move past the commandline options */
162         argv += optind;
163         //argc -= optind;
164
165 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
166         if (argv[1]) {
167                 struct group *gr;
168
169                 if (opts & OPT_GID) {
170                         /* -g was there, but "addgroup -g num user group"
171                          * is a no-no */
172                         bb_show_usage();
173                 }
174
175                 /* check if group and user exist */
176                 xuname2uid(argv[0]); /* unknown user: exit */
177                 gr = xgetgrnam(argv[1]); /* unknown group: exit */
178                 /* check if user is already in this group */
179                 for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) {
180                         if (strcmp(argv[0], *(gr->gr_mem)) == 0) {
181                                 /* user is already in group: do nothing */
182                                 return EXIT_SUCCESS;
183                         }
184                 }
185                 if (update_passwd(bb_path_group_file, argv[1], NULL, argv[0]) < 0) {
186                         return EXIT_FAILURE;
187                 }
188 # if ENABLE_FEATURE_SHADOWPASSWDS
189                 update_passwd(bb_path_gshadow_file, argv[1], NULL, argv[0]);
190 # endif
191         } else
192 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
193         {
194                 die_if_bad_username(argv[0]);
195                 new_group(argv[0], xatou_range(gid, 0, CONFIG_LAST_ID));
196         }
197         /* Reached only on success */
198         return EXIT_SUCCESS;
199 }