Debian Codegraph @

src/ident.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server daemon
3 * Copyright (c) 1997, 1998 Public Flood Software
4 * Copyright (c) 2001-2016 The ProFTPD Project team
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, Public Flood Software/MacGyver aka Habeeb J. Dihu
21 * and other respective copyright holders give permission to link this program
22 * with OpenSSL, and distribute the resulting executable, without including
23 * the source code for OpenSSL in the source distribution.
24 */
25
26 /* Ident (RFC1413) protocol support */
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 /* Abort the NetIO stream, which will cause netio_poll (and thus
40 * netio_read) to also abort. This is similar to the way data connects
41 * are aborted.
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 /* Set up our timer before going any further. */
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 /* Not yet connected. */
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 /* Aborted, timed out */
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 /* Error. */
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 /* Connected. */
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 /* If the timer fires while in netio_gets(), netio_gets() will simply return
181 * either a partial string, or NULL. This works because ident_timeout_cb
182 * aborts the stream from which we are reading. netio_set_poll_interval() is
183 * used to make sure significant delays don't occur on systems that
184 * automatically restart syscalls after the SIGALRM signal.
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 }