Deleted Added
sdiff udiff text old ( 248102 ) new ( 273791 )
full compact
1/*-
2 * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
3 * 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 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libutil/gr_util.c 273791 2014-10-28 16:27:29Z bapt $");
29
30#include <sys/param.h>
31#include <sys/errno.h>
32#include <sys/stat.h>
33
34#include <ctype.h>
35#include <err.h>
36#include <fcntl.h>
37#include <grp.h>
38#include <inttypes.h>
39#include <libutil.h>
40#include <paths.h>
41#include <stdbool.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47static int lockfd = -1;
48static char group_dir[PATH_MAX];
49static char group_file[PATH_MAX];
50static char tempname[PATH_MAX];
51static int initialized;
52static size_t grmemlen(const struct group *, const char *, int *);
53static struct group *grcopy(const struct group *gr, char *mem, const char *, int ndx);
54
55/*
56 * Initialize statics
57 */
58int
59gr_init(const char *dir, const char *group)
60{
61
62 if (dir == NULL) {
63 strcpy(group_dir, _PATH_ETC);
64 } else {
65 if (strlen(dir) >= sizeof(group_dir)) {
66 errno = ENAMETOOLONG;
67 return (-1);
68 }
69 strcpy(group_dir, dir);
70 }
71
72 if (group == NULL) {
73 if (dir == NULL) {
74 strcpy(group_file, _PATH_GROUP);
75 } else if (snprintf(group_file, sizeof(group_file), "%s/group",
76 group_dir) > (int)sizeof(group_file)) {
77 errno = ENAMETOOLONG;
78 return (-1);
79 }
80 } else {
81 if (strlen(group) >= sizeof(group_file)) {
82 errno = ENAMETOOLONG;
83 return (-1);
84 }
85 strcpy(group_file, group);
86 }
87
88 initialized = 1;
89 return (0);
90}
91
92/*
93 * Lock the group file
94 */
95int
96gr_lock(void)
97{
98 if (*group_file == '\0')
99 return (-1);
100
101 for (;;) {
102 struct stat st;
103
104 lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0);
105 if (lockfd == -1) {
106 if (errno == EWOULDBLOCK) {
107 errx(1, "the group file is busy");
108 } else {
109 err(1, "could not lock the group file: ");
110 }
111 }
112 if (fstat(lockfd, &st) == -1)
113 err(1, "fstat() failed: ");
114 if (st.st_nlink != 0)
115 break;
116 close(lockfd);
117 lockfd = -1;
118 }
119 return (lockfd);
120}
121
122/*
123 * Create and open a presmuably safe temp file for editing group data
124 */
125int
126gr_tmp(int mfd)
127{
128 char buf[8192];
129 ssize_t nr;
130 const char *p;
131 int tfd;
132
133 if (*group_file == '\0')
134 return (-1);
135 if ((p = strrchr(group_file, '/')))
136 ++p;
137 else
138 p = group_file;
139 if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
140 (int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
141 errno = ENAMETOOLONG;
142 return (-1);
143 }
144 if ((tfd = mkstemp(tempname)) == -1)
145 return (-1);
146 if (mfd != -1) {
147 while ((nr = read(mfd, buf, sizeof(buf))) > 0)
148 if (write(tfd, buf, (size_t)nr) != nr)
149 break;
150 if (nr != 0) {
151 unlink(tempname);
152 *tempname = '\0';
153 close(tfd);
154 return (-1);
155 }
156 }
157 return (tfd);
158}
159
160/*
161 * Copy the group file from one descriptor to another, replacing, deleting
162 * or adding a single record on the way.
163 */
164int
165gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
166{
167 char buf[8192], *end, *line, *p, *q, *r, t;
168 struct group *fgr;
169 const struct group *sgr;
170 size_t len;
171 int eof, readlen;
172
173 if (old_gr == NULL && gr == NULL)
174 return(-1);
175
176 sgr = old_gr;
177 /* deleting a group */
178 if (gr == NULL) {
179 line = NULL;
180 } else {
181 if ((line = gr_make(gr)) == NULL)
182 return (-1);
183 }
184
185 /* adding a group */
186 if (sgr == NULL)
187 sgr = gr;
188
189 eof = 0;
190 len = 0;
191 p = q = end = buf;
192 for (;;) {
193 /* find the end of the current line */
194 for (p = q; q < end && *q != '\0'; ++q)
195 if (*q == '\n')
196 break;
197
198 /* if we don't have a complete line, fill up the buffer */
199 if (q >= end) {
200 if (eof)
201 break;
202 if ((size_t)(q - p) >= sizeof(buf)) {
203 warnx("group line too long");
204 errno = EINVAL; /* hack */
205 goto err;
206 }
207 if (p < end) {
208 q = memmove(buf, p, end -p);
209 end -= p - buf;
210 } else {
211 p = q = end = buf;
212 }
213 readlen = read(ffd, end, sizeof(buf) - (end -buf));
214 if (readlen == -1)
215 goto err;
216 else
217 len = (size_t)readlen;
218 if (len == 0 && p == buf)
219 break;
220 end += len;
221 len = end - buf;
222 if (len < (ssize_t)sizeof(buf)) {
223 eof = 1;
224 if (len > 0 && buf[len -1] != '\n')
225 ++len, *end++ = '\n';
226 }
227 continue;
228 }
229
230 /* is it a blank line or a comment? */
231 for (r = p; r < q && isspace(*r); ++r)
232 /* nothing */;
233 if (r == q || *r == '#') {
234 /* yep */
235 if (write(tfd, p, q -p + 1) != q - p + 1)
236 goto err;
237 ++q;
238 continue;
239 }
240
241 /* is it the one we're looking for? */
242
243 t = *q;
244 *q = '\0';
245
246 fgr = gr_scan(r);
247
248 /* fgr is either a struct group for the current line,
249 * or NULL if the line is malformed.
250 */
251
252 *q = t;
253 if (fgr == NULL || fgr->gr_gid != sgr->gr_gid) {
254 /* nope */
255 if (fgr != NULL)
256 free(fgr);
257 if (write(tfd, p, q - p + 1) != q - p + 1)
258 goto err;
259 ++q;
260 continue;
261 }
262 if (old_gr && !gr_equal(fgr, old_gr)) {
263 warnx("entry inconsistent");
264 free(fgr);
265 errno = EINVAL; /* hack */
266 goto err;
267 }
268 free(fgr);
269
270 /* it is, replace or remove it */
271 if (line != NULL) {
272 len = strlen(line);
273 if (write(tfd, line, len) != (int) len)
274 goto err;
275 } else {
276 /* when removed, avoid the \n */
277 q++;
278 }
279 /* we're done, just copy the rest over */
280 for (;;) {
281 if (write(tfd, q, end - q) != end - q)
282 goto err;
283 q = buf;
284 readlen = read(ffd, buf, sizeof(buf));
285 if (readlen == 0)
286 break;
287 else
288 len = (size_t)readlen;
289 if (readlen == -1)
290 goto err;
291 end = buf + len;
292 }
293 goto done;
294 }
295
296 /* if we got here, we didn't find the old entry */
297 if (line == NULL) {
298 errno = ENOENT;
299 goto err;
300 }
301 len = strlen(line);
302 if ((size_t)write(tfd, line, len) != len ||
303 write(tfd, "\n", 1) != 1)
304 goto err;
305 done:
306 if (line != NULL)
307 free(line);
308 return (0);
309 err:
310 if (line != NULL)
311 free(line);
312 return (-1);
313}
314
315/*
316 * Regenerate the group file
317 */
318int
319gr_mkdb(void)
320{
321 if (chmod(tempname, 0644) != 0)
322 return (-1);
323
324 return (rename(tempname, group_file));
325}
326
327/*
328 * Clean up. Preserves errno for the caller's convenience.
329 */
330void
331gr_fini(void)
332{
333 int serrno;
334
335 if (!initialized)
336 return;
337 initialized = 0;
338 serrno = errno;
339 if (*tempname != '\0') {
340 unlink(tempname);
341 *tempname = '\0';
342 }
343 if (lockfd != -1)
344 close(lockfd);
345 errno = serrno;
346}
347
348/*
349 * Compares two struct group's.
350 */
351int
352gr_equal(const struct group *gr1, const struct group *gr2)
353{
354 int gr1_ndx;
355 int gr2_ndx;
356
357 /* Check that the non-member information is the same. */
358 if (gr1->gr_name == NULL || gr2->gr_name == NULL) {
359 if (gr1->gr_name != gr2->gr_name)
360 return (false);
361 } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0)
362 return (false);
363 if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) {
364 if (gr1->gr_passwd != gr2->gr_passwd)
365 return (false);
366 } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0)
367 return (false);
368 if (gr1->gr_gid != gr2->gr_gid)
369 return (false);
370
371 /* Check all members in both groups.
372 * getgrnam can return gr_mem with a pointer to NULL.
373 * gr_dup and gr_add strip out this superfluous NULL, setting
374 * gr_mem to NULL for no members.
375 */
376 if (gr1->gr_mem != NULL && gr2->gr_mem != NULL) {
377 int i;
378
379 for (i = 0; gr1->gr_mem[i] != NULL; i++) {
380 if (strcmp(gr1->gr_mem[i], gr2->gr_mem[i]) != 0)
381 return (false);
382 }
383 }
384 /* Count number of members in both structs */
385 gr2_ndx = 0;
386 if (gr2->gr_mem != NULL)
387 for(; gr2->gr_mem[gr2_ndx] != NULL; gr2_ndx++)
388 /* empty */;
389 gr1_ndx = 0;
390 if (gr1->gr_mem != NULL)
391 for(; gr1->gr_mem[gr1_ndx] != NULL; gr1_ndx++)
392 /* empty */;
393 if (gr1_ndx != gr2_ndx)
394 return (false);
395
396 return (true);
397}
398
399/*
400 * Make a group line out of a struct group.
401 */
402char *
403gr_make(const struct group *gr)
404{
405 const char *group_line_format = "%s:%s:%ju:";
406 const char *sep;
407 char *line;
408 char *p;
409 size_t line_size;
410 int ndx;
411
412 /* Calculate the length of the group line. */
413 line_size = snprintf(NULL, 0, group_line_format, gr->gr_name,
414 gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1;
415 if (gr->gr_mem != NULL) {
416 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++)
417 line_size += strlen(gr->gr_mem[ndx]) + 1;
418 if (ndx > 0)
419 line_size--;
420 }
421
422 /* Create the group line and fill it. */
423 if ((line = p = malloc(line_size)) == NULL)
424 return (NULL);
425 p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd,
426 (uintmax_t)gr->gr_gid);
427 if (gr->gr_mem != NULL) {
428 sep = "";
429 for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) {
430 p = stpcpy(p, sep);
431 p = stpcpy(p, gr->gr_mem[ndx]);
432 sep = ",";
433 }
434 }
435
436 return (line);
437}
438
439/*
440 * Duplicate a struct group.
441 */
442struct group *
443gr_dup(const struct group *gr)
444{
445 return (gr_add(gr, NULL));
446}
447/*
448 * Add a new member name to a struct group.
449 */
450struct group *
451gr_add(const struct group *gr, const char *newmember)
452{
453 char *mem;
454 size_t len;
455 int num_mem;
456
457 num_mem = 0;
458 len = grmemlen(gr, newmember, &num_mem);
459 /* Create new group and copy old group into it. */
460 if ((mem = malloc(len)) == NULL)
461 return (NULL);
462 return (grcopy(gr, mem, newmember, num_mem));
463}
464
465/* It is safer to walk the pointers given at gr_mem since there is no
466 * guarantee the gr_mem + strings are contiguous in the given struct group
467 * but compactify the new group into the following form.
468 *
469 * The new struct is laid out like this in memory. The example given is
470 * for a group with two members only.
471 *
472 * {
473 * (char *name)
474 * (char *passwd)
475 * (int gid)
476 * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area
477 * gr_mem area
478 * (member1 *)
479 * (member2 *)
480 * (NULL)
481 * (name string)
482 * (passwd string)
483 * (member1 string)
484 * (member2 string)
485 * }
486 */
487/*
488 * Copy the contents of a group plus given name to a preallocated group struct
489 */
490static struct group *
491grcopy(const struct group *gr, char *dst, const char *name, int ndx)
492{
493 int i;
494 struct group *newgr;
495
496 newgr = (struct group *)(void *)dst; /* avoid alignment warning */
497 dst += sizeof(*newgr);
498 if (ndx != 0) {
499 newgr->gr_mem = (char **)(void *)(dst); /* avoid alignment warning */
500 dst += (ndx + 1) * sizeof(*newgr->gr_mem);
501 } else
502 newgr->gr_mem = NULL;
503 if (gr->gr_name != NULL) {
504 newgr->gr_name = dst;
505 dst = stpcpy(dst, gr->gr_name) + 1;
506 } else
507 newgr->gr_name = NULL;
508 if (gr->gr_passwd != NULL) {
509 newgr->gr_passwd = dst;
510 dst = stpcpy(dst, gr->gr_passwd) + 1;
511 } else
512 newgr->gr_passwd = NULL;
513 newgr->gr_gid = gr->gr_gid;
514 i = 0;
515 /* Original group struct might have a NULL gr_mem */
516 if (gr->gr_mem != NULL) {
517 for (; gr->gr_mem[i] != NULL; i++) {
518 newgr->gr_mem[i] = dst;
519 dst = stpcpy(dst, gr->gr_mem[i]) + 1;
520 }
521 }
522 /* If name is not NULL, newgr->gr_mem is known to be not NULL */
523 if (name != NULL) {
524 newgr->gr_mem[i++] = dst;
525 dst = stpcpy(dst, name) + 1;
526 }
527 /* if newgr->gr_mem is not NULL add NULL marker */
528 if (newgr->gr_mem != NULL)
529 newgr->gr_mem[i] = NULL;
530
531 return (newgr);
532}
533
534/*
535 * Calculate length of a struct group + given name
536 */
537static size_t
538grmemlen(const struct group *gr, const char *name, int *num_mem)
539{
540 size_t len;
541 int i;
542
543 if (gr == NULL)
544 return (0);
545 /* Calculate size of the group. */
546 len = sizeof(*gr);
547 if (gr->gr_name != NULL)
548 len += strlen(gr->gr_name) + 1;
549 if (gr->gr_passwd != NULL)
550 len += strlen(gr->gr_passwd) + 1;
551 i = 0;
552 if (gr->gr_mem != NULL) {
553 for (; gr->gr_mem[i] != NULL; i++) {
554 len += strlen(gr->gr_mem[i]) + 1;
555 len += sizeof(*gr->gr_mem);
556 }
557 }
558 if (name != NULL) {
559 i++;
560 len += strlen(name) + 1;
561 len += sizeof(*gr->gr_mem);
562 }
563 /* Allow for NULL pointer */
564 if (i != 0)
565 len += sizeof(*gr->gr_mem);
566 *num_mem = i;
567 return(len);
568}
569
570/*
571 * Scan a line and place it into a group structure.
572 */
573static bool
574__gr_scan(char *line, struct group *gr)
575{
576 char *loc;
577 int ndx;
578
579 /* Assign non-member information to structure. */
580 gr->gr_name = line;
581 if ((loc = strchr(line, ':')) == NULL)
582 return (false);
583 *loc = '\0';
584 gr->gr_passwd = loc + 1;
585 if (*gr->gr_passwd == ':')
586 *gr->gr_passwd = '\0';
587 else {
588 if ((loc = strchr(loc + 1, ':')) == NULL)
589 return (false);
590 *loc = '\0';
591 }
592 if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1)
593 return (false);
594
595 /* Assign member information to structure. */
596 if ((loc = strchr(loc + 1, ':')) == NULL)
597 return (false);
598 line = loc + 1;
599 gr->gr_mem = NULL;
600 ndx = 0;
601 do {
602 gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) *
603 (ndx + 1));
604 if (gr->gr_mem == NULL)
605 return (false);
606
607 /* Skip locations without members (i.e., empty string). */
608 do {
609 gr->gr_mem[ndx] = strsep(&line, ",");
610 } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0');
611 } while (gr->gr_mem[ndx++] != NULL);
612
613 return (true);
614}
615
616/*
617 * Create a struct group from a line.
618 */
619struct group *
620gr_scan(const char *line)
621{
622 struct group gr;
623 char *line_copy;
624 struct group *new_gr;
625
626 if ((line_copy = strdup(line)) == NULL)
627 return (NULL);
628 if (!__gr_scan(line_copy, &gr)) {
629 free(line_copy);
630 return (NULL);
631 }
632 new_gr = gr_dup(&gr);
633 free(line_copy);
634 if (gr.gr_mem != NULL)
635 free(gr.gr_mem);
636
637 return (new_gr);
638}