Move RADCLIENT* in mainconfig to rbtree's. This means that
[freeradius.git] / src / modules / rlm_unix / rlm_unix.c
1 /*
2  * rlm_unix.c   authentication: Unix user authentication
3  *              accounting:     Functions to write radwtmp file.
4  *              Also contains handler for "Group".
5  *
6  * Version:     $Id$
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * Copyright 2000  The FreeRADIUS server project
23  * Copyright 2000  Jeff Carneal <jeff@apex.net>
24  * Copyright 2000  Alan Curry <pacman@world.std.com>
25  */
26 static const char rcsid[] = "$Id$";
27
28 #include        "autoconf.h"
29
30 #include        <stdlib.h>
31 #include        <string.h>
32 #include        <grp.h>
33 #include        <pwd.h>
34 #include        <sys/types.h>
35 #include        <sys/stat.h>
36
37 #include "config.h"
38
39 #ifdef HAVE_SHADOW_H
40 #  include      <shadow.h>
41 #endif
42
43 #ifdef OSFC2
44 #  include      <sys/security.h>
45 #  include      <prot.h>
46 #endif
47
48 #ifdef OSFSIA
49 #  include      <sia.h>
50 #  include      <siad.h>
51 #endif
52
53 #include        "radiusd.h"
54 #include        "modules.h"
55 #include        "sysutmp.h"
56
57 static char trans[64] =
58    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
59 #define ENC(c) trans[c]
60
61 struct unix_instance {
62         const char *radwtmp;
63 };
64
65 static const CONF_PARSER module_config[] = {
66         { "radwtmp",  PW_TYPE_STRING_PTR,
67           offsetof(struct unix_instance,radwtmp), NULL,   "NULL" },
68
69         { NULL, -1, 0, NULL, NULL }             /* end the list */
70 };
71
72
73 /*
74  *      The Group = handler.
75  */
76 static int groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request,
77                     VALUE_PAIR *check, VALUE_PAIR *check_pairs,
78                     VALUE_PAIR **reply_pairs)
79 {
80         struct passwd   *pwd;
81         struct group    *grp;
82         char            **member;
83         char            *username;
84         int             retval;
85         VALUE_PAIR      *vp;
86
87         instance = instance;
88         check_pairs = check_pairs;
89         reply_pairs = reply_pairs;
90
91         /*
92          *      No user name, doesn't compare.
93          */
94         vp = pairfind(request, PW_STRIPPED_USER_NAME);
95         if (!vp) {
96                 vp = pairfind(request, PW_USER_NAME);
97                 if (!vp) {
98                         return -1;
99                 }
100         }
101         username = (char *)vp->strvalue;
102
103         pwd = getpwnam(username);
104         if (pwd == NULL)
105                 return -1;
106
107         grp = getgrnam((char *)check->strvalue);
108         if (grp == NULL)
109                 return -1;
110
111         retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
112         if (retval < 0) {
113                 for (member = grp->gr_mem; *member && retval; member++) {
114                         if (strcmp(*member, pwd->pw_name) == 0)
115                                 retval = 0;
116                 }
117         }
118         return retval;
119 }
120
121
122 /*
123  *      Done once when the module is loaded, and NOT on a per-instance
124  *      basis.
125  */
126 static int unix_init(void)
127 {
128         /* FIXME - delay these until a group file has been read so we know
129          * groupcmp can actually do something */
130         paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
131 #ifdef PW_GROUP_NAME /* compat */
132         paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
133 #endif
134         return 0;
135 }
136
137
138 /*
139  *      Detach.
140  */
141 static int unix_detach(void *instance)
142 {
143 #define inst ((struct unix_instance *)instance)
144         if (inst->radwtmp)
145                 free(inst->radwtmp);
146 #undef inst
147         free(instance);
148         return 0;
149 }
150
151 /*
152  *      Read the config
153  */
154 static int unix_instantiate(CONF_SECTION *conf, void **instance)
155 {
156         struct unix_instance *inst;
157
158         /*
159          *      Allocate room for the instance.
160          */
161         inst = *instance = rad_malloc(sizeof(*inst));
162         if (!inst) {
163                 return -1;
164         }
165         memset(inst, 0, sizeof(*inst));
166
167         /*
168          *      Parse the configuration, failing if we can't do so.
169          */
170         if (cf_section_parse(conf, inst, module_config) < 0) {
171                 unix_detach(inst);
172                 return -1;
173         }
174
175 #undef inst
176
177         return 0;
178 }
179
180
181
182 static int unix_destroy(void)
183 {
184         paircompare_unregister(PW_GROUP, groupcmp);
185 #ifdef PW_GROUP_NAME
186         paircompare_unregister(PW_GROUP_NAME, groupcmp);
187 #endif
188         return 0;
189 }
190
191
192 /*
193  *      Pull the users password from where-ever, and add it to
194  *      the given vp list.
195  */
196 static int unix_getpw(void *instance, REQUEST *request, VALUE_PAIR **vp_list)
197 {
198         const char      *name;
199         const char      *encrypted_pass;
200 #ifdef HAVE_GETSPNAM
201         struct spwd     *spwd = NULL;
202 #endif
203 #ifdef OSFC2
204         struct pr_passwd *pr_pw;
205 #else
206         struct passwd   *pwd;
207 #endif
208 #ifdef HAVE_GETUSERSHELL
209         char            *shell;
210 #endif
211         VALUE_PAIR      *vp;
212
213         /*
214          *      We can only authenticate user requests which HAVE
215          *      a User-Name attribute.
216          */
217         if (!request->username) {
218                 return RLM_MODULE_NOOP;
219         }
220
221         name = (char *)request->username->strvalue;
222         encrypted_pass = NULL;
223
224 #ifdef OSFC2
225         if ((pr_pw = getprpwnam(name)) == NULL)
226                 return RLM_MODULE_NOTFOUND;
227         encrypted_pass = pr_pw->ufld.fd_encrypt;
228
229         /*
230          *      Check if account is locked.
231          */
232         if (pr_pw->uflg.fg_lock!=1) {
233                 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
234                 return RLM_MODULE_USERLOCK;
235         }
236 #else /* OSFC2 */
237         if ((pwd = getpwnam(name)) == NULL) {
238                 return RLM_MODULE_NOTFOUND;
239         }
240         encrypted_pass = pwd->pw_passwd;
241 #endif /* OSFC2 */
242
243 #ifdef HAVE_GETSPNAM
244         /*
245          *      See if there is a shadow password.
246          *
247          *      Only query the _system_ shadow file if the encrypted
248          *      password from the passwd file is < 10 characters (i.e.
249          *      a valid password would never crypt() to it).  This will
250          *      prevents users from using NULL password fields as things
251          *      stand right now.
252          */
253         if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
254                 if ((spwd = getspnam(name)) == NULL) {
255                         return RLM_MODULE_NOTFOUND;
256                 }
257                 encrypted_pass = spwd->sp_pwdp;
258         }
259 #endif  /* HAVE_GETSPNAM */
260
261 /*
262  *      These require 'pwd != NULL', which isn't true on OSFC2
263  */
264 #ifndef OSFC2
265 #ifdef DENY_SHELL
266         /*
267          *      Users with a particular shell are denied access
268          */
269         if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
270                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
271                 return RLM_MODULE_REJECT;
272         }
273 #endif
274
275 #ifdef HAVE_GETUSERSHELL
276         /*
277          *      Check /etc/shells for a valid shell. If that file
278          *      contains /RADIUSD/ANY/SHELL then any shell will do.
279          */
280         while ((shell = getusershell()) != NULL) {
281                 if (strcmp(shell, pwd->pw_shell) == 0 ||
282                     strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
283                         break;
284                 }
285         }
286         endusershell();
287         if (shell == NULL) {
288                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
289                        name, pwd->pw_shell);
290                 return RLM_MODULE_REJECT;
291         }
292 #endif
293 #endif /* OSFC2 */
294
295 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
296         /*
297          *      Check if password has expired.
298          */
299         if (spwd && spwd->sp_expire > 0 &&
300             (request->timestamp / 86400) > spwd->sp_expire) {
301                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
302                 return RLM_MODULE_REJECT;
303         }
304 #endif
305
306 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
307         /*
308          *      Check if password has expired.
309          */
310         if ((pwd->pw_expire > 0) &&
311             (request->timestamp > pwd->pw_expire)) {
312                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
313                 return RLM_MODULE_REJECT;
314         }
315 #endif
316
317         /*
318          *      We might have a passwordless account.
319          *
320          *      FIXME: Maybe add Auth-Type := Accept?
321          */
322         if (encrypted_pass[0] == 0)
323                 return RLM_MODULE_NOOP;
324
325         vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
326         if (!vp) return RLM_MODULE_FAIL;
327
328         pairmove(vp_list, &vp);
329         pairfree(&vp);          /* might not be NULL; */
330
331         return RLM_MODULE_UPDATED;
332 }
333
334
335 /*
336  *      Pull the users password from where-ever, and add it to
337  *      the given vp list.
338  */
339 static int unix_authorize(void *instance, REQUEST *request)
340 {
341         return unix_getpw(instance, request, &request->config_items);
342 }
343
344 /*
345  *      Pull the users password from where-ever, and add it to
346  *      the given vp list.
347  */
348 static int unix_authenticate(void *instance, REQUEST *request)
349 {
350 #ifdef OSFSIA
351         char            *info[2];
352         char            *progname = "radius";
353         SIAENTITY       *ent = NULL;
354
355         info[0] = progname;
356         info[1] = NULL;
357         if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
358             SIASUCCESS)
359                 return RLM_MODULE_NOTFOUND;
360         if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
361                 if (ret & SIASTOP)
362                         sia_ses_release (&ent);
363                 return RLM_MODULE_NOTFOUND;
364         }
365         if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
366                 sia_ses_release (&ent);
367                 return RLM_MODULE_NOTFOUND;
368         }
369 #else  /* OSFSIA */
370         int rcode;
371         VALUE_PAIR *vp = NULL;
372
373         if (!request->password ||
374             (request->password->attribute != PW_USER_PASSWORD)) {
375                 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
376                 return RLM_MODULE_INVALID;
377         }
378
379         rcode = unix_getpw(instance, request, &vp);
380         if (rcode != RLM_MODULE_UPDATED) return rcode;
381
382         /*
383          *      0 means "ok"
384          */
385         if (lrad_crypt_check((char *) request->password->strvalue,
386                              (char *) vp->strvalue) != 0) {
387                 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
388                        request->username->strvalue);
389                 return RLM_MODULE_REJECT;
390         }
391 #endif /* OSFFIA */
392
393         return RLM_MODULE_OK;
394 }
395
396
397 /*
398  *      UUencode 4 bits base64. We use this to turn a 4 byte field
399  *      (an IP address) into 6 bytes of ASCII. This is used for the
400  *      wtmp file if we didn't find a short name in the naslist file.
401  */
402 static char *uue(void *in)
403 {
404         int i;
405         static unsigned char res[7];
406         unsigned char *data = (unsigned char *)in;
407
408         res[0] = ENC( data[0] >> 2 );
409         res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
410         res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
411         res[3] = ENC( data[2] & 077 );
412
413         res[4] = ENC( data[3] >> 2 );
414         res[5] = ENC( (data[3] << 4) & 060 );
415         res[6] = 0;
416
417         for(i = 0; i < 6; i++) {
418                 if (res[i] == ' ') res[i] = '`';
419                 if (res[i] < 32 || res[i] > 127)
420                         printf("uue: protocol error ?!\n");
421         }
422         return (char *)res;
423 }
424
425
426 /*
427  *      Unix accounting - write a wtmp file.
428  */
429 static int unix_accounting(void *instance, REQUEST *request)
430 {
431         VALUE_PAIR      *vp;
432         FILE            *fp;
433         struct utmp     ut;
434         time_t          t;
435         char            buf[64];
436         const char      *s;
437         int             delay = 0;
438         int             status = -1;
439         int             nas_address = 0;
440         int             framed_address = 0;
441         int             protocol = -1;
442         int             nas_port = 0;
443         int             port_seen = 0;
444         int             nas_port_type = 0;
445         struct unix_instance *inst = (struct unix_instance *) instance;
446
447         /*
448          *      No radwtmp.  Don't do anything.
449          */
450         if (!inst->radwtmp) {
451                 DEBUG2("rlm_unix: No radwtmp file configured.  Ignoring accounting request.");
452                 return RLM_MODULE_NOOP;
453         }
454
455         if (request->packet->src_ipaddr.af != AF_INET) {
456                 DEBUG2("rlm_unix: IPv6 is not supported!");
457                 return RLM_MODULE_NOOP;
458         }
459
460         /*
461          *      Which type is this.
462          */
463         if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
464                 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
465                 return RLM_MODULE_NOOP;
466         }
467         status = vp->lvalue;
468
469         /*
470          *      FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
471          */
472         if (status != PW_STATUS_START &&
473             status != PW_STATUS_STOP)
474                 return RLM_MODULE_NOOP;
475
476         /*
477          *      We're only interested in accounting messages
478          *      with a username in it.
479          */
480         if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
481                 return RLM_MODULE_NOOP;
482
483         t = request->timestamp;
484         memset(&ut, 0, sizeof(ut));
485
486         /*
487          *      First, find the interesting attributes.
488          */
489         for (vp = request->packet->vps; vp; vp = vp->next) {
490                 switch (vp->attribute) {
491                         case PW_USER_NAME:
492                                 if (vp->length >= sizeof(ut.ut_name)) {
493                                         memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
494                                 } else {
495                                         strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
496                                 }
497                                 break;
498                         case PW_LOGIN_IP_HOST:
499                         case PW_FRAMED_IP_ADDRESS:
500                                 framed_address = vp->lvalue;
501                                 break;
502                         case PW_FRAMED_PROTOCOL:
503                                 protocol = vp->lvalue;
504                                 break;
505                         case PW_NAS_IP_ADDRESS:
506                                 nas_address = vp->lvalue;
507                                 break;
508                         case PW_NAS_PORT:
509                                 nas_port = vp->lvalue;
510                                 port_seen = 1;
511                                 break;
512                         case PW_ACCT_DELAY_TIME:
513                                 delay = vp->lvalue;
514                                 break;
515                         case PW_NAS_PORT_TYPE:
516                                 nas_port_type = vp->lvalue;
517                                 break;
518                 }
519         }
520
521         /*
522          *      We don't store !root sessions, or sessions
523          *      where we didn't see a NAS-Port attribute.
524          */
525         if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
526                 return RLM_MODULE_NOOP;
527
528         s = "";
529
530         /*
531          *      If we didn't find out the NAS address, use the
532          *      originator's IP address.
533          */
534         if (nas_address == 0) {
535                 RADCLIENT *cl;
536                 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
537                 
538                 if ((cl = client_find_old(&request->packet->src_ipaddr)) != NULL)
539                         s = cl->shortname;
540         }
541         if (!s || s[0] == 0) s = uue(&(nas_address));
542         
543 #ifdef __linux__
544         /*
545          *      Linux has a field for the client address.
546          */
547         ut.ut_addr = framed_address;
548 #endif
549         /*
550          *      We use the tty field to store the terminal servers' port
551          *      and address so that the tty field is unique.
552          */
553         sprintf(buf, "%03d:%s", nas_port, s);
554         strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
555
556         /*
557          *      We store the dynamic IP address in the hostname field.
558          */
559 #ifdef UT_HOSTSIZE
560         if (framed_address) {
561                 ip_ntoa(buf, framed_address);
562                 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
563         }
564 #endif
565 #ifdef HAVE_UTMPX_H
566         ut.ut_xtime = t- delay;
567 #else
568         ut.ut_time = t - delay;
569 #endif
570 #ifdef USER_PROCESS
571         /*
572          *      And we can use the ID field to store
573          *      the protocol.
574          */
575         if (protocol == PW_PPP)
576                 strcpy(ut.ut_id, "P");
577         else if (protocol == PW_SLIP)
578                 strcpy(ut.ut_id, "S");
579         else
580                 strcpy(ut.ut_id, "T");
581         ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
582 #endif
583         if (status == PW_STATUS_STOP)
584                 ut.ut_name[0] = 0;
585
586         /*
587          *      Write a RADIUS wtmp log file.
588          *
589          *      Try to open the file if we can't, we don't write the
590          *      wtmp file. If we can try to write. If we fail,
591          *      return RLM_MODULE_FAIL ..
592          */
593         if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
594                 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
595                         fclose(fp);
596                         return RLM_MODULE_FAIL;
597                 }
598                 fclose(fp);
599         } else
600                 return RLM_MODULE_FAIL;
601
602         return RLM_MODULE_OK;
603 }
604
605 /* globally exported name */
606 module_t rlm_unix = {
607   "System",
608   RLM_TYPE_THREAD_UNSAFE,        /* type */
609   unix_init,                    /* initialization */
610   unix_instantiate,             /* instantiation */
611   {
612           unix_authenticate,    /* authentication */
613           unix_authorize,       /* authorization */
614           NULL,                 /* preaccounting */
615           unix_accounting,      /* accounting */
616           NULL,                  /* checksimul */
617           NULL,                 /* pre-proxy */
618           NULL,                 /* post-proxy */
619           NULL                  /* post-auth */
620   },
621   unix_detach,                  /* detach */
622   unix_destroy,                  /* destroy */
623 };