]> git.pld-linux.org Git - projects/setup.git/commitdiff
- grouprename replaced by joinpasswd+delpasswd combo
authorJan Rękorajski <baggins@pld-linux.org>
Sun, 1 Mar 2009 23:57:46 +0000 (23:57 +0000)
committerJan Rękorajski <baggins@pld-linux.org>
Sun, 1 Mar 2009 23:57:46 +0000 (23:57 +0000)
Changed files:
    Makefile -> 1.26
    delpasswd.c -> 1.1
    grouprename.c -> 1.3

Makefile
delpasswd.c [moved from grouprename.c with 68% similarity]

index 6e038e60516f7d8c9426e667a1411e9bd977b8da..ae7ab9f807c05396eefd3794830b14690413ea9f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ ENVDIR                = $(ETCDIR)/env.d
 SHRCDIR                = $(ETCDIR)/shrc.d
 #########################################
 
-BIN_FILES      = joinpasswd update-fstab postshell grouprename
+BIN_FILES      = joinpasswd update-fstab postshell delpasswd
 DOCS           = ChangeLog
 ETC_FILES      = filesystems fstab group host.conf hosts motd mtab \
            netgroup passwd profile protocols resolv.conf securetty \
@@ -23,7 +23,7 @@ PROFILE_FILES = tmp-dir.csh tmp-dir.sh
 ENV_FILES      = EDITOR HISTFILESIZE HOME_ETC MAILCHECK MAILPATH \
            NNTPSERVER ORGANIZATION TMOUT VISUAL
 
-SOURCES                = joinpasswd.c update-fstab.c postshell.c grouprename.c
+SOURCES                = joinpasswd.c update-fstab.c postshell.c delpasswd.c
 CLEAN          = $(BIN_FILES) *.o core *.tar.gz *.tar.bz2 *~ *.swp
 
 #########################################
@@ -32,7 +32,7 @@ all: $(BIN_FILES)
 joinpasswd: joinpasswd.o
 update-fstab: update-fstab.o
 postshell: postshell.o
-grouprename: grouprename.o
+dellpasswd: dellpasswd.o
 
 .c.o:
        $(CC) $(CPPFLAGS) $(CFLAGS) $(CDEFS) -c $< -o $@
similarity index 68%
rename from grouprename.c
rename to delpasswd.c
index 447577dc00065aa772e1416562ccb15dcf1d274e..d83a9b7d7254eaf39e1d2d46dcf12f466919675f 100644 (file)
  *
  *
  * 
- * USAGE: grouprename old-name new-name
+ * USAGE: delpasswd [-u|-g] name1 name2 ...
  *
- * Change the name of a group from old-name to new-name,
- * It is usable as part of setup package, during upgrade in case a group name
- * has to change. If new-name is already found in system database or old-name
- * is not found, nothing changes,. UIDs/GIDs are *not* checked anyhow. 
+ * Remove specified groups from /etc/{passwd,shadow,group}.
+ * It is usable as part of setup package, during upgrade where some system
+ * users/groups should be removed. UIDs/GIDs are *not* checked anyhow. 
  *
  * Written for PLD Linux (http://www.pld-linux.org/) setup package.
  *
  * Compilation against uClibc:
  * UCROOT=/usr/lib/bootdisk/usr
- * gcc -I$UCROOT/include -nostdlib -O2 grouprename.c $UCROOT/lib/crt0.o \
- *             $UCROOT/lib/libc.a -lgcc -o grouprename
- * strip -R .comment -R .note grouprename
+ * gcc -I$UCROOT/include -nostdlib -O2 delpasswd.c $UCROOT/lib/crt0.o \
+ *             $UCROOT/lib/libc.a -lgcc -o delpasswd
+ * strip -R .comment -R .note delpasswd
  *
- * Shamelss ripoff of the joinpasswd.c program
- * 
  */
 
 #include <sys/types.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdlib.h>
-#include <stdio.h> // for rename(2)
 #include <string.h>
 #include <signal.h>
 
+#if 0
+#define FILE1 "passwd"
+#define FILE2 "shadow"
+#define FILE3 "group"
+#define FILE4 "gshadow"
+#else
+#define FILE1 "/etc/passwd"
+#define FILE2 "/etc/shadow"
 #define FILE3 "/etc/group"
 #define FILE4 "/etc/gshadow"
+#endif
 
 /* #define OLD_LOCK */
 
@@ -87,7 +92,7 @@ void fatal(const char *msg)
        exit(1);
 }
 
-char *map_file(const char *name, int *sz, int *mfd)
+char *map_file(const char *name, int *sz)
 {
        int fd;
        void *ptr;
@@ -96,8 +101,8 @@ char *map_file(const char *name, int *sz, int *mfd)
        fd = open(name, O_RDONLY);
        if (fd == -1)
                return NULL;
-       *mfd = fd;
-       fstat(fd, &st);
+       if (fstat(fd, &st) < 0)
+               return NULL;
        *sz = st.st_size;
        ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
        if (ptr == MAP_FAILED)
@@ -106,16 +111,13 @@ char *map_file(const char *name, int *sz, int *mfd)
        return ptr;
 }
 
