pam_rhosts.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * pam_rhosts module
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, and the entire permission notice in its entirety,
  9. * including the disclaimer of warranties.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote
  14. * products derived from this software without specific prior
  15. * written permission.
  16. *
  17. * ALTERNATIVELY, this product may be distributed under the terms of
  18. * the GNU Public License, in which case the provisions of the GPL are
  19. * required INSTEAD OF the above restrictions. (This clause is
  20. * necessary due to a potential bad interaction between the GPL and
  21. * the restrictions contained in a BSD-style copyright.)
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  27. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  31. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  33. * OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include "config.h"
  36. #include <pwd.h>
  37. #include <netdb.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <syslog.h>
  41. #include <security/pam_modules.h>
  42. #include <security/pam_modutil.h>
  43. #include <security/pam_ext.h>
  44. #include "pam_inline.h"
  45. int pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc,
  46. const char **argv)
  47. {
  48. const char *luser = NULL;
  49. const char *ruser = NULL, *rhost = NULL;
  50. const char *opt_superuser = NULL;
  51. const void *c_void;
  52. int opt_debug = 0;
  53. int opt_silent;
  54. int as_root;
  55. int retval;
  56. opt_silent = flags & PAM_SILENT;
  57. while (argc-- > 0) {
  58. const char *str;
  59. if (strcmp(*argv, "debug") == 0)
  60. opt_debug = 1;
  61. else if (strcmp (*argv, "silent") == 0 || strcmp(*argv, "suppress") == 0)
  62. opt_silent = 1;
  63. else if ((str = pam_str_skip_prefix(*argv, "superuser=")) != NULL)
  64. opt_superuser = str;
  65. else
  66. pam_syslog(pamh, LOG_WARNING, "unrecognized option '%s'", *argv);
  67. ++argv;
  68. }
  69. retval = pam_get_item (pamh, PAM_RHOST, &c_void);
  70. if (retval != PAM_SUCCESS) {
  71. pam_syslog(pamh, LOG_ERR, "could not get the remote host name");
  72. return retval;
  73. }
  74. rhost = c_void;
  75. retval = pam_get_item(pamh, PAM_RUSER, &c_void);
  76. ruser = c_void;
  77. if (retval != PAM_SUCCESS) {
  78. pam_syslog(pamh, LOG_ERR, "could not get the remote username");
  79. return retval;
  80. }
  81. retval = pam_get_user(pamh, &luser, NULL);
  82. if (retval != PAM_SUCCESS) {
  83. pam_syslog(pamh, LOG_NOTICE, "cannot determine local user name: %s",
  84. pam_strerror(pamh, retval));
  85. return retval;
  86. }
  87. if (rhost == NULL || ruser == NULL)
  88. return PAM_AUTH_ERR;
  89. if (opt_superuser && strcmp(opt_superuser, luser) == 0)
  90. as_root = 1;
  91. else {
  92. struct passwd *lpwd;
  93. lpwd = pam_modutil_getpwnam(pamh, luser);
  94. if (lpwd == NULL) {
  95. if (opt_debug)
  96. /* don't print by default, could be the user's password */
  97. pam_syslog(pamh, LOG_DEBUG,
  98. "user '%s' unknown to this system", luser);
  99. return PAM_USER_UNKNOWN;
  100. }
  101. as_root = (lpwd->pw_uid == 0);
  102. }
  103. #ifdef HAVE_RUSEROK_AF
  104. retval = ruserok_af (rhost, as_root, ruser, luser, PF_UNSPEC);
  105. #else
  106. retval = ruserok (rhost, as_root, ruser, luser);
  107. #endif
  108. if (retval != 0) {
  109. if (!opt_silent || opt_debug)
  110. pam_syslog(pamh, LOG_WARNING, "denied access to %s@%s as %s",
  111. ruser, rhost, luser);
  112. return PAM_AUTH_ERR;
  113. } else {
  114. if (!opt_silent || opt_debug)
  115. pam_syslog(pamh, LOG_NOTICE, "allowed access to %s@%s as %s",
  116. ruser, rhost, luser);
  117. return PAM_SUCCESS;
  118. }
  119. }
  120. int
  121. pam_sm_setcred (pam_handle_t *pamh UNUSED, int flags UNUSED,
  122. int argc UNUSED, const char **argv UNUSED)
  123. {
  124. return PAM_SUCCESS;
  125. }