-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
cache.c
205 lines (171 loc) · 5.66 KB
/
cache.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
/* Reads and updates cache files
*
* Copyright (C) 2003-2004 Narcis Ilisei <[email protected]>
* Copyright (C) 2010-2021 Joachim Wiberg <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, visit the Free Software Foundation
* website at http://www.gnu.org/licenses/gpl-2.0.html or write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* A user may have several DNS records to update. Earlier versions of
* inadyn supports this, but only recorded changes in one cache file.
* This made keeping track of update times per record impossible. Now
* inadyn records each DNS entry to be updated in its own cache file,
* enabling individual updates and tracking the file MTIME better.
*
* At startup inadyn will fall back to the old cache file and remove it
* once it has read the IP and the modification time.
*/
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>
#include <sys/stat.h>
#include "ddns.h"
#include "cache.h"
extern ddns_info_t *conf_info_iterator(int first);
static int nslookup(ddns_alias_t *alias)
{
int error;
struct addrinfo hints;
struct addrinfo *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* IPv4 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
error = getaddrinfo(alias->name, NULL, &hints, &result);
if (!error) {
char address[MAX_ADDRESS_LEN];
/* DNS reply for alias found, convert to IP# */
if (!getnameinfo(result->ai_addr, result->ai_addrlen, address, sizeof(address), NULL, 0, NI_NUMERICHOST)) {
/* Update local record for next checkip call. */
alias->last_update = 0;
strlcpy(alias->address, address, sizeof(alias->address));
logit(LOG_INFO, "Resolving hostname %s => IP# %s", alias->name, address);
}
freeaddrinfo(result);
return 0;
}
logit(LOG_WARNING, "Failed resolving hostname %s: %s", alias->name, gai_strerror(error));
return 1;
}
static void read_one(ddns_alias_t *alias, const char *name, int nonslookup)
{
FILE *fp;
char path[256];
alias->last_update = 0;
memset(alias->address, 0, sizeof(alias->address));
cache_file(alias->name, name, path, sizeof(path));
fp = fopen(path, "r");
if (!fp) {
/* Exception for dnsomatic's special global hostname */
if (nonslookup || !strncmp(alias->name, "all.dnsomatic.com", sizeof(alias->name)))
return;
/* Try a DNS lookup of our last known IP#. */
nslookup(alias);
} else {
struct stat st;
char address[MAX_ADDRESS_LEN];
if (fgets(address, sizeof(address), fp)) {
logit(LOG_INFO, "Cached IP# %s for %s from previous invocation.", address, alias->name);
strlcpy(alias->address, address, sizeof(alias->address));
}
/* Initialize time since last update from modification time of cache file. */
if (!fstat(fileno(fp), &st)) {
alias->last_update = st.st_mtime;
logit(LOG_INFO, "Last update of %s on %s", alias->name, ctime(&st.st_mtime));
}
fclose(fp);
}
}
char *cache_file(char *name, const char *sysname, char *buf, size_t len)
{
if (!buf || !name)
return NULL;
if (snprintf(buf, len, "%s/%s-%s.cache", cache_dir, sysname, name) >= (int)len)
logit(LOG_WARNING, "Too long name for buffer: '%s/' + '%s' + '.cache'", cache_dir, name);
return buf;
}
/*
* At boot, or when restarting inadyn at runtime, the memory struct holding our
* current IP# is empty. We want to avoid unnecessary updates of our DDNS server
* record, since we might get locked out for abuse, so we "seed" each of the DDNS
* records of our struct with the cached IP# from our cache file, or from a regular
* DNS query.
*/
int read_cache_file(ddns_t *ctx)
{
ddns_info_t *info;
/*
* Clear DNS cache before querying for the IP below, this to
* prevent any artefacts from, e.g., nscd, which is a known
* problem with DDNS clients.
*/
res_init();
if (!ctx)
return RC_INVALID_POINTER;
info = conf_info_iterator(1);
while (info) {
/* XXX: Possibly move this exception to each plugin */
const char *except[] = {
};
const char *name = info->system->name;
size_t i, j;
int nonslookup = 0;
/* Exceptions -- no name to lookup */
for (i = 0; i < NELEMS(except); i++) {
if (!strncmp(name, except[i], strlen(name))) {
nonslookup = 1;
break;
}
}
// XXX: TODO better plugin identifiction here
for (j = 0; j < info->alias_count; j++)
read_one(&info->alias[j], name, nonslookup);
info = conf_info_iterator(0);
}
return 0;
}
/*
* Update cache with new IP
* /var/cache/inadyn/my.server.name.cache { LAST-IPADDR } MTIME
*/
int write_cache_file(ddns_alias_t *alias, const char *name)
{
FILE *fp;
char path[256];
cache_file(alias->name, name, path, sizeof(path));
fp = fopen(path, "w");
if (fp) {
if (strstr(name, "v6"))
logit(LOG_NOTICE, "Updating IPv6 cache for %s", alias->name);
else
logit(LOG_NOTICE, "Updating IPv4 cache for %s", alias->name);
fprintf(fp, "%s", alias->address);
fclose(fp);
return 0;
}
return 1;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/