Coverage Report

Created: 2023-12-08 06:48

/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 socks_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
        socks_statename[oldstate], socks_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
      conn->resolve_async.dns = dns;
343
0
      conn->resolve_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 > 255) {
406
        /* there is no real size limit to this field in the protocol, but
407
           SOCKS5 limits the proxy user field to 255 bytes and it seems likely
408
           that a longer field is either a mistake or malicious input */
409
0
        failf(data, "Too long SOCKS proxy user name");
410
0
        return CURLPX_LONG_USER;
411
0
      }
412
      /* copy the proxy name WITH trailing zero */
413
0
      memcpy(socksreq + 8, sx->proxy_user, plen + 1);
414
0
    }
415
416
    /*
417
     * Make connection
418
     */
419
0
    {
420
0
      size_t packetsize = 9 +
421
0
        strlen((char *)socksreq + 8); /* size including NUL */
422
423
      /* If SOCKS4a, set special invalid IP address 0.0.0.x */
424
0
      if(protocol4a) {
425
0
        size_t hostnamelen = 0;
426
0
        socksreq[4] = 0;
427
0
        socksreq[5] = 0;
428
0
        socksreq[6] = 0;
429
0
        socksreq[7] = 1;
430
        /* append hostname */
431
0
        hostnamelen = strlen(sx->hostname) + 1; /* length including NUL */
432
0
        if((hostnamelen <= 255) &&
433
0
           (packetsize + hostnamelen < data->set.buffer_size))
434
0
          strcpy((char *)socksreq + packetsize, sx->hostname);
435
0
        else {
436
0
          failf(data, "SOCKS4: too long host name");
437
0
          return CURLPX_LONG_HOSTNAME;
438
0
        }
439
0
        packetsize += hostnamelen;
440
0
      }
441
0
      sx->outp = socksreq;
442
0
      sx->outstanding = packetsize;
443
0
      sxstate(sx, data, CONNECT_REQ_SENDING);
444
0
    }
445
    /* FALLTHROUGH */
446
0
  case CONNECT_REQ_SENDING:
447
    /* Send request */
448
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
449
0
                               "SOCKS4 connect request");
450
0
    if(CURLPX_OK != presult)
451
0
      return presult;
452
0
    else if(sx->outstanding) {
453
      /* remain in sending state */
454
0
      return CURLPX_OK;
455
0
    }
456
    /* done sending! */
457
0
    sx->outstanding = 8; /* receive data size */
458
0
    sx->outp = socksreq;
459
0
    sxstate(sx, data, CONNECT_SOCKS_READ);
460
461
    /* FALLTHROUGH */
462
0
  case CONNECT_SOCKS_READ:
463
    /* Receive response */
464
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
465
0
                               "connect request ack");
466
0
    if(CURLPX_OK != presult)
467
0
      return presult;
468
0
    else if(sx->outstanding) {
469
      /* remain in reading state */
470
0
      return CURLPX_OK;
471
0
    }
472
0
    sxstate(sx, data, CONNECT_DONE);
473
0
    break;
474
0
  default: /* lots of unused states in SOCKS4 */
475
0
    break;
476
0
  }
477
478
  /*
479
   * Response format
480
   *
481
   *     +----+----+----+----+----+----+----+----+
482
   *     | VN | CD | DSTPORT |      DSTIP        |
483
   *     +----+----+----+----+----+----+----+----+
484
   * # of bytes:  1    1      2              4
485
   *
486
   * VN is the version of the reply code and should be 0. CD is the result
487
   * code with one of the following values:
488
   *
489
   * 90: request granted
490
   * 91: request rejected or failed
491
   * 92: request rejected because SOCKS server cannot connect to
492
   *     identd on the client
493
   * 93: request rejected because the client program and identd
494
   *     report different user-ids
495
   */
496
497
  /* wrong version ? */
498
0
  if(socksreq[0]) {
499
0
    failf(data,
500
0
          "SOCKS4 reply has wrong version, version should be 0.");
501
0
    return CURLPX_BAD_VERSION;
502
0
  }
503
504
  /* Result */
