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