]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ident/Ident.cc
4 * DEBUG: section 30 Ident (RFC 931)
5 * AUTHOR: Duane Wessels
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
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.
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.
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.
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.
35 #include "squid-old.h"
40 #include "comm/Connection.h"
41 #include "comm/ConnOpener.h"
42 #include "CommCalls.h"
43 #include "comm/Write.h"
44 #include "ident/Config.h"
45 #include "ident/Ident.h"
51 #define IDENT_PORT 113
52 #define IDENT_KEY_SZ 50
53 #define IDENT_BUFSIZE 4096
55 typedef struct _IdentClient
{
59 struct _IdentClient
*next
;
62 typedef struct _IdentStateData
{
63 hash_link hash
; /* must be first */
64 Comm::ConnectionPointer conn
;
66 char buf
[IDENT_BUFSIZE
];
69 // TODO: make these all a series of Async job calls. They are self-contained callbacks now.
70 static IOCB ReadReply
;
73 static CNCB ConnectDone
;
74 static hash_table
*ident_hash
= NULL
;
75 static void ClientAdd(IdentStateData
* state
, IDCB
* callback
, void *callback_data
);
76 static void identCallback(IdentStateData
* state
, char *result
);
80 Ident::IdentConfig
Ident::TheConfig
;
82 /**** PRIVATE FUNCTIONS ****/
85 Ident::identCallback(IdentStateData
* state
, char *result
)
89 if (result
&& *result
== '\0')
92 while ((client
= state
->clients
)) {
94 state
->clients
= client
->next
;
96 if (cbdataReferenceValidDone(client
->callback_data
, &cbdata
))
97 client
->callback(result
, cbdata
);
104 Ident::Close(const CommCloseCbParams
¶ms
)
106 IdentStateData
*state
= (IdentStateData
*)params
.data
;
107 identCallback(state
, NULL
);
108 state
->conn
->close();
109 hash_remove_link(ident_hash
, (hash_link
*) state
);
110 xfree(state
->hash
.key
);
115 Ident::Timeout(const CommTimeoutCbParams
&io
)
117 debugs(30, 3, HERE
<< io
.conn
);
122 Ident::ConnectDone(const Comm::ConnectionPointer
&conn
, comm_err_t status
, int xerrno
, void *data
)
124 IdentStateData
*state
= (IdentStateData
*)data
;
126 if (status
!= COMM_OK
) {
127 if (status
== COMM_TIMEOUT
) {
128 debugs(30, 3, "IDENT connection timeout to " << state
->conn
->remote
);
133 assert(conn
!= NULL
&& conn
== state
->conn
);
136 * see if any of our clients still care
139 for (c
= state
->clients
; c
; c
= c
->next
) {
140 if (cbdataReferenceValid(c
->callback_data
))
145 /* no clients care */
150 comm_add_close_handler(conn
->fd
, Ident::Close
, state
);
154 mb
.Printf("%d, %d\r\n",
155 conn
->remote
.GetPort(),
156 conn
->local
.GetPort());
157 AsyncCall::Pointer nil
;
158 Comm::Write(conn
, &mb
, nil
);
159 AsyncCall::Pointer readCall
= commCbCall(5,4, "Ident::ReadReply",
160 CommIoCbPtrFun(Ident::ReadReply
, state
));
161 comm_read(conn
, state
->buf
, IDENT_BUFSIZE
, readCall
);
162 AsyncCall::Pointer timeoutCall
= commCbCall(5,4, "Ident::Timeout",
163 CommTimeoutCbPtrFun(Ident::Timeout
, state
));
164 commSetConnTimeout(conn
, Ident::TheConfig
.timeout
, timeoutCall
);
168 Ident::ReadReply(const Comm::ConnectionPointer
&conn
, char *buf
, size_t len
, comm_err_t flag
, int xerrno
, void *data
)
170 IdentStateData
*state
= (IdentStateData
*)data
;
174 assert(buf
== state
->buf
);
175 assert(conn
->fd
== state
->conn
->fd
);
177 if (flag
!= COMM_OK
|| len
<= 0) {
178 state
->conn
->close();
183 * XXX This isn't really very tolerant. It should read until EOL
184 * or EOF and then decode the answer... If the reply is fragmented
185 * then this will fail
189 if ((t
= strchr(buf
, '\r')))
192 if ((t
= strchr(buf
, '\n')))
195 debugs(30, 5, HERE
<< conn
<< ": Read '" << buf
<< "'");
197 if (strstr(buf
, "USERID")) {
198 if ((ident
= strrchr(buf
, ':'))) {
199 while (xisspace(*++ident
));
200 Ident::identCallback(state
, ident
);
204 state
->conn
->close();
208 Ident::ClientAdd(IdentStateData
* state
, IDCB
* callback
, void *callback_data
)
210 IdentClient
*c
= (IdentClient
*)xcalloc(1, sizeof(*c
));
212 c
->callback
= callback
;
213 c
->callback_data
= cbdataReference(callback_data
);
215 for (C
= &state
->clients
; *C
; C
= &(*C
)->next
);
219 CBDATA_TYPE(IdentStateData
);
221 /**** PUBLIC FUNCTIONS ****/
224 * start a TCP connection to the peer host on port 113
227 Ident::Start(const Comm::ConnectionPointer
&conn
, IDCB
* callback
, void *data
)
229 IdentStateData
*state
;
230 char key1
[IDENT_KEY_SZ
];
231 char key2
[IDENT_KEY_SZ
];
232 char key
[IDENT_KEY_SZ
];
234 conn
->local
.ToURL(key1
, IDENT_KEY_SZ
);
235 conn
->remote
.ToURL(key2
, IDENT_KEY_SZ
);
236 snprintf(key
, IDENT_KEY_SZ
, "%s,%s", key1
, key2
);
241 if ((state
= (IdentStateData
*)hash_lookup(ident_hash
, key
)) != NULL
) {
242 ClientAdd(state
, callback
, data
);
246 CBDATA_INIT_TYPE(IdentStateData
);
247 state
= cbdataAlloc(IdentStateData
);
248 state
->hash
.key
= xstrdup(key
);
250 // copy the conn details. We dont want the original FD to be re-used by IDENT.
251 state
->conn
= conn
->copyDetails();
252 // NP: use random port for secure outbound to IDENT_PORT
253 state
->conn
->local
.SetPort(0);
255 ClientAdd(state
, callback
, data
);
256 hash_join(ident_hash
, &state
->hash
);
258 AsyncCall::Pointer call
= commCbCall(30,3, "Ident::ConnectDone", CommConnectCbPtrFun(Ident::ConnectDone
, state
));
259 AsyncJob::Start(new Comm::ConnOpener(state
->conn
, call
, Ident::TheConfig
.timeout
));
266 debugs(30, DBG_CRITICAL
, "WARNING: Ident already initialized.");
270 ident_hash
= hash_create((HASHCMP
*) strcmp
,
271 hashPrime(Squid_MaxFD
/ 8),
275 #endif /* USE_IDENT */