blob: 76cbd6c4064319bac79f203508b86adfaab5f20f [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Code for uid-swapping.
Damien Millere4340be2000-09-16 13:29:08 +11006 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millere4340be2000-09-16 13:29:08 +110015RCSID("$OpenBSD: uidswap.c,v 1.9 2000/09/07 20:27:55 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "ssh.h"
18#include "uidswap.h"
19
Damien Miller95def091999-11-25 00:26:21 +110020/*
21 * Note: all these functions must work in all of the following cases:
22 * 1. euid=0, ruid=0
23 * 2. euid=0, ruid!=0
24 * 3. euid!=0, ruid!=0
25 * Additionally, they must work regardless of whether the system has
26 * POSIX saved uids or not.
27 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
29#ifdef _POSIX_SAVED_IDS
30/* Lets assume that posix saved ids also work with seteuid, even though that
31 is not part of the posix specification. */
32#define SAVED_IDS_WORK_WITH_SETEUID
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
34/* Saved effective uid. */
35static uid_t saved_euid = 0;
36
Damien Milleree1c0b32000-01-21 00:18:15 +110037#endif /* _POSIX_SAVED_IDS */
38
Damien Miller95def091999-11-25 00:26:21 +110039/*
40 * Temporarily changes to the given uid. If the effective user
41 * id is not root, this does nothing. This call cannot be nested.
42 */
Damien Miller4af51302000-04-16 11:18:38 +100043void
Damien Miller95def091999-11-25 00:26:21 +110044temporarily_use_uid(uid_t uid)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045{
46#ifdef SAVED_IDS_WORK_WITH_SETEUID
Damien Miller95def091999-11-25 00:26:21 +110047 /* Save the current euid. */
48 saved_euid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
Damien Miller95def091999-11-25 00:26:21 +110050 /* Set the effective uid to the given (unprivileged) uid. */
51 if (seteuid(uid) == -1)
Damien Millercaf6dd62000-08-29 11:33:50 +110052 debug("seteuid %u: %.100s", (u_int) uid, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#else /* SAVED_IDS_WORK_WITH_SETUID */
Damien Miller95def091999-11-25 00:26:21 +110054 /* Propagate the privileged uid to all of our uids. */
55 if (setuid(geteuid()) < 0)
Damien Millercaf6dd62000-08-29 11:33:50 +110056 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Damien Miller95def091999-11-25 00:26:21 +110058 /* Set the effective uid to the given (unprivileged) uid. */
59 if (seteuid(uid) == -1)
Damien Millercaf6dd62000-08-29 11:33:50 +110060 debug("seteuid %u: %.100s", (u_int) uid, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#endif /* SAVED_IDS_WORK_WITH_SETEUID */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062}
63
Damien Miller95def091999-11-25 00:26:21 +110064/*
65 * Restores to the original uid.
66 */
Damien Miller4af51302000-04-16 11:18:38 +100067void
Damien Miller95def091999-11-25 00:26:21 +110068restore_uid()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069{
70#ifdef SAVED_IDS_WORK_WITH_SETEUID
Damien Miller95def091999-11-25 00:26:21 +110071 /* Set the effective uid back to the saved uid. */
72 if (seteuid(saved_euid) < 0)
Damien Millercaf6dd62000-08-29 11:33:50 +110073 debug("seteuid %u: %.100s", (u_int) saved_euid, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074#else /* SAVED_IDS_WORK_WITH_SETEUID */
Damien Miller5428f641999-11-25 11:54:57 +110075 /*
76 * We are unable to restore the real uid to its unprivileged value.
77 * Propagate the real uid (usually more privileged) to effective uid
78 * as well.
79 */
Damien Miller95def091999-11-25 00:26:21 +110080 setuid(getuid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081#endif /* SAVED_IDS_WORK_WITH_SETEUID */
82}
83
Damien Miller95def091999-11-25 00:26:21 +110084/*
85 * Permanently sets all uids to the given uid. This cannot be
86 * called while temporarily_use_uid is effective.
87 */
Damien Miller4af51302000-04-16 11:18:38 +100088void
Damien Miller95def091999-11-25 00:26:21 +110089permanently_set_uid(uid_t uid)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090{
Damien Miller95def091999-11-25 00:26:21 +110091 if (setuid(uid) < 0)
Damien Millercaf6dd62000-08-29 11:33:50 +110092 debug("setuid %u: %.100s", (u_int) uid, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093}