pam_rootok.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * pam_rootok module
  3. *
  4. * Written by Andrew Morgan <morgan@linux.kernel.org> 1996/3/11
  5. */
  6. #include "config.h"
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <syslog.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <security/pam_modules.h>
  13. #include <security/pam_ext.h>
  14. #ifdef WITH_SELINUX
  15. #include <selinux/selinux.h>
  16. #include <selinux/avc.h>
  17. #endif
  18. #ifdef HAVE_LIBAUDIT
  19. #include <libaudit.h>
  20. #endif
  21. /* argument parsing */
  22. #define PAM_DEBUG_ARG 01
  23. static int
  24. _pam_parse (const pam_handle_t *pamh, int argc, const char **argv)
  25. {
  26. int ctrl=0;
  27. /* step through arguments */
  28. for (ctrl=0; argc-- > 0; ++argv) {
  29. /* generic options */
  30. if (!strcmp(*argv,"debug"))
  31. ctrl |= PAM_DEBUG_ARG;
  32. else {
  33. pam_syslog(pamh, LOG_ERR, "unknown option: %s", *argv);
  34. }
  35. }
  36. return ctrl;
  37. }
  38. #ifdef WITH_SELINUX
  39. static int
  40. PAM_FORMAT((printf, 2, 3))
  41. log_callback (int type UNUSED, const char *fmt, ...)
  42. {
  43. int audit_fd;
  44. va_list ap;
  45. #ifdef HAVE_LIBAUDIT
  46. audit_fd = audit_open();
  47. if (audit_fd >= 0) {
  48. char *buf;
  49. int ret;
  50. va_start(ap, fmt);
  51. ret = vasprintf (&buf, fmt, ap);
  52. va_end(ap);
  53. if (ret < 0) {
  54. return 0;
  55. }
  56. audit_log_user_avc_message(audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
  57. NULL, 0);
  58. audit_close(audit_fd);
  59. free(buf);
  60. va_end(ap);
  61. return 0;
  62. }
  63. #endif
  64. va_start(ap, fmt);
  65. vsyslog (LOG_USER | LOG_INFO, fmt, ap);
  66. va_end(ap);
  67. return 0;
  68. }
  69. static int
  70. selinux_check_root (void)
  71. {
  72. int status = -1;
  73. char *user_context_raw;
  74. union selinux_callback old_callback;
  75. if (is_selinux_enabled() < 1)
  76. return 0;
  77. old_callback = selinux_get_callback(SELINUX_CB_LOG);
  78. /* setup callbacks */
  79. selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) &log_callback);
  80. if ((status = getprevcon_raw(&user_context_raw)) < 0) {
  81. selinux_set_callback(SELINUX_CB_LOG, old_callback);
  82. return status;
  83. }
  84. status = selinux_check_access(user_context_raw, user_context_raw, "passwd", "rootok", NULL);
  85. selinux_set_callback(SELINUX_CB_LOG, old_callback);
  86. freecon(user_context_raw);
  87. return status;
  88. }
  89. #endif
  90. static int
  91. check_for_root (pam_handle_t *pamh, int ctrl)
  92. {
  93. int retval = PAM_AUTH_ERR;
  94. if (getuid() == 0)
  95. #ifdef WITH_SELINUX
  96. if (selinux_check_root() == 0 || security_getenforce() == 0)
  97. #endif
  98. retval = PAM_SUCCESS;
  99. if (ctrl & PAM_DEBUG_ARG) {
  100. pam_syslog(pamh, LOG_DEBUG, "root check %s",
  101. (retval==PAM_SUCCESS) ? "succeeded" : "failed");
  102. }
  103. return retval;
  104. }
  105. /* --- management functions --- */
  106. int
  107. pam_sm_authenticate (pam_handle_t *pamh, int flags UNUSED,
  108. int argc, const char **argv)
  109. {
  110. int ctrl;
  111. ctrl = _pam_parse(pamh, argc, argv);
  112. return check_for_root (pamh, ctrl);
  113. }
  114. int
  115. pam_sm_setcred (pam_handle_t *pamh UNUSED, int flags UNUSED,
  116. int argc UNUSED, const char **argv UNUSED)
  117. {
  118. return PAM_SUCCESS;
  119. }
  120. int
  121. pam_sm_acct_mgmt (pam_handle_t *pamh, int flags UNUSED,
  122. int argc, const char **argv)
  123. {
  124. int ctrl;
  125. ctrl = _pam_parse(pamh, argc, argv);
  126. return check_for_root (pamh, ctrl);
  127. }
  128. int
  129. pam_sm_chauthtok (pam_handle_t *pamh, int flags UNUSED,
  130. int argc, const char **argv)
  131. {
  132. int ctrl;
  133. ctrl = _pam_parse(pamh, argc, argv);
  134. return check_for_root (pamh, ctrl);
  135. }
  136. /* end of module definition */