1 /* Interface for main DDNS functions
2  *
3  * Copyright (C) 2003-2004  Narcis Ilisei <inarcis2002@hotpop.com>
4  * Copyright (C) 2006       Steve Horbachuk
5  * Copyright (C) 2010-2021  Joachim Wiberg <troglobit@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, visit the Free Software Foundation
19  * website at http://www.gnu.org/licenses/gpl-2.0.html or write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA  02110-1301, USA.
22  */
23 
24 #ifndef DDNS_H_
25 #define DDNS_H_
26 
27 #include "config.h"
28 #include "compat.h"
29 #include "os.h"
30 #include "error.h"
31 #include "http.h"
32 #include "log.h"
33 #include "plugin.h"
34 #include "queue.h"		/* BSD sys/queue.h API */
35 
36 #define VERSION_STRING	PACKAGE_NAME " version " VERSION " -- Dynamic DNS update client."
37 #define DDNS_USER_AGENT "inadyn/" VERSION " " PACKAGE_BUGREPORT
38 
39 /* 2017-01-05: Dyn.com does NOT support HTTPS for checkip */
40 #define DYNDNS_MY_IP_SERVER	"checkip.dyndns.com"
41 #define DYNDNS_MY_CHECKIP_URL	"/"
42 #define DYNDNS_MY_IP_SSL        DDNS_CHECKIP_SSL_UNSUPPORTED
43 
44 /*
45  * 2020-02-18: ipify.org support HTTPS and should be default for new
46  * providers, unless the provider has its own checkip infrastructure.
47  */
48 #define DDNS_MY_IP_SERVER       "api.ipify.org"
49 #define DDNS_MY_CHECKIP_URL	"/"
50 #define DDNS_MY_IP_SSL          DDNS_CHECKIP_SSL_SUPPORTED
51 
52 /* Some default settings */
53 #define DDNS_DEFAULT_STARTUP_SLEEP        0       /* sec */
54 #define DDNS_DEFAULT_PERIOD               120     /* sec */
55 #define DDNS_MIN_PERIOD                   30      /* sec */
56 #define DDNS_MAX_PERIOD                   (10 * 24 * 3600)        /* 10 days in sec */
57 #define DDNS_ERROR_UPDATE_PERIOD          600     /* 10 min */
58 #define DDNS_FORCED_UPDATE_PERIOD         (30 * 24 * 3600)        /* 30 days in sec */
59 #define DDNS_DEFAULT_CMD_CHECK_PERIOD     1       /* sec */
60 #define DDNS_DEFAULT_ITERATIONS           0       /* Forever */
61 #define DDNS_HTTP_RESPONSE_BUFFER_SIZE	  (BUFSIZ < 8192 ? 8192 : BUFSIZ) /* at least 8 Kib */
62 #define DDNS_HTTP_REQUEST_BUFFER_SIZE     2500    /* Bytes */
63 #define DDNS_MAX_ALIAS_NUMBER             50      /* maximum number of aliases per server that can be maintained */
64 #define DDNS_MAX_SERVER_NUMBER            5       /* maximum number of servers that can be maintained */
65 
66 /* SSL support status in plugin definition */
67 #define DDNS_CHECKIP_SSL_UNSUPPORTED     -1       /* HTTPS not supported by checkip-server (default) */
68 #define DDNS_CHECKIP_SSL_SUPPORTED        1       /* HTTPS supported by checkip-server */
69 #define DDNS_CHECKIP_SSL_REQUIRED         3       /* HTTPS required for checkip-server */
70 
71 /* local configs */
72 #define USERNAME_LEN                      128     /* chars */
73 #define PASSWORD_LEN                      256     /* chars */
74 #define SERVER_NAME_LEN                   256     /* chars */
75 #define SERVER_URL_LEN                    256     /* chars */
76 #ifdef INET6_ADDRSTRLEN
77 # define MAX_ADDRESS_LEN                  INET6_ADDRSTRLEN
78 #else
79 # define MAX_ADDRESS_LEN                  46
80 #endif
81 
82 #define MAX_NUM_RESPONSES                 5
83 #define MAX_RESPONSE_LEN                  32
84 
85 typedef enum {
86 	NO_CMD = 0,
87 	CMD_STOP,
88 	CMD_RESTART,
89 	CMD_FORCED_UPDATE,
90 	CMD_CHECK_NOW,
91 } ddns_cmd_t;
92 
93 typedef enum {
94 	EXEC_MODE_COMPAT,
95 	EXEC_MODE_EVENT
96 } ddns_exec_mode_t;
97 
98 typedef struct {
99 	char           username[USERNAME_LEN];
100 	char           password[PASSWORD_LEN];
101 	char          *encoded_password;
102 	int            size;
103 	int            encoded;
104 } ddns_creds_t;
105 
106 /* Server name and port */
107 typedef struct {
108 	char           name[SERVER_NAME_LEN];
109 	int            port;
110 } ddns_name_t;
111 
112 typedef struct {
113 	int            ip_has_changed;
114 	char           address[MAX_ADDRESS_LEN];
115 
116 	char           name[SERVER_NAME_LEN];
117 	int            update_required;
118 	time_t         last_update;
119 } ddns_alias_t;
120 
121 typedef struct di {
122 	LIST_ENTRY(di) link;
123 	int            id;
124 
125 	ddns_creds_t   creds;
126 	ddns_system_t *system;
127 
128 	/* Per provider custom user agent */
129 	char          *user_agent;
130 
131 	/* Address of DDNS update service */
132 	ddns_name_t    server_name;
133 	char           server_url[SERVER_URL_LEN];
134 	http_t         server;
135 
136 	/* Possble "OK" responses from DDNS provider */
137 	char           server_response[MAX_NUM_RESPONSES][MAX_RESPONSE_LEN];
138 	size_t         server_response_num;
139 
140 	/* Address of "What's my IP" checker */
141 	ddns_name_t    checkip_name;
142 	char           checkip_url[SERVER_URL_LEN];
143 	int            checkip_ssl; /* checkip server ssl mode */
144 	http_t         checkip;
145 
146 	/* Shell command for "What's my IP" checker */
147 	char          *checkip_cmd;
148 
149 	/* Optional local proxy server for this DDNS provider */
150 	tcp_proxy_type_t proxy_type;
151 	ddns_name_t    proxy_name;
152 
153 	/* Your aliases/names to update */
154 	ddns_alias_t   alias[DDNS_MAX_ALIAS_NUMBER];
155 	size_t         alias_count;
156 
157 	/* Use wildcard, *.foo.bar */
158 	int            wildcard;
159 
160 	/* DNS ttl option */
161 	long int       ttl;
162 
163 	/* CDN proxied option */
164 	int            proxied;
165 
166 	/*
167 	 * Provider specific data, per-conf-entry.  E.g., the Cloudflare
168 	 * plugin stores zone_id and hostname_id here.  Set up by the
169 	 * plugin setup callback, and is automatically freed by Inadyn.
170 	 */
171 	void          *data;
172 
173 	/* Does the provider support SSL? */
174 	int            ssl_enabled;
175 	int            append_myip; /* For custom setups! */
176 } ddns_info_t;
177 
178 /* Client context */
179 typedef struct {
180 	char          *cfgfile;
181 
182 	ddns_cmd_t     cmd;
183 	int            update_period; /* time between 2 updates */
184 	int            normal_update_period_sec;
185 	int            error_update_period_sec;
186 	int            forced_update_period_sec;
187 	int            forced_update_fake_addr;
188 	int            cmd_check_period; /*time to wait for a command */
189 	int            total_iterations;
190 	int            num_iterations;
191 	int            initialized;
192 	int            change_persona;
193 	int            force_addr_update;
194 	int            use_proxy;
195 	int            abort;
196 
197 	http_trans_t   http_transaction;
198 
199 	char          *work_buf; /* for HTTP responses */
200 	int            work_buflen;
201 
202 	char          *request_buf; /* for HTTP requests */
203 	size_t         request_buflen;
204 } ddns_t;
205 
206 extern int once;
207 extern int force;
208 extern int ignore_errors;
209 extern int startup_delay;
210 extern int allow_ipv6;
211 extern int verify_addr;
212 extern int exec_mode;
213 extern char *ident;
214 extern char *prognm;
215 extern char *iface;
216 extern char *use_iface;		/* Command line option */
217 extern char *user_agent;
218 extern char *script_cmd;
219 extern char *script_exec;
220 extern char *pidfile_name;
221 extern const char * const generic_responses[];
222 extern uid_t uid;
223 extern gid_t gid;
224 
225 int ddns_main_loop (ddns_t *ctx);
226 
227 int common_request (ddns_t       *ctx,   ddns_info_t *info, ddns_alias_t *alias);
228 int common_response(http_trans_t *trans, ddns_info_t *info, ddns_alias_t *alias);
229 
230 #endif /* DDNS_H_ */
231 
232 /**
233  * Local Variables:
234  *  indent-tabs-mode: t
235  *  c-file-style: "linux"
236  * End:
237  */
238