4 * Copyright (c) 2001 Michal Moskal <malekith@pld.org.pl>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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
41 * system database, it is left, otherwise it is added. UIDs/GIDs are *not*
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.
48 * Written for PLD Linux (http://www.pld.org.pl) setup package.
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
56 * The idea of this program comes from Lukasz Dobrek <dobrek@pld.org.pl>.
60 #include <sys/types.h>
69 #define FILE1 "/etc/passwd"
70 #define FILE2 "/etc/shadow"
71 #define FILE3 "/etc/group"
72 #define FILE4 "/etc/gshadow"
74 /* #define OLD_LOCK */
76 #define LOCK_FILE "/etc/.pwd.lock"
78 #define SUFFIX ".rpmnew"
79 /* maybe "-" or sth? */
84 void eputs(const char *msg)
86 write(2, msg, strlen(msg));
89 void fatal(const char *msg)
96 char *map_file(const char *name, int *sz)
102 fd = open(name, O_RDONLY);
107 ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
108 if (ptr == MAP_FAILED)
114 int exist(char *id, int id_len, char *ptr, int sz)
118 for (i = 0; i < sz; ) {
119 if (sz - i > id_len && memcmp(id, ptr + i, id_len + 1) == 0)
121 while (i < sz && ptr[i] != '\n')
129 void itoa(char *buf, long i)
143 for (p = tmp; i; i /= 10)
144 *p++ = (i % 10) + '0';
158 int try_lock(const char *name)
161 char file[strlen(name) + 32], lock[strlen(name) + 32];
168 strcat(lock, ".lock");
169 itoa(buf, (long)getpid());
173 fd = open(file, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
176 write(fd, buf, strlen(buf));
179 if (link(file, lock) == 0) {
184 fd = open(lock, O_RDONLY);
187 memset(buf, 0, sizeof(buf));
188 read(fd, buf, sizeof(buf));
190 if (pid == 0 || kill(pid, 0) != 0) {
195 return try_lock(name);
206 lock_fd = open(LOCK_FILE, O_RDWR|O_CREAT, 0600);
209 signal(SIGALRM, noop);
211 memset(&fl, 0, sizeof(fl));
213 fl.l_whence = SEEK_SET;
214 if (fcntl(lock_fd, F_SETLKW, &fl) != 0) {
226 void unlock(const char *name)
229 char lock[strlen(name) + 32];
232 strcat(lock, ".lock");
241 void lock(const char *name)
247 if (try_lock(name) == 0)
249 eputs("waiting for lock...\n");
252 fatal("cannot get lock");
255 int join(const char *old_name, const char *new_name, const char *backup_name)
257 char *old, *new, *id;
261 new = map_file(new_name, &new_sz);
266 old = map_file(old_name, &old_sz);
268 fatal("cannot mmap old");
270 fd = open(backup_name, O_WRONLY|O_CREAT|O_TRUNC, 0600);
272 fatal("cannot make backup");
273 write(fd, old, old_sz);
277 eputs("marging contest of `");
284 fd = open(old_name, O_WRONLY|O_APPEND);
286 for (i = 0; i < new_sz; ) {
288 while (i < new_sz && new[i] != ':' && new[i] != '\n')
290 if (i < new_sz && new[i] == ':') {
291 int id_len, line_len;
293 id_len = i - (id - new);
294 while (i < new_sz && new[i] != '\n')
298 line_len = i - (id - new);
300 if (!exist(id, id_len, old, old_sz)) {
304 write(2, id, id_len);
307 write(fd, id, line_len);
309 } else if (i < new_sz)
314 /* user may want to exime this file... */
325 join(FILE1, FILE1 SUFFIX, FILE1 BACKUP);
326 join(FILE2, FILE2 SUFFIX, FILE2 BACKUP);
327 join(FILE3, FILE3 SUFFIX, FILE3 BACKUP);
328 join(FILE4, FILE4 SUFFIX, FILE4 BACKUP);
330 join("test", "test.new", "test.old");