3 * Copyright (c) 2001 Michal Moskal <malekith@pld-linux.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Michal Moskal.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY MICHAL MOSKAL AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Add entries from freshly created {passwd,shadow,group}.rpmnew to existing
38 * {passwd,shadow,group}. It is usable as part of setup package, during upgrade
39 * where new system user/group is to be added. If entry is already found in
40 * system database, it is left, otherwise it is added. UIDs/GIDs are *not*
43 * For typical sizes of files in setup package, it takes about 1 second per
44 * 20000 users in system database on Pentium class machine. After static link
45 * against uClibc it is under 2k on x86. Stdio hasn't been used intentionally.
47 * Written for PLD Linux (http://www.pld-linux.org/) setup package.
49 * Compilation against uClibc:
50 * UCROOT=/usr/lib/bootdisk/usr
51 * gcc -I$UCROOT/include -nostdlib -O2 joinpasswd.c $UCROOT/lib/crt0.o \
52 * $UCROOT/lib/libc.a -lgcc -o joinpasswd
53 * strip -R .comment -R .note joinpasswd
55 * The idea of this program comes from Lukasz Dobrek <dobrek@pld-linux.org>.
59 #include <sys/types.h>
71 #define FILE1 "/etc/passwd"
72 #define FILE2 "/etc/shadow"
73 #define FILE3 "/etc/group"
74 #define FILE4 "/etc/gshadow"
76 /* #define OLD_LOCK */
78 #define LOCK_FILE "/etc/.pwd.lock"
80 #define SUFFIX ".rpmnew"
81 /* maybe "-" or sth? */
86 void eputs(const char *fmt, ...)
91 vfprintf(stderr, fmt, args);
95 void fatal(const char *fmt, ...)
107 char *map_file(const char *name, ssize_t *sz)
113 fd = open(name, O_RDONLY);
116 if (fstat(fd, &st) < 0)
119 ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
120 if (ptr == MAP_FAILED)
126 int exist(char *id, int id_len, char *ptr, ssize_t sz)
130 for (i = 0; i < sz; ) {
131 if (sz - i > id_len && memcmp(id, ptr + i, id_len + 1) == 0)
133 while (i < sz && ptr[i] != '\n')
141 void itoa(char *buf, long i)
155 for (p = tmp; i; i /= 10)
156 *p++ = (i % 10) + '0';
170 int try_lock(const char *name)
173 char file[strlen(name) + 32], lock[strlen(name) + 32];
180 strcat(lock, ".lock");
181 itoa(buf, (long)getpid());
185 fd = open(file, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
188 if (write(fd, buf, strlen(buf)) < 0) {
195 if (link(file, lock) == 0) {
200 fd = open(lock, O_RDONLY);
203 memset(buf, 0, sizeof(buf));
204 read(fd, buf, sizeof(buf));
206 if (pid == 0 || kill(pid, 0) != 0) {
211 return try_lock(name);
222 lock_fd = open(LOCK_FILE, O_RDWR|O_CREAT, 0600);
225 signal(SIGALRM, noop);
227 memset(&fl, 0, sizeof(fl));
229 fl.l_whence = SEEK_SET;
230 if (fcntl(lock_fd, F_SETLKW, &fl) != 0) {
242 void unlock(const char *name)
245 char lock[strlen(name) + 32];
248 strcat(lock, ".lock");
257 void lock(const char *name)
263 if (try_lock(name) == 0)
265 eputs("waiting for lock...\n");
268 fatal("cannot get lock");
271 int join(const char *old_name, const char *new_name, const char *backup_name)
273 char *old, *new, *id;
275 ssize_t old_sz, new_sz;
277 new = map_file(new_name, &new_sz);
282 old = map_file(old_name, &old_sz);
284 fatal("cannot mmap file `%s': %m", old_name);
286 fd = open(backup_name, O_WRONLY|O_CREAT|O_TRUNC, 0600);
288 fatal("cannot create backup file `%s': %m", backup_name);
289 if (write(fd, old, old_sz) < 0)
290 fatal("writting to backup file `%s' failed: %m", backup_name);
292 fatal("syncing backup file `%s' failed: %m", backup_name);
294 fatal("closing backup file `%s' failed: %m", backup_name);
297 eputs("merging content of `");
304 fd = open(old_name, O_WRONLY|O_APPEND);
306 fatal("cannot open old file `%s': %m", old_name);
308 for (i = 0; i < new_sz; ) {
310 while (i < new_sz && new[i] != ':' && new[i] != '\n')
312 if (i < new_sz && new[i] == ':') {
313 int id_len, line_len;
315 id_len = i - (id - new);
316 while (i < new_sz && new[i] != '\n')
320 line_len = i - (id - new);
322 if (!exist(id, id_len, old, old_sz)) {
326 write(2, id, id_len);
329 if (write(fd, id, line_len) < 0)
330 fatal("writting line to `%s' failed, check backup file: %m", old_name);
332 } else if (i < new_sz)
339 /* user may want to exime this file... */
350 join(FILE1, FILE1 SUFFIX, FILE1 BACKUP);
351 join(FILE2, FILE2 SUFFIX, FILE2 BACKUP);
352 join(FILE3, FILE3 SUFFIX, FILE3 BACKUP);
353 join(FILE4, FILE4 SUFFIX, FILE4 BACKUP);
355 join("test", "test.new", "test.old");