Allow for specifying the path to the group file. Make use of
[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 #include        "libradius.h"
30
31 #include        <stdlib.h>
32 #include        <string.h>
33 #include        <grp.h>
34 #include        <pwd.h>
35
36 #include "config.h"
37
38 #if HAVE_SHADOW_H
39 #  include      <shadow.h>
40 #endif
41
42 #if HAVE_CRYPT_H
43 #  include <crypt.h>
44 #endif
45
46 #ifdef OSFC2
47 #  include      <sys/security.h>
48 #  include      <prot.h>
49 #endif
50
51 #ifdef OSFSIA
52 #  include      <sia.h>
53 #  include      <siad.h>
54 #endif
55
56 #include        "radiusd.h"
57 #include        "modules.h"
58 #include        "sysutmp.h"
59 #include        "cache.h"
60 #include        "conffile.h"
61
62 static char trans[64] =
63    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
64 #define ENC(c) trans[c]
65
66 struct unix_instance {
67         int cache_passwd;
68         const char *passwd_file;
69         const char *shadow_file;
70         const char *group_file;
71         const char *radwtmp;
72         int usegroup;
73         struct pwcache *cache;
74 };
75
76 static CONF_PARSER module_config[] = {
77         /*
78          *      Cache the password by default.
79          */
80         { "cache",    PW_TYPE_BOOLEAN,
81           offsetof(struct unix_instance,cache_passwd), NULL, "yes" },
82         { "passwd",   PW_TYPE_STRING_PTR,
83           offsetof(struct unix_instance,passwd_file), NULL,  NULL },
84         { "shadow",   PW_TYPE_STRING_PTR,
85           offsetof(struct unix_instance,shadow_file), NULL,  NULL },
86         { "group",    PW_TYPE_STRING_PTR,
87           offsetof(struct unix_instance,group_file), NULL,   NULL },
88         { "radwtmp",  PW_TYPE_STRING_PTR,
89           offsetof(struct unix_instance,radwtmp), NULL,   "${logdir}/radwtmp" },
90         { "usegroup", PW_TYPE_BOOLEAN,
91           offsetof(struct unix_instance,usegroup), NULL,     "no" },
92         
93         { NULL, -1, 0, NULL, NULL }             /* end the list */
94 };
95
96 /*
97  * groupcmp is part of autz. But it uses the data from an auth instance. So
98  * here is where it gets it. By default this will be the first configured
99  * auth instance. That can be changed by putting "usegroup = yes" inside an
100  * auth instance to explicitly bind all Group checks to it.
101  */
102
103 /* binds "Group=" to an instance (a particular passwd file) */
104 static struct unix_instance *group_inst;
105
106 /* Tells if the above binding was explicit (usegroup=yes specified in config
107  * file) or not ("Group=" was bound to the first instance of rlm_unix */
108 static int group_inst_explicit;
109
110 struct passwd *fgetpwnam(const char *fname, const char *name) {
111         FILE            *file = fopen(fname, "ro");
112         struct passwd   *pwd = NULL;
113
114         if(file == NULL) return NULL;
115         do {
116                 pwd = fgetpwent(file);
117                 if(pwd == NULL) {
118                         fclose(file);
119                         return NULL;
120                 }
121         } while(strcmp(name, pwd->pw_name) != 0);
122         fclose(file);
123         return pwd;
124 }
125
126 struct passwd *fgetgrnam(const char *fname, const char *name) {
127         FILE            *file = fopen(fname, "ro");
128         struct group    *grp = NULL;
129         if(file == NULL) return NULL;
130         do {
131                 grp = fgetgrent(file);
132                 if(grp == NULL) {
133                         fclose(file);
134                         return NULL;
135                 }
136         } while(strcmp(name, grp->gr_name) != 0);
137         fclose(file);
138         return grp;
139 }
140
141 /*
142  *      The Group = handler.
143  */
144 static int groupcmp(void *instance, VALUE_PAIR *request, VALUE_PAIR *check,
145         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
146 {
147         struct passwd   *pwd;
148         struct group    *grp;
149         char            **member;
150         char            *username;
151         int             retval;
152
153         instance = instance;
154         check_pairs = check_pairs;
155         reply_pairs = reply_pairs;
156
157         if (!group_inst) {
158                 radlog(L_ERR, "groupcmp: no group list known.");
159                 return 1;
160         }
161
162         radlog(L_ERR, "group = %s", group_inst->group_file);
163
164         username = (char *)request->strvalue;
165
166         if (group_inst->cache_passwd &&
167             (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
168                 return retval;
169
170         if ((pwd = fgetpwnam(group_inst->passwd_file, username)) == NULL)
171                 return -1;
172
173         if ((grp = fgetgrnam(group_inst->group_file, (char *)check->strvalue)) == NULL)
174                 return -1;
175
176         retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
177         if (retval < 0) {
178                 for (member = grp->gr_mem; *member && retval; member++) {
179                         if (strcmp(*member, pwd->pw_name) == 0)
180                                 retval = 0;
181                 }
182         }
183         return retval;
184 }
185
186
187 /*
188  *      FIXME:  We really should have an 'init' which makes
189  *      System auth == Unix
190  */
191 static int unix_init(void)
192 {
193         /* FIXME - delay these until a group file has been read so we know
194          * groupcmp can actually do something */
195         paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
196 #ifdef PW_GROUP_NAME /* compat */
197         paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
198 #endif
199         return 0;
200 }
201
202 static int unix_instantiate(CONF_SECTION *conf, void **instance)
203 {
204         struct unix_instance *inst;
205
206         /*
207          *      Allocate room for the instance.
208          */
209         inst = *instance = rad_malloc(sizeof(struct unix_instance));
210
211         /*
212          *      Parse the configuration, failing if we can't do so.
213          */
214         if (cf_section_parse(conf, inst, module_config) < 0) {
215                 free(inst);
216                 return -1;
217         }
218
219         if (inst->cache_passwd) {
220                 radlog(L_INFO, "HASH:  Reinitializing hash structures "
221                         "and lists for caching...");
222                 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
223                                                      inst->shadow_file,
224                                                      inst->group_file))==NULL)
225                 {
226                         radlog(L_ERR, "HASH:  unable to create user "
227                                 "hash table.  disable caching and run debugs");
228                         free((char *) inst->passwd_file);
229                         free((char *) inst->shadow_file);
230                         free((char *) inst->group_file);
231                         free((char *) inst->radwtmp);
232                         free(inst);
233                         return -1;
234                 }
235         } else {
236                 inst->cache = NULL;
237         }
238
239         if (inst->usegroup) {
240                 if (group_inst_explicit) {
241                         radlog(L_ERR, "Only one group list may be active");
242                 } else {
243                         group_inst = inst;
244                         group_inst_explicit = 1;
245                 }
246         } else if (!group_inst) {
247                 group_inst = inst;
248         }
249 #undef inst
250
251         return 0;
252 }
253
254 /*
255  *      Detach.
256  */
257 static int unix_detach(void *instance)
258 {
259 #define inst ((struct unix_instance *)instance)
260         if (group_inst == inst) {
261                 group_inst = NULL;
262                 group_inst_explicit = 0;
263         }
264         free((char *) inst->passwd_file);
265         free((char *) inst->shadow_file);
266         free((char *) inst->group_file);
267         free((char *) inst->radwtmp);
268         if (inst->cache) {
269                 unix_freepwcache(inst->cache);
270         }
271 #undef inst
272         free(instance);
273         return 0;
274 }
275
276 static int unix_destroy(void)
277 {
278         paircompare_unregister(PW_GROUP, groupcmp);
279 #ifdef PW_GROUP_NAME
280         paircompare_unregister(PW_GROUP_NAME, groupcmp);
281 #endif
282         return 0;
283 }
284
285
286 /*
287  *      Check the users password against the standard UNIX
288  *      password table.
289  */
290 static int unix_authenticate(void *instance, REQUEST *request)
291 {
292 #define inst ((struct unix_instance *)instance)
293         char *name, *passwd;
294         struct passwd   *pwd;
295         char            *encpw;
296         char            *encrypted_pass;
297         int             ret;
298 #if HAVE_GETSPNAM
299 #if defined(M_UNIX)
300         struct passwd   *spwd;
301 #else
302         struct spwd     *spwd;
303 #endif
304 #endif
305 #ifdef OSFC2
306         struct pr_passwd *pr_pw;
307 #endif
308 #ifdef OSFSIA
309         char            *info[2];
310         char            *progname = "radius";
311         SIAENTITY       *ent = NULL;
312 #endif
313 #ifdef HAVE_GETUSERSHELL
314         char            *shell;
315 #endif
316
317         /*
318          *      We can only authenticate user requests which HAVE
319          *      a User-Name attribute.
320          */
321         if (!request->username) {
322                 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
323                 return RLM_MODULE_INVALID;
324         }
325
326         /*
327          *      We can only authenticate user requests which HAVE
328          *      a Password attribute.
329          */
330         if (!request->password) {
331                 radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication.");
332                 return RLM_MODULE_INVALID;
333         }
334
335         /*
336          *  Ensure that we're being passed a plain-text password,
337          *  and not anything else.
338          */
339         if (request->password->attribute != PW_PASSWORD) {
340                 radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication.  Cannot use \"%s\".", request->password->name);
341                 return RLM_MODULE_INVALID;
342         }
343
344         name = (char *)request->username->strvalue;
345         passwd = (char *)request->password->strvalue;
346
347         if (inst->cache_passwd &&
348             (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
349                 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
350
351 #ifdef OSFSIA
352         info[0] = progname;
353         info[1] = NULL;
354         if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
355             SIASUCCESS)
356                 return RLM_MODULE_NOTFOUND;
357         if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
358                 if (ret & SIASTOP)
359                         sia_ses_release (&ent);
360                 return RLM_MODULE_NOTFOUND;
361         }
362         if (sia_ses_estab (NULL, ent) == SIASUCCESS) {
363                 sia_ses_release (&ent);
364                 return RLM_MODULE_OK;
365         }
366
367         return RLM_MODULE_NOTFOUND;
368 #else /* OSFSIA */
369 #ifdef OSFC2
370         if ((pr_pw = getprpwnam(name)) == NULL)
371                 return RLM_MODULE_NOTFOUND;
372         encrypted_pass = pr_pw->ufld.fd_encrypt;
373 #else /* OSFC2 */
374         /*
375          *      Get encrypted password from password file
376          */
377         if ((pwd = getpwnam(name)) == NULL) {
378                 return RLM_MODULE_NOTFOUND;
379         }
380         encrypted_pass = pwd->pw_passwd;
381 #endif /* OSFC2 */
382
383 #if HAVE_GETSPNAM
384         /*
385          *      See if there is a shadow password.
386          */
387         if ((spwd = getspnam(name)) != NULL)
388 #if defined(M_UNIX)
389                 encrypted_pass = spwd->pw_passwd;
390 #else
391                 encrypted_pass = spwd->sp_pwdp;
392 #endif  /* M_UNIX */
393 #endif  /* HAVE_GETSPNAM */
394
395 #ifdef DENY_SHELL
396         /*
397          *      Undocumented temporary compatibility for iphil.NET
398          *      Users with a certain shell are always denied access.
399          */
400         if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
401                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
402                 return RLM_MODULE_REJECT;
403         }
404 #endif
405
406 #if HAVE_GETUSERSHELL
407         /*
408          *      Check /etc/shells for a valid shell. If that file
409          *      contains /RADIUSD/ANY/SHELL then any shell will do.
410          */
411         while ((shell = getusershell()) != NULL) {
412                 if (strcmp(shell, pwd->pw_shell) == 0 ||
413                     strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
414                         break;
415                 }
416         }
417         endusershell();
418         if (shell == NULL)
419                 return RLM_MODULE_REJECT;
420 #endif
421
422 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
423         /*
424          *      Check if password has expired.
425          */
426         if (spwd && spwd->sp_expire > 0 &&
427             (request->timestamp / 86400) > spwd->sp_expire) {
428                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
429                 return RLM_MODULE_REJECT;
430         }
431 #endif
432
433 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
434         /*
435          *      Check if password has expired.
436          */
437         if ((pwd->pw_expire > 0) &&
438             (request->timestamp > pwd->pw_expire)) {
439                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
440                 return RLM_MODULE_REJECT;
441         }
442 #endif
443
444 #ifdef OSFC2
445         /*
446          *      Check if account is locked.
447          */
448         if (pr_pw->uflg.fg_lock!=1) {
449                 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
450                 return RLM_MODULE_USERLOCK;
451         }
452 #endif /* OSFC2 */
453
454         /*
455          *      We might have a passwordless account.
456          */
457         if (encrypted_pass[0] == 0)
458                 return RLM_MODULE_OK;
459
460         /*
461          *      Check encrypted password.
462          */
463         encpw = crypt(passwd, encrypted_pass);
464         if (strcmp(encpw, encrypted_pass))
465                 return RLM_MODULE_REJECT;
466
467         return RLM_MODULE_OK;
468 #endif /* OSFSIA */
469 #undef inst
470 }
471
472 /*
473  *      UUencode 4 bits base64. We use this to turn a 4 byte field
474  *      (an IP address) into 6 bytes of ASCII. This is used for the
475  *      wtmp file if we didn't find a short name in the naslist file.
476  */
477 static char *uue(void *in)
478 {
479         int i;
480         static unsigned char res[7];
481         unsigned char *data = (unsigned char *)in;
482
483         res[0] = ENC( data[0] >> 2 );
484         res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
485         res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
486         res[3] = ENC( data[2] & 077 );
487
488         res[4] = ENC( data[3] >> 2 );
489         res[5] = ENC( (data[3] << 4) & 060 );
490         res[6] = 0;
491
492         for(i = 0; i < 6; i++) {
493                 if (res[i] == ' ') res[i] = '`';
494                 if (res[i] < 32 || res[i] > 127)
495                         printf("uue: protocol error ?!\n");
496         }
497         return (char *)res;
498 }
499
500
501 /*
502  *      Unix accounting - write a wtmp file.
503  */
504 static int unix_accounting(void *instance, REQUEST *request)
505 {
506         VALUE_PAIR      *vp;
507         NAS             *cl;
508         FILE            *fp;
509         struct utmp     ut;
510         time_t          t;
511         char            buf[64];
512         const char      *s;
513         int             delay = 0;
514         int             status = -1;
515         int             nas_address = 0;
516         int             framed_address = 0;
517         int             protocol = -1;
518         int             nas_port = 0;
519         int             port_seen = 0;
520         int             nas_port_type = 0;
521         struct unix_instance *inst = (struct unix_instance *) instance;
522
523         /*
524          *      Which type is this.
525          */
526         if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
527                 radlog(L_ERR, "Accounting: no Accounting-Status-Type record.");
528                 return RLM_MODULE_NOOP;
529         }
530         status = vp->lvalue;
531
532         /*
533          *      FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
534          */
535         if (status != PW_STATUS_START &&
536             status != PW_STATUS_STOP)
537                 return RLM_MODULE_NOOP;
538
539         /*
540          *      We're only interested in accounting messages
541          *      with a username in it.
542          */
543         if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
544                 return RLM_MODULE_NOOP;
545
546         t = request->timestamp;
547         memset(&ut, 0, sizeof(ut));
548
549         /*
550          *      First, find the interesting attributes.
551          */
552         for (vp = request->packet->vps; vp; vp = vp->next) {
553                 switch (vp->attribute) {
554                         case PW_USER_NAME:
555                                 if (vp->length >= sizeof(ut.ut_name)) {
556                                         memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
557                                 } else {
558                                         strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
559                                 }
560                                 break;
561                         case PW_LOGIN_IP_HOST:
562                         case PW_FRAMED_IP_ADDRESS:
563                                 framed_address = vp->lvalue;
564                                 break;
565                         case PW_FRAMED_PROTOCOL:
566                                 protocol = vp->lvalue;
567                                 break;
568                         case PW_NAS_IP_ADDRESS:
569                                 nas_address = vp->lvalue;
570                                 break;
571                         case PW_NAS_PORT_ID:
572                                 nas_port = vp->lvalue;
573                                 port_seen = 1;
574                                 break;
575                         case PW_ACCT_DELAY_TIME:
576                                 delay = vp->lvalue;
577                                 break;
578                         case PW_NAS_PORT_TYPE:
579                                 nas_port_type = vp->lvalue;
580                                 break;
581                 }
582         }
583
584         /*
585          *      We don't store !root sessions, or sessions
586          *      where we didn't see a PW_NAS_PORT_ID.
587          */
588         if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
589                 return RLM_MODULE_NOOP;
590
591         /*
592          *      If we didn't find out the NAS address, use the
593          *      originator's IP address.
594          */
595         if (nas_address == 0)
596                 nas_address = request->packet->src_ipaddr;
597
598 #ifdef __linux__
599         /*
600          *      Linux has a field for the client address.
601          */
602         ut.ut_addr = framed_address;
603 #endif
604         /*
605          *      We use the tty field to store the terminal servers' port
606          *      and address so that the tty field is unique.
607          */
608         s = "";
609         if ((cl = nas_find(nas_address)) != NULL)
610                 s = cl->shortname;
611         if (s == NULL || s[0] == 0) s = uue(&(nas_address));
612         sprintf(buf, "%03d:%s", nas_port, s);
613         strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
614
615         /*
616          *      We store the dynamic IP address in the hostname field.
617          */
618 #ifdef UT_HOSTSIZE
619         if (framed_address) {
620                 ip_ntoa(buf, framed_address);
621                 strncpy(ut.ut_host, buf, UT_HOSTSIZE);
622         }
623 #endif
624 #ifdef HAVE_UTMPX_H
625         ut.ut_xtime = t- delay;
626 #else
627         ut.ut_time = t - delay;
628 #endif
629 #ifdef USER_PROCESS
630         /*
631          *      And we can use the ID field to store
632          *      the protocol.
633          */
634         if (protocol == PW_PPP)
635                 strcpy(ut.ut_id, "P");
636         else if (protocol == PW_SLIP)
637                 strcpy(ut.ut_id, "S");
638         else
639                 strcpy(ut.ut_id, "T");
640         ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
641 #endif
642         if (status == PW_STATUS_STOP)
643                 ut.ut_name[0] = 0;
644
645         /*
646          *      Write a RADIUS wtmp log file.
647          *
648          *      Try to open the file if we can't, we don't write the
649          *      wtmp file. If we can try to write. If we fail,
650          *      return RLM_MODULE_FAIL ..
651          */
652         if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
653                 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
654                         fclose(fp);
655                         return RLM_MODULE_FAIL;
656                 }
657                 fclose(fp);
658         } else 
659                 return RLM_MODULE_FAIL;
660
661         return RLM_MODULE_OK;
662 }
663
664 /* globally exported name */
665 module_t rlm_unix = {
666   "System",
667   RLM_TYPE_THREAD_UNSAFE,        /* type: reserved */
668   unix_init,                    /* initialization */
669   unix_instantiate,             /* instantiation */
670   {
671           unix_authenticate,    /* authentication */
672           NULL,                 /* authorization */
673           NULL,                 /* preaccounting */
674           unix_accounting,      /* accounting */
675           NULL                  /* checksimul */
676   },
677   unix_detach,                  /* detach */
678   unix_destroy,                  /* destroy */
679 };
680