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>
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
48 * FIXME: put in header file.
50 #define SYS_FINGER "/usr/bin/finger"
51 #define FINGER_DIR "/usr/local/lib/finger"
54 * Header above output and format.
56 static const char *hdr1 =
57 "Login Name What TTY When From Location";
58 static const char *ufmt1 = "%-10.10s %-17.17s %-5.5s %-4.4s %-9.9s %-9.9s %-.16s%s";
59 static const char *ufmt1r = "%s,%s,%s,%s,%s,%s,%s%s";
60 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3d %-9.9s %-9.9s %-.19s%s";
61 static const char *rfmt1r = "%s,%s,%s,%s%d,%s,%s,%s%s";
63 static const char *hdr2 =
64 "Login Port What When From Location";
65 static const char *ufmt2 = "%-10.10s %-6.6d %-7.7s %-13.13s %-10.10s %-.16s%s";
66 static const char *ufmt2r = "%s,%d,%s,%s,%s,%s%s";
67 static const char *rfmt2 = "%-10.10s %s%-5d %-6.6s %-13.13s %-10.10s %-.28s%s";
68 static const char *rfmt2r = "%s,%s%d,%s,%s,%s,%s%s";
70 static const char *eol = "\n";
71 static int showname = -1;
72 static int showptype = 0;
73 static int showcid = 0;
75 const char *progname = "radwho";
76 const char *radlog_dir = NULL;
77 radlog_dest_t radlog_dest = RADLOG_FILES;
78 const char *radutmp_file = NULL;
80 int proxy_synchronous = TRUE;
81 int proxy_fallback = FALSE;
82 const char *radius_dir = NULL;
83 const char *radacct_dir = NULL;
84 const char *radlib_dir = NULL;
87 uint32_t myip = INADDR_ANY;
88 int proxy_retry_delay = RETRY_DELAY;
89 int proxy_retry_count = RETRY_COUNT;
91 int log_stripped_names;
92 struct main_config_t mainconfig;
95 * A mapping of configuration file names to internal variables
97 static CONF_PARSER server_config[] = {
98 { NULL, -1, 0, NULL, NULL }
104 static FILE *safe_popen(const char *cmd, const char *mode)
110 * Change all suspect characters into a space.
112 strncpy(buf, cmd, sizeof(buf));
113 buf[sizeof(buf) - 1] = 0;
114 for (p = buf; *p; p++) {
117 if (strchr("@%-_ \t+:,./", *p) == NULL)
121 return popen(buf, mode);
125 * Print a file from FINGER_DIR. If the file is executable,
126 * execute it instead. Return 0 if succesfull.
128 static int ffile(const char *arg)
135 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
136 if (access(fn, X_OK) == 0) {
138 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
139 fp = safe_popen(fn, "r");
140 } else fp = fopen(fn, "r");
145 while(fgets(fn, 1024, fp)) {
146 if ((s = strchr(fn, '\n')) != NULL)
148 fprintf(stdout, "%s\r\n", fn);
160 * Execute the system finger and translate LF to CRLF.
162 static void sys_finger(const char *l)
171 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
172 if ((fp = safe_popen(fn, "r")) == NULL) {
173 printf("popen: %s\r\n", strerror(errno));
177 while(fgets(fn, 1024, fp)) {
178 if ((p = strchr(fn, '\n')) != NULL)
180 fprintf(stdout, "%s\r\n", fn);
188 * Get fullname of a user.
190 static char *fullname(char *username)
195 if ((pwd = getpwnam(username)) != NULL) {
196 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
197 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 strncpy(s + 4, s + 11, 5);
236 strncpy(s + 4, s + 8, 8);
245 * See how long a tty has been idle.
247 char *idletime(char *line)
256 strcpy(tty, "/dev/");
259 strncat(tty, line, 10);
263 if (stat(tty, &st) == 0) {
273 snprintf(tmp, sizeof(tmp), "%dd", days);
275 snprintf(tmp, sizeof(tmp), "%2d:%02d", hr, min);
285 static const char *ttyshort(char *tty)
289 if (tty[0] == '/') tty += 5;
291 if (strncmp(tty, "tty", 3) == 0) {
292 if (tty[3] >= '0' && tty[3] <= '9')
293 snprintf(tmp, sizeof(tmp), "v%.14s", tty + 3);
295 snprintf(tmp, sizeof(tmp), "%.15s", tty + 3);
298 if (strncmp(tty, "vc", 2) == 0) {
299 snprintf(tmp, sizeof(tmp), "v.14%s", tty + 2);
302 if (strncmp(tty, "cu", 2) == 0) {
310 * Print address of NAS.
312 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
314 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
316 return ip_hostname(buf, buflen, ipaddr);
321 * Print usage message and exit.
323 static void usage(void)
325 fprintf(stderr, "Usage: radwho [-lhfnsipcr]\n");
326 fprintf(stderr, " -l: show local (shell) users too\n");
327 fprintf(stderr, " -h: hide shell users from radius\n");
328 fprintf(stderr, " -f: give fingerd output\n");
329 fprintf(stderr, " -n: no full name\n");
330 fprintf(stderr, " -s: show full name\n");
331 fprintf(stderr, " -i: show session ID\n");
332 fprintf(stderr, " -p: show port type\n");
333 fprintf(stderr, " -c: show caller ID, if available\n");
334 fprintf(stderr, " -r: output as raw data\n");
340 * Main program, either pmwho or fingerd.
342 int main(int argc, char **argv)
353 char session_id[sizeof(rt.session_id)+1];
363 radius_dir = strdup(RADIUS_DIR);
364 radutmp_file = strdup(RADUTMP);
366 while((c = getopt(argc, argv, "flhnsipcr")) != EOF) switch(c) {
401 /* Read radiusd.conf */
402 if(read_radius_conf_file() < 0) {
403 fprintf(stderr, "%s: Errors reading radiusd.conf\n", argv[0]);
407 cs = cf_section_find(NULL);
409 fprintf(stderr, "%s: No configuration information in radiusd.conf!\n",
413 cf_section_parse(cs, NULL, server_config);
416 * See if we are "fingerd".
418 if (strstr(argv[0], "fingerd")) {
421 if (showname < 0) showname = 0;
423 if (showname < 0) showname = 1;
427 * Read first line of the input.
429 fgets(inbuf, 128, stdin);
431 while(*p == ' ' || *p == '\t') p++;
432 if (*p == '/' && *(p + 1)) p += 2;
433 while(*p == ' ' || *p == '\t') p++;
434 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
439 * See if we fingered a specific user.
442 if (*p) sys_finger(p);
445 if (showlocal && (fp = fopen(radutmp_file, "r"))) {
448 fputs(showname ? hdr1 : hdr2, stdout);
454 * Show the logged in UNIX users.
456 gethostname(myname, 128);
457 while(fread(&ut, sizeof(ut), 1, fp) == 1) {
459 if (ut.ut_user[0] && ut.ut_line[0] &&
460 ut.ut_type == USER_PROCESS) {
462 if (ut.ut_user[0] && ut.ut_line[0]) {
467 # define UT_TIME ut_xtime
469 # define UT_TIME ut_time
472 printf((rawoutput == 0? ufmt1: ufmt1r),
474 fullname(ut.ut_name),
476 ttyshort(ut.ut_line),
481 printf((rawoutput==0? ufmt2:ufmt2r),
483 ttyshort(ut.ut_line),
495 * Show the users logged in on the terminal server(s).
497 if ((fp = fopen(RADUTMP, "r")) == NULL)
501 fputs(showname ? hdr1 : hdr2, stdout);
505 while(fread(&rt, sizeof(rt), 1, fp) == 1) {
506 if (rt.type == P_LOGIN) {
508 * We don't show shell users if we are
509 * fingerd, as we have done that above.
511 if (hideshell && !strchr("PCS", rt.proto))
514 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
515 session_id[sizeof(rt.session_id)] = 0;
517 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
519 portno = (showname ? 999 : 99999);
522 portno = rt.nas_port;
525 printf((rawoutput == 0? rfmt1: rfmt1r),
527 showcid ? rt.caller_id :
528 (showsid? session_id : fullname(rt.login)),
529 proto(rt.proto, rt.porttype),
532 nas_name3(nasname, sizeof(nasname), rt.nas_address),
533 hostname(othername, sizeof(othername), rt.framed_address), eol);
535 printf((rawoutput == 0? rfmt2: rfmt2r),
538 proto(rt.proto, rt.porttype),
540 nas_name3(nasname, sizeof(nasname), rt.nas_address),
541 hostname(othername, sizeof(othername), rt.framed_address),