| 1 |
/* |
| 2 |
* stunnel TLS offloading and load-balancing proxy |
| 3 |
* Copyright (C) 1998-2026 Michal Trojnara <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the |
| 7 |
* Free Software Foundation; either version 2 of the License, or (at your |
| 8 |
* option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, see <http://www.gnu.org/licenses>. |
| 17 |
* |
| 18 |
* Linking stunnel statically or dynamically with other modules is making |
| 19 |
* a combined work based on stunnel. Thus, the terms and conditions of |
| 20 |
* the GNU General Public License cover the whole combination. |
| 21 |
* |
| 22 |
* In addition, as a special exception, the copyright holder of stunnel |
| 23 |
* gives you permission to combine stunnel with free software programs or |
| 24 |
* libraries that are released under the GNU LGPL and with code included |
| 25 |
* in the standard release of OpenSSL under the OpenSSL License (or |
| 26 |
* modified versions of such code, with unchanged license). You may copy |
| 27 |
* and distribute such a system following the terms of the GNU GPL for |
| 28 |
* stunnel and the licenses of the other code concerned. |
| 29 |
* |
| 30 |
* Note that people who make modified versions of stunnel are not obligated |
| 31 |
* to grant this special exception for their modified versions; it is their |
| 32 |
* choice whether to do so. The GNU General Public License gives permission |
| 33 |
* to release a modified version without this exception; this exception |
| 34 |
* also makes it possible to release a modified version which carries |
| 35 |
* forward this exception. |
| 36 |
*/ |
| 37 |
|
| 38 |
#include "prototypes.h" |
| 39 |
|
| 40 |
#define CAPWIN_BUFFER_SIZE 100 |
| 41 |
|
| 42 |
/* protocol-specific function prototypes */ |
| 43 |
NOEXPORT void socks_client_late(CLI *); |
| 44 |
NOEXPORT void socks5_client_method(CLI *); |
| 45 |
NOEXPORT void socks5_client_address(CLI *); |
| 46 |
NOEXPORT const char *socks_server_init(SERVICE_OPTIONS *); |
| 47 |
NOEXPORT void socks_server_middle(CLI *); |
| 48 |
NOEXPORT void socks_server_late(CLI *); |
| 49 |
NOEXPORT void socks4_server(CLI *); |
| 50 |
NOEXPORT void socks5_server_method(CLI *); |
| 51 |
NOEXPORT void socks5_server(CLI *); |
| 52 |
NOEXPORT int validate_connect_addr(CLI *); |
| 53 |
|
| 54 |
NOEXPORT void proxy_server_late(CLI *); |
| 55 |
|
| 56 |
NOEXPORT void cifs_client_middle(CLI *); |
| 57 |
NOEXPORT void cifs_server_early(CLI *); |
| 58 |
|
| 59 |
NOEXPORT void pgsql_client_middle(CLI *); |
| 60 |
NOEXPORT void pgsql_server_early(CLI *); |
| 61 |
|
| 62 |
NOEXPORT void smtp_client_middle(CLI *); |
| 63 |
NOEXPORT void smtp_client_late(CLI *); |
| 64 |
NOEXPORT void smtp_client_negotiate(CLI *); |
| 65 |
NOEXPORT void smtp_client_plain(CLI *, const char *, const char *); |
| 66 |
NOEXPORT void smtp_client_login(CLI *, const char *, const char *); |
| 67 |
NOEXPORT const char *smtp_server_init(SERVICE_OPTIONS *); |
| 68 |
NOEXPORT void smtp_server_middle(CLI *); |
| 69 |
|
| 70 |
NOEXPORT void pop3_client_middle(CLI *); |
| 71 |
NOEXPORT const char *pop3_server_init(SERVICE_OPTIONS *); |
| 72 |
NOEXPORT void pop3_server_middle(CLI *); |
| 73 |
|
| 74 |
NOEXPORT void imap_client_middle(CLI *); |
| 75 |
NOEXPORT const char *imap_server_init(SERVICE_OPTIONS *); |
| 76 |
NOEXPORT void imap_server_middle(CLI *); |
| 77 |
|
| 78 |
NOEXPORT void nntp_client_middle(CLI *); |
| 79 |
|
| 80 |
NOEXPORT void ldap_client_middle(CLI *); |
| 81 |
|
| 82 |
NOEXPORT void connect_server_early(CLI *); |
| 83 |
NOEXPORT const char *connect_client_init(SERVICE_OPTIONS *); |
| 84 |
NOEXPORT void connect_client_middle(CLI *); |
| 85 |
#ifndef OPENSSL_NO_MD4 |
| 86 |
NOEXPORT void ntlm(CLI *); |
| 87 |
NOEXPORT char *ntlm1(void); |
| 88 |
NOEXPORT char *ntlm3(char *, char *, char *, char *); |
| 89 |
NOEXPORT void crypt_DES(DES_cblock, const_DES_cblock, unsigned char[7]); |
| 90 |
#endif |
| 91 |
NOEXPORT char *base64(int, const char *, int); |
| 92 |
|
| 93 |
NOEXPORT void capwin_server_middle(CLI *); |
| 94 |
NOEXPORT void capwin_server_late(CLI *); |
| 95 |
NOEXPORT void capwin_client_late(CLI *); |
| 96 |
NOEXPORT const char *capwinctrl_client_init(SERVICE_OPTIONS *); |
| 97 |
NOEXPORT void capwinctrl_client_early(CLI *); |
| 98 |
NOEXPORT int capwin_decode(const char *, char **, char **, char **, char **); |
| 99 |
NOEXPORT int ldap_auth(CLI *, const char *, const char *); |
| 100 |
NOEXPORT char *ldap_escape_dn(const char *); |
| 101 |
|
| 102 |
/* global state */ |
| 103 |
NOEXPORT char capwin_auth[CAPWIN_BUFFER_SIZE]={0}; |
| 104 |
#ifdef USE_WIN32 |
| 105 |
HWND capwin_hwnd=NULL; |
| 106 |
LONG capwin_connectivity=0; |
| 107 |
#endif |
| 108 |
|
| 109 |
/**************************************** public interface */ |
| 110 |
|
| 111 |
const char *protocol_init(SERVICE_OPTIONS *opt) { |
| 112 |
typedef struct { |
| 113 |
const char *(*init)(SERVICE_OPTIONS *); |
| 114 |
void (*early)(CLI *); |
| 115 |
void (*middle)(CLI *); |
| 116 |
void (*late)(CLI *); |
| 117 |
} MODE; |
| 118 |
typedef struct { |
| 119 |
const char *name; |
| 120 |
MODE client, server; |
| 121 |
} PROTOCOLS; |
| 122 |
const PROTOCOLS protocols[]={ |
| 123 |
{.name="socks", |
| 124 |
.client={.late=socks_client_late}, |
| 125 |
.server={.init=socks_server_init, .middle=socks_server_middle, .late=socks_server_late}}, |
| 126 |
{.name="proxy", |
| 127 |
.server={.late=proxy_server_late}}, |
| 128 |
{.name="cifs", |
| 129 |
.client={.middle=cifs_client_middle}, |
| 130 |
.server={.early=cifs_server_early}}, |
| 131 |
{.name="pgsql", |
| 132 |
.client={.middle=pgsql_client_middle}, |
| 133 |
.server={.early=pgsql_server_early}}, |
| 134 |
{.name="smtp", |
| 135 |
.client={.middle=smtp_client_middle, .late=smtp_client_late}, |
| 136 |
.server={.init=smtp_server_init, .middle=smtp_server_middle}}, |
| 137 |
{.name="pop3", |
| 138 |
.client={.middle=pop3_client_middle}, |
| 139 |
.server={.init=pop3_server_init, .middle=pop3_server_middle}}, |
| 140 |
{.name="imap", |
| 141 |
.client={.middle=imap_client_middle}, |
| 142 |
.server={.init=imap_server_init, .middle=imap_server_middle}}, |
| 143 |
{.name="nntp", |
| 144 |
.client={.middle=nntp_client_middle}}, |
| 145 |
{.name="ldap", |
| 146 |
.client={.middle=ldap_client_middle}}, |
| 147 |
{.name="connect", |
| 148 |
.client={.init=connect_client_init, .middle=connect_client_middle}, |
| 149 |
.server={.early=connect_server_early}}, |
| 150 |
{.name="capwin", |
| 151 |
.client={.late=capwin_client_late}, |
| 152 |
.server={.middle=capwin_server_middle, .late=capwin_server_late}}, |
| 153 |
{.name="capwinctrl", |
| 154 |
.client={.init=capwinctrl_client_init, .early=capwinctrl_client_early}}, |
| 155 |
{.name=NULL} |
| 156 |
}, *p; |
| 157 |
|
| 158 |
/* the default values to be overridden in protocol initialization */ |
| 159 |
opt->option.connect_before_ssl=opt->option.client; |
| 160 |
opt->option.protocol_endpoint=0; |
| 161 |
|
| 162 |
if(!opt->protocol) { /* no protocol specified */ |
| 163 |
opt->protocol_early=NULL; |
| 164 |
opt->protocol_middle=NULL; |
| 165 |
opt->protocol_late=NULL; |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
|
| 169 |
for(p=protocols; p->name; p++) { |
| 170 |
if(!strcasecmp(p->name, opt->protocol)) { |
| 171 |
const MODE *m=opt->option.client ? &p->client : &p->server; |
| 172 |
if(!m->init && !m->early && !m->middle && !m->late) { |
| 173 |
if(opt->option.client) |
| 174 |
return "The configured protocol is not supported in the client mode"; |
| 175 |
else |
| 176 |
return "The configured protocol is not supported in the server mode"; |
| 177 |
} |
| 178 |
if(m->init) { |
| 179 |
const char *err=m->init(opt); |
| 180 |
if(err) |
| 181 |
return err; |
| 182 |
} |
| 183 |
opt->protocol_early=m->early; |
| 184 |
opt->protocol_middle=m->middle; |
| 185 |
opt->protocol_late=m->late; |
| 186 |
return NULL; /* success */ |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return "Protocol not supported"; |
| 191 |
} |
| 192 |
|
| 193 |
/**************************************** socks */ |
| 194 |
|
| 195 |
/* SOCKS over TLS (SOCKS protocol itself is also encrypted) */ |
| 196 |
|
| 197 |
typedef union { |
| 198 |
struct { |
| 199 |
uint8_t ver, cmd, rsv, atyp; |
| 200 |
} req; |
| 201 |
struct { |
| 202 |
uint8_t ver, rep, rsv, atyp; |
| 203 |
} resp; |
| 204 |
struct { |
| 205 |
uint8_t ver, code, rsv, atyp, addr[4], port[2]; |
| 206 |
} v4; |
| 207 |
struct { |
| 208 |
uint8_t ver, code, rsv, atyp, len, data[255+2]; /* data: name+port */ |
| 209 |
} host; |
| 210 |
struct { |
| 211 |
uint8_t ver, code, rsv, atyp, addr[16], port[2]; |
| 212 |
} v6; |
| 213 |
} SOCKS5_UNION; |
| 214 |
|
| 215 |
NOEXPORT void socks_client_late(CLI *c) { |
| 216 |
socks5_client_method(c); |
| 217 |
socks5_client_address(c); |
| 218 |
} |
| 219 |
|
| 220 |
NOEXPORT void socks5_client_method(CLI *c) { |
| 221 |
const struct { |
| 222 |
uint8_t ver, nmethods, method; |
| 223 |
} req={5, 1, 0x00}; /* NO AUTHENTICATION REQUIRED */ |
| 224 |
struct { |
| 225 |
uint8_t ver, method; |
| 226 |
} resp; |
| 227 |
|
| 228 |
s_ssl_write(c, &req, sizeof req); |
| 229 |
|
| 230 |
s_ssl_read(c, &resp, sizeof resp); |
| 231 |
if(resp.ver!=5) { |
| 232 |
s_log(LOG_ERR, "Invalid SOCKS5 message version 0x%02x", resp.ver); |
| 233 |
throw_exception(c, 2); /* don't reset */ |
| 234 |
} |
| 235 |
/* TODO: add USERNAME/PASSWORD authentication */ |
| 236 |
if(resp.method!=0x00) { |
| 237 |
s_log(LOG_ERR, "No supported SOCKS5 authentication method received"); |
| 238 |
throw_exception(c, 2); /* don't reset */ |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
NOEXPORT void socks5_client_address(CLI *c) { |
| 243 |
SOCKADDR_UNION addr; |
| 244 |
SOCKS5_UNION socks; |
| 245 |
|
| 246 |
memset(&socks, 0, sizeof socks); |
| 247 |
socks.req.ver=5; /* SOCKS5 */ |
| 248 |
socks.req.cmd=0x01; /* CONNECT */ |
| 249 |
|
| 250 |
if(c->opt->protocol_host) { /* explicit destination */ |
| 251 |
char *tmp_str, *host_str, *port_str; |
| 252 |
size_t host_len, offset; |
| 253 |
u_short port_num; |
| 254 |
struct addrinfo hints, *result=NULL; |
| 255 |
int error; |
| 256 |
|
| 257 |
/* parse c->opt->protocol_host */ |
| 258 |
socks.req.atyp=0x03; /* DOMAINNAME */ |
| 259 |
tmp_str=strrchr(c->opt->protocol_host, ':'); |
| 260 |
if(tmp_str) { |
| 261 |
host_str=c->opt->protocol_host; |
| 262 |
host_len=(size_t)(tmp_str - host_str); |
| 263 |
port_str=tmp_str+1; |
| 264 |
} else { |
| 265 |
host_str="localhost"; |
| 266 |
host_len=strlen(host_str); |
| 267 |
port_str=c->opt->protocol_host; |
| 268 |
} |
| 269 |
|
| 270 |
/* set the host name */ |
| 271 |
if(host_len > 0xff) { |
| 272 |
s_log(LOG_ERR, "protocolHost too long"); |
| 273 |
throw_exception(c, 2); /* don't reset */ |
| 274 |
} |
| 275 |
socks.host.len=(uint8_t)host_len; |
| 276 |
memcpy(socks.host.data, host_str, host_len); |
| 277 |
offset=host_len; |
| 278 |
|
| 279 |
/* set the port number */ |
| 280 |
memset(&hints, 0, sizeof(hints)); |
| 281 |
hints.ai_family=AF_UNSPEC; /* both IPv4 and IPv6 */ |
| 282 |
hints.ai_socktype=SOCK_STREAM; /* TCP */ |
| 283 |
error=getaddrinfo(NULL, port_str, &hints, &result); |
| 284 |
if(error || !result) { |
| 285 |
s_log(LOG_ERR, "Invalid port: %s (error: %s)", port_str, |
| 286 |
#ifdef USE_WIN32 |
| 287 |
gai_strerrorA(error) |
| 288 |
#else |
| 289 |
gai_strerror(error) |
| 290 |
#endif |
| 291 |
); |
| 292 |
throw_exception(c, 2); /* don't reset */ |
| 293 |
} |
| 294 |
port_num=ntohs(((struct sockaddr_in *)result->ai_addr)->sin_port); |
| 295 |
freeaddrinfo(result); |
| 296 |
socks.host.data[offset++]=(uint8_t)(port_num>>8); /* MSB */ |
| 297 |
socks.host.data[offset++]=(uint8_t)(port_num&0xff); /* LSB */ |
| 298 |
|
| 299 |
s_log(LOG_INFO, "Sending SOCKS5 DOMAINNAME"); |
| 300 |
s_ssl_write(c, &socks, (int)(offsetof(SOCKS5_UNION, host.data)+offset)); |
| 301 |
} else { /* transparent destination */ |
| 302 |
if(original_dst(c->local_rfd.fd, &addr)) |
| 303 |
throw_exception(c, 2); /* don't reset */ |
| 304 |
switch(addr.sa.sa_family) { |
| 305 |
case AF_INET: |
| 306 |
socks.req.atyp=0x01; /* IP v4 address */ |
| 307 |
memcpy(&socks.v4.addr, &addr.in.sin_addr, 4); |
| 308 |
memcpy(&socks.v4.port, &addr.in.sin_port, 2); |
| 309 |
s_log(LOG_INFO, "Sending SOCKS5 IPv4 address"); |
| 310 |
s_ssl_write(c, &socks, sizeof socks.v4); |
| 311 |
break; |
| 312 |
#ifdef USE_IPV6 |
| 313 |
case AF_INET6: |
| 314 |
socks.req.atyp=0x04; /* IP v6 address */ |
| 315 |
memcpy(&socks.v6.addr, &addr.in6.sin6_addr, 16); |
| 316 |
memcpy(&socks.v6.port, &addr.in6.sin6_port, 2); |
| 317 |
s_log(LOG_INFO, "Sending SOCKS5 IPv6 address"); |
| 318 |
s_ssl_write(c, &socks, sizeof socks.v6); |
| 319 |
break; |
| 320 |
#endif |
| 321 |
default: |
| 322 |
s_log(LOG_ERR, "Unsupported address type 0x%02x", addr.sa.sa_family); |
| 323 |
throw_exception(c, 2); /* don't reset */ |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
s_ssl_read(c, &socks, sizeof socks.resp); |
| 328 |
if(socks.resp.atyp==0x04) /* IP V6 address */ |
| 329 |
s_ssl_read(c, &socks.v6.addr, 16+2); |
| 330 |
else |
| 331 |
s_ssl_read(c, &socks.v4.addr, 4+2); |
| 332 |
if(socks.resp.ver!=5) { |
| 333 |
s_log(LOG_ERR, "Invalid SOCKS5 message version 0x%02x", socks.req.ver); |
| 334 |
throw_exception(c, 2); /* don't reset */ |
| 335 |
} |
| 336 |
switch(socks.resp.rep) { |
| 337 |
case 0x00: |
| 338 |
s_log(LOG_INFO, |
| 339 |
"SOCKS5 request succeeded"); |
| 340 |
return; /* SUCCESS */ |
| 341 |
case 0x01: |
| 342 |
s_log(LOG_ERR, |
| 343 |
"SOCKS5 request failed: General SOCKS server failure"); |
| 344 |
break; |
| 345 |
case 0x02: |
| 346 |
s_log(LOG_ERR, |
| 347 |
"SOCKS5 request failed: Connection not allowed by ruleset"); |
| 348 |
break; |
| 349 |
case 0x03: |
| 350 |
s_log(LOG_ERR, |
| 351 |
"SOCKS5 request failed: Network unreachable"); |
| 352 |
break; |
| 353 |
case 0x04: |
| 354 |
s_log(LOG_ERR, |
| 355 |
"SOCKS5 request failed: Host unreachable"); |
| 356 |
break; |
| 357 |
case 0x05: |
| 358 |
s_log(LOG_ERR, |
| 359 |
"SOCKS5 request failed: Connection refused"); |
| 360 |
break; |
| 361 |
case 0x06: |
| 362 |
s_log(LOG_ERR, |
| 363 |
"SOCKS5 request failed: TTL expired"); |
| 364 |
break; |
| 365 |
case 0x07: |
| 366 |
s_log(LOG_ERR, |
| 367 |
"SOCKS5 request failed: Command not supported"); |
| 368 |
break; |
| 369 |
case 0x08: |
| 370 |
s_log(LOG_ERR, |
| 371 |
"SOCKS5 request failed: Address type not supported"); |
| 372 |
break; |
| 373 |
default: |
| 374 |
s_log(LOG_ERR, |
| 375 |
"SOCKS5 request failed: Unknown error 0x%02x", socks.resp.rep); |
| 376 |
} |
| 377 |
throw_exception(c, 2); /* don't reset */ |
| 378 |
} |
| 379 |
|
| 380 |
NOEXPORT const char *socks_server_init(SERVICE_OPTIONS *opt) { |
| 381 |
opt->option.protocol_endpoint=1; |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
NOEXPORT void socks_server_middle(CLI *c) { |
| 386 |
uint8_t version; |
| 387 |
|
| 388 |
s_log(LOG_DEBUG, "Waiting for the SOCKS request"); |
| 389 |
s_ssl_read(c, &version, sizeof version); |
| 390 |
switch(version) { |
| 391 |
case 4: |
| 392 |
socks4_server(c); |
| 393 |
break; |
| 394 |
case 5: |
| 395 |
socks5_server_method(c); |
| 396 |
socks5_server(c); |
| 397 |
break; |
| 398 |
default: |
| 399 |
s_log(LOG_ERR, "Unsupported SOCKS version 0x%02x", version); |
| 400 |
throw_exception(c, 1); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
NOEXPORT void socks_server_late(CLI *c) { |
| 405 |
(void)c; /* squash the unused parameter warning */ |
| 406 |
/* TODO: send the SOCKS reply *after* the target is connected */ |
| 407 |
/* FIXME: the SOCKS replies do not report CONNECT failures */ |
| 408 |
/* FIXME: the SOCKS replies do not contain the bound IP address */ |
| 409 |
} |
| 410 |
|
| 411 |
/* SOCKS4 or SOCKS4a */ |
| 412 |
/* BIND command is not supported */ |
| 413 |
/* USERID parameter is ignored */ |
| 414 |
|
| 415 |
NOEXPORT void socks4_server(CLI *c) { |
| 416 |
struct { |
| 417 |
uint8_t vn, cd; |
| 418 |
u_short sin_port; |
| 419 |
struct in_addr sin_addr; |
| 420 |
} socks; |
| 421 |
char *user_name, *host_name, *port_name; |
| 422 |
SOCKADDR_UNION addr; |
| 423 |
int close_connection=1; |
| 424 |
|
| 425 |
memset(&socks, 0, sizeof socks); |
| 426 |
s_ssl_read(c, &socks.cd, sizeof socks-sizeof socks.vn); |
| 427 |
socks.vn=0; /* response version 0 */ |
| 428 |
user_name=ssl_getstring(c); /* ignore the username */ |
| 429 |
str_free(user_name); |
| 430 |
|
| 431 |
if(socks.cd==0x01) { /* CONNECT */ |
| 432 |
if(ntohl(socks.sin_addr.s_addr)>0 && |
| 433 |
ntohl(socks.sin_addr.s_addr)<256) { /* 0.0.0.x */ |
| 434 |
host_name=ssl_getstring(c); |
| 435 |
port_name=str_printf("%u", ntohs(socks.sin_port)); |
| 436 |
hostport2addrlist(&c->connect_addr, host_name, port_name); |
| 437 |
str_free(port_name); |
| 438 |
if(c->connect_addr.num) { |
| 439 |
s_log(LOG_INFO, "SOCKS4a resolved \"%s\" to %u host(s)", |
| 440 |
host_name, c->connect_addr.num); |
| 441 |
if(validate_connect_addr(c)) { |
| 442 |
socks.cd=90; /* access granted */ |
| 443 |
close_connection=0; |
| 444 |
} else { |
| 445 |
socks.cd=91; /* rejected */ |
| 446 |
} |
| 447 |
} else { |
| 448 |
s_log(LOG_ERR, "SOCKS4a failed to resolve \"%s\"", host_name); |
| 449 |
socks.cd=91; /* failed */ |
| 450 |
} |
| 451 |
str_free(host_name); |
| 452 |
} else { |
| 453 |
c->connect_addr.num=1; |
| 454 |
c->connect_addr.addr=str_alloc(sizeof(SOCKADDR_UNION)); |
| 455 |
c->connect_addr.addr[0].in.sin_family=AF_INET; |
| 456 |
c->connect_addr.addr[0].in.sin_port=socks.sin_port; |
| 457 |
c->connect_addr.addr[0].in.sin_addr.s_addr=socks.sin_addr.s_addr; |
| 458 |
s_log(LOG_INFO, "SOCKS4 address received"); |
| 459 |
if(validate_connect_addr(c)) { |
| 460 |
socks.cd=90; /* access granted */ |
| 461 |
close_connection=0; |
| 462 |
} else { |
| 463 |
socks.cd=91; /* rejected */ |
| 464 |
} |
| 465 |
} |
| 466 |
} else if(socks.cd==0xf0) { /* RESOLVE (a TOR extension) */ |
| 467 |
host_name=ssl_getstring(c); |
| 468 |
if(hostport2addr(&addr, host_name, "0", 0) && addr.sa.sa_family==AF_INET) { |
| 469 |
memcpy(&socks.sin_addr, &addr.in.sin_addr, 4); |
| 470 |
s_log(LOG_INFO, "SOCKS4a/TOR resolved \"%s\"", host_name); |
| 471 |
socks.cd=90; /* access granted */ |
| 472 |
} else { |
| 473 |
s_log(LOG_ERR, "SOCKS4a/TOR failed to resolve \"%s\"", host_name); |
| 474 |
socks.cd=91; /* failed */ |
| 475 |
} |
| 476 |
str_free(host_name); |
| 477 |
} else { |
| 478 |
s_log(LOG_ERR, "Unsupported SOCKS4/SOCKS4a command 0x%02x", socks.cd); |
| 479 |
socks.cd=91; /* failed */ |
| 480 |
} |
| 481 |
s_ssl_write(c, &socks, sizeof socks); |
| 482 |
if(close_connection) |
| 483 |
throw_exception(c, 2); /* don't reset */ |
| 484 |
} |
| 485 |
|
| 486 |
NOEXPORT void socks5_server_method(CLI *c) { |
| 487 |
uint8_t nmethods, *methods; |
| 488 |
struct { |
| 489 |
uint8_t ver, method; |
| 490 |
} response; |
| 491 |
int i; |
| 492 |
|
| 493 |
response.ver=0x05; |
| 494 |
response.method=0xff; /* NO ACCEPTABLE METHODS */ |
| 495 |
s_ssl_read(c, &nmethods, sizeof nmethods); |
| 496 |
methods=str_alloc(nmethods); |
| 497 |
s_ssl_read(c, methods, nmethods); |
| 498 |
for(i=0; i<nmethods; ++i) |
| 499 |
if(methods[i]==0x00) { /* NO AUTHENTICATION REQUIRED */ |
| 500 |
response.method=0x00; /* use this method */ |
| 501 |
break; |
| 502 |
} |
| 503 |
str_free(methods); |
| 504 |
s_ssl_write(c, &response, sizeof response); |
| 505 |
if(response.method) { /* request failed */ |
| 506 |
s_log(LOG_ERR, "No supported SOCKS5 authentication method received"); |
| 507 |
throw_exception(c, 2); /* don't reset */ |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
/* CONNECT does not return valid BND.ADDR and BND.PORT values */ |
| 512 |
|
| 513 |
NOEXPORT void socks5_server(CLI *c) { |
| 514 |
SOCKS5_UNION socks; |
| 515 |
uint8_t host_len; |
| 516 |
char *host_name, *port_name; |
| 517 |
u_short port_number; |
| 518 |
SOCKADDR_UNION addr; |
| 519 |
int close_connection=1; |
| 520 |
|
| 521 |
/* parse request */ |
| 522 |
memset(&socks, 0, sizeof socks); |
| 523 |
s_ssl_read(c, &socks, sizeof socks.req); |
| 524 |
if(socks.req.ver!=0x05) { |
| 525 |
s_log(LOG_ERR, "Invalid SOCKS5 message version 0x%02x", socks.req.ver); |
| 526 |
socks.resp.ver=0x05; /* response version 5 */ |
| 527 |
socks.resp.rep=0x01; /* general SOCKS server failure */ |
| 528 |
} else if(socks.req.cmd==0x01) { /* CONNECT */ |
| 529 |
if(socks.req.atyp==0x01) { /* IP v4 address */ |
| 530 |
c->connect_addr.num=1; |
| 531 |
c->connect_addr.addr=str_alloc(sizeof(SOCKADDR_UNION)); |
| 532 |
c->connect_addr.addr[0].in.sin_family=AF_INET; |
| 533 |
s_ssl_read(c, &socks.v4.addr, 4+2); |
| 534 |
memcpy(&c->connect_addr.addr[0].in.sin_addr, &socks.v4.addr, 4); |
| 535 |
memcpy(&c->connect_addr.addr[0].in.sin_port, &socks.v4.port, 2); |
| 536 |
s_log(LOG_INFO, "SOCKS5 IPv4 address received"); |
| 537 |
if(validate_connect_addr(c)) { |
| 538 |
socks.resp.rep=0x00; /* succeeded */ |
| 539 |
close_connection=0; |
| 540 |
} else { |
| 541 |
socks.resp.rep=0x02; /* connection not allowed by ruleset */ |
| 542 |
} |
| 543 |
} else if(socks.req.atyp==0x03) { /* DOMAINNAME */ |
| 544 |
s_ssl_read(c, &host_len, sizeof host_len); |
| 545 |
host_name=str_alloc((size_t)host_len+1); |
| 546 |
s_ssl_read(c, host_name, host_len); |
| 547 |
host_name[host_len]='\0'; |
| 548 |
s_ssl_read(c, &port_number, 2); |
| 549 |
port_name=str_printf("%u", ntohs(port_number)); |
| 550 |
hostport2addrlist(&c->connect_addr, host_name, port_name); |
| 551 |
str_free(port_name); |
| 552 |
if(c->connect_addr.num) { |
| 553 |
s_log(LOG_INFO, "SOCKS5 resolved \"%s\" to %u host(s)", |
| 554 |
host_name, c->connect_addr.num); |
| 555 |
if(validate_connect_addr(c)) { |
| 556 |
socks.resp.rep=0x00; /* succeeded */ |
| 557 |
close_connection=0; |
| 558 |
} else { |
| 559 |
socks.resp.rep=0x02; /* connection not allowed by ruleset */ |
| 560 |
} |
| 561 |
} else { |
| 562 |
s_log(LOG_ERR, "SOCKS5 failed to resolve \"%s\"", host_name); |
| 563 |
socks.resp.rep=0x04; /* Host unreachable */ |
| 564 |
} |
| 565 |
str_free(host_name); |
| 566 |
#ifdef USE_IPV6 |
| 567 |
} else if(socks.req.atyp==0x04) { /* IP v6 address */ |
| 568 |
c->connect_addr.num=1; |
| 569 |
c->connect_addr.addr=str_alloc(sizeof(SOCKADDR_UNION)); |
| 570 |
c->connect_addr.addr[0].in6.sin6_family=AF_INET6; |
| 571 |
s_ssl_read(c, &socks.v6.addr, 16+2); |
| 572 |
memcpy(&c->connect_addr.addr[0].in6.sin6_addr, &socks.v6.addr, 16); |
| 573 |
memcpy(&c->connect_addr.addr[0].in6.sin6_port, &socks.v6.port, 2); |
| 574 |
s_log(LOG_INFO, "SOCKS5 IPv6 address received"); |
| 575 |
if(validate_connect_addr(c)) { |
| 576 |
socks.resp.rep=0x00; /* succeeded */ |
| 577 |
close_connection=0; |
| 578 |
} else { |
| 579 |
socks.resp.rep=0x02; /* connection not allowed by ruleset */ |
| 580 |
} |
| 581 |
#endif |
| 582 |
} else { |
| 583 |
s_log(LOG_ERR, |
| 584 |
"Unsupported SOCKS5 address type 0x%02x", socks.req.atyp); |
| 585 |
socks.resp.rep=0x07; /* Address type not supported */ |
| 586 |
} |
| 587 |
} else if(socks.req.cmd==0xf0) { /* RESOLVE (a TOR extension) */ |
| 588 |
s_ssl_read(c, &host_len, sizeof host_len); |
| 589 |
host_name=str_alloc((size_t)host_len+1); |
| 590 |
s_ssl_read(c, host_name, host_len); |
| 591 |
host_name[host_len]='\0'; |
| 592 |
s_ssl_read(c, &port_number, 2); |
| 593 |
port_name=str_printf("%u", ntohs(port_number)); |
| 594 |
if(hostport2addr(&addr, host_name, port_name, 0)) { |
| 595 |
if(addr.sa.sa_family==AF_INET) { |
| 596 |
s_log(LOG_INFO, "SOCKS5/TOR resolved \"%s\" to IPv4", host_name); |
| 597 |
memcpy(&socks.v4.addr, &addr.in.sin_addr, 4); |
| 598 |
socks.resp.atyp=0x01; /* IP v4 address */ |
| 599 |
socks.resp.rep=0x00; /* succeeded */ |
| 600 |
#ifdef USE_IPV6 |
| 601 |
} else if(addr.sa.sa_family==AF_INET6) { |
| 602 |
s_log(LOG_INFO, "SOCKS5/TOR resolved \"%s\" to IPv6", host_name); |
| 603 |
memcpy(&socks.v6.addr, &addr.in6.sin6_addr, 16); |
| 604 |
socks.resp.atyp=0x04; /* IP v6 address */ |
| 605 |
socks.resp.rep=0x00; /* succeeded */ |
| 606 |
#endif |
| 607 |
} else { |
| 608 |
s_log(LOG_ERR, "SOCKS5/TOR unsupported address type for \"%s\"", |
| 609 |
host_name); |
| 610 |
socks.resp.rep=0x04; /* Host unreachable */ |
| 611 |
} |
| 612 |
} else { |
| 613 |
s_log(LOG_ERR, "SOCKS5/TOR failed to resolve \"%s\"", host_name); |
| 614 |
socks.resp.rep=0x04; /* Host unreachable */ |
| 615 |
} |
| 616 |
str_free(host_name); |
| 617 |
str_free(port_name); |
| 618 |
} else { |
| 619 |
s_log(LOG_ERR, "Unsupported SOCKS5 command 0x%02x", socks.req.cmd); |
| 620 |
socks.resp.rep=0x07; /* Command not supported */ |
| 621 |
} |
| 622 |
|
| 623 |
/* send response */ |
| 624 |
/* broken clients tend to expect the same address family for response, |
| 625 |
* so stunnel tries to preserve the address family if possible */ |
| 626 |
if(socks.resp.atyp==0x04) { /* IP V6 address */ |
| 627 |
s_ssl_write(c, &socks, sizeof socks.v6); |
| 628 |
} else { |
| 629 |
socks.resp.atyp=0x01; /* IP v4 address */ |
| 630 |
s_ssl_write(c, &socks, sizeof socks.v4); |
| 631 |
} |
| 632 |
if(close_connection) /* request failed */ |
| 633 |
throw_exception(c, 2); /* don't reset */ |
| 634 |
} |
| 635 |
|
| 636 |
/* validate the allocated address */ |
| 637 |
NOEXPORT int validate_connect_addr(CLI *c) { |
| 638 |
#ifdef USE_IPV6 |
| 639 |
const unsigned char ipv6_loopback[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; |
| 640 |
#endif |
| 641 |
unsigned i; |
| 642 |
|
| 643 |
for(i=0; i<c->connect_addr.num; ++i) { |
| 644 |
SOCKADDR_UNION *addr=&c->connect_addr.addr[i]; |
| 645 |
#ifdef USE_IPV6 |
| 646 |
if(addr->sa.sa_family==AF_INET6) { |
| 647 |
if(!memcmp(&addr->in6.sin6_addr, ipv6_loopback, 16)) { |
| 648 |
s_log(LOG_ERR, |
| 649 |
"SOCKS connection to the IPv6 loopback rejected"); |
| 650 |
return 0; |
| 651 |
} |
| 652 |
/* TODO: implement more checks */ |
| 653 |
} else |
| 654 |
#endif |
| 655 |
if(addr->sa.sa_family==AF_INET) { |
| 656 |
if((ntohl(addr->in.sin_addr.s_addr)&0xff000000)==0x7f000000) { |
| 657 |
s_log(LOG_ERR, |
| 658 |
"SOCKS connection to the IPv4 loopback rejected"); |
| 659 |
return 0; |
| 660 |
} |
| 661 |
/* TODO: implement more checks */ |
| 662 |
} else { |
| 663 |
s_log(LOG_ERR, "Unsupported address type 0x%02x", |
| 664 |
addr->sa.sa_family); |
| 665 |
return 0; |
| 666 |
} |
| 667 |
} |
| 668 |
return 1; |
| 669 |
} |
| 670 |
|
| 671 |
/**************************************** proxy */ |
| 672 |
|
| 673 |
/* |
| 674 |
* PROXY protocol: http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt |
| 675 |
* this is a protocol client support for stunnel acting as an TLS server |
| 676 |
* I don't think anything else is useful, but feel free to discuss on the |
| 677 |
* stunnel-users mailing list if you disagree |
| 678 |
*/ |
| 679 |
|
| 680 |
/* IP address textual representation length */ |
| 681 |
/* 1234:6789:1234:6789:1234:6789:1234:6789 -> 40 chars with '\0' */ |
| 682 |
#define IP_LEN 40 |
| 683 |
#define PORT_LEN 6 |
| 684 |
|
| 685 |
NOEXPORT void proxy_server_late(CLI *c) { |
| 686 |
SOCKADDR_UNION addr; |
| 687 |
socklen_t addrlen; |
| 688 |
char src_host[IP_LEN], dst_host[IP_LEN]; |
| 689 |
char src_port[PORT_LEN], dst_port[PORT_LEN]; |
| 690 |
const char *proto; |
| 691 |
int err; |
| 692 |
|
| 693 |
addrlen=sizeof addr; |
| 694 |
if(getpeername(c->local_rfd.fd, &addr.sa, &addrlen)) { |
| 695 |
sockerror("getpeername"); |
| 696 |
throw_exception(c, 1); |
| 697 |
} |
| 698 |
err=getnameinfo(&addr.sa, addr_len(&addr), src_host, IP_LEN, |
| 699 |
src_port, PORT_LEN, NI_NUMERICHOST|NI_NUMERICSERV); |
| 700 |
if(err) { |
| 701 |
s_log(LOG_ERR, "getnameinfo: %s", s_gai_strerror(err)); |
| 702 |
throw_exception(c, 1); |
| 703 |
} |
| 704 |
|
| 705 |
addrlen=sizeof addr; |
| 706 |
if(getsockname(c->local_rfd.fd, &addr.sa, &addrlen)) { |
| 707 |
sockerror("getsockname"); |
| 708 |
throw_exception(c, 1); |
| 709 |
} |
| 710 |
err=getnameinfo(&addr.sa, addr_len(&addr), dst_host, IP_LEN, |
| 711 |
dst_port, PORT_LEN, NI_NUMERICHOST|NI_NUMERICSERV); |
| 712 |
if(err) { |
| 713 |
s_log(LOG_ERR, "getnameinfo: %s", s_gai_strerror(err)); |
| 714 |
throw_exception(c, 1); |
| 715 |
} |
| 716 |
|
| 717 |
switch(addr.sa.sa_family) { |
| 718 |
case AF_INET: |
| 719 |
proto="TCP4"; |
| 720 |
break; |
| 721 |
#ifdef USE_IPV6 |
| 722 |
case AF_INET6: |
| 723 |
proto="TCP6"; |
| 724 |
break; |
| 725 |
#endif |
| 726 |
default: /* AF_UNIX */ |
| 727 |
proto="UNKNOWN"; |
| 728 |
} |
| 729 |
fd_printf(c, c->remote_fd.fd, "PROXY %s %s %s %s %s", |
| 730 |
proto, src_host, dst_host, src_port, dst_port); |
| 731 |
} |
| 732 |
|
| 733 |
/**************************************** cifs */ |
| 734 |
|
| 735 |
NOEXPORT void cifs_client_middle(CLI *c) { |
| 736 |
uint8_t buffer[5]; |
| 737 |
uint8_t request_dummy[4]={0x81, 0, 0, 0}; /* a zero-length request */ |
| 738 |
|
| 739 |
s_write(c, c->remote_fd.fd, request_dummy, 4); |
| 740 |
s_read(c, c->remote_fd.fd, buffer, 5); |
| 741 |
if(buffer[0]!=0x83) { /* NB_SSN_NEGRESP */ |
| 742 |
s_log(LOG_ERR, "Negative response expected"); |
| 743 |
throw_exception(c, 1); |
| 744 |
} |
| 745 |
if(buffer[2]!=0 || buffer[3]!=1) { /* length != 1 */ |
| 746 |
s_log(LOG_ERR, "Unexpected NetBIOS response size"); |
| 747 |
throw_exception(c, 1); |
| 748 |
} |
| 749 |
if(buffer[4]!=0x8e) { /* use TLS */ |
| 750 |
s_log(LOG_ERR, "Remote server does not require TLS"); |
| 751 |
throw_exception(c, 1); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
NOEXPORT void cifs_server_early(CLI *c) { |
| 756 |
uint8_t buffer[128]; |
| 757 |
uint8_t response_access_denied[5]={0x83, 0, 0, 1, 0x81}; |
| 758 |
uint8_t response_use_ssl[5]={0x83, 0, 0, 1, 0x8e}; |
| 759 |
uint16_t len; |
| 760 |
|
| 761 |
s_read(c, c->local_rfd.fd, buffer, 4); /* NetBIOS header */ |
| 762 |
len=(uint16_t)(((uint16_t)(buffer[2])<<8)|buffer[3]); |
| 763 |
if(len>sizeof buffer-4) { |
| 764 |
s_log(LOG_ERR, "Received block too long"); |
| 765 |
throw_exception(c, 1); |
| 766 |
} |
| 767 |
s_read(c, c->local_rfd.fd, buffer+4, len); |
| 768 |
if(buffer[0]!=0x81) { /* NB_SSN_REQUEST */ |
| 769 |
s_log(LOG_ERR, "Client did not send session setup"); |
| 770 |
s_write(c, c->local_wfd.fd, response_access_denied, 5); |
| 771 |
throw_exception(c, 1); |
| 772 |
} |
| 773 |
s_write(c, c->local_wfd.fd, response_use_ssl, 5); |
| 774 |
} |
| 775 |
|
| 776 |
/**************************************** pgsql */ |
| 777 |
|
| 778 |
/* http://www.postgresql.org/docs/8.3/static/protocol-flow.html#AEN73982 */ |
| 779 |
static const uint8_t ssl_request[8]={0, 0, 0, 8, 0x04, 0xd2, 0x16, 0x2f}; |
| 780 |
|
| 781 |
NOEXPORT void pgsql_client_middle(CLI *c) { |
| 782 |
uint8_t buffer[1]; |
| 783 |
|
| 784 |
s_write(c, c->remote_fd.fd, ssl_request, sizeof ssl_request); |
| 785 |
s_read(c, c->remote_fd.fd, buffer, 1); |
| 786 |
/* S - accepted, N - rejected, non-TLS preferred */ |
| 787 |
if(buffer[0]!='S') { |
| 788 |
s_log(LOG_ERR, "PostgreSQL server rejected TLS"); |
| 789 |
throw_exception(c, 1); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
NOEXPORT void pgsql_server_early(CLI *c) { |
| 794 |
uint8_t buffer[8], ssl_ok[1]={'S'}; |
| 795 |
/* https://www.postgresql.org/docs/current/protocol-message-formats.html */ |
| 796 |
static const uint8_t gss_request[8]={0, 0, 0, 8, 0x04, 0xd2, 0x16, 0x30}; |
| 797 |
static const uint8_t gss_response[62]= |
| 798 |
{'E', 0, 0, 0, 61, 'S', 'E', 'R', 'R', 'O', 'R', 0, 'C', 'X', 'X', '0', |
| 799 |
'0', '0', 0, 'M', 'S', 'S', 'L', ' ', 'e', 'x', 'p', 'e', 'c', 't', 'e', 'd', |
| 800 |
' ', 'b', 'u', 't', ' ', 'n', 'o', 't', ' ', 'r', 'e', 'q', 'u', 'e', 's', 't', |
| 801 |
'e', 'd', ' ', 'b', 'y', ' ', 'c', 'l', 'i', 'e', 'n', 't', 0, 0}; |
| 802 |
|
| 803 |
s_log(LOG_DEBUG, "Started server-side psql protocol negotiation"); |
| 804 |
memset(buffer, 0, sizeof buffer); |
| 805 |
s_read(c, c->local_rfd.fd, buffer, sizeof buffer); |
| 806 |
if(!safe_memcmp(buffer, gss_request, sizeof gss_request)) { |
| 807 |
s_log(LOG_INFO, "GSSAPI encryption requested, rejecting gracefully"); |
| 808 |
s_write(c, c->local_wfd.fd, gss_response, sizeof gss_response); |
| 809 |
throw_exception(c, 2); /* don't reset */ |
| 810 |
} |
| 811 |
if(safe_memcmp(buffer, ssl_request, sizeof ssl_request)) { |
| 812 |
s_log(LOG_ERR, "PostgreSQL client did not request TLS, rejecting"); |
| 813 |
/* no way to send error on startup, so just drop the client */ |
| 814 |
throw_exception(c, 1); |
| 815 |
} |
| 816 |
s_log(LOG_DEBUG, "SSLRequest received"); |
| 817 |
s_write(c, c->local_wfd.fd, ssl_ok, sizeof ssl_ok); |
| 818 |
} |
| 819 |
|
| 820 |
/**************************************** smtp */ |
| 821 |
|
| 822 |
NOEXPORT void smtp_client_middle(CLI *c) { |
| 823 |
smtp_client_negotiate(c); |
| 824 |
} |
| 825 |
|
| 826 |
NOEXPORT void smtp_client_late(CLI *c) { |
| 827 |
if(c->opt->protocol_username && c->opt->protocol_password) { |
| 828 |
char *line; |
| 829 |
|
| 830 |
ssl_printf(c, "EHLO %s", |
| 831 |
c->opt->protocol_host ? c->opt->protocol_host : "localhost"); |
| 832 |
line=ssl_getline(c); /* ignore the reply */ |
| 833 |
while(is_prefix(line, "250-")) { |
| 834 |
str_free(line); |
| 835 |
line=ssl_getline(c); |
| 836 |
} |
| 837 |
str_free(line); |
| 838 |
if(!strcasecmp(c->opt->protocol_authentication, "LOGIN")) |
| 839 |
smtp_client_login(c, |
| 840 |
c->opt->protocol_username, c->opt->protocol_password); |
| 841 |
else /* use PLAIN by default */ |
| 842 |
smtp_client_plain(c, |
| 843 |
c->opt->protocol_username, c->opt->protocol_password); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
NOEXPORT void smtp_client_negotiate(CLI *c) { |
| 848 |
char *line; |
| 849 |
|
| 850 |
line=str_dup(""); |
| 851 |
do { /* copy multiline greeting */ |
| 852 |
str_free(line); |
| 853 |
line=fd_getline(c, c->remote_fd.fd); |
| 854 |
fd_putline(c, c->local_wfd.fd, line); |
| 855 |
} while(is_prefix(line, "220-")); |
| 856 |
|
| 857 |
fd_printf(c, c->remote_fd.fd, "EHLO %s", |
| 858 |
c->opt->protocol_host ? c->opt->protocol_host : "localhost"); |
| 859 |
do { /* skip multiline reply */ |
| 860 |
str_free(line); |
| 861 |
line=fd_getline(c, c->remote_fd.fd); |
| 862 |
} while(is_prefix(line, "250-")); |
| 863 |
if(!is_prefix(line, "250 ")) { /* error */ |
| 864 |
s_log(LOG_ERR, "Remote server is not RFC 1425 compliant"); |
| 865 |
str_free(line); |
| 866 |
throw_exception(c, 1); |
| 867 |
} |
| 868 |
|
| 869 |
fd_putline(c, c->remote_fd.fd, "STARTTLS"); |
| 870 |
do { /* skip multiline reply */ |
| 871 |
str_free(line); |
| 872 |
line=fd_getline(c, c->remote_fd.fd); |
| 873 |
} while(is_prefix(line, "220-")); |
| 874 |
if(!is_prefix(line, "220 ")) { /* error */ |
| 875 |
s_log(LOG_ERR, "Remote server is not RFC 2487 compliant"); |
| 876 |
str_free(line); |
| 877 |
throw_exception(c, 1); |
| 878 |
} |
| 879 |
str_free(line); |
| 880 |
} |
| 881 |
|
| 882 |
/* http://www.samlogic.net/articles/smtp-commands-reference-auth.htm */ |
| 883 |
|
| 884 |
NOEXPORT void smtp_client_plain(CLI *c, const char *user, const char *pass) { |
| 885 |
char *line, *encoded; |
| 886 |
|
| 887 |
line=str_printf("%c%s%c%s", '\0', user, '\0', pass); |
| 888 |
encoded=base64(1, line, (int)strlen(user) + (int)strlen(pass) + 2); |
| 889 |
if(!encoded) { |
| 890 |
s_log(LOG_ERR, "Base64 encoder failed"); |
| 891 |
str_free(line); |
| 892 |
throw_exception(c, 1); |
| 893 |
} |
| 894 |
str_free(line); |
| 895 |
line=str_printf("AUTH PLAIN %s", encoded); |
| 896 |
str_free(encoded); |
| 897 |
ssl_putline(c, line); |
| 898 |
str_free(line); |
| 899 |
|
| 900 |
line=ssl_getline(c); |
| 901 |
if(!is_prefix(line, "235 ")) { /* not 'Authentication successful' */ |
| 902 |
s_log(LOG_ERR, "PLAIN Authentication Failed"); |
| 903 |
str_free(line); |
| 904 |
throw_exception(c, 1); |
| 905 |
} |
| 906 |
str_free(line); |
| 907 |
} |
| 908 |
|
| 909 |
NOEXPORT void smtp_client_login(CLI *c, const char *user, const char *pass) { |
| 910 |
char *line, *encoded; |
| 911 |
|
| 912 |
ssl_putline(c, "AUTH LOGIN"); |
| 913 |
line=ssl_getline(c); |
| 914 |
if(!is_prefix(line, "334 ")) { /* not the username challenge */ |
| 915 |
s_log(LOG_ERR, "Remote server does not support LOGIN authentication"); |
| 916 |
str_free(line); |
| 917 |
throw_exception(c, 1); |
| 918 |
} |
| 919 |
str_free(line); |
| 920 |
|
| 921 |
encoded=base64(1, user, (int)strlen(user)); |
| 922 |
if(!encoded) { |
| 923 |
s_log(LOG_ERR, "Base64 encoder failed"); |
| 924 |
throw_exception(c, 1); |
| 925 |
} |
| 926 |
ssl_putline(c, encoded); |
| 927 |
str_free(encoded); |
| 928 |
line=ssl_getline(c); |
| 929 |
if(!is_prefix(line, "334 ")) { /* not the password challenge */ |
| 930 |
s_log(LOG_ERR, "LOGIN authentication failed"); |
| 931 |
str_free(line); |
| 932 |
throw_exception(c, 1); |
| 933 |
} |
| 934 |
str_free(line); |
| 935 |
|
| 936 |
encoded=base64(1, pass, (int)strlen(pass)); |
| 937 |
if(!encoded) { |
| 938 |
s_log(LOG_ERR, "Base64 encoder failed"); |
| 939 |
throw_exception(c, 1); |
| 940 |
} |
| 941 |
ssl_putline(c, encoded); |
| 942 |
str_free(encoded); |
| 943 |
line=ssl_getline(c); |
| 944 |
if(!is_prefix(line, "235 ")) { /* not 'Authentication successful' */ |
| 945 |
s_log(LOG_ERR, "LOGIN authentication failed"); |
| 946 |
str_free(line); |
| 947 |
throw_exception(c, 1); |
| 948 |
} |
| 949 |
str_free(line); |
| 950 |
} |
| 951 |
|
| 952 |
NOEXPORT const char *smtp_server_init(SERVICE_OPTIONS *opt) { |
| 953 |
opt->option.connect_before_ssl=1; /* c->remote_fd needed */ |
| 954 |
return NULL; |
| 955 |
} |
| 956 |
|
| 957 |
NOEXPORT void smtp_server_middle(CLI *c) { |
| 958 |
char *line, *domain, *greeting; |
| 959 |
|
| 960 |
/* detect RFC 2487 */ |
| 961 |
s_poll_init(c->fds, 0); |
| 962 |
s_poll_add(c->fds, c->local_rfd.fd, 1, 0); |
| 963 |
switch(s_poll_wait(c->fds, 0, 200)) { /* wait up to 200ms */ |
| 964 |
case 0: /* fd not ready to read */ |
| 965 |
s_log(LOG_DEBUG, "RFC 2487 detected"); |
| 966 |
break; |
| 967 |
case 1: /* fd ready to read */ |
| 968 |
s_log(LOG_DEBUG, "RFC 2487 not detected"); |
| 969 |
return; /* return if RFC 2487 is not used */ |
| 970 |
default: /* -1 */ |
| 971 |
sockerror("RFC2487 (s_poll_wait)"); |
| 972 |
throw_exception(c, 1); |
| 973 |
} |
| 974 |
|
| 975 |
/* process server's greeting */ |
| 976 |
line=fd_getline(c, c->remote_fd.fd); |
| 977 |
if(!(is_prefix(line, "220 ") || is_prefix(line, "220-"))) { |
| 978 |
s_log(LOG_ERR, "Unknown server welcome"); |
| 979 |
str_free(line); |
| 980 |
throw_exception(c, 1); |
| 981 |
} |
| 982 |
domain=str_dup(line+4); /* skip "220" and the separator */ |
| 983 |
line[4]='\0'; /* only leave "220" and the separator */ |
| 984 |
greeting=strchr(domain, ' '); |
| 985 |
if(greeting) { |
| 986 |
*greeting++='\0'; /* truncate anything after the domain name */ |
| 987 |
fd_printf(c, c->local_wfd.fd, "%s%s stunnel for %s", |
| 988 |
line, domain, greeting); |
| 989 |
} else { |
| 990 |
fd_printf(c, c->local_wfd.fd, "%s%s stunnel", line, domain); |
| 991 |
} |
| 992 |
while(is_prefix(line, "220-")) { /* copy multiline greeting */ |
| 993 |
str_free(line); |
| 994 |
line=fd_getline(c, c->remote_fd.fd); |
| 995 |
fd_putline(c, c->local_wfd.fd, line); |
| 996 |
} |
| 997 |
str_free(line); |
| 998 |
|
| 999 |
/* process client's EHLO */ |
| 1000 |
line=fd_getline(c, c->local_rfd.fd); |
| 1001 |
if(!is_prefix(line, "EHLO ")) { |
| 1002 |
s_log(LOG_ERR, "Unknown client EHLO"); |
| 1003 |
str_free(line); |
| 1004 |
str_free(domain); |
| 1005 |
throw_exception(c, 1); |
| 1006 |
} |
| 1007 |
str_free(line); |
| 1008 |
fd_printf(c, c->local_wfd.fd, "250-%s", domain); |
| 1009 |
str_free(domain); |
| 1010 |
fd_putline(c, c->local_wfd.fd, "250 STARTTLS"); |
| 1011 |
|
| 1012 |
/* process client's STARTTLS */ |
| 1013 |
line=fd_getline(c, c->local_rfd.fd); |
| 1014 |
if(!is_prefix(line, "STARTTLS")) { |
| 1015 |
s_log(LOG_ERR, "STARTTLS expected"); |
| 1016 |
str_free(line); |
| 1017 |
throw_exception(c, 1); |
| 1018 |
} |
| 1019 |
fd_putline(c, c->local_wfd.fd, "220 Go ahead"); |
| 1020 |
str_free(line); |
| 1021 |
} |
| 1022 |
|
| 1023 |
/**************************************** pop3 */ |
| 1024 |
|
| 1025 |
NOEXPORT void pop3_client_middle(CLI *c) { |
| 1026 |
char *line; |
| 1027 |
|
| 1028 |
line=fd_getline(c, c->remote_fd.fd); |
| 1029 |
if(!is_prefix(line, "+OK ")) { |
| 1030 |
s_log(LOG_ERR, "Unknown server welcome"); |
| 1031 |
str_free(line); |
| 1032 |
throw_exception(c, 1); |
| 1033 |
} |
| 1034 |
fd_putline(c, c->local_wfd.fd, line); |
| 1035 |
fd_putline(c, c->remote_fd.fd, "STLS"); |
| 1036 |
str_free(line); |
| 1037 |
line=fd_getline(c, c->remote_fd.fd); |
| 1038 |
if(!is_prefix(line, "+OK ")) { |
| 1039 |
s_log(LOG_ERR, "Server does not support TLS"); |
| 1040 |
str_free(line); |
| 1041 |
throw_exception(c, 1); |
| 1042 |
} |
| 1043 |
str_free(line); |
| 1044 |
} |
| 1045 |
|
| 1046 |
NOEXPORT const char *pop3_server_init(SERVICE_OPTIONS *opt) { |
| 1047 |
opt->option.connect_before_ssl=1; /* c->remote_fd needed */ |
| 1048 |
return NULL; |
| 1049 |
} |
| 1050 |
|
| 1051 |
NOEXPORT void pop3_server_middle(CLI *c) { |
| 1052 |
char *line=fd_getline(c, c->remote_fd.fd); |
| 1053 |
fd_printf(c, c->local_wfd.fd, "%s + stunnel", line); |
| 1054 |
str_free(line); |
| 1055 |
line=fd_getline(c, c->local_rfd.fd); |
| 1056 |
if(is_prefix(line, "CAPA")) { /* client wants RFC 2449 extensions */ |
| 1057 |
fd_putline(c, c->local_wfd.fd, "+OK Stunnel capability list follows"); |
| 1058 |
fd_putline(c, c->local_wfd.fd, "STLS"); |
| 1059 |
fd_putline(c, c->local_wfd.fd, "."); |
| 1060 |
str_free(line); |
| 1061 |
line=fd_getline(c, c->local_rfd.fd); |
| 1062 |
} |
| 1063 |
if(!is_prefix(line, "STLS")) { |
| 1064 |
s_log(LOG_ERR, "Client does not want TLS"); |
| 1065 |
str_free(line); |
| 1066 |
throw_exception(c, 1); |
| 1067 |
} |
| 1068 |
str_free(line); |
| 1069 |
fd_putline(c, c->local_wfd.fd, "+OK Stunnel starts TLS negotiation"); |
| 1070 |
} |
| 1071 |
|
| 1072 |
/**************************************** imap */ |
| 1073 |
|
| 1074 |
NOEXPORT void imap_client_middle(CLI *c) { |
| 1075 |
char *line=fd_getline(c, c->remote_fd.fd); |
| 1076 |
if(!is_prefix(line, "* OK")) { |
| 1077 |
s_log(LOG_ERR, "Unknown server welcome"); |
| 1078 |
str_free(line); |
| 1079 |
throw_exception(c, 1); |
| 1080 |
} |
| 1081 |
str_free(line); |
| 1082 |
fd_putline(c, c->local_wfd.fd, "* OK Connected."); |
| 1083 |
fd_putline(c, c->remote_fd.fd, "stunnel STARTTLS"); |
| 1084 |
line=fd_getline(c, c->remote_fd.fd); |
| 1085 |
if(!is_prefix(line, "stunnel OK")) { |
| 1086 |
fd_putline(c, c->local_wfd.fd, |
| 1087 |
"* BYE stunnel: Server does not support TLS"); |
| 1088 |
s_log(LOG_ERR, "Server does not support TLS"); |
| 1089 |
str_free(line); |
| 1090 |
throw_exception(c, 2); /* don't reset */ |
| 1091 |
} |
| 1092 |
str_free(line); |
| 1093 |
} |
| 1094 |
|
| 1095 |
NOEXPORT const char *imap_server_init(SERVICE_OPTIONS *opt) { |
| 1096 |
opt->option.connect_before_ssl=1; /* c->remote_fd needed */ |
| 1097 |
return NULL; |
| 1098 |
} |
| 1099 |
|
| 1100 |
NOEXPORT void imap_server_middle(CLI *c) { |
| 1101 |
char *line, *id, *tail, *capa; |
| 1102 |
|
| 1103 |
s_poll_init(c->fds, 0); |
| 1104 |
s_poll_add(c->fds, c->local_rfd.fd, 1, 0); |
| 1105 |
switch(s_poll_wait(c->fds, 0, 200)) { |
| 1106 |
case 0: /* fd not ready to read */ |
| 1107 |
s_log(LOG_DEBUG, "RFC 2595 detected"); |
| 1108 |
break; |
| 1109 |
case 1: /* fd ready to read */ |
| 1110 |
s_log(LOG_DEBUG, "RFC 2595 not detected"); |
| 1111 |
return; /* return if RFC 2595 is not used */ |
| 1112 |
default: /* -1 */ |
| 1113 |
sockerror("RFC2595 (s_poll_wait)"); |
| 1114 |
throw_exception(c, 1); |
| 1115 |
} |
| 1116 |
|
| 1117 |
/* process server welcome and send it to client */ |
| 1118 |
line=fd_getline(c, c->remote_fd.fd); |
| 1119 |
if(!is_prefix(line, "* OK")) { |
| 1120 |
s_log(LOG_ERR, "Unknown server welcome"); |
| 1121 |
str_free(line); |
| 1122 |
throw_exception(c, 1); |
| 1123 |
} |
| 1124 |
capa=strstr(line, "CAPABILITY"); |
| 1125 |
if(!capa) |
| 1126 |
capa=strstr(line, "capability"); |
| 1127 |
if(capa) |
| 1128 |
*capa='K'; /* disable CAPABILITY within greeting */ |
| 1129 |
fd_printf(c, c->local_wfd.fd, "%s (stunnel)", line); |
| 1130 |
|
| 1131 |
id=str_dup(""); |
| 1132 |
while(1) { /* process client commands */ |
| 1133 |
str_free(line); |
| 1134 |
line=fd_getline(c, c->local_rfd.fd); |
| 1135 |
/* split line into id and tail */ |
| 1136 |
str_free(id); |
| 1137 |
id=str_dup(line); |
| 1138 |
tail=strchr(id, ' '); |
| 1139 |
if(!tail) |
| 1140 |
break; |
| 1141 |
*tail++='\0'; |
| 1142 |
|
| 1143 |
if(is_prefix(tail, "STARTTLS")) { |
| 1144 |
fd_printf(c, c->local_wfd.fd, |
| 1145 |
"%s OK Begin TLS negotiation now", id); |
| 1146 |
str_free(line); |
| 1147 |
str_free(id); |
| 1148 |
return; /* success */ |
| 1149 |
} else if(is_prefix(tail, "CAPABILITY")) { |
| 1150 |
fd_putline(c, c->remote_fd.fd, line); /* send it to server */ |
| 1151 |
str_free(line); |
| 1152 |
line=fd_getline(c, c->remote_fd.fd); /* get the capabilities */ |
| 1153 |
if(*line=='*') { |
| 1154 |
/* |
| 1155 |
* append STARTTLS |
| 1156 |
* should also add LOGINDISABLED, but can't because |
| 1157 |
* of Mozilla bug #324138/#312009 |
| 1158 |
* LOGIN would fail as "unexpected command", anyway |
| 1159 |
*/ |
| 1160 |
fd_printf(c, c->local_wfd.fd, "%s STARTTLS", line); |
| 1161 |
str_free(line); |
| 1162 |
line=fd_getline(c, c->remote_fd.fd); /* next line */ |
| 1163 |
} |
| 1164 |
fd_putline(c, c->local_wfd.fd, line); /* forward to the client */ |
| 1165 |
tail=strchr(line, ' '); |
| 1166 |
if(!tail || !is_prefix(tail+1, "OK")) { /* not OK? */ |
| 1167 |
fd_putline(c, c->local_wfd.fd, |
| 1168 |
"* BYE unexpected server response"); |
| 1169 |
s_log(LOG_ERR, "Unexpected server response: %s", line); |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
} else if(is_prefix(tail, "LOGOUT")) { |
| 1173 |
fd_putline(c, c->local_wfd.fd, "* BYE server terminating"); |
| 1174 |
fd_printf(c, c->local_wfd.fd, "%s OK LOGOUT completed", id); |
| 1175 |
break; |
| 1176 |
} else { |
| 1177 |
fd_putline(c, c->local_wfd.fd, "* BYE stunnel: unexpected command"); |
| 1178 |
fd_printf(c, c->local_wfd.fd, "%s BAD %s unexpected", id, tail); |
| 1179 |
s_log(LOG_ERR, "Unexpected client command %s", tail); |
| 1180 |
break; |
| 1181 |
} |
| 1182 |
} |
| 1183 |
/* clean server shutdown */ |
| 1184 |
str_free(id); |
| 1185 |
fd_putline(c, c->remote_fd.fd, "stunnel LOGOUT"); |
| 1186 |
str_free(line); |
| 1187 |
line=fd_getline(c, c->remote_fd.fd); |
| 1188 |
if(*line=='*') { |
| 1189 |
str_free(line); |
| 1190 |
line=fd_getline(c, c->remote_fd.fd); |
| 1191 |
} |
| 1192 |
str_free(line); |
| 1193 |
throw_exception(c, 2); /* don't reset */ |
| 1194 |
} |
| 1195 |
|
| 1196 |
/**************************************** nntp */ |
| 1197 |
|
| 1198 |
NOEXPORT void nntp_client_middle(CLI *c) { |
| 1199 |
char *line=fd_getline(c, c->remote_fd.fd); |
| 1200 |
if(!is_prefix(line, "200 ") && !is_prefix(line, "201 ")) { |
| 1201 |
s_log(LOG_ERR, "Unknown server welcome"); |
| 1202 |
str_free(line); |
| 1203 |
throw_exception(c, 1); |
| 1204 |
} |
| 1205 |
fd_putline(c, c->local_wfd.fd, line); |
| 1206 |
fd_putline(c, c->remote_fd.fd, "STARTTLS"); |
| 1207 |
str_free(line); |
| 1208 |
line=fd_getline(c, c->remote_fd.fd); |
| 1209 |
if(!is_prefix(line, "382 ")) { |
| 1210 |
s_log(LOG_ERR, "Server does not support TLS"); |
| 1211 |
str_free(line); |
| 1212 |
throw_exception(c, 1); |
| 1213 |
} |
| 1214 |
str_free(line); |
| 1215 |
} |
| 1216 |
|
| 1217 |
/**************************************** LDAP, RFC 2830 */ |
| 1218 |
|
| 1219 |
uint8_t ldap_starttls_message[0x1d + 2]={ |
| 1220 |
0x30, /* tag = UNIVERSAL SEQUENCE */ |
| 1221 |
0x1d, /* len = 29 */ |
| 1222 |
0x02, /* tag = INTEGER (messageID) */ |
| 1223 |
0x01, /* len = 1 */ |
| 1224 |
0x01, /* val = 1 (this is messageID 1) */ |
| 1225 |
0x77, /* tag = APPLICATION 23 (ExtendedRequest) |
| 1226 |
* 0b01xxxxxx => Class = Application |
| 1227 |
* 0bxx1xxxxx => P/C = Constructed |
| 1228 |
* 0bxxx10111 => Number = 23 */ |
| 1229 |
0x18, /* len = 24 */ |
| 1230 |
0x80, /* tag = CONTEXT-SPECIFIC 0 (requestName) */ |
| 1231 |
0x16, /* len = 22 */ |
| 1232 |
/* val = LDAP_START_TLS_OID |
| 1233 |
* OID: 1.3.6.1.4.1.1466.20037 */ |
| 1234 |
'1', '.', |
| 1235 |
'3', '.', |
| 1236 |
'6', '.', |
| 1237 |
'1', '.', |
| 1238 |
'4', '.', |
| 1239 |
'1', '.', |
| 1240 |
'1', '4', '6', '6', '.', |
| 1241 |
'2', '0', '0', '3', '7' |
| 1242 |
/* no requestValue, as per RFC2830 |
| 1243 |
* (section 2.1: "The requestValue field is absent") */ |
| 1244 |
}; |
| 1245 |
|
| 1246 |
#define LDAP_UNIVERSAL_SEQUENCE 0x30 |
| 1247 |
#define LDAP_WINLDAP_FOUR_BYTE_LEN_FLAG 0x84 |
| 1248 |
#define LDAP_RESPONSE_MSG_ID_TAG_INTEGER 0x02 |
| 1249 |
#define LDAP_RESPONSE_MSG_ID_LEN 0x01 |
| 1250 |
#define LDAP_RESPONSE_MSG_ID_VAL 0x01 |
| 1251 |
#define LDAP_RESPONSE_OP_APPLICATION_24 0x78 |
| 1252 |
#define LDAP_RESPONSE_RESULT_TAG_ENUMERATED 0x0a |
| 1253 |
#define LDAP_RESPONSE_RESULT_LEN 0x01 |
| 1254 |
#define LDAP_RESPONSE_RESULT_VAL_SUCCESS 0x00 |
| 1255 |
|
| 1256 |
/* also see: |
| 1257 |
* https://ldap.com/ldapv3-wire-protocol-reference-extended/ |
| 1258 |
*/ |
| 1259 |
|
| 1260 |
NOEXPORT void ldap_client_middle(CLI *c) { |
| 1261 |
uint8_t buffer_8; |
| 1262 |
uint32_t buffer_32; |
| 1263 |
size_t resp_len; |
| 1264 |
uint8_t ldap_response[128]; |
| 1265 |
size_t resp_idx; |
| 1266 |
|
| 1267 |
s_log(LOG_DEBUG, "Sending LDAP Start TLS request"); |
| 1268 |
s_write(c, c->remote_fd.fd, ldap_starttls_message, sizeof(ldap_starttls_message)); |
| 1269 |
|
| 1270 |
s_log(LOG_DEBUG, "Receiving LDAP response tag"); |
| 1271 |
s_read(c, c->remote_fd.fd, &buffer_8, 1); |
| 1272 |
if(buffer_8!=LDAP_UNIVERSAL_SEQUENCE) { |
| 1273 |
s_log(LOG_ERR, "LDAP response is not UNIVERSAL SEQUENCE"); |
| 1274 |
throw_exception(c, 1); |
| 1275 |
} |
| 1276 |
|
| 1277 |
s_log(LOG_DEBUG, "Receiving LDAP response length"); |
| 1278 |
s_read(c, c->remote_fd.fd, &buffer_8, 1); |
| 1279 |
if(buffer_8==LDAP_WINLDAP_FOUR_BYTE_LEN_FLAG) { /* WinLDAP */ |
| 1280 |
/* receive response length (4 bytes, network byte order) */ |
| 1281 |
s_read(c, c->remote_fd.fd, &buffer_32, 4); |
| 1282 |
resp_len=ntohl(buffer_32); |
| 1283 |
} else { |
| 1284 |
resp_len=buffer_8; |
| 1285 |
} |
| 1286 |
if(resp_len>sizeof(ldap_response)) { |
| 1287 |
s_log(LOG_ERR, "LDAP response too long (%lu byte(s))", |
| 1288 |
(unsigned long)resp_len); |
| 1289 |
throw_exception(c, 1); |
| 1290 |
} |
| 1291 |
|
| 1292 |
s_log(LOG_DEBUG, "Receiving LDAP response value (%lu byte(s))", |
| 1293 |
(unsigned long)resp_len); |
| 1294 |
memset(ldap_response, 0, sizeof(ldap_response)); /* prevent data leaks */ |
| 1295 |
s_read(c, c->remote_fd.fd, ldap_response, resp_len); |
| 1296 |
|
| 1297 |
s_log(LOG_DEBUG, "Decoding LDAP response value"); |
| 1298 |
resp_idx=0; |
| 1299 |
if(ldap_response[resp_idx++]!=LDAP_RESPONSE_MSG_ID_TAG_INTEGER) { |
| 1300 |
s_log(LOG_ERR, "LDAP response has an incorrect message ID type"); |
| 1301 |
throw_exception(c, 1); |
| 1302 |
} |
| 1303 |
if(ldap_response[resp_idx++]!=LDAP_RESPONSE_MSG_ID_LEN) { |
| 1304 |
s_log(LOG_ERR, "LDAP response has an unexpected message ID length"); |
| 1305 |
throw_exception(c, 1); |
| 1306 |
} |
| 1307 |
if(ldap_response[resp_idx++]!=LDAP_RESPONSE_MSG_ID_VAL) { |
| 1308 |
s_log(LOG_ERR, "LDAP response has an unexpected message ID value"); |
| 1309 |
throw_exception(c, 1); |
| 1310 |
} |
| 1311 |
if(ldap_response[resp_idx++]!=LDAP_RESPONSE_OP_APPLICATION_24) { |
| 1312 |
s_log(LOG_ERR, "LDAP response protocol op is not ExtendedResponse"); |
| 1313 |
throw_exception(c, 1); |
| 1314 |
} |
| 1315 |
/* we do not validate the protocol op sequence length */ |
| 1316 |
if(ldap_response[resp_idx++]==LDAP_WINLDAP_FOUR_BYTE_LEN_FLAG) { /* WinLDAP */ |
| 1317 |
resp_idx+=4; /* skip next 4 bytes */ |
| 1318 |
} |
| 1319 |
if(ldap_response[resp_idx++]!=LDAP_RESPONSE_RESULT_TAG_ENUMERATED) { |
| 1320 |
s_log(LOG_ERR, "LDAP response has an unexpected result code type"); |
| 1321 |
throw_exception(c, 1); |
| 1322 |
} |
| 1323 |
if(ldap_response[resp_idx++]!=LDAP_RESPONSE_RESULT_LEN) { |
| 1324 |
s_log(LOG_ERR, "LDAP response has an unexpected result code length"); |
| 1325 |
throw_exception(c, 1); |
| 1326 |
} |
| 1327 |
if(ldap_response[resp_idx]!=LDAP_RESPONSE_RESULT_VAL_SUCCESS) { |
| 1328 |
s_log(LOG_ERR, "LDAP response has indicated an error (%u)", |
| 1329 |
ldap_response[resp_idx]); |
| 1330 |
throw_exception(c, 1); |
| 1331 |
} |
| 1332 |
/* any remaining data is ignored */ |
| 1333 |
|
| 1334 |
s_log(LOG_INFO, "LDAP Start TLS successfully negotiated"); |
| 1335 |
} |
| 1336 |
|
| 1337 |
/**************************************** connect */ |
| 1338 |
|
| 1339 |
NOEXPORT void connect_server_early(CLI *c) { |
| 1340 |
char *request, *proto, *header; |
| 1341 |
|
| 1342 |
request=fd_getline(c, c->local_rfd.fd); |
| 1343 |
if(!is_prefix(request, "CONNECT ")) { |
| 1344 |
fd_putline(c, c->local_wfd.fd, "HTTP/1.0 400 Bad Request Method"); |
| 1345 |
fd_putline(c, c->local_wfd.fd, "Server: stunnel/" STUNNEL_VERSION); |
| 1346 |
fd_putline(c, c->local_wfd.fd, ""); |
| 1347 |
str_free(request); |
| 1348 |
throw_exception(c, 1); |
| 1349 |
} |
| 1350 |
proto=strchr(request+8, ' '); |
| 1351 |
if(!proto || !is_prefix(proto, " HTTP/")) { |
| 1352 |
fd_putline(c, c->local_wfd.fd, "HTTP/1.0 400 Bad Request Protocol"); |
| 1353 |
fd_putline(c, c->local_wfd.fd, "Server: stunnel/" STUNNEL_VERSION); |
| 1354 |
fd_putline(c, c->local_wfd.fd, ""); |
| 1355 |
str_free(request); |
| 1356 |
throw_exception(c, 1); |
| 1357 |
} |
| 1358 |
*proto='\0'; |
| 1359 |
|
| 1360 |
header=str_dup(""); |
| 1361 |
do { /* ignore any headers */ |
| 1362 |
str_free(header); |
| 1363 |
header=fd_getline(c, c->local_rfd.fd); |
| 1364 |
} while(*header); /* not empty */ |
| 1365 |
str_free(header); |
| 1366 |
|
| 1367 |
if(!name2addrlist(&c->connect_addr, request+8)) { |
| 1368 |
fd_putline(c, c->local_wfd.fd, "HTTP/1.0 404 Not Found"); |
| 1369 |
fd_putline(c, c->local_wfd.fd, "Server: stunnel/" STUNNEL_VERSION); |
| 1370 |
fd_putline(c, c->local_wfd.fd, ""); |
| 1371 |
str_free(request); |
| 1372 |
throw_exception(c, 1); |
| 1373 |
} |
| 1374 |
str_free(request); |
| 1375 |
fd_putline(c, c->local_wfd.fd, "HTTP/1.0 200 OK"); |
| 1376 |
fd_putline(c, c->local_wfd.fd, "Server: stunnel/" STUNNEL_VERSION); |
| 1377 |
fd_putline(c, c->local_wfd.fd, ""); |
| 1378 |
} |
| 1379 |
|
| 1380 |
NOEXPORT const char *connect_client_init(SERVICE_OPTIONS *opt) { |
| 1381 |
if(!opt->protocol_host) |
| 1382 |
return "protocolHost not specified"; |
| 1383 |
return NULL; |
| 1384 |
} |
| 1385 |
|
| 1386 |
NOEXPORT void connect_client_middle(CLI *c) { |
| 1387 |
char *line, *encoded; |
| 1388 |
NAME_LIST *ptr; |
| 1389 |
|
| 1390 |
fd_printf(c, c->remote_fd.fd, "CONNECT %s HTTP/1.1", |
| 1391 |
c->opt->protocol_host); |
| 1392 |
fd_printf(c, c->remote_fd.fd, "Host: %s", c->opt->protocol_host); |
| 1393 |
if(c->opt->protocol_username && c->opt->protocol_password) { |
| 1394 |
if(!strcasecmp(c->opt->protocol_authentication, "ntlm")) { |
| 1395 |
#ifndef OPENSSL_NO_MD4 |
| 1396 |
ntlm(c); |
| 1397 |
#else |
| 1398 |
s_log(LOG_ERR, "NTLM authentication is not available"); |
| 1399 |
throw_exception(c, 1); |
| 1400 |
#endif |
| 1401 |
} else { /* basic authentication */ |
| 1402 |
line=str_printf("%s:%s", |
| 1403 |
c->opt->protocol_username, c->opt->protocol_password); |
| 1404 |
encoded=base64(1, line, (int)strlen(line)); |
| 1405 |
str_free(line); |
| 1406 |
if(!encoded) { |
| 1407 |
s_log(LOG_ERR, "Base64 encoder failed"); |
| 1408 |
throw_exception(c, 1); |
| 1409 |
} |
| 1410 |
fd_printf(c, c->remote_fd.fd, "Proxy-Authorization: basic %s", |
| 1411 |
encoded); |
| 1412 |
str_free(encoded); |
| 1413 |
} |
| 1414 |
} |
| 1415 |
for(ptr=c->opt->protocol_header; ptr; ptr=ptr->next) |
| 1416 |
fd_putline(c, c->remote_fd.fd, ptr->name); /* custom header */ |
| 1417 |
fd_putline(c, c->remote_fd.fd, ""); /* empty line */ |
| 1418 |
|
| 1419 |
line=fd_getline(c, c->remote_fd.fd); |
| 1420 |
if(!is_prefix(line, "HTTP/1.0 2") && !is_prefix(line, "HTTP/1.1 2")) { |
| 1421 |
/* not "HTTP/1.x 2xx Connection established" */ |
| 1422 |
s_log(LOG_ERR, "CONNECT request rejected"); |
| 1423 |
do { /* read all headers */ |
| 1424 |
str_free(line); |
| 1425 |
line=fd_getline(c, c->remote_fd.fd); |
| 1426 |
} while(*line); |
| 1427 |
str_free(line); |
| 1428 |
throw_exception(c, 1); |
| 1429 |
} |
| 1430 |
s_log(LOG_INFO, "CONNECT request accepted"); |
| 1431 |
do { |
| 1432 |
str_free(line); |
| 1433 |
line=fd_getline(c, c->remote_fd.fd); /* read all headers */ |
| 1434 |
} while(*line); |
| 1435 |
str_free(line); |
| 1436 |
} |
| 1437 |
|
| 1438 |
#ifndef OPENSSL_NO_MD4 |
| 1439 |
|
| 1440 |
/* |
| 1441 |
* NTLM code is based on the following documentation: |
| 1442 |
* http://davenport.sourceforge.net/ntlm.html |
| 1443 |
* http://www.innovation.ch/personal/ronald/ntlm.html |
| 1444 |
*/ |
| 1445 |
|
| 1446 |
#define s_min(a, b) ((a)>(b)?(b):(a)) |
| 1447 |
|
| 1448 |
NOEXPORT void ntlm(CLI *c) { |
| 1449 |
char *line, buf[BUFSIZ], *ntlm1_txt, *ntlm2_txt, *ntlm3_txt, *tmpstr; |
| 1450 |
long content_length=0; /* no HTTP content */ |
| 1451 |
|
| 1452 |
/* send Proxy-Authorization (phase 1) */ |
| 1453 |
fd_printf(c, c->remote_fd.fd, "Proxy-Connection: keep-alive"); |
| 1454 |
ntlm1_txt=ntlm1(); |
| 1455 |
if(!ntlm1_txt) { |
| 1456 |
s_log(LOG_ERR, "Proxy-Authenticate: Failed to build NTLM request"); |
| 1457 |
throw_exception(c, 1); |
| 1458 |
} |
| 1459 |
fd_printf(c, c->remote_fd.fd, "Proxy-Authorization: NTLM %s", ntlm1_txt); |
| 1460 |
str_free(ntlm1_txt); |
| 1461 |
fd_putline(c, c->remote_fd.fd, ""); /* empty line */ |
| 1462 |
line=fd_getline(c, c->remote_fd.fd); |
| 1463 |
|
| 1464 |
/* receive Proxy-Authenticate (phase 2) */ |
| 1465 |
if(!is_prefix(line, "HTTP/1.0 407") && !is_prefix(line, "HTTP/1.1 407")) { |
| 1466 |
s_log(LOG_ERR, "Proxy-Authenticate: NTLM authorization request rejected"); |
| 1467 |
do { /* read all headers */ |
| 1468 |
str_free(line); |
| 1469 |
line=fd_getline(c, c->remote_fd.fd); |
| 1470 |
} while(*line); |
| 1471 |
str_free(line); |
| 1472 |
throw_exception(c, 1); |
| 1473 |
} |
| 1474 |
ntlm2_txt=NULL; |
| 1475 |
do { /* read all headers */ |
| 1476 |
str_free(line); |
| 1477 |
line=fd_getline(c, c->remote_fd.fd); |
| 1478 |
if(is_prefix(line, "Proxy-Authenticate: NTLM ")) |
| 1479 |
ntlm2_txt=str_dup(line+25); |
| 1480 |
else if(is_prefix(line, "Content-Length: ")) { |
| 1481 |
content_length=strtol(line+16, &tmpstr, 10); |
| 1482 |
if(tmpstr>line+16) /* found some digits */ |
| 1483 |
while(*tmpstr && isspace((int)*tmpstr)) |
| 1484 |
++tmpstr; |
| 1485 |
if(tmpstr==line+16 || *tmpstr || content_length<0) { |
| 1486 |
s_log(LOG_ERR, "Proxy-Authenticate: Invalid Content-Length"); |
| 1487 |
str_free(line); |
| 1488 |
throw_exception(c, 1); |
| 1489 |
} |
| 1490 |
} |
| 1491 |
} while(*line); |
| 1492 |
if(!ntlm2_txt) { /* no Proxy-Authenticate: NTLM header */ |
| 1493 |
s_log(LOG_ERR, "Proxy-Authenticate: NTLM header not found"); |
| 1494 |
str_free(line); |
| 1495 |
throw_exception(c, 1); |
| 1496 |
} |
| 1497 |
|
| 1498 |
/* read and ignore HTTP content (if any) */ |
| 1499 |
while(content_length>0) { |
| 1500 |
size_t n=s_min((size_t)content_length, BUFSIZ); |
| 1501 |
s_read(c, c->remote_fd.fd, buf, n); |
| 1502 |
content_length-=(long)n; |
| 1503 |
} |
| 1504 |
|
| 1505 |
/* send Proxy-Authorization (phase 3) */ |
| 1506 |
fd_printf(c, c->remote_fd.fd, "CONNECT %s HTTP/1.1", c->opt->protocol_host); |
| 1507 |
fd_printf(c, c->remote_fd.fd, "Host: %s", c->opt->protocol_host); |
| 1508 |
ntlm3_txt=ntlm3(c->opt->protocol_domain, |
| 1509 |
c->opt->protocol_username, c->opt->protocol_password, ntlm2_txt); |
| 1510 |
str_free(ntlm2_txt); |
| 1511 |
if(!ntlm3_txt) { |
| 1512 |
s_log(LOG_ERR, "Proxy-Authenticate: Failed to build NTLM response"); |
| 1513 |
throw_exception(c, 1); |
| 1514 |
} |
| 1515 |
fd_printf(c, c->remote_fd.fd, "Proxy-Authorization: NTLM %s", ntlm3_txt); |
| 1516 |
str_free(ntlm3_txt); |
| 1517 |
} |
| 1518 |
|
| 1519 |
NOEXPORT char *ntlm1(void) { |
| 1520 |
char phase1[32]; |
| 1521 |
|
| 1522 |
memset(phase1, 0, sizeof phase1); |
| 1523 |
strcpy(phase1, "NTLMSSP"); |
| 1524 |
phase1[8]=1; /* type: 1 */ |
| 1525 |
phase1[12]=2; /* flag: negotiate OEM */ |
| 1526 |
phase1[13]=2; /* flag: negotiate NTLM */ |
| 1527 |
/* bytes 16-23: supplied domain security buffer */ |
| 1528 |
/* bytes 24-31: supplied workstation security buffer */ |
| 1529 |
return base64(1, phase1, sizeof phase1); /* encode */ |
| 1530 |
} |
| 1531 |
|
| 1532 |
NOEXPORT char *ntlm3(char *domain, |
| 1533 |
char *user, char *password, char *phase2) { |
| 1534 |
MD4_CTX md4; |
| 1535 |
uint8_t *decoded; /* decoded reply from proxy */ |
| 1536 |
uint8_t phase3[146]; |
| 1537 |
uint8_t md4_hash[21]; |
| 1538 |
const size_t ntlm_len=24; /* length of the NTLM hash response */ |
| 1539 |
const size_t domain_len=strlen(domain); |
| 1540 |
const size_t user_len=strlen(user); |
| 1541 |
const size_t ntlm_off=64; /* start of the data block in version 2 */ |
| 1542 |
const size_t domain_off=ntlm_off+ntlm_len; |
| 1543 |
const size_t user_off=domain_off+domain_len; |
| 1544 |
const size_t end_off=user_off+user_len; |
| 1545 |
|
| 1546 |
/* setup the phase3 structure */ |
| 1547 |
if(end_off>sizeof phase3) |
| 1548 |
return NULL; |
| 1549 |
memset(phase3, 0, sizeof phase3); |
| 1550 |
/* bytes 0-7: null-terminated NTLMSSP signature */ |
| 1551 |
strcpy((char *)phase3, "NTLMSSP"); |
| 1552 |
/* bytes 8-11: NTLM message type */ |
| 1553 |
phase3[8]=3; /* type: 3 */ |
| 1554 |
/* bytes 12-19: LM/LMv2 response */ |
| 1555 |
phase3[16]=(uint8_t)end_off; /* LM response offset */ |
| 1556 |
/* bytes 20-27: NTLM/NTLMv2 response */ |
| 1557 |
phase3[20]=(uint8_t)ntlm_len; /* NTLM response length */ |
| 1558 |
phase3[22]=(uint8_t)ntlm_len; /* NTLM response length */ |
| 1559 |
phase3[24]=(uint8_t)ntlm_off; /* NTLM response offset */ |
| 1560 |
/* bytes 28-35: target (domain/server) name */ |
| 1561 |
phase3[28]=(uint8_t)domain_len; /* domain length */ |
| 1562 |
phase3[30]=(uint8_t)domain_len; /* domain length */ |
| 1563 |
phase3[32]=(uint8_t)domain_off; /* domain offset */ |
| 1564 |
/* bytes 36-43: user name */ |
| 1565 |
phase3[36]=(uint8_t)user_len; /* user length */ |
| 1566 |
phase3[38]=(uint8_t)user_len; /* user length */ |
| 1567 |
phase3[40]=(uint8_t)user_off; /* user offset */ |
| 1568 |
/* bytes 44-51: workstation name */ |
| 1569 |
phase3[48]=(uint8_t)end_off; /* host offset */ |
| 1570 |
/* bytes 52-59: session key */ |
| 1571 |
phase3[56]=(uint8_t)end_off; /* session key offset */ |
| 1572 |
/* bytes 60-63: flags */ |
| 1573 |
phase3[60]=2; /* flag: negotiate OEM */ |
| 1574 |
phase3[61]=2; /* flag: negotiate NTLM */ |
| 1575 |
|
| 1576 |
/* calculate MD4 of the UTF-16 encoded password */ |
| 1577 |
MD4_Init(&md4); |
| 1578 |
while(*password) { |
| 1579 |
MD4_Update(&md4, password++, 1); |
| 1580 |
MD4_Update(&md4, "", 1); /* UTF-16 */ |
| 1581 |
} |
| 1582 |
MD4_Final(md4_hash, &md4); |
| 1583 |
memset(md4_hash+16, 0, 5); /* pad to 21 bytes */ |
| 1584 |
|
| 1585 |
/* decode the challenge and calculate the response */ |
| 1586 |
decoded=(uint8_t *)base64(0, phase2, (int)strlen(phase2)); /* decode */ |
| 1587 |
if(!decoded) |
| 1588 |
return NULL; |
| 1589 |
crypt_DES(phase3+ntlm_off, decoded+24, md4_hash); |
| 1590 |
crypt_DES(phase3+ntlm_off+8, decoded+24, md4_hash+7); |
| 1591 |
crypt_DES(phase3+ntlm_off+16, decoded+24, md4_hash+14); |
| 1592 |
str_free(decoded); |
| 1593 |
|
| 1594 |
memcpy((char *)phase3+domain_off, domain, domain_len); |
| 1595 |
memcpy((char *)phase3+user_off, user, user_len); |
| 1596 |
|
| 1597 |
return base64(1, (char *)phase3, (int)end_off); /* encode */ |
| 1598 |
} |
| 1599 |
|
| 1600 |
NOEXPORT void crypt_DES(DES_cblock dst, const_DES_cblock src, |
| 1601 |
unsigned char hash[7]) { |
| 1602 |
DES_cblock key; |
| 1603 |
DES_key_schedule sched; |
| 1604 |
|
| 1605 |
/* convert 56-bit hash to 64-bit DES key */ |
| 1606 |
key[0]=hash[0]; |
| 1607 |
key[1]=(unsigned char)(((hash[0]&1)<<7)|(hash[1]>>1)); |
| 1608 |
key[2]=(unsigned char)(((hash[1]&3)<<6)|(hash[2]>>2)); |
| 1609 |
key[3]=(unsigned char)(((hash[2]&7)<<5)|(hash[3]>>3)); |
| 1610 |
key[4]=(unsigned char)(((hash[3]&15)<<4)|(hash[4]>>4)); |
| 1611 |
key[5]=(unsigned char)(((hash[4]&31)<<3)|(hash[5]>>5)); |
| 1612 |
key[6]=(unsigned char)(((hash[5]&63)<<2)|(hash[6]>>6)); |
| 1613 |
key[7]=(unsigned char)(((hash[6]&127)<<1)); |
| 1614 |
DES_set_odd_parity(&key); |
| 1615 |
|
| 1616 |
/* encrypt */ |
| 1617 |
DES_set_key_unchecked(&key, &sched); |
| 1618 |
DES_ecb_encrypt((const_DES_cblock *)src, |
| 1619 |
(DES_cblock *)dst, &sched, DES_ENCRYPT); |
| 1620 |
} |
| 1621 |
|
| 1622 |
#endif |
| 1623 |
|
| 1624 |
NOEXPORT char *base64(int encode, const char *in, int len) { |
| 1625 |
BIO *bio, *b64; |
| 1626 |
char *out; |
| 1627 |
int n; |
| 1628 |
|
| 1629 |
b64=BIO_new(BIO_f_base64()); |
| 1630 |
if(!b64) |
| 1631 |
return NULL; |
| 1632 |
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
| 1633 |
bio=BIO_new(BIO_s_mem()); |
| 1634 |
if(!bio) { |
| 1635 |
BIO_free(b64); |
| 1636 |
return NULL; |
| 1637 |
} |
| 1638 |
if(encode) |
| 1639 |
bio=BIO_push(b64, bio); |
| 1640 |
BIO_write(bio, in, len); |
| 1641 |
(void)BIO_flush(bio); /* ignore the error if any */ |
| 1642 |
if(encode) { |
| 1643 |
bio=BIO_pop(bio); |
| 1644 |
BIO_free(b64); |
| 1645 |
} else { |
| 1646 |
bio=BIO_push(b64, bio); |
| 1647 |
} |
| 1648 |
n=BIO_pending(bio); |
| 1649 |
/* 32 bytes as a safety precaution for passing decoded data to crypt_DES */ |
| 1650 |
/* n+1 to get null-terminated string on encode */ |
| 1651 |
out=str_alloc(n<32?32:(size_t)n+1); |
| 1652 |
n=BIO_read(bio, out, n); |
| 1653 |
if(n<0) { |
| 1654 |
BIO_free_all(bio); |
| 1655 |
str_free(out); |
| 1656 |
return NULL; |
| 1657 |
} |
| 1658 |
BIO_free_all(bio); |
| 1659 |
return out; |
| 1660 |
} |
| 1661 |
|
| 1662 |
/**************************************** capwin authentication support */ |
| 1663 |
|
| 1664 |
NOEXPORT void capwin_server_middle(CLI *c) { |
| 1665 |
char *buffer, *user, *pass, *esc_user, *dn; |
| 1666 |
const char *failure="FAILED"; |
| 1667 |
int i; |
| 1668 |
|
| 1669 |
buffer=str_alloc(CAPWIN_BUFFER_SIZE); |
| 1670 |
for(i=0; i<CAPWIN_BUFFER_SIZE - 1; ++i) { |
| 1671 |
s_ssl_read(c, buffer+i, 1); |
| 1672 |
if(buffer[i] == '\x1c') |
| 1673 |
break; |
| 1674 |
} |
| 1675 |
if(capwin_decode(buffer, NULL, &user, &pass, NULL)) { |
| 1676 |
/* malformed request: reset instead of sending "FAILED" */ |
| 1677 |
str_free(buffer); |
| 1678 |
throw_exception(c, 1); |
| 1679 |
} |
| 1680 |
str_free(buffer); |
| 1681 |
esc_user=ldap_escape_dn(user); |
| 1682 |
str_free(user); |
| 1683 |
dn=str_printf("uid=%s,ou=people,O=CAPWIN,C=US", esc_user); |
| 1684 |
str_free(esc_user); |
| 1685 |
if(ldap_auth(c, dn, pass)) { |
| 1686 |
str_free(dn); |
| 1687 |
str_free(pass); |
| 1688 |
s_log(LOG_ERR, "CapWIN: Authentication failed"); |
| 1689 |
s_ssl_write(c, failure, (int)strlen(failure)); |
| 1690 |
throw_exception(c, 2); /* don't reset */ |
| 1691 |
} |
| 1692 |
str_free(dn); |
| 1693 |
str_free(pass); |
| 1694 |
s_log(LOG_NOTICE, "CapWIN: Authentication succeeded"); |
| 1695 |
} |
| 1696 |
|
| 1697 |
NOEXPORT void capwin_server_late(CLI *c) { |
| 1698 |
const char *success="BINGO"; |
| 1699 |
s_ssl_write(c, success, (int)strlen(success)); |
| 1700 |
} |
| 1701 |
|
| 1702 |
NOEXPORT void capwin_client_late(CLI *c) { |
| 1703 |
char *cmd, *user, *pass, *ctrl, *req, resp[5]; |
| 1704 |
|
| 1705 |
/* we extract the username and the password to work around a bug in the |
| 1706 |
* original server-side code that required unused units to be empty */ |
| 1707 |
/* otherwise, we could simply forward capwin_auth directly */ |
| 1708 |
if(!capwin_auth[0]) { |
| 1709 |
s_log(LOG_ERR, "CapWIN: No credentials set"); |
| 1710 |
throw_exception(c, 1); |
| 1711 |
} |
| 1712 |
if(capwin_decode(capwin_auth, &cmd, &user, &pass, &ctrl)) |
| 1713 |
throw_exception(c, 1); |
| 1714 |
if(strcmp(cmd, "AUTH")) { |
| 1715 |
s_log(LOG_ERR, "CapWIN: Invalid authentication request"); |
| 1716 |
str_free(cmd); |
| 1717 |
str_free(user); |
| 1718 |
str_free(pass); |
| 1719 |
str_free(ctrl); |
| 1720 |
throw_exception(c, 1); |
| 1721 |
} |
| 1722 |
str_free(cmd); |
| 1723 |
req=str_printf("\x1f%s\x1f%s\x1f\x1c", user, pass); |
| 1724 |
str_free(user); |
| 1725 |
str_free(pass); |
| 1726 |
#ifdef USE_WIN32 |
| 1727 |
capwin_hwnd=(HWND)(uintptr_t)atoi(ctrl); |
| 1728 |
#endif |
| 1729 |
str_free(ctrl); |
| 1730 |
|
| 1731 |
s_log(LOG_DEBUG, "CapWIN: Sending credentials"); |
| 1732 |
s_ssl_write(c, req, (int)strlen(req)); |
| 1733 |
str_free(req); |
| 1734 |
|
| 1735 |
s_log(LOG_DEBUG, "CapWIN: Waiting for response"); |
| 1736 |
s_ssl_read(c, resp, sizeof resp); |
| 1737 |
#ifdef USE_WIN32 |
| 1738 |
/* we received a response, so network is up */ |
| 1739 |
if(!InterlockedExchange(&capwin_connectivity, 1)) |
| 1740 |
PostMessage(capwin_hwnd, WM_CAPWIN_NET_UP, 0, 0); |
| 1741 |
#endif |
| 1742 |
if(memcmp(resp, "BINGO", sizeof resp)) { |
| 1743 |
s_log(LOG_ERR, "CapWIN: Authentication failed"); |
| 1744 |
#ifdef USE_WIN32 |
| 1745 |
PostMessage(capwin_hwnd, WM_CAPWIN_AUTH_FAIL, 0, 0); |
| 1746 |
#endif |
| 1747 |
throw_exception(c, 1); |
| 1748 |
} |
| 1749 |
s_log(LOG_NOTICE, "CapWIN: Authentication succeeded"); |
| 1750 |
#ifdef USE_WIN32 |
| 1751 |
PostMessage(capwin_hwnd, WM_CAPWIN_AUTH_OK, 0, 0); |
| 1752 |
#endif |
| 1753 |
} |
| 1754 |
|
| 1755 |
NOEXPORT const char *capwinctrl_client_init(SERVICE_OPTIONS *opt) { |
| 1756 |
opt->option.protocol_endpoint=1; |
| 1757 |
return NULL; |
| 1758 |
} |
| 1759 |
|
| 1760 |
NOEXPORT void capwinctrl_client_early(CLI *c) { |
| 1761 |
s_log(LOG_DEBUG, "CapWIN: Setting credentials"); |
| 1762 |
memset(capwin_auth, 0, CAPWIN_BUFFER_SIZE); |
| 1763 |
s_read_eof(c, c->local_rfd.fd, capwin_auth, CAPWIN_BUFFER_SIZE - 1); |
| 1764 |
s_log(LOG_NOTICE, "CapWIN: Credentials set"); |
| 1765 |
/* skip connecting a remote host */ |
| 1766 |
throw_exception(c, 2); /* don't reset */ |
| 1767 |
} |
| 1768 |
|
| 1769 |
NOEXPORT int capwin_decode(const char *src, |
| 1770 |
char **cmd, char **user, char **pass, char **ctrl) { |
| 1771 |
char *us1, *us2, *us3, *fs; |
| 1772 |
|
| 1773 |
us1=strchr(src, '\x1f'); |
| 1774 |
if(!us1) { |
| 1775 |
s_log(LOG_ERR, "CapWIN: Malformed credentials (1)"); |
| 1776 |
return 1; /* FAILED */ |
| 1777 |
} |
| 1778 |
us2=strchr(us1 + 1, '\x1f'); |
| 1779 |
if(!us2) { |
| 1780 |
s_log(LOG_ERR, "CapWIN: Malformed credentials (2)"); |
| 1781 |
return 1; /* FAILED */ |
| 1782 |
} |
| 1783 |
us3=strchr(us2 + 1, '\x1f'); |
| 1784 |
if(!us3) { |
| 1785 |
s_log(LOG_ERR, "CapWIN: Malformed credentials (3)"); |
| 1786 |
return 1; /* FAILED */ |
| 1787 |
} |
| 1788 |
fs=strchr(us3 + 1, '\x1c'); |
| 1789 |
if(!fs) { |
| 1790 |
s_log(LOG_ERR, "CapWIN: Malformed credentials (4)"); |
| 1791 |
return 1; /* FAILED */ |
| 1792 |
} |
| 1793 |
if(cmd) { |
| 1794 |
size_t len=(size_t)(us1 - src); |
| 1795 |
*cmd=str_alloc(len + 1); |
| 1796 |
memcpy(*cmd, src, len); |
| 1797 |
} |
| 1798 |
if(user) { |
| 1799 |
size_t len=(size_t)(us2 - us1) - 1; |
| 1800 |
*user=str_alloc(len + 1); |
| 1801 |
memcpy(*user, us1 + 1, len); |
| 1802 |
} |
| 1803 |
if(pass) { |
| 1804 |
size_t len=(size_t)(us3 - us2) - 1; |
| 1805 |
*pass=str_alloc(len + 1); |
| 1806 |
memcpy(*pass, us2 + 1, len); |
| 1807 |
} |
| 1808 |
if(ctrl) { |
| 1809 |
size_t len=(size_t)(fs - us3) - 1; |
| 1810 |
*ctrl=str_alloc(len + 1); |
| 1811 |
memcpy(*ctrl, us3 + 1, len); |
| 1812 |
} |
| 1813 |
return 0; /* SUCCESS */ |
| 1814 |
} |
| 1815 |
|
| 1816 |
NOEXPORT int ldap_auth(CLI *c, const char *dn, const char *pass) { |
| 1817 |
size_t dn_len, pass_len, req_len; |
| 1818 |
SOCKADDR_UNION addr; |
| 1819 |
int i; |
| 1820 |
unsigned char *req, resp[22]; |
| 1821 |
const unsigned char resp_ok[22]= |
| 1822 |
{0x30, 0x84, 0x00, 0x00, 0x00, 0x10, 0x02, 0x01, |
| 1823 |
0x01, 0x61, 0x84, 0x00, 0x00, 0x00, 0x07, 0x0a, |
| 1824 |
0x01, 0x00, 0x04, 0x00, 0x04, 0x00}; |
| 1825 |
|
| 1826 |
/* reject parameters too long for simple encoding */ |
| 1827 |
dn_len=strlen(dn); |
| 1828 |
pass_len=strlen(pass); |
| 1829 |
req_len=dn_len + pass_len + 14; |
| 1830 |
if(req_len > 120) { |
| 1831 |
s_log(LOG_ERR, "LDAP: Request too long"); |
| 1832 |
return 1; /* FAILED */ |
| 1833 |
} |
| 1834 |
|
| 1835 |
/* connect the configured LDAP server */ |
| 1836 |
if(!name2addr(&addr, c->opt->protocol_host, 0)) { |
| 1837 |
s_log(LOG_ERR, "LDAP: Failed to resolve protocolHost"); |
| 1838 |
return 1; /* FAILED */ |
| 1839 |
} |
| 1840 |
c->fd=s_socket(addr.sa.sa_family, SOCK_STREAM, 0, 1, "LDAP socket"); |
| 1841 |
if(c->fd==INVALID_SOCKET) |
| 1842 |
return 1; /* FAILED */ |
| 1843 |
s_log(LOG_DEBUG, "LDAP: Connecting the server"); |
| 1844 |
if(s_connect(c, &addr, addr_len(&addr), c->opt->timeout_connect)) { |
| 1845 |
closesocket(c->fd); |
| 1846 |
c->fd=INVALID_SOCKET; /* avoid double close on cleanup */ |
| 1847 |
return 1; /* FAILED */ |
| 1848 |
} |
| 1849 |
|
| 1850 |
/* send BindRequest */ |
| 1851 |
req=str_alloc(req_len); |
| 1852 |
i=0; |
| 1853 |
req[i++]=0x30; /* SEQUENCE */ |
| 1854 |
req[i++]=(unsigned char)(dn_len + pass_len + 12); |
| 1855 |
req[i++]=0x02; /* INTEGER */ |
| 1856 |
req[i++]=0x01; /* length */ |
| 1857 |
req[i++]=0x01; /* MessageID */ |
| 1858 |
req[i++]=0x60; /* [APPLICATION 0]: BindRequest */ |
| 1859 |
req[i++]=(unsigned char)(dn_len + pass_len + 7); |
| 1860 |
req[i++]=0x02; /* INTEGER */ |
| 1861 |
req[i++]=0x01; /* length */ |
| 1862 |
req[i++]=0x03; /* LDAP protocol version */ |
| 1863 |
req[i++]=0x04; /* OCTET STRING */ |
| 1864 |
req[i++]=(unsigned char)dn_len; |
| 1865 |
memcpy(req + i, dn, dn_len); |
| 1866 |
i+=(int)dn_len; |
| 1867 |
req[i++]=0x80; /* [IMPLICIT 0]: simple authentication */ |
| 1868 |
req[i++]=(unsigned char)pass_len; |
| 1869 |
memcpy(req + i, pass, pass_len); |
| 1870 |
s_log(LOG_DEBUG, "LDAP: Sending BindRequest"); |
| 1871 |
s_write(c, c->fd, req, req_len); |
| 1872 |
str_free(req); |
| 1873 |
|
| 1874 |
/* receive BindResponse */ |
| 1875 |
s_log(LOG_DEBUG, "LDAP: Waiting for BindResponse"); |
| 1876 |
s_read(c, c->fd, resp, sizeof resp); |
| 1877 |
closesocket(c->fd); |
| 1878 |
c->fd=INVALID_SOCKET; /* avoid double close on cleanup */ |
| 1879 |
return memcmp(resp, resp_ok, sizeof resp); |
| 1880 |
} |
| 1881 |
|
| 1882 |
NOEXPORT char *ldap_escape_dn(const char *src) { |
| 1883 |
int i=0, j=0; |
| 1884 |
char *dst=str_alloc(2 * strlen(src) + 1); |
| 1885 |
|
| 1886 |
while(src[i]) { |
| 1887 |
if(strchr("+;,\\\"<>#", src[i])) |
| 1888 |
dst[j++]='\\'; |
| 1889 |
dst[j++]=src[i++]; |
| 1890 |
} |
| 1891 |
return dst; |
| 1892 |
} |
| 1893 |
|
| 1894 |
/* end of protocol.c */ |