2 * rlm_unix.c authentication: Unix user authentication
3 * accounting: Functions to write radwtmp file.
4 * Also contains handler for "Group".
9 static const char rcsid[] = "$Id$";
33 # include <sys/security.h>
43 static char trans[64] =
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45 #define ENC(c) trans[c]
47 struct unix_instance {
53 struct pwcache *cache;
56 static struct unix_instance config;
58 static CONF_PARSER module_config[] = {
60 * Cache the password by default.
62 { "cache", PW_TYPE_BOOLEAN, &config.cache_passwd, "yes" },
63 { "passwd", PW_TYPE_STRING_PTR, &config.passwd_file, NULL },
64 { "shadow", PW_TYPE_STRING_PTR, &config.shadow_file, NULL },
65 { "group", PW_TYPE_STRING_PTR, &config.group_file, NULL },
66 { "usegroup", PW_TYPE_BOOLEAN, &config.usegroup, "no" },
68 { NULL, -1, NULL, NULL } /* end the list */
72 * groupcmp is part of autz. But it uses the data from an auth instance. So
73 * here is where it gets it. By default this will be the first configured
74 * auth instance. That can be changed by putting "usegroup = yes" inside an
75 * auth instance to explicitly bind all Group checks to it.
78 /* binds "Group=" to an instance (a particular passwd file) */
79 static struct unix_instance *group_inst;
81 /* Tells if the above binding was explicit (usegroup=yes specified in config
82 * file) or not ("Group=" was bound to the first instance of rlm_unix */
83 static int group_inst_explicit;
86 * The Group = handler.
88 static int groupcmp(VALUE_PAIR *request, VALUE_PAIR *check,
89 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
96 check_pairs = check_pairs; reply_pairs = reply_pairs;
99 radlog(L_ERR, "groupcmp: no group list known.");
103 username = (char *)request->strvalue;
105 if (group_inst->cache_passwd &&
106 (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
109 if ((pwd = getpwnam(username)) == NULL)
112 if ((grp = getgrnam((char *)check->strvalue)) == NULL)
115 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
117 for (member = grp->gr_mem; *member && retval; member++) {
118 if (strcmp(*member, pwd->pw_name) == 0)
127 * FIXME: We really should have an 'init' which makes
128 * System auth == Unix
130 static int unix_init(void)
132 /* FIXME - delay these until a group file has been read so we know
133 * groupcmp can actually do something */
134 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp);
135 #ifdef PW_GROUP_NAME /* compat */
136 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp);
141 static int unix_instantiate(CONF_SECTION *conf, void **instance)
143 *instance = malloc(sizeof(struct unix_instance));
148 #define inst ((struct unix_instance *)*instance)
149 if (cf_section_parse(conf, module_config) < 0) {
155 * Copy the configuration into the instance data
157 inst->cache_passwd = config.cache_passwd;
158 inst->passwd_file = config.passwd_file;
159 inst->shadow_file = config.shadow_file;
160 inst->group_file = config.group_file;
161 inst->usegroup = config.usegroup;
162 config.passwd_file = NULL;
163 config.shadow_file = NULL;
164 config.group_file = NULL;
166 if (inst->cache_passwd) {
167 radlog(L_INFO, "HASH: Reinitializing hash structures "
168 "and lists for caching...");
169 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
170 inst->shadow_file))==NULL)
172 radlog(L_ERR, "HASH: unable to create user "
173 "hash table. disable caching and run debugs");
179 if (inst->usegroup) {
180 if (group_inst_explicit) {
181 radlog(L_ERR, "Only one group list may be active");
184 group_inst_explicit = 1;
186 } else if (!group_inst) {
197 static int unix_detach(void *instance)
199 #define inst ((struct unix_instance *)instance)
200 if (group_inst == inst) {
202 group_inst_explicit = 0;
204 free(inst->passwd_file);
205 free(inst->shadow_file);
206 free(inst->group_file);
208 unix_freepwcache(inst->cache);
215 static int unix_destroy(void)
217 paircompare_unregister(PW_GROUP, groupcmp);
219 paircompare_unregister(PW_GROUP_NAME, groupcmp);
226 * Check the users password against the standard UNIX
229 static int unix_authenticate(void *instance, REQUEST *request)
231 #define inst ((struct unix_instance *)instance)
235 char *encrypted_pass;
245 struct pr_passwd *pr_pw;
247 #ifdef HAVE_GETUSERSHELL
253 * We can only authenticate user requests which HAVE
254 * a User-Name attribute.
256 if (!request->username) {
257 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
258 return RLM_MODULE_INVALID;
262 * We can only authenticate user requests which HAVE
263 * a Password attribute.
265 if (!request->password) {
266 radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication.");
267 return RLM_MODULE_INVALID;
271 * Ensure that we're being passed a plain-text password,
272 * and not anything else.
274 if (request->password->attribute != PW_PASSWORD) {
275 radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
276 return RLM_MODULE_INVALID;
279 name = (char *)request->username->strvalue;
280 passwd = (char *)request->password->strvalue;
282 if (inst->cache_passwd &&
283 (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
284 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
287 if ((pr_pw = getprpwnam(name)) == NULL)
288 return RLM_MODULE_NOTFOUND;
289 encrypted_pass = pr_pw->ufld.fd_encrypt;
292 * Get encrypted password from password file
294 if ((pwd = getpwnam(name)) == NULL) {
295 return RLM_MODULE_NOTFOUND;
297 encrypted_pass = pwd->pw_passwd;
302 * See if there is a shadow password.
304 if ((spwd = getspnam(name)) != NULL)
306 encrypted_pass = spwd->pw_passwd;
308 encrypted_pass = spwd->sp_pwdp;
310 #endif /* HAVE_GETSPNAM */
314 * Undocumented temporary compatibility for iphil.NET
315 * Users with a certain shell are always denied access.
317 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
318 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
319 return RLM_MODULE_REJECT;
323 #if HAVE_GETUSERSHELL
325 * Check /etc/shells for a valid shell. If that file
326 * contains /RADIUSD/ANY/SHELL then any shell will do.
328 while ((shell = getusershell()) != NULL) {
329 if (strcmp(shell, pwd->pw_shell) == 0 ||
330 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
336 return RLM_MODULE_REJECT;
339 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
341 * Check if password has expired.
343 if (spwd && spwd->sp_expire > 0 &&
344 (request->timestamp / 86400) > spwd->sp_expire) {
345 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
346 return RLM_MODULE_REJECT;
350 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
352 * Check if password has expired.
354 if ((pwd->pw_expire > 0) &&
355 (request->timestamp > pwd->pw_expire)) {
356 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
357 return RLM_MODULE_REJECT;
363 * Check if account is locked.
365 if (pr_pw->uflg.fg_lock!=1) {
366 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
367 return RLM_MODULE_USERLOCK;
372 * We might have a passwordless account.
374 if (encrypted_pass[0] == 0)
375 return RLM_MODULE_OK;
378 * Check encrypted password.
380 encpw = crypt(passwd, encrypted_pass);
381 if (strcmp(encpw, encrypted_pass))
382 return RLM_MODULE_REJECT;
384 return RLM_MODULE_OK;
389 * UUencode 4 bits base64. We use this to turn a 4 byte field
390 * (an IP address) into 6 bytes of ASCII. This is used for the
391 * wtmp file if we didn't find a short name in the naslist file.
393 static char *uue(void *in)
396 static unsigned char res[7];
397 unsigned char *data = (unsigned char *)in;
399 res[0] = ENC( data[0] >> 2 );
400 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
401 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
402 res[3] = ENC( data[2] & 077 );
404 res[4] = ENC( data[3] >> 2 );
405 res[5] = ENC( (data[3] << 4) & 060 );
408 for(i = 0; i < 6; i++) {
409 if (res[i] == ' ') res[i] = '`';
410 if (res[i] < 32 || res[i] > 127)
411 printf("uue: protocol error ?!\n");
418 * Unix accounting - write a wtmp file.
420 static int unix_accounting(void *instance, REQUEST *request)
432 int framed_address = 0;
436 int nas_port_type = 0;
441 * Which type is this.
443 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
444 radlog(L_ERR, "Accounting: no Accounting-Status-Type record.");
445 return RLM_MODULE_NOOP;
450 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
452 if (status != PW_STATUS_START &&
453 status != PW_STATUS_STOP)
454 return RLM_MODULE_NOOP;
457 * We're only interested in accounting messages
458 * with a username in it.
460 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
461 return RLM_MODULE_NOOP;
463 t = request->timestamp;
464 memset(&ut, 0, sizeof(ut));
467 * First, find the interesting attributes.
469 for (vp = request->packet->vps; vp; vp = vp->next) {
470 switch (vp->attribute) {
472 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
474 case PW_LOGIN_IP_HOST:
475 case PW_FRAMED_IP_ADDRESS:
476 framed_address = vp->lvalue;
478 case PW_FRAMED_PROTOCOL:
479 protocol = vp->lvalue;
481 case PW_NAS_IP_ADDRESS:
482 nas_address = vp->lvalue;
485 nas_port = vp->lvalue;
488 case PW_ACCT_DELAY_TIME:
491 case PW_NAS_PORT_TYPE:
492 nas_port_type = vp->lvalue;
498 * We don't store !root sessions, or sessions
499 * where we didn't see a PW_NAS_PORT_ID.
501 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
502 return RLM_MODULE_NOOP;
505 * If we didn't find out the NAS address, use the
506 * originator's IP address.
508 if (nas_address == 0)
509 nas_address = request->packet->src_ipaddr;
513 * Linux has a field for the client address.
515 ut.ut_addr = framed_address;
518 * We use the tty field to store the terminal servers' port
519 * and address so that the tty field is unique.
522 if ((cl = nas_find(nas_address)) != NULL)
524 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
525 sprintf(buf, "%03d:%s", nas_port, s);
526 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
529 * We store the dynamic IP address in the hostname field.
532 if (framed_address) {
533 ip_ntoa(buf, framed_address);
534 strncpy(ut.ut_host, buf, UT_HOSTSIZE);
538 ut.ut_xtime = t- delay;
540 ut.ut_time = t - delay;
544 * And we can use the ID field to store
547 if (protocol == PW_PPP)
548 strcpy(ut.ut_id, "P");
549 else if (protocol == PW_SLIP)
550 strcpy(ut.ut_id, "S");
552 strcpy(ut.ut_id, "T");
553 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
555 if (status == PW_STATUS_STOP)
559 * Write a RADIUS wtmp log file.
560 * FIXME: return correct error.
561 * Check if file is there. If not, we don't write the
562 * wtmp file. If it is, we try to write. If we fail,
563 * return RLM_MODULE_FAIL ..
565 if ((fp = fopen(RADWTMP, "a")) != NULL) {
566 fwrite(&ut, sizeof(ut), 1, fp);
570 return RLM_MODULE_OK;
573 /* globally exported name */
574 module_t rlm_unix = {
576 0, /* type: reserved */
577 unix_init, /* initialization */
578 unix_instantiate, /* instantiation */
579 NULL, /* authorization */
580 unix_authenticate, /* authentication */
581 NULL, /* preaccounting */
582 unix_accounting, /* accounting */
583 unix_detach, /* detach */
584 unix_destroy, /* destroy */