-int exist(char *id, int id_len, char *ptr, int sz)
+int exist(char *id, int id_len, int namesc, const char **names)
 {
        int i;
 
-       for (i = 0; i < sz; ) {
-               if (sz - i > id_len && memcmp(id, ptr, id_len) == 0)
+       for (i = 0; i < namesc; i++) {
+               if (strlen(names[i]) == id_len && memcmp(id, names[i], id_len) == 0)
                        return 1;
-               while (i < sz && ptr[i] != '\n')
-                       i++;
-               i++;
        }
 
        return 0;
@@ -247,96 +249,102 @@ void lock(const char *name)
        fatal("cannot get lock");
 }
 
-int join(const char *oldgr, const char *newgr, const char *name, const char *backup)
+int delp(const char *old_name, const char *backup_name,
+               int namesc, const char **names)
 {
-       char *old, *id;
-       int i, fd, mfd;
-       int sz;
+       char *old, *tmp, *id;
+       int i, fd;
+       int old_sz;
 
-//     lock(name);
-       old = map_file(name, &sz, &mfd);
-       if (old == NULL)
-               fatal("cannot mmap old");
+       // Fail silently if file does not exist
+       if (access(old_name, F_OK) == -1)
+               return -1;
 
-       fd = open(backup, O_WRONLY|O_CREAT|O_TRUNC, 0600);
+       lock(old_name);
+       tmp = map_file(old_name, &old_sz);
+       if (tmp == NULL)
+               fatal("cannot mmap old for backup");
+       
+       fd = open(backup_name, O_WRONLY|O_CREAT|O_TRUNC, 0600);
        if (fd < 0)
                fatal("cannot make backup");
-       write(fd, old, sz);
+       write(fd, tmp, old_sz);
        close(fd);
-
-       munmap(old, sz);
-       close(mfd);
-
+       
+       old = map_file(backup_name, &old_sz);
+       if (old == NULL)
+               fatal("cannot mmap old");
+       
 #ifndef SILENT
-       eputs("working on `");
-       eputs(name);
+       eputs("removing from `");
+       eputs(old_name);
        eputs("'\n");
 #endif /* SILENT */
 
-       old = map_file(backup, &sz, &mfd);
-       if (old == NULL)
-               fatal("cannot mmap backup");
-
-       fd = open(name, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+       fd = open(old_name, O_WRONLY|O_TRUNC);
        if (fd < 0)
-               fatal("cannot open new file");
+               fatal("cannot open old file");
        
-       for (i = 0; i < sz; ) {
+       for (i = 0; i < old_sz; ) {
                id = old + i;
-               while (i < sz && old[i] != ':' && old[i] != '\n')
+               while (i < old_sz && old[i] != ':' && old[i] != '\n')
                        i++;
-               if (i < sz && old[i] == ':') {
+               if (i < old_sz && old[i] == ':') {
                        int id_len, line_len;
-                       char *rem = old + i;
 
                        id_len = i - (id - old);
-                       while (i < sz && old[i] != '\n')
+                       while (i < old_sz && old[i] != '\n')
                                i++;
-                       if (i < sz)
+                       if (i < old_sz)
                                i++;
-                       line_len = i - (rem - old);
-
-                       if (id_len == strlen(oldgr) &&
-                                       memcmp(id, oldgr, id_len) == 0) {
+                       line_len = i - (id - old);
+                       
+                       if (!exist(id, id_len, namesc, names)) {
+                               write(fd, id, line_len);
+                       }
 #ifndef SILENT
-                               eputs(oldgr);
-                               eputs(": renaming to `");
-                               eputs(newgr);
+                       else {
+                               eputs(old_name);
+                               eputs(": removing `");
+                               write(2, id, id_len);
                                eputs("'\n");
+                       }
 #endif /* SILENT */
-                               write(fd, newgr, strlen(newgr));
-                       } else
-                               write(fd, id, id_len);
-                       write(fd, rem, line_len);
-               } else if (i < sz)
+               } else if (i < old_sz)
                        i++;
        }
-       close(fd);
 
-       munmap(old, sz);
-       close(mfd);
-
-//     unlock(name);
+       close(fd);
+       unlock(old_name);
 
        return 0;
 }
 
-int main(int argc, const char *argv[])
+int main(int argc, const char **argv)
 {
-       const char *oldgr, *newgr;
-
-       if (argc != 3) {
-               eputs("Usage: grouprename old-name new-name");
-               return 1;
+       int what = 0;
+
+       if (argc < 3)
+               fatal("Usage: delpasswd [-u|-g] name1 name2 ... nameN");
+
+       if (strncmp(argv[1], "-u", 2) == 0)
+               what = 1;
+       else if (strncmp(argv[1], "-g", 2) == 0)
+               what = 2;
+
+       if (what == 0)
+               fatal("Usage: delpasswd [-u|-g] name1 name2 ... nameN");
+#if 1
+       if (what == 1) {
+               delp(FILE1, FILE1 BACKUP, argc-2, argv+2);
+               delp(FILE2, FILE2 BACKUP, argc-2, argv+2);
+       }
+       if (what == 2) {
+               delp(FILE3, FILE3 BACKUP, argc-2, argv+2);
+               delp(FILE4, FILE4 BACKUP, argc-2, argv+2);
        }
-       oldgr = argv[1];
-       newgr = argv[2];
-
-#if 0
-       join(oldgr, newgr, FILE3, FILE3 BACKUP);
-       join(oldgr, newgr, FILE4, FILE4 BACKUP);
 #else
-       join(oldgr, newgr, "test", "test.old");
+       delp("test", "test.old", argc-2, argv+2);
 #endif
        return 0;
 }
This page took 0.248374 seconds and 4 git commands to generate.