xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/uidswap.c (revision 5600:3cc59f3fc148)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
30Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
40Sstevel@tonic-gate  *                    All rights reserved
50Sstevel@tonic-gate  * Code for uid-swapping.
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
80Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
90Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
100Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
110Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
120Sstevel@tonic-gate  */
13*5600Sjp161948 /*
14*5600Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
15*5600Sjp161948  * Use is subject to license terms.
16*5600Sjp161948  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #include "includes.h"
190Sstevel@tonic-gate RCSID("$OpenBSD: uidswap.c,v 1.23 2002/07/15 17:15:31 stevesk Exp $");
200Sstevel@tonic-gate 
210Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
220Sstevel@tonic-gate 
230Sstevel@tonic-gate #include "log.h"
240Sstevel@tonic-gate #include "uidswap.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Note: all these functions must work in all of the following cases:
280Sstevel@tonic-gate  *    1. euid=0, ruid=0
290Sstevel@tonic-gate  *    2. euid=0, ruid!=0
300Sstevel@tonic-gate  *    3. euid!=0, ruid!=0
310Sstevel@tonic-gate  * Additionally, they must work regardless of whether the system has
320Sstevel@tonic-gate  * POSIX saved uids or not.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
360Sstevel@tonic-gate /* Lets assume that posix saved ids also work with seteuid, even though that
370Sstevel@tonic-gate    is not part of the posix specification. */
380Sstevel@tonic-gate #define SAVED_IDS_WORK
390Sstevel@tonic-gate /* Saved effective uid. */
400Sstevel@tonic-gate static uid_t 	saved_euid = 0;
410Sstevel@tonic-gate static gid_t	saved_egid = 0;
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /* Saved effective uid. */
450Sstevel@tonic-gate static int	privileged = 0;
460Sstevel@tonic-gate static int	temporarily_use_uid_effective = 0;
47*5600Sjp161948 static gid_t	saved_egroups[NGROUPS_UMAX], user_groups[NGROUPS_UMAX];
480Sstevel@tonic-gate static int	saved_egroupslen = -1, user_groupslen = -1;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Temporarily changes to the given uid.  If the effective user
520Sstevel@tonic-gate  * id is not root, this does nothing.  This call cannot be nested.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate void
550Sstevel@tonic-gate temporarily_use_uid(struct passwd *pw)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	/* Save the current euid, and egroups. */
580Sstevel@tonic-gate #ifdef SAVED_IDS_WORK
590Sstevel@tonic-gate 	saved_euid = geteuid();
600Sstevel@tonic-gate 	saved_egid = getegid();
610Sstevel@tonic-gate 	debug("temporarily_use_uid: %u/%u (e=%u/%u)",
620Sstevel@tonic-gate 	    (u_int)pw->pw_uid, (u_int)pw->pw_gid,
630Sstevel@tonic-gate 	    (u_int)saved_euid, (u_int)saved_egid);
640Sstevel@tonic-gate 	if (saved_euid != 0) {
650Sstevel@tonic-gate 		privileged = 0;
660Sstevel@tonic-gate 		return;
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate #else
690Sstevel@tonic-gate 	if (geteuid() != 0) {
700Sstevel@tonic-gate 		privileged = 0;
710Sstevel@tonic-gate 		return;
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	privileged = 1;
760Sstevel@tonic-gate 	temporarily_use_uid_effective = 1;
77*5600Sjp161948 	saved_egroupslen = getgroups(NGROUPS_UMAX, saved_egroups);
780Sstevel@tonic-gate 	if (saved_egroupslen < 0)
790Sstevel@tonic-gate 		fatal("getgroups: %.100s", strerror(errno));
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	/* set and save the user's groups */
820Sstevel@tonic-gate 	if (user_groupslen == -1) {
830Sstevel@tonic-gate 		if (initgroups(pw->pw_name, pw->pw_gid) < 0)
840Sstevel@tonic-gate 			fatal("initgroups: %s: %.100s", pw->pw_name,
850Sstevel@tonic-gate 			    strerror(errno));
86*5600Sjp161948 		user_groupslen = getgroups(NGROUPS_UMAX, user_groups);
870Sstevel@tonic-gate 		if (user_groupslen < 0)
880Sstevel@tonic-gate 			fatal("getgroups: %.100s", strerror(errno));
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	/* Set the effective uid to the given (unprivileged) uid. */
910Sstevel@tonic-gate 	if (setgroups(user_groupslen, user_groups) < 0)
920Sstevel@tonic-gate 		fatal("setgroups: %.100s", strerror(errno));
930Sstevel@tonic-gate #ifdef SAVED_IDS_WORK
940Sstevel@tonic-gate 	/* Set saved gid and set real gid */
950Sstevel@tonic-gate 	if (setregid(pw->pw_gid, -1) == -1)
960Sstevel@tonic-gate 		debug("setregid(%u, -1): %.100s", (uint_t)pw->pw_gid, strerror(errno));
970Sstevel@tonic-gate 	/* Set saved uid and set real uid */
980Sstevel@tonic-gate 	if (setreuid(pw->pw_uid, -1) == -1)
990Sstevel@tonic-gate 		debug("setreuid(%u, -1): %.100s", (uint_t)pw->pw_uid, strerror(errno));
1000Sstevel@tonic-gate #else
1010Sstevel@tonic-gate 	/* Propagate the privileged gid to all of our gids. */
1020Sstevel@tonic-gate 	if (setgid(getegid()) < 0)
1030Sstevel@tonic-gate 		debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
1040Sstevel@tonic-gate 	/* Propagate the privileged uid to all of our uids. */
1050Sstevel@tonic-gate 	if (setuid(geteuid()) < 0)
1060Sstevel@tonic-gate 		debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
1070Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */
1080Sstevel@tonic-gate 	/* Set effective gid */
1090Sstevel@tonic-gate 	if (setegid(pw->pw_gid) == -1)
1100Sstevel@tonic-gate 		fatal("setegid %u: %.100s", (u_int)pw->pw_uid,
1110Sstevel@tonic-gate 		    strerror(errno));
1120Sstevel@tonic-gate 	/* Set effective uid */
1130Sstevel@tonic-gate 	if (seteuid(pw->pw_uid) == -1)
1140Sstevel@tonic-gate 		fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
1150Sstevel@tonic-gate 		    strerror(errno));
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * If saved set ids work then
1180Sstevel@tonic-gate 	 *
1190Sstevel@tonic-gate 	 *	ruid == euid == pw->pw_uid
1200Sstevel@tonic-gate 	 *	saved uid = previous euid
1210Sstevel@tonic-gate 	 *	rgid == egid == pw->pw_gid
1220Sstevel@tonic-gate 	 *	saved gid = previous egid
1230Sstevel@tonic-gate 	 */
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Restores to the original (privileged) uid.
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate void
1300Sstevel@tonic-gate restore_uid(void)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	/* it's a no-op unless privileged */
1330Sstevel@tonic-gate 	if (!privileged) {
1340Sstevel@tonic-gate 		debug("restore_uid: (unprivileged)");
1350Sstevel@tonic-gate 		return;
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	if (!temporarily_use_uid_effective)
1380Sstevel@tonic-gate 		fatal("restore_uid: temporarily_use_uid not effective");
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #ifdef SAVED_IDS_WORK
1410Sstevel@tonic-gate 	debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
1420Sstevel@tonic-gate 	/* Set the effective uid back to the saved privileged uid. */
1430Sstevel@tonic-gate 	if (seteuid(saved_euid) < 0)
1440Sstevel@tonic-gate 		fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
1450Sstevel@tonic-gate 	if (setuid(saved_euid) < 0)
1460Sstevel@tonic-gate 		fatal("setuid %u: %.100s", (u_int)saved_euid, strerror(errno));
1470Sstevel@tonic-gate 	if (setegid(saved_egid) < 0)
1480Sstevel@tonic-gate 		fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
1490Sstevel@tonic-gate 	if (setgid(saved_egid) < 0)
1500Sstevel@tonic-gate 		fatal("setgid %u: %.100s", (u_int)saved_egid, strerror(errno));
1510Sstevel@tonic-gate #else /* SAVED_IDS_WORK */
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * We are unable to restore the real uid to its unprivileged value.
1540Sstevel@tonic-gate 	 * Propagate the real uid (usually more privileged) to effective uid
1550Sstevel@tonic-gate 	 * as well.
1560Sstevel@tonic-gate 	 */
1570Sstevel@tonic-gate 	setuid(getuid());
1580Sstevel@tonic-gate 	setgid(getgid());
1590Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (setgroups(saved_egroupslen, saved_egroups) < 0)
1620Sstevel@tonic-gate 		fatal("setgroups: %.100s", strerror(errno));
1630Sstevel@tonic-gate 	temporarily_use_uid_effective = 0;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Permanently sets all uids to the given uid.  This cannot be
1680Sstevel@tonic-gate  * called while temporarily_use_uid is effective.
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate void
1710Sstevel@tonic-gate permanently_set_uid(struct passwd *pw)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	if (temporarily_use_uid_effective)
1740Sstevel@tonic-gate 		fatal("permanently_set_uid: temporarily_use_uid effective");
1750Sstevel@tonic-gate 	debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
1760Sstevel@tonic-gate 	    (u_int)pw->pw_gid);
1770Sstevel@tonic-gate 	if (initgroups(pw->pw_name, pw->pw_gid) < 0)
1780Sstevel@tonic-gate 		fatal("initgroups: %s: %.100s", pw->pw_name,
1790Sstevel@tonic-gate 		    strerror(errno));
1800Sstevel@tonic-gate 	if (setgid(pw->pw_gid) < 0)
1810Sstevel@tonic-gate 		fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
1820Sstevel@tonic-gate 	if (setuid(pw->pw_uid) < 0)
1830Sstevel@tonic-gate 		fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
1840Sstevel@tonic-gate }
185