home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume39 / tcp_wrappers / part03 / percent_x.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-29  |  2.5 KB  |  88 lines

  1.  /*
  2.   * percent_x() takes a string and performs %a (host address), %c (client
  3.   * info), %h (host name or address), %d (daemon name), %p (process id) and
  4.   * %u (user name) substitutions. It aborts the program when the result of
  5.   * expansion would overflow the output buffer. Because the result of %<char>
  6.   * expansion is typically passed on to a shell process, characters that may
  7.   * confuse the shell are replaced by underscores.
  8.   * 
  9.   * Diagnostics are reported through syslog(3).
  10.   * 
  11.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  12.   */
  13.  
  14. #ifndef lint
  15. static char sccsid[] = "@(#) percent_x.c 1.3 93/09/11 20:45:38";
  16. #endif
  17.  
  18. /* System libraries. */
  19.  
  20. #include <stdio.h>
  21. #include <syslog.h>
  22.  
  23. extern char *strncpy();
  24. extern char *strchr();
  25. extern void exit();
  26.  
  27. /* Local stuff. */
  28.  
  29. #include "log_tcp.h"
  30.  
  31. /* percent_x - do %<char> expansion, abort if result buffer is too small */
  32.  
  33. void    percent_x(result, result_len, str, daemon, client, pid)
  34. char   *result;
  35. int     result_len;
  36. char   *str;
  37. char   *daemon;
  38. struct client_info *client;
  39. int     pid;
  40. {
  41.     char   *end = result + result_len - 1;    /* end of result buffer */
  42.     char   *expansion;
  43.     int     expansion_len;
  44.     char    pid_buf[10];
  45.     static char ok_chars[] = "1234567890!@%-_=+\\:,./\
  46. abcdefghijklmnopqrstuvwxyz\
  47. ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  48.     char   *cp;
  49.  
  50.     /*
  51.      * %a becomes the client address; %c all user and host information we
  52.      * have about the client; %d the daemon process name; %h the client host
  53.      * name or address; %p the daemon process id; %u the remote user name; %%
  54.      * becomes a %, and %other is ignored. We terminate with a diagnostic if
  55.      * we would overflow the result buffer. Characters that may confuse the
  56.      * shell are mapped to underscores.
  57.      */
  58.  
  59.     while (*str) {
  60.     if (*str == '%') {
  61.         str++;
  62.         expansion =
  63.         *str == 'a' ? (str++, client->addr) :
  64.         *str == 'c' ? (str++, hosts_info(client)) :
  65.         *str == 'd' ? (str++, daemon) :
  66.         *str == 'h' ? (str++, FROM_HOST(client)) :
  67.         *str == 'p' ? (str++, sprintf(pid_buf, "%d", pid), pid_buf) :
  68.         *str == 'u' ? (str++, client->user) :
  69.         *str == '%' ? (str++, "%") :
  70.         *str == 0 ? "" : (str++, "");
  71.         expansion_len = strlen(expansion);
  72.         for (cp = expansion; *cp; cp++)
  73.         if (strchr(ok_chars, *cp) == 0)
  74.             *cp = '_';
  75.     } else {
  76.         expansion = str++;
  77.         expansion_len = 1;
  78.     }
  79.     if (result + expansion_len >= end) {
  80.         syslog(LOG_ERR, "shell command too long: %30s...", result);
  81.         exit(0);
  82.     }
  83.     strncpy(result, expansion, expansion_len);
  84.     result += expansion_len;
  85.     }
  86.     *result = 0;
  87. }
  88.