2002-06-05 02:15:46 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-19 23:34:15 +05:30
|
|
|
/*
|
|
|
|
Licensed under the GPL v2, see the file LICENSE in this tarball.
|
|
|
|
*/
|
2002-06-05 02:15:46 +05:30
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utmp.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2002-06-23 09:54:25 +05:30
|
|
|
#include "busybox.h"
|
2002-06-05 02:15:46 +05:30
|
|
|
|
|
|
|
/* The shell to run if none is given in the user's passwd entry. */
|
2006-01-19 23:34:15 +05:30
|
|
|
#ifndef DEFAULT_SHELL
|
|
|
|
#define DEFAULT_SHELL "/bin/sh"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Default user. */
|
2002-06-05 02:15:46 +05:30
|
|
|
#define DEFAULT_USER "root"
|
|
|
|
|
2006-01-19 23:34:15 +05:30
|
|
|
/* #define SYSLOG_SUCCESS */
|
2002-06-05 02:15:46 +05:30
|
|
|
#define SYSLOG_FAILURE
|
|
|
|
|
|
|
|
|
|
|
|
#if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
|
2003-08-29 13:08:56 +05:30
|
|
|
/* Log the fact that someone has run su */
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2003-08-29 13:08:56 +05:30
|
|
|
# if defined( SYSLOG_SUCCESS ) && defined( SYSLOG_FAILURE )
|
2006-01-19 23:34:15 +05:30
|
|
|
static void log_su (const char *successful, const char *old_user,
|
|
|
|
const char *tty)
|
2002-06-05 02:15:46 +05:30
|
|
|
{
|
2003-08-29 13:08:56 +05:30
|
|
|
syslog ( LOG_NOTICE, "%s%s on %s", successful, old_user, tty);
|
2002-06-05 02:15:46 +05:30
|
|
|
}
|
2003-08-29 13:08:56 +05:30
|
|
|
# define log_su_successful(cu, u, tty) if(!cu) log_su("", u, tty)
|
|
|
|
# define log_su_failure(cu, u, tty) if(!cu) log_su("FAILED SU ", u, tty)
|
|
|
|
# else
|
|
|
|
/* partial logging */
|
|
|
|
# if !defined( SYSLOG_SUCESS )
|
|
|
|
# define log_su_successful(cu, u, tty)
|
|
|
|
# define log_su_failure(cu, u, t) if(!cu) \
|
|
|
|
syslog(LOG_NOTICE, "FAILED SU %s on %s", u, t)
|
|
|
|
# else
|
|
|
|
# define log_su_successful(cu, u, t) if(!cu) \
|
|
|
|
syslog(LOG_NOTICE, "%s on %s", u, t)
|
|
|
|
# define log_su_failure(cu, u, tty)
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
/* logging not used */
|
|
|
|
# define log_su_successful(cu, u, tty)
|
|
|
|
# define log_su_failure(cu, u, tty)
|
2002-06-05 02:15:46 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
int su_main ( int argc, char **argv )
|
|
|
|
{
|
2003-08-29 13:08:56 +05:30
|
|
|
unsigned long flags;
|
2002-06-05 02:15:46 +05:30
|
|
|
char *opt_shell = 0;
|
|
|
|
char *opt_command = 0;
|
|
|
|
char *opt_username = DEFAULT_USER;
|
|
|
|
char **opt_args = 0;
|
2003-08-29 13:08:56 +05:30
|
|
|
struct passwd *pw;
|
|
|
|
uid_t cur_uid = getuid();
|
|
|
|
|
|
|
|
#if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
|
|
|
|
const char *tty;
|
|
|
|
const char *old_user;
|
|
|
|
#endif
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2003-08-29 13:08:56 +05:30
|
|
|
flags = bb_getopt_ulflags(argc, argv, "mplc:s:",
|
|
|
|
&opt_command, &opt_shell);
|
2006-06-14 22:06:45 +05:30
|
|
|
#define SU_OPT_m (3)
|
|
|
|
#define SU_OPT_p (3)
|
|
|
|
#define SU_OPT_l (4)
|
2003-08-29 13:08:56 +05:30
|
|
|
|
|
|
|
if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) {
|
2006-06-14 22:06:45 +05:30
|
|
|
flags |= SU_OPT_l;
|
2002-06-05 02:15:46 +05:30
|
|
|
++optind;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get user if specified */
|
2004-03-15 13:59:22 +05:30
|
|
|
if ( optind < argc )
|
2002-06-05 02:15:46 +05:30
|
|
|
opt_username = argv [optind++];
|
|
|
|
|
|
|
|
if ( optind < argc )
|
|
|
|
opt_args = argv + optind;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2003-08-29 13:08:56 +05:30
|
|
|
#if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
|
2005-07-01 06:34:32 +05:30
|
|
|
#ifdef CONFIG_FEATURE_UTMP
|
2003-08-29 13:08:56 +05:30
|
|
|
/* The utmp entry (via getlogin) is probably the best way to identify
|
|
|
|
the user, especially if someone su's from a su-shell. */
|
|
|
|
old_user = getlogin ( );
|
|
|
|
if ( !old_user )
|
|
|
|
#endif
|
|
|
|
{
|
2006-01-19 23:34:15 +05:30
|
|
|
/* getlogin can fail -- usually due to lack of utmp entry.
|
|
|
|
Resort to getpwuid. */
|
2003-08-29 13:08:56 +05:30
|
|
|
pw = getpwuid ( cur_uid );
|
|
|
|
old_user = ( pw ? pw->pw_name : "" );
|
|
|
|
}
|
|
|
|
tty = ttyname ( 2 );
|
|
|
|
if(!tty)
|
|
|
|
tty = "none";
|
|
|
|
|
|
|
|
openlog ( bb_applet_name, 0, LOG_AUTH );
|
|
|
|
#endif
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2002-06-05 02:15:46 +05:30
|
|
|
pw = getpwnam ( opt_username );
|
|
|
|
if ( !pw )
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die ( "user %s does not exist", opt_username );
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2002-06-05 02:15:46 +05:30
|
|
|
/* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
|
|
|
|
is a username that is retrieved via NIS (YP), but that doesn't have
|
|
|
|
a default shell listed. */
|
2006-01-19 23:34:15 +05:30
|
|
|
if ( !pw->pw_shell || !pw->pw_shell [0] )
|
|
|
|
pw->pw_shell = (char *) DEFAULT_SHELL;
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2003-08-29 13:08:56 +05:30
|
|
|
if ((( cur_uid == 0 ) || correct_password ( pw ))) {
|
|
|
|
log_su_successful(pw->pw_uid, old_user, tty );
|
|
|
|
} else {
|
|
|
|
log_su_failure (pw->pw_uid, old_user, tty );
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die ( "incorrect password" );
|
2002-06-05 02:15:46 +05:30
|
|
|
}
|
|
|
|
|
2003-08-29 13:08:56 +05:30
|
|
|
#if defined( SYSLOG_SUCCESS ) || defined( SYSLOG_FAILURE )
|
|
|
|
closelog();
|
|
|
|
#endif
|
|
|
|
|
2006-06-14 22:06:45 +05:30
|
|
|
if ( !opt_shell && (flags & SU_OPT_p))
|
2002-06-05 02:15:46 +05:30
|
|
|
opt_shell = getenv ( "SHELL" );
|
|
|
|
|
2006-01-19 23:34:15 +05:30
|
|
|
if ( opt_shell && cur_uid && restricted_shell ( pw->pw_shell )) {
|
2002-06-05 02:15:46 +05:30
|
|
|
/* The user being su'd to has a nonstandard shell, and so is
|
|
|
|
probably a uucp account or has restricted access. Don't
|
|
|
|
compromise the account by allowing access with a standard
|
|
|
|
shell. */
|
|
|
|
fputs ( "using restricted shell\n", stderr );
|
|
|
|
opt_shell = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !opt_shell )
|
2003-08-29 13:08:56 +05:30
|
|
|
opt_shell = pw->pw_shell;
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2004-03-15 13:59:22 +05:30
|
|
|
change_identity ( pw );
|
2006-06-14 22:06:45 +05:30
|
|
|
setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_p), pw);
|
2006-01-19 23:34:15 +05:30
|
|
|
#if ENABLE_SELINUX
|
2005-05-03 11:55:50 +05:30
|
|
|
set_current_security_context(NULL);
|
2003-07-03 15:37:04 +05:30
|
|
|
#endif
|
2006-06-14 22:06:45 +05:30
|
|
|
run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)opt_args);
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2002-06-05 02:15:46 +05:30
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|