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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Copyright 2000 The FreeRADIUS server project
23 * Copyright 2000 Alan DeKok <aland@ox.org>
26 static const char rcsid[] =
29 #include <freeradius-devel/autoconf.h>
35 #include <sys/utsname.h>
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
41 #include <freeradius-devel/sysutmp.h>
42 #include <freeradius-devel/radutmp.h>
43 #include <freeradius-devel/radiusd.h>
44 #include <freeradius-devel/conffile.h>
47 * FIXME: put in header file.
49 #define SYS_FINGER "/usr/bin/finger"
50 #define FINGER_DIR "/usr/local/lib/finger"
53 * Header above output and format.
55 static const char *hdr1 =
56 "Login Name What TTY When From Location";
57 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-9.9s %-.19s%s";
58 static const char *rfmt1r = "%s,%s,%s,%s%u,%s,%s,%s%s";
60 static const char *hdr2 =
61 "Login Port What When From Location";
62 static const char *rfmt2 = "%-10.10s %s%-5u %-6.6s %-13.13s %-10.10s %-.28s%s";
63 static const char *rfmt2r = "%s,%s%u,%s,%s,%s,%s%s";
65 static const char *eol = "\n";
66 static int showname = -1;
67 static int showptype = 0;
68 static int showcid = 0;
70 const char *progname = "radwho";
71 const char *radlog_dir = NULL;
72 const char *radutmp_file = NULL;
74 const char *radius_dir = NULL;
75 const char *radacct_dir = NULL;
76 const char *radlib_dir = NULL;
77 uint32_t myip = INADDR_ANY;
78 int log_stripped_names;
81 * Global, for log.c to use.
83 struct main_config_t mainconfig;
85 struct radutmp_config_t {
89 static const CONF_PARSER module_config[] = {
90 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
91 { NULL, -1, 0, NULL, NULL }
97 static FILE *safe_popen(const char *cmd, const char *mode)
103 * Change all suspect characters into a space.
105 strncpy(buf, cmd, sizeof(buf));
106 buf[sizeof(buf) - 1] = 0;
107 for (p = buf; *p; p++) {
108 if (isalnum((int) *p))
110 if (strchr("@%-_ \t+:,./", *p) == NULL)
114 return popen(buf, mode);
118 * Print a file from FINGER_DIR. If the file is executable,
119 * execute it instead. Return 0 if succesfull.
121 static int ffile(const char *arg)
128 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
129 if (access(fn, X_OK) == 0) {
131 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
132 fp = safe_popen(fn, "r");
133 } else fp = fopen(fn, "r");
138 while(fgets(fn, 1024, fp)) {
139 if ((s = strchr(fn, '\n')) != NULL)
141 fprintf(stdout, "%s\r\n", fn);
153 * Execute the system finger and translate LF to CRLF.
155 static void sys_finger(const char *l)
164 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
165 if ((fp = safe_popen(fn, "r")) == NULL) {
166 printf("popen: %s\r\n", strerror(errno));
170 while(fgets(fn, 1024, fp)) {
171 if ((p = strchr(fn, '\n')) != NULL)
173 fprintf(stdout, "%s\r\n", fn);
181 * Get fullname of a user.
183 static char *fullname(char *username)
188 if ((pwd = getpwnam(username)) != NULL) {
189 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
190 return pwd->pw_gecos;
196 * Return protocol type.
198 static const char *proto(int id, int porttype)
203 if (!strchr("ASITX", porttype))
206 snprintf(buf, sizeof(buf), "SLP %c", porttype);
208 snprintf(buf, sizeof(buf), "PPP %c", porttype);
210 snprintf(buf, sizeof(buf), "shl %c", porttype);
213 if (id == 'S') return "SLIP";
214 if (id == 'P') return "PPP";
219 * Return a time in the form day hh:mm
221 static char *dotime(time_t t)
226 strncpy(s + 4, s + 11, 5);
229 strncpy(s + 4, s + 8, 8);
238 * Print address of NAS.
240 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
243 * WTF is this code for?
245 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
248 return inet_ntop(AF_INET, &ipaddr, buf, buflen);
254 * Print usage message and exit.
256 static void NEVER_RETURNS usage(int status)
258 FILE *output = status?stderr:stdout;
260 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
261 fprintf(output, " -c: show caller ID, if available\n");
262 fprintf(output, " -d: set the raddb directory (default is %s)\n",
264 fprintf(output, " -f: give fingerd output\n");
265 fprintf(output, " -i: show session ID\n");
266 fprintf(output, " -n: no full name\n");
267 fprintf(output, " -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
268 fprintf(output, " -p: show port type\n");
269 fprintf(output, " -P <port>: Show entries matching the given nas port\n");
270 fprintf(output, " -r: Print output as raw comma-delimited data\n");
271 fprintf(output, " -R: Print output as RADIUS attributes and values\n");
272 fprintf(output, " Includes ALL information from the radutmp record.\n");
273 fprintf(output, " -s: show full name\n");
274 fprintf(output, " -S: hide shell users from radius\n");
275 fprintf(output, " -u <user>: Show entries matching the given user\n");
276 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
277 fprintf(output, " -Z: Include accounting stop information in radius output. Requires -R.\n");
283 * Main program, either pmwho or fingerd.
285 int main(int argc, char **argv)
287 CONF_SECTION *maincs, *cs;
293 char session_id[sizeof(rt.session_id)+1];
298 int radiusoutput = 0; /* Radius attributes */
304 const char *user = NULL;
307 uint32_t nas_port = ~0;
308 uint32_t nas_ip_address = INADDR_NONE;
311 radius_dir = RADIUS_DIR;
313 while((c = getopt(argc, argv, "d:flnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
331 if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
345 nas_port = atoi(optarg);
377 if (zap && !radiusoutput) zap = 0;
380 * zap EVERYONE, but only on this nas
382 if (zap && !user && (~nas_port == 0)) {
384 * We need to know which NAS to zap users in.
386 if (nas_ip_address == INADDR_NONE) usage(1);
388 printf("Acct-Status-Type = Accounting-Off\n");
389 printf("NAS-IP-Address = %s\n",
390 hostname(buffer, sizeof(buffer), nas_ip_address));
391 printf("Acct-Delay-Time = 0\n");
392 exit(0); /* don't bother printing anything else */
396 * Initialize mainconfig
398 memset(&mainconfig, 0, sizeof(mainconfig));
399 mainconfig.radlog_dest = RADLOG_STDOUT;
401 /* Read radiusd.conf */
402 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
403 maincs = conf_read(NULL, 0, buffer, NULL);
405 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
409 /* Read the radutmp section of radiusd.conf */
410 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
412 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
417 cf_section_parse(cs, NULL, module_config);
419 /* Assign the correct path for the radutmp file */
420 radutmp_file = radutmpconfig.radutmp_fn;
423 * See if we are "fingerd".
425 if (strstr(argv[0], "fingerd")) {
428 if (showname < 0) showname = 0;
430 if (showname < 0) showname = 1;
434 * Read first line of the input.
436 fgets(inbuf, 128, stdin);
438 while(*p == ' ' || *p == '\t') p++;
439 if (*p == '/' && *(p + 1)) p += 2;
440 while(*p == ' ' || *p == '\t') p++;
441 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
446 * See if we fingered a specific user.
449 if (*p) sys_finger(p);
453 * Show the users logged in on the terminal server(s).
455 if ((fp = fopen(radutmp_file, "r")) == NULL) {
456 fprintf(stderr, "%s: Error reading %s: %s\n",
457 progname, radutmp_file, strerror(errno));
462 * Don't print the headers if raw or RADIUS
464 if (!rawoutput && !radiusoutput) {
465 fputs(showname ? hdr1 : hdr2, stdout);
470 * Read the file, printing out active entries.
472 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
473 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
476 * We don't show shell users if we are
477 * fingerd, as we have done that above.
479 if (hideshell && !strchr("PCS", rt.proto))
483 * Print out sessions only for the given user.
485 if (user) { /* only for a particular user */
486 if (((user_cmp == 0) &&
487 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
489 (strncmp(rt.login, user, strlen(user)) != 0))) {
495 * Print out only for the given NAS port.
497 if (~nas_port != 0) {
498 if (rt.nas_port != nas_port) continue;
502 * Print out only for the given NAS IP address
504 if (nas_ip_address != INADDR_NONE) {
505 if (rt.nas_address != nas_ip_address) continue;
508 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
509 session_id[sizeof(rt.session_id)] = 0;
511 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
513 portno = (showname ? 999 : 99999);
516 portno = rt.nas_port;
520 * Print output as RADIUS attributes
523 memcpy(nasname, rt.login, sizeof(rt.login));
524 nasname[sizeof(rt.login)] = '\0';
526 librad_safeprint(nasname, -1, buffer,
528 printf("User-Name = \"%s\"\n", buffer);
530 librad_safeprint(session_id, -1, buffer,
532 printf("Acct-Session-Id = \"%s\"\n", buffer);
534 if (zap) printf("Acct-Status-Type = Stop\n");
536 printf("NAS-IP-Address = %s\n",
537 hostname(buffer, sizeof(buffer),
539 printf("NAS-Port = %u\n", rt.nas_port);
543 printf("Service-Type = Framed-User\n");
544 printf("Framed-Protocol = SLIP\n");
547 printf("Service-Type = Framed-User\n");
548 printf("Framed-Protocol = PPP\n");
551 printf("Service-type = Login-User\n");
554 if (rt.framed_address != INADDR_NONE) {
555 printf("Framed-IP-Address = %s\n",
556 hostname(buffer, sizeof(buffer),
561 * Some sanity checks on the time
563 if ((rt.time <= now) &&
564 (now - rt.time) <= (86400 * 365)) {
565 printf("Acct-Session-Time = %ld\n",
569 if (rt.caller_id[0] != '\0') {
570 memcpy(nasname, rt.caller_id,
571 sizeof(rt.caller_id));
572 nasname[sizeof(rt.caller_id)] = '\0';
574 librad_safeprint(nasname, -1, buffer,
576 printf("Calling-Station-Id = \"%s\"\n", buffer);
579 printf("\n"); /* separate entries with a blank line */
584 * Show the fill name, or not.
587 printf((rawoutput == 0? rfmt1: rfmt1r),
589 showcid ? rt.caller_id :
590 (showsid? session_id : fullname(rt.login)),
591 proto(rt.proto, rt.porttype),
594 hostname(nasname, sizeof(nasname), rt.nas_address),
595 hostname(othername, sizeof(othername), rt.framed_address), eol);
597 printf((rawoutput == 0? rfmt2: rfmt2r),
600 proto(rt.proto, rt.porttype),
602 hostname(nasname, sizeof(nasname), rt.nas_address),
603 hostname(othername, sizeof(othername), rt.framed_address),