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