]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ident/Ident.cc
Send HTTP/1.1 on CONNECT responses
[thirdparty/squid.git] / src / ident / Ident.cc
CommitLineData
f1443bd9 1/*
262a0e14 2 * $Id$
632e2027 3 *
3c66d057 4 * DEBUG: section 30 Ident (RFC 931)
632e2027 5 * AUTHOR: Duane Wessels
6 *
2b6662ba 7 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 8 * ----------------------------------------------------------
632e2027 9 *
2b6662ba 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.
632e2027 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.
26ac0430 23 *
632e2027 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.
26ac0430 28 *
632e2027 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
cbdec147 31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 32 *
f1443bd9 33 */
34
35#include "squid.h"
4daaf3cb
AJ
36
37#if USE_IDENT
38
a46d2c0e 39#include "comm.h"
4daaf3cb
AJ
40#include "ident/Config.h"
41#include "ident/Ident.h"
0eb49b6d 42#include "MemBuf.h"
f1443bd9 43
04f7fd38
AJ
44namespace Ident
45{
4daaf3cb 46
f1443bd9 47#define IDENT_PORT 113
05832ae1 48#define IDENT_KEY_SZ 50
49
26ac0430 50typedef struct _IdentClient {
05832ae1 51 IDCB *callback;
52 void *callback_data;
62e76326 53
05832ae1 54 struct _IdentClient *next;
2fadd50d 55} IdentClient;
05832ae1 56
26ac0430 57typedef struct _IdentStateData {
186477c1 58 hash_link hash; /* must be first */
05832ae1 59 int fd; /* IDENT fd */
62e76326 60
b7ac5457
AJ
61 Ip::Address me;
62 Ip::Address my_peer;
05832ae1 63 IdentClient *clients;
c4b7a5a9 64 char buf[4096];
2fadd50d 65} IdentStateData;
f1443bd9 66
4daaf3cb
AJ
67// TODO: make these all a series of Async jobs. They are self-contained callbacks now.
68static IOCB ReadReply;
69static PF Close;
70static PF Timeout;
71static CNCB ConnectDone;
05832ae1 72static hash_table *ident_hash = NULL;
4daaf3cb
AJ
73static void ClientAdd(IdentStateData * state, IDCB * callback, void *callback_data);
74static void identCallback(IdentStateData * state, char *result);
75
76}; // namespace Ident
77
78Ident::IdentConfig Ident::TheConfig;
05832ae1 79
80/**** PRIVATE FUNCTIONS ****/
81
52631ccc 82void
4daaf3cb 83Ident::identCallback(IdentStateData * state, char *result)
05832ae1 84{
85 IdentClient *client;
62e76326 86
05832ae1 87 if (result && *result == '\0')
62e76326 88 result = NULL;
89
05832ae1 90 while ((client = state->clients)) {
62e76326 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);
05832ae1 98 }
99}
f1443bd9 100
52631ccc 101void
4daaf3cb 102Ident::Close(int fdnotused, void *data)
f1443bd9 103{
be005338 104 IdentStateData *state = (IdentStateData *)data;
05832ae1 105 identCallback(state, NULL);
106 comm_close(state->fd);
107 hash_remove_link(ident_hash, (hash_link *) state);
6fa85d6f 108 xfree(state->hash.key);
05832ae1 109 cbdataFree(state);
f1443bd9 110}
111
52631ccc 112void
4daaf3cb 113Ident::Timeout(int fd, void *data)
86cf9987 114{
be005338 115 IdentStateData *state = (IdentStateData *)data;
cc192b50 116 debugs(30, 3, "identTimeout: FD " << fd << ", " << state->my_peer);
bf8fe701 117
86cf9987 118 comm_close(fd);
119}
120
52631ccc 121void
3ff65596 122Ident::ConnectDone(int fd, const DnsLookupDetails &, comm_err_t status, int xerrno, void *data)
e5f6c5c2 123{
be005338 124 IdentStateData *state = (IdentStateData *)data;
05832ae1 125 IdentClient *c;
62e76326 126
9e975e4e 127 if (status != COMM_OK) {
62e76326 128 /* Failed to connect */
129 comm_close(fd);
130 return;
f1443bd9 131 }
62e76326 132
05832ae1 133 /*
fa80a8ef 134 * see if any of our clients still care
05832ae1 135 */
136 for (c = state->clients; c; c = c->next) {
62e76326 137 if (cbdataReferenceValid(c->callback_data))
138 break;
05832ae1 139 }
62e76326 140
ac2a30fc 141 if (c == NULL) {
62e76326 142 /* no clients care */
143 comm_close(fd);
144 return;
ac2a30fc 145 }
62e76326 146
032785bf 147 MemBuf mb;
2fe7eff9 148 mb.init();
149 mb.Printf("%d, %d\r\n",
cc192b50 150 state->my_peer.GetPort(),
151 state->me.GetPort());
2b663917 152 comm_write_mbuf(fd, &mb, NULL, state);
4daaf3cb
AJ
153 comm_read(fd, state->buf, BUFSIZ, Ident::ReadReply, state);
154 commSetTimeout(fd, Ident::TheConfig.timeout, Ident::Timeout, state);
f1443bd9 155}
156
52631ccc 157void
4daaf3cb 158Ident::ReadReply(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
f1443bd9 159{
be005338 160 IdentStateData *state = (IdentStateData *)data;
05832ae1 161 char *ident = NULL;
f1443bd9 162 char *t = NULL;
c4b7a5a9 163
164 assert (buf == state->buf);
62e76326 165
be005338 166 if (flag != COMM_OK || len <= 0) {
62e76326 167 comm_close(fd);
168 return;
05832ae1 169 }
62e76326 170
05832ae1 171 /*
172 * XXX This isn't really very tolerant. It should read until EOL
173 * or EOF and then decode the answer... If the reply is fragmented
174 * then this will fail
175 */
176 buf[len] = '\0';
62e76326 177
05832ae1 178 if ((t = strchr(buf, '\r')))
62e76326 179 *t = '\0';
180
05832ae1 181 if ((t = strchr(buf, '\n')))
62e76326 182 *t = '\0';
183
bf8fe701 184 debugs(30, 5, "identReadReply: FD " << fd << ": Read '" << buf << "'");
62e76326 185
05832ae1 186 if (strstr(buf, "USERID")) {
62e76326 187 if ((ident = strrchr(buf, ':'))) {
3d0ac046 188 while (xisspace(*++ident));
4daaf3cb 189 Ident::identCallback(state, ident);
62e76326 190 }
f1443bd9 191 }
62e76326 192
f1443bd9 193 comm_close(fd);
ba1d2afa 194}
195
52631ccc 196void
4daaf3cb 197Ident::ClientAdd(IdentStateData * state, IDCB * callback, void *callback_data)
05832ae1 198{
be005338 199 IdentClient *c = (IdentClient *)xcalloc(1, sizeof(*c));
05832ae1 200 IdentClient **C;
201 c->callback = callback;
fa80a8ef 202 c->callback_data = cbdataReference(callback_data);
62e76326 203
3d0ac046 204 for (C = &state->clients; *C; C = &(*C)->next);
05832ae1 205 *C = c;
206}
207
28c60158 208CBDATA_TYPE(IdentStateData);
209
05832ae1 210/**** PUBLIC FUNCTIONS ****/
211
212/*
213 * start a TCP connection to the peer host on port 113
214 */
215void
b7ac5457 216Ident::Start(Ip::Address &me, Ip::Address &my_peer, IDCB * callback, void *data)
05832ae1 217{
218 IdentStateData *state;
219 int fd;
220 char key1[IDENT_KEY_SZ];
221 char key2[IDENT_KEY_SZ];
222 char key[IDENT_KEY_SZ];
cc192b50 223 char ntoabuf[MAX_IPSTRLEN];
224
225 me.ToURL(key1, IDENT_KEY_SZ);
226 my_peer.ToURL(key2, IDENT_KEY_SZ);
05832ae1 227 snprintf(key, IDENT_KEY_SZ, "%s,%s", key1, key2);
62e76326 228
4daaf3cb
AJ
229 if (!ident_hash) {
230 Init();
231 }
26ac0430 232 if ((state = (IdentStateData *)hash_lookup(ident_hash, key)) != NULL) {
4daaf3cb 233 ClientAdd(state, callback, data);
62e76326 234 return;
05832ae1 235 }
62e76326 236
b7ac5457 237 Ip::Address addr = me;
c26c5e62
AJ
238 addr.SetPort(0); // NP: use random port for secure outbound to IDENT_PORT
239
31be869c 240 fd = comm_open_listener(SOCK_STREAM,
04f7fd38
AJ
241 IPPROTO_TCP,
242 addr,
243 COMM_NONBLOCKING,
244 "ident");
62e76326 245
26ac0430 246 if (fd == COMM_ERROR) {
62e76326 247 /* Failed to get a local socket */
248 callback(NULL, data);
249 return;
05832ae1 250 }
62e76326 251
28c60158 252 CBDATA_INIT_TYPE(IdentStateData);
72711e31 253 state = cbdataAlloc(IdentStateData);
186477c1 254 state->hash.key = xstrdup(key);
05832ae1 255 state->fd = fd;
cc192b50 256 state->me = me;
257 state->my_peer = my_peer;
4daaf3cb 258 ClientAdd(state, callback, data);
186477c1 259 hash_join(ident_hash, &state->hash);
4daaf3cb
AJ
260 comm_add_close_handler(fd, Ident::Close, state);
261 commSetTimeout(fd, Ident::TheConfig.timeout, Ident::Timeout, state);
262 state->my_peer.NtoA(ntoabuf,MAX_IPSTRLEN);
263 commConnectStart(fd, ntoabuf, IDENT_PORT, Ident::ConnectDone, state);
05832ae1 264}
265
266void
4daaf3cb 267Ident::Init(void)
ba1d2afa 268{
04f7fd38 269 if (ident_hash) {
4daaf3cb
AJ
270 debugs(30, DBG_CRITICAL, "WARNING: Ident already initialized.");
271 return;
272 }
273
30abd221 274 ident_hash = hash_create((HASHCMP *) strcmp,
62e76326 275 hashPrime(Squid_MaxFD / 8),
276 hash4);
f1443bd9 277}
4daaf3cb
AJ
278
279#endif /* USE_IDENT */