- (dtucker) [platform.c session.c] Move the getluid call out of session.c and
[openssh-git.git] / platform.c
blob60b87a6ae84a2d8394359ef41c11ef0b4b28fc30
1 /* $Id: platform.c,v 1.17 2010/11/23 23:09:13 dtucker Exp $ */
3 /*
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.
19 #include "includes.h"
21 #include <sys/types.h>
23 #include <stdarg.h>
24 #include <unistd.h>
26 #include "log.h"
27 #include "buffer.h"
28 #include "servconf.h"
29 #include "platform.h"
31 #include "openbsd-compat/openbsd-compat.h"
33 extern int use_privsep;
34 extern ServerOptions options;
36 void
37 platform_pre_listen(void)
39 #ifdef LINUX_OOM_ADJUST
40 /* Adjust out-of-memory killer so listening process is not killed */
41 oom_adjust_setup();
42 #endif
45 void
46 platform_pre_fork(void)
48 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
49 solaris_contract_pre_fork();
50 #endif
53 void
54 platform_post_fork_parent(pid_t child_pid)
56 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
57 solaris_contract_post_fork_parent(child_pid);
58 #endif
61 void
62 platform_post_fork_child(void)
64 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
65 solaris_contract_post_fork_child();
66 #endif
67 #ifdef LINUX_OOM_ADJUST
68 oom_adjust_restore();
69 #endif
72 /* return 1 if we are running with privilege to swap UIDs, 0 otherwise */
73 int
74 platform_privileged_uidswap(void)
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
85 * This gets called before switching UIDs, and is called even when sshd is
86 * not running as root.
88 void
89 platform_setusercontext(struct passwd *pw)
91 #ifdef WITH_SELINUX
92 /* Cache selinux status for later use */
93 (void)ssh_selinux_enabled();
94 #endif
96 #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
102 #if defined(HAVE_LOGIN_CAP) && defined (__bsdi__)
103 if (getuid() == 0 || geteuid() == 0)
104 setpgid(0, 0);
105 # endif
107 #if defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
109 * If we have both LOGIN_CAP and PAM, we want to establish creds
110 * before calling setusercontext (in session.c:do_setusercontext).
112 if (getuid() == 0 || geteuid() == 0) {
113 if (options.use_pam) {
114 do_pam_setcred(use_privsep);
117 # endif /* USE_PAM */
119 #if !defined(HAVE_LOGIN_CAP) && defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
120 if (getuid() == 0 || geteuid() == 0) {
121 /* Sets login uid for accounting */
122 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
123 error("setluid: %s", strerror(errno));
125 #endif
129 * This gets called after we've established the user's groups, and is only
130 * called if sshd is running as root.
132 void
133 platform_setusercontext_post_groups(struct passwd *pw)
135 #if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
137 * PAM credentials may take the form of supplementary groups.
138 * These will have been wiped by the above initgroups() call.
139 * Reestablish them here.
141 if (options.use_pam) {
142 do_pam_setcred(use_privsep);
144 #endif /* USE_PAM */
146 #if !defined(HAVE_LOGIN_CAP) && (defined(WITH_IRIX_PROJECT) || \
147 defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY))
148 irix_setusercontext(pw);
149 #endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
151 #ifdef _AIX
152 aix_usrinfo(pw);
153 #endif /* _AIX */
155 #if !defined(HAVE_LOGIN_CAP) && defined(USE_LIBIAF)
156 if (set_id(pw->pw_name) != 0) {
157 exit(1);
159 # endif /* USE_LIBIAF */
161 #ifdef HAVE_SETPCRED
163 * If we have a chroot directory, we set all creds except real
164 * uid which we will need for chroot. If we don't have a
165 * chroot directory, we don't override anything.
168 char **creds = NULL, *chroot_creds[] =
169 { "REAL_USER=root", NULL };
171 if (options.chroot_directory != NULL &&
172 strcasecmp(options.chroot_directory, "none") != 0)
173 creds = chroot_creds;
175 if (setpcred(pw->pw_name, creds) == -1)
176 fatal("Failed to set process credentials");
178 #endif /* HAVE_SETPCRED */
179 #ifdef WITH_SELINUX
180 ssh_selinux_setup_exec_context(pw->pw_name);
181 #endif
184 char *
185 platform_krb5_get_principal_name(const char *pw_name)
187 #ifdef USE_AIX_KRB_NAME
188 return aix_krb5_get_principal_name(pw_name);
189 #else
190 return NULL;
191 #endif