📄 loader.c
字号:
/** This file is part of Firestorm NIDS* Copyright (c) 2002 Gianni Tedesco* This program is released under the terms of the GNU GPL version 2** This is the firestorm plugin loader. We can load individually* named plugins or scan directories for them. We don't recurse* directories. We also perform all the plugin initialisation* and unloading from here. Plugin unloading is the last thing* to happen before exiting*/#include <sys/types.h>#include <stdlib.h>#include <string.h>#include <firestorm.h>#include <config.h>#include <cleanup.h>#include <loader.h>#ifdef HAVE_DIRENT_H#include <dirent.h>#endif#ifdef HAVE_DLFCN_H#include <dlfcn.h>#else#warning "Can't find dlfcn.h, this should be part of your libc"#endif#define MAX_PLUGINS 64proc_plugin_import callback=NULL;struct plugin *ld_list=NULL;unsigned int num_plugins=0;unsigned int plugin_warn=0;static inline char *loader_strerr(int code){ static char *estr[]={ "Success", "Failure", "Bad parameters", "Out of memory", "Bad object size", "API not supported", }; if ( code>=0 && code <= PLUGIN_ERR_OBJECT ) return estr[code]; return "Unknown";}struct plugin *loader_find(char *name){ struct plugin *l; for(l=ld_list; l; l=l->next) { if ( !strcmp(name, l->p.name) ) { return l; } } return NULL;}void *loader_symbol(struct plugin *p, char *name){ return dlsym(p->lib, name);}void loader_cleanup(int code, void *priv){ struct plugin *foo, *bar; mesg(M_INFO,"loader: unloading all plugins"); for(foo=ld_list; foo;) { bar=foo; foo=foo->next; bar->unload(code); free(bar); }}void loader_init(proc_plugin_import cb){ if ( !cb ) cleanup(EXIT_ERR, "loader_init() called with NULL cb"); if ( callback ) cleanup(EXIT_ERR, "loader_init() called twice"); cleanup_final(loader_cleanup, NULL); callback=cb;}/* Load a plugin given a filename */int loader_load(const char *path){ struct plugin *p; struct plugin_in pin; int r; if ( num_plugins >= MAX_PLUGINS ) { if ( !plugin_warn ) { plugin_warn++; mesg(M_WARN,"loader: MAX_PLUGINS exceeded."); } return 0; } /* Allocate private structure */ if ( !(p=calloc(1, sizeof(struct plugin))) ) { cperror("calloc"); } /* dlopen() the library */ if ( !(p->lib=dlopen(path, RTLD_NOW)) ) { mesg(M_ERR,"loader: %s", dlerror()); goto llerr; } /* Resolve the entry point symbol */ if ( !(p->init=dlsym(p->lib, plugin_init)) ) { mesg(M_ERR,"loader: %s: Not a valid firestorm plugin", path); goto llerr; } /* Resolve the exit point symbol */ if ( !(p->unload=dlsym(p->lib, plugin_unload)) ) { mesg(M_ERR,"loader: %s: Not a valid firestorm plugin", path); goto llerr; } /* prepare to call the entry point */ p->p.size=sizeof(struct plugin_out); pin.size=sizeof(struct plugin_in); pin.import=callback; /* Call the entry point function */ switch ( (r=p->init(&pin, &p->p)) ) { case PLUGIN_ERR_OK: /* TODO: Check for older versions * of the plugin, and replace */ p->next=ld_list; ld_list=p; num_plugins++; mesg(M_INFO,"plugin: %s[%i.%i]: %s", p->p.name, p->p.ver_major, p->p.ver_minor, p->p.desc); break; default: mesg(M_ERR,"loader: %s: %s: report bugs to %s <%s>", path, loader_strerr(r), p->p.author_name, p->p.author_email); goto llerr; } return 1;llerr: if ( p->lib ) dlclose(p->lib); free(p); return 0;}void loader_perror(struct plugin *p, char *sys, int code){ char *estr; if ( code==PLUGIN_ERR_OK ) return; estr=loader_strerr(code); mesg(M_ERR, "%s: %s: %s: report bugs to %s <%s>", sys, p->p.name, estr, p->p.author_name, p->p.author_email);}/* Iterate all files in a directory and try * to load them as plugins, oh what folly... */void loader_load_dir(const char *dir){ DIR *d; struct dirent *f; char fn[1024]; /* XXX: Should be >MAX_PATH on all platforms */ if ( !(d=opendir(dir)) ) { mesg(M_ERR,"loader: %s: opendir(): %s", dir, get_err()); return; } while( (f=readdir(d)) ) { int len; /* Don't load hidden files or . or .. */ if ( f->d_name[0] == '.' ) continue; /* only load files which end with .so */ len=strlen(f->d_name); if ( len<3 || strcmp(f->d_name+len-3, ".so") ) continue; if ( snprintf(fn, sizeof(fn), "%s/%s", dir, f->d_name)>0 ) { loader_load(fn); }else{ mesg(M_ERR,"loader: %s: path overflow.", dir); } } closedir(d);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -