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