Merge commit 'crater/master'
[dragonfly.git] / usr.sbin / pw / fileupd.c
blobac2068830fae374816ca9000b72f4c37d88d3a56
1 /*-
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
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.
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.
26 * $FreeBSD: src/usr.sbin/pw/fileupd.c,v 1.9 1999/10/26 04:27:13 davidn Exp $
27 * $DragonFly: src/usr.sbin/pw/fileupd.c,v 1.2 2003/06/17 04:30:01 dillon Exp $
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37 #include <errno.h>
38 #include <unistd.h>
40 #include "pwupd.h"
42 int
43 extendline(char **buf, int * buflen, int needed)
45 if (needed > *buflen) {
46 char *tmp = realloc(*buf, needed);
47 if (tmp == NULL)
48 return -1;
49 *buf = tmp;
50 *buflen = needed;
52 return *buflen;
55 int
56 extendarray(char ***buf, int * buflen, int needed)
58 if (needed > *buflen) {
59 char **tmp = realloc(*buf, needed * sizeof(char *));
60 if (tmp == NULL)
61 return -1;
62 *buf = tmp;
63 *buflen = needed;
65 return *buflen;
69 int
70 fileupdate(char const * filename, mode_t fmode, char const * newline, char const * prefix, int pfxlen, int updmode)
72 int rc = 0;
74 if (pfxlen <= 1)
75 rc = EINVAL;
76 else {
77 int infd = open(filename, O_RDWR | O_CREAT, fmode);
79 if (infd == -1)
80 rc = errno;
81 else {
82 FILE *infp = fdopen(infd, "r+");
84 if (infp == NULL) {
85 rc = errno; /* Assumes fopen(3) sets errno from open(2) */
86 close(infd);
87 } else {
88 int outfd;
89 char file[MAXPATHLEN];
91 strcpy(file, filename);
92 strcat(file, ".new");
93 outfd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, fmode);
94 if (outfd == -1)
95 rc = errno;
96 else {
97 FILE *outfp = fdopen(outfd, "w+");
99 if (outfp == NULL) {
100 rc = errno;
101 close(outfd);
102 } else {
103 int updated = UPD_CREATE;
104 int linesize = PWBUFSZ;
105 char *line = malloc(linesize);
107 nextline:
108 while (fgets(line, linesize, infp) != NULL) {
109 char *p = strchr(line, '\n');
111 while ((p = strchr(line, '\n')) == NULL) {
112 int l;
113 if (extendline(&line, &linesize, linesize + PWBUFSZ) == -1) {
114 int ch;
115 fputs(line, outfp);
116 while ((ch = fgetc(infp)) != EOF) {
117 fputc(ch, outfp);
118 if (ch == '\n')
119 break;
121 goto nextline;
123 l = strlen(line);
124 if (fgets(line + l, linesize - l, infp) == NULL)
125 break;
127 if (*line != '#' && *line != '\n') {
128 if (!updated && strncmp(line, prefix, pfxlen) == 0) {
129 updated = updmode == UPD_REPLACE ? UPD_REPLACE : UPD_DELETE;
132 * Only actually write changes if updating
134 if (updmode == UPD_REPLACE)
135 strcpy(line, newline);
136 else if (updmode == UPD_DELETE)
137 continue;
140 fputs(line, outfp);
144 * Now, we need to decide what to do: If we are in
145 * update mode, and no record was updated, then error If
146 * we are in insert mode, and record already exists,
147 * then error
149 if (updmode != updated)
150 /* -1 return means:
151 * update,delete=no user entry
152 * create=entry exists
154 rc = -1;
155 else {
158 * If adding a new record, append it to the end
160 if (updmode == UPD_CREATE)
161 fputs(newline, outfp);
164 * Flush the file and check for the result
166 if (fflush(outfp) == EOF)
167 rc = errno; /* Failed to update */
168 else {
170 * Copy data back into the
171 * original file and truncate
173 rewind(infp);
174 rewind(outfp);
175 while (fgets(line, linesize, outfp) != NULL)
176 fputs(line, infp);
179 * If there was a problem with copying
180 * we will just rename 'file.new'
181 * to 'file'.
182 * This is a gross hack, but we may have
183 * corrupted the original file
184 * Unfortunately, it will lose the inode
185 * and hence the lock.
187 if (fflush(infp) == EOF || ferror(infp))
188 rename(file, filename);
189 else
190 ftruncate(infd, ftell(infp));
193 free(line);
194 fclose(outfp);
196 remove(file);
198 fclose(infp);
202 return rc;