blob: 0d5b4f1bcedf207b4f6845ca62ad57557e54a84b [file] [log] [blame]
Darren Tucker9283d8c2010-11-05 18:56:08 +11001/* $Id: platform.c,v 1.15 2010/11/05 07:56:08 dtucker Exp $ */
Damien Miller1b06dc32006-08-31 03:24:41 +10002
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 Tuckerb12fe272010-11-05 14:47:01 +110020
21#include <sys/types.h>
Darren Tucker9283d8c2010-11-05 18:56:08 +110022
23#include <stdarg.h>
Darren Tuckerb12fe272010-11-05 14:47:01 +110024#include <unistd.h>
25
Darren Tucker9283d8c2010-11-05 18:56:08 +110026#include "log.h"
27#include "buffer.h"
28#include "servconf.h"
Damien Miller1b06dc32006-08-31 03:24:41 +100029#include "platform.h"
30
31#include "openbsd-compat/openbsd-compat.h"
32
Darren Tucker728d8372010-11-05 13:00:05 +110033extern int use_privsep;
Darren Tucker9283d8c2010-11-05 18:56:08 +110034extern ServerOptions options;
Darren Tucker728d8372010-11-05 13:00:05 +110035
Damien Miller1b06dc32006-08-31 03:24:41 +100036void
Darren Tuckerc8802aa2009-12-08 13:39:48 +110037platform_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
45void
Damien Miller1b06dc32006-08-31 03:24:41 +100046platform_pre_fork(void)
47{
48#ifdef USE_SOLARIS_PROCESS_CONTRACTS
49 solaris_contract_pre_fork();
50#endif
51}
52
53void
54platform_post_fork_parent(pid_t child_pid)
55{
56#ifdef USE_SOLARIS_PROCESS_CONTRACTS
57 solaris_contract_post_fork_parent(child_pid);
58#endif
59}
60
61void
62platform_post_fork_child(void)
63{
64#ifdef USE_SOLARIS_PROCESS_CONTRACTS
65 solaris_contract_post_fork_child();
66#endif
Darren Tuckerc8802aa2009-12-08 13:39:48 +110067#ifdef LINUX_OOM_ADJUST
68 oom_adjust_restore();
69#endif
Damien Miller1b06dc32006-08-31 03:24:41 +100070}
Darren Tucker1bf35032009-12-21 10:49:21 +110071
Darren Tuckerb12fe272010-11-05 14:47:01 +110072/* return 1 if we are running with privilege to swap UIDs, 0 otherwise */
73int
74platform_privileged_uidswap(void)
75{
76#ifdef HAVE_CYGWIN
77 /* uid 0 is not special on Cygwin so always try */
78 return 1;
79#else
80 return (getuid() == 0 || geteuid() == 0);
81#endif
82}
83
Darren Tucker920612e2010-11-05 12:36:15 +110084/*
85 * This gets called before switching UIDs, and is called even when sshd is
86 * not running as root.
87 */
Darren Tucker97528352010-11-05 12:03:05 +110088void
89platform_setusercontext(struct passwd *pw)
90{
Darren Tucker920612e2010-11-05 12:36:15 +110091#ifdef WITH_SELINUX
92 /* Cache selinux status for later use */
93 (void)ssh_selinux_enabled();
94#endif
95
Darren Tucker97528352010-11-05 12:03:05 +110096#ifdef USE_SOLARIS_PROJECTS
97 /* if solaris projects were detected, set the default now */
98 if (getuid() == 0 || geteuid() == 0)
99 solaris_set_default_project(pw);
100#endif
Darren Tucker44a97be2010-11-05 12:45:18 +1100101
102#if defined(HAVE_LOGIN_CAP) && defined (__bsdi__)
Darren Tuckerfd4d8aa2010-11-05 12:50:41 +1100103 if (getuid() == 0 || geteuid() == 0)
104 setpgid(0, 0);
Darren Tucker44a97be2010-11-05 12:45:18 +1100105# endif
Darren Tucker728d8372010-11-05 13:00:05 +1100106
107#if defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
108 /*
109 * If we have both LOGIN_CAP and PAM, we want to establish creds
110 * before calling setusercontext (in session.c:do_setusercontext).
111 */
112 if (getuid() == 0 || geteuid() == 0) {
113 if (options.use_pam) {
114 do_pam_setcred(use_privsep);
115 }
116 }
117# endif /* USE_PAM */
Darren Tucker97528352010-11-05 12:03:05 +1100118}
119
Darren Tucker920612e2010-11-05 12:36:15 +1100120/*
121 * This gets called after we've established the user's groups, and is only
122 * called if sshd is running as root.
123 */
124void
125platform_setusercontext_post_groups(struct passwd *pw)
126{
Darren Tuckercc124182010-11-05 13:32:52 +1100127#if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
128 /*
129 * PAM credentials may take the form of supplementary groups.
130 * These will have been wiped by the above initgroups() call.
131 * Reestablish them here.
132 */
133 if (options.use_pam) {
134 do_pam_setcred(use_privsep);
135 }
136#endif /* USE_PAM */
137
Darren Tucker0b2ee642010-11-05 13:29:25 +1100138#if !defined(HAVE_LOGIN_CAP) && (defined(WITH_IRIX_PROJECT) || \
139 defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY))
140 irix_setusercontext(pw);
141#endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
142
Darren Tucker676b9122010-11-05 13:11:04 +1100143#ifdef _AIX
144 aix_usrinfo(pw);
145#endif /* _AIX */
146
Darren Tucker7a8afe32010-11-05 13:07:24 +1100147#if !defined(HAVE_LOGIN_CAP) && defined(USE_LIBIAF)
148 if (set_id(pw->pw_name) != 0) {
149 exit(1);
150 }
151# endif /* USE_LIBIAF */
152
Darren Tucker4db38072010-11-05 12:41:13 +1100153#ifdef HAVE_SETPCRED
154 /*
155 * If we have a chroot directory, we set all creds except real
156 * uid which we will need for chroot. If we don't have a
157 * chroot directory, we don't override anything.
158 */
159 {
160 char **creds = NULL, *chroot_creds[] =
161 { "REAL_USER=root", NULL };
162
163 if (options.chroot_directory != NULL &&
164 strcasecmp(options.chroot_directory, "none") != 0)
165 creds = chroot_creds;
166
167 if (setpcred(pw->pw_name, creds) == -1)
168 fatal("Failed to set process credentials");
169 }
170#endif /* HAVE_SETPCRED */
Darren Tucker920612e2010-11-05 12:36:15 +1100171#ifdef WITH_SELINUX
172 ssh_selinux_setup_exec_context(pw->pw_name);
173#endif
174}
175
Darren Tucker1bf35032009-12-21 10:49:21 +1100176char *
177platform_krb5_get_principal_name(const char *pw_name)
178{
179#ifdef USE_AIX_KRB_NAME
180 return aix_krb5_get_principal_name(pw_name);
181#else
182 return NULL;
183#endif
184}