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/property_tree/ptree.hpp>
35 #include <boost/property_tree/ini_parser.hpp>
36 #include <boost/iostreams/device/file_descriptor.hpp>
37 #include <boost/iostreams/stream.hpp>
39 using namespace boost;
40 typedef property_tree::ptree::path_type path;
42 static void write(int fd, const char *str)
44 size_t len = strlen(str);
45 ssize_t written = write(fd, str, len);
46 if (written < 0 || (size_t)written != len) {
47 std::cerr << "Error writing pipe." << std::endl;
52 int main(int argc, const char ** argv)
54 static const regex regex_ip("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
55 static const regex regex_password("[a-zA-Z0-9.:;,_-]+");
56 static const regex regex_domain("[a-zA-Z0-9.]+");
60 std::cerr << "Usage: " << argv[0] << " <domain> <password> <IP address>" << std::endl;
65 std::string domain = argv[1];
66 std::string password = argv[2];
67 std::string ip = argv[3];
70 if (!regex_match(ip, regex_ip)) {
71 std::cerr << "Invalid IP address " << ip << "." << std::endl;
74 if (!regex_match(domain, regex_domain)) {
75 std::cerr << "Invalid domain " << domain << "." << std::endl;
78 if (!regex_match(password, regex_password)) {
79 std::cerr << "Invalid password " << password << "." << std::endl;
83 /* read configuration */
84 property_tree::ptree config;
85 property_tree::ini_parser::read_ini(CONFIG_FILE, config);
86 std::string nsupdate = config.get<std::string>("nsupdate");
87 unsigned server_port = config.get<unsigned>("port", 53);
89 /* Given the domain, check whether the password matches */
90 optional<std::string> correct_password = config.get_optional<std::string>(path(domain+"/password", '/'));
91 if (!correct_password || *correct_password != password) {
92 std::cerr << "Password incorrect." << std::endl;
96 /* preapre the pipe */
98 if (pipe(pipe_ends) < 0) {
99 std::cerr << "Error opening pipe." << std::endl;
103 /* Launch nsupdate */
104 pid_t child_pid = fork();
106 std::cerr << "Error while forking." << std::endl;
109 if (child_pid == 0) {
110 /* We're in the child */
111 /* Close write end, use read end as stdin */
113 if (dup2(pipe_ends[0], fileno(stdin)) < 0) {
114 std::cerr << "There was an error redirecting stdin." << std::endl;
118 execl(nsupdate.c_str(), nsupdate.c_str(), "-p", std::to_string(server_port).c_str(), "-l", (char *)NULL);
119 /* There was an error */
120 std::cerr << "There was an error executing nsupdate." << std::endl;
124 /* Send it the command */
125 write(pipe_ends[1], "update delete ");
126 write(pipe_ends[1], domain.c_str());
127 write(pipe_ends[1], ". A\n");
129 write(pipe_ends[1], "update add ");
130 write(pipe_ends[1], domain.c_str());
131 write(pipe_ends[1], ". 60 A ");
132 write(pipe_ends[1], ip.c_str());
133 write(pipe_ends[1], "\n");
135 write(pipe_ends[1], "send\n");
137 /* Close both ends */
141 /* Wait for child to be gone */
143 waitpid(child_pid, &child_status, 0);
144 if (child_status != 0) {
145 std::cerr << "There was an error in the child." << std::endl;