pam_shells.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * pam_shells module
  3. *
  4. * by Erik Troan <ewt@redhat.com>, Red Hat Software.
  5. * August 5, 1996.
  6. * This code shamelessly ripped from the pam_securetty module.
  7. */
  8. #include "config.h"
  9. #include <pwd.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <sys/stat.h>
  15. #include <syslog.h>
  16. #include <unistd.h>
  17. #include <security/pam_modules.h>
  18. #include <security/pam_modutil.h>
  19. #include <security/pam_ext.h>
  20. #define SHELL_FILE "/etc/shells"
  21. #define DEFAULT_SHELL "/bin/sh"
  22. static int perform_check(pam_handle_t *pamh)
  23. {
  24. int retval = PAM_AUTH_ERR;
  25. const char *userName;
  26. const char *userShell;
  27. char shellFileLine[256];
  28. struct stat sb;
  29. struct passwd * pw;
  30. FILE * shellFile;
  31. retval = pam_get_user(pamh, &userName, NULL);
  32. if (retval != PAM_SUCCESS) {
  33. return PAM_SERVICE_ERR;
  34. }
  35. pw = pam_modutil_getpwnam(pamh, userName);
  36. if (pw == NULL || pw->pw_shell == NULL) {
  37. return PAM_AUTH_ERR; /* user doesn't exist */
  38. }
  39. userShell = pw->pw_shell;
  40. if (userShell[0] == '\0')
  41. userShell = DEFAULT_SHELL;
  42. if (stat(SHELL_FILE,&sb)) {
  43. pam_syslog(pamh, LOG_ERR, "Cannot stat %s: %m", SHELL_FILE);
  44. return PAM_AUTH_ERR; /* must have /etc/shells */
  45. }
  46. if ((sb.st_mode & S_IWOTH) || !S_ISREG(sb.st_mode)) {
  47. pam_syslog(pamh, LOG_ERR,
  48. "%s is either world writable or not a normal file",
  49. SHELL_FILE);
  50. return PAM_AUTH_ERR;
  51. }
  52. shellFile = fopen(SHELL_FILE,"r");
  53. if (shellFile == NULL) { /* Check that we opened it successfully */
  54. pam_syslog(pamh, LOG_ERR, "Error opening %s: %m", SHELL_FILE);
  55. return PAM_SERVICE_ERR;
  56. }
  57. retval = 1;
  58. while(retval && (fgets(shellFileLine, 255, shellFile) != NULL)) {
  59. if (shellFileLine[strlen(shellFileLine) - 1] == '\n')
  60. shellFileLine[strlen(shellFileLine) - 1] = '\0';
  61. retval = strcmp(shellFileLine, userShell);
  62. }
  63. fclose(shellFile);
  64. if (retval) {
  65. return PAM_AUTH_ERR;
  66. } else {
  67. return PAM_SUCCESS;
  68. }
  69. }
  70. /* --- authentication management functions (only) --- */
  71. int pam_sm_authenticate(pam_handle_t *pamh, int flags UNUSED,
  72. int argc UNUSED, const char **argv UNUSED)
  73. {
  74. return perform_check(pamh);
  75. }
  76. int pam_sm_setcred(pam_handle_t *pamh UNUSED, int flags UNUSED,
  77. int argc UNUSED, const char **argv UNUSED)
  78. {
  79. return PAM_SUCCESS;
  80. }
  81. /* --- account management functions (only) --- */
  82. int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags UNUSED,
  83. int argc UNUSED, const char **argv UNUSED)
  84. {
  85. return perform_check(pamh);
  86. }
  87. /* end of module definition */