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