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