5 #include <boost/regex.hpp>
6 #include <boost/property_tree/ptree.hpp>
7 #include <boost/property_tree/ini_parser.hpp>
8 #include <boost/iostreams/device/file_descriptor.hpp>
9 #include <boost/iostreams/stream.hpp>
11 using namespace boost;
12 typedef property_tree::ptree::path_type path;
14 static void write(int fd, const char *str)
16 size_t len = strlen(str);
17 ssize_t written = write(fd, str, len);
18 if (written < 0 || (size_t)written != len) {
19 std::cerr << "Error writing pipe." << std::endl;
24 int main(int argc, const char ** argv)
26 static const regex regex_ip("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
27 static const regex regex_password("[a-zA-Z0-9.:;,_-]+");
28 static const regex regex_domain("[a-zA-Z0-9.]+");
32 std::cerr << "Usage: " << argv[0] << " <domain> <password> <IP address>" << std::endl;
37 std::string domain = argv[1];
38 std::string password = argv[2];
39 std::string ip = argv[3];
42 if (!regex_match(ip, regex_ip)) {
43 std::cerr << "Invalid IP address " << ip << "." << std::endl;
46 if (!regex_match(domain, regex_domain)) {
47 std::cerr << "Invalid domain " << domain << "." << std::endl;
50 if (!regex_match(password, regex_password)) {
51 std::cerr << "Invalid password " << password << "." << std::endl;
55 /* read configuration */
56 property_tree::ptree config;
57 property_tree::ini_parser::read_ini(CONFIG_FILE, config);
58 std::string nsupdate = config.get<std::string>("nsupdate");
60 /* Given the domain, check whether the password matches */
61 optional<std::string> correct_password = config.get_optional<std::string>(path(domain+"/password", '/'));
62 if (!correct_password || *correct_password != password) {
63 std::cerr << "Password incorrect." << std::endl;
67 /* preapre the pipe */
69 if (pipe(pipe_ends) < 0) {
70 std::cerr << "Error opening pipe." << std::endl;
75 pid_t child_pid = fork();
77 std::cerr << "Error while forking." << std::endl;
81 /* We're in the child */
82 /* Close write end, use read end as stdin */
84 if (dup2(pipe_ends[0], fileno(stdin)) < 0) {
85 std::cerr << "There was an error redirecting stdin." << std::endl;
89 execl(nsupdate.c_str(), nsupdate.c_str(), "-l", (char *)NULL);
90 /* There was an error */
91 std::cerr << "There was an error executing nsupdate." << std::endl;
95 /* Send it the command */
96 write(pipe_ends[1], "update delete ");
97 write(pipe_ends[1], domain.c_str());
98 write(pipe_ends[1], ". A\n");
100 write(pipe_ends[1], "update add ");
101 write(pipe_ends[1], domain.c_str());
102 write(pipe_ends[1], ". 60 A ");
103 write(pipe_ends[1], ip.c_str());
104 write(pipe_ends[1], "\n");
106 write(pipe_ends[1], "send\n");
108 /* Close both ends */
112 /* Wait for child to be gone */
114 waitpid(child_pid, &child_status, 0);
115 if (child_status != 0) {
116 std::cerr << "There was an error in the child." << std::endl;