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>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/sysutmp.h>
29 #include <freeradius-devel/radutmp.h>
36 * Header above output and format.
38 static char const *hdr1 =
39 "Login Name What TTY When From Location";
41 static char const *hdr2 =
42 "Login Port What When From Location";
44 static char const *eol = "\n";
45 static int showname = -1;
46 static int showptype = 0;
47 static int showcid = 0;
48 char const *progname = "radwho";
49 char const *radlog_dir = NULL;
51 static char const *radutmp_file = NULL;
52 static char const *raddb_dir = RADDBDIR;
53 static char const *dict_dir = DICTDIR;
55 char const *radacct_dir = NULL;
56 char const *radlib_dir = NULL;
58 bool log_stripped_names;
61 * Global, for log.c to use.
63 main_config_t main_config;
72 pid_t rad_waitpid(pid_t pid, int *status)
74 return waitpid(pid, status, 0);
78 static struct radutmp_config_t {
79 char const *radutmp_fn;
82 static const CONF_PARSER module_config[] = {
83 { "filename", FR_CONF_POINTER(PW_TYPE_FILE_INPUT, &radutmpconfig.radutmp_fn), RADUTMP },
84 CONF_PARSER_TERMINATOR
88 * Get fullname of a user.
90 static char *fullname(char *username)
96 if ((pwd = getpwnam(username)) != NULL) {
97 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
106 * Return protocol type.
108 static char const *proto(int id, int porttype)
113 if (!strchr("ASITX", porttype))
116 snprintf(buf, sizeof(buf), "SLP %c", porttype);
118 snprintf(buf, sizeof(buf), "PPP %c", porttype);
120 snprintf(buf, sizeof(buf), "shl %c", porttype);
123 if (id == 'S') return "SLIP";
124 if (id == 'P') return "PPP";
129 * Return a time in the form day hh:mm
131 static char *dotime(time_t t)
136 strlcpy(s + 4, s + 11, 6);
139 strlcpy(s + 4, s + 8, 9);
148 * Print address of NAS.
150 static char const *hostname(char *buf, size_t buflen, uint32_t ipaddr)
153 * WTF is this code for?
155 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
158 return inet_ntop(AF_INET, &ipaddr, buf, buflen);
164 * Print usage message and exit.
166 static void NEVER_RETURNS usage(int status)
168 FILE *output = status?stderr:stdout;
170 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
171 fprintf(output, " -c Show caller ID, if available.\n");
172 fprintf(output, " -d Set the raddb directory (default is %s).\n", RADIUS_DIR);
173 fprintf(output, " -F <file> Use radutmp <file>.\n");
174 fprintf(output, " -i Show session ID.\n");
175 fprintf(output, " -n No full name.\n");
176 fprintf(output, " -N <nas-ip-address> Show entries matching the given NAS IP address.\n");
177 fprintf(output, " -p Show port type.\n");
178 fprintf(output, " -P <port> Show entries matching the given nas port.\n");
179 fprintf(output, " -r Print output as raw comma-delimited data.\n");
180 fprintf(output, " -R Print output as RADIUS attributes and values.\n");
181 fprintf(output, " includes ALL information from the radutmp record.\n");
182 fprintf(output, " -s Show full name.\n");
183 fprintf(output, " -S Hide shell users from radius.\n");
184 fprintf(output, " -u <user> Show entries matching the given user.\n");
185 fprintf(output, " -U <user> Like -u, but case-sensitive.\n");
186 fprintf(output, " -Z Include accounting stop information in radius output. Requires -R.\n");
194 int main(int argc, char **argv)
196 CONF_SECTION *maincs, *cs;
201 char session_id[sizeof(rt.session_id)+1];
205 int radiusoutput = 0; /* Radius attributes */
210 char const *user = NULL;
213 uint32_t nas_port = ~0;
214 uint32_t nas_ip_address = INADDR_NONE;
217 raddb_dir = RADIUS_DIR;
220 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
226 talloc_set_log_stderr();
228 while((c = getopt(argc, argv, "d:D:fF:nN:sSipP:crRu:U:Z")) != EOF) switch (c) {
236 radutmp_file = optarg;
239 usage(0); /* never returns */
248 if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
262 nas_port = atoi(optarg);
288 usage(1); /* never returns */
292 * Mismatch between the binary and the libraries it depends on
294 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
299 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
304 if (dict_read(raddb_dir, RADIUS_DICTIONARY) == -1) {
308 fr_strerror(); /* Clear the error buffer */
313 if (zap && !radiusoutput) zap = 0;
316 * zap EVERYONE, but only on this nas
318 if (zap && !user && (~nas_port == 0)) {
320 * We need to know which NAS to zap users in.
322 if (nas_ip_address == INADDR_NONE) usage(1);
324 printf("Acct-Status-Type = Accounting-Off\n");
325 printf("NAS-IP-Address = %s\n",
326 hostname(buffer, sizeof(buffer), nas_ip_address));
327 printf("Acct-Delay-Time = 0\n");
328 exit(0); /* don't bother printing anything else */
331 if (radutmp_file) goto have_radutmp;
334 * Initialize main_config
336 memset(&main_config, 0, sizeof(main_config));
338 /* Read radiusd.conf */
339 maincs = cf_section_alloc(NULL, "main", NULL);
340 if (!maincs) exit(1);
342 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", raddb_dir);
343 if (cf_file_read(maincs, buffer) < 0) {
344 fprintf(stderr, "%s: Error reading or parsing radiusd.conf\n", argv[0]);
349 cs = cf_section_sub_find(maincs, "modules");
351 fprintf(stderr, "%s: No modules section found in radiusd.conf\n", argv[0]);
354 /* Read the radutmp section of radiusd.conf */
355 cs = cf_section_sub_find_name2(cs, "radutmp", NULL);
357 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf\n", argv[0]);
361 cf_section_parse(cs, NULL, module_config);
363 /* Assign the correct path for the radutmp file */
364 radutmp_file = radutmpconfig.radutmp_fn;
367 if (showname < 0) showname = 1;
370 * Show the users logged in on the terminal server(s).
372 if ((fp = fopen(radutmp_file, "r")) == NULL) {
373 fprintf(stderr, "%s: Error reading %s: %s\n",
374 progname, radutmp_file, fr_syserror(errno));
379 * Don't print the headers if raw or RADIUS
381 if (!rawoutput && !radiusoutput) {
382 fputs(showname ? hdr1 : hdr2, stdout);
387 * Read the file, printing out active entries.
389 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
390 char name[sizeof(rt.login) + 1];
392 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
395 * We don't show shell users if we are
396 * fingerd, as we have done that above.
398 if (hideshell && !strchr("PCS", rt.proto))
402 * Print out sessions only for the given user.
404 if (user) { /* only for a particular user */
405 if (((user_cmp == 0) &&
406 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
408 (strncmp(rt.login, user, strlen(user)) != 0))) {
414 * Print out only for the given NAS port.
416 if (~nas_port != 0) {
417 if (rt.nas_port != nas_port) continue;
421 * Print out only for the given NAS IP address
423 if (nas_ip_address != INADDR_NONE) {
424 if (rt.nas_address != nas_ip_address) continue;
427 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
428 session_id[sizeof(rt.session_id)] = 0;
430 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
432 portno = (showname ? 999 : 99999);
435 portno = rt.nas_port;
439 * Print output as RADIUS attributes
442 memcpy(nasname, rt.login, sizeof(rt.login));
443 nasname[sizeof(rt.login)] = '\0';
445 fr_prints(buffer, sizeof(buffer), nasname, -1, '"');
446 printf("User-Name = \"%s\"\n", buffer);
448 fr_prints(buffer, sizeof(buffer), session_id, -1, '"');
449 printf("Acct-Session-Id = \"%s\"\n", buffer);
451 if (zap) printf("Acct-Status-Type = Stop\n");
453 printf("NAS-IP-Address = %s\n",
454 hostname(buffer, sizeof(buffer),
456 printf("NAS-Port = %u\n", rt.nas_port);
460 printf("Service-Type = Framed-User\n");
461 printf("Framed-Protocol = SLIP\n");
465 printf("Service-Type = Framed-User\n");
466 printf("Framed-Protocol = PPP\n");
470 printf("Service-type = Login-User\n");
473 if (rt.framed_address != INADDR_NONE) {
474 printf("Framed-IP-Address = %s\n",
475 hostname(buffer, sizeof(buffer),
480 * Some sanity checks on the time
482 if ((rt.time <= now) &&
483 (now - rt.time) <= (86400 * 365)) {
484 printf("Acct-Session-Time = %" PRId64 "\n", (int64_t) (now - rt.time));
487 if (rt.caller_id[0] != '\0') {
488 memcpy(nasname, rt.caller_id,
489 sizeof(rt.caller_id));
490 nasname[sizeof(rt.caller_id)] = '\0';
492 fr_prints(buffer, sizeof(buffer), nasname, -1, '"');
493 printf("Calling-Station-Id = \"%s\"\n", buffer);
496 printf("\n"); /* separate entries with a blank line */
501 * Show the fill name, or not.
503 memcpy(name, rt.login, sizeof(rt.login));
504 name[sizeof(rt.login)] = '\0';
507 if (rawoutput == 0) {
508 printf("%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-15.15s %-.19s%s",
510 showcid ? rt.caller_id :
511 (showsid? session_id : fullname(rt.login)),
512 proto(rt.proto, rt.porttype),
515 hostname(nasname, sizeof(nasname), rt.nas_address),
516 hostname(othername, sizeof(othername), rt.framed_address), eol);
518 printf("%s,%s,%s,%s%u,%s,%s,%s%s",
520 showcid ? rt.caller_id :
521 (showsid? session_id : fullname(rt.login)),
522 proto(rt.proto, rt.porttype),
525 hostname(nasname, sizeof(nasname), rt.nas_address),
526 hostname(othername, sizeof(othername), rt.framed_address), eol);
529 if (rawoutput == 0) {
530 printf("%-10.10s %s%-5u %-6.6s %-13.13s %-15.15s %-.28s%s",
533 proto(rt.proto, rt.porttype),
535 hostname(nasname, sizeof(nasname), rt.nas_address),
536 hostname(othername, sizeof(othername), rt.framed_address),
539 printf("%s,%s%u,%s,%s,%s,%s%s",
542 proto(rt.proto, rt.porttype),
544 hostname(nasname, sizeof(nasname), rt.nas_address),
545 hostname(othername, sizeof(othername), rt.framed_address),