edgroup.c revision 20302
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 *	$Id: edgroup.c,v 1.1.1.1 1996/12/09 14:05:35 joerg Exp $
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <stdarg.h>
34#include <errno.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
45static int
46isingroup(char const * name, char **mem)
47{
48	int             i;
49
50	for (i = 0; i < MAXGROUPS && mem[i] != NULL; i++)
51		if (strcmp(name, mem[i]) == 0)
52			return i;
53	return -1;
54}
55
56static char     groupfile[] = _PATH_GROUP;
57static char     grouptmp[] = _PATH_GROUP ".new";
58
59int
60editgroups(char *name, char **groups)
61{
62	int             rc = 0;
63	int             infd;
64
65	if ((infd = open(groupfile, O_RDWR | O_CREAT | O_EXLOCK, 0644)) != -1) {
66		FILE           *infp;
67
68		if ((infp = fdopen(infd, "r+")) == NULL)
69			close(infd);
70		else {
71			int             outfd;
72
73			if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, 0644)) != -1) {
74				FILE           *outfp;
75
76				if ((outfp = fdopen(outfd, "w+")) == NULL)
77					close(outfd);
78				else {
79					char            line[MAXPWLINE];
80					char            outl[MAXPWLINE];
81
82					while (fgets(line, sizeof(line), infp) != NULL) {
83						char           *p = strchr(line, '\n');
84
85						if (p == NULL) {	/* Line too long */
86							int             ch;
87
88							fputs(line, outfp);
89							while ((ch = fgetc(infp)) != EOF) {
90								fputc(ch, outfp);
91								if (ch == '\n')
92									break;
93							}
94							continue;
95						}
96						if (*line == '#')
97							strcpy(outl, line);
98						else if (*line == '\n')
99							*outl = '\0';
100						else {
101							int             i,
102							                mno = 0;
103							char           *cp = line;
104							char const     *sep = ":\n";
105							struct group    grp;
106							char           *mems[MAXGROUPS];
107
108							memset(&grp, 0, sizeof grp);
109							grp.gr_mem = mems;
110							for (i = 0; (p = strsep(&cp, sep)) != NULL; i++) {
111								switch (i) {
112								case 0:	/* Group name */
113									grp.gr_name = p;
114									break;
115								case 1:	/* Group password */
116									grp.gr_passwd = p;
117									break;
118								case 2:	/* Group id */
119									grp.gr_gid = atoi(p);
120									break;
121								case 3:	/* Member list */
122									cp = p;
123									sep = ",\n";
124									break;
125								default:	/* Individual members */
126									if (mno < MAXGROUPS && *p)
127										mems[mno++] = p;
128									break;
129								}
130							}
131							if (i < 2)	/* Bail out -
132									 * insufficient fields */
133								continue;
134
135							for (i = mno; i < MAXGROUPS; i++)
136								mems[i] = NULL;
137
138							/*
139							 * Delete from group, or add to group?
140							 */
141							if (groups == NULL || isingroup(grp.gr_name, groups) == -1) {	/* Delete */
142								int             idx;
143
144								while ((idx = isingroup(name, mems)) != -1) {
145									for (i = idx; i < (MAXGROUPS - 1); i++)
146										mems[i] = mems[i + 1];
147									mems[i] = NULL;
148									--mno;
149								}
150
151								/*
152								 * Special case - deleting user and group may be user's own
153								 */
154								if (groups == NULL && mems[0] == NULL && strcmp(name, grp.gr_name) == 0) {	/* First, make _sure_ we
155																		 * don't have other
156																		 * members */
157									struct passwd  *pwd;
158
159									setpwent();
160									while ((pwd = getpwent()) != NULL && pwd->pw_gid != grp.gr_gid);
161									endpwent();
162									if (pwd == NULL)	/* No members at all */
163										continue;	/* Drop the group */
164								}
165							} else if (isingroup(name, mems) == -1)
166								mems[mno++] = name;
167							fmtgrentry(outl, &grp, PWF_GROUP);
168						}
169						fputs(outl, outfp);
170					}
171					if (fflush(outfp) != EOF) {
172						rc = 1;
173
174						/*
175						 * Copy data back into the original file and truncate
176						 */
177						rewind(infp);
178						rewind(outfp);
179						while (fgets(line, sizeof(line), outfp) != NULL)
180							fputs(line, infp);
181
182						/*
183						 * This is a gross hack, but we may have corrupted the
184						 * original file. Unfortunately, it will lose preservation
185						 * of the inode.
186						 */
187						if (fflush(infp) == EOF || ferror(infp))
188							rc = rename(grouptmp, groupfile) == 0;
189						else
190							ftruncate(infd, ftell(infp));
191					}
192					fclose(outfp);
193				}
194				remove(grouptmp);
195			}
196			fclose(infp);
197		}
198	}
199	return rc;
200}
201