]>
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 struct group_storage
{
52 static int lockfd
= -1;
53 static char group_dir
[PATH_MAX
];
54 static char group_file
[PATH_MAX
];
55 static char tempname
[PATH_MAX
];
56 static int initialized
;
58 static const char group_line_format
[] = "%s:%s:%ju:";
64 gr_init(const char *dir
, const char *group
)
68 strcpy(group_dir
, _PATH_ETC
);
70 if (strlen(dir
) >= sizeof(group_dir
)) {
74 strcpy(group_dir
, dir
);
79 strcpy(group_file
, _PATH_GROUP
);
80 } else if (snprintf(group_file
, sizeof(group_file
), "%s/group",
81 group_dir
) > (int)sizeof(group_file
)) {
86 if (strlen(group
) >= sizeof(group_file
)) {
90 strcpy(group_file
, group
);
103 if (*group_file
== '\0')
109 lockfd
= flopen(group_file
, O_RDONLY
|O_NONBLOCK
, 0);
111 if (errno
== EWOULDBLOCK
) {
112 errx(1, "the group file is busy");
114 err(1, "could not lock the group file: ");
117 if (fstat(lockfd
, &st
) == -1)
118 err(1, "fstat() failed: ");
119 if (st
.st_nlink
!= 0)
128 * Create and open a presmuably safe temp file for editing group data
138 if (*group_file
== '\0')
140 if ((p
= strrchr(group_file
, '/')))
144 if (snprintf(tempname
, sizeof(tempname
), "%.*sgroup.XXXXXX",
145 (int)(p
- group_file
), group_file
) >= (int)sizeof(tempname
)) {
146 errno
= ENAMETOOLONG
;
149 if ((tfd
= mkstemp(tempname
)) == -1)
152 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
153 if (write(tfd
, buf
, (size_t)nr
) != nr
)
166 * Copy the group file from one descriptor to another, replacing, deleting
167 * or adding a single record on the way.
170 gr_copy(int ffd
, int tfd
, const struct group
*gr
, struct group
*old_gr
)
172 char buf
[8192], *end
, *line
, *p
, *q
, *r
, t
;
174 const struct group
*sgr
;
184 } else if ((line
= gr_make(gr
)) == NULL
)
191 /* find the end of the current line */
192 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
196 /* if we don't have a complete line, fill up the buffer */
200 if ((size_t)(q
- p
) >= sizeof(buf
)) {
201 warnx("group line too long");
202 errno
= EINVAL
; /* hack */
206 q
= memmove(buf
, p
, end
-p
);
211 readlen
= read(ffd
, end
, sizeof(buf
) - (end
-buf
));
215 len
= (size_t)readlen
;
216 if (len
== 0 && p
== buf
)
220 if (len
< (ssize_t
)sizeof(buf
)) {
222 if (len
> 0 && buf
[len
-1] != '\n')
223 ++len
, *end
++ = '\n';
228 /* is it a blank line or a comment? */
229 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
231 if (r
== q
|| *r
== '#') {
233 if (write(tfd
, p
, q
-p
+ 1) != q
- p
+ 1)
239 /* is it the one we're looking for? */
246 /* fgr is either a struct group for the current line,
247 * or NULL if the line is malformed.
251 if (fgr
== NULL
|| fgr
->gr_gid
!= sgr
->gr_gid
) {
255 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
260 if (old_gr
&& !gr_equal(fgr
, old_gr
)) {
261 warnx("entry inconsistent");
263 errno
= EINVAL
; /* hack */
268 /* it is, replace or remove it */
271 if (write(tfd
, line
, len
) != (int) len
)
274 /* when removed, avoid the \n */
277 /* we're done, just copy the rest over */
279 if (write(tfd
, q
, end
- q
) != end
- q
)
282 readlen
= read(ffd
, buf
, sizeof(buf
));
286 len
= (size_t)readlen
;
294 /* if we got here, we didn't find the old entry */
300 if ((size_t)write(tfd
, line
, len
) != len
||
301 write(tfd
, "\n", 1) != 1)
314 * Regenerate the group file
319 if (chmod(tempname
, 0644) != 0)
322 return (rename(tempname
, group_file
));
326 * Clean up. Preserver errno for the caller's convenience.
337 if (*tempname
!= '\0') {
347 * Compares two struct group's.
350 gr_equal(const struct group
*gr1
, const struct group
*gr2
)
356 /* Check that the non-member information is the same. */
357 if (gr1
->gr_name
== NULL
|| gr2
->gr_name
== NULL
) {
358 if (gr1
->gr_name
!= gr2
->gr_name
)
360 } else if (strcmp(gr1
->gr_name
, gr2
->gr_name
) != 0)
362 if (gr1
->gr_passwd
== NULL
|| gr2
->gr_passwd
== NULL
) {
363 if (gr1
->gr_passwd
!= gr2
->gr_passwd
)
365 } else if (strcmp(gr1
->gr_passwd
, gr2
->gr_passwd
) != 0)
367 if (gr1
->gr_gid
!= gr2
->gr_gid
)
370 /* Check all members in both groups. */
371 if (gr1
->gr_mem
== NULL
|| gr2
->gr_mem
== NULL
) {
372 if (gr1
->gr_mem
!= gr2
->gr_mem
)
375 for (found
= false, gr1_ndx
= 0; gr1
->gr_mem
[gr1_ndx
] != NULL
;
377 for (gr2_ndx
= 0; gr2
->gr_mem
[gr2_ndx
] != NULL
;
379 if (strcmp(gr1
->gr_mem
[gr1_ndx
],
380 gr2
->gr_mem
[gr2_ndx
]) == 0) {
388 /* Check that group2 does not have more members than group1. */
389 if (gr2
->gr_mem
[gr1_ndx
] != NULL
)
397 * Make a group line out of a struct group.
400 gr_make(const struct group
*gr
)
406 /* Calculate the length of the group line. */
407 line_size
= snprintf(NULL
, 0, group_line_format
, gr
->gr_name
,
408 gr
->gr_passwd
, (uintmax_t)gr
->gr_gid
) + 1;
409 if (gr
->gr_mem
!= NULL
) {
410 for (ndx
= 0; gr
->gr_mem
[ndx
] != NULL
; ndx
++)
411 line_size
+= strlen(gr
->gr_mem
[ndx
]) + 1;
416 /* Create the group line and fill it. */
417 if ((line
= malloc(line_size
)) == NULL
)
419 snprintf(line
, line_size
, group_line_format
, gr
->gr_name
, gr
->gr_passwd
,
420 (uintmax_t)gr
->gr_gid
);
421 if (gr
->gr_mem
!= NULL
)
422 for (ndx
= 0; gr
->gr_mem
[ndx
] != NULL
; ndx
++) {
423 strcat(line
, gr
->gr_mem
[ndx
]);
424 if (gr
->gr_mem
[ndx
+ 1] != NULL
)
432 * Duplicate a struct group.
435 gr_dup(const struct group
*gr
)
439 struct group_storage
*gs
;
443 /* Calculate size of the group. */
445 if (gr
->gr_name
!= NULL
)
446 len
+= strlen(gr
->gr_name
) + 1;
447 if (gr
->gr_passwd
!= NULL
)
448 len
+= strlen(gr
->gr_passwd
) + 1;
449 if (gr
->gr_mem
!= NULL
) {
450 for (num_mem
= 0; gr
->gr_mem
[num_mem
] != NULL
; num_mem
++)
451 len
+= strlen(gr
->gr_mem
[num_mem
]) + 1;
452 len
+= (num_mem
+ 1) * sizeof(*gr
->gr_mem
);
456 /* Create new group and copy old group into it. */
457 if ((gs
= calloc(1, len
)) == NULL
)
459 dst
= (char *)&gs
->members
[num_mem
+ 1];
460 if (gr
->gr_name
!= NULL
) {
461 gs
->gr
.gr_name
= dst
;
462 dst
= stpcpy(gs
->gr
.gr_name
, gr
->gr_name
) + 1;
464 if (gr
->gr_passwd
!= NULL
) {
465 gs
->gr
.gr_passwd
= dst
;
466 dst
= stpcpy(gs
->gr
.gr_passwd
, gr
->gr_passwd
) + 1;
468 gs
->gr
.gr_gid
= gr
->gr_gid
;
469 if (gr
->gr_mem
!= NULL
) {
470 gs
->gr
.gr_mem
= gs
->members
;
471 for (ndx
= 0; ndx
< num_mem
; ndx
++) {
472 gs
->gr
.gr_mem
[ndx
] = dst
;
473 dst
= stpcpy(gs
->gr
.gr_mem
[ndx
], gr
->gr_mem
[ndx
]) + 1;
475 gs
->gr
.gr_mem
[ndx
] = NULL
;
482 * Scan a line and place it into a group structure.
485 __gr_scan(char *line
, struct group
*gr
)
490 /* Assign non-member information to structure. */
492 if ((loc
= strchr(line
, ':')) == NULL
)
495 gr
->gr_passwd
= loc
+ 1;
496 if (*gr
->gr_passwd
== ':')
497 *gr
->gr_passwd
= '\0';
499 if ((loc
= strchr(loc
+ 1, ':')) == NULL
)
503 if (sscanf(loc
+ 1, "%u", &gr
->gr_gid
) != 1)
506 /* Assign member information to structure. */
507 if ((loc
= strchr(loc
+ 1, ':')) == NULL
)
513 gr
->gr_mem
= reallocf(gr
->gr_mem
, sizeof(*gr
->gr_mem
) *
515 if (gr
->gr_mem
== NULL
)
518 /* Skip locations without members (i.e., empty string). */
520 gr
->gr_mem
[ndx
] = strsep(&line
, ",");
521 } while (gr
->gr_mem
[ndx
] != NULL
&& *gr
->gr_mem
[ndx
] == '\0');
522 } while (gr
->gr_mem
[ndx
++] != NULL
);
528 * Create a struct group from a line.
531 gr_scan(const char *line
)
535 struct group
*new_gr
;
537 if ((line_copy
= strdup(line
)) == NULL
)
539 if (!__gr_scan(line_copy
, &gr
)) {
543 new_gr
= gr_dup(&gr
);
545 if (gr
.gr_mem
!= NULL
)