]> git.pld-linux.org Git - projects/setup.git/blob - joinpasswd.c
- use neutral timezone when generating changelogs
[projects/setup.git] / joinpasswd.c
1 /*
2  * $Id$
3  * 
4  * Copyright (c) 2001 Michal Moskal <malekith@pld-linux.org>.
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 
41  * system database, it is left, otherwise it is added. UIDs/GIDs are *not* 
42  * checked anyhow. 
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  * 
48  * Written for PLD Linux (http://www.pld-linux.org/) setup package.
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  *
56  * The idea of this program comes from Lukasz Dobrek <dobrek@pld-linux.org>.
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>
67 #include <signal.h>
68
69 #define FILE1 "/etc/passwd"
70 #define FILE2 "/etc/shadow"
71 #define FILE3 "/etc/group"
72 #define FILE4 "/etc/gshadow"
73
74 /* #define OLD_LOCK */
75
76 #define LOCK_FILE "/etc/.pwd.lock"
77
78 #define SUFFIX ".rpmnew"
79 /* maybe "-" or sth? */
80 #define BACKUP ".old"
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         if (fstat(fd, &st) < 0)
106                 return NULL;
107         *sz = st.st_size;
108         ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
109         if (ptr == MAP_FAILED)
110                 return NULL;
111
112         return ptr;
113 }
114
115 int exist(char *id, int id_len, char *ptr, int sz)
116 {
117         int i;
118
119         for (i = 0; i < sz; ) {
120                 if (sz - i > id_len && memcmp(id, ptr + i, id_len + 1) == 0)
121                         return 1;
122                 while (i < sz && ptr[i] != '\n')
123                         i++;
124                 i++;
125         }
126
127         return 0;
128 }
129
130 void itoa(char *buf, long i)
131 {
132         char tmp[32];
133         char *p;
134
135         if (i < 0) {
136                 strcpy(buf, "-");
137                 buf++;
138                 i = -i;
139         }
140         if (i == 0) {
141                 strcpy(buf, "0"); 
142                 return;
143         }
144         for (p = tmp; i; i /= 10)
145                 *p++ = (i % 10) + '0';
146         while (p > tmp)
147                 *buf++ = *--p;
148         *buf = 0;
149 }
150
151 #ifndef OLD_LOCK
152 int lock_fd = -1;
153 void noop(int x)
154 {
155         (void)x;
156 }
157 #endif
158
159 int try_lock(const char *name)
160 {
161 #ifdef OLD_LOCK
162         char file[strlen(name) + 32], lock[strlen(name) + 32];
163         char buf[32];
164         int fd;
165         long pid;
166
167         strcpy(lock, name);
168         strcpy(file, name);
169         strcat(lock, ".lock");
170         itoa(buf, (long)getpid());
171         strcat(file, ".");
172         strcat(file, buf);
173         
174         fd = open(file, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
175         if (fd < 0)
176                 return -1;
177         write(fd, buf, strlen(buf));
178         close(fd);
179
180         if (link(file, lock) == 0) {
181                 unlink(file);
182                 return 0;
183         }
184
185         fd = open(lock, O_RDONLY);
186         if (fd < 0)
187                 goto oops;
188         memset(buf, 0, sizeof(buf));
189         read(fd, buf, sizeof(buf));
190         pid = atol(buf);
191         if (pid == 0 || kill(pid, 0) != 0) {
192                 /* stale lock */
193                 unlink(file);
194                 unlink(lock);
195                 /* try again */
196                 return try_lock(name);
197         }
198
199 oops:
200         unlink(file);
201         return -1;
202 #else
203         struct flock fl;
204         
205         if (lock_fd != -1)
206                 return -1;
207         lock_fd = open(LOCK_FILE, O_RDWR|O_CREAT, 0600);
208         if (lock_fd == -1)
209                 return -1;
210         signal(SIGALRM, noop);
211         alarm(15);
212         memset(&fl, 0, sizeof(fl));
213         fl.l_type = F_WRLCK;
214         fl.l_whence = SEEK_SET;
215         if (fcntl(lock_fd, F_SETLKW, &fl) != 0) {
216                 alarm(0);
217                 close(lock_fd);
218                 lock_fd = -1;
219                 return -1;
220         }
221         alarm(0);
222         
223         return 0;
224 #endif
225 }
226
227 void unlock(const char *name)
228 {
229 #ifdef OLD_LOCK
230         char lock[strlen(name) + 32];
231
232         strcpy(lock, name);
233         strcat(lock, ".lock");
234         unlink(lock);
235 #else
236         if (lock_fd != -1)
237                 close(lock_fd);
238         lock_fd = -1;
239 #endif
240 }
241
242 void lock(const char *name)
243 {
244         int n;
245
246         n = 5;
247         while (n--) {
248                 if (try_lock(name) == 0)
249                         return;
250                 eputs("waiting for lock...\n");
251                 sleep(1);
252         }
253         fatal("cannot get lock");
254 }
255
256 int join(const char *old_name, const char *new_name, const char *backup_name)
257 {
258         char *old, *new, *id;
259         int i, fd;
260         int old_sz, new_sz;
261
262         new = map_file(new_name, &new_sz);
263         if (new == NULL)
264                 return -1;
265         
266         lock(old_name);
267         old = map_file(old_name, &old_sz);
268         if (old == NULL)
269                 fatal("cannot mmap old");
270         
271         fd = open(backup_name, O_WRONLY|O_CREAT|O_TRUNC, 0600);
272         if (fd < 0)
273                 fatal("cannot make backup");
274         write(fd, old, old_sz);
275         close(fd);
276         
277 #ifndef SILENT
278         eputs("merging content of `");
279         eputs(old_name);
280         eputs("' with `");
281         eputs(new_name);
282         eputs("'\n");
283 #endif /* SILENT */
284
285         fd = open(old_name, O_WRONLY|O_APPEND);
286         if (fd < 0)
287                 fatal("cannot open old file");
288         
289         for (i = 0; i < new_sz; ) {
290                 id = new + i;
291                 while (i < new_sz && new[i] != ':' && new[i] != '\n')
292                         i++;
293                 if (i < new_sz && new[i] == ':') {
294                         int id_len, line_len;
295
296                         id_len = i - (id - new);
297                         while (i < new_sz && new[i] != '\n')
298                                 i++;
299                         if (i < new_sz)
300                                 i++;
301                         line_len = i - (id - new);
302                         
303                         if (!exist(id, id_len, old, old_sz)) {
304 #ifndef SILENT
305                                 eputs(old_name);
306                                 eputs(": adding `");
307                                 write(2, id, id_len);
308                                 eputs("'\n");
309 #endif /* SILENT */
310                                 write(fd, id, line_len);
311                         }
312                 } else if (i < new_sz)
313                         i++;
314         }
315
316 #if 0
317         /* user may want to exime this file... */
318         unlink(new_name);
319 #endif
320         unlock(old_name);
321
322         return 0;
323 }
324
325 int main()
326 {
327 #if 1
328         join(FILE1, FILE1 SUFFIX, FILE1 BACKUP);
329         join(FILE2, FILE2 SUFFIX, FILE2 BACKUP);
330         join(FILE3, FILE3 SUFFIX, FILE3 BACKUP);
331         join(FILE4, FILE4 SUFFIX, FILE4 BACKUP);
332 #else
333         join("test", "test.new", "test.old");
334 #endif
335         return 0;
336 }
This page took 0.067179 seconds and 3 git commands to generate.