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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Jeff Carneal <jeff@apex.net>
25 static const char rcsid[] = "$Id$";
28 #include "libradius.h"
39 # include <sys/security.h>
44 # include <netinet/in.h>
51 * Return a short string showing the terminal server, port
52 * and calling station ID.
54 char *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli) {
59 if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID)) == NULL)
61 if ((pair = pairfind(request->packet->vps, PW_NAS_PORT_ID)) != NULL)
64 snprintf(buf, buflen, "from nas %.128s port %d%s%.128s",
65 nas_name2(request->packet), port,
66 (do_cli ? " cli " : ""), (do_cli ? (char *)cli->strvalue : ""));
73 * Check if account has expired, and if user may login now.
75 static int check_expiration(REQUEST *request)
78 VALUE_PAIR *check_item = request->config_items;
81 while (result == 0 && check_item != NULL) {
84 * Check expiration date if we are doing password aging.
86 if (check_item->attribute == PW_EXPIRATION) {
88 * Has this user's password expired?
90 * If so, remove ALL reply attributes,
91 * and add our own Reply-Message, saying
92 * why they're being rejected.
94 if (check_item->lvalue < (unsigned) time(NULL)) {
98 vp = pairmake("Reply-Message",
99 "Password Has Expired\r\n",
101 pairfree(&request->reply->vps);
102 request->reply->vps = vp;
106 check_item = check_item->next;
113 * Make sure user/pass are clean
116 static int rad_authlog(const char *msg, REQUEST *request, int goodpass) {
118 char clean_password[1024];
119 char clean_username[1024];
122 if (!mainconfig.log_auth)
126 * Clean up the username
128 if (request->username == NULL) {
129 DEBUG2("rad_authlog: no username found");
133 if (request->username->strvalue) {
134 librad_safeprint((char *)request->username->strvalue,
135 request->username->length,
136 clean_username, sizeof(clean_username));
138 strcpy(clean_username, "<No Username>");
142 * Clean up the password
144 if (mainconfig.log_auth_badpass || mainconfig.log_auth_goodpass) {
145 if (!request->password) {
146 DEBUG2("rad_authlog: no password found");
150 if (request->password->attribute == PW_CHAP_PASSWORD) {
151 strcpy(clean_password, "<CHAP-Password>");
153 if (request->username->strvalue) {
154 librad_safeprint((char *)request->password->strvalue,
155 request->password->length,
156 clean_password, sizeof(clean_password));
158 strcpy(clean_password, "<No Password>");
164 radlog(L_AUTH, "%s: [%s%s%s] (%s)",
167 mainconfig.log_auth_goodpass ? "/" : "",
168 mainconfig.log_auth_goodpass ? clean_password : "",
169 auth_name(buf, sizeof(buf), request, 1));
171 radlog(L_AUTH, "%s: [%s%s%s] (%s)",
174 mainconfig.log_auth_badpass ? "/" : "",
175 mainconfig.log_auth_badpass ? clean_password : "",
176 auth_name(buf, sizeof(buf), request, 1));
187 * -2 Rejected (Auth-Type = Reject, send Port-Message back)
188 * 1 End check & return, don't reply
190 * NOTE: NOT the same as the RLM_ values !
192 int rad_check_password(REQUEST *request)
194 VALUE_PAIR *auth_type_pair;
195 VALUE_PAIR *cur_config_item;
196 VALUE_PAIR *password_pair;
197 VALUE_PAIR *auth_item;
198 char string[MAX_STRING_LEN];
201 int auth_type_count = 0;
205 * Look for matching check items. We skip the whole lot
206 * if the authentication type is PW_AUTHTYPE_ACCEPT or
207 * PW_AUTHTYPE_REJECT.
209 cur_config_item = request->config_items;
210 while(((auth_type_pair = pairfind(cur_config_item, PW_AUTHTYPE))) != NULL) {
211 auth_type = auth_type_pair->lvalue;
214 DEBUG2(" rad_check_password: Found auth-type %s",
215 auth_type_pair->strvalue);
216 cur_config_item = auth_type_pair->next;
218 if (auth_type == PW_AUTHTYPE_REJECT) {
219 DEBUG2(" rad_check_password: Auth-Type = Reject, rejecting user");
224 if((auth_type_count>1) && (debug_flag)) {
225 radlog(L_ERR, "Warning: Found %d auth-types on request for user '%s'",
226 auth_type_count, request->username->strvalue);
230 * This means we have a proxy reply or an accept
231 * and it wasn't rejected in the above loop. So
232 * that means it is accepted and we do no further
235 if ((auth_type == PW_AUTHTYPE_ACCEPT) || (request->proxy)) {
236 DEBUG2(" rad_check_password: Auth-Type = Accept, accepting the user");
241 * Find the password sent by the user. It SHOULD be there,
242 * if it's not authentication fails.
244 auth_item = request->password;
245 if (auth_item == NULL) {
246 DEBUG2(" auth: No password in the request");
251 * Find the password from the users file.
253 if ((password_pair = pairfind(request->config_items, PW_CRYPT_PASSWORD)) != NULL)
254 auth_type = PW_AUTHTYPE_CRYPT;
256 password_pair = pairfind(request->config_items, PW_PASSWORD);
259 * For backward compatibility, we check the
260 * password to see if it is the magic value
261 * UNIX if auth_type was not set.
265 !strcmp((char *)password_pair->strvalue, "UNIX"))
266 auth_type = PW_AUTHTYPE_SYSTEM;
267 else if(password_pair &&
268 !strcmp((char *)password_pair->strvalue,"PAM"))
269 auth_type = PW_AUTHTYPE_PAM;
271 auth_type = PW_AUTHTYPE_LOCAL;
275 case PW_AUTHTYPE_CRYPT:
276 DEBUG2(" auth: Crypt");
277 if (password_pair == NULL) {
278 result = auth_item->strvalue ? -1 : 0;
281 if (strcmp((char *)password_pair->strvalue,
282 crypt((char *)auth_item->strvalue,
283 (char *)password_pair->strvalue)) != 0)
286 case PW_AUTHTYPE_LOCAL:
287 DEBUG2(" auth: Local");
289 * Local password is just plain text.
291 if (auth_item->attribute != PW_CHAP_PASSWORD) {
294 * Plain text password.
296 if (password_pair == NULL ||
297 strcmp((char *)password_pair->strvalue,
298 (char *)auth_item->strvalue)!=0)
304 * CHAP - calculate MD5 sum over CHAP-ID,
305 * plain-text password and the Chap-Challenge.
306 * Compare to Chap-Response (strvalue + 1).
308 if (password_pair == NULL) {
312 rad_chap_encode(request->packet, string,
313 auth_item->strvalue[0], password_pair);
318 if (memcmp(string + 1, auth_item->strvalue + 1,
319 CHAP_VALUE_LENGTH) != 0)
324 dict_valbyattr(PW_AUTHTYPE, auth_type)->name);
326 * See if there is a module that handles
327 * this type, and turn the RLM_ return
328 * status into the values as defined at
329 * the top of this function.
331 result = module_authenticate(auth_type, request);
334 * An authentication module FAIL
335 * return code, or any return code that
336 * is not expected from authentication,
337 * is the same as an explicit REJECT!
339 case RLM_MODULE_FAIL:
340 case RLM_MODULE_REJECT:
341 case RLM_MODULE_USERLOCK:
342 case RLM_MODULE_INVALID:
343 case RLM_MODULE_NOTFOUND:
344 case RLM_MODULE_NOOP:
345 case RLM_MODULE_UPDATED:
351 case RLM_MODULE_HANDLED:
362 * Process and reply to an authentication request
364 * The return value of this function isn't actually used right now, so
365 * it's not entirely clear if it is returning the right things. --Pac.
367 int rad_authenticate(REQUEST *request)
369 VALUE_PAIR *namepair;
370 VALUE_PAIR *check_item;
371 VALUE_PAIR *reply_item;
372 VALUE_PAIR *auth_item;
375 char umsg[MAX_STRING_LEN + 1];
376 const char *user_msg = NULL;
377 const char *password;
380 int seen_callback_id;
382 char buf[1024], logstr[1024];
387 * Free any pre-existing configuration items.
389 * This should ONLY be happening for proxy replies.
391 if ((request->proxy_reply) && (request->config_items)) {
392 pairfree(&request->config_items);
396 * If this request got proxied to another server,
397 * AND it was an authentication request, then we need
398 * to add an initial Auth-Type: Auth-Accept for success,
399 * Auth-Reject for fail. We also need to add the reply
400 * pairs from the server to the initial reply.
402 if ((request->proxy_reply) &&
403 (request->packet->code == PW_AUTHENTICATION_REQUEST)) {
404 tmp = paircreate(PW_AUTHTYPE, PW_TYPE_INTEGER);
406 radlog(L_ERR|L_CONS, "no memory");
411 * Reply of ACCEPT means accept, ALL other
412 * replies mean reject. This is fail-safe.
414 if (request->proxy_reply->code == PW_AUTHENTICATION_ACK)
415 tmp->lvalue = PW_AUTHTYPE_ACCEPT;
417 tmp->lvalue = PW_AUTHTYPE_REJECT;
418 pairadd(&request->config_items, tmp);
421 * Initialize our reply to the user, by taking
422 * the reply attributes from the proxy.
424 if (request->proxy_reply->vps) {
425 request->reply->vps = request->proxy_reply->vps;
426 request->proxy_reply->vps = NULL;
430 * If it's an Access-Reject, then do NOT do any
431 * authorization or authentication. They're being
432 * rejected, so we minimize the amount of work
433 * done by the server, by rejecting them here.
435 if (request->proxy_reply->code != PW_AUTHENTICATION_ACK) {
436 request->reply->code = PW_AUTHENTICATION_REJECT;
437 return RLM_MODULE_REJECT;
442 * Get the username from the request.
444 * Note that namepair MAY be NULL, in which case there
445 * is no User-Name attribute in the request.
447 namepair = request->username;
450 * Discover which password we want to use.
452 if ((auth_item = rad_getpass(request)) != NULL) {
453 password = (const char *)auth_item->strvalue;
457 * Maybe there's a CHAP-Password?
459 if (auth_item == NULL) {
460 if ((auth_item = pairfind(request->packet->vps,
461 PW_CHAP_PASSWORD)) != NULL) {
462 password = "<CHAP-PASSWORD>";
466 * No password we recognize.
468 password = "<NO-PASSWORD>";
471 request->password = auth_item;
474 * Get the user's authorization information from the database
476 r = module_authorize(request);
477 if (r != RLM_MODULE_NOTFOUND &&
478 r != RLM_MODULE_NOOP &&
479 r != RLM_MODULE_OK &&
480 r != RLM_MODULE_UPDATED) {
481 if (r != RLM_MODULE_FAIL && r != RLM_MODULE_HANDLED) {
482 rad_authlog("Invalid user", request, 0);
483 request->reply->code = PW_AUTHENTICATION_REJECT;
485 pairfree(&request->reply->vps);
486 request->reply->vps = NULL;
491 * If we haven't already proxied the packet, then check
492 * to see if we should. Maybe one of the authorize
493 * modules has decided that a proxy should be used. If
494 * so, get out of here and send the packet.
496 if ((request->proxy == NULL) &&
497 (pairfind(request->config_items, PW_PROXY_TO_REALM) != NULL)) {
498 return RLM_MODULE_OK;
502 * Perhaps there is a Stripped-User-Name now.
504 namepair = request->username;
510 if ((result = check_expiration(request)) < 0)
512 result = rad_check_password(request);
515 return RLM_MODULE_HANDLED;
520 * Failed to validate the user.
522 * We PRESUME that the code which failed will clean up
523 * request->reply->vps, to be ONLY the reply items it
524 * wants to send back.
528 DEBUG2(" auth: Failed to validate the user.");
529 request->reply->code = PW_AUTHENTICATION_REJECT;
531 rad_authlog("Login incorrect", request, 0);
533 /* double check: maybe the secret is wrong? */
534 if ((debug_flag > 1) &&
535 (auth_item->attribute == PW_PASSWORD)) {
538 p = auth_item->strvalue;
541 log_debug(" WARNING: Unprintable characters in the password.\n\t Double-check the shared secret on the server and the NAS!");
550 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE)) != NULL) {
552 * User authenticated O.K. Now we have to check
553 * for the Simultaneous-Use parameter.
556 (r = module_checksimul(request, check_item->lvalue)) != 0) {
558 if (check_item->lvalue > 1) {
560 "\r\nYou are already logged in %d times - access denied\r\n\n",
561 (int)check_item->lvalue);
564 user_msg = "\r\nYou are already logged in - access denied\r\n\n";
567 request->reply->code = PW_AUTHENTICATION_REJECT;
570 * They're trying to log in too many times.
571 * Remove ALL reply attributes.
573 pairfree(&request->reply->vps);
574 tmp = pairmake("Reply-Message", user_msg, T_OP_SET);
575 request->reply->vps = tmp;
577 strcpy(logstr, "Multiple logins");
578 sprintf(logstr, "%s (max %d) %s", logstr, check_item->lvalue,
579 r == 2 ? "[MPP attempt]" : "");
580 rad_authlog(logstr, request, 1);
587 (check_item = pairfind(request->config_items, PW_LOGIN_TIME)) != NULL) {
590 * Authentication is OK. Now see if this
591 * user may login at this time of the day.
593 r = timestr_match((char *)check_item->strvalue,
596 * Session-Timeout needs to be at least
597 * 60 seconds, some terminal servers
598 * ignore smaller values.
602 * User called outside allowed time interval.
605 user_msg = "You are calling outside your allowed timespan\r\n";
607 request->reply->code = PW_AUTHENTICATION_REJECT;
608 pairfree(&request->reply->vps);
610 tmp = pairmake("Reply-Message", user_msg, T_OP_SET);
611 request->reply->vps = tmp;
613 strcpy(logstr, "Outside allowed timespan");
614 sprintf(logstr, "%s (time allowed %s)", logstr,
615 check_item->strvalue);
616 rad_authlog(logstr, request, 1);
621 * User is allowed, but set Session-Timeout.
623 if ((reply_item = pairfind(request->reply->vps,
624 PW_SESSION_TIMEOUT)) != NULL) {
625 if (reply_item->lvalue > (unsigned) r)
626 reply_item->lvalue = r;
628 if ((reply_item = paircreate(
630 PW_TYPE_INTEGER)) == NULL) {
631 radlog(L_ERR|L_CONS, "no memory");
634 reply_item->lvalue = r;
635 pairadd(&request->reply->vps, reply_item);
641 * Result should be >= 0 here - if not, we return.
644 return RLM_MODULE_OK;
648 * We might need this later. The 'password' string
649 * is NOT used anywhere below here, except for logging,
650 * so it should be safe...
652 if (auth_item->attribute == PW_CHAP_PASSWORD) {
653 password = "CHAP-Password";
657 * Add the port number to the Framed-IP-Address if
658 * vp->addport is set, or if the Add-Port-To-IP-Address
661 * FIXME: This doesn't work because PW_ADD_PORT_TO_IP_ADDRESS
662 * is never added to the request pairs!
664 if ((tmp = pairfind(request->reply->vps,
665 PW_FRAMED_IP_ADDRESS)) != NULL) {
669 * Find the NAS port ID.
671 if ((tmp = pairfind(request->packet->vps,
672 PW_NAS_PORT_ID)) != NULL)
673 nas_port = tmp->lvalue;
675 if((tmp2 = pairfind(request->reply->vps,
676 PW_ADD_PORT_TO_IP_ADDRESS)) != NULL) {
677 if (tmp->addport || (tmp2 && tmp2->lvalue)) {
678 tmp->lvalue = htonl(ntohl(tmp->lvalue) + nas_port);
681 pairdelete(&request->reply->vps,
682 PW_ADD_PORT_TO_IP_ADDRESS);
687 * See if we need to execute a program.
688 * FIXME: somehow cache this info, and only execute the
689 * program when we receive an Accounting-START packet.
690 * Only at that time we know dynamic IP etc.
694 if ((auth_item = pairfind(request->reply->vps, PW_EXEC_PROGRAM)) != NULL) {
696 exec_program = strdup((char *)auth_item->strvalue);
697 pairdelete(&request->reply->vps, PW_EXEC_PROGRAM);
699 if ((auth_item = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT)) != NULL) {
701 exec_program = strdup((char *)auth_item->strvalue);
702 pairdelete(&request->reply->vps, PW_EXEC_PROGRAM_WAIT);
706 * Hack - allow % expansion in certain value strings.
707 * This is nice for certain Exec-Program programs.
709 seen_callback_id = 0;
710 if ((auth_item = pairfind(request->reply->vps, PW_CALLBACK_ID)) != NULL) {
711 seen_callback_id = 1;
712 radius_xlat2(buf, sizeof(auth_item->strvalue),
713 (char *)auth_item->strvalue, request);
714 strNcpy((char *)auth_item->strvalue, buf,
715 sizeof(auth_item->strvalue));
716 auth_item->length = strlen((char *)auth_item->strvalue);
721 * If we want to exec a program, but wait for it,
722 * do it first before sending the reply.
724 if (exec_program && exec_wait) {
725 if (radius_exec_program(exec_program, request,
726 exec_wait, &user_msg) != 0) {
728 * Error. radius_exec_program() returns -1 on
729 * fork/exec errors, or >0 if the exec'ed program
730 * had a non-zero exit status.
732 if (user_msg == NULL)
733 user_msg = "\r\nAccess denied (external check failed).";
735 request->reply->code = PW_AUTHENTICATION_REJECT;
736 pairfree(&request->reply->vps);
737 tmp = pairmake("Reply-Message", user_msg, T_OP_SET);
738 request->reply->vps = tmp;
740 rad_authlog("Login incorrect (external check failed)",
743 return RLM_MODULE_OK;
748 * Delete "normal" A/V pairs when using callback.
750 * FIXME: This is stupid. The portmaster should accept
751 * these settings instead of insisting on using a
754 * FIXME2: Move this into the above exec thingy?
755 * (if you knew how I use the exec_wait, you'd understand).
757 if (seen_callback_id) {
758 pairdelete(&request->reply->vps, PW_FRAMED_PROTOCOL);
759 pairdelete(&request->reply->vps, PW_FRAMED_IP_ADDRESS);
760 pairdelete(&request->reply->vps, PW_FRAMED_IP_NETMASK);
761 pairdelete(&request->reply->vps, PW_FRAMED_ROUTE);
762 pairdelete(&request->reply->vps, PW_FRAMED_MTU);
763 pairdelete(&request->reply->vps, PW_FRAMED_COMPRESSION);
764 pairdelete(&request->reply->vps, PW_FILTER_ID);
765 pairdelete(&request->reply->vps, PW_PORT_LIMIT);
766 pairdelete(&request->reply->vps, PW_CALLBACK_NUMBER);
770 * Filter (possibly multiple) Reply-Message attributes
771 * through radius_xlat2, modifying them in place.
773 if (user_msg == NULL) {
774 reply_item = pairfind(request->reply->vps, PW_REPLY_MESSAGE);
776 radius_xlat2(buf, sizeof(reply_item->strvalue),
777 (char *)reply_item->strvalue, request);
778 strNcpy((char *)reply_item->strvalue, buf,
779 sizeof(reply_item->strvalue));
780 reply_item->length = strlen((char *)reply_item->strvalue);
782 reply_item = pairfind(reply_item->next, PW_REPLY_MESSAGE);
786 request->reply->code = PW_AUTHENTICATION_ACK;
788 rad_authlog("Login OK", request, 1);
789 if (exec_program && !exec_wait) {
791 * No need to check the exit status here.
793 radius_exec_program(exec_program, request, exec_wait, NULL);
798 return RLM_MODULE_OK;
803 * These definitions are local, and shouldn't be used by anyone else.
809 * Find the password pair, decode pass if
810 * needed, and return the value pair. If
811 * not found, return NULL
813 VALUE_PAIR *rad_getpass(REQUEST *request) {
814 VALUE_PAIR *auth_item;
817 * First, look up the password in the request header.
819 auth_item = request->password;
822 * It's there, but it's not a clear-text password.
825 if (auth_item->attribute != PW_PASSWORD) {
830 * Go find the request password.
832 auth_item = pairfind(request->packet->vps, PW_PASSWORD);
838 * Save the found password for later.
840 request->password = auth_item;
845 * If we proxied already, it's been decoded
846 * Or if the decoded flag is set...just return
848 if ((request->proxy != NULL) ||
849 (auth_item->lvalue == PW_DECODED)) {
854 * If we get here, we have to decode the password.
856 rad_pwdecode((char *)auth_item->strvalue,
857 auth_item->length, request->secret,
858 (char *)request->packet->vector);
861 * Set lvalue to PW_DECODED so we know not to
862 * decode next time we get here
864 auth_item->lvalue = PW_DECODED;
866 /* ignore more than one trailing '\0' */
867 auth_item->length = strlen(auth_item->strvalue);