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>
64 static char trans[64] =
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
66 #define ENC(c) trans[c]
68 struct unix_instance {
70 const char *passwd_file;
71 const char *shadow_file;
72 const char *group_file;
75 struct pwcache *cache;
81 static CONF_PARSER module_config[] = {
83 * Cache the password by default.
85 { "cache", PW_TYPE_BOOLEAN,
86 offsetof(struct unix_instance,cache_passwd), NULL, "yes" },
87 { "passwd", PW_TYPE_STRING_PTR,
88 offsetof(struct unix_instance,passwd_file), NULL, NULL },
89 { "shadow", PW_TYPE_STRING_PTR,
90 offsetof(struct unix_instance,shadow_file), NULL, NULL },
91 { "group", PW_TYPE_STRING_PTR,
92 offsetof(struct unix_instance,group_file), NULL, NULL },
93 { "radwtmp", PW_TYPE_STRING_PTR,
94 offsetof(struct unix_instance,radwtmp), NULL, "${logdir}/radwtmp" },
95 { "usegroup", PW_TYPE_BOOLEAN,
96 offsetof(struct unix_instance,usegroup), NULL, "no" },
97 { "cache_reload", PW_TYPE_INTEGER,
98 offsetof(struct unix_instance,cache_reload), NULL, "600" },
100 { NULL, -1, 0, NULL, NULL } /* end the list */
104 * groupcmp is part of autz. But it uses the data from an auth instance. So
105 * here is where it gets it. By default this will be the first configured
106 * auth instance. That can be changed by putting "usegroup = yes" inside an
107 * auth instance to explicitly bind all Group checks to it.
110 /* binds "Group=" to an instance (a particular passwd file) */
111 static struct unix_instance *group_inst;
113 /* Tells if the above binding was explicit (usegroup=yes specified in config
114 * file) or not ("Group=" was bound to the first instance of rlm_unix */
115 static int group_inst_explicit;
117 struct passwd *fgetpwnam(const char *fname, const char *name) {
118 FILE *file = fopen(fname, "ro");
119 struct passwd *pwd = NULL;
121 if(file == NULL) return NULL;
123 pwd = fgetpwent(file);
128 } while(strcmp(name, pwd->pw_name) != 0);
133 struct passwd *fgetgrnam(const char *fname, const char *name) {
134 FILE *file = fopen(fname, "ro");
135 struct group *grp = NULL;
136 if(file == NULL) return NULL;
138 grp = fgetgrent(file);
143 } while(strcmp(name, grp->gr_name) != 0);
149 * The Group = handler.
151 static int groupcmp(void *instance, VALUE_PAIR *request, VALUE_PAIR *check,
152 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
161 check_pairs = check_pairs;
162 reply_pairs = reply_pairs;
165 radlog(L_ERR, "groupcmp: no group list known.");
169 username = (char *)request->strvalue;
171 if (group_inst->cache_passwd &&
172 (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
175 if ((pwd = fgetpwnam(group_inst->passwd_file, username)) == NULL)
178 if ((grp = fgetgrnam(group_inst->group_file, (char *)check->strvalue)) == NULL)
181 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
183 for (member = grp->gr_mem; *member && retval; member++) {
184 if (strcmp(*member, pwd->pw_name) == 0)
193 * FIXME: We really should have an 'init' which makes
194 * System auth == Unix
196 static int unix_init(void)
198 /* FIXME - delay these until a group file has been read so we know
199 * groupcmp can actually do something */
200 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
201 #ifdef PW_GROUP_NAME /* compat */
202 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
207 static int unix_instantiate(CONF_SECTION *conf, void **instance)
209 struct unix_instance *inst;
212 * Allocate room for the instance.
214 inst = *instance = rad_malloc(sizeof(struct unix_instance));
217 * Parse the configuration, failing if we can't do so.
219 if (cf_section_parse(conf, inst, module_config) < 0) {
224 if (inst->cache_passwd) {
225 radlog(L_INFO, "HASH: Reinitializing hash structures "
226 "and lists for caching...");
227 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
229 inst->group_file))==NULL)
231 radlog(L_ERR, "HASH: unable to create user "
232 "hash table. disable caching and run debugs");
233 free((char *) inst->passwd_file);
234 free((char *) inst->shadow_file);
235 free((char *) inst->group_file);
236 free((char *) inst->radwtmp);
241 if (inst->cache_reload) {
242 inst->last_reload = 0;
243 inst->next_reload = time(NULL) + inst->cache_reload;
249 if (inst->usegroup) {
250 if (group_inst_explicit) {
251 radlog(L_ERR, "Only one group list may be active");
254 group_inst_explicit = 1;
256 } else if (!group_inst) {
267 static int unix_detach(void *instance)
269 #define inst ((struct unix_instance *)instance)
270 if (group_inst == inst) {
272 group_inst_explicit = 0;
274 free((char *) inst->passwd_file);
275 free((char *) inst->shadow_file);
276 free((char *) inst->group_file);
277 free((char *) inst->radwtmp);
279 unix_freepwcache(inst->cache);
286 static int unix_destroy(void)
288 paircompare_unregister(PW_GROUP, groupcmp);
290 paircompare_unregister(PW_GROUP_NAME, groupcmp);
297 * Check the users password against the standard UNIX
300 static int unix_authenticate(void *instance, REQUEST *request)
302 #define inst ((struct unix_instance *)instance)
306 char *encrypted_pass;
316 struct pr_passwd *pr_pw;
320 char *progname = "radius";
321 SIAENTITY *ent = NULL;
323 #ifdef HAVE_GETUSERSHELL
327 /* See if we should refresh the cache */
328 if (inst->cache && inst->cache_reload
329 && (inst->next_reload < request->timestamp)) {
330 /* Time to refresh, maybe ? */
334 DEBUG2("rlm_users : Time to refresh cache.");
335 /* Check if any of the files has changed */
336 if (inst->passwd_file
337 && (stat(inst->passwd_file, &statbuf) != -1)
338 && (statbuf.st_mtime > inst->last_reload)) {
342 if (inst->shadow_file
343 && (stat(inst->shadow_file, &statbuf) != -1)
344 && (statbuf.st_mtime > inst->last_reload)) {
349 && (stat(inst->group_file, &statbuf) != -1)
350 && (statbuf.st_mtime > inst->last_reload)) {
355 /* Build a new cache to replace old one */
356 struct pwcache *oldcache;
357 struct pwcache *newcache = unix_buildpwcache(
363 oldcache = inst->cache;
364 inst->cache = newcache;
365 unix_freepwcache(oldcache);
367 inst->last_reload = time(NULL);
370 DEBUG2("rlm_users : Files were unchanged. Not reloading.");
373 /* Schedule next refresh */
374 inst->next_reload = time(NULL) + inst->cache_reload;
378 * We can only authenticate user requests which HAVE
379 * a User-Name attribute.
381 if (!request->username) {
382 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
383 return RLM_MODULE_INVALID;
387 * We can only authenticate user requests which HAVE
388 * a Password attribute.
390 if (!request->password) {
391 radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication.");
392 return RLM_MODULE_INVALID;
396 * Ensure that we're being passed a plain-text password,
397 * and not anything else.
399 if (request->password->attribute != PW_PASSWORD) {
400 radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
401 return RLM_MODULE_INVALID;
404 name = (char *)request->username->strvalue;
405 passwd = (char *)request->password->strvalue;
407 if (inst->cache_passwd &&
408 (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
409 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
414 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
416 return RLM_MODULE_NOTFOUND;
417 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
419 sia_ses_release (&ent);
420 return RLM_MODULE_NOTFOUND;
422 if (sia_ses_estab (NULL, ent) == SIASUCCESS) {
423 sia_ses_release (&ent);
424 return RLM_MODULE_OK;
427 return RLM_MODULE_NOTFOUND;
430 if ((pr_pw = getprpwnam(name)) == NULL)
431 return RLM_MODULE_NOTFOUND;
432 encrypted_pass = pr_pw->ufld.fd_encrypt;
435 * Get encrypted password from password file
437 if ((pwd = getpwnam(name)) == NULL) {
438 return RLM_MODULE_NOTFOUND;
440 encrypted_pass = pwd->pw_passwd;
445 * See if there is a shadow password.
447 if ((spwd = getspnam(name)) != NULL)
449 encrypted_pass = spwd->pw_passwd;
451 encrypted_pass = spwd->sp_pwdp;
453 #endif /* HAVE_GETSPNAM */
457 * Undocumented temporary compatibility for iphil.NET
458 * Users with a certain shell are always denied access.
460 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
461 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
462 return RLM_MODULE_REJECT;
466 #if HAVE_GETUSERSHELL
468 * Check /etc/shells for a valid shell. If that file
469 * contains /RADIUSD/ANY/SHELL then any shell will do.
471 while ((shell = getusershell()) != NULL) {
472 if (strcmp(shell, pwd->pw_shell) == 0 ||
473 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
479 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
480 name, pws->pw_shell);
481 return RLM_MODULE_REJECT;
485 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
487 * Check if password has expired.
489 if (spwd && spwd->sp_expire > 0 &&
490 (request->timestamp / 86400) > spwd->sp_expire) {
491 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
492 return RLM_MODULE_REJECT;
496 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
498 * Check if password has expired.
500 if ((pwd->pw_expire > 0) &&
501 (request->timestamp > pwd->pw_expire)) {
502 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
503 return RLM_MODULE_REJECT;
509 * Check if account is locked.
511 if (pr_pw->uflg.fg_lock!=1) {
512 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
513 return RLM_MODULE_USERLOCK;
518 * We might have a passwordless account.
520 if (encrypted_pass[0] == 0)
521 return RLM_MODULE_OK;
524 * Check encrypted password.
526 encpw = crypt(passwd, encrypted_pass);
527 if (strcmp(encpw, encrypted_pass)) {
528 radlog(L_AUTH, "rlm_unix: [%s]: invalid password", name);
529 return RLM_MODULE_REJECT;
531 return RLM_MODULE_OK;
537 * UUencode 4 bits base64. We use this to turn a 4 byte field
538 * (an IP address) into 6 bytes of ASCII. This is used for the
539 * wtmp file if we didn't find a short name in the naslist file.
541 static char *uue(void *in)
544 static unsigned char res[7];
545 unsigned char *data = (unsigned char *)in;
547 res[0] = ENC( data[0] >> 2 );
548 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
549 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
550 res[3] = ENC( data[2] & 077 );
552 res[4] = ENC( data[3] >> 2 );
553 res[5] = ENC( (data[3] << 4) & 060 );
556 for(i = 0; i < 6; i++) {
557 if (res[i] == ' ') res[i] = '`';
558 if (res[i] < 32 || res[i] > 127)
559 printf("uue: protocol error ?!\n");
566 * Unix accounting - write a wtmp file.
568 static int unix_accounting(void *instance, REQUEST *request)
580 int framed_address = 0;
584 int nas_port_type = 0;
585 struct unix_instance *inst = (struct unix_instance *) instance;
588 * Which type is this.
590 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
591 radlog(L_ERR, "Accounting: no Accounting-Status-Type record.");
592 return RLM_MODULE_NOOP;
597 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
599 if (status != PW_STATUS_START &&
600 status != PW_STATUS_STOP)
601 return RLM_MODULE_NOOP;
604 * We're only interested in accounting messages
605 * with a username in it.
607 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
608 return RLM_MODULE_NOOP;
610 t = request->timestamp;
611 memset(&ut, 0, sizeof(ut));
614 * First, find the interesting attributes.
616 for (vp = request->packet->vps; vp; vp = vp->next) {
617 switch (vp->attribute) {
619 if (vp->length >= sizeof(ut.ut_name)) {
620 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
622 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
625 case PW_LOGIN_IP_HOST:
626 case PW_FRAMED_IP_ADDRESS:
627 framed_address = vp->lvalue;
629 case PW_FRAMED_PROTOCOL:
630 protocol = vp->lvalue;
632 case PW_NAS_IP_ADDRESS:
633 nas_address = vp->lvalue;
636 nas_port = vp->lvalue;
639 case PW_ACCT_DELAY_TIME:
642 case PW_NAS_PORT_TYPE:
643 nas_port_type = vp->lvalue;
649 * We don't store !root sessions, or sessions
650 * where we didn't see a PW_NAS_PORT_ID.
652 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
653 return RLM_MODULE_NOOP;
656 * If we didn't find out the NAS address, use the
657 * originator's IP address.
659 if (nas_address == 0)
660 nas_address = request->packet->src_ipaddr;
664 * Linux has a field for the client address.
666 ut.ut_addr = framed_address;
669 * We use the tty field to store the terminal servers' port
670 * and address so that the tty field is unique.
673 if ((cl = nas_find(nas_address)) != NULL)
675 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
676 sprintf(buf, "%03d:%s", nas_port, s);
677 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
680 * We store the dynamic IP address in the hostname field.
683 if (framed_address) {
684 ip_ntoa(buf, framed_address);
685 strncpy(ut.ut_host, buf, UT_HOSTSIZE);
689 ut.ut_xtime = t- delay;
691 ut.ut_time = t - delay;
695 * And we can use the ID field to store
698 if (protocol == PW_PPP)
699 strcpy(ut.ut_id, "P");
700 else if (protocol == PW_SLIP)
701 strcpy(ut.ut_id, "S");
703 strcpy(ut.ut_id, "T");
704 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
706 if (status == PW_STATUS_STOP)
710 * Write a RADIUS wtmp log file.
712 * Try to open the file if we can't, we don't write the
713 * wtmp file. If we can try to write. If we fail,
714 * return RLM_MODULE_FAIL ..
716 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
717 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
719 return RLM_MODULE_FAIL;
723 return RLM_MODULE_FAIL;
725 return RLM_MODULE_OK;
728 /* globally exported name */
729 module_t rlm_unix = {
731 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
732 unix_init, /* initialization */
733 unix_instantiate, /* instantiation */
735 unix_authenticate, /* authentication */
736 NULL, /* authorization */
737 NULL, /* preaccounting */
738 unix_accounting, /* accounting */
739 NULL /* checksimul */
741 unix_detach, /* detach */
742 unix_destroy, /* destroy */