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>
45 # include <sys/security.h>
61 static char trans[64] =
62 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
63 #define ENC(c) trans[c]
65 struct unix_instance {
67 const char *passwd_file;
68 const char *shadow_file;
69 const char *group_file;
72 struct pwcache *cache;
78 static CONF_PARSER module_config[] = {
80 * Cache the password by default.
82 { "cache", PW_TYPE_BOOLEAN,
83 offsetof(struct unix_instance,cache_passwd), NULL, "no" },
84 { "passwd", PW_TYPE_STRING_PTR,
85 offsetof(struct unix_instance,passwd_file), NULL, NULL },
86 { "shadow", PW_TYPE_STRING_PTR,
87 offsetof(struct unix_instance,shadow_file), NULL, NULL },
88 { "group", PW_TYPE_STRING_PTR,
89 offsetof(struct unix_instance,group_file), NULL, NULL },
90 { "radwtmp", PW_TYPE_STRING_PTR,
91 offsetof(struct unix_instance,radwtmp), NULL, "NULL" },
92 { "usegroup", PW_TYPE_BOOLEAN,
93 offsetof(struct unix_instance,usegroup), NULL, "no" },
94 { "cache_reload", PW_TYPE_INTEGER,
95 offsetof(struct unix_instance,cache_reload), NULL, "600" },
97 { NULL, -1, 0, NULL, NULL } /* end the list */
101 * groupcmp is part of autz. But it uses the data from an auth instance. So
102 * here is where it gets it. By default this will be the first configured
103 * auth instance. That can be changed by putting "usegroup = yes" inside an
104 * auth instance to explicitly bind all Group checks to it.
107 /* binds "Group=" to an instance (a particular passwd file) */
108 static struct unix_instance *group_inst;
110 /* Tells if the above binding was explicit (usegroup=yes specified in config
111 * file) or not ("Group=" was bound to the first instance of rlm_unix */
112 static int group_inst_explicit;
116 static inline const char *get_shadow_name(shadow_pwd_t *spwd) {
117 if (spwd == NULL) return NULL;
118 return (spwd->pw_name);
121 static inline const char *get_shadow_encrypted_pwd(shadow_pwd_t *spwd) {
122 if (spwd == NULL) return NULL;
123 return (spwd->pw_passwd);
126 static inline const char *get_shadow_name(shadow_pwd_t *spwd) {
127 if (spwd == NULL) return NULL;
128 return (spwd->sp_namp);
130 static inline const char *get_shadow_encrypted_pwd(shadow_pwd_t *spwd) {
131 if (spwd == NULL) return NULL;
132 return (spwd->sp_pwdp);
135 #endif /* HAVE_GETSPNAM */
137 static struct passwd *fgetpwnam(const char *fname, const char *name) {
138 FILE *file = fopen(fname, "ro");
139 struct passwd *pwd = NULL;
141 if(file == NULL) return NULL;
143 pwd = fgetpwent(file);
148 } while (strcmp(name, pwd->pw_name) != 0);
154 static struct group *fgetgrnam(const char *fname, const char *name) {
155 FILE *file = fopen(fname, "ro");
156 struct group *grp = NULL;
158 if(file == NULL) return NULL;
161 grp = fgetgrent(file);
166 } while(strcmp(name, grp->gr_name) != 0);
173 static shadow_pwd_t *fgetspnam(const char *fname, const char *name) {
174 FILE *file = fopen(fname, "ro");
175 shadow_pwd_t *spwd = NULL;
177 if(file == NULL) return NULL;
179 spwd = fgetspent(file);
184 } while(strcmp(name, get_shadow_name(spwd)) != 0);
192 * The Group = handler.
194 static int groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
195 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
204 check_pairs = check_pairs;
205 reply_pairs = reply_pairs;
208 radlog(L_ERR, "groupcmp: no group list known.");
212 username = (char *)eq->username->strvalue;
214 if (group_inst->cache_passwd &&
215 (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
218 if (group_inst->passwd_file)
219 pwd = fgetpwnam(group_inst->passwd_file, username);
221 pwd = getpwnam(username);
225 if (group_inst->group_file)
226 grp = fgetgrnam(group_inst->group_file, (char *)check->strvalue);
228 grp = getgrnam((char *)check->strvalue);
232 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
234 for (member = grp->gr_mem; *member && retval; member++) {
235 if (strcmp(*member, pwd->pw_name) == 0)
244 * FIXME: We really should have an 'init' which makes
245 * System auth == Unix
247 static int unix_init(void)
249 /* FIXME - delay these until a group file has been read so we know
250 * groupcmp can actually do something */
251 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
252 #ifdef PW_GROUP_NAME /* compat */
253 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
258 static int unix_instantiate(CONF_SECTION *conf, void **instance)
260 struct unix_instance *inst;
263 * Allocate room for the instance.
265 inst = *instance = rad_malloc(sizeof(struct unix_instance));
268 * Parse the configuration, failing if we can't do so.
270 if (cf_section_parse(conf, inst, module_config) < 0) {
275 if (inst->cache_passwd) {
276 radlog(L_INFO, "HASH: Reinitializing hash structures "
277 "and lists for caching...");
278 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
280 inst->group_file))==NULL)
282 radlog(L_ERR, "HASH: unable to create user "
283 "hash table. disable caching and run debugs");
284 if (inst->passwd_file)
285 free((char *) inst->passwd_file);
286 if (inst->shadow_file)
287 free((char *) inst->shadow_file);
288 if (inst->group_file)
289 free((char *) inst->group_file);
291 free((char *) inst->radwtmp);
296 if (inst->cache_reload) {
297 inst->last_reload = 0;
298 inst->next_reload = time(NULL) + inst->cache_reload;
304 if (inst->usegroup) {
305 if (group_inst_explicit) {
306 radlog(L_ERR, "Only one group list may be active");
309 group_inst_explicit = 1;
311 } else if (!group_inst) {
322 static int unix_detach(void *instance)
324 #define inst ((struct unix_instance *)instance)
325 if (group_inst == inst) {
327 group_inst_explicit = 0;
329 if (inst->passwd_file)
330 free((char *) inst->passwd_file);
331 if (inst->shadow_file)
332 free((char *) inst->shadow_file);
333 if (inst->group_file)
334 free((char *) inst->group_file);
336 free((char *) inst->radwtmp);
338 unix_freepwcache(inst->cache);
345 static int unix_destroy(void)
347 paircompare_unregister(PW_GROUP, groupcmp);
349 paircompare_unregister(PW_GROUP_NAME, groupcmp);
356 * Check the users password against the standard UNIX
359 static int unix_authenticate(void *instance, REQUEST *request)
361 #define inst ((struct unix_instance *)instance)
364 const char *encrypted_pass;
367 shadow_pwd_t *spwd = NULL;
370 struct pr_passwd *pr_pw;
374 char *progname = "radius";
375 SIAENTITY *ent = NULL;
377 #ifdef HAVE_GETUSERSHELL
381 /* See if we should refresh the cache */
382 if (inst->cache && inst->cache_reload
383 && (inst->next_reload < request->timestamp)) {
384 /* Time to refresh, maybe ? */
388 DEBUG2("rlm_users : Time to refresh cache.");
389 /* Check if any of the files has changed */
390 if (inst->passwd_file
391 && (stat(inst->passwd_file, &statbuf) != -1)
392 && (statbuf.st_mtime > inst->last_reload)) {
396 if (inst->shadow_file
397 && (stat(inst->shadow_file, &statbuf) != -1)
398 && (statbuf.st_mtime > inst->last_reload)) {
403 && (stat(inst->group_file, &statbuf) != -1)
404 && (statbuf.st_mtime > inst->last_reload)) {
409 /* Build a new cache to replace old one */
410 struct pwcache *oldcache;
411 struct pwcache *newcache = unix_buildpwcache(
417 oldcache = inst->cache;
418 inst->cache = newcache;
419 unix_freepwcache(oldcache);
421 inst->last_reload = time(NULL);
424 DEBUG2("rlm_users : Files were unchanged. Not reloading.");
427 /* Schedule next refresh */
428 inst->next_reload = time(NULL) + inst->cache_reload;
432 * We can only authenticate user requests which HAVE
433 * a User-Name attribute.
435 if (!request->username) {
436 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
437 return RLM_MODULE_INVALID;
441 * We can only authenticate user requests which HAVE
442 * a User-Password attribute.
444 if (!request->password) {
445 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
446 return RLM_MODULE_INVALID;
450 * Ensure that we're being passed a plain-text password,
451 * and not anything else.
453 if (request->password->attribute != PW_PASSWORD) {
454 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
455 return RLM_MODULE_INVALID;
458 name = (char *)request->username->strvalue;
459 passwd = (char *)request->password->strvalue;
461 if (inst->cache_passwd &&
462 (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
463 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
468 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
470 return RLM_MODULE_NOTFOUND;
471 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
473 sia_ses_release (&ent);
474 return RLM_MODULE_NOTFOUND;
476 if (sia_ses_estab (NULL, ent) == SIASUCCESS) {
477 sia_ses_release (&ent);
478 return RLM_MODULE_OK;
481 return RLM_MODULE_NOTFOUND;
484 if ((pr_pw = getprpwnam(name)) == NULL)
485 return RLM_MODULE_NOTFOUND;
486 encrypted_pass = pr_pw->ufld.fd_encrypt;
489 * Get encrypted password from password file
491 * If a password file was explicitly specified, use it,
492 * otherwise, use the system routines to read the
493 * system password file
495 if (inst->passwd_file != NULL) {
496 if ((pwd = fgetpwnam(inst->passwd_file, name)) == NULL)
497 return RLM_MODULE_NOTFOUND;
498 } else if ((pwd = getpwnam(name)) == NULL) {
499 return RLM_MODULE_NOTFOUND;
501 encrypted_pass = pwd->pw_passwd;
506 * See if there is a shadow password.
508 * If a shadow file was explicitly specified, use it,
509 * otherwise, use the system routines to read the
510 * system shadow file.
512 * Also, if we explicitly specify the password file,
513 * only query the _system_ shadow file if the encrypted
514 * password from the passwd file is < 10 characters (i.e.
515 * a valid password would never crypt() to it). This will
516 * prevents users from using NULL password fields as things
519 if (inst->shadow_file != NULL) {
520 if ((spwd = fgetspnam(inst->shadow_file, name)) != NULL)
521 encrypted_pass = get_shadow_encrypted_pwd(spwd);
522 } else if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
523 if ((spwd = getspnam(name)) != NULL)
524 encrypted_pass = get_shadow_encrypted_pwd(spwd);
526 #endif /* HAVE_GETSPNAM */
530 * Undocumented temporary compatibility for iphil.NET
531 * Users with a certain shell are always denied access.
533 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
534 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
535 return RLM_MODULE_REJECT;
539 #if HAVE_GETUSERSHELL
541 * Check /etc/shells for a valid shell. If that file
542 * contains /RADIUSD/ANY/SHELL then any shell will do.
544 while ((shell = getusershell()) != NULL) {
545 if (strcmp(shell, pwd->pw_shell) == 0 ||
546 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
552 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
553 name, pwd->pw_shell);
554 return RLM_MODULE_REJECT;
558 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
560 * Check if password has expired.
562 if (spwd && spwd->sp_expire > 0 &&
563 (request->timestamp / 86400) > spwd->sp_expire) {
564 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
565 return RLM_MODULE_REJECT;
569 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
571 * Check if password has expired.
573 if ((pwd->pw_expire > 0) &&
574 (request->timestamp > pwd->pw_expire)) {
575 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
576 return RLM_MODULE_REJECT;
582 * Check if account is locked.
584 if (pr_pw->uflg.fg_lock!=1) {
585 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
586 return RLM_MODULE_USERLOCK;
591 * We might have a passwordless account.
593 if (encrypted_pass[0] == 0)
594 return RLM_MODULE_OK;
597 * Check encrypted password.
599 if (lrad_crypt_check(passwd, encrypted_pass)) {
600 radlog(L_AUTH, "rlm_unix: [%s]: invalid password", name);
601 return RLM_MODULE_REJECT;
603 return RLM_MODULE_OK;
609 * UUencode 4 bits base64. We use this to turn a 4 byte field
610 * (an IP address) into 6 bytes of ASCII. This is used for the
611 * wtmp file if we didn't find a short name in the naslist file.
613 static char *uue(void *in)
616 static unsigned char res[7];
617 unsigned char *data = (unsigned char *)in;
619 res[0] = ENC( data[0] >> 2 );
620 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
621 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
622 res[3] = ENC( data[2] & 077 );
624 res[4] = ENC( data[3] >> 2 );
625 res[5] = ENC( (data[3] << 4) & 060 );
628 for(i = 0; i < 6; i++) {
629 if (res[i] == ' ') res[i] = '`';
630 if (res[i] < 32 || res[i] > 127)
631 printf("uue: protocol error ?!\n");
638 * Unix accounting - write a wtmp file.
640 static int unix_accounting(void *instance, REQUEST *request)
652 int framed_address = 0;
656 int nas_port_type = 0;
657 struct unix_instance *inst = (struct unix_instance *) instance;
660 * No radwtmp. Don't do anything.
662 if (!inst->radwtmp) {
663 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
664 return RLM_MODULE_NOOP;
668 * Which type is this.
670 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
671 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
672 return RLM_MODULE_NOOP;
677 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
679 if (status != PW_STATUS_START &&
680 status != PW_STATUS_STOP)
681 return RLM_MODULE_NOOP;
684 * We're only interested in accounting messages
685 * with a username in it.
687 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
688 return RLM_MODULE_NOOP;
690 t = request->timestamp;
691 memset(&ut, 0, sizeof(ut));
694 * First, find the interesting attributes.
696 for (vp = request->packet->vps; vp; vp = vp->next) {
697 switch (vp->attribute) {
699 if (vp->length >= sizeof(ut.ut_name)) {
700 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
702 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
705 case PW_LOGIN_IP_HOST:
706 case PW_FRAMED_IP_ADDRESS:
707 framed_address = vp->lvalue;
709 case PW_FRAMED_PROTOCOL:
710 protocol = vp->lvalue;
712 case PW_NAS_IP_ADDRESS:
713 nas_address = vp->lvalue;
716 nas_port = vp->lvalue;
719 case PW_ACCT_DELAY_TIME:
722 case PW_NAS_PORT_TYPE:
723 nas_port_type = vp->lvalue;
729 * We don't store !root sessions, or sessions
730 * where we didn't see a NAS-Port attribute.
732 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
733 return RLM_MODULE_NOOP;
736 * If we didn't find out the NAS address, use the
737 * originator's IP address.
739 if (nas_address == 0)
740 nas_address = request->packet->src_ipaddr;
744 * Linux has a field for the client address.
746 ut.ut_addr = framed_address;
749 * We use the tty field to store the terminal servers' port
750 * and address so that the tty field is unique.
753 if ((cl = client_find(nas_address)) != NULL)
755 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
756 sprintf(buf, "%03d:%s", nas_port, s);
757 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
760 * We store the dynamic IP address in the hostname field.
763 if (framed_address) {
764 ip_ntoa(buf, framed_address);
765 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
769 ut.ut_xtime = t- delay;
771 ut.ut_time = t - delay;
775 * And we can use the ID field to store
778 if (protocol == PW_PPP)
779 strcpy(ut.ut_id, "P");
780 else if (protocol == PW_SLIP)
781 strcpy(ut.ut_id, "S");
783 strcpy(ut.ut_id, "T");
784 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
786 if (status == PW_STATUS_STOP)
790 * Write a RADIUS wtmp log file.
792 * Try to open the file if we can't, we don't write the
793 * wtmp file. If we can try to write. If we fail,
794 * return RLM_MODULE_FAIL ..
796 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
797 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
799 return RLM_MODULE_FAIL;
803 return RLM_MODULE_FAIL;
805 return RLM_MODULE_OK;
808 /* globally exported name */
809 module_t rlm_unix = {
811 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
812 unix_init, /* initialization */
813 unix_instantiate, /* instantiation */
815 unix_authenticate, /* authentication */
816 NULL, /* authorization */
817 NULL, /* preaccounting */
818 unix_accounting, /* accounting */
819 NULL, /* checksimul */
820 NULL, /* pre-proxy */
821 NULL, /* post-proxy */
824 unix_detach, /* detach */
825 unix_destroy, /* destroy */