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