]>
git.cameronkatri.com Git - pw-darwin.git/blob - libutil/gr_util.c
99f268cf1833a6fc2a79fdb33232cbbdcb62d5c9
2 * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
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 * 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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/errno.h>
47 static int lockfd
= -1;
48 static char group_dir
[PATH_MAX
];
49 static char group_file
[PATH_MAX
];
50 static char tempname
[PATH_MAX
];
51 static int initialized
;
52 static size_t grmemlen(const struct group
*, const char *, int *);
53 static struct group
*grcopy(const struct group
*gr
, char *mem
, const char *, int ndx
);
59 gr_init(const char *dir
, const char *group
)
63 strcpy(group_dir
, _PATH_ETC
);
65 if (strlen(dir
) >= sizeof(group_dir
)) {
69 strcpy(group_dir
, dir
);
74 strcpy(group_file
, _PATH_GROUP
);
75 } else if (snprintf(group_file
, sizeof(group_file
), "%s/group",
76 group_dir
) > (int)sizeof(group_file
)) {
81 if (strlen(group
) >= sizeof(group_file
)) {
85 strcpy(group_file
, group
);
98 if (*group_file
== '\0')
104 lockfd
= flopen(group_file
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
, 0);
106 if (errno
== EWOULDBLOCK
) {
107 errx(1, "the group file is busy");
109 err(1, "could not lock the group file: ");
112 if (fstat(lockfd
, &st
) == -1)
113 err(1, "fstat() failed: ");
114 if (st
.st_nlink
!= 0)
123 * Create and open a presmuably safe temp file for editing group data
133 if (*group_file
== '\0')
135 if ((p
= strrchr(group_file
, '/')))
139 if (snprintf(tempname
, sizeof(tempname
), "%.*sgroup.XXXXXX",
140 (int)(p
- group_file
), group_file
) >= (int)sizeof(tempname
)) {
141 errno
= ENAMETOOLONG
;
144 if ((tfd
= mkostemp(tempname
, 0)) == -1)
147 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
148 if (write(tfd
, buf
, (size_t)nr
) != nr
)
161 * Copy the group file from one descriptor to another, replacing, deleting
162 * or adding a single record on the way.
165 gr_copy(int ffd
, int tfd
, const struct group
*gr
, struct group
*old_gr
)
167 char *buf
, *end
, *line
, *p
, *q
, *r
, *tmp
;
169 const struct group
*sgr
;
174 if (old_gr
== NULL
&& gr
== NULL
)
178 /* deleting a group */
182 if ((line
= gr_make(gr
)) == NULL
)
190 /* initialize the buffer */
191 if ((buf
= malloc(size
= 1024)) == NULL
)
198 /* find the end of the current line */
199 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
203 /* if we don't have a complete line, fill up the buffer */
207 while ((size_t)(q
- p
) >= size
) {
208 if ((tmp
= realloc(buf
, size
* 2)) == NULL
) {
209 warnx("group line too long");
214 end
= tmp
+ (end
- buf
);
219 q
= memmove(buf
, p
, end
-p
);
224 readlen
= read(ffd
, end
, size
- (end
- buf
));
228 len
= (size_t)readlen
;
229 if (len
== 0 && p
== buf
)
235 if (len
> 0 && buf
[len
-1] != '\n')
236 ++len
, *end
++ = '\n';
241 /* is it a blank line or a comment? */
242 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
244 if (r
== q
|| *r
== '#') {
246 if (write(tfd
, p
, q
-p
+ 1) != q
- p
+ 1)
252 /* is it the one we're looking for? */
259 /* fgr is either a struct group for the current line,
260 * or NULL if the line is malformed.
264 if (fgr
== NULL
|| fgr
->gr_gid
!= sgr
->gr_gid
) {
268 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
273 if (old_gr
&& !gr_equal(fgr
, old_gr
)) {
274 warnx("entry inconsistent");
276 errno
= EINVAL
; /* hack */
281 /* it is, replace or remove it */
284 if (write(tfd
, line
, len
) != (int) len
)
287 /* when removed, avoid the \n */
290 /* we're done, just copy the rest over */
292 if (write(tfd
, q
, end
- q
) != end
- q
)
295 readlen
= read(ffd
, buf
, size
);
299 len
= (size_t)readlen
;
307 /* if we got here, we didn't find the old entry */
313 if ((size_t)write(tfd
, line
, len
) != len
||
314 write(tfd
, "\n", 1) != 1)
327 * Regenerate the group file
334 if (chmod(tempname
, 0644) != 0)
337 if (rename(tempname
, group_file
) != 0)
341 * Make sure new group file is safe on disk. To improve performance we
342 * will call fsync() to the directory where file lies
344 if ((fd
= open(group_dir
, O_RDONLY
|O_DIRECTORY
)) == -1)
347 if (fsync(fd
) != 0) {
357 * Clean up. Preserves errno for the caller's convenience.
368 if (*tempname
!= '\0') {
378 * Compares two struct group's.
381 gr_equal(const struct group
*gr1
, const struct group
*gr2
)
384 /* Check that the non-member information is the same. */
385 if (gr1
->gr_name
== NULL
|| gr2
->gr_name
== NULL
) {
386 if (gr1
->gr_name
!= gr2
->gr_name
)
388 } else if (strcmp(gr1
->gr_name
, gr2
->gr_name
) != 0)
390 if (gr1
->gr_passwd
== NULL
|| gr2
->gr_passwd
== NULL
) {
391 if (gr1
->gr_passwd
!= gr2
->gr_passwd
)
393 } else if (strcmp(gr1
->gr_passwd
, gr2
->gr_passwd
) != 0)
395 if (gr1
->gr_gid
!= gr2
->gr_gid
)
399 * Check all members in both groups.
400 * getgrnam can return gr_mem with a pointer to NULL.
401 * gr_dup and gr_add strip out this superfluous NULL, setting
402 * gr_mem to NULL for no members.
404 if (gr1
->gr_mem
!= NULL
&& gr2
->gr_mem
!= NULL
) {
408 gr1
->gr_mem
[i
] != NULL
&& gr2
->gr_mem
[i
] != NULL
; i
++) {
409 if (strcmp(gr1
->gr_mem
[i
], gr2
->gr_mem
[i
]) != 0)
412 if (gr1
->gr_mem
[i
] != NULL
|| gr2
->gr_mem
[i
] != NULL
)
414 } else if (gr1
->gr_mem
!= NULL
&& gr1
->gr_mem
[0] != NULL
) {
416 } else if (gr2
->gr_mem
!= NULL
&& gr2
->gr_mem
[0] != NULL
) {
424 * Make a group line out of a struct group.
427 gr_make(const struct group
*gr
)
429 const char *group_line_format
= "%s:%s:%ju:";
436 /* Calculate the length of the group line. */
437 line_size
= snprintf(NULL
, 0, group_line_format
, gr
->gr_name
,
438 gr
->gr_passwd
, (uintmax_t)gr
->gr_gid
) + 1;
439 if (gr
->gr_mem
!= NULL
) {
440 for (ndx
= 0; gr
->gr_mem
[ndx
] != NULL
; ndx
++)
441 line_size
+= strlen(gr
->gr_mem
[ndx
]) + 1;
446 /* Create the group line and fill it. */
447 if ((line
= p
= malloc(line_size
)) == NULL
)
449 p
+= sprintf(p
, group_line_format
, gr
->gr_name
, gr
->gr_passwd
,
450 (uintmax_t)gr
->gr_gid
);
451 if (gr
->gr_mem
!= NULL
) {
453 for (ndx
= 0; gr
->gr_mem
[ndx
] != NULL
; ndx
++) {
455 p
= stpcpy(p
, gr
->gr_mem
[ndx
]);
464 * Duplicate a struct group.
467 gr_dup(const struct group
*gr
)
469 return (gr_add(gr
, NULL
));
472 * Add a new member name to a struct group.
475 gr_add(const struct group
*gr
, const char *newmember
)
482 len
= grmemlen(gr
, newmember
, &num_mem
);
483 /* Create new group and copy old group into it. */
484 if ((mem
= malloc(len
)) == NULL
)
486 return (grcopy(gr
, mem
, newmember
, num_mem
));
489 /* It is safer to walk the pointers given at gr_mem since there is no
490 * guarantee the gr_mem + strings are contiguous in the given struct group
491 * but compactify the new group into the following form.
493 * The new struct is laid out like this in memory. The example given is
494 * for a group with two members only.
500 * (gr_mem * newgrp + sizeof(struct group) + sizeof(**)) points to gr_mem area
512 * Copy the contents of a group plus given name to a preallocated group struct
514 static struct group
*
515 grcopy(const struct group
*gr
, char *dst
, const char *name
, int ndx
)
520 newgr
= (struct group
*)(void *)dst
; /* avoid alignment warning */
521 dst
+= sizeof(*newgr
);
523 newgr
->gr_mem
= (char **)(void *)(dst
); /* avoid alignment warning */
524 dst
+= (ndx
+ 1) * sizeof(*newgr
->gr_mem
);
526 newgr
->gr_mem
= NULL
;
527 if (gr
->gr_name
!= NULL
) {
528 newgr
->gr_name
= dst
;
529 dst
= stpcpy(dst
, gr
->gr_name
) + 1;
531 newgr
->gr_name
= NULL
;
532 if (gr
->gr_passwd
!= NULL
) {
533 newgr
->gr_passwd
= dst
;
534 dst
= stpcpy(dst
, gr
->gr_passwd
) + 1;
536 newgr
->gr_passwd
= NULL
;
537 newgr
->gr_gid
= gr
->gr_gid
;
539 /* Original group struct might have a NULL gr_mem */
540 if (gr
->gr_mem
!= NULL
) {
541 for (; gr
->gr_mem
[i
] != NULL
; i
++) {
542 newgr
->gr_mem
[i
] = dst
;
543 dst
= stpcpy(dst
, gr
->gr_mem
[i
]) + 1;
546 /* If name is not NULL, newgr->gr_mem is known to be not NULL */
548 newgr
->gr_mem
[i
++] = dst
;
549 dst
= stpcpy(dst
, name
) + 1;
551 /* if newgr->gr_mem is not NULL add NULL marker */
552 if (newgr
->gr_mem
!= NULL
)
553 newgr
->gr_mem
[i
] = NULL
;
559 * Calculate length of a struct group + given name
562 grmemlen(const struct group
*gr
, const char *name
, int *num_mem
)
569 /* Calculate size of the group. */
571 if (gr
->gr_name
!= NULL
)
572 len
+= strlen(gr
->gr_name
) + 1;
573 if (gr
->gr_passwd
!= NULL
)
574 len
+= strlen(gr
->gr_passwd
) + 1;
576 if (gr
->gr_mem
!= NULL
) {
577 for (; gr
->gr_mem
[i
] != NULL
; i
++) {
578 len
+= strlen(gr
->gr_mem
[i
]) + 1;
579 len
+= sizeof(*gr
->gr_mem
);
584 len
+= strlen(name
) + 1;
585 len
+= sizeof(*gr
->gr_mem
);
587 /* Allow for NULL pointer */
589 len
+= sizeof(*gr
->gr_mem
);
595 * Scan a line and place it into a group structure.
598 __gr_scan(char *line
, struct group
*gr
)
603 /* Assign non-member information to structure. */
605 if ((loc
= strchr(line
, ':')) == NULL
)
608 gr
->gr_passwd
= loc
+ 1;
609 if (*gr
->gr_passwd
== ':')
610 *gr
->gr_passwd
= '\0';
612 if ((loc
= strchr(loc
+ 1, ':')) == NULL
)
616 if (sscanf(loc
+ 1, "%u", &gr
->gr_gid
) != 1)
619 /* Assign member information to structure. */
620 if ((loc
= strchr(loc
+ 1, ':')) == NULL
)
626 gr
->gr_mem
= reallocf(gr
->gr_mem
, sizeof(*gr
->gr_mem
) *
628 if (gr
->gr_mem
== NULL
)
631 /* Skip locations without members (i.e., empty string). */
633 gr
->gr_mem
[ndx
] = strsep(&line
, ",");
634 } while (gr
->gr_mem
[ndx
] != NULL
&& *gr
->gr_mem
[ndx
] == '\0');
635 } while (gr
->gr_mem
[ndx
++] != NULL
);
641 * Create a struct group from a line.
644 gr_scan(const char *line
)
648 struct group
*new_gr
;
650 if ((line_copy
= strdup(line
)) == NULL
)
652 if (!__gr_scan(line_copy
, &gr
)) {
656 new_gr
= gr_dup(&gr
);
658 if (gr
.gr_mem
!= NULL
)