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