6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
21 * FIXME add copyrights
26 #include <sys/types.h>
42 #define LOCK_LEN sizeof(struct radutmp)
44 static const char porttypes[] = "ASITX";
47 * Internal wrapper for locking, to minimize the number of ifdef's
50 * Lock the utmp file, prefer lockf() over flock()
52 static void radutmp_lock(int fd)
54 #if defined(F_LOCK) && !defined(BSD)
55 (void)lockf(fd, F_LOCK, LOCK_LEN);
57 (void)flock(fd, LOCK_EX);
62 * Internal wrapper for unlocking, to minimize the number of ifdef's
65 * Unlock the utmp file, prefer lockf() over flock()
67 static void radutmp_unlock(int fd)
69 #if defined(F_LOCK) && !defined(BSD)
70 (void)lockf(fd, F_ULOCK, LOCK_LEN);
72 (void)flock(fd, LOCK_UN);
77 * used for caching radutmp lookups in the accounting component. The
78 * session (checksimul) component doesn't use it, but probably should.
80 typedef struct nas_port {
84 struct nas_port *next;
87 struct radutmp_instance {
88 NAS_PORT *nas_port_list;
94 static CONF_PARSER module_config[] = {
95 { "filename", PW_TYPE_STRING_PTR,
96 offsetof(struct radutmp_instance,radutmp_fn), NULL, RADUTMP },
97 { "perm", PW_TYPE_INTEGER,
98 offsetof(struct radutmp_instance,permission), NULL, "0644" },
99 { "callerid", PW_TYPE_BOOLEAN,
100 offsetof(struct radutmp_instance,callerid_ok), NULL, "no" },
101 { NULL, -1, 0, NULL, NULL } /* end the list */
104 static int radutmp_instantiate(CONF_SECTION *conf, void **instance)
106 struct radutmp_instance *inst;
107 inst = rad_malloc(sizeof(*inst));
108 if (cf_section_parse(conf, inst, module_config)) {
112 inst->nas_port_list = NULL;
121 static int radutmp_detach(void *instance)
124 struct radutmp_instance *inst = instance;
126 for(p=inst->nas_port_list ; p ; p=next) {
130 free(inst->radutmp_fn);
136 * Zap all users on a NAS from the radutmp file.
138 static int radutmp_zap(struct radutmp_instance *inst, uint32_t nasaddr, time_t t)
143 if (t == 0) time(&t);
145 fd = open(inst->radutmp_fn, O_RDWR);
148 * Lock the utmp file, prefer lockf() over flock().
153 * Find the entry for this NAS / portno combination.
155 while (read(fd, &u, sizeof(u)) == sizeof(u)) {
156 if ((nasaddr != 0 && nasaddr != u.nas_address) ||
162 if (lseek(fd, -(off_t)sizeof(u), SEEK_CUR) < 0) {
163 radlog(L_ERR, "Accounting: radutmp_zap: "
164 "negative lseek!\n");
165 lseek(fd, (off_t)0, SEEK_SET);
169 write(fd, &u, sizeof(u));
173 radlog(L_ERR, "Accounting: %s: %m", inst->radutmp_fn);
180 * Lookup a NAS_PORT in the nas_port_list
182 static NAS_PORT *nas_port_find(NAS_PORT *nas_port_list, uint32_t nasaddr, int port)
186 for(cl = nas_port_list; cl; cl = cl->next)
187 if (nasaddr == cl->nasaddr &&
195 * Store logins in the RADIUS utmp file.
197 static int radutmp_accounting(void *instance, REQUEST *request)
199 struct radutmp ut, u;
203 uint32_t nas_address = 0;
204 uint32_t framed_address = 0;
208 int just_an_update = 0;
210 int nas_port_type = 0;
212 struct radutmp_instance *inst = instance;
215 * Which type is this.
217 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) == NULL) {
218 radlog(L_ERR, "Accounting: no Accounting-Status-Type record.");
219 return RLM_MODULE_NOOP;
222 if (status == PW_STATUS_ACCOUNTING_ON ||
223 status == PW_STATUS_ACCOUNTING_OFF) rb_record = 1;
226 (vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL) do {
231 * ComOS (up to and including 3.5.1b20) does not send
232 * standard PW_STATUS_ACCOUNTING_XXX messages.
234 * Check for: o no Acct-Session-Time, or time of 0
235 * o Acct-Session-Id of "00000000".
237 * We could also check for NAS-Port, that attribute
238 * should NOT be present (but we don't right now).
240 if ((vp = pairfind(request->packet->vps, PW_ACCT_SESSION_TIME))
241 == NULL || vp->lvalue == 0)
243 if ((vp = pairfind(request->packet->vps, PW_ACCT_SESSION_ID))
244 != NULL && vp->length == 8 &&
245 memcmp(vp->strvalue, "00000000", 8) == 0)
247 if (check1 == 0 || check2 == 0) {
248 #if 0 /* Cisco sometimes sends START records without username. */
249 radlog(L_ERR, "Accounting: no username in record");
250 return RLM_MODULE_FAIL;
255 radlog(L_INFO, "Accounting: converting reboot records.");
256 if (status == PW_STATUS_STOP)
257 status = PW_STATUS_ACCOUNTING_OFF;
258 if (status == PW_STATUS_START)
259 status = PW_STATUS_ACCOUNTING_ON;
264 memset(&ut, 0, sizeof(ut));
268 * First, find the interesting attributes.
270 for (vp = request->packet->vps; vp; vp = vp->next) {
271 switch (vp->attribute) {
273 strncpy(ut.login, vp->strvalue, RUT_NAMESIZE);
275 case PW_LOGIN_IP_HOST:
276 case PW_FRAMED_IP_ADDRESS:
277 framed_address = vp->lvalue;
278 ut.framed_address = vp->lvalue;
280 case PW_FRAMED_PROTOCOL:
281 protocol = vp->lvalue;
283 case PW_NAS_IP_ADDRESS:
284 nas_address = vp->lvalue;
285 ut.nas_address = vp->lvalue;
288 ut.nas_port = vp->lvalue;
291 case PW_ACCT_DELAY_TIME:
292 ut.delay = vp->lvalue;
294 case PW_ACCT_SESSION_ID:
296 * If length > 8, only store the
299 off = vp->length - sizeof(ut.session_id);
301 * Ascend is br0ken - it adds a \0
302 * to the end of any string.
305 if (vp->length > 0 &&
306 vp->strvalue[vp->length - 1] == 0)
308 if (off < 0) off = 0;
309 memcpy(ut.session_id, vp->strvalue + off,
310 sizeof(ut.session_id));
312 case PW_NAS_PORT_TYPE:
313 if (vp->lvalue >= 0 && vp->lvalue <= 4)
314 ut.porttype = porttypes[vp->lvalue];
315 nas_port_type = vp->lvalue;
317 case PW_CALLING_STATION_ID:
318 if(inst->callerid_ok)
319 strNcpy(ut.caller_id,
320 (char *)vp->strvalue,
321 sizeof(ut.caller_id));
327 * If we didn't find out the NAS address, use the
328 * originator's IP address.
330 if (nas_address == 0) {
331 nas_address = request->packet->src_ipaddr;
332 ut.nas_address = nas_address;
335 if (protocol == PW_PPP)
337 else if (protocol == PW_SLIP)
341 ut.time = t - ut.delay;
344 * See if this was a portmaster reboot.
346 if (status == PW_STATUS_ACCOUNTING_ON && nas_address) {
347 radlog(L_INFO, "NAS %s restarted (Accounting-On packet seen)",
348 nas_name(nas_address));
349 radutmp_zap(inst, nas_address, ut.time);
350 return RLM_MODULE_OK;
352 if (status == PW_STATUS_ACCOUNTING_OFF && nas_address) {
353 radlog(L_INFO, "NAS %s rebooted (Accounting-Off packet seen)",
354 nas_name(nas_address));
355 radutmp_zap(inst, nas_address, ut.time);
356 return RLM_MODULE_OK;
360 * If we don't know this type of entry pretend we succeeded.
362 if (status != PW_STATUS_START &&
363 status != PW_STATUS_STOP &&
364 status != PW_STATUS_ALIVE) {
365 radlog(L_ERR, "NAS %s port %d unknown packet type %d)",
366 nas_name(nas_address), ut.nas_port, status);
367 return RLM_MODULE_NOOP;
371 * Perhaps we don't want to store this record into
372 * radutmp. We skip records:
374 * - without a NAS-Port-Id (telnet / tcp access)
375 * - with the username "!root" (console admin login)
377 if (!port_seen || strncmp(ut.login, "!root", RUT_NAMESIZE) == 0)
378 return RLM_MODULE_NOOP;
381 * Enter into the radutmp file.
383 fd = open(inst->radutmp_fn, O_RDWR|O_CREAT, inst->permission);
389 * Lock the utmp file, prefer lockf() over flock().
394 * Find the entry for this NAS / portno combination.
396 if ((cache = nas_port_find(inst->nas_port_list, ut.nas_address,
397 ut.nas_port)) != NULL)
398 lseek(fd, (off_t)cache->offset, SEEK_SET);
402 while (read(fd, &u, sizeof(u)) == sizeof(u)) {
404 if (u.nas_address != ut.nas_address ||
405 u.nas_port != ut.nas_port)
408 if (status == PW_STATUS_STOP &&
409 strncmp(ut.session_id, u.session_id,
410 sizeof(u.session_id)) != 0) {
412 * Don't complain if this is not a
413 * login record (some clients can
414 * send _only_ logout records).
416 if (u.type == P_LOGIN)
418 "Accounting: logout: entry for NAS %s port %d has wrong ID",
419 nas_name(nas_address), u.nas_port);
424 if (status == PW_STATUS_START &&
425 strncmp(ut.session_id, u.session_id,
426 sizeof(u.session_id)) == 0 &&
428 if (u.type == P_LOGIN) {
430 "Accounting: login: entry for NAS %s port %d duplicate",
431 nas_name(nas_address), u.nas_port);
436 "Accounting: login: entry for NAS %s port %d wrong order",
437 nas_name(nas_address), u.nas_port);
443 * FIXME: the ALIVE record could need
444 * some more checking, but anyway I'd
445 * rather rewrite this mess -- miquels.
447 if (status == PW_STATUS_ALIVE &&
448 strncmp(ut.session_id, u.session_id,
449 sizeof(u.session_id)) == 0 &&
452 * Keep the original login time.
459 if (lseek(fd, -(off_t)sizeof(u), SEEK_CUR) < 0) {
460 radlog(L_ERR, "Accounting: negative lseek!\n");
461 lseek(fd, (off_t)0, SEEK_SET);
469 if (r >= 0 && (status == PW_STATUS_START ||
470 status == PW_STATUS_ALIVE)) {
472 cache = rad_malloc(sizeof(NAS_PORT));
473 cache->nasaddr = ut.nas_address;
474 cache->port = ut.nas_port;
476 cache->next = inst->nas_port_list;
477 inst->nas_port_list = cache;
480 write(fd, &ut, sizeof(u));
482 if (status == PW_STATUS_STOP) {
487 write(fd, &u, sizeof(u));
490 "Accounting: logout: login entry for NAS %s port %d not found",
491 nas_name(nas_address), ut.nas_port);
497 radlog(L_ERR, "Accounting: %s: %m", inst->radutmp_fn);
498 return RLM_MODULE_FAIL;
501 return RLM_MODULE_OK;
505 * See if a user is already logged in. Sets request->simul_count to the
506 * current session count for this user and sets request->simul_mpp to 2
507 * if it looks like a multilink attempt based on the requested IP
508 * address, otherwise leaves request->simul_mpp alone.
510 * Check twice. If on the first pass the user exceeds his
511 * max. number of logins, do a second pass and validate all
512 * logins by querying the terminal server (using eg. SNMP).
514 static int radutmp_checksimul(void *instance, REQUEST *request)
521 const char *name = (char *)request->username->strvalue;
522 struct radutmp_instance *inst = instance;
524 if ((fd = open(inst->radutmp_fn, O_RDWR)) < 0) {
526 return RLM_MODULE_FAIL;
527 request->simul_count=0;
528 return RLM_MODULE_OK;
531 request->simul_count = 0;
532 while(read(fd, &u, sizeof(u)) == sizeof(u)) {
533 if (strncmp(name, u.login, RUT_NAMESIZE) == 0
534 && u.type == P_LOGIN)
535 ++request->simul_count;
538 if(request->simul_count < request->simul_max) {
540 return RLM_MODULE_OK;
542 lseek(fd, (off_t)0, SEEK_SET);
545 * Setup some stuff, like for MPP detection.
547 if ((fra = pairfind(request->packet->vps, PW_FRAMED_IP_ADDRESS)) != NULL)
551 * lockf() the file while reading/writing.
555 request->simul_count = 0;
556 while (read(fd, &u, sizeof(u)) == sizeof(u)) {
557 if (strncmp(name, u.login, RUT_NAMESIZE) == 0
558 && u.type == P_LOGIN) {
559 char login[sizeof u.login+1];
560 char session_id[sizeof u.session_id+1];
561 strNcpy(login, u.login, sizeof login);
562 strNcpy(session_id, u.session_id, sizeof session_id);
565 * rad_check_ts may take seconds to return,
566 * and we don't want to block everyone else
567 * while that's happening.
570 rcode = rad_check_ts(u.nas_address, u.nas_port, login,
575 ++request->simul_count;
577 * Does it look like a MPP attempt?
579 if (strchr("SCPA", u.proto) &&
580 ipno && u.framed_address == ipno)
581 request->simul_mpp = 2;
585 * False record - zap it.
588 session_zap(u.nas_address, u.nas_port, login,
589 session_id, u.framed_address,
596 return RLM_MODULE_OK;
599 /* globally exported name */
600 module_t rlm_radutmp = {
602 0, /* type: reserved */
603 NULL, /* initialization */
604 radutmp_instantiate, /* instantiation */
605 NULL, /* authorization */
606 NULL, /* authentication */
607 NULL, /* preaccounting */
608 radutmp_accounting, /* accounting */
609 radutmp_checksimul, /* checksimul */
610 radutmp_detach, /* detach */