]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/pam_unix_auth.c
pam_unix: Use pam_get_authtok() instead of direct pam_prompt() calls.
[linux-pam] / modules / pam_unix / pam_unix_auth.c
1 /*
2  * Copyright Alexander O. Yuriev, 1996.  All rights reserved.
3  * NIS+ support by Thorsten Kukuk <kukuk@weber.uni-paderborn.de>
4  * Copyright Jan Rêkorajski, 1999.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, and the entire permission notice in its entirety,
11  *    including the disclaimer of warranties.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote
16  *    products derived from this software without specific prior
17  *    written permission.
18  *
19  * ALTERNATIVELY, this product may be distributed under the terms of
20  * the GNU Public License, in which case the provisions of the GPL are
21  * required INSTEAD OF the above restrictions.  (This clause is
22  * necessary due to a potential bad interaction between the GPL and
23  * the restrictions contained in a BSD-style copyright.)
24  *
25  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35  * OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include "config.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <ctype.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <syslog.h>
50
51 /* indicate the following groups are defined */
52
53 #define PAM_SM_AUTH
54
55 #define _PAM_EXTERN_FUNCTIONS
56 #include <security/_pam_macros.h>
57 #include <security/pam_modules.h>
58 #include <security/pam_ext.h>
59
60 #include "support.h"
61
62 /*
63  * PAM framework looks for these entry-points to pass control to the
64  * authentication module.
65  */
66
67 /* Fun starts here :)
68
69  * pam_sm_authenticate() performs UNIX/shadow authentication
70  *
71  *      First, if shadow support is available, attempt to perform
72  *      authentication using shadow passwords. If shadow is not
73  *      available, or user does not have a shadow password, fallback
74  *      onto a normal UNIX authentication
75  */
76
77 #define _UNIX_AUTHTOK  "-UN*X-PASS"
78
79 #define AUTH_RETURN                                             \
80 do {                                                            \
81         if (ret_data) {                                         \
82                 D(("recording return code for next time [%d]",  \
83                                         retval));               \
84                 *ret_data = retval;                             \
85                 pam_set_data(pamh, "unix_setcred_return",       \
86                              (void *) ret_data, setcred_free);  \
87         }                                                       \
88         D(("done. [%s]", pam_strerror(pamh, retval)));          \
89         return retval;                                          \
90 } while (0)
91
92
93 static void
94 setcred_free (pam_handle_t *pamh UNUSED, void *ptr, int err UNUSED)
95 {
96         if (ptr)
97                 free (ptr);
98 }
99
100 int
101 pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
102 {
103         unsigned int ctrl;
104         int retval, *ret_data = NULL;
105         const char *name;
106         const char *p;
107
108         D(("called."));
109
110         ctrl = _set_ctrl(pamh, flags, NULL, NULL, NULL, argc, argv);
111
112         /* Get a few bytes so we can pass our return value to
113            pam_sm_setcred() and pam_sm_acct_mgmt(). */
114         ret_data = malloc(sizeof(int));
115
116         /* get the user'name' */
117
118         retval = pam_get_user(pamh, &name, NULL);
119         if (retval == PAM_SUCCESS) {
120                 /*
121                  * Various libraries at various times have had bugs related to
122                  * '+' or '-' as the first character of a user name. Don't
123                  * allow this characters here.
124                  */
125                 if (name == NULL || name[0] == '-' || name[0] == '+') {
126                         pam_syslog(pamh, LOG_ERR, "bad username [%s]", name);
127                         retval = PAM_USER_UNKNOWN;
128                         AUTH_RETURN;
129                 }
130                 if (on(UNIX_DEBUG, ctrl))
131                         D(("username [%s] obtained", name));
132         } else {
133                 D(("trouble reading username"));
134                 if (retval == PAM_CONV_AGAIN) {
135                         D(("pam_get_user/conv() function is not ready yet"));
136                         /* it is safe to resume this function so we translate this
137                          * retval to the value that indicates we're happy to resume.
138                          */
139                         retval = PAM_INCOMPLETE;
140                 }
141                 AUTH_RETURN;
142         }
143
144         /* if this user does not have a password... */
145
146         if (_unix_blankpasswd(pamh, ctrl, name)) {
147                 D(("user '%s' has blank passwd", name));
148                 name = NULL;
149                 retval = PAM_SUCCESS;
150                 AUTH_RETURN;
151         }
152         /* get this user's authentication token */
153
154         retval = pam_get_authtok(pamh, PAM_AUTHTOK, &p , NULL);
155         if (retval != PAM_SUCCESS) {
156                 if (retval != PAM_CONV_AGAIN) {
157                         pam_syslog(pamh, LOG_CRIT,
158                             "auth could not identify password for [%s]", name);
159                 } else {
160                         D(("conversation function is not ready yet"));
161                         /*
162                          * it is safe to resume this function so we translate this
163                          * retval to the value that indicates we're happy to resume.
164                          */
165                         retval = PAM_INCOMPLETE;
166                 }
167                 name = NULL;
168                 AUTH_RETURN;
169         }
170         D(("user=%s, password=[%s]", name, p));
171
172         /* verify the password of this user */
173         retval = _unix_verify_password(pamh, name, p, ctrl);
174         name = p = NULL;
175
176         AUTH_RETURN;
177 }
178
179
180 /*
181  * The only thing _pam_set_credentials_unix() does is initialization of
182  * UNIX group IDs.
183  *
184  * Well, everybody but me on linux-pam is convinced that it should not
185  * initialize group IDs, so I am not doing it but don't say that I haven't
186  * warned you. -- AOY
187  */
188
189 int
190 pam_sm_setcred (pam_handle_t *pamh, int flags,
191                 int argc, const char **argv)
192 {
193         int retval;
194         const void *pretval = NULL;
195         unsigned int ctrl;
196
197         D(("called."));
198
199         ctrl = _set_ctrl(pamh, flags, NULL, NULL, NULL, argc, argv);
200
201         retval = PAM_SUCCESS;
202
203         D(("recovering return code from auth call"));
204         /* We will only find something here if UNIX_LIKE_AUTH is set --
205            don't worry about an explicit check of argv. */
206         if (on(UNIX_LIKE_AUTH, ctrl)
207             && pam_get_data(pamh, "unix_setcred_return", &pretval) == PAM_SUCCESS
208             && pretval) {
209                 retval = *(const int *)pretval;
210                 pam_set_data(pamh, "unix_setcred_return", NULL, NULL);
211                 D(("recovered data indicates that old retval was %d", retval));
212         }
213
214         return retval;
215 }