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