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)
44 if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID)) == NULL)
46 if ((pair = pairfind(request->packet->vps, PW_NAS_PORT)) != NULL)
47 port = pair->vp_integer;
49 snprintf(buf, buflen, "from client %.128s port %u%s%.128s%s",
50 request->client->shortname, port,
51 (do_cli ? " cli " : ""), (do_cli ? (char *)cli->vp_strvalue : ""),
52 (request->packet->dst_port == 0) ? " via TLS tunnel" : "");
60 * Make sure user/pass are clean
63 static int rad_authlog(const char *msg, REQUEST *request, int goodpass) {
65 char clean_password[1024];
66 char clean_username[1024];
68 VALUE_PAIR *username = NULL;
70 if (!request->root->log_auth) {
75 * Get the correct username based on the configured value
77 if (log_stripped_names == 0) {
78 username = pairfind(request->packet->vps, PW_USER_NAME);
80 username = request->username;
84 * Clean up the username
86 if (username == NULL) {
87 strcpy(clean_username, "<no User-Name attribute>");
89 librad_safeprint((char *)username->vp_strvalue,
91 clean_username, sizeof(clean_username));
95 * Clean up the password
97 if (request->root->log_auth_badpass || request->root->log_auth_goodpass) {
98 if (!request->password) {
99 VALUE_PAIR *auth_type;
101 auth_type = pairfind(request->config_items,
103 if (auth_type && (auth_type->vp_strvalue[0] != '\0')) {
104 snprintf(clean_password, sizeof(clean_password),
105 "<via Auth-Type = %s>",
106 auth_type->vp_strvalue);
108 strcpy(clean_password, "<no User-Password attribute>");
110 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD)) {
111 strcpy(clean_password, "<CHAP-Password>");
113 librad_safeprint((char *)request->password->vp_strvalue,
114 request->password->length,
115 clean_password, sizeof(clean_password));
120 radlog(L_AUTH, "%s: [%s%s%s] (%s)",
123 request->root->log_auth_goodpass ? "/" : "",
124 request->root->log_auth_goodpass ? clean_password : "",
125 auth_name(buf, sizeof(buf), request, 1));
127 radlog(L_AUTH, "%s: [%s%s%s] (%s)",
130 request->root->log_auth_badpass ? "/" : "",
131 request->root->log_auth_badpass ? clean_password : "",
132 auth_name(buf, sizeof(buf), request, 1));
143 * -2 Rejected (Auth-Type = Reject, send Port-Message back)
144 * 1 End check & return, don't reply
146 * NOTE: NOT the same as the RLM_ values !
148 static int rad_check_password(REQUEST *request)
150 VALUE_PAIR *auth_type_pair;
151 VALUE_PAIR *cur_config_item;
152 VALUE_PAIR *password_pair;
153 VALUE_PAIR *auth_item;
154 uint8_t my_chap[MAX_STRING_LEN];
157 int auth_type_count = 0;
161 * Look for matching check items. We skip the whole lot
162 * if the authentication type is PW_AUTHTYPE_ACCEPT or
163 * PW_AUTHTYPE_REJECT.
165 cur_config_item = request->config_items;
166 while(((auth_type_pair = pairfind(cur_config_item, PW_AUTH_TYPE))) != NULL) {
167 auth_type = auth_type_pair->vp_integer;
170 DEBUG2(" rad_check_password: Found Auth-Type %s",
171 auth_type_pair->vp_strvalue);
172 cur_config_item = auth_type_pair->next;
174 if (auth_type == PW_AUTHTYPE_REJECT) {
175 DEBUG2(" rad_check_password: Auth-Type = Reject, rejecting user");
180 if (( auth_type_count > 1) && (debug_flag)) {
181 radlog(L_ERR, "Warning: Found %d auth-types on request for user '%s'",
182 auth_type_count, request->username->vp_strvalue);
186 * This means we have a proxy reply or an accept
187 * and it wasn't rejected in the above loop. So
188 * that means it is accepted and we do no further
191 if ((auth_type == PW_AUTHTYPE_ACCEPT)
196 DEBUG2(" rad_check_password: Auth-Type = Accept, accepting the user");
200 password_pair = pairfind(request->config_items, PW_USER_PASSWORD);
202 pairfind(request->config_items, PW_CLEARTEXT_PASSWORD)) {
203 pairdelete(&request->config_items, PW_USER_PASSWORD);
204 password_pair = NULL;
210 DEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
211 DEBUG("!!! Replacing User-Password in config items with Cleartext-Password. !!!");
212 DEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
213 DEBUG("!!! Please update your configuration so that the \"known good\" !!!");
214 DEBUG("!!! clear text password is in Cleartext-Password, and not in User-Password. !!!");
215 DEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
216 password_pair->attribute = PW_CLEARTEXT_PASSWORD;
217 da = dict_attrbyvalue(PW_CLEARTEXT_PASSWORD);
219 radlog(L_ERR, "FATAL: You broke the dictionaries. Please use the default dictionaries!");
223 password_pair->name = da->name;
227 * Find the "known good" password.
229 * FIXME: We should get rid of these hacks, and replace
230 * them with a module.
232 if ((password_pair = pairfind(request->config_items, PW_CRYPT_PASSWORD)) != NULL) {
234 * Re-write Auth-Type, but ONLY if it isn't already
237 if (auth_type == -1) auth_type = PW_AUTHTYPE_CRYPT;
239 password_pair = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
244 auth_type = PW_AUTHTYPE_LOCAL;
247 * The admin hasn't told us how to
248 * authenticate the user, so we reject them!
252 DEBUG2("auth: No authenticate method (Auth-Type) configuration found for the request: Rejecting the user");
260 case PW_AUTHTYPE_CRYPT:
262 * Find the password sent by the user. It
263 * SHOULD be there, if it's not
264 * authentication fails.
266 auth_item = request->password;
267 if (auth_item == NULL) {
268 DEBUG2("auth: No User-Password or CHAP-Password attribute in the request");
272 DEBUG2("auth: type Crypt");
273 if (password_pair == NULL) {
274 DEBUG2("No Crypt-Password configured for the user");
275 rad_authlog("Login incorrect "
276 "(No Crypt-Password configured for the user)", request, 0);
280 switch (fr_crypt_check((char *)auth_item->vp_strvalue,
281 (char *)password_pair->vp_strvalue)) {
283 rad_authlog("Login incorrect "
284 "(system failed to supply an encrypted password for comparison)", request, 0);
289 case PW_AUTHTYPE_LOCAL:
290 DEBUG2("auth: type Local");
293 * Find the password sent by the user. It
294 * SHOULD be there, if it's not
295 * authentication fails.
297 auth_item = request->password;
299 auth_item = pairfind(request->packet->vps,
302 DEBUG2("auth: No User-Password or CHAP-Password attribute in the request");
307 * Plain text password.
309 if (password_pair == NULL) {
310 DEBUG2("auth: No password configured for the user");
311 rad_authlog("Login incorrect "
312 "(No password configured for the user)", request, 0);
317 * Local password is just plain text.
319 if (auth_item->attribute == PW_USER_PASSWORD) {
320 if (strcmp((char *)password_pair->vp_strvalue,
321 (char *)auth_item->vp_strvalue) != 0) {
322 DEBUG2("auth: user supplied User-Password does NOT match local User-Password");
325 DEBUG2("auth: user supplied User-Password matches local User-Password");
328 } else if (auth_item->attribute != PW_CHAP_PASSWORD) {
329 DEBUG2("The user did not supply a User-Password or a CHAP-Password attribute");
330 rad_authlog("Login incorrect "
331 "(no User-Password or CHAP-Password attribute)", request, 0);
335 rad_chap_encode(request->packet, my_chap,
336 auth_item->vp_octets[0], password_pair);
341 if (memcmp(my_chap + 1, auth_item->vp_strvalue + 1,
342 CHAP_VALUE_LENGTH) != 0) {
343 DEBUG2("auth: user supplied CHAP-Password does NOT match local User-Password");
346 DEBUG2("auth: user supplied CHAP-Password matches local User-Password");
349 dval = dict_valbyattr(PW_AUTH_TYPE, auth_type);
351 DEBUG2("auth: type \"%s\"", dval->name);
353 DEBUG2("auth: type UNKNOWN-%d", auth_type);
357 * See if there is a module that handles
358 * this type, and turn the RLM_ return
359 * status into the values as defined at
360 * the top of this function.
362 result = module_authenticate(auth_type, request);
365 * An authentication module FAIL
366 * return code, or any return code that
367 * is not expected from authentication,
368 * is the same as an explicit REJECT!
370 case RLM_MODULE_FAIL:
371 case RLM_MODULE_INVALID:
372 case RLM_MODULE_NOOP:
373 case RLM_MODULE_NOTFOUND:
374 case RLM_MODULE_REJECT:
375 case RLM_MODULE_UPDATED:
376 case RLM_MODULE_USERLOCK:
383 case RLM_MODULE_HANDLED:
394 * Post-authentication step processes the response before it is
395 * sent to the NAS. It can receive both Access-Accept and Access-Reject
398 int rad_postauth(REQUEST *request)
401 int postauth_type = 0;
405 * Do post-authentication calls. ignoring the return code.
407 vp = pairfind(request->config_items, PW_POST_AUTH_TYPE);
409 DEBUG2(" Found Post-Auth-Type %s", vp->vp_strvalue);
410 postauth_type = vp->vp_integer;
412 result = module_post_auth(postauth_type, request);
415 * The module failed, or said to reject the user: Do so.
417 case RLM_MODULE_FAIL:
418 case RLM_MODULE_INVALID:
419 case RLM_MODULE_REJECT:
420 case RLM_MODULE_USERLOCK:
422 request->reply->code = PW_AUTHENTICATION_REJECT;
423 result = RLM_MODULE_REJECT;
426 * The module handled the request, cancel the reply.
428 case RLM_MODULE_HANDLED:
432 * The module had a number of OK return codes.
434 case RLM_MODULE_NOOP:
435 case RLM_MODULE_NOTFOUND:
437 case RLM_MODULE_UPDATED:
438 result = RLM_MODULE_OK;
445 * Process and reply to an authentication request
447 * The return value of this function isn't actually used right now, so
448 * it's not entirely clear if it is returning the right things. --Pac.
450 int rad_authenticate(REQUEST *request)
452 VALUE_PAIR *namepair;
453 #ifdef WITH_SESSION_MGMT
454 VALUE_PAIR *check_item;
456 VALUE_PAIR *auth_item = NULL;
457 VALUE_PAIR *module_msg;
458 VALUE_PAIR *tmp = NULL;
460 const char *password;
468 * If this request got proxied to another server, we need
469 * to check whether it authenticated the request or not.
471 if (request->proxy_reply) {
472 switch (request->proxy_reply->code) {
474 * Reply of ACCEPT means accept, thus set Auth-Type
477 case PW_AUTHENTICATION_ACK:
478 tmp = radius_paircreate(request,
479 &request->config_items,
480 PW_AUTH_TYPE, PW_TYPE_INTEGER);
481 if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
485 * Challenges are punted back to the NAS without any
486 * further processing.
488 case PW_ACCESS_CHALLENGE:
489 request->reply->code = PW_ACCESS_CHALLENGE;
490 return RLM_MODULE_OK;
492 * ALL other replies mean reject. (this is fail-safe)
494 * Do NOT do any authorization or authentication. They
495 * are being rejected, so we minimize the amount of work
496 * done by the server, by rejecting them here.
498 case PW_AUTHENTICATION_REJECT:
500 rad_authlog("Login incorrect (Home Server says so)",
502 request->reply->code = PW_AUTHENTICATION_REJECT;
503 return RLM_MODULE_REJECT;
509 * Get the username from the request.
511 * Note that namepair MAY be NULL, in which case there
512 * is no User-Name attribute in the request.
514 namepair = request->username;
517 * Look for, and cache, passwords.
519 if (!request->password) {
520 request->password = pairfind(request->packet->vps,
525 * Discover which password we want to use.
527 auth_item = request->password;
529 password = (const char *)auth_item->vp_strvalue;
533 * Maybe there's a CHAP-Password?
535 if ((auth_item = pairfind(request->packet->vps,
536 PW_CHAP_PASSWORD)) != NULL) {
537 password = "<CHAP-PASSWORD>";
541 * No password we recognize.
543 password = "<NO-PASSWORD>";
546 request->password = auth_item;
549 * Get the user's authorization information from the database
552 result = module_authorize(autz_type, request);
554 case RLM_MODULE_NOOP:
555 case RLM_MODULE_NOTFOUND:
557 case RLM_MODULE_UPDATED:
559 case RLM_MODULE_HANDLED:
561 case RLM_MODULE_FAIL:
562 case RLM_MODULE_INVALID:
563 case RLM_MODULE_REJECT:
564 case RLM_MODULE_USERLOCK:
566 if ((module_msg = pairfind(request->packet->vps,
567 PW_MODULE_FAILURE_MESSAGE)) != NULL) {
568 char msg[MAX_STRING_LEN + 16];
569 snprintf(msg, sizeof(msg), "Invalid user (%s)",
570 module_msg->vp_strvalue);
571 rad_authlog(msg,request,0);
573 rad_authlog("Invalid user", request, 0);
575 request->reply->code = PW_AUTHENTICATION_REJECT;
579 tmp = pairfind(request->config_items, PW_AUTZ_TYPE);
581 DEBUG2(" Found Autz-Type %s", tmp->vp_strvalue);
582 autz_type = tmp->vp_integer;
589 * If we haven't already proxied the packet, then check
590 * to see if we should. Maybe one of the authorize
591 * modules has decided that a proxy should be used. If
592 * so, get out of here and send the packet.
596 (request->proxy == NULL) &&
598 ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM)) != NULL)) {
601 realm = realm_find2(tmp->vp_strvalue);
604 * Don't authenticate, as the request is going to
607 if (realm && realm->auth_pool) {
608 return RLM_MODULE_OK;
612 * Catch users who set Proxy-To-Realm to a LOCAL
613 * realm (sigh). But don't complain if it is
616 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
617 DEBUG2(" WARNING: You set Proxy-To-Realm = %s, but it is a LOCAL realm! Cancelling invalid proxy request.", realm->name);
621 DEBUG2(" WARNING: You set Proxy-To-Realm = %s, but the realm does not exist! Cancelling invalid proxy request.", tmp->vp_strvalue);
630 * Perhaps there is a Stripped-User-Name now.
632 namepair = request->username;
638 result = rad_check_password(request);
641 return RLM_MODULE_HANDLED;
646 * Failed to validate the user.
648 * We PRESUME that the code which failed will clean up
649 * request->reply->vps, to be ONLY the reply items it
650 * wants to send back.
653 DEBUG2("auth: Failed to validate the user.");
654 request->reply->code = PW_AUTHENTICATION_REJECT;
656 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_FAILURE_MESSAGE)) != NULL){
657 char msg[MAX_STRING_LEN+19];
659 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
660 module_msg->vp_strvalue);
661 rad_authlog(msg, request, 0);
663 rad_authlog("Login incorrect", request, 0);
666 /* double check: maybe the secret is wrong? */
667 if ((debug_flag > 1) && (auth_item != NULL) &&
668 (auth_item->attribute == PW_USER_PASSWORD)) {
671 p = auth_item->vp_strvalue;
673 if (!isprint((int) *p)) {
674 log_debug(" WARNING: Unprintable characters in the password.\n\t Double-check the shared secret on the server and the NAS!");
682 #ifdef WITH_SESSION_MGMT
684 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE)) != NULL) {
685 int r, session_type = 0;
687 char umsg[MAX_STRING_LEN + 1];
688 const char *user_msg = NULL;
690 tmp = pairfind(request->config_items, PW_SESSION_TYPE);
692 DEBUG2(" Found Session-Type %s", tmp->vp_strvalue);
693 session_type = tmp->vp_integer;
697 * User authenticated O.K. Now we have to check
698 * for the Simultaneous-Use parameter.
701 (r = module_checksimul(session_type, request, check_item->vp_integer)) != 0) {
705 /* Multilink attempt. Check if port-limit > simultaneous-use */
706 VALUE_PAIR *port_limit;
708 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT)) != NULL &&
709 port_limit->vp_integer > check_item->vp_integer){
710 DEBUG2("main auth: MPP is OK");
715 if (check_item->vp_integer > 1) {
716 snprintf(umsg, sizeof(umsg),
717 "\r\nYou are already logged in %d times - access denied\r\n\n",
718 (int)check_item->vp_integer);
721 user_msg = "\r\nYou are already logged in - access denied\r\n\n";
724 request->reply->code = PW_AUTHENTICATION_REJECT;
727 * They're trying to log in too many times.
728 * Remove ALL reply attributes.
730 pairfree(&request->reply->vps);
731 radius_pairmake(request, &request->reply->vps,
735 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
736 check_item->vp_integer,
737 r == 2 ? "[MPP attempt]" : "");
738 rad_authlog(logstr, request, 1);
747 * Result should be >= 0 here - if not, it means the user
748 * is rejected, so we just process post-auth and return.
751 return RLM_MODULE_REJECT;
755 * Add the port number to the Framed-IP-Address if
756 * vp->addport is set.
758 if (((tmp = pairfind(request->reply->vps,
759 PW_FRAMED_IP_ADDRESS)) != NULL) &&
760 (tmp->flags.addport != 0)) {
761 VALUE_PAIR *vpPortId;
764 * Find the NAS port ID.
766 if ((vpPortId = pairfind(request->packet->vps,
767 PW_NAS_PORT)) != NULL) {
768 unsigned long tvalue = ntohl(tmp->vp_integer);
769 tmp->vp_integer = htonl(tvalue + vpPortId->vp_integer);
770 tmp->flags.addport = 0;
771 ip_ntoa(tmp->vp_strvalue, tmp->vp_integer);
773 DEBUG2("WARNING: No NAS-Port attribute in request. CANNOT return a Framed-IP-Address + NAS-Port.\n");
774 pairdelete(&request->reply->vps, PW_FRAMED_IP_ADDRESS);
779 * Set the reply to Access-Accept, if it hasn't already
780 * been set to something. (i.e. Access-Challenge)
782 if (request->reply->code == 0)
783 request->reply->code = PW_AUTHENTICATION_ACK;
785 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_SUCCESS_MESSAGE)) != NULL){
786 char msg[MAX_STRING_LEN+12];
788 snprintf(msg, sizeof(msg), "Login OK (%s)",
789 module_msg->vp_strvalue);
790 rad_authlog(msg, request, 1);
792 rad_authlog("Login OK", request, 1);
796 * Run the modules in the 'post-auth' section.
798 result = rad_postauth(request);