Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Darren Tucker. All rights reserved. |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
Darren Tucker | d1ece6e | 2010-11-07 18:05:54 +1100 | [diff] [blame] | 17 | #include "includes.h" |
Darren Tucker | b12fe27 | 2010-11-05 14:47:01 +1100 | [diff] [blame] | 18 | |
Darren Tucker | 9283d8c | 2010-11-05 18:56:08 +1100 | [diff] [blame] | 19 | #include <stdarg.h> |
Darren Tucker | b12fe27 | 2010-11-05 14:47:01 +1100 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
Darren Tucker | 9283d8c | 2010-11-05 18:56:08 +1100 | [diff] [blame] | 22 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 23 | #include "misc.h" |
Darren Tucker | 9283d8c | 2010-11-05 18:56:08 +1100 | [diff] [blame] | 24 | #include "servconf.h" |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 25 | #include "sshkey.h" |
Damien Miller | 81ad4b1 | 2011-01-11 17:02:23 +1100 | [diff] [blame] | 26 | #include "hostfile.h" |
| 27 | #include "auth.h" |
| 28 | #include "auth-pam.h" |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 29 | #include "platform.h" |
| 30 | |
| 31 | #include "openbsd-compat/openbsd-compat.h" |
| 32 | |
Darren Tucker | 728d837 | 2010-11-05 13:00:05 +1100 | [diff] [blame] | 33 | extern int use_privsep; |
Darren Tucker | 9283d8c | 2010-11-05 18:56:08 +1100 | [diff] [blame] | 34 | extern ServerOptions options; |
Darren Tucker | 728d837 | 2010-11-05 13:00:05 +1100 | [diff] [blame] | 35 | |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 36 | void |
Darren Tucker | c8802aa | 2009-12-08 13:39:48 +1100 | [diff] [blame] | 37 | platform_pre_listen(void) |
| 38 | { |
| 39 | #ifdef LINUX_OOM_ADJUST |
| 40 | /* Adjust out-of-memory killer so listening process is not killed */ |
| 41 | oom_adjust_setup(); |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | void |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 46 | platform_pre_fork(void) |
| 47 | { |
| 48 | #ifdef USE_SOLARIS_PROCESS_CONTRACTS |
| 49 | solaris_contract_pre_fork(); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | void |
Darren Tucker | f2bf36c | 2013-09-22 19:02:40 +1000 | [diff] [blame] | 54 | platform_pre_restart(void) |
| 55 | { |
| 56 | #ifdef LINUX_OOM_ADJUST |
| 57 | oom_adjust_restore(); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | void |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 62 | platform_post_fork_parent(pid_t child_pid) |
| 63 | { |
| 64 | #ifdef USE_SOLARIS_PROCESS_CONTRACTS |
| 65 | solaris_contract_post_fork_parent(child_pid); |
| 66 | #endif |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | platform_post_fork_child(void) |
| 71 | { |
| 72 | #ifdef USE_SOLARIS_PROCESS_CONTRACTS |
| 73 | solaris_contract_post_fork_child(); |
| 74 | #endif |
Darren Tucker | c8802aa | 2009-12-08 13:39:48 +1100 | [diff] [blame] | 75 | #ifdef LINUX_OOM_ADJUST |
| 76 | oom_adjust_restore(); |
| 77 | #endif |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 78 | } |
Darren Tucker | 1bf3503 | 2009-12-21 10:49:21 +1100 | [diff] [blame] | 79 | |
Darren Tucker | b12fe27 | 2010-11-05 14:47:01 +1100 | [diff] [blame] | 80 | /* return 1 if we are running with privilege to swap UIDs, 0 otherwise */ |
| 81 | int |
| 82 | platform_privileged_uidswap(void) |
| 83 | { |
| 84 | #ifdef HAVE_CYGWIN |
| 85 | /* uid 0 is not special on Cygwin so always try */ |
| 86 | return 1; |
| 87 | #else |
| 88 | return (getuid() == 0 || geteuid() == 0); |
| 89 | #endif |
| 90 | } |
| 91 | |
Darren Tucker | 920612e | 2010-11-05 12:36:15 +1100 | [diff] [blame] | 92 | /* |
| 93 | * This gets called before switching UIDs, and is called even when sshd is |
| 94 | * not running as root. |
| 95 | */ |
Darren Tucker | 9752835 | 2010-11-05 12:03:05 +1100 | [diff] [blame] | 96 | void |
| 97 | platform_setusercontext(struct passwd *pw) |
| 98 | { |
Darren Tucker | 920612e | 2010-11-05 12:36:15 +1100 | [diff] [blame] | 99 | #ifdef WITH_SELINUX |
| 100 | /* Cache selinux status for later use */ |
| 101 | (void)ssh_selinux_enabled(); |
| 102 | #endif |
| 103 | |
Darren Tucker | 9752835 | 2010-11-05 12:03:05 +1100 | [diff] [blame] | 104 | #ifdef USE_SOLARIS_PROJECTS |
Darren Tucker | 8c47393 | 2016-03-15 09:24:43 +1100 | [diff] [blame] | 105 | /* |
| 106 | * If solaris projects were detected, set the default now, unless |
| 107 | * we are using PAM in which case it is the responsibility of the |
| 108 | * PAM stack. |
| 109 | */ |
| 110 | if (!options.use_pam && (getuid() == 0 || geteuid() == 0)) |
Darren Tucker | 9752835 | 2010-11-05 12:03:05 +1100 | [diff] [blame] | 111 | solaris_set_default_project(pw); |
| 112 | #endif |
Darren Tucker | 44a97be | 2010-11-05 12:45:18 +1100 | [diff] [blame] | 113 | |
| 114 | #if defined(HAVE_LOGIN_CAP) && defined (__bsdi__) |
Darren Tucker | fd4d8aa | 2010-11-05 12:50:41 +1100 | [diff] [blame] | 115 | if (getuid() == 0 || geteuid() == 0) |
| 116 | setpgid(0, 0); |
Darren Tucker | 44a97be | 2010-11-05 12:45:18 +1100 | [diff] [blame] | 117 | # endif |
Darren Tucker | 728d837 | 2010-11-05 13:00:05 +1100 | [diff] [blame] | 118 | |
| 119 | #if defined(HAVE_LOGIN_CAP) && defined(USE_PAM) |
| 120 | /* |
| 121 | * If we have both LOGIN_CAP and PAM, we want to establish creds |
| 122 | * before calling setusercontext (in session.c:do_setusercontext). |
| 123 | */ |
| 124 | if (getuid() == 0 || geteuid() == 0) { |
| 125 | if (options.use_pam) { |
| 126 | do_pam_setcred(use_privsep); |
| 127 | } |
| 128 | } |
| 129 | # endif /* USE_PAM */ |
Darren Tucker | d995712 | 2010-11-24 10:09:13 +1100 | [diff] [blame] | 130 | |
| 131 | #if !defined(HAVE_LOGIN_CAP) && defined(HAVE_GETLUID) && defined(HAVE_SETLUID) |
| 132 | if (getuid() == 0 || geteuid() == 0) { |
| 133 | /* Sets login uid for accounting */ |
| 134 | if (getluid() == -1 && setluid(pw->pw_uid) == -1) |
| 135 | error("setluid: %s", strerror(errno)); |
| 136 | } |
| 137 | #endif |
Darren Tucker | 9752835 | 2010-11-05 12:03:05 +1100 | [diff] [blame] | 138 | } |
| 139 | |
Darren Tucker | 920612e | 2010-11-05 12:36:15 +1100 | [diff] [blame] | 140 | /* |
| 141 | * This gets called after we've established the user's groups, and is only |
| 142 | * called if sshd is running as root. |
| 143 | */ |
| 144 | void |
| 145 | platform_setusercontext_post_groups(struct passwd *pw) |
| 146 | { |
Darren Tucker | cc12418 | 2010-11-05 13:32:52 +1100 | [diff] [blame] | 147 | #if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM) |
| 148 | /* |
| 149 | * PAM credentials may take the form of supplementary groups. |
| 150 | * These will have been wiped by the above initgroups() call. |
| 151 | * Reestablish them here. |
| 152 | */ |
| 153 | if (options.use_pam) { |
| 154 | do_pam_setcred(use_privsep); |
| 155 | } |
| 156 | #endif /* USE_PAM */ |
| 157 | |
Darren Tucker | 0b2ee64 | 2010-11-05 13:29:25 +1100 | [diff] [blame] | 158 | #if !defined(HAVE_LOGIN_CAP) && (defined(WITH_IRIX_PROJECT) || \ |
| 159 | defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)) |
| 160 | irix_setusercontext(pw); |
| 161 | #endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */ |
| 162 | |
Darren Tucker | 676b912 | 2010-11-05 13:11:04 +1100 | [diff] [blame] | 163 | #ifdef _AIX |
| 164 | aix_usrinfo(pw); |
| 165 | #endif /* _AIX */ |
| 166 | |
Darren Tucker | 4db3807 | 2010-11-05 12:41:13 +1100 | [diff] [blame] | 167 | #ifdef HAVE_SETPCRED |
| 168 | /* |
| 169 | * If we have a chroot directory, we set all creds except real |
| 170 | * uid which we will need for chroot. If we don't have a |
| 171 | * chroot directory, we don't override anything. |
| 172 | */ |
| 173 | { |
| 174 | char **creds = NULL, *chroot_creds[] = |
| 175 | { "REAL_USER=root", NULL }; |
| 176 | |
| 177 | if (options.chroot_directory != NULL && |
| 178 | strcasecmp(options.chroot_directory, "none") != 0) |
| 179 | creds = chroot_creds; |
| 180 | |
| 181 | if (setpcred(pw->pw_name, creds) == -1) |
| 182 | fatal("Failed to set process credentials"); |
| 183 | } |
| 184 | #endif /* HAVE_SETPCRED */ |
Darren Tucker | 920612e | 2010-11-05 12:36:15 +1100 | [diff] [blame] | 185 | #ifdef WITH_SELINUX |
| 186 | ssh_selinux_setup_exec_context(pw->pw_name); |
| 187 | #endif |
| 188 | } |
| 189 | |
Darren Tucker | 1bf3503 | 2009-12-21 10:49:21 +1100 | [diff] [blame] | 190 | char * |
| 191 | platform_krb5_get_principal_name(const char *pw_name) |
| 192 | { |
| 193 | #ifdef USE_AIX_KRB_NAME |
| 194 | return aix_krb5_get_principal_name(pw_name); |
| 195 | #else |
| 196 | return NULL; |
| 197 | #endif |
| 198 | } |