505
0
  switch(socksreq[1]) {
506
0
  case 90:
507
0
    infof(data, "SOCKS4%s request granted.", protocol4a?"a":"");
508
0
    break;
509
0
  case 91:
510
0
    failf(data,
511
0
          "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
512
0
          ", request rejected or failed.",
513
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
514
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
515
0
          (unsigned char)socksreq[1]);
516
0
    return CURLPX_REQUEST_FAILED;
517
0
  case 92:
518
0
    failf(data,
519
0
          "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
520
0
          ", request rejected because SOCKS server cannot connect to "
521
0
          "identd on the client.",
522
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
523
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
524
0
          (unsigned char)socksreq[1]);
525
0
    return CURLPX_IDENTD;
526
0
  case 93:
527
0
    failf(data,
528
0
          "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
529
0
          ", request rejected because the client program and identd "
530
0
          "report different user-ids.",
531
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
532
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
533
0
          (unsigned char)socksreq[1]);
534
0
    return CURLPX_IDENTD_DIFFER;
535
0
  default:
536
0
    failf(data,
537
0
          "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
538
0
          ", Unknown.",
539
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
540
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
541
0
          (unsigned char)socksreq[1]);
542
0
    return CURLPX_UNKNOWN_FAIL;
543
0
  }
544
545
0
  return CURLPX_OK; /* Proxy was successful! */
546
0
}
547
548
/*
549
 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
550
 * destination server.
551
 */
552
static CURLproxycode do_SOCKS5(struct Curl_cfilter *cf,
553
                               struct socks_state *sx,
554
                               struct Curl_easy *data)
555
0
{
556
  /*
557
    According to the RFC1928, section "6.  Replies". This is what a SOCK5
558
    replies:
559
560
        +----+-----+-------+------+----------+----------+
561
        |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
562
        +----+-----+-------+------+----------+----------+
563
        | 1  |  1  | X'00' |  1   | Variable |    2     |
564
        +----+-----+-------+------+----------+----------+
565
566
    Where:
567
568
    o  VER    protocol version: X'05'
569
    o  REP    Reply field:
570
    o  X'00' succeeded
571
  */
572
0
  struct connectdata *conn = cf->conn;
573
0
  unsigned char *socksreq = (unsigned char *)data->state.buffer;
574
0
  int idx;
575
0
  CURLcode result;
576
0
  CURLproxycode presult;
577
0
  bool socks5_resolve_local =
578
0
    (conn->socks_proxy.proxytype == CURLPROXY_SOCKS5) ? TRUE : FALSE;
579
0
  const size_t hostname_len = strlen(sx->hostname);
580
0
  ssize_t len = 0;
581
0
  const unsigned char auth = data->set.socks5auth;
582
0
  bool allow_gssapi = FALSE;
583
0
  struct Curl_dns_entry *dns = NULL;
584
585
0
  DEBUGASSERT(auth & (CURLAUTH_BASIC | CURLAUTH_GSSAPI));
586
0
  switch(sx->state) {
587
0
  case CONNECT_SOCKS_INIT:
588
0
    if(conn->bits.httpproxy)
589
0
      infof(data, "SOCKS5: connecting to HTTP proxy %s port %d",
590
0
            sx->hostname, sx->remote_port);
591
592
    /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
593
0
    if(!socks5_resolve_local && hostname_len > 255) {
594
0
      failf(data, "SOCKS5: the destination hostname is too long to be "
595
0
            "resolved remotely by the proxy.");
596
0
      return CURLPX_LONG_HOSTNAME;
597
0
    }
598
599
0
    if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
600
0
      infof(data,
601
0
            "warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %u",
602
0
            auth);
603
0
    if(!(auth & CURLAUTH_BASIC))
604
      /* disable username/password auth */
605
0
      sx->proxy_user = NULL;
606
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
607
    if(auth & CURLAUTH_GSSAPI)
608
      allow_gssapi = TRUE;
609
#endif
610
611
0
    idx = 0;
612
0
    socksreq[idx++] = 5;   /* version */
613
0
    idx++;                 /* number of authentication methods */
614
0
    socksreq[idx++] = 0;   /* no authentication */
615
0
    if(allow_gssapi)
616
0
      socksreq[idx++] = 1; /* GSS-API */
617
0
    if(sx->proxy_user)
618
0
      socksreq[idx++] = 2; /* username/password */
619
    /* write the number of authentication methods */
620
0
    socksreq[1] = (unsigned char) (idx - 2);
621
622
0
    sx->outp = socksreq;
623
0
    sx->outstanding = idx;
624
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
625
0
                               "initial SOCKS5 request");
626
0
    if(CURLPX_OK != presult)
627
0
      return presult;
628
0
    else if(sx->outstanding) {
629
      /* remain in sending state */
630
0
      return CURLPX_OK;
631
0
    }
632
0
    sxstate(sx, data, CONNECT_SOCKS_READ);
633
0
    goto CONNECT_SOCKS_READ_INIT;
634
0
  case CONNECT_SOCKS_SEND:
635
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
636
0
                               "initial SOCKS5 request");
