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 static 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;
57 bool log_stripped_names;
60 * Global, for log.c to use.
62 main_config_t main_config;
71 pid_t rad_waitpid(pid_t pid, int *status)
73 return waitpid(pid, status, 0);
77 static struct radutmp_config_t {
78 char const *radutmp_fn;
81 static const CONF_PARSER module_config[] = {
82 { "filename", FR_CONF_POINTER(PW_TYPE_FILE_INPUT, &radutmpconfig.radutmp_fn), RADUTMP },
83 CONF_PARSER_TERMINATOR
87 * Get fullname of a user.
89 static char *fullname(char *username)
95 if ((pwd = getpwnam(username)) != NULL) {
96 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
105 * Return protocol type.
107 static char const *proto(int id, int porttype)
112 if (!strchr("ASITX", porttype))
115 snprintf(buf, sizeof(buf), "SLP %c", porttype);
117 snprintf(buf, sizeof(buf), "PPP %c", porttype);
119 snprintf(buf, sizeof(buf), "shl %c", porttype);
122 if (id == 'S') return "SLIP";
123 if (id == 'P') return "PPP";
128 * Return a time in the form day hh:mm
130 static char *dotime(time_t t)
135 strlcpy(s + 4, s + 11, 6);
138 strlcpy(s + 4, s + 8, 9);
147 * Print address of NAS.
149 static char const *hostname(char *buf, size_t buflen, uint32_t ipaddr)
152 * WTF is this code for?
154 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
157 return inet_ntop(AF_INET, &ipaddr, buf, buflen);
163 * Print usage message and exit.
165 static void NEVER_RETURNS usage(int status)
167 FILE *output = status?stderr:stdout;
169 fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
170 fprintf(output, " -c Show caller ID, if available.\n");
171 fprintf(output, " -d Set the raddb directory (default is %s).\n", RADIUS_DIR);
172 fprintf(output, " -F <file> Use radutmp <file>.\n");
173 fprintf(output, " -i Show session ID.\n");
174 fprintf(output, " -n No full name.\n");
175 fprintf(output, " -N <nas-ip-address> Show entries matching the given NAS IP address.\n");
176 fprintf(output, " -p Show port type.\n");
177 fprintf(output, " -P <port> Show entries matching the given nas port.\n");
178 fprintf(output, " -r Print output as raw comma-delimited data.\n");
179 fprintf(output, " -R Print output as RADIUS attributes and values.\n");
180 fprintf(output, " includes ALL information from the radutmp record.\n");
181 fprintf(output, " -s Show full name.\n");
182 fprintf(output, " -S Hide shell users from radius.\n");
183 fprintf(output, " -u <user> Show entries matching the given user.\n");
184 fprintf(output, " -U <user> Like -u, but case-sensitive.\n");
185 fprintf(output, " -Z Include accounting stop information in radius output. Requires -R.\n");
193 int main(int argc, char **argv)
195 CONF_SECTION *maincs, *cs;
200 char session_id[sizeof(rt.session_id)+1];
204 int radiusoutput = 0; /* Radius attributes */
209 char const *user = NULL;
212 uint32_t nas_port = ~0;
213 uint32_t nas_ip_address = INADDR_NONE;
216 raddb_dir = RADIUS_DIR;
219 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
225 talloc_set_log_stderr();
227 while((c = getopt(argc, argv, "d:D:fF:nN:sSipP:crRu:U:Z")) != EOF) switch (c) {
235 radutmp_file = optarg;
238 usage(0); /* never returns */
247 if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
261 nas_port = atoi(optarg);
287 usage(1); /* never returns */
291 * Mismatch between the binary and the libraries it depends on
293 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
298 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
303 if (dict_read(raddb_dir, RADIUS_DICTIONARY) == -1) {
307 fr_strerror(); /* Clear the error buffer */
312 if (zap && !radiusoutput) zap = 0;
315 * zap EVERYONE, but only on this nas
317 if (zap && !user && (~nas_port == 0)) {
319 * We need to know which NAS to zap users in.
321 if (nas_ip_address == INADDR_NONE) usage(1);
323 printf("Acct-Status-Type = Accounting-Off\n");
324 printf("NAS-IP-Address = %s\n",
325 hostname(buffer, sizeof(buffer), nas_ip_address));
326 printf("Acct-Delay-Time = 0\n");
327 exit(0); /* don't bother printing anything else */
330 if (radutmp_file) goto have_radutmp;
333 * Initialize main_config
335 memset(&main_config, 0, sizeof(main_config));
337 /* Read radiusd.conf */
338 maincs = cf_section_alloc(NULL, "main", NULL);
339 if (!maincs) exit(1);
341 snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", raddb_dir);
342 if (cf_file_read(maincs, buffer) < 0) {
343 fprintf(stderr, "%s: Error reading or parsing radiusd.conf\n", argv[0]);
348 cs = cf_section_sub_find(maincs, "modules");
350 fprintf(stderr, "%s: No modules section found in radiusd.conf\n", argv[0]);
353 /* Read the radutmp section of radiusd.conf */
354 cs = cf_section_sub_find_name2(cs, "radutmp", NULL);
356 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf\n", argv[0]);
360 cf_section_parse(cs, NULL, module_config);
362 /* Assign the correct path for the radutmp file */
363 radutmp_file = radutmpconfig.radutmp_fn;
366 if (showname < 0) showname = 1;
369 * Show the users logged in on the terminal server(s).
371 if ((fp = fopen(radutmp_file, "r")) == NULL) {
372 fprintf(stderr, "%s: Error reading %s: %s\n",
373 progname, radutmp_file, fr_syserror(errno));
378 * Don't print the headers if raw or RADIUS
380 if (!rawoutput && !radiusoutput) {
381 fputs(showname ? hdr1 : hdr2, stdout);
386 * Read the file, printing out active entries.
388 while (fread(&rt, sizeof(rt), 1, fp) == 1) {
389 char name[sizeof(rt.login) + 1];
391 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
394 * We don't show shell users if we are
395 * fingerd, as we have done that above.
397 if (hideshell && !strchr("PCS", rt.proto))
401 * Print out sessions only for the given user.
403 if (user) { /* only for a particular user */
404 if (((user_cmp == 0) &&
405 (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
407 (strncmp(rt.login, user, strlen(user)) != 0))) {
413 * Print out only for the given NAS port.
415 if (~nas_port != 0) {
416 if (rt.nas_port != nas_port) continue;
420 * Print out only for the given NAS IP address
422 if (nas_ip_address != INADDR_NONE) {
423 if (rt.nas_address != nas_ip_address) continue;
426 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
427 session_id[sizeof(rt.session_id)] = 0;
429 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
431 portno = (showname ? 999 : 99999);
434 portno = rt.nas_port;
438 * Print output as RADIUS attributes
441 memcpy(nasname, rt.login, sizeof(rt.login));
442 nasname[sizeof(rt.login)] = '\0';
444 fr_prints(buffer, sizeof(buffer), nasname, -1, '"');
445 printf("User-Name = \"%s\"\n", buffer);
447 fr_prints(buffer, sizeof(buffer), session_id, -1, '"');
448 printf("Acct-Session-Id = \"%s\"\n", buffer);
450 if (zap) printf("Acct-Status-Type = Stop\n");
452 printf("NAS-IP-Address = %s\n",
453 hostname(buffer, sizeof(buffer),
455 printf("NAS-Port = %u\n", rt.nas_port);
459 printf("Service-Type = Framed-User\n");
460 printf("Framed-Protocol = SLIP\n");
464 printf("Service-Type = Framed-User\n");
465 printf("Framed-Protocol = PPP\n");
469 printf("Service-type = Login-User\n");
472 if (rt.framed_address != INADDR_NONE) {
473 printf("Framed-IP-Address = %s\n",
474 hostname(buffer, sizeof(buffer),
479 * Some sanity checks on the time
481 if ((rt.time <= now) &&
482 (now - rt.time) <= (86400 * 365)) {
483 printf("Acct-Session-Time = %" PRId64 "\n", (int64_t) (now - rt.time));
486 if (rt.caller_id[0] != '\0') {
487 memcpy(nasname, rt.caller_id,
488 sizeof(rt.caller_id));
489 nasname[sizeof(rt.caller_id)] = '\0';
491 fr_prints(buffer, sizeof(buffer), nasname, -1, '"');
492 printf("Calling-Station-Id = \"%s\"\n", buffer);
495 printf("\n"); /* separate entries with a blank line */
500 * Show the fill name, or not.
502 memcpy(name, rt.login, sizeof(rt.login));
503 name[sizeof(rt.login)] = '\0';
506 if (rawoutput == 0) {
507 printf("%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-15.15s %-.19s%s",
509 showcid ? rt.caller_id :
510 (showsid? session_id : fullname(rt.login)),
511 proto(rt.proto, rt.porttype),
514 hostname(nasname, sizeof(nasname), rt.nas_address),
515 hostname(othername, sizeof(othername), rt.framed_address), eol);
517 printf("%s,%s,%s,%s%u,%s,%s,%s%s",
519 showcid ? rt.caller_id :
520 (showsid? session_id : fullname(rt.login)),
521 proto(rt.proto, rt.porttype),
524 hostname(nasname, sizeof(nasname), rt.nas_address),
525 hostname(othername, sizeof(othername), rt.framed_address), eol);
528 if (rawoutput == 0) {
529 printf("%-10.10s %s%-5u %-6.6s %-13.13s %-15.15s %-.28s%s",
532 proto(rt.proto, rt.porttype),
534 hostname(nasname, sizeof(nasname), rt.nas_address),
535 hostname(othername, sizeof(othername), rt.framed_address),
538 printf("%s,%s%u,%s,%s,%s,%s%s",
541 proto(rt.proto, rt.porttype),
543 hostname(nasname, sizeof(nasname), rt.nas_address),
544 hostname(othername, sizeof(othername), rt.framed_address),