Deleted Added
full compact
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
27#ifndef lint
28static const char rcsid[] =
29 "$FreeBSD: head/usr.sbin/pw/pw_group.c 273772 2014-10-28 11:20:30Z bapt $";
29 "$FreeBSD: head/usr.sbin/pw/pw_group.c 273782 2014-10-28 14:54:04Z bapt $";
30#endif /* not lint */
31
32#include <ctype.h>
33#include <err.h>
34#include <termios.h>
35#include <stdbool.h>
36#include <unistd.h>
37#include <grp.h>
38#include <libutil.h>
39
40#include "pw.h"
41#include "bitmap.h"
42
43
44static struct passwd *lookup_pwent(const char *user);
45static void delete_members(char ***members, int *grmembers, int *i,
46 struct carg *arg, struct group *grp);
47static int print_group(struct group * grp, int pretty);
48static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args);
49
50int
51pw_group(struct userconf * cnf, int mode, struct cargs * args)
52{
53 int rc;
54 struct carg *a_newname = getarg(args, 'l');
55 struct carg *a_name = getarg(args, 'n');
56 struct carg *a_gid = getarg(args, 'g');
57 struct carg *arg;
58 struct group *grp = NULL;
59 int grmembers = 0;
60 char **members = NULL;
61
62 static struct group fakegroup =
63 {
64 "nogroup",
65 "*",
66 -1,
67 NULL
68 };
69
70 if (a_gid != NULL) {
71 if (strspn(a_gid->val, "0123456789") != strlen(a_gid->val))
72 errx(EX_USAGE, "-g expects a number");
73 }
74
75 if (mode == M_LOCK || mode == M_UNLOCK)
76 errx(EX_USAGE, "'lock' command is not available for groups");
77
78 /*
79 * With M_NEXT, we only need to return the
80 * next gid to stdout
81 */
82 if (mode == M_NEXT) {
83 gid_t next = gr_gidpolicy(cnf, args);
84 if (getarg(args, 'q'))
85 return next;
86 printf("%ld\n", (long)next);
87 return EXIT_SUCCESS;
88 }
89
90 if (mode == M_PRINT && getarg(args, 'a')) {
91 int pretty = getarg(args, 'P') != NULL;
92
93 SETGRENT();
94 while ((grp = GETGRENT()) != NULL)
95 print_group(grp, pretty);
96 ENDGRENT();
97 return EXIT_SUCCESS;
98 }
99 if (a_gid == NULL) {
100 if (a_name == NULL)
101 errx(EX_DATAERR, "group name or id required");
102
103 if (mode != M_ADD && grp == NULL && isdigit((unsigned char)*a_name->val)) {
104 (a_gid = a_name)->ch = 'g';
105 a_name = NULL;
106 }
107 }
108 grp = (a_name != NULL) ? GETGRNAM(a_name->val) : GETGRGID((gid_t) atoi(a_gid->val));
109
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));
113
114 if (grp == NULL) {
115 if (mode == M_PRINT && getarg(args, 'F')) {
116 char *fmems[1];
117 fmems[0] = NULL;
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);
122 }
123 errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val);
124 }
125 if (a_name == NULL) /* Needed later */
126 a_name = addarg(args, 'n', grp->gr_name);
127
128 /*
129 * Handle deletions now
130 */
131 if (mode == M_DELETE) {
132 gid_t gid = grp->gr_gid;
133
134 rc = delgrent(grp);
135 if (rc == -1)
136 err(EX_IOERR, "group '%s' not available (NIS?)", grp->gr_name);
137 else if (rc != 0) {
138 warn("group update");
139 return EX_IOERR;
140 }
141 pw_log(cnf, mode, W_GROUP, "%s(%ld) removed", a_name->val, (long) gid);
142 return EXIT_SUCCESS;
143 } else if (mode == M_PRINT)
144 return print_group(grp, getarg(args, 'P') != NULL);
145
146 if (a_gid)
147 grp->gr_gid = (gid_t) atoi(a_gid->val);
148
149 if (a_newname != NULL)
150 grp->gr_name = pw_checkname((u_char *)a_newname->val, 0);
151 } else {
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);
156
157 extendarray(&members, &grmembers, 200);
158 members[0] = NULL;
159 grp = &fakegroup;
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;
164 }
165
166 /*
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
170 * software.
171 */
172
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 */
177 else {
178 int fd = atoi(arg->val);
179 int precrypt = (arg->ch == 'H');
180 int b;
181 int istty = isatty(fd);
182 struct termios t;
183 char *p, line[256];
184
185 if (istty) {
186 if (tcgetattr(fd, &t) == -1)
187 istty = 0;
188 else {
189 struct termios n = t;
190
191 /* Disable echo */
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);
195 fflush(stdout);
196 }
197 }
198 b = read(fd, line, sizeof(line) - 1);
199 if (istty) { /* Restore state */
200 tcsetattr(fd, TCSANOW, &t);
201 fputc('\n', stdout);
202 fflush(stdout);
203 }
204 if (b < 0) {
205 warn("-h file descriptor");
206 return EX_OSERR;
207 }
208 line[b] = '\0';
209 if ((p = strpbrk(line, " \t\r\n")) != NULL)
210 *p = '\0';
211 if (!*line)
212 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
213 if (precrypt) {
214 if (strchr(line, ':') != NULL)
215 return EX_DATAERR;
216 grp->gr_passwd = line;
217 } else
218 grp->gr_passwd = pw_pwcrypt(line);
219 }
220 }
221
222 if (((arg = getarg(args, 'M')) != NULL ||
223 (arg = getarg(args, 'd')) != NULL ||
224 (arg = getarg(args, 'm')) != NULL) && arg->val) {
225 int i = 0;
226 char *p;
227 struct passwd *pwd;
228
229 /* Make sure this is not stay NULL with -M "" */
230 extendarray(&members, &grmembers, 200);
231 if (arg->ch == 'd')
232 delete_members(&members, &grmembers, &i, arg, grp);
233 else if (arg->ch == 'm') {
234 int k = 0;
235
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];
240 k++;
241 }
242 }
243 }
244
245 if (arg->ch != 'd')
246 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
247 int j;
248
249 /*
250 * Check for duplicates
251 */
252 pwd = lookup_pwent(p);
253 for (j = 0; j < i && strcmp(members[j], pwd->pw_name) != 0; j++)
254 ;
255 if (j == i && extendarray(&members, &grmembers, i + 2) != -1)
256 members[i++] = newstr(pwd->pw_name);
257 }
258 while (i < grmembers)
259 members[i++] = NULL;
260 grp->gr_mem = members;
261 }
262
263 if (getarg(args, 'N') != NULL)
264 return print_group(grp, getarg(args, 'P') != NULL);
265
266 if (mode == M_ADD && (rc = addgrent(grp)) != 0) {
267 if (rc == -1)
268 warnx("group '%s' already exists", grp->gr_name);
269 else
270 warn("group update");
271 return EX_IOERR;
272 } else if (mode == M_UPDATE && (rc = chggrent(a_name->val, grp)) != 0) {
273 if (rc == -1)
274 warnx("group '%s' not available (NIS?)", grp->gr_name);
275 else
276 warn("group update");
277 return EX_IOERR;
278 }
279
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");
284
285 pw_log(cnf, mode, W_GROUP, "%s(%ld)", grp->gr_name, (long) grp->gr_gid);
286
287 free(members);
288
289 return EXIT_SUCCESS;
290}
291
292
293/*
294 * Lookup a passwd entry using a name or UID.
295 */
296static struct passwd *
297lookup_pwent(const char *user)
298{
299 struct passwd *pwd;
300
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);
305
306 return (pwd);
307}
308
309
310/*
311 * Delete requested members from a group.
312 */
313static void
314delete_members(char ***members, int *grmembers, int *i, struct carg *arg,
315 struct group *grp)
316{
317 bool matchFound;
318 char *user;
319 char *valueCopy;
320 char *valuePtr;
321 int k;
322 struct passwd *pwd;
323
324 if (grp->gr_mem == NULL)
325 return;
326
327 k = 0;
328 while (grp->gr_mem[k] != NULL) {
329 matchFound = false;
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) {
336 matchFound = true;
337 break;
338 }
339 }
340 free(valueCopy);
341
342 if (!matchFound &&
343 extendarray(members, grmembers, *i + 2) != -1)
344 (*members)[(*i)++] = grp->gr_mem[k];
345
346 k++;
347 }
348
349 return;
350}
351
352
353static gid_t
354gr_gidpolicy(struct userconf * cnf, struct cargs * args)
355{
356 struct group *grp;
357 gid_t gid = (gid_t) - 1;
358 struct carg *a_gid = getarg(args, 'g');
359
360 /*
361 * Check the given gid, if any
362 */
363 if (a_gid != NULL) {
364 gid = (gid_t) atol(a_gid->val);
365
366 if ((grp = GETGRGID(gid)) != NULL && getarg(args, 'o') == NULL)
367 errx(EX_DATAERR, "gid `%ld' has already been allocated", (long) grp->gr_gid);
368 } else {
369 struct bitmap bm;
370
371 /*
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
375 */
376 if (cnf->min_gid >= cnf->max_gid) { /* Sanity claus^H^H^H^Hheck */
377 cnf->min_gid = 1000;
378 cnf->max_gid = 32000;
379 }
380 bm = bm_alloc(cnf->max_gid - cnf->min_gid + 1);
381
382 /*
383 * Now, let's fill the bitmap from the password file
384 */
385 SETGRENT();
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);
390 ENDGRENT();
391
392 /*
393 * Then apply the policy, with fallback to reuse if necessary
394 */
395 if (cnf->reuse_gids)
396 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
397 else {
398 gid = (gid_t) (bm_lastset(&bm) + 1);
399 if (!bm_isset(&bm, gid))
400 gid += cnf->min_gid;
401 else
402 gid = (gid_t) (bm_firstunset(&bm) + cnf->min_gid);
403 }
404
405 /*
406 * Another sanity check
407 */
408 if (gid < cnf->min_gid || gid > cnf->max_gid)
409 errx(EX_SOFTWARE, "unable to allocate a new gid - range fully used");
410 bm_dealloc(&bm);
411 }
412 return gid;
413}
414
415
416static int
417print_group(struct group * grp, int pretty)
418{
419 if (!pretty) {
420 char *buf = NULL;
421
422 buf = gr_make(grp);
423 printf("%s\n", buf);
424 free(buf);
425 } else {
426 int i;
427
428 printf("Group Name: %-15s #%lu\n"
429 " Members: ",
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]);
434 }
435 fputs("\n\n", stdout);
436 }
437 return EXIT_SUCCESS;
438}