move User-Password warning from auth.c to rlm_pap
[freeradius.git] / src / main / auth.c
1 /*
2  * auth.c       User authentication.
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000,2006  The FreeRADIUS server project
21  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
22  * Copyright 2000  Jeff Carneal <jeff@apex.net>
23  */
24
25 #include <freeradius-devel/ident.h>
26 RCSID("$Id$")
27
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
30 #include <freeradius-devel/rad_assert.h>
31
32 #include <ctype.h>
33
34 /*
35  *      Return a short string showing the terminal server, port
36  *      and calling station ID.
37  */
38 char *auth_name(char *buf, size_t buflen, REQUEST *request, int do_cli)
39 {
40         VALUE_PAIR      *cli;
41         VALUE_PAIR      *pair;
42         int             port = 0;
43         const char      *tls = "";
44
45         if ((cli = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0)) == NULL)
46                 do_cli = 0;
47         if ((pair = pairfind(request->packet->vps, PW_NAS_PORT, 0)) != NULL)
48                 port = pair->vp_integer;
49
50         if (request->packet->dst_port == 0) {
51                 if (pairfind(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0)) {
52                         tls = " via TLS tunnel";
53                 } else {
54                         tls = " via proxy to virtual server";
55                 }
56         }
57
58         snprintf(buf, buflen, "from client %.128s port %u%s%.128s%s",
59                         request->client->shortname, port,
60                  (do_cli ? " cli " : ""), (do_cli ? (char *)cli->vp_strvalue : ""),
61                  tls);
62
63         return buf;
64 }
65
66
67
68 /*
69  * Make sure user/pass are clean
70  * and then log them
71  */
72 static int rad_authlog(const char *msg, REQUEST *request, int goodpass)
73 {
74         int logit;
75         const char *extra_msg = NULL;
76         char clean_password[1024];
77         char clean_username[1024];
78         char buf[1024];
79         char extra[1024];
80         VALUE_PAIR *username = NULL;
81
82         if (!request->root->log_auth) {
83                 return 0;
84         }
85
86         /*
87          * Get the correct username based on the configured value
88          */
89         if (log_stripped_names == 0) {
90                 username = pairfind(request->packet->vps, PW_USER_NAME, 0);
91         } else {
92                 username = request->username;
93         }
94
95         /*
96          *      Clean up the username
97          */
98         if (username == NULL) {
99                 strcpy(clean_username, "<no User-Name attribute>");
100         } else {
101                 fr_print_string((char *)username->vp_strvalue,
102                                 username->length,
103                                 clean_username, sizeof(clean_username));
104         }
105
106         /*
107          *      Clean up the password
108          */
109         if (request->root->log_auth_badpass || request->root->log_auth_goodpass) {
110                 if (!request->password) {
111                         VALUE_PAIR *auth_type;
112
113                         auth_type = pairfind(request->config_items,
114                                              PW_AUTH_TYPE, 0);
115                         if (auth_type) {
116                                 snprintf(clean_password, sizeof(clean_password),
117                                          "<via Auth-Type = %s>",
118                                          dict_valnamebyattr(PW_AUTH_TYPE, 0,
119                                                             auth_type->vp_integer));
120                         } else {
121                                 strcpy(clean_password, "<no User-Password attribute>");
122                         }
123                 } else if (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0)) {
124                         strcpy(clean_password, "<CHAP-Password>");
125                 } else {
126                         fr_print_string((char *)request->password->vp_strvalue,
127                                          request->password->length,
128                                          clean_password, sizeof(clean_password));
129                 }
130         }
131
132         if (goodpass) {
133                 logit = request->root->log_auth_goodpass;
134                 extra_msg = request->root->auth_goodpass_msg;
135         } else {
136                 logit = request->root->log_auth_badpass;
137                 extra_msg = request->root->auth_badpass_msg;
138         }
139
140         if (extra_msg) {
141                 extra[0] = ' ';
142                 radius_xlat(extra + 1, sizeof(extra) - 1, extra_msg, request,
143                             NULL);
144         } else {
145                 *extra = '\0';
146         }
147
148         radlog_request(L_AUTH, 0, request, "%s: [%s%s%s] (%s)%s",
149                        msg,
150                        clean_username,
151                        logit ? "/" : "",
152                        logit ? clean_password : "",
153                        auth_name(buf, sizeof(buf), request, 1),
154                        extra);
155
156         return 0;
157 }
158
159 /*
160  *      Check password.
161  *
162  *      Returns:        0  OK
163  *                      -1 Password fail
164  *                      -2 Rejected (Auth-Type = Reject, send Port-Message back)
165  *                      1  End check & return, don't reply
166  *
167  *      NOTE: NOT the same as the RLM_ values !
168  */
169 static int rad_check_password(REQUEST *request)
170 {
171         VALUE_PAIR *auth_type_pair;
172         VALUE_PAIR *cur_config_item;
173         int auth_type = -1;
174         int result;
175         int auth_type_count = 0;
176         result = 0;
177
178         /*
179          *      Look for matching check items. We skip the whole lot
180          *      if the authentication type is PW_AUTHTYPE_ACCEPT or
181          *      PW_AUTHTYPE_REJECT.
182          */
183         cur_config_item = request->config_items;
184         while(((auth_type_pair = pairfind(cur_config_item, PW_AUTH_TYPE, 0))) != NULL) {
185                 auth_type = auth_type_pair->vp_integer;
186                 auth_type_count++;
187
188                 RDEBUG2("Found Auth-Type = %s",
189                         dict_valnamebyattr(PW_AUTH_TYPE, 0, auth_type));
190                 cur_config_item = auth_type_pair->next;
191
192                 if (auth_type == PW_AUTHTYPE_REJECT) {
193                         RDEBUG2("Auth-Type = Reject, rejecting user");
194                         return -2;
195                 }
196         }
197
198         if (( auth_type_count > 1) && (debug_flag)) {
199                 radlog_request(L_ERR, 0, request, "Warning:  Found %d auth-types on request for user '%s'",
200                         auth_type_count, request->username->vp_strvalue);
201         }
202
203         /*
204          *      This means we have a proxy reply or an accept
205          *  and it wasn't rejected in the above loop.  So
206          *  that means it is accepted and we do no further
207          *  authentication
208          */
209         if ((auth_type == PW_AUTHTYPE_ACCEPT)
210 #ifdef WITH_PROXY
211             || (request->proxy)
212 #endif
213             ) {
214                 RDEBUG2("Auth-Type = Accept, accepting the user");
215                 return 0;
216         }
217
218         /*
219          *      Check that Auth-Type has been set, and reject if not.
220          *
221          *      Do quick checks to see if Cleartext-Password or Crypt-Password have
222          *      been set, and complain if so.
223          */
224         if (auth_type < 0) {
225                 if (pairfind(request->config_items, PW_CRYPT_PASSWORD, 0) != NULL) {
226                         RDEBUG2("WARNING: Please update your configuration, and remove 'Auth-Type = Crypt'");
227                         RDEBUG2("WARNING: Use the PAP module instead.");
228                 }
229                 else if (pairfind(request->config_items, PW_CLEARTEXT_PASSWORD, 0) != NULL) {
230                         RDEBUG2("WARNING: Please update your configuration, and remove 'Auth-Type = Local'");
231                         RDEBUG2("WARNING: Use the PAP or CHAP modules instead.");
232                 }
233
234                 /*
235                  *      The admin hasn't told us how to
236                  *      authenticate the user, so we reject them!
237                  *
238                  *      This is fail-safe.
239                  */
240
241                 RDEBUG2("ERROR: No authenticate method (Auth-Type) found for the request: Rejecting the user");
242                 return -2;
243         }
244
245         /*
246          *      See if there is a module that handles
247          *      this Auth-Type, and turn the RLM_ return
248          *      status into the values as defined at
249          *      the top of this function.
250          */
251         result = module_authenticate(auth_type, request);
252         switch (result) {
253                 /*
254                  *      An authentication module FAIL
255                  *      return code, or any return code that
256                  *      is not expected from authentication,
257                  *      is the same as an explicit REJECT!
258                  */
259                 case RLM_MODULE_FAIL:
260                 case RLM_MODULE_INVALID:
261                 case RLM_MODULE_NOOP:
262                 case RLM_MODULE_NOTFOUND:
263                 case RLM_MODULE_REJECT:
264                 case RLM_MODULE_UPDATED:
265                 case RLM_MODULE_USERLOCK:
266                 default:
267                         result = -1;
268                         break;
269                 case RLM_MODULE_OK:
270                         result = 0;
271                         break;
272                 case RLM_MODULE_HANDLED:
273                         result = 1;
274                         break;
275         }
276
277         return result;
278 }
279
280 /*
281  *      Post-authentication step processes the response before it is
282  *      sent to the NAS. It can receive both Access-Accept and Access-Reject
283  *      replies.
284  */
285 int rad_postauth(REQUEST *request)
286 {
287         int     result;
288         int     postauth_type = 0;
289         VALUE_PAIR *vp;
290
291         /*
292          *      Do post-authentication calls. ignoring the return code.
293          */
294         vp = pairfind(request->config_items, PW_POST_AUTH_TYPE, 0);
295         if (vp) {
296                 postauth_type = vp->vp_integer;
297                 RDEBUG2("Using Post-Auth-Type %s",
298                         dict_valnamebyattr(PW_POST_AUTH_TYPE, 0, postauth_type));
299         }
300         result = module_post_auth(postauth_type, request);
301         switch (result) {
302                 /*
303                  *      The module failed, or said to reject the user: Do so.
304                  */
305                 case RLM_MODULE_FAIL:
306                 case RLM_MODULE_INVALID:
307                 case RLM_MODULE_REJECT:
308                 case RLM_MODULE_USERLOCK:
309                 default:
310                         request->reply->code = PW_AUTHENTICATION_REJECT;
311                         result = RLM_MODULE_REJECT;
312                         break;
313                 /*
314                  *      The module handled the request, cancel the reply.
315                  */
316                 case RLM_MODULE_HANDLED:
317                         /* FIXME */
318                         break;
319                 /*
320                  *      The module had a number of OK return codes.
321                  */
322                 case RLM_MODULE_NOOP:
323                 case RLM_MODULE_NOTFOUND:
324                 case RLM_MODULE_OK:
325                 case RLM_MODULE_UPDATED:
326                         result = RLM_MODULE_OK;
327                         break;
328         }
329         return result;
330 }
331
332 /*
333  *      Process and reply to an authentication request
334  *
335  *      The return value of this function isn't actually used right now, so
336  *      it's not entirely clear if it is returning the right things. --Pac.
337  */
338 int rad_authenticate(REQUEST *request)
339 {
340         VALUE_PAIR      *namepair;
341 #ifdef WITH_SESSION_MGMT
342         VALUE_PAIR      *check_item;
343 #endif
344         VALUE_PAIR      *auth_item = NULL;
345         VALUE_PAIR      *module_msg;
346         VALUE_PAIR      *tmp = NULL;
347         int             result;
348         const char      *password;
349         char            autz_retry = 0;
350         int             autz_type = 0;
351
352         password = "";
353
354 #ifdef WITH_PROXY
355         /*
356          *      If this request got proxied to another server, we need
357          *      to check whether it authenticated the request or not.
358          */
359         if (request->proxy_reply) {
360                 switch (request->proxy_reply->code) {
361                 /*
362                  *      Reply of ACCEPT means accept, thus set Auth-Type
363                  *      accordingly.
364                  */
365                 case PW_AUTHENTICATION_ACK:
366                         tmp = radius_paircreate(request,
367                                                 &request->config_items,
368                                                 PW_AUTH_TYPE, 0, PW_TYPE_INTEGER);
369                         if (tmp) tmp->vp_integer = PW_AUTHTYPE_ACCEPT;
370                         goto authenticate;
371
372                 /*
373                  *      Challenges are punted back to the NAS without any
374                  *      further processing.
375                  */
376                 case PW_ACCESS_CHALLENGE:
377                         request->reply->code = PW_ACCESS_CHALLENGE;
378                         return RLM_MODULE_OK;
379                 /*
380                  *      ALL other replies mean reject. (this is fail-safe)
381                  *
382                  *      Do NOT do any authorization or authentication. They
383                  *      are being rejected, so we minimize the amount of work
384                  *      done by the server, by rejecting them here.
385                  */
386                 case PW_AUTHENTICATION_REJECT:
387                         rad_authlog("Login incorrect (Home Server says so)",
388                                     request, 0);
389                         request->reply->code = PW_AUTHENTICATION_REJECT;
390                         return RLM_MODULE_REJECT;
391
392                 default:
393                         rad_authlog("Login incorrect (Home Server failed to respond)",
394                                     request, 0);
395                         return RLM_MODULE_REJECT;
396                 }
397         }
398 #endif
399
400         /*
401          *      Get the username from the request.
402          *
403          *      Note that namepair MAY be NULL, in which case there
404          *      is no User-Name attribute in the request.
405          */
406         namepair = request->username;
407
408         /*
409          *      Look for, and cache, passwords.
410          */
411         if (!request->password) {
412                 request->password = pairfind(request->packet->vps,
413                                              PW_USER_PASSWORD, 0);
414         }
415
416         /*
417          *      Discover which password we want to use.
418          */
419         auth_item = request->password;
420         if (auth_item) {
421                 password = (const char *)auth_item->vp_strvalue;
422
423         } else {
424                 /*
425                  *      Maybe there's a CHAP-Password?
426                  */
427                 if ((auth_item = pairfind(request->packet->vps,
428                                           PW_CHAP_PASSWORD, 0)) != NULL) {
429                         password = "<CHAP-PASSWORD>";
430
431                 } else {
432                         /*
433                          *      No password we recognize.
434                          */
435                         password = "<NO-PASSWORD>";
436                 }
437         }
438         request->password = auth_item;
439
440         /*
441          *      Get the user's authorization information from the database
442          */
443 autz_redo:
444         result = module_authorize(autz_type, request);
445         switch (result) {
446                 case RLM_MODULE_NOOP:
447                 case RLM_MODULE_NOTFOUND:
448                 case RLM_MODULE_OK:
449                 case RLM_MODULE_UPDATED:
450                         break;
451                 case RLM_MODULE_HANDLED:
452                         return result;
453                 case RLM_MODULE_FAIL:
454                 case RLM_MODULE_INVALID:
455                 case RLM_MODULE_REJECT:
456                 case RLM_MODULE_USERLOCK:
457                 default:
458                         if ((module_msg = pairfind(request->packet->vps,
459                                                    PW_MODULE_FAILURE_MESSAGE, 0)) != NULL) {
460                                 char msg[MAX_STRING_LEN + 16];
461                                 snprintf(msg, sizeof(msg), "Invalid user (%s)",
462                                          module_msg->vp_strvalue);
463                                 rad_authlog(msg,request,0);
464                         } else {
465                                 rad_authlog("Invalid user", request, 0);
466                         }
467                         request->reply->code = PW_AUTHENTICATION_REJECT;
468                         return result;
469         }
470         if (!autz_retry) {
471                 tmp = pairfind(request->config_items, PW_AUTZ_TYPE, 0);
472                 if (tmp) {
473                         autz_type = tmp->vp_integer;
474                         RDEBUG2("Using Autz-Type %s",
475                                 dict_valnamebyattr(PW_AUTZ_TYPE, 0, autz_type));
476                         autz_retry = 1;
477                         goto autz_redo;
478                 }
479         }
480
481         /*
482          *      If we haven't already proxied the packet, then check
483          *      to see if we should.  Maybe one of the authorize
484          *      modules has decided that a proxy should be used. If
485          *      so, get out of here and send the packet.
486          */
487         if (
488 #ifdef WITH_PROXY
489             (request->proxy == NULL) &&
490 #endif
491             ((tmp = pairfind(request->config_items, PW_PROXY_TO_REALM, 0)) != NULL)) {
492                 REALM *realm;
493
494                 realm = realm_find2(tmp->vp_strvalue);
495
496                 /*
497                  *      Don't authenticate, as the request is going to
498                  *      be proxied.
499                  */
500                 if (realm && realm->auth_pool) {
501                         return RLM_MODULE_OK;
502                 }
503
504                 /*
505                  *      Catch users who set Proxy-To-Realm to a LOCAL
506                  *      realm (sigh).  But don't complain if it is
507                  *      *the* LOCAL realm.
508                  */
509                 if (realm &&(strcmp(realm->name, "LOCAL") != 0)) {
510                         RDEBUG2("WARNING: You set Proxy-To-Realm = %s, but it is a LOCAL realm!  Cancelling proxy request.", realm->name);
511                 }
512
513                 if (!realm) {
514                         RDEBUG2("WARNING: You set Proxy-To-Realm = %s, but the realm does not exist!  Cancelling invalid proxy request.", tmp->vp_strvalue);
515                 }
516         }
517
518 #ifdef WITH_PROXY
519  authenticate:
520 #endif
521
522         /*
523          *      Perhaps there is a Stripped-User-Name now.
524          */
525         namepair = request->username;
526
527         /*
528          *      Validate the user
529          */
530         do {
531                 result = rad_check_password(request);
532                 if (result > 0) {
533                         /* don't reply! */
534                         return RLM_MODULE_HANDLED;
535                 }
536         } while(0);
537
538         /*
539          *      Failed to validate the user.
540          *
541          *      We PRESUME that the code which failed will clean up
542          *      request->reply->vps, to be ONLY the reply items it
543          *      wants to send back.
544          */
545         if (result < 0) {
546                 RDEBUG2("Failed to authenticate the user.");
547                 request->reply->code = PW_AUTHENTICATION_REJECT;
548
549                 if ((module_msg = pairfind(request->packet->vps,PW_MODULE_FAILURE_MESSAGE, 0)) != NULL){
550                         char msg[MAX_STRING_LEN+19];
551
552                         snprintf(msg, sizeof(msg), "Login incorrect (%s)",
553                                  module_msg->vp_strvalue);
554                         rad_authlog(msg, request, 0);
555                 } else {
556                         rad_authlog("Login incorrect", request, 0);
557                 }
558
559                 /* double check: maybe the secret is wrong? */
560                 if ((debug_flag > 1) && (auth_item != NULL) &&
561                                 (auth_item->attribute == PW_USER_PASSWORD)) {
562                         uint8_t *p;
563
564                         p = (uint8_t *) auth_item->vp_strvalue;
565                         while (*p) {
566                                 int size;
567
568                                 size = fr_utf8_char(p);
569                                 if (!size) {
570                                         log_debug("  WARNING: Unprintable characters in the password.  Double-check the shared secret on the server and the NAS!");
571                                         break;
572                                 }
573                                 p += size;
574                         }
575                 }
576         }
577
578 #ifdef WITH_SESSION_MGMT
579         if (result >= 0 &&
580             (check_item = pairfind(request->config_items, PW_SIMULTANEOUS_USE, 0)) != NULL) {
581                 int r, session_type = 0;
582                 char            logstr[1024];
583                 char            umsg[MAX_STRING_LEN + 1];
584                 const char      *user_msg = NULL;
585
586                 tmp = pairfind(request->config_items, PW_SESSION_TYPE, 0);
587                 if (tmp) {
588                         session_type = tmp->vp_integer;
589                         RDEBUG2("Using Session-Type %s",
590                                 dict_valnamebyattr(PW_SESSION_TYPE, 0, session_type));
591                 }
592
593                 /*
594                  *      User authenticated O.K. Now we have to check
595                  *      for the Simultaneous-Use parameter.
596                  */
597                 if (namepair &&
598                     (r = module_checksimul(session_type, request, check_item->vp_integer)) != 0) {
599                         char mpp_ok = 0;
600
601                         if (r == 2){
602                                 /* Multilink attempt. Check if port-limit > simultaneous-use */
603                                 VALUE_PAIR *port_limit;
604
605                                 if ((port_limit = pairfind(request->reply->vps, PW_PORT_LIMIT, 0)) != NULL &&
606                                         port_limit->vp_integer > check_item->vp_integer){
607                                         RDEBUG2("MPP is OK");
608                                         mpp_ok = 1;
609                                 }
610                         }
611                         if (!mpp_ok){
612                                 if (check_item->vp_integer > 1) {
613                                 snprintf(umsg, sizeof(umsg),
614                                                         "\r\nYou are already logged in %d times  - access denied\r\n\n",
615                                                         (int)check_item->vp_integer);
616                                         user_msg = umsg;
617                                 } else {
618                                         user_msg = "\r\nYou are already logged in - access denied\r\n\n";
619                                 }
620
621                                 request->reply->code = PW_AUTHENTICATION_REJECT;
622
623                                 /*
624                                  *      They're trying to log in too many times.
625                                  *      Remove ALL reply attributes.
626                                  */
627                                 pairfree(&request->reply->vps);
628                                 radius_pairmake(request, &request->reply->vps,
629                                                 "Reply-Message",
630                                                 user_msg, T_OP_SET);
631
632                                 snprintf(logstr, sizeof(logstr), "Multiple logins (max %d) %s",
633                                         check_item->vp_integer,
634                                         r == 2 ? "[MPP attempt]" : "");
635                                 rad_authlog(logstr, request, 1);
636
637                                 result = -1;
638                         }
639                 }
640         }
641 #endif
642
643         /*
644          *      Result should be >= 0 here - if not, it means the user
645          *      is rejected, so we just process post-auth and return.
646          */
647         if (result < 0) {
648                 return RLM_MODULE_REJECT;
649         }
650
651         /*
652          *      Set the reply to Access-Accept, if it hasn't already
653          *      been set to something.  (i.e. Access-Challenge)
654          */
655         if (request->reply->code == 0)
656           request->reply->code = PW_AUTHENTICATION_ACK;
657
658         if ((module_msg = pairfind(request->packet->vps,PW_MODULE_SUCCESS_MESSAGE, 0)) != NULL){
659                 char msg[MAX_STRING_LEN+12];
660
661                 snprintf(msg, sizeof(msg), "Login OK (%s)",
662                          module_msg->vp_strvalue);
663                 rad_authlog(msg, request, 1);
664         } else {
665                 rad_authlog("Login OK", request, 1);
666         }
667
668         return result;
669 }
670
671 /*
672  *      Run a virtual server auth and postauth
673  *
674  */
675 int rad_virtual_server(REQUEST *request)
676 {
677         VALUE_PAIR *vp;
678         int result;
679
680         /*
681          *      We currently only handle AUTH packets here.
682          *      This could be expanded to handle other packets as well if required.
683          */
684         rad_assert(request->packet->code == PW_AUTHENTICATION_REQUEST);
685
686         result = rad_authenticate(request);
687
688         if (request->reply->code == PW_AUTHENTICATION_REJECT) {
689                 pairdelete(&request->config_items, PW_POST_AUTH_TYPE, 0);
690                 vp = radius_pairmake(request, &request->config_items,
691                                      "Post-Auth-Type", "Reject",
692                                      T_OP_SET);
693                 if (vp) rad_postauth(request);
694         }
695
696         if (request->reply->code == PW_AUTHENTICATION_ACK) {
697                 rad_postauth(request);
698         }
699
700         return result;
701 }
702