blob: d81fde9d26d03d2fe199c3942b35a8c0816a72e5 [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
5 * Created: Sat Sep 9 01:56:14 1995 ylo
6 * Code for uid-swapping.
7 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10008
9#include "includes.h"
Damien Miller5428f641999-11-25 11:54:57 +110010RCSID("$Id: uidswap.c,v 1.3 1999/11/25 00:55:00 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100011
12#include "ssh.h"
13#include "uidswap.h"
14
Damien Miller95def091999-11-25 00:26:21 +110015/*
16 * Note: all these functions must work in all of the following cases:
17 * 1. euid=0, ruid=0
18 * 2. euid=0, ruid!=0
19 * 3. euid!=0, ruid!=0
20 * Additionally, they must work regardless of whether the system has
21 * POSIX saved uids or not.
22 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023
24#ifdef _POSIX_SAVED_IDS
25/* Lets assume that posix saved ids also work with seteuid, even though that
26 is not part of the posix specification. */
27#define SAVED_IDS_WORK_WITH_SETEUID
28#endif /* _POSIX_SAVED_IDS */
29
30/* Saved effective uid. */
31static uid_t saved_euid = 0;
32
Damien Miller95def091999-11-25 00:26:21 +110033/*
34 * Temporarily changes to the given uid. If the effective user
35 * id is not root, this does nothing. This call cannot be nested.
36 */
37void
38temporarily_use_uid(uid_t uid)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039{
40#ifdef SAVED_IDS_WORK_WITH_SETEUID
Damien Miller95def091999-11-25 00:26:21 +110041 /* Save the current euid. */
42 saved_euid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Damien Miller95def091999-11-25 00:26:21 +110044 /* Set the effective uid to the given (unprivileged) uid. */
45 if (seteuid(uid) == -1)
46 debug("seteuid %d: %.100s", (int) uid, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#else /* SAVED_IDS_WORK_WITH_SETUID */
Damien Miller95def091999-11-25 00:26:21 +110048 /* Propagate the privileged uid to all of our uids. */
49 if (setuid(geteuid()) < 0)
50 debug("setuid %d: %.100s", (int) geteuid(), strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Miller95def091999-11-25 00:26:21 +110052 /* Set the effective uid to the given (unprivileged) uid. */
53 if (seteuid(uid) == -1)
54 debug("seteuid %d: %.100s", (int) uid, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055#endif /* SAVED_IDS_WORK_WITH_SETEUID */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056}
57
Damien Miller95def091999-11-25 00:26:21 +110058/*
59 * Restores to the original uid.
60 */
61void
62restore_uid()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063{
64#ifdef SAVED_IDS_WORK_WITH_SETEUID
Damien Miller95def091999-11-25 00:26:21 +110065 /* Set the effective uid back to the saved uid. */
66 if (seteuid(saved_euid) < 0)
67 debug("seteuid %d: %.100s", (int) saved_euid, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068#else /* SAVED_IDS_WORK_WITH_SETEUID */
Damien Miller5428f641999-11-25 11:54:57 +110069 /*
70 * We are unable to restore the real uid to its unprivileged value.
71 * Propagate the real uid (usually more privileged) to effective uid
72 * as well.
73 */
Damien Miller95def091999-11-25 00:26:21 +110074 setuid(getuid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075#endif /* SAVED_IDS_WORK_WITH_SETEUID */
76}
77
Damien Miller95def091999-11-25 00:26:21 +110078/*
79 * Permanently sets all uids to the given uid. This cannot be
80 * called while temporarily_use_uid is effective.
81 */
82void
83permanently_set_uid(uid_t uid)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084{
Damien Miller95def091999-11-25 00:26:21 +110085 if (setuid(uid) < 0)
86 debug("setuid %d: %.100s", (int) uid, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087}