engine/parse_config.c

Go to the documentation of this file.
00001 #include "../config.h"
00002 #include "parse_config.h"
00003 #include "../packets/packet.h"
00004 #include "../routes/route.h"
00005 #include "../actions/action.h"
00006 #include "../engine/message.h"
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <stdlib.h>
00010 #ifdef _SOLARIS_
00011 #include <strings.h>
00012 #endif
00013 
00014 extern GlobalVars       Globals;
00015 
00016 //#define DEBUGPATH
00017 //#define DEBUG
00018 
00019 /****************************************
00020 * Get a line out of the rules file
00021 * TODO: Make this recursive
00022 ****************************************/
00023 int GetLine(FILE* fp, char* buff, int buff_len){
00024         int     Done;
00025         char    LineBuff[65536];
00026         char*   Begin;
00027         char*   End;
00028         
00029 #ifdef DEBUGPATH
00030         printf("In GetLine\n");
00031 #endif
00032         bzero(buff, buff_len);
00033         Done=FALSE;
00034         while (!Done){
00035                 if (!fgets(LineBuff, 65536, fp)) return FALSE;
00036                 if (LineBuff[0]=='#') continue;
00037                 
00038                 /*erase any whitespace at the front*/
00039                 Begin=LineBuff;
00040                 while (((*Begin==' ') || (*Begin=='\t')) && (*Begin!='\n') && (*Begin!='\0')) Begin++;
00041                 if (*Begin=='\0') continue;
00042                 if (*Begin=='\n') continue;
00043                 
00044                 /*erase the line feed at the end*/
00045                 End=Begin+strlen(Begin)-1;
00046                 if (*End=='\n'){
00047                         *End='\0';
00048                         End--;
00049                 }
00050                 
00051                 if (*End==0x09){
00052                         *End='\0';
00053                         End--;
00054                 }
00055                 
00056                 if (*End==';'){
00057                         *End='\0';
00058                         End--;
00059                 }
00060                 
00061                 /*if the line ends with a slash, read in the next line*/
00062                 if (*End=='\\'){
00063 #ifdef DEBUG1
00064                         printf("Line ends with a continuation character\n");
00065 #endif                  
00066                         if (!fgets(End-1, 65536, fp)) return FALSE;
00067                                         /*erase the line feed at the end*/
00068                         End=Begin+strlen(Begin)-1;
00069                         if (*End=='\n'){
00070                                 *End='\0';
00071                                 End--;
00072                         }
00073                 
00074                         if (*End==0x09){
00075                                 *End='\0';
00076                                 End--;
00077                         }
00078                 
00079                         if (*End==';'){
00080                                 *End='\0';
00081                                 End--;
00082                         }
00083 
00084                 }
00085                                 
00086                 snprintf(buff, buff_len, "%s", Begin);
00087                 return TRUE;
00088         }       
00089         
00090         return FALSE;
00091 }
00092 
00093 /***********************************
00094 * Make sense out of this list
00095 ***********************************/
00096 int ParseList(FILE* fp, char* Name, int ListType){
00097         char            LineBuff[10240];
00098         int             ListID;
00099         GlobalList*     List;
00100         
00101 #ifdef DEBUGPATH
00102         printf("In ParseList\n");
00103 #endif
00104 
00105         if (!Name) return FALSE;        
00106         while (*Name==' ') Name++;
00107 
00108 #ifdef DEBUG
00109         printf("Setting for list %s\n",Name);
00110 #endif
00111 
00112         ListID=GetListByName(Name);
00113         if (ListID!=LIST_NONE){
00114                 printf("There is already a list name \"%s\"\n",Name);
00115                 return FALSE;
00116         }
00117 
00118         List=&Globals.Lists[Globals.NumLists];
00119         
00120         List->List=InitNumList(LIST_TYPE_NORMAL);
00121         snprintf(List->Name, MAX_NAME_LEN, Name);
00122 #ifdef DEBUG
00123         printf("Setting list name to \"%s\"\n",List->Name);
00124 #endif  
00125 
00126         while(GetLine(fp, LineBuff, 10240)){
00127                 if (*LineBuff=='#') continue;
00128                 if (strcasecmp(LineBuff, "</list>")==0){
00129 #ifdef DEBUG
00130                         printf("All done with list \"%s\"\n",List->Name);
00131 #endif                  
00132                         Globals.NumLists++;
00133                         return TRUE;
00134                 }else{
00135                         switch(ListType){
00136                         case LIST_TYPE_IP:
00137                                 if (!AddIPRanges(List->List, LineBuff)){
00138                                         printf("I couldn't understand ip list %s\n",LineBuff);
00139                                         return FALSE;
00140                                 }
00141 #ifdef DEBUG
00142                                 printf("Added %s to ip list %s\n",LineBuff, List->Name);
00143 #endif                          
00144                                 break;
00145                         default:
00146                                 printf("I don't understand that list type\n");
00147                                 return FALSE;
00148                         }
00149                 }
00150         }
00151 
00152 
00153         return FALSE;
00154 }
00155 
00156 
00157 /***********************************
00158 * Make sense out of this action
00159 ***********************************/
00160 int ParseAction(FILE* fp, char* Name){
00161         char            LineBuff[10240];
00162         int                     ActionNum;
00163         ActionRec*      Action;
00164         int                     ActionItemID;
00165         char*           Args;
00166         char*           Args2;
00167         
00168 #ifdef DEBUGPATH
00169         printf("In ParseAction\n");
00170 #endif
00171 
00172 #ifdef DEBUG
00173         printf("Parsing Action\n");
00174 #endif  
00175 
00176         if (!Name) return FALSE;
00177         
00178         while(*Name==' ') Name++;
00179         
00180         ActionNum=Globals.NumActions;
00181         Action=&Globals.Actions[ActionNum];
00182         
00183         /*set the defaults*/
00184         bzero(Action, sizeof(ActionRec));
00185         snprintf(Action->Name, MAX_NAME_LEN, "%s",Name);
00186         Action->ID=ActionNum;
00187 
00188         while(GetLine(fp, LineBuff, 10240)){
00189                 if (strcasecmp(LineBuff, "</action>")==0){
00190 #ifdef DEBUG
00191                         printf("All done with action \"%s\"\n",Action->Name);
00192 #endif                  
00193                         Globals.NumActions++;
00194                         return TRUE;
00195                 }else if (strncasecmp(LineBuff, "response=",9)==0){
00196 #ifdef DEBUG
00197                         printf("Adding Response %s\n",LineBuff+9);
00198 #endif
00199                         Args=strchr(LineBuff+9,'(');
00200                         if (Args){
00201                                 Args2=strchr(Args, ')');
00202                                 if (!Args2){
00203                                         printf("Expected \"(\"\n");
00204                                         return FALSE;
00205                                 }
00206                                 *Args=0x00;
00207                                 Args++;
00208                                 *Args2=0x00;
00209                         }
00210                         
00211                         ActionItemID=GetActionByName(LineBuff+9);
00212                         if (ActionItemID==ACTION_NONE){
00213                                 printf("There is no response named \"%s\"\n",LineBuff+9);
00214                                 return FALSE;
00215                         }
00216                         
00217                         Action->ActionItems[Action->NumItems]=ActionItemID;
00218                         if (Globals.ActionItems[ActionItemID].ParseArgs)
00219                         if (Args) Action->ActionItemData[Action->NumItems]=Globals.ActionItems[ActionItemID].ParseArgs(Args);
00220                         Action->NumItems++;                     
00221                 }else{  
00222                         printf("I don't understand %s\n",LineBuff);
00223                 }
00224         }
00225         
00226         return FALSE;
00227 }
00228 
00229 
00230 /*******************************************
00231 * make sense of the system options
00232 *******************************************/
00233 int ParseSystem(FILE* fp){
00234         char            LineBuff[10240];
00235         char*           Current;
00236         
00237 #ifdef DEBUGPATH
00238         printf("In ParseSystem\n");
00239 #endif
00240 
00241         /*set the defaults*/
00242         if (Globals.SensorName) free(Globals.SensorName);
00243         Globals.SensorName=(char*)calloc(strlen(DEFAULT_SENSOR_NAME)+2, sizeof(char));
00244         snprintf(Globals.SensorName, strlen(DEFAULT_SENSOR_NAME)+1, DEFAULT_SENSOR_NAME);
00245         Globals.SensorID=0;
00246 
00247         /*loop through the lines*/
00248         while(GetLine(fp, LineBuff, 10240)){
00249                 if (strcasecmp(LineBuff, "</system>")==0){
00250 #ifdef DEBUG
00251                         printf("All Done with system options\n");
00252 #endif          
00253                         return TRUE;
00254                 }else if (strncasecmp(LineBuff, "name=",5)==0){
00255                         Current=LineBuff+strlen("name=");
00256                         if (Globals.SensorName) free(Globals.SensorName);
00257                         Globals.SensorName=(char*)calloc(strlen(Current)+2, sizeof(char));
00258                         snprintf(Globals.SensorName, strlen(Current)+1, Current);
00259 #ifdef DEBUG
00260                         printf("Sensor Name is %s\n",Globals.SensorName);
00261 #endif                  
00262                 }else if (strncasecmp(LineBuff, "ID=",3)==0){
00263                         Current=LineBuff+strlen("ID=");
00264                         Globals.SensorID=atoi(Current);
00265 #ifdef DEBUG
00266                         printf("Sensor ID is %i\n",Globals.SensorID);
00267 #endif                  
00268                 }else if (strncasecmp(LineBuff, "AlertHeader=",12)==0){
00269                         Current=LineBuff+strlen("AlertHeader=");
00270                         Globals.AlertHeader=ParseMessageString(Current);
00271 #ifdef DEBUG
00272                         printf("AlertHeader set\n");
00273 #endif                  
00274                 }else if (strncasecmp(LineBuff, "Threads=",8)==0){
00275                         Current=LineBuff+strlen("Threads=");
00276                         switch (*Current){
00277                         case 'Y':
00278                         case 'y':
00279                         case '1':
00280                         case 't':
00281                         case 'T':
00282                                 Globals.UseThreads=TRUE;
00283                                 break;
00284                         case 'n':
00285                         case 'N':
00286                         case '0':
00287                         case 'f':
00288                         case 'F':
00289                                 Globals.UseThreads=FALSE;
00290                                 break;
00291                         default:
00292                                 printf("I don't understand thread option %c\n",*Current);
00293                                 Globals.UseThreads=TRUE;
00294                         }
00295 #ifdef DEBUG
00296                         printf("UseThreads is %i\n",Globals.UseThreads);
00297 #endif                  
00298                 }else{
00299                         printf("Warning: Unknown System Option: %s\n",LineBuff);
00300                 }
00301         }
00302 
00303         return FALSE;
00304 }
00305 
00306 /*******************************************
00307 * make sense of the interface options
00308 *******************************************/
00309 int ParseInterface(FILE* fp, char* Name){
00310         char                    LineBuff[10240];
00311         InterfaceRec*   Interface;
00312         char*                   Current;
00313         
00314 #ifdef DEBUGPATH
00315         printf("In ParseInterface\n");
00316 #endif
00317 
00318         /*get the next free interface*/
00319         if (Globals.NumInterfaces==MAX_INTERFACES){
00320                 printf("You can only have a maximum of %i interfaces\n",MAX_INTERFACES);
00321                 return FALSE;
00322         }
00323         Interface=&Globals.Interfaces[Globals.NumInterfaces];
00324         Interface->ID=Globals.NumInterfaces;
00325         Globals.NumInterfaces++;
00326         
00327         /*set the defaults*/
00328         Interface->Type=PACKET_TYPE_NONE;
00329         Interface->MTU=1500;
00330         Interface->Proto=PACKET_PROTO_ETHERNET;
00331         Interface->FD=-1;
00332         snprintf(Interface->Name, MAX_INTERFACE_NAME_LEN, Name);
00333 #ifdef DEBUG
00334         printf("Interface Name is %s\n",Interface->Name);
00335 #endif                  
00336         
00337         /*loop through the lines*/
00338         while(GetLine(fp, LineBuff, 10240)){
00339                 if (strcasecmp(LineBuff, "</interface>")==0){
00340 #ifdef DEBUG
00341                         printf("All Done with this interface\n");
00342 #endif          
00343                         return TRUE;
00344                 }else if (strncasecmp(LineBuff, "type=",5)==0){
00345                         Current=LineBuff+strlen("type=");
00346                         Interface->Type=GetPacketTypeByName(Current);
00347 #ifdef DEBUG
00348                         printf("Interface Type is %i\n",Interface->Type);
00349 #endif                  
00350                 }else if (strncasecmp(LineBuff, "proto=",6)==0){
00351                         Current=LineBuff+strlen("proto=");
00352                         Interface->Proto=GetPacketProtoByName(Current);
00353 #ifdef DEBUG
00354                         printf("Interface Proto is %i\n",Interface->Proto);
00355 #endif                  
00356                 }else if (strncasecmp(LineBuff, "role=",5)==0){
00357                         Current=LineBuff+strlen("role=");
00358                         Interface->Role=GetPacketRoleByName(Current);
00359 #ifdef DEBUG
00360                         printf("Interface role is %i\n",Interface->Role);
00361 #endif                  
00362                 }else{
00363                         printf("Warning: Unknown Interface Option: %s\n",LineBuff);
00364                 }
00365         }
00366 
00367         return FALSE;
00368 }
00369 
00370 /*******************************************
00371 * make sense of the routing options
00372 *******************************************/
00373 int ParseRouting(FILE* fp){
00374         char                    LineBuff[10240];
00375         int                             RouteID;
00376         char*                   Pos;
00377         char*                   Pos2;
00378                 
00379 #ifdef DEBUGPATH
00380         printf("In ParseRouting\n");
00381 #endif
00382 
00383         /*set the defaults*/
00384 
00385         /*loop through the lines*/
00386         while(GetLine(fp, LineBuff, 10240)){
00387                 if (strcasecmp(LineBuff, "</routing>")==0){
00388 #ifdef DEBUG
00389                         printf("All Done with routing options\n");
00390 #endif          
00391                         return TRUE;
00392                 }else{
00393                         Pos=strchr(LineBuff, '(');
00394                         if (Pos){
00395                                 *Pos=0x00;
00396                                 Pos2=strchr(Pos+1, ')');
00397                                 if (!Pos2){
00398                                         printf("Error: Expected ) is %s\n",LineBuff);
00399                                         return FALSE;
00400                                 }
00401                                 *Pos2=0x00;
00402                         }
00403                         if ( (RouteID=GetRouteByName(LineBuff))==ROUTE_NONE){
00404                                 printf("ERROR: Unknown Routing Option: %s\n",LineBuff);
00405                                 return FALSE;
00406                         }
00407                         
00408                         if (Pos){
00409                                 if (!RouteAdd(RouteID, Pos+1)){
00410                                         printf("Routing option \"%s\" failed\n",LineBuff);
00411                                         return FALSE;
00412                                 }
00413                                 Globals.Routes[RouteID].Active=TRUE;
00414                         }else{
00415                                 if (!RouteAdd(RouteID, NULL)){
00416                                         printf("Routing option \"%s\" failed\n",LineBuff);
00417                                         return FALSE;
00418                                 }                               
00419                                 Globals.Routes[RouteID].Active=TRUE;
00420                         }
00421                 }
00422         }
00423 
00424         return FALSE;
00425 }
00426 
00427 
00428 /*******************************************
00429 * make sense of the config file
00430 *******************************************/
00431 int ParseConfig(){
00432         FILE*           fp;
00433         char            LineBuff[10240];
00434         char*           End;
00435         char*           Start;
00436         
00437 #ifdef DEBUGPATH
00438         printf("In ParseConfig\n");
00439 #endif  
00440 
00441         /*set some defaults*/
00442         Globals.UseThreads=TRUE;
00443         
00444         fp=fopen(Globals.ConfigFilename, "r");
00445         if (!fp){
00446                 printf("Couldn't open config file %s\n",Globals.ConfigFilename);
00447                 return FALSE;
00448         }
00449 
00450         while (GetLine(fp, LineBuff, 10240)){
00451 
00452                 if (strncasecmp(LineBuff, "<system>",8)==0){
00453                         /*Process the system options*/
00454                         if (!ParseSystem(fp)) return FALSE;
00455                 }else if(strncasecmp(LineBuff, "<interface",10)==0){
00456                         Start=LineBuff+10;
00457                         while (*Start==' ') Start++;
00458                         if (*Start=='>'){
00459                                 printf("Error parsing %s\nFormat <interface NAME>\n",LineBuff);
00460                                 return FALSE;
00461                         }
00462                         End=strchr(LineBuff+10,'>');
00463                         if (!End){
00464                                 printf("Expected \">\"\n");
00465                                 return FALSE;
00466                         }
00467                         *End=0x00;
00468                         if (!ParseInterface(fp, Start)) return FALSE;
00469                 }else if (strncasecmp(LineBuff, "<routing>",11)==0){
00470                         if (!ParseRouting(fp)) return FALSE;                    
00471                 }else if(strncasecmp(LineBuff, "<action",7)==0){
00472                         Start=LineBuff+7;
00473                         while (*Start==' ') Start++;
00474                         if (*Start=='>'){
00475                                 printf("Error parsing %s\nFormat <action NAME>\n",LineBuff);
00476                                 return FALSE;
00477                         }
00478                         End=strchr(LineBuff+7,'>');
00479                         if (!End){
00480                                 printf("Expected \">\"\n");
00481                                 return FALSE;
00482                         }
00483                         *End=0x00;
00484                         if (!ParseAction(fp, Start)) return FALSE;                                                                      
00485                 }else if(strncasecmp(LineBuff, "<module ",8)==0){
00486                         Start=LineBuff+7;
00487                         while (*Start==' ') Start++;
00488                         if (*Start=='>'){
00489                                 printf("Error parsing %s\nFormat <module NAME>\n",LineBuff);
00490                                 return FALSE;
00491                         }
00492                         End=strchr(LineBuff+7,'>');
00493                         if (!End){
00494                                 printf("Expected \">\"\n");
00495                                 return FALSE;
00496                         }
00497                         *End=0x00;              
00498                 }else if(strncasecmp(LineBuff, "<iplist ",8)==0){
00499                         Start=LineBuff+7;
00500                         while (*Start==' ') Start++;
00501                         if (*Start=='>'){
00502                                 printf("Error parsing %s\nFormat <iplist NAME>\n",LineBuff);
00503                                 return FALSE;
00504                         }
00505                         End=strchr(LineBuff+7,'>');
00506                         if (!End){
00507                                 printf("Expected \">\"\n");
00508                                 return FALSE;
00509                         }
00510                         *End=0x00;              
00511                         if (!ParseList(fp, Start, LIST_TYPE_IP)) return FALSE;                  
00512                 }else{
00513                         printf("Unexpected section %s\n",LineBuff);
00514                         return FALSE;
00515                 }
00516         }
00517 
00518 
00519         return TRUE;
00520 }

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2