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