Remove redundant file from freeradius-abfab list.
[freeradius.git] / src / main / radwho.c
1 /*@-skipposixheaders@*/
2 /*
3  * radwho.c     Show who is logged in on the terminal servers.
4  *
5  * Version:     $Id$
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * Copyright 2000,2006  The FreeRADIUS server project
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  */
24
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/sysutmp.h>
29 #include <freeradius-devel/radutmp.h>
30
31 #include <pwd.h>
32 #include <sys/stat.h>
33 #include <ctype.h>
34
35 /*
36  *      Header above output and format.
37  */
38 static char const *hdr1 =
39 "Login      Name              What  TTY  When      From     Location";
40
41 static char const *hdr2 =
42 "Login      Port    What      When        From      Location";
43
44 static char const *eol = "\n";
45 static int showname = -1;
46 static int showptype = 0;
47 static int showcid = 0;
48 char const *progname = "radwho";
49 char const *radlog_dir = NULL;
50
51 static char const *radutmp_file = NULL;
52 static char const *raddb_dir = RADDBDIR;
53 static char const *dict_dir = DICTDIR;
54
55 char const *radacct_dir = NULL;
56 char const *radlib_dir = NULL;
57
58 bool log_stripped_names;
59
60 /*
61  *      Global, for log.c to use.
62  */
63 main_config_t main_config;
64
65 #include <sys/wait.h>
66 pid_t rad_fork(void)
67 {
68         return fork();
69 }
70
71 #ifdef HAVE_PTHREAD_H
72 pid_t rad_waitpid(pid_t pid, int *status)
73 {
74         return waitpid(pid, status, 0);
75 }
76 #endif
77
78 static struct radutmp_config_t {
79         char const *radutmp_fn;
80 } radutmpconfig;
81
82 static const CONF_PARSER module_config[] = {
83         { "filename", FR_CONF_POINTER(PW_TYPE_FILE_INPUT, &radutmpconfig.radutmp_fn), RADUTMP },
84         CONF_PARSER_TERMINATOR
85 };
86
87 /*
88  *      Get fullname of a user.
89  */
90 static char *fullname(char *username)
91 {
92 #ifdef HAVE_PWD_H
93         struct passwd *pwd;
94         char *s;
95
96         if ((pwd = getpwnam(username)) != NULL) {
97                 if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
98                 return pwd->pw_gecos;
99         }
100 #endif
101
102         return username;
103 }
104
105 /*
106  *      Return protocol type.
107  */
108 static char const *proto(int id, int porttype)
109 {
110         static char buf[8];
111
112         if (showptype) {
113                 if (!strchr("ASITX", porttype))
114                         porttype = ' ';
115                 if (id == 'S')
116                         snprintf(buf, sizeof(buf), "SLP %c", porttype);
117                 else if (id == 'P')
118                         snprintf(buf, sizeof(buf), "PPP %c", porttype);
119                 else
120                         snprintf(buf, sizeof(buf), "shl %c", porttype);
121                 return buf;
122         }
123         if (id == 'S') return "SLIP";
124         if (id == 'P') return "PPP";
125         return "shell";
126 }
127
128 /*
129  *      Return a time in the form day hh:mm
130  */
131 static char *dotime(time_t t)
132 {
133         char *s = ctime(&t);
134
135         if (showname) {
136                 strlcpy(s + 4, s + 11, 6);
137                 s[9] = 0;
138         } else {
139                 strlcpy(s + 4, s + 8, 9);
140                 s[12] = 0;
141         }
142
143         return s;
144 }
145
146
147 /*
148  *      Print address of NAS.
149  */
150 static char const *hostname(char *buf, size_t buflen, uint32_t ipaddr)
151 {
152         /*
153          *      WTF is this code for?
154          */
155         if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
156                 return "";
157
158         return inet_ntop(AF_INET, &ipaddr, buf, buflen);
159
160 }
161
162
163 /*
164  *      Print usage message and exit.
165  */
166 static void NEVER_RETURNS usage(int status)
167 {
168         FILE *output = status?stderr:stdout;
169
170         fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
171         fprintf(output, "  -c                   Show caller ID, if available.\n");
172         fprintf(output, "  -d                   Set the raddb directory (default is %s).\n", RADIUS_DIR);
173         fprintf(output, "  -F <file>            Use radutmp <file>.\n");
174         fprintf(output, "  -i                   Show session ID.\n");
175         fprintf(output, "  -n                   No full name.\n");
176         fprintf(output, "  -N <nas-ip-address>  Show entries matching the given NAS IP address.\n");
177         fprintf(output, "  -p                   Show port type.\n");
178         fprintf(output, "  -P <port>            Show entries matching the given nas port.\n");
179         fprintf(output, "  -r                   Print output as raw comma-delimited data.\n");
180         fprintf(output, "  -R                   Print output as RADIUS attributes and values.\n");
181         fprintf(output, "                       includes ALL information from the radutmp record.\n");
182         fprintf(output, "  -s                   Show full name.\n");
183         fprintf(output, "  -S                   Hide shell users from radius.\n");
184         fprintf(output, "  -u <user>            Show entries matching the given user.\n");
185         fprintf(output, "  -U <user>            Like -u, but case-sensitive.\n");
186         fprintf(output, "  -Z                   Include accounting stop information in radius output.  Requires -R.\n");
187         exit(status);
188 }
189
190
191 /*
192  *      Main program
193  */
194 int main(int argc, char **argv)
195 {
196         CONF_SECTION *maincs, *cs;
197         FILE *fp;
198         struct radutmp rt;
199         char othername[256];
200         char nasname[1024];
201         char session_id[sizeof(rt.session_id)+1];
202         int hideshell = 0;
203         int showsid = 0;
204         int rawoutput = 0;
205         int radiusoutput = 0;   /* Radius attributes */
206         char const *portind;
207         int c;
208         unsigned int portno;
209         char buffer[2048];
210         char const *user = NULL;
211         int user_cmp = 0;
212         time_t now = 0;
213         uint32_t nas_port = ~0;
214         uint32_t nas_ip_address = INADDR_NONE;
215         int zap = 0;
216
217         raddb_dir = RADIUS_DIR;
218
219 #ifndef NDEBUG
220         if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
221                 fr_perror("radwho");
222                 exit(EXIT_FAILURE);
223         }
224 #endif
225
226         talloc_set_log_stderr();
227
228         while((c = getopt(argc, argv, "d:D:fF:nN:sSipP:crRu:U:Z")) != EOF) switch (c) {
229                 case 'd':
230                         raddb_dir = optarg;
231                         break;
232                 case 'D':
233                         dict_dir = optarg;
234                         break;
235                 case 'F':
236                         radutmp_file = optarg;
237                         break;
238                 case 'h':
239                         usage(0);       /* never returns */
240
241                 case 'S':
242                         hideshell = 1;
243                         break;
244                 case 'n':
245                         showname = 0;
246                         break;
247                 case 'N':
248                         if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
249                                 usage(1);
250                         }
251                         break;
252                 case 's':
253                         showname = 1;
254                         break;
255                 case 'i':
256                         showsid = 1;
257                         break;
258                 case 'p':
259                         showptype = 1;
260                         break;
261                 case 'P':
262                         nas_port = atoi(optarg);
263                         break;
264                 case 'c':
265                         showcid = 1;
266                         showname = 1;
267                         break;
268                 case 'r':
269                         rawoutput = 1;
270                         break;
271                 case 'R':
272                         radiusoutput = 1;
273                         now = time(NULL);
274                         break;
275                 case 'u':
276                         user = optarg;
277                         user_cmp = 0;
278                         break;
279                 case 'U':
280                         user = optarg;
281                         user_cmp = 1;
282                         break;
283                 case 'Z':
284                         zap = 1;
285                         break;
286
287                 default:
288                         usage(1);       /* never returns */
289         }
290
291         /*
292          *      Mismatch between the binary and the libraries it depends on
293          */
294         if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
295                 fr_perror("radwho");
296                 return 1;
297         }
298
299         if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
300                 fr_perror("radwho");
301                 return 1;
302         }
303
304         if (dict_read(raddb_dir, RADIUS_DICTIONARY) == -1) {
305                 fr_perror("radwho");
306                 return 1;
307         }
308         fr_strerror();  /* Clear the error buffer */
309
310         /*
311          *      Be safe.
312          */
313         if (zap && !radiusoutput) zap = 0;
314
315         /*
316          *      zap EVERYONE, but only on this nas
317          */
318         if (zap && !user && (~nas_port == 0)) {
319                 /*
320                  *      We need to know which NAS to zap users in.
321                  */
322                 if (nas_ip_address == INADDR_NONE) usage(1);
323
324                 printf("Acct-Status-Type = Accounting-Off\n");
325                 printf("NAS-IP-Address = %s\n",
326                        hostname(buffer, sizeof(buffer), nas_ip_address));
327                 printf("Acct-Delay-Time = 0\n");
328                 exit(0);        /* don't bother printing anything else */
329         }
330
331         if (radutmp_file) goto have_radutmp;
332
333         /*
334          *      Initialize main_config
335          */
336         memset(&main_config, 0, sizeof(main_config));
337
338         /* Read radiusd.conf */
339         maincs = cf_section_alloc(NULL, "main", NULL);
340         if (!maincs) exit(1);
341
342         snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", raddb_dir);
343         if (cf_file_read(maincs, buffer) < 0) {
344                 fprintf(stderr, "%s: Error reading or parsing radiusd.conf\n", argv[0]);
345                 talloc_free(maincs);
346                 exit(1);
347         }
348
349         cs = cf_section_sub_find(maincs, "modules");
350         if (!cs) {
351                 fprintf(stderr, "%s: No modules section found in radiusd.conf\n", argv[0]);
352                 exit(1);
353         }
354         /* Read the radutmp section of radiusd.conf */
355         cs = cf_section_sub_find_name2(cs, "radutmp", NULL);
356         if (!cs) {
357                 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf\n", argv[0]);
358                 exit(1);
359         }
360
361         cf_section_parse(cs, NULL, module_config);
362
363         /* Assign the correct path for the radutmp file */
364         radutmp_file = radutmpconfig.radutmp_fn;
365
366  have_radutmp:
367         if (showname < 0) showname = 1;
368
369         /*
370          *      Show the users logged in on the terminal server(s).
371          */
372         if ((fp = fopen(radutmp_file, "r")) == NULL) {
373                 fprintf(stderr, "%s: Error reading %s: %s\n",
374                         progname, radutmp_file, fr_syserror(errno));
375                 return 0;
376         }
377
378         /*
379          *      Don't print the headers if raw or RADIUS
380          */
381         if (!rawoutput && !radiusoutput) {
382                 fputs(showname ? hdr1 : hdr2, stdout);
383                 fputs(eol, stdout);
384         }
385
386         /*
387          *      Read the file, printing out active entries.
388          */
389         while (fread(&rt, sizeof(rt), 1, fp) == 1) {
390                 char name[sizeof(rt.login) + 1];
391
392                 if (rt.type != P_LOGIN) continue; /* hide logout sessions */
393
394                 /*
395                  *      We don't show shell users if we are
396                  *      fingerd, as we have done that above.
397                  */
398                 if (hideshell && !strchr("PCS", rt.proto))
399                         continue;
400
401                 /*
402                  *      Print out sessions only for the given user.
403                  */
404                 if (user) {     /* only for a particular user */
405                         if (((user_cmp == 0) &&
406                              (strncasecmp(rt.login, user, strlen(user)) != 0)) ||
407                             ((user_cmp == 1) &&
408                              (strncmp(rt.login, user, strlen(user)) != 0))) {
409                                 continue;
410                         }
411                 }
412
413                 /*
414                  *      Print out only for the given NAS port.
415                  */
416                 if (~nas_port != 0) {
417                         if (rt.nas_port != nas_port) continue;
418                 }
419
420                 /*
421                  *      Print out only for the given NAS IP address
422                  */
423                 if (nas_ip_address != INADDR_NONE) {
424                         if (rt.nas_address != nas_ip_address) continue;
425                 }
426
427                 memcpy(session_id, rt.session_id, sizeof(rt.session_id));
428                 session_id[sizeof(rt.session_id)] = 0;
429
430                 if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
431                         portind = ">";
432                         portno = (showname ? 999 : 99999);
433                 } else {
434                         portind = "S";
435                         portno = rt.nas_port;
436                 }
437
438                 /*
439                  *      Print output as RADIUS attributes
440                  */
441                 if (radiusoutput) {
442                         memcpy(nasname, rt.login, sizeof(rt.login));
443                         nasname[sizeof(rt.login)] = '\0';
444
445                         fr_prints(buffer, sizeof(buffer), nasname, -1, '"');
446                         printf("User-Name = \"%s\"\n", buffer);
447
448                         fr_prints(buffer, sizeof(buffer), session_id, -1, '"');
449                         printf("Acct-Session-Id = \"%s\"\n", buffer);
450
451                         if (zap) printf("Acct-Status-Type = Stop\n");
452
453                         printf("NAS-IP-Address = %s\n",
454                                hostname(buffer, sizeof(buffer),
455                                         rt.nas_address));
456                         printf("NAS-Port = %u\n", rt.nas_port);
457
458                         switch (rt.proto) {
459                         case 'S':
460                                 printf("Service-Type = Framed-User\n");
461                                 printf("Framed-Protocol = SLIP\n");
462                                 break;
463
464                         case 'P':
465                                 printf("Service-Type = Framed-User\n");
466                                 printf("Framed-Protocol = PPP\n");
467                                 break;
468
469                         default:
470                                 printf("Service-type = Login-User\n");
471                                 break;
472                         }
473                         if (rt.framed_address != INADDR_NONE) {
474                                 printf("Framed-IP-Address = %s\n",
475                                        hostname(buffer, sizeof(buffer),
476                                                 rt.framed_address));
477                         }
478
479                         /*
480                          *      Some sanity checks on the time
481                          */
482                         if ((rt.time <= now) &&
483                             (now - rt.time) <= (86400 * 365)) {
484                                 printf("Acct-Session-Time = %" PRId64 "\n", (int64_t) (now - rt.time));
485                         }
486
487                         if (rt.caller_id[0] != '\0') {
488                                 memcpy(nasname, rt.caller_id,
489                                        sizeof(rt.caller_id));
490                                 nasname[sizeof(rt.caller_id)] = '\0';
491
492                                 fr_prints(buffer, sizeof(buffer), nasname, -1, '"');
493                                 printf("Calling-Station-Id = \"%s\"\n", buffer);
494                         }
495
496                         printf("\n"); /* separate entries with a blank line */
497                         continue;
498                 }
499
500                 /*
501                  *      Show the fill name, or not.
502                  */
503                 memcpy(name, rt.login, sizeof(rt.login));
504                 name[sizeof(rt.login)] = '\0';
505
506                 if (showname) {
507                         if (rawoutput == 0) {
508                                 printf("%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-15.15s %-.19s%s",
509                                        name,
510                                        showcid ? rt.caller_id :
511                                        (showsid? session_id : fullname(rt.login)),
512                                        proto(rt.proto, rt.porttype),
513                                        portind, portno,
514                                        dotime(rt.time),
515                                        hostname(nasname, sizeof(nasname), rt.nas_address),
516                                        hostname(othername, sizeof(othername), rt.framed_address), eol);
517                         } else {
518                                 printf("%s,%s,%s,%s%u,%s,%s,%s%s",
519                                        name,
520                                        showcid ? rt.caller_id :
521                                        (showsid? session_id : fullname(rt.login)),
522                                        proto(rt.proto, rt.porttype),
523                                        portind, portno,
524                                        dotime(rt.time),
525                                        hostname(nasname, sizeof(nasname), rt.nas_address),
526                                        hostname(othername, sizeof(othername), rt.framed_address), eol);
527                         }
528                 } else {
529                         if (rawoutput == 0) {
530                                 printf("%-10.10s %s%-5u  %-6.6s %-13.13s %-15.15s %-.28s%s",
531                                        name,
532                                        portind, portno,
533                                        proto(rt.proto, rt.porttype),
534                                        dotime(rt.time),
535                                        hostname(nasname, sizeof(nasname), rt.nas_address),
536                                        hostname(othername, sizeof(othername), rt.framed_address),
537                                        eol);
538                         } else {
539                                 printf("%s,%s%u,%s,%s,%s,%s%s",
540                                        name,
541                                        portind, portno,
542                                        proto(rt.proto, rt.porttype),
543                                        dotime(rt.time),
544                                        hostname(nasname, sizeof(nasname), rt.nas_address),
545                                        hostname(othername, sizeof(othername), rt.framed_address),
546                                        eol);
547                         }
548                 }
549         }
550         fclose(fp);
551
552         return 0;
553 }