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