1 /*@-skipposixheaders@*/
3 * radwho.c Show who is logged in on the terminal servers.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Copyright 2000,2006 The FreeRADIUS server project
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/sysutmp.h>
30 #include <freeradius-devel/radutmp.h>
41 * Header above output and format.
43 static const char *hdr1 =
44 "Login Name What TTY When From Location";
45 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-15.15s %-.19s%s";
46 static const char *rfmt1r = "%s,%s,%s,%s%u,%s,%s,%s%s";
48 static const char *hdr2 =
49 "Login Port What When From Location";
50 static const char *rfmt2 = "%-10.10s %s%-5u %-6.6s %-13.13s %-15.15s %-.28s%s";
51 static const char *rfmt2r = "%s,%s%u,%s,%s,%s,%s%s";
53 static const char *eol = "\n";
54 static int showname = -1;
55 static int showptype = 0;
56 static int showcid = 0;
58 const char *progname = "radwho";
59 const char *radlog_dir = NULL;
60 const char *radutmp_file = NULL;
61 int check_config = FALSE;
63 const char *raddb_dir = NULL;
64 const char *radacct_dir = NULL;
65 const char *radlib_dir = NULL;
66 uint32_t myip = INADDR_ANY;
67 int log_stripped_names;
70 * Global, for log.c to use.
72 struct main_config_t mainconfig;
73 char *request_log_file = NULL;
74 char *debug_log_file = NULL;
75 size_t radius_xlat(char *out, UNUSED int outlen, UNUSED const char *fmt,
76 UNUSED REQUEST *request,
77 UNUSED RADIUS_ESCAPE_STRING func, UNUSED void *arg)
83 struct radutmp_config_t {
87 static const CONF_PARSER module_config[] = {
88 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
89 { NULL, -1, 0, NULL, NULL }
93 * Get fullname of a user.
95 static char *fullname(char *username)
101 if ((pwd = getpwnam(username)) != NULL) {
102 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
103 return pwd->pw_gecos;
111 * Return protocol type.
113 static const char *proto(int id, int porttype)
118 if (!strchr("ASITX", porttype))
121 snprintf(buf, sizeof(buf), "SLP %c", porttype);
123 snprintf(buf, sizeof(buf), "PPP %c", porttype);
125 snprintf(buf, sizeof(buf), "shl %c", porttype);
128 if (id == 'S') return "SLIP";
129 if (id == 'P') return "PPP";
134 * Return a time in the form day hh:mm
136 static char *dotime(time_t t)
141 strlcpy(s + 4, s + 11, 6);
144 strlcpy(s + 4, s + 8, 9);
153 * Print address of NAS.
155 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
158 * WTF is this code for?
160 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
163 return inet_ntop(AF_INET, &ipaddr, buf, buflen);
169 * Print usage message and exit.
171 static void NEVER_RETURNS usage(int status)
173 FILE *output = status?stderr:stdout;
175 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
176 fprintf(output, " -c: show caller ID, if available\n");
177 fprintf(output, " -d: set the raddb directory (default is %s)\n",
179 fprintf(output, " -F <file>: Use radutmp <file>\n");
180 fprintf(output, " -i: show session ID\n");
181 fprintf(output, " -n: no full name\n");
182 fprintf(output, " -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
183 fprintf(output, " -p: show port type\n");
184 fprintf(output, " -P <port>: Show entries matching the given nas port\n");
185 fprintf(output, " -r: Print output as raw comma-delimited data\n");
186 fprintf(output, " -R: Print output as RADIUS attributes and values\n");
187 fprintf(output, " Includes ALL information from the radutmp record.\n");
188 fprintf(output, " -s: show full name\n");
189 fprintf(output, " -S: hide shell users from radius\n");
190 fprintf(output, " -u <user>: Show entries matching the given user\n");
191 fprintf(output, " -U <user>: like -u, but case-sensitive\n");
192 fprintf(output, " -Z: Include accounting stop information in radius output. Requires -R.\n");
200 int main(int argc, char **argv)
202 CONF_SECTION *maincs, *cs;
207 char session_id[sizeof(rt.session_id)+1];
211 int radiusoutput = 0; /* Radius attributes */
216 const char *user = NULL;
219 uint32_t nas_port = ~0;
220 uint32_t nas_ip_address = INADDR_NONE;
223 raddb_dir = RADIUS_DIR;
225 while((c = getopt(argc, argv, "d:fF:nN:sSipP:crRu:U:Z")) != EOF) switch(c) {
230 radutmp_file = optarg;
242 if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
256 nas_port = atoi(optarg);
288 if (zap && !radiusoutput) zap = 0;
291 * zap EVERYONE, but only on this nas
293 if (zap && !user && (~nas_port == 0)) {
295 * We need to know which NAS to zap users in.
297 if (nas_ip_address == INADDR_NONE) usage(1);
299 printf("Acct-Status-Type = Accounting-Off\n");
300 printf("NAS-IP-Address = %s\n",
301 hostname(buffer, sizeof(buffer), nas_ip_address));
302 printf("Acct-Delay-Time = 0\n");
303 exit(0); /* don't bother printing anything else */
306 if (radutmp_file) goto have_radutmp;
309 * Initialize mainconfig
311 memset(&mainconfig, 0, sizeof(mainconfig));
312 mainconfig.radlog_dest = RADLOG_STDOUT;
314 /* Read radiusd.conf */
315 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", raddb_dir);
316 maincs = cf_file_read(buffer);
318 fprintf(stderr, "%s: Error reading or parsing radiusd.conf.\n", argv[0]);
322 /* Read the radutmp section of radiusd.conf */
323 cs = cf_section_find_name2(cf_section_sub_find(maincs, "modules"), "radutmp", NULL);
325 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
330 cf_section_parse(cs, NULL, module_config);
332 /* Assign the correct path for the radutmp file */
333 radutmp_file = radutmpconfig.radutmp_fn;
336 if (showname < 0) showname = 1;
339 * Show the users logged in on the terminal server(s).
341 if ((fp = fopen(radutmp_file, "r")) == NULL) {
342 fprintf(stderr, "%s: Error reading %s: %s\n",
343 progname, radutmp_file, strerror(errno));
348 * Don't print the headers if raw or RADIUS
350 if (!rawoutput && !radiusoutput) {
351 fputs(showname ? hdr1 : hdr2, stdout);
356 * Read the file, printing out active entries.
358 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
359 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
362 * We don't show shell users if we are
363 * fingerd, as we have done that above.
365 if (hideshell && !strchr("PCS", rt.proto))
369 * Print out sessions only for the given user.
371 if (user) { /* only for a particular user */
372 if (((user_cmp == 0) &&
373 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
375 (strncmp(rt.login, user, strlen(user)) != 0))) {
381 * Print out only for the given NAS port.
383 if (~nas_port != 0) {
384 if (rt.nas_port != nas_port) continue;
388 * Print out only for the given NAS IP address
390 if (nas_ip_address != INADDR_NONE) {
391 if (rt.nas_address != nas_ip_address) continue;
394 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
395 session_id[sizeof(rt.session_id)] = 0;
397 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
399 portno = (showname ? 999 : 99999);
402 portno = rt.nas_port;
406 * Print output as RADIUS attributes
409 memcpy(nasname, rt.login, sizeof(rt.login));
410 nasname[sizeof(rt.login)] = '\0';
412 fr_print_string(nasname, 0, buffer,
414 printf("User-Name = \"%s\"\n", buffer);
416 fr_print_string(session_id, 0, buffer,
418 printf("Acct-Session-Id = \"%s\"\n", buffer);
420 if (zap) printf("Acct-Status-Type = Stop\n");
422 printf("NAS-IP-Address = %s\n",
423 hostname(buffer, sizeof(buffer),
425 printf("NAS-Port = %u\n", rt.nas_port);
429 printf("Service-Type = Framed-User\n");
430 printf("Framed-Protocol = SLIP\n");
433 printf("Service-Type = Framed-User\n");
434 printf("Framed-Protocol = PPP\n");
437 printf("Service-type = Login-User\n");
440 if (rt.framed_address != INADDR_NONE) {
441 printf("Framed-IP-Address = %s\n",
442 hostname(buffer, sizeof(buffer),
447 * Some sanity checks on the time
449 if ((rt.time <= now) &&
450 (now - rt.time) <= (86400 * 365)) {
451 printf("Acct-Session-Time = %ld\n",
455 if (rt.caller_id[0] != '\0') {
456 memcpy(nasname, rt.caller_id,
457 sizeof(rt.caller_id));
458 nasname[sizeof(rt.caller_id)] = '\0';
460 fr_print_string(nasname, 0, buffer,
462 printf("Calling-Station-Id = \"%s\"\n", buffer);
465 printf("\n"); /* separate entries with a blank line */
470 * Show the fill name, or not.
473 char login[sizeof(rt.login) + 1];
474 strncpy(login, rt.login, sizeof(rt.login));
476 printf((rawoutput == 0? rfmt1: rfmt1r),
478 showcid ? rt.caller_id :
479 (showsid? session_id : fullname(rt.login)),
480 proto(rt.proto, rt.porttype),
483 hostname(nasname, sizeof(nasname), rt.nas_address),
484 hostname(othername, sizeof(othername), rt.framed_address), eol);
486 printf((rawoutput == 0? rfmt2: rfmt2r),
489 proto(rt.proto, rt.porttype),
491 hostname(nasname, sizeof(nasname), rt.nas_address),
492 hostname(othername, sizeof(othername), rt.framed_address),