Add OSFFIA stuff back in. It looks like it's not used, so it
[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 #ifdef OSFSIA
352         char            *info[2];
353         char            *progname = "radius";
354         SIAENTITY       *ent = NULL;
355
356         info[0] = progname;
357         info[1] = NULL;
358         if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
359             SIASUCCESS)
360                 return RLM_MODULE_NOTFOUND;
361         if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
362                 if (ret & SIASTOP)
363                         sia_ses_release (&ent);
364                 return RLM_MODULE_NOTFOUND;
365         }
366         if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
367                 sia_ses_release (&ent);
368                 return RLM_MODULE_NOTFOUND;
369         }
370 #else  /* OSFSIA */
371         int rcode;
372         VALUE_PAIR *vp = NULL;
373
374         if (!request->password ||
375             (request->password->attribute != PW_USER_PASSWORD)) {
376                 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
377                 return RLM_MODULE_INVALID;
378         }
379
380         rcode = unix_getpw(instance, request, &vp);
381         if (rcode != RLM_MODULE_UPDATED) return rcode;
382
383         /*
384          *      0 means "ok"
385          */
386         if (lrad_crypt_check((char *) request->password->strvalue,
387                              (char *) vp->strvalue) != 0) {
388                 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
389                        request->username->strvalue);
390                 return RLM_MODULE_REJECT;
391         }
392 #endif /* OSFFIA */
393
394         return RLM_MODULE_OK;
395 }
396
397
398 /*
399  *      UUencode 4 bits base64. We use this to turn a 4 byte field
400  *      (an IP address) into 6 bytes of ASCII. This is used for the
401  *      wtmp file if we didn't find a short name in the naslist file.
402  */
403 static char *uue(void *in)
404 {
405         int i;
406         static unsigned char res[7];
407         unsigned char *data = (unsigned char *)in;
408
409         res[0] = ENC( data[0] >> 2 );
410         res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
411         res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
412         res[3] = ENC( data[2] & 077 );
413
414         res[4] = ENC( data[3] >> 2 );
415         res[5] = ENC( (data[3] << 4) & 060 );
416         res[6] = 0;
417
418         for(i = 0; i < 6; i++) {
419                 if (res[i] == ' ') res[i] = '`';
420                 if (res[i] < 32 || res[i] > 127)
421                         printf("uue: protocol error ?!\n");
422         }
423         return (char *)res;
424 }
425
426
427 /*
428  *      Unix accounting - write a wtmp file.
429  */
430 static int unix_accounting(void *instance, REQUEST *request)
431 {
432         VALUE_PAIR      *vp;
433         RADCLIENT       *cl;
434         FILE            *fp;
435         struct utmp     ut;
436         time_t          t;
437         char            buf[64];
438         const char      *s;
439         int             delay = 0;
440         int             status = -1;
441         int             nas_address = 0;
442         int             framed_address = 0;
443         int             protocol = -1;
444         int             nas_port = 0;
445         int             port_seen = 0;
446         int             nas_port_type = 0;
447         struct unix_instance *inst = (struct unix_instance *) instance;
448
449         /*
450          *      No radwtmp.  Don't do anything.
451          */
452         if (!inst->radwtmp) {
453                 DEBUG2("rlm_unix: No radwtmp file configured.  Ignoring accounting request.");
454                 return RLM_MODULE_NOOP;
455         }
456
457         /*
458          *      Which type is this.
459          */
460         if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
461                 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
462                 return RLM_MODULE_NOOP;
463         }
464         status = vp->lvalue;
465
466         /*
467          *      FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
468          */
469         if (status != PW_STATUS_START &&
470             status != PW_STATUS_STOP)
471                 return RLM_MODULE_NOOP;
472
473         /*
474          *      We're only interested in accounting messages
475          *      with a username in it.
476          */
477         if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
478                 return RLM_MODULE_NOOP;
479
480         t = request->timestamp;
481         memset(&ut, 0, sizeof(ut));
482
483         /*
484          *      First, find the interesting attributes.
485          */
486         for (vp = request->packet->vps; vp; vp = vp->next) {
487                 switch (vp->attribute) {
488                         case PW_USER_NAME:
489                                 if (vp->length >= sizeof(ut.ut_name)) {
490                                         memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
491                                 } else {
492                                         strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
493                                 }
494                                 break;
495                         case PW_LOGIN_IP_HOST:
496                         case PW_FRAMED_IP_ADDRESS:
497                                 framed_address = vp->lvalue;
498                                 break;
499                         case PW_FRAMED_PROTOCOL:
500                                 protocol = vp->lvalue;
501                                 break;
502                         case PW_NAS_IP_ADDRESS:
503                                 nas_address = vp->lvalue;
504                                 break;
505                         case PW_NAS_PORT:
506                                 nas_port = vp->lvalue;
507                                 port_seen = 1;
508                                 break;
509                         case PW_ACCT_DELAY_TIME:
510                                 delay = vp->lvalue;
511                                 break;
512                         case PW_NAS_PORT_TYPE:
513                                 nas_port_type = vp->lvalue;
514                                 break;
515                 }
516         }
517
518         /*
519          *      We don't store !root sessions, or sessions
520          *      where we didn't see a NAS-Port attribute.
521          */
522         if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
523                 return RLM_MODULE_NOOP;
524
525         /*
526          *      If we didn't find out the NAS address, use the
527          *      originator's IP address.
528          */
529         if (nas_address == 0)
530                 nas_address = request->packet->src_ipaddr;
531
532 #ifdef __linux__
533         /*
534          *      Linux has a field for the client address.
535          */
536         ut.ut_addr = framed_address;
537 #endif
538         /*
539          *      We use the tty field to store the terminal servers' port
540          *      and address so that the tty field is unique.
541          */
542         s = "";
543         if ((cl = client_find(nas_address)) != NULL)
544                 s = cl->shortname;
545         if (s == NULL || s[0] == 0) s = uue(&(nas_address));
546         sprintf(buf, "%03d:%s", nas_port, s);
547         strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
548
549         /*
550          *      We store the dynamic IP address in the hostname field.
551          */
552 #ifdef UT_HOSTSIZE
553         if (framed_address) {
554                 ip_ntoa(buf, framed_address);
555                 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
556         }
557 #endif
558 #ifdef HAVE_UTMPX_H
559         ut.ut_xtime = t- delay;
560 #else
561         ut.ut_time = t - delay;
562 #endif
563 #ifdef USER_PROCESS
564         /*
565          *      And we can use the ID field to store
566          *      the protocol.
567          */
568         if (protocol == PW_PPP)
569                 strcpy(ut.ut_id, "P");
570         else if (protocol == PW_SLIP)
571                 strcpy(ut.ut_id, "S");
572         else
573                 strcpy(ut.ut_id, "T");
574         ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
575 #endif
576         if (status == PW_STATUS_STOP)
577                 ut.ut_name[0] = 0;
578
579         /*
580          *      Write a RADIUS wtmp log file.
581          *
582          *      Try to open the file if we can't, we don't write the
583          *      wtmp file. If we can try to write. If we fail,
584          *      return RLM_MODULE_FAIL ..
585          */
586         if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
587                 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
588                         fclose(fp);
589                         return RLM_MODULE_FAIL;
590                 }
591                 fclose(fp);
592         } else
593                 return RLM_MODULE_FAIL;
594
595         return RLM_MODULE_OK;
596 }
597
598 /* globally exported name */
599 module_t rlm_unix = {
600   "System",
601   RLM_TYPE_THREAD_UNSAFE,        /* type */
602   unix_init,                    /* initialization */
603   unix_instantiate,             /* instantiation */
604   {
605           unix_authenticate,    /* authentication */
606           unix_authorize,       /* authorization */
607           NULL,                 /* preaccounting */
608           unix_accounting,      /* accounting */
609           NULL,                  /* checksimul */
610           NULL,                 /* pre-proxy */
611           NULL,                 /* post-proxy */
612           NULL                  /* post-auth */
613   },
614   unix_detach,                  /* detach */
615   unix_destroy,                  /* destroy */
616 };