blob: 6d896c2293f6cf416b3ed44ca80cf9d9dc9d4319 [file] [log] [blame]
Darren Tuckerf2bf36c2013-09-22 19:02:40 +10001/* $Id: platform.c,v 1.20 2013/09/22 09:02:40 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
Darren Tuckerd1ece6e2010-11-07 18:05:54 +110019#include "includes.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 Miller81ad4b12011-01-11 17:02:23 +110029#include "key.h"
30#include "hostfile.h"
31#include "auth.h"
32#include "auth-pam.h"
Damien Miller1b06dc32006-08-31 03:24:41 +100033#include "platform.h"
34
35#include "openbsd-compat/openbsd-compat.h"
36
Darren Tucker728d8372010-11-05 13:00:05 +110037extern int use_privsep;
Darren Tucker9283d8c2010-11-05 18:56:08 +110038extern ServerOptions options;
Darren Tucker728d8372010-11-05 13:00:05 +110039
Damien Miller1b06dc32006-08-31 03:24:41 +100040void
Darren Tuckerc8802aa2009-12-08 13:39:48 +110041platform_pre_listen(void)
42{
43#ifdef LINUX_OOM_ADJUST
44 /* Adjust out-of-memory killer so listening process is not killed */
45 oom_adjust_setup();
46#endif
47}
48
49void
Damien Miller1b06dc32006-08-31 03:24:41 +100050platform_pre_fork(void)
51{
52#ifdef USE_SOLARIS_PROCESS_CONTRACTS
53 solaris_contract_pre_fork();
54#endif
55}
56
57void
Darren Tuckerf2bf36c2013-09-22 19:02:40 +100058platform_pre_restart(void)
59{
60#ifdef LINUX_OOM_ADJUST
61 oom_adjust_restore();
62#endif
63}
64
65void
Damien Miller1b06dc32006-08-31 03:24:41 +100066platform_post_fork_parent(pid_t child_pid)
67{
68#ifdef USE_SOLARIS_PROCESS_CONTRACTS
69 solaris_contract_post_fork_parent(child_pid);
70#endif
71}
72
73void
74platform_post_fork_child(void)
75{
76#ifdef USE_SOLARIS_PROCESS_CONTRACTS
77 solaris_contract_post_fork_child();
78#endif
Darren Tuckerc8802aa2009-12-08 13:39:48 +110079#ifdef LINUX_OOM_ADJUST
80 oom_adjust_restore();
81#endif
Damien Miller1b06dc32006-08-31 03:24:41 +100082}
Darren Tucker1bf35032009-12-21 10:49:21 +110083
Darren Tuckerb12fe272010-11-05 14:47:01 +110084/* return 1 if we are running with privilege to swap UIDs, 0 otherwise */
85int
86platform_privileged_uidswap(void)
87{
88#ifdef HAVE_CYGWIN
89 /* uid 0 is not special on Cygwin so always try */
90 return 1;
91#else
92 return (getuid() == 0 || geteuid() == 0);
93#endif
94}
95
Darren Tucker920612e2010-11-05 12:36:15 +110096/*
97 * This gets called before switching UIDs, and is called even when sshd is
98 * not running as root.
99 */
Darren Tucker97528352010-11-05 12:03:05 +1100100void
101platform_setusercontext(struct passwd *pw)
102{
Darren Tucker920612e2010-11-05 12:36:15 +1100103#ifdef WITH_SELINUX
104 /* Cache selinux status for later use */
105 (void)ssh_selinux_enabled();
106#endif
107
Darren Tucker97528352010-11-05 12:03:05 +1100108#ifdef USE_SOLARIS_PROJECTS
109 /* if solaris projects were detected, set the default now */
110 if (getuid() == 0 || geteuid() == 0)
111 solaris_set_default_project(pw);
112#endif
Darren Tucker44a97be2010-11-05 12:45:18 +1100113
114#if defined(HAVE_LOGIN_CAP) && defined (__bsdi__)
Darren Tuckerfd4d8aa2010-11-05 12:50:41 +1100115 if (getuid() == 0 || geteuid() == 0)
116 setpgid(0, 0);
Darren Tucker44a97be2010-11-05 12:45:18 +1100117# endif
Darren Tucker728d8372010-11-05 13:00:05 +1100118
119#if defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
120 /*
121 * If we have both LOGIN_CAP and PAM, we want to establish creds
122 * before calling setusercontext (in session.c:do_setusercontext).
123 */
124 if (getuid() == 0 || geteuid() == 0) {
125 if (options.use_pam) {
126 do_pam_setcred(use_privsep);
127 }
128 }
129# endif /* USE_PAM */
Darren Tuckerd9957122010-11-24 10:09:13 +1100130
131#if !defined(HAVE_LOGIN_CAP) && defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
132 if (getuid() == 0 || geteuid() == 0) {
133 /* Sets login uid for accounting */
134 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
135 error("setluid: %s", strerror(errno));
136 }
137#endif
Darren Tucker97528352010-11-05 12:03:05 +1100138}
139
Darren Tucker920612e2010-11-05 12:36:15 +1100140/*
141 * This gets called after we've established the user's groups, and is only
142 * called if sshd is running as root.
143 */
144void
145platform_setusercontext_post_groups(struct passwd *pw)
146{
Darren Tuckercc124182010-11-05 13:32:52 +1100147#if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
148 /*
149 * PAM credentials may take the form of supplementary groups.
150 * These will have been wiped by the above initgroups() call.
151 * Reestablish them here.
152 */
153 if (options.use_pam) {
154 do_pam_setcred(use_privsep);
155 }
156#endif /* USE_PAM */
157
Darren Tucker0b2ee642010-11-05 13:29:25 +1100158#if !defined(HAVE_LOGIN_CAP) && (defined(WITH_IRIX_PROJECT) || \
159 defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY))
160 irix_setusercontext(pw);
161#endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
162
Darren Tucker676b9122010-11-05 13:11:04 +1100163#ifdef _AIX
164 aix_usrinfo(pw);
165#endif /* _AIX */
166
Darren Tucker7a8afe32010-11-05 13:07:24 +1100167#if !defined(HAVE_LOGIN_CAP) && defined(USE_LIBIAF)
168 if (set_id(pw->pw_name) != 0) {
169 exit(1);
170 }
171# endif /* USE_LIBIAF */
172
Darren Tucker4db38072010-11-05 12:41:13 +1100173#ifdef HAVE_SETPCRED
174 /*
175 * If we have a chroot directory, we set all creds except real
176 * uid which we will need for chroot. If we don't have a
177 * chroot directory, we don't override anything.
178 */
179 {
180 char **creds = NULL, *chroot_creds[] =
181 { "REAL_USER=root", NULL };
182
183 if (options.chroot_directory != NULL &&
184 strcasecmp(options.chroot_directory, "none") != 0)
185 creds = chroot_creds;
186
187 if (setpcred(pw->pw_name, creds) == -1)
188 fatal("Failed to set process credentials");
189 }
190#endif /* HAVE_SETPCRED */
Darren Tucker920612e2010-11-05 12:36:15 +1100191#ifdef WITH_SELINUX
192 ssh_selinux_setup_exec_context(pw->pw_name);
193#endif
194}
195
Darren Tucker1bf35032009-12-21 10:49:21 +1100196char *
197platform_krb5_get_principal_name(const char *pw_name)
198{
199#ifdef USE_AIX_KRB_NAME
200 return aix_krb5_get_principal_name(pw_name);
201#else
202 return NULL;
203#endif
204}
Darren Tuckeraa97d132013-03-12 11:31:05 +1100205
206/*
207 * return 1 if the specified uid is a uid that may own a system directory
208 * otherwise 0.
209 */
210int
211platform_sys_dir_uid(uid_t uid)
212{
213 if (uid == 0)
214 return 1;
215#ifdef PLATFORM_SYS_DIR_UID
216 if (uid == PLATFORM_SYS_DIR_UID)
217 return 1;
218#endif
219 return 0;
220}