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