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