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[] =
35 #include <sys/utsname.h>
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.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%-3d %-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%-5d %-6.6s %-13.13s %-10.10s %-.28s%s";
63 static const char *rfmt2r = "%s,%s%d,%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)
242 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
244 return ip_hostname(buf, buflen, ipaddr);
249 * Print usage message and exit.
251 static void usage(int status)
253 FILE *output = status?stderr:stdout;
255 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
256 fprintf(output, " -c: show caller ID, if available\n");
257 fprintf(output, " -d: set the raddb directory (default is %s)\n",
259 fprintf(output, " -f: give fingerd output\n");
260 fprintf(output, " -i: show session ID\n");
261 fprintf(output, " -n: no full name\n");
262 fprintf(output, " -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
263 fprintf(output, " -p: show port type\n");
264 fprintf(output, " -P <port>: Show entries matching the given nas port\n");
265 fprintf(output, " -r: Print output as raw comma-delimited data\n");
266 fprintf(output, " -R: Print output as RADIUS attributes and values\n");
267 fprintf(output, " Includes ALL information from the radutmp record.\n");
268 fprintf(output, " -s: show full name\n");
269 fprintf(output, " -S: hide shell users from radius\n");
270 fprintf(output, " -u <user>: Show entries matching the given user\n");
271 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
272 fprintf(output, " -Z: Include accounting stop information in radius output. Requires -R.\n");
278 * Main program, either pmwho or fingerd.
280 int main(int argc, char **argv)
282 CONF_SECTION *maincs, *cs;
288 char session_id[sizeof(rt.session_id)+1];
293 int radiusoutput = 0; /* Radius attributes */
299 const char *user = NULL;
302 uint32_t nas_port = ~0;
303 uint32_t nas_ip_address = INADDR_NONE;
306 radius_dir = RADIUS_DIR;
308 while((c = getopt(argc, argv, "d:flnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
326 nas_ip_address = ip_addr(optarg);
327 if (nas_ip_address == INADDR_NONE) {
341 nas_port = atoi(optarg);
373 if (zap && !radiusoutput) zap = 0;
376 * zap EVERYONE, but only on this nas
378 if (zap && !user && (~nas_port == 0)) {
380 * We need to know which NAS to zap users in.
382 if (nas_ip_address == INADDR_NONE) usage(1);
384 printf("Acct-Status-Type = Accounting-Off\n");
385 printf("NAS-IP-Address = %s\n",
386 ip_hostname(buffer, sizeof(buffer), nas_ip_address));
387 printf("Acct-Delay-Time = 0\n");
388 exit(0); /* don't bother printing anything else */
392 * Initialize mainconfig
394 memset(&mainconfig, 0, sizeof(mainconfig));
395 mainconfig.radlog_dest = RADLOG_STDOUT;
397 /* Read radiusd.conf */
398 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
399 maincs = conf_read(NULL, 0, buffer, NULL);
401 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
405 /* Read the radutmp section of radiusd.conf */
406 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
408 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
413 cf_section_parse(cs, NULL, module_config);
415 /* Assign the correct path for the radutmp file */
416 radutmp_file = radutmpconfig.radutmp_fn;
419 * See if we are "fingerd".
421 if (strstr(argv[0], "fingerd")) {
424 if (showname < 0) showname = 0;
426 if (showname < 0) showname = 1;
430 * Read first line of the input.
432 fgets(inbuf, 128, stdin);
434 while(*p == ' ' || *p == '\t') p++;
435 if (*p == '/' && *(p + 1)) p += 2;
436 while(*p == ' ' || *p == '\t') p++;
437 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
442 * See if we fingered a specific user.
445 if (*p) sys_finger(p);
449 * Show the users logged in on the terminal server(s).
451 if ((fp = fopen(radutmp_file, "r")) == NULL) {
452 fprintf(stderr, "%s: Error reading %s: %s\n",
453 progname, radutmp_file, strerror(errno));
458 * Don't print the headers if raw or RADIUS
460 if (!rawoutput && !radiusoutput) {
461 fputs(showname ? hdr1 : hdr2, stdout);
466 * Read the file, printing out active entries.
468 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
469 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
472 * We don't show shell users if we are
473 * fingerd, as we have done that above.
475 if (hideshell && !strchr("PCS", rt.proto))
479 * Print out sessions only for the given user.
481 if (user) { /* only for a particular user */
482 if (((user_cmp == 0) &&
483 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
485 (strncmp(rt.login, user, strlen(user)) != 0))) {
491 * Print out only for the given NAS port.
493 if (~nas_port != 0) {
494 if (rt.nas_port != nas_port) continue;
498 * Print out only for the given NAS IP address
500 if (nas_ip_address != INADDR_NONE) {
501 if (rt.nas_address != nas_ip_address) continue;
504 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
505 session_id[sizeof(rt.session_id)] = 0;
507 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
509 portno = (showname ? 999 : 99999);
512 portno = rt.nas_port;
516 * Print output as RADIUS attributes
519 memcpy(nasname, rt.login, sizeof(rt.login));
520 nasname[sizeof(rt.login)] = '\0';
522 librad_safeprint(nasname, -1, buffer,
524 printf("User-Name = \"%s\"\n", buffer);
526 librad_safeprint(session_id, -1, buffer,
528 printf("Acct-Session-Id = \"%s\"\n", buffer);
530 if (zap) printf("Acct-Status-Type = Stop\n");
532 printf("NAS-IP-Address = %s\n",
533 ip_hostname(buffer, sizeof(buffer),
535 printf("NAS-Port = %d\n", rt.nas_port);
539 printf("Service-Type = Framed-User\n");
540 printf("Framed-Protocol = SLIP\n");
543 printf("Service-Type = Framed-User\n");
544 printf("Framed-Protocol = PPP\n");
547 printf("Service-type = Login-User\n");
550 if (rt.framed_address != INADDR_NONE) {
551 printf("Framed-IP-Address = %s\n",
552 ip_hostname(buffer, sizeof(buffer),
557 * Some sanity checks on the time
559 if ((rt.time <= now) &&
560 (now - rt.time) <= (86400 * 365)) {
561 printf("Acct-Session-Time = %ld\n",
565 if (rt.caller_id[0] != '\0') {
566 memcpy(nasname, rt.caller_id,
567 sizeof(rt.caller_id));
568 nasname[sizeof(rt.caller_id)] = '\0';
570 librad_safeprint(nasname, -1, buffer,
572 printf("Calling-Station-Id = \"%s\"\n", buffer);
575 printf("\n"); /* separate entries with a blank line */
580 * Show the fill name, or not.
583 printf((rawoutput == 0? rfmt1: rfmt1r),
585 showcid ? rt.caller_id :
586 (showsid? session_id : fullname(rt.login)),
587 proto(rt.proto, rt.porttype),
590 ip_hostname(nasname, sizeof(nasname), rt.nas_address),
591 hostname(othername, sizeof(othername), rt.framed_address), eol);
593 printf((rawoutput == 0? rfmt2: rfmt2r),
596 proto(rt.proto, rt.porttype),
598 ip_hostname(nasname, sizeof(nasname), rt.nas_address),
599 hostname(othername, sizeof(othername), rt.framed_address),