1 /*-
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/usr.sbin/pw/pw_group.c,v 1.16 2008/02/23 01:25:22 scf Exp $
27 */
28
29 #include <ctype.h>
30 #include <err.h>
31 #include <termios.h>
32 #include <stdbool.h>
33 #include <unistd.h>
34
35 #include "pw.h"
36 #include "bitmap.h"
37
38
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);
44
45 static
46 int
alldigits(const char * str)47 alldigits(const char *str)
48 {
49 while (*str) {
50 if (*str < '0' || *str > '9')
51 return(0);
52 ++str;
53 }
54 return(1);
55 }
56
57 int
pw_group(struct userconf * cnf,int mode,struct cargs * args)58 pw_group(struct userconf * cnf, int mode, struct cargs * args)
59 {
60 int rc;
61 struct carg *a_name = getarg(args, 'n');
62 struct carg *a_gid = getarg(args, 'g');
63 struct carg *arg;
64 struct group *grp = NULL;
65 int grmembers = 0;
66 char **members = NULL;
67
68 static struct group fakegroup =
69 {
70 "nogroup",
71 "*",
72 -1,
73 NULL
74 };
75
76 if (mode == M_LOCK || mode == M_UNLOCK)
77 errx(EX_USAGE, "'lock' command is not available for groups");
78
79 /*
80 * With M_NEXT, we only need to return the
81 * next gid to stdout
82 */
83 if (mode == M_NEXT) {
84 gid_t next = gr_gidpolicy(cnf, args);
85 if (getarg(args, 'q'))
86 return next;
87 printf("%ld\n", (long)next);
88 return EXIT_SUCCESS;
89 }
90
91 if (mode == M_PRINT && getarg(args, 'a')) {
92 int pretty = getarg(args, 'P') != NULL;
93
94 SETGRENT();
95 while ((grp = GETGRENT()) != NULL)
96 print_group(grp, pretty);
97 ENDGRENT();
98 return EXIT_SUCCESS;
99 }
100 if (a_gid == NULL) {
101 if (a_name == NULL)
102 errx(EX_DATAERR, "group name or id required");
103
104 if (mode != M_ADD && grp == NULL && alldigits(a_name->val)) {
105 (a_gid = a_name)->ch = 'g';
106 a_name = NULL;
107 }
108 }
109 grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val));
110
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));
114
115 if (grp == NULL) {
116 if (mode == M_PRINT && getarg(args, 'F')) {
117 char *fmems[1];
118 fmems[0] = NULL;
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);
123 }
124 errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val);
125 }
126 if (a_name == NULL) /* Needed later */
127 a_name = addarg(args, 'n', grp->gr_name);
128
129 /*
130 * Handle deletions now
131 */
132 if (mode == M_DELETE) {
133 gid_t gid = grp->gr_gid;
134
135 rc = delgrent(grp);
136 if (rc == -1)
137 err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name);
138 else if (rc != 0) {
139 warn("group update");
140 return EX_IOERR;
141 }
142 pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid);
143 return EXIT_SUCCESS;
144 } else if (mode == M_PRINT)
145 return print_group(grp, getarg(args, 'P') != NULL);
146
147 if (a_gid)
148 grp->gr_gid = (gid_t) atoi(a_gid->val);
149
150 if ((arg = getarg(args, 'l')) != NULL)
151 grp->gr_name = pw_checkname((u_char *)arg->val, 0);
152 } else {
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);
157
158 extendarray(&members, &grmembers, 200);
159 members[0] = NULL;
160 grp = &fakegroup;
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;
165 }
166
167 /*
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
171 * software.
172 */
173
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 */
178 else {
179 int fd = atoi(arg->val);
180 int precrypt = (arg->ch == 'H');
181 int b;
182 int istty = isatty(fd);
183 struct termios t;
184 char *p, line[256];
185
186 if (istty) {
187 if (tcgetattr(fd, &t) == -1)
188 istty = 0;
189 else {
190 struct termios n = t;
191
192 /* Disable echo */
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);
196 fflush(stdout);
197 }
198 }
199 b = read(fd, line, sizeof(line) - 1);
200 if (istty) { /* Restore state */
201 tcsetattr(fd, TCSANOW, &t);
202 fputc('\n', stdout);
203 fflush(stdout);
204 }
205 if (b < 0) {
206 warn("-h file descriptor");
207 return EX_OSERR;
208 }
209 line[b] = '\0';
210 if ((p = strpbrk(line, " \t\r\n")) != NULL)
211 *p = '\0';
212 if (!*line)
213 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
214 if (precrypt) {
215 if (strchr(line, ':') != NULL)
216 return EX_DATAERR;
217 grp->gr_passwd = line;
218 } else
219 grp->gr_passwd = pw_pwcrypt(line);
220 }
221 }
222
223 if (((arg = getarg(args, 'M')) != NULL ||
224 (arg = getarg(args, 'd')) != NULL ||
225 (arg = getarg(args, 'm')) != NULL) && arg->val) {
226 int i = 0;
227 char *p;
228 struct passwd *pwd;
229
230 /* Make sure this is not stay NULL with -M "" */
231 extendarray(&members, &grmembers, 200);
232 if (arg->ch == 'd')
233 delete_members(&members, &grmembers, &i, arg, grp);
234 else if (arg->ch == 'm') {
235 int k = 0;
236
237 while (grp->gr_mem[k] != NULL) {
238 if (extendarray(&members, &grmembers, i + 2) != -1)
239 members[i++] = grp->gr_mem[k];
240 k++;
241 }
242 }
243
244 if (arg->ch != 'd')
245 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
246 int j;
247
248 /*
249 * Check for duplicates
250 */
251 pwd = lookup_pwent(p);
252 for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
253 ;
254 if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
255 members[i++] = newstr(pwd->pw_name);
256 }
257 while (i < grmembers)
258 members[i++] = NULL;
259 grp->gr_mem = members;
260 }
261
262 if (getarg(args, 'N') != NULL)
263 return print_group(grp, getarg(args, 'P') != NULL);
264
265 if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
266 if (rc == -1)
267 warnx("group '%s' already exists", grp->gr_name);
268 else
269 warn("group update");
270 return EX_IOERR;
271 } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) {
272 if (rc == -1)
273 warnx("group '%s' not available (NIS?)", grp->gr_name);
274 else
275 warn("group update");
276 return EX_IOERR;
277 }
278 /* grp may have been invalidated */
279 if ((grp = GETGRNAM(a_name->val)) == NULL)
280 errx(EX_SOFTWARE, "group disappeared during update");
281
282 pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
283
284 if (members)
285 free(members);
286
287 return EXIT_SUCCESS;
288 }
289
290
291 /*
292 * Lookup a passwd entry using a name or UID.
293 */
294 static struct passwd *
lookup_pwent(const char * user)295 lookup_pwent(const char *user)
296 {
297 struct passwd *pwd;
298
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);
303
304 return (pwd);
305 }
306
307
308 /*
309 * Delete requested members from a group.
310 */
311 static void
delete_members(char *** members,int * grmembers,int * i,struct carg * arg,struct group * grp)312 delete_members(char ***members, int *grmembers, int *i, struct carg *arg,
313 struct group *grp)
314 {
315 bool matchFound;
316 char *user;
317 char *valueCopy;
318 char *valuePtr;
319 int k;
320 struct passwd *pwd;
321
322 k = 0;
323 while (grp->gr_mem[k] != NULL) {
324 matchFound = false;
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) {
331 matchFound = true;
332 break;
333 }
334 }
335 free(valueCopy);
336
337 if (!matchFound &&
338 extendarray(members, grmembers, *i + 2) != -1)
339 (*members)[(*i)++] = grp->gr_mem[k];
340
341 k++;
342 }
343
344 return;
345 }
346
347
348 static gid_t
gr_gidpolicy(struct userconf * cnf,struct cargs * args)349 gr_gidpolicy(struct userconf * cnf, struct cargs * args)
350 {
351 struct group *grp;
352 gid_t gid = (gid_t) - 1;
353 struct carg *a_gid = getarg(args, 'g');
354
355 /*
356 * Check the given gid, if any
357 */
358 if (a_gid != NULL) {
359 gid = (gid_t) atol(a_gid->val);
360
361 if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL)
362 errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid);
363 } else {
364 struct bitmap bm;
365
366 /*
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
370 */
371 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */
372 cnf->min_gid = 1000;
373 cnf->max_gid = 32000;
374 }
375 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
376
377 /*
378 * Now, let's fill the bitmap from the password file
379 */
380 SETGRENT();
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);
385 ENDGRENT();
386
387 /*
388 * Then apply the policy, with fallback to reuse if necessary
389 */
390 if (cnf->reuse_gids)
391 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
392 else {
393 gid = (gid_t) (bm_lastset(&bm) + 1);
394 if (!bm_isset(&bm, gid))
395 gid += cnf->min_gid;
396 else
397 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
398 }
399
400 /*
401 * Another sanity check
402 */
403 if (gid < cnf->min_gid || gid > cnf->max_gid)
404 errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
405 bm_dealloc(&bm);
406 }
407 return gid;
408 }
409
410
411 static int
print_group(struct group * grp,int pretty)412 print_group(struct group * grp, int pretty)
413 {
414 if (!pretty) {
415 int buflen = 0;
416 char *buf = NULL;
417
418 fmtgrent(&buf, &buflen, grp);
419 fputs(buf, stdout);
420 free(buf);
421 } else {
422 int i;
423
424 printf("Group Name: %-15s #%lu\n"
425 " Members: ",
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);
430 }
431 return EXIT_SUCCESS;
432 }
433