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, 0);
94 vp = pairfind(request, PW_USER_NAME, 0);
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_request(L_AUTH, 0, request,
253 "rlm_unix: [%s]: invalid shell", name);
254 return RLM_MODULE_REJECT;
258 #ifdef HAVE_GETUSERSHELL
260 * Check /etc/shells for a valid shell. If that file
261 * contains /RADIUSD/ANY/SHELL then any shell will do.
263 while ((shell = getusershell()) != NULL) {
264 if (strcmp(shell, pwd->pw_shell) == 0 ||
265 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
271 radlog_request(L_AUTH, 0, request, "[%s]: invalid shell [%s]",
272 name, pwd->pw_shell);
273 return RLM_MODULE_REJECT;
278 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
280 * Check if password has expired.
282 if (spwd && spwd->sp_expire > 0 &&
283 (request->timestamp / 86400) > spwd->sp_expire) {
284 radlog_request(L_AUTH, 0, request, "[%s]: password has expired", name);
285 return RLM_MODULE_REJECT;
289 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
291 * Check if password has expired.
293 if ((pwd->pw_expire > 0) &&
294 (request->timestamp > pwd->pw_expire)) {
295 radlog_request(L_AUTH, 0, request, "[%s]: password has expired", name);
296 return RLM_MODULE_REJECT;
301 * We might have a passwordless account.
303 * FIXME: Maybe add Auth-Type := Accept?
305 if (encrypted_pass[0] == 0)
306 return RLM_MODULE_NOOP;
308 vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
309 if (!vp) return RLM_MODULE_FAIL;
311 pairmove(vp_list, &vp);
312 pairfree(&vp); /* might not be NULL; */
314 return RLM_MODULE_UPDATED;
319 * Pull the users password from where-ever, and add it to
322 static int unix_authorize(void *instance, REQUEST *request)
324 return unix_getpw(instance, request, &request->config_items);
328 * Pull the users password from where-ever, and add it to
331 static int unix_authenticate(void *instance, REQUEST *request)
335 char *progname = "radius";
336 SIAENTITY *ent = NULL;
340 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
342 return RLM_MODULE_NOTFOUND;
343 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
345 sia_ses_release (&ent);
346 return RLM_MODULE_NOTFOUND;
348 if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
349 sia_ses_release (&ent);
350 return RLM_MODULE_NOTFOUND;
354 VALUE_PAIR *vp = NULL;
356 if (!request->password ||
357 (request->password->attribute != PW_USER_PASSWORD)) {
358 radlog_request(L_AUTH, 0, request, "Attribute \"User-Password\" is required for authentication.");
359 return RLM_MODULE_INVALID;
362 rcode = unix_getpw(instance, request, &vp);
363 if (rcode != RLM_MODULE_UPDATED) return rcode;
368 if (fr_crypt_check((char *) request->password->vp_strvalue,
369 (char *) vp->vp_strvalue) != 0) {
370 radlog_request(L_AUTH, 0, request, "invalid password \"%s\"",
371 request->username->vp_strvalue);
372 return RLM_MODULE_REJECT;
376 return RLM_MODULE_OK;
381 * UUencode 4 bits base64. We use this to turn a 4 byte field
382 * (an IP address) into 6 bytes of ASCII. This is used for the
383 * wtmp file if we didn't find a short name in the naslist file.
385 static char *uue(void *in)
388 static unsigned char res[7];
389 unsigned char *data = (unsigned char *)in;
391 res[0] = ENC( data[0] >> 2 );
392 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
393 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
394 res[3] = ENC( data[2] & 077 );
396 res[4] = ENC( data[3] >> 2 );
397 res[5] = ENC( (data[3] << 4) & 060 );
400 for(i = 0; i < 6; i++) {
401 if (res[i] == ' ') res[i] = '`';
402 if (res[i] < 32 || res[i] > 127)
403 printf("uue: protocol error ?!\n");
410 * Unix accounting - write a wtmp file.
412 static int unix_accounting(void *instance, REQUEST *request)
423 int framed_address = 0;
429 struct unix_instance *inst = (struct unix_instance *) instance;
432 * No radwtmp. Don't do anything.
434 if (!inst->radwtmp) {
435 RDEBUG2("No radwtmp file configured. Ignoring accounting request.");
436 return RLM_MODULE_NOOP;
439 if (request->packet->src_ipaddr.af != AF_INET) {
440 RDEBUG2("IPv6 is not supported!");
441 return RLM_MODULE_NOOP;
445 * Which type is this.
447 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0))==NULL) {
448 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
449 return RLM_MODULE_NOOP;
451 status = vp->vp_integer;
454 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
456 if (status != PW_STATUS_START &&
457 status != PW_STATUS_STOP)
458 return RLM_MODULE_NOOP;
461 * We're only interested in accounting messages
462 * with a username in it.
464 if (pairfind(request->packet->vps, PW_USER_NAME, 0) == NULL)
465 return RLM_MODULE_NOOP;
467 t = request->timestamp;
468 memset(&ut, 0, sizeof(ut));
471 * First, find the interesting attributes.
473 for (vp = request->packet->vps; vp; vp = vp->next) {
474 switch (vp->attribute) {
476 if (vp->length >= sizeof(ut.ut_name)) {
477 memcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
479 strlcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
482 case PW_LOGIN_IP_HOST:
483 case PW_FRAMED_IP_ADDRESS:
484 framed_address = vp->vp_ipaddr;
487 case PW_FRAMED_PROTOCOL:
488 protocol = vp->vp_integer;
491 case PW_NAS_IP_ADDRESS:
492 nas_address = vp->vp_ipaddr;
495 nas_port = vp->vp_integer;
498 case PW_ACCT_DELAY_TIME:
499 delay = vp->vp_ipaddr;
505 * We don't store !root sessions, or sessions
506 * where we didn't see a NAS-Port attribute.
508 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
509 return RLM_MODULE_NOOP;
512 * If we didn't find out the NAS address, use the
513 * originator's IP address.
515 if (nas_address == 0) {
516 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
518 s = request->client->shortname;
519 if (!s || s[0] == 0) s = uue(&(nas_address));
523 * Linux has a field for the client address.
525 ut.ut_addr = framed_address;
528 * We use the tty field to store the terminal servers' port
529 * and address so that the tty field is unique.
531 snprintf(buf, sizeof(buf), "%03d:%s", nas_port, s);
532 strlcpy(ut.ut_line, buf, sizeof(ut.ut_line));
535 * We store the dynamic IP address in the hostname field.
538 if (framed_address) {
539 ip_ntoa(buf, framed_address);
540 strlcpy(ut.ut_host, buf, sizeof(ut.ut_host));
544 ut.ut_xtime = t- delay;
546 ut.ut_time = t - delay;
550 * And we can use the ID field to store
553 if (protocol == PW_PPP)
554 strcpy(ut.ut_id, "P");
555 else if (protocol == PW_SLIP)
556 strcpy(ut.ut_id, "S");
558 strcpy(ut.ut_id, "T");
559 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
561 if (status == PW_STATUS_STOP)
565 * Write a RADIUS wtmp log file.
567 * Try to open the file if we can't, we don't write the
568 * wtmp file. If we can try to write. If we fail,
569 * return RLM_MODULE_FAIL ..
571 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
572 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
574 return RLM_MODULE_FAIL;
578 return RLM_MODULE_FAIL;
580 return RLM_MODULE_OK;
583 /* globally exported name */
584 module_t rlm_unix = {
587 RLM_TYPE_THREAD_UNSAFE | RLM_TYPE_CHECK_CONFIG_SAFE,
588 unix_instantiate, /* instantiation */
589 unix_detach, /* detach */
591 unix_authenticate, /* authentication */
592 unix_authorize, /* authorization */
593 NULL, /* preaccounting */
594 unix_accounting, /* accounting */
595 NULL, /* checksimul */
596 NULL, /* pre-proxy */
597 NULL, /* post-proxy */