*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
- * Copyright 2000 The FreeRADIUS server project
+ * Copyright 2000,2006 The FreeRADIUS server project
* Copyright 2000 Jeff Carneal <jeff@apex.net>
- * Copyright 2000 Alan Curry <pacman-radius@cqc.com>
+ * Copyright 2000 Alan Curry <pacman@world.std.com>
*/
-static const char rcsid[] = "$Id$";
-#include "autoconf.h"
-#include "libradius.h"
+#include <freeradius-devel/ident.h>
+RCSID("$Id$")
+
+#include <freeradius-devel/radiusd.h>
-#include <stdlib.h>
-#include <string.h>
#include <grp.h>
#include <pwd.h>
+#include <sys/stat.h>
#include "config.h"
-#if HAVE_MALLOC_H
-# include <malloc.h>
-#endif
-
-#if HAVE_SHADOW_H
+#ifdef HAVE_SHADOW_H
# include <shadow.h>
#endif
-#if HAVE_CRYPT_H
-# include <crypt.h>
-#endif
-
#ifdef OSFC2
# include <sys/security.h>
# include <prot.h>
#endif
-#include "radiusd.h"
-#include "modules.h"
-#include "sysutmp.h"
-#include "cache.h"
-#include "conffile.h"
+#ifdef OSFSIA
+# include <sia.h>
+# include <siad.h>
+#endif
+
+#include <freeradius-devel/modules.h>
+#include <freeradius-devel/sysutmp.h>
static char trans[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define ENC(c) trans[c]
struct unix_instance {
- int cache_passwd;
- const char *passwd_file;
- const char *shadow_file;
- const char *group_file;
const char *radwtmp;
- int usegroup;
- struct pwcache *cache;
};
-static struct unix_instance config;
+static const CONF_PARSER module_config[] = {
+ { "radwtmp", PW_TYPE_STRING_PTR,
+ offsetof(struct unix_instance,radwtmp), NULL, "NULL" },
-static CONF_PARSER module_config[] = {
- /*
- * Cache the password by default.
- */
- { "cache", PW_TYPE_BOOLEAN, &config.cache_passwd, "yes" },
- { "passwd", PW_TYPE_STRING_PTR, &config.passwd_file, NULL },
- { "shadow", PW_TYPE_STRING_PTR, &config.shadow_file, NULL },
- { "group", PW_TYPE_STRING_PTR, &config.group_file, NULL },
- { "radwtmp", PW_TYPE_STRING_PTR, &config.radwtmp, "${logdir}/radwtmp" },
- { "usegroup", PW_TYPE_BOOLEAN, &config.usegroup, "no" },
-
- { NULL, -1, NULL, NULL } /* end the list */
+ { NULL, -1, 0, NULL, NULL } /* end the list */
};
-/*
- * groupcmp is part of autz. But it uses the data from an auth instance. So
- * here is where it gets it. By default this will be the first configured
- * auth instance. That can be changed by putting "usegroup = yes" inside an
- * auth instance to explicitly bind all Group checks to it.
- */
-
-/* binds "Group=" to an instance (a particular passwd file) */
-static struct unix_instance *group_inst;
-
-/* Tells if the above binding was explicit (usegroup=yes specified in config
- * file) or not ("Group=" was bound to the first instance of rlm_unix */
-static int group_inst_explicit;
/*
* The Group = handler.
*/
-static int groupcmp(void *instance, VALUE_PAIR *request, VALUE_PAIR *check,
- VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
+static int groupcmp(void *instance, UNUSED REQUEST *req, VALUE_PAIR *request,
+ VALUE_PAIR *check, VALUE_PAIR *check_pairs,
+ VALUE_PAIR **reply_pairs)
{
struct passwd *pwd;
struct group *grp;
char **member;
char *username;
int retval;
+ VALUE_PAIR *vp;
instance = instance;
check_pairs = check_pairs;
reply_pairs = reply_pairs;
- if (!group_inst) {
- radlog(L_ERR, "groupcmp: no group list known.");
- return 1;
+ /*
+ * No user name, doesn't compare.
+ */
+ vp = pairfind(request, PW_STRIPPED_USER_NAME);
+ if (!vp) {
+ vp = pairfind(request, PW_USER_NAME);
+ if (!vp) {
+ return -1;
+ }
}
+ username = (char *)vp->vp_strvalue;
- username = (char *)request->strvalue;
-
- if (group_inst->cache_passwd &&
- (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
- return retval;
-
- if ((pwd = getpwnam(username)) == NULL)
+ pwd = getpwnam(username);
+ if (pwd == NULL)
return -1;
- if ((grp = getgrnam((char *)check->strvalue)) == NULL)
+ grp = getgrnam((char *)check->vp_strvalue);
+ if (grp == NULL)
return -1;
retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
/*
- * FIXME: We really should have an 'init' which makes
- * System auth == Unix
+ * Detach.
*/
-static int unix_init(void)
+static int unix_detach(void *instance)
{
- /* FIXME - delay these until a group file has been read so we know
- * groupcmp can actually do something */
- paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
-#ifdef PW_GROUP_NAME /* compat */
- paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
+#define inst ((struct unix_instance *)instance)
+
+ paircompare_unregister(PW_GROUP, groupcmp);
+#ifdef PW_GROUP_NAME
+ paircompare_unregister(PW_GROUP_NAME, groupcmp);
#endif
+#undef inst
+ free(instance);
return 0;
}
+/*
+ * Read the config
+ */
static int unix_instantiate(CONF_SECTION *conf, void **instance)
{
struct unix_instance *inst;
/*
- * Parse the configuration, failing if we can't do so.
- */
- if (cf_section_parse(conf, module_config) < 0) {
- return -1;
- }
-
- /*
* Allocate room for the instance.
*/
- inst = *instance = malloc(sizeof(struct unix_instance));
- if(!inst) {
- return -1;
+ inst = *instance = rad_malloc(sizeof(*inst));
+ if (!inst) {
+ return -1;
}
+ memset(inst, 0, sizeof(*inst));
/*
- * Copy the configuration into the instance data
+ * Parse the configuration, failing if we can't do so.
*/
- inst->cache_passwd = config.cache_passwd;
- inst->passwd_file = config.passwd_file;
- inst->shadow_file = config.shadow_file;
- inst->group_file = config.group_file;
- inst->radwtmp = config.radwtmp;
- inst->usegroup = config.usegroup;
- config.passwd_file = NULL;
- config.shadow_file = NULL;
- config.group_file = NULL;
- config.radwtmp = NULL;
-
- if (inst->cache_passwd) {
- radlog(L_INFO, "HASH: Reinitializing hash structures "
- "and lists for caching...");
- if ((inst->cache = unix_buildpwcache(inst->passwd_file,
- inst->shadow_file))==NULL)
- {
- radlog(L_ERR, "HASH: unable to create user "
- "hash table. disable caching and run debugs");
- free((char *) inst->passwd_file);
- free((char *) inst->shadow_file);
- free((char *) inst->group_file);
- free((char *) inst->radwtmp);
- free(inst);
- return -1;
- }
- } else {
- inst->cache = NULL;
- }
-
- if (inst->usegroup) {
- if (group_inst_explicit) {
- radlog(L_ERR, "Only one group list may be active");
- } else {
- group_inst = inst;
- group_inst_explicit = 1;
- }
- } else if (!group_inst) {
- group_inst = inst;
+ if (cf_section_parse(conf, inst, module_config) < 0) {
+ unix_detach(inst);
+ return -1;
}
-#undef inst
- return 0;
-}
+ /* FIXME - delay these until a group file has been read so we know
+ * groupcmp can actually do something */
+ paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
+#ifdef PW_GROUP_NAME /* compat */
+ paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
+#endif
-/*
- * Detach.
- */
-static int unix_detach(void *instance)
-{
-#define inst ((struct unix_instance *)instance)
- if (group_inst == inst) {
- group_inst = NULL;
- group_inst_explicit = 0;
- }
- free((char *) inst->passwd_file);
- free((char *) inst->shadow_file);
- free((char *) inst->group_file);
- free((char *) inst->radwtmp);
- if (inst->cache) {
- unix_freepwcache(inst->cache);
- }
#undef inst
- free(instance);
- return 0;
-}
-static int unix_destroy(void)
-{
- paircompare_unregister(PW_GROUP, groupcmp);
-#ifdef PW_GROUP_NAME
- paircompare_unregister(PW_GROUP_NAME, groupcmp);
-#endif
return 0;
}
/*
- * Check the users password against the standard UNIX
- * password table.
+ * Pull the users password from where-ever, and add it to
+ * the given vp list.
*/
-static int unix_authenticate(void *instance, REQUEST *request)
+static int unix_getpw(UNUSED void *instance, REQUEST *request,
+ VALUE_PAIR **vp_list)
{
-#define inst ((struct unix_instance *)instance)
- char *name, *passwd;
- struct passwd *pwd;
- char *encpw;
- char *encrypted_pass;
- int ret;
-#if HAVE_GETSPNAM
-#if defined(M_UNIX)
- struct passwd *spwd;
-#else
- struct spwd *spwd;
-#endif
+ const char *name;
+ const char *encrypted_pass;
+#ifdef HAVE_GETSPNAM
+ struct spwd *spwd = NULL;
#endif
#ifdef OSFC2
struct pr_passwd *pr_pw;
+#else
+ struct passwd *pwd;
#endif
#ifdef HAVE_GETUSERSHELL
char *shell;
#endif
+ VALUE_PAIR *vp;
/*
* We can only authenticate user requests which HAVE
* a User-Name attribute.
*/
if (!request->username) {
- radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
- return RLM_MODULE_INVALID;
- }
-
- /*
- * We can only authenticate user requests which HAVE
- * a Password attribute.
- */
- if (!request->password) {
- radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication.");
- return RLM_MODULE_INVALID;
- }
-
- /*
- * Ensure that we're being passed a plain-text password,
- * and not anything else.
- */
- if (request->password->attribute != PW_PASSWORD) {
- radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
- return RLM_MODULE_INVALID;
+ return RLM_MODULE_NOOP;
}
- name = (char *)request->username->strvalue;
- passwd = (char *)request->password->strvalue;
-
- if (inst->cache_passwd &&
- (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
- return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
+ name = (char *)request->username->vp_strvalue;
+ encrypted_pass = NULL;
#ifdef OSFC2
if ((pr_pw = getprpwnam(name)) == NULL)
return RLM_MODULE_NOTFOUND;
encrypted_pass = pr_pw->ufld.fd_encrypt;
-#else /* OSFC2 */
+
/*
- * Get encrypted password from password file
+ * Check if account is locked.
*/
+ if (pr_pw->uflg.fg_lock!=1) {
+ radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
+ return RLM_MODULE_USERLOCK;
+ }
+#else /* OSFC2 */
if ((pwd = getpwnam(name)) == NULL) {
return RLM_MODULE_NOTFOUND;
}
encrypted_pass = pwd->pw_passwd;
#endif /* OSFC2 */
-#if HAVE_GETSPNAM
+#ifdef HAVE_GETSPNAM
/*
* See if there is a shadow password.
+ *
+ * Only query the _system_ shadow file if the encrypted
+ * password from the passwd file is < 10 characters (i.e.
+ * a valid password would never crypt() to it). This will
+ * prevents users from using NULL password fields as things
+ * stand right now.
*/
- if ((spwd = getspnam(name)) != NULL)
-#if defined(M_UNIX)
- encrypted_pass = spwd->pw_passwd;
-#else
+ if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
+ if ((spwd = getspnam(name)) == NULL) {
+ return RLM_MODULE_NOTFOUND;
+ }
encrypted_pass = spwd->sp_pwdp;
-#endif /* M_UNIX */
+ }
#endif /* HAVE_GETSPNAM */
+/*
+ * These require 'pwd != NULL', which isn't true on OSFC2
+ */
+#ifndef OSFC2
#ifdef DENY_SHELL
/*
- * Undocumented temporary compatibility for iphil.NET
- * Users with a certain shell are always denied access.
+ * Users with a particular shell are denied access
*/
if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
}
#endif
-#if HAVE_GETUSERSHELL
+#ifdef HAVE_GETUSERSHELL
/*
* Check /etc/shells for a valid shell. If that file
* contains /RADIUSD/ANY/SHELL then any shell will do.
}
}
endusershell();
- if (shell == NULL)
+ if (shell == NULL) {
+ radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
+ name, pwd->pw_shell);
return RLM_MODULE_REJECT;
+ }
#endif
+#endif /* OSFC2 */
#if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
/*
}
#endif
-#ifdef OSFC2
- /*
- * Check if account is locked.
- */
- if (pr_pw->uflg.fg_lock!=1) {
- radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
- return RLM_MODULE_USERLOCK;
- }
-#endif /* OSFC2 */
-
/*
* We might have a passwordless account.
+ *
+ * FIXME: Maybe add Auth-Type := Accept?
*/
if (encrypted_pass[0] == 0)
- return RLM_MODULE_OK;
+ return RLM_MODULE_NOOP;
+
+ vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
+ if (!vp) return RLM_MODULE_FAIL;
+
+ pairmove(vp_list, &vp);
+ pairfree(&vp); /* might not be NULL; */
+
+ return RLM_MODULE_UPDATED;
+}
+
+
+/*
+ * Pull the users password from where-ever, and add it to
+ * the given vp list.
+ */
+static int unix_authorize(void *instance, REQUEST *request)
+{
+ return unix_getpw(instance, request, &request->config_items);
+}
+
+/*
+ * Pull the users password from where-ever, and add it to
+ * the given vp list.
+ */
+static int unix_authenticate(void *instance, REQUEST *request)
+{
+#ifdef OSFSIA
+ char *info[2];
+ char *progname = "radius";
+ SIAENTITY *ent = NULL;
+
+ info[0] = progname;
+ info[1] = NULL;
+ if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
+ SIASUCCESS)
+ return RLM_MODULE_NOTFOUND;
+ if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
+ if (ret & SIASTOP)
+ sia_ses_release (&ent);
+ return RLM_MODULE_NOTFOUND;
+ }
+ if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
+ sia_ses_release (&ent);
+ return RLM_MODULE_NOTFOUND;
+ }
+#else /* OSFSIA */
+ int rcode;
+ VALUE_PAIR *vp = NULL;
+
+ if (!request->password ||
+ (request->password->attribute != PW_USER_PASSWORD)) {
+ radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
+ return RLM_MODULE_INVALID;
+ }
+
+ rcode = unix_getpw(instance, request, &vp);
+ if (rcode != RLM_MODULE_UPDATED) return rcode;
/*
- * Check encrypted password.
+ * 0 means "ok"
*/
- encpw = crypt(passwd, encrypted_pass);
- if (strcmp(encpw, encrypted_pass))
+ if (lrad_crypt_check((char *) request->password->vp_strvalue,
+ (char *) vp->vp_strvalue) != 0) {
+ radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
+ request->username->vp_strvalue);
return RLM_MODULE_REJECT;
+ }
+#endif /* OSFFIA */
return RLM_MODULE_OK;
-#undef inst
}
+
/*
* UUencode 4 bits base64. We use this to turn a 4 byte field
* (an IP address) into 6 bytes of ASCII. This is used for the
static int unix_accounting(void *instance, REQUEST *request)
{
VALUE_PAIR *vp;
- NAS *cl;
FILE *fp;
struct utmp ut;
time_t t;
struct unix_instance *inst = (struct unix_instance *) instance;
/*
+ * No radwtmp. Don't do anything.
+ */
+ if (!inst->radwtmp) {
+ DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
+ return RLM_MODULE_NOOP;
+ }
+
+ if (request->packet->src_ipaddr.af != AF_INET) {
+ DEBUG2("rlm_unix: IPv6 is not supported!");
+ return RLM_MODULE_NOOP;
+ }
+
+ /*
* Which type is this.
*/
if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
- radlog(L_ERR, "Accounting: no Accounting-Status-Type record.");
+ radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
return RLM_MODULE_NOOP;
}
status = vp->lvalue;
for (vp = request->packet->vps; vp; vp = vp->next) {
switch (vp->attribute) {
case PW_USER_NAME:
- strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
+ if (vp->length >= sizeof(ut.ut_name)) {
+ memcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
+ } else {
+ strlcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
+ }
break;
case PW_LOGIN_IP_HOST:
case PW_FRAMED_IP_ADDRESS:
case PW_NAS_IP_ADDRESS:
nas_address = vp->lvalue;
break;
- case PW_NAS_PORT_ID:
+ case PW_NAS_PORT:
nas_port = vp->lvalue;
port_seen = 1;
break;
/*
* We don't store !root sessions, or sessions
- * where we didn't see a PW_NAS_PORT_ID.
+ * where we didn't see a NAS-Port attribute.
*/
if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
return RLM_MODULE_NOOP;
+ s = "";
+
/*
* If we didn't find out the NAS address, use the
* originator's IP address.
*/
- if (nas_address == 0)
- nas_address = request->packet->src_ipaddr;
-
+ if (nas_address == 0) {
+ RADCLIENT *cl;
+ nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
+
+ if ((cl = client_find_old(&request->packet->src_ipaddr)) != NULL)
+ s = cl->shortname;
+ }
+ if (!s || s[0] == 0) s = uue(&(nas_address));
+
#ifdef __linux__
/*
* Linux has a field for the client address.
* We use the tty field to store the terminal servers' port
* and address so that the tty field is unique.
*/
- s = "";
- if ((cl = nas_find(nas_address)) != NULL)
- s = cl->shortname;
- if (s == NULL || s[0] == 0) s = uue(&(nas_address));
sprintf(buf, "%03d:%s", nas_port, s);
- strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
+ strlcpy(ut.ut_line, buf, sizeof(ut.ut_line));
/*
* We store the dynamic IP address in the hostname field.
#ifdef UT_HOSTSIZE
if (framed_address) {
ip_ntoa(buf, framed_address);
- strncpy(ut.ut_host, buf, UT_HOSTSIZE);
+ strlcpy(ut.ut_host, buf, sizeof(ut.ut_host));
}
#endif
#ifdef HAVE_UTMPX_H
* return RLM_MODULE_FAIL ..
*/
if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
- if ((fwrite(&ut, sizeof(ut), 1, fp)) != sizeof(ut)) {
+ if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
fclose(fp);
return RLM_MODULE_FAIL;
}
fclose(fp);
- } else
+ } else
return RLM_MODULE_FAIL;
return RLM_MODULE_OK;
/* globally exported name */
module_t rlm_unix = {
- "System",
- 0, /* type: reserved */
- unix_init, /* initialization */
- unix_instantiate, /* instantiation */
- NULL, /* authorization */
- unix_authenticate, /* authentication */
- NULL, /* preaccounting */
- unix_accounting, /* accounting */
- NULL, /* checksimul */
- unix_detach, /* detach */
- unix_destroy, /* destroy */
+ RLM_MODULE_INIT,
+ "System",
+ RLM_TYPE_THREAD_UNSAFE, /* type */
+ unix_instantiate, /* instantiation */
+ unix_detach, /* detach */
+ {
+ unix_authenticate, /* authentication */
+ unix_authorize, /* authorization */
+ NULL, /* preaccounting */
+ unix_accounting, /* accounting */
+ NULL, /* checksimul */
+ NULL, /* pre-proxy */
+ NULL, /* post-proxy */
+ NULL /* post-auth */
+ },
};
-