00001
00002
00010
00011
00012 #include "packet.h"
00013 #include "plugin.h"
00014 #include "policy.h"
00015 #include <string.h>
00016 #include <errno.h>
00017 #include <sys/types.h>
00018 #include <dirent.h>
00019 #include <unistd.h>
00020 #include <dlfcn.h>
00021 #include <stdio.h>
00022 #include "memory.h"
00023 #include "naming.h"
00024
00025 #ifndef DEBUG_PLUGIN_INT
00026 #ifdef DEBUG
00027 #define DEBUG_PLUGIN_INT DEBUG
00028 #else
00029 #define DEBUG_PLUGIN_INT 0
00030 #endif
00031 #endif
00032
00033 extern PLUGIN *inp_list;
00034 extern PLUGIN *anp_list;
00035 extern PLUGIN *protop_list;
00036 extern PLUGIN *outp_list;
00037 extern PLUGIN *all_plugins;
00038
00039 void appendProtocol(PACKET *p, PROTO_LL *proto){
00040 PROTO_LL *temp;
00041 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00042 printf("Enter appendProtocol(PACKET*p(%p), PROTO_LL*proto(%p))\n", p, proto);
00043 }
00044
00045 if(!p) return;
00046 if(!p->proto){
00047 p->proto = proto;
00048 }
00049 else {
00050 temp = p->proto;
00051 while(temp->next){
00052 temp = temp->next;
00053 }
00054 temp->next = proto;
00055 }
00056 return;
00057 }
00058
00059
00060
00061
00062
00063
00064
00068
00069 int loadPlugins(char *dir){
00070 DIR *d;
00071 struct dirent *dirent;
00072 char *plugname;
00073 char *sTemp;
00074 unsigned int buf_sz=512;
00075 unsigned int i;
00076 void *dlopened;
00077 PLUGIN *plug;
00078 int err;
00079 unsigned short s;
00080
00081 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00082 printf("Enter loadPlugins(char*dir(%s))\n", dir);
00083 }
00084
00085 d = opendir(dir);
00086 if(!d){
00087 printf("ERR: opendir failed with %s\n", strerror(errno));
00088 return 0;
00089 }
00090
00091 plugname = (char*)malloc_safe(buf_sz+NAME_MAX+1);
00092 if(!plugname){
00093 printf("ERR: malloc failed!\n");
00094 closedir(d);
00095 return 0;
00096 }
00097 dirent = readdir(d);
00098 while(dirent){
00099 if(DEBUG_PLUGIN_INT>=DEBUG_DO_LOOP){
00100 printf("In while(dirent) loop. d_name=%s\n", dirent->d_name);
00101 }
00102 i = strlen(dirent->d_name);
00103 if(i>4){
00104 sTemp = dirent->d_name + i-4;
00105 if(!strcasecmp(sTemp, ".plg")){
00106 if(dirent->d_name[0]!='/'){
00107 sTemp = getcwd(plugname, buf_sz);
00108 while(!sTemp && errno==ERANGE){
00109 buf_sz+=256;
00110 plugname = (char*)realloc_safe(plugname, buf_sz+NAME_MAX+1);
00111 if(!plugname){
00112 printf("ERR: realloc failed\n");
00113 closedir(d);
00114 return 0;
00115 }
00116 sTemp = getcwd(plugname, buf_sz);
00117 }
00118 plugname=sTemp;
00119 strcat(plugname, "/");
00120 strcat(plugname, dirent->d_name);
00121 }
00122 else strcpy(plugname, dirent->d_name);
00123 if(DEBUG_PLUGIN_INT>=DEBUG_DO_LOOP){
00124 printf("......... Got plugin %s\n", plugname);
00125 }
00126
00127 dlopened = dlopen(plugname, RTLD_NOW | RTLD_GLOBAL);
00128 if(!dlopened){
00129 printf("ERR: %s couldn't be dlopened\n", plugname);
00130 dirent = readdir(d);
00131 continue;
00132 }
00133
00134 plug = (PLUGIN*)malloc(sizeof(PLUGIN));
00135 if(!plug){
00136 printf("ERR: Malloc failed\n");
00137 return 0;
00138 }
00139 memset(plug, 0, sizeof(PLUGIN));
00140
00141 plug->pluginRegister = dlsym(dlopened, "pluginRegister");
00142 if(!plug->pluginRegister){
00143 printf("ERR: Couldn't find register fn for %s\n", plugname);
00144 dirent=readdir(d);
00145 continue;
00146 }
00147
00148 (*(plug->pluginRegister))(&(plug->inPlug), &(plug->anPlug),
00149 &(plug->protoPlug), &(plug->outPlug),
00150 &decode);
00151
00152 plug->pluginInit = dlsym(dlopened, "pluginInit");
00153 if(!plug->pluginRegister){
00154 printf("ERR: Couldn't find init fn for %s\n", plugname);
00155 dirent=readdir(d);
00156 free(plug);
00157 continue;
00158 }
00159
00160 plug->pluginCleanup = dlsym(dlopened, "pluginCleanup");
00161 if(!plug->pluginRegister){
00162 printf("ERR: Couldn't find cleanup fn for %s\n", plugname);
00163 dirent=readdir(d);
00164 free(plug);
00165 continue;
00166 }
00167
00168 if(plug->inPlug){
00169 plug->pluginGetPacket = dlsym(dlopened, "pluginGetPacket");
00170 if(!plug->pluginGetPacket){
00171 printf("ERR: Couldn't find getpacket fn for %s\n", plugname);
00172 plug->inPlug = (INPLUG*)0;
00173 }
00174 else {
00175 plug->iNext = inp_list;
00176 inp_list = plug;
00177 }
00178 }
00179
00180 if(plug->outPlug){
00181 plug->pluginOutput = dlsym(dlopened, "pluginOutput");
00182 if(!plug->pluginOutput){
00183 printf("ERR: Couldn't find output fn for %s\n", plugname);
00184 plug->outPlug = (OUTPLUG*)0;
00185 }
00186 else {
00187 plug->oNext = outp_list;
00188 outp_list = plug;
00189 }
00190 }
00191
00192 if(plug->anPlug){
00193 plug->pluginAnalyse = dlsym(dlopened, "pluginAnalyse");
00194 if(!plug->pluginAnalyse){
00195 printf("ERR: Couldn't find analyse fn for %s\n", plugname);
00196 plug->anPlug = (ANPLUG*)0;
00197 }
00198 else {
00199 plug->aNext = anp_list;
00200 anp_list = plug;
00201 }
00202 }
00203
00204 if(plug->protoPlug){
00205 err = 0;
00206 for(s=0;s<plug->protoPlug->nProto;s++){
00207 storeProtoMapping((plug->protoPlug->sProto)[s],
00208 (plug->protoPlug->pProto)[s]);
00209 }
00210 plug->pluginDecode = dlsym(dlopened, "pluginDecode");
00211 if(!plug->pluginDecode){
00212 err = 1;
00213 printf("ERR: Couldn't find decode fn for %s\n", plugname);
00214 }
00215 plug->pluginTest = dlsym(dlopened, "pluginTest");
00216 if(!plug->pluginTest){
00217 err = 1;
00218 printf("ERR: Couldn't find test fn for %s\n", plugname);
00219 }
00220 plug->pluginPrint = dlsym(dlopened, "pluginPrint");
00221 if(!plug->pluginPrint){
00222 err = 1;
00223 printf("ERR: Couldn't find print fn for %s\n", plugname);
00224 }
00225 plug->pluginMkTest = dlsym(dlopened, "pluginMkTest");
00226 if(!plug->pluginMkTest){
00227 err = 1;
00228 printf("ERR: Couldn't find MkTest fn for %s\n", plugname);
00229 }
00230
00231 if(err){
00232 plug->protoPlug = (PROTOPLUG*)0;
00233 }
00234 else {
00235 plug->pNext = anp_list;
00236 anp_list = plug;
00237 }
00238 }
00239
00240 if(!plug->inPlug && !plug->anPlug && !plug->outPlug
00241 && !plug->protoPlug){
00242 free(plug);
00243 }
00244 else {
00245 plug->next = all_plugins;
00246 all_plugins = plug;
00247 }
00248
00249 dlclose(dlopened);
00250 }
00251 }
00252 dirent = readdir(d);
00253 }
00254
00255 closedir(d);
00256
00257 return 1;
00258 }
00259
00260
00264
00265 int initPlugin(PLUGIN *plug, int ct, char **inits){
00266 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00267 printf("Enter initPlugin(PLUGIN*plug(%p), int ct(%d), char** inits(%p))\n", plug, ct, inits);
00268 }
00269 return 0;
00270 }
00271
00272
00276
00277 int decode(PACKET *p, PROTO protocol){
00278 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00279 printf("Enter decode(PACKET*p(%p), PROTO protocol(%lx))\n", p, protocol);
00280 }
00281 return 0;
00282 }
00283
00284
00288
00289 int testVar(PROTO proto, PACKET *p,
00290 long offset, char varType, char testType, unsigned long val){
00291 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00292 printf("Enter appendProtocol(PROTO proto(%lx),PACKET*p(%p),long offset(%ld),char varType(%c),char testType(%c),ulong val(%lx))\n", proto, p, offset, varType, testType, val);
00293 }
00294 return 0;
00295 }
00296
00297
00301
00302 int testMask(PROTO proto, PACKET *p,
00303 long offset, long len, BYTE *val, BYTE *mask, BOOL type){
00304 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00305 printf("Enter testMask(PROTO proto(%lx),PACKET*p(%p),long offset(%ld),long len(%ld),BYTE*val(%p),BYTE*mask(%p),BOOL type(%c))\n", proto, p, offset, len, val, mask, type);
00306 }
00307 return 0;
00308 }
00309
00310
00314
00315 int testSpecial(PROTO proto, PACKET *p,
00316 char *test, char *val){
00317 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00318 printf("Enter testSpecial(PROTO proto(%lx),PACKET*p(%p),char*test(%s),char*val(%s))\n", proto, p, test, val);
00319 }
00320 return 0;
00321 }
00322
00323
00327
00328 char *analyse(PROTO proto, PACKET *p, char *type, char *args){
00329 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00330 printf("Enter analyse(PROTO proto(%lx),PACKET*p(%p),char*type(%s),char*args(%s))\n", proto, p, type, args);
00331 }
00332 return (char*)0;
00333 }
00334
00335
00339
00340 int printVar(PROTO proto, PACKET *p,
00341 char *args, int(*printFn)(char *fmt, ...)){
00342 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00343 printf("Enter printVar(PROTO proto(%lx),PACKET*p(%p),char*args(%s),printFn(%p))\n", proto, p, args, printFn);
00344 }
00345 return 0;
00346 }
00347
00348
00352
00353 int mkTest(PROTO proto, char *val, char *test, char *field, POLICY_TEST *pTest){
00354 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00355 printf("Enter mkTest(PROTO proto(%lx),char*val(%s),char*test(%s),char*field(%s),POLICY_TEST*pTest(%p))\n", proto, val, test, field, pTest);
00356 }
00357 return 0;
00358 }
00359
00360
00364
00365 int cleanupPlugins(void){
00366 PLUGIN *p;
00367
00368 if(DEBUG_PLUGIN_INT>=DEBUG_DO_ENTRY_SOME){
00369 printf("Enter cleanupPlugins(void)\n");
00370 }
00371
00372 p = all_plugins;
00373 while(p){
00374 (*(p->pluginCleanup))(0);
00375 free_safe(p);
00376 }
00377 all_plugins = (PLUGIN*)0;
00378 inp_list = (PLUGIN*)0;
00379 anp_list = (PLUGIN*)0;
00380 protop_list = (PLUGIN*)0;
00381 outp_list = (PLUGIN*)0;
00382
00383 return 1;
00384 }