637
0
    if(CURLPX_OK != presult)
638
0
      return presult;
639
0
    else if(sx->outstanding) {
640
      /* remain in sending state */
641
0
      return CURLPX_OK;
642
0
    }
643
    /* FALLTHROUGH */
644
0
CONNECT_SOCKS_READ_INIT:
645
0
  case CONNECT_SOCKS_READ_INIT:
646
0
    sx->outstanding = 2; /* expect two bytes */
647
0
    sx->outp = socksreq; /* store it here */
648
    /* FALLTHROUGH */
649
0
  case CONNECT_SOCKS_READ:
650
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
651
0
                               "initial SOCKS5 response");
652
0
    if(CURLPX_OK != presult)
653
0
      return presult;
654
0
    else if(sx->outstanding) {
655
      /* remain in reading state */
656
0
      return CURLPX_OK;
657
0
    }
658
0
    else if(socksreq[0] != 5) {
659
0
      failf(data, "Received invalid version in initial SOCKS5 response.");
660
0
      return CURLPX_BAD_VERSION;
661
0
    }
662
0
    else if(socksreq[1] == 0) {
663
      /* DONE! No authentication needed. Send request. */
664
0
      sxstate(sx, data, CONNECT_REQ_INIT);
665
0
      goto CONNECT_REQ_INIT;
666
0
    }
667
0
    else if(socksreq[1] == 2) {
668
      /* regular name + password authentication */
669
0
      sxstate(sx, data, CONNECT_AUTH_INIT);
670
0
      goto CONNECT_AUTH_INIT;
671
0
    }
672
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
673
    else if(allow_gssapi && (socksreq[1] == 1)) {
674
      sxstate(sx, data, CONNECT_GSSAPI_INIT);
675
      result = Curl_SOCKS5_gssapi_negotiate(cf, data);
676
      if(result) {
677
        failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
678
        return CURLPX_GSSAPI;
679
      }
680
    }
681
#endif
682
0
    else {
683
      /* error */
684
0
      if(!allow_gssapi && (socksreq[1] == 1)) {
685
0
        failf(data,
686
0
              "SOCKS5 GSSAPI per-message authentication is not supported.");
687
0
        return CURLPX_GSSAPI_PERMSG;
688
0
      }
689
0
      else if(socksreq[1] == 255) {
690
0
        failf(data, "No authentication method was acceptable.");
691
0
        return CURLPX_NO_AUTH;
692
0
      }
693
0
    }
694
0
    failf(data,
695
0
          "Undocumented SOCKS5 mode attempted to be used by server.");
696
0
    return CURLPX_UNKNOWN_MODE;
697
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
698
  case CONNECT_GSSAPI_INIT:
699
    /* GSSAPI stuff done non-blocking */
700
    break;
701
#endif
702
703
0
  default: /* do nothing! */
704
0
    break;
705
706
0
CONNECT_AUTH_INIT:
707
0
  case CONNECT_AUTH_INIT: {
708
    /* Needs user name and password */
709
0
    size_t proxy_user_len, proxy_password_len;
710
0
    if(sx->proxy_user && sx->proxy_password) {
711
0
      proxy_user_len = strlen(sx->proxy_user);
712
0
      proxy_password_len = strlen(sx->proxy_password);
713
0
    }
714
0
    else {
715
0
      proxy_user_len = 0;
716
0
      proxy_password_len = 0;
717
0
    }
718
719
    /*   username/password request looks like
720
     * +----+------+----------+------+----------+
721
     * |VER | ULEN |  UNAME   | PLEN |  PASSWD  |
722
     * +----+------+----------+------+----------+
723
     * | 1  |  1   | 1 to 255 |  1   | 1 to 255 |
724
     * +----+------+----------+------+----------+
725
     */
726
0
    len = 0;
727
0
    socksreq[len++] = 1;    /* username/pw subnegotiation version */
728
0
    socksreq[len++] = (unsigned char) proxy_user_len;
729
0
    if(sx->proxy_user && proxy_user_len) {
730
      /* the length must fit in a single byte */
731
0
      if(proxy_user_len > 255) {
732
0
        failf(data, "Excessive user name length for proxy auth");
733
0
        return CURLPX_LONG_USER;
734
0
      }
735
0
      memcpy(socksreq + len, sx->proxy_user, proxy_user_len);
736
0
    }
737
0
    len += proxy_user_len;
738
0
    socksreq[len++] = (unsigned char) proxy_password_len;
739
0
    if(sx->proxy_password && proxy_password_len) {
740
      /* the length must fit in a single byte */
741
0
      if(proxy_password_len > 255) {
742
0
        failf(data, "Excessive password length for proxy auth");
743
0
        return CURLPX_LONG_PASSWD;
744
0
      }
745
0
      memcpy(socksreq + len, sx->proxy_password, proxy_password_len);
746
0
    }
747
0
    len += proxy_password_len;
748
0
    sxstate(sx, data, CONNECT_AUTH_SEND);
749
0
    sx->outstanding = len;
750
0
    sx->outp = socksreq;
751
0
  }
