Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libpam / modules / pam_radius / pam_radius.c
1 /*-
2  * Copyright 1998 Juniper Networks, Inc.
3  * All rights reserved.
4  * Copyright (c) 2001,2002 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * Portions of this software were developed for the FreeBSD Project by
8  * ThinkSec AS and NAI Labs, the Security Research Division of Network
9  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
10  * ("CBOSS"), as part of the DARPA CHATS research program.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote
21  *    products derived from this software without specific prior written
22  *    permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/lib/libpam/modules/pam_radius/pam_radius.c,v 1.2.6.2 2003/04/08 13:20:23 sobomax Exp $
37  * $DragonFly: src/lib/libpam/modules/pam_radius/Attic/pam_radius.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
38  */
39
40 #include <sys/param.h>
41 #include <pwd.h>
42 #include <radlib.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <syslog.h>
46 #include <unistd.h>
47
48 #define PAM_SM_AUTH
49
50 #include <security/pam_appl.h>
51 #include <security/pam_modules.h>
52 #include <security/pam_mod_misc.h>
53
54 enum {
55         PAM_OPT_CONF = PAM_OPT_STD_MAX,
56         PAM_OPT_TEMPLATE_USER,
57         PAM_OPT_NAS_ID
58 };
59
60 static struct opttab other_options[] = {
61         { "conf",               PAM_OPT_CONF },
62         { "template_user",      PAM_OPT_TEMPLATE_USER },
63         { "nas_id",             PAM_OPT_NAS_ID },
64         { NULL, 0 }
65 };
66
67 #define MAX_CHALLENGE_MSGS      10
68 #define PASSWORD_PROMPT         "RADIUS Password:"
69
70 static int       build_access_request(struct rad_handle *, const char *,
71                     const char *, const char *, const void *, size_t);
72 static int       do_accept(pam_handle_t *, struct rad_handle *);
73 static int       do_challenge(pam_handle_t *, struct rad_handle *,
74                     const char *);
75
76 /*
77  * Construct an access request, but don't send it.  Returns 0 on success,
78  * -1 on failure.
79  */
80 static int
81 build_access_request(struct rad_handle *radh, const char *user,
82     const char *pass, const char *nas_id, const void *state, size_t state_len)
83 {
84         char     host[MAXHOSTNAMELEN];
85
86         if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
87                 syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
88                 return (-1);
89         }
90         if (nas_id == NULL && gethostname(host, sizeof host) != -1)
91                 nas_id = host;
92         if ((user != NULL &&
93             rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
94             (pass != NULL &&
95             rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
96             (nas_id != NULL &&
97             rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
98                 syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
99                 return (-1);
100         }
101         if (state != NULL && rad_put_attr(radh, RAD_STATE, state,
102             state_len) == -1) {
103                 syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
104                 return (-1);
105         }
106         if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
107                 syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
108                 return (-1);
109         }
110         return (0);
111 }
112
113 static int
114 do_accept(pam_handle_t *pamh, struct rad_handle *radh)
115 {
116         int attrtype;
117         const void *attrval;
118         size_t attrlen;
119         char *s;
120
121         while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
122                 if (attrtype == RAD_USER_NAME) {
123                         s = rad_cvt_string(attrval, attrlen);
124                         if (s == NULL) {
125                                 syslog(LOG_CRIT,
126                                     "rad_cvt_string: out of memory");
127                                 return (-1);
128                         }
129                         pam_set_item(pamh, PAM_USER, s);
130                         free(s);
131                 }
132         }
133         if (attrtype == -1) {
134                 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
135                 return (-1);
136         }
137         return (0);
138 }
139
140 static int
141 do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user)
142 {
143         int retval;
144         int attrtype;
145         const void *attrval;
146         size_t attrlen;
147         const void *state;
148         size_t statelen;
149         struct pam_message msgs[MAX_CHALLENGE_MSGS];
150         const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
151         struct pam_response *resp;
152         int num_msgs;
153         const void *item;
154         const struct pam_conv *conv;
155
156         state = NULL;
157         statelen = 0;
158         num_msgs = 0;
159         while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
160                 switch (attrtype) {
161
162                 case RAD_STATE:
163                         state = attrval;
164                         statelen = attrlen;
165                         break;
166
167                 case RAD_REPLY_MESSAGE:
168                         if (num_msgs >= MAX_CHALLENGE_MSGS) {
169                                 syslog(LOG_CRIT,
170                                     "Too many RADIUS challenge messages");
171                                 return (PAM_SERVICE_ERR);
172                         }
173                         msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
174                         if (msgs[num_msgs].msg == NULL) {
175                                 syslog(LOG_CRIT,
176                                     "rad_cvt_string: out of memory");
177                                 return (PAM_SERVICE_ERR);
178                         }
179                         msgs[num_msgs].msg_style = PAM_TEXT_INFO;
180                         msg_ptrs[num_msgs] = &msgs[num_msgs];
181                         num_msgs++;
182                         break;
183                 }
184         }
185         if (attrtype == -1) {
186                 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
187                 return (PAM_SERVICE_ERR);
188         }
189         if (num_msgs == 0) {
190                 msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
191                 if (msgs[num_msgs].msg == NULL) {
192                         syslog(LOG_CRIT, "Out of memory");
193                         return (PAM_SERVICE_ERR);
194                 }
195                 msgs[num_msgs].msg_style = PAM_TEXT_INFO;
196                 msg_ptrs[num_msgs] = &msgs[num_msgs];
197                 num_msgs++;
198         }
199         msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
200         if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
201                 syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
202                 return (retval);
203         }
204         conv = (const struct pam_conv *)item;
205         if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
206             conv->appdata_ptr)) != PAM_SUCCESS)
207                 return (retval);
208         if (build_access_request(radh, user, resp[num_msgs-1].resp, NULL,
209             state, statelen) == -1)
210                 return (PAM_SERVICE_ERR);
211         memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
212         free(resp[num_msgs-1].resp);
213         free(resp);
214         while (num_msgs > 0)
215                 free(msgs[--num_msgs].msg);
216         return (PAM_SUCCESS);
217 }
218
219 PAM_EXTERN int
220 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
221     int argc, const char *argv[])
222 {
223         struct options options;
224         struct rad_handle *radh;
225         const char *user, *tmpuser, *pass;
226         char *conf_file, *template_user, *nas_id;
227         int retval;
228         int e;
229
230         pam_std_option(&options, other_options, argc, argv);
231
232         PAM_LOG("Options processed");
233
234         conf_file = NULL;
235         pam_test_option(&options, PAM_OPT_CONF, &conf_file);
236         template_user = NULL;
237         pam_test_option(&options, PAM_OPT_TEMPLATE_USER, &template_user);
238         nas_id = NULL;
239         pam_test_option(&options, PAM_OPT_NAS_ID, &nas_id);
240
241         retval = pam_get_user(pamh, &user, NULL);
242         if (retval != PAM_SUCCESS)
243                 return (retval);
244
245         PAM_LOG("Got user: %s", user);
246
247         retval = pam_get_pass(pamh, &pass, PASSWORD_PROMPT, &options);
248         if (retval != PAM_SUCCESS)
249                 return (retval);
250
251         PAM_LOG("Got password");
252
253         radh = rad_open();
254         if (radh == NULL) {
255                 syslog(LOG_CRIT, "rad_open failed");
256                 return (PAM_SERVICE_ERR);
257         }
258
259         PAM_LOG("Radius opened");
260
261         if (rad_config(radh, conf_file) == -1) {
262                 syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
263                 rad_close(radh);
264                 return (PAM_SERVICE_ERR);
265         }
266
267         PAM_LOG("Radius config file read");
268
269         if (build_access_request(radh, user, pass, nas_id, NULL, 0) == -1) {
270                 rad_close(radh);
271                 return (PAM_SERVICE_ERR);
272         }
273
274         PAM_LOG("Radius build access done");
275
276         for (;;) {
277                 switch (rad_send_request(radh)) {
278
279                 case RAD_ACCESS_ACCEPT:
280                         e = do_accept(pamh, radh);
281                         rad_close(radh);
282                         if (e == -1)
283                                 return (PAM_SERVICE_ERR);
284                         if (template_user != NULL) {
285
286                                 PAM_LOG("Trying template user: %s",
287                                     template_user);
288
289                                 /*
290                                  * If the given user name doesn't exist in
291                                  * the local password database, change it
292                                  * to the value given in the "template_user"
293                                  * option.
294                                  */
295                                 retval = pam_get_item(pamh, PAM_USER,
296                                     (const void **)&tmpuser);
297                                 if (retval != PAM_SUCCESS)
298                                         return (retval);
299                                 if (getpwnam(tmpuser) == NULL) {
300                                         pam_set_item(pamh, PAM_USER,
301                                             template_user);
302                                         PAM_LOG("Using template user");
303                                 }
304
305                         }
306                         return (PAM_SUCCESS);
307
308                 case RAD_ACCESS_REJECT:
309                         rad_close(radh);
310                         PAM_VERBOSE_ERROR("Radius rejection");
311                         return (PAM_AUTH_ERR);
312
313                 case RAD_ACCESS_CHALLENGE:
314                         retval = do_challenge(pamh, radh, user);
315                         if (retval != PAM_SUCCESS) {
316                                 rad_close(radh);
317                                 return (retval);
318                         }
319                         break;
320
321                 case -1:
322                         syslog(LOG_CRIT, "rad_send_request: %s",
323                             rad_strerror(radh));
324                         rad_close(radh);
325                         PAM_VERBOSE_ERROR("Radius failure");
326                         return (PAM_AUTHINFO_UNAVAIL);
327
328                 default:
329                         syslog(LOG_CRIT,
330                             "rad_send_request: unexpected return value");
331                         rad_close(radh);
332                         PAM_VERBOSE_ERROR("Radius error");
333                         return (PAM_SERVICE_ERR);
334                 }
335         }
336 }
337
338 PAM_EXTERN int
339 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
340     int argc __unused, const char *argv[] __unused)
341 {
342
343         return (PAM_SUCCESS);
344 }
345
346 PAM_MODULE_ENTRY("pam_radius");