]>
Commit | Line | Data |
---|---|---|
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" |
55cbb02b | 40 | #include "comm/Connection.h" |
aed188fd | 41 | #include "comm/ConnOpener.h" |
cfd66529 | 42 | #include "CommCalls.h" |
4daaf3cb AJ |
43 | #include "ident/Config.h" |
44 | #include "ident/Ident.h" | |
0eb49b6d | 45 | #include "MemBuf.h" |
f1443bd9 | 46 | |
04f7fd38 AJ |
47 | namespace Ident |
48 | { | |
4daaf3cb | 49 | |
f1443bd9 | 50 | #define IDENT_PORT 113 |
05832ae1 | 51 | #define IDENT_KEY_SZ 50 |
52 | ||
26ac0430 | 53 | typedef struct _IdentClient { |
05832ae1 | 54 | IDCB *callback; |
55 | void *callback_data; | |
62e76326 | 56 | |
05832ae1 | 57 | struct _IdentClient *next; |
2fadd50d | 58 | } IdentClient; |
05832ae1 | 59 | |
26ac0430 | 60 | typedef struct _IdentStateData { |
186477c1 | 61 | hash_link hash; /* must be first */ |
aed188fd | 62 | Comm::ConnectionPointer conn; |
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. |
68 | static IOCB ReadReply; | |
69 | static PF Close; | |
70 | static PF Timeout; | |
71 | static CNCB ConnectDone; | |
05832ae1 | 72 | static hash_table *ident_hash = NULL; |
4daaf3cb AJ |
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; | |
05832ae1 | 79 | |
80 | /**** PRIVATE FUNCTIONS ****/ | |
81 | ||
52631ccc | 82 | void |
4daaf3cb | 83 | Ident::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 | 101 | void |
4daaf3cb | 102 | Ident::Close(int fdnotused, void *data) |
f1443bd9 | 103 | { |
be005338 | 104 | IdentStateData *state = (IdentStateData *)data; |
05832ae1 | 105 | identCallback(state, NULL); |
aed188fd | 106 | state->conn->close(); |
05832ae1 | 107 | hash_remove_link(ident_hash, (hash_link *) state); |
6fa85d6f | 108 | xfree(state->hash.key); |
05832ae1 | 109 | cbdataFree(state); |
f1443bd9 | 110 | } |
111 | ||
52631ccc | 112 | void |
4daaf3cb | 113 | Ident::Timeout(int fd, void *data) |
86cf9987 | 114 | { |
be005338 | 115 | IdentStateData *state = (IdentStateData *)data; |
aed188fd AJ |
116 | debugs(30, 3, HERE << "FD " << fd << ", " << state->conn->remote); |
117 | state->conn->close(); | |
86cf9987 | 118 | } |
119 | ||
52631ccc | 120 | void |
aed188fd | 121 | Ident::ConnectDone(Comm::ConnectionPointer &conn, comm_err_t status, int xerrno, void *data) |
e5f6c5c2 | 122 | { |
be005338 | 123 | IdentStateData *state = (IdentStateData *)data; |
62e76326 | 124 | |
9e975e4e | 125 | if (status != COMM_OK) { |
cfd66529 | 126 | if (status == COMM_TIMEOUT) { |
aed188fd | 127 | debugs(30, 3, "IDENT connection timeout to " << state->conn->remote); |
cfd66529 | 128 | } |
62e76326 | 129 | return; |
f1443bd9 | 130 | } |
62e76326 | 131 | |
aed188fd | 132 | assert(conn != NULL && conn == state->conn); |
cfd66529 | 133 | |
05832ae1 | 134 | /* |
fa80a8ef | 135 | * see if any of our clients still care |
05832ae1 | 136 | */ |
cfd66529 | 137 | IdentClient *c; |
05832ae1 | 138 | for (c = state->clients; c; c = c->next) { |
62e76326 | 139 | if (cbdataReferenceValid(c->callback_data)) |
140 | break; | |
05832ae1 | 141 | } |
62e76326 | 142 | |
ac2a30fc | 143 | if (c == NULL) { |
62e76326 | 144 | /* no clients care */ |
55cbb02b | 145 | conn->close(); |
62e76326 | 146 | return; |
ac2a30fc | 147 | } |
62e76326 | 148 | |
cfd66529 AJ |
149 | comm_add_close_handler(conn->fd, Ident::Close, state); |
150 | ||
032785bf | 151 | MemBuf mb; |
2fe7eff9 | 152 | mb.init(); |
153 | mb.Printf("%d, %d\r\n", | |
cfd66529 AJ |
154 | conn->remote.GetPort(), |
155 | conn->local.GetPort()); | |
156 | comm_write_mbuf(conn->fd, &mb, NULL, state); | |
157 | comm_read(conn->fd, state->buf, BUFSIZ, Ident::ReadReply, state); | |
158 | commSetTimeout(conn->fd, Ident::TheConfig.timeout, Ident::Timeout, state); | |
f1443bd9 | 159 | } |
160 | ||
52631ccc | 161 | void |
4daaf3cb | 162 | Ident::ReadReply(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data) |
f1443bd9 | 163 | { |
be005338 | 164 | IdentStateData *state = (IdentStateData *)data; |
05832ae1 | 165 | char *ident = NULL; |
f1443bd9 | 166 | char *t = NULL; |
c4b7a5a9 | 167 | |
cfd66529 | 168 | assert(buf == state->buf); |
aed188fd | 169 | assert(fd == state->conn->fd); |
62e76326 | 170 | |
be005338 | 171 | if (flag != COMM_OK || len <= 0) { |
aed188fd | 172 | state->conn->close(); |
62e76326 | 173 | return; |
05832ae1 | 174 | } |
62e76326 | 175 | |
05832ae1 | 176 | /* |
177 | * XXX This isn't really very tolerant. It should read until EOL | |
178 | * or EOF and then decode the answer... If the reply is fragmented | |
179 | * then this will fail | |
180 | */ | |
181 | buf[len] = '\0'; | |
62e76326 | 182 | |
05832ae1 | 183 | if ((t = strchr(buf, '\r'))) |
62e76326 | 184 | *t = '\0'; |
185 | ||
05832ae1 | 186 | if ((t = strchr(buf, '\n'))) |
62e76326 | 187 | *t = '\0'; |
188 | ||
cfd66529 | 189 | debugs(30, 5, HERE << "FD " << fd << ": Read '" << buf << "'"); |
62e76326 | 190 | |
05832ae1 | 191 | if (strstr(buf, "USERID")) { |
62e76326 | 192 | if ((ident = strrchr(buf, ':'))) { |
3d0ac046 | 193 | while (xisspace(*++ident)); |
4daaf3cb | 194 | Ident::identCallback(state, ident); |
62e76326 | 195 | } |
f1443bd9 | 196 | } |
62e76326 | 197 | |
aed188fd | 198 | state->conn->close(); |
ba1d2afa | 199 | } |
200 | ||
52631ccc | 201 | void |
4daaf3cb | 202 | Ident::ClientAdd(IdentStateData * state, IDCB * callback, void *callback_data) |
05832ae1 | 203 | { |
be005338 | 204 | IdentClient *c = (IdentClient *)xcalloc(1, sizeof(*c)); |
05832ae1 | 205 | IdentClient **C; |
206 | c->callback = callback; | |
fa80a8ef | 207 | c->callback_data = cbdataReference(callback_data); |
62e76326 | 208 | |
3d0ac046 | 209 | for (C = &state->clients; *C; C = &(*C)->next); |
05832ae1 | 210 | *C = c; |
211 | } | |
212 | ||
28c60158 | 213 | CBDATA_TYPE(IdentStateData); |
214 | ||
05832ae1 | 215 | /**** PUBLIC FUNCTIONS ****/ |
216 | ||
217 | /* | |
218 | * start a TCP connection to the peer host on port 113 | |
219 | */ | |
220 | void | |
aed188fd | 221 | Ident::Start(Comm::ConnectionPointer &conn, IDCB * callback, void *data) |
05832ae1 | 222 | { |
223 | IdentStateData *state; | |
05832ae1 | 224 | char key1[IDENT_KEY_SZ]; |
225 | char key2[IDENT_KEY_SZ]; | |
226 | char key[IDENT_KEY_SZ]; | |
cc192b50 | 227 | |
cfd66529 AJ |
228 | conn->local.ToURL(key1, IDENT_KEY_SZ); |
229 | conn->remote.ToURL(key2, IDENT_KEY_SZ); | |
05832ae1 | 230 | snprintf(key, IDENT_KEY_SZ, "%s,%s", key1, key2); |
62e76326 | 231 | |
4daaf3cb AJ |
232 | if (!ident_hash) { |
233 | Init(); | |
234 | } | |
26ac0430 | 235 | if ((state = (IdentStateData *)hash_lookup(ident_hash, key)) != NULL) { |
4daaf3cb | 236 | ClientAdd(state, callback, data); |
62e76326 | 237 | return; |
05832ae1 | 238 | } |
62e76326 | 239 | |
28c60158 | 240 | CBDATA_INIT_TYPE(IdentStateData); |
72711e31 | 241 | state = cbdataAlloc(IdentStateData); |
186477c1 | 242 | state->hash.key = xstrdup(key); |
aed188fd AJ |
243 | |
244 | // copy the conn details. We dont want the original FD to be re-used by IDENT. | |
245 | state->conn = conn->copyDetails(); | |
246 | // NP: use random port for secure outbound to IDENT_PORT | |
247 | state->conn->local.SetPort(0); | |
cfd66529 | 248 | |
4daaf3cb | 249 | ClientAdd(state, callback, data); |
186477c1 | 250 | hash_join(ident_hash, &state->hash); |
cfd66529 AJ |
251 | |
252 | AsyncCall::Pointer call = commCbCall(30,3, "Ident::ConnectDone", CommConnectCbPtrFun(Ident::ConnectDone, state)); | |
4accd6d8 | 253 | AsyncJob::AsyncStart(new Comm::ConnOpener(state->conn, call, Ident::TheConfig.timeout)); |
05832ae1 | 254 | } |
255 | ||
256 | void | |
4daaf3cb | 257 | Ident::Init(void) |
ba1d2afa | 258 | { |
04f7fd38 | 259 | if (ident_hash) { |
4daaf3cb AJ |
260 | debugs(30, DBG_CRITICAL, "WARNING: Ident already initialized."); |
261 | return; | |
262 | } | |
263 | ||
30abd221 | 264 | ident_hash = hash_create((HASHCMP *) strcmp, |
62e76326 | 265 | hashPrime(Squid_MaxFD / 8), |
266 | hash4); | |
f1443bd9 | 267 | } |
4daaf3cb AJ |
268 | |
269 | #endif /* USE_IDENT */ |