Coverage Report

Created: 2023-06-07 07:02

/src/curl/lib/socks.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#if !defined(CURL_DISABLE_PROXY)
28
29
#ifdef HAVE_NETINET_IN_H
30
#include <netinet/in.h>
31
#endif
32
#ifdef HAVE_ARPA_INET_H
33
#include <arpa/inet.h>
34
#endif
35
36
#include "urldata.h"
37
#include "sendf.h"
38
#include "select.h"
39
#include "cfilters.h"
40
#include "connect.h"
41
#include "timeval.h"
42
#include "socks.h"
43
#include "multiif.h" /* for getsock macros */
44
#include "inet_pton.h"
45
#include "url.h"
46
47
/* The last 3 #include files should be in this order */
48
#include "curl_printf.h"
49
#include "curl_memory.h"
50
#include "memdebug.h"
51
52
/* for the (SOCKS) connect state machine */
53
enum connect_t {
54
  CONNECT_INIT,
55
  CONNECT_SOCKS_INIT, /* 1 */
56
  CONNECT_SOCKS_SEND, /* 2 waiting to send more first data */
57
  CONNECT_SOCKS_READ_INIT, /* 3 set up read */
58
  CONNECT_SOCKS_READ, /* 4 read server response */
59
  CONNECT_GSSAPI_INIT, /* 5 */
60
  CONNECT_AUTH_INIT, /* 6 setup outgoing auth buffer */
61
  CONNECT_AUTH_SEND, /* 7 send auth */
62
  CONNECT_AUTH_READ, /* 8 read auth response */
63
  CONNECT_REQ_INIT,  /* 9 init SOCKS "request" */
64
  CONNECT_RESOLVING, /* 10 */
65
  CONNECT_RESOLVED,  /* 11 */
66
  CONNECT_RESOLVE_REMOTE, /* 12 */
67
  CONNECT_REQ_SEND,  /* 13 */
68
  CONNECT_REQ_SENDING, /* 14 */
69
  CONNECT_REQ_READ,  /* 15 */
70
  CONNECT_REQ_READ_MORE, /* 16 */
71
  CONNECT_DONE /* 17 connected fine to the remote or the SOCKS proxy */
72
};
73
74
struct socks_state {
75
  enum connect_t state;
76
  ssize_t outstanding;  /* send this many bytes more */
77
  unsigned char *outp; /* send from this pointer */
78
79
  const char *hostname;
80
  int remote_port;
81
  const char *proxy_user;
82
  const char *proxy_password;
83
};
84
85
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
86
/*
87
 * Helper read-from-socket functions. Does the same as Curl_read() but it
88
 * blocks until all bytes amount of buffersize will be read. No more, no less.
89
 *
90
 * This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions.
91
 */
92
int Curl_blockread_all(struct Curl_cfilter *cf,
93
                       struct Curl_easy *data,   /* transfer */
94
                       char *buf,                /* store read data here */
95
                       ssize_t buffersize,       /* max amount to read */
96
                       ssize_t *n)               /* amount bytes read */
97
{
98
  ssize_t nread = 0;
99
  ssize_t allread = 0;
100
  int result;
101
  CURLcode err = CURLE_OK;
102
103
  *n = 0;
104
  for(;;) {
105
    timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
106
    if(timeout_ms < 0) {
107
      /* we already got the timeout */
108
      result = CURLE_OPERATION_TIMEDOUT;
109
      break;
110
    }
111
    if(!timeout_ms)
112
      timeout_ms = TIMEDIFF_T_MAX;
113
    if(SOCKET_READABLE(cf->conn->sock[cf->sockindex], timeout_ms) <= 0) {
114
      result = ~CURLE_OK;
115
      break;
116
    }
117
    nread = Curl_conn_cf_recv(cf->next, data, buf, buffersize, &err);
118
    if(nread <= 0) {
119
      result = err;
120
      if(CURLE_AGAIN == err)
121
        continue;
122
      if(err) {
123
        break;
124
      }
125
    }
126
127
    if(buffersize == nread) {
128
      allread += nread;
129
      *n = allread;
130
      result = CURLE_OK;
131
      break;
132
    }
133
    if(!nread) {
134
      result = ~CURLE_OK;
135
      break;
136
    }
137
138
    buffersize -= nread;
139
    buf += nread;
140
    allread += nread;
141
  }
142
  return result;
143
}
144
#endif
145
146
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
147
#define DEBUG_AND_VERBOSE
148
0
#define sxstate(x,d,y) socksstate(x,d,y, __LINE__)
149
#else
150
#define sxstate(x,d,y) socksstate(x,d,y)
151
#endif
152
153
/* always use this function to change state, to make debugging easier */
154
static void socksstate(struct socks_state *sx, struct Curl_easy *data,
155
                       enum connect_t state
156
#ifdef DEBUG_AND_VERBOSE
157
                       , int lineno
158
#endif
159
)
160
0
{
161
0
  enum connect_t oldstate = sx->state;
162
0
#ifdef DEBUG_AND_VERBOSE
163
  /* synced with the state list in urldata.h */
164
0
  static const char * const statename[] = {
165
0
    "INIT",
166
0
    "SOCKS_INIT",
167
0
    "SOCKS_SEND",
168
0
    "SOCKS_READ_INIT",
169
0
    "SOCKS_READ",
170
0
    "GSSAPI_INIT",
171
0
    "AUTH_INIT",
172
0
    "AUTH_SEND",
173
0
    "AUTH_READ",
174
0
    "REQ_INIT",
175
0
    "RESOLVING",
176
0
    "RESOLVED",
177
0
    "RESOLVE_REMOTE",
178
0
    "REQ_SEND",
179
0
    "REQ_SENDING",
180
0
    "REQ_READ",
181
0
    "REQ_READ_MORE",
182
0
    "DONE"
183
0
  };
184
0
#endif
185
186
0
  (void)data;
187
0
  if(oldstate == state)
188
    /* don't bother when the new state is the same as the old state */
189
0
    return;
190
191
0
  sx->state = state;
192
193
0
#ifdef DEBUG_AND_VERBOSE
194
0
  infof(data,
195
0
        "SXSTATE: %s => %s; line %d",
196
0
        statename[oldstate], statename[sx->state],
197
0
        lineno);
198
0
#endif
199
0
}
200
201
static CURLproxycode socks_state_send(struct Curl_cfilter *cf,
202
                                      struct socks_state *sx,
203
                                      struct Curl_easy *data,
204
                                      CURLproxycode failcode,
205
                                      const char *description)
