Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/loginutils/adduser.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/loginutils/adduser.c
r2725 r3232 8 8 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 9 9 */ 10 11 //usage:#define adduser_trivial_usage 12 //usage: "[OPTIONS] USER [GROUP]" 13 //usage:#define adduser_full_usage "\n\n" 14 //usage: "Create new user, or add USER to GROUP\n" 15 //usage: "\n -h DIR Home directory" 16 //usage: "\n -g GECOS GECOS field" 17 //usage: "\n -s SHELL Login shell" 18 //usage: "\n -G GRP Add user to existing group" 19 //usage: "\n -S Create a system user" 20 //usage: "\n -D Don't assign a password" 21 //usage: "\n -H Don't create home directory" 22 //usage: "\n -u UID User id" 23 10 24 #include "libbb.h" 11 25 … … 53 67 if (p->pw_uid == max) { 54 68 bb_error_msg_and_die("no %cids left", 'u'); 69 /* this format string is reused in adduser and addgroup */ 55 70 } 56 71 p->pw_uid++; … … 66 81 } 67 82 68 static void addgroup_wrapper(struct passwd *p, const char *group_name) 69 { 70 char *cmd; 71 72 if (group_name) /* Add user to existing group */ 73 cmd = xasprintf("addgroup '%s' '%s'", p->pw_name, group_name); 74 else /* Add user to his own group with the first free gid found in passwd_study */ 75 cmd = xasprintf("addgroup -g %u '%s'", (unsigned)p->pw_gid, p->pw_name); 76 /* Warning: to be compatible with external addgroup programs we should use --gid instead */ 77 system(cmd); 78 free(cmd); 79 } 80 81 static void passwd_wrapper(const char *login) NORETURN; 82 83 static void passwd_wrapper(const char *login) 84 { 85 BB_EXECLP("passwd", "passwd", login, NULL); 83 static int addgroup_wrapper(struct passwd *p, const char *group_name) 84 { 85 char *argv[6]; 86 87 argv[0] = (char*)"addgroup"; 88 if (group_name) { 89 /* Add user to existing group */ 90 argv[1] = (char*)"--"; 91 argv[2] = p->pw_name; 92 argv[3] = (char*)group_name; 93 argv[4] = NULL; 94 } else { 95 /* Add user to his own group with the first free gid 96 * found in passwd_study. 97 */ 98 #if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS || !ENABLE_ADDGROUP 99 /* We try to use --gid, not -g, because "standard" addgroup 100 * has no short option -g, it has only long --gid. 101 */ 102 argv[1] = (char*)"--gid"; 103 #else 104 /* Breaks if system in fact does NOT use busybox addgroup */ 105 argv[1] = (char*)"-g"; 106 #endif 107 argv[2] = utoa(p->pw_gid); 108 argv[3] = (char*)"--"; 109 argv[4] = p->pw_name; 110 argv[5] = NULL; 111 } 112 113 return spawn_and_wait(argv); 114 } 115 116 static void passwd_wrapper(const char *login_name) NORETURN; 117 118 static void passwd_wrapper(const char *login_name) 119 { 120 BB_EXECLP("passwd", "passwd", "--", login_name, NULL); 86 121 bb_error_msg_and_die("can't execute passwd, you must set password manually"); 87 122 } … … 124 159 125 160 pw.pw_gecos = (char *)"Linux User,,,"; 126 pw.pw_shell = (char *)DEFAULT_SHELL; 161 /* We assume that newly created users "inherit" root's shell setting */ 162 pw.pw_shell = (char *)get_shell_name(); 127 163 pw.pw_dir = NULL; 128 164 129 /* exactly one non-option arg*/165 /* at most two non-option args */ 130 166 /* disable interactive passwd for system accounts */ 131 opt_complementary = " =1:SD:u+";167 opt_complementary = "?2:SD:u+"; 132 168 if (sizeof(pw.pw_uid) == sizeof(int)) { 133 169 opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid); … … 140 176 } 141 177 argv += optind; 178 pw.pw_name = argv[0]; 179 180 if (!opts && argv[1]) { 181 /* if called with two non-option arguments, adduser 182 * will add an existing user to an existing group. 183 */ 184 return addgroup_wrapper(&pw, argv[1]); 185 } 142 186 143 187 /* fill in the passwd struct */ 144 pw.pw_name = argv[0];145 188 die_if_bad_username(pw.pw_name); 146 189 if (!pw.pw_dir) { … … 170 213 if (ENABLE_FEATURE_CLEAN_UP) 171 214 free(p); 172 173 215 #if ENABLE_FEATURE_SHADOWPASSWDS 174 216 /* /etc/shadow fields: … … 204 246 /* New home. Copy /etc/skel to it */ 205 247 const char *args[] = { 206 "chown", "-R", 248 "chown", 249 "-R", 207 250 xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid), 208 pw.pw_dir, NULL 251 pw.pw_dir, 252 NULL 209 253 }; 210 254 /* Be silent on any errors (like: no /etc/skel) */
Note:
See TracChangeset
for help on using the changeset viewer.