Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (16 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

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

    r821 r1765  
    99 */
    1010
    11 #include <stdio.h>
    12 #include <sys/types.h>
    13 #include <string.h>
    14 #include <unistd.h>
    15 #include <time.h>
    16 #include <getopt.h>
    17 #include <sys/stat.h>
    18 
    19 #include "busybox.h"
    20 
    21 #define DONT_SET_PASS           (1 << 4)
    22 #define DONT_MAKE_HOME          (1 << 6)
     11#include "libbb.h"
     12
     13#define OPT_DONT_SET_PASS  (1 << 4)
     14#define OPT_DONT_MAKE_HOME (1 << 6)
    2315
    2416
     
    2719static int passwd_study(const char *filename, struct passwd *p)
    2820{
    29     struct passwd *pw;
     21    enum { min = 500, max = 65000 };
    3022    FILE *passwd;
    31 
    32     const int min = 500;
    33     const int max = 65000;
    34 
    35     passwd = bb_xfopen(filename, "r");
     23    /* We are using reentrant fgetpwent_r() in order to avoid
     24     * pulling in static buffers from libc (think static build here) */
     25    char buffer[256];
     26    struct passwd pw;
     27    struct passwd *result;
     28
     29    passwd = xfopen(filename, "r");
    3630
    3731    /* EDR if uid is out of bounds, set to min */
     
    4337     * find free uid and gid;
    4438     */
    45     while ((pw = fgetpwent(passwd))) {
    46         if (strcmp(pw->pw_name, p->pw_name) == 0) {
     39    while (!fgetpwent_r(passwd, &pw, buffer, sizeof(buffer), &result)) {
     40        if (strcmp(pw.pw_name, p->pw_name) == 0) {
    4741            /* return 0; */
    4842            return 1;
    4943        }
    50         if ((pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max)
    51             && (pw->pw_uid >= min)) {
    52             p->pw_uid = pw->pw_uid + 1;
     44        if ((pw.pw_uid >= p->pw_uid) && (pw.pw_uid < max)
     45            && (pw.pw_uid >= min)) {
     46            p->pw_uid = pw.pw_uid + 1;
    5347        }
    5448    }
     
    7973    char *cmd;
    8074
    81     cmd = bb_xasprintf("addgroup -g %d \"%s\"", p->pw_gid, p->pw_name);
     75    cmd = xasprintf("addgroup -g %d \"%s\"", p->pw_gid, p->pw_name);
    8276    system(cmd);
    8377    free(cmd);
     
    8882static void passwd_wrapper(const char *login)
    8983{
    90     static const char prog[] = "passwd";
    91     execlp(prog, prog, login, NULL);
    92     bb_error_msg_and_die("Failed to execute '%s', you must set the password for '%s' manually", prog, login);
     84    static const char prog[] ALIGN1 = "passwd";
     85
     86    BB_EXECLP(prog, prog, login, NULL);
     87    bb_error_msg_and_die("failed to execute '%s', you must set the password for '%s' manually", prog, login);
    9388}
    9489
    9590/* putpwent(3) remix */
    96 static int adduser(struct passwd *p, unsigned long flags)
     91static int adduser(struct passwd *p)
    9792{
    9893    FILE *file;
     
    10095
    10196    /* make sure everything is kosher and setup uid && gid */
    102     file = bb_xfopen(bb_path_passwd_file, "a");
     97    file = xfopen(bb_path_passwd_file, "a");
    10398    fseek(file, 0, SEEK_END);
    10499
     
    110105        case 3:
    111106            bb_error_msg_and_die("%s: group name already in use", p->pw_name);
    112     }
     107    }
    113108
    114109    /* add to passwd */
     
    116111        bb_perror_nomsg_and_die();
    117112    }
     113    /* Do fclose even if !ENABLE_FEATURE_CLEAN_UP.
     114     * We will exec passwd, files must be flushed & closed before that! */
    118115    fclose(file);
    119116
    120117#if ENABLE_FEATURE_SHADOWPASSWDS
    121118    /* add to shadow if necessary */
    122     file = bb_xfopen(bb_path_shadow_file, "a");
    123     fseek(file, 0, SEEK_END);
    124     fprintf(file, "%s:!:%ld:%d:%d:%d:::\n",
    125                     p->pw_name,             /* username */
    126                     time(NULL) / 86400,     /* sp->sp_lstchg */
    127                     0,                      /* sp->sp_min */
    128                     99999,                  /* sp->sp_max */
    129                     7);                     /* sp->sp_warn */
    130     fclose(file);
     119    file = fopen_or_warn(bb_path_shadow_file, "a");
     120    if (file) {
     121        fseek(file, 0, SEEK_END);
     122        fprintf(file, "%s:!:%ld:%d:%d:%d:::\n",
     123                p->pw_name,             /* username */
     124                time(NULL) / 86400,     /* sp->sp_lstchg */
     125                0,                      /* sp->sp_min */
     126                99999,                  /* sp->sp_max */
     127                7);                     /* sp->sp_warn */
     128        fclose(file);
     129    }
    131130#endif
    132131
     
    137136
    138137    /* Clear the umask for this process so it doesn't
    139      * * screw up the permissions on the mkdir and chown. */
     138     * screw up the permissions on the mkdir and chown. */
    140139    umask(0);
    141     if (!(flags & DONT_MAKE_HOME)) {
     140    if (!(option_mask32 & OPT_DONT_MAKE_HOME)) {
    142141        /* Set the owner and group so it is owned by the new user,
    143142           then fix up the permissions to 2755. Can't do it before
     
    146145        || chown(p->pw_dir, p->pw_uid, p->pw_gid)
    147146        || chmod(p->pw_dir, 02755)) {
    148             bb_perror_msg("%s", p->pw_dir);
    149         }
    150     }
    151 
    152     if (!(flags & DONT_SET_PASS)) {
     147            bb_perror_msg("%s", p->pw_dir);
     148        }
     149    }
     150
     151    if (!(option_mask32 & OPT_DONT_SET_PASS)) {
    153152        /* interactively set passwd */
    154153        passwd_wrapper(p->pw_name);
     
    166165 *
    167166 * can be customized via command-line parameters.
    168  * ________________________________________________________________________ */
     167 */
     168int adduser_main(int argc, char **argv);
    169169int adduser_main(int argc, char **argv)
    170170{
    171171    struct passwd pw;
    172172    const char *usegroup = NULL;
    173     unsigned long flags;
    174 
    175     pw.pw_gecos = "Linux User,,,";
     173
     174    /* got root? */
     175    if (geteuid()) {
     176        bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
     177    }
     178
     179    pw.pw_gecos = (char *)"Linux User,,,";
    176180    pw.pw_shell = (char *)DEFAULT_SHELL;
    177181    pw.pw_dir = NULL;
    178182
    179     /* check for min, max and missing args and exit on error */
    180     bb_opt_complementally = "-1:?1:?";
    181     flags = bb_getopt_ulflags(argc, argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup);
    182 
    183     /* got root? */
    184     if(geteuid()) {
    185         bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
    186     }
    187 
    188     /* create string for $HOME if not specified already */
     183    /* exactly one non-option arg */
     184    opt_complementary = "=1";
     185    getopt32(argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup);
     186    argv += optind;
     187
     188    /* create a passwd struct */
     189    pw.pw_name = argv[0];
    189190    if (!pw.pw_dir) {
    190         snprintf(bb_common_bufsiz1, BUFSIZ, "/home/%s", argv[optind]);
    191         pw.pw_dir =  &bb_common_bufsiz1[0];
    192     }
    193 
    194     /* create a passwd struct */
    195     pw.pw_name = argv[optind];
    196     pw.pw_passwd = "x";
     191        /* create string for $HOME if not specified already */
     192        pw.pw_dir = xasprintf("/home/%s", argv[0]);
     193    }
     194    pw.pw_passwd = (char *)"x";
    197195    pw.pw_uid = 0;
    198     pw.pw_gid = (usegroup) ? bb_xgetgrnam(usegroup) : 0; /* exits on failure */
     196    pw.pw_gid = usegroup ? xgroup2gid(usegroup) : 0; /* exits on failure */
    199197
    200198    /* grand finale */
    201     return adduser(&pw, flags);
    202 }
     199    return adduser(&pw);
     200}
Note: See TracChangeset for help on using the changeset viewer.