Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/loginutils/su.c


Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/loginutils/su.c

    r1765 r2725  
    11/* vi: set sw=4 ts=4: */
    22/*
    3  *  Mini su implementation for busybox
     3 * Mini su implementation for busybox
    44 *
    5  *  Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
     5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    66 */
    77
     
    99#include <syslog.h>
    1010
     11#if ENABLE_FEATURE_SU_CHECKS_SHELLS
     12/* Return 1 if SHELL is a restricted shell (one not returned by
     13 * getusershell), else 0, meaning it is a standard shell.  */
     14static int restricted_shell(const char *shell)
     15{
     16    char *line;
     17    int result = 1;
     18
     19    /*setusershell(); - getusershell does it itself*/
     20    while ((line = getusershell()) != NULL) {
     21        if (/* *line != '#' && */ strcmp(line, shell) == 0) {
     22            result = 0;
     23            break;
     24        }
     25    }
     26    if (ENABLE_FEATURE_CLEAN_UP)
     27        endusershell();
     28    return result;
     29}
     30#endif
     31
    1132#define SU_OPT_mp (3)
    12 #define SU_OPT_l (4)
     33#define SU_OPT_l  (4)
    1334
    14 int su_main(int argc, char **argv);
    15 int su_main(int argc, char **argv)
     35int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     36int su_main(int argc UNUSED_PARAM, char **argv)
    1637{
    1738    unsigned flags;
     
    2243    uid_t cur_uid = getuid();
    2344    const char *tty;
    24     char *old_user;
     45    char user_buf[64];
     46    const char *old_user;
    2547
    2648    flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
    27     argc -= optind;
     49    //argc -= optind;
    2850    argv += optind;
    2951
    30     if (argc && LONE_DASH(argv[0])) {
     52    if (argv[0] && LONE_DASH(argv[0])) {
    3153        flags |= SU_OPT_l;
    32         argc--;
    3354        argv++;
    3455    }
    3556
    3657    /* get user if specified */
    37     if (argc) {
     58    if (argv[0]) {
    3859        opt_username = argv[0];
    39 //      argc--;
    4060        argv++;
    4161    }
    4262
    4363    if (ENABLE_FEATURE_SU_SYSLOG) {
    44         /* The utmp entry (via getlogin) is probably the best way to identify
    45         the user, especially if someone su's from a su-shell.
    46         But getlogin can fail -- usually due to lack of utmp entry.
    47         in this case resort to getpwuid.  */
    48         old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
    49         tty = ttyname(2) ? : "none";
     64        /* The utmp entry (via getlogin) is probably the best way to
     65         * identify the user, especially if someone su's from a su-shell.
     66         * But getlogin can fail -- usually due to lack of utmp entry.
     67         * in this case resort to getpwuid.  */
     68#if ENABLE_FEATURE_UTMP
     69        old_user = user_buf;
     70        if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
     71#endif
     72        {
     73            pw = getpwuid(cur_uid);
     74            old_user = pw ? xstrdup(pw->pw_name) : "";
     75        }
     76        tty = xmalloc_ttyname(2);
     77        if (!tty) {
     78            tty = "none";
     79        }
    5080        openlog(applet_name, 0, LOG_AUTH);
    5181    }
    5282
    53     pw = getpwnam(opt_username);
    54     if (!pw)
    55         bb_error_msg_and_die("unknown id: %s", opt_username);
     83    pw = xgetpwnam(opt_username);
    5684
    57     /* Make sure pw->pw_shell is non-NULL.  It may be NULL when NEW_USER
    58        is a username that is retrieved via NIS (YP), but that doesn't have
    59        a default shell listed.  */
    60     if (!pw->pw_shell || !pw->pw_shell[0])
    61         pw->pw_shell = (char *)DEFAULT_SHELL;
    62 
    63     if ((cur_uid == 0) || correct_password(pw)) {
     85    if (cur_uid == 0 || correct_password(pw)) {
    6486        if (ENABLE_FEATURE_SU_SYSLOG)
    6587            syslog(LOG_NOTICE, "%c %s %s:%s",
     
    7496    if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
    7597        closelog();
    76         free(old_user);
    7798    }
    7899
    79     if (!opt_shell && (flags & SU_OPT_mp))
     100    if (!opt_shell && (flags & SU_OPT_mp)) {
     101        /* -s SHELL is not given, but "preserve env" opt is */
    80102        opt_shell = getenv("SHELL");
     103    }
     104
     105    /* Make sure pw->pw_shell is non-NULL.  It may be NULL when NEW_USER
     106     * is a username that is retrieved via NIS (YP), that doesn't have
     107     * a default shell listed.  */
     108    if (!pw->pw_shell || !pw->pw_shell[0])
     109        pw->pw_shell = (char *)DEFAULT_SHELL;
    81110
    82111#if ENABLE_FEATURE_SU_CHECKS_SHELLS
    83     if (opt_shell && cur_uid && restricted_shell(pw->pw_shell)) {
     112    if (opt_shell && cur_uid != 0 && restricted_shell(pw->pw_shell)) {
    84113        /* The user being su'd to has a nonstandard shell, and so is
    85            probably a uucp account or has restricted access.  Don't
    86            compromise the account by allowing access with a standard
    87            shell.  */
     114         * probably a uucp account or has restricted access.  Don't
     115         * compromise the account by allowing access with a standard
     116         * shell.  */
    88117        bb_error_msg("using restricted shell");
    89         opt_shell = 0;
     118        opt_shell = NULL;
    90119    }
     120    /* else: user can run whatever he wants via "su -s PROG USER".
     121     * This is safe since PROG is run under user's uid/gid. */
    91122#endif
    92123    if (!opt_shell)
     
    94125
    95126    change_identity(pw);
    96     setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
    97     USE_SELINUX(set_current_security_context(NULL);)
     127    setup_environment(opt_shell,
     128            ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
     129            + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV),
     130            pw);
     131    IF_SELINUX(set_current_security_context(NULL);)
    98132
    99133    /* Never returns */
    100134    run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
    101135
    102     return EXIT_FAILURE;
     136    /* return EXIT_FAILURE; - not reached */
    103137}
Note: See TracChangeset for help on using the changeset viewer.