]>
Commit | Line | Data |
---|---|---|
f4fef543 MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2019 IPFire Development Team <info@ipfire.org> | |
5 | ||
6 | This library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | This library 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 GNU | |
14 | Lesser General Public License for more details. | |
15 | */ | |
16 | ||
17 | #include <resolv.h> | |
18 | #include <string.h> | |
19 | #include <time.h> | |
20 | ||
21 | #include <loc/format.h> | |
22 | #include <loc/private.h> | |
23 | #include <loc/resolv.h> | |
24 | ||
25 | static int parse_timestamp(const unsigned char* txt, time_t* t) { | |
26 | struct tm ts; | |
27 | ||
28 | // Parse timestamp | |
29 | char* p = strptime((const char*)txt, "%a, %d %b %Y %H:%M:%S GMT", &ts); | |
30 | ||
31 | // If the whole string has been parsed, we convert the parse value to time_t | |
32 | if (p && !*p) { | |
33 | *t = mktime(&ts); | |
34 | ||
35 | // Otherwise we reset t | |
36 | } else { | |
37 | *t = 0; | |
38 | return -1; | |
39 | } | |
40 | ||
41 | return 0; | |
42 | } | |
43 | ||
44 | LOC_EXPORT int loc_discover_latest_version(struct loc_ctx* ctx, const char* domain, time_t* t) { | |
45 | // Initialise the resolver | |
46 | int r = res_init(); | |
47 | if (r) { | |
48 | ERROR(ctx, "res_init() failed\n"); | |
49 | return r; | |
50 | } | |
51 | ||
52 | // Fall back to default domain | |
53 | if (!domain) | |
54 | domain = LOC_DATABASE_DOMAIN_LATEST(LOC_DATABASE_VERSION); | |
55 | ||
56 | unsigned char answer[PACKETSZ]; | |
57 | int len; | |
58 | ||
59 | DEBUG(ctx, "Querying %s\n", domain); | |
60 | ||
61 | // Send a query | |
62 | if ((len = res_query(domain, C_IN, T_TXT, answer, sizeof(answer))) < 0 || len > PACKETSZ) { | |
63 | ERROR(ctx, "Could not query %s: \n", domain); | |
64 | ||
65 | return -1; | |
66 | } | |
67 | ||
68 | unsigned char* end = answer + len; | |
69 | unsigned char* payload = answer + sizeof(HEADER); | |
70 | ||
71 | // Expand domain name | |
72 | char host[128]; | |
73 | if ((len = dn_expand(answer, end, payload, host, sizeof(host))) < 0) { | |
74 | ERROR(ctx, "dn_expand() failed\n"); | |
75 | return -1; | |
76 | } | |
77 | ||
78 | // Payload starts after hostname | |
79 | payload += len; | |
80 | ||
81 | if (payload > end - 4) { | |
82 | ERROR(ctx, "DNS reply too short\n"); | |
83 | return -1; | |
84 | } | |
85 | ||
86 | int type; | |
87 | GETSHORT(type, payload); | |
88 | if (type != T_TXT) { | |
89 | ERROR(ctx, "DNS reply of unexpected type: %d\n", type); | |
90 | return -1; | |
91 | } | |
92 | ||
93 | // Skip class | |
94 | payload += INT16SZ; | |
95 | ||
96 | // Walk through CNAMEs | |
97 | unsigned int size = 0; | |
98 | int ttl; | |
99 | do { | |
100 | payload += size; | |
101 | ||
102 | if ((len = dn_expand(answer, end, payload, host, sizeof(host))) < 0) { | |
103 | ERROR(ctx, "dn_expand() failed\n"); | |
104 | return -1; | |
105 | } | |
106 | ||
107 | payload += len; | |
108 | ||
109 | if (payload > end - 10) { | |
110 | ERROR(ctx, "DNS reply too short\n"); | |
111 | return -1; | |
112 | } | |
113 | ||
114 | // Skip type, class, ttl | |
115 | GETSHORT(type, payload); | |
116 | payload += INT16SZ; | |
117 | GETLONG(ttl, payload); | |
118 | ||
119 | // Read size | |
120 | GETSHORT(size, payload); | |
121 | if (payload + size < answer || payload + size > end) { | |
122 | ERROR(ctx, "DNS RR overflow\n"); | |
123 | return -1; | |
124 | } | |
125 | } while (type == T_CNAME); | |
126 | ||
127 | if (type != T_TXT) { | |
128 | ERROR(ctx, "Not a TXT record\n"); | |
129 | return -1; | |
130 | } | |
131 | ||
132 | if (!size || (len = *payload) >= size || !len) { | |
133 | ERROR(ctx, "Broken TXT record (len = %d, size = %d)\n", len, size); | |
134 | return -1; | |
135 | } | |
136 | ||
137 | // Get start of the string | |
138 | unsigned char* txt = payload + 1; | |
139 | txt[len] = '\0'; | |
140 | ||
141 | DEBUG(ctx, "Resolved to: %s\n", txt); | |
142 | ||
143 | // Parse timestamp | |
144 | r = parse_timestamp(txt, t); | |
145 | ||
146 | return r; | |
147 | } |