aboutsummaryrefslogtreecommitdiff
path: root/src/vmod_geoip.c
blob: de26eea77f0ce7a9337476d82a911d8bf1988e69 (plain)
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
#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>

#if VARNISHAPI_MAJOR > 5
# include <cache/cache.h>
# include <vcl.h>
# include <vcc_if.h>
#else
# include <vcl.h>
# include <vrt.h>
# include <vcc_if.h>
# include <cache/cache.h>
#endif

#ifdef VPFX
# define VEVENT(a) VPFX(a)
#else
/* For compatibility with varnish prior to 6.2 */
# define VEVENT(a) a
#endif

#include <vtcp.h>
#include <vsa.h>

#include <maxminddb.h>
#include <assert.h>

#define DEFAULT_DIR  "/usr/share/GeoIP"
#define DEFAULT_FILE "GeoLite2-City.mmdb"
#define DEFAULT_DATABASE DEFAULT_DIR "/" DEFAULT_FILE

struct geoip_config {
	char *database;
};

static void
free_conf(void *data)
{
	struct geoip_config *cfg = data;
	free(data);
}

int
VEVENT(geoip_event)(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
{
        if (e == VCL_EVENT_LOAD) {
                struct geoip_config *conf = calloc(1, sizeof(*conf));
                AN(conf);
                priv->priv = conf;
		priv->free = free_conf;
        }
        return 0;
}

void
vmod_init(VRT_CTX, struct vmod_priv *priv, const char *dir)
{
	struct geoip_config *conf = priv->priv;
	struct stat st;
	char *name;
	
	if (stat(dir, &st)) {
		VSLb(ctx->vsl, SLT_Error,
		     "geoip.init: can't stat \"%s\": %s",
		     dir, strerror(errno));
		abort();
	}

	if (S_ISDIR(st.st_mode)) {
		size_t dlen = strlen (dir);
		size_t len;
		
		if (dir[dlen-1] == '/')
			dlen--;
		name = malloc (dlen + 1 + sizeof (DEFAULT_FILE));
		AN(name);
		memcpy (name, dir, dlen);
		name[dlen++] = '/';
		strcpy (name + dlen, DEFAULT_FILE);
	} else if (S_ISREG (st.st_mode)) {
		name = strdup (dir);
		AN(name);
	} else {
		VSLb(ctx->vsl, SLT_Error,
		      "geoip.init: \"%s\": bad file type", dir);
		abort ();
	}
		
	free(conf->database);
	conf->database = name;
}

static int
open_geoip_database(VRT_CTX, struct geoip_config *conf, MMDB_s *dbptr)
{
	char *database = conf->database ? conf->database : DEFAULT_DATABASE;
	int rc;
	
	rc = MMDB_open(database, MMDB_MODE_MMAP, dbptr);
	if (rc != MMDB_SUCCESS) {
		VSLb(ctx->vsl, SLT_Error,
		      "can't open database \"%s\": %s",
		      database, MMDB_strerror(rc));
		return -1;
	}
	return 0;
}

static char *
conv_utf_string (struct ws *ws, MMDB_entry_data_s *dptr)
{
	char *retval = WS_Alloc(ws, dptr->data_size + 1);
	AN(retval);
	memcpy(retval, dptr->utf8_string, dptr->data_size);
	retval[dptr->data_size] = 0;
	return retval;
}

static char *
conv_uint16(struct ws *ws, MMDB_entry_data_s *dptr)
{
	return WS_Printf(ws, "%" PRIu16, dptr->uint16);
}

static char *
conv_uint32(struct ws *ws, MMDB_entry_data_s *dptr)
{
	return WS_Printf(ws, "%" PRIu32, dptr->uint32);
}

static char *
conv_int32(struct ws *ws, MMDB_entry_data_s *dptr)
{
	return WS_Printf(ws, "%" PRIi32, dptr->int32);
}

static char *
conv_bool(struct ws *ws, MMDB_entry_data_s *dptr)
{
	return WS_Printf(ws, "%01d", dptr->boolean ? 1 : 0);
}

static char *
conv_double(struct ws *ws, MMDB_entry_data_s *dptr)
{
	return WS_Printf(ws, "%g", dptr->double_value);
}

static char *
conv_float(struct ws *ws, MMDB_entry_data_s *dptr)
{
	return WS_Printf(ws, "%g", dptr->float_value);
}

static char *(*entry_conv[]) (struct ws *, MMDB_entry_data_s *) = {
	[MMDB_DATA_TYPE_UTF8_STRING] = conv_utf_string,
	[MMDB_DATA_TYPE_UINT16]      = conv_uint16,
	[MMDB_DATA_TYPE_UINT32]      = conv_uint32,
	[MMDB_DATA_TYPE_INT32]       = conv_int32,
	[MMDB_DATA_TYPE_BOOLEAN]     = conv_bool,
	[MMDB_DATA_TYPE_DOUBLE]      = conv_double,
	[MMDB_DATA_TYPE_FLOAT]       = conv_float
};

static char *
lookup_geoip_database(VRT_CTX,
		      MMDB_s *dbfile, VCL_IP ip,
		      char const *pathstr, char **path)
{
	MMDB_lookup_result_s result;
	int mmdb_error;
	int rc;
	MMDB_entry_data_s entry_data;
	char *retval;
	const struct sockaddr *sa;
	socklen_t sl;
	char ipstr[VTCP_ADDRBUFSIZE];
	
	if (!(sa = VSA_Get_Sockaddr(ip, &sl)))
		return NULL;

	result = MMDB_lookup_sockaddr(dbfile, sa, &mmdb_error);
	if (mmdb_error != MMDB_SUCCESS) {
		VTCP_name(ip, ipstr, sizeof ipstr, NULL, 0);
		VSLb(ctx->vsl, SLT_Error,
		     "%s: %s",
		     pathstr, ipstr, MMDB_strerror(mmdb_error));
		return NULL;
	}

	if (!result.found_entry)
		return NULL;
	
	rc = MMDB_aget_value(&result.entry, &entry_data,
			     (const char * const* const) path);
	if (rc != MMDB_SUCCESS) {
		VTCP_name(ip, ipstr, sizeof ipstr, NULL, 0);
		VSLb(ctx->vsl, SLT_Error,
		     "%s %s: MMDB_aget_value %s",
		     pathstr, ipstr, MMDB_strerror(rc));
		return NULL;
	}

	if (!entry_data.has_data)
		return NULL;

	if (entry_data.type >= 0
	    && entry_data.type <= sizeof (entry_conv) / sizeof (entry_conv[0])
	    && entry_conv[entry_data.type]) {
		retval = entry_conv[entry_data.type] (ctx->ws, &entry_data);
	} else {
		VTCP_name(ip, ipstr, sizeof ipstr, NULL, 0);
		retval = WS_Printf(ctx->ws, "[%s: can't format %s of type %d]",
				   ipstr, pathstr, entry_data.type);
	}
	return retval;
}

static char **
split(char const *path)
{
	int c = 0;
	int i, j;
	char *base;
	char **retv;
	char *str;

	c = 1;
	for (i = 0; path[i]; i++)
		if (path[i] == '.')
			c++;
	base = malloc(i + 1 + (c + 1) * sizeof(char*));
	AN(base);
	retv = (char**) base;
	str  = (char*) &retv[c+1];
	strcpy(str, path);

	i = 0;
	j = 0;
	retv[j++] = str;
	while (str[i]) {
		if (str[i] == '.') {
			str[i] = 0;
			retv[j++] = str + i + 1;
		}
		i++;
	}
	retv[j] = NULL;
	return retv;
}

VCL_STRING
vmod_get(VRT_CTX, struct vmod_priv *priv, VCL_IP ip, VCL_STRING path)
{
	struct geoip_config *conf = priv->priv;
	MMDB_s dbfile;
	char *retval = NULL;
	char **pathv;

	if (open_geoip_database(ctx, conf, &dbfile))
		return NULL;
	pathv = split(path);
	retval = lookup_geoip_database(ctx, &dbfile, ip, path, pathv);
	free(pathv);
	MMDB_close(&dbfile);
	return retval;
}

VCL_STRING
vmod_country_code(VRT_CTX, struct vmod_priv *priv, VCL_IP ip)
{
	struct geoip_config *conf = priv->priv;
	MMDB_s dbfile;
	char *retval = NULL;
	static char *country_code_path[] = {
		"country",
		"iso_code",
		NULL
	};
	
	if (open_geoip_database(ctx, conf, &dbfile))
		return NULL;
	retval = lookup_geoip_database(ctx, &dbfile, ip,
				       "country.iso_code", country_code_path);
	MMDB_close(&dbfile);
	return retval;
}

VCL_STRING
vmod_country_name(VRT_CTX, struct vmod_priv *priv, VCL_IP ip)
{
	struct geoip_config *conf = priv->priv;
	MMDB_s dbfile;
	char *retval = NULL;
	static char *country_name_path[] = {
		"country",
		"names",
		"en",
		NULL
	};

	if (open_geoip_database(ctx, conf, &dbfile))
		return NULL;
	retval = lookup_geoip_database(ctx, &dbfile, ip,
				       "country.names.en",
				       country_name_path);
	MMDB_close(&dbfile);
	return retval;
}

VCL_STRING
vmod_city_name(VRT_CTX, struct vmod_priv *priv, VCL_IP ip)
{
	struct geoip_config *conf = priv->priv;
	MMDB_s dbfile;
	char *retval = NULL;
	static char *city_name_path[] = {
		"city",
		"names",
		"en",
		NULL
	};
	
	if (open_geoip_database(ctx, conf, &dbfile))
		return NULL;
	retval = lookup_geoip_database(ctx, &dbfile, ip,
				       "city.names.en",
				       city_name_path);
	MMDB_close(&dbfile);
	return retval;
}

Return to:

Send suggestions and report system problems to the System administrator.