206
0
{
207
0
  ssize_t nwritten;
208
0
  CURLcode result;
209
210
0
  nwritten = Curl_conn_cf_send(cf->next, data, (char *)sx->outp,
211
0
                               sx->outstanding, &result);
212
0
  if(nwritten <= 0) {
213
0
    if(CURLE_AGAIN == result) {
214
0
      return CURLPX_OK;
215
0
    }
216
0
    else if(CURLE_OK == result) {
217
      /* connection closed */
218
0
      failf(data, "connection to proxy closed");
219
0
      return CURLPX_CLOSED;
220
0
    }
221
0
    failf(data, "Failed to send %s: %s", description,
222
0
          curl_easy_strerror(result));
223
0
    return failcode;
224
0
  }
225
0
  DEBUGASSERT(sx->outstanding >= nwritten);
226
  /* not done, remain in state */
227
0
  sx->outstanding -= nwritten;
228
0
  sx->outp += nwritten;
229
0
  return CURLPX_OK;
230
0
}
231
232
static CURLproxycode socks_state_recv(struct Curl_cfilter *cf,
233
                                      struct socks_state *sx,
234
                                      struct Curl_easy *data,
235
                                      CURLproxycode failcode,
236
                                      const char *description)
237
0
{
238
0
  ssize_t nread;
239
0
  CURLcode result;
240
241
0
  nread = Curl_conn_cf_recv(cf->next, data, (char *)sx->outp,
242
0
                            sx->outstanding, &result);
243
0
  if(nread <= 0) {
244
0
    if(CURLE_AGAIN == result) {
245
0
      return CURLPX_OK;
246
0
    }
247
0
    else if(CURLE_OK == result) {
248
      /* connection closed */
249
0
      failf(data, "connection to proxy closed");
250
0
      return CURLPX_CLOSED;
251
0
    }
252
0
    failf(data, "SOCKS4: Failed receiving %s: %s", description,
253
0
          curl_easy_strerror(result));
254
0
    return failcode;
255
0
  }
256
  /* remain in reading state */
257
0
  DEBUGASSERT(sx->outstanding >= nread);
258
0
  sx->outstanding -= nread;
259
0
  sx->outp += nread;
260
0
  return CURLPX_OK;
261
0
}
262
263
/*
264
* This function logs in to a SOCKS4 proxy and sends the specifics to the final
265
* destination server.
266
*
267
* Reference :
268
*   https://www.openssh.com/txt/socks4.protocol
269
*
270
* Note :
271
*   Set protocol4a=true for  "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
272
*   Nonsupport "Identification Protocol (RFC1413)"
273
*/
274
static CURLproxycode do_SOCKS4(struct Curl_cfilter *cf,
275
                               struct socks_state *sx,
276
                               struct Curl_easy *data)
277
0
{
278
0
  struct connectdata *conn = cf->conn;
279
0
  const bool protocol4a =
280
0
    (conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A) ? TRUE : FALSE;
281
0
  unsigned char *socksreq = (unsigned char *)data->state.buffer;
282
0
  CURLcode result;
283
0
  CURLproxycode presult;
284
0
  struct Curl_dns_entry *dns = NULL;
285
286
  /* make sure that the buffer is at least 600 bytes */
287
0
  DEBUGASSERT(READBUFFER_MIN >= 600);
288
289
0
  switch(sx->state) {
290
0
  case CONNECT_SOCKS_INIT:
291
    /* SOCKS4 can only do IPv4, insist! */
292
0
    conn->ip_version = CURL_IPRESOLVE_V4;
293
0
    if(conn->bits.httpproxy)
294
0
      infof(data, "SOCKS4%s: connecting to HTTP proxy %s port %d",
295
0
            protocol4a ? "a" : "", sx->hostname, sx->remote_port);
296
297
0
    infof(data, "SOCKS4 communication to %s:%d",
298
0
          sx->hostname, sx->remote_port);
299
300
    /*
301
     * Compose socks4 request
302
     *
303
     * Request format
304
     *
305
     *     +----+----+----+----+----+----+----+----+----+----+....+----+
306
     *     | VN | CD | DSTPORT |      DSTIP        | USERID       |NULL|
307
     *     +----+----+----+----+----+----+----+----+----+----+....+----+
308
     * # of bytes:  1    1      2              4           variable       1
309
     */
310
311
0
    socksreq[0] = 4; /* version (SOCKS4) */
312
0
    socksreq[1] = 1; /* connect */
313
0
    socksreq[2] = (unsigned char)((sx->remote_port >> 8) & 0xff); /* MSB */
314
0
    socksreq[3] = (unsigned char)(sx->remote_port & 0xff);        /* LSB */
315
316
    /* DNS resolve only for SOCKS4, not SOCKS4a */
317
0
    if(!protocol4a) {
318
0
      enum resolve_t rc =
319
0
        Curl_resolv(data, sx->hostname, sx->remote_port, TRUE, &dns);
320
321
0
      if(rc == CURLRESOLV_ERROR)
322
0
        return CURLPX_RESOLVE_HOST;
323
0
      else if(rc == CURLRESOLV_PENDING) {
324
0
        sxstate(sx, data, CONNECT_RESOLVING);
325
0
        infof(data, "SOCKS4 non-blocking resolve of %s", sx->hostname);
326
0
        return CURLPX_OK;
327
0
      }
328
0
      sxstate(sx, data, CONNECT_RESOLVED);
329
0
      goto CONNECT_RESOLVED;
330
0
    }
331
332
    /* socks4a doesn't resolve anything locally */
333
0
    sxstate(sx, data, CONNECT_REQ_INIT);
334
0
    goto CONNECT_REQ_INIT;
335
336
0
  case CONNECT_RESOLVING:
337
    /* check if we have the name resolved by now */
338
0
    dns = Curl_fetch_addr(data, sx->hostname, (int)conn->port);
339
340
0
    if(dns) {
341
0
#ifdef CURLRES_ASYNCH
342
0
      data->state.async.dns = dns;
343
0
      data->state.async.done = TRUE;
344
0
#endif
345
0
      infof(data, "Hostname '%s' was found", sx->hostname);
346
0
      sxstate(sx, data, CONNECT_RESOLVED);
347
0
    }
348
0
    else {
349
0
      result = Curl_resolv_check(data, &dns);
350
0
      if(!dns) {
351
0
        if(result)
352
0
          return CURLPX_RESOLVE_HOST;
353
0
        return CURLPX_OK;
354
0
      }
355
0
    }
356
    /* FALLTHROUGH */
357
0
CONNECT_RESOLVED:
358
0
  case CONNECT_RESOLVED: {
359
0
    struct Curl_addrinfo *hp = NULL;
360
    /*
361
     * We cannot use 'hostent' as a struct that Curl_resolv() returns.  It
362
     * returns a Curl_addrinfo pointer that may not always look the same.
363
     */
364
0
    if(dns) {
365
0
      hp = dns->addr;
366
367
      /* scan for the first IPv4 address */
368
0
      while(hp && (hp->ai_family != AF_INET))
369
0
        hp = hp->ai_next;
370
371
0
      if(hp) {
372
0
        struct sockaddr_in *saddr_in;
373
0
        char buf[64];
374
0
        Curl_printable_address(hp, buf, sizeof(buf));
375
376
0
        saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
377
0
        socksreq[4] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[0];
378
0
        socksreq[5] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[1];
379
0
        socksreq[6] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[2];
380
0
        socksreq[7] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[3];
381
382
0
        infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)", buf);
383
384
0
        Curl_resolv_unlock(data, dns); /* not used anymore from now on */
385
0
      }
386
0
      else
387
0
        failf(data, "SOCKS4 connection to %s not supported", sx->hostname);
388
0
    }
389
0
    else
390
0
      failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
391
0
            sx->hostname);
392
393
0
    if(!hp)
394
0
      return CURLPX_RESOLVE_HOST;
395
0
  }
