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