]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/pam_unix_auth.c
Relevant BUGIDs:
[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 /* #define DEBUG */
39
40 #include "config.h"
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <ctype.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <syslog.h>
52
53 /* indicate the following groups are defined */
54
55 #define PAM_SM_AUTH
56
57 #define _PAM_EXTERN_FUNCTIONS
58 #include <security/_pam_macros.h>
59 #include <security/pam_modules.h>
60 #include <security/pam_ext.h>
61
62 #include "support.h"
63
64 /*
65  * PAM framework looks for these entry-points to pass control to the
66  * authentication module.
67  */
68
69 /* Fun starts here :)
70
71  * pam_sm_authenticate() performs UNIX/shadow authentication
72  *
73  *      First, if shadow support is available, attempt to perform
74  *      authentication using shadow passwords. If shadow is not
75  *      available, or user does not have a shadow password, fallback
76  *      onto a normal UNIX authentication
77  */
78
79 #define _UNIX_AUTHTOK  "-UN*X-PASS"
80
81 #define AUTH_RETURN                                             \
82 do {                                                            \
83         if (on(UNIX_LIKE_AUTH, ctrl) && ret_data) {             \
84                 D(("recording return code for next time [%d]",  \
85                                         retval));               \
86                 *ret_data = retval;                             \
87                 pam_set_data(pamh, "unix_setcred_return",       \
88                              (void *) ret_data, setcred_free);  \
89         } else if (ret_data)                                    \
90           free (ret_data);                                      \
91         D(("done. [%s]", pam_strerror(pamh, retval)));          \
92         return retval;                                          \
93 } while (0)
94
95
96 static void
97 setcred_free (pam_handle_t *pamh UNUSED, void *ptr, int err UNUSED)
98 {
99         if (ptr)
100                 free (ptr);
101 }
102
103
104 PAM_EXTERN int pam_sm_authenticate(pam_handle_t * pamh, int flags
105                                    ,int argc, const char **argv)
106 {
107         unsigned int ctrl;
108         int retval, *ret_data = NULL;
109         const char *name;
110         const void *p;
111
112         D(("called."));
113
114         ctrl = _set_ctrl(pamh, flags, NULL, argc, argv);
115
116         /* Get a few bytes so we can pass our return value to
117            pam_sm_setcred(). */
118         if (on(UNIX_LIKE_AUTH, ctrl))
119                 ret_data = malloc(sizeof(int));
120
121         /* get the user'name' */
122
123         retval = pam_get_user(pamh, &name, NULL);
124         if (retval == PAM_SUCCESS) {
125                 /*
126                  * Various libraries at various times have had bugs related to
127                  * '+' or '-' as the first character of a user name. Don't take
128                  * any chances here. Require that the username starts with an
129                  * alphanumeric character.
130                  */
131                 if (name == NULL || !isalnum(*name)) {
132                         pam_syslog(pamh, LOG_ERR, "bad username [%s]", name);
133                         retval = PAM_USER_UNKNOWN;
134                         AUTH_RETURN;
135                 }
136                 if (retval == PAM_SUCCESS && on(UNIX_DEBUG, ctrl))
137                         D(("username [%s] obtained", name));
138         } else {
139                 D(("trouble reading username"));
140                 if (retval == PAM_CONV_AGAIN) {
141                         D(("pam_get_user/conv() function is not ready yet"));
142                         /* it is safe to resume this function so we translate this
143                          * retval to the value that indicates we're happy to resume.
144                          */
145                         retval = PAM_INCOMPLETE;
146                 }
147                 AUTH_RETURN;
148         }
149
150         /* if this user does not have a password... */
151
152         if (_unix_blankpasswd(pamh, ctrl, name)) {
153                 D(("user '%s' has blank passwd", name));
154                 name = NULL;
155                 retval = PAM_SUCCESS;
156                 AUTH_RETURN;
157         }
158         /* get this user's authentication token */
159
160         retval = _unix_read_password(pamh, ctrl, NULL, _("Password: "), NULL
161                                      ,_UNIX_AUTHTOK, &p);
162         if (retval != PAM_SUCCESS) {
163                 if (retval != PAM_CONV_AGAIN) {
164                         pam_syslog(pamh, LOG_CRIT,
165                             "auth could not identify password for [%s]", name);
166                 } else {
167                         D(("conversation function is not ready yet"));
168                         /*
169                          * it is safe to resume this function so we translate this
170                          * retval to the value that indicates we're happy to resume.
171                          */
172                         retval = PAM_INCOMPLETE;
173                 }
174                 name = NULL;
175                 AUTH_RETURN;
176         }
177         D(("user=%s, password=[%s]", name, p));
178
179         /* verify the password of this user */
180         retval = _unix_verify_password(pamh, name, p, ctrl);
181         name = p = NULL;
182
183         AUTH_RETURN;
184 }
185
186
187 /*
188  * The only thing _pam_set_credentials_unix() does is initialization of
189  * UNIX group IDs.
190  *
191  * Well, everybody but me on linux-pam is convinced that it should not
192  * initialize group IDs, so I am not doing it but don't say that I haven't
193  * warned you. -- AOY
194  */
195
196 PAM_EXTERN int
197 pam_sm_setcred (pam_handle_t *pamh, int flags UNUSED,
198                 int argc UNUSED, const char **argv UNUSED)
199 {
200         int retval;
201         const void *pretval = NULL;
202
203         D(("called."));
204
205         retval = PAM_SUCCESS;
206
207         D(("recovering return code from auth call"));
208         /* We will only find something here if UNIX_LIKE_AUTH is set --
209            don't worry about an explicit check of argv. */
210         if (pam_get_data(pamh, "unix_setcred_return", &pretval) == PAM_SUCCESS
211             && pretval) {
212                 retval = *(const int *)pretval;
213                 pam_set_data(pamh, "unix_setcred_return", NULL, NULL);
214                 D(("recovered data indicates that old retval was %d", retval));
215         }
216
217         return retval;
218 }
219
220 #ifdef PAM_STATIC
221 struct pam_module _pam_unix_auth_modstruct = {
222     "pam_unix_auth",
223     pam_sm_authenticate,
224     pam_sm_setcred,
225     NULL,
226     NULL,
227     NULL,
228     NULL,
229 };
230 #endif