396
    /* FALLTHROUGH */
397
0
CONNECT_REQ_INIT:
398
0
  case CONNECT_REQ_INIT:
399
    /*
400
     * This is currently not supporting "Identification Protocol (RFC1413)".
401
     */
402
0
    socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
403
0
    if(sx->proxy_user) {
404
0
      size_t plen = strlen(sx->proxy_user);
405
0
      if(plen >= (size_t)data->set.buffer_size - 8) {
406
0
        failf(data, "Too long SOCKS proxy user name, can't use");
407
0
        return CURLPX_LONG_USER;
408
0
      }
409
      /* copy the proxy name WITH trailing zero */
410
0
      memcpy(socksreq + 8, sx->proxy_user, plen + 1);
411
0
    }
412
413
    /*
414
     * Make connection
415
     */
416
0
    {
417
0
      size_t packetsize = 9 +
418
0
        strlen((char *)socksreq + 8); /* size including NUL */
419
420
      /* If SOCKS4a, set special invalid IP address 0.0.0.x */
421
0
      if(protocol4a) {
422
0
        size_t hostnamelen = 0;
423
0
        socksreq[4] = 0;
424
0
        socksreq[5] = 0;
425
0
        socksreq[6] = 0;
426
0
        socksreq[7] = 1;
427
        /* append hostname */
428
0
        hostnamelen = strlen(sx->hostname) + 1; /* length including NUL */
429
0
        if(hostnamelen <= 255)
430
0
          strcpy((char *)socksreq + packetsize, sx->hostname);
431
0
        else {
432
0
          failf(data, "SOCKS4: too long host name");
433
0
          return CURLPX_LONG_HOSTNAME;
434
0
        }
435
0
        packetsize += hostnamelen;
436
0
      }
437
0
      sx->outp = socksreq;
438
0
      sx->outstanding = packetsize;
439
0
      sxstate(sx, data, CONNECT_REQ_SENDING);
440
0
    }
441
    /* FALLTHROUGH */
442
0
  case CONNECT_REQ_SENDING:
443
    /* Send request */
444
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
445
0
                               "SOCKS4 connect request");
446
0
    if(CURLPX_OK != presult)
447
0
      return presult;
448
0
    else if(sx->outstanding) {
449
      /* remain in sending state */
450
0
      return CURLPX_OK;
451
0
    }
452
    /* done sending! */
453
0
    sx->outstanding = 8; /* receive data size */
454
0
    sx->outp = socksreq;
455
0
    sxstate(sx, data, CONNECT_SOCKS_READ);
456
457
    /* FALLTHROUGH */
458
0
  case CONNECT_SOCKS_READ:
459
    /* Receive response */
460
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
461
0
                               "connect request ack");
462
0
    if(CURLPX_OK != presult)
463
0
      return presult;
464
0
    else if(sx->outstanding) {
465
      /* remain in reading state */
466
0
      return CURLPX_OK;
467
0
    }
468
0
    sxstate(sx, data, CONNECT_DONE);
469
0
    break;
470
0
  default: /* lots of unused states in SOCKS4 */
471
0
    break;
472
0
  }
473
474
  /*
475
   * Response format
476
   *
477
   *     +----+----+----+----+----+----+----+----+
478
   *     | VN | CD | DSTPORT |      DSTIP        |
479
   *     +----+----+----+----+----+----+----+----+
480
   * # of bytes:  1    1      2              4
481
   *
482
   * VN is the version of the reply code and should be 0. CD is the result
483
   * code with one of the following values:
484
   *
485
   * 90: request granted
486
   * 91: request rejected or failed
487
   * 92: request rejected because SOCKS server cannot connect to
488
   *     identd on the client
489
   * 93: request rejected because the client program and identd
490
   *     report different user-ids
491
   */
492
493
  /* wrong version ? */
494
0
  if(socksreq[0]) {
495
0
    failf(data,
496
0
          "SOCKS4 reply has wrong version, version should be 0.");
497
0
    return CURLPX_BAD_VERSION;
498
0
  }
499
500
  /* Result */
