home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / mail / sendmail / UCB / sendmail_wrapper.c.Z / sendmail_wrapper.c
Encoding:
C/C++ Source or Header  |  1995-02-22  |  5.6 KB  |  198 lines

  1. /*
  2. **  sendmail_wrapper.c - wrap sendmail to prevent newlines in command line
  3. **                       and clean up the environment.
  4. **
  5. **  Authors:    Eric Halil, Danny Smith
  6. **              AUSCERT
  7. **              c/o Prentice Centre
  8. **              The University of Queensland
  9. **              Qld.  4072.
  10. **              Australia
  11. **              22-Feb-1995
  12. **
  13. **  Disclaimer:  The use of this program is at your own risk.  It is 
  14. **               designed to combat a particular vulnerability, and may
  15. **               not combat other vulnerabilities, either past or future.
  16. **               The decision to use this program is yours, as are the
  17. **               consequences of its use.
  18. **
  19. **               This program is designed to be an interim relief measure
  20. **               until appropriate patches can be obtained from your vendor.
  21. **
  22. **  Installation instructions
  23. **  =========================
  24. **
  25. **  1.  su to root.
  26. **
  27. **  2.  Determine the location of sendmail.  On SunOS and Ultrix
  28. **      systems, it is located in the /usr/lib directory.  On BSDI
  29. **      systems, it is located in the /usr/sbin directory.  For example
  30. **      purposes only, /usr/lib will be used in the following instructions
  31. **      steps.
  32. **
  33. **  3.  Copy the sendmail program to sendmail.real.  Change the permissions
  34. **      on the copy of sendmail.
  35. **
  36. **              # cd /usr/lib
  37. **              # cp sendmail sendmail.real
  38. **              # chmod 0700 sendmail.real
  39. **
  40. **  4.  Determine the permissions, owner, and group of sendmail.  This
  41. **      information will be used later.
  42. **
  43. **      For BSD users:
  44. **              # ls -lg sendmail
  45. **      For System V users:
  46. **              # ls -l sendmail
  47. **
  48. **  5.  Edit this wrapper program and define REAL_SENDMAIL.  By default,
  49. **      REAL_SENDMAIL is defined as "/usr/lib/sendmail.real".
  50. **
  51. **  6.  Compile this program in a directory other than /usr/lib.  For
  52. **      example to use /tmp, first copy this file into /tmp.
  53. **
  54. **              # cd /tmp
  55. **              # cc -O -o sendmail sendmail_wrapper.c
  56. **
  57. **  7.  Copy this new wrapper program into the directory containing sendmail.
  58. **      Make sure this directory and its parent directories are protected so
  59. **      only root is able to make changes to files in the directory.  This
  60. **      will replace the existing sendmail.  The following steps should be
  61. **      executed quickly.
  62. **
  63. **      Users will not be able to send e-mail during the time when the
  64. **      wrapper is copied into place until the chmod command has been
  65. **      executed.  Use the information from step #4 and set the permissions
  66. **      owner, and group of the new sendmail.
  67. **
  68. **              # cp sendmail /usr/lib/sendmail
  69. **              # cd /usr/lib
  70. **              # chown root sendmail
  71. **              # chmod 4511 sendmail
  72. **
  73. **  8.  Kill the running sendmail process and start the new sendmail.
  74. **
  75. **      For SunOS and Ultrix:
  76. **              # kill -9 `head -1 /etc/sendmail.pid`
  77. **              # /usr/lib/sendmail -bd -q1h
  78. **
  79. **      For BSDI:
  80. **              # kill -9 `head -1 /var/run/sendmail.pid`
  81. **              # /usr/sbin/sendmail -bd -q1h
  82. **
  83. **      For other systems, follow your vendors guidelines or use the
  84. **      following command.  Kill the processes and start the new sendmail.
  85. **              # ps -auxw | grep sendmail | grep -v grep
  86. **              # kill -9 (process id numbers)
  87. **              # ./sendmail -bd -q1h
  88. **
  89. **  9.  Test that mail still works.
  90.  
  91. ** Version 1.1  22-Feb-1995.
  92. */
  93.  
  94. #include <stdio.h>
  95.  
  96. /*
  97. **      REAL_SENDMAIL needs to be defined using the full pathname
  98. **      of the real sendmail.  A few known locations have been defined.
  99. */
  100.  
  101. #ifdef sun
  102. #define REAL_SENDMAIL   "/usr/lib/sendmail.real"
  103. #endif
  104.  
  105. #ifdef ultrix
  106. #define REAL_SENDMAIL   "/usr/lib/sendmail.real"
  107. #endif
  108.  
  109. #if defined (__bsdi__) || defined(__386BSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
  110.  
  111. #define REAL_SENDMAIL   "/usr/sbin/sendmail.real"
  112. #endif
  113.  
  114. int main( argc, argv, envp)
  115. int     argc;
  116. char    *argv[];
  117. char    *envp[];
  118. {
  119.     char        *cp;
  120.     int         i;
  121.     int         j;
  122.     int         status;
  123.  
  124. /*
  125. **  Ensure that there are no newlines in the arguments
  126. */
  127.     for ( i = 1; i < argc; i++)
  128.     {
  129.         for ( cp = argv[ i]; *cp != '\0'; cp++)
  130.         {
  131.             if ( ( *cp == '\r') || ( *cp == '\n'))
  132.             {
  133.                 *cp = ' ';
  134.             }
  135.         }
  136.     }
  137.  
  138. /*
  139. **  While we are at it, let's clean up the environment
  140. **  Remove LD_*, IFS, and PATH enviroment variables before execing
  141. */
  142.     i = 0;
  143.     while( envp[ i] != NULL)
  144.     {
  145.         if ( strncmp( envp[ i], "LD_", 3) == 0)
  146.         {
  147.             j = i;
  148.             while ( envp[ j] != NULL)
  149.             {
  150.                 envp[ j] = envp[ j + 1];
  151.                 j++;
  152.             }
  153.             continue;
  154.         }
  155.         if ( strncmp( envp[ i], "IFS=", 4) == 0)
  156.         {
  157.             j = i;
  158.             while ( envp[ j] != NULL)
  159.             {
  160.                 envp[ j] = envp[ j + 1];
  161.                 j++;
  162.             }
  163.             continue;
  164.         }
  165.         if ( strncmp( envp[ i], "PATH=", 5) == 0)
  166.         {
  167.             j = i;
  168.             while ( envp[ j] != NULL)
  169.             {
  170.                 envp[ j] = envp[ j + 1];
  171.                 j++;
  172.             }
  173.             continue;
  174.         }
  175. /*
  176. **  Now check for newlines in environment variables
  177. */
  178.         for ( cp = envp[ i]; *cp != '\0'; cp++)
  179.         {
  180.             if ( ( *cp == '\r') || ( *cp == '\n'))
  181.             {
  182.                 *cp = ' ';
  183.             }
  184.         }
  185. /*
  186. **  next environment variable
  187. */
  188.         i++;
  189.     }
  190.  
  191. /*
  192. ** exec the real sendmail now
  193. */
  194.     status = execve( REAL_SENDMAIL, argv, envp);
  195.     perror( "execve sendmail");
  196.     return( status);
  197. }
  198.