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, "no" },
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, "NULL" },
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 (group_inst->passwd_file)
222 pwd = fgetpwnam(group_inst->passwd_file, username);
224 pwd = getpwnam(username);
228 if (group_inst->group_file)
229 grp = fgetgrnam(group_inst->group_file, (char *)check->strvalue);
231 grp = getgrnam((char *)check->strvalue);
235 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
237 for (member = grp->gr_mem; *member && retval; member++) {
238 if (strcmp(*member, pwd->pw_name) == 0)
247 * FIXME: We really should have an 'init' which makes
248 * System auth == Unix
250 static int unix_init(void)
252 /* FIXME - delay these until a group file has been read so we know
253 * groupcmp can actually do something */
254 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
255 #ifdef PW_GROUP_NAME /* compat */
256 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
261 static int unix_instantiate(CONF_SECTION *conf, void **instance)
263 struct unix_instance *inst;
266 * Allocate room for the instance.
268 inst = *instance = rad_malloc(sizeof(struct unix_instance));
271 * Parse the configuration, failing if we can't do so.
273 if (cf_section_parse(conf, inst, module_config) < 0) {
278 if (inst->cache_passwd) {
279 radlog(L_INFO, "HASH: Reinitializing hash structures "
280 "and lists for caching...");
281 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
283 inst->group_file))==NULL)
285 radlog(L_ERR, "HASH: unable to create user "
286 "hash table. disable caching and run debugs");
287 if (inst->passwd_file)
288 free((char *) inst->passwd_file);
289 if (inst->shadow_file)
290 free((char *) inst->shadow_file);
291 if (inst->group_file)
292 free((char *) inst->group_file);
294 free((char *) inst->radwtmp);
299 if (inst->cache_reload) {
300 inst->last_reload = 0;
301 inst->next_reload = time(NULL) + inst->cache_reload;
307 if (inst->usegroup) {
308 if (group_inst_explicit) {
309 radlog(L_ERR, "Only one group list may be active");
312 group_inst_explicit = 1;
314 } else if (!group_inst) {
325 static int unix_detach(void *instance)
327 #define inst ((struct unix_instance *)instance)
328 if (group_inst == inst) {
330 group_inst_explicit = 0;
332 if (inst->passwd_file)
333 free((char *) inst->passwd_file);
334 if (inst->shadow_file)
335 free((char *) inst->shadow_file);
336 if (inst->group_file)
337 free((char *) inst->group_file);
339 free((char *) inst->radwtmp);
341 unix_freepwcache(inst->cache);
348 static int unix_destroy(void)
350 paircompare_unregister(PW_GROUP, groupcmp);
352 paircompare_unregister(PW_GROUP_NAME, groupcmp);
359 * Check the users password against the standard UNIX
362 static int unix_authenticate(void *instance, REQUEST *request)
364 #define inst ((struct unix_instance *)instance)
368 char *encrypted_pass;
371 shadow_pwd_t *spwd = NULL;
374 struct pr_passwd *pr_pw;
378 char *progname = "radius";
379 SIAENTITY *ent = NULL;
381 #ifdef HAVE_GETUSERSHELL
385 /* See if we should refresh the cache */
386 if (inst->cache && inst->cache_reload
387 && (inst->next_reload < request->timestamp)) {
388 /* Time to refresh, maybe ? */
392 DEBUG2("rlm_users : Time to refresh cache.");
393 /* Check if any of the files has changed */
394 if (inst->passwd_file
395 && (stat(inst->passwd_file, &statbuf) != -1)
396 && (statbuf.st_mtime > inst->last_reload)) {
400 if (inst->shadow_file
401 && (stat(inst->shadow_file, &statbuf) != -1)
402 && (statbuf.st_mtime > inst->last_reload)) {
407 && (stat(inst->group_file, &statbuf) != -1)
408 && (statbuf.st_mtime > inst->last_reload)) {
413 /* Build a new cache to replace old one */
414 struct pwcache *oldcache;
415 struct pwcache *newcache = unix_buildpwcache(
421 oldcache = inst->cache;
422 inst->cache = newcache;
423 unix_freepwcache(oldcache);
425 inst->last_reload = time(NULL);
428 DEBUG2("rlm_users : Files were unchanged. Not reloading.");
431 /* Schedule next refresh */
432 inst->next_reload = time(NULL) + inst->cache_reload;
436 * We can only authenticate user requests which HAVE
437 * a User-Name attribute.
439 if (!request->username) {
440 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
441 return RLM_MODULE_INVALID;
445 * We can only authenticate user requests which HAVE
446 * a User-Password attribute.
448 if (!request->password) {
449 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
450 return RLM_MODULE_INVALID;
454 * Ensure that we're being passed a plain-text password,
455 * and not anything else.
457 if (request->password->attribute != PW_PASSWORD) {
458 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
459 return RLM_MODULE_INVALID;
462 name = (char *)request->username->strvalue;
463 passwd = (char *)request->password->strvalue;
465 if (inst->cache_passwd &&
466 (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
467 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
472 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
474 return RLM_MODULE_NOTFOUND;
475 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
477 sia_ses_release (&ent);
478 return RLM_MODULE_NOTFOUND;
480 if (sia_ses_estab (NULL, ent) == SIASUCCESS) {
481 sia_ses_release (&ent);
482 return RLM_MODULE_OK;
485 return RLM_MODULE_NOTFOUND;
488 if ((pr_pw = getprpwnam(name)) == NULL)
489 return RLM_MODULE_NOTFOUND;
490 encrypted_pass = pr_pw->ufld.fd_encrypt;
493 * Get encrypted password from password file
495 * If a password file was explicitly specified, use it,
496 * otherwise, use the system routines to read the
497 * system password file
499 if (inst->passwd_file != NULL) {
500 if ((pwd = fgetpwnam(inst->passwd_file, name)) == NULL)
501 return RLM_MODULE_NOTFOUND;
502 } else if ((pwd = getpwnam(name)) == NULL) {
503 return RLM_MODULE_NOTFOUND;
505 encrypted_pass = pwd->pw_passwd;
510 * See if there is a shadow password.
512 * If a shadow file was explicitly specified, use it,
513 * otherwise, use the system routines to read the
514 * system shadow file.
516 * Also, if we explicitly specify the password file,
517 * only query the _system_ shadow file if the encrypted
518 * password from the passwd file is < 10 characters (i.e.
519 * a valid password would never crypt() to it). This will
520 * prevents users from using NULL password fields as things
523 if (inst->shadow_file != NULL) {
524 if ((spwd = fgetspnam(inst->shadow_file, name)) != NULL)
525 encrypted_pass = get_shadow_encrypted_pwd(spwd);
526 } else if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
527 if ((spwd = getspnam(name)) != NULL)
528 encrypted_pass = get_shadow_encrypted_pwd(spwd);
530 #endif /* HAVE_GETSPNAM */
534 * Undocumented temporary compatibility for iphil.NET
535 * Users with a certain shell are always denied access.
537 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
538 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
539 return RLM_MODULE_REJECT;
543 #if HAVE_GETUSERSHELL
545 * Check /etc/shells for a valid shell. If that file
546 * contains /RADIUSD/ANY/SHELL then any shell will do.
548 while ((shell = getusershell()) != NULL) {
549 if (strcmp(shell, pwd->pw_shell) == 0 ||
550 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
556 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
557 name, pwd->pw_shell);
558 return RLM_MODULE_REJECT;
562 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
564 * Check if password has expired.
566 if (spwd && spwd->sp_expire > 0 &&
567 (request->timestamp / 86400) > spwd->sp_expire) {
568 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
569 return RLM_MODULE_REJECT;
573 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
575 * Check if password has expired.
577 if ((pwd->pw_expire > 0) &&
578 (request->timestamp > pwd->pw_expire)) {
579 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
580 return RLM_MODULE_REJECT;
586 * Check if account is locked.
588 if (pr_pw->uflg.fg_lock!=1) {
589 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
590 return RLM_MODULE_USERLOCK;
595 * We might have a passwordless account.
597 if (encrypted_pass[0] == 0)
598 return RLM_MODULE_OK;
601 * Check encrypted password.
603 encpw = crypt(passwd, encrypted_pass);
604 if (strcmp(encpw, encrypted_pass)) {
605 radlog(L_AUTH, "rlm_unix: [%s]: invalid password", name);
606 return RLM_MODULE_REJECT;
608 return RLM_MODULE_OK;
614 * UUencode 4 bits base64. We use this to turn a 4 byte field
615 * (an IP address) into 6 bytes of ASCII. This is used for the
616 * wtmp file if we didn't find a short name in the naslist file.
618 static char *uue(void *in)
621 static unsigned char res[7];
622 unsigned char *data = (unsigned char *)in;
624 res[0] = ENC( data[0] >> 2 );
625 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
626 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
627 res[3] = ENC( data[2] & 077 );
629 res[4] = ENC( data[3] >> 2 );
630 res[5] = ENC( (data[3] << 4) & 060 );
633 for(i = 0; i < 6; i++) {
634 if (res[i] == ' ') res[i] = '`';
635 if (res[i] < 32 || res[i] > 127)
636 printf("uue: protocol error ?!\n");
643 * Unix accounting - write a wtmp file.
645 static int unix_accounting(void *instance, REQUEST *request)
657 int framed_address = 0;
661 int nas_port_type = 0;
662 struct unix_instance *inst = (struct unix_instance *) instance;
665 * No radwtmp. Don't do anything.
667 if (!inst->radwtmp) {
668 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
669 return RLM_MODULE_NOOP;
673 * Which type is this.
675 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
676 radlog(L_ERR, "Accounting: no Accounting-Status-Type record.");
677 return RLM_MODULE_NOOP;
682 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
684 if (status != PW_STATUS_START &&
685 status != PW_STATUS_STOP)
686 return RLM_MODULE_NOOP;
689 * We're only interested in accounting messages
690 * with a username in it.
692 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
693 return RLM_MODULE_NOOP;
695 t = request->timestamp;
696 memset(&ut, 0, sizeof(ut));
699 * First, find the interesting attributes.
701 for (vp = request->packet->vps; vp; vp = vp->next) {
702 switch (vp->attribute) {
704 if (vp->length >= sizeof(ut.ut_name)) {
705 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
707 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
710 case PW_LOGIN_IP_HOST:
711 case PW_FRAMED_IP_ADDRESS:
712 framed_address = vp->lvalue;
714 case PW_FRAMED_PROTOCOL:
715 protocol = vp->lvalue;
717 case PW_NAS_IP_ADDRESS:
718 nas_address = vp->lvalue;
721 nas_port = vp->lvalue;
724 case PW_ACCT_DELAY_TIME:
727 case PW_NAS_PORT_TYPE:
728 nas_port_type = vp->lvalue;
734 * We don't store !root sessions, or sessions
735 * where we didn't see a PW_NAS_PORT_ID.
737 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
738 return RLM_MODULE_NOOP;
741 * If we didn't find out the NAS address, use the
742 * originator's IP address.
744 if (nas_address == 0)
745 nas_address = request->packet->src_ipaddr;
749 * Linux has a field for the client address.
751 ut.ut_addr = framed_address;
754 * We use the tty field to store the terminal servers' port
755 * and address so that the tty field is unique.
758 if ((cl = client_find(nas_address)) != NULL)
760 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
761 sprintf(buf, "%03d:%s", nas_port, s);
762 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
765 * We store the dynamic IP address in the hostname field.
768 if (framed_address) {
769 ip_ntoa(buf, framed_address);
770 strncpy(ut.ut_host, buf, UT_HOSTSIZE);
774 ut.ut_xtime = t- delay;
776 ut.ut_time = t - delay;
780 * And we can use the ID field to store
783 if (protocol == PW_PPP)
784 strcpy(ut.ut_id, "P");
785 else if (protocol == PW_SLIP)
786 strcpy(ut.ut_id, "S");
788 strcpy(ut.ut_id, "T");
789 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
791 if (status == PW_STATUS_STOP)
795 * Write a RADIUS wtmp log file.
797 * Try to open the file if we can't, we don't write the
798 * wtmp file. If we can try to write. If we fail,
799 * return RLM_MODULE_FAIL ..
801 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
802 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
804 return RLM_MODULE_FAIL;
808 return RLM_MODULE_FAIL;
810 return RLM_MODULE_OK;
813 /* globally exported name */
814 module_t rlm_unix = {
816 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
817 unix_init, /* initialization */
818 unix_instantiate, /* instantiation */
820 unix_authenticate, /* authentication */
821 NULL, /* authorization */
822 NULL, /* preaccounting */
823 unix_accounting, /* accounting */
824 NULL, /* checksimul */
825 NULL, /* pre-proxy */
826 NULL, /* post-proxy */
829 unix_detach, /* detach */
830 unix_destroy, /* destroy */