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