752
    /* FALLTHROUGH */
753
0
  case CONNECT_AUTH_SEND:
754
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_AUTH,
755
0
                               "SOCKS5 sub-negotiation request");
756
0
    if(CURLPX_OK != presult)
757
0
      return presult;
758
0
    else if(sx->outstanding) {
759
      /* remain in sending state */
760
0
      return CURLPX_OK;
761
0
    }
762
0
    sx->outp = socksreq;
763
0
    sx->outstanding = 2;
764
0
    sxstate(sx, data, CONNECT_AUTH_READ);
765
    /* FALLTHROUGH */
766
0
  case CONNECT_AUTH_READ:
767
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_AUTH,
768
0
                               "SOCKS5 sub-negotiation response");
769
0
    if(CURLPX_OK != presult)
770
0
      return presult;
771
0
    else if(sx->outstanding) {
772
      /* remain in reading state */
773
0
      return CURLPX_OK;
774
0
    }
775
    /* ignore the first (VER) byte */
776
0
    else if(socksreq[1]) { /* status */
777
0
      failf(data, "User was rejected by the SOCKS5 server (%d %d).",
778
0
            socksreq[0], socksreq[1]);
779
0
      return CURLPX_USER_REJECTED;
780
0
    }
781
782
    /* Everything is good so far, user was authenticated! */
783
0
    sxstate(sx, data, CONNECT_REQ_INIT);
784
    /* FALLTHROUGH */
785
0
CONNECT_REQ_INIT:
786
0
  case CONNECT_REQ_INIT:
787
0
    if(socks5_resolve_local) {
788
0
      enum resolve_t rc = Curl_resolv(data, sx->hostname, sx->remote_port,
789
0
                                      TRUE, &dns);
790
791
0
      if(rc == CURLRESOLV_ERROR)
792
0
        return CURLPX_RESOLVE_HOST;
793
794
0
      if(rc == CURLRESOLV_PENDING) {
795
0
        sxstate(sx, data, CONNECT_RESOLVING);
796
0
        return CURLPX_OK;
797
0
      }
798
0
      sxstate(sx, data, CONNECT_RESOLVED);
799
0
      goto CONNECT_RESOLVED;
800
0
    }
801
0
    goto CONNECT_RESOLVE_REMOTE;
802
803
0
  case CONNECT_RESOLVING:
804
    /* check if we have the name resolved by now */
805
0
    dns = Curl_fetch_addr(data, sx->hostname, sx->remote_port);
806
807
0
    if(dns) {
808
0
#ifdef CURLRES_ASYNCH
809
0
      conn->resolve_async.dns = dns;
810
0
      conn->resolve_async.done = TRUE;
811
0
#endif
812
0
      infof(data, "SOCKS5: hostname '%s' found", sx->hostname);
813
0
    }
814
815
0
    if(!dns) {
816
0
      result = Curl_resolv_check(data, &dns);
817
0
      if(!dns) {
818
0
        if(result)
819
0
          return CURLPX_RESOLVE_HOST;
820
0
        return CURLPX_OK;
821
0
      }
822
0
    }
823
    /* FALLTHROUGH */
824
0
CONNECT_RESOLVED:
825
0
  case CONNECT_RESOLVED: {
826
0
    char dest[MAX_IPADR_LEN];  /* printable address */
827
0
    struct Curl_addrinfo *hp = NULL;
828
0
    if(dns)
829
0
      hp = dns->addr;
830
0
#ifdef ENABLE_IPV6
831
0
    if(data->set.ipver != CURL_IPRESOLVE_WHATEVER) {
832
0
      int wanted_family = data->set.ipver == CURL_IPRESOLVE_V4 ?
833
0
        AF_INET : AF_INET6;
834
      /* scan for the first proper address */
835
0
      while(hp && (hp->ai_family != wanted_family))
836
0
        hp = hp->ai_next;
837
0
    }
838
0
#endif
839
0
    if(!hp) {
840
0
      failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
841
0
            sx->hostname);
842
0
      return CURLPX_RESOLVE_HOST;
843
0
    }
