Move debug messages into rad_virtual_server
[freeradius.git] / src / main / auth.c
1 /*
2  * auth.c       User authentication.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
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>
23  */
24 RCSID("$Id$")
25
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
29
30 #include <ctype.h>
31
32 /*
33  *      Return a short string showing the terminal server, port
34  *      and calling station ID.
35  */
36 char *auth_name(char *buf, size_t buflen, REQUEST *request, bool do_cli)
37 {
38         VALUE_PAIR      *cli;
39         VALUE_PAIR      *pair;
40         uint16_t        port = 0;
41         char const      *tls = "";
42
43         if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) == NULL) {
44                 do_cli = false;
45         }
46
47         if ((pair = pairfind(request->packet->vps, PW_NAS_PORT, 0, TAG_ANY)) != NULL) {
48                 port = pair->vp_integer;
49         }
50
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";
54                 } else {
55                         tls = " via proxy to virtual server";
56                 }
57         }
58
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 : ""),
62                  tls);
63
64         return buf;
65 }
66
67
68
69 /*
70  * Make sure user/pass are clean
71  * and then log them
72  */
73 static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
74 {
75         int logit;
76         char const *extra_msg = NULL;
77         char clean_password[1024];
78         char clean_username[1024];
79         char buf[1024];
80         char extra[1024];
81         char *p;
82         VALUE_PAIR *username = NULL;
83
84         if (!request->root->log_auth) {
85                 return 0;
86         }
87
88         /*
89          * Get the correct username based on the configured value
90          */
91         if (!log_stripped_names) {
92                 username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
93         } else {
94                 username = request->username;
95         }
96
97         /*
98          *      Clean up the username
99          */
100         if (username == NULL) {
101                 strcpy(clean_username, "<no User-Name attribute>");
102         } else {
103                 fr_print_string(username->vp_strvalue,
104                                 username->length,
105                                 clean_username, sizeof(clean_username));
106         }
107
108         /*
109          *      Clean up the password
110          */
111         if (request->root->log_auth_badpass || request->root->log_auth_goodpass) {
112                 if (!request->password) {
113                         VALUE_PAIR *auth_type;
114
115                         auth_type = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
116                         if (auth_type) {
117                                 snprintf(clean_password, sizeof(clean_password),
118                                          "<via Auth-Type = %s>",
119                                          dict_valnamebyattr(PW_AUTH_TYPE, 0,
120                                                             auth_type->vp_integer));
121                         } else {
122                                 strcpy(clean_password, "<no User-Password attribute>");
123                         }
124                 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) {
125                         strcpy(clean_password, "<CHAP-Password>");
126                 } else {
127                         fr_print_string(request->password->vp_strvalue,
128                                          request->password->length,
129                                          clean_password, sizeof(clean_password));
130                 }
131         }
132
133         if (goodpass) {
134                 logit = request->root->log_auth_goodpass;
135                 extra_msg = request->root->auth_goodpass_msg;
136         } else {
137                 logit = request->root->log_auth_badpass;
138                 extra_msg = request->root->auth_badpass_msg;
139         }
140
141         if (extra_msg) {
142                 extra[0] = ' ';
143                 p = extra + 1;
144                 if (radius_xlat(p, sizeof(extra) - 1, request, extra_msg, NULL, NULL) < 0) {
145                         return -1;
146                 }
147         } else {
148                 *extra = '\0';
149         }
150
151         RAUTH("%s: [%s%s%s] (%s)%s",
152                        msg,
153                        clean_username,
154                        logit ? "/" : "",
155                        logit ? clean_password : "",
156                        auth_name(buf, sizeof(buf), request, 1),
157                        extra);
158
159         return 0;
160 }
161
162 /*
163  *      Check password.
164  *
165  *      Returns:        0  OK
166  *                      -1 Password fail
167  *                      -2 Rejected (Auth-Type = Reject, send Port-Message back)
168  *                      1  End check & return, don't reply
169  *
170  *      NOTE: NOT the same as the RLM_ values !
171  */
172 static int CC_HINT(nonnull) rad_check_password(REQUEST *request)
173 {
174         vp_cursor_t cursor;
175         VALUE_PAIR *auth_type_pair;
176         int auth_type = -1;
177         int result;
178         int auth_type_count = 0;
179
180         /*
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.
184          */
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;
188                 auth_type_count++;
189
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");
193
194                         return -2;
195                 }
196         }
197
198         /*
199          *      Warn if more than one Auth-Type was found, because only the last
200          *      one found will actually be used.
201          */
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);
205         }
206
207         /*
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.
211          */
212         if ((auth_type == PW_AUTHTYPE_ACCEPT)
213 #ifdef WITH_PROXY
214             || (request->proxy)
215 #endif
216             ) {
217                 RDEBUG2("Auth-Type = Accept, accepting the user");
218                 return 0;
219         }
220
221         /*
222          *      Check that Auth-Type has been set, and reject if not.
223          *
224          *      Do quick checks to see if Cleartext-Password or Crypt-Password have
225          *      been set, and complain if so.
226          */
227         if (auth_type < 0) {
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");
231                 }
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");
235                 }
236
237                 /*
238                  *      The admin hasn't told us how to
239                  *      authenticate the user, so we reject them!
240                  *
241                  *      This is fail-safe.
242                  */
243
244                 REDEBUG2("No Auth-Type found: rejecting the user via Post-Auth-Type = Reject");
245                 return -2;
246         }
247
248         /*
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.
253          */
254         result = process_authenticate(auth_type, request);
255         switch (result) {
256                 /*
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!
261                  */
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:
269                 default:
270                         result = -1;
271                         break;
272                 case RLM_MODULE_OK:
273                         result = 0;
274                         break;
275                 case RLM_MODULE_HANDLED:
276                         result = 1;
277                         break;
278         }
279
280         return result;
281 }
282
283 /*
284  *      Post-authentication step processes the response before it is
285  *      sent to the NAS. It can receive both Access-Accept and Access-Reject
286  *      replies.
287  */
288 int rad_postauth(REQUEST *request)
289 {
290         int     result;
291         int     postauth_type = 0;
292         VALUE_PAIR *vp;
293
294         /*
295          *      Do post-authentication calls. ignoring the return code.
296          */
297         vp = pairfind(request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
298         if (vp) {
299                 postauth_type = vp->vp_integer;
300                 RDEBUG2("Using Post-Auth-Type %s",
301                         dict_valnamebyattr(PW_POST_AUTH_TYPE, 0, postauth_type));
302         }
303         result = process_post_auth(postauth_type, request);
304         switch (result) {
305                 /*
306                  *      The module failed, or said to reject the user: Do so.
307                  */
308                 case RLM_MODULE_FAIL:
309                 case RLM_MODULE_INVALID:
310                 case RLM_MODULE_REJECT:
311                 case RLM_MODULE_USERLOCK:
312                 default:
313                         request->reply->code = PW_CODE_ACCESS_REJECT;
314                         result = RLM_MODULE_REJECT;
315                         break;
316                 /*
317                  *      The module handled the request, cancel the reply.
318                  */
319                 case RLM_MODULE_HANDLED:
320                         /* FIXME */
321                         break;
322                 /*
323                  *      The module had a number of OK return codes.
324                  */
325                 case RLM_MODULE_NOOP:
326                 case RLM_MODULE_NOTFOUND:
327                 case RLM_MODULE_OK:
328                 case RLM_MODULE_UPDATED:
329                         result = RLM_MODULE_OK;
330                         break;
331         }
332         return result;
333 }
334
335 /*
336  *      Process and reply to an authentication request
337  *
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.
340  */
341 int rad_authenticate(REQUEST *request)
342 {
343 #ifdef WITH_SESSION_MGMT
344         VALUE_PAIR      *check_item;
345 #endif
346         VALUE_PAIR      *module_msg;
347         VALUE_PAIR      *tmp = NULL;
348         int             result;
349         char            autz_retry = 0;
350         int             autz_type = 0;
351
352 #ifdef WITH_PROXY
353         /*
354          *      If this request got proxied to another server, we need
355          *      to check whether it authenticated the request or not.
356          *
357          *      request->proxy gets set only AFTER authorization, so
358          *      it's safe to check it here.  If it exists, it means
359          *      we're doing a second pass through rad_authenticate().
360          */
361         if (request->proxy) {
362                 int code = 0;
363
364                 if (request->proxy_reply) code = request->proxy_reply->code;
365
366                 switch (code) {
367                 /*
368                  *      Reply of ACCEPT means accept, thus set Auth-Type
369                  *      accordingly.
370                  */
371                 case PW_CODE_ACCESS_ACCEPT:
372                         tmp = radius_paircreate(request,
373                                                 &request->config_items,
374                                                 PW_AUTH_TYPE, 0);
375                         if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
376                         goto authenticate;
377
378                 /*
379                  *      Challenges are punted back to the NAS without any
380                  *      further processing.
381                  */
382                 case PW_CODE_ACCESS_CHALLENGE:
383                         request->reply->code = PW_CODE_ACCESS_CHALLENGE;
384                         return RLM_MODULE_OK;
385
386                 /*
387                  *      ALL other replies mean reject. (this is fail-safe)
388                  *
389                  *      Do NOT do any authorization or authentication. They
390                  *      are being rejected, so we minimize the amount of work
391                  *      done by the server, by rejecting them here.
392                  */
393                 case PW_CODE_ACCESS_REJECT:
394                         rad_authlog("Login incorrect (Home Server says so)",
395                                     request, 0);
396                         request->reply->code = PW_CODE_ACCESS_REJECT;
397                         return RLM_MODULE_REJECT;
398
399                 default:
400                         rad_authlog("Login incorrect (Home Server failed to respond)",
401                                     request, 0);
402                         return RLM_MODULE_REJECT;
403                 }
404         }
405 #endif
406         /*
407          *      Look for, and cache, passwords.
408          */
409         if (!request->password) {
410                 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
411         }
412         if (!request->password) {
413                 request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
414         }
415
416         /*
417          *      Get the user's authorization information from the database
418          */
419 autz_redo:
420         result = process_authorize(autz_type, request);
421         switch (result) {
422                 case RLM_MODULE_NOOP:
423                 case RLM_MODULE_NOTFOUND:
424                 case RLM_MODULE_OK:
425                 case RLM_MODULE_UPDATED:
426                         break;
427                 case RLM_MODULE_HANDLED:
428                         return result;
429                 case RLM_MODULE_FAIL:
430                 case RLM_MODULE_INVALID:
431                 case RLM_MODULE_REJECT:
432                 case RLM_MODULE_USERLOCK:
433                 default:
434                         if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL) {
435                                 char msg[MAX_STRING_LEN + 16];
436                                 snprintf(msg, sizeof(msg), "Invalid user (%s)",
437                                          module_msg->vp_strvalue);
438                                 rad_authlog(msg,request,0);
439                         } else {
440                                 rad_authlog("Invalid user", request, 0);
441                         }
442                         request->reply->code = PW_CODE_ACCESS_REJECT;
443                         return result;
444         }
445         if (!autz_retry) {
446                 tmp = pairfind(request->config_items, PW_AUTZ_TYPE, 0, TAG_ANY);
447                 if (tmp) {
448                         autz_type = tmp->vp_integer;
449                         RDEBUG2("Using Autz-Type %s",
450                                 dict_valnamebyattr(PW_AUTZ_TYPE, 0, autz_type));
451                         autz_retry = 1;
452                         goto autz_redo;
453                 }
454         }
455
456         /*
457          *      If we haven't already proxied the packet, then check
458          *      to see if we should.  Maybe one of the authorize
459          *      modules has decided that a proxy should be used. If
460          *      so, get out of here and send the packet.
461          */
462         if (
463 #ifdef WITH_PROXY
464             (request->proxy == NULL) &&
465 #endif
466             ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY)) != NULL)) {
467                 REALM *realm;
468
469                 realm = realm_find2(tmp->vp_strvalue);
470
471                 /*
472                  *      Don't authenticate, as the request is going to
473                  *      be proxied.
474                  */
475                 if (realm && realm->auth_pool) {
476                         return RLM_MODULE_OK;
477                 }
478
479                 /*
480                  *      Catch users who set Proxy-To-Realm to a LOCAL
481                  *      realm (sigh).  But don't complain if it is
482                  *      *the* LOCAL realm.
483                  */
484                 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
485                         RWDEBUG2("You set Proxy-To-Realm = %s, but it is a LOCAL realm!  Cancelling proxy request.", realm->name);
486                 }
487
488                 if (!realm) {
489                         RWDEBUG2("You set Proxy-To-Realm = %s, but the realm does not exist!  Cancelling invalid proxy request.", tmp->vp_strvalue);
490                 }
491         }
492
493 #ifdef WITH_PROXY
494  authenticate:
495 #endif
496
497         /*
498          *      Validate the user
499          */
500         do {
501                 result = rad_check_password(request);
502                 if (result > 0) {
503                         /* don't reply! */
504                         return RLM_MODULE_HANDLED;
505                 }
506         } while(0);
507
508         /*
509          *      Failed to validate the user.
510          *
511          *      We PRESUME that the code which failed will clean up
512          *      request->reply->vps, to be ONLY the reply items it
513          *      wants to send back.
514          */
515         if (result < 0) {
516                 RDEBUG2("Failed to authenticate the user");
517                 request->reply->code = PW_CODE_ACCESS_REJECT;
518
519                 if ((module_msg = pairfind(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL){
520                         char msg[MAX_STRING_LEN+19];
521
522                         snprintf(msg, sizeof(msg), "Login incorrect (%s)",
523                                  module_msg->vp_strvalue);
524                         rad_authlog(msg, request, 0);
525                 } else {
526                         rad_authlog("Login incorrect", request, 0);
527                 }
528
529                 if (request->password) {
530                         VERIFY_VP(request->password);
531                         /* double check: maybe the secret is wrong? */
532                         if ((debug_flag > 1) && (request->password->da->attr == PW_USER_PASSWORD)) {
533                                 uint8_t const *p;
534
535                                 p = (uint8_t const *) request->password->vp_strvalue;
536                                 while (*p) {
537                                         int size;
538
539                                         size = fr_utf8_char(p);
540                                         if (!size) {
541                                                 RWDEBUG("Unprintable characters in the password.  Double-check the "
542                                                         "shared secret on the server and the NAS!");
543                                                 break;
544                                         }
545                                         p += size;
546                                 }
547                         }
548                 }
549         }
550
551 #ifdef WITH_SESSION_MGMT
552         if (result >= 0 &&
553             (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE, 0, TAG_ANY)) != NULL) {
554                 int r, session_type = 0;
555                 char            logstr[1024];
556                 char            umsg[MAX_STRING_LEN + 1];
557
558                 tmp = pairfind(request->config_items, PW_SESSION_TYPE, 0, TAG_ANY);
559                 if (tmp) {
560                         session_type = tmp->vp_integer;
561                         RDEBUG2("Using Session-Type %s",
562                                 dict_valnamebyattr(PW_SESSION_TYPE, 0, session_type));
563                 }
564
565                 /*
566                  *      User authenticated O.K. Now we have to check
567                  *      for the Simultaneous-Use parameter.
568                  */
569                 if (request->username &&
570                     (r = process_checksimul(session_type, request, check_item->vp_integer)) != 0) {
571                         char mpp_ok = 0;
572
573                         if (r == 2){
574                                 /* Multilink attempt. Check if port-limit > simultaneous-use */
575                                 VALUE_PAIR *port_limit;
576
577                                 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT, 0, TAG_ANY)) != NULL &&
578                                         port_limit->vp_integer > check_item->vp_integer){
579                                         RDEBUG2("MPP is OK");
580                                         mpp_ok = 1;
581                                 }
582                         }
583                         if (!mpp_ok){
584                                 if (check_item->vp_integer > 1) {
585                                         snprintf(umsg, sizeof(umsg),
586                                                  "\r\n%s (%d)\r\n\n",
587                                                  main_config.denied_msg,
588                                                  (int)check_item->vp_integer);
589                                 } else {
590                                         snprintf(umsg, sizeof(umsg),
591                                                  "\r\n%s\r\n\n",
592                                                  main_config.denied_msg);
593                                 }
594
595                                 request->reply->code = PW_CODE_ACCESS_REJECT;
596
597                                 /*
598                                  *      They're trying to log in too many times.
599                                  *      Remove ALL reply attributes.
600                                  */
601                                 pairfree(&request->reply->vps);
602                                 pairmake_reply("Reply-Message",
603                                                umsg, T_OP_SET);
604
605                                 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
606                                         check_item->vp_integer,
607                                         r == 2 ? "[MPP attempt]" : "");
608                                 rad_authlog(logstr, request, 1);
609
610                                 result = -1;
611                         }
612                 }
613         }
614 #endif
615
616         /*
617          *      Result should be >= 0 here - if not, it means the user
618          *      is rejected, so we just process post-auth and return.
619          */
620         if (result < 0) {
621                 return RLM_MODULE_REJECT;
622         }
623
624         /*
625          *      Set the reply to Access-Accept, if it hasn't already
626          *      been set to something.  (i.e. Access-Challenge)
627          */
628         if (request->reply->code == 0)
629           request->reply->code = PW_CODE_ACCESS_ACCEPT;
630
631         if ((module_msg = pairfind(request->packet->vps, PW_MODULE_SUCCESS_MESSAGE, 0, TAG_ANY)) != NULL){
632                 char msg[MAX_STRING_LEN+12];
633
634                 snprintf(msg, sizeof(msg), "Login OK (%s)",
635                          module_msg->vp_strvalue);
636                 rad_authlog(msg, request, 1);
637         } else {
638                 rad_authlog("Login OK", request, 1);
639         }
640
641         return result;
642 }
643
644 /*
645  *      Run a virtual server auth and postauth
646  *
647  */
648 int rad_virtual_server(REQUEST *request)
649 {
650         VALUE_PAIR *vp;
651         int result;
652
653         RDEBUG("server %s {", request->server);
654         RDEBUG("  Request:");
655         debug_pair_list(request->packet->vps);
656
657         /*
658          *      We currently only handle AUTH packets here.
659          *      This could be expanded to handle other packets as well if required.
660          */
661         rad_assert(request->packet->code == PW_CODE_ACCESS_REQUEST);
662
663         result = rad_authenticate(request);
664
665         if (request->reply->code == PW_CODE_ACCESS_REJECT) {
666                 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0, TAG_ANY);
667                 vp = pairmake_config("Post-Auth-Type", "Reject", T_OP_SET);
668                 if (vp) rad_postauth(request);
669         }
670
671         if (request->reply->code == PW_CODE_ACCESS_ACCEPT) {
672                 rad_postauth(request);
673         }
674
675         RDEBUG("  Reply:");
676         debug_pair_list(request->reply->vps);
677         RDEBUG("} # server %s", request->server);
678
679         return result;
680 }