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$";
34 #include <sys/types.h>
44 # include <sys/security.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, 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->strvalue;
103 pwd = getpwnam(username);
107 grp = getgrnam((char *)check->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)
123 * Done once when the module is loaded, and NOT on a per-instance
126 static int unix_init(void)
128 /* FIXME - delay these until a group file has been read so we know
129 * groupcmp can actually do something */
130 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
131 #ifdef PW_GROUP_NAME /* compat */
132 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
141 static int unix_detach(void *instance)
143 #define inst ((struct unix_instance *)instance)
154 static int unix_instantiate(CONF_SECTION *conf, void **instance)
156 struct unix_instance *inst;
159 * Allocate room for the instance.
161 inst = *instance = rad_malloc(sizeof(*inst));
165 memset(inst, 0, sizeof(*inst));
168 * Parse the configuration, failing if we can't do so.
170 if (cf_section_parse(conf, inst, module_config) < 0) {
182 static int unix_destroy(void)
184 paircompare_unregister(PW_GROUP, groupcmp);
186 paircompare_unregister(PW_GROUP_NAME, groupcmp);
193 * Pull the users password from where-ever, and add it to
196 static int unix_getpw(void *instance, REQUEST *request, VALUE_PAIR **vp_list)
199 const char *encrypted_pass;
201 struct spwd *spwd = NULL;
204 struct pr_passwd *pr_pw;
208 #ifdef HAVE_GETUSERSHELL
214 * We can only authenticate user requests which HAVE
215 * a User-Name attribute.
217 if (!request->username) {
218 return RLM_MODULE_NOOP;
221 name = (char *)request->username->strvalue;
222 encrypted_pass = NULL;
225 if ((pr_pw = getprpwnam(name)) == NULL)
226 return RLM_MODULE_NOTFOUND;
227 encrypted_pass = pr_pw->ufld.fd_encrypt;
230 * Check if account is locked.
232 if (pr_pw->uflg.fg_lock!=1) {
233 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
234 return RLM_MODULE_USERLOCK;
237 if ((pwd = getpwnam(name)) == NULL) {
238 return RLM_MODULE_NOTFOUND;
240 encrypted_pass = pwd->pw_passwd;
245 * See if there is a shadow password.
247 * Only query the _system_ shadow file if the encrypted
248 * password from the passwd file is < 10 characters (i.e.
249 * a valid password would never crypt() to it). This will
250 * prevents users from using NULL password fields as things
253 if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
254 if ((spwd = getspnam(name)) == NULL) {
255 return RLM_MODULE_NOTFOUND;
257 encrypted_pass = spwd->sp_pwdp;
259 #endif /* HAVE_GETSPNAM */
262 * These require 'pwd != NULL', which isn't true on OSFC2
267 * Users with a particular shell are denied access
269 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
270 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
271 return RLM_MODULE_REJECT;
275 #ifdef HAVE_GETUSERSHELL
277 * Check /etc/shells for a valid shell. If that file
278 * contains /RADIUSD/ANY/SHELL then any shell will do.
280 while ((shell = getusershell()) != NULL) {
281 if (strcmp(shell, pwd->pw_shell) == 0 ||
282 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
288 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
289 name, pwd->pw_shell);
290 return RLM_MODULE_REJECT;
295 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
297 * Check if password has expired.
299 if (spwd && spwd->sp_expire > 0 &&
300 (request->timestamp / 86400) > spwd->sp_expire) {
301 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
302 return RLM_MODULE_REJECT;
306 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
308 * Check if password has expired.
310 if ((pwd->pw_expire > 0) &&
311 (request->timestamp > pwd->pw_expire)) {
312 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
313 return RLM_MODULE_REJECT;
318 * We might have a passwordless account.
320 * FIXME: Maybe add Auth-Type := Accept?
322 if (encrypted_pass[0] == 0)
323 return RLM_MODULE_NOOP;
325 vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
326 if (!vp) return RLM_MODULE_FAIL;
328 pairmove(vp_list, &vp);
329 pairfree(&vp); /* might not be NULL; */
331 return RLM_MODULE_UPDATED;
336 * Pull the users password from where-ever, and add it to
339 static int unix_authorize(void *instance, REQUEST *request)
341 return unix_getpw(instance, request, &request->config_items);
345 * Pull the users password from where-ever, and add it to
348 static int unix_authenticate(void *instance, REQUEST *request)
352 char *progname = "radius";
353 SIAENTITY *ent = NULL;
357 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
359 return RLM_MODULE_NOTFOUND;
360 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
362 sia_ses_release (&ent);
363 return RLM_MODULE_NOTFOUND;
365 if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
366 sia_ses_release (&ent);
367 return RLM_MODULE_NOTFOUND;
371 VALUE_PAIR *vp = NULL;
373 if (!request->password ||
374 (request->password->attribute != PW_USER_PASSWORD)) {
375 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
376 return RLM_MODULE_INVALID;
379 rcode = unix_getpw(instance, request, &vp);
380 if (rcode != RLM_MODULE_UPDATED) return rcode;
385 if (lrad_crypt_check((char *) request->password->strvalue,
386 (char *) vp->strvalue) != 0) {
387 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
388 request->username->strvalue);
389 return RLM_MODULE_REJECT;
393 return RLM_MODULE_OK;
398 * UUencode 4 bits base64. We use this to turn a 4 byte field
399 * (an IP address) into 6 bytes of ASCII. This is used for the
400 * wtmp file if we didn't find a short name in the naslist file.
402 static char *uue(void *in)
405 static unsigned char res[7];
406 unsigned char *data = (unsigned char *)in;
408 res[0] = ENC( data[0] >> 2 );
409 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
410 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
411 res[3] = ENC( data[2] & 077 );
413 res[4] = ENC( data[3] >> 2 );
414 res[5] = ENC( (data[3] << 4) & 060 );
417 for(i = 0; i < 6; i++) {
418 if (res[i] == ' ') res[i] = '`';
419 if (res[i] < 32 || res[i] > 127)
420 printf("uue: protocol error ?!\n");
427 * Unix accounting - write a wtmp file.
429 static int unix_accounting(void *instance, REQUEST *request)
440 int framed_address = 0;
444 int nas_port_type = 0;
445 struct unix_instance *inst = (struct unix_instance *) instance;
448 * No radwtmp. Don't do anything.
450 if (!inst->radwtmp) {
451 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
452 return RLM_MODULE_NOOP;
455 if (request->packet->src_ipaddr.af != AF_INET) {
456 DEBUG2("rlm_unix: IPv6 is not supported!");
457 return RLM_MODULE_NOOP;
461 * Which type is this.
463 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
464 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
465 return RLM_MODULE_NOOP;
470 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
472 if (status != PW_STATUS_START &&
473 status != PW_STATUS_STOP)
474 return RLM_MODULE_NOOP;
477 * We're only interested in accounting messages
478 * with a username in it.
480 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
481 return RLM_MODULE_NOOP;
483 t = request->timestamp;
484 memset(&ut, 0, sizeof(ut));
487 * First, find the interesting attributes.
489 for (vp = request->packet->vps; vp; vp = vp->next) {
490 switch (vp->attribute) {
492 if (vp->length >= sizeof(ut.ut_name)) {
493 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
495 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
498 case PW_LOGIN_IP_HOST:
499 case PW_FRAMED_IP_ADDRESS:
500 framed_address = vp->lvalue;
502 case PW_FRAMED_PROTOCOL:
503 protocol = vp->lvalue;
505 case PW_NAS_IP_ADDRESS:
506 nas_address = vp->lvalue;
509 nas_port = vp->lvalue;
512 case PW_ACCT_DELAY_TIME:
515 case PW_NAS_PORT_TYPE:
516 nas_port_type = vp->lvalue;
522 * We don't store !root sessions, or sessions
523 * where we didn't see a NAS-Port attribute.
525 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
526 return RLM_MODULE_NOOP;
531 * If we didn't find out the NAS address, use the
532 * originator's IP address.
534 if (nas_address == 0) {
536 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
538 if ((cl = client_find_old(&request->packet->src_ipaddr)) != NULL)
541 if (!s || s[0] == 0) s = uue(&(nas_address));
545 * Linux has a field for the client address.
547 ut.ut_addr = framed_address;
550 * We use the tty field to store the terminal servers' port
551 * and address so that the tty field is unique.
553 sprintf(buf, "%03d:%s", nas_port, s);
554 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
557 * We store the dynamic IP address in the hostname field.
560 if (framed_address) {
561 ip_ntoa(buf, framed_address);
562 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
566 ut.ut_xtime = t- delay;
568 ut.ut_time = t - delay;
572 * And we can use the ID field to store
575 if (protocol == PW_PPP)
576 strcpy(ut.ut_id, "P");
577 else if (protocol == PW_SLIP)
578 strcpy(ut.ut_id, "S");
580 strcpy(ut.ut_id, "T");
581 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
583 if (status == PW_STATUS_STOP)
587 * Write a RADIUS wtmp log file.
589 * Try to open the file if we can't, we don't write the
590 * wtmp file. If we can try to write. If we fail,
591 * return RLM_MODULE_FAIL ..
593 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
594 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
596 return RLM_MODULE_FAIL;
600 return RLM_MODULE_FAIL;
602 return RLM_MODULE_OK;
605 /* globally exported name */
606 module_t rlm_unix = {
608 RLM_TYPE_THREAD_UNSAFE, /* type */
609 unix_init, /* initialization */
610 unix_instantiate, /* instantiation */
612 unix_authenticate, /* authentication */
613 unix_authorize, /* authorization */
614 NULL, /* preaccounting */
615 unix_accounting, /* accounting */
616 NULL, /* checksimul */
617 NULL, /* pre-proxy */
618 NULL, /* post-proxy */
621 unix_detach, /* detach */
622 unix_destroy, /* destroy */