]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ident/Ident.cc
Audit round 2 fallout.
[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.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 "ident/Config.h"
44 #include "ident/Ident.h"
45 #include "MemBuf.h"
46
47 namespace Ident
48 {
49
50 #define IDENT_PORT 113
51 #define IDENT_KEY_SZ 50
52
53 typedef struct _IdentClient {
54 IDCB *callback;
55 void *callback_data;
56
57 struct _IdentClient *next;
58 } IdentClient;
59
60 typedef struct _IdentStateData {
61 hash_link hash; /* must be first */
62 Comm::ConnectionPointer conn;
63 IdentClient *clients;
64 char buf[4096];
65 } IdentStateData;
66
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;
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(int fdnotused, void *data)
103 {
104 IdentStateData *state = (IdentStateData *)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(int fd, void *data)
114 {
115 IdentStateData *state = (IdentStateData *)data;
116 debugs(30, 3, HERE << "FD " << fd << ", " << state->conn->remote);
117 state->conn->close();
118 }
119
120 void
121 Ident::ConnectDone(Comm::ConnectionPointer &conn, comm_err_t status, int xerrno, void *data)
122 {
123 IdentStateData *state = (IdentStateData *)data;
124
125 if (status != COMM_OK) {
126 if (status == COMM_TIMEOUT) {
127 debugs(30, 3, "IDENT connection timeout to " << state->conn->remote);
128 }
129 return;
130 }
131
132 assert(conn != NULL && conn == state->conn);
133
134 /*
135 * see if any of our clients still care
136 */
137 IdentClient *c;
138 for (c = state->clients; c; c = c->next) {
139 if (cbdataReferenceValid(c->callback_data))
140 break;
141 }
142
143 if (c == NULL) {
144 /* no clients care */
145 conn->close();
146 return;
147 }
148
149 comm_add_close_handler(conn->fd, Ident::Close, state);
150
151 MemBuf mb;
152 mb.init();
153 mb.Printf("%d, %d\r\n",
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);
159 }
160
161 void
162 Ident::ReadReply(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
163 {
164 IdentStateData *state = (IdentStateData *)data;
165 char *ident = NULL;
166 char *t = NULL;
167
168 assert(buf == state->buf);
169 assert(fd == state->conn->fd);
170
171 if (flag != COMM_OK || len <= 0) {
172 state->conn->close();
173 return;
174 }
175
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';
182
183 if ((t = strchr(buf, '\r')))
184 *t = '\0';
185
186 if ((t = strchr(buf, '\n')))
187 *t = '\0';
188
189 debugs(30, 5, HERE << "FD " << fd << ": Read '" << buf << "'");
190
191 if (strstr(buf, "USERID")) {
192 if ((ident = strrchr(buf, ':'))) {
193 while (xisspace(*++ident));
194 Ident::identCallback(state, ident);
195 }
196 }
197
198 state->conn->close();
199 }
200
201 void
202 Ident::ClientAdd(IdentStateData * state, IDCB * callback, void *callback_data)
203 {
204 IdentClient *c = (IdentClient *)xcalloc(1, sizeof(*c));
205 IdentClient **C;
206 c->callback = callback;
207 c->callback_data = cbdataReference(callback_data);
208
209 for (C = &state->clients; *C; C = &(*C)->next);
210 *C = c;
211 }
212
213 CBDATA_TYPE(IdentStateData);
214
215 /**** PUBLIC FUNCTIONS ****/
216
217 /*
218 * start a TCP connection to the peer host on port 113
219 */
220 void
221 Ident::Start(Comm::ConnectionPointer &conn, IDCB * callback, void *data)
222 {
223 IdentStateData *state;
224 char key1[IDENT_KEY_SZ];
225 char key2[IDENT_KEY_SZ];
226 char key[IDENT_KEY_SZ];
227
228 conn->local.ToURL(key1, IDENT_KEY_SZ);
229 conn->remote.ToURL(key2, IDENT_KEY_SZ);
230 snprintf(key, IDENT_KEY_SZ, "%s,%s", key1, key2);
231
232 if (!ident_hash) {
233 Init();
234 }
235 if ((state = (IdentStateData *)hash_lookup(ident_hash, key)) != NULL) {
236 ClientAdd(state, callback, data);
237 return;
238 }
239
240 CBDATA_INIT_TYPE(IdentStateData);
241 state = cbdataAlloc(IdentStateData);
242 state->hash.key = xstrdup(key);
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);
248
249 ClientAdd(state, callback, data);
250 hash_join(ident_hash, &state->hash);
251
252 AsyncCall::Pointer call = commCbCall(30,3, "Ident::ConnectDone", CommConnectCbPtrFun(Ident::ConnectDone, state));
253 ConnOpener *cs = new ConnOpener(state->conn, call);
254 cs->connect_timeout = Ident::TheConfig.timeout;
255 cs->start();
256 }
257
258 void
259 Ident::Init(void)
260 {
261 if (ident_hash) {
262 debugs(30, DBG_CRITICAL, "WARNING: Ident already initialized.");
263 return;
264 }
265
266 ident_hash = hash_create((HASHCMP *) strcmp,
267 hashPrime(Squid_MaxFD / 8),
268 hash4);
269 }
270
271 #endif /* USE_IDENT */