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