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 | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 10 | RCSID("$OpenBSD: uidswap.c,v 1.8 2000/08/28 03:50:54 deraadt Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 11 | |
| 12 | #include "ssh.h" |
| 13 | #include "uidswap.h" |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 14 | #ifdef WITH_IRIX_AUDIT |
| 15 | #include <sat.h> |
| 16 | #endif /* WITH_IRIX_AUDIT */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 17 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 18 | /* |
| 19 | * Note: all these functions must work in all of the following cases: |
| 20 | * 1. euid=0, ruid=0 |
| 21 | * 2. euid=0, ruid!=0 |
| 22 | * 3. euid!=0, ruid!=0 |
| 23 | * Additionally, they must work regardless of whether the system has |
| 24 | * POSIX saved uids or not. |
| 25 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 26 | |
| 27 | #ifdef _POSIX_SAVED_IDS |
| 28 | /* Lets assume that posix saved ids also work with seteuid, even though that |
| 29 | is not part of the posix specification. */ |
| 30 | #define SAVED_IDS_WORK_WITH_SETEUID |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 31 | |
| 32 | /* Saved effective uid. */ |
| 33 | static uid_t saved_euid = 0; |
| 34 | |
Damien Miller | ee1c0b3 | 2000-01-21 00:18:15 +1100 | [diff] [blame] | 35 | #endif /* _POSIX_SAVED_IDS */ |
| 36 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | /* |
| 38 | * Temporarily changes to the given uid. If the effective user |
| 39 | * id is not root, this does nothing. This call cannot be nested. |
| 40 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 41 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 42 | temporarily_use_uid(uid_t uid) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 43 | { |
| 44 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 45 | /* Save the current euid. */ |
| 46 | saved_euid = geteuid(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 47 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 48 | /* Set the effective uid to the given (unprivileged) uid. */ |
| 49 | if (seteuid(uid) == -1) |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 50 | debug("seteuid %u: %.100s", (u_int) uid, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | #else /* SAVED_IDS_WORK_WITH_SETUID */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 52 | /* Propagate the privileged uid to all of our uids. */ |
| 53 | if (setuid(geteuid()) < 0) |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 54 | debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 55 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 56 | /* Set the effective uid to the given (unprivileged) uid. */ |
| 57 | if (seteuid(uid) == -1) |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 58 | debug("seteuid %u: %.100s", (u_int) uid, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | } |
| 61 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 62 | /* |
| 63 | * Restores to the original uid. |
| 64 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 65 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 66 | restore_uid() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 67 | { |
| 68 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 69 | /* Set the effective uid back to the saved uid. */ |
| 70 | if (seteuid(saved_euid) < 0) |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 71 | debug("seteuid %u: %.100s", (u_int) saved_euid, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | #else /* SAVED_IDS_WORK_WITH_SETEUID */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 73 | /* |
| 74 | * We are unable to restore the real uid to its unprivileged value. |
| 75 | * Propagate the real uid (usually more privileged) to effective uid |
| 76 | * as well. |
| 77 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 78 | setuid(getuid()); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ |
| 80 | } |
| 81 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 82 | /* |
| 83 | * Permanently sets all uids to the given uid. This cannot be |
| 84 | * called while temporarily_use_uid is effective. |
| 85 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 86 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 87 | permanently_set_uid(uid_t uid) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | { |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 89 | #ifdef WITH_IRIX_AUDIT |
| 90 | if (sysconf(_SC_AUDIT)) { |
| 91 | debug("Setting sat id to %d", (int) uid); |
| 92 | if (satsetid(uid)) |
Damien Miller | c83aa83 | 2000-08-15 10:08:00 +1000 | [diff] [blame] | 93 | debug("error setting satid: %.100s", strerror(errno)); |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 94 | } |
| 95 | #endif /* WITH_IRIX_AUDIT */ |
| 96 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 97 | if (setuid(uid) < 0) |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 98 | debug("setuid %u: %.100s", (u_int) uid, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 99 | } |