fileupd.c revision 37711
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
28static const char rcsid[] =
29	"$Id: fileupd.c,v 1.5 1997/10/10 06:23:31 charnier Exp $";
30#endif /* not lint */
31
32#include <stdio.h>
33#include <fcntl.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/param.h>
39#include <errno.h>
40#include <unistd.h>
41
42#include "pwupd.h"
43
44int
45extendline(char **buf, int * buflen, int needed)
46{
47	if (needed > *buflen) {
48		char	*tmp = realloc(*buf, needed);
49		if (tmp == NULL)
50			return -1;
51		*buf = tmp;
52		*buflen = needed;
53	}
54	return *buflen;
55}
56
57int
58extendarray(char ***buf, int * buflen, int needed)
59{
60	if (needed > *buflen) {
61		char	**tmp = realloc(*buf, needed * sizeof(char *));
62		if (tmp == NULL)
63			return -1;
64		*buf = tmp;
65		*buflen = needed;
66	}
67	return *buflen;
68}
69
70
71int
72fileupdate(char const * filename, mode_t fmode, char const * newline, char const * prefix, int pfxlen, int updmode)
73{
74	int             rc = 0;
75
76	if (pfxlen <= 1)
77		errno = EINVAL;
78	else {
79		int             infd = open(filename, O_RDWR | O_CREAT, fmode);
80
81		if (infd != -1) {
82			FILE           *infp = fdopen(infd, "r+");
83
84			if (infp == NULL)
85				close(infd);
86			else {
87				int             outfd;
88				char            file[MAXPATHLEN];
89
90				strcpy(file, filename);
91				strcat(file, ".new");
92				outfd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, fmode);
93				if (outfd != -1) {
94					FILE           *outfp = fdopen(outfd, "w+");
95
96					if (outfp == NULL)
97						close(outfd);
98					else {
99						int             updated = UPD_CREATE;
100						int		linesize = PWBUFSZ;
101						char           *line = malloc(linesize);
102
103					nextline:
104						while (fgets(line, linesize, infp) != NULL) {
105							char           *p = strchr(line, '\n');
106
107							while ((p = strchr(line, '\n')) == NULL) {
108								int	l;
109								if (extendline(&line, &linesize, linesize + PWBUFSZ) == -1) {
110									int	ch;
111									fputs(line, outfp);
112									while ((ch = fgetc(infp)) != EOF) {
113										fputc(ch, outfp);
114										if (ch == '\n')
115											break;
116									}
117									goto nextline;
118								}
119								l = strlen(line);
120								if (fgets(line + l, linesize - l, infp) == NULL)
121									break;
122							}
123							if (*line != '#' && *line != '\n') {
124								if (!updated && strncmp(line, prefix, pfxlen) == 0) {
125									updated = updmode == UPD_REPLACE ? UPD_REPLACE : UPD_DELETE;
126
127									/*
128									 * Only actually write changes if updating
129									 */
130									if (updmode == UPD_REPLACE)
131										strcpy(line, newline);
132									else if (updmode == UPD_DELETE)
133										continue;
134								}
135							}
136							fputs(line, outfp);
137						}
138
139						/*
140						 * Now, we need to decide what to do: If we are in
141						 * update mode, and no record was updated, then error If
142						 * we are in insert mode, and record already exists,
143						 * then error
144						 */
145						if (updmode != updated)
146							errno = (updmode == UPD_CREATE) ? EEXIST : ENOENT;
147						else {
148
149							/*
150							 * If adding a new record, append it to the end
151							 */
152							if (updmode == UPD_CREATE)
153								fputs(newline, outfp);
154
155							/*
156							 * Flush the file and check for the result
157							 */
158							rc = fflush(outfp) != EOF;
159							if (rc) {
160
161								/*
162								 * Copy data back into the
163								 * original file and truncate
164								 */
165								rewind(infp);
166								rewind(outfp);
167								while (fgets(line, linesize, outfp) != NULL)
168									fputs(line, infp);
169
170								/*
171                                                                 * If there was a problem with copying
172                                                                 * we will just rename 'file.new'
173                                                                 * to 'file'.
174								 * This is a gross hack, but we may have
175								 * corrupted the original file
176								 * Unfortunately, it will lose the inode
177                                                                 * and hence the lock.
178                                                                 *
179                                                                 * The implications of this is that this invocation of pw
180                                                                 * won't have the file locked and concurrent copies
181                                                                 * of pw, vipw etc could clobber what this one is doing.
182                                                                 *
183                                                                 * It should probably just return an error instead
184                                                                 * of going on like nothing is wrong.
185								 */
186								if (fflush(infp) == EOF || ferror(infp))
187									rc = rename(file, filename) == 0;
188								else
189									ftruncate(infd, ftell(infp));
190							}
191						}
192						free(line);
193						fclose(outfp);
194					}
195					remove(file);
196				}
197				fclose(infp);
198			}
199		}
200	}
201	return rc;
202}
203