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)
46 port = pair->vp_integer;
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 uint8_t my_chap[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->vp_integer;
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, "Cleartext-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, my_chap,
323 auth_item->vp_octets[0], password_pair);
328 if (memcmp(my_chap + 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->vp_integer;
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 * Process and reply to an authentication request
434 * The return value of this function isn't actually used right now, so
435 * it's not entirely clear if it is returning the right things. --Pac.
437 int rad_authenticate(REQUEST *request)
439 VALUE_PAIR *namepair;
440 VALUE_PAIR *check_item;
441 VALUE_PAIR *auth_item;
442 VALUE_PAIR *module_msg;
443 VALUE_PAIR *tmp = NULL;
445 char umsg[MAX_STRING_LEN + 1];
446 const char *user_msg = NULL;
447 const char *password;
455 * If this request got proxied to another server, we need
456 * to check whether it authenticated the request or not.
458 if (request->proxy_reply) {
459 switch (request->proxy_reply->code) {
461 * Reply of ACCEPT means accept, thus set Auth-Type
464 case PW_AUTHENTICATION_ACK:
465 tmp = paircreate(PW_AUTH_TYPE, PW_TYPE_INTEGER);
467 radlog(L_ERR|L_CONS, "Not enough memory");
470 tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
471 pairadd(&request->config_items, tmp);
474 * Challenges are punted back to the NAS without any
475 * further processing.
477 case PW_ACCESS_CHALLENGE:
478 request->reply->code = PW_ACCESS_CHALLENGE;
479 return RLM_MODULE_OK;
481 * ALL other replies mean reject. (this is fail-safe)
483 * Do NOT do any authorization or authentication. They
484 * are being rejected, so we minimize the amount of work
485 * done by the server, by rejecting them here.
487 case PW_AUTHENTICATION_REJECT:
489 rad_authlog("Login incorrect (Home Server says so)",
491 request->reply->code = PW_AUTHENTICATION_REJECT;
492 return RLM_MODULE_REJECT;
497 * Get the username from the request.
499 * Note that namepair MAY be NULL, in which case there
500 * is no User-Name attribute in the request.
502 namepair = request->username;
505 * Look for, and cache, passwords.
507 if (!request->password) {
508 request->password = pairfind(request->packet->vps,
513 * Discover which password we want to use.
515 auth_item = request->password;
517 password = (const char *)auth_item->vp_strvalue;
521 * Maybe there's a CHAP-Password?
523 if ((auth_item = pairfind(request->packet->vps,
524 PW_CHAP_PASSWORD)) != NULL) {
525 password = "<CHAP-PASSWORD>";
529 * No password we recognize.
531 password = "<NO-PASSWORD>";
534 request->password = auth_item;
537 * Get the user's authorization information from the database
540 result = module_authorize(autz_type, request);
542 case RLM_MODULE_NOOP:
543 case RLM_MODULE_NOTFOUND:
545 case RLM_MODULE_UPDATED:
547 case RLM_MODULE_FAIL:
548 case RLM_MODULE_HANDLED:
550 case RLM_MODULE_INVALID:
551 case RLM_MODULE_REJECT:
552 case RLM_MODULE_USERLOCK:
554 if ((module_msg = pairfind(request->packet->vps,
555 PW_MODULE_FAILURE_MESSAGE)) != NULL) {
556 char msg[MAX_STRING_LEN + 16];
557 snprintf(msg, sizeof(msg), "Invalid user (%s)",
558 module_msg->vp_strvalue);
559 rad_authlog(msg,request,0);
561 rad_authlog("Invalid user", request, 0);
563 request->reply->code = PW_AUTHENTICATION_REJECT;
567 tmp = pairfind(request->config_items, PW_AUTZ_TYPE);
569 DEBUG2(" Found Autz-Type %s", tmp->vp_strvalue);
570 autz_type = tmp->vp_integer;
577 * If we haven't already proxied the packet, then check
578 * to see if we should. Maybe one of the authorize
579 * modules has decided that a proxy should be used. If
580 * so, get out of here and send the packet.
582 if ((request->proxy == NULL) &&
583 ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM)) != NULL)) {
587 * Catch users who set Proxy-To-Realm to a LOCAL
590 realm = realm_find(tmp->vp_strvalue);
591 if (realm && !realm->auth_pool) {
592 DEBUG2(" WARNING: You set Proxy-To-Realm = %s, but it is a LOCAL realm! Cancelling invalid proxy request.", realm->name);
595 * Don't authenticate, as the request is
598 return RLM_MODULE_OK;
603 * Perhaps there is a Stripped-User-Name now.
605 namepair = request->username;
611 result = rad_check_password(request);
614 return RLM_MODULE_HANDLED;
619 * Failed to validate the user.
621 * We PRESUME that the code which failed will clean up
622 * request->reply->vps, to be ONLY the reply items it
623 * wants to send back.
626 DEBUG2("auth: Failed to validate the user.");
627 request->reply->code = PW_AUTHENTICATION_REJECT;
629 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_FAILURE_MESSAGE)) != NULL){
630 char msg[MAX_STRING_LEN+19];
632 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
633 module_msg->vp_strvalue);
634 rad_authlog(msg, request, 0);
636 rad_authlog("Login incorrect", request, 0);
639 /* double check: maybe the secret is wrong? */
640 if ((debug_flag > 1) && (auth_item != NULL) &&
641 (auth_item->attribute == PW_USER_PASSWORD)) {
644 p = auth_item->vp_strvalue;
646 if (!isprint((int) *p)) {
647 log_debug(" WARNING: Unprintable characters in the password.\n\t Double-check the shared secret on the server and the NAS!");
656 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE)) != NULL) {
657 int r, session_type = 0;
659 tmp = pairfind(request->config_items, PW_SESSION_TYPE);
661 DEBUG2(" Found Session-Type %s", tmp->vp_strvalue);
662 session_type = tmp->vp_integer;
666 * User authenticated O.K. Now we have to check
667 * for the Simultaneous-Use parameter.
670 (r = module_checksimul(session_type, request, check_item->vp_integer)) != 0) {
674 /* Multilink attempt. Check if port-limit > simultaneous-use */
675 VALUE_PAIR *port_limit;
677 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT)) != NULL &&
678 port_limit->vp_integer > check_item->vp_integer){
679 DEBUG2("main auth: MPP is OK");
684 if (check_item->vp_integer > 1) {
685 snprintf(umsg, sizeof(umsg),
686 "\r\nYou are already logged in %d times - access denied\r\n\n",
687 (int)check_item->vp_integer);
690 user_msg = "\r\nYou are already logged in - access denied\r\n\n";
693 request->reply->code = PW_AUTHENTICATION_REJECT;
696 * They're trying to log in too many times.
697 * Remove ALL reply attributes.
699 pairfree(&request->reply->vps);
700 tmp = pairmake("Reply-Message", user_msg, T_OP_SET);
701 request->reply->vps = tmp;
703 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
704 check_item->vp_integer,
705 r == 2 ? "[MPP attempt]" : "");
706 rad_authlog(logstr, request, 1);
714 * Result should be >= 0 here - if not, it means the user
715 * is rejected, so we just process post-auth and return.
718 return RLM_MODULE_REJECT;
722 * We might need this later. The 'password' string
723 * is NOT used anywhere below here, except for logging,
724 * so it should be safe...
726 if ((auth_item != NULL) && (auth_item->attribute == PW_CHAP_PASSWORD)) {
727 password = "CHAP-Password";
731 * Add the port number to the Framed-IP-Address if
732 * vp->addport is set.
734 if (((tmp = pairfind(request->reply->vps,
735 PW_FRAMED_IP_ADDRESS)) != NULL) &&
736 (tmp->flags.addport != 0)) {
737 VALUE_PAIR *vpPortId;
740 * Find the NAS port ID.
742 if ((vpPortId = pairfind(request->packet->vps,
743 PW_NAS_PORT)) != NULL) {
744 unsigned long tvalue = ntohl(tmp->vp_integer);
745 tmp->vp_integer = htonl(tvalue + vpPortId->vp_integer);
746 tmp->flags.addport = 0;
747 ip_ntoa(tmp->vp_strvalue, tmp->vp_integer);
749 DEBUG2("WARNING: No NAS-Port attribute in request. CANNOT return a Framed-IP-Address + NAS-Port.\n");
750 pairdelete(&request->reply->vps, PW_FRAMED_IP_ADDRESS);
755 * Set the reply to Access-Accept, if it hasn't already
756 * been set to something. (i.e. Access-Challenge)
758 if (request->reply->code == 0)
759 request->reply->code = PW_AUTHENTICATION_ACK;
761 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_SUCCESS_MESSAGE)) != NULL){
762 char msg[MAX_STRING_LEN+12];
764 snprintf(msg, sizeof(msg), "Login OK (%s)",
765 module_msg->vp_strvalue);
766 rad_authlog(msg, request, 1);
768 rad_authlog("Login OK", request, 1);
772 * Run the modules in the 'post-auth' section.
774 result = rad_postauth(request);