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 = "stdout";
75 int proxy_synchronous = TRUE;
76 const char *radius_dir = NULL;
77 const char *radacct_dir = NULL;
78 const char *radlib_dir = NULL;
81 uint32_t myip = INADDR_ANY;
82 int proxy_retry_delay = RETRY_DELAY;
83 int proxy_retry_count = RETRY_COUNT;
84 int log_stripped_names;
85 struct main_config_t mainconfig;
88 * A mapping of configuration file names to internal variables
90 static CONF_PARSER server_config[] = {
91 { NULL, -1, 0, NULL, NULL }
97 static FILE *safe_popen(const char *cmd, const char *mode)
103 * Change all suspect characters into a space.
105 strncpy(buf, cmd, sizeof(buf));
106 buf[sizeof(buf) - 1] = 0;
107 for (p = buf; *p; p++) {
110 if (strchr("@%-_ \t+:,./", *p) == NULL)
114 return popen(buf, mode);
118 * Print a file from FINGER_DIR. If the file is executable,
119 * execute it instead. Return 0 if succesfull.
121 static int ffile(const char *arg)
128 sprintf(fn, "%s/%.32s", FINGER_DIR, arg);
129 if (access(fn, X_OK) == 0) {
131 sprintf(fn, "exec %s/%.32s 2>&1", FINGER_DIR, arg);
132 fp = safe_popen(fn, "r");
133 } else fp = fopen(fn, "r");
135 if (fp == NULL) return -1;
137 while(fgets(fn, 1024, fp)) {
138 if ((s = strchr(fn, '\n')) != NULL)
140 fprintf(stdout, "%s\r\n", fn);
152 * Execute the system finger and translate LF to CRLF.
154 static void sys_finger(const char *l)
163 sprintf(fn, "exec %s %s", SYS_FINGER, l);
164 if ((fp = safe_popen(fn, "r")) == NULL) {
165 printf("popen: %s\r\n", strerror(errno));
169 while(fgets(fn, 1024, fp)) {
170 if ((p = strchr(fn, '\n')) != NULL)
172 fprintf(stdout, "%s\r\n", fn);
180 * Get fullname of a user.
182 static char *fullname(char *username)
187 if ((pwd = getpwnam(username)) != NULL) {
188 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
189 return pwd->pw_gecos;
195 * Return protocol type.
197 static const char *proto(int id, int porttype)
202 if (!strchr("ASITX", porttype))
205 sprintf(buf, "SLP %c", porttype);
207 sprintf(buf, "PPP %c", porttype);
209 sprintf(buf, "shl %c", porttype);
212 if (id == 'S') return "SLIP";
213 if (id == 'P') return "PPP";
218 * Return a time in the form day hh:mm
220 static char *dotime(time_t t)
225 strncpy(s + 4, s + 11, 5);
228 strncpy(s + 4, s + 8, 8);
237 * See how long a tty has been idle.
239 char *idletime(char *line)
248 strcpy(tty, "/dev/");
251 strncat(tty, line, 10);
255 if (stat(tty, &st) == 0) {
265 sprintf(tmp, "%dd", days);
267 sprintf(tmp, "%2d:%02d", hr, min);
277 static const char *ttyshort(char *tty)
281 if (tty[0] == '/') tty += 5;
283 if (strncmp(tty, "tty", 3) == 0) {
284 if (tty[3] >= '0' && tty[3] <= '9')
285 sprintf(tmp, "v%.14s", tty + 3);
287 sprintf(tmp, "%.15s", tty + 3);
290 if (strncmp(tty, "vc", 2) == 0) {
291 sprintf(tmp, "v.14%s", tty + 2);
294 if (strncmp(tty, "cu", 2) == 0) {
302 * Print address of NAS.
304 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
306 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
308 return ip_hostname(buf, buflen, ipaddr);
313 * Print usage message and exit.
315 static void usage(void)
317 fprintf(stderr, "Usage: radwho [-lhfnsipcr]\n");
318 fprintf(stderr, " -l: show local (shell) users too\n");
319 fprintf(stderr, " -h: hide shell users from radius\n");
320 fprintf(stderr, " -f: give fingerd output\n");
321 fprintf(stderr, " -n: no full name\n");
322 fprintf(stderr, " -s: show full name\n");
323 fprintf(stderr, " -i: show session ID\n");
324 fprintf(stderr, " -p: show port type\n");
325 fprintf(stderr, " -c: show caller ID, if available\n");
326 fprintf(stderr, " -r: output as raw data\n");
332 * Main program, either pmwho or fingerd.
334 int main(int argc, char **argv)
354 radius_dir = strdup(RADIUS_DIR);
356 while((c = getopt(argc, argv, "flhnsipcr")) != EOF) switch(c) {
391 /* Read radiusd.conf */
392 if(read_radius_conf_file() < 0) {
393 fprintf(stderr, "%s: Errors reading radiusd.conf\n", argv[0]);
397 cs = cf_section_find(NULL);
399 fprintf(stderr, "%s: No configuration information in radiusd.conf!\n",
403 cf_section_parse(cs, NULL, server_config);
406 * See if we are "fingerd".
408 if (strstr(argv[0], "fingerd")) {
411 if (showname < 0) showname = 0;
413 if (showname < 0) showname = 1;
417 * Read first line of the input.
419 fgets(inbuf, 128, stdin);
421 while(*p == ' ' || *p == '\t') p++;
422 if (*p == '/' && *(p + 1)) p += 2;
423 while(*p == ' ' || *p == '\t') p++;
424 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
429 * See if we fingered a specific user.
432 if (*p) sys_finger(p);
435 if (showlocal && (fp = fopen(UTMP_FILE, "r"))) {
438 fputs(showname ? hdr1 : hdr2, stdout);
444 * Show the logged in UNIX users.
446 gethostname(myname, 128);
447 while(fread(&ut, sizeof(ut), 1, fp) == 1) {
449 if (ut.ut_user[0] && ut.ut_line[0] &&
450 ut.ut_type == USER_PROCESS) {
452 if (ut.ut_user[0] && ut.ut_line[0]) {
456 printf((rawoutput == 0? ufmt1: ufmt1r),
458 fullname(ut.ut_name),
460 ttyshort(ut.ut_line),
469 printf((rawoutput==0? ufmt2:ufmt2r),
471 ttyshort(ut.ut_line),
487 * Show the users logged in on the terminal server(s).
489 if ((fp = fopen(RADUTMP, "r")) == NULL)
493 fputs(showname ? hdr1 : hdr2, stdout);
497 while(fread(&rt, sizeof(rt), 1, fp) == 1) {
498 if (rt.type == P_LOGIN) {
500 * We don't show shell users if we are
501 * fingerd, as we have done that above.
503 if (hideshell && !strchr("PCS", rt.proto))
506 sprintf(session_id, "%.8s", rt.session_id);
508 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
510 portno = (showname ? 999 : 99999);
513 portno = rt.nas_port;
516 printf((rawoutput == 0? rfmt1: rfmt1r),
518 showcid ? rt.caller_id :
519 (showsid? session_id : fullname(rt.login)),
520 proto(rt.proto, rt.porttype),
523 nas_name(rt.nas_address),
524 hostname(othername, sizeof(othername), rt.framed_address), eol);
526 printf((rawoutput == 0? rfmt2: rfmt2r),
529 proto(rt.proto, rt.porttype),
531 nas_name(rt.nas_address),
532 hostname(othername, sizeof(othername), rt.framed_address), eol);