platform.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2006 Darren Tucker. All rights reserved.
  3. *
  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.
  7. *
  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.
  15. */
  16. #include "includes.h"
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include "log.h"
  21. #include "misc.h"
  22. #include "servconf.h"
  23. #include "sshkey.h"
  24. #include "hostfile.h"
  25. #include "auth.h"
  26. #include "auth-pam.h"
  27. #include "platform.h"
  28. #include "openbsd-compat/openbsd-compat.h"
  29. extern int use_privsep;
  30. extern ServerOptions options;
  31. void
  32. platform_pre_listen(void)
  33. {
  34. #ifdef LINUX_OOM_ADJUST
  35. /* Adjust out-of-memory killer so listening process is not killed */
  36. oom_adjust_setup();
  37. #endif
  38. }
  39. void
  40. platform_pre_fork(void)
  41. {
  42. #ifdef USE_SOLARIS_PROCESS_CONTRACTS
  43. solaris_contract_pre_fork();
  44. #endif
  45. }
  46. void
  47. platform_pre_restart(void)
  48. {
  49. #ifdef LINUX_OOM_ADJUST
  50. oom_adjust_restore();
  51. #endif
  52. }
  53. void
  54. platform_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. void
  61. platform_post_fork_child(void)
  62. {
  63. #ifdef USE_SOLARIS_PROCESS_CONTRACTS
  64. solaris_contract_post_fork_child();
  65. #endif
  66. #ifdef LINUX_OOM_ADJUST
  67. oom_adjust_restore();
  68. #endif
  69. }
  70. /* return 1 if we are running with privilege to swap UIDs, 0 otherwise */
  71. int
  72. platform_privileged_uidswap(void)
  73. {
  74. #ifdef HAVE_CYGWIN
  75. /* uid 0 is not special on Cygwin so always try */
  76. return 1;
  77. #else
  78. return (getuid() == 0 || geteuid() == 0);
  79. #endif
  80. }
  81. /*
  82. * This gets called before switching UIDs, and is called even when sshd is
  83. * not running as root.
  84. */
  85. void
  86. platform_setusercontext(struct passwd *pw)
  87. {
  88. #ifdef WITH_SELINUX
  89. /* Cache selinux status for later use */
  90. (void)sshd_selinux_enabled();
  91. #endif
  92. #ifdef USE_SOLARIS_PROJECTS
  93. /*
  94. * If solaris projects were detected, set the default now, unless
  95. * we are using PAM in which case it is the responsibility of the
  96. * PAM stack.
  97. */
  98. if (!options.use_pam && (getuid() == 0 || geteuid() == 0))
  99. solaris_set_default_project(pw);
  100. #endif
  101. #if defined(HAVE_LOGIN_CAP) && defined (__bsdi__)
  102. if (getuid() == 0 || geteuid() == 0)
  103. setpgid(0, 0);
  104. # endif
  105. #if defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
  106. /*
  107. * If we have both LOGIN_CAP and PAM, we want to establish creds
  108. * before calling setusercontext (in session.c:do_setusercontext).
  109. */
  110. if (getuid() == 0 || geteuid() == 0) {
  111. if (options.use_pam) {
  112. do_pam_setcred(use_privsep);
  113. }
  114. }
  115. # endif /* USE_PAM */
  116. #if !defined(HAVE_LOGIN_CAP) && defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
  117. if (getuid() == 0 || geteuid() == 0) {
  118. /* Sets login uid for accounting */
  119. if (getluid() == -1 && setluid(pw->pw_uid) == -1)
  120. error("setluid: %s", strerror(errno));
  121. }
  122. #endif
  123. }
  124. /*
  125. * This gets called after we've established the user's groups, and is only
  126. * called if sshd is running as root.
  127. */
  128. void
  129. platform_setusercontext_post_groups(struct passwd *pw)
  130. {
  131. #if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
  132. /*
  133. * PAM credentials may take the form of supplementary groups.
  134. * These will have been wiped by the above initgroups() call.
  135. * Reestablish them here.
  136. */
  137. if (options.use_pam) {
  138. do_pam_setcred(use_privsep);
  139. }
  140. #endif /* USE_PAM */
  141. #if !defined(HAVE_LOGIN_CAP) && (defined(WITH_IRIX_PROJECT) || \
  142. defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY))
  143. irix_setusercontext(pw);
  144. #endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
  145. #ifdef _AIX
  146. aix_usrinfo(pw);
  147. #endif /* _AIX */
  148. #ifdef HAVE_SETPCRED
  149. /*
  150. * If we have a chroot directory, we set all creds except real
  151. * uid which we will need for chroot. If we don't have a
  152. * chroot directory, we don't override anything.
  153. */
  154. {
  155. char **creds = NULL, *chroot_creds[] =
  156. { "REAL_USER=root", NULL };
  157. if (options.chroot_directory != NULL &&
  158. strcasecmp(options.chroot_directory, "none") != 0)
  159. creds = chroot_creds;
  160. if (setpcred(pw->pw_name, creds) == -1)
  161. fatal("Failed to set process credentials");
  162. }
  163. #endif /* HAVE_SETPCRED */
  164. #ifdef WITH_SELINUX
  165. sshd_selinux_setup_exec_context(pw->pw_name);
  166. #endif
  167. }
  168. char *
  169. platform_krb5_get_principal_name(const char *pw_name)
  170. {
  171. #ifdef USE_AIX_KRB_NAME
  172. return aix_krb5_get_principal_name(pw_name);
  173. #else
  174. return NULL;
  175. #endif
  176. }