]> git.cameronkatri.com Git - pw-darwin.git/blob - pw/grupd.c
MF22: Remove confusing comma
[pw-darwin.git] / pw / grupd.c
1 /*-
2 * Copyright (C) 1996
3 * David L. Nugent. 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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #ifndef lint
28 static const char rcsid[] =
29 "$Id$";
30 #endif /* not lint */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 #include "pwupd.h"
41
42 int
43 fmtgrentry(char **buf, int * buflen, struct group * grp, int type)
44 {
45 int i, l;
46
47 /*
48 * Since a group line is of arbitrary length,
49 * we need to calculate up-front just how long
50 * it will need to be...
51 */
52 /* groupname : password : gid : */
53 l = strlen(grp->gr_name) + 1 + strlen(grp->gr_passwd) + 1 + 5 + 1;
54 /* group members + comma separator */
55 for (i = 0; grp->gr_mem[i] != NULL; i++) {
56 l += strlen(grp->gr_mem[i]) + 1;
57 }
58 l += 2; /* For newline & NUL */
59 if (extendline(buf, buflen, l) == -1)
60 l = -1;
61 else{
62 /*
63 * Now we can safely format
64 */
65 if (type == PWF_STANDARD)
66 l = sprintf(*buf, "%s:*:%ld:", grp->gr_name, (long) grp->gr_gid);
67 else
68 l = sprintf(*buf, "%s:%s:%ld:", grp->gr_name, grp->gr_passwd, (long) grp->gr_gid);
69
70 /*
71 * List members
72 */
73 for (i = 0; grp->gr_mem[i] != NULL; i++) {
74 l += sprintf(*buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]);
75 }
76
77 (*buf)[l++] = '\n';
78 (*buf)[l] = '\0';
79 }
80 return l;
81 }
82
83
84 int
85 fmtgrent(char **buf, int * buflen, struct group * grp)
86 {
87 return fmtgrentry(buf, buflen, grp, PWF_STANDARD);
88 }
89
90
91 static int
92 gr_update(struct group * grp, char const * group, int mode)
93 {
94 int l;
95 char pfx[64];
96 int grbuflen = 0;
97 char *grbuf = NULL;
98
99 endgrent();
100 l = snprintf(pfx, sizeof pfx, "%s:", group);
101
102 /*
103 * Update the group file
104 */
105 if (grp != NULL && fmtgrentry(&grbuf, &grbuflen, grp, PWF_PASSWD) == -1)
106 l = -1;
107 else
108 l = fileupdate(_PATH_GROUP, 0644, grbuf, pfx, l, mode);
109 if (grbuf != NULL)
110 free(grbuf);
111 return l;
112 }
113
114
115 int
116 addgrent(struct group * grp)
117 {
118 return gr_update(grp, grp->gr_name, UPD_CREATE);
119 }
120
121 int
122 chggrent(char const * login, struct group * grp)
123 {
124 return gr_update(grp, login, UPD_REPLACE);
125 }
126
127 int
128 delgrent(struct group * grp)
129 {
130 return gr_update(NULL, grp->gr_name, UPD_DELETE);
131 }