/*
* Copyright (c) 1988 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
if (ruid
#ifdef KERBEROS5
&& (!use_kerberos || kerberos5(username, user, pwd->pw_uid))
#endif
) {
char *pass = pwd->pw_passwd;
int ok = pwd->pw_uid != 0;
#ifdef SU_ROOTAUTH
/*
* Allow those in group rootauth to su to root, by supplying
* their own password.
*/
if (!ok) {
if ((ok = check_ingroup(-1, SU_ROOTAUTH, username, 0))) {
pass = userpass;
user = username;
}
}
#endif
/*
* Only allow those in group SU_GROUP to su to root,
* but only if that group has any members.
* If SU_GROUP has no members, allow anyone to su root
*/
if (!ok)
ok = check_ingroup(-1, SU_GROUP, username, 1);
if (!ok)
errx(EXIT_FAILURE,
"you are not listed in the correct secondary group (%s) to su %s.",
SU_GROUP, user);
/* if target requires a password, verify it */
if (*pass && pwd->pw_uid != ruid) { /* XXX - OK? */
p = getpass("Password:");
#ifdef SKEY
if (strcasecmp(p, "s/key") == 0) {
if (skey_haskey(user))
errx(EXIT_FAILURE,
"Sorry, you have no s/key.");
else {
if (skey_authenticate(user)) {
goto badlogin;
}
}
} else
#endif
if (consttime_memequal(pass,
crypt(p, pass), strlen(pass)) == 0) {
#ifdef SKEY
badlogin:
#endif
(void)fprintf(stderr, "Sorry\n");
syslog(LOG_WARNING,
"BAD SU %s to %s%s", username,
pwd->pw_name, ontty());
exit(EXIT_FAILURE);
}
}
}
if (asme) {
/* if asme and non-standard target shell, must be root */
if (chshell(pwd->pw_shell) == 0 && ruid)
errx(EXIT_FAILURE, "permission denied (shell).");
} else if (pwd->pw_shell && *pwd->pw_shell) {
shell = pwd->pw_shell;
iscsh = UNSET;
} else {
shell = _PATH_BSHELL;
iscsh = NO;
}
/* if we're forking a csh, we want to slightly muck the args */
if (iscsh == UNSET)
iscsh = strstr(avshell, "csh") ? YES : NO;
/* set permissions */
#ifdef LOGIN_CAP
# ifdef ALLOW_GROUP_CHANGE
/* if we aren't changing users, keep the current group members */
if (ruid != pwd->pw_uid &&
setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) == -1)
err(EXIT_FAILURE, "setting user context");
addgroup(lc, gname, pwd, ruid, GROUP_PASSWORD);
if (setusercontext(lc, pwd, pwd->pw_uid,
(u_int)(asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
LOGIN_SETRESOURCES | LOGIN_SETUSER) == -1)
err(EXIT_FAILURE, "setting user context");
# else
if (setusercontext(lc, pwd, pwd->pw_uid,
(u_int)(asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER) == -1)
err(EXIT_FAILURE, "setting user context");
# endif
#else
if (setgid(pwd->pw_gid) == -1)
err(EXIT_FAILURE, "setgid");
/* if we aren't changing users, keep the current group members */
if (ruid != pwd->pw_uid && initgroups(user, pwd->pw_gid) != 0)
errx(EXIT_FAILURE, "initgroups failed");
# ifdef ALLOW_GROUP_CHANGE
addgroup(/*EMPTY*/, gname, pwd, ruid, GROUP_PASSWORD);
# endif
if (setuid(pwd->pw_uid) == -1)
err(EXIT_FAILURE, "setuid");
#endif
if (!asme) {
if (asthem) {
p = getenv("TERM");
/* Create an empty environment */
environ = emalloc(sizeof(char *));
environ[0] = NULL;
#ifdef LOGIN_CAP
if (setusercontext(lc, pwd, pwd->pw_uid,
LOGIN_SETPATH) == -1)
err(EXIT_FAILURE, "setting user context");
#else
(void)setenv("PATH", _PATH_DEFPATH, 1);
#endif
if (p)
(void)setenv("TERM", p, 1);
if (gohome && chdir(pwd->pw_dir) == -1)
errx(EXIT_FAILURE, "no directory");
}
static int
check_ingroup(int gid, const char *gname, const char *user, int ifempty)
{
struct group *gr;
char **g;
#ifdef SU_INDIRECT_GROUP
char **gr_mem;
int n = 0;
int i = 0;
#endif
int ok = 0;
if (gname == NULL)
gr = getgrgid((gid_t) gid);
else
gr = getgrnam(gname);
/*
* XXX we are relying on the fact that we only set ifempty when
* calling to check for SU_GROUP and that is the only time a
* missing group is acceptable.
*/
if (gr == NULL)
return ifempty;
if (!*gr->gr_mem) /* empty */
return ifempty;
/*
* Ok, first see if user is in gr_mem
*/
for (g = gr->gr_mem; *g; ++g) {
if (strcmp(*g, user) == 0)
return 1; /* ok */
#ifdef SU_INDIRECT_GROUP
++n; /* count them */
#endif
}
#ifdef SU_INDIRECT_GROUP
/*
* No.
* Now we need to duplicate the gr_mem list, and recurse for
* each member to see if it is a group, and if so whether user is
* in it.
*/
gr_mem = NULL;
ereallocarr(&gr_mem, n + 1, sizeof(char *));
for (g = gr->gr_mem, i = 0; *g; ++g) {
gr_mem[i] = estrdup(*g);
i++;
}
gr_mem[i++] = NULL;
for (g = gr_mem; ok == 0 && *g; ++g) {
/*
* If we get this far we don't accept empty/missing groups.
*/
ok = check_ingroup(-1, *g, user, 0);
}
for (g = gr_mem; *g; ++g) {
free(*g);
}
free(gr_mem);
#endif
return ok;
}