tab -> whitespaces
[dyn-nsupdate.git] / dyn-nsupdate.cpp
1 /* Copyright (c) 2014, Ralf Jung <post@ralfj.de>
2 * All rights reserved.
3
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 *    list of conditions and the following disclaimer. 
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 *    this list of conditions and the following disclaimer in the documentation
11 *    and/or other materials provided with the distribution.
12
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24 * The views and conclusions contained in the software and documentation are those
25 * of the authors and should not be interpreted as representing official policies, 
26 * either expressed or implied, of the FreeBSD Project.
27 */
28
29 #include <iostream>
30 #include <fstream>
31 #include <sys/wait.h>
32
33 #include <boost/regex.hpp>
34 #include <boost/program_options.hpp>
35 #include <boost/property_tree/ptree.hpp>
36 #include <boost/property_tree/ini_parser.hpp>
37 #include <boost/iostreams/device/file_descriptor.hpp>
38 #include <boost/iostreams/stream.hpp>
39
40 namespace pt = boost::property_tree;
41 namespace po = boost::program_options;
42
43 static void write(int fd, const char *str)
44 {
45     size_t len = strlen(str);
46     ssize_t written = write(fd, str, len);
47     if (written < 0 || (size_t)written != len) {
48         std::cerr << "Error writing pipe." << std::endl;
49         exit(1);
50     }
51 }
52
53 int main(int argc, const char ** argv)
54 {
55     static const boost::regex regex_ip("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
56     static const boost::regex regex_password("[a-zA-Z0-9.:;,_-]+");
57     static const boost::regex regex_domain("[a-zA-Z0-9.]+");
58     
59     
60     if (argc != 4) {
61         std::cerr << "Usage: " << argv[0] << " <domain> <password> <IP address>" << std::endl;
62         return 1;
63     }
64     
65     /* Obtain input */
66     std::string domain = argv[1];
67     std::string password = argv[2];
68     std::string ip = argv[3];
69     
70     /* Validate input */
71     if (!regex_match(ip, regex_ip)) {
72         std::cerr << "Invalid IP address " << ip << "." << std::endl;
73         exit(1);
74     }
75     if (!regex_match(domain, regex_domain)) {
76         std::cerr << "Invalid domain " << domain << "." << std::endl;
77         exit(1);
78     }
79     if (!regex_match(password, regex_password)) {
80         std::cerr << "Invalid password " << password << "." << std::endl;
81         exit(1);
82     }
83     
84     /* read configuration */
85     pt::ptree config;
86     pt::ini_parser::read_ini(CONFIG_FILE, config);
87     std::string nsupdate = config.get<std::string>("nsupdate");
88     unsigned server_port = config.get<unsigned>("port", 53);
89     
90     /* Given the domain, check whether the password matches */
91     boost::optional<std::string> correct_password = config.get_optional<std::string>(pt::ptree::path_type(domain+"/password", '/'));
92     if (!correct_password || *correct_password != password) {
93         std::cerr << "Password incorrect." << std::endl;
94         exit(1);
95     }
96     
97     /* preapre the pipe */
98     int pipe_ends[2];
99     if (pipe(pipe_ends) < 0) {
100         std::cerr << "Error opening pipe." << std::endl;
101         exit(1);
102     }
103
104     /* Launch nsupdate */
105     pid_t child_pid = fork();
106     if (child_pid < 0) {
107         std::cerr << "Error while forking." << std::endl;
108         exit(1);
109     }
110     if (child_pid == 0) {
111         /* We're in the child */
112         /* Close write end, use read end as stdin */
113         close(pipe_ends[1]);
114         if (dup2(pipe_ends[0], fileno(stdin)) < 0) {
115             std::cerr << "There was an error redirecting stdin." << std::endl;
116             exit(1);
117         }
118         /* exec nsupdate */
119         execl(nsupdate.c_str(), nsupdate.c_str(), "-p", std::to_string(server_port).c_str(), "-l", (char *)NULL);
120         /* There was an error */
121         std::cerr << "There was an error executing nsupdate." << std::endl;
122         exit(1);
123     }
124     
125     /* Send it the command */
126     write(pipe_ends[1], "update delete ");
127     write(pipe_ends[1], domain.c_str());
128     write(pipe_ends[1], ". A\n");
129     
130     write(pipe_ends[1], "update add ");
131     write(pipe_ends[1], domain.c_str());
132     write(pipe_ends[1], ". 60 A ");
133     write(pipe_ends[1], ip.c_str());
134     write(pipe_ends[1], "\n");
135     
136     write(pipe_ends[1], "send\n");
137     
138     /* Close both ends */
139     close(pipe_ends[0]);
140     close(pipe_ends[1]);
141     
142     /* Wait for child to be gone */
143     int child_status;
144     waitpid(child_pid, &child_status, 0);
145     if (child_status != 0) {
146         std::cerr << "There was an error in the child." << std::endl;
147         exit(1);
148     }
149     
150     return 0;
151 }