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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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$";
28 #include <freeradius-devel/autoconf.h>
34 #include <sys/types.h>
44 # include <sys/security.h>
53 #include <freeradius-devel/radiusd.h>
54 #include <freeradius-devel/modules.h>
55 #include <freeradius-devel/sysutmp.h>
57 static char trans[64] =
58 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
59 #define ENC(c) trans[c]
61 struct unix_instance {
65 static const CONF_PARSER module_config[] = {
66 { "radwtmp", PW_TYPE_STRING_PTR,
67 offsetof(struct unix_instance,radwtmp), NULL, "NULL" },
69 { NULL, -1, 0, NULL, NULL } /* end the list */
74 * The Group = handler.
76 static int groupcmp(void *instance, UNUSED REQUEST *req, VALUE_PAIR *request,
77 VALUE_PAIR *check, VALUE_PAIR *check_pairs,
78 VALUE_PAIR **reply_pairs)
88 check_pairs = check_pairs;
89 reply_pairs = reply_pairs;
92 * No user name, doesn't compare.
94 vp = pairfind(request, PW_STRIPPED_USER_NAME);
96 vp = pairfind(request, PW_USER_NAME);
101 username = (char *)vp->vp_strvalue;
103 pwd = getpwnam(username);
107 grp = getgrnam((char *)check->vp_strvalue);
111 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
113 for (member = grp->gr_mem; *member && retval; member++) {
114 if (strcmp(*member, pwd->pw_name) == 0)
125 static int unix_detach(void *instance)
127 #define inst ((struct unix_instance *)instance)
131 paircompare_unregister(PW_GROUP, groupcmp);
133 paircompare_unregister(PW_GROUP_NAME, groupcmp);
143 static int unix_instantiate(CONF_SECTION *conf, void **instance)
145 struct unix_instance *inst;
148 * Allocate room for the instance.
150 inst = *instance = rad_malloc(sizeof(*inst));
154 memset(inst, 0, sizeof(*inst));
157 * Parse the configuration, failing if we can't do so.
159 if (cf_section_parse(conf, inst, module_config) < 0) {
164 /* FIXME - delay these until a group file has been read so we know
165 * groupcmp can actually do something */
166 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
167 #ifdef PW_GROUP_NAME /* compat */
168 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
178 * Pull the users password from where-ever, and add it to
181 static int unix_getpw(UNUSED void *instance, REQUEST *request,
182 VALUE_PAIR **vp_list)
185 const char *encrypted_pass;
187 struct spwd *spwd = NULL;
190 struct pr_passwd *pr_pw;
194 #ifdef HAVE_GETUSERSHELL
200 * We can only authenticate user requests which HAVE
201 * a User-Name attribute.
203 if (!request->username) {
204 return RLM_MODULE_NOOP;
207 name = (char *)request->username->vp_strvalue;
208 encrypted_pass = NULL;
211 if ((pr_pw = getprpwnam(name)) == NULL)
212 return RLM_MODULE_NOTFOUND;
213 encrypted_pass = pr_pw->ufld.fd_encrypt;
216 * Check if account is locked.
218 if (pr_pw->uflg.fg_lock!=1) {
219 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
220 return RLM_MODULE_USERLOCK;
223 if ((pwd = getpwnam(name)) == NULL) {
224 return RLM_MODULE_NOTFOUND;
226 encrypted_pass = pwd->pw_passwd;
231 * See if there is a shadow password.
233 * Only query the _system_ shadow file if the encrypted
234 * password from the passwd file is < 10 characters (i.e.
235 * a valid password would never crypt() to it). This will
236 * prevents users from using NULL password fields as things
239 if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
240 if ((spwd = getspnam(name)) == NULL) {
241 return RLM_MODULE_NOTFOUND;
243 encrypted_pass = spwd->sp_pwdp;
245 #endif /* HAVE_GETSPNAM */
248 * These require 'pwd != NULL', which isn't true on OSFC2
253 * Users with a particular shell are denied access
255 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
256 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
257 return RLM_MODULE_REJECT;
261 #ifdef HAVE_GETUSERSHELL
263 * Check /etc/shells for a valid shell. If that file
264 * contains /RADIUSD/ANY/SHELL then any shell will do.
266 while ((shell = getusershell()) != NULL) {
267 if (strcmp(shell, pwd->pw_shell) == 0 ||
268 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
274 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
275 name, pwd->pw_shell);
276 return RLM_MODULE_REJECT;
281 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
283 * Check if password has expired.
285 if (spwd && spwd->sp_expire > 0 &&
286 (request->timestamp / 86400) > spwd->sp_expire) {
287 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
288 return RLM_MODULE_REJECT;
292 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
294 * Check if password has expired.
296 if ((pwd->pw_expire > 0) &&
297 (request->timestamp > pwd->pw_expire)) {
298 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
299 return RLM_MODULE_REJECT;
304 * We might have a passwordless account.
306 * FIXME: Maybe add Auth-Type := Accept?
308 if (encrypted_pass[0] == 0)
309 return RLM_MODULE_NOOP;
311 vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
312 if (!vp) return RLM_MODULE_FAIL;
314 pairmove(vp_list, &vp);
315 pairfree(&vp); /* might not be NULL; */
317 return RLM_MODULE_UPDATED;
322 * Pull the users password from where-ever, and add it to
325 static int unix_authorize(void *instance, REQUEST *request)
327 return unix_getpw(instance, request, &request->config_items);
331 * Pull the users password from where-ever, and add it to
334 static int unix_authenticate(void *instance, REQUEST *request)
338 char *progname = "radius";
339 SIAENTITY *ent = NULL;
343 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
345 return RLM_MODULE_NOTFOUND;
346 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
348 sia_ses_release (&ent);
349 return RLM_MODULE_NOTFOUND;
351 if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
352 sia_ses_release (&ent);
353 return RLM_MODULE_NOTFOUND;
357 VALUE_PAIR *vp = NULL;
359 if (!request->password ||
360 (request->password->attribute != PW_USER_PASSWORD)) {
361 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
362 return RLM_MODULE_INVALID;
365 rcode = unix_getpw(instance, request, &vp);
366 if (rcode != RLM_MODULE_UPDATED) return rcode;
371 if (lrad_crypt_check((char *) request->password->vp_strvalue,
372 (char *) vp->vp_strvalue) != 0) {
373 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
374 request->username->vp_strvalue);
375 return RLM_MODULE_REJECT;
379 return RLM_MODULE_OK;
384 * UUencode 4 bits base64. We use this to turn a 4 byte field
385 * (an IP address) into 6 bytes of ASCII. This is used for the
386 * wtmp file if we didn't find a short name in the naslist file.
388 static char *uue(void *in)
391 static unsigned char res[7];
392 unsigned char *data = (unsigned char *)in;
394 res[0] = ENC( data[0] >> 2 );
395 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
396 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
397 res[3] = ENC( data[2] & 077 );
399 res[4] = ENC( data[3] >> 2 );
400 res[5] = ENC( (data[3] << 4) & 060 );
403 for(i = 0; i < 6; i++) {
404 if (res[i] == ' ') res[i] = '`';
405 if (res[i] < 32 || res[i] > 127)
406 printf("uue: protocol error ?!\n");
413 * Unix accounting - write a wtmp file.
415 static int unix_accounting(void *instance, REQUEST *request)
426 int framed_address = 0;
430 int nas_port_type = 0;
431 struct unix_instance *inst = (struct unix_instance *) instance;
434 * No radwtmp. Don't do anything.
436 if (!inst->radwtmp) {
437 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
438 return RLM_MODULE_NOOP;
441 if (request->packet->src_ipaddr.af != AF_INET) {
442 DEBUG2("rlm_unix: IPv6 is not supported!");
443 return RLM_MODULE_NOOP;
447 * Which type is this.
449 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
450 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
451 return RLM_MODULE_NOOP;
456 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
458 if (status != PW_STATUS_START &&
459 status != PW_STATUS_STOP)
460 return RLM_MODULE_NOOP;
463 * We're only interested in accounting messages
464 * with a username in it.
466 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
467 return RLM_MODULE_NOOP;
469 t = request->timestamp;
470 memset(&ut, 0, sizeof(ut));
473 * First, find the interesting attributes.
475 for (vp = request->packet->vps; vp; vp = vp->next) {
476 switch (vp->attribute) {
478 if (vp->length >= sizeof(ut.ut_name)) {
479 memcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
481 strNcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
484 case PW_LOGIN_IP_HOST:
485 case PW_FRAMED_IP_ADDRESS:
486 framed_address = vp->lvalue;
488 case PW_FRAMED_PROTOCOL:
489 protocol = vp->lvalue;
491 case PW_NAS_IP_ADDRESS:
492 nas_address = vp->lvalue;
495 nas_port = vp->lvalue;
498 case PW_ACCT_DELAY_TIME:
501 case PW_NAS_PORT_TYPE:
502 nas_port_type = vp->lvalue;
508 * We don't store !root sessions, or sessions
509 * where we didn't see a NAS-Port attribute.
511 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
512 return RLM_MODULE_NOOP;
517 * If we didn't find out the NAS address, use the
518 * originator's IP address.
520 if (nas_address == 0) {
522 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
524 if ((cl = client_find_old(&request->packet->src_ipaddr)) != NULL)
527 if (!s || s[0] == 0) s = uue(&(nas_address));
531 * Linux has a field for the client address.
533 ut.ut_addr = framed_address;
536 * We use the tty field to store the terminal servers' port
537 * and address so that the tty field is unique.
539 sprintf(buf, "%03d:%s", nas_port, s);
540 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
543 * We store the dynamic IP address in the hostname field.
546 if (framed_address) {
547 ip_ntoa(buf, framed_address);
548 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
552 ut.ut_xtime = t- delay;
554 ut.ut_time = t - delay;
558 * And we can use the ID field to store
561 if (protocol == PW_PPP)
562 strcpy(ut.ut_id, "P");
563 else if (protocol == PW_SLIP)
564 strcpy(ut.ut_id, "S");
566 strcpy(ut.ut_id, "T");
567 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
569 if (status == PW_STATUS_STOP)
573 * Write a RADIUS wtmp log file.
575 * Try to open the file if we can't, we don't write the
576 * wtmp file. If we can try to write. If we fail,
577 * return RLM_MODULE_FAIL ..
579 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
580 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
582 return RLM_MODULE_FAIL;
586 return RLM_MODULE_FAIL;
588 return RLM_MODULE_OK;
591 /* globally exported name */
592 module_t rlm_unix = {
595 RLM_TYPE_THREAD_UNSAFE, /* type */
596 unix_instantiate, /* instantiation */
597 unix_detach, /* detach */
599 unix_authenticate, /* authentication */
600 unix_authorize, /* authorization */
601 NULL, /* preaccounting */
602 unix_accounting, /* accounting */
603 NULL, /* checksimul */
604 NULL, /* pre-proxy */
605 NULL, /* post-proxy */