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 ((pwd = fgetpwnam(group_inst->passwd_file, username)) == NULL)
224 if ((grp = fgetgrnam(group_inst->group_file, (char *)check->strvalue)) == NULL)
227 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
229 for (member = grp->gr_mem; *member && retval; member++) {
230 if (strcmp(*member, pwd->pw_name) == 0)
239 * FIXME: We really should have an 'init' which makes
240 * System auth == Unix
242 static int unix_init(void)
244 /* FIXME - delay these until a group file has been read so we know
245 * groupcmp can actually do something */
246 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
247 #ifdef PW_GROUP_NAME /* compat */
248 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
253 static int unix_instantiate(CONF_SECTION *conf, void **instance)
255 struct unix_instance *inst;
258 * Allocate room for the instance.
260 inst = *instance = rad_malloc(sizeof(struct unix_instance));
263 * Parse the configuration, failing if we can't do so.
265 if (cf_section_parse(conf, inst, module_config) < 0) {
270 if (inst->cache_passwd) {
271 radlog(L_INFO, "HASH: Reinitializing hash structures "
272 "and lists for caching...");
273 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
275 inst->group_file))==NULL)
277 radlog(L_ERR, "HASH: unable to create user "
278 "hash table. disable caching and run debugs");
279 free((char *) inst->passwd_file);
280 free((char *) inst->shadow_file);
281 free((char *) inst->group_file);
282 free((char *) inst->radwtmp);
287 if (inst->cache_reload) {
288 inst->last_reload = 0;
289 inst->next_reload = time(NULL) + inst->cache_reload;
295 if (inst->usegroup) {
296 if (group_inst_explicit) {
297 radlog(L_ERR, "Only one group list may be active");
300 group_inst_explicit = 1;
302 } else if (!group_inst) {
313 static int unix_detach(void *instance)
315 #define inst ((struct unix_instance *)instance)
316 if (group_inst == inst) {
318 group_inst_explicit = 0;
320 free((char *) inst->passwd_file);
321 free((char *) inst->shadow_file);
322 free((char *) inst->group_file);
323 free((char *) inst->radwtmp);
325 unix_freepwcache(inst->cache);
332 static int unix_destroy(void)
334 paircompare_unregister(PW_GROUP, groupcmp);
336 paircompare_unregister(PW_GROUP_NAME, groupcmp);
343 * Check the users password against the standard UNIX
346 static int unix_authenticate(void *instance, REQUEST *request)
348 #define inst ((struct unix_instance *)instance)
352 char *encrypted_pass;
355 shadow_pwd_t *spwd = NULL;
358 struct pr_passwd *pr_pw;
362 char *progname = "radius";
363 SIAENTITY *ent = NULL;
365 #ifdef HAVE_GETUSERSHELL
369 /* See if we should refresh the cache */
370 if (inst->cache && inst->cache_reload
371 && (inst->next_reload < request->timestamp)) {
372 /* Time to refresh, maybe ? */
376 DEBUG2("rlm_users : Time to refresh cache.");
377 /* Check if any of the files has changed */
378 if (inst->passwd_file
379 && (stat(inst->passwd_file, &statbuf) != -1)
380 && (statbuf.st_mtime > inst->last_reload)) {
384 if (inst->shadow_file
385 && (stat(inst->shadow_file, &statbuf) != -1)
386 && (statbuf.st_mtime > inst->last_reload)) {
391 && (stat(inst->group_file, &statbuf) != -1)
392 && (statbuf.st_mtime > inst->last_reload)) {
397 /* Build a new cache to replace old one */
398 struct pwcache *oldcache;
399 struct pwcache *newcache = unix_buildpwcache(
405 oldcache = inst->cache;
406 inst->cache = newcache;
407 unix_freepwcache(oldcache);
409 inst->last_reload = time(NULL);
412 DEBUG2("rlm_users : Files were unchanged. Not reloading.");
415 /* Schedule next refresh */
416 inst->next_reload = time(NULL) + inst->cache_reload;
420 * We can only authenticate user requests which HAVE
421 * a User-Name attribute.
423 if (!request->username) {
424 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
425 return RLM_MODULE_INVALID;
429 * We can only authenticate user requests which HAVE
430 * a User-Password attribute.
432 if (!request->password) {
433 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
434 return RLM_MODULE_INVALID;
438 * Ensure that we're being passed a plain-text password,
439 * and not anything else.
441 if (request->password->attribute != PW_PASSWORD) {
442 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
443 return RLM_MODULE_INVALID;
446 name = (char *)request->username->strvalue;
447 passwd = (char *)request->password->strvalue;
449 if (inst->cache_passwd &&
450 (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
451 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
456 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
458 return RLM_MODULE_NOTFOUND;
459 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
461 sia_ses_release (&ent);
462 return RLM_MODULE_NOTFOUND;
464 if (sia_ses_estab (NULL, ent) == SIASUCCESS) {
465 sia_ses_release (&ent);
466 return RLM_MODULE_OK;
469 return RLM_MODULE_NOTFOUND;
472 if ((pr_pw = getprpwnam(name)) == NULL)
473 return RLM_MODULE_NOTFOUND;
474 encrypted_pass = pr_pw->ufld.fd_encrypt;
477 * Get encrypted password from password file
479 * If a password file was explicitly specified, use it,
480 * otherwise, use the system routines to read the
481 * system password file
483 if (inst->passwd_file != NULL) {
484 if ((pwd = fgetpwnam(inst->passwd_file, name)) == NULL)
485 return RLM_MODULE_NOTFOUND;
486 } else if ((pwd = getpwnam(name)) == NULL) {
487 return RLM_MODULE_NOTFOUND;
489 encrypted_pass = pwd->pw_passwd;
494 * See if there is a shadow password.
496 * If a shadow file was explicitly specified, use it,
497 * otherwise, use the system routines to read the
498 * system shadow file.
500 * Also, if we explicitly specify the password file,
501 * only query the _system_ shadow file if the encrypted
502 * password from the passwd file is < 10 characters (i.e.
503 * a valid password would never crypt() to it). This will
504 * prevents users from using NULL password fields as things
507 if (inst->shadow_file != NULL) {
508 if ((spwd = fgetspnam(inst->shadow_file, name)) == NULL)
509 encrypted_pass = get_shadow_encrypted_pwd(spwd);
510 } else if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
511 if ((spwd = getspnam(name)) != NULL)
512 encrypted_pass = get_shadow_encrypted_pwd(spwd);
514 #endif /* HAVE_GETSPNAM */
518 * Undocumented temporary compatibility for iphil.NET
519 * Users with a certain shell are always denied access.
521 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
522 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
523 return RLM_MODULE_REJECT;
527 #if HAVE_GETUSERSHELL
529 * Check /etc/shells for a valid shell. If that file
530 * contains /RADIUSD/ANY/SHELL then any shell will do.
532 while ((shell = getusershell()) != NULL) {
533 if (strcmp(shell, pwd->pw_shell) == 0 ||
534 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
540 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
541 name, pwd->pw_shell);
542 return RLM_MODULE_REJECT;
546 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
548 * Check if password has expired.
550 if (spwd && spwd->sp_expire > 0 &&
551 (request->timestamp / 86400) > spwd->sp_expire) {
552 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
553 return RLM_MODULE_REJECT;
557 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
559 * Check if password has expired.
561 if ((pwd->pw_expire > 0) &&
562 (request->timestamp > pwd->pw_expire)) {
563 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
564 return RLM_MODULE_REJECT;
570 * Check if account is locked.
572 if (pr_pw->uflg.fg_lock!=1) {
573 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
574 return RLM_MODULE_USERLOCK;
579 * We might have a passwordless account.
581 if (encrypted_pass[0] == 0)
582 return RLM_MODULE_OK;
585 * Check encrypted password.
587 encpw = crypt(passwd, encrypted_pass);
588 if (strcmp(encpw, encrypted_pass)) {
589 radlog(L_AUTH, "rlm_unix: [%s]: invalid password", name);
590 return RLM_MODULE_REJECT;
592 return RLM_MODULE_OK;
598 * UUencode 4 bits base64. We use this to turn a 4 byte field
599 * (an IP address) into 6 bytes of ASCII. This is used for the
600 * wtmp file if we didn't find a short name in the naslist file.
602 static char *uue(void *in)
605 static unsigned char res[7];
606 unsigned char *data = (unsigned char *)in;
608 res[0] = ENC( data[0] >> 2 );
609 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
610 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
611 res[3] = ENC( data[2] & 077 );
613 res[4] = ENC( data[3] >> 2 );
614 res[5] = ENC( (data[3] << 4) & 060 );
617 for(i = 0; i < 6; i++) {
618 if (res[i] == ' ') res[i] = '`';
619 if (res[i] < 32 || res[i] > 127)
620 printf("uue: protocol error ?!\n");
627 * Unix accounting - write a wtmp file.
629 static int unix_accounting(void *instance, REQUEST *request)
641 int framed_address = 0;
645 int nas_port_type = 0;
646 struct unix_instance *inst = (struct unix_instance *) instance;
649 * Which type is this.
651 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
652 radlog(L_ERR, "Accounting: no Accounting-Status-Type record.");
653 return RLM_MODULE_NOOP;
658 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
660 if (status != PW_STATUS_START &&
661 status != PW_STATUS_STOP)
662 return RLM_MODULE_NOOP;
665 * We're only interested in accounting messages
666 * with a username in it.
668 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
669 return RLM_MODULE_NOOP;
671 t = request->timestamp;
672 memset(&ut, 0, sizeof(ut));
675 * First, find the interesting attributes.
677 for (vp = request->packet->vps; vp; vp = vp->next) {
678 switch (vp->attribute) {
680 if (vp->length >= sizeof(ut.ut_name)) {
681 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
683 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
686 case PW_LOGIN_IP_HOST:
687 case PW_FRAMED_IP_ADDRESS:
688 framed_address = vp->lvalue;
690 case PW_FRAMED_PROTOCOL:
691 protocol = vp->lvalue;
693 case PW_NAS_IP_ADDRESS:
694 nas_address = vp->lvalue;
697 nas_port = vp->lvalue;
700 case PW_ACCT_DELAY_TIME:
703 case PW_NAS_PORT_TYPE:
704 nas_port_type = vp->lvalue;
710 * We don't store !root sessions, or sessions
711 * where we didn't see a PW_NAS_PORT_ID.
713 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
714 return RLM_MODULE_NOOP;
717 * If we didn't find out the NAS address, use the
718 * originator's IP address.
720 if (nas_address == 0)
721 nas_address = request->packet->src_ipaddr;
725 * Linux has a field for the client address.
727 ut.ut_addr = framed_address;
730 * We use the tty field to store the terminal servers' port
731 * and address so that the tty field is unique.
734 if ((cl = nas_find(nas_address)) != NULL)
736 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
737 sprintf(buf, "%03d:%s", nas_port, s);
738 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
741 * We store the dynamic IP address in the hostname field.
744 if (framed_address) {
745 ip_ntoa(buf, framed_address);
746 strncpy(ut.ut_host, buf, UT_HOSTSIZE);
750 ut.ut_xtime = t- delay;
752 ut.ut_time = t - delay;
756 * And we can use the ID field to store
759 if (protocol == PW_PPP)
760 strcpy(ut.ut_id, "P");
761 else if (protocol == PW_SLIP)
762 strcpy(ut.ut_id, "S");
764 strcpy(ut.ut_id, "T");
765 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
767 if (status == PW_STATUS_STOP)
771 * Write a RADIUS wtmp log file.
773 * Try to open the file if we can't, we don't write the
774 * wtmp file. If we can try to write. If we fail,
775 * return RLM_MODULE_FAIL ..
777 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
778 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
780 return RLM_MODULE_FAIL;
784 return RLM_MODULE_FAIL;
786 return RLM_MODULE_OK;
789 /* globally exported name */
790 module_t rlm_unix = {
792 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
793 unix_init, /* initialization */
794 unix_instantiate, /* instantiation */
796 unix_authenticate, /* authentication */
797 NULL, /* authorization */
798 NULL, /* preaccounting */
799 unix_accounting, /* accounting */
800 NULL /* checksimul */
802 unix_detach, /* detach */
803 unix_destroy, /* destroy */