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