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
39 #include "config.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <syslog.h>
45 #include <unistd.h>
46
47 #include <security/pam_modules.h>
48 #include <security/pam_modutil.h>
49 #include <security/pam_ext.h>
50 #include "pam_inline.h"
51
52 int
53 pam_sm_authenticate(pam_handle_t *pamh, int flags UNUSED,
54 int argc, const char **argv)
55 {
56 int i;
57 int rc;
58 int debug = 0;
59 const char *file_name = NULL;
60 const char *user_name = NULL;
61
62 /* Process arguments. */
63 for (i = 0; i < argc; ++i) {
64 if (strcmp("debug", argv[i]) == 0) {
65 debug = 1;
66 }
67 }
68 for (i = 0; i < argc; ++i) {
69 const char *str;
70
71 if (strcmp("debug", argv[i]) == 0) {
72 /* Already processed. */
73 continue;
74 }
75 if ((str = pam_str_skip_prefix(argv[i], "file=")) != NULL) {
76 file_name = str;
77 if (debug) {
78 pam_syslog(pamh, LOG_DEBUG,
79 "set filename to %s", file_name);
80 }
81 } else {
82 pam_syslog(pamh, LOG_ERR, "unrecognized option: %s",
83 argv[i]);
84 }
85 }
86
87 /* Obtain the user name. */
88 if ((rc = pam_get_user(pamh, &user_name, NULL)) != PAM_SUCCESS) {
89 pam_syslog(pamh, LOG_NOTICE, "cannot determine user name: %s",
90 pam_strerror(pamh, rc));
91 return rc == PAM_CONV_AGAIN ? PAM_INCOMPLETE : rc;
92 }
93
94 return pam_modutil_check_user_in_passwd(pamh, user_name, file_name);
95 }
96
97 int
98 pam_sm_setcred(pam_handle_t *pamh UNUSED, int flags UNUSED,
99 int argc UNUSED, const char **argv UNUSED)
100 {
101 return PAM_SUCCESS;
102 }
103
104 int
105 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
106 {
107 return pam_sm_authenticate(pamh, flags, argc, argv);
108 }
109
110 int
111 pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
112 {
113 return pam_sm_authenticate(pamh, flags, argc, argv);
114 }
115
116 int
117 pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
118 {
119 return pam_sm_authenticate(pamh, flags, argc, argv);
120 }
121
122 int
123 pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
124 {
125 return pam_sm_authenticate(pamh, flags, argc, argv);
126 }