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