2 * rlm_unix.c authentication: Unix user authentication
3 * accounting: Functions to write radwtmp file.
4 * Also contains handler for "Group".
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.
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.
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
22 * Copyright 2000 The FreeRADIUS server project
23 * Copyright 2000 Jeff Carneal <jeff@apex.net>
24 * Copyright 2000 Alan Curry <pacman@world.std.com>
26 static const char rcsid[] = "$Id$";
29 #include "libradius.h"
35 #include <sys/types.h>
49 # include <sys/security.h>
65 static char trans[64] =
66 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
67 #define ENC(c) trans[c]
69 struct unix_instance {
71 const char *passwd_file;
72 const char *shadow_file;
73 const char *group_file;
76 struct pwcache *cache;
82 static CONF_PARSER module_config[] = {
84 * Cache the password by default.
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" },
101 { NULL, -1, 0, NULL, NULL } /* end the list */
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.
111 /* binds "Group=" to an instance (a particular passwd file) */
112 static struct unix_instance *group_inst;
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;
120 static inline const char *get_shadow_name(shadow_pwd_t *spwd) {
121 if (spwd == NULL) return NULL;
122 return (spwd->pw_name);
124 static inline const char *get_shadow_encrypted_pwd(shadow_pwd_t *spwd) {
125 if (spwd == NULL) return NULL;
126 return (spwd->pw_passwd);
129 static inline const char *get_shadow_name(shadow_pwd_t *spwd) {
130 if (spwd == NULL) return NULL;
131 return (spwd->sp_namp);
133 static inline const char *get_shadow_encrypted_pwd(shadow_pwd_t *spwd) {
134 if (spwd == NULL) return NULL;
135 return (spwd->sp_pwdp);
138 #endif /* HAVE_GETSPNAM */
140 static struct passwd *fgetpwnam(const char *fname, const char *name) {
141 FILE *file = fopen(fname, "ro");
142 struct passwd *pwd = NULL;
144 if(file == NULL) return NULL;
146 pwd = fgetpwent(file);
151 } while (strcmp(name, pwd->pw_name) != 0);
157 static struct group *fgetgrnam(const char *fname, const char *name) {
158 FILE *file = fopen(fname, "ro");
159 struct group *grp = NULL;
161 if(file == NULL) return NULL;
164 grp = fgetgrent(file);
169 } while(strcmp(name, grp->gr_name) != 0);
176 static shadow_pwd_t *fgetspnam(const char *fname, const char *name) {
177 FILE *file = fopen(fname, "ro");
178 shadow_pwd_t *spwd = NULL;
180 if(file == NULL) return NULL;
182 spwd = fgetspent(file);
187 } while(strcmp(name, get_shadow_name(spwd)) != 0);
195 * The Group = handler.
197 static int groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
198 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
207 check_pairs = check_pairs;
208 reply_pairs = reply_pairs;
211 radlog(L_ERR, "groupcmp: no group list known.");
215 username = (char *)request->strvalue;
217 if (group_inst->cache_passwd &&
218 (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
221 if (group_inst->passwd_file)
222 pwd = fgetpwnam(group_inst->passwd_file, username);
224 pwd = getpwnam(username);
228 if (group_inst->group_file)
229 grp = fgetgrnam(group_inst->group_file, (char *)check->strvalue);
231 grp = getgrnam((char *)check->strvalue);
235 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
237 for (member = grp->gr_mem; *member && retval; member++) {
238 if (strcmp(*member, pwd->pw_name) == 0)
247 * FIXME: We really should have an 'init' which makes
248 * System auth == Unix
250 static int unix_init(void)
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);
261 static int unix_instantiate(CONF_SECTION *conf, void **instance)
263 struct unix_instance *inst;
266 * Allocate room for the instance.
268 inst = *instance = rad_malloc(sizeof(struct unix_instance));
271 * Parse the configuration, failing if we can't do so.
273 if (cf_section_parse(conf, inst, module_config) < 0) {
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,
283 inst->group_file))==NULL)
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);
295 if (inst->cache_reload) {
296 inst->last_reload = 0;
297 inst->next_reload = time(NULL) + inst->cache_reload;
303 if (inst->usegroup) {
304 if (group_inst_explicit) {
305 radlog(L_ERR, "Only one group list may be active");
308 group_inst_explicit = 1;
310 } else if (!group_inst) {
321 static int unix_detach(void *instance)
323 #define inst ((struct unix_instance *)instance)
324 if (group_inst == inst) {
326 group_inst_explicit = 0;
328 free((char *) inst->passwd_file);
329 free((char *) inst->shadow_file);
330 free((char *) inst->group_file);
331 free((char *) inst->radwtmp);
333 unix_freepwcache(inst->cache);
340 static int unix_destroy(void)
342 paircompare_unregister(PW_GROUP, groupcmp);
344 paircompare_unregister(PW_GROUP_NAME, groupcmp);
351 * Check the users password against the standard UNIX
354 static int unix_authenticate(void *instance, REQUEST *request)
356 #define inst ((struct unix_instance *)instance)
360 char *encrypted_pass;
363 shadow_pwd_t *spwd = NULL;
366 struct pr_passwd *pr_pw;
370 char *progname = "radius";
371 SIAENTITY *ent = NULL;
373 #ifdef HAVE_GETUSERSHELL
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 ? */
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)) {
392 if (inst->shadow_file
393 && (stat(inst->shadow_file, &statbuf) != -1)
394 && (statbuf.st_mtime > inst->last_reload)) {
399 && (stat(inst->group_file, &statbuf) != -1)
400 && (statbuf.st_mtime > inst->last_reload)) {
405 /* Build a new cache to replace old one */
406 struct pwcache *oldcache;
407 struct pwcache *newcache = unix_buildpwcache(
413 oldcache = inst->cache;
414 inst->cache = newcache;
415 unix_freepwcache(oldcache);
417 inst->last_reload = time(NULL);
420 DEBUG2("rlm_users : Files were unchanged. Not reloading.");
423 /* Schedule next refresh */
424 inst->next_reload = time(NULL) + inst->cache_reload;
428 * We can only authenticate user requests which HAVE
429 * a User-Name attribute.
431 if (!request->username) {
432 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
433 return RLM_MODULE_INVALID;
437 * We can only authenticate user requests which HAVE
438 * a User-Password attribute.
440 if (!request->password) {
441 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
442 return RLM_MODULE_INVALID;
446 * Ensure that we're being passed a plain-text password,
447 * and not anything else.
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;
454 name = (char *)request->username->strvalue;
455 passwd = (char *)request->password->strvalue;
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;
464 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
466 return RLM_MODULE_NOTFOUND;
467 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
469 sia_ses_release (&ent);
470 return RLM_MODULE_NOTFOUND;
472 if (sia_ses_estab (NULL, ent) == SIASUCCESS) {
473 sia_ses_release (&ent);
474 return RLM_MODULE_OK;
477 return RLM_MODULE_NOTFOUND;
480 if ((pr_pw = getprpwnam(name)) == NULL)
481 return RLM_MODULE_NOTFOUND;
482 encrypted_pass = pr_pw->ufld.fd_encrypt;
485 * Get encrypted password from password file
487 * If a password file was explicitly specified, use it,
488 * otherwise, use the system routines to read the
489 * system password file
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;
497 encrypted_pass = pwd->pw_passwd;
502 * See if there is a shadow password.
504 * If a shadow file was explicitly specified, use it,
505 * otherwise, use the system routines to read the
506 * system shadow file.
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
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);
522 #endif /* HAVE_GETSPNAM */
526 * Undocumented temporary compatibility for iphil.NET
527 * Users with a certain shell are always denied access.
529 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
530 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
531 return RLM_MODULE_REJECT;
535 #if HAVE_GETUSERSHELL
537 * Check /etc/shells for a valid shell. If that file
538 * contains /RADIUSD/ANY/SHELL then any shell will do.
540 while ((shell = getusershell()) != NULL) {
541 if (strcmp(shell, pwd->pw_shell) == 0 ||
542 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
548 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
549 name, pwd->pw_shell);
550 return RLM_MODULE_REJECT;
554 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
556 * Check if password has expired.
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;
565 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
567 * Check if password has expired.
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;
578 * Check if account is locked.
580 if (pr_pw->uflg.fg_lock!=1) {
581 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
582 return RLM_MODULE_USERLOCK;
587 * We might have a passwordless account.
589 if (encrypted_pass[0] == 0)
590 return RLM_MODULE_OK;
593 * Check encrypted password.
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;
600 return RLM_MODULE_OK;
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.
610 static char *uue(void *in)
613 static unsigned char res[7];
614 unsigned char *data = (unsigned char *)in;
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 );
621 res[4] = ENC( data[3] >> 2 );
622 res[5] = ENC( (data[3] << 4) & 060 );
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");
635 * Unix accounting - write a wtmp file.
637 static int unix_accounting(void *instance, REQUEST *request)
649 int framed_address = 0;
653 int nas_port_type = 0;
654 struct unix_instance *inst = (struct unix_instance *) instance;
657 * Which type is this.
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;
666 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
668 if (status != PW_STATUS_START &&
669 status != PW_STATUS_STOP)
670 return RLM_MODULE_NOOP;
673 * We're only interested in accounting messages
674 * with a username in it.
676 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
677 return RLM_MODULE_NOOP;
679 t = request->timestamp;
680 memset(&ut, 0, sizeof(ut));
683 * First, find the interesting attributes.
685 for (vp = request->packet->vps; vp; vp = vp->next) {
686 switch (vp->attribute) {
688 if (vp->length >= sizeof(ut.ut_name)) {
689 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
691 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
694 case PW_LOGIN_IP_HOST:
695 case PW_FRAMED_IP_ADDRESS:
696 framed_address = vp->lvalue;
698 case PW_FRAMED_PROTOCOL:
699 protocol = vp->lvalue;
701 case PW_NAS_IP_ADDRESS:
702 nas_address = vp->lvalue;
705 nas_port = vp->lvalue;
708 case PW_ACCT_DELAY_TIME:
711 case PW_NAS_PORT_TYPE:
712 nas_port_type = vp->lvalue;
718 * We don't store !root sessions, or sessions
719 * where we didn't see a PW_NAS_PORT_ID.
721 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
722 return RLM_MODULE_NOOP;
725 * If we didn't find out the NAS address, use the
726 * originator's IP address.
728 if (nas_address == 0)
729 nas_address = request->packet->src_ipaddr;
733 * Linux has a field for the client address.
735 ut.ut_addr = framed_address;
738 * We use the tty field to store the terminal servers' port
739 * and address so that the tty field is unique.
742 if ((cl = client_find(nas_address)) != NULL)
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));
749 * We store the dynamic IP address in the hostname field.
752 if (framed_address) {
753 ip_ntoa(buf, framed_address);
754 strncpy(ut.ut_host, buf, UT_HOSTSIZE);
758 ut.ut_xtime = t- delay;
760 ut.ut_time = t - delay;
764 * And we can use the ID field to store
767 if (protocol == PW_PPP)
768 strcpy(ut.ut_id, "P");
769 else if (protocol == PW_SLIP)
770 strcpy(ut.ut_id, "S");
772 strcpy(ut.ut_id, "T");
773 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
775 if (status == PW_STATUS_STOP)
779 * Write a RADIUS wtmp log file.
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 ..
785 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
786 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
788 return RLM_MODULE_FAIL;
792 return RLM_MODULE_FAIL;
794 return RLM_MODULE_OK;
797 /* globally exported name */
798 module_t rlm_unix = {
800 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
801 unix_init, /* initialization */
802 unix_instantiate, /* instantiation */
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 */
813 unix_detach, /* detach */
814 unix_destroy, /* destroy */