Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (7 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi-busybox/loginutils/adduser.c

    r2725 r3232  
    88 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    99 */
     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
    1024#include "libbb.h"
    1125
     
    5367        if (p->pw_uid == max) {
    5468            bb_error_msg_and_die("no %cids left", 'u');
     69            /* this format string is reused in adduser and addgroup */
    5570        }
    5671        p->pw_uid++;
     
    6681}
    6782
    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);
     83static 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
     116static void passwd_wrapper(const char *login_name) NORETURN;
     117
     118static void passwd_wrapper(const char *login_name)
     119{
     120    BB_EXECLP("passwd", "passwd", "--", login_name, NULL);
    86121    bb_error_msg_and_die("can't execute passwd, you must set password manually");
    87122}
     
    124159
    125160    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();
    127163    pw.pw_dir = NULL;
    128164
    129     /* exactly one non-option arg */
     165    /* at most two non-option args */
    130166    /* disable interactive passwd for system accounts */
    131     opt_complementary = "=1:SD:u+";
     167    opt_complementary = "?2:SD:u+";
    132168    if (sizeof(pw.pw_uid) == sizeof(int)) {
    133169        opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
     
    140176    }
    141177    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    }
    142186
    143187    /* fill in the passwd struct */
    144     pw.pw_name = argv[0];
    145188    die_if_bad_username(pw.pw_name);
    146189    if (!pw.pw_dir) {
     
    170213    if (ENABLE_FEATURE_CLEAN_UP)
    171214        free(p);
    172 
    173215#if ENABLE_FEATURE_SHADOWPASSWDS
    174216    /* /etc/shadow fields:
     
    204246            /* New home. Copy /etc/skel to it */
    205247            const char *args[] = {
    206                 "chown", "-R",
     248                "chown",
     249                "-R",
    207250                xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid),
    208                 pw.pw_dir, NULL
     251                pw.pw_dir,
     252                NULL
    209253            };
    210254            /* Be silent on any errors (like: no /etc/skel) */
Note: See TracChangeset for help on using the changeset viewer.