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,
116 snprintf(clean_password, sizeof(clean_password),
117 "<via Auth-Type = %s>",
118 dict_valnamebyattr(PW_AUTH_TYPE, 0,
119 auth_type->vp_integer));
121 strcpy(clean_password, "<no User-Password attribute>");
123 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0)) {
124 strcpy(clean_password, "<CHAP-Password>");
126 fr_print_string((char *)request->password->vp_strvalue,
127 request->password->length,
128 clean_password, sizeof(clean_password));
133 logit = request->root->log_auth_goodpass;
134 extra_msg = request->root->auth_goodpass_msg;
136 logit = request->root->log_auth_badpass;
137 extra_msg = request->root->auth_badpass_msg;
142 radius_xlat(extra + 1, sizeof(extra) - 1, extra_msg, request,
148 radlog_request(L_AUTH, 0, request, "%s: [%s%s%s] (%s)%s",
152 logit ? clean_password : "",
153 auth_name(buf, sizeof(buf), request, 1),
164 * -2 Rejected (Auth-Type = Reject, send Port-Message back)
165 * 1 End check & return, don't reply
167 * NOTE: NOT the same as the RLM_ values !
169 static int rad_check_password(REQUEST *request)
171 VALUE_PAIR *auth_type_pair;
172 VALUE_PAIR *cur_config_item;
175 int auth_type_count = 0;
179 * Look for matching check items. We skip the whole lot
180 * if the authentication type is PW_AUTHTYPE_ACCEPT or
181 * PW_AUTHTYPE_REJECT.
183 cur_config_item = request->config_items;
184 while(((auth_type_pair = pairfind(cur_config_item, PW_AUTH_TYPE, 0))) != NULL) {
185 auth_type = auth_type_pair->vp_integer;
188 RDEBUG2("Found Auth-Type = %s",
189 dict_valnamebyattr(PW_AUTH_TYPE, 0, auth_type));
190 cur_config_item = auth_type_pair->next;
192 if (auth_type == PW_AUTHTYPE_REJECT) {
193 RDEBUG2("Auth-Type = Reject, rejecting user");
199 * Warn if more than one Auth-Type was found, because only the last
200 * one found will actually be used.
202 if (( auth_type_count > 1) && (debug_flag)) {
203 radlog_request(L_ERR, 0, request, "Warning: Found %d auth-types on request for user '%s'",
204 auth_type_count, request->username->vp_strvalue);
208 * This means we have a proxy reply or an accept and it wasn't
209 * rejected in the above loop. So that means it is accepted and we
210 * do no further authentication.
212 if ((auth_type == PW_AUTHTYPE_ACCEPT)
217 RDEBUG2("Auth-Type = Accept, accepting the user");
222 * Check that Auth-Type has been set, and reject if not.
224 * Do quick checks to see if Cleartext-Password or Crypt-Password have
225 * been set, and complain if so.
228 if (pairfind(request->config_items, PW_CRYPT_PASSWORD, 0) != NULL) {
229 RDEBUG2("WARNING: Please update your configuration, and remove 'Auth-Type = Crypt'");
230 RDEBUG2("WARNING: Use the PAP module instead.");
232 else if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0) != NULL) {
233 RDEBUG2("WARNING: Please update your configuration, and remove 'Auth-Type = Local'");
234 RDEBUG2("WARNING: Use the PAP or CHAP modules instead.");
238 * The admin hasn't told us how to
239 * authenticate the user, so we reject them!
244 RDEBUG2("ERROR: No authenticate method (Auth-Type) found for the request: Rejecting the user");
249 * See if there is a module that handles
250 * this Auth-Type, and turn the RLM_ return
251 * status into the values as defined at
252 * the top of this function.
254 result = module_authenticate(auth_type, request);
257 * An authentication module FAIL
258 * return code, or any return code that
259 * is not expected from authentication,
260 * is the same as an explicit REJECT!
262 case RLM_MODULE_FAIL:
263 case RLM_MODULE_INVALID:
264 case RLM_MODULE_NOOP:
265 case RLM_MODULE_NOTFOUND:
266 case RLM_MODULE_REJECT:
267 case RLM_MODULE_UPDATED:
268 case RLM_MODULE_USERLOCK:
275 case RLM_MODULE_HANDLED:
284 * Post-authentication step processes the response before it is
285 * sent to the NAS. It can receive both Access-Accept and Access-Reject
288 int rad_postauth(REQUEST *request)
291 int postauth_type = 0;
295 * Do post-authentication calls. ignoring the return code.
297 vp = pairfind(request->config_items, PW_POST_AUTH_TYPE, 0);
299 postauth_type = vp->vp_integer;
300 RDEBUG2("Using Post-Auth-Type %s",
301 dict_valnamebyattr(PW_POST_AUTH_TYPE, 0, postauth_type));
303 result = module_post_auth(postauth_type, request);
306 * The module failed, or said to reject the user: Do so.
308 case RLM_MODULE_FAIL:
309 case RLM_MODULE_INVALID:
310 case RLM_MODULE_REJECT:
311 case RLM_MODULE_USERLOCK:
313 request->reply->code = PW_AUTHENTICATION_REJECT;
314 result = RLM_MODULE_REJECT;
317 * The module handled the request, cancel the reply.
319 case RLM_MODULE_HANDLED:
323 * The module had a number of OK return codes.
325 case RLM_MODULE_NOOP:
326 case RLM_MODULE_NOTFOUND:
328 case RLM_MODULE_UPDATED:
329 result = RLM_MODULE_OK;
336 * Process and reply to an authentication request
338 * The return value of this function isn't actually used right now, so
339 * it's not entirely clear if it is returning the right things. --Pac.
341 int rad_authenticate(REQUEST *request)
343 VALUE_PAIR *namepair;
344 #ifdef WITH_SESSION_MGMT
345 VALUE_PAIR *check_item;
347 VALUE_PAIR *auth_item = NULL;
348 VALUE_PAIR *module_msg;
349 VALUE_PAIR *tmp = NULL;
351 const char *password;
359 * If this request got proxied to another server, we need
360 * to check whether it authenticated the request or not.
362 if (request->proxy_reply) {
363 switch (request->proxy_reply->code) {
365 * Reply of ACCEPT means accept, thus set Auth-Type
368 case PW_AUTHENTICATION_ACK:
369 tmp = radius_paircreate(request,
370 &request->config_items,
371 PW_AUTH_TYPE, 0, PW_TYPE_INTEGER);
372 if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
376 * Challenges are punted back to the NAS without any
377 * further processing.
379 case PW_ACCESS_CHALLENGE:
380 request->reply->code = PW_ACCESS_CHALLENGE;
381 return RLM_MODULE_OK;
383 * ALL other replies mean reject. (this is fail-safe)
385 * Do NOT do any authorization or authentication. They
386 * are being rejected, so we minimize the amount of work
387 * done by the server, by rejecting them here.
389 case PW_AUTHENTICATION_REJECT:
390 rad_authlog("Login incorrect (Home Server says so)",
392 request->reply->code = PW_AUTHENTICATION_REJECT;
393 return RLM_MODULE_REJECT;
396 rad_authlog("Login incorrect (Home Server failed to respond)",
398 return RLM_MODULE_REJECT;
404 * Get the username from the request.
406 * Note that namepair MAY be NULL, in which case there
407 * is no User-Name attribute in the request.
409 namepair = request->username;
412 * Look for, and cache, passwords.
414 if (!request->password) {
415 request->password = pairfind(request->packet->vps,
416 PW_USER_PASSWORD, 0);
420 * Discover which password we want to use.
422 auth_item = request->password;
424 password = (const char *)auth_item->vp_strvalue;
428 * Maybe there's a CHAP-Password?
430 if ((auth_item = pairfind(request->packet->vps,
431 PW_CHAP_PASSWORD, 0)) != NULL) {
432 password = "<CHAP-PASSWORD>";
436 * No password we recognize.
438 password = "<NO-PASSWORD>";
441 request->password = auth_item;
444 * Get the user's authorization information from the database
447 result = module_authorize(autz_type, request);
449 case RLM_MODULE_NOOP:
450 case RLM_MODULE_NOTFOUND:
452 case RLM_MODULE_UPDATED:
454 case RLM_MODULE_HANDLED:
456 case RLM_MODULE_FAIL:
457 case RLM_MODULE_INVALID:
458 case RLM_MODULE_REJECT:
459 case RLM_MODULE_USERLOCK:
461 if ((module_msg = pairfind(request->packet->vps,
462 PW_MODULE_FAILURE_MESSAGE, 0)) != NULL) {
463 char msg[MAX_STRING_LEN + 16];
464 snprintf(msg, sizeof(msg), "Invalid user (%s)",
465 module_msg->vp_strvalue);
466 rad_authlog(msg,request,0);
468 rad_authlog("Invalid user", request, 0);
470 request->reply->code = PW_AUTHENTICATION_REJECT;
474 tmp = pairfind(request->config_items, PW_AUTZ_TYPE, 0);
476 autz_type = tmp->vp_integer;
477 RDEBUG2("Using Autz-Type %s",
478 dict_valnamebyattr(PW_AUTZ_TYPE, 0, autz_type));
485 * If we haven't already proxied the packet, then check
486 * to see if we should. Maybe one of the authorize
487 * modules has decided that a proxy should be used. If
488 * so, get out of here and send the packet.
492 (request->proxy == NULL) &&
494 ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0)) != NULL)) {
497 realm = realm_find2(tmp->vp_strvalue);
500 * Don't authenticate, as the request is going to
503 if (realm && realm->auth_pool) {
504 return RLM_MODULE_OK;
508 * Catch users who set Proxy-To-Realm to a LOCAL
509 * realm (sigh). But don't complain if it is
512 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
513 RDEBUG2("WARNING: You set Proxy-To-Realm = %s, but it is a LOCAL realm! Cancelling proxy request.", realm->name);
517 RDEBUG2("WARNING: You set Proxy-To-Realm = %s, but the realm does not exist! Cancelling invalid proxy request.", tmp->vp_strvalue);
526 * Perhaps there is a Stripped-User-Name now.
528 namepair = request->username;
534 result = rad_check_password(request);
537 return RLM_MODULE_HANDLED;
542 * Failed to validate the user.
544 * We PRESUME that the code which failed will clean up
545 * request->reply->vps, to be ONLY the reply items it
546 * wants to send back.
549 RDEBUG2("Failed to authenticate the user.");
550 request->reply->code = PW_AUTHENTICATION_REJECT;
552 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_FAILURE_MESSAGE, 0)) != NULL){
553 char msg[MAX_STRING_LEN+19];
555 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
556 module_msg->vp_strvalue);
557 rad_authlog(msg, request, 0);
559 rad_authlog("Login incorrect", request, 0);
562 /* double check: maybe the secret is wrong? */
563 if ((debug_flag > 1) && (auth_item != NULL) &&
564 (auth_item->attribute == PW_USER_PASSWORD)) {
567 p = (uint8_t *) auth_item->vp_strvalue;
571 size = fr_utf8_char(p);
573 log_debug(" WARNING: Unprintable characters in the password. Double-check the shared secret on the server and the NAS!");
581 #ifdef WITH_SESSION_MGMT
583 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE, 0)) != NULL) {
584 int r, session_type = 0;
586 char umsg[MAX_STRING_LEN + 1];
587 const char *user_msg = NULL;
589 tmp = pairfind(request->config_items, PW_SESSION_TYPE, 0);
591 session_type = tmp->vp_integer;
592 RDEBUG2("Using Session-Type %s",
593 dict_valnamebyattr(PW_SESSION_TYPE, 0, session_type));
597 * User authenticated O.K. Now we have to check
598 * for the Simultaneous-Use parameter.
601 (r = module_checksimul(session_type, request, check_item->vp_integer)) != 0) {
605 /* Multilink attempt. Check if port-limit > simultaneous-use */
606 VALUE_PAIR *port_limit;
608 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT, 0)) != NULL &&
609 port_limit->vp_integer > check_item->vp_integer){
610 RDEBUG2("MPP is OK");
615 if (check_item->vp_integer > 1) {
616 snprintf(umsg, sizeof(umsg),
617 "\r\nYou are already logged in %d times - access denied\r\n\n",
618 (int)check_item->vp_integer);
621 user_msg = "\r\nYou are already logged in - access denied\r\n\n";
624 request->reply->code = PW_AUTHENTICATION_REJECT;
627 * They're trying to log in too many times.
628 * Remove ALL reply attributes.
630 pairfree(&request->reply->vps);
631 radius_pairmake(request, &request->reply->vps,
635 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
636 check_item->vp_integer,
637 r == 2 ? "[MPP attempt]" : "");
638 rad_authlog(logstr, request, 1);
647 * Result should be >= 0 here - if not, it means the user
648 * is rejected, so we just process post-auth and return.
651 return RLM_MODULE_REJECT;
655 * Set the reply to Access-Accept, if it hasn't already
656 * been set to something. (i.e. Access-Challenge)
658 if (request->reply->code == 0)
659 request->reply->code = PW_AUTHENTICATION_ACK;
661 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_SUCCESS_MESSAGE, 0)) != NULL){
662 char msg[MAX_STRING_LEN+12];
664 snprintf(msg, sizeof(msg), "Login OK (%s)",
665 module_msg->vp_strvalue);
666 rad_authlog(msg, request, 1);
668 rad_authlog("Login OK", request, 1);
675 * Run a virtual server auth and postauth
678 int rad_virtual_server(REQUEST *request)
684 * We currently only handle AUTH packets here.
685 * This could be expanded to handle other packets as well if required.
687 rad_assert(request->packet->code == PW_AUTHENTICATION_REQUEST);
689 result = rad_authenticate(request);
691 if (request->reply->code == PW_AUTHENTICATION_REJECT) {
692 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0);
693 vp = radius_pairmake(request, &request->config_items,
694 "Post-Auth-Type", "Reject",
696 if (vp) rad_postauth(request);
699 if (request->reply->code == PW_AUTHENTICATION_ACK) {
700 rad_postauth(request);