All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
radwho.c
Go to the documentation of this file.
1 /*@-skipposixheaders@*/
2 /*
3  * radwho.c Show who is logged in on the terminal servers.
4  *
5  * Version: $Id: 895807dce431321b4b5d8d6828c91beec72d051c $
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: 895807dce431321b4b5d8d6828c91beec72d051c $")
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 
58 
59 /*
60  * Global, for log.c to use.
61  */
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;
80 
81 static const CONF_PARSER module_config[] = {
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  fr_dict_t *dict = NULL;
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  */
295  fr_perror("radwho");
296  return 1;
297  }
298 
299  if (fr_dict_init(NULL, &dict, dict_dir, RADIUS_DICTIONARY, "radius") < 0) {
300  fr_perror("radwho");
301  return 1;
302  }
303 
304  if (fr_dict_read(dict, 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_snprint(buffer, sizeof(buffer), nasname, -1, '"');
446  printf("User-Name = \"%s\"\n", buffer);
447 
448  fr_snprint(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_snprint(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  talloc_free(dict);
552 
553  return 0;
554 }
int type
Definition: radutmp.h:64
char login[32]
Definition: radutmp.h:54
#define PW_TYPE_FILE_INPUT
File matching value must exist, and must be readable.
Definition: conffile.h:204
static char const * dict_dir
Definition: radwho.c:53
static int showname
Definition: radwho.c:45
static char const * proto(int id, int porttype)
Definition: radwho.c:107
Main server configuration.
Definition: radiusd.h:108
int fr_dict_read(fr_dict_t *dict, char const *dir, char const *filename)
Definition: dict.c:2291
char const * radutmp_fn
Definition: radwho.c:78
static char const * name
#define RADUTMP
Definition: conf.h:12
#define CONF_PARSER_TERMINATOR
Definition: conffile.h:289
static int showcid
Definition: radwho.c:47
static char const * eol
Definition: radwho.c:44
int cf_file_read(CONF_SECTION *cs, char const *file)
Definition: conffile.c:3421
char const * inet_ntop(int af, void const *src, char *dst, size_t cnt)
Definition: missing.c:538
pid_t rad_fork(void)
Definition: radwho.c:65
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition: snprintf.c:686
unsigned int nas_address
Definition: radutmp.h:59
static char const * raddb_dir
Definition: radwho.c:52
static char const * hostname(char *buf, size_t buflen, uint32_t ipaddr)
Definition: radwho.c:149
#define rad_waitpid(a, b)
Definition: radiusd.h:564
char session_id[8]
Definition: radutmp.h:57
unsigned int nas_port
Definition: radutmp.h:56
Defines a CONF_PAIR to C data type mapping.
Definition: conffile.h:267
char porttype
Definition: radutmp.h:65
size_t fr_snprint(char *out, size_t outlen, char const *in, ssize_t inlen, char quote)
Escape any non printable or non-UTF8 characters in the input string.
Definition: print.c:179
int fr_fault_setup(char const *cmd, char const *program)
Registers signal handlers to execute panic_action on fatal signal.
Definition: debug.c:890
static char * dotime(time_t t)
Definition: radwho.c:130
char const * radlog_dir
Definition: radwho.c:49
Vendors and attribute names.
Definition: dict.c:61
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition: log.c:238
unsigned int framed_address
Definition: radutmp.h:60
static void NEVER_RETURNS usage(int status)
Definition: radwho.c:165
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition: version.c:38
char caller_id[16]
Definition: radutmp.h:67
time_t time
Definition: radutmp.h:62
static const CONF_PARSER module_config[]
Definition: radwho.c:81
int cf_section_parse(CONF_SECTION *, void *base, CONF_PARSER const *variables)
Parse a configuration section into user-supplied variables.
Definition: conffile.c:2234
void void fr_perror(char const *,...) CC_HINT(format(printf
int proto
Definition: radutmp.h:61
#define RADIUS_DICTIONARY
Definition: conf.h:7
static int showptype
Definition: radwho.c:46
main_config_t main_config
Main server configuration.
Definition: radwho.c:62
static char const * radutmp_file
Definition: radwho.c:51
static char const * hdr2
Definition: radwho.c:41
int inet_pton(int af, char const *src, void *dst)
Definition: missing.c:522
char const * fr_strerror(void)
Get the last library error.
Definition: log.c:212
CONF_SECTION * cf_section_sub_find(CONF_SECTION const *, char const *name)
Find a sub-section in a section.
Definition: conffile.c:3708
static char const * hdr1
Definition: radwho.c:38
bool log_stripped_names
Definition: radwho.c:57
#define P_LOGIN
Definition: radutmp.h:51
int strncasecmp(char *s1, char *s2, int n)
Definition: missing.c:43
CONF_SECTION * cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
Allocate a CONF_SECTION.
Definition: conffile.c:626
static struct radutmp_config_t radutmpconfig
char const * radacct_dir
Definition: radwho.c:55
static char * fullname(char *username)
Definition: radwho.c:89
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition: strlcpy.c:38
int main(int argc, char **argv)
Definition: radwho.c:193
#define NEVER_RETURNS
Definition: libradius.h:133
#define FR_CONF_POINTER(_n, _t, _p)
Definition: conffile.h:172
CONF_SECTION * cf_section_sub_find_name2(CONF_SECTION const *, char const *name1, char const *name2)
Find a CONF_SECTION with both names.
Definition: conffile.c:3728
#define RCSID(id)
Definition: build.h:135
#define RADIUS_DIR
Definition: conf.h:3
int fr_dict_init(TALLOC_CTX *ctx, fr_dict_t **out, char const *dir, char const *fn, char const *name)
(re)initialize a protocol dictionary
Definition: dict.c:2148
#define RADIUSD_MAGIC_NUMBER
Definition: libradius.h:51
static char const * progname
Definition: radwho.c:48