forked from elia/authoxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnections.c
475 lines (431 loc) · 16.4 KB
/
connections.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* connections.c
* Authoxy
*
* Created by Heath Raftery on Mon Nov 11 2002.
* Copyright (c) 2002, 2003, 2004 HRSoftWorks. All rights reserved.
*
This file is part of Authoxy.
Authoxy is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Authoxy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Authoxy; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "AuthoxyDaemon.h"
//**************************************************************************
// conductSession(int clientConnection, char authStr[], int serverSocket, char logging, struct NTLMSettings *theNTLMSettings)
//
// split into two threads to conduct a two way HTTP session
//
//**************************************************************************
int conductSession(int clientConnection, char authStr[], int serverSocket, int logging, struct NTLMSettings *theNTLMSettings)
{
if(theNTLMSettings)
{
if(establishNTLMAuthentication(clientConnection, &serverSocket, logging, theNTLMSettings))
return -1;
else if(logging)
syslog(LOG_NOTICE, "Finished NTLM!");
}
pid_t pid;
switch(pid = fork()) //spawn a new process to handle the request
{
case -1: //trouble!!
syslog(LOG_ERR, "Fatal Error. Unable to create new process: %m");
close(clientConnection);
exit(1);
case 0: //the child
if( (authStr[0]!='\0' ?
conductClientSide(clientConnection, authStr, serverSocket, logging) :
conductClientSideDirectly(clientConnection, serverSocket, logging)) < 0)
{
if(logging)
syslog(LOG_NOTICE, "Client closed ungracefully. Killing session processes.");
kill(getppid(), SIGKILL);
exit(EXIT_FAILURE);
}
else
{
if(logging)
syslog(LOG_NOTICE, "Client closed connection, killing session processes.");
kill(getppid(), SIGKILL); //the client closed the connection, so kill the server connection as well
exit(EXIT_SUCCESS);
}
default: //the parent
if (conductServerSide(clientConnection, serverSocket, logging) < 0)
{
if(logging)
syslog(LOG_NOTICE, "Server closed ungracefully. Killing session processes.");
kill(pid, SIGKILL);
exit(EXIT_FAILURE);
}
else
{
if(logging)
syslog(LOG_NOTICE, "Server closed connection, killing session processes.");
kill(pid, SIGKILL); //the server closed the connection, so kill the client connection as well
exit(EXIT_SUCCESS);
}
}
close(serverSocket);
close(clientConnection);
return 1;
}
//**************************************************************************
// int handleConnection(int clientSocket)
//
// accept a connection on the clientSocket
//
//**************************************************************************
int handleConnection(int clientSocket)
{
int connection;
unsigned int sinSize = sizeof(struct sockaddr_in);
struct sockaddr_in clientAddr;
if((connection = accept(clientSocket, (struct sockaddr*) &clientAddr, &sinSize)) < 0) //wait for connection
{
syslog(LOG_ERR, "Fatal Error. Unable to accept connection on listen socket: %m");
return -1;
}
else
{
//syslog(LOG_NOTICE, "Accepted connection from:");
//syslog(LOG_NOTICE, inet_ntoa(clientAddr.sin_addr));
return connection;
}
}
//**************************************************************************
// int establishServerSide(char *hostname, unsigned short portnum, char direct=0)
//
// socket and connect to server
//
//**************************************************************************
int establishServerSide(char *hostname, unsigned short portnum)
{
//if hostname is NULL, these values will be used instead
static char lastServerConnected[256] = "";
static unsigned short lastPortConnected=0;
struct sockaddr_in talkSockAddr;
struct hostent *hp;
int talkSocket;
if((hp = gethostbyname(hostname ? hostname : lastServerConnected)) == NULL)
{
errno = ECONNREFUSED; //if we can't resolve the hostname, return -1.
return -1;
}
memset(&talkSockAddr, 0, sizeof(struct sockaddr_in));
memcpy((char *)&talkSockAddr.sin_addr, hp->h_addr, hp->h_length); /* set address */
talkSockAddr.sin_family = hp->h_addrtype;
talkSockAddr.sin_port = htons((u_short) (hostname ? portnum : lastPortConnected));
if((talkSocket = socket(hp->h_addrtype,SOCK_STREAM,0)) < 0) //establish talker socket
{
syslog(LOG_ERR, "Fatal Error. Unable to establish talker socket: %m");
return -1;
}
if(connect(talkSocket, (struct sockaddr *)&talkSockAddr, sizeof(struct sockaddr_in)) < 0) //connect to talker socket
{
close(talkSocket);
syslog(LOG_ERR, "Fatal Error. Unable to connect to talker socket: %m");
return -1;
}
if(hostname)
{
strcpy(lastServerConnected, hostname);
lastPortConnected = portnum;
}
return talkSocket;
}
//**************************************************************************
// int establishClientSide(int port, int maxpend)
//
// socket and bind to the client
//
//**************************************************************************
int establishClientSide(int port, int maxpend, char external)
{
int listenSocket;
struct sockaddr_in listenSockAddr;
int yes=1;
if((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) //open up a internet socket
{
syslog(LOG_ERR, "Fatal Error. Unable to establish listener socket: %m");
return -1;
}
memset(&listenSockAddr, 0, sizeof(struct sockaddr_in)); //clear the struct
listenSockAddr.sin_family = AF_INET;
listenSockAddr.sin_port = htons(port);
listenSockAddr.sin_addr.s_addr = external ? INADDR_ANY : inet_addr("127.0.0.1");
// lose the pesky "Address already in use" error message
if (setsockopt(listenSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
syslog(LOG_ERR, "Unable to set socket options. Bit strange that. Port may be blocked. Errno: %m");
if(bind(listenSocket, (struct sockaddr *) &listenSockAddr, sizeof(struct sockaddr)) < 0)
{
close(listenSocket);
syslog(LOG_ERR, "Fatal Error: unable to bind listen socket. Port probably has not been released. Wait a minute or so and try again. Errno: %m");
return -1;
}
if(listen(listenSocket, maxpend) < 0) //make it a listen port with maxpend max pending connections
{
close(listenSocket);
syslog(LOG_ERR, "Fatal Error. Unable to establish listen socket: %m");
return -1;
}
return listenSocket;
}
//**************************************************************************
// void conductClientSide(int clientConnection, char authStr[], int serverSocket, char logging)
//
// loop continuously, pushing data from the client to the server
//
//**************************************************************************
#define CR1st 1
#define LF1st 2
#define CR2nd 3
#define LF2nd 4
int conductClientSide(int clientConnection, char authStr[], int serverSocket, int logging)
{
while(1)
{
int recvBufSize=0, bytesSent, i, j, connectionClosed=0, endHeader=0;
char listenBuf[INCOMING_BUF_SIZE+strlen(authStr)+1], *tempBuf=NULL;
char debugBuf[81];
int CRLFCRLF=0; //value to determine if the CRLFCRLF has been seen yet
while((recvBufSize = recv(clientConnection, listenBuf, INCOMING_BUF_SIZE, 0)) > 0) //retrieve the data on the listen socket
{
if(endHeader && (//first time we enter this, we know it is a header and don't need to do this checking. Otherwise...
(listenBuf[0]=='G' && listenBuf[1]=='E' && listenBuf[2]=='T') ||
(listenBuf[0]=='C' && listenBuf[1]=='O' && listenBuf[2]=='N' && listenBuf[3]=='N' && listenBuf[4]=='E' && listenBuf[5]=='C' && listenBuf[6]=='T') ||
(listenBuf[0]=='P' && listenBuf[1]=='O' && listenBuf[2]=='S' && listenBuf[3]=='T') ||
(listenBuf[0]=='O' && listenBuf[1]=='P' && listenBuf[2]=='T' && listenBuf[3]=='I' && listenBuf[4]=='O' && listenBuf[5]=='N' && listenBuf[6]=='S') ||
(listenBuf[0]=='H' && listenBuf[1]=='E' && listenBuf[2]=='A' && listenBuf[3]=='D') ||
(listenBuf[0]=='P' && listenBuf[1]=='U' && listenBuf[2]=='T') ||
(listenBuf[0]=='D' && listenBuf[1]=='E' && listenBuf[2]=='L' && listenBuf[3]=='E' && listenBuf[4]=='T' && listenBuf[5]=='E') ||
(listenBuf[0]=='T' && listenBuf[1]=='R' && listenBuf[2]=='A' && listenBuf[3]=='C' && listenBuf[4]=='E'))
)//list of headers above from RFC2616, pg 24
endHeader=0; //looks like a new header
// syslog(LOG_NOTICE, "just recv'ed");
//Right, we've got some data, check to see if it has a CRLFCRLF
if(!endHeader)
{
for(i=0; i<recvBufSize; i++)
{
if(CRLFCRLF == CR2nd && listenBuf[i]==LF) //found it!
{
CRLFCRLF = LF2nd;
//we've found the end of the headers! Lets insert our little auth string.
tempBuf = (char*) malloc((recvBufSize-i-1)*sizeof(char));
memcpy(tempBuf, &listenBuf[i+1], recvBufSize-i-1);
if (i==0)
{
memcpy(listenBuf + i, authStr, strlen(authStr));
memcpy(listenBuf + i+strlen(authStr), tempBuf, recvBufSize-i-1);
}
else
{
memcpy(listenBuf + i-1, authStr, strlen(authStr));
memcpy(listenBuf + i-1+strlen(authStr), tempBuf, recvBufSize-i-1);
}
free(tempBuf);
recvBufSize += strlen(authStr)-2; //-2 for the CRLF which gets taken off the old end of headers
endHeader=1;
break;
}
else if(CRLFCRLF == LF1st && listenBuf[i]==CR)
{
CRLFCRLF = CR2nd;
if(i==recvBufSize-1) //then damn, looks like the second CRLF crosses the buffer limit.
recvBufSize--; //We're going to have to assume another LF is coming, and make sure we don't print the CR.
}
else if(CRLFCRLF == CR1st && listenBuf[i]==LF)
CRLFCRLF = LF1st;
else if(listenBuf[i]==CR)
CRLFCRLF = CR1st;
else
CRLFCRLF = 0;
}
if(logging == LOGGING)
{
logging=0; //only make a log entry once per connection
i=0; j=0;
while(i<80 && i<recvBufSize && listenBuf[i] != '\r' && listenBuf[i] != '\n')
{
if(listenBuf[i] >= ' ' && listenBuf[i] <= '~') //is a legit printable character
{
if(listenBuf[i]=='%') //we must escape it!
debugBuf[j++] = '%';
debugBuf[j++] = listenBuf[i];
}
i++;
}
debugBuf[i] = '\0';
if(i>4)
syslog(LOG_NOTICE, debugBuf);
}
}
//here's a change: the send has moved inside the recv loop,
//meaning we send the data _as_ we get it, rather than at the end
//removes _lots_ of complexity associated with parsing the data as it came through
//we are much more transparent now
bytesSent=0;
while(bytesSent<recvBufSize)
{
if((bytesSent += send(serverSocket, &listenBuf[bytesSent], recvBufSize-bytesSent, 0)) < 0)
{
syslog(LOG_NOTICE, "Couldn't send to talk connection: %m");
return -1;
}
// syslog(LOG_NOTICE, "just sent this");
// syslog(LOG_NOTICE, listenBuf);
}
if(logging == TESTING)
logClientToServer(listenBuf, bytesSent);
}
if(recvBufSize==0) //connection has been closed
connectionClosed = 1;
else //recvBufSize<0 - an error occured
{
if(errno==54) //that's the connection reset by peer error. Strangely, IE seems to do it all the time. I can't see why. RFC2616, Ch8.1 has the details on persistent connections, and I can't see why the connection wouldn't be dropped gracefully instead of this 'forceful drop by the server' we see.
{
// syslog(LOG_NOTICE, "Connection reset by peer, closing connection.");
close(clientConnection);
close(serverSocket);
}
else
syslog(LOG_NOTICE, "Odd packet received. Ignoring. Errno: %m");
return -1;
}
if(connectionClosed)
{
// syslog(LOG_NOTICE, "Connection closed by client.");
close(clientConnection);
close(serverSocket);
return 0; //there is nothing more we can do, no client to talk to!
}
}
return 1; //should be unreachable of course
}
//**************************************************************************
// int conductClientSideDirectly(int clientConnection, int serverSocket, char logging)
//
// loop continuously, pushing data from the client to the server
//
//**************************************************************************
int conductClientSideDirectly(int clientConnection, int serverSocket, int logging)
{
int recvBufSize=0, bytesSent, directed=0;
char listenBuf[INCOMING_BUF_SIZE+1];
if(logging)
syslog(LOG_NOTICE, "Handling a direct connection");
while((recvBufSize = recv(clientConnection, listenBuf, INCOMING_BUF_SIZE, 0)) > 0) //retrieve the data on the listen socket
{
if(!directed)
{
int i, startOfAddress, endOfAddress;
for(i=3; i<recvBufSize; i++)
{
if(listenBuf[i]==':')
{
startOfAddress=i;
while(listenBuf[startOfAddress-1]!=' ')
startOfAddress--;
endOfAddress=i+3;
while(listenBuf[endOfAddress]!='/')
endOfAddress++;
int j;
for(i=startOfAddress, j=endOfAddress; j<recvBufSize; i++, j++)
listenBuf[i] = listenBuf[j];
recvBufSize-=(endOfAddress-startOfAddress);
directed=1;
break;
}
else if(listenBuf[i]=='/' || listenBuf[i]==CR || listenBuf[i]==LF)
{
directed=1;
break;
}
}
}
bytesSent=0;
while(bytesSent<recvBufSize)
{
if((bytesSent += send(serverSocket, &listenBuf[bytesSent], recvBufSize-bytesSent, 0)) < 0)
{
if(logging)
syslog(LOG_NOTICE, "Couldn't send direct to server: %m");
return -1;
}
}
if(logging == TESTING)
logClientToServer(listenBuf, recvBufSize);
}
if(recvBufSize==0) //connection has been closed
{
close(clientConnection);
close(serverSocket);
return 0; //there is nothing more we can do, no client to talk to!
}
else //recvBufSize<0 - an error occured
{
if(errno==54)
{
close(clientConnection);
close(serverSocket);
}
else if(logging)
syslog(LOG_NOTICE, "Odd packet received direct from server. Ignoring. Errno: %m");
return -1;
}
}
//**************************************************************************
// int conductServerSide(int clientConnection, int serverSocket, char logging)
//
// loop continuously, pushing data from the server to the client
//
//**************************************************************************
int conductServerSide(int clientConnection, int serverSocket, int logging)
{
while(1)
{
int recvBufSize;
char listenBuf[INCOMING_BUF_SIZE+1];
while((recvBufSize = recv(serverSocket, listenBuf, INCOMING_BUF_SIZE, 0)) > 0)
{
int sentChars=0;
// listenBuf[recvBufSize] = '\0';
// syslog(LOG_NOTICE, "About to send back data");
// syslog(LOG_NOTICE, listenBuf);
while(sentChars<recvBufSize)
{
if((sentChars+=send(clientConnection, &listenBuf[sentChars], recvBufSize-sentChars, 0)) < 0)
{
if(logging)
syslog(LOG_NOTICE, "Couldn't send to listen connection: %m");
close(serverSocket);
close(clientConnection);
return -1;
}
}
if(logging == TESTING)
logServerToClient(listenBuf, recvBufSize);
}
if(recvBufSize < 0)
{
if(logging)
syslog(LOG_NOTICE, "Unexpected closure of server socket: %m");
close(serverSocket);
close(clientConnection);
return -1;
}
// syslog(LOG_NOTICE, "Connection closed by server.");
close(serverSocket);
close(clientConnection);
return 0;
}
}