Debian Codegraph @

contrib/mod_unique_id.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_unique_id -- a module for generating a unique ID for each
3 * FTP session.
4 * Copyright (c) 2006-2024 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_unique_id, contrib software for proftpd 1.2.x/1.3.x and above.
26 * For more information contact TJ Saunders <[email protected]>.
27 */
28
29 #include "conf.h"
30
31 /* A lot of ideas for this module were liberally borrowed from the mod_uniq_id
32 * module for Apache.
33 */
34
35 #define MOD_UNIQUE_ID_VERSION "mod_unique_id/0.2"
36
37 /* Make sure the version of proftpd is as necessary. */
38 #if PROFTPD_VERSION_NUMBER < 0x0001030402
39 # error "ProFTPD 1.3.4rc2 or later required"
40 #endif
41
42 module unique_id_module;
43
44 static const char base64[64] = {
45 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
46 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
47 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
48 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
49 '8', '9', '+', '/'
50 };
51
52 static unsigned int host_ipaddr = 0;
53
54 /* Configuration handlers
55 */
56
57 /* usage: UniqueIDEngine on|off */
58 MODRET set_uniqueidengine(cmd_rec *cmd) {
59 int engine = -1;
60 config_rec *c = NULL;
61
62 CHECK_ARGS(cmd, 1);
63 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
64
65 engine = get_boolean(cmd, 1);
66 if (engine == -1) {
67 CONF_ERROR(cmd, "expected Boolean parameter");
68 }
69
70 c = add_config_param(cmd->argv[0], 1, NULL);
71 c->argv[0] = pcalloc(c->pool, sizeof(int));
72 *((int *) c->argv[0]) = engine;
73
74 return PR_HANDLED(cmd);
75 }
76
77 /* Event handlers
78 */
79
80 #if defined(PR_SHARED_MODULE)
81 static void uniqid_mod_unload_ev(const void *event_data, void *user_data) {
82 if (strcmp("mod_unique_id.c", (const char *) event_data) == 0) {
83 /* Unregister ourselves from all events. */
84 pr_event_unregister(&unique_id_module, NULL, NULL);
85 }
86 }
87 #endif /* PR_SHARED_MODULE */
88
89 static void uniqid_postparse_ev(const void *event_data, void *user_data) {
90 pool *tmp_pool;
91 const char *host_name = NULL;
92 const pr_netaddr_t *host_addr = NULL;
93 void *addr_data = NULL;
94
95 tmp_pool = make_sub_pool(main_server->pool);
96
97 host_name = pr_netaddr_get_localaddr_str(tmp_pool);
98 if (host_name == NULL) {
99 pr_log_pri(PR_LOG_WARNING, MOD_UNIQUE_ID_VERSION
100 ": unable to determine hostname");
101 destroy_pool(tmp_pool);
102 pr_session_disconnect(&unique_id_module, PR_SESS_DISCONNECT_BY_APPLICATION,
103 NULL);
104 }
105
106 host_addr = pr_netaddr_get_addr(tmp_pool, host_name, NULL);
107 if (host_addr == NULL) {
108 pr_log_pri(PR_LOG_WARNING, MOD_UNIQUE_ID_VERSION
109 ": unable to resolve '%s' to an IP address", host_name);
110 destroy_pool(tmp_pool);
111 pr_session_disconnect(&unique_id_module, PR_SESS_DISCONNECT_BY_APPLICATION,
112 NULL);
113 }
114
115 addr_data = pr_netaddr_get_inaddr(host_addr);
116 if (addr_data != NULL) {
117 /* Copy bits from addr_data up to the size of an unsigned int (32 bits,
118 * ideally). In the case of an IPv6 address, this will only use the
119 * low-order bits of the entire address. C'est la vie.
120 */
121 memcpy(&host_ipaddr, addr_data, sizeof(host_ipaddr));
122 }
123
124 destroy_pool(tmp_pool);
125 }
126
127 /* Initialization functions
128 */
129
130 static int uniqid_init(void) {
131
132 #if defined(PR_SHARED_MODULE)
133 pr_event_register(&unique_id_module, "core.module-unload",
134 uniqid_mod_unload_ev, NULL);
135 #endif /* PR_SHARED_MODULE */
136 pr_event_register(&unique_id_module, "core.postparse", uniqid_postparse_ev,
137 NULL);
138
139 return 0;
140 }
141
142 static int uniqid_sess_init(void) {
143 config_rec *c;
144 int uniqid_engine = TRUE;
145 unsigned int client_ipaddr = 0;
146 void *addr_data = NULL;
147 unsigned int pid;
148 unsigned int now;
149 unsigned short counter;
150 struct timeval tv;
151 struct timezone tz;
152 char *key = "UNIQUE_ID", *id = NULL;
153 unsigned char *x, *y;
154 register unsigned int i, j;
155
156 /* Four pieces of data as unsigned ints:
157 *
158 * host IP address
159 * client IP address
160 * PID
161 * timestamp (in seconds)
162 *
163 * and one piece of unsigned short data:
164 *
165 * counter (derived from number of microsecond)
166 */
167 unsigned short id_sz = (sizeof(unsigned int) * 4) + (sizeof(unsigned short));
168 unsigned short id_encoded_sz = (id_sz * 8 + 5) / 6;
169 unsigned char buf[id_sz];
170
171 c = find_config(main_server->conf, CONF_PARAM, "UniqueIDEngine", FALSE);
172 if (c != NULL) {
173 uniqid_engine = *((int *) c->argv[0]);
174 }
175
176 if (uniqid_engine == FALSE) {
177 return 0;
178 }
179
180 pr_log_debug(DEBUG8, MOD_UNIQUE_ID_VERSION ": generating unique session ID");
181
182 if (gettimeofday(&tv, &tz) < 0) {
183 pr_log_debug(DEBUG1, MOD_UNIQUE_ID_VERSION
184 ": error getting time of day: %s", strerror(errno));
185 now = 0;
186 counter = 0;
187
188 } else {
189 now = htonl((unsigned int) tv.tv_sec);
190 counter = htons((unsigned short) (tv.tv_usec / 10));
191 }
192
193 pid = htonl((unsigned int) getpid());
194
195 addr_data = pr_netaddr_get_inaddr(session.c->remote_addr);
196 if (addr_data != NULL) {
197 /* Copy bits from addr_data up to the size of an unsigned int (32 bits,
198 * ideally). In the case of an IPv6 address, this will only use the
199 * low-order bits of the entire address. C'est la vie.
200 */
201 memcpy(&client_ipaddr, addr_data, sizeof(client_ipaddr));
202 }
203
204 /* Populate buf with the binary pieces of data we've collected. */
205 memset(buf, '\0', sizeof(buf));
206 x = buf;
207
208 j = 0;
209 y = (unsigned char *) &now;
210 for (i = 0; i < sizeof(unsigned int); i++, j++) {
211 x[j] = y[i];
212 }
213
214 y = (unsigned char *) &host_ipaddr;
215 for (i = 0; i < sizeof(unsigned int); i++, j++) {
216 x[j] = y[i];
217 }
218
219 y = (unsigned char *) &client_ipaddr;
220 for (i = 0; i < sizeof(unsigned int); i++, j++) {
221 x[j] = y[i];
222 }
223
224 y = (unsigned char *) &pid;
225 for (i = 0; i < sizeof(unsigned int); i++, j++) {
226 x[j] = y[i];
227 }
228
229 y = (unsigned char *) &counter;
230 for (i = 0; i < sizeof(unsigned short); i++, j++) {
231 x[j] = y[i];
232 }
233
234 /* Add one to the encoded size, for the trailing NUL. */
235 id = pcalloc(session.pool, id_encoded_sz + 1);
236
237 j = 0;
238 for (i = 0; i < id_sz; i += 3) {
239 y = x + i;
240
241 id[j++] = base64[y[0] >> 2];
242 id[j++] = base64[((y[0] & 0x03) << 4) | ((y[1] & 0xf0) >> 4)];
243
244 if (j == id_encoded_sz) {
245 break;
246 }
247
248 id[j++] = base64[((y[1] & 0x0f) << 2) | ((y[2] & 0xc0) >> 6)];
249
250 if (j == id_encoded_sz) {
251 break;
252 }
253
254 id[j++] = base64[y[2] & 0x3f];
255 }
256
257 if (j >= id_encoded_sz) {
258 j = id_encoded_sz;
259 }
260 id[j] = '\0';
261
262 if (pr_env_set(session.pool, key, id) < 0) {
263 pr_log_debug(DEBUG0, MOD_UNIQUE_ID_VERSION
264 ": error setting UNIQUE_ID environment variable: %s", strerror(errno));
265
266 } else {
267 pr_log_debug(DEBUG8, MOD_UNIQUE_ID_VERSION
268 ": unique session ID is '%s'", id);
269 }
270
271 if (pr_table_add_dup(session.notes, pstrdup(session.pool, key), id, 0) < 0) {
272 pr_log_debug(DEBUG0, MOD_UNIQUE_ID_VERSION
273 ": error adding %s session note: %s", key, strerror(errno));
274 }
275
276 return 0;
277 }
278
279 /* Module API tables
280 */
281
282 static conftable uniqid_conftab[] = {
283 { "UniqueIDEngine", set_uniqueidengine, NULL },
284 { NULL }
285 };
286
287 module unique_id_module = {
288 NULL, NULL,
289
290 /* Module API version 2.0 */
291 0x20,
292
293 /* Module name */
294 "unique_id",
295
296 /* Module configuration handler table */
297 uniqid_conftab,
298
299 /* Module command handler table */
300 NULL,
301
302 /* Module authentication handler table */
303 NULL,
304
305 /* Module initialization function */
306 uniqid_init,
307
308 /* Session initialization function */
309 uniqid_sess_init,
310
311 /* Module version */
312 MOD_UNIQUE_ID_VERSION
313 };