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%d,%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;
82 * Global, for log.c to use.
84 struct main_config_t mainconfig;
86 struct radutmp_config_t {
90 static CONF_PARSER module_config[] = {
91 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
92 { NULL, -1, 0, NULL, NULL }
98 static FILE *safe_popen(const char *cmd, const char *mode)
104 * Change all suspect characters into a space.
106 strncpy(buf, cmd, sizeof(buf));
107 buf[sizeof(buf) - 1] = 0;
108 for (p = buf; *p; p++) {
109 if (isalnum((int) *p))
111 if (strchr("@%-_ \t+:,./", *p) == NULL)
115 return popen(buf, mode);
119 * Print a file from FINGER_DIR. If the file is executable,
120 * execute it instead. Return 0 if succesfull.
122 static int ffile(const char *arg)
129 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
130 if (access(fn, X_OK) == 0) {
132 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
133 fp = safe_popen(fn, "r");
134 } else fp = fopen(fn, "r");
139 while(fgets(fn, 1024, fp)) {
140 if ((s = strchr(fn, '\n')) != NULL)
142 fprintf(stdout, "%s\r\n", fn);
154 * Execute the system finger and translate LF to CRLF.
156 static void sys_finger(const char *l)
165 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
166 if ((fp = safe_popen(fn, "r")) == NULL) {
167 printf("popen: %s\r\n", strerror(errno));
171 while(fgets(fn, 1024, fp)) {
172 if ((p = strchr(fn, '\n')) != NULL)
174 fprintf(stdout, "%s\r\n", fn);
182 * Get fullname of a user.
184 static char *fullname(char *username)
189 if ((pwd = getpwnam(username)) != NULL) {
190 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
191 return pwd->pw_gecos;
197 * Return protocol type.
199 static const char *proto(int id, int porttype)
204 if (!strchr("ASITX", porttype))
207 snprintf(buf, sizeof(buf), "SLP %c", porttype);
209 snprintf(buf, sizeof(buf), "PPP %c", porttype);
211 snprintf(buf, sizeof(buf), "shl %c", porttype);
214 if (id == 'S') return "SLIP";
215 if (id == 'P') return "PPP";
220 * Return a time in the form day hh:mm
222 static char *dotime(time_t t)
227 strncpy(s + 4, s + 11, 5);
230 strncpy(s + 4, s + 8, 8);
240 static const char *ttyshort(char *tty)
244 if (tty[0] == '/') tty += 5;
246 if (strncmp(tty, "tty", 3) == 0) {
247 if (tty[3] >= '0' && tty[3] <= '9')
248 snprintf(tmp, sizeof(tmp), "v%.14s", tty + 3);
250 snprintf(tmp, sizeof(tmp), "%.15s", tty + 3);
253 if (strncmp(tty, "vc", 2) == 0) {
254 snprintf(tmp, sizeof(tmp), "v.14%s", tty + 2);
257 if (strncmp(tty, "cu", 2) == 0) {
265 * Print address of NAS.
267 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
269 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
271 return ip_hostname(buf, buflen, ipaddr);
276 * Print usage message and exit.
278 static void usage(int status)
280 FILE *output = status?stderr:stdout;
282 fprintf(output, "Usage: radwho [-d raddb] [-lhfnsipcr]\n");
283 fprintf(output, " -c: show caller ID, if available\n");
284 fprintf(output, " -d: set the raddb directory (default is %s)\n",
286 fprintf(output, " -f: give fingerd output\n");
287 fprintf(output, " -i: show session ID\n");
288 fprintf(output, " -n: no full name\n");
289 fprintf(output, " -p: show port type\n");
290 fprintf(output, " -r: output as raw data\n");
291 fprintf(output, " -R: output as RADIUS attributes and values\n");
292 fprintf(output, " -s: show full name\n");
293 fprintf(output, " -S: hide shell users from radius\n");
294 fprintf(output, " -u <user>: print information only for that user\n");
295 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
301 * Main program, either pmwho or fingerd.
303 int main(int argc, char **argv)
305 CONF_SECTION *maincs, *cs;
312 char session_id[sizeof(rt.session_id)+1];
317 int radiusoutput = 0; /* Radius attributes */
322 const char *user = NULL;
326 radius_dir = RADIUS_DIR;
328 while((c = getopt(argc, argv, "d:flnsSipcrRu:U:")) != EOF) switch(c) {
379 * Initialize mainconfig
381 memset(&mainconfig, 0, sizeof(mainconfig));
382 mainconfig.radlog_dest = RADLOG_STDOUT;
384 /* Read radiusd.conf */
385 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
386 maincs = conf_read(NULL, 0, buffer, NULL);
388 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
392 /* Read the radutmp section of radiusd.conf */
393 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
395 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
400 cf_section_parse(cs, NULL, module_config);
402 /* Assign the correct path for the radutmp file */
403 radutmp_file = radutmpconfig.radutmp_fn;
406 * See if we are "fingerd".
408 if (strstr(argv[0], "fingerd")) {
411 if (showname < 0) showname = 0;
413 if (showname < 0) showname = 1;
417 * Read first line of the input.
419 fgets(inbuf, 128, stdin);
421 while(*p == ' ' || *p == '\t') p++;
422 if (*p == '/' && *(p + 1)) p += 2;
423 while(*p == ' ' || *p == '\t') p++;
424 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
429 * See if we fingered a specific user.
432 if (*p) sys_finger(p);
436 * Show the users logged in on the terminal server(s).
438 if ((fp = fopen(radutmp_file, "r")) == NULL) {
439 fprintf(stderr, "%s: Error reading %s: %s\n",
440 progname, radutmp_file, strerror(errno));
445 * Don't print the headers if raw or RADIUS
447 if (!rawoutput && !radiusoutput) {
448 fputs(showname ? hdr1 : hdr2, stdout);
453 * Read the file, printing out active entries.
455 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
458 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
461 * We don't show shell users if we are
462 * fingerd, as we have done that above.
464 if (hideshell && !strchr("PCS", rt.proto))
467 if (user) { /* only for a particular user */
468 if (((user_cmp == 0) &&
469 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
471 (strncmp(rt.login, user, strlen(user)) != 0))) {
477 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
478 session_id[sizeof(rt.session_id)] = 0;
480 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
482 portno = (showname ? 999 : 99999);
485 portno = rt.nas_port;
488 #define CPY(foo) memcpy(buffer, foo, sizeof(foo));buffer[sizeof(foo)] = 0
491 printf("User-Name = \"%s\"\n", buffer);
492 printf("Acct-Session-Id = \"%s\"\n", session_id);
493 printf("NAS-IP-Address = %s\n",
494 ip_hostname(buffer, sizeof(buffer),
496 printf("NAS-Port = %d\n", rt.nas_port);
500 printf("Service-Type = SLIP\n");
503 printf("Service-Type = PPP\n");
506 printf("Service-type = Login-User\n");
509 if (rt.framed_address != INADDR_NONE) {
510 printf("Framed-IP-Address = %s\n",
511 ip_hostname(buffer, sizeof(buffer),
516 * Some sanity checks on the time
518 if ((rt.time <= now) &&
519 (now - rt.time) <= (86400 * 30)) {
520 printf("Acct-Session-Time = %ld\n",
524 if (did_user) break; /* only print out first one */
526 printf("\n"); /* separate entries with a blank line */
532 * Show the fill name, or not.
535 printf((rawoutput == 0? rfmt1: rfmt1r),
537 showcid ? rt.caller_id :
538 (showsid? session_id : fullname(rt.login)),
539 proto(rt.proto, rt.porttype),
542 ip_hostname(nasname, sizeof(nasname), rt.nas_address),
543 hostname(othername, sizeof(othername), rt.framed_address), eol);
545 printf((rawoutput == 0? rfmt2: rfmt2r),
548 proto(rt.proto, rt.porttype),
550 ip_hostname(nasname, sizeof(nasname), rt.nas_address),
551 hostname(othername, sizeof(othername), rt.framed_address),