]>
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" |
4daaf3cb AJ |
40 | #include "ident/Config.h" |
41 | #include "ident/Ident.h" | |
0eb49b6d | 42 | #include "MemBuf.h" |
f1443bd9 | 43 | |
4daaf3cb AJ |
44 | namespace Ident { |
45 | ||
f1443bd9 | 46 | #define IDENT_PORT 113 |
05832ae1 | 47 | #define IDENT_KEY_SZ 50 |
48 | ||
26ac0430 | 49 | typedef struct _IdentClient { |
05832ae1 | 50 | IDCB *callback; |
51 | void *callback_data; | |
62e76326 | 52 | |
05832ae1 | 53 | struct _IdentClient *next; |
2fadd50d | 54 | } IdentClient; |
05832ae1 | 55 | |
26ac0430 | 56 | typedef struct _IdentStateData { |
186477c1 | 57 | hash_link hash; /* must be first */ |
05832ae1 | 58 | int fd; /* IDENT fd */ |
62e76326 | 59 | |
ad61a2b4 | 60 | IpAddress me; |
62e76326 | 61 | |
ad61a2b4 | 62 | IpAddress 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. |
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 | ||
82 | static 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 | |
b8d8561b | 101 | static void |
4daaf3cb | 102 | Ident::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 | ||
86cf9987 | 112 | static void |
4daaf3cb | 113 | Ident::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 | ||
e5f6c5c2 | 121 | static void |
4daaf3cb | 122 | Ident::ConnectDone(int fd, 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 | ||
b8d8561b | 157 | static void |
4daaf3cb | 158 | Ident::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 | ||
196 | static void | |
4daaf3cb | 197 | Ident::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 | 208 | CBDATA_TYPE(IdentStateData); |
209 | ||
05832ae1 | 210 | /**** PUBLIC FUNCTIONS ****/ |
211 | ||
212 | /* | |
213 | * start a TCP connection to the peer host on port 113 | |
214 | */ | |
215 | void | |
4daaf3cb | 216 | Ident::Start(IpAddress &me, IpAddress &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 | |
c26c5e62 AJ |
237 | IpAddress addr = me; |
238 | addr.SetPort(0); // NP: use random port for secure outbound to IDENT_PORT | |
239 | ||
31be869c | 240 | fd = comm_open_listener(SOCK_STREAM, |
bdb741f4 | 241 | IPPROTO_TCP, |
c26c5e62 | 242 | addr, |
62e76326 | 243 | COMM_NONBLOCKING, |
244 | "ident"); | |
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 | ||
266 | void | |
4daaf3cb | 267 | Ident::Init(void) |
ba1d2afa | 268 | { |
4daaf3cb AJ |
269 | if(ident_hash) { |
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 */ |