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