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