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, 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.
96 pwd = getpwnam(req->username->vp_strvalue);
100 grp = getgrnam(check->vp_strvalue);
104 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
106 for (member = grp->gr_mem; *member && retval; member++) {
107 if (strcmp(*member, pwd->pw_name) == 0)
118 static int unix_detach(void *instance)
120 #define inst ((struct unix_instance *)instance)
122 paircompare_unregister(PW_GROUP, groupcmp);
124 paircompare_unregister(PW_GROUP_NAME, groupcmp);
134 static int unix_instantiate(CONF_SECTION *conf, void **instance)
136 struct unix_instance *inst;
139 * Allocate room for the instance.
141 inst = *instance = rad_malloc(sizeof(*inst));
145 memset(inst, 0, sizeof(*inst));
148 * Parse the configuration, failing if we can't do so.
150 if (cf_section_parse(conf, inst, module_config) < 0) {
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);
169 * Pull the users password from where-ever, and add it to
172 static int unix_getpw(UNUSED void *instance, REQUEST *request,
173 VALUE_PAIR **vp_list)
176 const char *encrypted_pass;
178 struct spwd *spwd = NULL;
181 struct pr_passwd *pr_pw;
185 #ifdef HAVE_GETUSERSHELL
191 * We can only authenticate user requests which HAVE
192 * a User-Name attribute.
194 if (!request->username) {
195 return RLM_MODULE_NOOP;
198 name = (char *)request->username->vp_strvalue;
199 encrypted_pass = NULL;
202 if ((pr_pw = getprpwnam(name)) == NULL)
203 return RLM_MODULE_NOTFOUND;
204 encrypted_pass = pr_pw->ufld.fd_encrypt;
207 * Check if account is locked.
209 if (pr_pw->uflg.fg_lock!=1) {
210 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
211 return RLM_MODULE_USERLOCK;
214 if ((pwd = getpwnam(name)) == NULL) {
215 return RLM_MODULE_NOTFOUND;
217 encrypted_pass = pwd->pw_passwd;
222 * See if there is a shadow password.
224 * Only query the _system_ shadow file if the encrypted
225 * password from the passwd file is < 10 characters (i.e.
226 * a valid password would never crypt() to it). This will
227 * prevents users from using NULL password fields as things
230 if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
231 if ((spwd = getspnam(name)) == NULL) {
232 return RLM_MODULE_NOTFOUND;
234 encrypted_pass = spwd->sp_pwdp;
236 #endif /* HAVE_GETSPNAM */
239 * These require 'pwd != NULL', which isn't true on OSFC2
244 * Users with a particular shell are denied access
246 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
247 radlog_request(L_AUTH, 0, request,
248 "rlm_unix: [%s]: invalid shell", name);
249 return RLM_MODULE_REJECT;
253 #ifdef HAVE_GETUSERSHELL
255 * Check /etc/shells for a valid shell. If that file
256 * contains /RADIUSD/ANY/SHELL then any shell will do.
258 while ((shell = getusershell()) != NULL) {
259 if (strcmp(shell, pwd->pw_shell) == 0 ||
260 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
266 radlog_request(L_AUTH, 0, request, "[%s]: invalid shell [%s]",
267 name, pwd->pw_shell);
268 return RLM_MODULE_REJECT;
273 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
275 * Check if password has expired.
277 if (spwd && spwd->sp_expire > 0 &&
278 (request->timestamp / 86400) > spwd->sp_expire) {
279 radlog_request(L_AUTH, 0, request, "[%s]: password has expired", name);
280 return RLM_MODULE_REJECT;
284 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
286 * Check if password has expired.
288 if ((pwd->pw_expire > 0) &&
289 (request->timestamp > pwd->pw_expire)) {
290 radlog_request(L_AUTH, 0, request, "[%s]: password has expired", name);
291 return RLM_MODULE_REJECT;
296 * We might have a passwordless account.
298 * FIXME: Maybe add Auth-Type := Accept?
300 if (encrypted_pass[0] == 0)
301 return RLM_MODULE_NOOP;
303 vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
304 if (!vp) return RLM_MODULE_FAIL;
306 pairmove(vp_list, &vp);
307 pairfree(&vp); /* might not be NULL; */
309 return RLM_MODULE_UPDATED;
314 * Pull the users password from where-ever, and add it to
317 static int unix_authorize(void *instance, REQUEST *request)
319 return unix_getpw(instance, request, &request->config_items);
323 * Pull the users password from where-ever, and add it to
326 static int unix_authenticate(void *instance, REQUEST *request)
330 char *progname = "radius";
331 SIAENTITY *ent = NULL;
335 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
337 return RLM_MODULE_NOTFOUND;
338 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
340 sia_ses_release (&ent);
341 return RLM_MODULE_NOTFOUND;
343 if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
344 sia_ses_release (&ent);
345 return RLM_MODULE_NOTFOUND;
349 VALUE_PAIR *vp = NULL;
351 if (!request->password ||
352 (request->password->attribute != PW_USER_PASSWORD)) {
353 radlog_request(L_AUTH, 0, request, "Attribute \"User-Password\" is required for authentication.");
354 return RLM_MODULE_INVALID;
357 rcode = unix_getpw(instance, request, &vp);
358 if (rcode != RLM_MODULE_UPDATED) return rcode;
363 if (fr_crypt_check((char *) request->password->vp_strvalue,
364 (char *) vp->vp_strvalue) != 0) {
365 radlog_request(L_AUTH, 0, request, "invalid password \"%s\"",
366 request->username->vp_strvalue);
367 return RLM_MODULE_REJECT;
371 return RLM_MODULE_OK;
376 * UUencode 4 bits base64. We use this to turn a 4 byte field
377 * (an IP address) into 6 bytes of ASCII. This is used for the
378 * wtmp file if we didn't find a short name in the naslist file.
380 static char *uue(void *in)
383 static unsigned char res[7];
384 unsigned char *data = (unsigned char *)in;
386 res[0] = ENC( data[0] >> 2 );
387 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
388 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
389 res[3] = ENC( data[2] & 077 );
391 res[4] = ENC( data[3] >> 2 );
392 res[5] = ENC( (data[3] << 4) & 060 );
395 for(i = 0; i < 6; i++) {
396 if (res[i] == ' ') res[i] = '`';
397 if (res[i] < 32 || res[i] > 127)
398 printf("uue: protocol error ?!\n");
405 * Unix accounting - write a wtmp file.
407 static int unix_accounting(void *instance, REQUEST *request)
418 int framed_address = 0;
424 struct unix_instance *inst = (struct unix_instance *) instance;
427 * No radwtmp. Don't do anything.
429 if (!inst->radwtmp) {
430 RDEBUG2("No radwtmp file configured. Ignoring accounting request.");
431 return RLM_MODULE_NOOP;
434 if (request->packet->src_ipaddr.af != AF_INET) {
435 RDEBUG2("IPv6 is not supported!");
436 return RLM_MODULE_NOOP;
440 * Which type is this.
442 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0))==NULL) {
443 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
444 return RLM_MODULE_NOOP;
446 status = vp->vp_integer;
449 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
451 if (status != PW_STATUS_START &&
452 status != PW_STATUS_STOP)
453 return RLM_MODULE_NOOP;
456 * We're only interested in accounting messages
457 * with a username in it.
459 if (pairfind(request->packet->vps, PW_USER_NAME, 0) == NULL)
460 return RLM_MODULE_NOOP;
462 t = request->timestamp;
463 memset(&ut, 0, sizeof(ut));
466 * First, find the interesting attributes.
468 for (vp = request->packet->vps; vp; vp = vp->next) {
469 switch (vp->attribute) {
471 if (vp->length >= sizeof(ut.ut_name)) {
472 memcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
474 strlcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
477 case PW_LOGIN_IP_HOST:
478 case PW_FRAMED_IP_ADDRESS:
479 framed_address = vp->vp_ipaddr;
482 case PW_FRAMED_PROTOCOL:
483 protocol = vp->vp_integer;
486 case PW_NAS_IP_ADDRESS:
487 nas_address = vp->vp_ipaddr;
490 nas_port = vp->vp_integer;
493 case PW_ACCT_DELAY_TIME:
494 delay = vp->vp_ipaddr;
500 * We don't store !root sessions, or sessions
501 * where we didn't see a NAS-Port attribute.
503 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
504 return RLM_MODULE_NOOP;
507 * If we didn't find out the NAS address, use the
508 * originator's IP address.
510 if (nas_address == 0) {
511 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
513 s = request->client->shortname;
514 if (!s || s[0] == 0) s = uue(&(nas_address));
518 * Linux has a field for the client address.
520 ut.ut_addr = framed_address;
523 * We use the tty field to store the terminal servers' port
524 * and address so that the tty field is unique.
526 snprintf(buf, sizeof(buf), "%03d:%s", nas_port, s);
527 strlcpy(ut.ut_line, buf, sizeof(ut.ut_line));
530 * We store the dynamic IP address in the hostname field.
533 if (framed_address) {
534 ip_ntoa(buf, framed_address);
535 strlcpy(ut.ut_host, buf, sizeof(ut.ut_host));
539 ut.ut_xtime = t- delay;
541 ut.ut_time = t - delay;
545 * And we can use the ID field to store
548 if (protocol == PW_PPP)
549 strcpy(ut.ut_id, "P");
550 else if (protocol == PW_SLIP)
551 strcpy(ut.ut_id, "S");
553 strcpy(ut.ut_id, "T");
554 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
556 if (status == PW_STATUS_STOP)
560 * Write a RADIUS wtmp log file.
562 * Try to open the file if we can't, we don't write the
563 * wtmp file. If we can try to write. If we fail,
564 * return RLM_MODULE_FAIL ..
566 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
567 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
569 return RLM_MODULE_FAIL;
573 return RLM_MODULE_FAIL;
575 return RLM_MODULE_OK;
578 /* globally exported name */
579 module_t rlm_unix = {
582 RLM_TYPE_THREAD_UNSAFE | RLM_TYPE_CHECK_CONFIG_SAFE,
583 unix_instantiate, /* instantiation */
584 unix_detach, /* detach */
586 unix_authenticate, /* authentication */
587 unix_authorize, /* authorization */
588 NULL, /* preaccounting */
589 unix_accounting, /* accounting */
590 NULL, /* checksimul */
591 NULL, /* pre-proxy */
592 NULL, /* post-proxy */