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