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$";
12 #include "libradius.h"
14 /***********************************************************************
15 * Copyright (C) 2000 The FreeRADIUS server project.
17 * This program is is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License, version 2 if the
19 * License as published by the Free Software Foundation.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ***********************************************************************/
51 # 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;
75 static struct unix_instance config;
77 static CONF_PARSER module_config[] = {
79 * Cache the password by default.
81 { "cache", PW_TYPE_BOOLEAN, &config.cache_passwd, "yes" },
82 { "passwd", PW_TYPE_STRING_PTR, &config.passwd_file, NULL },
83 { "shadow", PW_TYPE_STRING_PTR, &config.shadow_file, NULL },
84 { "group", PW_TYPE_STRING_PTR, &config.group_file, NULL },
85 { "radwtmp", PW_TYPE_STRING_PTR, &config.radwtmp, "${logdir}/radwtmp" },
86 { "usegroup", PW_TYPE_BOOLEAN, &config.usegroup, "no" },
88 { NULL, -1, NULL, NULL } /* end the list */
92 * groupcmp is part of autz. But it uses the data from an auth instance. So
93 * here is where it gets it. By default this will be the first configured
94 * auth instance. That can be changed by putting "usegroup = yes" inside an
95 * auth instance to explicitly bind all Group checks to it.
98 /* binds "Group=" to an instance (a particular passwd file) */
99 static struct unix_instance *group_inst;
101 /* Tells if the above binding was explicit (usegroup=yes specified in config
102 * file) or not ("Group=" was bound to the first instance of rlm_unix */
103 static int group_inst_explicit;
106 * The Group = handler.
108 static int groupcmp(void *instance, VALUE_PAIR *request, VALUE_PAIR *check,
109 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
118 check_pairs = check_pairs;
119 reply_pairs = reply_pairs;
122 radlog(L_ERR, "groupcmp: no group list known.");
126 username = (char *)request->strvalue;
128 if (group_inst->cache_passwd &&
129 (retval = H_groupcmp(group_inst->cache, check, username)) != -2)
132 if ((pwd = getpwnam(username)) == NULL)
135 if ((grp = getgrnam((char *)check->strvalue)) == NULL)
138 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
140 for (member = grp->gr_mem; *member && retval; member++) {
141 if (strcmp(*member, pwd->pw_name) == 0)
150 * FIXME: We really should have an 'init' which makes
151 * System auth == Unix
153 static int unix_init(void)
155 /* FIXME - delay these until a group file has been read so we know
156 * groupcmp can actually do something */
157 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
158 #ifdef PW_GROUP_NAME /* compat */
159 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
164 static int unix_instantiate(CONF_SECTION *conf, void **instance)
166 struct unix_instance *inst;
169 * Parse the configuration, failing if we can't do so.
171 if (cf_section_parse(conf, module_config) < 0) {
176 * Allocate room for the instance.
178 inst = *instance = malloc(sizeof(struct unix_instance));
184 * Copy the configuration into the instance data
186 inst->cache_passwd = config.cache_passwd;
187 inst->passwd_file = config.passwd_file;
188 inst->shadow_file = config.shadow_file;
189 inst->group_file = config.group_file;
190 inst->radwtmp = config.radwtmp;
191 inst->usegroup = config.usegroup;
192 config.passwd_file = NULL;
193 config.shadow_file = NULL;
194 config.group_file = NULL;
195 config.radwtmp = NULL;
197 if (inst->cache_passwd) {
198 radlog(L_INFO, "HASH: Reinitializing hash structures "
199 "and lists for caching...");
200 if ((inst->cache = unix_buildpwcache(inst->passwd_file,
201 inst->shadow_file))==NULL)
203 radlog(L_ERR, "HASH: unable to create user "
204 "hash table. disable caching and run debugs");
210 if (inst->usegroup) {
211 if (group_inst_explicit) {
212 radlog(L_ERR, "Only one group list may be active");
215 group_inst_explicit = 1;
217 } else if (!group_inst) {
228 static int unix_detach(void *instance)
230 #define inst ((struct unix_instance *)instance)
231 if (group_inst == inst) {
233 group_inst_explicit = 0;
235 free((char *) inst->passwd_file);
236 free((char *) inst->shadow_file);
237 free((char *) inst->group_file);
238 free((char *) inst->radwtmp);
240 unix_freepwcache(inst->cache);
247 static int unix_destroy(void)
249 paircompare_unregister(PW_GROUP, groupcmp);
251 paircompare_unregister(PW_GROUP_NAME, groupcmp);
258 * Check the users password against the standard UNIX
261 static int unix_authenticate(void *instance, REQUEST *request)
263 #define inst ((struct unix_instance *)instance)
267 char *encrypted_pass;
277 struct pr_passwd *pr_pw;
279 #ifdef HAVE_GETUSERSHELL
285 * We can only authenticate user requests which HAVE
286 * a User-Name attribute.
288 if (!request->username) {
289 radlog(L_AUTH, "rlm_unix: Attribute \"User-Name\" is required for authentication.");
290 return RLM_MODULE_INVALID;
294 * We can only authenticate user requests which HAVE
295 * a Password attribute.
297 if (!request->password) {
298 radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication.");
299 return RLM_MODULE_INVALID;
303 * Ensure that we're being passed a plain-text password,
304 * and not anything else.
306 if (request->password->attribute != PW_PASSWORD) {
307 radlog(L_AUTH, "rlm_unix: Attribute \"Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
308 return RLM_MODULE_INVALID;
311 name = (char *)request->username->strvalue;
312 passwd = (char *)request->password->strvalue;
314 if (inst->cache_passwd &&
315 (ret = H_unix_pass(inst->cache, name, passwd, &request->reply->vps)) != -2)
316 return (ret == 0) ? RLM_MODULE_OK : RLM_MODULE_REJECT;
319 if ((pr_pw = getprpwnam(name)) == NULL)
320 return RLM_MODULE_NOTFOUND;
321 encrypted_pass = pr_pw->ufld.fd_encrypt;
324 * Get encrypted password from password file
326 if ((pwd = getpwnam(name)) == NULL) {
327 return RLM_MODULE_NOTFOUND;
329 encrypted_pass = pwd->pw_passwd;
334 * See if there is a shadow password.
336 if ((spwd = getspnam(name)) != NULL)
338 encrypted_pass = spwd->pw_passwd;
340 encrypted_pass = spwd->sp_pwdp;
342 #endif /* HAVE_GETSPNAM */
346 * Undocumented temporary compatibility for iphil.NET
347 * Users with a certain shell are always denied access.
349 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
350 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
351 return RLM_MODULE_REJECT;
355 #if HAVE_GETUSERSHELL
357 * Check /etc/shells for a valid shell. If that file
358 * contains /RADIUSD/ANY/SHELL then any shell will do.
360 while ((shell = getusershell()) != NULL) {
361 if (strcmp(shell, pwd->pw_shell) == 0 ||
362 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
368 return RLM_MODULE_REJECT;
371 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
373 * Check if password has expired.
375 if (spwd && spwd->sp_expire > 0 &&
376 (request->timestamp / 86400) > spwd->sp_expire) {
377 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
378 return RLM_MODULE_REJECT;
382 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
384 * Check if password has expired.
386 if ((pwd->pw_expire > 0) &&
387 (request->timestamp > pwd->pw_expire)) {
388 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
389 return RLM_MODULE_REJECT;
395 * Check if account is locked.
397 if (pr_pw->uflg.fg_lock!=1) {
398 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
399 return RLM_MODULE_USERLOCK;
404 * We might have a passwordless account.
406 if (encrypted_pass[0] == 0)
407 return RLM_MODULE_OK;
410 * Check encrypted password.
412 encpw = crypt(passwd, encrypted_pass);
413 if (strcmp(encpw, encrypted_pass))
414 return RLM_MODULE_REJECT;
416 return RLM_MODULE_OK;
421 * UUencode 4 bits base64. We use this to turn a 4 byte field
422 * (an IP address) into 6 bytes of ASCII. This is used for the
423 * wtmp file if we didn't find a short name in the naslist file.
425 static char *uue(void *in)
428 static unsigned char res[7];
429 unsigned char *data = (unsigned char *)in;
431 res[0] = ENC( data[0] >> 2 );
432 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
433 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
434 res[3] = ENC( data[2] & 077 );
436 res[4] = ENC( data[3] >> 2 );
437 res[5] = ENC( (data[3] << 4) & 060 );
440 for(i = 0; i < 6; i++) {
441 if (res[i] == ' ') res[i] = '`';
442 if (res[i] < 32 || res[i] > 127)
443 printf("uue: protocol error ?!\n");
450 * Unix accounting - write a wtmp file.
452 static int unix_accounting(void *instance, REQUEST *request)
464 int framed_address = 0;
468 int nas_port_type = 0;
469 struct unix_instance *inst = (struct unix_instance *) instance;
472 * Which type is this.
474 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
475 radlog(L_ERR, "Accounting: no Accounting-Status-Type record.");
476 return RLM_MODULE_NOOP;
481 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
483 if (status != PW_STATUS_START &&
484 status != PW_STATUS_STOP)
485 return RLM_MODULE_NOOP;
488 * We're only interested in accounting messages
489 * with a username in it.
491 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
492 return RLM_MODULE_NOOP;
494 t = request->timestamp;
495 memset(&ut, 0, sizeof(ut));
498 * First, find the interesting attributes.
500 for (vp = request->packet->vps; vp; vp = vp->next) {
501 switch (vp->attribute) {
503 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
505 case PW_LOGIN_IP_HOST:
506 case PW_FRAMED_IP_ADDRESS:
507 framed_address = vp->lvalue;
509 case PW_FRAMED_PROTOCOL:
510 protocol = vp->lvalue;
512 case PW_NAS_IP_ADDRESS:
513 nas_address = vp->lvalue;
516 nas_port = vp->lvalue;
519 case PW_ACCT_DELAY_TIME:
522 case PW_NAS_PORT_TYPE:
523 nas_port_type = vp->lvalue;
529 * We don't store !root sessions, or sessions
530 * where we didn't see a PW_NAS_PORT_ID.
532 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
533 return RLM_MODULE_NOOP;
536 * If we didn't find out the NAS address, use the
537 * originator's IP address.
539 if (nas_address == 0)
540 nas_address = request->packet->src_ipaddr;
544 * Linux has a field for the client address.
546 ut.ut_addr = framed_address;
549 * We use the tty field to store the terminal servers' port
550 * and address so that the tty field is unique.
553 if ((cl = nas_find(nas_address)) != NULL)
555 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
556 sprintf(buf, "%03d:%s", nas_port, s);
557 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
560 * We store the dynamic IP address in the hostname field.
563 if (framed_address) {
564 ip_ntoa(buf, framed_address);
565 strncpy(ut.ut_host, buf, UT_HOSTSIZE);
569 ut.ut_xtime = t- delay;
571 ut.ut_time = t - delay;
575 * And we can use the ID field to store
578 if (protocol == PW_PPP)
579 strcpy(ut.ut_id, "P");
580 else if (protocol == PW_SLIP)
581 strcpy(ut.ut_id, "S");
583 strcpy(ut.ut_id, "T");
584 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
586 if (status == PW_STATUS_STOP)
590 * Write a RADIUS wtmp log file.
591 * FIXME: return correct error.
592 * Check if file is there. If not, we don't write the
593 * wtmp file. If it is, we try to write. If we fail,
594 * return RLM_MODULE_FAIL ..
596 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
597 fwrite(&ut, sizeof(ut), 1, fp);
601 return RLM_MODULE_OK;
604 /* globally exported name */
605 module_t rlm_unix = {
607 0, /* type: reserved */
608 unix_init, /* initialization */
609 unix_instantiate, /* instantiation */
610 NULL, /* authorization */
611 unix_authenticate, /* authentication */
612 NULL, /* preaccounting */
613 unix_accounting, /* accounting */
614 NULL, /* checksimul */
615 unix_detach, /* detach */
616 unix_destroy, /* destroy */