Glob patch to change #if to #ifdef when checking variables, so that we
[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 #include        "cache.h"
58 #include        "conffile.h"
59 #include        "compat.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         time_t cache_reload;
74         time_t next_reload;
75         time_t last_reload;
76 };
77
78 static CONF_PARSER module_config[] = {
79         /*
80          *      Cache the password by default.
81          */
82         { "cache",    PW_TYPE_BOOLEAN,
83           offsetof(struct unix_instance,cache_passwd), NULL, "no" },
84         { "passwd",   PW_TYPE_STRING_PTR,
85           offsetof(struct unix_instance,passwd_file), NULL,  NULL },
86         { "shadow",   PW_TYPE_STRING_PTR,
87           offsetof(struct unix_instance,shadow_file), NULL,  NULL },
88         { "group",    PW_TYPE_STRING_PTR,
89           offsetof(struct unix_instance,group_file), NULL,   NULL },
90         { "radwtmp",  PW_TYPE_STRING_PTR,
91           offsetof(struct unix_instance,radwtmp), NULL,   "NULL" },
92         { "usegroup", PW_TYPE_BOOLEAN,
93           offsetof(struct unix_instance,usegroup), NULL,     "no" },
94         { "cache_reload", PW_TYPE_INTEGER,
95           offsetof(struct unix_instance,cache_reload), NULL, "600" },
96         
97         { NULL, -1, 0, NULL, NULL }             /* end the list */
98 };
99
100 /*
101  * groupcmp is part of autz. But it uses the data from an auth instance. So
102  * here is where it gets it. By default this will be the first configured
103  * auth instance. That can be changed by putting "usegroup = yes" inside an
104  * auth instance to explicitly bind all Group checks to it.
105  */
106
107 /* binds "Group=" to an instance (a particular passwd file) */
108 static struct unix_instance *group_inst;
109
110 /* Tells if the above binding was explicit (usegroup=yes specified in config
111  * file) or not ("Group=" was bound to the first instance of rlm_unix */
112 static int group_inst_explicit;
113
114 #ifdef HAVE_GETSPNAM
115 #if defined(M_UNIX)
116 static inline const char *get_shadow_name(shadow_pwd_t *spwd) {
117         if (spwd == NULL) return NULL;
118         return (spwd->pw_name);
119 }
120
121 static inline const char *get_shadow_encrypted_pwd(shadow_pwd_t *spwd) {
122         if (spwd == NULL) return NULL;
123         return (spwd->pw_passwd);
124 }
125 #else /* M_UNIX */
126         static inline const char *get_shadow_name(shadow_pwd_t *spwd) {
127                 if (spwd == NULL) return NULL;
128                 return (spwd->sp_namp);
129         }
130         static inline const char *get_shadow_encrypted_pwd(shadow_pwd_t *spwd) {
131                 if (spwd == NULL) return NULL;
132                 return (spwd->sp_pwdp);
133         }
134 #endif  /* M_UNIX */
135 #endif  /* HAVE_GETSPNAM */
136
137 static struct passwd *fgetpwnam(const char *fname, const char *name) {
138         FILE            *file = fopen(fname, "ro");
139         struct passwd   *pwd = NULL;
140
141         if(file == NULL) return NULL;
142         do {
143                 pwd = fgetpwent(file);
144                 if(pwd == NULL) {
145                         fclose(file);
146                         return NULL;
147                 }
148         } while (strcmp(name, pwd->pw_name) != 0);
149
150         fclose(file);
151         return pwd;
152 }
153
154 static struct group *fgetgrnam(const char *fname, const char *name) {
155         FILE            *file = fopen(fname, "ro");
156         struct group    *grp = NULL;
157
158         if(file == NULL) return NULL;
159
160         do {
161                 grp = fgetgrent(file);
162                 if(grp == NULL) {
163                         fclose(file);
164                         return NULL;
165                 }
166         } while(strcmp(name, grp->gr_name) != 0);
167         fclose(file);
168         return grp;
169 }
170
171 #ifdef HAVE_GETSPNAM
172
173 static shadow_pwd_t *fgetspnam(const char *fname, const char *name) {
174         FILE            *file = fopen(fname, "ro");
175         shadow_pwd_t    *spwd = NULL;
176
177         if(file == NULL) return NULL;
178         do {
179                 spwd = fgetspent(file);
180                 if(spwd == NULL) {
181                         fclose(file);
182                         return NULL;
183                 }
184         } while(strcmp(name, get_shadow_name(spwd)) != 0);
185         fclose(file);
186         return spwd;
187 }
188
189 #endif
190
191 /*
192  *      The Group = handler.
193  */
194 static int groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
195         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
196 {
197         struct passwd   *pwd;
198         struct group    *grp;
199         char            **member;
200         char            *username;
201         int             retval;
202
203         instance = instance;
204         check_pairs = check_pairs;
205         reply_pairs = reply_pairs;
206
207         if (!group_inst) {
208                 radlog(L_ERR, "groupcmp: no group list known.");
209                 return 1;
210         }
211
212         /*
213          *      No user name, doesn't compare.
214          */
215         if (!req->username) {
216                 return -1;
217         }
218         username = (char *)req->username->strvalue;
219
220         if (group_inst->cache_passwd &&
221             (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
222                 return retval;
223
224         if (group_inst->passwd_file)
225                 pwd = fgetpwnam(group_inst->passwd_file, username);
226         else
227                 pwd = getpwnam(username);
228         if (pwd == NULL)
229                 return -1;
230
231         if (group_inst->group_file)
232                 grp = fgetgrnam(group_inst->group_file, (char *)check->strvalue);
233         else
234                 grp = getgrnam((char *)check->strvalue);
235         if (grp == NULL)
236                 return -1;
237
238         retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
239         if (retval < 0) {
240                 for (member = grp->gr_mem; *member && retval; member++) {
241                         if (strcmp(*member, pwd->pw_name) == 0)
242                                 retval = 0;
243                 }
244         }
245         return retval;
246 }
247
248
249 /*
250  *      FIXME:  We really should have an 'init' which makes
251  *      System auth == Unix
252  */
253 static int unix_init(void)
254 {
255         /* FIXME - delay these until a group file has been read so we know
256          * groupcmp can actually do something */
257         paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
258 #ifdef PW_GROUP_NAME /* compat */
259         paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
260 #endif
261         return 0;
262 }
263
264 static int unix_instantiate(CONF_SECTION *conf, void **instance)
265 {
266         struct unix_instance *inst;
267
268         /*
269          *      Allocate room for the instance.
270          */
271         inst = *instance = rad_malloc(sizeof(*inst));
272         if (!inst) {
273                 return -1;
274         }
275         memset(inst, 0, sizeof(*inst));
276
277         /*
278          *      Parse the configuration, failing if we can't do so.
279          */
280         if (cf_section_parse(conf, inst, module_config) < 0) {
281                 free(inst);
282                 return -1;
283         }
284
285         if (inst->cache_passwd) {
286                 radlog(L_INFO, "HASH:  Reinitializing hash structures "
287                         "and lists for caching...");
288                 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
289                                                      inst->shadow_file,
290                                                      inst->group_file))==NULL)
291                 {
292                         radlog(L_ERR, "HASH:  unable to create user "
293                                 "hash table.  disable caching and run debugs");
294                         if (inst->passwd_file)
295                                 free((char *) inst->passwd_file);
296                         if (inst->shadow_file)
297                                 free((char *) inst->shadow_file);
298                         if (inst->group_file)
299                                 free((char *) inst->group_file);
300                         if (inst->radwtmp)
301                                 free((char *) inst->radwtmp);
302                         free(inst);
303                         return -1;
304                 }
305
306                 if (inst->cache_reload) {
307                         inst->last_reload = 0;
308                         inst->next_reload = time(NULL) + inst->cache_reload;
309                 }
310         } else {
311                 inst->cache = NULL;
312         }
313
314         if (inst->usegroup) {
315                 if (group_inst_explicit) {
316                         radlog(L_ERR, "Only one group list may be active");
317                 } else {
318                         group_inst = inst;
319                         group_inst_explicit = 1;
320                 }
321         } else if (!group_inst) {
322                 group_inst = inst;
323         }
324 #undef inst
325
326         return 0;
327 }
328
329 /*
330  *      Detach.
331  */
332 static int unix_detach(void *instance)
333 {
334 #define inst ((struct unix_instance *)instance)
335         if (group_inst == inst) {
336                 group_inst = NULL;
337                 group_inst_explicit = 0;
338         }
339         if (inst->passwd_file)
340                 free((char *) inst->passwd_file);
341         if (inst->shadow_file)
342                 free((char *) inst->shadow_file);
343         if (inst->group_file)
344                 free((char *) inst->group_file);
345         if (inst->radwtmp)
346                 free((char *) inst->radwtmp);
347         if (inst->cache) {
348                 unix_freepwcache(inst->cache);
349         }
350 #undef inst
351         free(instance);
352         return 0;
353 }
354
355 static int unix_destroy(void)
356 {
357         paircompare_unregister(PW_GROUP, groupcmp);
358 #ifdef PW_GROUP_NAME
359         paircompare_unregister(PW_GROUP_NAME, groupcmp);
360 #endif
361         return 0;
362 }
363
364
365 /*
366  *      Check the users password against the standard UNIX
367  *      password table.
368  */
369 static int unix_authenticate(void *instance, REQUEST *request)
370 {
371 #define inst ((struct unix_instance *)instance)
372         char *name, *passwd;
373         struct passwd   *pwd;
374         const char      *encrypted_pass;
375         int             ret;
376 #ifdef HAVE_GETSPNAM
377         shadow_pwd_t    *spwd = NULL;
378 #endif
379 #ifdef OSFC2
380         struct pr_passwd *pr_pw;
381 #endif
382 #ifdef OSFSIA
383         char            *info[2];
384         char            *progname = "radius";
385         SIAENTITY       *ent = NULL;
386 #endif
387 #ifdef HAVE_GETUSERSHELL
388         char            *shell;
389 #endif
390
391         /* See if we should refresh the cache */
392         if (inst->cache && inst->cache_reload
393          && (inst->next_reload < request->timestamp)) {
394                 /* Time to refresh, maybe ? */
395                 int must_reload = 0;
396                 struct stat statbuf;
397
398                 DEBUG2("rlm_users : Time to refresh cache.");
399                 /* Check if any of the files has changed */
400                 if (inst->passwd_file
401                  && (stat(inst->passwd_file, &statbuf) != -1)
402                  && (statbuf.st_mtime > inst->last_reload)) {
403                         must_reload++;
404                 }
405
406                 if (inst->shadow_file
407                  && (stat(inst->shadow_file, &statbuf) != -1)
408                  && (statbuf.st_mtime > inst->last_reload)) {
409                         must_reload++;
410                 }
411
412                 if (inst->group_file
413                  && (stat(inst->group_file, &statbuf) != -1)
414                  && (statbuf.st_mtime > inst->last_reload)) {
415                         must_reload++;
416                 }
417
418                 if (must_reload) {
419                         /* Build a new cache to replace old one */
420                         struct pwcache *oldcache;
421                         struct pwcache *newcache = unix_buildpwcache(
422                                                         inst->passwd_file,
423                                                         inst->shadow_file,
424                                                         inst->group_file);
425
426                         if (newcache) {
427                                 oldcache = inst->cache;
428                                 inst->cache = newcache;
429                                 unix_freepwcache(oldcache);
430
431                                 inst->last_reload = time(NULL);
432                         }
433                 } else {
434                         DEBUG2("rlm_users : Files were unchanged. Not reloading.");
435                 }
436
437                 /* Schedule next refresh */
438                 inst->next_reload = time(NULL) + inst->cache_reload;
439         }
440
441         /*
442          *      We can only authenticate user requests which HAVE
443          *      a User-Name attribute.
444          */
445         if (!request->username) {
446                 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
447                 return RLM_MODULE_INVALID;
448         }
449
450         /*
451          *      We can only authenticate user requests which HAVE
452          *      a User-Password attribute.
453          */
454         if (!request->password) {
455                 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
456                 return RLM_MODULE_INVALID;
457         }
458
459         /*
460          *  Ensure that we're being passed a plain-text password,
461          *  and not anything else.
462          */
463         if (request->password->attribute != PW_PASSWORD) {
464                 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.  Cannot use \"%s\".", request->password->name);
465                 return RLM_MODULE_INVALID;
466         }
467
468         name = (char *)request->username->strvalue;
469         passwd = (char *)request->password->strvalue;
470
471         if (inst->cache_passwd &&
472             (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
473                 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
474
475 #ifdef OSFSIA
476         info[0] = progname;
477         info[1] = NULL;
478         if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
479             SIASUCCESS)
480                 return RLM_MODULE_NOTFOUND;
481         if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
482                 if (ret & SIASTOP)
483                         sia_ses_release (&ent);
484                 return RLM_MODULE_NOTFOUND;
485         }
486         if (sia_ses_estab (NULL, ent) == SIASUCCESS) {
487                 sia_ses_release (&ent);
488                 return RLM_MODULE_OK;
489         }
490
491         return RLM_MODULE_NOTFOUND;
492 #else /* OSFSIA */
493 #ifdef OSFC2
494         if ((pr_pw = getprpwnam(name)) == NULL)
495                 return RLM_MODULE_NOTFOUND;
496         encrypted_pass = pr_pw->ufld.fd_encrypt;
497 #else /* OSFC2 */
498         /*
499          *      Get encrypted password from password file
500          *
501          *      If a password file was explicitly specified, use it,
502          *      otherwise, use the system routines to read the
503          *      system password file
504          */
505         if (inst->passwd_file != NULL) {
506                 if ((pwd = fgetpwnam(inst->passwd_file, name)) == NULL)
507                         return RLM_MODULE_NOTFOUND;
508         } else if ((pwd = getpwnam(name)) == NULL) {
509                 return RLM_MODULE_NOTFOUND;
510         }
511         encrypted_pass = pwd->pw_passwd;
512 #endif /* OSFC2 */
513
514 #ifdef HAVE_GETSPNAM
515         /*
516          *      See if there is a shadow password.
517          *
518          *      If a shadow file was explicitly specified, use it,
519          *      otherwise, use the system routines to read the
520          *      system shadow file.
521          *
522          *      Also, if we explicitly specify the password file,
523          *      only query the _system_ shadow file if the encrypted
524          *      password from the passwd file is < 10 characters (i.e.
525          *      a valid password would never crypt() to it).  This will
526          *      prevents users from using NULL password fields as things
527          *      stand right now.
528          */
529         if (inst->shadow_file != NULL) {
530                 if ((spwd = fgetspnam(inst->shadow_file, name)) != NULL)
531                         encrypted_pass = get_shadow_encrypted_pwd(spwd);
532         } else if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
533                 if ((spwd = getspnam(name)) != NULL)
534                         encrypted_pass = get_shadow_encrypted_pwd(spwd);
535         }
536 #endif  /* HAVE_GETSPNAM */
537
538 #ifdef DENY_SHELL
539         /*
540          *      Undocumented temporary compatibility for iphil.NET
541          *      Users with a certain shell are always denied access.
542          */
543         if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
544                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
545                 return RLM_MODULE_REJECT;
546         }
547 #endif
548
549 #ifdef HAVE_GETUSERSHELL
550         /*
551          *      Check /etc/shells for a valid shell. If that file
552          *      contains /RADIUSD/ANY/SHELL then any shell will do.
553          */
554         while ((shell = getusershell()) != NULL) {
555                 if (strcmp(shell, pwd->pw_shell) == 0 ||
556                     strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
557                         break;
558                 }
559         }
560         endusershell();
561         if (shell == NULL) {
562                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
563                         name, pwd->pw_shell);
564                 return RLM_MODULE_REJECT;
565         }
566 #endif
567
568 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
569         /*
570          *      Check if password has expired.
571          */
572         if (spwd && spwd->sp_expire > 0 &&
573             (request->timestamp / 86400) > spwd->sp_expire) {
574                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
575                 return RLM_MODULE_REJECT;
576         }
577 #endif
578
579 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
580         /*
581          *      Check if password has expired.
582          */
583         if ((pwd->pw_expire > 0) &&
584             (request->timestamp > pwd->pw_expire)) {
585                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
586                 return RLM_MODULE_REJECT;
587         }
588 #endif
589
590 #ifdef OSFC2
591         /*
592          *      Check if account is locked.
593          */
594         if (pr_pw->uflg.fg_lock!=1) {
595                 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
596                 return RLM_MODULE_USERLOCK;
597         }
598 #endif /* OSFC2 */
599
600         /*
601          *      We might have a passwordless account.
602          */
603         if (encrypted_pass[0] == 0)
604                 return RLM_MODULE_OK;
605
606         /*
607          *      Check encrypted password.
608          */
609         if (lrad_crypt_check(passwd, encrypted_pass)) {
610                 radlog(L_AUTH, "rlm_unix: [%s]: invalid password", name);
611                 return RLM_MODULE_REJECT;
612         }
613         return RLM_MODULE_OK;
614 #endif /* OSFSIA */
615 #undef inst
616 }
617
618 /*
619  *      UUencode 4 bits base64. We use this to turn a 4 byte field
620  *      (an IP address) into 6 bytes of ASCII. This is used for the
621  *      wtmp file if we didn't find a short name in the naslist file.
622  */
623 static char *uue(void *in)
624 {
625         int i;
626         static unsigned char res[7];
627         unsigned char *data = (unsigned char *)in;
628
629         res[0] = ENC( data[0] >> 2 );
630         res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
631         res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
632         res[3] = ENC( data[2] & 077 );
633
634         res[4] = ENC( data[3] >> 2 );
635         res[5] = ENC( (data[3] << 4) & 060 );
636         res[6] = 0;
637
638         for(i = 0; i < 6; i++) {
639                 if (res[i] == ' ') res[i] = '`';
640                 if (res[i] < 32 || res[i] > 127)
641                         printf("uue: protocol error ?!\n");
642         }
643         return (char *)res;
644 }
645
646
647 /*
648  *      Unix accounting - write a wtmp file.
649  */
650 static int unix_accounting(void *instance, REQUEST *request)
651 {
652         VALUE_PAIR      *vp;
653         RADCLIENT       *cl;
654         FILE            *fp;
655         struct utmp     ut;
656         time_t          t;
657         char            buf[64];
658         const char      *s;
659         int             delay = 0;
660         int             status = -1;
661         int             nas_address = 0;
662         int             framed_address = 0;
663         int             protocol = -1;
664         int             nas_port = 0;
665         int             port_seen = 0;
666         int             nas_port_type = 0;
667         struct unix_instance *inst = (struct unix_instance *) instance;
668
669         /*
670          *      No radwtmp.  Don't do anything.
671          */
672         if (!inst->radwtmp) {
673                 DEBUG2("rlm_unix: No radwtmp file configured.  Ignoring accounting request.");
674                 return RLM_MODULE_NOOP;
675         }
676
677         /*
678          *      Which type is this.
679          */
680         if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
681                 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
682                 return RLM_MODULE_NOOP;
683         }
684         status = vp->lvalue;
685
686         /*
687          *      FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
688          */
689         if (status != PW_STATUS_START &&
690             status != PW_STATUS_STOP)
691                 return RLM_MODULE_NOOP;
692
693         /*
694          *      We're only interested in accounting messages
695          *      with a username in it.
696          */
697         if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
698                 return RLM_MODULE_NOOP;
699
700         t = request->timestamp;
701         memset(&ut, 0, sizeof(ut));
702
703         /*
704          *      First, find the interesting attributes.
705          */
706         for (vp = request->packet->vps; vp; vp = vp->next) {
707                 switch (vp->attribute) {
708                         case PW_USER_NAME:
709                                 if (vp->length >= sizeof(ut.ut_name)) {
710                                         memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
711                                 } else {
712                                         strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
713                                 }
714                                 break;
715                         case PW_LOGIN_IP_HOST:
716                         case PW_FRAMED_IP_ADDRESS:
717                                 framed_address = vp->lvalue;
718                                 break;
719                         case PW_FRAMED_PROTOCOL:
720                                 protocol = vp->lvalue;
721                                 break;
722                         case PW_NAS_IP_ADDRESS:
723                                 nas_address = vp->lvalue;
724                                 break;
725                         case PW_NAS_PORT:
726                                 nas_port = vp->lvalue;
727                                 port_seen = 1;
728                                 break;
729                         case PW_ACCT_DELAY_TIME:
730                                 delay = vp->lvalue;
731                                 break;
732                         case PW_NAS_PORT_TYPE:
733                                 nas_port_type = vp->lvalue;
734                                 break;
735                 }
736         }
737
738         /*
739          *      We don't store !root sessions, or sessions
740          *      where we didn't see a NAS-Port attribute.
741          */
742         if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
743                 return RLM_MODULE_NOOP;
744
745         /*
746          *      If we didn't find out the NAS address, use the
747          *      originator's IP address.
748          */
749         if (nas_address == 0)
750                 nas_address = request->packet->src_ipaddr;
751
752 #ifdef __linux__
753         /*
754          *      Linux has a field for the client address.
755          */
756         ut.ut_addr = framed_address;
757 #endif
758         /*
759          *      We use the tty field to store the terminal servers' port
760          *      and address so that the tty field is unique.
761          */
762         s = "";
763         if ((cl = client_find(nas_address)) != NULL)
764                 s = cl->shortname;
765         if (s == NULL || s[0] == 0) s = uue(&(nas_address));
766         sprintf(buf, "%03d:%s", nas_port, s);
767         strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
768
769         /*
770          *      We store the dynamic IP address in the hostname field.
771          */
772 #ifdef UT_HOSTSIZE
773         if (framed_address) {
774                 ip_ntoa(buf, framed_address);
775                 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
776         }
777 #endif
778 #ifdef HAVE_UTMPX_H
779         ut.ut_xtime = t- delay;
780 #else
781         ut.ut_time = t - delay;
782 #endif
783 #ifdef USER_PROCESS
784         /*
785          *      And we can use the ID field to store
786          *      the protocol.
787          */
788         if (protocol == PW_PPP)
789                 strcpy(ut.ut_id, "P");
790         else if (protocol == PW_SLIP)
791                 strcpy(ut.ut_id, "S");
792         else
793                 strcpy(ut.ut_id, "T");
794         ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
795 #endif
796         if (status == PW_STATUS_STOP)
797                 ut.ut_name[0] = 0;
798
799         /*
800          *      Write a RADIUS wtmp log file.
801          *
802          *      Try to open the file if we can't, we don't write the
803          *      wtmp file. If we can try to write. If we fail,
804          *      return RLM_MODULE_FAIL ..
805          */
806         if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
807                 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
808                         fclose(fp);
809                         return RLM_MODULE_FAIL;
810                 }
811                 fclose(fp);
812         } else 
813                 return RLM_MODULE_FAIL;
814
815         return RLM_MODULE_OK;
816 }
817
818 /* globally exported name */
819 module_t rlm_unix = {
820   "System",
821   RLM_TYPE_THREAD_UNSAFE,        /* type: reserved */
822   unix_init,                    /* initialization */
823   unix_instantiate,             /* instantiation */
824   {
825           unix_authenticate,    /* authentication */
826           NULL,                 /* authorization */
827           NULL,                 /* preaccounting */
828           unix_accounting,      /* accounting */
829           NULL,                  /* checksimul */
830           NULL,                 /* pre-proxy */
831           NULL,                 /* post-proxy */
832           NULL                  /* post-auth */
833   },
834   unix_detach,                  /* detach */
835   unix_destroy,                  /* destroy */
836 };
837