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");
59 unsigned server_port = config.get<unsigned>("port", 53);
61 /* Given the domain, check whether the password matches */
62 optional<std::string> correct_password = config.get_optional<std::string>(path(domain+"/password", '/'));
63 if (!correct_password || *correct_password != password) {
64 std::cerr << "Password incorrect." << std::endl;
68 /* preapre the pipe */
70 if (pipe(pipe_ends) < 0) {
71 std::cerr << "Error opening pipe." << std::endl;
76 pid_t child_pid = fork();
78 std::cerr << "Error while forking." << std::endl;
82 /* We're in the child */
83 /* Close write end, use read end as stdin */
85 if (dup2(pipe_ends[0], fileno(stdin)) < 0) {
86 std::cerr << "There was an error redirecting stdin." << std::endl;
90 execl(nsupdate.c_str(), nsupdate.c_str(), "-p", std::to_string(server_port).c_str(), "-l", (char *)NULL);
91 /* There was an error */
92 std::cerr << "There was an error executing nsupdate." << std::endl;
96 /* Send it the command */
97 write(pipe_ends[1], "update delete ");
98 write(pipe_ends[1], domain.c_str());
99 write(pipe_ends[1], ". A\n");
101 write(pipe_ends[1], "update add ");
102 write(pipe_ends[1], domain.c_str());
103 write(pipe_ends[1], ". 60 A ");
104 write(pipe_ends[1], ip.c_str());
105 write(pipe_ends[1], "\n");
107 write(pipe_ends[1], "send\n");
109 /* Close both ends */
113 /* Wait for child to be gone */
115 waitpid(child_pid, &child_status, 0);
116 if (child_status != 0) {
117 std::cerr << "There was an error in the child." << std::endl;