rlm_eap: add eap_chbind.c to build
[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 char *radius_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, "       -i: show session ID\n");
274         fprintf(output, "       -n: no full name\n");
275         fprintf(output, "       -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
276         fprintf(output, "       -p: show port type\n");
277         fprintf(output, "       -P <port>: Show entries matching the given nas port\n");
278         fprintf(output, "       -r: Print output as raw comma-delimited data\n");
279         fprintf(output, "       -R: Print output as RADIUS attributes and values\n");
280         fprintf(output, "           Includes ALL information from the radutmp record.\n");
281         fprintf(output, "       -s: show full name\n");
282         fprintf(output, "       -S: hide shell users from radius\n");
283         fprintf(output, "       -u <user>: Show entries matching the given user\n");
284         fprintf(output, "       -U <user>: like -u, but case-sensitive\n");
285         fprintf(output, "       -Z: Include accounting stop information in radius output.  Requires -R.\n");
286         exit(status);
287 }
288
289
290 /*
291  *      Main program, either pmwho or fingerd.
292  */
293 int main(int argc, char **argv)
294 {
295         CONF_SECTION *maincs, *cs;
296         FILE *fp;
297         struct radutmp rt;
298         char inbuf[128];
299         char othername[256];
300         char nasname[1024];
301         char session_id[sizeof(rt.session_id)+1];
302         int fingerd = 0;
303         int hideshell = 0;
304         int showsid = 0;
305         int rawoutput = 0;
306         int radiusoutput = 0;   /* Radius attributes */
307         char *p, *q;
308         const char *portind;
309         int c;
310         unsigned int portno;
311         char buffer[2048];
312         const char *user = NULL;
313         int user_cmp = 0;
314         time_t now = 0;
315         uint32_t nas_port = ~0;
316         uint32_t nas_ip_address = INADDR_NONE;
317         int zap = 0;
318
319         radius_dir = RADIUS_DIR;
320
321         while((c = getopt(argc, argv, "d:fnN:sSipP:crRu:U:Z")) != EOF) switch(c) {
322                 case 'd':
323                         radius_dir = optarg;
324                         break;
325                 case 'f':
326                         fingerd++;
327                         showname = 0;
328                         break;
329                 case 'h':
330                         usage(0);
331                         break;
332                 case 'S':
333                         hideshell = 1;
334                         break;
335                 case 'n':
336                         showname = 0;
337                         break;
338                 case 'N':
339                         if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
340                                 usage(1);
341                         }
342                         break;
343                 case 's':
344                         showname = 1;
345                         break;
346                 case 'i':
347                         showsid = 1;
348                         break;
349                 case 'p':
350                         showptype = 1;
351                         break;
352                 case 'P':
353                         nas_port = atoi(optarg);
354                         break;
355                 case 'c':
356                         showcid = 1;
357                         showname = 1;
358                         break;
359                 case 'r':
360                         rawoutput = 1;
361                         break;
362                 case 'R':
363                         radiusoutput = 1;
364                         now = time(NULL);
365                         break;
366                 case 'u':
367                         user = optarg;
368                         user_cmp = 0;
369                         break;
370                 case 'U':
371                         user = optarg;
372                         user_cmp = 1;
373                         break;
374                 case 'Z':
375                         zap = 1;
376                         break;
377                 default:
378                         usage(1);
379                         break;
380         }
381
382         /*
383          *      Be safe.
384          */
385         if (zap && !radiusoutput) zap = 0;
386
387         /*
388          *      zap EVERYONE, but only on this nas
389          */
390         if (zap && !user && (~nas_port == 0)) {
391                 /*
392                  *      We need to know which NAS to zap users in.
393                  */
394                 if (nas_ip_address == INADDR_NONE) usage(1);
395
396                 printf("Acct-Status-Type = Accounting-Off\n");
397                 printf("NAS-IP-Address = %s\n",
398                        hostname(buffer, sizeof(buffer), nas_ip_address));
399                 printf("Acct-Delay-Time = 0\n");
400                 exit(0);        /* don't bother printing anything else */
401         }
402
403         /*
404          *      Initialize mainconfig
405          */
406         memset(&mainconfig, 0, sizeof(mainconfig));
407         mainconfig.radlog_dest = RADLOG_STDOUT;
408
409         /* Read radiusd.conf */
410         snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
411         maincs = cf_file_read(buffer);
412         if (!maincs) {
413                 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
414                 exit(1);
415         }
416
417         /* Read the radutmp section of radiusd.conf */
418         cs = cf_section_sub_find(cf_section_sub_find(maincs, "modules"), "radutmp");
419         if(!cs) {
420                 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
421                         argv[0]);
422                 exit(1);
423         }
424
425         cf_section_parse(cs, NULL, module_config);
426
427         /* Assign the correct path for the radutmp file */
428         radutmp_file = radutmpconfig.radutmp_fn;
429
430         /*
431          *      See if we are "fingerd".
432          */
433         if (strstr(argv[0], "fingerd")) {
434                 fingerd++;
435                 eol = "\r\n";
436                 if (showname < 0) showname = 0;
437         }
438         if (showname < 0) showname = 1;
439
440         if (fingerd) {
441                 /*
442                  *      Read first line of the input.
443                  */
444                 fgets(inbuf, 128, stdin);
445                 p = inbuf;
446                 while(*p == ' ' || *p == '\t') p++;
447                 if (*p == '/' && *(p + 1)) p += 2;
448                 while(*p == ' ' || *p == '\t') p++;
449                 for(q = p; *q && *q != '\r' && *q != '\n'; q++)
450                         ;
451                 *q = 0;
452
453                 /*
454                  *      See if we fingered a specific user.
455                  */
456                 ffile("header");
457                 if (*p) sys_finger(p);
458         }
459
460         /*
461          *      Show the users logged in on the terminal server(s).
462          */
463         if ((fp = fopen(radutmp_file, "r")) == NULL) {
464                 fprintf(stderr, "%s: Error reading %s: %s\n",
465                         progname, radutmp_file, strerror(errno));
466                 return 0;
467         }
468
469         /*
470          *      Don't print the headers if raw or RADIUS
471          */
472         if (!rawoutput && !radiusoutput) {
473                 fputs(showname ? hdr1 : hdr2, stdout);
474                 fputs(eol, stdout);
475         }
476
477         /*
478          *      Read the file, printing out active entries.
479          */
480         while (fread(&rt, sizeof(rt), 1, fp) == 1) {
481                 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
482
483                 /*
484                  *      We don't show shell users if we are
485                  *      fingerd, as we have done that above.
486                  */
487                 if (hideshell && !strchr("PCS", rt.proto))
488                         continue;
489
490                 /*
491                  *      Print out sessions only for the given user.
492                  */
493                 if (user) {     /* only for a particular user */
494                         if (((user_cmp == 0) &&
495                              (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
496                             ((user_cmp == 1) &&
497                              (strncmp(rt.login, user, strlen(user)) != 0))) {
498                                 continue;
499                         }
500                 }
501
502                 /*
503                  *      Print out only for the given NAS port.
504                  */
505                 if (~nas_port != 0) {
506                         if (rt.nas_port != nas_port) continue;
507                 }
508
509                 /*
510                  *      Print out only for the given NAS IP address
511                  */
512                 if (nas_ip_address != INADDR_NONE) {
513                         if (rt.nas_address != nas_ip_address) continue;
514                 }
515
516                 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
517                 session_id[sizeof(rt.session_id)] = 0;
518
519                 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
520                         portind = ">";
521                         portno = (showname ? 999 : 99999);
522                 } else {
523                         portind = "S";
524                         portno = rt.nas_port;
525                 }
526
527                 /*
528                  *      Print output as RADIUS attributes
529                  */
530                 if (radiusoutput) {
531                         memcpy(nasname, rt.login, sizeof(rt.login));
532                         nasname[sizeof(rt.login)] = '\0';
533
534                         fr_print_string(nasname, 0, buffer,
535                                          sizeof(buffer));
536                         printf("User-Name = \"%s\"\n", buffer);
537
538                         fr_print_string(session_id, 0, buffer,
539                                          sizeof(buffer));
540                         printf("Acct-Session-Id = \"%s\"\n", buffer);
541
542                         if (zap) printf("Acct-Status-Type = Stop\n");
543
544                         printf("NAS-IP-Address = %s\n",
545                                hostname(buffer, sizeof(buffer),
546                                         rt.nas_address));
547                         printf("NAS-Port = %u\n", rt.nas_port);
548
549                         switch (rt.proto) {
550                                 case 'S':
551                                         printf("Service-Type = Framed-User\n");
552                                         printf("Framed-Protocol = SLIP\n");
553                                         break;
554                                 case 'P':
555                                         printf("Service-Type = Framed-User\n");
556                                         printf("Framed-Protocol = PPP\n");
557                                         break;
558                                 default:
559                                         printf("Service-type = Login-User\n");
560                                         break;
561                         }
562                         if (rt.framed_address != INADDR_NONE) {
563                                 printf("Framed-IP-Address = %s\n",
564                                        hostname(buffer, sizeof(buffer),
565                                                 rt.framed_address));
566                         }
567
568                         /*
569                          *      Some sanity checks on the time
570                          */
571                         if ((rt.time <= now) &&
572                             (now - rt.time) <= (86400 * 365)) {
573                                 printf("Acct-Session-Time = %ld\n",
574                                        now - rt.time);
575                         }
576
577                         if (rt.caller_id[0] != '\0') {
578                                 memcpy(nasname, rt.caller_id,
579                                        sizeof(rt.caller_id));
580                                 nasname[sizeof(rt.caller_id)] = '\0';
581
582                                 fr_print_string(nasname, 0, buffer,
583                                                  sizeof(buffer));
584                                 printf("Calling-Station-Id = \"%s\"\n", buffer);
585                         }
586
587                         printf("\n"); /* separate entries with a blank line */
588                         continue;
589                 }
590
591                 /*
592                  *      Show the fill name, or not.
593                  */
594                 if (showname) {
595                         printf((rawoutput == 0? rfmt1: rfmt1r),
596                                rt.login,
597                                showcid ? rt.caller_id :
598                                (showsid? session_id : fullname(rt.login)),
599                                proto(rt.proto, rt.porttype),
600                                portind, portno,
601                                dotime(rt.time),
602                                hostname(nasname, sizeof(nasname), rt.nas_address),
603                                hostname(othername, sizeof(othername), rt.framed_address), eol);
604                 } else {
605                         printf((rawoutput == 0? rfmt2: rfmt2r),
606                                rt.login,
607                                portind, portno,
608                                proto(rt.proto, rt.porttype),
609                                dotime(rt.time),
610                                hostname(nasname, sizeof(nasname), rt.nas_address),
611                                hostname(othername, sizeof(othername), rt.framed_address),
612                                eol);
613                 }
614         }
615         fclose(fp);
616
617         return 0;
618 }
619