844
845
0
    Curl_printable_address(hp, dest, sizeof(dest));
846
847
0
    len = 0;
848
0
    socksreq[len++] = 5; /* version (SOCKS5) */
849
0
    socksreq[len++] = 1; /* connect */
850
0
    socksreq[len++] = 0; /* must be zero */
851
0
    if(hp->ai_family == AF_INET) {
852
0
      int i;
853
0
      struct sockaddr_in *saddr_in;
854
0
      socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
855
856
0
      saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
857
0
      for(i = 0; i < 4; i++) {
858
0
        socksreq[len++] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[i];
859
0
      }
860
861
0
      infof(data, "SOCKS5 connect to %s:%d (locally resolved)", dest,
862
0
            sx->remote_port);
863
0
    }
864
0
#ifdef ENABLE_IPV6
865
0
    else if(hp->ai_family == AF_INET6) {
866
0
      int i;
867
0
      struct sockaddr_in6 *saddr_in6;
868
0
      socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
869
870
0
      saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr;
871
0
      for(i = 0; i < 16; i++) {
872
0
        socksreq[len++] =
873
0
          ((unsigned char *)&saddr_in6->sin6_addr.s6_addr)[i];
874
0
      }
875
876
0
      infof(data, "SOCKS5 connect to [%s]:%d (locally resolved)", dest,
877
0
            sx->remote_port);
878
0
    }
879
0
#endif
880
0
    else {
881
0
      hp = NULL; /* fail! */
882
0
      failf(data, "SOCKS5 connection to %s not supported", dest);
883
0
    }
884
885
0
    Curl_resolv_unlock(data, dns); /* not used anymore from now on */
886
0
    goto CONNECT_REQ_SEND;
887
0
  }
888
0
CONNECT_RESOLVE_REMOTE:
889
0
  case CONNECT_RESOLVE_REMOTE:
890
    /* Authentication is complete, now specify destination to the proxy */
891
0
    len = 0;
892
0
    socksreq[len++] = 5; /* version (SOCKS5) */
893
0
    socksreq[len++] = 1; /* connect */
894
0
    socksreq[len++] = 0; /* must be zero */
895
896
0
    if(!socks5_resolve_local) {
897
      /* ATYP: domain name = 3,
898
         IPv6 == 4,
899
         IPv4 == 1 */
900
0
      unsigned char ip4[4];
901
0
#ifdef ENABLE_IPV6
902
0
      if(conn->bits.ipv6_ip) {
903
0
        char ip6[16];
904
0
        if(1 != Curl_inet_pton(AF_INET6, sx->hostname, ip6))
905
0
          return CURLPX_BAD_ADDRESS_TYPE;
906
0
        socksreq[len++] = 4;
907
0
        memcpy(&socksreq[len], ip6, sizeof(ip6));
908
0
        len += sizeof(ip6);
909
0
      }
910
0
      else
911
0
#endif
912
0
      if(1 == Curl_inet_pton(AF_INET, sx->hostname, ip4)) {
913
0
        socksreq[len++] = 1;
914
0
        memcpy(&socksreq[len], ip4, sizeof(ip4));
915
0
        len += sizeof(ip4);
916
0
      }
917
0
      else {
918
0
        socksreq[len++] = 3;
919
0
        socksreq[len++] = (unsigned char) hostname_len; /* one byte length */
920
0
        memcpy(&socksreq[len], sx->hostname, hostname_len); /* w/o NULL */
921
0
        len += hostname_len;
922
0
      }
923
0
      infof(data, "SOCKS5 connect to %s:%d (remotely resolved)",
924
0
            sx->hostname, sx->remote_port);
925
0
    }
926
    /* FALLTHROUGH */
927
928
0
CONNECT_REQ_SEND:
929
0
  case CONNECT_REQ_SEND:
930
    /* PORT MSB */
931
0
    socksreq[len++] = (unsigned char)((sx->remote_port >> 8) & 0xff);
932
    /* PORT LSB */
933
0
    socksreq[len++] = (unsigned char)(sx->remote_port & 0xff);
934
935
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
936
    if(conn->socks5_gssapi_enctype) {
937
      failf(data, "SOCKS5 GSS-API protection not yet implemented.");
938
      return CURLPX_GSSAPI_PROTECTION;
939
    }
