run nsupdate as child and send it the update commands
[dyn-nsupdate.git] / dyn-nsupdate.cpp
index 745595df3d1abf6e4b02872a6c4c1b2e9f358320..4cb7ead86ec2ff1c178cffedee3cbb7037b66597 100644 (file)
 #include <iostream>
 #include <fstream>
+#include <sys/wait.h>
 
+#include <boost/regex.hpp>
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/ini_parser.hpp>
+#include <boost/iostreams/device/file_descriptor.hpp>
+#include <boost/iostreams/stream.hpp>
 
-using namespace boost::property_tree;
+using namespace boost;
 
-int main(int, const char **)
+static void write(int fd, const char *str)
 {
-       ptree config;
-       ini_parser::read_ini(CONFIG_FILE, config);
+       size_t len = strlen(str);
+       ssize_t written = write(fd, str, len);
+       if (written < 0 || (size_t)written != len) {
+               std::cerr << "Error writing pipe." << std::endl;
+               exit(1);
+       }
+}
+
+int main(int argc, const char ** argv)
+{
+       static const regex regex_ip("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
+       static const regex regex_user("[a-zA-Z]+");
+       
+       
+       if (argc != 5) {
+               std::cerr << "Usage: " << argv[0] << " <username> <password> <domain> <IP address>" << std::endl;
+               return 1;
+       }
+       
+       /* Obtain and validate inpit */
+       std::string user = argv[1];
+       std::string password = argv[2];
+       std::string domain = argv[3];
+       std::string ip = argv[4];
+       
+       if (!regex_match(ip, regex_ip)) {
+               std::cerr << "Invalid IP address " << ip << "." << std::endl;
+               exit(1);
+       }
+       if (!regex_match(user, regex_user)) {
+               std::cerr << "Invalid username " << user << "." << std::endl;
+               exit(1);
+       }
+       
+       /* read configuration */
+       property_tree::ptree config;
+       property_tree::ini_parser::read_ini(CONFIG_FILE, config);
+       std::string nsupdate = config.get<std::string>("nsupdate");
+       std::string keyfile = config.get<std::string>("key");
+       
+       /* Check username, password, domain */
+       optional<std::string> correct_password = config.get_optional<std::string>(user+".password");
+       if (!correct_password || *correct_password != password) {
+               std::cerr << "Username or password incorrect." << std::endl;
+               exit(1);
+       }
+       if (config.get<std::string>(user+".domain") != domain) {
+               std::cerr << "Domain incorrect." << std::endl;
+               exit(1);
+       }
+       
+       /* preapre the pipe */
+       int pipe_ends[] = {0,0};
+       pipe(pipe_ends);
+
+       /* Launch nsupdate */
+       pid_t child_pid = fork();
+       if (child_pid < 0) {
+               std::cerr << "Error while forking." << std::endl;
+               exit(1);
+       }
+       if (child_pid == 0) {
+               /* We're in the child */
+               /* Close write end, use read and as stdin */
+               close(pipe_ends[1]);
+               dup2(pipe_ends[0], fileno(stdin));
+               /* exec nsupdate */
+               execl(nsupdate.c_str(), nsupdate.c_str(), "-k", keyfile.c_str(), NULL);
+               /* There was an error */
+               std::cerr << "There was an error executing nsupdate." << std::endl;
+               exit(1);
+       }
+       
+       /* Send it the command */
+       write(pipe_ends[1], "server localhost\n");
+       
+       write(pipe_ends[1], "update delete ");
+       write(pipe_ends[1], domain.c_str());
+       write(pipe_ends[1], ".\n");
+       
+       write(pipe_ends[1], "update add ");
+       write(pipe_ends[1], domain.c_str());
+       write(pipe_ends[1], ". 60 A ");
+       write(pipe_ends[1], ip.c_str());
+       write(pipe_ends[1], "\n");
+       
+       write(pipe_ends[1], "send\n");
+       
+       /* Close both ends */
+       close(pipe_ends[0]);
+       close(pipe_ends[1]);
+       
+       /* Wait for child to be gone */
+       int child_status;
+       waitpid(child_pid, &child_status, 0);
+       if (child_status != 0) {
+               std::cerr << "There was an error in the child." << std::endl;
+               exit(1);
+       }
        
-       std::cout << "Hi world! " << CONFIG_FILE << std::endl;
        return 0;
 }