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 const char *radius_dir = NULL;
81 const char *radacct_dir = NULL;
82 const char *radlib_dir = NULL;
85 uint32_t myip = INADDR_ANY;
86 int log_stripped_names;
88 struct radutmp_config_t {
92 static CONF_PARSER module_config[] = {
93 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
94 { NULL, -1, 0, NULL, NULL }
100 static FILE *safe_popen(const char *cmd, const char *mode)
106 * Change all suspect characters into a space.
108 strncpy(buf, cmd, sizeof(buf));
109 buf[sizeof(buf) - 1] = 0;
110 for (p = buf; *p; p++) {
111 if (isalnum((int) *p))
113 if (strchr("@%-_ \t+:,./", *p) == NULL)
117 return popen(buf, mode);
121 * Print a file from FINGER_DIR. If the file is executable,
122 * execute it instead. Return 0 if succesfull.
124 static int ffile(const char *arg)
131 snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
132 if (access(fn, X_OK) == 0) {
134 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
135 fp = safe_popen(fn, "r");
136 } else fp = fopen(fn, "r");
141 while(fgets(fn, 1024, fp)) {
142 if ((s = strchr(fn, '\n')) != NULL)
144 fprintf(stdout, "%s\r\n", fn);
156 * Execute the system finger and translate LF to CRLF.
158 static void sys_finger(const char *l)
167 snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
168 if ((fp = safe_popen(fn, "r")) == NULL) {
169 printf("popen: %s\r\n", strerror(errno));
173 while(fgets(fn, 1024, fp)) {
174 if ((p = strchr(fn, '\n')) != NULL)
176 fprintf(stdout, "%s\r\n", fn);
184 * Get fullname of a user.
186 static char *fullname(char *username)
191 if ((pwd = getpwnam(username)) != NULL) {
192 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
193 return pwd->pw_gecos;
199 * Return protocol type.
201 static const char *proto(int id, int porttype)
206 if (!strchr("ASITX", porttype))
209 snprintf(buf, sizeof(buf), "SLP %c", porttype);
211 snprintf(buf, sizeof(buf), "PPP %c", porttype);
213 snprintf(buf, sizeof(buf), "shl %c", porttype);
216 if (id == 'S') return "SLIP";
217 if (id == 'P') return "PPP";
222 * Return a time in the form day hh:mm
224 static char *dotime(time_t t)
229 strncpy(s + 4, s + 11, 5);
232 strncpy(s + 4, s + 8, 8);
241 * See how long a tty has been idle.
243 char *idletime(char *line)
252 strcpy(tty, "/dev/");
255 strncat(tty, line, 10);
259 if (stat(tty, &st) == 0) {
269 snprintf(tmp, sizeof(tmp), "%dd", days);
271 snprintf(tmp, sizeof(tmp), "%2d:%02d", hr, min);
281 static const char *ttyshort(char *tty)
285 if (tty[0] == '/') tty += 5;
287 if (strncmp(tty, "tty", 3) == 0) {
288 if (tty[3] >= '0' && tty[3] <= '9')
289 snprintf(tmp, sizeof(tmp), "v%.14s", tty + 3);
291 snprintf(tmp, sizeof(tmp), "%.15s", tty + 3);
294 if (strncmp(tty, "vc", 2) == 0) {
295 snprintf(tmp, sizeof(tmp), "v.14%s", tty + 2);
298 if (strncmp(tty, "cu", 2) == 0) {
306 * Print address of NAS.
308 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
310 if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
312 return ip_hostname(buf, buflen, ipaddr);
317 * Print usage message and exit.
319 static void usage(void)
321 fprintf(stderr, "Usage: radwho [-d raddb] [-lhfnsipcr]\n");
322 fprintf(stderr, " -d: set the raddb directory (default is %s)\n",
324 fprintf(stderr, " -l: show local (shell) users too\n");
325 fprintf(stderr, " -h: hide shell users from radius\n");
326 fprintf(stderr, " -f: give fingerd output\n");
327 fprintf(stderr, " -n: no full name\n");
328 fprintf(stderr, " -s: show full name\n");
329 fprintf(stderr, " -i: show session ID\n");
330 fprintf(stderr, " -p: show port type\n");
331 fprintf(stderr, " -c: show caller ID, if available\n");
332 fprintf(stderr, " -r: output as raw data\n");
338 * Main program, either pmwho or fingerd.
340 int main(int argc, char **argv)
342 CONF_SECTION *maincs, *cs;
351 char session_id[sizeof(rt.session_id)+1];
361 radius_dir = strdup(RADIUS_DIR);
363 while((c = getopt(argc, argv, "d:flhnsipcr")) != EOF) switch(c) {
365 if (radius_dir) free(radius_dir);
366 radius_dir = strdup(optarg);
402 /* Read radiusd.conf */
403 if ((maincs = read_radius_conf_file()) == NULL) {
404 fprintf(stderr, "%s: Errors reading radiusd.conf\n", argv[0]);
408 /* Read the radutmp section of radiusd.conf */
409 cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
411 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
416 cf_section_parse(cs, NULL, module_config);
418 /* Assign the correct path for the radutmp file */
419 radutmp_file = radutmpconfig.radutmp_fn;
422 * See if we are "fingerd".
424 if (strstr(argv[0], "fingerd")) {
427 if (showname < 0) showname = 0;
429 if (showname < 0) showname = 1;
433 * Read first line of the input.
435 fgets(inbuf, 128, stdin);
437 while(*p == ' ' || *p == '\t') p++;
438 if (*p == '/' && *(p + 1)) p += 2;
439 while(*p == ' ' || *p == '\t') p++;
440 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
445 * See if we fingered a specific user.
448 if (*p) sys_finger(p);
451 if (showlocal && (fp = fopen(radutmp_file, "r"))) {
454 fputs(showname ? hdr1 : hdr2, stdout);
460 * Show the logged in UNIX users.
462 gethostname(myname, 128);
463 while(fread(&ut, sizeof(ut), 1, fp) == 1) {
465 if (ut.ut_user[0] && ut.ut_line[0] &&
466 ut.ut_type == USER_PROCESS) {
468 if (ut.ut_user[0] && ut.ut_line[0]) {
473 # define UT_TIME ut_xtime
475 # define UT_TIME ut_time
478 printf((rawoutput == 0? ufmt1: ufmt1r),
480 fullname(ut.ut_name),
482 ttyshort(ut.ut_line),
487 printf((rawoutput==0? ufmt2:ufmt2r),
489 ttyshort(ut.ut_line),
501 * Show the users logged in on the terminal server(s).
503 if ((fp = fopen(radutmp_file, "r")) == NULL)
507 fputs(showname ? hdr1 : hdr2, stdout);
511 while(fread(&rt, sizeof(rt), 1, fp) == 1) {
512 if (rt.type == P_LOGIN) {
514 * We don't show shell users if we are
515 * fingerd, as we have done that above.
517 if (hideshell && !strchr("PCS", rt.proto))
520 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
521 session_id[sizeof(rt.session_id)] = 0;
523 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
525 portno = (showname ? 999 : 99999);
528 portno = rt.nas_port;
531 printf((rawoutput == 0? rfmt1: rfmt1r),
533 showcid ? rt.caller_id :
534 (showsid? session_id : fullname(rt.login)),
535 proto(rt.proto, rt.porttype),
538 nas_name3(nasname, sizeof(nasname), rt.nas_address),
539 hostname(othername, sizeof(othername), rt.framed_address), eol);
541 printf((rawoutput == 0? rfmt2: rfmt2r),
544 proto(rt.proto, rt.porttype),
546 nas_name3(nasname, sizeof(nasname), rt.nas_address),
547 hostname(othername, sizeof(othername), rt.framed_address),