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)
352 VALUE_PAIR *vp = NULL;
354 if (!request->password ||
355 (request->password->attribute != PW_USER_PASSWORD)) {
356 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
357 return RLM_MODULE_INVALID;
360 rcode = unix_getpw(instance, request, &vp);
361 if (rcode != RLM_MODULE_UPDATED) return rcode;
366 if (lrad_crypt_check((char *) request->password->strvalue,
367 (char *) vp->strvalue) != 0) {
368 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
369 request->username->strvalue);
370 return RLM_MODULE_REJECT;
373 return RLM_MODULE_OK;
378 * UUencode 4 bits base64. We use this to turn a 4 byte field
379 * (an IP address) into 6 bytes of ASCII. This is used for the
380 * wtmp file if we didn't find a short name in the naslist file.
382 static char *uue(void *in)
385 static unsigned char res[7];
386 unsigned char *data = (unsigned char *)in;
388 res[0] = ENC( data[0] >> 2 );
389 res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
390 res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
391 res[3] = ENC( data[2] & 077 );
393 res[4] = ENC( data[3] >> 2 );
394 res[5] = ENC( (data[3] << 4) & 060 );
397 for(i = 0; i < 6; i++) {
398 if (res[i] == ' ') res[i] = '`';
399 if (res[i] < 32 || res[i] > 127)
400 printf("uue: protocol error ?!\n");
407 * Unix accounting - write a wtmp file.
409 static int unix_accounting(void *instance, REQUEST *request)
421 int framed_address = 0;
425 int nas_port_type = 0;
426 struct unix_instance *inst = (struct unix_instance *) instance;
429 * No radwtmp. Don't do anything.
431 if (!inst->radwtmp) {
432 DEBUG2("rlm_unix: No radwtmp file configured. Ignoring accounting request.");
433 return RLM_MODULE_NOOP;
437 * Which type is this.
439 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
440 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
441 return RLM_MODULE_NOOP;
446 * FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
448 if (status != PW_STATUS_START &&
449 status != PW_STATUS_STOP)
450 return RLM_MODULE_NOOP;
453 * We're only interested in accounting messages
454 * with a username in it.
456 if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
457 return RLM_MODULE_NOOP;
459 t = request->timestamp;
460 memset(&ut, 0, sizeof(ut));
463 * First, find the interesting attributes.
465 for (vp = request->packet->vps; vp; vp = vp->next) {
466 switch (vp->attribute) {
468 if (vp->length >= sizeof(ut.ut_name)) {
469 memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
471 strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
474 case PW_LOGIN_IP_HOST:
475 case PW_FRAMED_IP_ADDRESS:
476 framed_address = vp->lvalue;
478 case PW_FRAMED_PROTOCOL:
479 protocol = vp->lvalue;
481 case PW_NAS_IP_ADDRESS:
482 nas_address = vp->lvalue;
485 nas_port = vp->lvalue;
488 case PW_ACCT_DELAY_TIME:
491 case PW_NAS_PORT_TYPE:
492 nas_port_type = vp->lvalue;
498 * We don't store !root sessions, or sessions
499 * where we didn't see a NAS-Port attribute.
501 if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
502 return RLM_MODULE_NOOP;
505 * If we didn't find out the NAS address, use the
506 * originator's IP address.
508 if (nas_address == 0)
509 nas_address = request->packet->src_ipaddr;
513 * Linux has a field for the client address.
515 ut.ut_addr = framed_address;
518 * We use the tty field to store the terminal servers' port
519 * and address so that the tty field is unique.
522 if ((cl = client_find(nas_address)) != NULL)
524 if (s == NULL || s[0] == 0) s = uue(&(nas_address));
525 sprintf(buf, "%03d:%s", nas_port, s);
526 strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
529 * We store the dynamic IP address in the hostname field.
532 if (framed_address) {
533 ip_ntoa(buf, framed_address);
534 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
538 ut.ut_xtime = t- delay;
540 ut.ut_time = t - delay;
544 * And we can use the ID field to store
547 if (protocol == PW_PPP)
548 strcpy(ut.ut_id, "P");
549 else if (protocol == PW_SLIP)
550 strcpy(ut.ut_id, "S");
552 strcpy(ut.ut_id, "T");
553 ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
555 if (status == PW_STATUS_STOP)
559 * Write a RADIUS wtmp log file.
561 * Try to open the file if we can't, we don't write the
562 * wtmp file. If we can try to write. If we fail,
563 * return RLM_MODULE_FAIL ..
565 if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
566 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
568 return RLM_MODULE_FAIL;
572 return RLM_MODULE_FAIL;
574 return RLM_MODULE_OK;
577 /* globally exported name */
578 module_t rlm_unix = {
580 RLM_TYPE_THREAD_UNSAFE, /* type */
581 unix_init, /* initialization */
582 unix_instantiate, /* instantiation */
584 unix_authenticate, /* authentication */
585 unix_authorize, /* authorization */
586 NULL, /* preaccounting */
587 unix_accounting, /* accounting */
588 NULL, /* checksimul */
589 NULL, /* pre-proxy */
590 NULL, /* post-proxy */
593 unix_detach, /* detach */
594 unix_destroy, /* destroy */