-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbudd.c
488 lines (411 loc) · 11.1 KB
/
budd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* COPYRIGHT AND PERMISSION NOTICE
*
* Copyright (c) 2016, John Keeping <john@keeping.me.uk>
*
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of a copyright holder shall not
* be used in advertising or otherwise to promote the sale, use or other
* dealings in this Software without prior written authorization of the
* copyright holder.
*/
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <getopt.h>
#include <curl/curl.h>
/* XXX: NoIP only supports IPv4, so we do too. */
#define MAX_IP_LEN 16
#define BUDD_USERAGENT \
"BUDD/" BUDD_VERSION
enum log_level {
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
};
struct buf {
size_t alloc;
size_t sz;
char *data;
};
#define BUF_INC 1024
static struct {
const char *ip_url;
const char *update_url;
enum log_level log_threshold;
FILE *log_file;
const char *target_host;
const char *username;
const char *password;;
char public_ip[MAX_IP_LEN + 1];
char target_ip[MAX_IP_LEN + 1];
} state = {
.ip_url = "http://ip1.dynupdate.no-ip.com/",
.update_url = "https://dynupdate.no-ip.com/nic/update",
.log_threshold = LOG_INFO,
};
static void __attribute__((noreturn, format(printf, 1, 2)))
die(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
static void __attribute__((noreturn, format(printf, 1, 2)))
die_errno(const char *msg, ...)
{
int err = errno;
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fprintf(stderr, ": %s\n", strerror(err));
exit(1);
}
static void __attribute__((noreturn, format(printf, 2, 3)))
die_curl(CURLcode cres, const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fprintf(stderr, ": %s\n", curl_easy_strerror(cres));
exit(1);
}
static void __attribute__((format(printf, 2, 3)))
log_msg(enum log_level level, const char *msg, ...)
{
static const char *const level_names[] = {
[LOG_DEBUG] = "DEBUG",
[LOG_INFO] = "INFO",
[LOG_WARN] = "WARNING",
[LOG_ERROR] = "ERROR",
};
va_list ap;
if (level < state.log_threshold)
return;
va_start(ap, msg);
fprintf(state.log_file, "[%s] ", level_names[level]);
vfprintf(state.log_file, msg, ap);
fprintf(state.log_file, "\n");
va_end(ap);
}
#define log_debug(...) log_msg(LOG_DEBUG, __VA_ARGS__)
#define log_info(...) log_msg(LOG_INFO, __VA_ARGS__)
#define log_warn(...) log_msg(LOG_WARN, __VA_ARGS__)
#define log_error(...) log_msg(LOG_ERROR, __VA_ARGS__)
static char *xstrdup(const char *s)
{
char *r = strdup(s);
if (!r)
die("out of memory");
return r;
}
static int sane_isspace(char c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
static char *strip_ws(char *v)
{
char *end;
while (sane_isspace(*v))
v++;
end = v + strlen(v);
if (end != v)
end--;
while (end != v && sane_isspace(*end))
*end-- = '\0';
return v;
}
static size_t buf_write_cb(char *ptr,
size_t size, size_t nmemb, void *user)
{
struct buf *buf = user;
size_t realsize = size * nmemb;
if ((buf->alloc - buf->sz) < realsize) {
size_t newsize = buf->alloc + BUF_INC;
char *newdata = realloc(buf->data, newsize);
if (!newdata)
return 0;
buf->data = newdata;
buf->alloc = newsize;
}
memcpy(buf->data + buf->sz, ptr, realsize);
buf->sz += realsize;
return realsize;
}
static size_t public_ip_write_cb(char *ptr,
size_t size, size_t nmemb, void *user)
{
size_t *offset = user;
size_t len = size * nmemb;
if (len + *offset > MAX_IP_LEN)
len = MAX_IP_LEN - *offset;
if (len)
memcpy(state.public_ip + *offset, ptr, len);
*offset += len;
return len;
}
static CURLcode get_public_ip(void)
{
CURLcode cres;
size_t offset = 0;
CURL *curl = curl_easy_init();
if (!curl)
return CURLE_FAILED_INIT;
curl_easy_setopt(curl, CURLOPT_USERAGENT, BUDD_USERAGENT);
curl_easy_setopt(curl, CURLOPT_URL, state.ip_url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, public_ip_write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &offset);
log_debug("requesting public IP from %s", state.ip_url);
cres = curl_easy_perform(curl);
if (cres == CURLE_OK)
log_debug("public IP: %s", state.public_ip);
else
log_error("failed to get public IP: %s",
curl_easy_strerror(cres));
curl_easy_cleanup(curl);
return cres;
}
static int get_target_ip(void)
{
struct addrinfo *info, *ai;
struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
int ret;
log_debug("looking up current IP for %s", state.target_host);
ret = getaddrinfo(state.target_host, NULL, &hints, &info);
if (ret) {
log_error("failed to get current target IP: %s\n",
gai_strerror(ret));
return -1;
}
for (ai = info; ai; ai = ai->ai_next) {
struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
if (inet_ntop(ai->ai_family, &addr->sin_addr,
state.target_ip,
sizeof(state.target_ip) - 1))
break;
}
if (ai) {
log_debug("result: %s", state.target_ip);
ret = 0;
} else {
log_warn("failed to get current target IP: no address found");
ret = -1;
}
freeaddrinfo(info);
return ret;
}
static CURLcode check_result(struct buf *b, bool *fatal)
{
char nul = '\0';
char *s, *end;
*fatal = false;
/* Ensure buffer is null-terminated. */
if (buf_write_cb(&nul, 1, 1, b) != 1)
return CURLE_OUT_OF_MEMORY;
s = strip_ws(b->data);
log_debug("response: %s", s);
end = strchrnul(s, ' ');
*end = '\0';
if (!strcmp(s, "good") || !strcmp(s, "nochg"))
return CURLE_OK;
*end = ' ';
if (!strcmp(s, "911")) {
log_warn("server will not accept request now: %s", s);
return CURLE_ABORTED_BY_CALLBACK;
}
/* Everything else is fatal. */
*fatal = true;
if (!strcmp(s, "nohost"))
log_error("hostname not found under specified account");
else if (!strcmp(s, "badauth"))
log_error("bad authentication credentials");
else if (!strcmp(s, "badagent"))
log_error("client has been disabled by server");
else if (!strcmp(s, "!donator"))
log_error("requested feature is not available to current user");
else if (!strcmp(s, "abuse"))
log_error("user is blocked due to abuse");
return CURLE_ABORTED_BY_CALLBACK;
}
static CURLcode send_update(void)
{
CURLcode cres = CURLE_OUT_OF_MEMORY;
bool fatal_error;
char *url = NULL, *hostname = NULL, *ip = NULL;
struct buf result_buf = { 0 };
CURL *curl = curl_easy_init();
if (!curl)
return CURLE_FAILED_INIT;
hostname = curl_easy_escape(curl, state.target_host, 0);
ip = curl_easy_escape(curl, state.public_ip, 0);
if (!hostname || !ip)
goto out;
if (asprintf(&url, "%s?hostname=%s&myip=%s",
state.update_url, hostname, ip) < 0)
goto out;
curl_easy_setopt(curl, CURLOPT_USERAGENT, BUDD_USERAGENT);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, buf_write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result_buf);
/* NoIP only supports basic auth. */
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long) CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERNAME, state.username);
curl_easy_setopt(curl, CURLOPT_PASSWORD, state.password);
log_debug("sending request %s", url);
cres = curl_easy_perform(curl);
if (cres != CURLE_OK) {
log_error("failed to update IP for %s: %s",
state.target_host, curl_easy_strerror(cres));
goto out;
}
cres = check_result(&result_buf, &fatal_error);
if (cres == CURLE_OK)
log_info("updated IP for %s to %s",
state.target_host, state.public_ip);
out:
curl_free(hostname);
curl_free(ip);
curl_easy_cleanup(curl);
free(url);
free(result_buf.data);
return cres;
}
static void handle_config_entry(const char *key, const char *value)
{
if (!strcasecmp(key, "ip-url"))
state.ip_url = xstrdup(value);
else if (!strcasecmp(key, "update-url"))
state.update_url = xstrdup(value);
else if (!strcasecmp(key, "host"))
state.target_host = xstrdup(value);
else if (!strcasecmp(key, "username"))
state.username = xstrdup(value);
else if (!strcasecmp(key, "password"))
state.password = xstrdup(value);
else
die("unknown config key: %s", key);
}
static void load_config(const char *filename)
{
char *line = NULL;
size_t line_len = 0;
FILE *f = fopen(filename, "r");
if (!f)
die_errno("failed to open config file '%s'", filename);
while (getline(&line, &line_len, f) >= 0) {
char *key = line, *value;
/* Comments must start in column zero. */
if (*line == '#')
continue;
value = strchr(key, '=');
if (value) {
*value++ = '\0';
value = strip_ws(value);
}
key = strip_ws(key);
if (!(value && *value) && *key)
die("invalid config entry: %s", key);
if (*key)
handle_config_entry(key, value);
}
if (!feof(f))
die_errno("failed to read config file '%s'", filename);
free(line);
fclose(f);
if (!state.target_host)
die("'host' not specified in config");
if (!state.username)
die("'username' not specified in config");
if (!state.password)
die("'password' not specified in config");
}
static void usage(FILE *f)
{
fprintf(f, "usage: budd [--force] --config <file>\n");
}
static const struct option long_opts[] = {
{ "--config", required_argument, 0, 'c' },
{ "--force", no_argument, 0, 'f' },
{ "--quiet", no_argument, 0, 'q' },
{ "--verbose", no_argument, 0, 'v' },
{ "--help", no_argument, 0, 'h' },
{ NULL }
};
int main(int argc, char *argv[])
{
CURLcode cres;
int force = 0, c;
const char *config_file = NULL;
state.log_file = stderr;
while ((c = getopt_long(argc, argv, "c:fqvh", long_opts, NULL)) >= 0) {
switch (c) {
case 'c':
config_file = optarg;
break;
case 'f':
force = 1;
break;
case 'q':
state.log_threshold = LOG_WARN;
break;
case 'v':
state.log_threshold = LOG_DEBUG;
break;
case 'h':
usage(stdout);
return 0;
case '?':
die("unknown option: %s", optarg);
default:
die("BUG: unhandled option: %s", optarg);
}
}
if (!config_file)
die("no configuration file specified");
load_config(config_file);
cres = curl_global_init(CURL_GLOBAL_DEFAULT);
if (cres != CURLE_OK)
die_curl(cres, "Failed to initialize curl");
cres = get_public_ip();
if (cres != CURLE_OK)
die_curl(cres, "Failed to get public IP");
if (get_target_ip() || force ||
strcmp(state.public_ip, state.target_ip))
cres = send_update();
else
log_info("skipping update: IP is already correct");
curl_global_cleanup();
return cres == CURLE_OK ? 0 : 1;
}