1 /*@-skipposixheaders@*/
3 * radwho.c Show who is logged in on the terminal servers.
4 * Can also be installed as fingerd on the UNIX
5 * machine RADIUS runs on.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Copyright 2000,2006 The FreeRADIUS server project
24 * Copyright 2000 Alan DeKok <aland@ox.org>
27 #include <freeradius-devel/ident.h>
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/sysutmp.h>
32 #include <freeradius-devel/radutmp.h>
43 * FIXME: put in header file.
45 #define SYS_FINGER "/usr/bin/finger"
46 #define FINGER_DIR "/usr/local/lib/finger"
49 * Header above output and format.
51 static const char *hdr1 =
52 "Login Name What TTY When From Location";
53 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-15.15s %-.19s%s";
54 static const char *rfmt1r = "%s,%s,%s,%s%u,%s,%s,%s%s";
56 static const char *hdr2 =
57 "Login Port What When From Location";
58 static const char *rfmt2 = "%-10.10s %s%-5u %-6.6s %-13.13s %-15.15s %-.28s%s";
59 static const char *rfmt2r = "%s,%s%u,%s,%s,%s,%s%s";
61 static const char *eol = "\n";
62 static int showname = -1;
63 static int showptype = 0;
64 static int showcid = 0;
66 const char *progname = "radwho";
67 const char *radlog_dir = NULL;
68 const char *radutmp_file = NULL;
69 int check_config = FALSE;
71 char *radius_dir = NULL;
72 const char *radacct_dir = NULL;
73 const char *radlib_dir = NULL;
74 uint32_t myip = INADDR_ANY;
75 int log_stripped_names;
78 * Global, for log.c to use.
80 struct main_config_t mainconfig;
81 char *request_log_file = NULL;
82 char *debug_log_file = NULL;
83 int radius_xlat(char *out, UNUSED int outlen, UNUSED const char *fmt,
84 UNUSED REQUEST *request, UNUSED RADIUS_ESCAPE_STRING func)
90 struct radutmp_config_t {
94 static const CONF_PARSER module_config[] = {
95 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
96 { NULL, -1, 0, NULL, NULL }
102 static FILE *safe_popen(const char *cmd, const char *mode)
108 * Change all suspect characters into a space.
110 strlcpy(buf, cmd, sizeof(buf));
111 buf[sizeof(buf) - 1] = 0;
112 for (p = buf; *p; p++) {
113 if (isalnum((int) *p))
115 if (strchr("@%-_ \t+:,./", *p) == NULL)
119 return popen(buf, mode);
123 * Print a file from FINGER_DIR. If the file is executable,
124 * execute it instead. Return 0 if successful.
126 static int ffile(const char *arg)
133 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
134 if (access(fn, X_OK) == 0) {
136 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
137 fp = safe_popen(fn, "r");
138 } else fp = fopen(fn, "r");
143 while(fgets(fn, 1024, fp)) {
144 if ((s = strchr(fn, '\n')) != NULL)
146 fprintf(stdout, "%s\r\n", fn);
158 * Execute the system finger and translate LF to CRLF.
160 static void sys_finger(const char *l)
169 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
170 if ((fp = safe_popen(fn, "r")) == NULL) {
171 printf("popen: %s\r\n", strerror(errno));
175 while(fgets(fn, 1024, fp)) {
176 if ((p = strchr(fn, '\n')) != NULL)
178 fprintf(stdout, "%s\r\n", fn);
186 * Get fullname of a user.
188 static char *fullname(char *username)
194 if ((pwd = getpwnam(username)) != NULL) {
195 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
196 return pwd->pw_gecos;
204 * Return protocol type.
206 static const char *proto(int id, int porttype)
211 if (!strchr("ASITX", porttype))
214 snprintf(buf, sizeof(buf), "SLP %c", porttype);
216 snprintf(buf, sizeof(buf), "PPP %c", porttype);
218 snprintf(buf, sizeof(buf), "shl %c", porttype);
221 if (id == 'S') return "SLIP";
222 if (id == 'P') return "PPP";
227 * Return a time in the form day hh:mm
229 static char *dotime(time_t t)
234 strlcpy(s + 4, s + 11, 6);
237 strlcpy(s + 4, s + 8, 9);
246 * Print address of NAS.
248 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
251 * WTF is this code for?
253 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
256 return inet_ntop(AF_INET, &ipaddr, buf, buflen);
262 * Print usage message and exit.
264 static void NEVER_RETURNS usage(int status)
266 FILE *output = status?stderr:stdout;
268 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
269 fprintf(output, " -c: show caller ID, if available\n");
270 fprintf(output, " -d: set the raddb directory (default is %s)\n",
272 fprintf(output, " -f: give fingerd output\n");
273 fprintf(output, " -i: show session ID\n");
274 fprintf(output, " -n: no full name\n");
275 fprintf(output, " -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
276 fprintf(output, " -p: show port type\n");
277 fprintf(output, " -P <port>: Show entries matching the given nas port\n");
278 fprintf(output, " -r: Print output as raw comma-delimited data\n");
279 fprintf(output, " -R: Print output as RADIUS attributes and values\n");
280 fprintf(output, " Includes ALL information from the radutmp record.\n");
281 fprintf(output, " -s: show full name\n");
282 fprintf(output, " -S: hide shell users from radius\n");
283 fprintf(output, " -u <user>: Show entries matching the given user\n");
284 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
285 fprintf(output, " -Z: Include accounting stop information in radius output. Requires -R.\n");
291 * Main program, either pmwho or fingerd.
293 int main(int argc, char **argv)
295 CONF_SECTION *maincs, *cs;
301 char session_id[sizeof(rt.session_id)+1];
306 int radiusoutput = 0; /* Radius attributes */
312 const char *user = NULL;
315 uint32_t nas_port = ~0;
316 uint32_t nas_ip_address = INADDR_NONE;
319 radius_dir = RADIUS_DIR;
321 while((c = getopt(argc, argv, "d:fnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
339 if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
353 nas_port = atoi(optarg);
385 if (zap && !radiusoutput) zap = 0;
388 * zap EVERYONE, but only on this nas
390 if (zap && !user && (~nas_port == 0)) {
392 * We need to know which NAS to zap users in.
394 if (nas_ip_address == INADDR_NONE) usage(1);
396 printf("Acct-Status-Type = Accounting-Off\n");
397 printf("NAS-IP-Address = %s\n",
398 hostname(buffer, sizeof(buffer), nas_ip_address));
399 printf("Acct-Delay-Time = 0\n");
400 exit(0); /* don't bother printing anything else */
404 * Initialize mainconfig
406 memset(&mainconfig, 0, sizeof(mainconfig));
407 mainconfig.radlog_dest = RADLOG_STDOUT;
409 /* Read radiusd.conf */
410 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
411 maincs = cf_file_read(buffer);
413 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
417 /* Read the radutmp section of radiusd.conf */
418 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
420 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
425 cf_section_parse(cs, NULL, module_config);
427 /* Assign the correct path for the radutmp file */
428 radutmp_file = radutmpconfig.radutmp_fn;
431 * See if we are "fingerd".
433 if (strstr(argv[0], "fingerd")) {
436 if (showname < 0) showname = 0;
438 if (showname < 0) showname = 1;
442 * Read first line of the input.
444 fgets(inbuf, 128, stdin);
446 while(*p == ' ' || *p == '\t') p++;
447 if (*p == '/' && *(p + 1)) p += 2;
448 while(*p == ' ' || *p == '\t') p++;
449 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
454 * See if we fingered a specific user.
457 if (*p) sys_finger(p);
461 * Show the users logged in on the terminal server(s).
463 if ((fp = fopen(radutmp_file, "r")) == NULL) {
464 fprintf(stderr, "%s: Error reading %s: %s\n",
465 progname, radutmp_file, strerror(errno));
470 * Don't print the headers if raw or RADIUS
472 if (!rawoutput && !radiusoutput) {
473 fputs(showname ? hdr1 : hdr2, stdout);
478 * Read the file, printing out active entries.
480 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
481 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
484 * We don't show shell users if we are
485 * fingerd, as we have done that above.
487 if (hideshell && !strchr("PCS", rt.proto))
491 * Print out sessions only for the given user.
493 if (user) { /* only for a particular user */
494 if (((user_cmp == 0) &&
495 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
497 (strncmp(rt.login, user, strlen(user)) != 0))) {
503 * Print out only for the given NAS port.
505 if (~nas_port != 0) {
506 if (rt.nas_port != nas_port) continue;
510 * Print out only for the given NAS IP address
512 if (nas_ip_address != INADDR_NONE) {
513 if (rt.nas_address != nas_ip_address) continue;
516 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
517 session_id[sizeof(rt.session_id)] = 0;
519 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
521 portno = (showname ? 999 : 99999);
524 portno = rt.nas_port;
528 * Print output as RADIUS attributes
531 memcpy(nasname, rt.login, sizeof(rt.login));
532 nasname[sizeof(rt.login)] = '\0';
534 fr_print_string(nasname, 0, buffer,
536 printf("User-Name = \"%s\"\n", buffer);
538 fr_print_string(session_id, 0, buffer,
540 printf("Acct-Session-Id = \"%s\"\n", buffer);
542 if (zap) printf("Acct-Status-Type = Stop\n");
544 printf("NAS-IP-Address = %s\n",
545 hostname(buffer, sizeof(buffer),
547 printf("NAS-Port = %u\n", rt.nas_port);
551 printf("Service-Type = Framed-User\n");
552 printf("Framed-Protocol = SLIP\n");
555 printf("Service-Type = Framed-User\n");
556 printf("Framed-Protocol = PPP\n");
559 printf("Service-type = Login-User\n");
562 if (rt.framed_address != INADDR_NONE) {
563 printf("Framed-IP-Address = %s\n",
564 hostname(buffer, sizeof(buffer),
569 * Some sanity checks on the time
571 if ((rt.time <= now) &&
572 (now - rt.time) <= (86400 * 365)) {
573 printf("Acct-Session-Time = %ld\n",
577 if (rt.caller_id[0] != '\0') {
578 memcpy(nasname, rt.caller_id,
579 sizeof(rt.caller_id));
580 nasname[sizeof(rt.caller_id)] = '\0';
582 fr_print_string(nasname, 0, buffer,
584 printf("Calling-Station-Id = \"%s\"\n", buffer);
587 printf("\n"); /* separate entries with a blank line */
592 * Show the fill name, or not.
595 printf((rawoutput == 0? rfmt1: rfmt1r),
597 showcid ? rt.caller_id :
598 (showsid? session_id : fullname(rt.login)),
599 proto(rt.proto, rt.porttype),
602 hostname(nasname, sizeof(nasname), rt.nas_address),
603 hostname(othername, sizeof(othername), rt.framed_address), eol);
605 printf((rawoutput == 0? rfmt2: rfmt2r),
608 proto(rt.proto, rt.porttype),
610 hostname(nasname, sizeof(nasname), rt.nas_address),
611 hostname(othername, sizeof(othername), rt.framed_address),