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