Port patch from branch_1_1 to the HEAD
[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 <freeradius-devel/autoconf.h>
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <sys/stat.h>
35 #include <sys/utsname.h>
36 #include <ctype.h>
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40
41 #include <freeradius-devel/sysutmp.h>
42 #include <freeradius-devel/radutmp.h>
43 #include <freeradius-devel/radiusd.h>
44 #include <freeradius-devel/conffile.h>
45
46 /*
47  *      FIXME: put in header file.
48  */
49 #define SYS_FINGER "/usr/bin/finger"
50 #define FINGER_DIR "/usr/local/lib/finger"
51
52 /*
53  *      Header above output and format.
54  */
55 static const char *hdr1 =
56 "Login      Name              What  TTY  When      From      Location";
57 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-9.9s %-.19s%s";
58 static const char *rfmt1r = "%s,%s,%s,%s%u,%s,%s,%s%s";
59
60 static const char *hdr2 =
61 "Login      Port    What      When          From       Location";
62 static const char *rfmt2 = "%-10.10s %s%-5u  %-6.6s %-13.13s %-10.10s %-.28s%s";
63 static const char *rfmt2r = "%s,%s%u,%s,%s,%s,%s%s";
64
65 static const char *eol = "\n";
66 static int showname = -1;
67 static int showptype = 0;
68 static int showcid = 0;
69 int debug_flag = 0;
70 const char *progname = "radwho";
71 const char *radlog_dir = NULL;
72 const char *radutmp_file = NULL;
73
74 const char *radius_dir = NULL;
75 const char *radacct_dir = NULL;
76 const char *radlib_dir = NULL;
77 uint32_t myip = INADDR_ANY;
78 int log_stripped_names;
79
80 /*
81  *      Global, for log.c to use.
82  */
83 struct main_config_t mainconfig;
84
85 struct radutmp_config_t {
86   char *radutmp_fn;
87 } radutmpconfig;
88
89 static const 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
237 /*
238  *      Print address of NAS.
239  */
240 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
241 {
242         /*
243          *      WTF is this code for?
244          */
245         if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
246                 return "";
247
248         return inet_ntop(AF_INET, &ipaddr, buf, buflen);
249
250 }
251
252
253 /*
254  *      Print usage message and exit.
255  */
256 static void NEVER_RETURNS usage(int status)
257 {
258         FILE *output = status?stderr:stdout;
259
260         fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
261         fprintf(output, "       -c: show caller ID, if available\n");
262         fprintf(output, "       -d: set the raddb directory (default is %s)\n",
263                 RADIUS_DIR);
264         fprintf(output, "       -f: give fingerd output\n");
265         fprintf(output, "       -i: show session ID\n");
266         fprintf(output, "       -n: no full name\n");
267         fprintf(output, "       -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
268         fprintf(output, "       -p: show port type\n");
269         fprintf(output, "       -P <port>: Show entries matching the given nas port\n");
270         fprintf(output, "       -r: Print output as raw comma-delimited data\n");
271         fprintf(output, "       -R: Print output as RADIUS attributes and values\n");
272         fprintf(output, "           Includes ALL information from the radutmp record.\n");
273         fprintf(output, "       -s: show full name\n");
274         fprintf(output, "       -S: hide shell users from radius\n");
275         fprintf(output, "       -u <user>: Show entries matching the given user\n");
276         fprintf(output, "       -U <user>: like -u, but case-sensitive\n");
277         fprintf(output, "       -Z: Include accounting stop information in radius output.  Requires -R.\n");
278         exit(status);
279 }
280
281
282 /*
283  *      Main program, either pmwho or fingerd.
284  */
285 int main(int argc, char **argv)
286 {
287         CONF_SECTION *maincs, *cs;
288         FILE *fp;
289         struct radutmp rt;
290         char inbuf[128];
291         char othername[256];
292         char nasname[1024];
293         char session_id[sizeof(rt.session_id)+1];
294         int fingerd = 0;
295         int hideshell = 0;
296         int showsid = 0;
297         int rawoutput = 0;
298         int radiusoutput = 0;   /* Radius attributes */
299         char *p, *q;
300         const char *portind;
301         int c;
302         unsigned int portno;
303         char buffer[2048];
304         const char *user = NULL;
305         int user_cmp = 0;
306         time_t now = 0;
307         uint32_t nas_port = ~0;
308         uint32_t nas_ip_address = INADDR_NONE;
309         int zap = 0;
310
311         radius_dir = RADIUS_DIR;
312
313         while((c = getopt(argc, argv, "d:flnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
314                 case 'd':
315                         radius_dir = optarg;
316                         break;
317                 case 'f':
318                         fingerd++;
319                         showname = 0;
320                         break;
321                 case 'h':
322                         usage(0);
323                         break;
324                 case 'S':
325                         hideshell = 1;
326                         break;
327                 case 'n':
328                         showname = 0;
329                         break;
330                 case 'N':
331                         if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
332                                 usage(1);
333                         }
334                         break;
335                 case 's':
336                         showname = 1;
337                         break;
338                 case 'i':
339                         showsid = 1;
340                         break;
341                 case 'p':
342                         showptype = 1;
343                         break;
344                 case 'P':
345                         nas_port = atoi(optarg);
346                         break;
347                 case 'c':
348                         showcid = 1;
349                         showname = 1;
350                         break;
351                 case 'r':
352                         rawoutput = 1;
353                         break;
354                 case 'R':
355                         radiusoutput = 1;
356                         now = time(NULL);
357                         break;
358                 case 'u':
359                         user = optarg;
360                         user_cmp = 0;
361                         break;
362                 case 'U':
363                         user = optarg;
364                         user_cmp = 1;
365                         break;
366                 case 'Z':
367                         zap = 1;
368                         break;
369                 default:
370                         usage(1);
371                         break;
372         }
373
374         /*
375          *      Be safe.
376          */
377         if (zap && !radiusoutput) zap = 0;
378
379         /*
380          *      zap EVERYONE, but only on this nas
381          */
382         if (zap && !user && (~nas_port == 0)) {
383                 /*
384                  *      We need to know which NAS to zap users in.
385                  */
386                 if (nas_ip_address == INADDR_NONE) usage(1);
387
388                 printf("Acct-Status-Type = Accounting-Off\n");
389                 printf("NAS-IP-Address = %s\n",
390                        hostname(buffer, sizeof(buffer), nas_ip_address));
391                 printf("Acct-Delay-Time = 0\n");
392                 exit(0);        /* don't bother printing anything else */
393         }
394         
395         /*
396          *      Initialize mainconfig
397          */
398         memset(&mainconfig, 0, sizeof(mainconfig));
399         mainconfig.radlog_dest = RADLOG_STDOUT;
400
401         /* Read radiusd.conf */
402         snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
403         maincs = conf_read(NULL, 0, buffer, NULL);
404         if (!maincs) {
405                 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
406                 exit(1);
407         }
408
409         /* Read the radutmp section of radiusd.conf */
410         cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
411         if(!cs) {
412                 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
413                         argv[0]);
414                 exit(1);
415         }
416
417         cf_section_parse(cs, NULL, module_config);
418
419         /* Assign the correct path for the radutmp file */
420         radutmp_file = radutmpconfig.radutmp_fn;
421
422         /*
423          *      See if we are "fingerd".
424          */
425         if (strstr(argv[0], "fingerd")) {
426                 fingerd++;
427                 eol = "\r\n";
428                 if (showname < 0) showname = 0;
429         }
430         if (showname < 0) showname = 1;
431
432         if (fingerd) {
433                 /*
434                  *      Read first line of the input.
435                  */
436                 fgets(inbuf, 128, stdin);
437                 p = inbuf;
438                 while(*p == ' ' || *p == '\t') p++;
439                 if (*p == '/' && *(p + 1)) p += 2;
440                 while(*p == ' ' || *p == '\t') p++;
441                 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
442                         ;
443                 *q = 0;
444
445                 /*
446                  *      See if we fingered a specific user.
447                  */
448                 ffile("header");
449                 if (*p) sys_finger(p);
450         }
451
452         /*
453          *      Show the users logged in on the terminal server(s).
454          */
455         if ((fp = fopen(radutmp_file, "r")) == NULL) {
456                 fprintf(stderr, "%s: Error reading %s: %s\n",
457                         progname, radutmp_file, strerror(errno));
458                 return 0;
459         }
460
461         /*
462          *      Don't print the headers if raw or RADIUS
463          */
464         if (!rawoutput && !radiusoutput) {
465                 fputs(showname ? hdr1 : hdr2, stdout);
466                 fputs(eol, stdout);
467         }
468
469         /*
470          *      Read the file, printing out active entries.
471          */
472         while (fread(&rt, sizeof(rt), 1, fp) == 1) {
473                 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
474
475                 /*
476                  *      We don't show shell users if we are
477                  *      fingerd, as we have done that above.
478                  */
479                 if (hideshell && !strchr("PCS", rt.proto))
480                         continue;
481
482                 /*
483                  *      Print out sessions only for the given user.
484                  */
485                 if (user) {     /* only for a particular user */
486                         if (((user_cmp == 0) &&
487                              (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
488                             ((user_cmp == 1) &&
489                              (strncmp(rt.login, user, strlen(user)) != 0))) {
490                                 continue;
491                         }
492                 }
493
494                 /*
495                  *      Print out only for the given NAS port.
496                  */
497                 if (~nas_port != 0) {
498                         if (rt.nas_port != nas_port) continue;
499                 }
500
501                 /*
502                  *      Print out only for the given NAS IP address
503                  */
504                 if (nas_ip_address != INADDR_NONE) {
505                         if (rt.nas_address != nas_ip_address) continue;
506                 }
507                 
508                 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
509                 session_id[sizeof(rt.session_id)] = 0;
510                 
511                 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
512                         portind = ">";
513                         portno = (showname ? 999 : 99999);
514                 } else {
515                         portind = "S";
516                         portno = rt.nas_port;
517                 }
518
519                 /*
520                  *      Print output as RADIUS attributes
521                  */
522                 if (radiusoutput) {
523                         memcpy(nasname, rt.login, sizeof(rt.login));
524                         nasname[sizeof(rt.login)] = '\0';
525
526                         librad_safeprint(nasname, -1, buffer,
527                                          sizeof(buffer));
528                         printf("User-Name = \"%s\"\n", buffer);
529
530                         librad_safeprint(session_id, -1, buffer,
531                                          sizeof(buffer));
532                         printf("Acct-Session-Id = \"%s\"\n", buffer);
533
534                         if (zap) printf("Acct-Status-Type = Stop\n");
535
536                         printf("NAS-IP-Address = %s\n",
537                                hostname(buffer, sizeof(buffer),
538                                         rt.nas_address));
539                         printf("NAS-Port = %u\n", rt.nas_port);
540
541                         switch (rt.proto) {
542                                 case 'S':
543                                         printf("Service-Type = Framed-User\n");
544                                         printf("Framed-Protocol = SLIP\n");
545                                         break;
546                                 case 'P':
547                                         printf("Service-Type = Framed-User\n");
548                                         printf("Framed-Protocol = PPP\n");
549                                         break;
550                                 default:
551                                         printf("Service-type = Login-User\n");
552                                         break;
553                         }
554                         if (rt.framed_address != INADDR_NONE) {
555                                 printf("Framed-IP-Address = %s\n",
556                                        hostname(buffer, sizeof(buffer),
557                                                 rt.framed_address));
558                         }
559                         
560                         /*
561                          *      Some sanity checks on the time
562                          */
563                         if ((rt.time <= now) &&
564                             (now - rt.time) <= (86400 * 365)) {
565                                 printf("Acct-Session-Time = %ld\n",
566                                        now - rt.time);
567                         }
568
569                         if (rt.caller_id[0] != '\0') {
570                                 memcpy(nasname, rt.caller_id,
571                                        sizeof(rt.caller_id));
572                                 nasname[sizeof(rt.caller_id)] = '\0';
573                                 
574                                 librad_safeprint(nasname, -1, buffer,
575                                                  sizeof(buffer));
576                                 printf("Calling-Station-Id = \"%s\"\n", buffer);
577                         }
578
579                         printf("\n"); /* separate entries with a blank line */
580                         continue;
581                 }
582
583                 /*
584                  *      Show the fill name, or not.
585                  */
586                 if (showname) {
587                         printf((rawoutput == 0? rfmt1: rfmt1r),
588                                rt.login,
589                                showcid ? rt.caller_id :
590                                (showsid? session_id : fullname(rt.login)),
591                                proto(rt.proto, rt.porttype),
592                                portind, portno,
593                                dotime(rt.time),
594                                hostname(nasname, sizeof(nasname), rt.nas_address),
595                                hostname(othername, sizeof(othername), rt.framed_address), eol);
596                 } else {
597                         printf((rawoutput == 0? rfmt2: rfmt2r),
598                                rt.login,
599                                portind, portno,
600                                proto(rt.proto, rt.porttype),
601                                dotime(rt.time),
602                                hostname(nasname, sizeof(nasname), rt.nas_address),
603                                hostname(othername, sizeof(othername), rt.framed_address),
604                                eol);
605                 }
606         }
607         fclose(fp);
608
609         return 0;
610 }
611