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 %-15.15s %-.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 %-15.15s %-.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;
80 char *request_log_file = NULL;
81 char *debug_log_file = NULL;
82 int radius_xlat(char *out, int outlen, const char *fmt,
83 REQUEST *request, RADIUS_ESCAPE_STRING func)
89 struct radutmp_config_t {
93 static const CONF_PARSER module_config[] = {
94 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
95 { NULL, -1, 0, NULL, NULL }
101 static FILE *safe_popen(const char *cmd, const char *mode)
107 * Change all suspect characters into a space.
109 strlcpy(buf, cmd, sizeof(buf));
110 buf[sizeof(buf) - 1] = 0;
111 for (p = buf; *p; p++) {
112 if (isalnum((int) *p))
114 if (strchr("@%-_ \t+:,./", *p) == NULL)
118 return popen(buf, mode);
122 * Print a file from FINGER_DIR. If the file is executable,
123 * execute it instead. Return 0 if successful.
125 static int ffile(const char *arg)
132 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
133 if (access(fn, X_OK) == 0) {
135 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
136 fp = safe_popen(fn, "r");
137 } else fp = fopen(fn, "r");
142 while(fgets(fn, 1024, fp)) {
143 if ((s = strchr(fn, '\n')) != NULL)
145 fprintf(stdout, "%s\r\n", fn);
157 * Execute the system finger and translate LF to CRLF.
159 static void sys_finger(const char *l)
168 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
169 if ((fp = safe_popen(fn, "r")) == NULL) {
170 printf("popen: %s\r\n", strerror(errno));
174 while(fgets(fn, 1024, fp)) {
175 if ((p = strchr(fn, '\n')) != NULL)
177 fprintf(stdout, "%s\r\n", fn);
185 * Get fullname of a user.
187 static char *fullname(char *username)
193 if ((pwd = getpwnam(username)) != NULL) {
194 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
195 return pwd->pw_gecos;
203 * Return protocol type.
205 static const char *proto(int id, int porttype)
210 if (!strchr("ASITX", porttype))
213 snprintf(buf, sizeof(buf), "SLP %c", porttype);
215 snprintf(buf, sizeof(buf), "PPP %c", porttype);
217 snprintf(buf, sizeof(buf), "shl %c", porttype);
220 if (id == 'S') return "SLIP";
221 if (id == 'P') return "PPP";
226 * Return a time in the form day hh:mm
228 static char *dotime(time_t t)
233 strlcpy(s + 4, s + 11, 6);
236 strlcpy(s + 4, s + 8, 9);
245 * Print address of NAS.
247 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
250 * WTF is this code for?
252 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
255 return inet_ntop(AF_INET, &ipaddr, buf, buflen);
261 * Print usage message and exit.
263 static void NEVER_RETURNS usage(int status)
265 FILE *output = status?stderr:stdout;
267 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
268 fprintf(output, " -c: show caller ID, if available\n");
269 fprintf(output, " -d: set the raddb directory (default is %s)\n",
271 fprintf(output, " -f: give fingerd output\n");
272 fprintf(output, " -i: show session ID\n");
273 fprintf(output, " -n: no full name\n");
274 fprintf(output, " -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
275 fprintf(output, " -p: show port type\n");
276 fprintf(output, " -P <port>: Show entries matching the given nas port\n");
277 fprintf(output, " -r: Print output as raw comma-delimited data\n");
278 fprintf(output, " -R: Print output as RADIUS attributes and values\n");
279 fprintf(output, " Includes ALL information from the radutmp record.\n");
280 fprintf(output, " -s: show full name\n");
281 fprintf(output, " -S: hide shell users from radius\n");
282 fprintf(output, " -u <user>: Show entries matching the given user\n");
283 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
284 fprintf(output, " -Z: Include accounting stop information in radius output. Requires -R.\n");
290 * Main program, either pmwho or fingerd.
292 int main(int argc, char **argv)
294 CONF_SECTION *maincs, *cs;
300 char session_id[sizeof(rt.session_id)+1];
305 int radiusoutput = 0; /* Radius attributes */
311 const char *user = NULL;
314 uint32_t nas_port = ~0;
315 uint32_t nas_ip_address = INADDR_NONE;
318 radius_dir = RADIUS_DIR;
320 while((c = getopt(argc, argv, "d:flnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
338 if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
352 nas_port = atoi(optarg);
384 if (zap && !radiusoutput) zap = 0;
387 * zap EVERYONE, but only on this nas
389 if (zap && !user && (~nas_port == 0)) {
391 * We need to know which NAS to zap users in.
393 if (nas_ip_address == INADDR_NONE) usage(1);
395 printf("Acct-Status-Type = Accounting-Off\n");
396 printf("NAS-IP-Address = %s\n",
397 hostname(buffer, sizeof(buffer), nas_ip_address));
398 printf("Acct-Delay-Time = 0\n");
399 exit(0); /* don't bother printing anything else */
403 * Initialize mainconfig
405 memset(&mainconfig, 0, sizeof(mainconfig));
406 mainconfig.radlog_dest = RADLOG_STDOUT;
408 /* Read radiusd.conf */
409 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
410 maincs = cf_file_read(buffer);
412 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
416 /* Read the radutmp section of radiusd.conf */
417 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
419 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
424 cf_section_parse(cs, NULL, module_config);
426 /* Assign the correct path for the radutmp file */
427 radutmp_file = radutmpconfig.radutmp_fn;
430 * See if we are "fingerd".
432 if (strstr(argv[0], "fingerd")) {
435 if (showname < 0) showname = 0;
437 if (showname < 0) showname = 1;
441 * Read first line of the input.
443 fgets(inbuf, 128, stdin);
445 while(*p == ' ' || *p == '\t') p++;
446 if (*p == '/' && *(p + 1)) p += 2;
447 while(*p == ' ' || *p == '\t') p++;
448 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
453 * See if we fingered a specific user.
456 if (*p) sys_finger(p);
460 * Show the users logged in on the terminal server(s).
462 if ((fp = fopen(radutmp_file, "r")) == NULL) {
463 fprintf(stderr, "%s: Error reading %s: %s\n",
464 progname, radutmp_file, strerror(errno));
469 * Don't print the headers if raw or RADIUS
471 if (!rawoutput && !radiusoutput) {
472 fputs(showname ? hdr1 : hdr2, stdout);
477 * Read the file, printing out active entries.
479 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
480 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
483 * We don't show shell users if we are
484 * fingerd, as we have done that above.
486 if (hideshell && !strchr("PCS", rt.proto))
490 * Print out sessions only for the given user.
492 if (user) { /* only for a particular user */
493 if (((user_cmp == 0) &&
494 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
496 (strncmp(rt.login, user, strlen(user)) != 0))) {
502 * Print out only for the given NAS port.
504 if (~nas_port != 0) {
505 if (rt.nas_port != nas_port) continue;
509 * Print out only for the given NAS IP address
511 if (nas_ip_address != INADDR_NONE) {
512 if (rt.nas_address != nas_ip_address) continue;
515 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
516 session_id[sizeof(rt.session_id)] = 0;
518 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
520 portno = (showname ? 999 : 99999);
523 portno = rt.nas_port;
527 * Print output as RADIUS attributes
530 memcpy(nasname, rt.login, sizeof(rt.login));
531 nasname[sizeof(rt.login)] = '\0';
533 fr_print_string(nasname, 0, buffer,
535 printf("User-Name = \"%s\"\n", buffer);
537 fr_print_string(session_id, 0, buffer,
539 printf("Acct-Session-Id = \"%s\"\n", buffer);
541 if (zap) printf("Acct-Status-Type = Stop\n");
543 printf("NAS-IP-Address = %s\n",
544 hostname(buffer, sizeof(buffer),
546 printf("NAS-Port = %u\n", rt.nas_port);
550 printf("Service-Type = Framed-User\n");
551 printf("Framed-Protocol = SLIP\n");
554 printf("Service-Type = Framed-User\n");
555 printf("Framed-Protocol = PPP\n");
558 printf("Service-type = Login-User\n");
561 if (rt.framed_address != INADDR_NONE) {
562 printf("Framed-IP-Address = %s\n",
563 hostname(buffer, sizeof(buffer),
568 * Some sanity checks on the time
570 if ((rt.time <= now) &&
571 (now - rt.time) <= (86400 * 365)) {
572 printf("Acct-Session-Time = %ld\n",
576 if (rt.caller_id[0] != '\0') {
577 memcpy(nasname, rt.caller_id,
578 sizeof(rt.caller_id));
579 nasname[sizeof(rt.caller_id)] = '\0';
581 fr_print_string(nasname, 0, buffer,
583 printf("Calling-Station-Id = \"%s\"\n", buffer);
586 printf("\n"); /* separate entries with a blank line */
591 * Show the fill name, or not.
594 printf((rawoutput == 0? rfmt1: rfmt1r),
596 showcid ? rt.caller_id :
597 (showsid? session_id : fullname(rt.login)),
598 proto(rt.proto, rt.porttype),
601 hostname(nasname, sizeof(nasname), rt.nas_address),
602 hostname(othername, sizeof(othername), rt.framed_address), eol);
604 printf((rawoutput == 0? rfmt2: rfmt2r),
607 proto(rt.proto, rt.porttype),
609 hostname(nasname, sizeof(nasname), rt.nas_address),
610 hostname(othername, sizeof(othername), rt.framed_address),