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