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