]>
git.cameronkatri.com Git - pw-darwin.git/blob - libutil/gr_util.c
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
;
53 static const char group_line_format
[] = "%s:%s:%ju:";
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
= mkstemp(tempname
)) == -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
[8192], *end
, *line
, *p
, *q
, *r
, t
;
169 const struct group
*sgr
;
179 } else if ((line
= gr_make(gr
)) == NULL
)
186 /* find the end of the current line */
187 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
191 /* if we don't have a complete line, fill up the buffer */
195 if ((size_t)(q
- p
) >= sizeof(buf
)) {
196 warnx("group line too long");
197 errno
= EINVAL
; /* hack */
201 q
= memmove(buf
, p
, end
-p
);
206 readlen
= read(ffd
, end
, sizeof(buf
) - (end
-buf
));
210 len
= (size_t)readlen
;
211 if (len
== 0 && p
== buf
)
215 if (len
< (ssize_t
)sizeof(buf
)) {
217 if (len
> 0 && buf
[len
-1] != '\n')
218 ++len
, *end
++ = '\n';
223 /* is it a blank line or a comment? */
224 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
226 if (r
== q
|| *r
== '#') {
228 if (write(tfd
, p
, q
-p
+ 1) != q
- p
+ 1)
234 /* is it the one we're looking for? */
241 /* fgr is either a struct group for the current line,
242 * or NULL if the line is malformed.
246 if (fgr
== NULL
|| fgr
->gr_gid
!= sgr
->gr_gid
) {
250 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
255 if (old_gr
&& !gr_equal(fgr
, old_gr
)) {
256 warnx("entry inconsistent");
258 errno
= EINVAL
; /* hack */
263 /* it is, replace or remove it */
266 if (write(tfd
, line
, len
) != (int) len
)
269 /* when removed, avoid the \n */
272 /* we're done, just copy the rest over */
274 if (write(tfd
, q
, end
- q
) != end
- q
)
277 readlen
= read(ffd
, buf
, sizeof(buf
));
281 len
= (size_t)readlen
;
289 /* if we got here, we didn't find the old entry */
295 if ((size_t)write(tfd
, line
, len
) != len
||
296 write(tfd
, "\n", 1) != 1)
309 * Regenerate the group file
314 if (chmod(tempname
, 0644) != 0)
317 return (rename(tempname
, group_file
));
321 * Clean up. Preserver errno for the caller's convenience.
332 if (*tempname
!= '\0') {
342 * Compares two struct group's.
345 gr_equal(const struct group
*gr1
, const struct group
*gr2
)
351 /* Check that the non-member information is the same. */
352 if (gr1
->gr_name
== NULL
|| gr2
->gr_name
== NULL
) {
353 if (gr1
->gr_name
!= gr2
->gr_name
)
355 } else if (strcmp(gr1
->gr_name
, gr2
->gr_name
) != 0)
357 if (gr1
->gr_passwd
== NULL
|| gr2
->gr_passwd
== NULL
) {
358 if (gr1
->gr_passwd
!= gr2
->gr_passwd
)
360 } else if (strcmp(gr1
->gr_passwd
, gr2
->gr_passwd
) != 0)
362 if (gr1
->gr_gid
!= gr2
->gr_gid
)
365 /* Check all members in both groups. */
366 if (gr1
->gr_mem
== NULL
|| gr2
->gr_mem
== NULL
) {
367 if (gr1
->gr_mem
!= gr2
->gr_mem
)
370 for (found
= false, gr1_ndx
= 0; gr1
->gr_mem
[gr1_ndx
] != NULL
;
372 for (gr2_ndx
= 0; gr2
->gr_mem
[gr2_ndx
] != NULL
;
374 if (strcmp(gr1
->gr_mem
[gr1_ndx
],
375 gr2
->gr_mem
[gr2_ndx
]) == 0) {
383 /* Check that group2 does not have more members than group1. */
384 if (gr2
->gr_mem
[gr1_ndx
] != NULL
)
392 * Make a group line out of a struct group.
395 gr_make(const struct group
*gr
)
401 /* Calculate the length of the group line. */
402 line_size
= snprintf(NULL
, 0, group_line_format
, gr
->gr_name
,
403 gr
->gr_passwd
, (uintmax_t)gr
->gr_gid
) + 1;
404 if (gr
->gr_mem
!= NULL
) {
405 for (ndx
= 0; gr
->gr_mem
[ndx
] != NULL
; ndx
++)
406 line_size
+= strlen(gr
->gr_mem
[ndx
]) + 1;
411 /* Create the group line and fill it. */
412 if ((line
= malloc(line_size
)) == NULL
)
414 snprintf(line
, line_size
, group_line_format
, gr
->gr_name
, gr
->gr_passwd
,
415 (uintmax_t)gr
->gr_gid
);
416 if (gr
->gr_mem
!= NULL
)
417 for (ndx
= 0; gr
->gr_mem
[ndx
] != NULL
; ndx
++) {
418 strcat(line
, gr
->gr_mem
[ndx
]);
419 if (gr
->gr_mem
[ndx
+ 1] != NULL
)
427 * Duplicate a struct group.
430 gr_dup(const struct group
*gr
)
438 /* Calculate size of the group. */
439 len
= sizeof(*newgr
);
440 if (gr
->gr_name
!= NULL
)
441 len
+= strlen(gr
->gr_name
) + 1;
442 if (gr
->gr_passwd
!= NULL
)
443 len
+= strlen(gr
->gr_passwd
) + 1;
444 if (gr
->gr_mem
!= NULL
) {
445 for (num_mem
= 0; gr
->gr_mem
[num_mem
] != NULL
; num_mem
++)
446 len
+= strlen(gr
->gr_mem
[num_mem
]) + 1;
447 len
+= (num_mem
+ 1) * sizeof(*gr
->gr_mem
);
450 /* Create new group and copy old group into it. */
451 if ((newgr
= malloc(len
)) == NULL
)
453 /* point new gr_mem to end of struct + 1 */
454 if (gr
->gr_mem
!= NULL
)
455 newgr
->gr_mem
= (char **)(newgr
+ 1);
457 newgr
->gr_mem
= NULL
;
458 /* point dst after the end of all the gr_mem pointers in newgr */
459 dst
= (char *)newgr
+ sizeof(struct group
) +
460 (num_mem
+ 1) * sizeof(*gr
->gr_mem
);
461 if (gr
->gr_name
!= NULL
) {
462 newgr
->gr_name
= dst
;
463 dst
= stpcpy(dst
, gr
->gr_name
) + 1;
465 newgr
->gr_name
= NULL
;
467 if (gr
->gr_passwd
!= NULL
) {
468 newgr
->gr_passwd
= dst
;
469 dst
= stpcpy(dst
, gr
->gr_passwd
) + 1;
471 newgr
->gr_passwd
= NULL
;
473 newgr
->gr_gid
= gr
->gr_gid
;
474 if (gr
->gr_mem
!= NULL
) {
475 for (ndx
= 0; ndx
< num_mem
; ndx
++) {
476 newgr
->gr_mem
[ndx
] = dst
;
477 dst
= stpcpy(dst
, gr
->gr_mem
[ndx
]) + 1;
479 newgr
->gr_mem
[ndx
] = NULL
;
485 * Add a new member name to a struct group.
488 gr_add(struct group
*gr
, char *newmember
)
495 if (newmember
== NULL
)
498 if (gr
->gr_mem
!= NULL
) {
499 for (num_mem
= 0; gr
->gr_mem
[num_mem
] != NULL
; num_mem
++) {
500 if (strcmp(gr
->gr_mem
[num_mem
], newmember
) == 0) {
506 /* Allocate enough for current pointers + 1 more and NULL marker */
507 mlen
= (num_mem
+ 2) * sizeof(*gr
->gr_mem
);
508 if ((members
= malloc(mlen
)) == NULL
) {
512 memcpy(members
, gr
->gr_mem
, num_mem
* sizeof(*gr
->gr_mem
));
513 members
[num_mem
++] = newmember
;
514 members
[num_mem
] = NULL
;
515 gr
->gr_mem
= members
;
524 * Scan a line and place it into a group structure.
527 __gr_scan(char *line
, struct group
*gr
)
532 /* Assign non-member information to structure. */
534 if ((loc
= strchr(line
, ':')) == NULL
)
537 gr
->gr_passwd
= loc
+ 1;
538 if (*gr
->gr_passwd
== ':')
539 *gr
->gr_passwd
= '\0';
541 if ((loc
= strchr(loc
+ 1, ':')) == NULL
)
545 if (sscanf(loc
+ 1, "%u", &gr
->gr_gid
) != 1)
548 /* Assign member information to structure. */
549 if ((loc
= strchr(loc
+ 1, ':')) == NULL
)
555 gr
->gr_mem
= reallocf(gr
->gr_mem
, sizeof(*gr
->gr_mem
) *
557 if (gr
->gr_mem
== NULL
)
560 /* Skip locations without members (i.e., empty string). */
562 gr
->gr_mem
[ndx
] = strsep(&line
, ",");
563 } while (gr
->gr_mem
[ndx
] != NULL
&& *gr
->gr_mem
[ndx
] == '\0');
564 } while (gr
->gr_mem
[ndx
++] != NULL
);
570 * Create a struct group from a line.
573 gr_scan(const char *line
)
577 struct group
*new_gr
;
579 if ((line_copy
= strdup(line
)) == NULL
)
581 if (!__gr_scan(line_copy
, &gr
)) {
585 new_gr
= gr_dup(&gr
);
587 if (gr
.gr_mem
!= NULL
)