2 * radwho Show who is logged in on the terminal servers.
3 * Can also be installed as fingerd on the UNIX
4 * machine RADIUS runs on.
6 * Version: @(#)radwho 1.25 27-Apr-1999 miquels@cistron.nl
8 * Patch: 22-Apr-1999 promera@cistron.nl
9 * Added support for raw, comma seperated output
21 #include <sys/utsname.h>
24 #include <netinet/in.h>
35 * FIXME: put in header file.
37 #define SYS_FINGER "/usr/bin/finger"
38 #define FINGER_DIR "/usr/local/lib/finger"
41 * Header above output and format.
44 "Login Name What TTY When From Location";
45 char *ufmt1 = "%-10.10s %-17.17s %-5.5s %-4.4s %-9.9s %-9.9s %-.16s%s";
46 char *ufmt1r = "%s,%s,%s,%s,%s,%s,%s%s";
47 char *rfmt1 = "%-10.10s %-17.17s %-5.5s S%-3d %-9.9s %-9.9s %-.19s%s";
48 char *rfmt1r = "%s,%s,%s,S%d,%s,%s,%s%s";
51 "Login Port What When From Location";
52 char *ufmt2 = "%-10.10s %-7.7d %-9.9s %-13.13s %-10.10s %-.16s%s";
53 char *ufmt2r = "%s,%d,%s,%s,%s,%s%s";
54 char *rfmt2 = "%-10.10s S%-5d %-9.9s %-13.13s %-10.10s %-.28s%s";
55 char *rfmt2r = "%s,S%d,%s,%s,%s,%s%s";
66 FILE *safe_popen(char *cmd, char *mode)
72 * Change all suspect characters into a space.
74 strncpy(buf, cmd, sizeof(buf));
75 buf[sizeof(buf) - 1] = 0;
76 for (p = buf; *p; p++) {
79 if (strchr("@%-_ \t+:,./", *p) == NULL)
83 return popen(buf, mode);
87 * Read the naslist file.
89 static NAS *my_read_naslist_file(char *file)
100 if ((fp = fopen(file, "r")) == NULL) {
101 fprintf(stderr, "cannot open %s\n", file);
104 while(fgets(buffer, 256, fp) != NULL) {
106 if (buffer[0] == '#' || buffer[0] == '\n')
109 if (sscanf(buffer, "%s%s%s", hostnm, shortnm, nastype) < 2) {
110 fprintf(stderr, "%s[%d]: syntax error\n", file, lineno);
113 if ((c = malloc(sizeof(NAS))) == NULL) {
114 fprintf(stderr, "%s[%d]: out of memory\n",
119 c->ipaddr = ip_getaddr(hostnm);
120 strcpy(c->nastype, nastype);
121 strcpy(c->shortname, shortnm);
122 strcpy(c->longname, ip_hostname(c->ipaddr));
134 * Print a file from FINGER_DIR. If the file is executable,
135 * execute it instead. Return 0 if succesfull.
144 sprintf(fn, "%s/%.32s", FINGER_DIR, arg);
145 if (access(fn, X_OK) == 0) {
147 sprintf(fn, "exec %s/%.32s 2>&1", FINGER_DIR, arg);
148 fp = safe_popen(fn, "r");
149 } else fp = fopen(fn, "r");
151 if (fp == NULL) return -1;
153 while(fgets(fn, 1024, fp)) {
154 if ((s = strchr(fn, '\n')) != NULL)
156 fprintf(stdout, "%s\r\n", fn);
168 * Execute the system finger and translate LF to CRLF.
170 void sys_finger(char *l)
179 sprintf(fn, "exec %s %s", SYS_FINGER, l);
180 if ((fp = safe_popen(fn, "r")) == NULL) {
181 printf("popen: %s\r\n", strerror(errno));
185 while(fgets(fn, 1024, fp)) {
186 if ((p = strchr(fn, '\n')) != NULL)
188 fprintf(stdout, "%s\r\n", fn);
196 * Get fullname of a user.
198 char *fullname(char *login)
203 if ((pwd = getpwnam(login)) != NULL) {
204 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
205 return pwd->pw_gecos;
211 * Return protocol type.
213 char *proto(int id, int porttype)
218 if (!strchr("ASITX", porttype))
221 sprintf(buf, "SLP %c", porttype);
223 sprintf(buf, "PPP %c", porttype);
225 sprintf(buf, "shl %c", porttype);
228 if (id == 'S') return "SLIP";
229 if (id == 'P') return "PPP";
234 * Return a time in the form day hh:mm
236 char *dotime(time_t t)
241 strncpy(s + 4, s + 11, 5);
244 strncpy(s + 4, s + 8, 8);
253 * See how long a tty has been idle.
255 char *idletime(char *line)
264 strcpy(tty, "/dev/");
270 if (stat(tty, &st) == 0) {
280 sprintf(tmp, "%dd", days);
282 sprintf(tmp, "%2d:%02d", hr, min);
292 char *ttyshort(char *tty)
296 if (tty[0] == '/') tty += 5;
298 if (strncmp(tty, "tty", 3) == 0) {
299 if (tty[3] >= '0' && tty[3] <= '9')
300 sprintf(tmp, "v%s", tty + 3);
302 sprintf(tmp, "%s", tty + 3);
305 if (strncmp(tty, "vc", 2) == 0) {
306 sprintf(tmp, "v%s", tty + 2);
309 if (strncmp(tty, "cu", 2) == 0) {
319 char *nasname(UINT4 ipaddr)
323 for(cl = naslist; cl; cl = cl->next)
324 if (cl->ipaddr == ipaddr)
328 if (cl->shortname[0])
329 return cl->shortname;
335 * Print address of NAS.
337 char *hostname(UINT4 ipaddr)
339 if (ipaddr == 0 || ipaddr == (UINT4)-1 || ipaddr == (UINT4)-2)
341 return ip_hostname(ipaddr);
346 * Print usage message and exit.
350 fprintf(stderr, "Usage: radwho [-lhfnsipcr]\n");
351 fprintf(stderr, " -l: show local (shell) users too\n");
352 fprintf(stderr, " -h: hide shell users from radius\n");
353 fprintf(stderr, " -f: give fingerd output\n");
354 fprintf(stderr, " -n: no full name\n");
355 fprintf(stderr, " -s: show full name\n");
356 fprintf(stderr, " -i: show session ID\n");
357 fprintf(stderr, " -p: show port type\n");
358 fprintf(stderr, " -c: show caller ID, if available\n");
359 fprintf(stderr, " -r: output as raw data\n");
365 * Main program, either pmwho or fingerd.
367 int main(int argc, char **argv)
384 while((c = getopt(argc, argv, "flhnsipcr")) != EOF) switch(c) {
420 * Read the "naslist" file.
422 sprintf(inbuf, "%s/%s", RADIUS_DIR, RADIUS_NASLIST);
423 if ((naslist = my_read_naslist_file(inbuf)) == NULL)
427 * See if we are "fingerd".
429 if (strstr(argv[0], "fingerd")) {
432 if (showname < 0) showname = 0;
434 if (showname < 0) showname = 1;
438 * Read first line of the input.
440 fgets(inbuf, 128, stdin);
442 while(*p == ' ' || *p == '\t') p++;
443 if (*p == '/' && *(p + 1)) p += 2;
444 while(*p == ' ' || *p == '\t') p++;
445 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
450 * See if we fingered a specific user.
453 if (*p) sys_finger(p);
456 if (showlocal && (fp = fopen(UTMP_FILE, "r"))) {
459 fputs(showname ? hdr1 : hdr2, stdout);
465 * Show the logged in UNIX users.
467 gethostname(myname, 128);
468 while(fread(&ut, sizeof(ut), 1, fp) == 1) {
470 if (ut.ut_user[0] && ut.ut_line[0] &&
471 ut.ut_type == USER_PROCESS) {
473 if (ut.ut_user[0] && ut.ut_line[0]) {
476 printf((rawoutput == 0? ufmt1: ufmt1r),
478 fullname(ut.ut_name),
480 ttyshort(ut.ut_line),
489 printf((rawoutput==0? ufmt2:ufmt2r),
491 ttyshort(ut.ut_line),
506 * Show the users logged in on the terminal server(s).
508 if ((fp = fopen(RADUTMP, "r")) == NULL)
512 fputs(showname ? hdr1 : hdr2, stdout);
516 while(fread(&rt, sizeof(rt), 1, fp) == 1) {
517 if (rt.type == P_LOGIN) {
519 * We don't show shell users if we are
520 * fingerd, as we have done that above.
522 if (hideshell && !strchr("PCS", rt.proto))
525 sprintf(session_id, "%.8s", rt.session_id);
528 printf((rawoutput == 0? rfmt1: rfmt1r),
530 showcid ? rt.caller_id :
531 (showsid? session_id : fullname(rt.login)),
532 proto(rt.proto, rt.porttype),
535 nasname(rt.nas_address),
536 hostname(rt.framed_address), eol);
538 printf((rawoutput == 0? rfmt2: rfmt2r),
541 proto(rt.proto, rt.porttype),
543 nasname(rt.nas_address),
544 hostname(rt.framed_address), eol);