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/state.h>
29 #include <freeradius-devel/rad_assert.h>
34 * Return a short string showing the terminal server, port
35 * and calling station ID.
37 char *auth_name(char *buf, size_t buflen, REQUEST *request, bool do_cli)
44 if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) == NULL) {
48 if ((pair = pairfind(request->packet->vps, PW_NAS_PORT, 0, TAG_ANY)) != NULL) {
49 port = pair->vp_integer;
52 if (request->packet->dst_port == 0) {
53 if (pairfind(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0, TAG_ANY)) {
54 tls = " via TLS tunnel";
56 tls = " via proxy to virtual server";
60 snprintf(buf, buflen, "from client %.128s port %u%s%.128s%s",
61 request->client->shortname, port,
62 (do_cli ? " cli " : ""), (do_cli ? cli->vp_strvalue : ""),
71 * Make sure user/pass are clean
74 static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
77 char const *extra_msg = NULL;
78 char clean_password[1024];
79 char clean_username[1024];
83 VALUE_PAIR *username = NULL;
85 if (!request->root->log_auth) {
90 * Get the correct username based on the configured value
92 if (!log_stripped_names) {
93 username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
95 username = request->username;
99 * Clean up the username
101 if (username == NULL) {
102 strcpy(clean_username, "<no User-Name attribute>");
104 fr_prints(clean_username, sizeof(clean_username), username->vp_strvalue, username->vp_length, '\0');
108 * Clean up the password
110 if (request->root->log_auth_badpass || request->root->log_auth_goodpass) {
111 if (!request->password) {
112 VALUE_PAIR *auth_type;
114 auth_type = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
116 snprintf(clean_password, sizeof(clean_password),
117 "<via Auth-Type = %s>",
118 dict_valnamebyattr(PW_AUTH_TYPE, 0,
119 auth_type->vp_integer));
121 strcpy(clean_password, "<no User-Password attribute>");
123 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) {
124 strcpy(clean_password, "<CHAP-Password>");
126 fr_prints(clean_password, sizeof(clean_password),
127 request->password->vp_strvalue, request->password->vp_length, '\0');
132 logit = request->root->log_auth_goodpass;
133 extra_msg = request->root->auth_goodpass_msg;
135 logit = request->root->log_auth_badpass;
136 extra_msg = request->root->auth_badpass_msg;
142 if (radius_xlat(p, sizeof(extra) - 1, request, extra_msg, NULL, NULL) < 0) {
149 RAUTH("%s: [%s%s%s] (%s)%s",
153 logit ? clean_password : "",
154 auth_name(buf, sizeof(buf), request, 1),
165 * -2 Rejected (Auth-Type = Reject, send Port-Message back)
166 * 1 End check & return, don't reply
168 * NOTE: NOT the same as the RLM_ values !
170 static int CC_HINT(nonnull) rad_check_password(REQUEST *request)
173 VALUE_PAIR *auth_type_pair;
176 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 fr_cursor_init(&cursor, &request->config_items);
184 while ((auth_type_pair = fr_cursor_next_by_num(&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:
275 case RLM_MODULE_HANDLED:
284 * Post-authentication step processes the response before it is
285 * sent to the NAS. It can receive both Access-Accept and Access-Reject
288 int rad_postauth(REQUEST *request)
291 int postauth_type = 0;
294 if (request->reply->code == PW_CODE_ACCESS_CHALLENGE) {
295 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
296 vp = pairmake_config("Post-Auth-Type", "Challenge", T_OP_SET);
297 if (!vp) return RLM_MODULE_OK;
299 } else if (request->reply->code == PW_CODE_ACCESS_REJECT) {
300 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
301 vp = pairmake_config("Post-Auth-Type", "Reject", T_OP_SET);
302 if (!vp) return RLM_MODULE_OK;
305 vp = pairfind(request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
309 * If a method was chosen, use that.
312 postauth_type = vp->vp_integer;
313 RDEBUG2("Using Post-Auth-Type %s",
314 dict_valnamebyattr(PW_POST_AUTH_TYPE, 0, postauth_type));
317 result = process_post_auth(postauth_type, request);
320 * The module failed, or said to reject the user: Do so.
322 case RLM_MODULE_FAIL:
323 case RLM_MODULE_INVALID:
324 case RLM_MODULE_REJECT:
325 case RLM_MODULE_USERLOCK:
328 * We WERE going to have a nice reply, but
329 * something went wrong. So we've got to run
330 * Post-Auth-Type Reject, which is defined in the
331 * dictionaries as having value "1".
333 if (request->reply->code != PW_CODE_ACCESS_REJECT) {
334 RDEBUG("Using Post-Auth-Type Reject");
336 process_post_auth(1, request);
339 fr_state_discard(request, request->packet);
340 result = RLM_MODULE_REJECT;
343 * The module handled the request, cancel the reply.
345 case RLM_MODULE_HANDLED:
349 * The module had a number of OK return codes.
351 case RLM_MODULE_NOOP:
352 case RLM_MODULE_NOTFOUND:
354 case RLM_MODULE_UPDATED:
355 result = RLM_MODULE_OK;
357 if (request->reply->code == PW_CODE_ACCESS_CHALLENGE) {
358 fr_state_put_vps(request, request->packet, request->reply);
361 fr_state_discard(request, request->packet);
367 * Rejects during authorize, etc. are handled by the
368 * earlier code, which logs a reason for the rejection.
369 * If the packet is rejected in post-auth, we need to log
370 * that as a separate reason.
372 if (result == RLM_MODULE_REJECT) {
373 if (request->reply->code != RLM_MODULE_REJECT) {
374 rad_authlog("Rejected in post-auth", request, 0);
376 request->reply->code = PW_CODE_ACCESS_REJECT;
380 * If we're still accepting the user, say so.
382 if (request->reply->code == PW_CODE_ACCESS_ACCEPT) {
383 if ((vp = pairfind(request->packet->vps, PW_MODULE_SUCCESS_MESSAGE, 0, TAG_ANY)) != NULL) {
384 char msg[MAX_STRING_LEN+12];
386 snprintf(msg, sizeof(msg), "Login OK (%s)",
388 rad_authlog(msg, request, 1);
390 rad_authlog("Login OK", request, 1);
398 * Process and reply to an authentication request
400 * The return value of this function isn't actually used right now, so
401 * it's not entirely clear if it is returning the right things. --Pac.
403 int rad_authenticate(REQUEST *request)
405 #ifdef WITH_SESSION_MGMT
406 VALUE_PAIR *check_item;
408 VALUE_PAIR *module_msg;
409 VALUE_PAIR *tmp = NULL;
416 * If this request got proxied to another server, we need
417 * to check whether it authenticated the request or not.
419 * request->proxy gets set only AFTER authorization, so
420 * it's safe to check it here. If it exists, it means
421 * we're doing a second pass through rad_authenticate().
423 if (request->proxy) {
426 if (request->proxy_reply) code = request->proxy_reply->code;
430 * Reply of ACCEPT means accept, thus set Auth-Type
433 case PW_CODE_ACCESS_ACCEPT:
434 tmp = radius_paircreate(request,
435 &request->config_items,
437 if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
441 * Challenges are punted back to the NAS without any
442 * further processing.
444 case PW_CODE_ACCESS_CHALLENGE:
445 request->reply->code = PW_CODE_ACCESS_CHALLENGE;
446 fr_state_put_vps(request, request->packet, request->reply);
447 return RLM_MODULE_OK;
450 * ALL other replies mean reject. (this is fail-safe)
452 * Do NOT do any authorization or authentication. They
453 * are being rejected, so we minimize the amount of work
454 * done by the server, by rejecting them here.
456 case PW_CODE_ACCESS_REJECT:
457 rad_authlog("Login incorrect (Home Server says so)",
459 request->reply->code = PW_CODE_ACCESS_REJECT;
460 fr_state_discard(request, request->packet);
461 return RLM_MODULE_REJECT;
464 rad_authlog("Login incorrect (Home Server failed to respond)",
466 fr_state_discard(request, request->packet);
467 return RLM_MODULE_REJECT;
472 * Look for, and cache, passwords.
474 if (!request->password) {
475 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
477 if (!request->password) {
478 request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
482 * Grab the VPS associated with the State attribute.
484 fr_state_get_vps(request, request->packet);
487 * Get the user's authorization information from the database
490 result = process_authorize(autz_type, request);
492 case RLM_MODULE_NOOP:
493 case RLM_MODULE_NOTFOUND:
495 case RLM_MODULE_UPDATED:
497 case RLM_MODULE_HANDLED:
499 case RLM_MODULE_FAIL:
500 case RLM_MODULE_INVALID:
501 case RLM_MODULE_REJECT:
502 case RLM_MODULE_USERLOCK:
504 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL) {
505 char msg[MAX_STRING_LEN + 16];
506 snprintf(msg, sizeof(msg), "Invalid user (%s)",
507 module_msg->vp_strvalue);
508 rad_authlog(msg,request,0);
510 rad_authlog("Invalid user", request, 0);
512 request->reply->code = PW_CODE_ACCESS_REJECT;
516 tmp = pairfind(request->config_items, PW_AUTZ_TYPE, 0, TAG_ANY);
518 autz_type = tmp->vp_integer;
519 RDEBUG2("Using Autz-Type %s",
520 dict_valnamebyattr(PW_AUTZ_TYPE, 0, autz_type));
527 * If we haven't already proxied the packet, then check
528 * to see if we should. Maybe one of the authorize
529 * modules has decided that a proxy should be used. If
530 * so, get out of here and send the packet.
534 (request->proxy == NULL) &&
536 ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY)) != NULL)) {
539 realm = realm_find2(tmp->vp_strvalue);
542 * Don't authenticate, as the request is going to
545 if (realm && realm->auth_pool) {
546 return RLM_MODULE_OK;
550 * Catch users who set Proxy-To-Realm to a LOCAL
551 * realm (sigh). But don't complain if it is
554 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
555 RWDEBUG2("You set Proxy-To-Realm = %s, but it is a LOCAL realm! Cancelling proxy request.", realm->name);
559 RWDEBUG2("You set Proxy-To-Realm = %s, but the realm does not exist! Cancelling invalid proxy request.", tmp->vp_strvalue);
571 result = rad_check_password(request);
574 * We presume that the reply has been set by someone.
576 if (request->reply->code == PW_CODE_ACCESS_CHALLENGE) {
577 fr_state_put_vps(request, request->packet, request->reply);
580 fr_state_discard(request, request->packet);
582 return RLM_MODULE_HANDLED;
588 * Failed to validate the user.
590 * We PRESUME that the code which failed will clean up
591 * request->reply->vps, to be ONLY the reply items it
592 * wants to send back.
595 RDEBUG2("Failed to authenticate the user");
596 request->reply->code = PW_CODE_ACCESS_REJECT;
598 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL){
599 char msg[MAX_STRING_LEN+19];
601 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
602 module_msg->vp_strvalue);
603 rad_authlog(msg, request, 0);
605 rad_authlog("Login incorrect", request, 0);
608 if (request->password) {
609 VERIFY_VP(request->password);
610 /* double check: maybe the secret is wrong? */
611 if ((debug_flag > 1) && (request->password->da->attr == PW_USER_PASSWORD)) {
614 p = (uint8_t const *) request->password->vp_strvalue;
618 size = fr_utf8_char(p);
620 RWDEBUG("Unprintable characters in the password. Double-check the "
621 "shared secret on the server and the NAS!");
630 #ifdef WITH_SESSION_MGMT
632 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE, 0, TAG_ANY)) != NULL) {
633 int r, session_type = 0;
635 char umsg[MAX_STRING_LEN + 1];
637 tmp = pairfind(request->config_items, PW_SESSION_TYPE, 0, TAG_ANY);
639 session_type = tmp->vp_integer;
640 RDEBUG2("Using Session-Type %s",
641 dict_valnamebyattr(PW_SESSION_TYPE, 0, session_type));
645 * User authenticated O.K. Now we have to check
646 * for the Simultaneous-Use parameter.
648 if (request->username &&
649 (r = process_checksimul(session_type, request, check_item->vp_integer)) != 0) {
653 /* Multilink attempt. Check if port-limit > simultaneous-use */
654 VALUE_PAIR *port_limit;
656 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT, 0, TAG_ANY)) != NULL &&
657 port_limit->vp_integer > check_item->vp_integer){
658 RDEBUG2("MPP is OK");
663 if (check_item->vp_integer > 1) {
664 snprintf(umsg, sizeof(umsg), "%s (%u)", main_config.denied_msg,
665 check_item->vp_integer);
667 strlcpy(umsg, main_config.denied_msg, sizeof(umsg));
670 request->reply->code = PW_CODE_ACCESS_REJECT;
673 * They're trying to log in too many times.
674 * Remove ALL reply attributes.
676 pairfree(&request->reply->vps);
677 pairmake_reply("Reply-Message", umsg, T_OP_SET);
679 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
680 check_item->vp_integer,
681 r == 2 ? "[MPP attempt]" : "");
682 rad_authlog(logstr, request, 1);
691 * Result should be >= 0 here - if not, it means the user
692 * is rejected, so we just process post-auth and return.
695 return RLM_MODULE_REJECT;
699 * Set the reply to Access-Accept, if it hasn't already
700 * been set to something. (i.e. Access-Challenge)
702 if (request->reply->code == 0) {
703 request->reply->code = PW_CODE_ACCESS_ACCEPT;
710 * Run a virtual server auth and postauth
713 int rad_virtual_server(REQUEST *request)
718 RDEBUG("Virtual server received request");
719 rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, NULL);
721 RDEBUG("server %s {", request->server);
725 * We currently only handle AUTH packets here.
726 * This could be expanded to handle other packets as well if required.
728 rad_assert(request->packet->code == PW_CODE_ACCESS_REQUEST);
730 result = rad_authenticate(request);
732 if (request->reply->code == PW_CODE_ACCESS_REJECT) {
733 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
734 vp = pairmake_config("Post-Auth-Type", "Reject", T_OP_SET);
735 if (vp) rad_postauth(request);
738 if (request->reply->code == PW_CODE_ACCESS_ACCEPT) {
739 rad_postauth(request);
743 RDEBUG("} # server %s", request->server);
745 RDEBUG("Virtual server sending reply");
746 rdebug_pair_list(L_DBG_LVL_1, request, request->reply->vps, NULL);