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/autoconf.h>
36 #include <sys/utsname.h>
39 #include <freeradius-devel/sysutmp.h>
40 #include <freeradius-devel/radutmp.h>
41 #include <freeradius-devel/radiusd.h>
44 * FIXME: put in header file.
46 #define SYS_FINGER "/usr/bin/finger"
47 #define FINGER_DIR "/usr/local/lib/finger"
50 * Header above output and format.
52 static const char *hdr1 =
53 "Login Name What TTY When From Location";
54 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-9.9s %-.19s%s";
55 static const char *rfmt1r = "%s,%s,%s,%s%u,%s,%s,%s%s";
57 static const char *hdr2 =
58 "Login Port What When From Location";
59 static const char *rfmt2 = "%-10.10s %s%-5u %-6.6s %-13.13s %-10.10s %-.28s%s";
60 static const char *rfmt2r = "%s,%s%u,%s,%s,%s,%s%s";
62 static const char *eol = "\n";
63 static int showname = -1;
64 static int showptype = 0;
65 static int showcid = 0;
67 const char *progname = "radwho";
68 const char *radlog_dir = NULL;
69 const char *radutmp_file = NULL;
71 const 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;
82 struct radutmp_config_t {
86 static const CONF_PARSER module_config[] = {
87 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
88 { NULL, -1, 0, NULL, NULL }
94 static FILE *safe_popen(const char *cmd, const char *mode)
100 * Change all suspect characters into a space.
102 strlcpy(buf, cmd, sizeof(buf));
103 buf[sizeof(buf) - 1] = 0;
104 for (p = buf; *p; p++) {
105 if (isalnum((int) *p))
107 if (strchr("@%-_ \t+:,./", *p) == NULL)
111 return popen(buf, mode);
115 * Print a file from FINGER_DIR. If the file is executable,
116 * execute it instead. Return 0 if succesfull.
118 static int ffile(const char *arg)
125 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
126 if (access(fn, X_OK) == 0) {
128 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
129 fp = safe_popen(fn, "r");
130 } else fp = fopen(fn, "r");
135 while(fgets(fn, 1024, fp)) {
136 if ((s = strchr(fn, '\n')) != NULL)
138 fprintf(stdout, "%s\r\n", fn);
150 * Execute the system finger and translate LF to CRLF.
152 static void sys_finger(const char *l)
161 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
162 if ((fp = safe_popen(fn, "r")) == NULL) {
163 printf("popen: %s\r\n", strerror(errno));
167 while(fgets(fn, 1024, fp)) {
168 if ((p = strchr(fn, '\n')) != NULL)
170 fprintf(stdout, "%s\r\n", fn);
178 * Get fullname of a user.
180 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;
193 * Return protocol type.
195 static const char *proto(int id, int porttype)
200 if (!strchr("ASITX", porttype))
203 snprintf(buf, sizeof(buf), "SLP %c", porttype);
205 snprintf(buf, sizeof(buf), "PPP %c", porttype);
207 snprintf(buf, sizeof(buf), "shl %c", porttype);
210 if (id == 'S') return "SLIP";
211 if (id == 'P') return "PPP";
216 * Return a time in the form day hh:mm
218 static char *dotime(time_t t)
223 strlcpy(s + 4, s + 11, 5);
226 strlcpy(s + 4, s + 8, 8);
235 * Print address of NAS.
237 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
240 * WTF is this code for?
242 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
245 return inet_ntop(AF_INET, &ipaddr, buf, buflen);
251 * Print usage message and exit.
253 static void NEVER_RETURNS usage(int status)
255 FILE *output = status?stderr:stdout;
257 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
258 fprintf(output, " -c: show caller ID, if available\n");
259 fprintf(output, " -d: set the raddb directory (default is %s)\n",
261 fprintf(output, " -f: give fingerd output\n");
262 fprintf(output, " -i: show session ID\n");
263 fprintf(output, " -n: no full name\n");
264 fprintf(output, " -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
265 fprintf(output, " -p: show port type\n");
266 fprintf(output, " -P <port>: Show entries matching the given nas port\n");
267 fprintf(output, " -r: Print output as raw comma-delimited data\n");
268 fprintf(output, " -R: Print output as RADIUS attributes and values\n");
269 fprintf(output, " Includes ALL information from the radutmp record.\n");
270 fprintf(output, " -s: show full name\n");
271 fprintf(output, " -S: hide shell users from radius\n");
272 fprintf(output, " -u <user>: Show entries matching the given user\n");
273 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
274 fprintf(output, " -Z: Include accounting stop information in radius output. Requires -R.\n");
280 * Main program, either pmwho or fingerd.
282 int main(int argc, char **argv)
284 CONF_SECTION *maincs, *cs;
290 char session_id[sizeof(rt.session_id)+1];
295 int radiusoutput = 0; /* Radius attributes */
301 const char *user = NULL;
304 uint32_t nas_port = ~0;
305 uint32_t nas_ip_address = INADDR_NONE;
308 radius_dir = RADIUS_DIR;
310 while((c = getopt(argc, argv, "d:flnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
328 if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
342 nas_port = atoi(optarg);
374 if (zap && !radiusoutput) zap = 0;
377 * zap EVERYONE, but only on this nas
379 if (zap && !user && (~nas_port == 0)) {
381 * We need to know which NAS to zap users in.
383 if (nas_ip_address == INADDR_NONE) usage(1);
385 printf("Acct-Status-Type = Accounting-Off\n");
386 printf("NAS-IP-Address = %s\n",
387 hostname(buffer, sizeof(buffer), nas_ip_address));
388 printf("Acct-Delay-Time = 0\n");
389 exit(0); /* don't bother printing anything else */
393 * Initialize mainconfig
395 memset(&mainconfig, 0, sizeof(mainconfig));
396 mainconfig.radlog_dest = RADLOG_STDOUT;
398 /* Read radiusd.conf */
399 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
400 maincs = cf_file_read(buffer);
402 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
406 /* Read the radutmp section of radiusd.conf */
407 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
409 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
414 cf_section_parse(cs, NULL, module_config);
416 /* Assign the correct path for the radutmp file */
417 radutmp_file = radutmpconfig.radutmp_fn;
420 * See if we are "fingerd".
422 if (strstr(argv[0], "fingerd")) {
425 if (showname < 0) showname = 0;
427 if (showname < 0) showname = 1;
431 * Read first line of the input.
433 fgets(inbuf, 128, stdin);
435 while(*p == ' ' || *p == '\t') p++;
436 if (*p == '/' && *(p + 1)) p += 2;
437 while(*p == ' ' || *p == '\t') p++;
438 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
443 * See if we fingered a specific user.
446 if (*p) sys_finger(p);
450 * Show the users logged in on the terminal server(s).
452 if ((fp = fopen(radutmp_file, "r")) == NULL) {
453 fprintf(stderr, "%s: Error reading %s: %s\n",
454 progname, radutmp_file, strerror(errno));
459 * Don't print the headers if raw or RADIUS
461 if (!rawoutput && !radiusoutput) {
462 fputs(showname ? hdr1 : hdr2, stdout);
467 * Read the file, printing out active entries.
469 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
470 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
473 * We don't show shell users if we are
474 * fingerd, as we have done that above.
476 if (hideshell && !strchr("PCS", rt.proto))
480 * Print out sessions only for the given user.
482 if (user) { /* only for a particular user */
483 if (((user_cmp == 0) &&
484 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
486 (strncmp(rt.login, user, strlen(user)) != 0))) {
492 * Print out only for the given NAS port.
494 if (~nas_port != 0) {
495 if (rt.nas_port != nas_port) continue;
499 * Print out only for the given NAS IP address
501 if (nas_ip_address != INADDR_NONE) {
502 if (rt.nas_address != nas_ip_address) continue;
505 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
506 session_id[sizeof(rt.session_id)] = 0;
508 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
510 portno = (showname ? 999 : 99999);
513 portno = rt.nas_port;
517 * Print output as RADIUS attributes
520 memcpy(nasname, rt.login, sizeof(rt.login));
521 nasname[sizeof(rt.login)] = '\0';
523 librad_safeprint(nasname, -1, buffer,
525 printf("User-Name = \"%s\"\n", buffer);
527 librad_safeprint(session_id, -1, buffer,
529 printf("Acct-Session-Id = \"%s\"\n", buffer);
531 if (zap) printf("Acct-Status-Type = Stop\n");
533 printf("NAS-IP-Address = %s\n",
534 hostname(buffer, sizeof(buffer),
536 printf("NAS-Port = %u\n", rt.nas_port);
540 printf("Service-Type = Framed-User\n");
541 printf("Framed-Protocol = SLIP\n");
544 printf("Service-Type = Framed-User\n");
545 printf("Framed-Protocol = PPP\n");
548 printf("Service-type = Login-User\n");
551 if (rt.framed_address != INADDR_NONE) {
552 printf("Framed-IP-Address = %s\n",
553 hostname(buffer, sizeof(buffer),
558 * Some sanity checks on the time
560 if ((rt.time <= now) &&
561 (now - rt.time) <= (86400 * 365)) {
562 printf("Acct-Session-Time = %ld\n",
566 if (rt.caller_id[0] != '\0') {
567 memcpy(nasname, rt.caller_id,
568 sizeof(rt.caller_id));
569 nasname[sizeof(rt.caller_id)] = '\0';
571 librad_safeprint(nasname, -1, buffer,
573 printf("Calling-Station-Id = \"%s\"\n", buffer);
576 printf("\n"); /* separate entries with a blank line */
581 * Show the fill name, or not.
584 printf((rawoutput == 0? rfmt1: rfmt1r),
586 showcid ? rt.caller_id :
587 (showsid? session_id : fullname(rt.login)),
588 proto(rt.proto, rt.porttype),
591 hostname(nasname, sizeof(nasname), rt.nas_address),
592 hostname(othername, sizeof(othername), rt.framed_address), eol);
594 printf((rawoutput == 0? rfmt2: rfmt2r),
597 proto(rt.proto, rt.porttype),
599 hostname(nasname, sizeof(nasname), rt.nas_address),
600 hostname(othername, sizeof(othername), rt.framed_address),