* Remove a bunch of variables that were included but never really
[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         sprintf(fn, "%s/%.32s", FINGER_DIR, arg);
129         if (access(fn, X_OK) == 0) {
130                 p = 1;
131                 sprintf(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) return -1;
136
137         while(fgets(fn, 1024, fp)) {
138                 if ((s = strchr(fn, '\n')) != NULL)
139                         *s = 0;
140                 fprintf(stdout, "%s\r\n", fn);
141         }
142         if (p)
143                 pclose(fp);
144         else
145                 fclose(fp);
146         fflush(stdout);
147         return 0;
148 }
149
150
151 /*
152  *      Execute the system finger and translate LF to CRLF.
153  */
154 static void sys_finger(const char *l)
155 {
156         FILE *fp;
157         char fn[1024];
158         char *p;
159
160         if (ffile(l) == 0)
161                 exit(0);
162
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));
166                 exit(1);
167         }
168
169         while(fgets(fn, 1024, fp)) {
170                 if ((p = strchr(fn, '\n')) != NULL)
171                         *p = 0;
172                 fprintf(stdout, "%s\r\n", fn);
173         }
174         pclose(fp);
175         exit(0);
176 }
177
178
179 /*
180  *      Get fullname of a user.
181  */
182 static char *fullname(char *username)
183 {
184         struct passwd *pwd;
185         char *s;
186
187         if ((pwd = getpwnam(username)) != NULL) {
188                 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
189                 return pwd->pw_gecos;
190         }
191         return username;
192 }
193
194 /*
195  *      Return protocol type.
196  */
197 static const char *proto(int id, int porttype)
198 {
199         static char buf[8];
200
201         if (showptype) {
202                 if (!strchr("ASITX", porttype))
203                         porttype = ' ';
204                 if (id == 'S')
205                         sprintf(buf, "SLP %c", porttype);
206                 else if (id == 'P')
207                         sprintf(buf, "PPP %c", porttype);
208                 else
209                         sprintf(buf, "shl %c", porttype);
210                 return buf;
211         }
212         if (id == 'S') return "SLIP";
213         if (id == 'P') return "PPP";
214         return "shell";
215 }
216
217 /*
218  *      Return a time in the form day hh:mm
219  */
220 static char *dotime(time_t t)
221 {
222         char *s = ctime(&t);
223
224         if (showname) {
225                 strncpy(s + 4, s + 11, 5);
226                 s[9] = 0;
227         } else {
228                 strncpy(s + 4, s + 8, 8);
229                 s[12] = 0;
230         }
231
232         return s;
233 }
234
235 #if 0 /*UNUSED*/
236 /*
237  *      See how long a tty has been idle.
238  */
239 char *idletime(char *line)
240 {
241         char tty[16];
242         static char tmp[8];
243         time_t t;
244         struct stat st;
245         int hr, min, days;
246
247         if (line[0] == '/')
248                 strcpy(tty, "/dev/");
249         else
250                 tty[0] = 0;
251         strncat(tty, line, 10);
252         tty[15] = 0;
253
254         tmp[0] = 0;
255         if (stat(tty, &st) == 0) {
256                 time(&t);
257                 t -= st.st_mtime;
258                 if (t >= 60) {
259                         min = (t / 60);
260                         hr = min / 24;
261                         days = hr / 24;
262                         min %= 60;
263                         hr %= 24;
264                         if (days > 0)
265                                 sprintf(tmp, "%dd", days);
266                         else
267                                 sprintf(tmp, "%2d:%02d", hr, min);
268                 }
269         }
270         return tmp;
271 }
272 #endif
273
274 /*
275  *      Shorten tty name.
276  */
277 static const char *ttyshort(char *tty)
278 {
279         static char tmp[16];
280
281         if (tty[0] == '/') tty += 5;
282
283         if (strncmp(tty, "tty", 3) == 0) {
284                 if (tty[3] >= '0' && tty[3] <= '9')
285                         sprintf(tmp, "v%.14s", tty + 3);
286                 else
287                         sprintf(tmp, "%.15s", tty + 3);
288                 return tmp;
289         }
290         if (strncmp(tty, "vc", 2) == 0) {
291                 sprintf(tmp, "v.14%s", tty + 2);
292                 return tmp;
293         }
294         if (strncmp(tty, "cu", 2) == 0) {
295                 return tmp + 2;
296         }
297         return "??";
298 }
299
300
301 /*
302  *      Print address of NAS.
303  */
304 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
305 {
306         if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
307                 return "";
308         return ip_hostname(buf, buflen, ipaddr);
309 }
310
311
312 /*
313  *      Print usage message and exit.
314  */
315 static void usage(void)
316 {
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");
327         exit(1);
328 }
329
330
331 /*
332  *      Main program, either pmwho or fingerd.
333  */
334 int main(int argc, char **argv)
335 {
336         CONF_SECTION *cs;
337         FILE *fp;
338         struct radutmp rt;
339         struct utmp ut;
340         int hdrdone = 0;
341         char inbuf[128];
342         char myname[128];
343         char othername[256];
344         char session_id[16];
345         int fingerd = 0;
346         int showlocal = 0;
347         int hideshell = 0;
348         int showsid = 0;
349         int rawoutput = 0;
350         char *p, *q;
351         const char *portind;
352         int c, portno;
353
354         radius_dir = strdup(RADIUS_DIR);
355
356         while((c = getopt(argc, argv, "flhnsipcr")) != EOF) switch(c) {
357                 case 'f':
358                         fingerd++;
359                         showname = 0;
360                         break;
361                 case 'l':
362                         showlocal = 1;
363                         break;
364                 case 'h':
365                         hideshell = 1;
366                         break;
367                 case 'n':
368                         showname = 0;
369                         break;
370                 case 's':
371                         showname = 1;
372                         break;
373                 case 'i':
374                         showsid = 1;
375                         break;
376                 case 'p':
377                         showptype = 1;
378                         break;
379                 case 'c':
380                         showcid = 1;
381                         showname = 1;
382                         break;
383                 case 'r':
384                         rawoutput = 1;
385                         break;
386                 default:
387                         usage();
388                         break;
389         }
390
391         /* Read radiusd.conf */
392         if(read_radius_conf_file() < 0) {
393                 fprintf(stderr, "%s: Errors reading radiusd.conf\n", argv[0]);
394                 exit(1);
395         }
396
397         cs = cf_section_find(NULL);
398         if(!cs) {
399                 fprintf(stderr, "%s: No configuration information in radiusd.conf!\n",
400                         argv[0]);
401                 exit(1);
402         }
403         cf_section_parse(cs, NULL, server_config);
404
405         /*
406          *      See if we are "fingerd".
407          */
408         if (strstr(argv[0], "fingerd")) {
409                 fingerd++;
410                 eol = "\r\n";
411                 if (showname < 0) showname = 0;
412         }
413         if (showname < 0) showname = 1;
414
415         if (fingerd) {
416                 /*
417                  *      Read first line of the input.
418                  */
419                 fgets(inbuf, 128, stdin);
420                 p = inbuf;
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++)
425                         ;
426                 *q = 0;
427
428                 /*
429                  *      See if we fingered a specific user.
430                  */
431                 ffile("header");
432                 if (*p) sys_finger(p);
433         }
434
435         if (showlocal && (fp = fopen(UTMP_FILE, "r"))) {
436                 if (rawoutput == 0)
437                 {       
438                         fputs(showname ? hdr1 : hdr2, stdout);
439                         fputs(eol, stdout);
440                 }
441                 hdrdone = 1;
442
443                 /*
444                  *      Show the logged in UNIX users.
445                  */
446                 gethostname(myname, 128);
447                 while(fread(&ut, sizeof(ut), 1, fp) == 1) {
448 #ifdef USER_PROCESS
449                         if (ut.ut_user[0] && ut.ut_line[0] &&
450                                 ut.ut_type == USER_PROCESS) {
451 #else
452                         if (ut.ut_user[0] && ut.ut_line[0]) {
453 #endif
454 #ifdef UT_HOSTSIZE
455                            if (showname)
456                                 printf((rawoutput == 0? ufmt1: ufmt1r),
457                                         ut.ut_name,
458                                         fullname(ut.ut_name),
459                                         "shell",
460                                         ttyshort(ut.ut_line),
461 #ifdef HAVE_UTMPX_H
462                                         dotime(ut.ut_xtime),
463 #else
464                                         dotime(ut.ut_time),
465 #endif
466                                         ut.ut_host,
467                                         myname, eol);
468                             else
469                                 printf((rawoutput==0? ufmt2:ufmt2r),
470                                         ut.ut_name,
471                                         ttyshort(ut.ut_line),
472                                         "shell",
473 #ifdef HAVE_UTMPX_H
474                                         dotime(ut.ut_xtime),
475 #else
476                                         dotime(ut.ut_time),
477 #endif
478                                         ut.ut_host,
479                                         myname, eol);
480 #endif
481                         }
482                 }
483                 fclose(fp);
484         }
485
486         /*
487          *      Show the users logged in on the terminal server(s).
488          */
489         if ((fp = fopen(RADUTMP, "r")) == NULL)
490                 return 0;
491
492         if (!hdrdone) {
493                 fputs(showname ? hdr1 : hdr2, stdout);
494                 fputs(eol, stdout);
495         }
496
497         while(fread(&rt, sizeof(rt), 1, fp) == 1) {
498                 if (rt.type == P_LOGIN) {
499                         /*
500                          *      We don't show shell users if we are
501                          *      fingerd, as we have done that above.
502                          */
503                         if (hideshell && !strchr("PCS", rt.proto))
504                                 continue;
505
506                         sprintf(session_id, "%.8s", rt.session_id);
507
508                         if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
509                                 portind = ">";
510                                 portno = (showname ? 999 : 99999);
511                         } else {
512                                 portind = "S";
513                                 portno = rt.nas_port;
514                         }
515                         if (showname)
516                             printf((rawoutput == 0? rfmt1: rfmt1r),
517                                 rt.login,
518                                 showcid ? rt.caller_id :
519                                 (showsid? session_id : fullname(rt.login)),
520                                 proto(rt.proto, rt.porttype),
521                                 portind, portno,
522                                 dotime(rt.time),
523                                 nas_name(rt.nas_address),
524                                 hostname(othername, sizeof(othername), rt.framed_address), eol);
525                         else
526                             printf((rawoutput == 0? rfmt2: rfmt2r),
527                                 rt.login,
528                                 portind, portno,
529                                 proto(rt.proto, rt.porttype),
530                                 dotime(rt.time),
531                                 nas_name(rt.nas_address),
532                                 hostname(othername, sizeof(othername), rt.framed_address), eol);
533                 }
534         }
535         fflush(stdout);
536         fflush(stderr);
537         fclose(fp);
538
539         return 0;
540 }
541