Merge tag 'release_3_0_12' into branch moonshot-fr-3.0.12-upgrade.
[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/state.h>
29 #include <freeradius-devel/rad_assert.h>
30
31 #include <ctype.h>
32
33 /*
34  *      Return a short string showing the terminal server, port
35  *      and calling station ID.
36  */
37 char *auth_name(char *buf, size_t buflen, REQUEST *request, bool do_cli)
38 {
39         VALUE_PAIR      *cli;
40         VALUE_PAIR      *pair;
41         uint32_t        port = 0;       /* RFC 2865 NAS-Port is 4 bytes */
42         char const      *tls = "";
43
44         if ((cli = fr_pair_find_by_num(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) == NULL) {
45                 do_cli = false;
46         }
47
48         if ((pair = fr_pair_find_by_num(request->packet->vps, PW_NAS_PORT, 0, TAG_ANY)) != NULL) {
49                 port = pair->vp_integer;
50         }
51
52         if (request->packet->dst_port == 0) {
53                 if (fr_pair_find_by_num(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0, TAG_ANY)) {
54                         tls = " via TLS tunnel";
55                 } else {
56                         tls = " via proxy to virtual server";
57                 }
58         }
59
60         snprintf(buf, buflen, "from client %.128s port %u%s%.128s%s",
61                         request->client->shortname, port,
62                  (do_cli ? " cli " : ""), (do_cli ? cli->vp_strvalue : ""),
63                  tls);
64
65         return buf;
66 }
67
68
69
70 /*
71  * Make sure user/pass are clean
72  * and then log them
73  */
74 static int rad_authlog(char const *msg, REQUEST *request, int goodpass)
75 {
76         int logit;
77         char const *extra_msg = NULL;
78         char clean_password[1024];
79         char clean_username[1024];
80         char buf[1024];
81         char extra[1024];
82         char *p;
83         VALUE_PAIR *username = NULL;
84
85         if (!request->root->log_auth) {
86                 return 0;
87         }
88
89         /*
90          * Get the correct username based on the configured value
91          */
92         if (!log_stripped_names) {
93                 username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
94         } else {
95                 username = request->username;
96         }
97
98         /*
99          *      Clean up the username
100          */
101         if (username == NULL) {
102                 strcpy(clean_username, "<no User-Name attribute>");
103         } else {
104                 fr_prints(clean_username, sizeof(clean_username), username->vp_strvalue, username->vp_length, '\0');
105         }
106
107         /*
108          *      Clean up the password
109          */
110         if (request->root->log_auth_badpass || request->root->log_auth_goodpass) {
111                 if (!request->password) {
112                         VALUE_PAIR *auth_type;
113
114                         auth_type = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
115                         if (auth_type) {
116                                 snprintf(clean_password, sizeof(clean_password),
117                                          "<via Auth-Type = %s>",
118                                          dict_valnamebyattr(PW_AUTH_TYPE, 0,
119                                                             auth_type->vp_integer));
120                         } else {
121                                 strcpy(clean_password, "<no User-Password attribute>");
122                         }
123                 } else if (fr_pair_find_by_num(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) {
124                         strcpy(clean_password, "<CHAP-Password>");
125                 } else {
126                         fr_prints(clean_password, sizeof(clean_password),
127                                   request->password->vp_strvalue, request->password->vp_length, '\0');
128                 }
129         }
130
131         if (goodpass) {
132                 logit = request->root->log_auth_goodpass;
133                 extra_msg = request->root->auth_goodpass_msg;
134         } else {
135                 logit = request->root->log_auth_badpass;
136                 extra_msg = request->root->auth_badpass_msg;
137         }
138
139         if (extra_msg) {
140                 extra[0] = ' ';
141                 p = extra + 1;
142                 if (radius_xlat(p, sizeof(extra) - 1, request, extra_msg, NULL, NULL) < 0) {
143                         return -1;
144                 }
145         } else {
146                 *extra = '\0';
147         }
148
149         RAUTH("%s: [%s%s%s] (%s)%s",
150                        msg,
151                        clean_username,
152                        logit ? "/" : "",
153                        logit ? clean_password : "",
154                        auth_name(buf, sizeof(buf), request, 1),
155                        extra);
156
157         return 0;
158 }
159
160 /*
161  *      Check password.
162  *
163  *      Returns:        0  OK
164  *                      -1 Password fail
165  *                      -2 Rejected (Auth-Type = Reject, send Port-Message back)
166  *                      1  End check & return, don't reply
167  *
168  *      NOTE: NOT the same as the RLM_ values !
169  */
170 static int CC_HINT(nonnull) rad_check_password(REQUEST *request)
171 {
172         vp_cursor_t cursor;
173         VALUE_PAIR *auth_type_pair;
174         int auth_type = -1;
175         int result;
176         int auth_type_count = 0;
177
178         /*
179          *      Look for matching check items. We skip the whole lot
180          *      if the authentication type is PW_AUTH_TYPE_ACCEPT or
181          *      PW_AUTH_TYPE_REJECT.
182          */
183         fr_cursor_init(&cursor, &request->config);
184         while ((auth_type_pair = fr_cursor_next_by_num(&cursor, PW_AUTH_TYPE, 0, TAG_ANY))) {
185                 auth_type = auth_type_pair->vp_integer;
186                 auth_type_count++;
187
188                 RDEBUG2("Found Auth-Type = %s", dict_valnamebyattr(PW_AUTH_TYPE, 0, auth_type));
189                 if (auth_type == PW_AUTH_TYPE_REJECT) {
190                         RDEBUG2("Auth-Type = Reject, rejecting user");
191
192                         return -2;
193                 }
194         }
195
196         /*
197          *      Warn if more than one Auth-Type was found, because only the last
198          *      one found will actually be used.
199          */
200         if ((auth_type_count > 1) && (rad_debug_lvl)) {
201                 RERROR("Warning:  Found %d auth-types on request for user '%s'",
202                         auth_type_count, request->username->vp_strvalue);
203         }
204
205         /*
206          *      This means we have a proxy reply or an accept and it wasn't
207          *      rejected in the above loop. So that means it is accepted and we
208          *      do no further authentication.
209          */
210         if ((auth_type == PW_AUTH_TYPE_ACCEPT)
211 #ifdef WITH_PROXY
212             || (request->proxy)
213 #endif
214             ) {
215                 RDEBUG2("Auth-Type = Accept, accepting the user");
216                 return 0;
217         }
218
219         /*
220          *      Check that Auth-Type has been set, and reject if not.
221          *
222          *      Do quick checks to see if Cleartext-Password or Crypt-Password have
223          *      been set, and complain if so.
224          */
225         if (auth_type < 0) {
226                 if (fr_pair_find_by_num(request->config, PW_CRYPT_PASSWORD, 0, TAG_ANY) != NULL) {
227                         RWDEBUG2("Please update your configuration, and remove 'Auth-Type = Crypt'");
228                         RWDEBUG2("Use the PAP module instead");
229                 }
230                 else if (fr_pair_find_by_num(request->config, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY) != NULL) {
231                         RWDEBUG2("Please update your configuration, and remove 'Auth-Type = Local'");
232                         RWDEBUG2("Use the PAP or CHAP modules instead");
233                 }
234
235                 /*
236                  *      The admin hasn't told us how to
237                  *      authenticate the user, so we reject them!
238                  *
239                  *      This is fail-safe.
240                  */
241
242                 REDEBUG2("No Auth-Type found: rejecting the user via Post-Auth-Type = Reject");
243                 return -2;
244         }
245
246         /*
247          *      See if there is a module that handles
248          *      this Auth-Type, and turn the RLM_ return
249          *      status into the values as defined at
250          *      the top of this function.
251          */
252         result = process_authenticate(auth_type, request);
253         switch (result) {
254         /*
255          *      An authentication module FAIL
256          *      return code, or any return code that
257          *      is not expected from authentication,
258          *      is the same as an explicit REJECT!
259          */
260         case RLM_MODULE_FAIL:
261         case RLM_MODULE_INVALID:
262         case RLM_MODULE_NOOP:
263         case RLM_MODULE_NOTFOUND:
264         case RLM_MODULE_REJECT:
265         case RLM_MODULE_UPDATED:
266         case RLM_MODULE_USERLOCK:
267         default:
268                 result = -1;
269                 break;
270
271         case RLM_MODULE_OK:
272                 result = 0;
273                 break;
274
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         if (request->reply->code == PW_CODE_ACCESS_CHALLENGE) {
295                 fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
296                 vp = pair_make_config("Post-Auth-Type", "Challenge", T_OP_SET);
297                 if (!vp) return RLM_MODULE_OK;
298
299         } else if (request->reply->code == PW_CODE_ACCESS_REJECT) {
300                 fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
301                 vp = pair_make_config("Post-Auth-Type", "Reject", T_OP_SET);
302                 if (!vp) return RLM_MODULE_OK;
303
304         } else {
305                 vp = fr_pair_find_by_num(request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
306         }
307
308         /*
309          *      If a method was chosen, use that.
310          */
311         if (vp) {
312                 postauth_type = vp->vp_integer;
313                 RDEBUG2("Using Post-Auth-Type %s",
314                         dict_valnamebyattr(PW_POST_AUTH_TYPE, 0, postauth_type));
315         }
316
317         result = process_post_auth(postauth_type, request);
318         switch (result) {
319         /*
320          *      The module failed, or said to reject the user: Do so.
321          */
322         case RLM_MODULE_FAIL:
323         case RLM_MODULE_INVALID:
324         case RLM_MODULE_REJECT:
325         case RLM_MODULE_USERLOCK:
326         default:
327                 /*
328                  *      We WERE going to have a nice reply, but
329                  *      something went wrong.  So we've got to run
330                  *      Post-Auth-Type Reject.
331                  */
332                 if (request->reply->code != PW_CODE_ACCESS_REJECT) {
333                         RDEBUG("Using Post-Auth-Type Reject");
334
335                         process_post_auth(PW_POST_AUTH_TYPE_REJECT, request);
336                 }
337
338                 fr_state_discard(request, request->packet);
339                 result = RLM_MODULE_REJECT;
340                 break;
341         /*
342          *      The module handled the request, cancel the reply.
343          */
344         case RLM_MODULE_HANDLED:
345                 /* FIXME */
346                 break;
347         /*
348          *      The module had a number of OK return codes.
349          */
350         case RLM_MODULE_NOOP:
351         case RLM_MODULE_NOTFOUND:
352         case RLM_MODULE_OK:
353         case RLM_MODULE_UPDATED:
354                 result = RLM_MODULE_OK;
355
356                 if (request->reply->code == PW_CODE_ACCESS_CHALLENGE) {
357                         fr_state_put_vps(request, request->packet, request->reply);
358
359                 } else {
360                         fr_state_discard(request, request->packet);
361                 }
362                 break;
363         }
364
365         /*
366          *      Rejects during authorize, etc. are handled by the
367          *      earlier code, which logs a reason for the rejection.
368          *      If the packet is rejected in post-auth, we need to log
369          *      that as a separate reason.
370          */
371         if (result == RLM_MODULE_REJECT) {
372                 if (request->reply->code != RLM_MODULE_REJECT) {
373                         rad_authlog("Rejected in post-auth", request, 0);
374                 }
375                 request->reply->code = PW_CODE_ACCESS_REJECT;
376         }
377
378         /*
379          *      If we're still accepting the user, say so.
380          */
381         if (request->reply->code == PW_CODE_ACCESS_ACCEPT) {
382                 if ((vp = fr_pair_find_by_num(request->packet->vps, PW_MODULE_SUCCESS_MESSAGE, 0, TAG_ANY)) != NULL) {
383                         char msg[MAX_STRING_LEN+12];
384
385                         snprintf(msg, sizeof(msg), "Login OK (%s)",
386                                  vp->vp_strvalue);
387                         rad_authlog(msg, request, 1);
388                 } else {
389                         rad_authlog("Login OK", request, 1);
390                 }
391         }
392
393         return result;
394 }
395
396 /*
397  *      Process and reply to an authentication request
398  *
399  *      The return value of this function isn't actually used right now, so
400  *      it's not entirely clear if it is returning the right things. --Pac.
401  */
402 int rad_authenticate(REQUEST *request)
403 {
404 #ifdef WITH_SESSION_MGMT
405         VALUE_PAIR      *check_item;
406 #endif
407         VALUE_PAIR      *module_msg;
408         VALUE_PAIR      *tmp = NULL;
409         int             result;
410         char            autz_retry = 0;
411         int             autz_type = 0;
412
413 #ifdef WITH_PROXY
414         /*
415          *      If this request got proxied to another server, we need
416          *      to check whether it authenticated the request or not.
417          *
418          *      request->proxy gets set only AFTER authorization, so
419          *      it's safe to check it here.  If it exists, it means
420          *      we're doing a second pass through rad_authenticate().
421          */
422         if (request->proxy) {
423                 int code = 0;
424
425                 if (request->proxy_reply) code = request->proxy_reply->code;
426
427                 switch (code) {
428                 /*
429                  *      Reply of ACCEPT means accept, thus set Auth-Type
430                  *      accordingly.
431                  */
432                 case PW_CODE_ACCESS_ACCEPT:
433                         tmp = radius_pair_create(request,
434                                                 &request->config,
435                                                 PW_AUTH_TYPE, 0);
436                         if (tmp) tmp->vp_integer = PW_AUTH_TYPE_ACCEPT;
437                         goto authenticate;
438
439                 /*
440                  *      Challenges are punted back to the NAS without any
441                  *      further processing.
442                  */
443                 case PW_CODE_ACCESS_CHALLENGE:
444                         request->reply->code = PW_CODE_ACCESS_CHALLENGE;
445                         fr_state_put_vps(request, request->packet, request->reply);
446                         return RLM_MODULE_OK;
447
448                 /*
449                  *      ALL other replies mean reject. (this is fail-safe)
450                  *
451                  *      Do NOT do any authorization or authentication. They
452                  *      are being rejected, so we minimize the amount of work
453                  *      done by the server, by rejecting them here.
454                  */
455                 case PW_CODE_ACCESS_REJECT:
456                         rad_authlog("Login incorrect (Home Server says so)",
457                                     request, 0);
458                         request->reply->code = PW_CODE_ACCESS_REJECT;
459                         fr_state_discard(request, request->packet);
460                         return RLM_MODULE_REJECT;
461
462                 default:
463                         rad_authlog("Login incorrect (Home Server failed to respond)",
464                                     request, 0);
465                         fr_state_discard(request, request->packet);
466                         return RLM_MODULE_REJECT;
467                 }
468         }
469 #endif
470         /*
471          *      Look for, and cache, passwords.
472          */
473         if (!request->password) {
474                 request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
475         }
476         if (!request->password) {
477                 request->password = fr_pair_find_by_num(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
478         }
479
480         /*
481          *      Grab the VPS associated with the State attribute.
482          */
483         fr_state_get_vps(request, request->packet);
484
485         /*
486          *      Get the user's authorization information from the database
487          */
488 autz_redo:
489         result = process_authorize(autz_type, request);
490         switch (result) {
491         case RLM_MODULE_NOOP:
492         case RLM_MODULE_NOTFOUND:
493         case RLM_MODULE_OK:
494         case RLM_MODULE_UPDATED:
495                 break;
496         case RLM_MODULE_HANDLED:
497                 return result;
498         case RLM_MODULE_FAIL:
499         case RLM_MODULE_INVALID:
500         case RLM_MODULE_REJECT:
501         case RLM_MODULE_USERLOCK:
502         default:
503                 if ((module_msg = fr_pair_find_by_num(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL) {
504                         char msg[MAX_STRING_LEN + 16];
505                         snprintf(msg, sizeof(msg), "Invalid user (%s)",
506                                  module_msg->vp_strvalue);
507                         rad_authlog(msg,request,0);
508                 } else {
509                         rad_authlog("Invalid user", request, 0);
510                 }
511                 request->reply->code = PW_CODE_ACCESS_REJECT;
512                 return result;
513         }
514         if (!autz_retry) {
515                 tmp = fr_pair_find_by_num(request->config, PW_AUTZ_TYPE, 0, TAG_ANY);
516                 if (tmp) {
517                         autz_type = tmp->vp_integer;
518                         RDEBUG2("Using Autz-Type %s",
519                                 dict_valnamebyattr(PW_AUTZ_TYPE, 0, autz_type));
520                         autz_retry = 1;
521                         goto autz_redo;
522                 }
523         }
524
525         /*
526          *      If we haven't already proxied the packet, then check
527          *      to see if we should.  Maybe one of the authorize
528          *      modules has decided that a proxy should be used. If
529          *      so, get out of here and send the packet.
530          */
531         if (
532 #ifdef WITH_PROXY
533             (request->proxy == NULL) &&
534 #endif
535             ((tmp = fr_pair_find_by_num(request->config, PW_PROXY_TO_REALM, 0, TAG_ANY)) != NULL)) {
536                 REALM *realm;
537
538                 realm = realm_find2(tmp->vp_strvalue);
539
540                 /*
541                  *      Don't authenticate, as the request is going to
542                  *      be proxied.
543                  */
544                 if (realm && realm->auth_pool) {
545                         return RLM_MODULE_OK;
546                 }
547
548                 /*
549                  *      Catch users who set Proxy-To-Realm to a LOCAL
550                  *      realm (sigh).  But don't complain if it is
551                  *      *the* LOCAL realm.
552                  */
553                 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
554                         RWDEBUG2("You set Proxy-To-Realm = %s, but it is a LOCAL realm!  Cancelling proxy request.", realm->name);
555                 }
556
557                 if (!realm) {
558                         RWDEBUG2("You set Proxy-To-Realm = %s, but the realm does not exist!  Cancelling invalid proxy request.", tmp->vp_strvalue);
559                 }
560         }
561
562 #ifdef WITH_PROXY
563 authenticate:
564 #endif
565
566         /*
567          *      Validate the user
568          */
569         do {
570                 result = rad_check_password(request);
571                 if (result > 0) {
572                         return RLM_MODULE_HANDLED;
573                 }
574
575         } while(0);
576
577         /*
578          *      Failed to validate the user.
579          *
580          *      We PRESUME that the code which failed will clean up
581          *      request->reply->vps, to be ONLY the reply items it
582          *      wants to send back.
583          */
584         if (result < 0) {
585                 RDEBUG2("Failed to authenticate the user");
586                 request->reply->code = PW_CODE_ACCESS_REJECT;
587
588                 if ((module_msg = fr_pair_find_by_num(request->packet->vps, PW_MODULE_FAILURE_MESSAGE, 0, TAG_ANY)) != NULL){
589                         char msg[MAX_STRING_LEN+19];
590
591                         snprintf(msg, sizeof(msg), "Login incorrect (%s)",
592                                  module_msg->vp_strvalue);
593                         rad_authlog(msg, request, 0);
594                 } else {
595                         rad_authlog("Login incorrect", request, 0);
596                 }
597
598                 if (request->password) {
599                         VERIFY_VP(request->password);
600                         /* double check: maybe the secret is wrong? */
601                         if ((rad_debug_lvl > 1) && (request->password->da->attr == PW_USER_PASSWORD)) {
602                                 uint8_t const *p;
603
604                                 p = (uint8_t const *) request->password->vp_strvalue;
605                                 while (*p) {
606                                         int size;
607
608                                         size = fr_utf8_char(p, -1);
609                                         if (!size) {
610                                                 RWDEBUG("Unprintable characters in the password.  Double-check the "
611                                                         "shared secret on the server and the NAS!");
612                                                 break;
613                                         }
614                                         p += size;
615                                 }
616                         }
617                 }
618         }
619
620 #ifdef WITH_SESSION_MGMT
621         if (result >= 0 &&
622             (check_item = fr_pair_find_by_num(request->config, PW_SIMULTANEOUS_USE, 0, TAG_ANY)) != NULL) {
623                 int r, session_type = 0;
624                 char            logstr[1024];
625                 char            umsg[MAX_STRING_LEN + 1];
626
627                 tmp = fr_pair_find_by_num(request->config, PW_SESSION_TYPE, 0, TAG_ANY);
628                 if (tmp) {
629                         session_type = tmp->vp_integer;
630                         RDEBUG2("Using Session-Type %s",
631                                 dict_valnamebyattr(PW_SESSION_TYPE, 0, session_type));
632                 }
633
634                 /*
635                  *      User authenticated O.K. Now we have to check
636                  *      for the Simultaneous-Use parameter.
637                  */
638                 if (request->username &&
639                     (r = process_checksimul(session_type, request, check_item->vp_integer)) != 0) {
640                         char mpp_ok = 0;
641
642                         if (r == 2){
643                                 /* Multilink attempt. Check if port-limit > simultaneous-use */
644                                 VALUE_PAIR *port_limit;
645
646                                 if ((port_limit = fr_pair_find_by_num(request->reply->vps, PW_PORT_LIMIT, 0, TAG_ANY)) != NULL &&
647                                         port_limit->vp_integer > check_item->vp_integer){
648                                         RDEBUG2("MPP is OK");
649                                         mpp_ok = 1;
650                                 }
651                         }
652                         if (!mpp_ok){
653                                 if (check_item->vp_integer > 1) {
654                                         snprintf(umsg, sizeof(umsg), "%s (%u)", main_config.denied_msg,
655                                                  check_item->vp_integer);
656                                 } else {
657                                         strlcpy(umsg, main_config.denied_msg, sizeof(umsg));
658                                 }
659
660                                 request->reply->code = PW_CODE_ACCESS_REJECT;
661
662                                 /*
663                                  *      They're trying to log in too many times.
664                                  *      Remove ALL reply attributes.
665                                  */
666                                 fr_pair_list_free(&request->reply->vps);
667                                 pair_make_reply("Reply-Message", umsg, T_OP_SET);
668
669                                 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
670                                         check_item->vp_integer,
671                                         r == 2 ? "[MPP attempt]" : "");
672                                 rad_authlog(logstr, request, 1);
673
674                                 result = -1;
675                         }
676                 }
677         }
678 #endif
679
680         /*
681          *      Result should be >= 0 here - if not, it means the user
682          *      is rejected, so we just process post-auth and return.
683          */
684         if (result < 0) {
685                 return RLM_MODULE_REJECT;
686         }
687
688         /*
689          *      Set the reply to Access-Accept, if it hasn't already
690          *      been set to something.  (i.e. Access-Challenge)
691          */
692         if (request->reply->code == 0) {
693                 request->reply->code = PW_CODE_ACCESS_ACCEPT;
694         }
695
696         return result;
697 }
698
699 /*
700  *      Run a virtual server auth and postauth
701  *
702  */
703 int rad_virtual_server(REQUEST *request)
704 {
705         VALUE_PAIR *vp;
706         int result;
707
708         RDEBUG("Virtual server %s received request", request->server);
709         rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, NULL);
710
711         if (!request->username) {
712                 request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
713         }
714
715         /*
716          *      Complain about possible issues related to tunnels.
717          */
718         if (request->parent && request->parent->username && request->username) {
719                 /*
720                  *      Look at the full User-Name with realm.
721                  */
722                 if (request->parent->username->da->attr == PW_STRIPPED_USER_NAME) {
723                         vp = fr_pair_find_by_num(request->parent->packet->vps, PW_USER_NAME, 0, TAG_ANY);
724                         rad_assert(vp != NULL);
725                 } else {
726                         vp = request->parent->username;
727                 }
728
729                 /*
730                  *      If the names aren't identical, we do some detailed checks.
731                  */
732                 if (strcmp(vp->vp_strvalue, request->username->vp_strvalue) != 0) {
733                         char const *outer, *inner;
734
735                         outer = strchr(vp->vp_strvalue, '@');
736
737                         /*
738                          *      If there's no realm, or there's a user identifier before
739                          *      the realm name, check the user identifier.
740                          *
741                          *      It SHOULD be "anonymous", or "anonymous@realm"
742                          */
743                         if (outer) {
744                                 if ((outer != vp->vp_strvalue) &&
745                                     ((vp->vp_length < 10) || (memcmp(vp->vp_strvalue, "anonymous@", 10) != 0))) {
746                                         RWDEBUG("Outer User-Name is not anonymized.  User privacy is compromised.");
747                                 } /* else it is anonymized */
748
749                                 /*
750                                  *      Check when there's no realm, and without the trailing '@'
751                                  */
752                         } else if ((vp->vp_length < 9) || (memcmp(vp->vp_strvalue, "anonymous", 9) != 0)) {
753                                         RWDEBUG("Outer User-Name is not anonymized.  User privacy is compromised.");
754
755                         } /* else the user identifier is anonymized */
756
757                         /*
758                          *      Look for an inner realm, which may or may not exist.
759                          */
760                         inner = strchr(request->username->vp_strvalue, '@');
761                         if (outer && inner) {
762                                 outer++;
763                                 inner++;
764
765                                 /*
766                                  *      The realms are different, do
767                                  *      more detailed checks.
768                                  */
769                                 if (strcmp(outer, inner) != 0) {
770                                         size_t outer_len, inner_len;
771
772                                         outer_len = vp->vp_length;
773                                         outer_len -= (outer - vp->vp_strvalue);
774
775                                         inner_len = request->username->vp_length;
776                                         inner_len -= (inner - request->username->vp_strvalue);
777
778                                         /*
779                                          *      Inner: secure.example.org
780                                          *      Outer: example.org
781                                          */
782                                         if (inner_len > outer_len) {
783                                                 char const *suffix;
784
785                                                 suffix = inner + (inner_len - outer_len) - 1;
786
787                                                 if ((*suffix != '.') ||
788                                                     (strcmp(suffix + 1, outer) != 0)) {
789                                                         RWDEBUG("Possible spoofing: Inner realm '%s' is not a subdomain of the outer realm '%s'", inner, outer);
790                                                 }
791
792                                         } else {
793                                                 RWDEBUG("Possible spoofing: Inner realm and outer realms are different");
794                                         }
795                                 }
796                         }
797
798                 } else {
799                         RWDEBUG("Outer and inner identities are the same.  User privacy is compromised.");
800                 }
801         }
802
803         RDEBUG("server %s {", request->server);
804         RINDENT();
805
806         /*
807          *      We currently only handle AUTH packets here.
808          *      This could be expanded to handle other packets as well if required.
809          */
810         rad_assert(request->packet->code == PW_CODE_ACCESS_REQUEST);
811
812         result = rad_authenticate(request);
813
814         if (request->reply->code == PW_CODE_ACCESS_REJECT) {
815                 fr_pair_delete_by_num(&request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
816                 vp = pair_make_config("Post-Auth-Type", "Reject", T_OP_SET);
817                 if (vp) rad_postauth(request);
818         }
819
820         if (request->reply->code == PW_CODE_ACCESS_ACCEPT) {
821                 rad_postauth(request);
822         }
823
824         REXDENT();
825         RDEBUG("} # server %s", request->server);
826
827         RDEBUG("Virtual server sending reply");
828         rdebug_pair_list(L_DBG_LVL_1, request, request->reply->vps, NULL);
829
830         return result;
831 }