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>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
30 #include <freeradius-devel/rad_assert.h>
35 * Return a short string showing the terminal server, port
36 * and calling station ID.
38 char *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli)
45 if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0)) == NULL)
47 if ((pair = pairfind(request->packet->vps, PW_NAS_PORT, 0)) != NULL)
48 port = pair->vp_integer;
50 if (request->packet->dst_port == 0) {
51 if (pairfind(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0)) {
52 tls = " via TLS tunnel";
54 tls = " via proxy to virtual server";
58 snprintf(buf, buflen, "from client %.128s port %u%s%.128s%s",
59 request->client->shortname, port,
60 (do_cli ? " cli " : ""), (do_cli ? (char *)cli->vp_strvalue : ""),
69 * Make sure user/pass are clean
72 static int rad_authlog(const char *msg, REQUEST *request, int goodpass)
75 const char *extra_msg = NULL;
76 char clean_password[1024];
77 char clean_username[1024];
80 VALUE_PAIR *username = NULL;
82 if (!request->root->log_auth) {
87 * Get the correct username based on the configured value
89 if (log_stripped_names == 0) {
90 username = pairfind(request->packet->vps, PW_USER_NAME, 0);
92 username = request->username;
96 * Clean up the username
98 if (username == NULL) {
99 strcpy(clean_username, "<no User-Name attribute>");
101 fr_print_string((char *)username->vp_strvalue,
103 clean_username, sizeof(clean_username));
107 * Clean up the password
109 if (request->root->log_auth_badpass || request->root->log_auth_goodpass) {
110 if (!request->password) {
111 VALUE_PAIR *auth_type;
113 auth_type = pairfind(request->config_items,
115 if (auth_type && (auth_type->vp_strvalue[0] != '\0')) {
116 snprintf(clean_password, sizeof(clean_password),
117 "<via Auth-Type = %s>",
118 auth_type->vp_strvalue);
120 strcpy(clean_password, "<no User-Password attribute>");
122 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0)) {
123 strcpy(clean_password, "<CHAP-Password>");
125 fr_print_string((char *)request->password->vp_strvalue,
126 request->password->length,
127 clean_password, sizeof(clean_password));
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;
141 radius_xlat(extra + 1, sizeof(extra) - 1, extra_msg, request,
147 radlog_request(L_AUTH, 0, request, "%s: [%s%s%s] (%s)%s",
151 logit ? clean_password : "",
152 auth_name(buf, sizeof(buf), request, 1),
163 * -2 Rejected (Auth-Type = Reject, send Port-Message back)
164 * 1 End check & return, don't reply
166 * NOTE: NOT the same as the RLM_ values !
168 static int rad_check_password(REQUEST *request)
170 VALUE_PAIR *auth_type_pair;
171 VALUE_PAIR *cur_config_item;
172 VALUE_PAIR *password_pair;
173 VALUE_PAIR *auth_item;
174 uint8_t my_chap[MAX_STRING_LEN];
177 int auth_type_count = 0;
181 * Look for matching check items. We skip the whole lot
182 * if the authentication type is PW_AUTHTYPE_ACCEPT or
183 * PW_AUTHTYPE_REJECT.
185 cur_config_item = request->config_items;
186 while(((auth_type_pair = pairfind(cur_config_item, PW_AUTH_TYPE, 0))) != NULL) {
188 auth_type = auth_type_pair->vp_integer;
190 dv = dict_valbyattr(PW_AUTH_TYPE,
191 auth_type_pair->vp_integer, 0);
193 RDEBUG2("Found Auth-Type = %s",
194 (dv != NULL) ? dv->name : "?");
195 cur_config_item = auth_type_pair->next;
197 if (auth_type == PW_AUTHTYPE_REJECT) {
198 RDEBUG2("Auth-Type = Reject, rejecting user");
203 if (( auth_type_count > 1) && (debug_flag)) {
204 radlog_request(L_ERR, 0, request, "Warning: Found %d auth-types on request for user '%s'",
205 auth_type_count, request->username->vp_strvalue);
209 * This means we have a proxy reply or an accept
210 * and it wasn't rejected in the above loop. So
211 * that means it is accepted and we do no further
214 if ((auth_type == PW_AUTHTYPE_ACCEPT)
219 RDEBUG2("Auth-Type = Accept, accepting the user");
223 password_pair = pairfind(request->config_items, PW_USER_PASSWORD, 0);
225 pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0)) {
226 pairdelete(&request->config_items, PW_USER_PASSWORD, 0);
227 password_pair = NULL;
233 RDEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
234 RDEBUG("!!! Replacing User-Password in config items with Cleartext-Password. !!!");
235 RDEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
236 RDEBUG("!!! Please update your configuration so that the \"known good\" !!!");
237 RDEBUG("!!! clear text password is in Cleartext-Password, and not in User-Password. !!!");
238 RDEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
239 password_pair->attribute = PW_CLEARTEXT_PASSWORD;
240 da = dict_attrbyvalue(PW_CLEARTEXT_PASSWORD, 0);
242 radlog_request(L_ERR, 0, request, "FATAL: You broke the dictionaries. Please use the default dictionaries!");
246 password_pair->name = da->name;
250 * Find the "known good" password.
252 * FIXME: We should get rid of these hacks, and replace
253 * them with a module.
255 if ((password_pair = pairfind(request->config_items, PW_CRYPT_PASSWORD, 0)) != NULL) {
257 * Re-write Auth-Type, but ONLY if it isn't already
260 if (auth_type == -1) auth_type = PW_AUTHTYPE_CRYPT;
262 password_pair = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0);
267 auth_type = PW_AUTHTYPE_LOCAL;
270 * The admin hasn't told us how to
271 * authenticate the user, so we reject them!
275 RDEBUG2("ERROR: No authenticate method (Auth-Type) found for the request: Rejecting the user");
281 case PW_AUTHTYPE_CRYPT:
282 RDEBUG2("WARNING: Please update your configuration, and remove 'Auth-Type = Crypt'");
283 RDEBUG2("WARNING: Use the PAP module instead.");
286 * Find the password sent by the user. It
287 * SHOULD be there, if it's not
288 * authentication fails.
290 auth_item = request->password;
291 if (auth_item == NULL) {
292 RDEBUG2("No User-Password or CHAP-Password attribute in the request");
296 if (password_pair == NULL) {
297 RDEBUG2("No Crypt-Password configured for the user");
298 rad_authlog("Login incorrect "
299 "(No Crypt-Password configured for the user)", request, 0);
303 switch (fr_crypt_check((char *)auth_item->vp_strvalue,
304 (char *)password_pair->vp_strvalue)) {
306 rad_authlog("Login incorrect "
307 "(system failed to supply an encrypted password for comparison)", request, 0);
313 case PW_AUTHTYPE_LOCAL:
314 RDEBUG2("WARNING: Please update your configuration, and remove 'Auth-Type = Local'");
315 RDEBUG2("WARNING: Use the PAP or CHAP modules instead.");
318 * Find the password sent by the user. It
319 * SHOULD be there, if it's not
320 * authentication fails.
322 auth_item = request->password;
324 auth_item = pairfind(request->packet->vps,
325 PW_CHAP_PASSWORD, 0);
327 RDEBUG2("No User-Password or CHAP-Password attribute in the request.");
328 RDEBUG2("Cannot perform authentication.");
333 * Plain text password.
335 if (password_pair == NULL) {
336 RDEBUG2("No \"known good\" password was configured for the user.");
337 RDEBUG2("As a result, we cannot authenticate the user.");
338 rad_authlog("Login incorrect "
339 "(No password configured for the user)", request, 0);
344 * Local password is just plain text.
346 if (auth_item->attribute == PW_USER_PASSWORD) {
347 if (strcmp((char *)password_pair->vp_strvalue,
348 (char *)auth_item->vp_strvalue) != 0) {
349 RDEBUG2("User-Password in the request does NOT match \"known good\" password.");
352 RDEBUG2("User-Password in the request is correct.");
355 } else if (auth_item->attribute != PW_CHAP_PASSWORD) {
356 RDEBUG2("The user did not supply a User-Password or a CHAP-Password attribute");
357 rad_authlog("Login incorrect "
358 "(no User-Password or CHAP-Password attribute)", request, 0);
362 rad_chap_encode(request->packet, my_chap,
363 auth_item->vp_octets[0], password_pair);
368 if (memcmp(my_chap + 1, auth_item->vp_strvalue + 1,
369 CHAP_VALUE_LENGTH) != 0) {
370 RDEBUG2("CHAP-Password is incorrect.");
373 RDEBUG2("CHAP-Password is correct.");
377 * See if there is a module that handles
378 * this type, and turn the RLM_ return
379 * status into the values as defined at
380 * the top of this function.
382 result = module_authenticate(auth_type, request);
385 * An authentication module FAIL
386 * return code, or any return code that
387 * is not expected from authentication,
388 * is the same as an explicit REJECT!
390 case RLM_MODULE_FAIL:
391 case RLM_MODULE_INVALID:
392 case RLM_MODULE_NOOP:
393 case RLM_MODULE_NOTFOUND:
394 case RLM_MODULE_REJECT:
395 case RLM_MODULE_UPDATED:
396 case RLM_MODULE_USERLOCK:
403 case RLM_MODULE_HANDLED:
414 * Post-authentication step processes the response before it is
415 * sent to the NAS. It can receive both Access-Accept and Access-Reject
418 int rad_postauth(REQUEST *request)
421 int postauth_type = 0;
425 * Do post-authentication calls. ignoring the return code.
427 vp = pairfind(request->config_items, PW_POST_AUTH_TYPE, 0);
429 RDEBUG2("Using Post-Auth-Type %s", vp->vp_strvalue);
430 postauth_type = vp->vp_integer;
432 result = module_post_auth(postauth_type, request);
435 * The module failed, or said to reject the user: Do so.
437 case RLM_MODULE_FAIL:
438 case RLM_MODULE_INVALID:
439 case RLM_MODULE_REJECT:
440 case RLM_MODULE_USERLOCK:
442 request->reply->code = PW_AUTHENTICATION_REJECT;
443 result = RLM_MODULE_REJECT;
446 * The module handled the request, cancel the reply.
448 case RLM_MODULE_HANDLED:
452 * The module had a number of OK return codes.
454 case RLM_MODULE_NOOP:
455 case RLM_MODULE_NOTFOUND:
457 case RLM_MODULE_UPDATED:
458 result = RLM_MODULE_OK;
465 * Process and reply to an authentication request
467 * The return value of this function isn't actually used right now, so
468 * it's not entirely clear if it is returning the right things. --Pac.
470 int rad_authenticate(REQUEST *request)
472 VALUE_PAIR *namepair;
473 #ifdef WITH_SESSION_MGMT
474 VALUE_PAIR *check_item;
476 VALUE_PAIR *auth_item = NULL;
477 VALUE_PAIR *module_msg;
478 VALUE_PAIR *tmp = NULL;
480 const char *password;
488 * If this request got proxied to another server, we need
489 * to check whether it authenticated the request or not.
491 if (request->proxy_reply) {
492 switch (request->proxy_reply->code) {
494 * Reply of ACCEPT means accept, thus set Auth-Type
497 case PW_AUTHENTICATION_ACK:
498 tmp = radius_paircreate(request,
499 &request->config_items,
500 PW_AUTH_TYPE, 0, PW_TYPE_INTEGER);
501 if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
505 * Challenges are punted back to the NAS without any
506 * further processing.
508 case PW_ACCESS_CHALLENGE:
509 request->reply->code = PW_ACCESS_CHALLENGE;
510 return RLM_MODULE_OK;
512 * ALL other replies mean reject. (this is fail-safe)
514 * Do NOT do any authorization or authentication. They
515 * are being rejected, so we minimize the amount of work
516 * done by the server, by rejecting them here.
518 case PW_AUTHENTICATION_REJECT:
519 rad_authlog("Login incorrect (Home Server says so)",
521 request->reply->code = PW_AUTHENTICATION_REJECT;
522 return RLM_MODULE_REJECT;
525 rad_authlog("Login incorrect (Home Server failed to respond)",
527 return RLM_MODULE_REJECT;
533 * Get the username from the request.
535 * Note that namepair MAY be NULL, in which case there
536 * is no User-Name attribute in the request.
538 namepair = request->username;
541 * Look for, and cache, passwords.
543 if (!request->password) {
544 request->password = pairfind(request->packet->vps,
545 PW_USER_PASSWORD, 0);
549 * Discover which password we want to use.
551 auth_item = request->password;
553 password = (const char *)auth_item->vp_strvalue;
557 * Maybe there's a CHAP-Password?
559 if ((auth_item = pairfind(request->packet->vps,
560 PW_CHAP_PASSWORD, 0)) != NULL) {
561 password = "<CHAP-PASSWORD>";
565 * No password we recognize.
567 password = "<NO-PASSWORD>";
570 request->password = auth_item;
573 * Get the user's authorization information from the database
576 result = module_authorize(autz_type, request);
578 case RLM_MODULE_NOOP:
579 case RLM_MODULE_NOTFOUND:
581 case RLM_MODULE_UPDATED:
583 case RLM_MODULE_HANDLED:
585 case RLM_MODULE_FAIL:
586 case RLM_MODULE_INVALID:
587 case RLM_MODULE_REJECT:
588 case RLM_MODULE_USERLOCK:
590 if ((module_msg = pairfind(request->packet->vps,
591 PW_MODULE_FAILURE_MESSAGE, 0)) != NULL) {
592 char msg[MAX_STRING_LEN + 16];
593 snprintf(msg, sizeof(msg), "Invalid user (%s)",
594 module_msg->vp_strvalue);
595 rad_authlog(msg,request,0);
597 rad_authlog("Invalid user", request, 0);
599 request->reply->code = PW_AUTHENTICATION_REJECT;
603 tmp = pairfind(request->config_items, PW_AUTZ_TYPE, 0);
605 RDEBUG2("Using Autz-Type %s", tmp->vp_strvalue);
606 autz_type = tmp->vp_integer;
613 * If we haven't already proxied the packet, then check
614 * to see if we should. Maybe one of the authorize
615 * modules has decided that a proxy should be used. If
616 * so, get out of here and send the packet.
620 (request->proxy == NULL) &&
622 ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0)) != NULL)) {
625 realm = realm_find2(tmp->vp_strvalue);
628 * Don't authenticate, as the request is going to
631 if (realm && realm->auth_pool) {
632 return RLM_MODULE_OK;
636 * Catch users who set Proxy-To-Realm to a LOCAL
637 * realm (sigh). But don't complain if it is
640 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
641 RDEBUG2("WARNING: You set Proxy-To-Realm = %s, but it is a LOCAL realm! Cancelling proxy request.", realm->name);
645 RDEBUG2("WARNING: You set Proxy-To-Realm = %s, but the realm does not exist! Cancelling invalid proxy request.", tmp->vp_strvalue);
654 * Perhaps there is a Stripped-User-Name now.
656 namepair = request->username;
662 result = rad_check_password(request);
665 return RLM_MODULE_HANDLED;
670 * Failed to validate the user.
672 * We PRESUME that the code which failed will clean up
673 * request->reply->vps, to be ONLY the reply items it
674 * wants to send back.
677 RDEBUG2("Failed to authenticate the user.");
678 request->reply->code = PW_AUTHENTICATION_REJECT;
680 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_FAILURE_MESSAGE, 0)) != NULL){
681 char msg[MAX_STRING_LEN+19];
683 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
684 module_msg->vp_strvalue);
685 rad_authlog(msg, request, 0);
687 rad_authlog("Login incorrect", request, 0);
690 /* double check: maybe the secret is wrong? */
691 if ((debug_flag > 1) && (auth_item != NULL) &&
692 (auth_item->attribute == PW_USER_PASSWORD)) {
695 p = (uint8_t *) auth_item->vp_strvalue;
699 size = fr_utf8_char(p);
701 log_debug(" WARNING: Unprintable characters in the password. Double-check the shared secret on the server and the NAS!");
709 #ifdef WITH_SESSION_MGMT
711 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE, 0)) != NULL) {
712 int r, session_type = 0;
714 char umsg[MAX_STRING_LEN + 1];
715 const char *user_msg = NULL;
717 tmp = pairfind(request->config_items, PW_SESSION_TYPE, 0);
719 RDEBUG2("Using Session-Type %s", tmp->vp_strvalue);
720 session_type = tmp->vp_integer;
724 * User authenticated O.K. Now we have to check
725 * for the Simultaneous-Use parameter.
728 (r = module_checksimul(session_type, request, check_item->vp_integer)) != 0) {
732 /* Multilink attempt. Check if port-limit > simultaneous-use */
733 VALUE_PAIR *port_limit;
735 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT, 0)) != NULL &&
736 port_limit->vp_integer > check_item->vp_integer){
737 RDEBUG2("MPP is OK");
742 if (check_item->vp_integer > 1) {
743 snprintf(umsg, sizeof(umsg),
744 "\r\nYou are already logged in %d times - access denied\r\n\n",
745 (int)check_item->vp_integer);
748 user_msg = "\r\nYou are already logged in - access denied\r\n\n";
751 request->reply->code = PW_AUTHENTICATION_REJECT;
754 * They're trying to log in too many times.
755 * Remove ALL reply attributes.
757 pairfree(&request->reply->vps);
758 radius_pairmake(request, &request->reply->vps,
762 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
763 check_item->vp_integer,
764 r == 2 ? "[MPP attempt]" : "");
765 rad_authlog(logstr, request, 1);
774 * Result should be >= 0 here - if not, it means the user
775 * is rejected, so we just process post-auth and return.
778 return RLM_MODULE_REJECT;
782 * Set the reply to Access-Accept, if it hasn't already
783 * been set to something. (i.e. Access-Challenge)
785 if (request->reply->code == 0)
786 request->reply->code = PW_AUTHENTICATION_ACK;
788 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_SUCCESS_MESSAGE, 0)) != NULL){
789 char msg[MAX_STRING_LEN+12];
791 snprintf(msg, sizeof(msg), "Login OK (%s)",
792 module_msg->vp_strvalue);
793 rad_authlog(msg, request, 1);
795 rad_authlog("Login OK", request, 1);
799 * Run the modules in the 'post-auth' section.
801 result = rad_postauth(request);