3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 static const char rcsid[] =
44 static struct passwd *lookup_pwent(const char *user);
45 static void delete_members(char ***members, int *grmembers, int *i,
46 struct carg *arg, struct group *grp);
47 static int print_group(struct group * grp, int pretty);
48 static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args);
51 pw_group(struct userconf * cnf, int mode, struct cargs * args)
54 struct carg *a_newname = getarg(args, 'l');
55 struct carg *a_name = getarg(args, 'n');
56 struct carg *a_gid = getarg(args, 'g');
58 struct group *grp = NULL;
60 char **members = NULL;
62 static struct group fakegroup =
71 if (strspn(a_gid->val, "0123456789") != strlen(a_gid->val))
72 errx(EX_USAGE, "-g expects a number");
75 if (mode == M_LOCK || mode == M_UNLOCK)
76 errx(EX_USAGE, "'lock' command is not available for groups");
79 * With M_NEXT, we only need to return the
83 gid_t next = gr_gidpolicy(cnf, args);
84 if (getarg(args, 'q'))
86 printf("%ld\n", (long)next);
90 if (mode == M_PRINT && getarg(args, 'a')) {
91 int pretty = getarg(args, 'P') != NULL;
94 while ((grp = GETGRENT()) != NULL)
95 print_group(grp, pretty);
101 errx(EX_DATAERR, "group name or id required");
103 if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) {
104 (a_gid = a_name)->ch = 'g';
108 grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val));
110 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
111 if (a_name == NULL && grp == NULL) /* Try harder */
112 grp = GETGRGID(atoi(a_gid->val));
115 if (mode == M_PRINT && getarg(args, 'F')) {
118 fakegroup.gr_name = a_name ? a_name->val : "nogroup";
119 fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1;
120 fakegroup.gr_mem = fmems;
121 return print_group(&fakegroup, getarg(args, 'P') != NULL);
123 errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val);
125 if (a_name == NULL) /* Needed later */
126 a_name = addarg(args, 'n', grp->gr_name);
129 * Handle deletions now
131 if (mode == M_DELETE) {
132 gid_t gid = grp->gr_gid;
136 err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name);
138 warn("group update");
141 pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid);
143 } else if (mode == M_PRINT)
144 return print_group(grp, getarg(args, 'P') != NULL);
147 grp->gr_gid = (gid_t) atoi(a_gid->val);
149 if (a_newname != NULL)
150 grp->gr_name = pw_checkname((u_char *)a_newname->val, 0);
152 if (a_name == NULL) /* Required */
153 errx(EX_DATAERR, "group name required");
154 else if (grp != NULL) /* Exists */
155 errx(EX_DATAERR, "group name `%s' already exists", a_name->val);
157 extendarray(&members, &grmembers, 200);
160 grp->gr_name = pw_checkname((u_char *)a_name->val, 0);
161 grp->gr_passwd = "*";
162 grp->gr_gid = gr_gidpolicy(cnf, args);
163 grp->gr_mem = members;
167 * This allows us to set a group password Group passwords is an
168 * antique idea, rarely used and insecure (no secure database) Should
169 * be discouraged, but it is apparently still supported by some
173 if ((arg = getarg(args, 'h')) != NULL ||
174 (arg = getarg(args, 'H')) != NULL) {
175 if (strcmp(arg->val, "-") == 0)
176 grp->gr_passwd = "*"; /* No access */
178 int fd = atoi(arg->val);
179 int precrypt = (arg->ch == 'H');
181 int istty = isatty(fd);
186 if (tcgetattr(fd, &t) == -1)
189 struct termios n = t;
192 n.c_lflag &= ~(ECHO);
193 tcsetattr(fd, TCSANOW, &n);
194 printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name);
198 b = read(fd, line, sizeof(line) - 1);
199 if (istty) { /* Restore state */
200 tcsetattr(fd, TCSANOW, &t);
205 warn("-h file descriptor");
209 if ((p = strpbrk(line, " \t\r\n")) != NULL)
212 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
214 if (strchr(line, ':') != NULL)
216 grp->gr_passwd = line;
218 grp->gr_passwd = pw_pwcrypt(line);
222 if (((arg = getarg(args, 'M')) != NULL ||
223 (arg = getarg(args, 'd')) != NULL ||
224 (arg = getarg(args, 'm')) != NULL) && arg->val) {
229 /* Make sure this is not stay NULL with -M "" */
230 extendarray(&members, &grmembers, 200);
232 delete_members(&members, &grmembers, &i, arg, grp);
233 else if (arg->ch == 'm') {
236 if (grp->gr_mem != NULL) {
237 while (grp->gr_mem[k] != NULL) {
238 if (extendarray(&members, &grmembers, i + 2) != -1)
239 members[i++] = grp->gr_mem[k];
246 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
250 * Check for duplicates
252 pwd = lookup_pwent(p);
253 for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
255 if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
256 members[i++] = newstr(pwd->pw_name);
258 while (i < grmembers)
260 grp->gr_mem = members;
263 if (getarg(args, 'N') != NULL)
264 return print_group(grp, getarg(args, 'P') != NULL);
266 if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
268 warnx("group '%s' already exists", grp->gr_name);
270 warn("group update");
272 } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) {
274 warnx("group '%s' not available (NIS?)", grp->gr_name);
276 warn("group update");
280 arg = a_newname != NULL ? a_newname : a_name;
281 /* grp may have been invalidated */
282 if ((grp = GETGRNAM(arg->val)) == NULL)
283 errx(EX_SOFTWARE, "group disappeared during update");
285 pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
294 * Lookup a passwd entry using a name or UID.
296 static struct passwd *
297 lookup_pwent(const char *user)
301 if ((pwd = GETPWNAM(user)) == NULL &&
302 (!isdigit((unsigned char)*user) ||
303 (pwd = getpwuid((uid_t) atoi(user))) == NULL))
304 errx(EX_NOUSER, "user `%s' does not exist", user);
311 * Delete requested members from a group.
314 delete_members(char ***members, int *grmembers, int *i, struct carg *arg,
324 if (grp->gr_mem == NULL)
328 while (grp->gr_mem[k] != NULL) {
330 if ((valueCopy = strdup(arg->val)) == NULL)
331 errx(EX_UNAVAILABLE, "out of memory");
332 valuePtr = valueCopy;
333 while ((user = strsep(&valuePtr, ", \t")) != NULL) {
334 pwd = lookup_pwent(user);
335 if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) {
343 extendarray(members, grmembers, *i + 2) != -1)
344 (*members)[(*i)++] = grp->gr_mem[k];
354 gr_gidpolicy(struct userconf * cnf, struct cargs * args)
357 gid_t gid = (gid_t) - 1;
358 struct carg *a_gid = getarg(args, 'g');
361 * Check the given gid, if any
364 gid = (gid_t) atol(a_gid->val);
366 if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL)
367 errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid);
372 * We need to allocate the next available gid under one of
373 * two policies a) Grab the first unused gid b) Grab the
374 * highest possible unused gid
376 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */
378 cnf->max_gid = 32000;
380 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
383 * Now, let's fill the bitmap from the password file
386 while ((grp = GETGRENT()) != NULL)
387 if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
388 (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
389 bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
393 * Then apply the policy, with fallback to reuse if necessary
396 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
398 gid = (gid_t) (bm_lastset(&bm) + 1);
399 if (!bm_isset(&bm, gid))
402 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
406 * Another sanity check
408 if (gid < cnf->min_gid || gid > cnf->max_gid)
409 errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
417 print_group(struct group * grp, int pretty)
428 printf("Group Name: %-15s #%lu\n"
430 grp->gr_name, (long) grp->gr_gid);
431 if (grp->gr_mem != NULL) {
432 for (i = 0; grp->gr_mem[i]; i++)
433 printf("%s%s", i ? "," : "", grp->gr_mem[i]);
435 fputs("\n\n", stdout);