| 1 | /* |
|---|
| 2 | * ddns.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2006 Sebastian Gottschall <gottschall@dd-wrt.com> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of the GNU General Public License |
|---|
| 8 | * as published by the Free Software Foundation; either version 2 |
|---|
| 9 | * of the License, or (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * $Id: |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | #include <signal.h> |
|---|
| 27 | #include <unistd.h> |
|---|
| 28 | #include <errno.h> |
|---|
| 29 | #include <sys/ioctl.h> |
|---|
| 30 | #include <sys/types.h> |
|---|
| 31 | #include <sys/stat.h> |
|---|
| 32 | #include <sys/socket.h> |
|---|
| 33 | #include <netinet/in.h> |
|---|
| 34 | #include <arpa/inet.h> |
|---|
| 35 | #include <sys/time.h> |
|---|
| 36 | #include <syslog.h> |
|---|
| 37 | #include <sys/wait.h> |
|---|
| 38 | |
|---|
| 39 | #include <bcmnvram.h> |
|---|
| 40 | #include <netconf.h> |
|---|
| 41 | #include <shutils.h> |
|---|
| 42 | #include <utils.h> |
|---|
| 43 | #include <rc.h> |
|---|
| 44 | |
|---|
| 45 | /* |
|---|
| 46 | * inadyn scripts by lawnmowerguy1 |
|---|
| 47 | */ |
|---|
| 48 | |
|---|
| 49 | char service[32]; |
|---|
| 50 | char _username[] = "ddns_username_X"; |
|---|
| 51 | char _passwd[] = "ddns_passwd_X"; |
|---|
| 52 | char _hostname[] = "ddns_hostname_X"; |
|---|
| 53 | char _dyndnstype[] = "ddns_dyndnstype_X"; |
|---|
| 54 | char _wildcard[] = "ddns_wildcard_X"; |
|---|
| 55 | char _url[] = "ddns_url_X"; |
|---|
| 56 | char _conf[] = "ddns_conf_X"; |
|---|
| 57 | |
|---|
| 58 | int init_ddns(void) |
|---|
| 59 | { |
|---|
| 60 | |
|---|
| 61 | int flag = atoi(nvram_safe_get("ddns_enable")); |
|---|
| 62 | |
|---|
| 63 | switch (flag) { |
|---|
| 64 | case 0: // ddns disabled |
|---|
| 65 | return -1; |
|---|
| 66 | break; |
|---|
| 67 | |
|---|
| 68 | case 1: |
|---|
| 69 | if (nvram_match("ddns_dyndnstype", "2")) |
|---|
| 70 | strcpy(service, "statdns@dyndns.org"); |
|---|
| 71 | else if (nvram_match("ddns_dyndnstype", "3")) |
|---|
| 72 | strcpy(service, "custom@dyndns.org"); |
|---|
| 73 | else |
|---|
| 74 | strcpy(service, "dyndns@dyndns.org"); |
|---|
| 75 | |
|---|
| 76 | snprintf(_username, sizeof(_username), "%s", "ddns_username"); |
|---|
| 77 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd"); |
|---|
| 78 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname"); |
|---|
| 79 | snprintf(_dyndnstype, sizeof(_dyndnstype), "%s", |
|---|
| 80 | "ddns_dyndnstype"); |
|---|
| 81 | snprintf(_wildcard, sizeof(_wildcard), "%s", "ddns_wildcard"); |
|---|
| 82 | break; |
|---|
| 83 | |
|---|
| 84 | case 2: |
|---|
| 85 | strcpy(service, "default@freedns.afraid.org"); |
|---|
| 86 | |
|---|
| 87 | snprintf(_username, sizeof(_username), "%s", "ddns_username_2"); |
|---|
| 88 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd_2"); |
|---|
| 89 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname_2"); |
|---|
| 90 | break; |
|---|
| 91 | |
|---|
| 92 | case 3: |
|---|
| 93 | strcpy(service, "default@zoneedit.com"); |
|---|
| 94 | |
|---|
| 95 | snprintf(_username, sizeof(_username), "%s", "ddns_username_3"); |
|---|
| 96 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd_3"); |
|---|
| 97 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname_3"); |
|---|
| 98 | break; |
|---|
| 99 | |
|---|
| 100 | case 4: |
|---|
| 101 | strcpy(service, "default@no-ip.com"); |
|---|
| 102 | |
|---|
| 103 | snprintf(_username, sizeof(_username), "%s", "ddns_username_4"); |
|---|
| 104 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd_4"); |
|---|
| 105 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname_4"); |
|---|
| 106 | break; |
|---|
| 107 | |
|---|
| 108 | case 5: |
|---|
| 109 | strcpy(service, "custom@http_svr_basic_auth"); |
|---|
| 110 | |
|---|
| 111 | snprintf(_username, sizeof(_username), "%s", "ddns_username_5"); |
|---|
| 112 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd_5"); |
|---|
| 113 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname_5"); |
|---|
| 114 | snprintf(_url, sizeof(_url), "%s", "ddns_url"); |
|---|
| 115 | snprintf(_conf, sizeof(_conf), "%s", "ddns_conf"); |
|---|
| 116 | break; |
|---|
| 117 | |
|---|
| 118 | case 6: |
|---|
| 119 | strcpy(service, "dyndns@3322.org"); |
|---|
| 120 | |
|---|
| 121 | snprintf(_username, sizeof(_username), "%s", "ddns_username_6"); |
|---|
| 122 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd_6"); |
|---|
| 123 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname_6"); |
|---|
| 124 | snprintf(_dyndnstype, sizeof(_dyndnstype), "%s", |
|---|
| 125 | "ddns_dyndnstype_6"); |
|---|
| 126 | snprintf(_wildcard, sizeof(_wildcard), "%s", "ddns_wildcard_6"); |
|---|
| 127 | break; |
|---|
| 128 | |
|---|
| 129 | case 7: |
|---|
| 130 | strcpy(service, "default@easydns.com"); |
|---|
| 131 | |
|---|
| 132 | snprintf(_username, sizeof(_username), "%s", "ddns_username_7"); |
|---|
| 133 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd_7"); |
|---|
| 134 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname_7"); |
|---|
| 135 | snprintf(_wildcard, sizeof(_wildcard), "%s", "ddns_wildcard_7"); |
|---|
| 136 | break; |
|---|
| 137 | |
|---|
| 138 | case 8: |
|---|
| 139 | strcpy(service, "default@tzo.com"); |
|---|
| 140 | |
|---|
| 141 | snprintf(_username, sizeof(_username), "%s", "ddns_username_8"); |
|---|
| 142 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd_8"); |
|---|
| 143 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname_8"); |
|---|
| 144 | break; |
|---|
| 145 | |
|---|
| 146 | case 9: |
|---|
| 147 | strcpy(service, "default@dynsip.org"); |
|---|
| 148 | |
|---|
| 149 | snprintf(_username, sizeof(_username), "%s", "ddns_username_9"); |
|---|
| 150 | snprintf(_passwd, sizeof(_passwd), "%s", "ddns_passwd_9"); |
|---|
| 151 | snprintf(_hostname, sizeof(_hostname), "%s", "ddns_hostname_9"); |
|---|
| 152 | break; |
|---|
| 153 | |
|---|
| 154 | default: |
|---|
| 155 | return -1; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | return 0; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | void start_ddns(void) |
|---|
| 162 | { |
|---|
| 163 | int ret; |
|---|
| 164 | FILE *fp; |
|---|
| 165 | |
|---|
| 166 | /* |
|---|
| 167 | * Get correct username, password and hostname |
|---|
| 168 | */ |
|---|
| 169 | if (init_ddns() < 0) |
|---|
| 170 | return; |
|---|
| 171 | |
|---|
| 172 | /* |
|---|
| 173 | * We don't want to update, if user don't input below field |
|---|
| 174 | */ |
|---|
| 175 | if (nvram_match(_username, "") || |
|---|
| 176 | nvram_match(_passwd, "") || nvram_match(_hostname, "")) |
|---|
| 177 | return; |
|---|
| 178 | |
|---|
| 179 | mkdir("/tmp/ddns", 0744); |
|---|
| 180 | |
|---|
| 181 | if (strcmp(nvram_safe_get("ddns_enable_buf"), nvram_safe_get("ddns_enable")) || // ddns |
|---|
| 182 | // mode |
|---|
| 183 | // change |
|---|
| 184 | strcmp(nvram_safe_get("ddns_username_buf"), nvram_safe_get(_username)) || // ddns |
|---|
| 185 | // username |
|---|
| 186 | // chane |
|---|
| 187 | strcmp(nvram_safe_get("ddns_passwd_buf"), nvram_safe_get(_passwd)) || // ddns |
|---|
| 188 | // password |
|---|
| 189 | // change |
|---|
| 190 | strcmp(nvram_safe_get("ddns_hostname_buf"), nvram_safe_get(_hostname)) || // ddns |
|---|
| 191 | // hostname |
|---|
| 192 | // change |
|---|
| 193 | strcmp(nvram_safe_get("ddns_dyndnstype_buf"), nvram_safe_get(_dyndnstype)) || // ddns |
|---|
| 194 | // dyndnstype |
|---|
| 195 | // change |
|---|
| 196 | strcmp(nvram_safe_get("ddns_wildcard_buf"), nvram_safe_get(_wildcard)) || // ddns |
|---|
| 197 | // wildcard |
|---|
| 198 | // change |
|---|
| 199 | strcmp(nvram_safe_get("ddns_url_buf"), nvram_safe_get(_url)) || // ddns |
|---|
| 200 | // url |
|---|
| 201 | // change |
|---|
| 202 | strcmp(nvram_safe_get("ddns_conf_buf"), nvram_safe_get(_conf)) || // ddns |
|---|
| 203 | // conf |
|---|
| 204 | // change |
|---|
| 205 | strcmp(nvram_safe_get("ddns_custom_5_buf"), |
|---|
| 206 | nvram_safe_get("ddns_custom_5"))) { |
|---|
| 207 | /* |
|---|
| 208 | * If the user changed anything in the GUI, delete all cache and log |
|---|
| 209 | */ |
|---|
| 210 | nvram_unset("ddns_cache"); |
|---|
| 211 | nvram_unset("ddns_time"); |
|---|
| 212 | unlink("/tmp/ddns/ddns.log"); |
|---|
| 213 | unlink("/tmp/ddns/inadyn_ip.cache"); |
|---|
| 214 | unlink("/tmp/ddns/inadyn_time.cache"); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /* |
|---|
| 218 | * Generate ddns configuration file |
|---|
| 219 | */ |
|---|
| 220 | if ((fp = fopen("/tmp/ddns/inadyn.conf", "w"))) { |
|---|
| 221 | fprintf(fp, "--background"); |
|---|
| 222 | fprintf(fp, " --dyndns_system %s", service); // service |
|---|
| 223 | fprintf(fp, " -u %s", nvram_safe_get(_username)); // username/email |
|---|
| 224 | fprintf(fp, " -p %s", nvram_safe_get(_passwd)); // password |
|---|
| 225 | fprintf(fp, " -a %s", nvram_safe_get(_hostname)); // alias/hostname |
|---|
| 226 | if (nvram_match("ddns_enable", "1") |
|---|
| 227 | || nvram_match("ddns_enable", "6") |
|---|
| 228 | || nvram_match("ddns_enable", "7")) { |
|---|
| 229 | if (nvram_match(_wildcard, "1")) |
|---|
| 230 | fprintf(fp, " --wildcard"); |
|---|
| 231 | } |
|---|
| 232 | if (nvram_match("ddns_enable", "7")) |
|---|
| 233 | fprintf(fp, " --update_period_sec %s", "900"); // check ip |
|---|
| 234 | // every 15 |
|---|
| 235 | // mins |
|---|
| 236 | else |
|---|
| 237 | fprintf(fp, " --update_period_sec %s", "360"); // check ip |
|---|
| 238 | // every 6 |
|---|
| 239 | // mins |
|---|
| 240 | // fprintf (fp, " --forced_update_period %s", "2419200"); //force |
|---|
| 241 | // update after 28days |
|---|
| 242 | fprintf(fp, " --forced_update_period %d", atoi(nvram_safe_get("ddns_force")) * 24 * 60 * 60); // force |
|---|
| 243 | // update |
|---|
| 244 | // after |
|---|
| 245 | // 28days |
|---|
| 246 | fprintf(fp, " --log_file %s", "/tmp/ddns/ddns.log"); // log to |
|---|
| 247 | // file |
|---|
| 248 | fprintf(fp, " --cache_dir %s", "/tmp/ddns"); // cache dir |
|---|
| 249 | fprintf(fp, " --exec %s", "ddns_success"); // run after update |
|---|
| 250 | if (nvram_match("ddns_enable", "5")) { |
|---|
| 251 | fprintf(fp, " --dyndns_server_name %s", |
|---|
| 252 | nvram_safe_get("ddns_custom_5")); |
|---|
| 253 | if (nvram_invmatch(_url, "")) |
|---|
| 254 | fprintf(fp, " --dyndns_server_url %s", |
|---|
| 255 | nvram_safe_get(_url)); |
|---|
| 256 | if (nvram_invmatch(_conf, "")) |
|---|
| 257 | fprintf(fp, " %s", nvram_safe_get(_conf)); |
|---|
| 258 | } |
|---|
| 259 | fprintf(fp, "\n"); |
|---|
| 260 | fclose(fp); |
|---|
| 261 | } else { |
|---|
| 262 | perror("/tmp/ddns/inadyn.conf"); |
|---|
| 263 | return; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /* |
|---|
| 267 | * Restore cache data to file from NV |
|---|
| 268 | */ |
|---|
| 269 | if (nvram_invmatch("ddns_cache", "") |
|---|
| 270 | && nvram_invmatch("ddns_time", "")) { |
|---|
| 271 | nvram2file("ddns_cache", "/tmp/ddns/inadyn_ip.cache"); |
|---|
| 272 | nvram2file("ddns_time", "/tmp/ddns/inadyn_time.cache"); |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | ret = eval("inadyn", "--input_file", "/tmp/ddns/inadyn.conf"); |
|---|
| 276 | dd_syslog(LOG_INFO, "DDNS : inadyn daemon successfully started\n"); |
|---|
| 277 | |
|---|
| 278 | cprintf("done\n"); |
|---|
| 279 | |
|---|
| 280 | return; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | void stop_ddns(void) |
|---|
| 284 | { |
|---|
| 285 | int ret; |
|---|
| 286 | |
|---|
| 287 | if (pidof("inadyn") > 0) |
|---|
| 288 | dd_syslog(LOG_INFO, |
|---|
| 289 | "DDNS : inadyn daemon successfully stopped\n"); |
|---|
| 290 | |
|---|
| 291 | unlink("/tmp/ddns/ddns.log"); |
|---|
| 292 | ret = killall("inadyn", SIGTERM); |
|---|
| 293 | |
|---|
| 294 | cprintf("done\n"); |
|---|
| 295 | |
|---|
| 296 | return; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | int ddns_success_main(int argc, char *argv[]) |
|---|
| 300 | { |
|---|
| 301 | char buf[80]; |
|---|
| 302 | char buf2[80]; |
|---|
| 303 | FILE *fp; |
|---|
| 304 | |
|---|
| 305 | init_ddns(); |
|---|
| 306 | |
|---|
| 307 | if ((fp = fopen("/tmp/ddns/inadyn_ip.cache", "r"))) { |
|---|
| 308 | fgets(buf, sizeof(buf), fp); |
|---|
| 309 | fclose(fp); |
|---|
| 310 | nvram_set("ddns_cache", buf); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | if ((fp = fopen("/tmp/ddns/inadyn_time.cache", "r"))) { |
|---|
| 314 | fgets(buf2, sizeof(buf2), fp); |
|---|
| 315 | fclose(fp); |
|---|
| 316 | nvram_set("ddns_time", buf2); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | nvram_set("ddns_enable_buf", nvram_safe_get("ddns_enable")); |
|---|
| 320 | nvram_set("ddns_username_buf", nvram_safe_get(_username)); |
|---|
| 321 | nvram_set("ddns_passwd_buf", nvram_safe_get(_passwd)); |
|---|
| 322 | nvram_set("ddns_hostname_buf", nvram_safe_get(_hostname)); |
|---|
| 323 | nvram_set("ddns_dyndnstype_buf", nvram_safe_get(_dyndnstype)); |
|---|
| 324 | nvram_set("ddns_wildcard_buf", nvram_safe_get(_wildcard)); |
|---|
| 325 | nvram_set("ddns_conf_buf", nvram_safe_get(_conf)); |
|---|
| 326 | nvram_set("ddns_url_buf", nvram_safe_get(_url)); |
|---|
| 327 | nvram_set("ddns_custom_5_buf", nvram_safe_get("ddns_custom_5")); |
|---|
| 328 | |
|---|
| 329 | nvram_commit(); |
|---|
| 330 | |
|---|
| 331 | cprintf("done\n"); |
|---|
| 332 | |
|---|
| 333 | return 0; |
|---|
| 334 | } |
|---|