| 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 |
#include "conf.h" |
| 26 |
|
| 27 |
#define MOD_IDENT_VERSION "mod_ident/1.0" |
| 28 |
|
| 29 |
#if PROFTPD_VERSION_NUMBER < 0x0001030401 |
| 30 |
# error "ProFTPD 1.3.4rc1 or later required" |
| 31 |
#endif |
| 32 |
|
| 33 |
module ident_module; |
| 34 |
|
| 35 |
static int ident_engine = FALSE; |
| 36 |
static pr_netio_stream_t *ident_nstrm = NULL; |
| 37 |
static int ident_timeout_triggered = FALSE; |
| 38 |
|
| 39 |
static const char *trace_channel = "ident"; |
| 40 |
|
| 41 |
|
| 42 |
static int ident_sess_init(void); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static int ident_timeout_cb(CALLBACK_FRAME) { |
| 48 |
ident_timeout_triggered = TRUE; |
| 49 |
|
| 50 |
if (ident_nstrm) { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pr_netio_abort(ident_nstrm); |
| 56 |
} |
| 57 |
|
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static char *ident_lookup(pool *p, conn_t *conn) { |
| 62 |
conn_t *ident_conn = NULL, *ident_io = NULL; |
| 63 |
char buf[256], *ident = NULL; |
| 64 |
int ident_port, timerno, res = 0; |
| 65 |
const pr_netaddr_t *bind_addr; |
| 66 |
|
| 67 |
ident_nstrm = NULL; |
| 68 |
ident_timeout_triggered = FALSE; |
| 69 |
|
| 70 |
ident_port = pr_inet_getservport(p, "ident", "tcp"); |
| 71 |
if (ident_port == -1) { |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
timerno = pr_timer_add(PR_TUNABLE_TIMEOUTIDENT, -1, &ident_module, |
| 76 |
ident_timeout_cb, "ident (RFC1413) lookup"); |
| 77 |
if (timerno <= 0) { |
| 78 |
pr_trace_msg(trace_channel, 8, "error adding timer: %s", strerror(errno)); |
| 79 |
return NULL; |
| 80 |
} |
| 81 |
|
| 82 |
if (pr_netaddr_get_family(conn->local_addr) == pr_netaddr_get_family(conn->remote_addr)) { |
| 83 |
bind_addr = conn->local_addr; |
| 84 |
|
| 85 |
} else { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
bind_addr = pr_netaddr_v6tov4(p, session.c->local_addr); |
| 90 |
} |
| 91 |
|
| 92 |
ident_conn = pr_inet_create_conn(p, -1, bind_addr, INPORT_ANY, FALSE); |
| 93 |
if (ident_conn == NULL) { |
| 94 |
pr_trace_msg(trace_channel, 3, "error creating connection: %s", |
| 95 |
strerror(errno)); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
res = pr_inet_connect_nowait(p, ident_conn, conn->remote_addr, ident_port); |
| 104 |
if (res < 0) { |
| 105 |
int xerrno = errno; |
| 106 |
|
| 107 |
pr_timer_remove(timerno, &ident_module); |
| 108 |
pr_inet_close(p, ident_conn); |
| 109 |
|
| 110 |
pr_trace_msg(trace_channel, 5, "connection to %s, port %d failed: %s", |
| 111 |
pr_netaddr_get_ipstr(conn->remote_addr), ident_port, strerror(xerrno)); |
| 112 |
|
| 113 |
errno = xerrno; |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
|
| 117 |
if (res == 0) { |
| 118 |
|
| 119 |
ident_nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, |
| 120 |
ident_conn->listen_fd, PR_NETIO_IO_RD); |
| 121 |
if (ident_nstrm == NULL) { |
| 122 |
int xerrno = errno; |
| 123 |
|
| 124 |
pr_timer_remove(timerno, &ident_module); |
| 125 |
pr_inet_close(p, ident_conn); |
| 126 |
|
| 127 |
pr_trace_msg(trace_channel, 5, "error opening NetIO stream: %s", |
| 128 |
strerror(xerrno)); |
| 129 |
|
| 130 |
errno = xerrno; |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
|
| 134 |
pr_netio_set_poll_interval(ident_nstrm, 1); |
| 135 |
|
| 136 |
switch (pr_netio_poll(ident_nstrm)) { |
| 137 |
case 1: { |
| 138 |
|
| 139 |
if (ident_timeout_triggered) { |
| 140 |
pr_netio_close(ident_nstrm); |
| 141 |
ident_nstrm = NULL; |
| 142 |
|
| 143 |
pr_inet_close(p, ident_conn); |
| 144 |
pr_timer_remove(timerno, &ident_module); |
| 145 |
|
| 146 |
pr_trace_msg(trace_channel, 5, "ident lookup timed out after %u secs", |
| 147 |
PR_TUNABLE_TIMEOUTIDENT); |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
|
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
case -1: { |
| 155 |
|
| 156 |
int xerrno = errno; |
| 157 |
|
| 158 |
pr_netio_close(ident_nstrm); |
| 159 |
ident_nstrm = NULL; |
| 160 |
|
| 161 |
pr_inet_close(p, ident_conn); |
| 162 |
pr_timer_remove(timerno, &ident_module); |
| 163 |
|
| 164 |
pr_trace_msg(trace_channel, 6, "ident lookup failed: %s", |
| 165 |
strerror(xerrno)); |
| 166 |
|
| 167 |
errno = xerrno; |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
default: { |
| 172 |
|
| 173 |
ident_conn->mode = CM_OPEN; |
| 174 |
|
| 175 |
if (pr_inet_get_conn_info(ident_conn, ident_conn->listen_fd) < 0) { |
| 176 |
int xerrno = errno; |
| 177 |
|
| 178 |
pr_netio_close(ident_nstrm); |
| 179 |
ident_nstrm = NULL; |
| 180 |
|
| 181 |
pr_inet_close(p, ident_conn); |
| 182 |
pr_timer_remove(timerno, &ident_module); |
| 183 |
|
| 184 |
pr_trace_msg(trace_channel, 3, |
| 185 |
"error retrieving ident peer details: %s", strerror(xerrno)); |
| 186 |
|
| 187 |
errno = xerrno; |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
|
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
ident_io = pr_inet_openrw(p, ident_conn, NULL, PR_NETIO_STRM_OTHR, |
| 197 |
-1, -1, -1, FALSE); |
| 198 |
if (ident_io == NULL) { |
| 199 |
int xerrno = errno; |
| 200 |
|
| 201 |
pr_netio_close(ident_nstrm); |
| 202 |
ident_nstrm = NULL; |
| 203 |
|
| 204 |
pr_inet_close(p, ident_conn); |
| 205 |
pr_timer_remove(timerno, &ident_module); |
| 206 |
|
| 207 |
pr_trace_msg(trace_channel, 3, "failed opening read/write connection: %s", |
| 208 |
strerror(xerrno)); |
| 209 |
|
| 210 |
errno = xerrno; |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
|
| 214 |
ident_nstrm = ident_io->instrm; |
| 215 |
|
| 216 |
pr_inet_set_nonblock(p, ident_io); |
| 217 |
pr_netio_set_poll_interval(ident_io->instrm, 1); |
| 218 |
pr_netio_set_poll_interval(ident_io->outstrm, 1); |
| 219 |
|
| 220 |
res = pr_netio_printf(ident_io->outstrm, "%d, %d\r\n", conn->remote_port, |
| 221 |
conn->local_port); |
| 222 |
if (res < 0) { |
| 223 |
pr_trace_msg(trace_channel, 1, "error writing command to ident server: %s", |
| 224 |
strerror(errno)); |
| 225 |
} |
| 226 |
|
| 227 |
pr_trace_msg(trace_channel, 4, "reading response from ident server at %s", |
| 228 |
pr_netaddr_get_ipstr(conn->remote_addr)); |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
if (pr_netio_gets(buf, sizeof(buf), ident_io->instrm)) { |
| 238 |
char *tok = NULL, *tmp = NULL; |
| 239 |
|
| 240 |
pr_str_strip_end(buf, "\r\n"); |
| 241 |
|
| 242 |
pr_trace_msg(trace_channel, 6, "received '%s' from ident server", buf); |
| 243 |
|
| 244 |
tmp = buf; |
| 245 |
tok = pr_str_get_token(&tmp, ":"); |
| 246 |
if (tok && |
| 247 |
(tok = pr_str_get_token(&tmp, ":"))) { |
| 248 |
while (*tok && PR_ISSPACE(*tok)) { |
| 249 |
pr_signals_handle(); |
| 250 |
tok++; |
| 251 |
} |
| 252 |
|
| 253 |
pr_str_strip_end(tok, " \t"); |
| 254 |
|
| 255 |
if (strcasecmp(tok, "ERROR") == 0) { |
| 256 |
if (tmp) { |
| 257 |
while (*tmp && PR_ISSPACE(*tmp)) { |
| 258 |
pr_signals_handle(); |
| 259 |
tmp++; |
| 260 |
} |
| 261 |
|
| 262 |
pr_str_strip_end(tmp, " \t"); |
| 263 |
|
| 264 |
if (strcasecmp(tmp, "HIDDEN-USER") == 0) |
| 265 |
ident = "HIDDEN-USER"; |
| 266 |
} |
| 267 |
|
| 268 |
} else if (strcasecmp(tok, "USERID") == 0) { |
| 269 |
if (tmp && |
| 270 |
(tok = pr_str_get_token(&tmp, ":"))) { |
| 271 |
if (tmp) { |
| 272 |
while (*tmp && PR_ISSPACE(*tmp)) { |
| 273 |
pr_signals_handle(); |
| 274 |
tmp++; |
| 275 |
} |
| 276 |
|
| 277 |
pr_str_strip_end(tmp, " \t"); |
| 278 |
ident = tmp; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
pr_inet_close(p, ident_io); |
| 286 |
pr_inet_close(p, ident_conn); |
| 287 |
pr_timer_remove(timerno, &ident_module); |
| 288 |
|
| 289 |
return pstrdup(p, ident); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
MODRET set_identlookups(cmd_rec *cmd) { |
| 297 |
int engine = -1; |
| 298 |
config_rec *c = NULL; |
| 299 |
|
| 300 |
CHECK_ARGS(cmd, 1); |
| 301 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 302 |
|
| 303 |
engine = get_boolean(cmd, 1); |
| 304 |
if (engine == -1) { |
| 305 |
CONF_ERROR(cmd, "expected Boolean parameter"); |
| 306 |
} |
| 307 |
|
| 308 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 309 |
c->argv[0] = pcalloc(c->pool, sizeof(int)); |
| 310 |
*((int *) c->argv[0]) = engine; |
| 311 |
|
| 312 |
return PR_HANDLED(cmd); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
static void ident_sess_reinit_ev(const void *event_data, void *user_data) { |
| 319 |
int res; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
pr_event_unregister(&ident_module, "core.session-reinit", |
| 324 |
ident_sess_reinit_ev); |
| 325 |
|
| 326 |
ident_engine = FALSE; |
| 327 |
|
| 328 |
res = ident_sess_init(); |
| 329 |
if (res < 0) { |
| 330 |
pr_session_disconnect(&ident_module, |
| 331 |
PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
static int ident_sess_init(void) { |
| 339 |
pool *tmp_pool = NULL; |
| 340 |
config_rec *c; |
| 341 |
char *ident = NULL; |
| 342 |
|
| 343 |
pr_event_register(&ident_module, "core.session-reinit", ident_sess_reinit_ev, |
| 344 |
NULL); |
| 345 |
|
| 346 |
c = find_config(main_server->conf, CONF_PARAM, "IdentLookups", FALSE); |
| 347 |
if (c != NULL) { |
| 348 |
ident_engine = *((int *) c->argv[0]); |
| 349 |
} |
| 350 |
|
| 351 |
if (ident_engine == FALSE) { |
| 352 |
pr_log_debug(DEBUG6, MOD_IDENT_VERSION ": ident lookup disabled"); |
| 353 |
return 0; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
if (pr_table_get(session.notes, "mod_ident.rfc1413-ident", NULL) != NULL) { |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
tmp_pool = make_sub_pool(session.pool); |
| 365 |
pr_pool_tag(tmp_pool, "IdentLookup pool"); |
| 366 |
|
| 367 |
|
| 368 |
pr_log_debug(DEBUG6, MOD_IDENT_VERSION ": performing ident lookup"); |
| 369 |
|
| 370 |
ident = ident_lookup(tmp_pool, session.c); |
| 371 |
if (ident) { |
| 372 |
pr_log_debug(DEBUG6, MOD_IDENT_VERSION ": ident lookup returned '%s'", |
| 373 |
ident); |
| 374 |
|
| 375 |
} else { |
| 376 |
ident = "UNKNOWN"; |
| 377 |
pr_log_debug(DEBUG6, MOD_IDENT_VERSION ": ident lookup failed, using '%s'", |
| 378 |
ident); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
if (pr_table_add_dup(session.notes, "mod_ident.rfc1413-ident", |
| 385 |
ident, 0) < 0) { |
| 386 |
pr_log_debug(DEBUG3, MOD_IDENT_VERSION |
| 387 |
": error stashing 'mod_ident.rfc1413-ident' value '%s': %s", ident, |
| 388 |
strerror(errno)); |
| 389 |
} |
| 390 |
|
| 391 |
destroy_pool(tmp_pool); |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static conftable ident_conftab[] = { |
| 399 |
{ "IdentLookups", set_identlookups, NULL }, |
| 400 |
{ NULL } |
| 401 |
}; |
| 402 |
|
| 403 |
module ident_module = { |
| 404 |
NULL, NULL, |
| 405 |
|
| 406 |
|
| 407 |
0x20, |
| 408 |
|
| 409 |
|
| 410 |
"ident", |
| 411 |
|
| 412 |
|
| 413 |
ident_conftab, |
| 414 |
|
| 415 |
|
| 416 |
NULL, |
| 417 |
|
| 418 |
|
| 419 |
NULL, |
| 420 |
|
| 421 |
|
| 422 |
NULL, |
| 423 |
|
| 424 |
|
| 425 |
ident_sess_init, |
| 426 |
|
| 427 |
|
| 428 |
MOD_IDENT_VERSION |
| 429 |
}; |