1 /* Copyright (c) 2014, Ralf Jung <post@ralfj.de>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
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>
40 namespace pt = boost::property_tree;
41 namespace po = boost::program_options;
43 static void write(int fd, const char *str)
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;
53 int main(int argc, const char ** argv)
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.]+");
61 std::cerr << "Usage: " << argv[0] << " <domain> <password> <IP address>" << std::endl;
66 std::string domain = argv[1];
67 std::string password = argv[2];
68 std::string ip = argv[3];
71 if (!regex_match(ip, regex_ip)) {
72 std::cerr << "Invalid IP address " << ip << "." << std::endl;
75 if (!regex_match(domain, regex_domain)) {
76 std::cerr << "Invalid domain " << domain << "." << std::endl;
79 if (!regex_match(password, regex_password)) {
80 std::cerr << "Invalid password " << password << "." << std::endl;
84 /* read configuration */
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);
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;
97 /* preapre the pipe */
99 if (pipe(pipe_ends) < 0) {
100 std::cerr << "Error opening pipe." << std::endl;
104 /* Launch nsupdate */
105 pid_t child_pid = fork();
107 std::cerr << "Error while forking." << std::endl;
110 if (child_pid == 0) {
111 /* We're in the child */
112 /* Close write end, use read end as stdin */
114 if (dup2(pipe_ends[0], fileno(stdin)) < 0) {
115 std::cerr << "There was an error redirecting stdin." << std::endl;
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;
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");
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");
136 write(pipe_ends[1], "send\n");
138 /* Close both ends */
142 /* Wait for child to be gone */
144 waitpid(child_pid, &child_status, 0);
145 if (child_status != 0) {
146 std::cerr << "There was an error in the child." << std::endl;