]>
git.cameronkatri.com Git - pw-darwin.git/blob - libutil/gr_util.c
70a4c03a5609702835d3c0755010f3fb3943c6af
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
;
57 gr_init(const char *dir
, const char *group
)
61 strcpy(group_dir
, _PATH_ETC
);
63 if (strlen(dir
) >= sizeof(group_dir
)) {
67 strcpy(group_dir
, dir
);
72 strcpy(group_file
, _PATH_GROUP
);
73 } else if (snprintf(group_file
, sizeof(group_file
), "%s/group",
74 group_dir
) > (int)sizeof(group_file
)) {
79 if (strlen(group
) >= sizeof(group_file
)) {
83 strcpy(group_file
, group
);
96 if (*group_file
== '\0')
102 lockfd
= flopen(group_file
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
, 0);
104 if (errno
== EWOULDBLOCK
) {
105 errx(1, "the group file is busy");
107 err(1, "could not lock the group file: ");
110 if (fstat(lockfd
, &st
) == -1)
111 err(1, "fstat() failed: ");
112 if (st
.st_nlink
!= 0)
121 * Create and open a presmuably safe temp file for editing group data
131 if (*group_file
== '\0')
133 if ((p
= strrchr(group_file
, '/')))
137 if (snprintf(tempname
, sizeof(tempname
), "%.*sgroup.XXXXXX",
138 (int)(p
- group_file
), group_file
) >= (int)sizeof(tempname
)) {
139 errno
= ENAMETOOLONG
;
142 if ((tfd
= mkstemp(tempname
)) == -1)
145 while ((nr
= read(mfd
, buf
, sizeof(buf
))) > 0)
146 if (write(tfd
, buf
, (size_t)nr
) != nr
)
159 * Copy the group file from one descriptor to another, replacing, deleting
160 * or adding a single record on the way.
163 gr_copy(int ffd
, int tfd
, const struct group
*gr
, struct group
*old_gr
)
165 char buf
[8192], *end
, *line
, *p
, *q
, *r
, t
;
167 const struct group
*sgr
;
177 } else if ((line
= gr_make(gr
)) == NULL
)
184 /* find the end of the current line */
185 for (p
= q
; q
< end
&& *q
!= '\0'; ++q
)
189 /* if we don't have a complete line, fill up the buffer */
193 if ((size_t)(q
- p
) >= sizeof(buf
)) {
194 warnx("group line too long");
195 errno
= EINVAL
; /* hack */
199 q
= memmove(buf
, p
, end
-p
);
204 readlen
= read(ffd
, end
, sizeof(buf
) - (end
-buf
));
208 len
= (size_t)readlen
;
209 if (len
== 0 && p
== buf
)
213 if (len
< (ssize_t
)sizeof(buf
)) {
215 if (len
> 0 && buf
[len
-1] != '\n')
216 ++len
, *end
++ = '\n';
221 /* is it a blank line or a comment? */
222 for (r
= p
; r
< q
&& isspace(*r
); ++r
)
224 if (r
== q
|| *r
== '#') {
226 if (write(tfd
, p
, q
-p
+ 1) != q
- p
+ 1)
232 /* is it the one we're looking for? */
239 /* fgr is either a struct group for the current line,
240 * or NULL if the line is malformed.
244 if (fgr
== NULL
|| fgr
->gr_gid
!= sgr
->gr_gid
) {
248 if (write(tfd
, p
, q
- p
+ 1) != q
- p
+ 1)
253 if (old_gr
&& !gr_equal(fgr
, old_gr
)) {
254 warnx("entry inconsistent");
256 errno
= EINVAL
; /* hack */
261 /* it is, replace or remove it */
264 if (write(tfd
, line
, len
) != (int) len
)
267 /* when removed, avoid the \n */
270 /* we're done, just copy the rest over */
272 if (write(tfd
, q
, end
- q
) != end
- q
)
275 readlen
= read(ffd
, buf
, sizeof(buf
));
279 len
= (size_t)readlen
;
287 /* if we got here, we didn't find the old entry */
293 if ((size_t)write(tfd
, line
, len
) != len
||
294 write(tfd
, "\n", 1) != 1)
307 * Regenerate the group file
312 if (chmod(tempname
, 0644) != 0)
315 return (rename(tempname
, group_file
));
319 * Clean up. Preserver errno for the caller's convenience.
330 if (*tempname
!= '\0') {
340 * Compares two struct group's.
343 gr_equal(const struct group
*gr1
, const struct group
*gr2
)
348 /* Check that the non-member information is the same. */
349 if (gr1
->gr_name
== NULL
|| gr2
->gr_name
== NULL
) {
350 if (gr1
->gr_name
!= gr2
->gr_name
)
352 } else if (strcmp(gr1
->gr_name
, gr2
->gr_name
) != 0)
354 if (gr1
->gr_passwd
== NULL
|| gr2
->gr_passwd
== NULL
) {
355 if (gr1
->gr_passwd
!= gr2
->gr_passwd
)
357 } else if (strcmp(gr1
->gr_passwd
, gr2
->gr_passwd
) != 0)
359 if (gr1
->gr_gid
!= gr2
->gr_gid
)
362 /* Check all members in both groups. */
363 if (gr1
->gr_mem
== NULL
|| gr2
->gr_mem
== NULL
) {
364 if (gr1
->gr_mem
!= gr2
->gr_mem
)
367 for (gr1_ndx
= 0; gr1
->gr_mem
[gr1_ndx
] != NULL
; gr1_ndx
++) {
368 for (gr2_ndx
= 0;; gr2_ndx
++) {
369 if (gr2
->gr_mem
[gr2_ndx
] == NULL
)
371 if (strcmp(gr1
->gr_mem
[gr1_ndx
],
372 gr2
->gr_mem
[gr2_ndx
]) == 0) {
378 /* Check that group2 does not have more members than group1. */
379 if (gr2
->gr_mem
[gr1_ndx
] != NULL
)
387 * Make a group line out of a struct group.
390 gr_make(const struct group
*gr
)
392 const char *group_line_format
= "%s:%s:%ju:";
397 /* Calculate the length of the group line. */
398 line_size
= snprintf(NULL
, 0, group_line_format
, gr
->gr_name
,
399 gr
->gr_passwd
, (uintmax_t)gr
->gr_gid
) + 1;
400 if (gr
->gr_mem
!= NULL
) {
401 for (ndx
= 0; gr
->gr_mem
[ndx
] != NULL
; ndx
++)
402 line_size
+= strlen(gr
->gr_mem
[ndx
]) + 1;
407 /* Create the group line and fill it. */
408 if ((line
= malloc(line_size
)) == NULL
)
410 snprintf(line
, line_size
, group_line_format
, gr
->gr_name
, gr
->gr_passwd
,
411 (uintmax_t)gr
->gr_gid
);
412 if (gr
->gr_mem
!= NULL
)
413 for (ndx
= 0; gr
->gr_mem
[ndx
] != NULL
; ndx
++) {
414 strcat(line
, gr
->gr_mem
[ndx
]);
415 if (gr
->gr_mem
[ndx
+ 1] != NULL
)
423 * Duplicate a struct group.
426 gr_dup(const struct group
*gr
)
434 /* Calculate size of the group. */
435 len
= sizeof(*newgr
);
436 if (gr
->gr_name
!= NULL
)
437 len
+= strlen(gr
->gr_name
) + 1;
438 if (gr
->gr_passwd
!= NULL
)
439 len
+= strlen(gr
->gr_passwd
) + 1;
440 if (gr
->gr_mem
!= NULL
) {
441 for (num_mem
= 0; gr
->gr_mem
[num_mem
] != NULL
; num_mem
++)
442 len
+= strlen(gr
->gr_mem
[num_mem
]) + 1;
443 len
+= (num_mem
+ 1) * sizeof(*gr
->gr_mem
);
446 /* Create new group and copy old group into it. */
447 if ((newgr
= malloc(len
)) == NULL
)
449 /* point new gr_mem to end of struct + 1 */
450 if (gr
->gr_mem
!= NULL
)
451 newgr
->gr_mem
= (char **)(newgr
+ 1);
453 newgr
->gr_mem
= NULL
;
454 /* point dst after the end of all the gr_mem pointers in newgr */
455 dst
= (char *)&newgr
->gr_mem
[num_mem
+ 1];
456 if (gr
->gr_name
!= NULL
) {
457 newgr
->gr_name
= dst
;
458 dst
= stpcpy(dst
, gr
->gr_name
) + 1;
460 newgr
->gr_name
= NULL
;
462 if (gr
->gr_passwd
!= NULL
) {
463 newgr
->gr_passwd
= dst
;
464 dst
= stpcpy(dst
, gr
->gr_passwd
) + 1;
466 newgr
->gr_passwd
= NULL
;
468 newgr
->gr_gid
= gr
->gr_gid
;
469 if (gr
->gr_mem
!= NULL
) {
470 for (ndx
= 0; ndx
< num_mem
; ndx
++) {
471 newgr
->gr_mem
[ndx
] = dst
;
472 dst
= stpcpy(dst
, gr
->gr_mem
[ndx
]) + 1;
474 newgr
->gr_mem
[ndx
] = NULL
;
480 * Add a new member name to a struct group.
483 gr_add(struct group
*gr
, char *newmember
)
490 if (newmember
== NULL
)
493 if (gr
->gr_mem
!= NULL
) {
494 for (num_mem
= 0; gr
->gr_mem
[num_mem
] != NULL
; num_mem
++) {
495 if (strcmp(gr
->gr_mem
[num_mem
], newmember
) == 0) {
501 /* Allocate enough for current pointers + 1 more and NULL marker */
502 mlen
= (num_mem
+ 2) * sizeof(*gr
->gr_mem
);
503 if ((members
= malloc(mlen
)) == NULL
)
505 memcpy(members
, gr
->gr_mem
, num_mem
* sizeof(*gr
->gr_mem
));
506 members
[num_mem
++] = newmember
;
507 members
[num_mem
] = NULL
;
508 gr
->gr_mem
= members
;
515 * Scan a line and place it into a group structure.
518 __gr_scan(char *line
, struct group
*gr
)
523 /* Assign non-member information to structure. */
525 if ((loc
= strchr(line
, ':')) == NULL
)
528 gr
->gr_passwd
= loc
+ 1;
529 if (*gr
->gr_passwd
== ':')
530 *gr
->gr_passwd
= '\0';
532 if ((loc
= strchr(loc
+ 1, ':')) == NULL
)
536 if (sscanf(loc
+ 1, "%u", &gr
->gr_gid
) != 1)
539 /* Assign member information to structure. */
540 if ((loc
= strchr(loc
+ 1, ':')) == NULL
)
546 gr
->gr_mem
= reallocf(gr
->gr_mem
, sizeof(*gr
->gr_mem
) *
548 if (gr
->gr_mem
== NULL
)
551 /* Skip locations without members (i.e., empty string). */
553 gr
->gr_mem
[ndx
] = strsep(&line
, ",");
554 } while (gr
->gr_mem
[ndx
] != NULL
&& *gr
->gr_mem
[ndx
] == '\0');
555 } while (gr
->gr_mem
[ndx
++] != NULL
);
561 * Create a struct group from a line.
564 gr_scan(const char *line
)
568 struct group
*new_gr
;
570 if ((line_copy
= strdup(line
)) == NULL
)
572 if (!__gr_scan(line_copy
, &gr
)) {
576 new_gr
= gr_dup(&gr
);
578 if (gr
.gr_mem
!= NULL
)