Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 8 | |
| 9 | #include "includes.h" |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 10 | RCSID("$Id: uidswap.c,v 1.5 2000/04/16 01:18:49 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 11 | |
| 12 | #include "ssh.h" |
| 13 | #include "uidswap.h" |
| 14 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 15 | /* |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 23 | |
| 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 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | |
| 29 | /* Saved effective uid. */ |
| 30 | static uid_t saved_euid = 0; |
| 31 | |
Damien Miller | ee1c0b3 | 2000-01-21 00:18:15 +1100 | [diff] [blame] | 32 | #endif /* _POSIX_SAVED_IDS */ |
| 33 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 34 | /* |
| 35 | * Temporarily changes to the given uid. If the effective user |
| 36 | * id is not root, this does nothing. This call cannot be nested. |
| 37 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 38 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 39 | temporarily_use_uid(uid_t uid) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | { |
| 41 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 42 | /* Save the current euid. */ |
| 43 | saved_euid = geteuid(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 45 | /* Set the effective uid to the given (unprivileged) uid. */ |
| 46 | if (seteuid(uid) == -1) |
| 47 | debug("seteuid %d: %.100s", (int) uid, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 48 | #else /* SAVED_IDS_WORK_WITH_SETUID */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 49 | /* Propagate the privileged uid to all of our uids. */ |
| 50 | if (setuid(geteuid()) < 0) |
| 51 | debug("setuid %d: %.100s", (int) geteuid(), strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 52 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 53 | /* Set the effective uid to the given (unprivileged) uid. */ |
| 54 | if (seteuid(uid) == -1) |
| 55 | debug("seteuid %d: %.100s", (int) uid, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | } |
| 58 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 59 | /* |
| 60 | * Restores to the original uid. |
| 61 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 62 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 63 | restore_uid() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | { |
| 65 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 66 | /* Set the effective uid back to the saved uid. */ |
| 67 | if (seteuid(saved_euid) < 0) |
| 68 | debug("seteuid %d: %.100s", (int) saved_euid, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | #else /* SAVED_IDS_WORK_WITH_SETEUID */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 70 | /* |
| 71 | * We are unable to restore the real uid to its unprivileged value. |
| 72 | * Propagate the real uid (usually more privileged) to effective uid |
| 73 | * as well. |
| 74 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 75 | setuid(getuid()); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ |
| 77 | } |
| 78 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 79 | /* |
| 80 | * Permanently sets all uids to the given uid. This cannot be |
| 81 | * called while temporarily_use_uid is effective. |
| 82 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 83 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 84 | permanently_set_uid(uid_t uid) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 85 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 86 | if (setuid(uid) < 0) |
| 87 | debug("setuid %d: %.100s", (int) uid, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | } |