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$";
29 #include "libradius.h"
35 #include <sys/types.h>
45 # include <sys/security.h>
58 static char trans[64] =
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
60 #define ENC(c) trans[c]
62 struct unix_instance {
66 static CONF_PARSER module_config[] = {
67 { "radwtmp", PW_TYPE_STRING_PTR,
68 offsetof(struct unix_instance,radwtmp), NULL, "NULL" },
70 { NULL, -1, 0, NULL, NULL } /* end the list */
75 * The Group = handler.
77 static int groupcmp(void *instance, REQUEST *req, VALUE_PAIR *request,
78 VALUE_PAIR *check, VALUE_PAIR *check_pairs,
79 VALUE_PAIR **reply_pairs)
89 check_pairs = check_pairs;
90 reply_pairs = reply_pairs;
93 * No user name, doesn't compare.
95 vp = pairfind(request, PW_STRIPPED_USER_NAME);
97 vp = pairfind(request, PW_USER_NAME);
102 username = (char *)vp->strvalue;
104 pwd = getpwnam(username);
108 grp = getgrnam((char *)check->strvalue);
112 retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
114 for (member = grp->gr_mem; *member && retval; member++) {
115 if (strcmp(*member, pwd->pw_name) == 0)
124 * Done once when the module is loaded, and NOT on a per-instance
127 static int unix_init(void)
129 /* FIXME - delay these until a group file has been read so we know
130 * groupcmp can actually do something */
131 paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
132 #ifdef PW_GROUP_NAME /* compat */
133 paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
142 static int unix_detach(void *instance)
144 #define inst ((struct unix_instance *)instance)
155 static int unix_instantiate(CONF_SECTION *conf, void **instance)
157 struct unix_instance *inst;
160 * Allocate room for the instance.
162 inst = *instance = rad_malloc(sizeof(*inst));
166 memset(inst, 0, sizeof(*inst));
169 * Parse the configuration, failing if we can't do so.
171 if (cf_section_parse(conf, inst, module_config) < 0) {
183 static int unix_destroy(void)
185 paircompare_unregister(PW_GROUP, groupcmp);
187 paircompare_unregister(PW_GROUP_NAME, groupcmp);
194 * Pull the users password from where-ever, and add it to
197 static int unix_getpw(void *instance, REQUEST *request, VALUE_PAIR **vp_list)
200 const char *encrypted_pass;
202 struct spwd *spwd = NULL;
205 struct pr_passwd *pr_pw;
209 #ifdef HAVE_GETUSERSHELL
215 * We can only authenticate user requests which HAVE
216 * a User-Name attribute.
218 if (!request->username) {
219 return RLM_MODULE_NOOP;
222 name = (char *)request->username->strvalue;
223 encrypted_pass = NULL;
226 if ((pr_pw = getprpwnam(name)) == NULL)
227 return RLM_MODULE_NOTFOUND;
228 encrypted_pass = pr_pw->ufld.fd_encrypt;
231 * Check if account is locked.
233 if (pr_pw->uflg.fg_lock!=1) {
234 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
235 return RLM_MODULE_USERLOCK;
238 if ((pwd = getpwnam(name)) == NULL) {
239 return RLM_MODULE_NOTFOUND;
241 encrypted_pass = pwd->pw_passwd;
246 * See if there is a shadow password.
248 * Only query the _system_ shadow file if the encrypted
249 * password from the passwd file is < 10 characters (i.e.
250 * a valid password would never crypt() to it). This will
251 * prevents users from using NULL password fields as things
254 if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
255 if ((spwd = getspnam(name)) == NULL) {
256 return RLM_MODULE_NOTFOUND;
258 encrypted_pass = spwd->sp_pwdp;
260 #endif /* HAVE_GETSPNAM */
263 * These require 'pwd != NULL', which isn't true on OSFC2
268 * Users with a particular shell are denied access
270 if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
271 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
272 return RLM_MODULE_REJECT;
276 #ifdef HAVE_GETUSERSHELL
278 * Check /etc/shells for a valid shell. If that file
279 * contains /RADIUSD/ANY/SHELL then any shell will do.
281 while ((shell = getusershell()) != NULL) {
282 if (strcmp(shell, pwd->pw_shell) == 0 ||
283 strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
289 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
290 name, pwd->pw_shell);
291 return RLM_MODULE_REJECT;
296 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
298 * Check if password has expired.
300 if (spwd && spwd->sp_expire > 0 &&
301 (request->timestamp / 86400) > spwd->sp_expire) {
302 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
303 return RLM_MODULE_REJECT;
307 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
309 * Check if password has expired.
311 if ((pwd->pw_expire > 0) &&
312 (request->timestamp > pwd->pw_expire)) {
313 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
314 return RLM_MODULE_REJECT;
319 * We might have a passwordless account.
321 * FIXME: Maybe add Auth-Type := Accept?
323 if (encrypted_pass[0] == 0)
324 return RLM_MODULE_NOOP;
326 vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
327 if (!vp) return RLM_MODULE_FAIL;
329 pairmove(vp_list, &vp);
330 pairfree(&vp); /* might not be NULL; */
332 return RLM_MODULE_UPDATED;
337 * Pull the users password from where-ever, and add it to
340 static int unix_authorize(void *instance, REQUEST *request)
342 return unix_getpw(instance, request, &request->config_items);
346 * Pull the users password from where-ever, and add it to
349 static int unix_authenticate(void *instance, REQUEST *request)
353 char *progname = "radius";
354 SIAENTITY *ent = NULL;
358 if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
360 return RLM_MODULE_NOTFOUND;
361 if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
363 sia_ses_release (&ent);
364 return RLM_MODULE_NOTFOUND;
366 if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
367 sia_ses_release (&ent);
368 return RLM_MODULE_NOTFOUND;
372 VALUE_PAIR *vp = NULL;
374 if (!request->password ||
375 (request->password->attribute != PW_USER_PASSWORD)) {
376 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
377 return RLM_MODULE_INVALID;
380 rcode = unix_getpw(instance, request, &vp);
381 if (rcode != RLM_MODULE_UPDATED) return rcode;
386 if (lrad_crypt_check((char *) request->password->strvalue,
387 (char *) vp->strvalue) != 0) {
388 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
389 request->username->strvalue);
390 return RLM_MODULE_REJECT;
394 return RLM_MODULE_OK;
399 * UUencode 4 bits base64. We use this to turn a 4 byte field
400 * (an IP address) into 6 bytes of ASCII. This is used for the
401 * wtmp file if we didn't find a short name in the naslist file.
403 static char *uue(void *in)
406 static unsigned char res[7];
407 unsigned char *data = (unsigned char *)in;
409 res[0] = ENC( data[0] >> 2 );
410 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
411 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
412 res[3] = ENC( data[2] & 077 );
414 res[4] = ENC( data[3] >> 2 );
415 res[5] = ENC( (data[3] << 4) & 060 );
418 for(i = 0; i < 6; i++) {
419 if (res[i] == ' ') res[i] = '`';
420 if (res[i] < 32 || res[i] > 127)
421 printf("uue: protocol error ?!\n");
428 * Unix accounting - write a wtmp file.
430 static int unix_accounting(void *instance, REQUEST *request)
442 int framed_address = 0;
446 int nas_port_type = 0;
447 struct unix_instance *inst = (struct unix_instance *) instance;
450 * No radwtmp. Don't do anything.
452 if (!inst->radwtmp) {
453 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
454 return RLM_MODULE_NOOP;
458 * Which type is this.
460 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
461 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
462 return RLM_MODULE_NOOP;
467 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
469 if (status != PW_STATUS_START &&
470 status != PW_STATUS_STOP)
471 return RLM_MODULE_NOOP;
474 * We're only interested in accounting messages
475 * with a username in it.
477 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
478 return RLM_MODULE_NOOP;
480 t = request->timestamp;
481 memset(&ut, 0, sizeof(ut));
484 * First, find the interesting attributes.
486 for (vp = request->packet->vps; vp; vp = vp->next) {
487 switch (vp->attribute) {
489 if (vp->length >= sizeof(ut.ut_name)) {
490 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
492 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
495 case PW_LOGIN_IP_HOST:
496 case PW_FRAMED_IP_ADDRESS:
497 framed_address = vp->lvalue;
499 case PW_FRAMED_PROTOCOL:
500 protocol = vp->lvalue;
502 case PW_NAS_IP_ADDRESS:
503 nas_address = vp->lvalue;
506 nas_port = vp->lvalue;
509 case PW_ACCT_DELAY_TIME:
512 case PW_NAS_PORT_TYPE:
513 nas_port_type = vp->lvalue;
519 * We don't store !root sessions, or sessions
520 * where we didn't see a NAS-Port attribute.
522 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
523 return RLM_MODULE_NOOP;
526 * If we didn't find out the NAS address, use the
527 * originator's IP address.
529 if (nas_address == 0)
530 nas_address = request->packet->src_ipaddr;
534 * Linux has a field for the client address.
536 ut.ut_addr = framed_address;
539 * We use the tty field to store the terminal servers' port
540 * and address so that the tty field is unique.
543 if ((cl = client_find(nas_address)) != NULL)
545 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
546 sprintf(buf, "%03d:%s", nas_port, s);
547 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
550 * We store the dynamic IP address in the hostname field.
553 if (framed_address) {
554 ip_ntoa(buf, framed_address);
555 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
559 ut.ut_xtime = t- delay;
561 ut.ut_time = t - delay;
565 * And we can use the ID field to store
568 if (protocol == PW_PPP)
569 strcpy(ut.ut_id, "P");
570 else if (protocol == PW_SLIP)
571 strcpy(ut.ut_id, "S");
573 strcpy(ut.ut_id, "T");
574 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
576 if (status == PW_STATUS_STOP)
580 * Write a RADIUS wtmp log file.
582 * Try to open the file if we can't, we don't write the
583 * wtmp file. If we can try to write. If we fail,
584 * return RLM_MODULE_FAIL ..
586 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
587 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
589 return RLM_MODULE_FAIL;
593 return RLM_MODULE_FAIL;
595 return RLM_MODULE_OK;
598 /* globally exported name */
599 module_t rlm_unix = {
601 RLM_TYPE_THREAD_UNSAFE, /* type */
602 unix_init, /* initialization */
603 unix_instantiate, /* instantiation */
605 unix_authenticate, /* authentication */
606 unix_authorize, /* authorization */
607 NULL, /* preaccounting */
608 unix_accounting, /* accounting */
609 NULL, /* checksimul */
610 NULL, /* pre-proxy */
611 NULL, /* post-proxy */
614 unix_detach, /* detach */
615 unix_destroy, /* destroy */