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)
41 uint32_t port = 0; /* RFC 2865 NAS-Port is 4 bytes */
44 if ((cli = fr_pair_find_by_num(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) == NULL) {
48 if ((pair = fr_pair_find_by_num(request->packet->vps, PW_NAS_PORT, 0, TAG_ANY)) != NULL) {
49 port = pair->vp_integer;
52 if (request->packet->dst_port == 0) {
53 if (fr_pair_find_by_num(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 = fr_pair_find_by_num(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 = fr_pair_find_by_num(request->config, 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 (fr_pair_find_by_num(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_AUTH_TYPE_ACCEPT or
181 * PW_AUTH_TYPE_REJECT.
183 fr_cursor_init(&cursor, &request->config);
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_AUTH_TYPE_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) && (rad_debug_lvl)) {
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_AUTH_TYPE_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 (fr_pair_find_by_num(request->config, 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 (fr_pair_find_by_num(request->config, 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 fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
296 vp = pair_make_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 fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
301 vp = pair_make_config("Post-Auth-Type", "Reject", T_OP_SET);
302 if (!vp) return RLM_MODULE_OK;
305 vp = fr_pair_find_by_num(request->config, 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 = fr_pair_find_by_num(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_pair_create(request,
437 if (tmp) tmp->vp_integer = PW_AUTH_TYPE_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 = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
477 if (!request->password) {
478 request->password = fr_pair_find_by_num(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 = fr_pair_find_by_num(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 = fr_pair_find_by_num(request->config, 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 = fr_pair_find_by_num(request->config, 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);
573 return RLM_MODULE_HANDLED;
579 * Failed to validate the user.
581 * We PRESUME that the code which failed will clean up
582 * request->reply->vps, to be ONLY the reply items it
583 * wants to send back.
586 RDEBUG2("Failed to authenticate the user");
587 request->reply->code = PW_CODE_ACCESS_REJECT;
589 if ((module_msg = fr_pair_find_by_num(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL){
590 char msg[MAX_STRING_LEN+19];
592 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
593 module_msg->vp_strvalue);
594 rad_authlog(msg, request, 0);
596 rad_authlog("Login incorrect", request, 0);
599 if (request->password) {
600 VERIFY_VP(request->password);
601 /* double check: maybe the secret is wrong? */
602 if ((rad_debug_lvl > 1) && (request->password->da->attr == PW_USER_PASSWORD)) {
605 p = (uint8_t const *) request->password->vp_strvalue;
609 size = fr_utf8_char(p, -1);
611 RWDEBUG("Unprintable characters in the password. Double-check the "
612 "shared secret on the server and the NAS!");
621 #ifdef WITH_SESSION_MGMT
623 (check_item = fr_pair_find_by_num(request->config, PW_SIMULTANEOUS_USE, 0, TAG_ANY)) != NULL) {
624 int r, session_type = 0;
626 char umsg[MAX_STRING_LEN + 1];
628 tmp = fr_pair_find_by_num(request->config, PW_SESSION_TYPE, 0, TAG_ANY);
630 session_type = tmp->vp_integer;
631 RDEBUG2("Using Session-Type %s",
632 dict_valnamebyattr(PW_SESSION_TYPE, 0, session_type));
636 * User authenticated O.K. Now we have to check
637 * for the Simultaneous-Use parameter.
639 if (request->username &&
640 (r = process_checksimul(session_type, request, check_item->vp_integer)) != 0) {
644 /* Multilink attempt. Check if port-limit > simultaneous-use */
645 VALUE_PAIR *port_limit;
647 if ((port_limit = fr_pair_find_by_num(request->reply->vps, PW_PORT_LIMIT, 0, TAG_ANY)) != NULL &&
648 port_limit->vp_integer > check_item->vp_integer){
649 RDEBUG2("MPP is OK");
654 if (check_item->vp_integer > 1) {
655 snprintf(umsg, sizeof(umsg), "%s (%u)", main_config.denied_msg,
656 check_item->vp_integer);
658 strlcpy(umsg, main_config.denied_msg, sizeof(umsg));
661 request->reply->code = PW_CODE_ACCESS_REJECT;
664 * They're trying to log in too many times.
665 * Remove ALL reply attributes.
667 fr_pair_list_free(&request->reply->vps);
668 pair_make_reply("Reply-Message", umsg, T_OP_SET);
670 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
671 check_item->vp_integer,
672 r == 2 ? "[MPP attempt]" : "");
673 rad_authlog(logstr, request, 1);
682 * Result should be >= 0 here - if not, it means the user
683 * is rejected, so we just process post-auth and return.
686 return RLM_MODULE_REJECT;
690 * Set the reply to Access-Accept, if it hasn't already
691 * been set to something. (i.e. Access-Challenge)
693 if (request->reply->code == 0) {
694 request->reply->code = PW_CODE_ACCESS_ACCEPT;
701 * Run a virtual server auth and postauth
704 int rad_virtual_server(REQUEST *request)
709 RDEBUG("Virtual server %s received request", request->server);
710 rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, NULL);
712 RDEBUG("server %s {", request->server);
716 * We currently only handle AUTH packets here.
717 * This could be expanded to handle other packets as well if required.
719 rad_assert(request->packet->code == PW_CODE_ACCESS_REQUEST);
721 result = rad_authenticate(request);
723 if (request->reply->code == PW_CODE_ACCESS_REJECT) {
724 fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
725 vp = pair_make_config("Post-Auth-Type", "Reject", T_OP_SET);
726 if (vp) rad_postauth(request);
729 if (request->reply->code == PW_CODE_ACCESS_ACCEPT) {
730 rad_postauth(request);
734 RDEBUG("} # server %s", request->server);
736 RDEBUG("Virtual server sending reply");
737 rdebug_pair_list(L_DBG_LVL_1, request, request->reply->vps, NULL);