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 %-9.9s %-.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 %-10.10s %-.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;
70 const char *radius_dir = NULL;
71 const char *radacct_dir = NULL;
72 const char *radlib_dir = NULL;
73 uint32_t myip = INADDR_ANY;
74 int log_stripped_names;
77 * Global, for log.c to use.
79 struct main_config_t mainconfig;
81 struct radutmp_config_t {
85 static const CONF_PARSER module_config[] = {
86 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
87 { NULL, -1, 0, NULL, NULL }
93 static FILE *safe_popen(const char *cmd, const char *mode)
99 * Change all suspect characters into a space.
101 strlcpy(buf, cmd, sizeof(buf));
102 buf[sizeof(buf) - 1] = 0;
103 for (p = buf; *p; p++) {
104 if (isalnum((int) *p))
106 if (strchr("@%-_ \t+:,./", *p) == NULL)
110 return popen(buf, mode);
114 * Print a file from FINGER_DIR. If the file is executable,
115 * execute it instead. Return 0 if succesfull.
117 static int ffile(const char *arg)
124 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
125 if (access(fn, X_OK) == 0) {
127 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
128 fp = safe_popen(fn, "r");
129 } else fp = fopen(fn, "r");
134 while(fgets(fn, 1024, fp)) {
135 if ((s = strchr(fn, '\n')) != NULL)
137 fprintf(stdout, "%s\r\n", fn);
149 * Execute the system finger and translate LF to CRLF.
151 static void sys_finger(const char *l)
160 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
161 if ((fp = safe_popen(fn, "r")) == NULL) {
162 printf("popen: %s\r\n", strerror(errno));
166 while(fgets(fn, 1024, fp)) {
167 if ((p = strchr(fn, '\n')) != NULL)
169 fprintf(stdout, "%s\r\n", fn);
177 * Get fullname of a user.
179 static char *fullname(char *username)
185 if ((pwd = getpwnam(username)) != NULL) {
186 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
187 return pwd->pw_gecos;
195 * Return protocol type.
197 static const char *proto(int id, int porttype)
202 if (!strchr("ASITX", porttype))
205 snprintf(buf, sizeof(buf), "SLP %c", porttype);
207 snprintf(buf, sizeof(buf), "PPP %c", porttype);
209 snprintf(buf, sizeof(buf), "shl %c", porttype);
212 if (id == 'S') return "SLIP";
213 if (id == 'P') return "PPP";
218 * Return a time in the form day hh:mm
220 static char *dotime(time_t t)
225 strlcpy(s + 4, s + 11, 5);
228 strlcpy(s + 4, s + 8, 8);
237 * Print address of NAS.
239 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
242 * WTF is this code for?
244 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
247 return inet_ntop(AF_INET, &ipaddr, buf, buflen);
253 * Print usage message and exit.
255 static void NEVER_RETURNS usage(int status)
257 FILE *output = status?stderr:stdout;
259 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
260 fprintf(output, " -c: show caller ID, if available\n");
261 fprintf(output, " -d: set the raddb directory (default is %s)\n",
263 fprintf(output, " -f: give fingerd output\n");
264 fprintf(output, " -i: show session ID\n");
265 fprintf(output, " -n: no full name\n");
266 fprintf(output, " -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
267 fprintf(output, " -p: show port type\n");
268 fprintf(output, " -P <port>: Show entries matching the given nas port\n");
269 fprintf(output, " -r: Print output as raw comma-delimited data\n");
270 fprintf(output, " -R: Print output as RADIUS attributes and values\n");
271 fprintf(output, " Includes ALL information from the radutmp record.\n");
272 fprintf(output, " -s: show full name\n");
273 fprintf(output, " -S: hide shell users from radius\n");
274 fprintf(output, " -u <user>: Show entries matching the given user\n");
275 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
276 fprintf(output, " -Z: Include accounting stop information in radius output. Requires -R.\n");
282 * Main program, either pmwho or fingerd.
284 int main(int argc, char **argv)
286 CONF_SECTION *maincs, *cs;
292 char session_id[sizeof(rt.session_id)+1];
297 int radiusoutput = 0; /* Radius attributes */
303 const char *user = NULL;
306 uint32_t nas_port = ~0;
307 uint32_t nas_ip_address = INADDR_NONE;
310 radius_dir = RADIUS_DIR;
312 while((c = getopt(argc, argv, "d:flnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
330 if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
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 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));
398 mainconfig.radlog_dest = RADLOG_STDOUT;
400 /* Read radiusd.conf */
401 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
402 maincs = cf_file_read(buffer);
404 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
408 /* Read the radutmp section of radiusd.conf */
409 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
411 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
416 cf_section_parse(cs, NULL, module_config);
418 /* Assign the correct path for the radutmp file */
419 radutmp_file = radutmpconfig.radutmp_fn;
422 * See if we are "fingerd".
424 if (strstr(argv[0], "fingerd")) {
427 if (showname < 0) showname = 0;
429 if (showname < 0) showname = 1;
433 * Read first line of the input.
435 fgets(inbuf, 128, stdin);
437 while(*p == ' ' || *p == '\t') p++;
438 if (*p == '/' && *(p + 1)) p += 2;
439 while(*p == ' ' || *p == '\t') p++;
440 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
445 * See if we fingered a specific user.
448 if (*p) sys_finger(p);
452 * Show the users logged in on the terminal server(s).
454 if ((fp = fopen(radutmp_file, "r")) == NULL) {
455 fprintf(stderr, "%s: Error reading %s: %s\n",
456 progname, radutmp_file, strerror(errno));
461 * Don't print the headers if raw or RADIUS
463 if (!rawoutput && !radiusoutput) {
464 fputs(showname ? hdr1 : hdr2, stdout);
469 * Read the file, printing out active entries.
471 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
472 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
475 * We don't show shell users if we are
476 * fingerd, as we have done that above.
478 if (hideshell && !strchr("PCS", rt.proto))
482 * Print out sessions only for the given user.
484 if (user) { /* only for a particular user */
485 if (((user_cmp == 0) &&
486 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
488 (strncmp(rt.login, user, strlen(user)) != 0))) {
494 * Print out only for the given NAS port.
496 if (~nas_port != 0) {
497 if (rt.nas_port != nas_port) continue;
501 * Print out only for the given NAS IP address
503 if (nas_ip_address != INADDR_NONE) {
504 if (rt.nas_address != nas_ip_address) continue;
507 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
508 session_id[sizeof(rt.session_id)] = 0;
510 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
512 portno = (showname ? 999 : 99999);
515 portno = rt.nas_port;
519 * Print output as RADIUS attributes
522 memcpy(nasname, rt.login, sizeof(rt.login));
523 nasname[sizeof(rt.login)] = '\0';
525 librad_safeprint(nasname, -1, buffer,
527 printf("User-Name = \"%s\"\n", buffer);
529 librad_safeprint(session_id, -1, buffer,
531 printf("Acct-Session-Id = \"%s\"\n", buffer);
533 if (zap) printf("Acct-Status-Type = Stop\n");
535 printf("NAS-IP-Address = %s\n",
536 hostname(buffer, sizeof(buffer),
538 printf("NAS-Port = %u\n", rt.nas_port);
542 printf("Service-Type = Framed-User\n");
543 printf("Framed-Protocol = SLIP\n");
546 printf("Service-Type = Framed-User\n");
547 printf("Framed-Protocol = PPP\n");
550 printf("Service-type = Login-User\n");
553 if (rt.framed_address != INADDR_NONE) {
554 printf("Framed-IP-Address = %s\n",
555 hostname(buffer, sizeof(buffer),
560 * Some sanity checks on the time
562 if ((rt.time <= now) &&
563 (now - rt.time) <= (86400 * 365)) {
564 printf("Acct-Session-Time = %ld\n",
568 if (rt.caller_id[0] != '\0') {
569 memcpy(nasname, rt.caller_id,
570 sizeof(rt.caller_id));
571 nasname[sizeof(rt.caller_id)] = '\0';
573 librad_safeprint(nasname, -1, buffer,
575 printf("Calling-Station-Id = \"%s\"\n", buffer);
578 printf("\n"); /* separate entries with a blank line */
583 * Show the fill name, or not.
586 printf((rawoutput == 0? rfmt1: rfmt1r),
588 showcid ? rt.caller_id :
589 (showsid? session_id : fullname(rt.login)),
590 proto(rt.proto, rt.porttype),
593 hostname(nasname, sizeof(nasname), rt.nas_address),
594 hostname(othername, sizeof(othername), rt.framed_address), eol);
596 printf((rawoutput == 0? rfmt2: rfmt2r),
599 proto(rt.proto, rt.porttype),
601 hostname(nasname, sizeof(nasname), rt.nas_address),
602 hostname(othername, sizeof(othername), rt.framed_address),