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