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,2006 The FreeRADIUS server project
23 * Copyright 2000 Jeff Carneal <jeff@apex.net>
24 * Copyright 2000 Alan Curry <pacman@world.std.com>
27 #include <freeradius-devel/ident.h>
30 #include <freeradius-devel/radiusd.h>
43 # include <sys/security.h>
52 #include <freeradius-devel/modules.h>
53 #include <freeradius-devel/sysutmp.h>
55 static char trans[64] =
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
57 #define ENC(c) trans[c]
59 struct unix_instance {
63 static const CONF_PARSER module_config[] = {
64 { "radwtmp", PW_TYPE_STRING_PTR,
65 offsetof(struct unix_instance,radwtmp), NULL, "NULL" },
67 { NULL, -1, 0, NULL, NULL } /* end the list */
72 * The Group = handler.
74 static int groupcmp(void *instance, UNUSED REQUEST *req, VALUE_PAIR *request,
75 VALUE_PAIR *check, VALUE_PAIR *check_pairs,
76 VALUE_PAIR **reply_pairs)
86 check_pairs = check_pairs;
87 reply_pairs = reply_pairs;
90 * No user name, doesn't compare.
92 vp = pairfind(request, PW_STRIPPED_USER_NAME);
94 vp = pairfind(request, PW_USER_NAME);
99 username = (char *)vp->vp_strvalue;
101 pwd = getpwnam(username);
105 grp = getgrnam((char *)check->vp_strvalue);
109 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
111 for (member = grp->gr_mem; *member && retval; member++) {
112 if (strcmp(*member, pwd->pw_name) == 0)
123 static int unix_detach(void *instance)
125 #define inst ((struct unix_instance *)instance)
127 paircompare_unregister(PW_GROUP, groupcmp);
129 paircompare_unregister(PW_GROUP_NAME, groupcmp);
139 static int unix_instantiate(CONF_SECTION *conf, void **instance)
141 struct unix_instance *inst;
144 * Allocate room for the instance.
146 inst = *instance = rad_malloc(sizeof(*inst));
150 memset(inst, 0, sizeof(*inst));
153 * Parse the configuration, failing if we can't do so.
155 if (cf_section_parse(conf, inst, module_config) < 0) {
160 /* FIXME - delay these until a group file has been read so we know
161 * groupcmp can actually do something */
162 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
163 #ifdef PW_GROUP_NAME /* compat */
164 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
174 * Pull the users password from where-ever, and add it to
177 static int unix_getpw(UNUSED void *instance, REQUEST *request,
178 VALUE_PAIR **vp_list)
181 const char *encrypted_pass;
183 struct spwd *spwd = NULL;
186 struct pr_passwd *pr_pw;
190 #ifdef HAVE_GETUSERSHELL
196 * We can only authenticate user requests which HAVE
197 * a User-Name attribute.
199 if (!request->username) {
200 return RLM_MODULE_NOOP;
203 name = (char *)request->username->vp_strvalue;
204 encrypted_pass = NULL;
207 if ((pr_pw = getprpwnam(name)) == NULL)
208 return RLM_MODULE_NOTFOUND;
209 encrypted_pass = pr_pw->ufld.fd_encrypt;
212 * Check if account is locked.
214 if (pr_pw->uflg.fg_lock!=1) {
215 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
216 return RLM_MODULE_USERLOCK;
219 if ((pwd = getpwnam(name)) == NULL) {
220 return RLM_MODULE_NOTFOUND;
222 encrypted_pass = pwd->pw_passwd;
227 * See if there is a shadow password.
229 * Only query the _system_ shadow file if the encrypted
230 * password from the passwd file is < 10 characters (i.e.
231 * a valid password would never crypt() to it). This will
232 * prevents users from using NULL password fields as things
235 if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
236 if ((spwd = getspnam(name)) == NULL) {
237 return RLM_MODULE_NOTFOUND;
239 encrypted_pass = spwd->sp_pwdp;
241 #endif /* HAVE_GETSPNAM */
244 * These require 'pwd != NULL', which isn't true on OSFC2
249 * Users with a particular shell are denied access
251 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
252 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
253 return RLM_MODULE_REJECT;
257 #ifdef HAVE_GETUSERSHELL
259 * Check /etc/shells for a valid shell. If that file
260 * contains /RADIUSD/ANY/SHELL then any shell will do.
262 while ((shell = getusershell()) != NULL) {
263 if (strcmp(shell, pwd->pw_shell) == 0 ||
264 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
270 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
271 name, pwd->pw_shell);
272 return RLM_MODULE_REJECT;
277 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
279 * Check if password has expired.
281 if (spwd && spwd->sp_expire > 0 &&
282 (request->timestamp / 86400) > spwd->sp_expire) {
283 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
284 return RLM_MODULE_REJECT;
288 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
290 * Check if password has expired.
292 if ((pwd->pw_expire > 0) &&
293 (request->timestamp > pwd->pw_expire)) {
294 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
295 return RLM_MODULE_REJECT;
300 * We might have a passwordless account.
302 * FIXME: Maybe add Auth-Type := Accept?
304 if (encrypted_pass[0] == 0)
305 return RLM_MODULE_NOOP;
307 vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
308 if (!vp) return RLM_MODULE_FAIL;
310 pairmove(vp_list, &vp);
311 pairfree(&vp); /* might not be NULL; */
313 return RLM_MODULE_UPDATED;
318 * Pull the users password from where-ever, and add it to
321 static int unix_authorize(void *instance, REQUEST *request)
323 return unix_getpw(instance, request, &request->config_items);
327 * Pull the users password from where-ever, and add it to
330 static int unix_authenticate(void *instance, REQUEST *request)
334 char *progname = "radius";
335 SIAENTITY *ent = NULL;
339 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
341 return RLM_MODULE_NOTFOUND;
342 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
344 sia_ses_release (&ent);
345 return RLM_MODULE_NOTFOUND;
347 if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
348 sia_ses_release (&ent);
349 return RLM_MODULE_NOTFOUND;
353 VALUE_PAIR *vp = NULL;
355 if (!request->password ||
356 (request->password->attribute != PW_USER_PASSWORD)) {
357 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
358 return RLM_MODULE_INVALID;
361 rcode = unix_getpw(instance, request, &vp);
362 if (rcode != RLM_MODULE_UPDATED) return rcode;
367 if (lrad_crypt_check((char *) request->password->vp_strvalue,
368 (char *) vp->vp_strvalue) != 0) {
369 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
370 request->username->vp_strvalue);
371 return RLM_MODULE_REJECT;
375 return RLM_MODULE_OK;
380 * UUencode 4 bits base64. We use this to turn a 4 byte field
381 * (an IP address) into 6 bytes of ASCII. This is used for the
382 * wtmp file if we didn't find a short name in the naslist file.
384 static char *uue(void *in)
387 static unsigned char res[7];
388 unsigned char *data = (unsigned char *)in;
390 res[0] = ENC( data[0] >> 2 );
391 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
392 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
393 res[3] = ENC( data[2] & 077 );
395 res[4] = ENC( data[3] >> 2 );
396 res[5] = ENC( (data[3] << 4) & 060 );
399 for(i = 0; i < 6; i++) {
400 if (res[i] == ' ') res[i] = '`';
401 if (res[i] < 32 || res[i] > 127)
402 printf("uue: protocol error ?!\n");
409 * Unix accounting - write a wtmp file.
411 static int unix_accounting(void *instance, REQUEST *request)
422 int framed_address = 0;
426 int nas_port_type = 0;
427 struct unix_instance *inst = (struct unix_instance *) instance;
430 * No radwtmp. Don't do anything.
432 if (!inst->radwtmp) {
433 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
434 return RLM_MODULE_NOOP;
437 if (request->packet->src_ipaddr.af != AF_INET) {
438 DEBUG2("rlm_unix: IPv6 is not supported!");
439 return RLM_MODULE_NOOP;
443 * Which type is this.
445 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
446 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
447 return RLM_MODULE_NOOP;
452 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
454 if (status != PW_STATUS_START &&
455 status != PW_STATUS_STOP)
456 return RLM_MODULE_NOOP;
459 * We're only interested in accounting messages
460 * with a username in it.
462 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
463 return RLM_MODULE_NOOP;
465 t = request->timestamp;
466 memset(&ut, 0, sizeof(ut));
469 * First, find the interesting attributes.
471 for (vp = request->packet->vps; vp; vp = vp->next) {
472 switch (vp->attribute) {
474 if (vp->length >= sizeof(ut.ut_name)) {
475 memcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
477 strlcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
480 case PW_LOGIN_IP_HOST:
481 case PW_FRAMED_IP_ADDRESS:
482 framed_address = vp->lvalue;
484 case PW_FRAMED_PROTOCOL:
485 protocol = vp->lvalue;
487 case PW_NAS_IP_ADDRESS:
488 nas_address = vp->lvalue;
491 nas_port = vp->lvalue;
494 case PW_ACCT_DELAY_TIME:
497 case PW_NAS_PORT_TYPE:
498 nas_port_type = vp->lvalue;
504 * We don't store !root sessions, or sessions
505 * where we didn't see a NAS-Port attribute.
507 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
508 return RLM_MODULE_NOOP;
513 * If we didn't find out the NAS address, use the
514 * originator's IP address.
516 if (nas_address == 0) {
518 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
520 if ((cl = client_find_old(&request->packet->src_ipaddr)) != NULL)
523 if (!s || s[0] == 0) s = uue(&(nas_address));
527 * Linux has a field for the client address.
529 ut.ut_addr = framed_address;
532 * We use the tty field to store the terminal servers' port
533 * and address so that the tty field is unique.
535 sprintf(buf, "%03d:%s", nas_port, s);
536 strlcpy(ut.ut_line, buf, sizeof(ut.ut_line));
539 * We store the dynamic IP address in the hostname field.
542 if (framed_address) {
543 ip_ntoa(buf, framed_address);
544 strlcpy(ut.ut_host, buf, sizeof(ut.ut_host));
548 ut.ut_xtime = t- delay;
550 ut.ut_time = t - delay;
554 * And we can use the ID field to store
557 if (protocol == PW_PPP)
558 strcpy(ut.ut_id, "P");
559 else if (protocol == PW_SLIP)
560 strcpy(ut.ut_id, "S");
562 strcpy(ut.ut_id, "T");
563 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
565 if (status == PW_STATUS_STOP)
569 * Write a RADIUS wtmp log file.
571 * Try to open the file if we can't, we don't write the
572 * wtmp file. If we can try to write. If we fail,
573 * return RLM_MODULE_FAIL ..
575 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
576 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
578 return RLM_MODULE_FAIL;
582 return RLM_MODULE_FAIL;
584 return RLM_MODULE_OK;
587 /* globally exported name */
588 module_t rlm_unix = {
591 RLM_TYPE_THREAD_UNSAFE, /* type */
592 unix_instantiate, /* instantiation */
593 unix_detach, /* detach */
595 unix_authenticate, /* authentication */
596 unix_authorize, /* authorization */
597 NULL, /* preaccounting */
598 unix_accounting, /* accounting */
599 NULL, /* checksimul */
600 NULL, /* pre-proxy */
601 NULL, /* post-proxy */