| 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 |
#include "conf.h" |
| 29 |
|
| 30 |
static int ident_timeout; |
| 31 |
static pr_netio_stream_t *nstrm = NULL; |
| 32 |
|
| 33 |
static const char *trace_channel = "ident"; |
| 34 |
|
| 35 |
static int ident_timeout_cb(CALLBACK_FRAME) { |
| 36 |
ident_timeout++; |
| 37 |
|
| 38 |
if (nstrm) { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
pr_netio_abort(nstrm); |
| 44 |
} |
| 45 |
|
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
char *pr_ident_lookup(pool *p, conn_t *c) { |
| 50 |
char *ret = "UNKNOWN"; |
| 51 |
pool *tmp_pool = NULL; |
| 52 |
conn_t *ident_conn = NULL, *ident_io = NULL; |
| 53 |
char buf[256] = {'\0'}, *tok = NULL, *tmp = NULL; |
| 54 |
int timerno, i = 0; |
| 55 |
int ident_port = pr_inet_getservport(p, "ident", "tcp"); |
| 56 |
|
| 57 |
tmp_pool = make_sub_pool(p); |
| 58 |
ident_timeout = 0; |
| 59 |
nstrm = NULL; |
| 60 |
|
| 61 |
if (ident_port == -1) { |
| 62 |
destroy_pool(tmp_pool); |
| 63 |
return pstrdup(p, ret); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
timerno = pr_timer_add(PR_TUNABLE_TIMEOUTIDENT, -1, NULL, |
| 68 |
(callback_t) ident_timeout_cb, "ident lookup"); |
| 69 |
if (timerno <= 0) { |
| 70 |
destroy_pool(tmp_pool); |
| 71 |
return pstrdup(p, ret); |
| 72 |
} |
| 73 |
|
| 74 |
ident_conn = pr_inet_create_connection(tmp_pool, NULL, -1, c->local_addr, |
| 75 |
INPORT_ANY, FALSE); |
| 76 |
pr_inet_set_nonblock(tmp_pool, ident_conn); |
| 77 |
|
| 78 |
i = pr_inet_connect_nowait(tmp_pool, ident_conn, c->remote_addr, ident_port); |
| 79 |
if (i < 0) { |
| 80 |
int xerrno = errno; |
| 81 |
|
| 82 |
pr_timer_remove(timerno, ANY_MODULE); |
| 83 |
pr_inet_close(tmp_pool, ident_conn); |
| 84 |
pr_trace_msg(trace_channel, 5, "connection to %s, port %d failed: %s", |
| 85 |
pr_netaddr_get_ipstr(c->remote_addr), ident_port, strerror(xerrno)); |
| 86 |
|
| 87 |
destroy_pool(tmp_pool); |
| 88 |
|
| 89 |
errno = xerrno; |
| 90 |
return pstrdup(p, ret); |
| 91 |
} |
| 92 |
|
| 93 |
if (!i) { |
| 94 |
|
| 95 |
nstrm = pr_netio_open(p, PR_NETIO_STRM_OTHR, ident_conn->listen_fd, |
| 96 |
PR_NETIO_IO_RD); |
| 97 |
pr_netio_set_poll_interval(nstrm, 1); |
| 98 |
|
| 99 |
switch (pr_netio_poll(nstrm)) { |
| 100 |
|
| 101 |
|
| 102 |
case 1: { |
| 103 |
if (ident_timeout) { |
| 104 |
pr_timer_remove(timerno, ANY_MODULE); |
| 105 |
pr_netio_close(nstrm); |
| 106 |
pr_inet_close(tmp_pool, ident_conn); |
| 107 |
|
| 108 |
pr_trace_msg(trace_channel, 5, "lookup timed out, returning '%s'", |
| 109 |
ret); |
| 110 |
destroy_pool(tmp_pool); |
| 111 |
return pstrdup(p, ret); |
| 112 |
} |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
case -1: { |
| 118 |
int xerrno = errno; |
| 119 |
|
| 120 |
pr_timer_remove(timerno, ANY_MODULE); |
| 121 |
pr_netio_close(nstrm); |
| 122 |
pr_inet_close(tmp_pool, ident_conn); |
| 123 |
|
| 124 |
pr_trace_msg(trace_channel, 6, "lookup failed (%s), returning '%s'", |
| 125 |
strerror(xerrno), ret); |
| 126 |
destroy_pool(tmp_pool); |
| 127 |
|
| 128 |
errno = xerrno; |
| 129 |
return pstrdup(p, ret); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
default: { |
| 134 |
ident_conn->mode = CM_OPEN; |
| 135 |
|
| 136 |
if (pr_inet_get_conn_info(ident_conn, ident_conn->listen_fd) < 0) { |
| 137 |
int xerrno = errno; |
| 138 |
|
| 139 |
pr_timer_remove(timerno, ANY_MODULE); |
| 140 |
pr_netio_close(nstrm); |
| 141 |
pr_inet_close(tmp_pool, ident_conn); |
| 142 |
|
| 143 |
pr_trace_msg(trace_channel, 2, |
| 144 |
"lookup timed out (%s), returning '%s'", strerror(xerrno), ret); |
| 145 |
destroy_pool(tmp_pool); |
| 146 |
|
| 147 |
errno = xerrno; |
| 148 |
return pstrdup(p, ret); |
| 149 |
} |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
ident_io = pr_inet_openrw(tmp_pool, ident_conn, NULL, PR_NETIO_STRM_OTHR, |
| 156 |
-1, -1, -1, FALSE); |
| 157 |
if (ident_io == NULL) { |
| 158 |
int xerrno = errno; |
| 159 |
|
| 160 |
pr_timer_remove(timerno, ANY_MODULE); |
| 161 |
pr_inet_close(tmp_pool, ident_conn); |
| 162 |
|
| 163 |
pr_trace_msg(trace_channel, 3, "failed opening read/write connection: %s", |
| 164 |
strerror(xerrno)); |
| 165 |
destroy_pool(tmp_pool); |
| 166 |
|
| 167 |
errno = xerrno; |
| 168 |
return pstrdup(p, ret); |
| 169 |
} |
| 170 |
|
| 171 |
nstrm = ident_io->instrm; |
| 172 |
|
| 173 |
pr_inet_set_nonblock(tmp_pool, ident_io); |
| 174 |
pr_netio_set_poll_interval(ident_io->instrm, 1); |
| 175 |
pr_netio_set_poll_interval(ident_io->outstrm, 1); |
| 176 |
|
| 177 |
pr_netio_printf(ident_io->outstrm, "%d, %d\r\n", c->remote_port, |
| 178 |
c->local_port); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
pr_trace_msg(trace_channel, 4, "reading response from remote ident server"); |
| 188 |
|
| 189 |
if (pr_netio_gets(buf, sizeof(buf), ident_io->instrm)) { |
| 190 |
strip_end(buf, "\r\n"); |
| 191 |
|
| 192 |
pr_trace_msg(trace_channel, 6, "received '%s' from remote ident server", |
| 193 |
buf); |
| 194 |
|
| 195 |
tmp = buf; |
| 196 |
tok = get_token(&tmp, ":"); |
| 197 |
if (tok && (tok = get_token(&tmp, ":"))) { |
| 198 |
while (*tok && isspace((int) *tok)) { |
| 199 |
pr_signals_handle(); |
| 200 |
tok++; |
| 201 |
} |
| 202 |
strip_end(tok, " \t"); |
| 203 |
|
| 204 |
if (strcasecmp(tok, "ERROR") == 0) { |
| 205 |
if (tmp) { |
| 206 |
while (*tmp && isspace((int) *tmp)) { |
| 207 |
pr_signals_handle(); |
| 208 |
tmp++; |
| 209 |
} |
| 210 |
strip_end(tmp, " \t"); |
| 211 |
if (strcasecmp(tmp, "HIDDEN-USER") == 0) |
| 212 |
ret = "HIDDEN-USER"; |
| 213 |
} |
| 214 |
|
| 215 |
} else if (strcasecmp(tok, "USERID") == 0) { |
| 216 |
if (tmp && (tok = get_token(&tmp, ":"))) { |
| 217 |
if (tmp) { |
| 218 |
while (*tmp && isspace((int) *tmp)) { |
| 219 |
pr_signals_handle(); |
| 220 |
tmp++; |
| 221 |
} |
| 222 |
strip_end(tmp, " \t"); |
| 223 |
ret = tmp; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
pr_timer_remove(timerno, ANY_MODULE); |
| 231 |
pr_inet_close(tmp_pool, ident_io); |
| 232 |
pr_inet_close(tmp_pool, ident_conn); |
| 233 |
destroy_pool(tmp_pool); |
| 234 |
|
| 235 |
return pstrdup(p, ret); |
| 236 |
} |