useradd.c (3021B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef _XOPEN_SOURCE 3 #define _XOPEN_SOURCE 700 4 #endif 5 #ifndef _DEFAULT_SOURCE 6 #define _DEFAULT_SOURCE 7 #endif 8 #include <errno.h> 9 #include <ftw.h> 10 #include <limits.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <sys/stat.h> 15 #include <unistd.h> 16 17 #include "accounts.h" 18 #include "fs.h" 19 #include "util.h" 20 21 #define SKEL "/etc/skel" 22 23 static uid_t owner; 24 25 static void 26 usage(void) 27 { 28 eprintf("usage: %s [-m] [-R root] [-d home] [-s shell] [-p hash] [-G group,...] name\n", argv0); 29 } 30 31 static int 32 own(const char *path, const struct stat *st, int type, struct FTW *ftw) 33 { 34 (void)st; 35 (void)type; 36 (void)ftw; 37 return lchown(path, owner, owner) < 0 ? -1 : 0; 38 } 39 40 /* the home: the skeleton copied, or an empty folder, all the person's */ 41 static void 42 make_home(const char *home, uid_t uid) 43 { 44 struct stat st; 45 46 if (stat(SKEL, &st) == 0 && S_ISDIR(st.st_mode)) { 47 cp_rflag = 1; 48 cp_pflag = 1; 49 if (cp(SKEL, home, 0) < 0 || cp_status) 50 eprintf("%s: the skeleton could not be copied\n", home); 51 } else if (mkdir(home, 0755) < 0 && errno != EEXIST) { 52 eprintf("mkdir %s:", home); 53 } 54 owner = uid; 55 if (nftw(home, own, 16, FTW_PHYS) < 0) 56 eprintf("chown %s:", home); 57 } 58 59 // ?man useradd: add a person 60 // ?man arguments: [-m] [-R root] [-d home] [-s shell] [-p hash] [-G group,...] name 61 // ?man adds name to the account files with the next free id from 1000 and a group of its own of the same id 62 int 63 main(int argc, char *argv[]) 64 { 65 char *root = NULL, *home = NULL, *shell = "/bin/sh", *hash = "!", *groups = NULL; 66 char buf[PATH_MAX], *g, *save; 67 int make = 0; 68 uid_t uid; 69 70 ARGBEGIN 71 { 72 // ?man -m: make the home from /etc/skel 73 case 'm': 74 make = 1; 75 break; 76 // ?man -R:str: work in another root 77 case 'R': 78 root = EARGF(usage()); 79 break; 80 // ?man -d:str: the home, /home/name otherwise 81 case 'd': 82 home = EARGF(usage()); 83 break; 84 // ?man -s:str: the shell, /bin/sh otherwise 85 case 's': 86 shell = EARGF(usage()); 87 break; 88 // ?man -p:str: the password as crypt(3) gives it, locked otherwise 89 case 'p': 90 hash = EARGF(usage()); 91 break; 92 // ?man -G:str: the groups the person is put in, apart by commas 93 case 'G': 94 groups = EARGF(usage()); 95 break; 96 default: 97 usage(); 98 } 99 ARGEND; 100 if (argc != 1) 101 usage(); 102 if (root && (chroot(root) < 0 || chdir("/") < 0)) 103 eprintf("chroot %s:", root); 104 if (!home) { 105 snprintf(buf, sizeof(buf), "/home/%s", argv[0]); 106 home = buf; 107 } 108 if (acct_add(argv[0], home, shell, hash, &uid) < 0) 109 eprintf( 110 errno == EEXIST ? "%s: there is one already\n" 111 : errno == EINVAL ? "%s: not a name\n" 112 : "%s:", 113 argv[0] 114 ); 115 for (g = groups ? strtok_r(groups, ",", &save) : NULL; g; g = strtok_r(NULL, ",", &save)) 116 if (acct_member(g, argv[0], 1) < 0) 117 weprintf("%s:", g); 118 if (make) 119 make_home(home, uid); 120 return 0; 121 }