pam_localuser.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * pam_localuser module
  3. *
  4. * Copyright 2001, 2004 Red Hat, Inc.
  5. * Copyright (c) 2020 Dmitry V. Levin <ldv@altlinux.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, and the entire permission notice in its entirety,
  12. * including the disclaimer of warranties.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote
  17. * products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * ALTERNATIVELY, this product may be distributed under the terms of
  21. * the GNU Public License, in which case the provisions of the GPL are
  22. * required INSTEAD OF the above restrictions. (This clause is
  23. * necessary due to a potential bad interaction between the GPL and
  24. * the restrictions contained in a BSD-style copyright.)
  25. *
  26. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  28. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  36. * OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include "config.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <syslog.h>
  43. #include <unistd.h>
  44. #include <security/pam_modules.h>
  45. #include <security/pam_modutil.h>
  46. #include <security/pam_ext.h>
  47. #include "pam_inline.h"
  48. int
  49. pam_sm_authenticate(pam_handle_t *pamh, int flags UNUSED,
  50. int argc, const char **argv)
  51. {
  52. int i;
  53. int rc;
  54. int debug = 0;
  55. const char *file_name = NULL;
  56. const char *user_name = NULL;
  57. /* Process arguments. */
  58. for (i = 0; i < argc; ++i) {
  59. if (strcmp("debug", argv[i]) == 0) {
  60. debug = 1;
  61. }
  62. }
  63. for (i = 0; i < argc; ++i) {
  64. const char *str;
  65. if (strcmp("debug", argv[i]) == 0) {
  66. /* Already processed. */
  67. continue;
  68. }
  69. if ((str = pam_str_skip_prefix(argv[i], "file=")) != NULL) {
  70. file_name = str;
  71. if (debug) {
  72. pam_syslog(pamh, LOG_DEBUG,
  73. "set filename to %s", file_name);
  74. }
  75. } else {
  76. pam_syslog(pamh, LOG_ERR, "unrecognized option: %s",
  77. argv[i]);
  78. }
  79. }
  80. /* Obtain the user name. */
  81. if ((rc = pam_get_user(pamh, &user_name, NULL)) != PAM_SUCCESS) {
  82. pam_syslog(pamh, LOG_NOTICE, "cannot determine user name: %s",
  83. pam_strerror(pamh, rc));
  84. return rc == PAM_CONV_AGAIN ? PAM_INCOMPLETE : rc;
  85. }
  86. return pam_modutil_check_user_in_passwd(pamh, user_name, file_name);
  87. }
  88. int
  89. pam_sm_setcred(pam_handle_t *pamh UNUSED, int flags UNUSED,
  90. int argc UNUSED, const char **argv UNUSED)
  91. {
  92. return PAM_SUCCESS;
  93. }
  94. int
  95. pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
  96. {
  97. return pam_sm_authenticate(pamh, flags, argc, argv);
  98. }
  99. int
  100. pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
  101. {
  102. return pam_sm_authenticate(pamh, flags, argc, argv);
  103. }
  104. int
  105. pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
  106. {
  107. return pam_sm_authenticate(pamh, flags, argc, argv);
  108. }
  109. int
  110. pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
  111. {
  112. return pam_sm_authenticate(pamh, flags, argc, argv);
  113. }