2 * Copyright (c) 2006 Darren Tucker. All rights reserved.
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.
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.
33 #include "openbsd-compat/openbsd-compat.h"
35 extern int use_privsep;
36 extern ServerOptions options;
39 platform_pre_listen(void)
41 #ifdef LINUX_OOM_ADJUST
42 /* Adjust out-of-memory killer so listening process is not killed */
48 platform_pre_fork(void)
50 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
51 solaris_contract_pre_fork();
56 platform_pre_restart(void)
58 #ifdef LINUX_OOM_ADJUST
64 platform_post_fork_parent(pid_t child_pid)
66 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
67 solaris_contract_post_fork_parent(child_pid);
72 platform_post_fork_child(void)
74 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
75 solaris_contract_post_fork_child();
77 #ifdef LINUX_OOM_ADJUST
82 /* return 1 if we are running with privilege to swap UIDs, 0 otherwise */
84 platform_privileged_uidswap(void)
87 /* uid 0 is not special on Cygwin so always try */
90 return (getuid() == 0 || geteuid() == 0);
95 * This gets called before switching UIDs, and is called even when sshd is
96 * not running as root.
99 platform_setusercontext(struct passwd *pw)
102 /* Cache selinux status for later use */
103 (void)ssh_selinux_enabled();
106 #ifdef USE_SOLARIS_PROJECTS
108 * If solaris projects were detected, set the default now, unless
109 * we are using PAM in which case it is the responsibility of the
112 if (!options.use_pam && (getuid() == 0 || geteuid() == 0))
113 solaris_set_default_project(pw);
116 #if defined(HAVE_LOGIN_CAP) && defined (__bsdi__)
117 if (getuid() == 0 || geteuid() == 0)
121 #if defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
123 * If we have both LOGIN_CAP and PAM, we want to establish creds
124 * before calling setusercontext (in session.c:do_setusercontext).
126 if (getuid() == 0 || geteuid() == 0) {
127 if (options.use_pam) {
128 do_pam_setcred(use_privsep);
131 # endif /* USE_PAM */
133 #if !defined(HAVE_LOGIN_CAP) && defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
134 if (getuid() == 0 || geteuid() == 0) {
135 /* Sets login uid for accounting */
136 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
137 error("setluid: %s", strerror(errno));
143 * This gets called after we've established the user's groups, and is only
144 * called if sshd is running as root.
147 platform_setusercontext_post_groups(struct passwd *pw)
149 #if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
151 * PAM credentials may take the form of supplementary groups.
152 * These will have been wiped by the above initgroups() call.
153 * Reestablish them here.
155 if (options.use_pam) {
156 do_pam_setcred(use_privsep);
160 #if !defined(HAVE_LOGIN_CAP) && (defined(WITH_IRIX_PROJECT) || \
161 defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY))
162 irix_setusercontext(pw);
163 #endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
171 * If we have a chroot directory, we set all creds except real
172 * uid which we will need for chroot. If we don't have a
173 * chroot directory, we don't override anything.
176 char **creds = NULL, *chroot_creds[] =
177 { "REAL_USER=root", NULL };
179 if (options.chroot_directory != NULL &&
180 strcasecmp(options.chroot_directory, "none") != 0)
181 creds = chroot_creds;
183 if (setpcred(pw->pw_name, creds) == -1)
184 fatal("Failed to set process credentials");
186 #endif /* HAVE_SETPCRED */
188 ssh_selinux_setup_exec_context(pw->pw_name);
193 platform_krb5_get_principal_name(const char *pw_name)
195 #ifdef USE_AIX_KRB_NAME
196 return aix_krb5_get_principal_name(pw_name);
202 /* returns 1 if account is locked */
204 platform_locked_account(struct passwd *pw)
207 char *passwd = pw->pw_passwd;
209 struct spwd *spw = NULL;
211 char *iaf_passwd = NULL;
214 spw = getspnam(pw->pw_name);
215 #ifdef HAS_SHADOW_EXPIRE
216 if (spw != NULL && auth_shadow_acctexpired(spw))
218 #endif /* HAS_SHADOW_EXPIRE */
222 iaf_passwd = passwd = get_iaf_password(pw);
224 passwd = spw->sp_pwdp;
225 #endif /* USE_LIBIAF */
228 /* check for locked account */
229 if (passwd && *passwd) {
230 #ifdef LOCKED_PASSWD_STRING
231 if (strcmp(passwd, LOCKED_PASSWD_STRING) == 0)
234 #ifdef LOCKED_PASSWD_PREFIX
235 if (strncmp(passwd, LOCKED_PASSWD_PREFIX,
236 strlen(LOCKED_PASSWD_PREFIX)) == 0)
239 #ifdef LOCKED_PASSWD_SUBSTR
240 if (strstr(passwd, LOCKED_PASSWD_SUBSTR))
245 if (iaf_passwd != NULL)
246 freezero(iaf_passwd, strlen(iaf_passwd));
247 #endif /* USE_LIBIAF */