3 * Copyright (c) 2001 Michal Moskal <malekith@pld-linux.org>.
4 * Copyright (c) 2009 Jan Rękorajski <baggins@pld-linux.org>.
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
36 * USAGE: delpasswd [-u|-g] name1 name2 ...
38 * Remove specified groups from /etc/{passwd,shadow,group}.
39 * It is usable as part of setup package, during upgrade where some system
40 * users/groups should be removed. UIDs/GIDs are *not* checked anyhow.
42 * Written for PLD Linux (http://www.pld-linux.org/) setup package.
44 * Compilation against uClibc:
45 * UCROOT=/usr/lib/bootdisk/usr
46 * gcc -I$UCROOT/include -nostdlib -O2 delpasswd.c $UCROOT/lib/crt0.o \
47 * $UCROOT/lib/libc.a -lgcc -o delpasswd
48 * strip -R .comment -R .note delpasswd
52 #include <sys/types.h>
62 #define FILE1 "passwd"
63 #define FILE2 "shadow"
65 #define FILE4 "gshadow"
67 #define FILE1 "/etc/passwd"
68 #define FILE2 "/etc/shadow"
69 #define FILE3 "/etc/group"
70 #define FILE4 "/etc/gshadow"
73 /* #define OLD_LOCK */
75 #define LOCK_FILE "/etc/.pwd.lock"
77 /* maybe "-" or sth? */
82 void eputs(const char *msg)
84 write(2, msg, strlen(msg));
87 void fatal(const char *msg)
94 char *map_file(const char *name, ssize_t *sz)
100 fd = open(name, O_RDONLY);
103 if (fstat(fd, &st) < 0)
106 ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
107 if (ptr == MAP_FAILED)
113 int exist(char *id, int id_len, int namesc, const char **names)
117 for (i = 0; i < namesc; i++) {
118 if (strlen(names[i]) == id_len && memcmp(id, names[i], id_len) == 0)
125 void itoa(char *buf, long i)
139 for (p = tmp; i; i /= 10)
140 *p++ = (i % 10) + '0';
154 int try_lock(const char *name)
157 char file[strlen(name) + 32], lock[strlen(name) + 32];
164 strcat(lock, ".lock");
165 itoa(buf, (long)getpid());
169 fd = open(file, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
172 write(fd, buf, strlen(buf));
175 if (link(file, lock) == 0) {
180 fd = open(lock, O_RDONLY);
183 memset(buf, 0, sizeof(buf));
184 read(fd, buf, sizeof(buf));
186 if (pid == 0 || kill(pid, 0) != 0) {
191 return try_lock(name);
202 lock_fd = open(LOCK_FILE, O_RDWR|O_CREAT, 0600);
205 signal(SIGALRM, noop);
207 memset(&fl, 0, sizeof(fl));
209 fl.l_whence = SEEK_SET;
210 if (fcntl(lock_fd, F_SETLKW, &fl) != 0) {
222 void unlock(const char *name)
225 char lock[strlen(name) + 32];
228 strcat(lock, ".lock");
237 void lock(const char *name)
243 if (try_lock(name) == 0)
245 eputs("waiting for lock...\n");
248 fatal("cannot get lock");
251 int verifyp(const char *old_name, int namesc, const char **names)
256 // Fail silently if file does not exist
257 if (access(old_name, F_OK) == -1)
260 old = map_file(old_name, &old_sz);
262 fatal("cannot mmap old");
264 for (i = 0; i < old_sz; ) {
266 while (i < old_sz && old[i] != ':' && old[i] != '\n')
268 if (i < old_sz && old[i] == ':') {
271 id_len = i - (id - old);
272 while (i < old_sz && old[i] != '\n')
277 if (exist(id, id_len, namesc, names))
279 } else if (i < old_sz)
285 int delp(const char *old_name, const char *backup_name,
286 int namesc, const char **names)
288 char *old, *tmp, *id;
292 // Fail silently if file does not exist
293 if (access(old_name, F_OK) == -1)
297 tmp = map_file(old_name, &old_sz);
299 fatal("cannot mmap old for backup");
301 fd = open(backup_name, O_WRONLY|O_CREAT|O_TRUNC, 0600);
303 fatal("cannot make backup");
304 if (write(fd, tmp, old_sz) < 0)
305 fatal("writting to backup failed");
308 old = map_file(backup_name, &old_sz);
310 fatal("cannot mmap old");
313 eputs("removing from `");
318 fd = open(old_name, O_WRONLY|O_TRUNC);
320 fatal("cannot open old file");
322 for (i = 0; i < old_sz; ) {
324 while (i < old_sz && old[i] != ':' && old[i] != '\n')
326 if (i < old_sz && old[i] == ':') {
327 int id_len, line_len;
329 id_len = i - (id - old);
330 while (i < old_sz && old[i] != '\n')
334 line_len = i - (id - old);
336 if (!exist(id, id_len, namesc, names)) {
337 write(fd, id, line_len);
342 eputs(": removing `");
343 write(2, id, id_len);
347 } else if (i < old_sz)
357 int main(int argc, const char **argv)
362 fatal("Usage: delpasswd [-u|-g] name1 name2 ... nameN");
364 if (strncmp(argv[1], "-u", 2) == 0)
366 else if (strncmp(argv[1], "-g", 2) == 0)
370 fatal("Usage: delpasswd [-u|-g] name1 name2 ... nameN");
373 if (verifyp(FILE1, argc-2, argv+2))
374 delp(FILE1, FILE1 BACKUP, argc-2, argv+2);
375 if (verifyp(FILE2, argc-2, argv+2))
376 delp(FILE2, FILE2 BACKUP, argc-2, argv+2);
379 if (verifyp(FILE3, argc-2, argv+2))
380 delp(FILE3, FILE3 BACKUP, argc-2, argv+2);
381 if (verifyp(FILE4, argc-2, argv+2))
382 delp(FILE4, FILE4 BACKUP, argc-2, argv+2);
385 delp("test", "test.old", argc-2, argv+2);