2 * radwho.c Show who is logged in on the terminal servers.
3 * Can also be installed as fingerd on the UNIX
4 * machine RADIUS runs on.
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 Alan DeKok <aland@ox.org>
26 static const char rcsid[] =
30 #include "libradius.h"
36 #include <sys/utsname.h>
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
48 * FIXME: put in header file.
50 #define SYS_FINGER "/usr/bin/finger"
51 #define FINGER_DIR "/usr/local/lib/finger"
54 * Header above output and format.
56 static const char *hdr1 =
57 "Login Name What TTY When From Location";
58 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3d %-9.9s %-9.9s %-.19s%s";
59 static const char *rfmt1r = "%s,%s,%s,%s%u,%s,%s,%s%s";
61 static const char *hdr2 =
62 "Login Port What When From Location";
63 static const char *rfmt2 = "%-10.10s %s%-5d %-6.6s %-13.13s %-10.10s %-.28s%s";
64 static const char *rfmt2r = "%s,%s%d,%s,%s,%s,%s%s";
66 static const char *eol = "\n";
67 static int showname = -1;
68 static int showptype = 0;
69 static int showcid = 0;
71 const char *progname = "radwho";
72 const char *radlog_dir = NULL;
73 const char *radutmp_file = NULL;
75 const char *radius_dir = NULL;
76 const char *radacct_dir = NULL;
77 const char *radlib_dir = NULL;
78 uint32_t myip = INADDR_ANY;
79 int log_stripped_names;
81 radlog_dest_t radlog_dest = RADLOG_STDOUT;
84 * Global, for log.c to use.
86 struct main_config_t mainconfig;
88 struct radutmp_config_t {
92 static const CONF_PARSER module_config[] = {
93 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
94 { NULL, -1, 0, NULL, NULL }
100 static FILE *safe_popen(const char *cmd, const char *mode)
106 * Change all suspect characters into a space.
108 strncpy(buf, cmd, sizeof(buf));
109 buf[sizeof(buf) - 1] = 0;
110 for (p = buf; *p; p++) {
111 if (isalnum((int) *p))
113 if (strchr("@%-_ \t+:,./", *p) == NULL)
117 return popen(buf, mode);
121 * Print a file from FINGER_DIR. If the file is executable,
122 * execute it instead. Return 0 if succesfull.
124 static int ffile(const char *arg)
131 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
132 if (access(fn, X_OK) == 0) {
134 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
135 fp = safe_popen(fn, "r");
136 } else fp = fopen(fn, "r");
141 while(fgets(fn, 1024, fp)) {
142 if ((s = strchr(fn, '\n')) != NULL)
144 fprintf(stdout, "%s\r\n", fn);
156 * Execute the system finger and translate LF to CRLF.
158 static void sys_finger(const char *l)
167 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
168 if ((fp = safe_popen(fn, "r")) == NULL) {
169 printf("popen: %s\r\n", strerror(errno));
173 while(fgets(fn, 1024, fp)) {
174 if ((p = strchr(fn, '\n')) != NULL)
176 fprintf(stdout, "%s\r\n", fn);
184 * Get fullname of a user.
186 static char *fullname(char *username)
191 if ((pwd = getpwnam(username)) != NULL) {
192 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
193 return pwd->pw_gecos;
199 * Return protocol type.
201 static const char *proto(int id, int porttype)
206 if (!strchr("ASITX", porttype))
209 snprintf(buf, sizeof(buf), "SLP %c", porttype);
211 snprintf(buf, sizeof(buf), "PPP %c", porttype);
213 snprintf(buf, sizeof(buf), "shl %c", porttype);
216 if (id == 'S') return "SLIP";
217 if (id == 'P') return "PPP";
222 * Return a time in the form day hh:mm
224 static char *dotime(time_t t)
229 strncpy(s + 4, s + 11, 5);
232 strncpy(s + 4, s + 8, 8);
241 * Print address of NAS.
243 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
245 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
247 return ip_hostname(buf, buflen, ipaddr);
252 * Print usage message and exit.
254 static void NEVER_RETURNS usage(int status)
256 FILE *output = status?stderr:stdout;
258 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
259 fprintf(output, " -c: show caller ID, if available\n");
260 fprintf(output, " -d: set the raddb directory (default is %s)\n",
262 fprintf(output, " -f: give fingerd output\n");
263 fprintf(output, " -i: show session ID\n");
264 fprintf(output, " -n: no full name\n");
265 fprintf(output, " -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
266 fprintf(output, " -p: show port type\n");
267 fprintf(output, " -P <port>: Show entries matching the given nas port\n");
268 fprintf(output, " -r: Print output as raw comma-delimited data\n");
269 fprintf(output, " -R: Print output as RADIUS attributes and values\n");
270 fprintf(output, " Includes ALL information from the radutmp record.\n");
271 fprintf(output, " -s: show full name\n");
272 fprintf(output, " -S: hide shell users from radius\n");
273 fprintf(output, " -u <user>: Show entries matching the given user\n");
274 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
275 fprintf(output, " -Z: Include accounting stop information in radius output. Requires -R.\n");
281 * Main program, either pmwho or fingerd.
283 int main(int argc, char **argv)
285 CONF_SECTION *maincs, *cs;
291 char session_id[sizeof(rt.session_id)+1];
296 int radiusoutput = 0; /* Radius attributes */
302 const char *user = NULL;
305 uint32_t nas_port = ~0;
306 uint32_t nas_ip_address = INADDR_NONE;
309 radius_dir = RADIUS_DIR;
311 while((c = getopt(argc, argv, "d:flnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
329 nas_ip_address = ip_addr(optarg);
330 if (nas_ip_address == INADDR_NONE) {
344 nas_port = atoi(optarg);
376 if (zap && !radiusoutput) zap = 0;
379 * zap EVERYONE, but only on this nas
381 if (zap && !user && (~nas_port == 0)) {
383 * We need to know which NAS to zap users in.
385 if (nas_ip_address == INADDR_NONE) usage(1);
387 printf("Acct-Status-Type = Accounting-Off\n");
388 printf("NAS-IP-Address = %s\n",
389 ip_hostname(buffer, sizeof(buffer), nas_ip_address));
390 printf("Acct-Delay-Time = 0\n");
391 exit(0); /* don't bother printing anything else */
395 * Initialize mainconfig
397 memset(&mainconfig, 0, sizeof(mainconfig));
399 /* Read radiusd.conf */
400 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
401 maincs = conf_read(NULL, 0, buffer, NULL);
403 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
407 /* Read the radutmp section of radiusd.conf */
408 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
410 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
415 cf_section_parse(cs, NULL, module_config);
417 /* Assign the correct path for the radutmp file */
418 radutmp_file = radutmpconfig.radutmp_fn;
421 * See if we are "fingerd".
423 if (strstr(argv[0], "fingerd")) {
426 if (showname < 0) showname = 0;
428 if (showname < 0) showname = 1;
432 * Read first line of the input.
434 fgets(inbuf, 128, stdin);
436 while(*p == ' ' || *p == '\t') p++;
437 if (*p == '/' && *(p + 1)) p += 2;
438 while(*p == ' ' || *p == '\t') p++;
439 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
444 * See if we fingered a specific user.
447 if (*p) sys_finger(p);
451 * Show the users logged in on the terminal server(s).
453 if ((fp = fopen(radutmp_file, "r")) == NULL) {
454 fprintf(stderr, "%s: Error reading %s: %s\n",
455 progname, radutmp_file, strerror(errno));
460 * Don't print the headers if raw or RADIUS
462 if (!rawoutput && !radiusoutput) {
463 fputs(showname ? hdr1 : hdr2, stdout);
468 * Read the file, printing out active entries.
470 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
471 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
474 * We don't show shell users if we are
475 * fingerd, as we have done that above.
477 if (hideshell && !strchr("PCS", rt.proto))
481 * Print out sessions only for the given user.
483 if (user) { /* only for a particular user */
484 if (((user_cmp == 0) &&
485 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
487 (strncmp(rt.login, user, strlen(user)) != 0))) {
493 * Print out only for the given NAS port.
495 if (~nas_port != 0) {
496 if (rt.nas_port != nas_port) continue;
500 * Print out only for the given NAS IP address
502 if (nas_ip_address != INADDR_NONE) {
503 if (rt.nas_address != nas_ip_address) continue;
506 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
507 session_id[sizeof(rt.session_id)] = 0;
509 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
511 portno = (showname ? 999 : 99999);
514 portno = rt.nas_port;
518 * Print output as RADIUS attributes
521 memcpy(nasname, rt.login, sizeof(rt.login));
522 nasname[sizeof(rt.login)] = '\0';
524 librad_safeprint(nasname, -1, buffer,
526 printf("User-Name = \"%s\"\n", buffer);
528 librad_safeprint(session_id, -1, buffer,
530 printf("Acct-Session-Id = \"%s\"\n", buffer);
532 if (zap) printf("Acct-Status-Type = Stop\n");
534 printf("NAS-IP-Address = %s\n",
535 ip_hostname(buffer, sizeof(buffer),
537 printf("NAS-Port = %u\n", rt.nas_port);
541 printf("Service-Type = Framed-User\n");
542 printf("Framed-Protocol = SLIP\n");
545 printf("Service-Type = Framed-User\n");
546 printf("Framed-Protocol = PPP\n");
549 printf("Service-type = Login-User\n");
552 if (rt.framed_address != INADDR_NONE) {
553 printf("Framed-IP-Address = %s\n",
554 ip_hostname(buffer, sizeof(buffer),
559 * Some sanity checks on the time
561 if ((rt.time <= now) &&
562 (now - rt.time) <= (86400 * 365)) {
563 printf("Acct-Session-Time = %ld\n",
567 if (rt.caller_id[0] != '\0') {
568 memcpy(nasname, rt.caller_id,
569 sizeof(rt.caller_id));
570 nasname[sizeof(rt.caller_id)] = '\0';
572 librad_safeprint(nasname, -1, buffer,
574 printf("Calling-Station-Id = \"%s\"\n", buffer);
577 printf("\n"); /* separate entries with a blank line */
582 * Show the fill name, or not.
585 printf((rawoutput == 0? rfmt1: rfmt1r),
587 showcid ? rt.caller_id :
588 (showsid? session_id : fullname(rt.login)),
589 proto(rt.proto, rt.porttype),
592 ip_hostname(nasname, sizeof(nasname), rt.nas_address),
593 hostname(othername, sizeof(othername), rt.framed_address), eol);
595 printf((rawoutput == 0? rfmt2: rfmt2r),
598 proto(rt.proto, rt.porttype),
600 ip_hostname(nasname, sizeof(nasname), rt.nas_address),
601 hostname(othername, sizeof(othername), rt.framed_address),