501
0
  switch(socksreq[1]) {
502
0
  case 90:
503
0
    infof(data, "SOCKS4%s request granted.", protocol4a?"a":"");
504
0
    break;
505
0
  case 91:
506
0
    failf(data,
507
0
          "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
508
0
          ", request rejected or failed.",
509
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
510
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
511
0
          (unsigned char)socksreq[1]);
512
0
    return CURLPX_REQUEST_FAILED;
513
0
  case 92:
514
0
    failf(data,
515
0
          "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
516
0
          ", request rejected because SOCKS server cannot connect to "
517
0
          "identd on the client.",
518
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
519
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
520
0
          (unsigned char)socksreq[1]);
521
0
    return CURLPX_IDENTD;
522
0
  case 93:
523
0
    failf(data,
524
0
          "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
525
0
          ", request rejected because the client program and identd "
526
0
          "report different user-ids.",
527
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
528
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
529
0
          (unsigned char)socksreq[1]);
530
0
    return CURLPX_IDENTD_DIFFER;
531
0
  default:
532
0
    failf(data,
533
0
          "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
534
0
          ", Unknown.",
535
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
536
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
537
0
          (unsigned char)socksreq[1]);
538
0
    return CURLPX_UNKNOWN_FAIL;
539
0
  }
540
541
0
  return CURLPX_OK; /* Proxy was successful! */
542
0
}
543
544
/*
545
 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
546
 * destination server.
547
 */
548
static CURLproxycode do_SOCKS5(struct Curl_cfilter *cf,
549
                               struct socks_state *sx,
550
                               struct Curl_easy *data)
551
0
{
552
  /*
553
    According to the RFC1928, section "6.  Replies". This is what a SOCK5
554
    replies:
555
556
        +----+-----+-------+------+----------+----------+
557
        |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
558
        +----+-----+-------+------+----------+----------+
559
        | 1  |  1  | X'00' |  1   | Variable |    2     |
560
        +----+-----+-------+------+----------+----------+
561
562
    Where:
563
564
    o  VER    protocol version: X'05'
565
    o  REP    Reply field:
566
    o  X'00' succeeded
567
  */
568
0
  struct connectdata *conn = cf->conn;
569
0
  unsigned char *socksreq = (unsigned char *)data->state.buffer;
570
0
  char dest[256] = "unknown";  /* printable hostname:port */
571
0
  int idx;
572
0
  CURLcode result;
573
0
  CURLproxycode presult;
574
0
  bool socks5_resolve_local =
575
0
    (conn->socks_proxy.proxytype == CURLPROXY_SOCKS5) ? TRUE : FALSE;
576
0
  const size_t hostname_len = strlen(sx->hostname);
577
0
  ssize_t len = 0;
578
0
  const unsigned char auth = data->set.socks5auth;
579
0
  bool allow_gssapi = FALSE;
580
0
  struct Curl_dns_entry *dns = NULL;
581
582
0
  DEBUGASSERT(auth & (CURLAUTH_BASIC | CURLAUTH_GSSAPI));
583
0
  switch(sx->state) {
584
0
  case CONNECT_SOCKS_INIT:
585
0
    if(conn->bits.httpproxy)
586
0
      infof(data, "SOCKS5: connecting to HTTP proxy %s port %d",
587
0
            sx->hostname, sx->remote_port);
588
589
    /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
590
0
    if(!socks5_resolve_local && hostname_len > 255) {
591
0
      infof(data, "SOCKS5: server resolving disabled for hostnames of "
592
0
            "length > 255 [actual len=%zu]", hostname_len);
593
0
      socks5_resolve_local = TRUE;
594
0
    }
595
596
0
    if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
597
0
      infof(data,
598
0
            "warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %u",
599
0
            auth);
600
0
    if(!(auth & CURLAUTH_BASIC))
601
      /* disable username/password auth */
602
0
      sx->proxy_user = NULL;
603
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
604
    if(auth & CURLAUTH_GSSAPI)
605
      allow_gssapi = TRUE;
606
#endif
607
608
0
    idx = 0;
609
0
    socksreq[idx++] = 5;   /* version */
610
0
    idx++;                 /* number of authentication methods */
611
0
    socksreq[idx++] = 0;   /* no authentication */
612
0
    if(allow_gssapi)
613
0
      socksreq[idx++] = 1; /* GSS-API */
614
0
    if(sx->proxy_user)
615
0
      socksreq[idx++] = 2; /* username/password */
616
    /* write the number of authentication methods */
617
0
    socksreq[1] = (unsigned char) (idx - 2);
618
619
0
    sx->outp = socksreq;
620
0
    sx->outstanding = idx;
621
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
622
0
                               "initial SOCKS5 request");
623
0
    if(CURLPX_OK != presult)
624
0
      return presult;
625
0
    else if(sx->outstanding) {
626
      /* remain in sending state */
627
0
      return CURLPX_OK;
628
0
    }
629
0
    sxstate(sx, data, CONNECT_SOCKS_READ);
630
0
    goto CONNECT_SOCKS_READ_INIT;
631
0
  case CONNECT_SOCKS_SEND:
632
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
633
0
                               "initial SOCKS5 request");
634
0
    if(CURLPX_OK != presult)
635
0
      return presult;
636
0
    else if(sx->outstanding) {
637
      /* remain in sending state */
638
0
      return CURLPX_OK;
639
0
    }
640
    /* FALLTHROUGH */
641
0
CONNECT_SOCKS_READ_INIT:
642
0
  case CONNECT_SOCKS_READ_INIT:
643
0
    sx->outstanding = 2; /* expect two bytes */
644
0
    sx->outp = socksreq; /* store it here */
645
    /* FALLTHROUGH */
646
0
  case CONNECT_SOCKS_READ:
647
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
648
0
                               "initial SOCKS5 response");
649
0
    if(CURLPX_OK != presult)
650
0
      return presult;
651
0
    else if(sx->outstanding) {
652
      /* remain in reading state */
653
0
      return CURLPX_OK;
654
0
    }
655
0
    else if(socksreq[0] != 5) {
656
0
      failf(data, "Received invalid version in initial SOCKS5 response.");
657
0
      return CURLPX_BAD_VERSION;
658
0
    }
659
0
    else if(socksreq[1] == 0) {
660
      /* DONE! No authentication needed. Send request. */
661
0
      sxstate(sx, data, CONNECT_REQ_INIT);
662
0
      goto CONNECT_REQ_INIT;
663
0
    }
664
0
    else if(socksreq[1] == 2) {
665
      /* regular name + password authentication */
666
0
      sxstate(sx, data, CONNECT_AUTH_INIT);
667
0
      goto CONNECT_AUTH_INIT;
668
0
    }
