Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / pw / fileupd.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/fileupd.c,v 1.9 1999/10/26 04:27:13 davidn 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
44 int
45 extendline(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
57 int
58 extendarray(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
71 int
72 fileupdate(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                 rc = EINVAL;
78         else {
79                 int    infd = open(filename, O_RDWR | O_CREAT, fmode);
80
81                 if (infd == -1)
82                         rc = errno;
83                 else {
84                         FILE   *infp = fdopen(infd, "r+");
85
86                         if (infp == NULL) {
87                                 rc = errno;             /* Assumes fopen(3) sets errno from open(2) */
88                                 close(infd);
89                         } else {
90                                 int       outfd;
91                                 char      file[MAXPATHLEN];
92
93                                 strcpy(file, filename);
94                                 strcat(file, ".new");
95                                 outfd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, fmode);
96                                 if (outfd == -1)
97                                         rc = errno;
98                                 else {
99                                         FILE    *outfp = fdopen(outfd, "w+");
100
101                                         if (outfp == NULL) {
102                                                 rc = errno;
103                                                 close(outfd);
104                                         } else {
105                                                 int   updated = UPD_CREATE;
106                                                 int             linesize = PWBUFSZ;
107                                                 char  *line = malloc(linesize);
108
109                                         nextline:
110                                                 while (fgets(line, linesize, infp) != NULL) {
111                                                         char  *p = strchr(line, '\n');
112
113                                                         while ((p = strchr(line, '\n')) == NULL) {
114                                                                 int     l;
115                                                                 if (extendline(&line, &linesize, linesize + PWBUFSZ) == -1) {
116                                                                         int     ch;
117                                                                         fputs(line, outfp);
118                                                                         while ((ch = fgetc(infp)) != EOF) {
119                                                                                 fputc(ch, outfp);
120                                                                                 if (ch == '\n')
121                                                                                         break;
122                                                                         }
123                                                                         goto nextline;
124                                                                 }
125                                                                 l = strlen(line);
126                                                                 if (fgets(line + l, linesize - l, infp) == NULL)
127                                                                         break;
128                                                         }
129                                                         if (*line != '#' && *line != '\n') {
130                                                                 if (!updated && strncmp(line, prefix, pfxlen) == 0) {
131                                                                         updated = updmode == UPD_REPLACE ? UPD_REPLACE : UPD_DELETE;
132
133                                                                         /*
134                                                                          * Only actually write changes if updating
135                                                                          */
136                                                                         if (updmode == UPD_REPLACE)
137                                                                                 strcpy(line, newline);
138                                                                         else if (updmode == UPD_DELETE)
139                                                                                 continue;
140                                                                 }
141                                                         }
142                                                         fputs(line, outfp);
143                                                 }
144
145                                                 /*
146                                                  * Now, we need to decide what to do: If we are in
147                                                  * update mode, and no record was updated, then error If
148                                                  * we are in insert mode, and record already exists,
149                                                  * then error
150                                                  */
151                                                 if (updmode != updated)
152                                                         /* -1 return means:
153                                                          * update,delete=no user entry
154                                                          * create=entry exists
155                                                          */
156                                                         rc = -1;
157                                                 else {
158
159                                                         /*
160                                                          * If adding a new record, append it to the end
161                                                          */
162                                                         if (updmode == UPD_CREATE)
163                                                                 fputs(newline, outfp);
164
165                                                         /*
166                                                          * Flush the file and check for the result
167                                                          */
168                                                         if (fflush(outfp) == EOF)
169                                                                 rc = errno;     /* Failed to update */
170                                                         else {
171                                                                 /*
172                                                                  * Copy data back into the
173                                                                  * original file and truncate
174                                                                  */
175                                                                 rewind(infp);
176                                                                 rewind(outfp);
177                                                                 while (fgets(line, linesize, outfp) != NULL)
178                                                                         fputs(line, infp);
179
180                                                                 /*
181                                                                  * If there was a problem with copying
182                                                                  * we will just rename 'file.new' 
183                                                                  * to 'file'.
184                                                                  * This is a gross hack, but we may have
185                                                                  * corrupted the original file
186                                                                  * Unfortunately, it will lose the inode
187                                                                  * and hence the lock.
188                                                                  */
189                                                                 if (fflush(infp) == EOF || ferror(infp))
190                                                                         rename(file, filename);
191                                                                 else
192                                                                         ftruncate(infd, ftell(infp));
193                                                         }
194                                                 }
195                                                 free(line);
196                                                 fclose(outfp);
197                                         }
198                                         remove(file);
199                                 }
200                                 fclose(infp);
201                         }
202                 }
203         }
204         return rc;
205 }