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