669
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
670
    else if(allow_gssapi && (socksreq[1] == 1)) {
671
      sxstate(sx, data, CONNECT_GSSAPI_INIT);
672
      result = Curl_SOCKS5_gssapi_negotiate(cf, data);
673
      if(result) {
674
        failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
675
        return CURLPX_GSSAPI;
676
      }
677
    }
678
#endif
679
0
    else {
680
      /* error */
681
0
      if(!allow_gssapi && (socksreq[1] == 1)) {
682
0
        failf(data,
683
0
              "SOCKS5 GSSAPI per-message authentication is not supported.");
684
0
        return CURLPX_GSSAPI_PERMSG;
685
0
      }
686
0
      else if(socksreq[1] == 255) {
687
0
        failf(data, "No authentication method was acceptable.");
688
0
        return CURLPX_NO_AUTH;
689
0
      }
690
0
    }
691
0
    failf(data,
692
0
          "Undocumented SOCKS5 mode attempted to be used by server.");
693
0
    return CURLPX_UNKNOWN_MODE;
694
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
695
  case CONNECT_GSSAPI_INIT:
696
    /* GSSAPI stuff done non-blocking */
697
    break;
698
#endif
699
700
0
  default: /* do nothing! */
701
0
    break;
702
703
0
CONNECT_AUTH_INIT:
704
0
  case CONNECT_AUTH_INIT: {
705
    /* Needs user name and password */
706
0
    size_t proxy_user_len, proxy_password_len;
707
0
    if(sx->proxy_user && sx->proxy_password) {
708
0
      proxy_user_len = strlen(sx->proxy_user);
709
0
      proxy_password_len = strlen(sx->proxy_password);
710
0
    }
711
0
    else {
712
0
      proxy_user_len = 0;
713
0
      proxy_password_len = 0;
714
0
    }
715
716
    /*   username/password request looks like
717
     * +----+------+----------+------+----------+
718
     * |VER | ULEN |  UNAME   | PLEN |  PASSWD  |
719
     * +----+------+----------+------+----------+
720
     * | 1  |  1   | 1 to 255 |  1   | 1 to 255 |
721
     * +----+------+----------+------+----------+
722
     */
723
0
    len = 0;
724
0
    socksreq[len++] = 1;    /* username/pw subnegotiation version */
725
0
    socksreq[len++] = (unsigned char) proxy_user_len;
726
0
    if(sx->proxy_user && proxy_user_len) {
727
      /* the length must fit in a single byte */
728
0
      if(proxy_user_len > 255) {
729
0
        failf(data, "Excessive user name length for proxy auth");
730
0
        return CURLPX_LONG_USER;
731
0
      }
732
0
      memcpy(socksreq + len, sx->proxy_user, proxy_user_len);
733
0
    }
734
0
    len += proxy_user_len;
735
0
    socksreq[len++] = (unsigned char) proxy_password_len;
736
0
    if(sx->proxy_password && proxy_password_len) {
737
      /* the length must fit in a single byte */
738
0
      if(proxy_password_len > 255) {
739
0
        failf(data, "Excessive password length for proxy auth");
740
0
        return CURLPX_LONG_PASSWD;
741
0
      }
742
0
      memcpy(socksreq + len, sx->proxy_password, proxy_password_len);
743
0
    }
744
0
    len += proxy_password_len;
745
0
    sxstate(sx, data, CONNECT_AUTH_SEND);
746
0
    sx->outstanding = len;
747
0
    sx->outp = socksreq;
748
0
  }
749
    /* FALLTHROUGH */
750
0
  case CONNECT_AUTH_SEND:
751
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_AUTH,
752
0
                               "SOCKS5 sub-negotiation request");
753
0
    if(CURLPX_OK != presult)
754
0
      return presult;
755
0
    else if(sx->outstanding) {
756
      /* remain in sending state */
757
0
      return CURLPX_OK;
758
0
    }
759
0
    sx->outp = socksreq;
760
0
    sx->outstanding = 2;
761
0
    sxstate(sx, data, CONNECT_AUTH_READ);
762
    /* FALLTHROUGH */
763
0
  case CONNECT_AUTH_READ:
764
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_AUTH,
765
0
                               "SOCKS5 sub-negotiation response");
766
0
    if(CURLPX_OK != presult)
767
0
      return presult;
768
0
    else if(sx->outstanding) {
769
      /* remain in reading state */
770
0
      return CURLPX_OK;
771
0
    }
772
    /* ignore the first (VER) byte */
773
0
    else if(socksreq[1]) { /* status */
774
0
      failf(data, "User was rejected by the SOCKS5 server (%d %d).",
775
0
            socksreq[0], socksreq[1]);
776
0
      return CURLPX_USER_REJECTED;
777
0
    }
778
779
    /* Everything is good so far, user was authenticated! */
780
0
    sxstate(sx, data, CONNECT_REQ_INIT);
781
    /* FALLTHROUGH */
782
0
CONNECT_REQ_INIT:
783
0
  case CONNECT_REQ_INIT:
784
0
    if(socks5_resolve_local) {
785
0
      enum resolve_t rc = Curl_resolv(data, sx->hostname, sx->remote_port,
786
0
                                      TRUE, &dns);
787
788
0
      if(rc == CURLRESOLV_ERROR)
789
0
        return CURLPX_RESOLVE_HOST;
790
791
0
      if(rc == CURLRESOLV_PENDING) {
792
0
        sxstate(sx, data, CONNECT_RESOLVING);
793
0
        return CURLPX_OK;
794
0
      }
795
0
      sxstate(sx, data, CONNECT_RESOLVED);
796
0
      goto CONNECT_RESOLVED;
797
0
    }
798
0
    goto CONNECT_RESOLVE_REMOTE;
799
800
0
  case CONNECT_RESOLVING:
801
    /* check if we have the name resolved by now */
802
0
    dns = Curl_fetch_addr(data, sx->hostname, sx->remote_port);
803
804
0
    if(dns) {
805
0
#ifdef CURLRES_ASYNCH
806
0
      data->state.async.dns = dns;
807
0
      data->state.async.done = TRUE;
808
0
#endif
809
0
      infof(data, "SOCKS5: hostname '%s' found", sx->hostname);
810
0
    }
