Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / pw / edgroup.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   "$FreeBSD: src/usr.sbin/pw/edgroup.c,v 1.8 1999/08/28 01:19:16 peter Exp $";
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 #include <pwd.h>
40 #include <grp.h>
41 #include <fcntl.h>
42 #include <sys/param.h>
43 #include <ctype.h>
44
45 #include "pwupd.h"
46
47 static int
48 isingroup(char const * name, char **mem)
49 {
50         int             i;
51
52         for (i = 0; mem[i] != NULL; i++)
53                 if (strcmp(name, mem[i]) == 0)
54                         return i;
55         return -1;
56 }
57
58 int
59 editgroups(char *name, char **groups)
60 {
61         int             rc = 0;
62         int             infd;
63         char            groupfile[MAXPATHLEN];
64         char            grouptmp[MAXPATHLEN];
65
66         strncpy(groupfile, getgrpath(_GROUP), MAXPATHLEN - 5);
67         groupfile[MAXPATHLEN - 5] = '\0';
68         strcpy(grouptmp, groupfile);
69         strcat(grouptmp, ".new");
70
71         if ((infd = open(groupfile, O_RDWR | O_CREAT, 0644)) != -1) {
72                 FILE           *infp;
73
74                 if ((infp = fdopen(infd, "r+")) == NULL)
75                         close(infd);
76                 else {
77                         int             outfd;
78
79                         if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, 0644)) != -1) {
80                                 FILE           *outfp;
81
82                                 if ((outfp = fdopen(outfd, "w+")) == NULL)
83                                         close(outfd);
84                                 else {
85                                         int             linelen = PWBUFSZ;
86                                         int             outlen =  PWBUFSZ;
87                                         int             memlen = 200; /* Arbitrary */
88                                         char           *line = malloc(linelen);
89                                         char           *outl = malloc(outlen);
90                                         char          **mems = malloc(memlen * sizeof(char *));
91                                         int             namlen = strlen(name);
92
93                                         if (line == NULL || outl == NULL || mems == NULL) {
94                                             mem_abort:
95                                                 rc = 0;
96                                         } else {
97                                                 while (fgets(line, linelen, infp) != NULL) {
98                                                         char           *p;
99                                                         int             l;
100
101                                                         while ((p = strchr(line, '\n')) == NULL)
102                                                         {
103                                                                 if (extendline(&line, &linelen, linelen + PWBUFSZ) == -1) {
104                                                                         goto mem_abort;
105                                                                 }
106                                                                 l = strlen(line);
107                                                                 if (fgets(line + l, linelen - l, infp) == NULL)
108                                                                         break;  /* No newline terminator on last line */
109                                                         }
110                                                         l = strlen(line) + namlen + 1;
111                                                         if (extendline(&outl, &outlen, l) == -1) {
112                                                                 goto mem_abort;
113                                                         }
114                                                         if (*line == '#')
115                                                                 strcpy(outl, line);
116                                                         else if (*line == '\n')
117                                                                 *outl = '\0';
118                                                         else {
119                                                                 int             i,
120                                                                                 mno = 0;
121                                                                 char           *cp = line;
122                                                                 char const     *sep = ":\n";
123                                                                 struct group    grp;
124
125                                                                 memset(&grp, 0, sizeof grp);
126                                                                 for (i = 0; (p = strsep(&cp, sep)) != NULL; i++) {
127                                                                         switch (i) {
128                                                                         case 0: /* Group name */
129                                                                                 grp.gr_name = p;
130                                                                                 break;
131                                                                         case 1: /* Group password */
132                                                                                 grp.gr_passwd = p;
133                                                                                 break;
134                                                                         case 2: /* Group id */
135                                                                                 grp.gr_gid = atoi(p);
136                                                                                 break;
137                                                                         case 3: /* Member list */
138                                                                                 cp = p;
139                                                                                 sep = ",\n";
140                                                                                 break;
141                                                                         default:        /* Individual members */
142                                                                                 if (*p) {
143                                                                                         if (extendarray(&mems, &memlen, mno + 2) == -1) {
144                                                                                                 goto mem_abort;
145                                                                                         }
146                                                                                         mems[mno++] = p;
147                                                                                 }
148                                                                                 break;
149                                                                         }
150                                                                 }
151                                                                 if (i < 2)      /* Bail out - insufficient fields */
152                                                                         continue;
153
154                                                                 grp.gr_mem = mems;
155                                                                 for (i = mno; i < memlen; i++)
156                                                                         mems[i] = NULL;
157
158                                                                 /*
159                                                                  * Delete from group, or add to group?
160                                                                  */
161                                                                 if (groups == NULL || isingroup(grp.gr_name, groups) == -1) {   /* Delete */
162                                                                         int             idx;
163
164                                                                         while ((idx = isingroup(name, mems)) != -1) {
165                                                                                 for (i = idx; i < (memlen - 1); i++)
166                                                                                         mems[i] = mems[i + 1];
167                                                                                 mems[i] = NULL;
168                                                                                 --mno;
169                                                                         }
170                                                                         /*
171                                                                          * Special case - deleting user and group may be user's own
172                                                                          */
173                                                                         if (groups == NULL && mems[0] == NULL && strcmp(name, grp.gr_name) == 0) {
174                                                                                 /*
175                                                                                  * First, make _sure_ we don't have other members
176                                                                                  */
177                                                                                 struct passwd  *pwd;
178
179                                                                                 SETPWENT();
180                                                                                 while ((pwd = GETPWENT()) != NULL && (gid_t)pwd->pw_gid != (gid_t)grp.gr_gid);
181                                                                                 ENDPWENT();
182                                                                                 if (pwd == NULL)        /* No members at all */
183                                                                                         continue;       /* Drop the group */
184                                                                         }
185                                                                 } else if (isingroup(name, mems) == -1) {
186                                                                         if (extendarray(&mems, &memlen, mno + 2) == -1) {
187                                                                                 goto mem_abort;
188                                                                         }
189                                                                         grp.gr_mem = mems;    /* May have realloced() */
190                                                                         mems[mno++] = name;
191                                                                         mems[mno  ] = NULL;
192                                                                 }
193                                                                 fmtgrentry(&outl, &outlen, &grp, PWF_GROUP);
194                                                         }
195                                                         fputs(outl, outfp);
196                                                 }
197                                                 if (fflush(outfp) != EOF) {
198                                                         rc = 1;
199
200                                                         /*
201                                                          * Copy data back into the original file and truncate
202                                                          */
203                                                         rewind(infp);
204                                                         rewind(outfp);
205                                                         while (fgets(outl, outlen, outfp) != NULL)
206                                                                 fputs(outl, infp);
207
208                                                         /*
209                                                          * This is a gross hack, but we may have corrupted the
210                                                          * original file. Unfortunately, it will lose preservation
211                                                          * of the inode.
212                                                          */
213                                                         if (fflush(infp) == EOF || ferror(infp))
214                                                                 rc = rename(grouptmp, groupfile) == 0;
215                                                         else
216                                                                 ftruncate(infd, ftell(infp));
217                                                 }
218                                         }
219                                         free(mems);
220                                         free(outl);
221                                         free(line);
222                                         fclose(outfp);
223                                 }
224                                 remove(grouptmp);
225                         }
226                         fclose(infp);
227                 }
228         }
229         return rc;
230 }