diff -uNr wvstreams.orig/uniconf/daemon/uniconfd.cc wvstreams/uniconf/daemon/uniconfd.cc --- wvstreams.orig/uniconf/daemon/uniconfd.cc 2010-05-11 12:01:02.740457000 +0200 +++ wvstreams/uniconf/daemon/uniconfd.cc 2010-05-12 09:27:50.204499401 +0200 @@ -25,12 +25,20 @@ #include "wvstrutils.h" #include "wvfileutils.h" #include "wvstreamsdaemon.h" +#include "uniwatch.h" #ifdef WITH_SLP #include "slp.h" #endif #include +#include +#include +#include +#include + +using namespace std; + using std::map; using wv::shared_ptr; @@ -68,6 +76,7 @@ class UniConfd : public WvStreamsDaemon { bool needauth; + WvString events; WvString permmon; WvStringList lmonikers; time_t commit_interval; @@ -78,6 +87,7 @@ bool namedgen_cb(WvStringParm option, void *) { + WvString name(option); WvString moniker; char* ptr; @@ -96,15 +106,64 @@ return true; } + + + void findandreplace( string& source, const string& find, const string& replace ) + { + size_t j; + while ((j = source.find( find )) != string::npos) + { + source.replace( j, find.length(), replace ); + } + } + + void execute(char *key, char *exec, bool is_a_directory) + { + + + + UniConf uni(cfg[""]); + + char val[200]; + + strcpy(val,uni[key].getme()); + + string test(exec); + string key_(key); + string val_(val) ; + + findandreplace(test,"$KEY",key_); + if (!is_a_directory) + findandreplace(test,"$VALUE",val_); + + if(!is_a_directory) + cout << "\n[!] Edition detected [!] [" + key_ + "] = " + val + "\n[!] Exec [!] " + test +"\n" << endl; + else + cout << "\n[!] Edition detected at Path "+ key_ + " [!]\n[!] Exec [!] " + test +"\n" << endl; + + + + int i=system(test.c_str()); + + + + + } + + + void commit_stream_cb(WvStream *s) { + cfg.commit(); + cfg.refresh(); if (permgen) permgen->refresh(); s->alarm(commit_interval * 1000); + } void startup() @@ -136,8 +195,122 @@ } cfg.refresh(); - } + + + if (events!=NULL) + { + + char dir_srt[300]; + + strcpy(dir_srt,events); + + log("Opening directory '%s':\n",dir_srt); + + DIR *d; + struct dirent *dir; + UniWatchList *uw=new UniWatchList(); + d=opendir(dir_srt); + int count=0; + if (d) + { + while((dir=readdir(d))!=NULL) + { + + bool is_a_directory=false; + + if(strcmp(dir->d_name,"..") ) + { + if(strcmp(dir->d_name,".")) + { + // log("%s\n",dir->d_name); + + char moniker[200]; + + strcpy(moniker,"ini:"); + strcat(moniker,dir_srt); + strcat(moniker,"/"); + strcat(moniker,dir->d_name); + + + + //log("%s\n",moniker); + log("\n\t\t\t> Reading file '%s'...\n",dir->d_name); + UniConfRoot tmp_root(moniker); + + if (tmp_root["key"].exists() && tmp_root["exec"].exists()) + { + log("\t\t\t\t* key = %s\n",tmp_root["key"].getme()); + log("\t\t\t\t* exec = %s\n\n",tmp_root["exec"].getme()); + } + else + log("\t\t\t[!] %s file contains no relevant info to build a trigger\n",dir->d_name); + + if (tmp_root["dir"].exists()) + { + + log("\t\t\t\t* Applying callback to every key in %s dir\n",tmp_root["key"].getme()); + is_a_directory=true; + } + + + if (tmp_root["key"].exists() && tmp_root["exec"].exists()) + { + char *key=new char[strlen(tmp_root["key"].getme())]; + char *exec=new char[strlen(tmp_root["exec"].getme())]; + strcpy(key,tmp_root["key"].getme()); + strcpy(exec,tmp_root["exec"].getme()); + + + + if (!is_a_directory) + { + UniConf uni(cfg[""]); + uw->add(uni[key],wv::bind(&UniConfd::execute,this,key,exec,is_a_directory),true); + } + else + { + char tmp[100]; + strcpy(tmp,"/"); + strcat(tmp,key); + + // cout << "uni(cfg[" + (string)tmp +"])" << endl; + + + UniConf uni(cfg[tmp]); + uw->add(uni,wv::bind(&UniConfd::execute,this,key,exec,is_a_directory),true); + } + + } + + } + + + } + count++; + } + + closedir(d); + + } + } + // log("%s",count); + + + + + + /* + UniConf uni(cfg[""]); + UniWatchList *uw=new UniWatchList(); + char *key=(char*)"VALUE"; + char *exec=(char*)"script1"; + uw->add(uni[key],wv::bind(&UniConfd::execute,this,key,exec),true); + */ + + } + + permgen = !!permmon ? wvcreate(permmon) : NULL; UniConfDaemon *daemon = new UniConfDaemon(cfg, needauth, permgen); @@ -154,6 +327,7 @@ for (i.rewind(); i.next(); ) daemon->listen(*i); + WvStream *commit_stream = new WvStream; commit_stream->setcallback(wv::bind(&UniConfd::commit_stream_cb, this, commit_stream)); @@ -170,7 +344,7 @@ WvStreamsDaemon("uniconfd", VERBOSE_WVPACKAGE_VERSION, wv::bind(&UniConfd::startup, this)), needauth(false), - commit_interval(5*60), + commit_interval(5*60), first_time(true), permgen(NULL) { @@ -179,6 +353,11 @@ pid_file); args.add_set_bool_option('a', "need-auth", "Require authentication on incoming connections", needauth); + + args.add_option('e', "events-path", + "Checks given directory to define triggers for events", "directory", + events); + args.add_option('A', "check-access", "Check all accesses against perms moniker", "moniker", permmon); @@ -199,6 +378,5 @@ int main(int argc, char **argv) { UniConfd uniconfd; - return uniconfd.run(argc, argv); }