811
812
0
    if(!dns) {
813
0
      result = Curl_resolv_check(data, &dns);
814
0
      if(!dns) {
815
0
        if(result)
816
0
          return CURLPX_RESOLVE_HOST;
817
0
        return CURLPX_OK;
818
0
      }
819
0
    }
820
    /* FALLTHROUGH */
821
0
CONNECT_RESOLVED:
822
0
  case CONNECT_RESOLVED: {
823
0
    struct Curl_addrinfo *hp = NULL;
824
0
    size_t destlen;
825
0
    if(dns)
826
0
      hp = dns->addr;
827
0
    if(!hp) {
828
0
      failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
829
0
            sx->hostname);
830
0
      return CURLPX_RESOLVE_HOST;
831
0
    }
832
833
0
    Curl_printable_address(hp, dest, sizeof(dest));
834
0
    destlen = strlen(dest);
835
0
    msnprintf(dest + destlen, sizeof(dest) - destlen, ":%d", sx->remote_port);
836
837
0
    len = 0;
838
0
    socksreq[len++] = 5; /* version (SOCKS5) */
839
0
    socksreq[len++] = 1; /* connect */
840
0
    socksreq[len++] = 0; /* must be zero */
841
0
    if(hp->ai_family == AF_INET) {
842
0
      int i;
843
0
      struct sockaddr_in *saddr_in;
844
0
      socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
845
846
0
      saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
847
0
      for(i = 0; i < 4; i++) {
848
0
        socksreq[len++] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[i];
849
0
      }
850
851
0
      infof(data, "SOCKS5 connect to IPv4 %s (locally resolved)", dest);
852
0
    }
853
0
#ifdef ENABLE_IPV6
854
0
    else if(hp->ai_family == AF_INET6) {
855
0
      int i;
856
0
      struct sockaddr_in6 *saddr_in6;
857
0
      socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
858
859
0
      saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr;
860
0
      for(i = 0; i < 16; i++) {
861
0
        socksreq[len++] =
862
0
          ((unsigned char *)&saddr_in6->sin6_addr.s6_addr)[i];
863
0
      }
864
865
0
      infof(data, "SOCKS5 connect to IPv6 %s (locally resolved)", dest);
866
0
    }
867
0
#endif
868
0
    else {
869
0
      hp = NULL; /* fail! */
870
0
      failf(data, "SOCKS5 connection to %s not supported", dest);
871
0
    }
872
873
0
    Curl_resolv_unlock(data, dns); /* not used anymore from now on */
874
0
    goto CONNECT_REQ_SEND;
875
0
  }
876
0
CONNECT_RESOLVE_REMOTE:
877
0
  case CONNECT_RESOLVE_REMOTE:
878
    /* Authentication is complete, now specify destination to the proxy */
879
0
    len = 0;
880
0
    socksreq[len++] = 5; /* version (SOCKS5) */
881
0
    socksreq[len++] = 1; /* connect */
882
0
    socksreq[len++] = 0; /* must be zero */
883
884
0
    if(!socks5_resolve_local) {
885
      /* ATYP: domain name = 3,
886
         IPv6 == 4,
887
         IPv4 == 1 */
888
0
      unsigned char ip4[4];
889
0
#ifdef ENABLE_IPV6
890
0
      if(conn->bits.ipv6_ip) {
891
0
        char ip6[16];
892
0
        if(1 != Curl_inet_pton(AF_INET6, sx->hostname, ip6))
893
0
          return CURLPX_BAD_ADDRESS_TYPE;
894
0
        socksreq[len++] = 4;
895
0
        memcpy(&socksreq[len], ip6, sizeof(ip6));
896
0
        len += sizeof(ip6);
897
0
      }
898
0
      else
899
0
#endif
900
0
      if(1 == Curl_inet_pton(AF_INET, sx->hostname, ip4)) {
901
0
        socksreq[len++] = 1;
902
0
        memcpy(&socksreq[len], ip4, sizeof(ip4));
903
0
        len += sizeof(ip4);
904
0
      }
905
0
      else {
906
0
        socksreq[len++] = 3;
907
0
        socksreq[len++] = (char) hostname_len; /* one byte address length */
908
0
        memcpy(&socksreq[len], sx->hostname, hostname_len); /* w/o NULL */
909
0
        len += hostname_len;
910
0
      }
911
0
      infof(data, "SOCKS5 connect to %s:%d (remotely resolved)",
912
0
            sx->hostname, sx->remote_port);
913
0
    }
914
    /* FALLTHROUGH */
915
916
0
CONNECT_REQ_SEND:
917
0
  case CONNECT_REQ_SEND:
918
    /* PORT MSB */
919
0
    socksreq[len++] = (unsigned char)((sx->remote_port >> 8) & 0xff);
920
    /* PORT LSB */
921
0
    socksreq[len++] = (unsigned char)(sx->remote_port & 0xff);
922
923
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
924
    if(conn->socks5_gssapi_enctype) {
925
      failf(data, "SOCKS5 GSS-API protection not yet implemented.");
926
      return CURLPX_GSSAPI_PROTECTION;
927
    }
928
#endif
929
0
    sx->outp = socksreq;
930
0
    sx->outstanding = len;
931
0
    sxstate(sx, data, CONNECT_REQ_SENDING);
932
    /* FALLTHROUGH */
933
0
  case CONNECT_REQ_SENDING:
934
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_REQUEST,
935
0
                               "SOCKS5 connect request");
936
0
    if(CURLPX_OK != presult)
937
0
      return presult;
938
0
    else if(sx->outstanding) {
939
      /* remain in send state */
940
0
      return CURLPX_OK;
941
0
    }
942
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
943
    if(conn->socks5_gssapi_enctype) {
944
      failf(data, "SOCKS5 GSS-API protection not yet implemented.");
945
      return CURLPX_GSSAPI_PROTECTION;
946
    }
947
#endif
948
0
    sx->outstanding = 10; /* minimum packet size is 10 */
949
0
    sx->outp = socksreq;
950
0
    sxstate(sx, data, CONNECT_REQ_READ);
951
    /* FALLTHROUGH */
952
0
  case CONNECT_REQ_READ:
