1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2015 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* SOCKS version 5 proxy, client-mode */
13 #ifdef SUPPORT_SOCKS /* entire file */
16 # define nelem(arr) (sizeof(arr)/sizeof(*arr))
21 #define SOCKS_PORT 1080
22 #define SOCKS_TIMEOUT 5
23 #define SOCKS_WEIGHT 1
24 #define SOCKS_PRIORITY 1
27 #define AUTH_NAME 2 /* user/password per RFC 1929 */
28 #define AUTH_NAME_VER 1
37 {US"general SOCKS server failure", EIO},
38 {US"connection not allowed by ruleset", EACCES},
39 {US"Network unreachable", ENETUNREACH},
40 {US"Host unreachable", EHOSTUNREACH},
41 {US"Connection refused", ECONNREFUSED},
42 {US"TTL expired", ECANCELED},
43 {US"Command not supported", EOPNOTSUPP},
44 {US"Address type not supported", EAFNOSUPPORT}
49 const uschar * proxy_host;
50 uschar auth_type; /* RFC 1928 encoding */
51 const uschar * auth_name;
52 const uschar * auth_pwd;
61 socks_option_defaults(socks_opts * sob)
63 sob->proxy_host = NULL;
64 sob->auth_type = AUTH_NONE;
65 sob->auth_name = US"";
67 sob->is_failed = FALSE;
68 sob->port = SOCKS_PORT;
69 sob->timeout = SOCKS_TIMEOUT;
70 sob->weight = SOCKS_WEIGHT;
71 sob->priority = SOCKS_PRIORITY;
75 socks_option(socks_opts * sob, const uschar * opt)
77 if (Ustrncmp(opt, "auth=", 5) == 0)
80 if (Ustrcmp(opt, "none") == 0) sob->auth_type = AUTH_NONE;
81 else if (Ustrcmp(opt, "name") == 0) sob->auth_type = AUTH_NAME;
83 else if (Ustrncmp(opt, "name=", 5) == 0)
84 sob->auth_name = opt + 5;
85 else if (Ustrncmp(opt, "pass=", 5) == 0)
86 sob->auth_pwd = opt + 5;
87 else if (Ustrncmp(opt, "port=", 5) == 0)
88 sob->port = atoi(CCS opt + 5);
89 else if (Ustrncmp(opt, "tmo=", 4) == 0)
90 sob->timeout = atoi(CCS opt + 4);
91 else if (Ustrncmp(opt, "pri=", 4) == 0)
92 sob->priority = atoi(CCS opt + 4);
93 else if (Ustrncmp(opt, "weight=", 7) == 0)
94 sob->weight = atoi(CCS opt + 7);
99 socks_auth(int fd, int method, socks_opts * sob, time_t tmo)
107 log_write(0, LOG_MAIN|LOG_PANIC,
108 "Unrecognised socks auth method %d", method);
113 HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" socks auth NAME '%s' '%s'\n",
114 sob->auth_name, sob->auth_pwd);
115 i = Ustrlen(sob->auth_name);
116 j = Ustrlen(sob->auth_pwd);
117 s = string_sprintf("%c%c%.255s%c%.255s", AUTH_NAME_VER,
118 i, sob->auth_name, j, sob->auth_pwd);
120 HDEBUG(D_transport|D_acl|D_v)
122 debug_printf_indent(" SOCKS>>");
123 for (int i = 0; i<len; i++) debug_printf(" %02x", s[i]);
126 if (send(fd, s, len, 0) < 0)
129 (void) setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, US &off, sizeof(off));
131 if (!fd_ready(fd, tmo-time(NULL)) || read(fd, s, 2) != 2)
133 HDEBUG(D_transport|D_acl|D_v)
134 debug_printf_indent(" SOCKS<< %02x %02x\n", s[0], s[1]);
135 if (s[0] == AUTH_NAME_VER && s[1] == 0)
137 HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" socks auth OK\n");
141 log_write(0, LOG_MAIN|LOG_PANIC, "socks auth failed");
149 /* Find a suitable proxy to use from the list.
150 Possible common code with spamd_get_server() ?
152 Return: index into proxy spec array, or -1
156 socks_get_proxy(socks_opts * proxies, unsigned nproxies)
160 socks_opts * lim = &proxies[nproxies];
163 static BOOL srandomed = FALSE;
165 if (nproxies == 1) /* shortcut, if we have only 1 server */
166 return (proxies[0].is_failed ? -1 : 0);
172 gettimeofday(&tv, NULL);
173 srandom((unsigned int)(tv.tv_usec/1000));
177 /* scan for highest pri */
178 for (pri = 0, sd = proxies; sd < lim; sd++)
179 if (!sd->is_failed && sd->priority > pri)
182 /* get sum of weights at this pri */
183 for (weights = 0, sd = proxies; sd < lim; sd++)
184 if (!sd->is_failed && sd->priority == pri)
185 weights += sd->weight;
186 if (weights == 0) /* all servers failed */
189 for (rnd = random() % weights, i = 0; i < nproxies; i++)
192 if (!sd->is_failed && sd->priority == pri)
193 if ((rnd -= sd->weight) <= 0)
197 log_write(0, LOG_MAIN|LOG_PANIC,
198 "%s unknown error (memory/cpu corruption?)", __FUNCTION__);
204 /* Make a connection via a socks proxy
207 host smtp target host
208 host_af address family
209 port remote tcp port number
210 interface local interface
212 timeout connection timeout (zero for indefinite)
215 0 on success; -1 on failure, with errno set
219 socks_sock_connect(host_item * host, int host_af, int port, uschar * interface,
220 transport_instance * tb, int timeout)
222 smtp_transport_options_block * ob =
223 (smtp_transport_options_block *)tb->options_block;
224 const uschar * proxy_list;
225 const uschar * proxy_spec;
229 const uschar * state;
231 socks_opts proxies[32]; /* max #proxies handled */
237 if (!timeout) timeout = 24*60*60; /* use 1 day for "indefinite" */
238 tmo = time(NULL) + timeout;
240 if (!(proxy_list = expand_string(ob->socks_proxy)))
242 log_write(0, LOG_MAIN|LOG_PANIC, "Bad expansion for socks_proxy in %s",
247 /* Read proxy list */
250 nproxies < nelem(proxies)
251 && (proxy_spec = string_nextinlist(&proxy_list, &sep, NULL, 0));
255 const uschar * option;
257 socks_option_defaults(sob = &proxies[nproxies]);
259 if (!(sob->proxy_host = string_nextinlist(&proxy_spec, &subsep, NULL, 0)))
261 /* paniclog config error */
265 /*XXX consider global options eg. "hide socks_password = wibble" on the tpt */
266 /* extract any further per-proxy options */
267 while ((option = string_nextinlist(&proxy_spec, &subsep, NULL, 0)))
268 socks_option(sob, option);
271 /* Set up the socks protocol method-selection message,
272 for sending on connection */
274 state = US"method select";
275 buf[0] = 5; buf[1] = 1; buf[2] = sob->auth_type;
276 early_data.data = buf;
279 /* Try proxies until a connection succeeds */
287 if ((idx = socks_get_proxy(proxies, nproxies)) < 0)
289 HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" no proxies left\n");
295 /* bodge up a host struct for the proxy */
296 proxy.address = proxy.name = sob->proxy_host;
297 proxy_af = Ustrchr(sob->proxy_host, ':') ? AF_INET6 : AF_INET;
299 /*XXX we trust that the method-select command is idempotent */
300 if ((fd = smtp_sock_connect(&proxy, proxy_af, sob->port,
301 interface, tb, sob->timeout, &early_data)) >= 0)
303 proxy_local_address = string_copy(proxy.address);
304 proxy_local_port = sob->port;
308 log_write(0, LOG_MAIN, "%s: %s", __FUNCTION__, strerror(errno));
309 sob->is_failed = TRUE;
312 /* Do the socks protocol stuff */
314 HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" SOCKS>> 05 01 %02x\n", sob->auth_type);
316 /* expect method response */
319 (void) setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, US &off, sizeof(off));
322 if ( !fd_ready(fd, tmo-time(NULL))
323 || read(fd, buf, 2) != 2
326 HDEBUG(D_transport|D_acl|D_v)
327 debug_printf_indent(" SOCKS<< %02x %02x\n", buf[0], buf[1]);
329 || socks_auth(fd, buf[1], sob, tmo) != OK
334 union sockaddr_46 sin;
335 (void) ip_addr(&sin, host_af, host->address, port);
337 /* send connect (ipver, ipaddr, port) */
339 buf[0] = 5; buf[1] = 1; buf[2] = 0; buf[3] = host_af == AF_INET6 ? 4 : 1;
341 if (host_af == AF_INET6)
343 memcpy(buf+4, &sin.v6.sin6_addr, sizeof(sin.v6.sin6_addr));
344 memcpy(buf+4+sizeof(sin.v6.sin6_addr),
345 &sin.v6.sin6_port, sizeof(sin.v6.sin6_port));
346 size = 4+sizeof(sin.v6.sin6_addr)+sizeof(sin.v6.sin6_port);
351 memcpy(buf+4, &sin.v4.sin_addr.s_addr, sizeof(sin.v4.sin_addr.s_addr));
352 memcpy(buf+4+sizeof(sin.v4.sin_addr.s_addr),
353 &sin.v4.sin_port, sizeof(sin.v4.sin_port));
354 size = 4+sizeof(sin.v4.sin_addr.s_addr)+sizeof(sin.v4.sin_port);
359 HDEBUG(D_transport|D_acl|D_v)
361 debug_printf_indent(" SOCKS>>");
362 for (int i = 0; i<size; i++) debug_printf(" %02x", buf[i]);
365 if (send(fd, buf, size, 0) < 0)
368 /* expect conn-reply (success, local(ipver, addr, port))
369 of same length as conn-request, or non-success fail code */
371 if ( !fd_ready(fd, tmo-time(NULL))
372 || (size = read(fd, buf, size)) < 2
375 HDEBUG(D_transport|D_acl|D_v)
377 debug_printf_indent(" SOCKS>>");
378 for (int i = 0; i<size; i++) debug_printf(" %02x", buf[i]);
386 proxy_external_address = string_copy(
387 host_ntoa(buf[3] == 4 ? AF_INET6 : AF_INET, buf+4, NULL, NULL));
388 proxy_external_port = ntohs(*((uint16_t *)(buf + (buf[3] == 4 ? 20 : 8))));
389 proxy_session = TRUE;
391 HDEBUG(D_transport|D_acl|D_v)
392 debug_printf_indent(" proxy farside: [%s]:%d\n", proxy_external_address, proxy_external_port);
397 HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" proxy snd_err %s: %s\n", state, strerror(errno));
402 struct socks_err * se =
403 buf[1] > nelem(socks_errs) ? NULL : socks_errs + buf[1];
404 HDEBUG(D_transport|D_acl|D_v)
405 debug_printf_indent(" proxy %s: %s\n", state, se ? se->reason : US"unknown error code received");
406 errno = se ? se->errcode : EPROTO;
410 HDEBUG(D_transport|D_acl|D_v) debug_printf_indent(" proxy rcv_err %s: %s\n", state, strerror(errno));
411 if (!errno) errno = EPROTO;
412 else if (errno == ENOENT) errno = ECONNABORTED;
416 #endif /* entire file */