These don't need radlog_dest any more
[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 const char *radutmp_file = NULL;
78
79 const char *radius_dir = NULL;
80 const char *radacct_dir = NULL;
81 const char *radlib_dir = NULL;
82 uint32_t myip = INADDR_ANY;
83 int log_stripped_names;
84
85 struct radutmp_config_t {
86   char *radutmp_fn;
87 } radutmpconfig;
88
89 static CONF_PARSER module_config[] = {
90   { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn,  RADUTMP },
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((int) *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 [-d raddb] [-lhfnsipcr]\n");
319         fprintf(stderr, "       -d: set the raddb directory (default is %s)\n",
320                 RADIUS_DIR);
321         fprintf(stderr, "       -l: show local (shell) users too\n");
322         fprintf(stderr, "       -h: hide shell users from radius\n");
323         fprintf(stderr, "       -f: give fingerd output\n");
324         fprintf(stderr, "       -n: no full name\n");
325         fprintf(stderr, "       -s: show full name\n");
326         fprintf(stderr, "       -i: show session ID\n");
327         fprintf(stderr, "       -p: show port type\n");
328         fprintf(stderr, "       -c: show caller ID, if available\n");
329         fprintf(stderr, "       -r: output as raw data\n");
330         exit(1);
331 }
332
333
334 /*
335  *      Main program, either pmwho or fingerd.
336  */
337 int main(int argc, char **argv)
338 {
339         CONF_SECTION *maincs, *cs;
340         FILE *fp;
341         struct radutmp rt;
342         struct utmp ut;
343         int hdrdone = 0;
344         char inbuf[128];
345         char myname[128];
346         char othername[256];
347         char nasname[1024];
348         char session_id[sizeof(rt.session_id)+1];
349         int fingerd = 0;
350         int showlocal = 0;
351         int hideshell = 0;
352         int showsid = 0;
353         int rawoutput = 0;
354         char *p, *q;
355         const char *portind;
356         int c, portno;
357
358         radius_dir = strdup(RADIUS_DIR);
359
360         while((c = getopt(argc, argv, "d:flhnsipcr")) != EOF) switch(c) {
361                 case 'd':
362                         if (radius_dir) free(radius_dir);
363                         radius_dir = strdup(optarg);
364                         break;
365                 case 'f':
366                         fingerd++;
367                         showname = 0;
368                         break;
369                 case 'l':
370                         showlocal = 1;
371                         break;
372                 case 'h':
373                         hideshell = 1;
374                         break;
375                 case 'n':
376                         showname = 0;
377                         break;
378                 case 's':
379                         showname = 1;
380                         break;
381                 case 'i':
382                         showsid = 1;
383                         break;
384                 case 'p':
385                         showptype = 1;
386                         break;
387                 case 'c':
388                         showcid = 1;
389                         showname = 1;
390                         break;
391                 case 'r':
392                         rawoutput = 1;
393                         break;
394                 default:
395                         usage();
396                         break;
397         }
398
399         /*
400          *      Ensure that the configuration is initialized.
401          */
402         memset(&mainconfig, 0, sizeof(mainconfig));
403
404         /* Read radiusd.conf */
405         if ((maincs = read_radius_conf_file()) == NULL) {
406                 fprintf(stderr, "%s: Errors reading radiusd.conf\n", argv[0]);
407                 exit(1);
408         }
409
410         /* Read the radutmp section of radiusd.conf */
411         cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
412         if(!cs) {
413                 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
414                         argv[0]);
415                 exit(1);
416         }
417
418         cf_section_parse(cs, NULL, module_config);
419
420         /* Assign the correct path for the radutmp file */
421         radutmp_file = radutmpconfig.radutmp_fn;
422
423         /*
424          *      See if we are "fingerd".
425          */
426         if (strstr(argv[0], "fingerd")) {
427                 fingerd++;
428                 eol = "\r\n";
429                 if (showname < 0) showname = 0;
430         }
431         if (showname < 0) showname = 1;
432
433         if (fingerd) {
434                 /*
435                  *      Read first line of the input.
436                  */
437                 fgets(inbuf, 128, stdin);
438                 p = inbuf;
439                 while(*p == ' ' || *p == '\t') p++;
440                 if (*p == '/' && *(p + 1)) p += 2;
441                 while(*p == ' ' || *p == '\t') p++;
442                 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
443                         ;
444                 *q = 0;
445
446                 /*
447                  *      See if we fingered a specific user.
448                  */
449                 ffile("header");
450                 if (*p) sys_finger(p);
451         }
452
453         if (showlocal && (fp = fopen(radutmp_file, "r"))) {
454                 if (rawoutput == 0)
455                 {
456                         fputs(showname ? hdr1 : hdr2, stdout);
457                         fputs(eol, stdout);
458                 }
459                 hdrdone = 1;
460
461                 /*
462                  *      Show the logged in UNIX users.
463                  */
464                 gethostname(myname, 128);
465                 while(fread(&ut, sizeof(ut), 1, fp) == 1) {
466 #ifdef USER_PROCESS
467                         if (ut.ut_user[0] && ut.ut_line[0] &&
468                                 ut.ut_type == USER_PROCESS) {
469 #else
470                         if (ut.ut_user[0] && ut.ut_line[0]) {
471 #endif
472 #ifdef UT_HOSTSIZE
473
474 #ifdef HAVE_UTMPX_H
475 #       define UT_TIME ut_xtime
476 #else
477 #       define UT_TIME ut_time
478 #endif
479                         if (showname)
480                                 printf((rawoutput == 0? ufmt1: ufmt1r),
481                                                 ut.ut_name,
482                                                 fullname(ut.ut_name),
483                                                 "shell",
484                                                 ttyshort(ut.ut_line),
485                                                 dotime(ut.UT_TIME),
486                                                 ut.ut_host,
487                                                 myname, eol);
488                         else
489                                 printf((rawoutput==0? ufmt2:ufmt2r),
490                                                 ut.ut_name,
491                                                 ttyshort(ut.ut_line),
492                                                 "shell",
493                                                 dotime(ut.UT_TIME),
494                                                 ut.ut_host,
495                                                 myname, eol);
496 #endif
497                         }
498                 }
499                 fclose(fp);
500         }
501
502         /*
503          *      Show the users logged in on the terminal server(s).
504          */
505         if ((fp = fopen(radutmp_file, "r")) == NULL)
506                 return 0;
507
508         if (!hdrdone) {
509                 fputs(showname ? hdr1 : hdr2, stdout);
510                 fputs(eol, stdout);
511         }
512
513         while(fread(&rt, sizeof(rt), 1, fp) == 1) {
514                 if (rt.type == P_LOGIN) {
515                         /*
516                          *      We don't show shell users if we are
517                          *      fingerd, as we have done that above.
518                          */
519                         if (hideshell && !strchr("PCS", rt.proto))
520                                 continue;
521
522                         memcpy(session_id, rt.session_id, sizeof(rt.session_id));
523                         session_id[sizeof(rt.session_id)] = 0;
524
525                         if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
526                                 portind = ">";
527                                 portno = (showname ? 999 : 99999);
528                         } else {
529                                 portind = "S";
530                                 portno = rt.nas_port;
531                         }
532                         if (showname)
533                                 printf((rawoutput == 0? rfmt1: rfmt1r),
534                                                 rt.login,
535                                                 showcid ? rt.caller_id :
536                                                 (showsid? session_id : fullname(rt.login)),
537                                                 proto(rt.proto, rt.porttype),
538                                                 portind, portno,
539                                                 dotime(rt.time),
540                                                 nas_name3(nasname, sizeof(nasname), rt.nas_address),
541                                                 hostname(othername, sizeof(othername), rt.framed_address), eol);
542                         else
543                                 printf((rawoutput == 0? rfmt2: rfmt2r),
544                                                 rt.login,
545                                                 portind, portno,
546                                                 proto(rt.proto, rt.porttype),
547                                                 dotime(rt.time),
548                                                 nas_name3(nasname, sizeof(nasname), rt.nas_address),
549                                                 hostname(othername, sizeof(othername), rt.framed_address),
550                                                 eol);
551                 }
552         }
553         fflush(stdout);
554         fflush(stderr);
555         fclose(fp);
556
557         return 0;
558 }
559