| 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 |
#include "conf.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#define MOD_UNIQUE_ID_VERSION "mod_unique_id/0.2" |
| 36 |
|
| 37 |
|
| 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 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 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 |
|
| 84 |
pr_event_unregister(&unique_id_module, NULL, NULL); |
| 85 |
} |
| 86 |
} |
| 87 |
#endif |
| 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 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
memcpy(&host_ipaddr, addr_data, sizeof(host_ipaddr)); |
| 122 |
} |
| 123 |
|
| 124 |
destroy_pool(tmp_pool); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 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 |
| 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 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 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 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
memcpy(&client_ipaddr, addr_data, sizeof(client_ipaddr)); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 291 |
0x20, |
| 292 |
|
| 293 |
|
| 294 |
"unique_id", |
| 295 |
|
| 296 |
|
| 297 |
uniqid_conftab, |
| 298 |
|
| 299 |
|
| 300 |
NULL, |
| 301 |
|
| 302 |
|
| 303 |
NULL, |
| 304 |
|
| 305 |
|
| 306 |
uniqid_init, |
| 307 |
|
| 308 |
|
| 309 |
uniqid_sess_init, |
| 310 |
|
| 311 |
|
| 312 |
MOD_UNIQUE_ID_VERSION |
| 313 |
}; |