Darren Tucker | b12fe27 | 2010-11-05 14:47:01 +1100 | [diff] [blame^] | 1 | /* $Id: platform.c,v 1.14 2010/11/05 03:47:01 dtucker 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 | |
| 19 | #include "config.h" |
Darren Tucker | b12fe27 | 2010-11-05 14:47:01 +1100 | [diff] [blame^] | 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 24 | #include "platform.h" |
| 25 | |
| 26 | #include "openbsd-compat/openbsd-compat.h" |
| 27 | |
Darren Tucker | 728d837 | 2010-11-05 13:00:05 +1100 | [diff] [blame] | 28 | extern int use_privsep; |
| 29 | |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 30 | void |
Darren Tucker | c8802aa | 2009-12-08 13:39:48 +1100 | [diff] [blame] | 31 | platform_pre_listen(void) |
| 32 | { |
| 33 | #ifdef LINUX_OOM_ADJUST |
| 34 | /* Adjust out-of-memory killer so listening process is not killed */ |
| 35 | oom_adjust_setup(); |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | void |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 40 | platform_pre_fork(void) |
| 41 | { |
| 42 | #ifdef USE_SOLARIS_PROCESS_CONTRACTS |
| 43 | solaris_contract_pre_fork(); |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | platform_post_fork_parent(pid_t child_pid) |
| 49 | { |
| 50 | #ifdef USE_SOLARIS_PROCESS_CONTRACTS |
| 51 | solaris_contract_post_fork_parent(child_pid); |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | platform_post_fork_child(void) |
| 57 | { |
| 58 | #ifdef USE_SOLARIS_PROCESS_CONTRACTS |
| 59 | solaris_contract_post_fork_child(); |
| 60 | #endif |
Darren Tucker | c8802aa | 2009-12-08 13:39:48 +1100 | [diff] [blame] | 61 | #ifdef LINUX_OOM_ADJUST |
| 62 | oom_adjust_restore(); |
| 63 | #endif |
Damien Miller | 1b06dc3 | 2006-08-31 03:24:41 +1000 | [diff] [blame] | 64 | } |
Darren Tucker | 1bf3503 | 2009-12-21 10:49:21 +1100 | [diff] [blame] | 65 | |
Darren Tucker | b12fe27 | 2010-11-05 14:47:01 +1100 | [diff] [blame^] | 66 | /* return 1 if we are running with privilege to swap UIDs, 0 otherwise */ |
| 67 | int |
| 68 | platform_privileged_uidswap(void) |
| 69 | { |
| 70 | #ifdef HAVE_CYGWIN |
| 71 | /* uid 0 is not special on Cygwin so always try */ |
| 72 | return 1; |
| 73 | #else |
| 74 | return (getuid() == 0 || geteuid() == 0); |
| 75 | #endif |
| 76 | } |
| 77 | |
Darren Tucker | 920612e | 2010-11-05 12:36:15 +1100 | [diff] [blame] | 78 | /* |
| 79 | * This gets called before switching UIDs, and is called even when sshd is |
| 80 | * not running as root. |
| 81 | */ |
Darren Tucker | 9752835 | 2010-11-05 12:03:05 +1100 | [diff] [blame] | 82 | void |
| 83 | platform_setusercontext(struct passwd *pw) |
| 84 | { |
Darren Tucker | 920612e | 2010-11-05 12:36:15 +1100 | [diff] [blame] | 85 | #ifdef WITH_SELINUX |
| 86 | /* Cache selinux status for later use */ |
| 87 | (void)ssh_selinux_enabled(); |
| 88 | #endif |
| 89 | |
Darren Tucker | 9752835 | 2010-11-05 12:03:05 +1100 | [diff] [blame] | 90 | #ifdef USE_SOLARIS_PROJECTS |
| 91 | /* if solaris projects were detected, set the default now */ |
| 92 | if (getuid() == 0 || geteuid() == 0) |
| 93 | solaris_set_default_project(pw); |
| 94 | #endif |
Darren Tucker | 44a97be | 2010-11-05 12:45:18 +1100 | [diff] [blame] | 95 | |
| 96 | #if defined(HAVE_LOGIN_CAP) && defined (__bsdi__) |
Darren Tucker | fd4d8aa | 2010-11-05 12:50:41 +1100 | [diff] [blame] | 97 | if (getuid() == 0 || geteuid() == 0) |
| 98 | setpgid(0, 0); |
Darren Tucker | 44a97be | 2010-11-05 12:45:18 +1100 | [diff] [blame] | 99 | # endif |
Darren Tucker | 728d837 | 2010-11-05 13:00:05 +1100 | [diff] [blame] | 100 | |
| 101 | #if defined(HAVE_LOGIN_CAP) && defined(USE_PAM) |
| 102 | /* |
| 103 | * If we have both LOGIN_CAP and PAM, we want to establish creds |
| 104 | * before calling setusercontext (in session.c:do_setusercontext). |
| 105 | */ |
| 106 | if (getuid() == 0 || geteuid() == 0) { |
| 107 | if (options.use_pam) { |
| 108 | do_pam_setcred(use_privsep); |
| 109 | } |
| 110 | } |
| 111 | # endif /* USE_PAM */ |
Darren Tucker | 9752835 | 2010-11-05 12:03:05 +1100 | [diff] [blame] | 112 | } |
| 113 | |
Darren Tucker | 920612e | 2010-11-05 12:36:15 +1100 | [diff] [blame] | 114 | /* |
| 115 | * This gets called after we've established the user's groups, and is only |
| 116 | * called if sshd is running as root. |
| 117 | */ |
| 118 | void |
| 119 | platform_setusercontext_post_groups(struct passwd *pw) |
| 120 | { |
Darren Tucker | cc12418 | 2010-11-05 13:32:52 +1100 | [diff] [blame] | 121 | #if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM) |
| 122 | /* |
| 123 | * PAM credentials may take the form of supplementary groups. |
| 124 | * These will have been wiped by the above initgroups() call. |
| 125 | * Reestablish them here. |
| 126 | */ |
| 127 | if (options.use_pam) { |
| 128 | do_pam_setcred(use_privsep); |
| 129 | } |
| 130 | #endif /* USE_PAM */ |
| 131 | |
Darren Tucker | 0b2ee64 | 2010-11-05 13:29:25 +1100 | [diff] [blame] | 132 | #if !defined(HAVE_LOGIN_CAP) && (defined(WITH_IRIX_PROJECT) || \ |
| 133 | defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)) |
| 134 | irix_setusercontext(pw); |
| 135 | #endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */ |
| 136 | |
Darren Tucker | 676b912 | 2010-11-05 13:11:04 +1100 | [diff] [blame] | 137 | #ifdef _AIX |
| 138 | aix_usrinfo(pw); |
| 139 | #endif /* _AIX */ |
| 140 | |
Darren Tucker | 7a8afe3 | 2010-11-05 13:07:24 +1100 | [diff] [blame] | 141 | #if !defined(HAVE_LOGIN_CAP) && defined(USE_LIBIAF) |
| 142 | if (set_id(pw->pw_name) != 0) { |
| 143 | exit(1); |
| 144 | } |
| 145 | # endif /* USE_LIBIAF */ |
| 146 | |
Darren Tucker | 4db3807 | 2010-11-05 12:41:13 +1100 | [diff] [blame] | 147 | #ifdef HAVE_SETPCRED |
| 148 | /* |
| 149 | * If we have a chroot directory, we set all creds except real |
| 150 | * uid which we will need for chroot. If we don't have a |
| 151 | * chroot directory, we don't override anything. |
| 152 | */ |
| 153 | { |
| 154 | char **creds = NULL, *chroot_creds[] = |
| 155 | { "REAL_USER=root", NULL }; |
| 156 | |
| 157 | if (options.chroot_directory != NULL && |
| 158 | strcasecmp(options.chroot_directory, "none") != 0) |
| 159 | creds = chroot_creds; |
| 160 | |
| 161 | if (setpcred(pw->pw_name, creds) == -1) |
| 162 | fatal("Failed to set process credentials"); |
| 163 | } |
| 164 | #endif /* HAVE_SETPCRED */ |
Darren Tucker | 920612e | 2010-11-05 12:36:15 +1100 | [diff] [blame] | 165 | #ifdef WITH_SELINUX |
| 166 | ssh_selinux_setup_exec_context(pw->pw_name); |
| 167 | #endif |
| 168 | } |
| 169 | |
Darren Tucker | 1bf3503 | 2009-12-21 10:49:21 +1100 | [diff] [blame] | 170 | char * |
| 171 | platform_krb5_get_principal_name(const char *pw_name) |
| 172 | { |
| 173 | #ifdef USE_AIX_KRB_NAME |
| 174 | return aix_krb5_get_principal_name(pw_name); |
| 175 | #else |
| 176 | return NULL; |
| 177 | #endif |
| 178 | } |