run nsupdate as child and send it the update commands
[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
13 static void write(int fd, const char *str)
14 {
15         size_t len = strlen(str);
16         ssize_t written = write(fd, str, len);
17         if (written < 0 || (size_t)written != len) {
18                 std::cerr << "Error writing pipe." << std::endl;
19                 exit(1);
20         }
21 }
22
23 int main(int argc, const char ** argv)
24 {
25         static const regex regex_ip("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
26         static const regex regex_user("[a-zA-Z]+");
27         
28         
29         if (argc != 5) {
30                 std::cerr << "Usage: " << argv[0] << " <username> <password> <domain> <IP address>" << std::endl;
31                 return 1;
32         }
33         
34         /* Obtain and validate inpit */
35         std::string user = argv[1];
36         std::string password = argv[2];
37         std::string domain = argv[3];
38         std::string ip = argv[4];
39         
40         if (!regex_match(ip, regex_ip)) {
41                 std::cerr << "Invalid IP address " << ip << "." << std::endl;
42                 exit(1);
43         }
44         if (!regex_match(user, regex_user)) {
45                 std::cerr << "Invalid username " << user << "." << std::endl;
46                 exit(1);
47         }
48         
49         /* read configuration */
50         property_tree::ptree config;
51         property_tree::ini_parser::read_ini(CONFIG_FILE, config);
52         std::string nsupdate = config.get<std::string>("nsupdate");
53         std::string keyfile = config.get<std::string>("key");
54         
55         /* Check username, password, domain */
56         optional<std::string> correct_password = config.get_optional<std::string>(user+".password");
57         if (!correct_password || *correct_password != password) {
58                 std::cerr << "Username or password incorrect." << std::endl;
59                 exit(1);
60         }
61         if (config.get<std::string>(user+".domain") != domain) {
62                 std::cerr << "Domain incorrect." << std::endl;
63                 exit(1);
64         }
65         
66         /* preapre the pipe */
67         int pipe_ends[] = {0,0};
68         pipe(pipe_ends);
69
70         /* Launch nsupdate */
71         pid_t child_pid = fork();
72         if (child_pid < 0) {
73                 std::cerr << "Error while forking." << std::endl;
74                 exit(1);
75         }
76         if (child_pid == 0) {
77                 /* We're in the child */
78                 /* Close write end, use read and as stdin */
79                 close(pipe_ends[1]);
80                 dup2(pipe_ends[0], fileno(stdin));
81                 /* exec nsupdate */
82                 execl(nsupdate.c_str(), nsupdate.c_str(), "-k", keyfile.c_str(), NULL);
83                 /* There was an error */
84                 std::cerr << "There was an error executing nsupdate." << std::endl;
85                 exit(1);
86         }
87         
88         /* Send it the command */
89         write(pipe_ends[1], "server localhost\n");
90         
91         write(pipe_ends[1], "update delete ");
92         write(pipe_ends[1], domain.c_str());
93         write(pipe_ends[1], ".\n");
94         
95         write(pipe_ends[1], "update add ");
96         write(pipe_ends[1], domain.c_str());
97         write(pipe_ends[1], ". 60 A ");
98         write(pipe_ends[1], ip.c_str());
99         write(pipe_ends[1], "\n");
100         
101         write(pipe_ends[1], "send\n");
102         
103         /* Close both ends */
104         close(pipe_ends[0]);
105         close(pipe_ends[1]);
106         
107         /* Wait for child to be gone */
108         int child_status;
109         waitpid(child_pid, &child_status, 0);
110         if (child_status != 0) {
111                 std::cerr << "There was an error in the child." << std::endl;
112                 exit(1);
113         }
114         
115         return 0;
116 }