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