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)
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(void *instance, REQUEST *request, VALUE_PAIR **vp_list)
184 const char *encrypted_pass;
186 struct spwd *spwd = NULL;
189 struct pr_passwd *pr_pw;
193 #ifdef HAVE_GETUSERSHELL
199 * We can only authenticate user requests which HAVE
200 * a User-Name attribute.
202 if (!request->username) {
203 return RLM_MODULE_NOOP;
206 name = (char *)request->username->strvalue;
207 encrypted_pass = NULL;
210 if ((pr_pw = getprpwnam(name)) == NULL)
211 return RLM_MODULE_NOTFOUND;
212 encrypted_pass = pr_pw->ufld.fd_encrypt;
215 * Check if account is locked.
217 if (pr_pw->uflg.fg_lock!=1) {
218 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
219 return RLM_MODULE_USERLOCK;
222 if ((pwd = getpwnam(name)) == NULL) {
223 return RLM_MODULE_NOTFOUND;
225 encrypted_pass = pwd->pw_passwd;
230 * See if there is a shadow password.
232 * Only query the _system_ shadow file if the encrypted
233 * password from the passwd file is < 10 characters (i.e.
234 * a valid password would never crypt() to it). This will
235 * prevents users from using NULL password fields as things
238 if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
239 if ((spwd = getspnam(name)) == NULL) {
240 return RLM_MODULE_NOTFOUND;
242 encrypted_pass = spwd->sp_pwdp;
244 #endif /* HAVE_GETSPNAM */
247 * These require 'pwd != NULL', which isn't true on OSFC2
252 * Users with a particular shell are denied access
254 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
255 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
256 return RLM_MODULE_REJECT;
260 #ifdef HAVE_GETUSERSHELL
262 * Check /etc/shells for a valid shell. If that file
263 * contains /RADIUSD/ANY/SHELL then any shell will do.
265 while ((shell = getusershell()) != NULL) {
266 if (strcmp(shell, pwd->pw_shell) == 0 ||
267 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
273 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
274 name, pwd->pw_shell);
275 return RLM_MODULE_REJECT;
280 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
282 * Check if password has expired.
284 if (spwd && spwd->sp_expire > 0 &&
285 (request->timestamp / 86400) > spwd->sp_expire) {
286 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
287 return RLM_MODULE_REJECT;
291 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
293 * Check if password has expired.
295 if ((pwd->pw_expire > 0) &&
296 (request->timestamp > pwd->pw_expire)) {
297 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
298 return RLM_MODULE_REJECT;
303 * We might have a passwordless account.
305 * FIXME: Maybe add Auth-Type := Accept?
307 if (encrypted_pass[0] == 0)
308 return RLM_MODULE_NOOP;
310 vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
311 if (!vp) return RLM_MODULE_FAIL;
313 pairmove(vp_list, &vp);
314 pairfree(&vp); /* might not be NULL; */
316 return RLM_MODULE_UPDATED;
321 * Pull the users password from where-ever, and add it to
324 static int unix_authorize(void *instance, REQUEST *request)
326 return unix_getpw(instance, request, &request->config_items);
330 * Pull the users password from where-ever, and add it to
333 static int unix_authenticate(void *instance, REQUEST *request)
337 char *progname = "radius";
338 SIAENTITY *ent = NULL;
342 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
344 return RLM_MODULE_NOTFOUND;
345 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
347 sia_ses_release (&ent);
348 return RLM_MODULE_NOTFOUND;
350 if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
351 sia_ses_release (&ent);
352 return RLM_MODULE_NOTFOUND;
356 VALUE_PAIR *vp = NULL;
358 if (!request->password ||
359 (request->password->attribute != PW_USER_PASSWORD)) {
360 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
361 return RLM_MODULE_INVALID;
364 rcode = unix_getpw(instance, request, &vp);
365 if (rcode != RLM_MODULE_UPDATED) return rcode;
370 if (lrad_crypt_check((char *) request->password->strvalue,
371 (char *) vp->strvalue) != 0) {
372 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
373 request->username->strvalue);
374 return RLM_MODULE_REJECT;
378 return RLM_MODULE_OK;
383 * UUencode 4 bits base64. We use this to turn a 4 byte field
384 * (an IP address) into 6 bytes of ASCII. This is used for the
385 * wtmp file if we didn't find a short name in the naslist file.
387 static char *uue(void *in)
390 static unsigned char res[7];
391 unsigned char *data = (unsigned char *)in;
393 res[0] = ENC( data[0] >> 2 );
394 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
395 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
396 res[3] = ENC( data[2] & 077 );
398 res[4] = ENC( data[3] >> 2 );
399 res[5] = ENC( (data[3] << 4) & 060 );
402 for(i = 0; i < 6; i++) {
403 if (res[i] == ' ') res[i] = '`';
404 if (res[i] < 32 || res[i] > 127)
405 printf("uue: protocol error ?!\n");
412 * Unix accounting - write a wtmp file.
414 static int unix_accounting(void *instance, REQUEST *request)
425 int framed_address = 0;
429 int nas_port_type = 0;
430 struct unix_instance *inst = (struct unix_instance *) instance;
433 * No radwtmp. Don't do anything.
435 if (!inst->radwtmp) {
436 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
437 return RLM_MODULE_NOOP;
440 if (request->packet->src_ipaddr.af != AF_INET) {
441 DEBUG2("rlm_unix: IPv6 is not supported!");
442 return RLM_MODULE_NOOP;
446 * Which type is this.
448 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
449 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
450 return RLM_MODULE_NOOP;
455 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
457 if (status != PW_STATUS_START &&
458 status != PW_STATUS_STOP)
459 return RLM_MODULE_NOOP;
462 * We're only interested in accounting messages
463 * with a username in it.
465 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
466 return RLM_MODULE_NOOP;
468 t = request->timestamp;
469 memset(&ut, 0, sizeof(ut));
472 * First, find the interesting attributes.
474 for (vp = request->packet->vps; vp; vp = vp->next) {
475 switch (vp->attribute) {
477 if (vp->length >= sizeof(ut.ut_name)) {
478 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
480 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
483 case PW_LOGIN_IP_HOST:
484 case PW_FRAMED_IP_ADDRESS:
485 framed_address = vp->lvalue;
487 case PW_FRAMED_PROTOCOL:
488 protocol = vp->lvalue;
490 case PW_NAS_IP_ADDRESS:
491 nas_address = vp->lvalue;
494 nas_port = vp->lvalue;
497 case PW_ACCT_DELAY_TIME:
500 case PW_NAS_PORT_TYPE:
501 nas_port_type = vp->lvalue;
507 * We don't store !root sessions, or sessions
508 * where we didn't see a NAS-Port attribute.
510 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
511 return RLM_MODULE_NOOP;
516 * If we didn't find out the NAS address, use the
517 * originator's IP address.
519 if (nas_address == 0) {
521 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
523 if ((cl = client_find_old(&request->packet->src_ipaddr)) != NULL)
526 if (!s || s[0] == 0) s = uue(&(nas_address));
530 * Linux has a field for the client address.
532 ut.ut_addr = framed_address;
535 * We use the tty field to store the terminal servers' port
536 * and address so that the tty field is unique.
538 sprintf(buf, "%03d:%s", nas_port, s);
539 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
542 * We store the dynamic IP address in the hostname field.
545 if (framed_address) {
546 ip_ntoa(buf, framed_address);
547 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
551 ut.ut_xtime = t- delay;
553 ut.ut_time = t - delay;
557 * And we can use the ID field to store
560 if (protocol == PW_PPP)
561 strcpy(ut.ut_id, "P");
562 else if (protocol == PW_SLIP)
563 strcpy(ut.ut_id, "S");
565 strcpy(ut.ut_id, "T");
566 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
568 if (status == PW_STATUS_STOP)
572 * Write a RADIUS wtmp log file.
574 * Try to open the file if we can't, we don't write the
575 * wtmp file. If we can try to write. If we fail,
576 * return RLM_MODULE_FAIL ..
578 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
579 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
581 return RLM_MODULE_FAIL;
585 return RLM_MODULE_FAIL;
587 return RLM_MODULE_OK;
590 /* globally exported name */
591 module_t rlm_unix = {
594 RLM_TYPE_THREAD_UNSAFE, /* type */
595 unix_instantiate, /* instantiation */
596 unix_detach, /* detach */
598 unix_authenticate, /* authentication */
599 unix_authorize, /* authorization */
600 NULL, /* preaccounting */
601 unix_accounting, /* accounting */
602 NULL, /* checksimul */
603 NULL, /* pre-proxy */
604 NULL, /* post-proxy */