Parcourir la source

Allow to configure the script inside the Kea configuration file

Baptiste Jonglez il y a 7 ans
Parent
commit
daf87a3e35
6 fichiers modifiés avec 50 ajouts et 8 suppressions
  1. 4 1
      Makefile
  2. 5 2
      src/lease_select.cc
  3. 25 0
      src/load.cc
  4. 5 2
      src/pkt_receive.cc
  5. 6 2
      src/runscript.cc
  6. 5 1
      src/runscript.h

+ 4 - 1
Makefile

@@ -1,5 +1,5 @@
 
-OBJECTS = src/runscript.cc src/pkt_receive.o src/lease_select.o src/version.o
+OBJECTS = src/load.o src/runscript.o src/pkt_receive.o src/lease_select.o src/version.o
 CXXFLAGS = -I /usr/include/kea -fPIC -Wno-deprecated
 LDFLAGS = -L /usr/lib/kea/lib -shared -lkea-dhcpsrv -lkea-dhcp++ -lkea-hooks -lkea-log -lkea-util -lkea-exceptions
 
@@ -9,6 +9,9 @@ kea-hook-runscript.so: $(OBJECTS)
 %.o: %.cc
 	g++ -c $(CXXFLAGS) -o $@ $<
 
+src/pkt_receive.o: src/runscript.h
+src/lease_select.o: src/runscript.h
+
 clean:
 	rm -f src/*.o
 	rm -f kea-auth-radius.so

+ 5 - 2
src/lease_select.cc

@@ -9,12 +9,15 @@
 using namespace isc::dhcp;
 using namespace isc::hooks;
 
+/* From load.cc */
+extern std::string script_path;
+
 extern "C" {
 
 int lease4_select(CalloutHandle& handle) {
     int ret;
-    char *env[] = { "HOME=/usr/home", "FOO=bar", (char *)NULL };
-    ret = run_script("/tmp/test.sh", "lease4_select", env);
+    char *env[] = { "FOO=bar", (char *)NULL };
+    ret = run_script(script_path.data(), "lease4_select", env);
     fprintf(stderr, "ret = %d\n", ret);
     return 0;
 }

+ 25 - 0
src/load.cc

@@ -0,0 +1,25 @@
+#include <hooks/hooks.h>
+
+using namespace isc::hooks;
+using namespace isc::data;
+
+/* Path of the script to run in hooks, accessed by the other files of this
+ * library. */
+std::string script_path;
+
+extern "C" {
+
+int load(LibraryHandle& handle) {
+    ConstElementPtr script = handle.getParameter("script");
+    if (!script) {
+        return 1;
+    }
+    if (script->getType() != Element::string) {
+        return 1;
+    }
+    script_path = script->stringValue();
+    
+    return 0;
+}
+
+} // end extern "C"

+ 5 - 2
src/pkt_receive.cc

@@ -8,12 +8,15 @@
 using namespace isc::dhcp;
 using namespace isc::hooks;
 
+/* From load.cc */
+extern std::string script_path;
+
 extern "C" {
 
 int pkt4_receive(CalloutHandle& handle) {
     int ret;
-    char *env[] = { "HOME=/usr/home", "FOO=bar", (char *)NULL };
-    ret = run_script("/tmp/test.sh", "pkt4_receive", env);
+    char *env[] = { "FOO=bar", "BAZ=bang", (char *)NULL };
+    ret = run_script(script_path.data(), "pkt4_receive", env);
     fprintf(stderr, "ret = %d\n", ret);
     return 0;
 }

+ 6 - 2
src/runscript.cc

@@ -6,10 +6,12 @@
 // TODO: Not nescessary
 #include <stdio.h>
 
+extern "C" {
+
 /* Runs the given script with the given argument and environment
  * variables.  Returns -1 upon failure, or the exit code of the script
  * upon success. */
-int run_script(char *scriptpath, const char *arg0, char *const *envp)
+int run_script(const char *scriptpath, const char *arg0, char *const *envp)
 {
     int ret, wstatus, exitcode;
     pid_t pid;
@@ -21,7 +23,7 @@ int run_script(char *scriptpath, const char *arg0, char *const *envp)
     }
     if (pid == 0) {
         /* Child process */
-        char *scriptname = strrchr(scriptpath, '/');
+        const char *scriptname = strrchr(scriptpath, '/');
         if (scriptname == NULL) {
             scriptname = scriptpath + strlen(scriptpath);
         }
@@ -47,3 +49,5 @@ int run_script(char *scriptpath, const char *arg0, char *const *envp)
         return exitcode;
     }
 }
+
+} // end extern "C"

+ 5 - 1
src/runscript.h

@@ -1,2 +1,6 @@
 
-int run_script(char *scriptpath, const char *arg0, char *const *envp);
+extern "C" {
+
+int run_script(const char *scriptpath, const char *arg0, char *const *envp);
+
+}