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
|
/* Proxytunnel - (C) 2001-2008 Jos Visser / Mark Janssen */
/* Contact: josv@osp.nl / maniac@maniac.nl */
/*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* http.c */
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "proxytunnel.h"
#include "io.h"
#include "basicauth.h"
#include "ntlm.h"
/*
* Analyze the proxy's HTTP response. This must be a HTTP/1.? 200 OK type
* header
*/
void analyze_HTTP(PTSTREAM *pts) {
char *p = strtok( buf, " ");
/* Strip html error pages for faulty proxies (Stephane Engel <steph[at]macchiati.org>) */
while (strncmp( p, "HTTP/", 5) != 0 ) {
if ( readline(pts) ) {
p = strtok( buf, " ");
} else {
message( "analyze_HTTP: readline failed: Connection closed by remote host\n" );
exit(2);
}
}
if (strcmp( p, "HTTP/1.0" ) != 0 && strcmp( p, "HTTP/1.1" ) != 0) {
message( "Unsupported HTTP version number %s\n", p );
exit( 1 );
}
p = strtok( NULL, " ");
if( strcmp( p, "200" ) != 0 ) {
if( ! args_info.quiet_flag )
message( "HTTP return code: %s ", p );
p += strlen( p ) + 1;
if( ! args_info.quiet_flag )
message( "%s", p );
if (!ntlm_challenge && strcmp( p, "407") != 0) {
do {
readline(pts);
if (strncmp( buf, "Proxy-Authenticate: NTLM ", 25) == 0) {
if (parse_type2((unsigned char *)&buf[25]) < 0)
exit(1);
}
} while ( strcmp( buf, "\r\n" ) != 0 );
}
if (ntlm_challenge == 1) {
proxy_protocol(pts);
return;
}
exit( 1 );
}
}
/*
* Prints lines from a buffer prepended with a prefix
*/
void print_line_prefix(char *buf, char *prefix) {
buf = strdup(buf);
char *cur = strtok(buf, "\r\n");
while ( cur != NULL) {
message( "%s%s\n", prefix, cur );
cur = strtok(NULL, "\r\n");
}
// message( "%s: '%s\n", prefix, buf );
}
/*
* Execute the basic proxy protocol of CONNECT and response, until the
* last line of the response has been read. The tunnel is then open.
*/
void proxy_protocol(PTSTREAM *pts) {
/* Create the proxy CONNECT command into buf */
if (args_info.remproxy_given ) {
if( args_info.verbose_flag )
message( "\nTunneling to %s (remote proxy)\n", args_info.remproxy_arg );
sprintf( buf, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n", args_info.remproxy_arg, args_info.remproxy_arg );
} else {
if( args_info.verbose_flag )
message( "\nTunneling to %s (destination)\n", args_info.dest_arg );
sprintf( buf, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n", args_info.dest_arg, args_info.dest_arg );
}
if ( args_info.user_given && args_info.pass_given ) {
/* Create connect string including the authorization part */
if (args_info.ntlm_flag) {
if (ntlm_challenge == 1) {
build_type3_response();
strzcat( buf, "Proxy-Authorization: NTLM %s\r\n", ntlm_type3_buf );
} else if (ntlm_challenge == 0) {
strzcat( buf, "Proxy-Authorization: NTLM %s\r\n", ntlm_type1_buf );
}
} else {
strzcat( buf, "Proxy-Authorization: Basic %s\r\n", basicauth(args_info.user_arg, args_info.pass_arg ) );
}
}
strzcat( buf, "Proxy-Connection: Keep-Alive\r\n");
/* Add extra header(s), headers are already \r\n terminated */
if ( args_info.header_given )
strzcat( buf, "%s", args_info.header_arg );
strzcat( buf, "\r\n" );
/* Print the CONNECT instruction before sending to proxy */
if( args_info.verbose_flag ) {
message( "Communication with local proxy:\n");
print_line_prefix(buf, " -> ");
}
/* Send the CONNECT instruction to the proxy */
if( stream_write( pts, buf, strlen( buf )) < 0 ) {
my_perror( "Socket write error" );
exit( 1 );
}
// if( args_info.verbose_flag )
// message( "Data received from local proxy:\n");
if( args_info.wa_bug_29744_flag && !args_info.encryptremproxy_flag && pts->ssl ) {
message( "Switching to non-SSL communication (local proxy)\n");
pts->ssl = 0;
}
/* Read the first line of the response and analyze it */
analyze_HTTP(pts);
if (args_info.remproxy_given ) {
/* Clean buffer for next analysis */
while ( strcmp( buf, "\r\n" ) != 0 )
readline(pts);
/* If --encrypt-remproxy is specified, connect to the remote proxy using SSL */
if ( args_info.encryptremproxy_flag )
stream_enable_ssl(stunnel);
if( args_info.verbose_flag )
message( "\nTunneling to %s (destination)\n", args_info.dest_arg );
sprintf( buf, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n", args_info.dest_arg, args_info.dest_arg);
if ( args_info.remuser_given && args_info.rempass_given )
strzcat( buf, "Proxy-Authorization: Basic %s\r\n", basicauth(args_info.remuser_arg, args_info.rempass_arg ));
strzcat( buf, "Proxy-Connection: Keep-Alive\r\n");
/* Add extra header(s), headers are already \r\n terminated */
if ( args_info.header_given )
strzcat( buf, "%s", args_info.header_arg );
strzcat( buf, "\r\n" );
/* Print the CONNECT instruction before sending to proxy */
if( args_info.verbose_flag ) {
message( "Communication with remote proxy:\n");
print_line_prefix(buf, " -> ");
}
/* Send the CONNECT instruction to the proxy */
if( stream_write( pts, buf, strlen( buf )) < 0 ) {
my_perror( "Socket write error" );
exit( 1 );
}
// if( args_info.verbose_flag )
// message( "Received from remote proxy:\n");
if( args_info.wa_bug_29744_flag && pts->ssl ) {
message( "Switching to non-SSL communication (remote proxy)\n");
pts->ssl = 0;
}
/* Read the first line of the response and analyze it */
analyze_HTTP(pts);
}
/*
* Then, repeat reading lines of the responses until a blank line
* (which signifies the end of the response) is encountered.
*/
if (ntlm_challenge == 1) {
ntlm_challenge = 2;
} else {
do {
readline(pts);
} while ( strcmp( buf, "\r\n" ) != 0 );
}
}
// vim:noexpandtab:ts=4
|