Coverage Report

Created: 2025-08-26 07:08

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