-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_log_iphash.c
174 lines (135 loc) · 3.65 KB
/
mod_log_iphash.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
/*
* mod_log_iphash.c
* $Id$
*
* Copyright 2010 Franz Schwartau <franz at electromail.org>
*
* based on mod_logio by Bojan Smojver
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* This module adds new '%' directives to LogFormat:
*
* %Z: md5 hashed ip address looking like IPv6 address
*
*/
#include <stdlib.h>
#include "mod_log_config.h"
#include "http_config.h"
#include "http_log.h"
#include "util_md5.h"
#include "apr_strings.h"
#define SALT_SIZE 128
#define HASHED_BUF 64
module AP_MODULE_DECLARE_DATA log_iphash_module;
static const char log_iphash_filter_name[] = "LOG_IP_HASH";
typedef struct iphash_config_t {
char salt[SALT_SIZE + 1];
} iphash_config_t;
/* seed_rand() copied from support/htpasswd.c */
static apr_status_t
seed_rand(void)
{
int seed = 0;
apr_status_t rv;
rv = apr_generate_random_bytes((unsigned char *)&seed, sizeof(seed));
if (rv) {
return rv;
}
srand(seed);
return rv;
}
/* generate_salt() copied from support/htpasswd.c */
static void
generate_salt(char *s, size_t size)
{
static unsigned char tbl[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
size_t i;
for (i = 0; i < size; ++i) {
int idx = (int)(64.0 * rand() / (RAND_MAX + 1.0));
s[i] = tbl[idx];
}
}
static void *
iphash_create_server_config(apr_pool_t *p, server_rec *s)
{
apr_status_t rv;
iphash_config_t *cf = apr_pcalloc(p, sizeof(iphash_config_t));
memset(cf->salt, 0, sizeof(cf->salt));
if ((rv = seed_rand())) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_log_iphash: Unable to generate random bytes: %pm. Exiting.", &rv);
exit(1);
}
generate_salt(cf->salt, SALT_SIZE);
return cf;
}
/*
* Format items...
*/
static const char *
log_ip_hash(request_rec *r, char *a)
{
char *hashed_ip;
char *salted_ip;
char *hashed_ipv6;
char *dest;
apr_size_t i = 0;
apr_size_t n = HASHED_BUF - 1;
hashed_ipv6 = apr_pcalloc(r->pool, HASHED_BUF);
iphash_config_t *cf = ap_get_module_config(r->server->module_config, &log_iphash_module);
salted_ip = apr_pstrcat(r->pool, cf->salt, r->connection->remote_ip, NULL);
hashed_ip = ap_md5(r->pool, (unsigned char *)salted_ip);
dest = hashed_ipv6;
do {
if ((*dest++ = *hashed_ip++) == 0) {
break;
}
if ((++i % 4 == 0) && *hashed_ip) {
*dest++ = ':';
if (--n == 0) {
break;
}
}
} while (--n != 0);
return hashed_ipv6;
}
/*
* The hooks...
*/
static int
log_iphash_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
{
static APR_OPTIONAL_FN_TYPE(ap_register_log_handler) * log_pfn_register;
log_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_log_handler);
if (log_pfn_register) {
log_pfn_register(p, "Z", log_ip_hash, 0);
}
return OK;
}
static void
register_hooks(apr_pool_t *p)
{
ap_hook_pre_config(log_iphash_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
}
module AP_MODULE_DECLARE_DATA log_iphash_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config */
NULL, /* merge per-dir config */
iphash_create_server_config, /* server config */
NULL, /* merge server config */
NULL, /* command apr_table_t */
register_hooks /* register hooks */
};