]>
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.
40 #include "comm/Connection.h"
41 #include "comm/ConnOpener.h"
42 #include "CommCalls.h"
43 #include "ident/Config.h"
44 #include "ident/Ident.h"
50 #define IDENT_PORT 113
51 #define IDENT_KEY_SZ 50
53 typedef struct _IdentClient
{
57 struct _IdentClient
*next
;
60 typedef struct _IdentStateData
{
61 hash_link hash
; /* must be first */
62 Comm::ConnectionPointer conn
;
67 // TODO: make these all a series of Async job calls. They are self-contained callbacks now.
68 static IOCB ReadReply
;
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
);
78 Ident::IdentConfig
Ident::TheConfig
;
80 /**** PRIVATE FUNCTIONS ****/
83 Ident::identCallback(IdentStateData
* state
, char *result
)
87 if (result
&& *result
== '\0')
90 while ((client
= state
->clients
)) {
92 state
->clients
= client
->next
;
94 if (cbdataReferenceValidDone(client
->callback_data
, &cbdata
))
95 client
->callback(result
, cbdata
);
102 Ident::Close(int fdnotused
, void *data
)
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
);
113 Ident::Timeout(int fd
, void *data
)
115 IdentStateData
*state
= (IdentStateData
*)data
;
116 debugs(30, 3, HERE
<< "FD " << fd
<< ", " << state
->conn
->remote
);
117 state
->conn
->close();
121 Ident::ConnectDone(const Comm::ConnectionPointer
&conn
, comm_err_t status
, int xerrno
, void *data
)
123 IdentStateData
*state
= (IdentStateData
*)data
;
125 if (status
!= COMM_OK
) {
126 if (status
== COMM_TIMEOUT
) {
127 debugs(30, 3, "IDENT connection timeout to " << state
->conn
->remote
);
132 assert(conn
!= NULL
&& conn
== state
->conn
);
135 * see if any of our clients still care
138 for (c
= state
->clients
; c
; c
= c
->next
) {
139 if (cbdataReferenceValid(c
->callback_data
))
144 /* no clients care */
149 comm_add_close_handler(conn
->fd
, Ident::Close
, state
);
153 mb
.Printf("%d, %d\r\n",
154 conn
->remote
.GetPort(),
155 conn
->local
.GetPort());
156 comm_write_mbuf(conn
, &mb
, NULL
, state
);
157 comm_read(conn
, state
->buf
, BUFSIZ
, Ident::ReadReply
, state
);
158 commSetTimeout(conn
->fd
, Ident::TheConfig
.timeout
, Ident::Timeout
, state
);
162 Ident::ReadReply(const Comm::ConnectionPointer
&conn
, char *buf
, size_t len
, comm_err_t flag
, int xerrno
, void *data
)
164 IdentStateData
*state
= (IdentStateData
*)data
;
168 assert(buf
== state
->buf
);
169 assert(conn
->fd
== state
->conn
->fd
);
171 if (flag
!= COMM_OK
|| len
<= 0) {
172 state
->conn
->close();
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
183 if ((t
= strchr(buf
, '\r')))
186 if ((t
= strchr(buf
, '\n')))
189 debugs(30, 5, HERE
<< conn
<< ": Read '" << buf
<< "'");
191 if (strstr(buf
, "USERID")) {
192 if ((ident
= strrchr(buf
, ':'))) {
193 while (xisspace(*++ident
));
194 Ident::identCallback(state
, ident
);
198 state
->conn
->close();
202 Ident::ClientAdd(IdentStateData
* state
, IDCB
* callback
, void *callback_data
)
204 IdentClient
*c
= (IdentClient
*)xcalloc(1, sizeof(*c
));
206 c
->callback
= callback
;
207 c
->callback_data
= cbdataReference(callback_data
);
209 for (C
= &state
->clients
; *C
; C
= &(*C
)->next
);
213 CBDATA_TYPE(IdentStateData
);
215 /**** PUBLIC FUNCTIONS ****/
218 * start a TCP connection to the peer host on port 113
221 Ident::Start(const Comm::ConnectionPointer
&conn
, IDCB
* callback
, void *data
)
223 IdentStateData
*state
;
224 char key1
[IDENT_KEY_SZ
];
225 char key2
[IDENT_KEY_SZ
];
226 char key
[IDENT_KEY_SZ
];
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
);
235 if ((state
= (IdentStateData
*)hash_lookup(ident_hash
, key
)) != NULL
) {
236 ClientAdd(state
, callback
, data
);
240 CBDATA_INIT_TYPE(IdentStateData
);
241 state
= cbdataAlloc(IdentStateData
);
242 state
->hash
.key
= xstrdup(key
);
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);
249 ClientAdd(state
, callback
, data
);
250 hash_join(ident_hash
, &state
->hash
);
252 AsyncCall::Pointer call
= commCbCall(30,3, "Ident::ConnectDone", CommConnectCbPtrFun(Ident::ConnectDone
, state
));
253 AsyncJob::Start(new Comm::ConnOpener(state
->conn
, call
, Ident::TheConfig
.timeout
));
260 debugs(30, DBG_CRITICAL
, "WARNING: Ident already initialized.");
264 ident_hash
= hash_create((HASHCMP
*) strcmp
,
265 hashPrime(Squid_MaxFD
/ 8),
269 #endif /* USE_IDENT */