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, "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" },
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);
125 static inline const char *get_shadow_encrypted_pwd(shadow_pwd_t *spwd) {
126 if (spwd == NULL) return NULL;
127 return (spwd->pw_passwd);
130 static inline const char *get_shadow_name(shadow_pwd_t *spwd) {
131 if (spwd == NULL) return NULL;
132 return (spwd->sp_namp);
134 static inline const char *get_shadow_encrypted_pwd(shadow_pwd_t *spwd) {
135 if (spwd == NULL) return NULL;
136 return (spwd->sp_pwdp);
139 #endif /* HAVE_GETSPNAM */
141 static struct passwd *fgetpwnam(const char *fname, const char *name) {
142 FILE *file = fopen(fname, "ro");
143 struct passwd *pwd = NULL;
145 if(file == NULL) return NULL;
147 pwd = fgetpwent(file);
152 } while (strcmp(name, pwd->pw_name) != 0);
158 static struct group *fgetgrnam(const char *fname, const char *name) {
159 FILE *file = fopen(fname, "ro");
160 struct group *grp = NULL;
162 if(file == NULL) return NULL;
165 grp = fgetgrent(file);
170 } while(strcmp(name, grp->gr_name) != 0);
177 static shadow_pwd_t *fgetspnam(const char *fname, const char *name) {
178 FILE *file = fopen(fname, "ro");
179 shadow_pwd_t *spwd = NULL;
181 if(file == NULL) return NULL;
183 spwd = fgetspent(file);
188 } while(strcmp(name, get_shadow_name(spwd)) != 0);
196 * The Group = handler.
198 static int groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
199 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
208 check_pairs = check_pairs;
209 reply_pairs = reply_pairs;
212 radlog(L_ERR, "groupcmp: no group list known.");
216 username = (char *)request->strvalue;
218 if (group_inst->cache_passwd &&
219 (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
222 if (group_inst->passwd_file)
223 pwd = fgetpwnam(group_inst->passwd_file, username);
225 pwd = getpwnam(username);
229 if (group_inst->group_file)
230 grp = fgetgrnam(group_inst->group_file, (char *)check->strvalue);
232 grp = getgrnam((char *)check->strvalue);
236 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
238 for (member = grp->gr_mem; *member && retval; member++) {
239 if (strcmp(*member, pwd->pw_name) == 0)
248 * FIXME: We really should have an 'init' which makes
249 * System auth == Unix
251 static int unix_init(void)
253 /* FIXME - delay these until a group file has been read so we know
254 * groupcmp can actually do something */
255 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
256 #ifdef PW_GROUP_NAME /* compat */
257 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
262 static int unix_instantiate(CONF_SECTION *conf, void **instance)
264 struct unix_instance *inst;
267 * Allocate room for the instance.
269 inst = *instance = rad_malloc(sizeof(struct unix_instance));
272 * Parse the configuration, failing if we can't do so.
274 if (cf_section_parse(conf, inst, module_config) < 0) {
279 if (inst->cache_passwd) {
280 radlog(L_INFO, "HASH: Reinitializing hash structures "
281 "and lists for caching...");
282 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
284 inst->group_file))==NULL)
286 radlog(L_ERR, "HASH: unable to create user "
287 "hash table. disable caching and run debugs");
288 if (inst->passwd_file)
289 free((char *) inst->passwd_file);
290 if (inst->shadow_file)
291 free((char *) inst->shadow_file);
292 if (inst->group_file)
293 free((char *) inst->group_file);
295 free((char *) inst->radwtmp);
300 if (inst->cache_reload) {
301 inst->last_reload = 0;
302 inst->next_reload = time(NULL) + inst->cache_reload;
308 if (inst->usegroup) {
309 if (group_inst_explicit) {
310 radlog(L_ERR, "Only one group list may be active");
313 group_inst_explicit = 1;
315 } else if (!group_inst) {
326 static int unix_detach(void *instance)
328 #define inst ((struct unix_instance *)instance)
329 if (group_inst == inst) {
331 group_inst_explicit = 0;
333 if (inst->passwd_file)
334 free((char *) inst->passwd_file);
335 if (inst->shadow_file)
336 free((char *) inst->shadow_file);
337 if (inst->group_file)
338 free((char *) inst->group_file);
340 free((char *) inst->radwtmp);
342 unix_freepwcache(inst->cache);
349 static int unix_destroy(void)
351 paircompare_unregister(PW_GROUP, groupcmp);
353 paircompare_unregister(PW_GROUP_NAME, groupcmp);
360 * Check the users password against the standard UNIX
363 static int unix_authenticate(void *instance, REQUEST *request)
365 #define inst ((struct unix_instance *)instance)
369 char *encrypted_pass;
372 shadow_pwd_t *spwd = NULL;
375 struct pr_passwd *pr_pw;
379 char *progname = "radius";
380 SIAENTITY *ent = NULL;
382 #ifdef HAVE_GETUSERSHELL
386 /* See if we should refresh the cache */
387 if (inst->cache && inst->cache_reload
388 && (inst->next_reload < request->timestamp)) {
389 /* Time to refresh, maybe ? */
393 DEBUG2("rlm_users : Time to refresh cache.");
394 /* Check if any of the files has changed */
395 if (inst->passwd_file
396 && (stat(inst->passwd_file, &statbuf) != -1)
397 && (statbuf.st_mtime > inst->last_reload)) {
401 if (inst->shadow_file
402 && (stat(inst->shadow_file, &statbuf) != -1)
403 && (statbuf.st_mtime > inst->last_reload)) {
408 && (stat(inst->group_file, &statbuf) != -1)
409 && (statbuf.st_mtime > inst->last_reload)) {
414 /* Build a new cache to replace old one */
415 struct pwcache *oldcache;
416 struct pwcache *newcache = unix_buildpwcache(
422 oldcache = inst->cache;
423 inst->cache = newcache;
424 unix_freepwcache(oldcache);
426 inst->last_reload = time(NULL);
429 DEBUG2("rlm_users : Files were unchanged. Not reloading.");
432 /* Schedule next refresh */
433 inst->next_reload = time(NULL) + inst->cache_reload;
437 * We can only authenticate user requests which HAVE
438 * a User-Name attribute.
440 if (!request->username) {
441 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
442 return RLM_MODULE_INVALID;
446 * We can only authenticate user requests which HAVE
447 * a User-Password attribute.
449 if (!request->password) {
450 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
451 return RLM_MODULE_INVALID;
455 * Ensure that we're being passed a plain-text password,
456 * and not anything else.
458 if (request->password->attribute != PW_PASSWORD) {
459 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
460 return RLM_MODULE_INVALID;
463 name = (char *)request->username->strvalue;
464 passwd = (char *)request->password->strvalue;
466 if (inst->cache_passwd &&
467 (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
468 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
473 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
475 return RLM_MODULE_NOTFOUND;
476 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
478 sia_ses_release (&ent);
479 return RLM_MODULE_NOTFOUND;
481 if (sia_ses_estab (NULL, ent) == SIASUCCESS) {
482 sia_ses_release (&ent);
483 return RLM_MODULE_OK;
486 return RLM_MODULE_NOTFOUND;
489 if ((pr_pw = getprpwnam(name)) == NULL)
490 return RLM_MODULE_NOTFOUND;
491 encrypted_pass = pr_pw->ufld.fd_encrypt;
494 * Get encrypted password from password file
496 * If a password file was explicitly specified, use it,
497 * otherwise, use the system routines to read the
498 * system password file
500 if (inst->passwd_file != NULL) {
501 if ((pwd = fgetpwnam(inst->passwd_file, name)) == NULL)
502 return RLM_MODULE_NOTFOUND;
503 } else if ((pwd = getpwnam(name)) == NULL) {
504 return RLM_MODULE_NOTFOUND;
506 encrypted_pass = pwd->pw_passwd;
511 * See if there is a shadow password.
513 * If a shadow file was explicitly specified, use it,
514 * otherwise, use the system routines to read the
515 * system shadow file.
517 * Also, if we explicitly specify the password file,
518 * only query the _system_ shadow file if the encrypted
519 * password from the passwd file is < 10 characters (i.e.
520 * a valid password would never crypt() to it). This will
521 * prevents users from using NULL password fields as things
524 if (inst->shadow_file != NULL) {
525 if ((spwd = fgetspnam(inst->shadow_file, name)) != NULL)
526 encrypted_pass = get_shadow_encrypted_pwd(spwd);
527 } else if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
528 if ((spwd = getspnam(name)) != NULL)
529 encrypted_pass = get_shadow_encrypted_pwd(spwd);
531 #endif /* HAVE_GETSPNAM */
535 * Undocumented temporary compatibility for iphil.NET
536 * Users with a certain shell are always denied access.
538 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
539 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
540 return RLM_MODULE_REJECT;
544 #if HAVE_GETUSERSHELL
546 * Check /etc/shells for a valid shell. If that file
547 * contains /RADIUSD/ANY/SHELL then any shell will do.
549 while ((shell = getusershell()) != NULL) {
550 if (strcmp(shell, pwd->pw_shell) == 0 ||
551 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
557 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
558 name, pwd->pw_shell);
559 return RLM_MODULE_REJECT;
563 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
565 * Check if password has expired.
567 if (spwd && spwd->sp_expire > 0 &&
568 (request->timestamp / 86400) > spwd->sp_expire) {
569 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
570 return RLM_MODULE_REJECT;
574 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
576 * Check if password has expired.
578 if ((pwd->pw_expire > 0) &&
579 (request->timestamp > pwd->pw_expire)) {
580 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
581 return RLM_MODULE_REJECT;
587 * Check if account is locked.
589 if (pr_pw->uflg.fg_lock!=1) {
590 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
591 return RLM_MODULE_USERLOCK;
596 * We might have a passwordless account.
598 if (encrypted_pass[0] == 0)
599 return RLM_MODULE_OK;
602 * Check encrypted password.
604 encpw = crypt(passwd, encrypted_pass);
605 if (strcmp(encpw, encrypted_pass)) {
606 radlog(L_AUTH, "rlm_unix: [%s]: invalid password", name);
607 return RLM_MODULE_REJECT;
609 return RLM_MODULE_OK;
615 * UUencode 4 bits base64. We use this to turn a 4 byte field
616 * (an IP address) into 6 bytes of ASCII. This is used for the
617 * wtmp file if we didn't find a short name in the naslist file.
619 static char *uue(void *in)
622 static unsigned char res[7];
623 unsigned char *data = (unsigned char *)in;
625 res[0] = ENC( data[0] >> 2 );
626 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
627 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
628 res[3] = ENC( data[2] & 077 );
630 res[4] = ENC( data[3] >> 2 );
631 res[5] = ENC( (data[3] << 4) & 060 );
634 for(i = 0; i < 6; i++) {
635 if (res[i] == ' ') res[i] = '`';
636 if (res[i] < 32 || res[i] > 127)
637 printf("uue: protocol error ?!\n");
644 * Unix accounting - write a wtmp file.
646 static int unix_accounting(void *instance, REQUEST *request)
658 int framed_address = 0;
662 int nas_port_type = 0;
663 struct unix_instance *inst = (struct unix_instance *) instance;
666 * No radwtmp. Don't do anything.
668 if (!inst->radwtmp) {
669 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
670 return RLM_MODULE_NOOP;
674 * Which type is this.
676 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
677 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
678 return RLM_MODULE_NOOP;
683 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
685 if (status != PW_STATUS_START &&
686 status != PW_STATUS_STOP)
687 return RLM_MODULE_NOOP;
690 * We're only interested in accounting messages
691 * with a username in it.
693 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
694 return RLM_MODULE_NOOP;
696 t = request->timestamp;
697 memset(&ut, 0, sizeof(ut));
700 * First, find the interesting attributes.
702 for (vp = request->packet->vps; vp; vp = vp->next) {
703 switch (vp->attribute) {
705 if (vp->length >= sizeof(ut.ut_name)) {
706 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
708 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
711 case PW_LOGIN_IP_HOST:
712 case PW_FRAMED_IP_ADDRESS:
713 framed_address = vp->lvalue;
715 case PW_FRAMED_PROTOCOL:
716 protocol = vp->lvalue;
718 case PW_NAS_IP_ADDRESS:
719 nas_address = vp->lvalue;
722 nas_port = vp->lvalue;
725 case PW_ACCT_DELAY_TIME:
728 case PW_NAS_PORT_TYPE:
729 nas_port_type = vp->lvalue;
735 * We don't store !root sessions, or sessions
736 * where we didn't see a NAS-Port attribute.
738 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
739 return RLM_MODULE_NOOP;
742 * If we didn't find out the NAS address, use the
743 * originator's IP address.
745 if (nas_address == 0)
746 nas_address = request->packet->src_ipaddr;
750 * Linux has a field for the client address.
752 ut.ut_addr = framed_address;
755 * We use the tty field to store the terminal servers' port
756 * and address so that the tty field is unique.
759 if ((cl = client_find(nas_address)) != NULL)
761 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
762 sprintf(buf, "%03d:%s", nas_port, s);
763 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
766 * We store the dynamic IP address in the hostname field.
769 if (framed_address) {
770 ip_ntoa(buf, framed_address);
771 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
775 ut.ut_xtime = t- delay;
777 ut.ut_time = t - delay;
781 * And we can use the ID field to store
784 if (protocol == PW_PPP)
785 strcpy(ut.ut_id, "P");
786 else if (protocol == PW_SLIP)
787 strcpy(ut.ut_id, "S");
789 strcpy(ut.ut_id, "T");
790 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
792 if (status == PW_STATUS_STOP)
796 * Write a RADIUS wtmp log file.
798 * Try to open the file if we can't, we don't write the
799 * wtmp file. If we can try to write. If we fail,
800 * return RLM_MODULE_FAIL ..
802 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
803 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
805 return RLM_MODULE_FAIL;
809 return RLM_MODULE_FAIL;
811 return RLM_MODULE_OK;
814 /* globally exported name */
815 module_t rlm_unix = {
817 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
818 unix_init, /* initialization */
819 unix_instantiate, /* instantiation */
821 unix_authenticate, /* authentication */
822 NULL, /* authorization */
823 NULL, /* preaccounting */
824 unix_accounting, /* accounting */
825 NULL, /* checksimul */
826 NULL, /* pre-proxy */
827 NULL, /* post-proxy */
830 unix_detach, /* detach */
831 unix_destroy, /* destroy */