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