1 /* $Id: platform.c,v 1.18 2011/01/11 06:02:25 djm Exp $ */
4 * Copyright (c) 2006 Darren Tucker. All rights reserved.
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.
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.
21 #include <sys/types.h>
35 #include "openbsd-compat/openbsd-compat.h"
37 extern int use_privsep
;
38 extern ServerOptions options
;
41 platform_pre_listen(void)
43 #ifdef LINUX_OOM_ADJUST
44 /* Adjust out-of-memory killer so listening process is not killed */
50 platform_pre_fork(void)
52 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
53 solaris_contract_pre_fork();
58 platform_post_fork_parent(pid_t child_pid
)
60 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
61 solaris_contract_post_fork_parent(child_pid
);
66 platform_post_fork_child(void)
68 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
69 solaris_contract_post_fork_child();
71 #ifdef LINUX_OOM_ADJUST
76 /* return 1 if we are running with privilege to swap UIDs, 0 otherwise */
78 platform_privileged_uidswap(void)
81 /* uid 0 is not special on Cygwin so always try */
84 return (getuid() == 0 || geteuid() == 0);
89 * This gets called before switching UIDs, and is called even when sshd is
90 * not running as root.
93 platform_setusercontext(struct passwd
*pw
)
96 /* Cache selinux status for later use */
97 (void)ssh_selinux_enabled();
100 #ifdef USE_SOLARIS_PROJECTS
101 /* if solaris projects were detected, set the default now */
102 if (getuid() == 0 || geteuid() == 0)
103 solaris_set_default_project(pw
);
106 #if defined(HAVE_LOGIN_CAP) && defined (__bsdi__)
107 if (getuid() == 0 || geteuid() == 0)
111 #if defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
113 * If we have both LOGIN_CAP and PAM, we want to establish creds
114 * before calling setusercontext (in session.c:do_setusercontext).
116 if (getuid() == 0 || geteuid() == 0) {
117 if (options
.use_pam
) {
118 do_pam_setcred(use_privsep
);
121 # endif /* USE_PAM */
123 #if !defined(HAVE_LOGIN_CAP) && defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
124 if (getuid() == 0 || geteuid() == 0) {
125 /* Sets login uid for accounting */
126 if (getluid() == -1 && setluid(pw
->pw_uid
) == -1)
127 error("setluid: %s", strerror(errno
));
133 * This gets called after we've established the user's groups, and is only
134 * called if sshd is running as root.
137 platform_setusercontext_post_groups(struct passwd
*pw
)
139 #if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
141 * PAM credentials may take the form of supplementary groups.
142 * These will have been wiped by the above initgroups() call.
143 * Reestablish them here.
145 if (options
.use_pam
) {
146 do_pam_setcred(use_privsep
);
150 #if !defined(HAVE_LOGIN_CAP) && (defined(WITH_IRIX_PROJECT) || \
151 defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY))
152 irix_setusercontext(pw
);
153 #endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
159 #if !defined(HAVE_LOGIN_CAP) && defined(USE_LIBIAF)
160 if (set_id(pw
->pw_name
) != 0) {
163 # endif /* USE_LIBIAF */
167 * If we have a chroot directory, we set all creds except real
168 * uid which we will need for chroot. If we don't have a
169 * chroot directory, we don't override anything.
172 char **creds
= NULL
, *chroot_creds
[] =
173 { "REAL_USER=root", NULL
};
175 if (options
.chroot_directory
!= NULL
&&
176 strcasecmp(options
.chroot_directory
, "none") != 0)
177 creds
= chroot_creds
;
179 if (setpcred(pw
->pw_name
, creds
) == -1)
180 fatal("Failed to set process credentials");
182 #endif /* HAVE_SETPCRED */
184 ssh_selinux_setup_exec_context(pw
->pw_name
);
189 platform_krb5_get_principal_name(const char *pw_name
)
191 #ifdef USE_AIX_KRB_NAME
192 return aix_krb5_get_principal_name(pw_name
);