document FritzBox setup
[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/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>
38
39 using namespace boost;
40 typedef property_tree::ptree::path_type path;
41
42 static void write(int fd, const char *str)
43 {
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;
48                 exit(1);
49         }
50 }
51
52 int main(int argc, const char ** argv)
53 {
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.]+");
57         
58         
59         if (argc != 4) {
60                 std::cerr << "Usage: " << argv[0] << " <domain> <password> <IP address>" << std::endl;
61                 return 1;
62         }
63         
64         /* Obtain input */
65         std::string domain = argv[1];
66         std::string password = argv[2];
67         std::string ip = argv[3];
68         
69         /* Validate input */
70         if (!regex_match(ip, regex_ip)) {
71                 std::cerr << "Invalid IP address " << ip << "." << std::endl;
72                 exit(1);
73         }
74         if (!regex_match(domain, regex_domain)) {
75                 std::cerr << "Invalid domain " << domain << "." << std::endl;
76                 exit(1);
77         }
78         if (!regex_match(password, regex_password)) {
79                 std::cerr << "Invalid password " << password << "." << std::endl;
80                 exit(1);
81         }
82         
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);
88         
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;
93                 exit(1);
94         }
95         
96         /* preapre the pipe */
97         int pipe_ends[2];
98         if (pipe(pipe_ends) < 0) {
99                 std::cerr << "Error opening pipe." << std::endl;
100                 exit(1);
101         }
102
103         /* Launch nsupdate */
104         pid_t child_pid = fork();
105         if (child_pid < 0) {
106                 std::cerr << "Error while forking." << std::endl;
107                 exit(1);
108         }
109         if (child_pid == 0) {
110                 /* We're in the child */
111                 /* Close write end, use read end as stdin */
112                 close(pipe_ends[1]);
113                 if (dup2(pipe_ends[0], fileno(stdin)) < 0) {
114                         std::cerr << "There was an error redirecting stdin." << std::endl;
115                         exit(1);
116                 }
117                 /* exec nsupdate */
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;
121                 exit(1);
122         }
123         
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");
128         
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");
134         
135         write(pipe_ends[1], "send\n");
136         
137         /* Close both ends */
138         close(pipe_ends[0]);
139         close(pipe_ends[1]);
140         
141         /* Wait for child to be gone */
142         int child_status;
143         waitpid(child_pid, &child_status, 0);
144         if (child_status != 0) {
145                 std::cerr << "There was an error in the child." << std::endl;
146                 exit(1);
147         }
148         
149         return 0;
150 }