]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ident/Ident.cc
Bug 3580: IDENT request makes squid crash
[thirdparty/squid.git] / src / ident / Ident.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 30 Ident (RFC 931)
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include "squid-old.h"
36
37 #if USE_IDENT
38
39 #include "comm.h"
40 #include "comm/Connection.h"
41 #include "comm/ConnOpener.h"
42 #include "CommCalls.h"
43 #include "comm/Write.h"
44 #include "ident/Config.h"
45 #include "ident/Ident.h"
46 #include "MemBuf.h"
47
48 namespace Ident
49 {
50
51 #define IDENT_PORT 113
52 #define IDENT_KEY_SZ 50
53 #define IDENT_BUFSIZE 4096
54
55 typedef struct _IdentClient {
56 IDCB *callback;
57 void *callback_data;
58
59 struct _IdentClient *next;
60 } IdentClient;
61
62 typedef struct _IdentStateData {
63 hash_link hash; /* must be first */
64 Comm::ConnectionPointer conn;
65 IdentClient *clients;
66 char buf[IDENT_BUFSIZE];
67 } IdentStateData;
68
69 // TODO: make these all a series of Async job calls. They are self-contained callbacks now.
70 static IOCB ReadReply;
71 static IOCB WriteFeedback;
72 static CLCB Close;
73 static CTCB Timeout;
74 static CNCB ConnectDone;
75 static hash_table *ident_hash = NULL;
76 static void ClientAdd(IdentStateData * state, IDCB * callback, void *callback_data);
77 static void identCallback(IdentStateData * state, char *result);
78
79 } // namespace Ident
80
81 Ident::IdentConfig Ident::TheConfig;
82
83 /**** PRIVATE FUNCTIONS ****/
84
85 void
86 Ident::identCallback(IdentStateData * state, char *result)
87 {
88 IdentClient *client;
89
90 if (result && *result == '\0')
91 result = NULL;
92
93 while ((client = state->clients)) {
94 void *cbdata;
95 state->clients = client->next;
96
97 if (cbdataReferenceValidDone(client->callback_data, &cbdata))
98 client->callback(result, cbdata);
99
100 xfree(client);
101 }
102 }
103
104 void
105 Ident::Close(const CommCloseCbParams &params)
106 {
107 IdentStateData *state = (IdentStateData *)params.data;
108 identCallback(state, NULL);
109 state->conn->close();
110 hash_remove_link(ident_hash, (hash_link *) state);
111 xfree(state->hash.key);
112 cbdataFree(state);
113 }
114
115 void
116 Ident::Timeout(const CommTimeoutCbParams &io)
117 {
118 debugs(30, 3, HERE << io.conn);
119 io.conn->close();
120 }
121
122 void
123 Ident::ConnectDone(const Comm::ConnectionPointer &conn, comm_err_t status, int xerrno, void *data)
124 {
125 IdentStateData *state = (IdentStateData *)data;
126
127 if (status != COMM_OK) {
128 if (status == COMM_TIMEOUT) {
129 debugs(30, 3, "IDENT connection timeout to " << state->conn->remote);
130 }
131 return;
132 }
133
134 assert(conn != NULL && conn == state->conn);
135
136 /*
137 * see if any of our clients still care
138 */
139 IdentClient *c;
140 for (c = state->clients; c; c = c->next) {
141 if (cbdataReferenceValid(c->callback_data))
142 break;
143 }
144
145 if (c == NULL) {
146 /* no clients care */
147 conn->close();
148 return;
149 }
150
151 comm_add_close_handler(conn->fd, Ident::Close, state);
152
153 MemBuf mb;
154 mb.init();
155 mb.Printf("%d, %d\r\n",
156 conn->remote.GetPort(),
157 conn->local.GetPort());
158 AsyncCall::Pointer writeCall = commCbCall(5,4, "Ident::WriteFeedback",
159 CommIoCbPtrFun(Ident::WriteFeedback, state));
160 Comm::Write(conn, &mb, writeCall);
161 AsyncCall::Pointer readCall = commCbCall(5,4, "Ident::ReadReply",
162 CommIoCbPtrFun(Ident::ReadReply, state));
163 comm_read(conn, state->buf, IDENT_BUFSIZE, readCall);
164 AsyncCall::Pointer timeoutCall = commCbCall(5,4, "Ident::Timeout",
165 CommTimeoutCbPtrFun(Ident::Timeout, state));
166 commSetConnTimeout(conn, Ident::TheConfig.timeout, timeoutCall);
167 }
168
169 void
170 Ident::WriteFeedback(const Comm::ConnectionPointer &conn, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
171 {
172 debugs(30, 5, HERE << conn << ": Wrote IDENT request " << len << " bytes.");
173
174 // TODO handle write errors better. retry or abort?
175 if (flag != COMM_OK) {
176 debugs(30, 2, HERE << conn << " err-flags=" << flag << " IDENT write error: " << xstrerr(xerrno));
177 conn->close();
178 }
179 }
180
181 void
182 Ident::ReadReply(const Comm::ConnectionPointer &conn, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
183 {
184 IdentStateData *state = (IdentStateData *)data;
185 char *ident = NULL;
186 char *t = NULL;
187
188 assert(buf == state->buf);
189 assert(conn->fd == state->conn->fd);
190
191 if (flag != COMM_OK || len <= 0) {
192 state->conn->close();
193 return;
194 }
195
196 /*
197 * XXX This isn't really very tolerant. It should read until EOL
198 * or EOF and then decode the answer... If the reply is fragmented
199 * then this will fail
200 */
201 buf[len] = '\0';
202
203 if ((t = strchr(buf, '\r')))
204 *t = '\0';
205
206 if ((t = strchr(buf, '\n')))
207 *t = '\0';
208
209 debugs(30, 5, HERE << conn << ": Read '" << buf << "'");
210
211 if (strstr(buf, "USERID")) {
212 if ((ident = strrchr(buf, ':'))) {
213 while (xisspace(*++ident));
214 Ident::identCallback(state, ident);
215 }
216 }
217
218 state->conn->close();
219 }
220
221 void
222 Ident::ClientAdd(IdentStateData * state, IDCB * callback, void *callback_data)
223 {
224 IdentClient *c = (IdentClient *)xcalloc(1, sizeof(*c));
225 IdentClient **C;
226 c->callback = callback;
227 c->callback_data = cbdataReference(callback_data);
228
229 for (C = &state->clients; *C; C = &(*C)->next);
230 *C = c;
231 }
232
233 CBDATA_TYPE(IdentStateData);
234
235 /**** PUBLIC FUNCTIONS ****/
236
237 /*
238 * start a TCP connection to the peer host on port 113
239 */
240 void
241 Ident::Start(const Comm::ConnectionPointer &conn, IDCB * callback, void *data)
242 {
243 IdentStateData *state;
244 char key1[IDENT_KEY_SZ];
245 char key2[IDENT_KEY_SZ];
246 char key[IDENT_KEY_SZ];
247
248 conn->local.ToURL(key1, IDENT_KEY_SZ);
249 conn->remote.ToURL(key2, IDENT_KEY_SZ);
250 snprintf(key, IDENT_KEY_SZ, "%s,%s", key1, key2);
251
252 if (!ident_hash) {
253 Init();
254 }
255 if ((state = (IdentStateData *)hash_lookup(ident_hash, key)) != NULL) {
256 ClientAdd(state, callback, data);
257 return;
258 }
259
260 CBDATA_INIT_TYPE(IdentStateData);
261 state = cbdataAlloc(IdentStateData);
262 state->hash.key = xstrdup(key);
263
264 // copy the conn details. We dont want the original FD to be re-used by IDENT.
265 state->conn = conn->copyDetails();
266 // NP: use random port for secure outbound to IDENT_PORT
267 state->conn->local.SetPort(0);
268 state->conn->remote.SetPort(IDENT_PORT);
269
270 ClientAdd(state, callback, data);
271 hash_join(ident_hash, &state->hash);
272
273 AsyncCall::Pointer call = commCbCall(30,3, "Ident::ConnectDone", CommConnectCbPtrFun(Ident::ConnectDone, state));
274 AsyncJob::Start(new Comm::ConnOpener(state->conn, call, Ident::TheConfig.timeout));
275 }
276
277 void
278 Ident::Init(void)
279 {
280 if (ident_hash) {
281 debugs(30, DBG_CRITICAL, "WARNING: Ident already initialized.");
282 return;
283 }
284
285 ident_hash = hash_create((HASHCMP *) strcmp,
286 hashPrime(Squid_MaxFD / 8),
287 hash4);
288 }
289
290 #endif /* USE_IDENT */