953
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_REQACK,
954
0
                               "SOCKS5 connect request ack");
955
0
    if(CURLPX_OK != presult)
956
0
      return presult;
957
0
    else if(sx->outstanding) {
958
      /* remain in reading state */
959
0
      return CURLPX_OK;
960
0
    }
961
0
    else if(socksreq[0] != 5) { /* version */
962
0
      failf(data,
963
0
            "SOCKS5 reply has wrong version, version should be 5.");
964
0
      return CURLPX_BAD_VERSION;
965
0
    }
966
0
    else if(socksreq[1]) { /* Anything besides 0 is an error */
967
0
      CURLproxycode rc = CURLPX_REPLY_UNASSIGNED;
968
0
      int code = socksreq[1];
969
0
      failf(data, "Can't complete SOCKS5 connection to %s. (%d)",
970
0
            sx->hostname, (unsigned char)socksreq[1]);
971
0
      if(code < 9) {
972
        /* RFC 1928 section 6 lists: */
973
0
        static const CURLproxycode lookup[] = {
974
0
          CURLPX_OK,
975
0
          CURLPX_REPLY_GENERAL_SERVER_FAILURE,
976
0
          CURLPX_REPLY_NOT_ALLOWED,
977
0
          CURLPX_REPLY_NETWORK_UNREACHABLE,
978
0
          CURLPX_REPLY_HOST_UNREACHABLE,
979
0
          CURLPX_REPLY_CONNECTION_REFUSED,
980
0
          CURLPX_REPLY_TTL_EXPIRED,
981
0
          CURLPX_REPLY_COMMAND_NOT_SUPPORTED,
982
0
          CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED,
983
0
        };
984
0
        rc = lookup[code];
985
0
      }
986
0
      return rc;
987
0
    }
988
989
    /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
990
       1928, so the reply packet should be read until the end to avoid errors
991
       at subsequent protocol level.
992
993
       +----+-----+-------+------+----------+----------+
994
       |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
995
       +----+-----+-------+------+----------+----------+
996
       | 1  |  1  | X'00' |  1   | Variable |    2     |
997
       +----+-----+-------+------+----------+----------+
998
999
       ATYP:
1000
       o  IP v4 address: X'01', BND.ADDR = 4 byte
1001
       o  domain name:  X'03', BND.ADDR = [ 1 byte length, string ]
1002
       o  IP v6 address: X'04', BND.ADDR = 16 byte
1003
    */
1004
1005
    /* Calculate real packet size */
1006
0
    if(socksreq[3] == 3) {
1007
      /* domain name */
1008
0
      int addrlen = (int) socksreq[4];
1009
0
      len = 5 + addrlen + 2;
1010
0
    }
1011
0
    else if(socksreq[3] == 4) {
1012
      /* IPv6 */
1013
0
      len = 4 + 16 + 2;
1014
0
    }
1015
0
    else if(socksreq[3] == 1) {
1016
0
      len = 4 + 4 + 2;
1017
0
    }
1018
0
    else {
1019
0
      failf(data, "SOCKS5 reply has wrong address type.");
1020
0
      return CURLPX_BAD_ADDRESS_TYPE;
1021
0
    }
1022
1023
    /* At this point we already read first 10 bytes */
1024
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1025
    if(!conn->socks5_gssapi_enctype) {
1026
      /* decrypt_gssapi_blockread already read the whole packet */
1027
#endif
1028
0
      if(len > 10) {
1029
0
        sx->outstanding = len - 10; /* get the rest */
1030
0
        sx->outp = &socksreq[10];
1031
0
        sxstate(sx, data, CONNECT_REQ_READ_MORE);
1032
0
      }
1033
0
      else {
1034
0
        sxstate(sx, data, CONNECT_DONE);
1035
0
        break;
1036
0
      }
1037
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1038
    }
1039
#endif
1040
    /* FALLTHROUGH */
1041
0
  case CONNECT_REQ_READ_MORE:
1042
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_ADDRESS,
1043
0
                               "SOCKS5 connect request address");
1044
0
    if(CURLPX_OK != presult)
1045
0
      return presult;
1046
0
    else if(sx->outstanding) {
1047
      /* remain in reading state */
1048
0
      return CURLPX_OK;
1049
0
    }
1050
0
    sxstate(sx, data, CONNECT_DONE);
1051
0
  }
1052
0
  infof(data, "SOCKS5 request granted.");
1053
1054
0
  return CURLPX_OK; /* Proxy was successful! */
1055
0
}
1056
1057
static CURLcode connect_SOCKS(struct Curl_cfilter *cf,
1058
                              struct socks_state *sxstate,
1059
                              struct Curl_easy *data)
1060
0
{
1061
0
  CURLcode result = CURLE_OK;
1062
0
  CURLproxycode pxresult = CURLPX_OK;
1063
0
  struct connectdata *conn = cf->conn;
1064
1065
0
  switch(conn->socks_proxy.proxytype) {
1066
0
  case CURLPROXY_SOCKS5:
1067
0
  case CURLPROXY_SOCKS5_HOSTNAME:
1068
0
    pxresult = do_SOCKS5(cf, sxstate, data);
1069
0
    break;
1070
1071
0
  case CURLPROXY_SOCKS4:
1072
0
  case CURLPROXY_SOCKS4A:
1073
0
    pxresult = do_SOCKS4(cf, sxstate, data);
1074
0
    break;
1075
1076
0
  default:
1077
0
    failf(data, "unknown proxytype option given");
1078
0
    result = CURLE_COULDNT_CONNECT;
1079
0
  } /* switch proxytype */
1080
0
  if(pxresult) {
1081
0
    result = CURLE_PROXY;
1082
0
    data->info.pxcode = pxresult;
1083
0
  }
1084
1085
0
  return result;
1086
0
}
1087
1088
static void socks_proxy_cf_free(struct Curl_cfilter *cf)
1089
0
{
1090
0
  struct socks_state *sxstate = cf->ctx;
1091
0
  if(sxstate) {
1092
0
    free(sxstate);
1093
0
    cf->ctx = NULL;
1094
0
  }
1095
0
}
1096
1097
/* After a TCP connection to the proxy has been verified, this function does
1098
   the next magic steps. If 'done' isn't set TRUE, it is not done yet and
1099
   must be called again.
1100
1101
   Note: this function's sub-functions call failf()
1102
1103
*/
1104
static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf,
1105
                                       struct Curl_easy *data,
