Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

spp_httpinspect.c

Go to the documentation of this file.
00001 /**
00002 **  @file       preproc_setup.c
00003 **  
00004 **  @author     Daniel Roelker <droelker@sourcefire.com>
00005 **
00006 **  @brief      This file initializes HttpInspect as a Snort 
00007 **              preprocessor.
00008 **
00009 **  This file registers the HttpInspect initialization function,
00010 **  adds the HttpInspect function into the preprocessor list, reads
00011 **  the user configuration in the snort.conf file, and prints out
00012 **  the configuration that is read.
00013 **
00014 **  In general, this file is a wrapper to HttpInspect functionality,
00015 **  by interfacing with the Snort preprocessor functions.  The rest
00016 **  of HttpInspect should be separate from the preprocessor hooks.
00017 **
00018 **  NOTES
00019 **
00020 **  - 2.10.03:  Initial Development.  DJR
00021 */
00022 
00023 #include <string.h>
00024 #include <sys/types.h>
00025 
00026 #include "decode.h"
00027 #include "plugbase.h"
00028 #include "debug.h"
00029 #include "util.h"
00030 
00031 #include "hi_ui_config.h"
00032 #include "hi_client.h"
00033 #include "hi_norm.h"
00034 #include "snort_httpinspect.h"
00035 
00036 /*
00037 **  Defines for preprocessor initialization
00038 */
00039 /**
00040 **  snort.conf preprocessor keyword
00041 */
00042 #define GLOBAL_KEYWORD   "http_inspect"
00043 #define SERVER_KEYWORD   "http_inspect_server"
00044 
00045 /**
00046 **  The length of the error string buffer.
00047 */
00048 #define ERRSTRLEN 1000
00049 
00050 /*
00051 **  External Global Variables
00052 **  Variables that we need from Snort to log errors correctly and such.
00053 */
00054 extern char *file_name;
00055 extern char *file_line;
00056 extern HttpUri UriBufs[URI_COUNT];
00057 
00058 /*
00059 **  Global Variables
00060 **  This is the only way to work with Snort preprocessors because
00061 **  the user configuration must be kept between the Init function
00062 **  the actual preprocessor.  There is no interaction between the
00063 **  two except through global variable usage.
00064 */
00065 HTTPINSPECT_GLOBAL_CONF GlobalConf;
00066 
00067 /*
00068 **  NAME
00069 **    HttpInspect::
00070 */
00071 /**
00072 **  This function wraps the functionality in the generic HttpInspect
00073 **  processing.  We get a Packet structure and pass this into the
00074 **  HttpInspect module where the first stage in HttpInspect is the
00075 **  Session Inspection stage where most of the other Snortisms are
00076 **  taken care of.  After that, the modules should be fairly generic,
00077 **  and that's what we're trying to do here.
00078 **
00079 **  @param p a Packet structure that contains Snort info about the
00080 **  packet.
00081 **
00082 **  @return void
00083 */
00084 static void HttpInspect(Packet *p, void *context)
00085 {
00086     /*
00087     **  IMPORTANT:
00088     **  This is where we initialize any variables that can impact other
00089     **  aspects of detection/processing.
00090     **
00091     **  First thing that we do is reset the p->uri_count to zero, so there
00092     **  is no way that we would inspect a buffer that was completely bogus.
00093     */
00094     p->uri_count = 0;
00095     UriBufs[0].decode_flags = 0;
00096 
00097     /*
00098     **  Check for valid packet
00099     **  if neither header or data is good, then we just abort.
00100     */
00101     if(!p->iph || !p->tcph || !p->data || !p->dsize)
00102     {
00103         return;
00104     }
00105 
00106     if(!(p->preprocessors & PP_HTTPINSPECT))
00107         return;
00108 
00109     /*
00110     **  Pass in the configuration and the packet.
00111     */
00112     SnortHttpInspect(&GlobalConf, p);
00113 
00114     p->uri_count = 0;
00115     UriBufs[0].decode_flags = 0;
00116 
00117     return;
00118 }
00119 
00120 /*
00121 **  NAME
00122 **    HttpInspectInit::
00123 */
00124 /**
00125 **  This function initializes HttpInspect with a user configuration.
00126 **
00127 **  The function is called when HttpInspect is configured in 
00128 **  snort.conf.  It gets passed a string of arguments, which gets
00129 **  parsed into configuration constructs that HttpInspect understands.
00130 **
00131 **  This function gets called for every HttpInspect configure line.  We
00132 **  use this characteristic to split up the configuration, so each line
00133 **  is a configuration construct.  We need to keep track of what part
00134 **  of the configuration has been configured, so we don't configure one
00135 **  part, then configure it again.
00136 **
00137 **  Any upfront memory is allocated here (if necessary).
00138 **
00139 **  @param args a string to the preprocessor arguments.
00140 **
00141 **  @return void
00142 */
00143 static void HttpInspectInit(u_char *args)
00144 {
00145     char ErrorString[ERRSTRLEN];
00146     int  iErrStrLen = ERRSTRLEN;
00147     int  iRet;
00148     static int siFirstConfig = 1;
00149     int  iGlobal = 0;
00150 
00151     if(siFirstConfig)
00152     {
00153         if((iRet = hi_ui_config_init_global_conf(&GlobalConf)))
00154         {
00155             snprintf(ErrorString, iErrStrLen,
00156                     "Error initializing Global Configuration.");
00157             FatalError("%s(%d) => %s\n", file_name, file_line, ErrorString);
00158 
00159             return;
00160         }
00161 
00162         if((iRet = hi_ui_config_default(&GlobalConf)))
00163         {
00164             snprintf(ErrorString, iErrStrLen,
00165                     "Error configuring default global configuration.");
00166             FatalError("%s(%d) => %s\n", file_name, file_line, ErrorString);
00167 
00168             return;
00169         }
00170 
00171         if((iRet = hi_client_init(&GlobalConf)))
00172         {
00173             snprintf(ErrorString, iErrStrLen,
00174                     "Error initializing client module.");
00175             FatalError("%s(%d) => %s\n", file_name, file_line, ErrorString);
00176 
00177             return;
00178         }
00179 
00180         if((iRet = hi_norm_init(&GlobalConf)))
00181         {
00182             snprintf(ErrorString, iErrStrLen,
00183                      "Error initializing normalization module.");
00184             FatalError("%s(%d) => %s\n", file_name, file_line, ErrorString);
00185 
00186             return;
00187         }
00188 
00189         /*
00190         **  We set the global configuration variable
00191         */
00192         iGlobal = 1;
00193     }
00194     
00195     if((iRet = HttpInspectSnortConf(&GlobalConf, args, iGlobal,
00196                     ErrorString, iErrStrLen)))
00197     {
00198         if(iRet > 0)
00199         {
00200             /*
00201             **  Non-fatal Error
00202             */
00203             if(ErrorString)
00204             {
00205                 ErrorMessage("%s(%d) => %s\n", 
00206                         file_name, file_line, ErrorString);
00207             }
00208         }
00209         else
00210         {
00211             /*
00212             **  Fatal Error, log error and exit.
00213             */
00214             if(ErrorString)
00215             {
00216                 FatalError("%s(%d) => %s\n", 
00217                         file_name, file_line, ErrorString);
00218             }
00219             else
00220             {
00221                 /*
00222                 **  Check if ErrorString is undefined.
00223                 */
00224                 if(iRet == -2)
00225                 {
00226                     FatalError("%s(%d) => ErrorString is undefined.\n", 
00227                             file_name, file_line);
00228                 }
00229                 else
00230                 {
00231                     FatalError("%s(%d) => Undefined Error.\n", 
00232                             file_name, file_line);
00233                 }
00234             }
00235         }
00236     }
00237 
00238     /*
00239     **  Only add the functions one time to the preproc list.
00240     */
00241     if(siFirstConfig)
00242     {
00243         /*
00244         **  Add HttpInspect into the preprocessor list
00245         */
00246         AddFuncToPreprocList(HttpInspect);
00247 
00248         /*
00249         **  Remember to add any cleanup functions into the appropriate
00250         **  lists.
00251         */
00252 
00253         siFirstConfig = 0;
00254     }
00255     
00256     return;
00257 }
00258 
00259 /*
00260 **  NAME
00261 **    SetupHttpInspect::
00262 */
00263 /**
00264 **  This function initializes HttpInspect as a Snort preprocessor.
00265 **
00266 **  It registers the preprocessor keyword for use in the snort.conf
00267 **  and sets up the initialization module for the preprocessor, in
00268 **  case it is configured.
00269 **
00270 **  This function must be called in InitPreprocessors() in plugbase.c
00271 **  in order to be recognized by Snort.
00272 **
00273 **  @param none
00274 **
00275 **  @return void
00276 */
00277 void SetupHttpInspect()
00278 {
00279     RegisterPreprocessor(GLOBAL_KEYWORD, HttpInspectInit);
00280     RegisterPreprocessor(SERVER_KEYWORD, HttpInspectInit);
00281 
00282     DEBUG_WRAP(DebugMessage(DEBUG_HTTPINSPECT, "Preprocessor: HttpInspect is "
00283                 "setup . . .\n"););
00284 }

Generated on Sun May 14 14:51:16 2006 by  doxygen 1.4.2