Merge branch 'sam'
[freeradius.git] / src / main / radwho.c
1 /*@-skipposixheaders@*/
2 /*
3  * radwho.c     Show who is logged in on the terminal servers.
4  *              Can also be installed as fingerd on the UNIX
5  *              machine RADIUS runs on.
6  *
7  * Version:     $Id$
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  *
23  * Copyright 2000,2006  The FreeRADIUS server project
24  * Copyright 2000  Alan DeKok <aland@ox.org>
25  */
26
27 #include <freeradius-devel/ident.h>
28 RCSID("$Id$")
29
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/sysutmp.h>
32 #include <freeradius-devel/radutmp.h>
33
34 #ifdef HAVE_PWD_H
35 #include <pwd.h>
36 #endif
37
38 #include <sys/stat.h>
39
40 #include <ctype.h>
41
42 /*
43  *      FIXME: put in header file.
44  */
45 #define SYS_FINGER "/usr/bin/finger"
46 #define FINGER_DIR "/usr/local/lib/finger"
47
48 /*
49  *      Header above output and format.
50  */
51 static const char *hdr1 =
52 "Login      Name              What  TTY  When      From            Location";
53 static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-15.15s %-.19s%s";
54 static const char *rfmt1r = "%s,%s,%s,%s%u,%s,%s,%s%s";
55
56 static const char *hdr2 =
57 "Login      Port    What      When          From            Location";
58 static const char *rfmt2 = "%-10.10s %s%-5u  %-6.6s %-13.13s %-15.15s %-.28s%s";
59 static const char *rfmt2r = "%s,%s%u,%s,%s,%s,%s%s";
60
61 static const char *eol = "\n";
62 static int showname = -1;
63 static int showptype = 0;
64 static int showcid = 0;
65 int debug_flag = 0;
66 const char *progname = "radwho";
67 const char *radlog_dir = NULL;
68 const char *radutmp_file = NULL;
69 int check_config = FALSE;
70
71 const char *raddb_dir = NULL;
72 const char *radacct_dir = NULL;
73 const char *radlib_dir = NULL;
74 uint32_t myip = INADDR_ANY;
75 int log_stripped_names;
76
77 /*
78  *      Global, for log.c to use.
79  */
80 struct main_config_t mainconfig;
81 char *request_log_file = NULL;
82 char *debug_log_file = NULL;
83 int radius_xlat(char *out, UNUSED int outlen, UNUSED const char *fmt,
84                 UNUSED REQUEST *request, UNUSED RADIUS_ESCAPE_STRING func)
85 {
86         *out = 0;
87         return 0;
88 }
89
90 struct radutmp_config_t {
91   char *radutmp_fn;
92 } radutmpconfig;
93
94 static const CONF_PARSER module_config[] = {
95   { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn,  RADUTMP },
96   { NULL, -1, 0, NULL, NULL }
97 };
98
99 /*
100  *      Safe popen. Ugh.
101  */
102 static FILE *safe_popen(const char *cmd, const char *mode)
103 {
104         char            *p;
105         char            buf[1024];
106
107         /*
108          *      Change all suspect characters into a space.
109          */
110         strlcpy(buf, cmd, sizeof(buf));
111         buf[sizeof(buf) - 1] = 0;
112         for (p = buf; *p; p++) {
113                 if (isalnum((int) *p))
114                         continue;
115                 if (strchr("@%-_ \t+:,./", *p) == NULL)
116                         *p = ' ';
117         }
118
119         return popen(buf, mode);
120 }
121
122 /*
123  *      Print a file from FINGER_DIR. If the file is executable,
124  *      execute it instead. Return 0 if successful.
125  */
126 static int ffile(const char *arg)
127 {
128         FILE *fp;
129         char fn[1024];
130         int p = 0;
131         char *s;
132
133         snprintf(fn, sizeof(fn), "%s/%.32s", FINGER_DIR, arg);
134         if (access(fn, X_OK) == 0) {
135                 p = 1;
136                 snprintf(fn, sizeof(fn), "exec %s/%.32s 2>&1", FINGER_DIR, arg);
137                 fp = safe_popen(fn, "r");
138         } else fp = fopen(fn, "r");
139
140         if (fp == NULL)
141                 return -1;
142
143         while(fgets(fn, 1024, fp)) {
144                 if ((s = strchr(fn, '\n')) != NULL)
145                         *s = 0;
146                 fprintf(stdout, "%s\r\n", fn);
147         }
148         if (p)
149                 pclose(fp);
150         else
151                 fclose(fp);
152         fflush(stdout);
153         return 0;
154 }
155
156
157 /*
158  *      Execute the system finger and translate LF to CRLF.
159  */
160 static void sys_finger(const char *l)
161 {
162         FILE *fp;
163         char fn[1024];
164         char *p;
165
166         if (ffile(l) == 0)
167                 exit(0);
168
169         snprintf(fn, sizeof(fn), "exec %s %s", SYS_FINGER, l);
170         if ((fp = safe_popen(fn, "r")) == NULL) {
171                 printf("popen: %s\r\n", strerror(errno));
172                 exit(1);
173         }
174
175         while(fgets(fn, 1024, fp)) {
176                 if ((p = strchr(fn, '\n')) != NULL)
177                         *p = 0;
178                 fprintf(stdout, "%s\r\n", fn);
179         }
180         pclose(fp);
181         exit(0);
182 }
183
184
185 /*
186  *      Get fullname of a user.
187  */
188 static char *fullname(char *username)
189 {
190 #ifdef HAVE_PWD_Hx
191         struct passwd *pwd;
192         char *s;
193
194         if ((pwd = getpwnam(username)) != NULL) {
195                 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
196                 return pwd->pw_gecos;
197         }
198 #endif
199
200         return username;
201 }
202
203 /*
204  *      Return protocol type.
205  */
206 static const char *proto(int id, int porttype)
207 {
208         static char buf[8];
209
210         if (showptype) {
211                 if (!strchr("ASITX", porttype))
212                         porttype = ' ';
213                 if (id == 'S')
214                         snprintf(buf, sizeof(buf), "SLP %c", porttype);
215                 else if (id == 'P')
216                         snprintf(buf, sizeof(buf), "PPP %c", porttype);
217                 else
218                         snprintf(buf, sizeof(buf), "shl %c", porttype);
219                 return buf;
220         }
221         if (id == 'S') return "SLIP";
222         if (id == 'P') return "PPP";
223         return "shell";
224 }
225
226 /*
227  *      Return a time in the form day hh:mm
228  */
229 static char *dotime(time_t t)
230 {
231         char *s = ctime(&t);
232
233         if (showname) {
234                 strlcpy(s + 4, s + 11, 6);
235                 s[9] = 0;
236         } else {
237                 strlcpy(s + 4, s + 8, 9);
238                 s[12] = 0;
239         }
240
241         return s;
242 }
243
244
245 /*
246  *      Print address of NAS.
247  */
248 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
249 {
250         /*
251          *      WTF is this code for?
252          */
253         if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
254                 return "";
255
256         return inet_ntop(AF_INET, &ipaddr, buf, buflen);
257
258 }
259
260
261 /*
262  *      Print usage message and exit.
263  */
264 static void NEVER_RETURNS usage(int status)
265 {
266         FILE *output = status?stderr:stdout;
267
268         fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
269         fprintf(output, "       -c: show caller ID, if available\n");
270         fprintf(output, "       -d: set the raddb directory (default is %s)\n",
271                 RADIUS_DIR);
272         fprintf(output, "       -f: give fingerd output\n");
273         fprintf(output, "       -F <file>: Use radutmp <file>\n");
274         fprintf(output, "       -i: show session ID\n");
275         fprintf(output, "       -n: no full name\n");
276         fprintf(output, "       -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
277         fprintf(output, "       -p: show port type\n");
278         fprintf(output, "       -P <port>: Show entries matching the given nas port\n");
279         fprintf(output, "       -r: Print output as raw comma-delimited data\n");
280         fprintf(output, "       -R: Print output as RADIUS attributes and values\n");
281         fprintf(output, "           Includes ALL information from the radutmp record.\n");
282         fprintf(output, "       -s: show full name\n");
283         fprintf(output, "       -S: hide shell users from radius\n");
284         fprintf(output, "       -u <user>: Show entries matching the given user\n");
285         fprintf(output, "       -U <user>: like -u, but case-sensitive\n");
286         fprintf(output, "       -Z: Include accounting stop information in radius output.  Requires -R.\n");
287         exit(status);
288 }
289
290
291 /*
292  *      Main program, either pmwho or fingerd.
293  */
294 int main(int argc, char **argv)
295 {
296         CONF_SECTION *maincs, *cs;
297         FILE *fp;
298         struct radutmp rt;
299         char inbuf[128];
300         char othername[256];
301         char nasname[1024];
302         char session_id[sizeof(rt.session_id)+1];
303         int fingerd = 0;
304         int hideshell = 0;
305         int showsid = 0;
306         int rawoutput = 0;
307         int radiusoutput = 0;   /* Radius attributes */
308         char *p, *q;
309         const char *portind;
310         int c;
311         unsigned int portno;
312         char buffer[2048];
313         const char *user = NULL;
314         int user_cmp = 0;
315         time_t now = 0;
316         uint32_t nas_port = ~0;
317         uint32_t nas_ip_address = INADDR_NONE;
318         int zap = 0;
319
320         raddb_dir = RADIUS_DIR;
321
322         while((c = getopt(argc, argv, "d:fF:nN:sSipP:crRu:U:Z")) != EOF) switch(c) {
323                 case 'd':
324                         raddb_dir = optarg;
325                         break;
326                 case 'f':
327                         fingerd++;
328                         showname = 0;
329                         break;
330                 case 'F':
331                         radutmp_file = optarg;
332                         break;
333                 case 'h':
334                         usage(0);
335                         break;
336                 case 'S':
337                         hideshell = 1;
338                         break;
339                 case 'n':
340                         showname = 0;
341                         break;
342                 case 'N':
343                         if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
344                                 usage(1);
345                         }
346                         break;
347                 case 's':
348                         showname = 1;
349                         break;
350                 case 'i':
351                         showsid = 1;
352                         break;
353                 case 'p':
354                         showptype = 1;
355                         break;
356                 case 'P':
357                         nas_port = atoi(optarg);
358                         break;
359                 case 'c':
360                         showcid = 1;
361                         showname = 1;
362                         break;
363                 case 'r':
364                         rawoutput = 1;
365                         break;
366                 case 'R':
367                         radiusoutput = 1;
368                         now = time(NULL);
369                         break;
370                 case 'u':
371                         user = optarg;
372                         user_cmp = 0;
373                         break;
374                 case 'U':
375                         user = optarg;
376                         user_cmp = 1;
377                         break;
378                 case 'Z':
379                         zap = 1;
380                         break;
381                 default:
382                         usage(1);
383                         break;
384         }
385
386         /*
387          *      Be safe.
388          */
389         if (zap && !radiusoutput) zap = 0;
390
391         /*
392          *      zap EVERYONE, but only on this nas
393          */
394         if (zap && !user && (~nas_port == 0)) {
395                 /*
396                  *      We need to know which NAS to zap users in.
397                  */
398                 if (nas_ip_address == INADDR_NONE) usage(1);
399
400                 printf("Acct-Status-Type = Accounting-Off\n");
401                 printf("NAS-IP-Address = %s\n",
402                        hostname(buffer, sizeof(buffer), nas_ip_address));
403                 printf("Acct-Delay-Time = 0\n");
404                 exit(0);        /* don't bother printing anything else */
405         }
406
407         if (radutmp_file) goto have_radutmp;
408
409         /*
410          *      Initialize mainconfig
411          */
412         memset(&mainconfig, 0, sizeof(mainconfig));
413         mainconfig.radlog_dest = RADLOG_STDOUT;
414
415         /* Read radiusd.conf */
416         snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", raddb_dir);
417         maincs = cf_file_read(buffer);
418         if (!maincs) {
419                 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
420                 exit(1);
421         }
422
423         /* Read the radutmp section of radiusd.conf */
424         cs = cf_section_find_name2(cf_section_sub_find(maincs, "modules"), "radutmp", NULL);
425         if(!cs) {
426                 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
427                         argv[0]);
428                 exit(1);
429         }
430
431         cf_section_parse(cs, NULL, module_config);
432
433         /* Assign the correct path for the radutmp file */
434         radutmp_file = radutmpconfig.radutmp_fn;
435
436  have_radutmp:
437         /*
438          *      See if we are "fingerd".
439          */
440         if (strstr(argv[0], "fingerd")) {
441                 fingerd++;
442                 eol = "\r\n";
443                 if (showname < 0) showname = 0;
444         }
445         if (showname < 0) showname = 1;
446
447         if (fingerd) {
448                 /*
449                  *      Read first line of the input.
450                  */
451                 fgets(inbuf, 128, stdin);
452                 p = inbuf;
453                 while(*p == ' ' || *p == '\t') p++;
454                 if (*p == '/' && *(p + 1)) p += 2;
455                 while(*p == ' ' || *p == '\t') p++;
456                 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
457                         ;
458                 *q = 0;
459
460                 /*
461                  *      See if we fingered a specific user.
462                  */
463                 ffile("header");
464                 if (*p) sys_finger(p);
465         }
466
467         /*
468          *      Show the users logged in on the terminal server(s).
469          */
470         if ((fp = fopen(radutmp_file, "r")) == NULL) {
471                 fprintf(stderr, "%s: Error reading %s: %s\n",
472                         progname, radutmp_file, strerror(errno));
473                 return 0;
474         }
475
476         /*
477          *      Don't print the headers if raw or RADIUS
478          */
479         if (!rawoutput && !radiusoutput) {
480                 fputs(showname ? hdr1 : hdr2, stdout);
481                 fputs(eol, stdout);
482         }
483
484         /*
485          *      Read the file, printing out active entries.
486          */
487         while (fread(&rt, sizeof(rt), 1, fp) == 1) {
488                 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
489
490                 /*
491                  *      We don't show shell users if we are
492                  *      fingerd, as we have done that above.
493                  */
494                 if (hideshell && !strchr("PCS", rt.proto))
495                         continue;
496
497                 /*
498                  *      Print out sessions only for the given user.
499                  */
500                 if (user) {     /* only for a particular user */
501                         if (((user_cmp == 0) &&
502                              (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
503                             ((user_cmp == 1) &&
504                              (strncmp(rt.login, user, strlen(user)) != 0))) {
505                                 continue;
506                         }
507                 }
508
509                 /*
510                  *      Print out only for the given NAS port.
511                  */
512                 if (~nas_port != 0) {
513                         if (rt.nas_port != nas_port) continue;
514                 }
515
516                 /*
517                  *      Print out only for the given NAS IP address
518                  */
519                 if (nas_ip_address != INADDR_NONE) {
520                         if (rt.nas_address != nas_ip_address) continue;
521                 }
522
523                 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
524                 session_id[sizeof(rt.session_id)] = 0;
525
526                 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
527                         portind = ">";
528                         portno = (showname ? 999 : 99999);
529                 } else {
530                         portind = "S";
531                         portno = rt.nas_port;
532                 }
533
534                 /*
535                  *      Print output as RADIUS attributes
536                  */
537                 if (radiusoutput) {
538                         memcpy(nasname, rt.login, sizeof(rt.login));
539                         nasname[sizeof(rt.login)] = '\0';
540
541                         fr_print_string(nasname, 0, buffer,
542                                          sizeof(buffer));
543                         printf("User-Name = \"%s\"\n", buffer);
544
545                         fr_print_string(session_id, 0, buffer,
546                                          sizeof(buffer));
547                         printf("Acct-Session-Id = \"%s\"\n", buffer);
548
549                         if (zap) printf("Acct-Status-Type = Stop\n");
550
551                         printf("NAS-IP-Address = %s\n",
552                                hostname(buffer, sizeof(buffer),
553                                         rt.nas_address));
554                         printf("NAS-Port = %u\n", rt.nas_port);
555
556                         switch (rt.proto) {
557                                 case 'S':
558                                         printf("Service-Type = Framed-User\n");
559                                         printf("Framed-Protocol = SLIP\n");
560                                         break;
561                                 case 'P':
562                                         printf("Service-Type = Framed-User\n");
563                                         printf("Framed-Protocol = PPP\n");
564                                         break;
565                                 default:
566                                         printf("Service-type = Login-User\n");
567                                         break;
568                         }
569                         if (rt.framed_address != INADDR_NONE) {
570                                 printf("Framed-IP-Address = %s\n",
571                                        hostname(buffer, sizeof(buffer),
572                                                 rt.framed_address));
573                         }
574
575                         /*
576                          *      Some sanity checks on the time
577                          */
578                         if ((rt.time <= now) &&
579                             (now - rt.time) <= (86400 * 365)) {
580                                 printf("Acct-Session-Time = %ld\n",
581                                        now - rt.time);
582                         }
583
584                         if (rt.caller_id[0] != '\0') {
585                                 memcpy(nasname, rt.caller_id,
586                                        sizeof(rt.caller_id));
587                                 nasname[sizeof(rt.caller_id)] = '\0';
588
589                                 fr_print_string(nasname, 0, buffer,
590                                                  sizeof(buffer));
591                                 printf("Calling-Station-Id = \"%s\"\n", buffer);
592                         }
593
594                         printf("\n"); /* separate entries with a blank line */
595                         continue;
596                 }
597
598                 /*
599                  *      Show the fill name, or not.
600                  */
601                 if (showname) {
602                         printf((rawoutput == 0? rfmt1: rfmt1r),
603                                rt.login,
604                                showcid ? rt.caller_id :
605                                (showsid? session_id : fullname(rt.login)),
606                                proto(rt.proto, rt.porttype),
607                                portind, portno,
608                                dotime(rt.time),
609                                hostname(nasname, sizeof(nasname), rt.nas_address),
610                                hostname(othername, sizeof(othername), rt.framed_address), eol);
611                 } else {
612                         printf((rawoutput == 0? rfmt2: rfmt2r),
613                                rt.login,
614                                portind, portno,
615                                proto(rt.proto, rt.porttype),
616                                dotime(rt.time),
617                                hostname(nasname, sizeof(nasname), rt.nas_address),
618                                hostname(othername, sizeof(othername), rt.framed_address),
619                                eol);
620                 }
621         }
622         fclose(fp);
623
624         return 0;
625 }
626