[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * adduser - add users to /etc/passwd and /etc/shadow
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 1999 by Lineo, inc. and John Beppu
|
---|
| 6 | * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
|
---|
| 7 | *
|
---|
[2725] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 9 | */
|
---|
[3232] | 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 |
|
---|
[1765] | 24 | #include "libbb.h"
|
---|
[821] | 25 |
|
---|
[2725] | 26 | #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
|
---|
| 27 | #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
|
---|
| 28 | #endif
|
---|
| 29 |
|
---|
| 30 | /* #define OPT_HOME (1 << 0) */ /* unused */
|
---|
| 31 | /* #define OPT_GECOS (1 << 1) */ /* unused */
|
---|
| 32 | #define OPT_SHELL (1 << 2)
|
---|
| 33 | #define OPT_GID (1 << 3)
|
---|
[1765] | 34 | #define OPT_DONT_SET_PASS (1 << 4)
|
---|
[2725] | 35 | #define OPT_SYSTEM_ACCOUNT (1 << 5)
|
---|
[1765] | 36 | #define OPT_DONT_MAKE_HOME (1 << 6)
|
---|
[2725] | 37 | #define OPT_UID (1 << 7)
|
---|
[821] | 38 |
|
---|
[2725] | 39 | /* We assume UID_T_MAX == INT_MAX */
|
---|
[821] | 40 | /* remix */
|
---|
[2725] | 41 | /* recoded such that the uid may be passed in *p */
|
---|
| 42 | static void passwd_study(struct passwd *p)
|
---|
[821] | 43 | {
|
---|
[2725] | 44 | int max = UINT_MAX;
|
---|
[821] | 45 |
|
---|
[2725] | 46 | if (getpwnam(p->pw_name)) {
|
---|
| 47 | bb_error_msg_and_die("%s '%s' in use", "user", p->pw_name);
|
---|
| 48 | /* this format string is reused in adduser and addgroup */
|
---|
| 49 | }
|
---|
[821] | 50 |
|
---|
[2725] | 51 | if (!(option_mask32 & OPT_UID)) {
|
---|
| 52 | if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
|
---|
| 53 | p->pw_uid = CONFIG_FIRST_SYSTEM_ID;
|
---|
| 54 | max = CONFIG_LAST_SYSTEM_ID;
|
---|
| 55 | } else {
|
---|
| 56 | p->pw_uid = CONFIG_LAST_SYSTEM_ID + 1;
|
---|
| 57 | max = 64999;
|
---|
[821] | 58 | }
|
---|
[2725] | 59 | }
|
---|
| 60 | /* check for a free uid (and maybe gid) */
|
---|
| 61 | while (getpwuid(p->pw_uid) || (p->pw_gid == (gid_t)-1 && getgrgid(p->pw_uid))) {
|
---|
| 62 | if (option_mask32 & OPT_UID) {
|
---|
| 63 | /* -u N, cannot pick uid other than N: error */
|
---|
| 64 | bb_error_msg_and_die("%s '%s' in use", "uid", itoa(p->pw_uid));
|
---|
| 65 | /* this format string is reused in adduser and addgroup */
|
---|
[821] | 66 | }
|
---|
[2725] | 67 | if (p->pw_uid == max) {
|
---|
| 68 | bb_error_msg_and_die("no %cids left", 'u');
|
---|
[3232] | 69 | /* this format string is reused in adduser and addgroup */
|
---|
[2725] | 70 | }
|
---|
| 71 | p->pw_uid++;
|
---|
[821] | 72 | }
|
---|
| 73 |
|
---|
[2725] | 74 | if (p->pw_gid == (gid_t)-1) {
|
---|
| 75 | p->pw_gid = p->pw_uid; /* new gid = uid */
|
---|
| 76 | if (getgrnam(p->pw_name)) {
|
---|
| 77 | bb_error_msg_and_die("%s '%s' in use", "group", p->pw_name);
|
---|
| 78 | /* this format string is reused in adduser and addgroup */
|
---|
| 79 | }
|
---|
[821] | 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[3232] | 83 | static int addgroup_wrapper(struct passwd *p, const char *group_name)
|
---|
[821] | 84 | {
|
---|
[3232] | 85 | char *argv[6];
|
---|
[821] | 86 |
|
---|
[3232] | 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);
|
---|
[821] | 114 | }
|
---|
| 115 |
|
---|
[3232] | 116 | static void passwd_wrapper(const char *login_name) NORETURN;
|
---|
[821] | 117 |
|
---|
[3232] | 118 | static void passwd_wrapper(const char *login_name)
|
---|
[821] | 119 | {
|
---|
[3232] | 120 | BB_EXECLP("passwd", "passwd", "--", login_name, NULL);
|
---|
[2725] | 121 | bb_error_msg_and_die("can't execute passwd, you must set password manually");
|
---|
[821] | 122 | }
|
---|
| 123 |
|
---|
[2725] | 124 | #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
|
---|
| 125 | static const char adduser_longopts[] ALIGN1 =
|
---|
| 126 | "home\0" Required_argument "h"
|
---|
| 127 | "gecos\0" Required_argument "g"
|
---|
| 128 | "shell\0" Required_argument "s"
|
---|
| 129 | "ingroup\0" Required_argument "G"
|
---|
| 130 | "disabled-password\0" No_argument "D"
|
---|
| 131 | "empty-password\0" No_argument "D"
|
---|
| 132 | "system\0" No_argument "S"
|
---|
| 133 | "no-create-home\0" No_argument "H"
|
---|
| 134 | "uid\0" Required_argument "u"
|
---|
| 135 | ;
|
---|
[821] | 136 | #endif
|
---|
| 137 |
|
---|
| 138 | /*
|
---|
| 139 | * adduser will take a login_name as its first parameter.
|
---|
[2725] | 140 | * home, shell, gecos:
|
---|
[821] | 141 | * can be customized via command-line parameters.
|
---|
[1765] | 142 | */
|
---|
[2725] | 143 | int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 144 | int adduser_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 145 | {
|
---|
| 146 | struct passwd pw;
|
---|
| 147 | const char *usegroup = NULL;
|
---|
[2725] | 148 | char *p;
|
---|
| 149 | unsigned opts;
|
---|
[821] | 150 |
|
---|
[2725] | 151 | #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
|
---|
| 152 | applet_long_options = adduser_longopts;
|
---|
| 153 | #endif
|
---|
| 154 |
|
---|
[1765] | 155 | /* got root? */
|
---|
| 156 | if (geteuid()) {
|
---|
| 157 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | pw.pw_gecos = (char *)"Linux User,,,";
|
---|
[3232] | 161 | /* We assume that newly created users "inherit" root's shell setting */
|
---|
| 162 | pw.pw_shell = (char *)get_shell_name();
|
---|
[821] | 163 | pw.pw_dir = NULL;
|
---|
| 164 |
|
---|
[3232] | 165 | /* at most two non-option args */
|
---|
[2725] | 166 | /* disable interactive passwd for system accounts */
|
---|
[3232] | 167 | opt_complementary = "?2:SD:u+";
|
---|
[2725] | 168 | if (sizeof(pw.pw_uid) == sizeof(int)) {
|
---|
| 169 | opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
|
---|
| 170 | } else {
|
---|
| 171 | unsigned uid;
|
---|
| 172 | opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
|
---|
| 173 | if (opts & OPT_UID) {
|
---|
| 174 | pw.pw_uid = uid;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
[1765] | 177 | argv += optind;
|
---|
[3232] | 178 | pw.pw_name = argv[0];
|
---|
[821] | 179 |
|
---|
[3232] | 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 | }
|
---|
| 186 |
|
---|
[2725] | 187 | /* fill in the passwd struct */
|
---|
| 188 | die_if_bad_username(pw.pw_name);
|
---|
[821] | 189 | if (!pw.pw_dir) {
|
---|
[1765] | 190 | /* create string for $HOME if not specified already */
|
---|
| 191 | pw.pw_dir = xasprintf("/home/%s", argv[0]);
|
---|
[821] | 192 | }
|
---|
[1765] | 193 | pw.pw_passwd = (char *)"x";
|
---|
[2725] | 194 | if (opts & OPT_SYSTEM_ACCOUNT) {
|
---|
| 195 | if (!usegroup) {
|
---|
| 196 | usegroup = "nogroup";
|
---|
| 197 | }
|
---|
| 198 | if (!(opts & OPT_SHELL)) {
|
---|
| 199 | pw.pw_shell = (char *) "/bin/false";
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
|
---|
[821] | 203 |
|
---|
[2725] | 204 | /* make sure everything is kosher and setup uid && maybe gid */
|
---|
| 205 | passwd_study(&pw);
|
---|
| 206 |
|
---|
| 207 | p = xasprintf("x:%u:%u:%s:%s:%s",
|
---|
| 208 | (unsigned) pw.pw_uid, (unsigned) pw.pw_gid,
|
---|
| 209 | pw.pw_gecos, pw.pw_dir, pw.pw_shell);
|
---|
| 210 | if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
|
---|
| 211 | return EXIT_FAILURE;
|
---|
| 212 | }
|
---|
| 213 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 214 | free(p);
|
---|
| 215 | #if ENABLE_FEATURE_SHADOWPASSWDS
|
---|
| 216 | /* /etc/shadow fields:
|
---|
| 217 | * 1. username
|
---|
| 218 | * 2. encrypted password
|
---|
| 219 | * 3. last password change (unix date (unix time/24*60*60))
|
---|
| 220 | * 4. minimum days required between password changes
|
---|
| 221 | * 5. maximum days password is valid
|
---|
| 222 | * 6. days before password is to expire that user is warned
|
---|
| 223 | * 7. days after password expires that account is disabled
|
---|
| 224 | * 8. unix date when login expires (i.e. when it may no longer be used)
|
---|
| 225 | */
|
---|
| 226 | /* fields: 2 3 4 5 6 78 */
|
---|
| 227 | p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60));
|
---|
| 228 | /* ignore errors: if file is missing we suppose admin doesn't want it */
|
---|
| 229 | update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
|
---|
| 230 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 231 | free(p);
|
---|
| 232 | #endif
|
---|
| 233 |
|
---|
| 234 | /* add to group */
|
---|
| 235 | addgroup_wrapper(&pw, usegroup);
|
---|
| 236 |
|
---|
| 237 | /* clear the umask for this process so it doesn't
|
---|
| 238 | * screw up the permissions on the mkdir and chown. */
|
---|
| 239 | umask(0);
|
---|
| 240 | if (!(opts & OPT_DONT_MAKE_HOME)) {
|
---|
| 241 | /* set the owner and group so it is owned by the new user,
|
---|
| 242 | * then fix up the permissions to 2755. Can't do it before
|
---|
| 243 | * since chown will clear the setgid bit */
|
---|
| 244 | int mkdir_err = mkdir(pw.pw_dir, 0755);
|
---|
| 245 | if (mkdir_err == 0) {
|
---|
| 246 | /* New home. Copy /etc/skel to it */
|
---|
| 247 | const char *args[] = {
|
---|
[3232] | 248 | "chown",
|
---|
| 249 | "-R",
|
---|
[2725] | 250 | xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid),
|
---|
[3232] | 251 | pw.pw_dir,
|
---|
| 252 | NULL
|
---|
[2725] | 253 | };
|
---|
| 254 | /* Be silent on any errors (like: no /etc/skel) */
|
---|
| 255 | logmode = LOGMODE_NONE;
|
---|
| 256 | copy_file("/etc/skel", pw.pw_dir, FILEUTILS_RECUR);
|
---|
| 257 | logmode = LOGMODE_STDIO;
|
---|
| 258 | chown_main(4, (char**)args);
|
---|
| 259 | }
|
---|
| 260 | if ((mkdir_err != 0 && errno != EEXIST)
|
---|
| 261 | || chown(pw.pw_dir, pw.pw_uid, pw.pw_gid) != 0
|
---|
| 262 | || chmod(pw.pw_dir, 02755) != 0 /* set setgid bit on homedir */
|
---|
| 263 | ) {
|
---|
| 264 | bb_simple_perror_msg(pw.pw_dir);
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | if (!(opts & OPT_DONT_SET_PASS)) {
|
---|
| 269 | /* interactively set passwd */
|
---|
| 270 | passwd_wrapper(pw.pw_name);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | return EXIT_SUCCESS;
|
---|
[821] | 274 | }
|
---|