util-socks.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. SOCKS server utils.
  3. Copyright (C) 2008, 2009, Joe Orton <[email protected]>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "config.h"
  17. #include <sys/types.h>
  18. #ifdef HAVE_STDLIB_H
  19. #include <stdlib.h>
  20. #endif
  21. #ifdef HAVE_STRING_H
  22. #include <string.h>
  23. #endif
  24. #include <time.h> /* for time() */
  25. #include "ne_socket.h"
  26. #include "ne_utils.h"
  27. #include "ne_alloc.h"
  28. #include "child.h"
  29. #include "tests.h"
  30. #include "utils.h"
  31. #define V5_METH_NONE 0x00
  32. #define V5_METH_AUTH 0x02
  33. #define V5_ADDR_IPV4 0x01
  34. #define V5_ADDR_FQDN 0x03
  35. #define V5_ADDR_IPV6 0x04
  36. static int read_socks_string(ne_socket *sock, const char *ctx,
  37. unsigned char *buf, unsigned int *olen)
  38. {
  39. unsigned char len;
  40. ssize_t ret;
  41. ret = ne_sock_read(sock, (char *)&len, 1);
  42. ONV(ret != 1, ("%s length read failed: %s", ctx, ne_sock_error(sock)));
  43. ONV(len == 0, ("%s gave zero-length string", ctx));
  44. ret = ne_sock_fullread(sock, (char *)buf, len);
  45. ONV(ret != 0, ("%s string read failed, got %" NE_FMT_SSIZE_T
  46. " bytes (%s)", ctx, ret, ne_sock_error(sock)));
  47. *olen = len;
  48. return OK;
  49. }
  50. static int read_socks_byte(ne_socket *sock, const char *ctx,
  51. unsigned char *buf)
  52. {
  53. ONV(ne_sock_read(sock, (char *)buf, 1) != 1,
  54. ("%s byte read failed: %s", ctx, ne_sock_error(sock)));
  55. return OK;
  56. }
  57. static int expect_socks_byte(ne_socket *sock, const char *ctx,
  58. unsigned char c)
  59. {
  60. unsigned char b;
  61. CALL(read_socks_byte(sock, ctx, &b));
  62. ONV(b != c, ("%s got byte %hx not %hx", ctx, b, c));
  63. return OK;
  64. }
  65. static int read_socks_0string(ne_socket *sock, const char *ctx,
  66. unsigned char *buf, unsigned *len)
  67. {
  68. unsigned char *end = buf + *len, *p = buf;
  69. while (p < end) {
  70. CALL(read_socks_byte(sock, ctx, p));
  71. if (*p == '\0')
  72. break;
  73. p++;
  74. }
  75. *len = p - buf;
  76. return OK;
  77. }
  78. int socks_server(ne_socket *sock, void *userdata)
  79. {
  80. struct socks_server *srv = userdata;
  81. unsigned char buf[1024];
  82. unsigned int len, port, version;
  83. unsigned char atype;
  84. ssize_t ret;
  85. version = srv->version == NE_SOCK_SOCKSV5 ? 5 : 4;
  86. ne_sock_read_timeout(sock, 5);
  87. CALL(expect_socks_byte(sock, "client version", version));
  88. if (version != 5) {
  89. unsigned char raw[16];
  90. CALL(expect_socks_byte(sock, "v4 command", 0x01));
  91. ret = ne_sock_fullread(sock, (char *)buf, 6);
  92. ONV(ret != 0,
  93. ("v4 address read failed with %" NE_FMT_SSIZE_T
  94. " (%s)", ret, ne_sock_error(sock)));
  95. ONN("bad v4A bogus address",
  96. srv->version == NE_SOCK_SOCKSV4A && srv->expect_addr == NULL
  97. && memcmp(buf + 2, "\0\0\0", 3) != 0 && buf[6] != 0);
  98. ONN("v4 server with no expected address! fail",
  99. srv->version == NE_SOCK_SOCKSV4 && srv->expect_addr == NULL);
  100. if (srv->expect_addr) {
  101. ONN("v4 address mismatch",
  102. memcmp(ne_iaddr_raw(srv->expect_addr, raw), buf + 2, 4) != 0);
  103. }
  104. port = (buf[0] << 8) | buf[1];
  105. ONV(port != srv->expect_port,
  106. ("got bad v4 port %u, expected %u", port, srv->expect_port));
  107. len = sizeof buf;
  108. CALL(read_socks_0string(sock, "v4 username read", buf, &len));
  109. ONV(srv->username == NULL && len, ("unexpected v4 username %s", buf));
  110. ONV(srv->username && !len,
  111. ("no v4 username given, expected %s", srv->username));
  112. ONV(srv->username && len && strcmp(srv->username, (char *)buf),
  113. ("bad v4 username, expected %s got %s", srv->username, buf));
  114. if (srv->expect_addr == NULL) {
  115. len = sizeof buf;
  116. CALL(read_socks_0string(sock, "v4A hostname read", buf, &len));
  117. ONV(strcmp(srv->expect_fqdn, (char *)buf) != 0,
  118. ("bad v4A hostname: %s not %s", buf, srv->expect_fqdn));
  119. }
  120. {
  121. static const char msg[] = "\x00\x5A"
  122. "\x00\x00" "\x00\x00\x00\x00"
  123. "ok!\n";
  124. if (srv->say_hello)
  125. CALL(full_write(sock, msg, 12));
  126. else
  127. CALL(full_write(sock, msg, 8));
  128. }
  129. return srv->server(sock, srv->userdata);
  130. }
  131. CALL(read_socks_string(sock, "client method list", buf, &len));
  132. if (srv->failure == fail_init_vers) {
  133. CALL(full_write(sock, "\x01\x02", 2));
  134. return OK;
  135. }
  136. else if (srv->failure == fail_init_close) {
  137. return OK;
  138. }
  139. else if (srv->failure == fail_init_trunc) {
  140. CALL(full_write(sock, "\x05", 1));
  141. return OK;
  142. }
  143. else if (srv->failure == fail_no_auth) {
  144. CALL(full_write(sock, "\x05\xff", 2));
  145. return OK;
  146. }
  147. else if (srv->failure == fail_bogus_auth) {
  148. CALL(full_write(sock, "\x05\xfe", 2));
  149. return OK;
  150. }
  151. ONN("client did not advertise no-auth method",
  152. memchr(buf, V5_METH_NONE, len) == NULL);
  153. if (srv->username) {
  154. int match = 0;
  155. ONN("client did not advertise authn method",
  156. memchr(buf, V5_METH_AUTH, len) == NULL);
  157. CALL(full_write(sock, "\x05\x02", 2));
  158. CALL(expect_socks_byte(sock, "client auth version", 0x01));
  159. CALL(read_socks_string(sock, "client username", buf, &len));
  160. match = len == strlen(srv->username)
  161. && memcmp(buf, srv->username, len) == 0;
  162. CALL(read_socks_string(sock, "client password", buf, &len));
  163. match = match && len == strlen(srv->password)
  164. && memcmp(buf, srv->password, len) == 0;
  165. if (srv->failure == fail_auth_close) {
  166. return OK;
  167. }
  168. if (match && srv->failure != fail_auth_denied) {
  169. CALL(full_write(sock, "\x01\x00", 2));
  170. }
  171. else {
  172. CALL(full_write(sock, "\x01\x01", 2));
  173. }
  174. if (srv->failure == fail_auth_denied) {
  175. return OK;
  176. }
  177. }
  178. else {
  179. CALL(full_write(sock, "\x05\x00", 2));
  180. }
  181. CALL(expect_socks_byte(sock, "command version", version));
  182. CALL(expect_socks_byte(sock, "command number", 0x01));
  183. CALL(read_socks_byte(sock, "reserved byte", buf));
  184. CALL(read_socks_byte(sock, "address type", &atype));
  185. ONN("bad address type byte",
  186. (atype != V5_ADDR_IPV4 && atype != V5_ADDR_IPV6
  187. && atype != V5_ADDR_FQDN));
  188. if (atype == V5_ADDR_FQDN) {
  189. ONN("unexpected FQDN from client", srv->expect_fqdn == NULL);
  190. CALL(read_socks_string(sock, "read FQDN", buf, &len));
  191. ONV(len != strlen(srv->expect_fqdn)
  192. || memcmp(srv->expect_fqdn, buf, len) != 0,
  193. ("FQDN mismatch: %.*s not %s", len, buf,
  194. srv->expect_fqdn));
  195. }
  196. else {
  197. unsigned char raw[16];
  198. ONN("unexpected IP literal from client", srv->expect_addr == NULL);
  199. ONV((atype == V5_ADDR_IPV4
  200. && ne_iaddr_typeof(srv->expect_addr) != ne_iaddr_ipv4)
  201. || (atype == V5_ADDR_IPV6
  202. && ne_iaddr_typeof(srv->expect_addr) != ne_iaddr_ipv6),
  203. ("address type mismatch: %hx not %d",
  204. atype, ne_iaddr_typeof(srv->expect_addr)));
  205. len = atype == V5_ADDR_IPV4 ? 4 : 16;
  206. ret = ne_sock_fullread(sock, (char *)buf, len);
  207. ONV(ret != 0,
  208. ("address read failed with %" NE_FMT_SSIZE_T
  209. " (%s)", ret, ne_sock_error(sock)));
  210. ne_iaddr_raw(srv->expect_addr, raw);
  211. ONN("address mismatch", memcmp(raw, buf, len) != 0);
  212. }
  213. CALL(read_socks_byte(sock, "port high byte", buf));
  214. CALL(read_socks_byte(sock, "port low byte", buf + 1));
  215. port = (buf[0] << 8) | buf[1];
  216. ONV(port != srv->expect_port,
  217. ("got bad port %u, expected %u", port, srv->expect_port));
  218. {
  219. static const char msg[] =
  220. "\x05\x00\x00"
  221. "\x01" "\x00\x00\x00\x00"
  222. "\x00\x00"
  223. "ok!\n";
  224. if (srv->say_hello)
  225. CALL(full_write(sock, msg, 14));
  226. else
  227. CALL(full_write(sock, msg, 10));
  228. }
  229. return srv->server(sock, srv->userdata);
  230. }