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
26 * $FreeBSD: src/usr.sbin/pw/pw_group.c,v 1.16 2008/02/23 01:25:22 scf Exp $
39 static struct passwd *lookup_pwent(const char *user);
40 static void delete_members(char ***members, int *grmembers, int *i,
41 struct carg *arg, struct group *grp);
42 static int print_group(struct group * grp, int pretty);
43 static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args);
47 alldigits(const char *str)
50 if (*str < '0' || *str > '9')
58 pw_group(struct userconf * cnf, int mode, struct cargs * args)
61 struct carg *a_name = getarg(args, 'n');
62 struct carg *a_gid = getarg(args, 'g');
64 struct group *grp = NULL;
66 char **members = NULL;
68 static struct group fakegroup =
76 if (mode == M_LOCK || mode == M_UNLOCK)
77 errx(EX_USAGE, "'lock' command is not available for groups");
80 * With M_NEXT, we only need to return the
84 gid_t next = gr_gidpolicy(cnf, args);
85 if (getarg(args, 'q'))
87 printf("%ld\n", (long)next);
91 if (mode == M_PRINT && getarg(args, 'a')) {
92 int pretty = getarg(args, 'P') != NULL;
95 while ((grp = GETGRENT()) != NULL)
96 print_group(grp, pretty);
102 errx(EX_DATAERR, "group name or id required");
104 if (mode != M_ADD && grp == NULL && alldigits(a_name->val)) {
105 (a_gid = a_name)->ch = 'g';
109 grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val));
111 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
112 if (a_name == NULL && grp == NULL) /* Try harder */
113 grp = GETGRGID(atoi(a_gid->val));
116 if (mode == M_PRINT && getarg(args, 'F')) {
119 fakegroup.gr_name = a_name ? a_name->val : "nogroup";
120 fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : -1;
121 fakegroup.gr_mem = fmems;
122 return print_group(&fakegroup, getarg(args, 'P') != NULL);
124 errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val);
126 if (a_name == NULL) /* Needed later */
127 a_name = addarg(args, 'n', grp->gr_name);
130 * Handle deletions now
132 if (mode == M_DELETE) {
133 gid_t gid = grp->gr_gid;
137 err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name);
139 warn("group update");
142 pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid);
144 } else if (mode == M_PRINT)
145 return print_group(grp, getarg(args, 'P') != NULL);
148 grp->gr_gid = (gid_t) atoi(a_gid->val);
150 if ((arg = getarg(args, 'l')) != NULL)
151 grp->gr_name = pw_checkname((u_char *)arg->val, 0);
153 if (a_name == NULL) /* Required */
154 errx(EX_DATAERR, "group name required");
155 else if (grp != NULL) /* Exists */
156 errx(EX_DATAERR, "group name `%s' already exists", a_name->val);
158 extendarray(&members, &grmembers, 200);
161 grp->gr_name = pw_checkname((u_char *)a_name->val, 0);
162 grp->gr_passwd = "*";
163 grp->gr_gid = gr_gidpolicy(cnf, args);
164 grp->gr_mem = members;
168 * This allows us to set a group password Group passwords is an
169 * antique idea, rarely used and insecure (no secure database) Should
170 * be discouraged, but it is apparently still supported by some
174 if ((arg = getarg(args, 'h')) != NULL ||
175 (arg = getarg(args, 'H')) != NULL) {
176 if (strcmp(arg->val, "-") == 0)
177 grp->gr_passwd = "*"; /* No access */
179 int fd = atoi(arg->val);
180 int precrypt = (arg->ch == 'H');
182 int istty = isatty(fd);
187 if (tcgetattr(fd, &t) == -1)
190 struct termios n = t;
193 n.c_lflag &= ~(ECHO);
194 tcsetattr(fd, TCSANOW, &n);
195 printf("%sassword for group %s:", (mode == M_UPDATE) ? "New p" : "P", grp->gr_name);
199 b = read(fd, line, sizeof(line) - 1);
200 if (istty) { /* Restore state */
201 tcsetattr(fd, TCSANOW, &t);
206 warn("-h file descriptor");
210 if ((p = strpbrk(line, " \t\r\n")) != NULL)
213 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
215 if (strchr(line, ':') != NULL)
217 grp->gr_passwd = line;
219 grp->gr_passwd = pw_pwcrypt(line);
223 if (((arg = getarg(args, 'M')) != NULL ||
224 (arg = getarg(args, 'd')) != NULL ||
225 (arg = getarg(args, 'm')) != NULL) && arg->val) {
230 /* Make sure this is not stay NULL with -M "" */
231 extendarray(&members, &grmembers, 200);
233 delete_members(&members, &grmembers, &i, arg, grp);
234 else if (arg->ch == 'm') {
237 while (grp->gr_mem[k] != NULL) {
238 if (extendarray(&members, &grmembers, i + 2) != -1)
239 members[i++] = grp->gr_mem[k];
245 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
249 * Check for duplicates
251 pwd = lookup_pwent(p);
252 for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
254 if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
255 members[i++] = newstr(pwd->pw_name);
257 while (i < grmembers)
259 grp->gr_mem = members;
262 if (getarg(args, 'N') != NULL)
263 return print_group(grp, getarg(args, 'P') != NULL);
265 if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
267 warnx("group '%s' already exists", grp->gr_name);
269 warn("group update");
271 } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) {
273 warnx("group '%s' not available (NIS?)", grp->gr_name);
275 warn("group update");
278 /* grp may have been invalidated */
279 if ((grp = GETGRNAM(a_name->val)) == NULL)
280 errx(EX_SOFTWARE, "group disappeared during update");
282 pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
292 * Lookup a passwd entry using a name or UID.
294 static struct passwd *
295 lookup_pwent(const char *user)
299 if ((pwd = GETPWNAM(user)) == NULL &&
300 (!isdigit((unsigned char)*user) ||
301 (pwd = getpwuid((uid_t) atoi(user))) == NULL))
302 errx(EX_NOUSER, "user `%s' does not exist", user);
309 * Delete requested members from a group.
312 delete_members(char ***members, int *grmembers, int *i, struct carg *arg,
323 while (grp->gr_mem[k] != NULL) {
325 if ((valueCopy = strdup(arg->val)) == NULL)
326 errx(EX_UNAVAILABLE, "out of memory");
327 valuePtr = valueCopy;
328 while ((user = strsep(&valuePtr, ", \t")) != NULL) {
329 pwd = lookup_pwent(user);
330 if (strcmp(grp->gr_mem[k], pwd->pw_name) == 0) {
338 extendarray(members, grmembers, *i + 2) != -1)
339 (*members)[(*i)++] = grp->gr_mem[k];
349 gr_gidpolicy(struct userconf * cnf, struct cargs * args)
352 gid_t gid = (gid_t) - 1;
353 struct carg *a_gid = getarg(args, 'g');
356 * Check the given gid, if any
359 gid = (gid_t) atol(a_gid->val);
361 if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL)
362 errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid);
367 * We need to allocate the next available gid under one of
368 * two policies a) Grab the first unused gid b) Grab the
369 * highest possible unused gid
371 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */
373 cnf->max_gid = 32000;
375 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
378 * Now, let's fill the bitmap from the password file
381 while ((grp = GETGRENT()) != NULL)
382 if ((gid_t)grp->gr_gid >= (gid_t)cnf->min_gid &&
383 (gid_t)grp->gr_gid <= (gid_t)cnf->max_gid)
384 bm_setbit(&bm, grp->gr_gid - cnf->min_gid);
388 * Then apply the policy, with fallback to reuse if necessary
391 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
393 gid = (gid_t) (bm_lastset(&bm) + 1);
394 if (!bm_isset(&bm, gid))
397 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
401 * Another sanity check
403 if (gid < cnf->min_gid || gid > cnf->max_gid)
404 errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
412 print_group(struct group * grp, int pretty)
418 fmtgrent(&buf, &buflen, grp);
424 printf("Group Name: %-15s #%lu\n"
426 grp->gr_name, (long) grp->gr_gid);
427 for (i = 0; grp->gr_mem[i]; i++)
428 printf("%s%s", i ? "," : "", grp->gr_mem[i]);
429 fputs("\n\n", stdout);