2 * auth.c User authentication.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Jeff Carneal <jeff@apex.net>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
33 * Return a short string showing the terminal server, port
34 * and calling station ID.
36 char *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli)
43 if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) == NULL)
45 if ((pair = pairfind(request->packet->vps, PW_NAS_PORT, 0, TAG_ANY)) != NULL)
46 port = pair->vp_integer;
48 if (request->packet->dst_port == 0) {
49 if (pairfind(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0, TAG_ANY)) {
50 tls = " via TLS tunnel";
52 tls = " via proxy to virtual server";
56 snprintf(buf, buflen, "from client %.128s port %u%s%.128s%s",
57 request->client->shortname, port,
58 (do_cli ? " cli " : ""), (do_cli ? cli->vp_strvalue : ""),
67 * Make sure user/pass are clean
70 static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
73 char const *extra_msg = NULL;
74 char clean_password[1024];
75 char clean_username[1024];
79 VALUE_PAIR *username = NULL;
81 if (!request->root->log_auth) {
86 * Get the correct username based on the configured value
88 if (log_stripped_names == 0) {
89 username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
91 username = request->username;
95 * Clean up the username
97 if (username == NULL) {
98 strcpy(clean_username, "<no User-Name attribute>");
100 fr_print_string(username->vp_strvalue,
102 clean_username, sizeof(clean_username));
106 * Clean up the password
108 if (request->root->log_auth_badpass || request->root->log_auth_goodpass) {
109 if (!request->password) {
110 VALUE_PAIR *auth_type;
112 auth_type = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
114 snprintf(clean_password, sizeof(clean_password),
115 "<via Auth-Type = %s>",
116 dict_valnamebyattr(PW_AUTH_TYPE, 0,
117 auth_type->vp_integer));
119 strcpy(clean_password, "<no User-Password attribute>");
121 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) {
122 strcpy(clean_password, "<CHAP-Password>");
124 fr_print_string(request->password->vp_strvalue,
125 request->password->length,
126 clean_password, sizeof(clean_password));
131 logit = request->root->log_auth_goodpass;
132 extra_msg = request->root->auth_goodpass_msg;
134 logit = request->root->log_auth_badpass;
135 extra_msg = request->root->auth_badpass_msg;
141 if (radius_xlat(p, sizeof(extra) - 1, request, extra_msg, NULL, NULL) < 0) {
148 RAUTH("%s: [%s%s%s] (%s)%s",
152 logit ? clean_password : "",
153 auth_name(buf, sizeof(buf), request, 1),
164 * -2 Rejected (Auth-Type = Reject, send Port-Message back)
165 * 1 End check & return, don't reply
167 * NOTE: NOT the same as the RLM_ values !
169 static int rad_check_password(REQUEST *request)
172 VALUE_PAIR *auth_type_pair;
175 int auth_type_count = 0;
179 * Look for matching check items. We skip the whole lot
180 * if the authentication type is PW_AUTHTYPE_ACCEPT or
181 * PW_AUTHTYPE_REJECT.
183 paircursor(&cursor, &request->config_items);
184 while ((auth_type_pair = pairfindnext(&cursor, PW_AUTH_TYPE, 0, TAG_ANY))) {
185 auth_type = auth_type_pair->vp_integer;
188 RDEBUG2("Found Auth-Type = %s", dict_valnamebyattr(PW_AUTH_TYPE, 0, auth_type));
189 if (auth_type == PW_AUTHTYPE_REJECT) {
190 RDEBUG2("Auth-Type = Reject, rejecting user");
197 * Warn if more than one Auth-Type was found, because only the last
198 * one found will actually be used.
200 if ((auth_type_count > 1) && (debug_flag)) {
201 RERROR("Warning: Found %d auth-types on request for user '%s'",
202 auth_type_count, request->username->vp_strvalue);
206 * This means we have a proxy reply or an accept and it wasn't
207 * rejected in the above loop. So that means it is accepted and we
208 * do no further authentication.
210 if ((auth_type == PW_AUTHTYPE_ACCEPT)
215 RDEBUG2("Auth-Type = Accept, accepting the user");
220 * Check that Auth-Type has been set, and reject if not.
222 * Do quick checks to see if Cleartext-Password or Crypt-Password have
223 * been set, and complain if so.
226 if (pairfind(request->config_items, PW_CRYPT_PASSWORD, 0, TAG_ANY) != NULL) {
227 RWDEBUG2("Please update your configuration, and remove 'Auth-Type = Crypt'");
228 RWDEBUG2("Use the PAP module instead.");
230 else if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) != NULL) {
231 RWDEBUG2("Please update your configuration, and remove 'Auth-Type = Local'");
232 RWDEBUG2("Use the PAP or CHAP modules instead.");
236 * The admin hasn't told us how to
237 * authenticate the user, so we reject them!
242 REDEBUG2("No Auth-Type found: rejecting the user via Post-Auth-Type = Reject");
247 * See if there is a module that handles
248 * this Auth-Type, and turn the RLM_ return
249 * status into the values as defined at
250 * the top of this function.
252 result = process_authenticate(auth_type, request);
255 * An authentication module FAIL
256 * return code, or any return code that
257 * is not expected from authentication,
258 * is the same as an explicit REJECT!
260 case RLM_MODULE_FAIL:
261 case RLM_MODULE_INVALID:
262 case RLM_MODULE_NOOP:
263 case RLM_MODULE_NOTFOUND:
264 case RLM_MODULE_REJECT:
265 case RLM_MODULE_UPDATED:
266 case RLM_MODULE_USERLOCK:
273 case RLM_MODULE_HANDLED:
282 * Post-authentication step processes the response before it is
283 * sent to the NAS. It can receive both Access-Accept and Access-Reject
286 int rad_postauth(REQUEST *request)
289 int postauth_type = 0;
293 * Do post-authentication calls. ignoring the return code.
295 vp = pairfind(request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
297 postauth_type = vp->vp_integer;
298 RDEBUG2("Using Post-Auth-Type %s",
299 dict_valnamebyattr(PW_POST_AUTH_TYPE, 0, postauth_type));
301 result = process_post_auth(postauth_type, request);
304 * The module failed, or said to reject the user: Do so.
306 case RLM_MODULE_FAIL:
307 case RLM_MODULE_INVALID:
308 case RLM_MODULE_REJECT:
309 case RLM_MODULE_USERLOCK:
311 request->reply->code = PW_AUTHENTICATION_REJECT;
312 result = RLM_MODULE_REJECT;
315 * The module handled the request, cancel the reply.
317 case RLM_MODULE_HANDLED:
321 * The module had a number of OK return codes.
323 case RLM_MODULE_NOOP:
324 case RLM_MODULE_NOTFOUND:
326 case RLM_MODULE_UPDATED:
327 result = RLM_MODULE_OK;
334 * Process and reply to an authentication request
336 * The return value of this function isn't actually used right now, so
337 * it's not entirely clear if it is returning the right things. --Pac.
339 int rad_authenticate(REQUEST *request)
341 VALUE_PAIR *namepair;
342 #ifdef WITH_SESSION_MGMT
343 VALUE_PAIR *check_item;
345 VALUE_PAIR *module_msg;
346 VALUE_PAIR *tmp = NULL;
353 * If this request got proxied to another server, we need
354 * to check whether it authenticated the request or not.
356 if (request->proxy_reply) {
357 switch (request->proxy_reply->code) {
359 * Reply of ACCEPT means accept, thus set Auth-Type
362 case PW_AUTHENTICATION_ACK:
363 tmp = radius_paircreate(request,
364 &request->config_items,
366 if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
370 * Challenges are punted back to the NAS without any
371 * further processing.
373 case PW_ACCESS_CHALLENGE:
374 request->reply->code = PW_ACCESS_CHALLENGE;
375 return RLM_MODULE_OK;
377 * ALL other replies mean reject. (this is fail-safe)
379 * Do NOT do any authorization or authentication. They
380 * are being rejected, so we minimize the amount of work
381 * done by the server, by rejecting them here.
383 case PW_AUTHENTICATION_REJECT:
384 rad_authlog("Login incorrect (Home Server says so)",
386 request->reply->code = PW_AUTHENTICATION_REJECT;
387 return RLM_MODULE_REJECT;
390 rad_authlog("Login incorrect (Home Server failed to respond)",
392 return RLM_MODULE_REJECT;
398 * Get the username from the request.
400 * Note that namepair MAY be NULL, in which case there
401 * is no User-Name attribute in the request.
403 namepair = request->username;
406 * Look for, and cache, passwords.
408 if (!request->password) {
409 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
411 if (!request->password) {
412 request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
416 * Get the user's authorization information from the database
419 result = process_authorize(autz_type, request);
421 case RLM_MODULE_NOOP:
422 case RLM_MODULE_NOTFOUND:
424 case RLM_MODULE_UPDATED:
426 case RLM_MODULE_HANDLED:
428 case RLM_MODULE_FAIL:
429 case RLM_MODULE_INVALID:
430 case RLM_MODULE_REJECT:
431 case RLM_MODULE_USERLOCK:
433 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL) {
434 char msg[MAX_STRING_LEN + 16];
435 snprintf(msg, sizeof(msg), "Invalid user (%s)",
436 module_msg->vp_strvalue);
437 rad_authlog(msg,request,0);
439 rad_authlog("Invalid user", request, 0);
441 request->reply->code = PW_AUTHENTICATION_REJECT;
445 tmp = pairfind(request->config_items, PW_AUTZ_TYPE, 0, TAG_ANY);
447 autz_type = tmp->vp_integer;
448 RDEBUG2("Using Autz-Type %s",
449 dict_valnamebyattr(PW_AUTZ_TYPE, 0, autz_type));
456 * If we haven't already proxied the packet, then check
457 * to see if we should. Maybe one of the authorize
458 * modules has decided that a proxy should be used. If
459 * so, get out of here and send the packet.
463 (request->proxy == NULL) &&
465 ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY)) != NULL)) {
468 realm = realm_find2(tmp->vp_strvalue);
471 * Don't authenticate, as the request is going to
474 if (realm && realm->auth_pool) {
475 return RLM_MODULE_OK;
479 * Catch users who set Proxy-To-Realm to a LOCAL
480 * realm (sigh). But don't complain if it is
483 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
484 RWDEBUG2("You set Proxy-To-Realm = %s, but it is a LOCAL realm! Cancelling proxy request.", realm->name);
488 RWDEBUG2("You set Proxy-To-Realm = %s, but the realm does not exist! Cancelling invalid proxy request.", tmp->vp_strvalue);
497 * Perhaps there is a Stripped-User-Name now.
499 namepair = request->username;
505 result = rad_check_password(request);
508 return RLM_MODULE_HANDLED;
513 * Failed to validate the user.
515 * We PRESUME that the code which failed will clean up
516 * request->reply->vps, to be ONLY the reply items it
517 * wants to send back.
520 RDEBUG2("Failed to authenticate the user.");
521 request->reply->code = PW_AUTHENTICATION_REJECT;
523 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL){
524 char msg[MAX_STRING_LEN+19];
526 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
527 module_msg->vp_strvalue);
528 rad_authlog(msg, request, 0);
530 rad_authlog("Login incorrect", request, 0);
533 if (request->password) {
534 VERIFY_VP(request->password);
535 /* double check: maybe the secret is wrong? */
536 if ((debug_flag > 1) && (request->password->da->attr == PW_USER_PASSWORD)) {
539 p = (uint8_t const *) request->password->vp_strvalue;
543 size = fr_utf8_char(p);
545 RWDEBUG("Unprintable characters in the password. Double-check the "
546 "shared secret on the server and the NAS!");
555 #ifdef WITH_SESSION_MGMT
557 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE, 0, TAG_ANY)) != NULL) {
558 int r, session_type = 0;
560 char umsg[MAX_STRING_LEN + 1];
561 char const *user_msg = NULL;
563 tmp = pairfind(request->config_items, PW_SESSION_TYPE, 0, TAG_ANY);
565 session_type = tmp->vp_integer;
566 RDEBUG2("Using Session-Type %s",
567 dict_valnamebyattr(PW_SESSION_TYPE, 0, session_type));
571 * User authenticated O.K. Now we have to check
572 * for the Simultaneous-Use parameter.
575 (r = process_checksimul(session_type, request, check_item->vp_integer)) != 0) {
579 /* Multilink attempt. Check if port-limit > simultaneous-use */
580 VALUE_PAIR *port_limit;
582 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT, 0, TAG_ANY)) != NULL &&
583 port_limit->vp_integer > check_item->vp_integer){
584 RDEBUG2("MPP is OK");
589 if (check_item->vp_integer > 1) {
590 snprintf(umsg, sizeof(umsg),
591 "\r\nYou are already logged in %d times - access denied\r\n\n",
592 (int)check_item->vp_integer);
595 user_msg = "\r\nYou are already logged in - access denied\r\n\n";
598 request->reply->code = PW_AUTHENTICATION_REJECT;
601 * They're trying to log in too many times.
602 * Remove ALL reply attributes.
604 pairfree(&request->reply->vps);
605 pairmake_reply("Reply-Message",
608 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
609 check_item->vp_integer,
610 r == 2 ? "[MPP attempt]" : "");
611 rad_authlog(logstr, request, 1);
620 * Result should be >= 0 here - if not, it means the user
621 * is rejected, so we just process post-auth and return.
624 return RLM_MODULE_REJECT;
628 * Set the reply to Access-Accept, if it hasn't already
629 * been set to something. (i.e. Access-Challenge)
631 if (request->reply->code == 0)
632 request->reply->code = PW_AUTHENTICATION_ACK;
634 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_SUCCESS_MESSAGE, 0, TAG_ANY)) != NULL){
635 char msg[MAX_STRING_LEN+12];
637 snprintf(msg, sizeof(msg), "Login OK (%s)",
638 module_msg->vp_strvalue);
639 rad_authlog(msg, request, 1);
641 rad_authlog("Login OK", request, 1);
648 * Run a virtual server auth and postauth
651 int rad_virtual_server(REQUEST *request)
657 * We currently only handle AUTH packets here.
658 * This could be expanded to handle other packets as well if required.
660 rad_assert(request->packet->code == PW_AUTHENTICATION_REQUEST);
662 result = rad_authenticate(request);
664 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
665 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
666 vp = pairmake_config("Post-Auth-Type", "Reject", T_OP_SET);
667 if (vp) rad_postauth(request);
670 if (request->reply->code == PW_AUTHENTICATION_ACK) {
671 rad_postauth(request);