08bbf265d262b1a3c019d04af6b923a1f637ce97
[freeradius.git] / src / main / radwho.c
1 /*
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.
5  *
6  * Version:     $Id$
7  *
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.
12  *
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.
17  *
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
21  *
22  * Copyright 2000  The FreeRADIUS server project
23  * Copyright 2000  Alan DeKok <aland@ox.org>
24  */
25
26 static const char rcsid[] =
27 "$Id$";
28
29 #include "autoconf.h"
30 #include "libradius.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <pwd.h>
35 #include <sys/stat.h>
36 #include <sys/utsname.h>
37 #include <ctype.h>
38
39 #include "sysutmp.h"
40 #include "radutmp.h"
41 #include "radiusd.h"
42 #include "conffile.h"
43
44 /*
45  *      FIXME: put in header file.
46  */
47 #define SYS_FINGER "/usr/bin/finger"
48 #define FINGER_DIR "/usr/local/lib/finger"
49
50 /*
51  *      Header above output and format.
52  */
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";
59
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";
66
67 static const char *eol = "\n";
68 static int showname = -1;
69 static int showptype = 0;
70 static int showcid = 0;
71 int debug_flag = 0;
72 const char *progname = "radwho";
73 const char *radlog_dir = "stdout";
74
75 int proxy_synchronous = TRUE;
76 const char *radius_dir = NULL;
77 const char *radacct_dir = NULL;
78 const char *radlib_dir = NULL;
79 int auth_port = 0;
80 int acct_port;
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;
86
87 /*
88  *      A mapping of configuration file names to internal variables
89  */
90 static CONF_PARSER server_config[] = {
91         { NULL, -1, 0, NULL, NULL }
92 };
93
94 /*
95  *      Safe popen. Ugh.
96  */
97 static FILE *safe_popen(const char *cmd, const char *mode)
98 {
99         char            *p;
100         char            buf[1024];
101
102         /*
103          *      Change all suspect characters into a space.
104          */
105         strncpy(buf, cmd, sizeof(buf));
106         buf[sizeof(buf) - 1] = 0;
107         for (p = buf; *p; p++) {
108                 if (isalnum(*p))
109                         continue;
110                 if (strchr("@%-_ \t+:,./", *p) == NULL)
111                         *p = ' ';
112         }
113
114         return popen(buf, mode);
115 }
116
117 /*
118  *      Print a file from FINGER_DIR. If the file is executable,
119  *      execute it instead. Return 0 if succesfull.
120  */
121 static int ffile(const char *arg)
122 {
123         FILE *fp;
124         char fn[1024];
125         int p = 0;
126         char *s;
127
128         snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
129         if (access(fn, X_OK) == 0) {
130                 p = 1;
131                 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
132                 fp = safe_popen(fn, "r");
133         } else fp = fopen(fn, "r");
134
135         if (fp == NULL) 
136                 return -1;
137
138         while(fgets(fn, 1024, fp)) {
139                 if ((s = strchr(fn, '\n')) != NULL)
140                         *s = 0;
141                 fprintf(stdout, "%s\r\n", fn);
142         }
143         if (p)
144                 pclose(fp);
145         else
146                 fclose(fp);
147         fflush(stdout);
148         return 0;
149 }
150
151
152 /*
153  *      Execute the system finger and translate LF to CRLF.
154  */
155 static void sys_finger(const char *l)
156 {
157         FILE *fp;
158         char fn[1024];
159         char *p;
160
161         if (ffile(l) == 0)
162                 exit(0);
163
164         snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
165         if ((fp = safe_popen(fn, "r")) == NULL) {
166                 printf("popen: %s\r\n", strerror(errno));
167                 exit(1);
168         }
169
170         while(fgets(fn, 1024, fp)) {
171                 if ((p = strchr(fn, '\n')) != NULL)
172                         *p = 0;
173                 fprintf(stdout, "%s\r\n", fn);
174         }
175         pclose(fp);
176         exit(0);
177 }
178
179
180 /*
181  *      Get fullname of a user.
182  */
183 static char *fullname(char *username)
184 {
185         struct passwd *pwd;
186         char *s;
187
188         if ((pwd = getpwnam(username)) != NULL) {
189                 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
190                 return pwd->pw_gecos;
191         }
192         return username;
193 }
194
195 /*
196  *      Return protocol type.
197  */
198 static const char *proto(int id, int porttype)
199 {
200         static char buf[8];
201
202         if (showptype) {
203                 if (!strchr("ASITX", porttype))
204                         porttype = ' ';
205                 if (id == 'S')
206                         snprintf(buf, sizeof(buf), "SLP %c", porttype);
207                 else if (id == 'P')
208                         snprintf(buf, sizeof(buf), "PPP %c", porttype);
209                 else
210                         snprintf(buf, sizeof(buf), "shl %c", porttype);
211                 return buf;
212         }
213         if (id == 'S') return "SLIP";
214         if (id == 'P') return "PPP";
215         return "shell";
216 }
217
218 /*
219  *      Return a time in the form day hh:mm
220  */
221 static char *dotime(time_t t)
222 {
223         char *s = ctime(&t);
224
225         if (showname) {
226                 strncpy(s + 4, s + 11, 5);
227                 s[9] = 0;
228         } else {
229                 strncpy(s + 4, s + 8, 8);
230                 s[12] = 0;
231         }
232
233         return s;
234 }
235
236 #if 0 /*UNUSED*/
237 /*
238  *      See how long a tty has been idle.
239  */
240 char *idletime(char *line)
241 {
242         char tty[16];
243         static char tmp[8];
244         time_t t;
245         struct stat st;
246         int hr, min, days;
247
248         if (line[0] == '/')
249                 strcpy(tty, "/dev/");
250         else
251                 tty[0] = 0;
252         strncat(tty, line, 10);
253         tty[15] = 0;
254
255         tmp[0] = 0;
256         if (stat(tty, &st) == 0) {
257                 time(&t);
258                 t -= st.st_mtime;
259                 if (t >= 60) {
260                         min = (t / 60);
261                         hr = min / 24;
262                         days = hr / 24;
263                         min %= 60;
264                         hr %= 24;
265                         if (days > 0)
266                                 snprintf(tmp, sizeof(tmp), "%dd", days);
267                         else
268                                 snprintf(tmp, sizeof(tmp), "%2d:%02d", hr, min);
269                 }
270         }
271         return tmp;
272 }
273 #endif
274
275 /*
276  *      Shorten tty name.
277  */
278 static const char *ttyshort(char *tty)
279 {
280         static char tmp[16];
281
282         if (tty[0] == '/') tty += 5;
283
284         if (strncmp(tty, "tty", 3) == 0) {
285                 if (tty[3] >= '0' && tty[3] <= '9')
286                         snprintf(tmp, sizeof(tmp), "v%.14s", tty + 3);
287                 else
288                         snprintf(tmp, sizeof(tmp), "%.15s", tty + 3);
289                 return tmp;
290         }
291         if (strncmp(tty, "vc", 2) == 0) {
292                 snprintf(tmp, sizeof(tmp), "v.14%s", tty + 2);
293                 return tmp;
294         }
295         if (strncmp(tty, "cu", 2) == 0) {
296                 return tmp + 2;
297         }
298         return "??";
299 }
300
301
302 /*
303  *      Print address of NAS.
304  */
305 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
306 {
307         if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
308                 return "";
309         return ip_hostname(buf, buflen, ipaddr);
310 }
311
312
313 /*
314  *      Print usage message and exit.
315  */
316 static void usage(void)
317 {
318         fprintf(stderr, "Usage: radwho [-lhfnsipcr]\n");
319         fprintf(stderr, "       -l: show local (shell) users too\n");
320         fprintf(stderr, "       -h: hide shell users from radius\n");
321         fprintf(stderr, "       -f: give fingerd output\n");
322         fprintf(stderr, "       -n: no full name\n");
323         fprintf(stderr, "       -s: show full name\n");
324         fprintf(stderr, "       -i: show session ID\n");
325         fprintf(stderr, "       -p: show port type\n");
326         fprintf(stderr, "       -c: show caller ID, if available\n");
327         fprintf(stderr, "       -r: output as raw data\n");
328         exit(1);
329 }
330
331
332 /*
333  *      Main program, either pmwho or fingerd.
334  */
335 int main(int argc, char **argv)
336 {
337         CONF_SECTION *cs;
338         FILE *fp;
339         struct radutmp rt;
340         struct utmp ut;
341         int hdrdone = 0;
342         char inbuf[128];
343         char myname[128];
344         char othername[256];
345         char session_id[16];
346         int fingerd = 0;
347         int showlocal = 0;
348         int hideshell = 0;
349         int showsid = 0;
350         int rawoutput = 0;
351         char *p, *q;
352         const char *portind;
353         int c, portno;
354
355         radius_dir = strdup(RADIUS_DIR);
356
357         while((c = getopt(argc, argv, "flhnsipcr")) != EOF) switch(c) {
358                 case 'f':
359                         fingerd++;
360                         showname = 0;
361                         break;
362                 case 'l':
363                         showlocal = 1;
364                         break;
365                 case 'h':
366                         hideshell = 1;
367                         break;
368                 case 'n':
369                         showname = 0;
370                         break;
371                 case 's':
372                         showname = 1;
373                         break;
374                 case 'i':
375                         showsid = 1;
376                         break;
377                 case 'p':
378                         showptype = 1;
379                         break;
380                 case 'c':
381                         showcid = 1;
382                         showname = 1;
383                         break;
384                 case 'r':
385                         rawoutput = 1;
386                         break;
387                 default:
388                         usage();
389                         break;
390         }
391
392         /* Read radiusd.conf */
393         if(read_radius_conf_file() < 0) {
394                 fprintf(stderr, "%s: Errors reading radiusd.conf\n", argv[0]);
395                 exit(1);
396         }
397
398         cs = cf_section_find(NULL);
399         if(!cs) {
400                 fprintf(stderr, "%s: No configuration information in radiusd.conf!\n",
401                         argv[0]);
402                 exit(1);
403         }
404         cf_section_parse(cs, NULL, server_config);
405
406         /*
407          *      See if we are "fingerd".
408          */
409         if (strstr(argv[0], "fingerd")) {
410                 fingerd++;
411                 eol = "\r\n";
412                 if (showname < 0) showname = 0;
413         }
414         if (showname < 0) showname = 1;
415
416         if (fingerd) {
417                 /*
418                  *      Read first line of the input.
419                  */
420                 fgets(inbuf, 128, stdin);
421                 p = inbuf;
422                 while(*p == ' ' || *p == '\t') p++;
423                 if (*p == '/' && *(p + 1)) p += 2;
424                 while(*p == ' ' || *p == '\t') p++;
425                 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
426                         ;
427                 *q = 0;
428
429                 /*
430                  *      See if we fingered a specific user.
431                  */
432                 ffile("header");
433                 if (*p) sys_finger(p);
434         }
435
436         if (showlocal && (fp = fopen(UTMP_FILE, "r"))) {
437                 if (rawoutput == 0)
438                 {       
439                         fputs(showname ? hdr1 : hdr2, stdout);
440                         fputs(eol, stdout);
441                 }
442                 hdrdone = 1;
443
444                 /*
445                  *      Show the logged in UNIX users.
446                  */
447                 gethostname(myname, 128);
448                 while(fread(&ut, sizeof(ut), 1, fp) == 1) {
449 #ifdef USER_PROCESS
450                         if (ut.ut_user[0] && ut.ut_line[0] &&
451                                 ut.ut_type == USER_PROCESS) {
452 #else
453                         if (ut.ut_user[0] && ut.ut_line[0]) {
454 #endif
455 #ifdef UT_HOSTSIZE
456                         if (showname)
457                                 printf((rawoutput == 0? ufmt1: ufmt1r),
458                                                 ut.ut_name,
459                                                 fullname(ut.ut_name),
460                                                 "shell",
461                                                 ttyshort(ut.ut_line),
462 #ifdef HAVE_UTMPX_H
463                                                 dotime(ut.ut_xtime),
464 #else
465                                                 dotime(ut.ut_time),
466 #endif
467                                                 ut.ut_host,
468                                                 myname, eol);
469                         else
470                                 printf((rawoutput==0? ufmt2:ufmt2r),
471                                                 ut.ut_name,
472                                                 ttyshort(ut.ut_line),
473                                                 "shell",
474 #ifdef HAVE_UTMPX_H
475                                                 dotime(ut.ut_xtime),
476 #else
477                                                 dotime(ut.ut_time),
478 #endif
479                                                 ut.ut_host,
480                                                 myname, eol);
481 #endif
482                         }
483                 }
484                 fclose(fp);
485         }
486
487         /*
488          *      Show the users logged in on the terminal server(s).
489          */
490         if ((fp = fopen(RADUTMP, "r")) == NULL)
491                 return 0;
492
493         if (!hdrdone) {
494                 fputs(showname ? hdr1 : hdr2, stdout);
495                 fputs(eol, stdout);
496         }
497
498         while(fread(&rt, sizeof(rt), 1, fp) == 1) {
499                 if (rt.type == P_LOGIN) {
500                         /*
501                          *      We don't show shell users if we are
502                          *      fingerd, as we have done that above.
503                          */
504                         if (hideshell && !strchr("PCS", rt.proto))
505                                 continue;
506
507                         snprintf(session_id, sizeof(session_id), "%.8s", rt.session_id);
508
509                         if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
510                                 portind = ">";
511                                 portno = (showname ? 999 : 99999);
512                         } else {
513                                 portind = "S";
514                                 portno = rt.nas_port;
515                         }
516                         if (showname)
517                                 printf((rawoutput == 0? rfmt1: rfmt1r),
518                                                 rt.login,
519                                                 showcid ? rt.caller_id :
520                                                 (showsid? session_id : fullname(rt.login)),
521                                                 proto(rt.proto, rt.porttype),
522                                                 portind, portno,
523                                                 dotime(rt.time),
524                                                 nas_name(rt.nas_address),
525                                                 hostname(othername, sizeof(othername), rt.framed_address), eol);
526                         else
527                                 printf((rawoutput == 0? rfmt2: rfmt2r),
528                                                 rt.login,
529                                                 portind, portno,
530                                                 proto(rt.proto, rt.porttype),
531                                                 dotime(rt.time),
532                                                 nas_name(rt.nas_address),
533                                                 hostname(othername, sizeof(othername), rt.framed_address), eol);
534                 }
535         }
536         fflush(stdout);
537         fflush(stderr);
538         fclose(fp);
539
540         return 0;
541 }
542