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>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
33 * Return a short string showing the terminal server, port
34 * and calling station ID.
36 char *auth_name(char *buf, size_t buflen, REQUEST *request, bool do_cli)
43 if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) == NULL) {
47 if ((pair = pairfind(request->packet->vps, PW_NAS_PORT, 0, TAG_ANY)) != NULL) {
48 port = pair->vp_integer;
51 if (request->packet->dst_port == 0) {
52 if (pairfind(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0, TAG_ANY)) {
53 tls = " via TLS tunnel";
55 tls = " via proxy to virtual server";
59 snprintf(buf, buflen, "from client %.128s port %u%s%.128s%s",
60 request->client->shortname, port,
61 (do_cli ? " cli " : ""), (do_cli ? cli->vp_strvalue : ""),
70 * Make sure user/pass are clean
73 static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
76 char const *extra_msg = NULL;
77 char clean_password[1024];
78 char clean_username[1024];
82 VALUE_PAIR *username = NULL;
84 if (!request->root->log_auth) {
89 * Get the correct username based on the configured value
91 if (!log_stripped_names) {
92 username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
94 username = request->username;
98 * Clean up the username
100 if (username == NULL) {
101 strcpy(clean_username, "<no User-Name attribute>");
103 fr_print_string(username->vp_strvalue,
105 clean_username, sizeof(clean_username), '\0');
109 * Clean up the password
111 if (request->root->log_auth_badpass || request->root->log_auth_goodpass) {
112 if (!request->password) {
113 VALUE_PAIR *auth_type;
115 auth_type = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
117 snprintf(clean_password, sizeof(clean_password),
118 "<via Auth-Type = %s>",
119 dict_valnamebyattr(PW_AUTH_TYPE, 0,
120 auth_type->vp_integer));
122 strcpy(clean_password, "<no User-Password attribute>");
124 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) {
125 strcpy(clean_password, "<CHAP-Password>");
127 fr_print_string(request->password->vp_strvalue,
128 request->password->length,
129 clean_password, sizeof(clean_password), '\0');
134 logit = request->root->log_auth_goodpass;
135 extra_msg = request->root->auth_goodpass_msg;
137 logit = request->root->log_auth_badpass;
138 extra_msg = request->root->auth_badpass_msg;
144 if (radius_xlat(p, sizeof(extra) - 1, request, extra_msg, NULL, NULL) < 0) {
151 RAUTH("%s: [%s%s%s] (%s)%s",
155 logit ? clean_password : "",
156 auth_name(buf, sizeof(buf), request, 1),
167 * -2 Rejected (Auth-Type = Reject, send Port-Message back)
168 * 1 End check & return, don't reply
170 * NOTE: NOT the same as the RLM_ values !
172 static int CC_HINT(nonnull) rad_check_password(REQUEST *request)
175 VALUE_PAIR *auth_type_pair;
178 int auth_type_count = 0;
181 * Look for matching check items. We skip the whole lot
182 * if the authentication type is PW_AUTHTYPE_ACCEPT or
183 * PW_AUTHTYPE_REJECT.
185 fr_cursor_init(&cursor, &request->config_items);
186 while ((auth_type_pair = fr_cursor_next_by_num(&cursor, PW_AUTH_TYPE, 0, TAG_ANY))) {
187 auth_type = auth_type_pair->vp_integer;
190 RDEBUG2("Found Auth-Type = %s", dict_valnamebyattr(PW_AUTH_TYPE, 0, auth_type));
191 if (auth_type == PW_AUTHTYPE_REJECT) {
192 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 RERROR("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, TAG_ANY) != NULL) {
229 RWDEBUG2("Please update your configuration, and remove 'Auth-Type = Crypt'");
230 RWDEBUG2("Use the PAP module instead");
232 else if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) != NULL) {
233 RWDEBUG2("Please update your configuration, and remove 'Auth-Type = Local'");
234 RWDEBUG2("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 REDEBUG2("No Auth-Type found: rejecting the user via Post-Auth-Type = Reject");
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 = process_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:
277 case RLM_MODULE_HANDLED:
286 * Post-authentication step processes the response before it is
287 * sent to the NAS. It can receive both Access-Accept and Access-Reject
290 int rad_postauth(REQUEST *request)
293 int postauth_type = 0;
297 * Do post-authentication calls. ignoring the return code.
299 vp = pairfind(request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
301 postauth_type = vp->vp_integer;
302 RDEBUG2("Using Post-Auth-Type %s",
303 dict_valnamebyattr(PW_POST_AUTH_TYPE, 0, postauth_type));
305 result = process_post_auth(postauth_type, request);
308 * The module failed, or said to reject the user: Do so.
310 case RLM_MODULE_FAIL:
311 case RLM_MODULE_INVALID:
312 case RLM_MODULE_REJECT:
313 case RLM_MODULE_USERLOCK:
315 request->reply->code = PW_CODE_ACCESS_REJECT;
316 result = RLM_MODULE_REJECT;
319 * The module handled the request, cancel the reply.
321 case RLM_MODULE_HANDLED:
325 * The module had a number of OK return codes.
327 case RLM_MODULE_NOOP:
328 case RLM_MODULE_NOTFOUND:
330 case RLM_MODULE_UPDATED:
331 result = RLM_MODULE_OK;
338 * Process and reply to an authentication request
340 * The return value of this function isn't actually used right now, so
341 * it's not entirely clear if it is returning the right things. --Pac.
343 int rad_authenticate(REQUEST *request)
345 #ifdef WITH_SESSION_MGMT
346 VALUE_PAIR *check_item;
348 VALUE_PAIR *module_msg;
349 VALUE_PAIR *tmp = NULL;
356 * If this request got proxied to another server, we need
357 * to check whether it authenticated the request or not.
359 * request->proxy gets set only AFTER authorization, so
360 * it's safe to check it here. If it exists, it means
361 * we're doing a second pass through rad_authenticate().
363 if (request->proxy) {
366 if (request->proxy_reply) code = request->proxy_reply->code;
370 * Reply of ACCEPT means accept, thus set Auth-Type
373 case PW_CODE_ACCESS_ACCEPT:
374 tmp = radius_paircreate(request,
375 &request->config_items,
377 if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
381 * Challenges are punted back to the NAS without any
382 * further processing.
384 case PW_CODE_ACCESS_CHALLENGE:
385 request->reply->code = PW_CODE_ACCESS_CHALLENGE;
386 return RLM_MODULE_OK;
389 * ALL other replies mean reject. (this is fail-safe)
391 * Do NOT do any authorization or authentication. They
392 * are being rejected, so we minimize the amount of work
393 * done by the server, by rejecting them here.
395 case PW_CODE_ACCESS_REJECT:
396 rad_authlog("Login incorrect (Home Server says so)",
398 request->reply->code = PW_CODE_ACCESS_REJECT;
399 return RLM_MODULE_REJECT;
402 rad_authlog("Login incorrect (Home Server failed to respond)",
404 return RLM_MODULE_REJECT;
409 * Look for, and cache, passwords.
411 if (!request->password) {
412 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
414 if (!request->password) {
415 request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
419 * Get the user's authorization information from the database
422 result = process_authorize(autz_type, request);
424 case RLM_MODULE_NOOP:
425 case RLM_MODULE_NOTFOUND:
427 case RLM_MODULE_UPDATED:
429 case RLM_MODULE_HANDLED:
431 case RLM_MODULE_FAIL:
432 case RLM_MODULE_INVALID:
433 case RLM_MODULE_REJECT:
434 case RLM_MODULE_USERLOCK:
436 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL) {
437 char msg[MAX_STRING_LEN + 16];
438 snprintf(msg, sizeof(msg), "Invalid user (%s)",
439 module_msg->vp_strvalue);
440 rad_authlog(msg,request,0);
442 rad_authlog("Invalid user", request, 0);
444 request->reply->code = PW_CODE_ACCESS_REJECT;
448 tmp = pairfind(request->config_items, PW_AUTZ_TYPE, 0, TAG_ANY);
450 autz_type = tmp->vp_integer;
451 RDEBUG2("Using Autz-Type %s",
452 dict_valnamebyattr(PW_AUTZ_TYPE, 0, autz_type));
459 * If we haven't already proxied the packet, then check
460 * to see if we should. Maybe one of the authorize
461 * modules has decided that a proxy should be used. If
462 * so, get out of here and send the packet.
466 (request->proxy == NULL) &&
468 ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY)) != NULL)) {
471 realm = realm_find2(tmp->vp_strvalue);
474 * Don't authenticate, as the request is going to
477 if (realm && realm->auth_pool) {
478 return RLM_MODULE_OK;
482 * Catch users who set Proxy-To-Realm to a LOCAL
483 * realm (sigh). But don't complain if it is
486 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
487 RWDEBUG2("You set Proxy-To-Realm = %s, but it is a LOCAL realm! Cancelling proxy request.", realm->name);
491 RWDEBUG2("You set Proxy-To-Realm = %s, but the realm does not exist! Cancelling invalid proxy request.", tmp->vp_strvalue);
503 result = rad_check_password(request);
506 return RLM_MODULE_HANDLED;
511 * Failed to validate the user.
513 * We PRESUME that the code which failed will clean up
514 * request->reply->vps, to be ONLY the reply items it
515 * wants to send back.
518 RDEBUG2("Failed to authenticate the user");
519 request->reply->code = PW_CODE_ACCESS_REJECT;
521 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL){
522 char msg[MAX_STRING_LEN+19];
524 snprintf(msg, sizeof(msg), "Login incorrect (%s)",
525 module_msg->vp_strvalue);
526 rad_authlog(msg, request, 0);
528 rad_authlog("Login incorrect", request, 0);
531 if (request->password) {
532 VERIFY_VP(request->password);
533 /* double check: maybe the secret is wrong? */
534 if ((debug_flag > 1) && (request->password->da->attr == PW_USER_PASSWORD)) {
537 p = (uint8_t const *) request->password->vp_strvalue;
541 size = fr_utf8_char(p);
543 RWDEBUG("Unprintable characters in the password. Double-check the "
544 "shared secret on the server and the NAS!");
553 #ifdef WITH_SESSION_MGMT
555 (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE, 0, TAG_ANY)) != NULL) {
556 int r, session_type = 0;
558 char umsg[MAX_STRING_LEN + 1];
560 tmp = pairfind(request->config_items, PW_SESSION_TYPE, 0, TAG_ANY);
562 session_type = tmp->vp_integer;
563 RDEBUG2("Using Session-Type %s",
564 dict_valnamebyattr(PW_SESSION_TYPE, 0, session_type));
568 * User authenticated O.K. Now we have to check
569 * for the Simultaneous-Use parameter.
571 if (request->username &&
572 (r = process_checksimul(session_type, request, check_item->vp_integer)) != 0) {
576 /* Multilink attempt. Check if port-limit > simultaneous-use */
577 VALUE_PAIR *port_limit;
579 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT, 0, TAG_ANY)) != NULL &&
580 port_limit->vp_integer > check_item->vp_integer){
581 RDEBUG2("MPP is OK");
586 if (check_item->vp_integer > 1) {
587 snprintf(umsg, sizeof(umsg),
589 main_config.denied_msg,
590 (int)check_item->vp_integer);
592 snprintf(umsg, sizeof(umsg),
594 main_config.denied_msg);
597 request->reply->code = PW_CODE_ACCESS_REJECT;
600 * They're trying to log in too many times.
601 * Remove ALL reply attributes.
603 pairfree(&request->reply->vps);
604 pairmake_reply("Reply-Message",
607 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
608 check_item->vp_integer,
609 r == 2 ? "[MPP attempt]" : "");
610 rad_authlog(logstr, request, 1);
619 * Result should be >= 0 here - if not, it means the user
620 * is rejected, so we just process post-auth and return.
623 return RLM_MODULE_REJECT;
627 * Set the reply to Access-Accept, if it hasn't already
628 * been set to something. (i.e. Access-Challenge)
630 if (request->reply->code == 0)
631 request->reply->code = PW_CODE_ACCESS_ACCEPT;
633 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_SUCCESS_MESSAGE, 0, TAG_ANY)) != NULL){
634 char msg[MAX_STRING_LEN+12];
636 snprintf(msg, sizeof(msg), "Login OK (%s)",
637 module_msg->vp_strvalue);
638 rad_authlog(msg, request, 1);
640 rad_authlog("Login OK", request, 1);
647 * Run a virtual server auth and postauth
650 int rad_virtual_server(REQUEST *request)
655 RDEBUG("server %s {", request->server);
657 RDEBUG("Received request");
659 rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps);
662 * We currently only handle AUTH packets here.
663 * This could be expanded to handle other packets as well if required.
665 rad_assert(request->packet->code == PW_CODE_ACCESS_REQUEST);
667 result = rad_authenticate(request);
669 if (request->reply->code == PW_CODE_ACCESS_REJECT) {
670 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
671 vp = pairmake_config("Post-Auth-Type", "Reject", T_OP_SET);
672 if (vp) rad_postauth(request);
675 if (request->reply->code == PW_CODE_ACCESS_ACCEPT) {
676 rad_postauth(request);
679 RDEBUG("Sending reply");
681 rdebug_pair_list(L_DBG_LVL_1, request, request->reply->vps);
683 RDEBUG("} # server %s", request->server);