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