b92bbd7d21403fad50314351fb146daafadc1f2e
[dragonfly.git] / usr.sbin / 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  * $FreeBSD: src/usr.sbin/pw/grupd.c,v 1.8 1999/10/26 04:27:14 davidn Exp $
27  * $DragonFly: src/usr.sbin/pw/grupd.c,v 1.2 2003/06/17 04:30:01 dillon Exp $
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdarg.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/param.h>
38
39 #include "pwupd.h"
40
41 static char * grpath = _PATH_PWD;
42
43 int
44 setgrdir(const char * dir)
45 {
46         if (dir == NULL)
47                 return -1;
48         else {
49                 char * d = malloc(strlen(dir)+1);
50                 if (d == NULL)
51                         return -1;
52                 grpath = strcpy(d, dir);
53         }
54         return 0;
55 }
56
57 char *
58 getgrpath(const char * file)
59 {
60         static char pathbuf[MAXPATHLEN];
61
62         snprintf(pathbuf, sizeof pathbuf, "%s/%s", grpath, file);
63         return pathbuf;
64 }
65
66 int
67 grdb(char *arg,...)
68 {
69         /*
70          * This is a stub for now, but maybe eventually be functional
71          * if ever an indexed version of /etc/groups is implemented.
72          */
73         arg=arg;
74         return 0;
75 }
76
77 int
78 fmtgrentry(char **buf, int * buflen, struct group * grp, int type)
79 {
80         int             i, l;
81
82         /*
83          * Since a group line is of arbitrary length,
84          * we need to calculate up-front just how long
85          * it will need to be...
86          */
87         /*  groupname              :   password                 :  gid  : */
88         l = strlen(grp->gr_name) + 1 + strlen(grp->gr_passwd) + 1 + 5 + 1;
89         /* group members + comma separator */
90         for (i = 0; grp->gr_mem[i] != NULL; i++) {
91                 l += strlen(grp->gr_mem[i]) + 1;
92         }
93         l += 2; /* For newline & NUL */
94         if (extendline(buf, buflen, l) == -1)
95                 l = -1;
96         else{
97                 /*
98                  * Now we can safely format
99                  */
100                 if (type == PWF_STANDARD)
101                         l = sprintf(*buf, "%s:*:%ld:", grp->gr_name, (long) grp->gr_gid);
102                 else
103                         l = sprintf(*buf, "%s:%s:%ld:", grp->gr_name, grp->gr_passwd, (long) grp->gr_gid);
104
105                 /*
106                  * List members
107                  */
108                 for (i = 0; grp->gr_mem[i] != NULL; i++) {
109                         l += sprintf(*buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]);
110                 }
111
112                 (*buf)[l++] = '\n';
113                 (*buf)[l] = '\0';
114         }
115         return l;
116 }
117
118
119 int
120 fmtgrent(char **buf, int * buflen, struct group * grp)
121 {
122         return fmtgrentry(buf, buflen, grp, PWF_STANDARD);
123 }
124
125
126 static int
127 gr_update(struct group * grp, char const * group, int mode)
128 {
129         int             l;
130         char            pfx[64];
131         int             grbuflen = 0;
132         char           *grbuf = NULL;
133
134         ENDGRENT();
135         l = snprintf(pfx, sizeof pfx, "%s:", group);
136
137         /*
138          * Update the group file
139          */
140         if (grp != NULL && fmtgrentry(&grbuf, &grbuflen, grp, PWF_PASSWD) == -1)
141                 l = -1;
142         else {
143                 l = fileupdate(getgrpath(_GROUP), 0644, grbuf, pfx, l, mode);
144                 if (l == 0)
145                         l = grdb(NULL);
146         }
147         if (grbuf != NULL)
148                 free(grbuf);
149         return l;
150 }
151
152
153 int
154 addgrent(struct group * grp)
155 {
156         return gr_update(grp, grp->gr_name, UPD_CREATE);
157 }
158
159 int
160 chggrent(char const * login, struct group * grp)
161 {
162         return gr_update(grp, login, UPD_REPLACE);
163 }
164
165 int
166 delgrent(struct group * grp)
167 {
168         return gr_update(NULL, grp->gr_name, UPD_DELETE);
169 }