940
#endif
941
0
    sx->outp = socksreq;
942
0
    sx->outstanding = len;
943
0
    sxstate(sx, data, CONNECT_REQ_SENDING);
944
    /* FALLTHROUGH */
945
0
  case CONNECT_REQ_SENDING:
946
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_REQUEST,
947
0
                               "SOCKS5 connect request");
948
0
    if(CURLPX_OK != presult)
949
0
      return presult;
950
0
    else if(sx->outstanding) {
951
      /* remain in send state */
952
0
      return CURLPX_OK;
953
0
    }
954
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
955
    if(conn->socks5_gssapi_enctype) {
956
      failf(data, "SOCKS5 GSS-API protection not yet implemented.");
957
      return CURLPX_GSSAPI_PROTECTION;
958
    }
959
#endif
960
0
    sx->outstanding = 10; /* minimum packet size is 10 */
961
0
    sx->outp = socksreq;
962
0
    sxstate(sx, data, CONNECT_REQ_READ);
963
    /* FALLTHROUGH */
964
0
  case CONNECT_REQ_READ:
965
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_REQACK,
966
0
                               "SOCKS5 connect request ack");
967
0
    if(CURLPX_OK != presult)
968
0
      return presult;
969
0
    else if(sx->outstanding) {
970
      /* remain in reading state */
971
0
      return CURLPX_OK;
972
0
    }
973
0
    else if(socksreq[0] != 5) { /* version */
974
0
      failf(data,
975
0
            "SOCKS5 reply has wrong version, version should be 5.");
976
0
      return CURLPX_BAD_VERSION;
977
0
    }
978
0
    else if(socksreq[1]) { /* Anything besides 0 is an error */
979
0
      CURLproxycode rc = CURLPX_REPLY_UNASSIGNED;
980
0
      int code = socksreq[1];
981
0
      failf(data, "Can't complete SOCKS5 connection to %s. (%d)",
982
0
            sx->hostname, (unsigned char)socksreq[1]);
983
0
      if(code < 9) {
984
        /* RFC 1928 section 6 lists: */
985
0
        static const CURLproxycode lookup[] = {
986
0
          CURLPX_OK,
987
0
          CURLPX_REPLY_GENERAL_SERVER_FAILURE,
988
0
          CURLPX_REPLY_NOT_ALLOWED,
989
0
          CURLPX_REPLY_NETWORK_UNREACHABLE,
990
0
          CURLPX_REPLY_HOST_UNREACHABLE,
991
0
          CURLPX_REPLY_CONNECTION_REFUSED,
992
0
          CURLPX_REPLY_TTL_EXPIRED,
993
0
          CURLPX_REPLY_COMMAND_NOT_SUPPORTED,
994
0
          CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED,
995
0
        };
996
0
        rc = lookup[code];
997
0
      }
998
0
      return rc;
999
0
    }
1000
1001
    /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
1002
       1928, so the reply packet should be read until the end to avoid errors
1003
       at subsequent protocol level.
1004
1005
       +----+-----+-------+------+----------+----------+
1006
       |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
1007
       +----+-----+-------+------+----------+----------+
1008
       | 1  |  1  | X'00' |  1   | Variable |    2     |
1009
       +----+-----+-------+------+----------+----------+
1010
1011
       ATYP:
1012
       o  IP v4 address: X'01', BND.ADDR = 4 byte
1013
       o  domain name:  X'03', BND.ADDR = [ 1 byte length, string ]
1014
       o  IP v6 address: X'04', BND.ADDR = 16 byte
1015
    */
1016
1017
    /* Calculate real packet size */
1018
0
    if(socksreq[3] == 3) {
1019
      /* domain name */
1020
0
      int addrlen = (int) socksreq[4];
1021
0
      len = 5 + addrlen + 2;
1022
0
    }
1023
0
    else if(socksreq[3] == 4) {
1024
      /* IPv6 */
1025
0
      len = 4 + 16 + 2;
1026
0
    }
1027
0
    else if(socksreq[3] == 1) {
1028
0
      len = 4 + 4 + 2;
1029
0
    }
1030
0
    else {
1031
0
      failf(data, "SOCKS5 reply has wrong address type.");
1032
0
      return CURLPX_BAD_ADDRESS_TYPE;
1033
0
    }
1034
1035
    /* At this point we already read first 10 bytes */
