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