support multiple domains in update script; get rid of usernames
[dyn-nsupdate.git] / dyn-nsupdate.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <sys/wait.h>
4
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>
10
11 using namespace boost;
12 typedef property_tree::ptree::path_type path;
13
14 static void write(int fd, const char *str)
15 {
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;
20                 exit(1);
21         }
22 }
23
24 int main(int argc, const char ** argv)
25 {
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.]+");
29         
30         
31         if (argc != 4) {
32                 std::cerr << "Usage: " << argv[0] << " <domain> <password> <IP address>" << std::endl;
33                 return 1;
34         }
35         
36         /* Obtain input */
37         std::string domain = argv[1];
38         std::string password = argv[2];
39         std::string ip = argv[3];
40         
41         /* Validate input */
42         if (!regex_match(ip, regex_ip)) {
43                 std::cerr << "Invalid IP address " << ip << "." << std::endl;
44                 exit(1);
45         }
46         if (!regex_match(domain, regex_domain)) {
47                 std::cerr << "Invalid domain " << domain << "." << std::endl;
48                 exit(1);
49         }
50         if (!regex_match(password, regex_password)) {
51                 std::cerr << "Invalid password " << password << "." << std::endl;
52                 exit(1);
53         }
54         
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         
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;
64                 exit(1);
65         }
66         
67         /* preapre the pipe */
68         int pipe_ends[2];
69         if (pipe(pipe_ends) < 0) {
70                 std::cerr << "Error opening pipe." << std::endl;
71                 exit(1);
72         }
73
74         /* Launch nsupdate */
75         pid_t child_pid = fork();
76         if (child_pid < 0) {
77                 std::cerr << "Error while forking." << std::endl;
78                 exit(1);
79         }
80         if (child_pid == 0) {
81                 /* We're in the child */
82                 /* Close write end, use read end as stdin */
83                 close(pipe_ends[1]);
84                 if (dup2(pipe_ends[0], fileno(stdin)) < 0) {
85                         std::cerr << "There was an error redirecting stdin." << std::endl;
86                         exit(1);
87                 }
88                 /* exec nsupdate */
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;
92                 exit(1);
93         }
94         
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");
99         
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");
105         
106         write(pipe_ends[1], "send\n");
107         
108         /* Close both ends */
109         close(pipe_ends[0]);
110         close(pipe_ends[1]);
111         
112         /* Wait for child to be gone */
113         int child_status;
114         waitpid(child_pid, &child_status, 0);
115         if (child_status != 0) {
116                 std::cerr << "There was an error in the child." << std::endl;
117                 exit(1);
118         }
119         
120         return 0;
121 }