1036
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1037
    if(!conn->socks5_gssapi_enctype) {
1038
      /* decrypt_gssapi_blockread already read the whole packet */
1039
#endif
1040
0
      if(len > 10) {
1041
0
        sx->outstanding = len - 10; /* get the rest */
1042
0
        sx->outp = &socksreq[10];
1043
0
        sxstate(sx, data, CONNECT_REQ_READ_MORE);
1044
0
      }
1045
0
      else {
1046
0
        sxstate(sx, data, CONNECT_DONE);
1047
0
        break;
1048
0
      }
1049
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1050
    }
1051
#endif
1052
    /* FALLTHROUGH */
1053
0
  case CONNECT_REQ_READ_MORE:
1054
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_ADDRESS,
1055
0
                               "SOCKS5 connect request address");
1056
0
    if(CURLPX_OK != presult)
1057
0
      return presult;
1058
0
    else if(sx->outstanding) {
1059
      /* remain in reading state */
1060
0
      return CURLPX_OK;
1061
0
    }
1062
0
    sxstate(sx, data, CONNECT_DONE);
1063
0
  }
1064
0
  infof(data, "SOCKS5 request granted.");
1065
1066
0
  return CURLPX_OK; /* Proxy was successful! */
1067
0
}
1068
1069
static CURLcode connect_SOCKS(struct Curl_cfilter *cf,
1070
                              struct socks_state *sxstate,
1071
                              struct Curl_easy *data)
1072
0
{
1073
0
  CURLcode result = CURLE_OK;
1074
0
  CURLproxycode pxresult = CURLPX_OK;
1075
0
  struct connectdata *conn = cf->conn;
1076
1077
0
  switch(conn->socks_proxy.proxytype) {
1078
0
  case CURLPROXY_SOCKS5:
1079
0
  case CURLPROXY_SOCKS5_HOSTNAME:
1080
0
    pxresult = do_SOCKS5(cf, sxstate, data);
1081
0
    break;
1082
1083
0
  case CURLPROXY_SOCKS4:
1084
0
  case CURLPROXY_SOCKS4A:
1085
0
    pxresult = do_SOCKS4(cf, sxstate, data);
1086
0
    break;
1087
1088
0
  default:
1089
0
    failf(data, "unknown proxytype option given");
1090
0
    result = CURLE_COULDNT_CONNECT;
1091
0
  } /* switch proxytype */
1092
0
  if(pxresult) {
1093
0
    result = CURLE_PROXY;
1094
0
    data->info.pxcode = pxresult;
1095
0
  }
1096
1097
0
  return result;
1098
0
}
1099
1100
static void socks_proxy_cf_free(struct Curl_cfilter *cf)
1101
0
{
1102
0
  struct socks_state *sxstate = cf->ctx;
1103
0
  if(sxstate) {
1104
0
    free(sxstate);
1105
0
    cf->ctx = NULL;
1106
0
  }
1107
0
}
1108
1109
/* After a TCP connection to the proxy has been verified, this function does
1110
   the next magic steps. If 'done' isn't set TRUE, it is not done yet and
1111
   must be called again.
1112
1113
   Note: this function's sub-functions call failf()
1114
1115
*/
1116
static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf,
1117
                                       struct Curl_easy *data,
1118
                                       bool blocking, bool *done)
1119
0
{
1120
0
  CURLcode result;
1121
0
  struct connectdata *conn = cf->conn;
1122
0
  int sockindex = cf->sockindex;
1123
0
  struct socks_state *sx = cf->ctx;
1124
1125
0
  if(cf->connected) {
1126
0
    *done = TRUE;
1127
0
    return CURLE_OK;
1128
0
  }
1129
1130
0
  result = cf->next->cft->do_connect(cf->next, data, blocking, done);
1131
0
  if(result || !*done)
1132
0
    return result;
1133
1134
0
  if(!sx) {
1135
0
    sx = calloc(1, sizeof(*sx));
1136
0
    if(!sx)
1137
0
      return CURLE_OUT_OF_MEMORY;
1138
0
    cf->ctx = sx;
1139
0
  }
1140
1141
0
  if(sx->state == CONNECT_INIT) {
1142
    /* for the secondary socket (FTP), use the "connect to host"
1143
     * but ignore the "connect to port" (use the secondary port)
1144
     */
1145
0
    sxstate(sx, data, CONNECT_SOCKS_INIT);
1146
0
    sx->hostname =
1147
0
      conn->bits.httpproxy ?
1148
0
      conn->http_proxy.host.name :
1149
0
      conn->bits.conn_to_host ?
1150
0
      conn->conn_to_host.name :
1151
0
      sockindex == SECONDARYSOCKET ?
1152
0
      conn->secondaryhostname : conn->host.name;
1153
0
    sx->remote_port =
1154
0
      conn->bits.httpproxy ? (int)conn->http_proxy.port :
1155
0
      sockindex == SECONDARYSOCKET ? conn->secondary_port :
1156
0
      conn->bits.conn_to_port ? conn->conn_to_port :
1157
0
      conn->remote_port;
1158
0
    sx->proxy_user = conn->socks_proxy.user;
1159
0
    sx->proxy_password = conn->socks_proxy.passwd;
1160
0
  }
1161
1162
0
  result = connect_SOCKS(cf, sx, data);
1163
0
  if(!result && sx->state == CONNECT_DONE) {
1164
0
    cf->connected = TRUE;
1165
0
    Curl_verboseconnect(data, conn);
1166
0
    socks_proxy_cf_free(cf);
1167
0
  }
1168
1169
0
  *done = cf->connected;
1170
0
  return result;
1171
0
}
1172
1173
static void socks_cf_adjust_pollset(struct Curl_cfilter *cf,
1174
                                     struct Curl_easy *data,
1175
                                     struct easy_pollset *ps)
