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) {
43 if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID)) == NULL)
45 if ((pair = pairfind(request->packet->vps, PW_NAS_PORT)) != NULL)
48 snprintf(buf, buflen, "from client %.128s port %u%s%.128s",
49 client_name_old(&request->packet->src_ipaddr), port,
50 (do_cli ? " cli " : ""), (do_cli ? (char *)cli->vp_strvalue : ""));
58 * Make sure user/pass are clean
61 static int rad_authlog(const char *msg, REQUEST *request, int goodpass) {
63 char clean_password[1024];
64 char clean_username[1024];
66 VALUE_PAIR *username = NULL;
68 if (!mainconfig.log_auth) {
73 * Get the correct username based on the configured value
75 if (log_stripped_names == 0) {
76 username = pairfind(request->packet->vps, PW_USER_NAME);
78 username = request->username;
82 * Clean up the username
84 if (username == NULL) {
85 strcpy(clean_username, "<no User-Name attribute>");
87 librad_safeprint((char *)username->vp_strvalue,
89 clean_username, sizeof(clean_username));
93 * Clean up the password
95 if (mainconfig.log_auth_badpass || mainconfig.log_auth_goodpass) {
96 if (!request->password) {
97 VALUE_PAIR *auth_type;
99 auth_type = pairfind(request->config_items,
101 if (auth_type && (auth_type->vp_strvalue[0] != '\0')) {
102 snprintf(clean_password, sizeof(clean_password),
103 "<via Auth-Type = %s>",
104 auth_type->vp_strvalue);
106 strcpy(clean_password, "<no User-Password attribute>");
108 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD)) {
109 strcpy(clean_password, "<CHAP-Password>");
111 librad_safeprint((char *)request->password->vp_strvalue,
112 request->password->length,
113 clean_password, sizeof(clean_password));
118 radlog(L_AUTH, "%s: [%s%s%s] (%s)",
121 mainconfig.log_auth_goodpass ? "/" : "",
122 mainconfig.log_auth_goodpass ? clean_password : "",
123 auth_name(buf, sizeof(buf), request, 1));
125 radlog(L_AUTH, "%s: [%s%s%s] (%s)",
128 mainconfig.log_auth_badpass ? "/" : "",
129 mainconfig.log_auth_badpass ? clean_password : "",
130 auth_name(buf, sizeof(buf), request, 1));
141 * -2 Rejected (Auth-Type = Reject, send Port-Message back)
142 * 1 End check & return, don't reply
144 * NOTE: NOT the same as the RLM_ values !
146 static int rad_check_password(REQUEST *request)
148 VALUE_PAIR *auth_type_pair;
149 VALUE_PAIR *cur_config_item;
150 VALUE_PAIR *password_pair;
151 VALUE_PAIR *auth_item;
152 char string[MAX_STRING_LEN];
155 int auth_type_count = 0;
159 * Look for matching check items. We skip the whole lot
160 * if the authentication type is PW_AUTHTYPE_ACCEPT or
161 * PW_AUTHTYPE_REJECT.
163 cur_config_item = request->config_items;
164 while(((auth_type_pair = pairfind(cur_config_item, PW_AUTH_TYPE))) != NULL) {
165 auth_type = auth_type_pair->lvalue;
168 DEBUG2(" rad_check_password: Found Auth-Type %s",
169 auth_type_pair->vp_strvalue);
170 cur_config_item = auth_type_pair->next;
172 if (auth_type == PW_AUTHTYPE_REJECT) {
173 DEBUG2(" rad_check_password: Auth-Type = Reject, rejecting user");
178 if (( auth_type_count > 1) && (debug_flag)) {
179 radlog(L_ERR, "Warning: Found %d auth-types on request for user '%s'",
180 auth_type_count, request->username->vp_strvalue);
184 * This means we have a proxy reply or an accept
185 * and it wasn't rejected in the above loop. So
186 * that means it is accepted and we do no further
189 if ((auth_type == PW_AUTHTYPE_ACCEPT) || (request->proxy)) {
190 DEBUG2(" rad_check_password: Auth-Type = Accept, accepting the user");
194 password_pair = pairfind(request->config_items, PW_USER_PASSWORD);
196 pairfind(request->config_items, PW_CLEARTEXT_PASSWORD)) {
197 pairdelete(&request->config_items, PW_USER_PASSWORD);
198 password_pair = NULL;
202 DEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
203 DEBUG("!!! Replacing User-Password in config items with Cleartext-Password. !!!");
204 DEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
205 DEBUG("!!! Please update your configuration so that the \"known good\" !!!");
206 DEBUG("!!! clear text password is in Cleartext-Password, and not in User-Password. !!!");
207 DEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
208 password_pair->attribute = PW_CLEARTEXT_PASSWORD;
209 strlcpy(password_pair->name, "Cleatext-Password",
210 sizeof(password_pair->name));
214 * Find the "known good" password.
216 * FIXME: We should get rid of these hacks, and replace
217 * them with a module.
219 if ((password_pair = pairfind(request->config_items, PW_CRYPT_PASSWORD)) != NULL) {
221 * Re-write Auth-Type, but ONLY if it isn't already
224 if (auth_type == -1) auth_type = PW_AUTHTYPE_CRYPT;
226 password_pair = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
231 auth_type = PW_AUTHTYPE_LOCAL;
234 * The admin hasn't told us how to
235 * authenticate the user, so we reject them!
239 DEBUG2("auth: No authenticate method (Auth-Type) configuration found for the request: Rejecting the user");
247 case PW_AUTHTYPE_CRYPT:
249 * Find the password sent by the user. It
250 * SHOULD be there, if it's not
251 * authentication fails.
253 auth_item = request->password;
254 if (auth_item == NULL) {
255 DEBUG2("auth: No User-Password or CHAP-Password attribute in the request");
259 DEBUG2("auth: type Crypt");
260 if (password_pair == NULL) {
261 DEBUG2("No Crypt-Password configured for the user");
262 rad_authlog("Login incorrect "
263 "(No Crypt-Password configured for the user)", request, 0);
267 switch (lrad_crypt_check((char *)auth_item->vp_strvalue,
268 (char *)password_pair->vp_strvalue)) {
270 rad_authlog("Login incorrect "
271 "(system failed to supply an encrypted password for comparison)", request, 0);
276 case PW_AUTHTYPE_LOCAL:
277 DEBUG2("auth: type Local");
280 * Find the password sent by the user. It
281 * SHOULD be there, if it's not
282 * authentication fails.
284 auth_item = request->password;
286 auth_item = pairfind(request->packet->vps,
289 DEBUG2("auth: No User-Password or CHAP-Password attribute in the request");
294 * Plain text password.
296 if (password_pair == NULL) {
297 DEBUG2("auth: No password configured for the user");
298 rad_authlog("Login incorrect "
299 "(No password configured for the user)", request, 0);
304 * Local password is just plain text.
306 if (auth_item->attribute == PW_USER_PASSWORD) {
307 if (strcmp((char *)password_pair->vp_strvalue,
308 (char *)auth_item->vp_strvalue) != 0) {
309 DEBUG2("auth: user supplied User-Password does NOT match local User-Password");
312 DEBUG2("auth: user supplied User-Password matches local User-Password");
315 } else if (auth_item->attribute != PW_CHAP_PASSWORD) {
316 DEBUG2("The user did not supply a User-Password or a CHAP-Password attribute");
317 rad_authlog("Login incorrect "
318 "(no User-Password or CHAP-Password attribute)", request, 0);
322 rad_chap_encode(request->packet, string,
323 auth_item->vp_strvalue[0], password_pair);
328 if (memcmp(string + 1, auth_item->vp_strvalue + 1,
329 CHAP_VALUE_LENGTH) != 0) {
330 DEBUG2("auth: user supplied CHAP-Password does NOT match local User-Password");
333 DEBUG2("auth: user supplied CHAP-Password matches local User-Password");
336 dval = dict_valbyattr(PW_AUTH_TYPE, auth_type);
338 DEBUG2("auth: type \"%s\"", dval->name);
340 DEBUG2("auth: type UNKNOWN-%d", auth_type);
344 * See if there is a module that handles
345 * this type, and turn the RLM_ return
346 * status into the values as defined at
347 * the top of this function.
349 result = module_authenticate(auth_type, request);
352 * An authentication module FAIL
353 * return code, or any return code that
354 * is not expected from authentication,
355 * is the same as an explicit REJECT!
357 case RLM_MODULE_FAIL:
358 case RLM_MODULE_INVALID:
359 case RLM_MODULE_NOOP:
360 case RLM_MODULE_NOTFOUND:
361 case RLM_MODULE_REJECT:
362 case RLM_MODULE_UPDATED:
363 case RLM_MODULE_USERLOCK:
370 case RLM_MODULE_HANDLED:
381 * Post-authentication step processes the response before it is
382 * sent to the NAS. It can receive both Access-Accept and Access-Reject
385 int rad_postauth(REQUEST *request)
388 int postauth_type = 0;
392 * Do post-authentication calls. ignoring the return code.
394 vp = pairfind(request->config_items, PW_POST_AUTH_TYPE);
396 DEBUG2(" Found Post-Auth-Type %s", vp->vp_strvalue);
397 postauth_type = vp->lvalue;
399 result = module_post_auth(postauth_type, request);
402 * The module failed, or said to reject the user: Do so.
404 case RLM_MODULE_FAIL:
405 case RLM_MODULE_INVALID:
406 case RLM_MODULE_REJECT:
407 case RLM_MODULE_USERLOCK:
409 request->reply->code = PW_AUTHENTICATION_REJECT;
410 result = RLM_MODULE_REJECT;
413 * The module handled the request, cancel the reply.
415 case RLM_MODULE_HANDLED:
419 * The module had a number of OK return codes.
421 case RLM_MODULE_NOOP:
422 case RLM_MODULE_NOTFOUND:
424 case RLM_MODULE_UPDATED:
425 result = RLM_MODULE_OK;
432 * Before sending an Access-Reject, call the modules in the
433 * Post-Auth-Type REJECT stanza.
435 static int rad_postauth_reject(REQUEST *request)
441 dval = dict_valbyname(PW_POST_AUTH_TYPE, "REJECT");
443 /* Overwrite the Post-Auth-Type with the value REJECT */
444 pairdelete(&request->config_items, PW_POST_AUTH_TYPE);
445 tmp = paircreate(PW_POST_AUTH_TYPE, PW_TYPE_INTEGER);
446 tmp->lvalue = dval->value;
447 pairadd(&request->config_items, tmp);
448 result = rad_postauth(request);
450 /* No REJECT stanza */
451 result = RLM_MODULE_OK;
457 * Process and reply to an authentication request
459 * The return value of this function isn't actually used right now, so
460 * it's not entirely clear if it is returning the right things. --Pac.
462 int rad_authenticate(REQUEST *request)
464 VALUE_PAIR *namepair;
465 VALUE_PAIR *check_item;
466 VALUE_PAIR *auth_item;
467 VALUE_PAIR *module_msg;
468 VALUE_PAIR *tmp = NULL;
470 char umsg[MAX_STRING_LEN + 1];
471 const char *user_msg = NULL;
472 const char *password;
480 * If this request got proxied to another server, we need
481 * to check whether it authenticated the request or not.
483 if (request->proxy_reply) {
484 switch (request->proxy_reply->code) {
486 * Reply of ACCEPT means accept, thus set Auth-Type
489 case PW_AUTHENTICATION_ACK:
490 tmp = paircreate(PW_AUTH_TYPE, PW_TYPE_INTEGER);
492 radlog(L_ERR|L_CONS, "Not enough memory");
495 tmp->lvalue = PW_AUTHTYPE_ACCEPT;
496 pairadd(&request->config_items, tmp);
499 * Challenges are punted back to the NAS without any
500 * further processing.
502 case PW_ACCESS_CHALLENGE:
503 request->reply->code = PW_ACCESS_CHALLENGE;
504 return RLM_MODULE_OK;
506 * ALL other replies mean reject. (this is fail-safe)
508 * Do NOT do any authorization or authentication. They
509 * are being rejected, so we minimize the amount of work
510 * done by the server, by rejecting them here.
512 case PW_AUTHENTICATION_REJECT:
514 rad_authlog("Login incorrect (Home Server says so)",
516 request->reply->code = PW_AUTHENTICATION_REJECT;
517 rad_postauth_reject(request);
518 return RLM_MODULE_REJECT;
523 * Get the username from the request.
525 * Note that namepair MAY be NULL, in which case there
526 * is no User-Name attribute in the request.
528 namepair = request->username;
531 * Look for, and cache, passwords.
533 if (!request->password) {
534 request->password = pairfind(request->packet->vps,
539 * Discover which password we want to use.
541 auth_item = request->password;
543 password = (const char *)auth_item->vp_strvalue;
547 * Maybe there's a CHAP-Password?
549 if ((auth_item = pairfind(request->packet->vps,
550 PW_CHAP_PASSWORD)) != NULL) {
551 password = "<CHAP-PASSWORD>";
555 * No password we recognize.
557 password = "<NO-PASSWORD>";
560 request->password = auth_item;
563 * Get the user's authorization information from the database
566 result = module_authorize(autz_type, request);
568 case RLM_MODULE_NOOP:
569 case RLM_MODULE_NOTFOUND:
571 case RLM_MODULE_UPDATED:
573 case RLM_MODULE_FAIL:
574 case RLM_MODULE_HANDLED:
576 case RLM_MODULE_INVALID:
577 case RLM_MODULE_REJECT:
578 case RLM_MODULE_USERLOCK:
580 if ((module_msg = pairfind(request->packet->vps,
581 PW_MODULE_FAILURE_MESSAGE)) != NULL) {
582 char msg[MAX_STRING_LEN + 16];
583 snprintf(msg, sizeof(msg), "Invalid user (%s)",
584 module_msg->vp_strvalue);
585 rad_authlog(msg,request,0);
587 rad_authlog("Invalid user", request, 0);
589 request->reply->code = PW_AUTHENTICATION_REJECT;
593 tmp = pairfind(request->config_items, PW_AUTZ_TYPE);
595 DEBUG2(" Found Autz-Type %s", tmp->vp_strvalue);
596 autz_type = tmp->lvalue;
603 * If we haven't already proxied the packet, then check
604 * to see if we should. Maybe one of the authorize
605 * modules has decided that a proxy should be used. If
606 * so, get out of here and send the packet.
608 if ((request->proxy == NULL) &&
609 ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM)) != NULL)) {
613 * Catch users who set Proxy-To-Realm to a LOCAL
616 realm = realm_find(tmp->vp_strvalue);
617 if (realm && !realm->auth_pool) {
618 DEBUG2(" WARNING: You set Proxy-To-Realm = %s, but it is a LOCAL realm! Cancelling invalid proxy request.", realm->name);
621 * Don't authenticate, as the request is
624 return RLM_MODULE_OK;
629 * Perhaps there is a Stripped-User-Name now.
631 namepair = request->username;
637 result = rad_check_password(request);
640 return RLM_MODULE_HANDLED;
645 * Failed to validate the user.
647 * We PRESUME that the code which failed will clean up
648 * request->reply->vps, to be ONLY the reply items it
649 * wants to send back.
652 DEBUG2("auth: Failed to validate the user.");
653 request->reply->code = PW_AUTHENTICATION_REJECT;
655 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_FAILURE_MESSAGE)) != NULL){
656 char msg[MAX_STRING_LEN+19];
658 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
659 module_msg->vp_strvalue);
660 rad_authlog(msg, request, 0);
662 rad_authlog("Login incorrect", request, 0);
665 /* double check: maybe the secret is wrong? */
666 if ((debug_flag > 1) && (auth_item != NULL) &&
667 (auth_item->attribute == PW_USER_PASSWORD)) {
670 p = auth_item->vp_strvalue;
672 if (!isprint((int) *p)) {
673 log_debug(" WARNING: Unprintable characters in the password.\n\t Double-check the shared secret on the server and the NAS!");
682 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE)) != NULL) {
683 int r, session_type = 0;
685 tmp = pairfind(request->config_items, PW_SESSION_TYPE);
687 DEBUG2(" Found Session-Type %s", tmp->vp_strvalue);
688 session_type = tmp->lvalue;
692 * User authenticated O.K. Now we have to check
693 * for the Simultaneous-Use parameter.
696 (r = module_checksimul(session_type, request, check_item->lvalue)) != 0) {
700 /* Multilink attempt. Check if port-limit > simultaneous-use */
701 VALUE_PAIR *port_limit;
703 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT)) != NULL &&
704 port_limit->lvalue > check_item->lvalue){
705 DEBUG2("main auth: MPP is OK");
710 if (check_item->lvalue > 1) {
711 snprintf(umsg, sizeof(umsg),
712 "\r\nYou are already logged in %d times - access denied\r\n\n",
713 (int)check_item->lvalue);
716 user_msg = "\r\nYou are already logged in - access denied\r\n\n";
719 request->reply->code = PW_AUTHENTICATION_REJECT;
722 * They're trying to log in too many times.
723 * Remove ALL reply attributes.
725 pairfree(&request->reply->vps);
726 tmp = pairmake("Reply-Message", user_msg, T_OP_SET);
727 request->reply->vps = tmp;
729 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
731 r == 2 ? "[MPP attempt]" : "");
732 rad_authlog(logstr, request, 1);
740 * Result should be >= 0 here - if not, it means the user
741 * is rejected, so we just process post-auth and return.
744 rad_postauth_reject(request);
745 return RLM_MODULE_REJECT;
749 * We might need this later. The 'password' string
750 * is NOT used anywhere below here, except for logging,
751 * so it should be safe...
753 if ((auth_item != NULL) && (auth_item->attribute == PW_CHAP_PASSWORD)) {
754 password = "CHAP-Password";
758 * Add the port number to the Framed-IP-Address if
759 * vp->addport is set.
761 if (((tmp = pairfind(request->reply->vps,
762 PW_FRAMED_IP_ADDRESS)) != NULL) &&
763 (tmp->flags.addport != 0)) {
764 VALUE_PAIR *vpPortId;
767 * Find the NAS port ID.
769 if ((vpPortId = pairfind(request->packet->vps,
770 PW_NAS_PORT)) != NULL) {
771 unsigned long tvalue = ntohl(tmp->lvalue);
772 tmp->lvalue = htonl(tvalue + vpPortId->lvalue);
773 tmp->flags.addport = 0;
774 ip_ntoa(tmp->vp_strvalue, tmp->lvalue);
776 DEBUG2("WARNING: No NAS-Port attribute in request. CANNOT return a Framed-IP-Address + NAS-Port.\n");
777 pairdelete(&request->reply->vps, PW_FRAMED_IP_ADDRESS);
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)) != 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);