2 * radwho.c Show who is logged in on the terminal servers.
3 * Can also be installed as fingerd on the UNIX
4 * machine RADIUS runs on.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Copyright 2000 The FreeRADIUS server project
23 * Copyright 2000 Alan DeKok <aland@ox.org>
26 static const char rcsid[] =
30 #include "libradius.h"
36 #include <sys/utsname.h>
45 * FIXME: put in header file.
47 #define SYS_FINGER "/usr/bin/finger"
48 #define FINGER_DIR "/usr/local/lib/finger"
51 * Header above output and format.
53 static const char *hdr1 =
54 "Login Name What TTY When From Location";
55 static const char *ufmt1 = "%-10.10s %-17.17s %-5.5s %-4.4s %-9.9s %-9.9s %-.16s%s";
56 static const char *ufmt1r = "%s,%s,%s,%s,%s,%s,%s%s";
57 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3d %-9.9s %-9.9s %-.19s%s";
58 static const char *rfmt1r = "%s,%s,%s,%s%d,%s,%s,%s%s";
60 static const char *hdr2 =
61 "Login Port What When From Location";
62 static const char *ufmt2 = "%-10.10s %-6.6d %-7.7s %-13.13s %-10.10s %-.16s%s";
63 static const char *ufmt2r = "%s,%d,%s,%s,%s,%s%s";
64 static const char *rfmt2 = "%-10.10s %s%-5d %-6.6s %-13.13s %-10.10s %-.28s%s";
65 static const char *rfmt2r = "%s,%s%d,%s,%s,%s,%s%s";
67 static const char *eol = "\n";
68 static int showname = -1;
69 static int showptype = 0;
70 static int showcid = 0;
72 const char *progname = "radwho";
73 const char *radlog_dir = NULL;
74 radlog_dest_t radlog_dest = RADLOG_FILES;
75 const char *radutmp_file = NULL;
77 int proxy_synchronous = TRUE;
78 int proxy_fallback = FALSE;
79 const char *radius_dir = NULL;
80 const char *radacct_dir = NULL;
81 const char *radlib_dir = NULL;
84 uint32_t myip = INADDR_ANY;
85 int proxy_retry_delay = RETRY_DELAY;
86 int proxy_retry_count = RETRY_COUNT;
88 int log_stripped_names;
89 struct main_config_t mainconfig;
92 * A mapping of configuration file names to internal variables
94 static CONF_PARSER server_config[] = {
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 strncpy(buf, cmd, sizeof(buf));
110 buf[sizeof(buf) - 1] = 0;
111 for (p = buf; *p; 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 succesfull.
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)
192 if ((pwd = getpwnam(username)) != NULL) {
193 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
194 return pwd->pw_gecos;
200 * Return protocol type.
202 static const char *proto(int id, int porttype)
207 if (!strchr("ASITX", porttype))
210 snprintf(buf, sizeof(buf), "SLP %c", porttype);
212 snprintf(buf, sizeof(buf), "PPP %c", porttype);
214 snprintf(buf, sizeof(buf), "shl %c", porttype);
217 if (id == 'S') return "SLIP";
218 if (id == 'P') return "PPP";
223 * Return a time in the form day hh:mm
225 static char *dotime(time_t t)
230 strncpy(s + 4, s + 11, 5);
233 strncpy(s + 4, s + 8, 8);
242 * See how long a tty has been idle.
244 char *idletime(char *line)
253 strcpy(tty, "/dev/");
256 strncat(tty, line, 10);
260 if (stat(tty, &st) == 0) {
270 snprintf(tmp, sizeof(tmp), "%dd", days);
272 snprintf(tmp, sizeof(tmp), "%2d:%02d", hr, min);
282 static const char *ttyshort(char *tty)
286 if (tty[0] == '/') tty += 5;
288 if (strncmp(tty, "tty", 3) == 0) {
289 if (tty[3] >= '0' && tty[3] <= '9')
290 snprintf(tmp, sizeof(tmp), "v%.14s", tty + 3);
292 snprintf(tmp, sizeof(tmp), "%.15s", tty + 3);
295 if (strncmp(tty, "vc", 2) == 0) {
296 snprintf(tmp, sizeof(tmp), "v.14%s", tty + 2);
299 if (strncmp(tty, "cu", 2) == 0) {
307 * Print address of NAS.
309 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
311 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
313 return ip_hostname(buf, buflen, ipaddr);
318 * Print usage message and exit.
320 static void usage(void)
322 fprintf(stderr, "Usage: radwho [-lhfnsipcr]\n");
323 fprintf(stderr, " -l: show local (shell) users too\n");
324 fprintf(stderr, " -h: hide shell users from radius\n");
325 fprintf(stderr, " -f: give fingerd output\n");
326 fprintf(stderr, " -n: no full name\n");
327 fprintf(stderr, " -s: show full name\n");
328 fprintf(stderr, " -i: show session ID\n");
329 fprintf(stderr, " -p: show port type\n");
330 fprintf(stderr, " -c: show caller ID, if available\n");
331 fprintf(stderr, " -r: output as raw data\n");
337 * Main program, either pmwho or fingerd.
339 int main(int argc, char **argv)
350 char session_id[sizeof(rt.session_id)+1];
360 radius_dir = strdup(RADIUS_DIR);
361 radutmp_file = strdup(UTMP_FILE);
363 while((c = getopt(argc, argv, "flhnsipcr")) != EOF) switch(c) {
398 /* Read radiusd.conf */
399 if(read_radius_conf_file() < 0) {
400 fprintf(stderr, "%s: Errors reading radiusd.conf\n", argv[0]);
404 cs = cf_section_find(NULL);
406 fprintf(stderr, "%s: No configuration information in radiusd.conf!\n",
410 cf_section_parse(cs, NULL, server_config);
413 * See if we are "fingerd".
415 if (strstr(argv[0], "fingerd")) {
418 if (showname < 0) showname = 0;
420 if (showname < 0) showname = 1;
424 * Read first line of the input.
426 fgets(inbuf, 128, stdin);
428 while(*p == ' ' || *p == '\t') p++;
429 if (*p == '/' && *(p + 1)) p += 2;
430 while(*p == ' ' || *p == '\t') p++;
431 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
436 * See if we fingered a specific user.
439 if (*p) sys_finger(p);
442 if (showlocal && (fp = fopen(radutmp_file, "r"))) {
445 fputs(showname ? hdr1 : hdr2, stdout);
451 * Show the logged in UNIX users.
453 gethostname(myname, 128);
454 while(fread(&ut, sizeof(ut), 1, fp) == 1) {
456 if (ut.ut_user[0] && ut.ut_line[0] &&
457 ut.ut_type == USER_PROCESS) {
459 if (ut.ut_user[0] && ut.ut_line[0]) {
464 # define UT_TIME ut_xtime
466 # define UT_TIME ut_time
469 printf((rawoutput == 0? ufmt1: ufmt1r),
471 fullname(ut.ut_name),
473 ttyshort(ut.ut_line),
478 printf((rawoutput==0? ufmt2:ufmt2r),
480 ttyshort(ut.ut_line),
492 * Show the users logged in on the terminal server(s).
494 if ((fp = fopen(RADUTMP, "r")) == NULL)
498 fputs(showname ? hdr1 : hdr2, stdout);
502 while(fread(&rt, sizeof(rt), 1, fp) == 1) {
503 if (rt.type == P_LOGIN) {
505 * We don't show shell users if we are
506 * fingerd, as we have done that above.
508 if (hideshell && !strchr("PCS", rt.proto))
511 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
512 session_id[sizeof(rt.session_id)] = 0;
514 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
516 portno = (showname ? 999 : 99999);
519 portno = rt.nas_port;
522 printf((rawoutput == 0? rfmt1: rfmt1r),
524 showcid ? rt.caller_id :
525 (showsid? session_id : fullname(rt.login)),
526 proto(rt.proto, rt.porttype),
529 nas_name3(nasname, sizeof(nasname), rt.nas_address),
530 hostname(othername, sizeof(othername), rt.framed_address), eol);
532 printf((rawoutput == 0? rfmt2: rfmt2r),
535 proto(rt.proto, rt.porttype),
537 nas_name3(nasname, sizeof(nasname), rt.nas_address),
538 hostname(othername, sizeof(othername), rt.framed_address),