]>
Commit | Line | Data |
---|---|---|
31aff165 MM |
1 | /* |
2 | * $Id$ | |
3 | * | |
e040c79e | 4 | * Copyright (c) 2001 Michal Moskal <malekith@pld-linux.org>. |
31aff165 MM |
5 | * All rights reserved. |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. All advertising materials mentioning features or use of this software | |
16 | * must display the following acknowledgement: | |
17 | * This product includes software developed by Michal Moskal. | |
18 | * 4. Neither the name of the author nor the names of any co-contributors | |
19 | * may be used to endorse or promote products derived from this software | |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY MICHAL MOSKAL AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
33 | * | |
34 | * | |
35 | * | |
36 | * USAGE: joinpasswd | |
37 | * | |
38 | * Add entries from freshly created {passwd,shadow,group}.rpmnew to existing | |
39 | * {passwd,shadow,group}. It is usable as part of setup package, during upgrade | |
40 | * where new system user/group is to be added. If entry is already found in | |
3da0d83f MM |
41 | * system database, it is left, otherwise it is added. UIDs/GIDs are *not* |
42 | * checked anyhow. | |
31aff165 MM |
43 | * |
44 | * For typical sizes of files in setup package, it takes about 1 second per | |
45 | * 20000 users in system database on Pentium class machine. After static link | |
46 | * against uClibc it is under 2k on x86. Stdio hasn't been used intentionally. | |
47 | * | |
e040c79e | 48 | * Written for PLD Linux (http://www.pld-linux.org/) setup package. |
31aff165 MM |
49 | * |
50 | * Compilation against uClibc: | |
51 | * UCROOT=/usr/lib/bootdisk/usr | |
52 | * gcc -I$UCROOT/include -nostdlib -O2 joinpasswd.c $UCROOT/lib/crt0.o \ | |
53 | * $UCROOT/lib/libc.a -lgcc -o joinpasswd | |
54 | * strip -R .comment -R .note joinpasswd | |
55 | * | |
e040c79e | 56 | * The idea of this program comes from Lukasz Dobrek <dobrek@pld-linux.org>. |
31aff165 MM |
57 | * |
58 | */ | |
59 | ||
60 | #include <sys/types.h> | |
61 | #include <sys/mman.h> | |
62 | #include <sys/stat.h> | |
63 | #include <unistd.h> | |
64 | #include <fcntl.h> | |
65 | #include <stdlib.h> | |
66 | #include <string.h> | |
507b603d | 67 | #include <signal.h> |
31aff165 MM |
68 | |
69 | #define FILE1 "/etc/passwd" | |
70 | #define FILE2 "/etc/shadow" | |
71 | #define FILE3 "/etc/group" | |
1a43487e | 72 | #define FILE4 "/etc/gshadow" |
31aff165 | 73 | |
507b603d MM |
74 | /* #define OLD_LOCK */ |
75 | ||
76 | #define LOCK_FILE "/etc/.pwd.lock" | |
77 | ||
31aff165 | 78 | #define SUFFIX ".rpmnew" |
f88ff4b9 MM |
79 | /* maybe "-" or sth? */ |
80 | #define BACKUP ".old" | |
31aff165 MM |
81 | |
82 | /* #define SILENT */ | |
83 | ||
84 | void eputs(const char *msg) | |
85 | { | |
86 | write(2, msg, strlen(msg)); | |
87 | } | |
88 | ||
89 | void fatal(const char *msg) | |
90 | { | |
91 | eputs(msg); | |
92 | eputs("\n"); | |
93 | exit(1); | |
94 | } | |
95 | ||
96 | char *map_file(const char *name, int *sz) | |
97 | { | |
98 | int fd; | |
99 | void *ptr; | |
100 | struct stat st; | |
101 | ||
102 | fd = open(name, O_RDONLY); | |
103 | if (fd == -1) | |
104 | return NULL; | |
105 | fstat(fd, &st); | |
106 | *sz = st.st_size; | |
107 | ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
108 | if (ptr == MAP_FAILED) | |
109 | return NULL; | |
110 | ||
111 | return ptr; | |
112 | } | |
113 | ||
114 | int exist(char *id, int id_len, char *ptr, int sz) | |
115 | { | |
116 | int i; | |
117 | ||
118 | for (i = 0; i < sz; ) { | |
119 | if (sz - i > id_len && memcmp(id, ptr + i, id_len + 1) == 0) | |
120 | return 1; | |
121 | while (i < sz && ptr[i] != '\n') | |
122 | i++; | |
123 | i++; | |
124 | } | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
3da0d83f MM |
129 | void itoa(char *buf, long i) |
130 | { | |
131 | char tmp[32]; | |
132 | char *p; | |
133 | ||
134 | if (i < 0) { | |
135 | strcpy(buf, "-"); | |
136 | buf++; | |
137 | i = -i; | |
138 | } | |
139 | if (i == 0) { | |
140 | strcpy(buf, "0"); | |
141 | return; | |
142 | } | |
143 | for (p = tmp; i; i /= 10) | |
144 | *p++ = (i % 10) + '0'; | |
145 | while (p > tmp) | |
146 | *buf++ = *--p; | |
147 | *buf = 0; | |
148 | } | |
149 | ||
507b603d MM |
150 | #ifndef OLD_LOCK |
151 | int lock_fd = -1; | |
152 | void noop(int x) | |
153 | { | |
154 | (void)x; | |
155 | } | |
156 | #endif | |
157 | ||
3da0d83f MM |
158 | int try_lock(const char *name) |
159 | { | |
507b603d | 160 | #ifdef OLD_LOCK |
3da0d83f MM |
161 | char file[strlen(name) + 32], lock[strlen(name) + 32]; |
162 | char buf[32]; | |
163 | int fd; | |
164 | long pid; | |
165 | ||
166 | strcpy(lock, name); | |
167 | strcpy(file, name); | |
168 | strcat(lock, ".lock"); | |
169 | itoa(buf, (long)getpid()); | |
170 | strcat(file, "."); | |
171 | strcat(file, buf); | |
172 | ||
173 | fd = open(file, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600); | |
174 | if (fd < 0) | |
175 | return -1; | |
176 | write(fd, buf, strlen(buf)); | |
177 | close(fd); | |
178 | ||
179 | if (link(file, lock) == 0) { | |
180 | unlink(file); | |
181 | return 0; | |
182 | } | |
183 | ||
184 | fd = open(lock, O_RDONLY); | |
185 | if (fd < 0) | |
186 | goto oops; | |
187 | memset(buf, 0, sizeof(buf)); | |
188 | read(fd, buf, sizeof(buf)); | |
189 | pid = atol(buf); | |
190 | if (pid == 0 || kill(pid, 0) != 0) { | |
191 | /* stale lock */ | |
192 | unlink(file); | |
193 | unlink(lock); | |
194 | /* try again */ | |
195 | return try_lock(name); | |
196 | } | |
197 | ||
198 | oops: | |
199 | unlink(file); | |
200 | return -1; | |
507b603d MM |
201 | #else |
202 | struct flock fl; | |
203 | ||
204 | if (lock_fd != -1) | |
205 | return -1; | |
206 | lock_fd = open(LOCK_FILE, O_RDWR|O_CREAT, 0600); | |
207 | if (lock_fd == -1) | |
208 | return -1; | |
209 | signal(SIGALRM, noop); | |
210 | alarm(15); | |
211 | memset(&fl, 0, sizeof(fl)); | |
212 | fl.l_type = F_WRLCK; | |
213 | fl.l_whence = SEEK_SET; | |
214 | if (fcntl(lock_fd, F_SETLKW, &fl) != 0) { | |
215 | alarm(0); | |
216 | close(lock_fd); | |
217 | lock_fd = -1; | |
218 | return -1; | |
219 | } | |
220 | alarm(0); | |
221 | ||
222 | return 0; | |
223 | #endif | |
3da0d83f MM |
224 | } |
225 | ||
226 | void unlock(const char *name) | |
227 | { | |
507b603d | 228 | #ifdef OLD_LOCK |
3da0d83f MM |
229 | char lock[strlen(name) + 32]; |
230 | ||
231 | strcpy(lock, name); | |
232 | strcat(lock, ".lock"); | |
233 | unlink(lock); | |
507b603d MM |
234 | #else |
235 | if (lock_fd != -1) | |
236 | close(lock_fd); | |
237 | lock_fd = -1; | |
238 | #endif | |
3da0d83f MM |
239 | } |
240 | ||
241 | void lock(const char *name) | |
242 | { | |
243 | int n; | |
244 | ||
245 | n = 5; | |
246 | while (n--) { | |
247 | if (try_lock(name) == 0) | |
248 | return; | |
249 | eputs("waiting for lock...\n"); | |
250 | sleep(1); | |
251 | } | |
252 | fatal("cannot get lock"); | |
253 | } | |
254 | ||
f88ff4b9 | 255 | int join(const char *old_name, const char *new_name, const char *backup_name) |
31aff165 MM |
256 | { |
257 | char *old, *new, *id; | |
258 | int i, fd; | |
259 | int old_sz, new_sz; | |
260 | ||
261 | new = map_file(new_name, &new_sz); | |
262 | if (new == NULL) | |
263 | return -1; | |
3da0d83f MM |
264 | |
265 | lock(old_name); | |
31aff165 MM |
266 | old = map_file(old_name, &old_sz); |
267 | if (old == NULL) | |
268 | fatal("cannot mmap old"); | |
f88ff4b9 MM |
269 | |
270 | fd = open(backup_name, O_WRONLY|O_CREAT|O_TRUNC, 0600); | |
271 | if (fd < 0) | |
272 | fatal("cannot make backup"); | |
273 | write(fd, old, old_sz); | |
274 | close(fd); | |
275 | ||
31aff165 | 276 | #ifndef SILENT |
128250fd | 277 | eputs("merging content of `"); |
31aff165 MM |
278 | eputs(old_name); |
279 | eputs("' with `"); | |
280 | eputs(new_name); | |
281 | eputs("'\n"); | |
282 | #endif /* SILENT */ | |
283 | ||
284 | fd = open(old_name, O_WRONLY|O_APPEND); | |
285 | ||
286 | for (i = 0; i < new_sz; ) { | |
287 | id = new + i; | |
288 | while (i < new_sz && new[i] != ':' && new[i] != '\n') | |
289 | i++; | |
290 | if (i < new_sz && new[i] == ':') { | |
291 | int id_len, line_len; | |
292 | ||
293 | id_len = i - (id - new); | |
294 | while (i < new_sz && new[i] != '\n') | |
295 | i++; | |
296 | if (i < new_sz) | |
297 | i++; | |
298 | line_len = i - (id - new); | |
299 | ||
300 | if (!exist(id, id_len, old, old_sz)) { | |
301 | #ifndef SILENT | |
302 | eputs(old_name); | |
303 | eputs(": adding `"); | |
304 | write(2, id, id_len); | |
305 | eputs("'\n"); | |
306 | #endif /* SILENT */ | |
307 | write(fd, id, line_len); | |
308 | } | |
309 | } else if (i < new_sz) | |
310 | i++; | |
311 | } | |
312 | ||
313 | #if 0 | |
314 | /* user may want to exime this file... */ | |
315 | unlink(new_name); | |
316 | #endif | |
3da0d83f | 317 | unlock(old_name); |
31aff165 MM |
318 | |
319 | return 0; | |
320 | } | |
321 | ||
322 | int main() | |
323 | { | |
1a43487e | 324 | #if 1 |
3da0d83f MM |
325 | join(FILE1, FILE1 SUFFIX, FILE1 BACKUP); |
326 | join(FILE2, FILE2 SUFFIX, FILE2 BACKUP); | |
327 | join(FILE3, FILE3 SUFFIX, FILE3 BACKUP); | |
1a43487e | 328 | join(FILE4, FILE4 SUFFIX, FILE4 BACKUP); |
31aff165 | 329 | #else |
3da0d83f | 330 | join("test", "test.new", "test.old"); |
31aff165 MM |
331 | #endif |
332 | return 0; | |
333 | } |