1176
0
{
1177
0
  struct socks_state *sx = cf->ctx;
1178
1179
0
  if(!cf->connected && sx) {
1180
    /* If we are not connected, the filter below is and has nothing
1181
     * to wait on, we determine what to wait for. */
1182
0
    curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
1183
0
    switch(sx->state) {
1184
0
    case CONNECT_RESOLVING:
1185
0
    case CONNECT_SOCKS_READ:
1186
0
    case CONNECT_AUTH_READ:
1187
0
    case CONNECT_REQ_READ:
1188
0
    case CONNECT_REQ_READ_MORE:
1189
0
      Curl_pollset_set_in_only(data, ps, sock);
1190
0
      break;
1191
0
    default:
1192
0
      Curl_pollset_set_out_only(data, ps, sock);
1193
0
      break;
1194
0
    }
1195
0
  }
1196
0
}
1197
1198
static void socks_proxy_cf_close(struct Curl_cfilter *cf,
1199
                                 struct Curl_easy *data)
1200
0
{
1201
1202
0
  DEBUGASSERT(cf->next);
1203
0
  cf->connected = FALSE;
1204
0
  socks_proxy_cf_free(cf);
1205
0
  cf->next->cft->do_close(cf->next, data);
1206
0
}
1207
1208
static void socks_proxy_cf_destroy(struct Curl_cfilter *cf,
1209
                                   struct Curl_easy *data)
1210
0
{
1211
0
  (void)data;
1212
0
  socks_proxy_cf_free(cf);
1213
0
}
1214
1215
static void socks_cf_get_host(struct Curl_cfilter *cf,
1216
                              struct Curl_easy *data,
1217
                              const char **phost,
1218
                              const char **pdisplay_host,
1219
                              int *pport)
1220
0
{
1221
0
  (void)data;
1222
0
  if(!cf->connected) {
1223
0
    *phost = cf->conn->socks_proxy.host.name;
1224
0
    *pdisplay_host = cf->conn->http_proxy.host.dispname;
1225
0
    *pport = (int)cf->conn->socks_proxy.port;
1226
0
  }
1227
0
  else {
1228
0
    cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
1229
0
  }
1230
0
}
1231
1232
struct Curl_cftype Curl_cft_socks_proxy = {
1233
  "SOCKS-PROXYY",
1234
  CF_TYPE_IP_CONNECT,
1235
  0,
1236
  socks_proxy_cf_destroy,
1237
  socks_proxy_cf_connect,
1238
  socks_proxy_cf_close,
1239
  socks_cf_get_host,
1240
  socks_cf_adjust_pollset,
1241
  Curl_cf_def_data_pending,
1242
  Curl_cf_def_send,
1243
  Curl_cf_def_recv,
1244
  Curl_cf_def_cntrl,
1245
  Curl_cf_def_conn_is_alive,
1246
  Curl_cf_def_conn_keep_alive,
1247
  Curl_cf_def_query,
1248
};
1249
1250
CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at,
1251
                                          struct Curl_easy *data)
1252
0
{
1253
0
  struct Curl_cfilter *cf;
1254
0
  CURLcode result;
1255
1256
0
  (void)data;
1257
0
  result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, NULL);
1258
0
  if(!result)
1259
0
    Curl_conn_cf_insert_after(cf_at, cf);
1260
0
  return result;
1261
0
}
1262
1263
#endif /* CURL_DISABLE_PROXY */