1106
                                       bool blocking, bool *done)
1107
0
{
1108
0
  CURLcode result;
1109
0
  struct connectdata *conn = cf->conn;
1110
0
  int sockindex = cf->sockindex;
1111
0
  struct socks_state *sx = cf->ctx;
1112
1113
0
  if(cf->connected) {
1114
0
    *done = TRUE;
1115
0
    return CURLE_OK;
1116
0
  }
1117
1118
0
  result = cf->next->cft->connect(cf->next, data, blocking, done);
1119
0
  if(result || !*done)
1120
0
    return result;
1121
1122
0
  if(!sx) {
1123
0
    sx = calloc(sizeof(*sx), 1);
1124
0
    if(!sx)
1125
0
      return CURLE_OUT_OF_MEMORY;
1126
0
    cf->ctx = sx;
1127
0
  }
1128
1129
0
  if(sx->state == CONNECT_INIT) {
1130
    /* for the secondary socket (FTP), use the "connect to host"
1131
     * but ignore the "connect to port" (use the secondary port)
1132
     */
1133
0
    sxstate(sx, data, CONNECT_SOCKS_INIT);
1134
0
    sx->hostname =
1135
0
      conn->bits.httpproxy ?
1136
0
      conn->http_proxy.host.name :
1137
0
      conn->bits.conn_to_host ?
1138
0
      conn->conn_to_host.name :
1139
0
      sockindex == SECONDARYSOCKET ?
1140
0
      conn->secondaryhostname : conn->host.name;
1141
0
    sx->remote_port =
1142
0
      conn->bits.httpproxy ? (int)conn->http_proxy.port :
1143
0
      sockindex == SECONDARYSOCKET ? conn->secondary_port :
1144
0
      conn->bits.conn_to_port ? conn->conn_to_port :
1145
0
      conn->remote_port;
1146
0
    sx->proxy_user = conn->socks_proxy.user;
1147
0
    sx->proxy_password = conn->socks_proxy.passwd;
1148
0
  }
1149
1150
0
  result = connect_SOCKS(cf, sx, data);
1151
0
  if(!result && sx->state == CONNECT_DONE) {
1152
0
    cf->connected = TRUE;
1153
0
    Curl_verboseconnect(data, conn);
1154
0
    socks_proxy_cf_free(cf);
1155
0
  }
1156
1157
0
  *done = cf->connected;
1158
0
  return result;
1159
0
}
1160
1161
static int socks_cf_get_select_socks(struct Curl_cfilter *cf,
1162
                                     struct Curl_easy *data,
1163
                                     curl_socket_t *socks)
1164
0
{
1165
0
  struct socks_state *sx = cf->ctx;
1166
0
  int fds;
1167
1168
0
  fds = cf->next->cft->get_select_socks(cf->next, data, socks);
1169
0
  if(!fds && cf->next->connected && !cf->connected && sx) {
1170
    /* If we are not connected, the filter below is and has nothing
1171
     * to wait on, we determine what to wait for. */
1172
0
    socks[0] = Curl_conn_cf_get_socket(cf, data);
1173
0
    switch(sx->state) {
1174
0
    case CONNECT_RESOLVING:
1175
0
    case CONNECT_SOCKS_READ:
1176
0
    case CONNECT_AUTH_READ:
1177
0
    case CONNECT_REQ_READ:
1178
0
    case CONNECT_REQ_READ_MORE:
1179
0
      fds = GETSOCK_READSOCK(0);
1180
0
      break;
1181
0
    default:
1182
0
      fds = GETSOCK_WRITESOCK(0);
1183
0
      break;
1184
0
    }
1185
0
  }
1186
0
  return fds;
1187
0
}
1188
1189
static void socks_proxy_cf_close(struct Curl_cfilter *cf,
1190
                                 struct Curl_easy *data)
1191
0
{
1192
1193
0
  DEBUGASSERT(cf->next);
1194
0
  cf->connected = FALSE;
1195
0
  socks_proxy_cf_free(cf);
1196
0
  cf->next->cft->close(cf->next, data);
1197
0
}
1198
1199
static void socks_proxy_cf_destroy(struct Curl_cfilter *cf,
1200
                                   struct Curl_easy *data)
1201
0
{
1202
0
  (void)data;
1203
0
  socks_proxy_cf_free(cf);
1204
0
}
1205
1206
static void socks_cf_get_host(struct Curl_cfilter *cf,
1207
                              struct Curl_easy *data,
1208
                              const char **phost,
1209
                              const char **pdisplay_host,
1210
                              int *pport)
1211
0
{
1212
0
  (void)data;
1213
0
  if(!cf->connected) {
1214
0
    *phost = cf->conn->socks_proxy.host.name;
1215
0
    *pdisplay_host = cf->conn->http_proxy.host.dispname;
1216
0
    *pport = (int)cf->conn->socks_proxy.port;
1217
0
  }
1218
0
  else {
1219
0
    cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
1220
0
  }
1221
0
}
1222
1223
struct Curl_cftype Curl_cft_socks_proxy = {
1224
  "SOCKS-PROXYY",
1225
  CF_TYPE_IP_CONNECT,
1226
  0,
1227
  socks_proxy_cf_destroy,
1228
  socks_proxy_cf_connect,
1229
  socks_proxy_cf_close,
1230
  socks_cf_get_host,
1231
  socks_cf_get_select_socks,
1232
  Curl_cf_def_data_pending,
1233
  Curl_cf_def_send,
1234
  Curl_cf_def_recv,
1235
  Curl_cf_def_cntrl,
1236
  Curl_cf_def_conn_is_alive,
1237
  Curl_cf_def_conn_keep_alive,
1238
  Curl_cf_def_query,
1239
};
1240
1241
CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at,
1242
                                          struct Curl_easy *data)
1243
0
{
1244
0
  struct Curl_cfilter *cf;
1245
0
  CURLcode result;
1246
1247
0
  (void)data;
1248
0
  result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, NULL);
1249
0
  if(!result)
1250
0
    Curl_conn_cf_insert_after(cf_at, cf);
1251
0
  return result;
1252
0
}
1253
1254
#endif /* CURL_DISABLE_PROXY */