msproxy_clientprotocol.c
上传用户:zm130024
上传日期:2007-01-04
资源大小:432k
文件大小:57k
源码类别:

代理服务器

开发平台:

Unix_Linux

  1. /*
  2.  * Copyright (c) 1997, 1998, 1999
  3.  *      Inferno Nettverk A/S, Norway.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. The above copyright notice, this list of conditions and the following
  9.  *    disclaimer must appear in all copies of the software, derivative works
  10.  *    or modified versions, and any portions thereof, aswell as in all
  11.  *    supporting documentation.
  12.  * 2. All advertising materials mentioning features or use of this software
  13.  *    must display the following acknowledgement:
  14.  *      This product includes software developed by
  15.  *      Inferno Nettverk A/S, Norway.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * Inferno Nettverk A/S requests users of this software to return to
  31.  *
  32.  *  Software Distribution Coordinator  or  sdc@inet.no
  33.  *  Inferno Nettverk A/S
  34.  *  Oslo Research Park
  35.  *  Gaustadal閑n 21
  36.  *  N-0349 Oslo
  37.  *  Norway
  38.  *
  39.  * any improvements or extensions that they make and grant Inferno Nettverk A/S
  40.  * the rights to redistribute these changes.
  41.  *
  42.  */
  43. /*
  44.  * This code is terrible so hopefully it will match the protocol.
  45.  */
  46. #include "common.h"
  47. static const char rcsid[] =
  48. "$Id: msproxy_clientprotocol.c,v 1.22 1999/09/02 10:41:41 michaels Exp $";
  49. static char executable[] = "TELNET.EXE";
  50. static struct sigaction oldsigio;
  51. __BEGIN_DECLS
  52. static int
  53. msproxy_connect __P((int s, int control, struct socks_t *packet));
  54. static int
  55. msproxy_bind __P((int s, int control, struct socks_t *packet));
  56. static char *
  57. mem2response __P((struct msproxy_response_t *res, char *mem, size_t len));
  58. static char *
  59. request2mem __P((const struct msproxy_request_t *req, char *mem));
  60. static void
  61. msproxy_sessionsend __P((void));
  62. /*
  63.  * Terminates all msproxy sessions.
  64.  */
  65. static void
  66. msproxy_sessionend __P((int s, struct msproxy_state_t *msproxy));
  67. /*
  68.  * ends the session negotiated with "s" and having state "msproxy".
  69.  */
  70. static void
  71. msproxy_keepalive __P((int signal));
  72. /*
  73.  * Sends a keepalive packet on behalf of all established sessions.
  74.  */
  75. static void
  76. sigio __P((int sig));
  77. __END_DECLS
  78. int
  79. msproxy_init(void)
  80. {
  81. const char *function = "msproxy_init()";
  82. struct itimerval timerval;
  83. struct sigaction sigact, oldsigact;
  84. if (atexit(msproxy_sessionsend) != 0) {
  85. swarn("%s: atexit()", function);
  86. return -1;
  87. }
  88. if (sigaction(SIGALRM, NULL, &oldsigact) != 0) {
  89. swarn("%s: sigaction(SIGALRM)", function);
  90. return -1;
  91. }
  92. /* XXX */
  93. if (oldsigact.sa_handler != SIG_DFL
  94. &&  oldsigact.sa_handler != SIG_IGN) {
  95. swarnx("%s: could not install signalhandler for SIGALRM, already set",
  96. function);
  97. return 0; /* will probably timeout, don't consider it fatal for now. */
  98. }
  99. sigemptyset(&sigact.sa_mask);
  100. sigact.sa_flags = SA_RESTART;
  101. sigact.sa_handler = msproxy_keepalive;
  102. if (sigaction(SIGALRM, &sigact, NULL) != 0) {
  103. swarn("%s: sigaction(SIGALRM)", function);
  104. return -1;
  105. }
  106. timerval.it_value.tv_sec = MSPROXY_PINGINTERVAL;
  107. timerval.it_value.tv_usec = 0;
  108. timerval.it_interval = timerval.it_value;
  109. if (setitimer(ITIMER_REAL, &timerval, NULL) != 0) {
  110. swarn("%s: setitimer()", function);
  111. return -1;
  112. }
  113. srand((unsigned int)time(NULL) * getpid());
  114. return 0;
  115. }
  116. int
  117. msproxy_negotiate(s, control, packet)
  118. int s;
  119. int control;
  120. struct socks_t *packet;
  121. {
  122. const char *function = "msproxy_negotiate()";
  123. struct msproxy_request_t req;
  124. struct msproxy_response_t res;
  125. int p;
  126. slog(LOG_DEBUG, "%s: packet #1", function);
  127. bzero(&req, sizeof(req));
  128. req.clientid = htonl(0x0a000000);
  129. req.command = htons(MSPROXY_HELLO);
  130. /* in case we don't get something more sensible. */
  131. packet->res.version = packet->req.version;
  132. packet->res.reply = MSPROXY_FAILURE;
  133. switch (packet->req.command) {
  134. case SOCKS_BIND:
  135. #if 1
  136. req.packet._1.magic5 = htons(0x4800);
  137. req.packet._1.magic10 = htons(0x1400);
  138. req.packet._1.magic15 = htons(0x0400);
  139. req.packet._1.magic20 = htons(0x5704);
  140. req.packet._1.magic25 = htons(0x0004);
  141. req.packet._1.magic30 = htons(0x0100);
  142. req.packet._1.magic35 = htons(0x4a02);
  143. req.packet._1.magic40 = htons(0x3000);
  144. req.packet._1.magic45 = htons(0x4100);
  145. req.packet._1.magic50 = htons(0x3900);
  146. #endif
  147. break;
  148. case SOCKS_CONNECT:
  149. #if 0
  150. req.packet._1.magic5 = htons(0x4b00);
  151. req.packet._1.magic10 = htons(0x1400);
  152. req.packet._1.magic15 = htons(0x0400);
  153. req.packet._1.magic20 = htons(0x5704);
  154. req.packet._1.magic25 = htons(0x0004);
  155. req.packet._1.magic30 = htons(0x0100);
  156. req.packet._1.magic35 = htons(0x4a02);
  157. req.packet._1.magic40 = htons(0x3000);
  158. req.packet._1.magic45 = htons(0x4400);
  159. req.packet._1.magic50 = htons(0x3900);
  160. #endif
  161. break;
  162. case SOCKS_UDPASSOCIATE:
  163. SERRX(packet->req.command);
  164. /* NOTREACHED */
  165. default:
  166. SERRX(packet->req.command);
  167. }
  168. if (socks_getusername(&packet->gw.host, req.username, sizeof(req.username))
  169. == NULL)
  170. return -1;
  171. *req.unknown = NUL;
  172. strncpy(req.executable, executable, sizeof(req.executable) - 1);
  173. req.executable[sizeof(req.executable) - 1] = NUL;
  174. *req.clienthost = NUL;
  175. #if 0
  176. strncpy(req.clienthost, "foo", sizeof(req.clienthost) - 1);
  177. req.clienthost[sizeof(req.clienthost) - 1] = NUL;
  178. #endif
  179. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  180. return -1;
  181. if (recv_mspresponse(control, &packet->state.msproxy, &res) == -1)
  182. return -1;
  183. if (strcmp(res.RWSP, "RWSP") != 0)
  184. serrx(EXIT_FAILURE, "expected "RWSP", got "%s"",
  185. str2vis(res.RWSP, sizeof(res.RWSP)));
  186. if (ntohs(res.command) >> 8 != 0x10)
  187. serrx(EXIT_FAILURE, "expected res.command = 10??, is %x",
  188. ntohs(res.command));
  189. packet->state.msproxy.controladdr.sin_family = AF_INET;
  190. packet->state.msproxy.controladdr.sin_port = res.packet._1.udpport;
  191. packet->state.msproxy.controladdr.sin_addr.s_addr = res.packet._1.udpaddr;
  192. packet->state.msproxy.clientid = htonl(rand());
  193. packet->state.msproxy.serverid = res.serverid;
  194. slog(LOG_DEBUG, "%s: clientid: 0x%x, serverid: 0x%0x",
  195. function, packet->state.msproxy.clientid, packet->state.msproxy.serverid);
  196. /* LINTED pointer casts may be troublesome */
  197. slog(LOG_DEBUG, "%s: msproxy controladdress: %s",
  198. function,
  199. sockaddr2string((struct sockaddr *)&packet->state.msproxy.controladdr,
  200. NULL, 0));
  201. slog(LOG_DEBUG, "%s: packet #2", function);
  202. /* almost identical. */
  203. req.clientid = packet->state.msproxy.clientid;
  204. req.serverid = packet->state.msproxy.serverid;
  205. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  206. return -1;
  207. if (recv_mspresponse(control, &packet->state.msproxy, &res) == -1)
  208. return -1;
  209. if (res.serverid != packet->state.msproxy.serverid)
  210. serrx(EXIT_FAILURE, "expected serverid = 0x%x, is 0x%x",
  211. packet->state.msproxy.serverid, res.serverid);
  212. if (res.sequence != 0x01)
  213. serrx(EXIT_FAILURE, "expected res.sequence = 0x%x, is 0x%x",
  214. 0x01, res.sequence);
  215. if (ntohs(res.command) != MSPROXY_USERINFO_ACK)
  216. serrx(EXIT_FAILURE, "expected res.command = 0x%x, is 0x%x",
  217. MSPROXY_USERINFO_ACK, ntohs(res.command));
  218. switch (packet->req.command) {
  219. case SOCKS_BIND:
  220. p = msproxy_bind(s, control, packet);
  221. break;
  222. case SOCKS_CONNECT:
  223. p = msproxy_connect(s, control, packet);
  224. break;
  225. case SOCKS_UDPASSOCIATE:
  226. default:
  227. SERRX(packet->req.command);
  228. }
  229. return p;
  230. }
  231. static int
  232. msproxy_connect(s, control, packet)
  233. int s;
  234. int control;
  235. struct socks_t *packet;
  236. {
  237. const char *function = "msproxy_connect()";
  238. struct msproxy_request_t req;
  239. struct msproxy_response_t res;
  240. struct sockaddr_in addr;
  241. socklen_t len;
  242. slog(LOG_DEBUG, function);
  243. #if 0
  244. bzero(&req, sizeof(req));
  245. req.clientid = packet->state.msproxy.clientid;
  246. req.serverid = packet->state.msproxy.serverid;
  247. req.command = htons(MSPROXY_SOMETHING);
  248. memcpy(req.packet._3.NTLMSSP, "NTLMSSP", sizeof("NTLMSSP"));
  249. req.packet._3.bindaddr = htonl(0x02000000);
  250. req.packet._3.magic5 = htons(0x0100);
  251. req.packet._3.magic10 = htons(0x9682);
  252. #if 0
  253. req.packet._3.magic50 = htons(0x3000);
  254. req.packet._3.magic55 = htons(0x3000);
  255. #endif
  256. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  257. return -1;
  258. if (recv_mspresponse(control, &packet->state.msproxy, &res) == -1)
  259. return -1;
  260. if (res.serverid != packet->state.msproxy.serverid)
  261. serrx(EXIT_FAILURE, "expected serverid = 0x%x, is 0x%x",
  262. packet->state.msproxy.serverid, res.serverid);
  263. if (ntohs(res.command) != MSPROXY_SOMETHING_1_ACK)
  264. serrx(EXIT_FAILURE, "expected res.command = 0x%x, is 0x%x",
  265. MSPROXY_SOMETHING_1_ACK, ntohs(res.command));
  266. slog(LOG_DEBUG, "%s: ntdomain: "%s"", function, res.packet._3.ntdomain);
  267. slog(LOG_DEBUG, "%s: packet #4", function);
  268. bzero(&req, sizeof(req));
  269. req.clientid = packet->state.msproxy.clientid;
  270. req.serverid = packet->state.msproxy.serverid;
  271. req.command = htons(MSPROXY_SOMETHING_2);
  272. #if 0
  273. memcpy(req.packet._4.NTLMSSP, "NTLMSSP", sizeof("NTLMSSP"));
  274. req.packet._4.magic3 = htons(0x0200);
  275. req.packet._4.magic5 = htons(0x0300);
  276. req.packet._4.magic10 = htons(0x1800);
  277. req.packet._4.magic15 = htons(0x1800);
  278. req.packet._4.magic20 = htons(0x4900);
  279. req.packet._4.magic30 = htons(0x6100);
  280. req.packet._4.magic35 = htons(0x0800);
  281. req.packet._4.magic40 = htons(0x0800);
  282. req.packet._4.magic45 = htons(0x3400);
  283. req.packet._4.magic50 = htons(0x0700);
  284. req.packet._4.magic55 = htons(0x0700);
  285. req.packet._4.magic60 = htons(0x3c00);
  286. req.packet._4.magic65 = htons(0x0600);
  287. req.packet._4.magic70 = htons(0x0600);
  288. req.packet._4.magic75 = htons(0x4300);
  289. #endif
  290. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  291. return -1;
  292. if (recv_mspresponse(control, &packet->state.msproxy, &res) == -1)
  293. return -1;
  294. if (res.serverid != packet->state.msproxy.serverid)
  295. serrx(EXIT_FAILURE, "expected res.serverid = 0x%x, is 0x%x",
  296. packet->state.msproxy.serverid, res.serverid);
  297. if (res.clientack != 0x01)
  298. serrx(EXIT_FAILURE, "expected res.clientack = 0x%x, is 0x%x",
  299. 0x01, res.clientack);
  300. if (ntohs(res.command) >> 8 != 0x47)
  301. serrx(EXIT_FAILURE, "expected res.command = 47??, is 0x%x",
  302. ntohs(res.command));
  303. #endif
  304. switch (packet->req.host.atype) {
  305. case SOCKS_ADDR_IPV4:
  306. /* LINTED pointer casts may be troublesome */
  307. sockshost2sockaddr(&packet->req.host, (struct sockaddr *)&addr);
  308. break;
  309. case SOCKS_ADDR_DOMAIN:
  310. slog(LOG_DEBUG, "%s: resolve packet", function);
  311. bzero(&req, sizeof(req));
  312. req.clientid = packet->state.msproxy.clientid;
  313. req.serverid = packet->state.msproxy.serverid;
  314. req.command = htons(MSPROXY_RESOLVE);
  315. req.packet.resolve.hostlength
  316. = (unsigned char)(strlen(packet->req.host.addr.domain) + 1);
  317. memcpy(&req.packet.resolve.host, packet->req.host.addr.domain,
  318. (size_t)req.packet.resolve.hostlength);
  319. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  320. return -1;
  321. if (recv_mspresponse(control, &packet->state.msproxy, &res) == -1)
  322. return -1;
  323. if (ntohs(res.command) != MSPROXY_RESOLVE_ACK)
  324. serrx(EXIT_FAILURE, "expected res.command = 0x%x, is 0x%x",
  325. MSPROXY_RESOLVE_ACK, ntohs(res.command));
  326. addr.sin_addr.s_addr = res.packet.resolve.hostaddr;
  327. slog(LOG_DEBUG, "%s: ip address of %s: %s",
  328. function, packet->req.host.addr.domain, inet_ntoa(addr.sin_addr));
  329. break;
  330. default:
  331. SERRX(packet->req.host.atype);
  332. }
  333. slog(LOG_DEBUG, "%s: packet #5", function);
  334. bzero(&req, sizeof(req));
  335. req.clientid = packet->state.msproxy.clientid;
  336. req.serverid = packet->state.msproxy.serverid;
  337. req.command = htons(MSPROXY_CONNECT);
  338. req.packet._5.magic6 = htons(0x0200);
  339. req.packet._5.destport = packet->req.host.port;
  340. req.packet._5.destaddr = addr.sin_addr.s_addr;
  341. /*
  342.  * need to tell server what port we will connect from, so if socket
  343.  * is not bound, bind it.
  344.  */
  345. len = sizeof(addr);
  346. /* LINTED pointer casts may be troublesome */
  347. if (getsockname(s, (struct sockaddr *)&addr, &len) != 0)
  348. return -1;
  349. if (!ADDRISBOUND(addr)) {
  350. /*
  351.  * Don't have any specific preference for what address to bind and
  352.  * proxyserver only expects to be told port.
  353.  */
  354. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  355. /* LINTED pointer casts may be troublesome */
  356. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) != 0)
  357. return -1;
  358. len = sizeof(addr);
  359. /* LINTED pointer casts may be troublesome */
  360. if (getsockname(s, (struct sockaddr *)&addr, &len) != 0)
  361. return -1;
  362. }
  363. req.packet._5.srcport = addr.sin_port;
  364. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  365. return -1;
  366. if (recv_mspresponse(control, &packet->state.msproxy, &res) == -1)
  367. return -1;
  368. if (ntohs(res.command) != MSPROXY_CONNECT_ACK) {
  369. swarnx("expected res.command = 0x%x, is 0x%x",
  370. MSPROXY_CONNECT_ACK, ntohs(res.command));
  371. packet->res.reply = MSPROXY_NOTALLOWED;
  372. return -1;
  373. }
  374. packet->res.host.atype = SOCKS_ADDR_IPV4;
  375. packet->res.host.port = res.packet._5.clientport;
  376. packet->res.host.addr.ipv4.s_addr = res.packet._5.clientaddr;
  377. if (socks_connect(s, &packet->res.host) != 0) {
  378. swarn("%s: failed to connect to %s",
  379. function, sockshost2string(&packet->res.host, NULL, 0));
  380. return -1;
  381. }
  382. else
  383. slog(LOG_DEBUG, "%s: connected to %s",
  384. function, sockshost2string(&packet->res.host, NULL, 0));
  385. packet->res.host.atype = SOCKS_ADDR_IPV4;
  386. packet->res.host.port = res.packet._5.clientport;
  387. packet->res.host.addr.ipv4.s_addr = res.packet._5.clientaddr;
  388. /* LINTED pointer casts may be troublesome */
  389. slog(LOG_DEBUG, "%s: server will use as source address: %s",
  390. function, sockshost2string(&packet->res.host, NULL, 0));
  391. slog(LOG_DEBUG, "%s: packet #6", function);
  392. bzero(&req, sizeof(req));
  393. req.clientid = packet->state.msproxy.clientid;
  394. req.serverid = packet->state.msproxy.serverid;
  395. req.command = htons(MSPROXY_CONNECTED);
  396. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  397. return -1;
  398. /* make response look sensible. */
  399. packet->res.version = packet->req.version;
  400. packet->res.reply = MSPROXY_SUCCESS;
  401. packet->res.flag = 0;
  402. packet->res.auth = NULL;
  403. return 0;
  404. }
  405. static int
  406. msproxy_bind(s, control, packet)
  407. int s;
  408. int control;
  409. struct socks_t *packet;
  410. {
  411. const char *function = "msproxy_bind()";
  412. struct msproxy_request_t req;
  413. struct msproxy_response_t res;
  414. struct sockaddr_in addr;
  415. socklen_t len;
  416. slog(LOG_DEBUG, function);
  417. bzero(&req, sizeof(req));
  418. req.clientid = packet->state.msproxy.clientid;
  419. req.serverid = packet->state.msproxy.serverid;
  420. req.command = htons(MSPROXY_BIND);
  421. req.packet._3.magic2 = htons(0x0100);
  422. req.packet._3.bindaddr = packet->req.host.addr.ipv4.s_addr;
  423. req.packet._3.bindport = packet->req.host.port;
  424. req.packet._3.magic3 = htons(0x0200);
  425. len = sizeof(addr);
  426. /* LINTED pointer casts may be troublesome */
  427. if (getsockname(s, (struct sockaddr *)&addr, &len) != 0)
  428. return -1;
  429. req.packet._3.boundport = addr.sin_port;
  430. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  431. return -1;
  432. if (recv_mspresponse(control, &packet->state.msproxy, &res) == -1)
  433. return -1;
  434. if (res.serverid != packet->state.msproxy.serverid)
  435. serrx(EXIT_FAILURE, "expected serverid = 0x%x, is 0x%x",
  436. packet->state.msproxy.serverid, res.serverid);
  437. if (ntohs(res.command) != MSPROXY_BIND_ACK) {
  438. swarnx("expected res.command = 0x%x, is 0x%x",
  439. MSPROXY_BIND_ACK, ntohs(res.command));
  440. packet->res.reply = MSPROXY_NOTALLOWED;
  441. return -1;
  442. }
  443. packet->state.msproxy.bindid = res.packet._3.magic10;
  444. packet->res.host.atype = SOCKS_ADDR_IPV4;
  445. packet->res.host.port = res.packet._3.boundport;
  446. packet->res.host.addr.ipv4.s_addr = res.packet._3.boundaddr;
  447. slog(LOG_DEBUG, "%s: server bound for us: %s",
  448. function, sockshost2string(&packet->res.host, NULL, 0));
  449. slog(LOG_DEBUG, "%s: packet #4", function);
  450. bzero(&req, sizeof(req));
  451. req.clientid = packet->state.msproxy.clientid;
  452. req.serverid = packet->state.msproxy.serverid;
  453. req.command = htons(MSPROXY_BIND2);
  454. req.packet._4.magic1 = htons(0x0100);
  455. req.packet._4.magic2 = packet->state.msproxy.bindid;
  456. req.packet._4.magic3 = htons(0x0500);
  457. memcpy(req.packet._4.pad3 + 2, &addr.sin_port, sizeof(addr.sin_port));
  458. req.packet._4.magic4 = htons(0x0200);
  459. req.packet._4.boundport = addr.sin_port;
  460. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  461. return -1;
  462. if (recv_mspresponse(control, &packet->state.msproxy, &res) == -1)
  463. return -1;
  464. if (res.serverid != packet->state.msproxy.serverid)
  465. serrx(EXIT_FAILURE, "expected res.serverid = 0x%x, is 0x%x",
  466. packet->state.msproxy.serverid, res.serverid);
  467. if (res.clientack != 0x01)
  468. serrx(EXIT_FAILURE, "expected res.clientack = 0x%x, is 0x%x",
  469. 0x01, res.clientack);
  470. if (ntohs(res.command) != MSPROXY_BIND2_ACK) {
  471. swarnx("expected res.command = %x, is 0x%x",
  472. MSPROXY_BIND2_ACK, ntohs(res.command));
  473. return -1;
  474. }
  475. slog(LOG_DEBUG, "%s: packet #5", function);
  476. bzero(&req, sizeof(req));
  477. req.clientid = packet->state.msproxy.clientid;
  478. req.serverid = packet->state.msproxy.serverid;
  479. req.command = htons(MSPROXY_LISTEN);
  480. req.packet._5.magic6 = htons(0x0200);
  481. req.packet._5.destport = packet->res.host.port;
  482. req.packet._5.destaddr = packet->res.host.addr.ipv4.s_addr;
  483. if (send_msprequest(control, &packet->state.msproxy, &req) == -1)
  484. return -1;
  485. /*
  486.  * When the server accepts the client, it will send us a new
  487.  * controlpacket.  That will be caught in sigio().
  488.  */
  489. slog(LOG_DEBUG, "%s: waiting for forwarded connection...", function);
  490. /* make response look sensible. */
  491. packet->res.version = packet->req.version;
  492. packet->res.reply = MSPROXY_SUCCESS;
  493. packet->res.flag = 0;
  494. packet->res.auth = NULL;
  495. return 0;
  496. }
  497. int
  498. msproxy_sigio(s)
  499. int s;
  500. {
  501. const char *function = "msproxy_sigio()";
  502. static int init;
  503. const int errno_s = errno;
  504. struct socksfd_t *socksfd;
  505. int p;
  506. /*
  507.  * The msproxy protocol sends a udp packet which we must ack
  508.  * before it will connect to us.  We set up the controlsocket
  509.  * for signaldriven i/o so we can ack it asynchronously.
  510.  *
  511.  */
  512. SASSERTX(socks_addrisok((unsigned int)s));
  513. socksfd = socks_getaddr((unsigned int)s);
  514. SASSERTX(socksfd->state.version == MSPROXY_V2);
  515. if (!init) { /* could be smarter about this... */
  516. struct sigaction sigact;
  517. sigemptyset(&sigact.sa_mask);
  518. sigact.sa_flags = SA_RESTART;
  519. sigact.sa_handler = sigio;
  520. if (sigaction(SIGIO, &sigact, &oldsigio) != 0)
  521. return -1;
  522. init = 1;
  523. }
  524. if (fcntl(socksfd->control, F_SETOWN, getpid()) == -1)
  525. return -1;
  526. p = 1;
  527. if (ioctl(socksfd->control, FIOASYNC, &p) == -1)
  528. return -1;
  529. slog(LOG_DEBUG, "%s: set up sigio for %d", function, socksfd->control);
  530. errno = errno_s;
  531. return 0;
  532. }
  533. /* ARGSUSED */
  534. static void
  535. sigio(sig)
  536. int sig;
  537. {
  538. const char *function = "sigio()";
  539. const int errno_s = errno;
  540. int i, max, dset;
  541. struct socksfd_t *socksfd;
  542. fd_set rset;
  543. struct timeval timeout;
  544. int dbits;
  545. slog(LOG_DEBUG, function);
  546. /*
  547.  * Find the socket we were signalled for.
  548.  */
  549. dbits = -1;
  550. FD_ZERO(&rset);
  551. for (i = 0, max = getdtablesize(); i < max; ++i) {
  552. if ((socksfd = socks_getaddr((unsigned int)i)) == NULL)
  553. continue;
  554. if (socksfd->state.command != SOCKS_BIND
  555. ||  socksfd->state.version != MSPROXY_V2
  556. || !socksfd->state.inprogress)
  557. continue;
  558. if (!socks_addrisok((unsigned int)i))
  559. continue;
  560. SASSERTX(fdisopen(socksfd->control));
  561. FD_SET(socksfd->control, &rset);
  562. dbits = MAX(dbits, socksfd->control);
  563. }
  564. if (dbits == -1) {
  565. if (oldsigio.sa_handler != NULL)
  566. oldsigio.sa_handler(sig);
  567. return; /* old signal handler. */
  568. }
  569. dset = 0;
  570. do {
  571. fd_set newrset;
  572. timeout.tv_sec = 0;
  573. timeout.tv_usec = 0;
  574. newrset = rset;
  575. switch (selectn(dbits + 1, &newrset, NULL, NULL, &timeout)) {
  576. case -1:
  577. SERR(-1);
  578. /* NOTREACHED */
  579. case 0:
  580. dset = 0;
  581. slog(LOG_DEBUG, "%s: no descriptors readable", function);
  582. if (oldsigio.sa_handler != NULL)
  583. oldsigio.sa_handler(sig);
  584. break;
  585. default: {
  586. dset = 1;
  587. for (i = 0, max = getdtablesize(); i < max; ++i) {
  588. if ((socksfd = socks_getaddr((unsigned int)i)) == NULL)
  589. continue;
  590. if (FD_ISSET(socksfd->control, &newrset)) {
  591. struct sockshost_t host;
  592. struct msproxy_request_t req;
  593. struct msproxy_response_t res;
  594. int p;
  595. SASSERTX(socksfd->state.command == SOCKS_BIND
  596. && socksfd->state.version == MSPROXY_V2
  597. && socksfd->state.inprogress);
  598. slog(LOG_DEBUG, "%s: attempting to receive bind info...",
  599. function);
  600. FD_CLR(socksfd->control, &newrset);
  601. if (recv_mspresponse(socksfd->control,
  602. &socksfd->state.msproxy, &res) == -1)
  603. continue;
  604. if (ntohs(res.command) != MSPROXY_BINDINFO) {
  605. swarnx("%s: expected res.command = %x, is 0x%x",
  606. function, MSPROXY_BINDINFO, ntohs(res.command));
  607. continue;
  608. }
  609. /* no need for more signals on this socket. */
  610. p = 0;
  611. if (ioctl(socksfd->control, FIOASYNC, &p) == -1) {
  612. swarn("%s: ioctl(socksfd->control)", function);
  613. continue;
  614. }
  615. slog(LOG_DEBUG, "%s: turned off sigio for %d",
  616. function, socksfd->control);
  617. /*
  618.  * if we asked server to bind INADDR_ANY, we don't know
  619.  * what address it bound until now.
  620.  */
  621. host.atype = SOCKS_ADDR_IPV4;
  622. host.port = res.packet._5.boundport;
  623. host.addr.ipv4.s_addr = res.packet._5.boundaddr;
  624. sockshost2sockaddr(&host, &socksfd->remote);
  625. slog(LOG_DEBUG, "%s: server bound address %s",
  626. function, sockshost2string(&host, NULL, 0));
  627. host.atype = SOCKS_ADDR_IPV4;
  628. host.port = res.packet._5.clientport;
  629. host.addr.ipv4.s_addr = res.packet._5.clientaddr;
  630. sockshost2sockaddr(&host, &socksfd->accepted);
  631. slog(LOG_DEBUG, "%s: server accepted: %s",
  632. function, sockshost2string(&host, NULL, 0));
  633. slog(LOG_DEBUG, "%s: server will connect to us from port %d",
  634. function, ntohs(res.packet._5.serverport));
  635. /* LINTED pointer casts may be troublesome */
  636. ((struct sockaddr_in *)&socksfd->reply)->sin_port
  637. = res.packet._5.serverport;
  638. slog(LOG_DEBUG, "%s: packet #6", function);
  639. bzero(&req, sizeof(req));
  640. req.clientid = socksfd->state.msproxy.clientid;
  641. req.serverid = socksfd->state.msproxy.serverid;
  642. req.command = htons(MSPROXY_BINDINFO_ACK);
  643. req.packet._6.magic1 = res.packet._5.magic1;
  644. req.packet._6.magic5 = htons(0x0100);
  645. req.packet._6.magic10 = socksfd->state.msproxy.bindid;
  646. req.packet._6.magic15 = htons(0x0100);
  647. req.packet._6.magic16 = socksfd->state.msproxy.bindid;
  648. req.packet._6.magic20 = htons(0x0200);
  649. req.packet._6.clientport = res.packet._5.clientport;
  650. req.packet._6.clientaddr = res.packet._5.clientaddr;
  651. req.packet._6.magic30 = res.packet._5.magic10;
  652. req.packet._6.magic35 = res.packet._5.magic15;
  653. req.packet._6.serverport = res.packet._5.serverport;
  654. req.packet._6.srcport = res.packet._5.srcport;
  655. req.packet._6.boundport = res.packet._5.boundport;
  656. req.packet._6.boundaddr = res.packet._5.boundaddr;
  657. if (send_msprequest(socksfd->control, &socksfd->state.msproxy,
  658. &req) == -1)
  659. continue;
  660. if (recv_mspresponse(socksfd->control,
  661. &socksfd->state.msproxy, &res) == -1)
  662. continue;
  663. /* all done.  Can accept(). */
  664. socksfd->state.inprogress = 0;
  665. }
  666. }
  667. }
  668. }
  669. } while (dset != 0);
  670. errno = errno_s;
  671. }
  672. int
  673. recv_mspresponse(s, state, response)
  674. int s;
  675. struct msproxy_state_t *state;
  676. struct msproxy_response_t *response;
  677. {
  678. const char *function = "recv_mspresponse()";
  679. /* CONSTCOND */
  680. char responsemem[MAX(sizeof(*response), 1024)];
  681. ssize_t r;
  682. /* CONSTCOND */
  683. while (1) {
  684. if ((r = read(s, responsemem, sizeof(responsemem))) < MSPROXY_MINLENGTH) {
  685. swarn("%s: expected to read atleast %d, read %d",
  686. function, MSPROXY_MINLENGTH, r);
  687. return -1;
  688. }
  689. if (mem2response(response, responsemem, (size_t)r) == NULL) {
  690. swarnx("%s: bad response from server", function);
  691. return -1;
  692. }
  693. if (state->seq_recv > 0) {
  694. if (response->sequence == state->seq_recv + 1)
  695. break; /* all ok. */
  696. else if (response->sequence < state->seq_recv) {
  697. /*
  698.  * sequence number less that last, sometimes this happens too,
  699.  * don't know why.
  700.  */
  701. slog(LOG_DEBUG, "%s: sequence (%d) < seq_recv (%d)",
  702. function, response->sequence, state->seq_recv);
  703. break;
  704. }
  705. else if (response->sequence == state->seq_recv) {
  706. slog(LOG_DEBUG, "%s: seq_recv: %d, dup response, seqnumber: 0x%x",
  707. function, state->seq_recv, response->sequence);
  708. if (response->clientack == state->seq_sent) {
  709. slog(LOG_DEBUG,
  710. "%s: ack matches last sent though, hoping it's a new one...",
  711. function);
  712. break;
  713. }
  714. }
  715. else if (response->sequence > state->seq_recv) {
  716. slog(LOG_DEBUG, "%s: sequence (%d) > seq_recv (%d)",
  717. function, response->sequence, state->seq_recv);
  718. break;
  719. }
  720. }
  721. else
  722. break; /* not started incrementing yet. */
  723. }
  724. state->seq_recv = response->sequence;
  725. return r;
  726. }
  727. int
  728. send_msprequest(s, state, request)
  729. int s;
  730. struct msproxy_state_t *state;
  731. struct msproxy_request_t *request;
  732. {
  733. const char *function = "send_msprequest()";
  734. ssize_t w;
  735. char requestmem[sizeof(struct msproxy_request_t)], *p;
  736. request->magic25 = htonl(MSPROXY_VERSION);
  737. request->serverack = state->seq_recv;
  738. /* don't start incrementing sequence until we are acking packet #2. */
  739. request->sequence
  740. = (unsigned char)(request->serverack >= 2 ? state->seq_sent + 1 : 0);
  741. memcpy(request->RWSP, "RWSP", sizeof(request->RWSP));
  742. p = request2mem(request, requestmem);
  743. /* all requests must be atleast MSPROXY_MINLENGTH it seems. */
  744. if (p - requestmem < MSPROXY_MINLENGTH) {
  745. bzero(p, (size_t)(MSPROXY_MINLENGTH - (p - requestmem)));
  746. p = requestmem + MSPROXY_MINLENGTH;
  747. }
  748. if ((w = write(s, requestmem, (size_t)(p - requestmem)))
  749. != (ssize_t)(p - requestmem)) {
  750. swarn("%s: write()", function);
  751. return -1;
  752. }
  753. state->seq_sent = request->sequence;
  754. return w;
  755. }
  756. static char *
  757. mem2response(res, mem, len)
  758. struct msproxy_response_t *res;
  759. char *mem;
  760. size_t len;
  761. {
  762. const char *function = "mem2response()";
  763. if (len < sizeof(res->packetid))
  764. return NULL;
  765. memcpy(&res->packetid, mem, sizeof(res->packetid));
  766. mem += sizeof(res->packetid);
  767. len -= sizeof(res->packetid);
  768. if (len < sizeof(res->magic5))
  769. return NULL;
  770. memcpy(&res->magic5, mem, sizeof(res->magic5));
  771. mem += sizeof(res->magic5);
  772. len -= sizeof(res->magic5);
  773. if (len < sizeof(res->serverid))
  774. return NULL;
  775. memcpy(&res->serverid, mem, sizeof(res->serverid));
  776. mem += sizeof(res->serverid);
  777. len -= sizeof(res->serverid);
  778. if (len < sizeof(res->clientack))
  779. return NULL;
  780. memcpy(&res->clientack, mem, sizeof(res->clientack));
  781. mem += sizeof(res->clientack);
  782. len -= sizeof(res->clientack);
  783. if (len < sizeof(res->pad5))
  784. return NULL;
  785. memcpy(res->pad5, mem, sizeof(res->pad5));
  786. mem += sizeof(res->pad5);
  787. len -= sizeof(res->pad5);
  788. if (len < sizeof(res->sequence))
  789. return NULL;
  790. memcpy(&res->sequence, mem, sizeof(res->sequence));
  791. mem += sizeof(res->sequence);
  792. len -= sizeof(res->sequence);
  793. if (len < sizeof(res->pad10))
  794. return NULL;
  795. memcpy(res->pad10, mem, sizeof(res->pad10));
  796. mem += sizeof(res->pad10);
  797. len -= sizeof(res->pad10);
  798. if (len < sizeof(res->RWSP))
  799. return NULL;
  800. memcpy(res->RWSP, mem, sizeof(res->RWSP));
  801. mem += sizeof(res->RWSP);
  802. len -= sizeof(res->RWSP);
  803. if (len < sizeof(res->pad15))
  804. return NULL;
  805. memcpy(res->pad15, mem, sizeof(res->pad15));
  806. mem += sizeof(res->pad15);
  807. len -= sizeof(res->pad15);
  808. if (len < sizeof(res->command))
  809. return NULL;
  810. memcpy(&res->command, mem, sizeof(res->command));
  811. mem += sizeof(res->command);
  812. len -= sizeof(res->command);
  813. switch (ntohs(res->command)) {
  814. case MSPROXY_HELLO_ACK:
  815. if (len < sizeof(res->packet._1.pad5))
  816. return NULL;
  817. memcpy(res->packet._1.pad5, mem, sizeof(res->packet._1.pad5));
  818. mem += sizeof(res->packet._1.pad5);
  819. len -= sizeof(res->packet._1.pad5);
  820. if (len < sizeof(res->packet._1.magic20))
  821. return NULL;
  822. memcpy(&res->packet._1.magic20, mem, sizeof(res->packet._1.magic20));
  823. mem += sizeof(res->packet._1.magic20);
  824. len -= sizeof(res->packet._1.magic20);
  825. if (len < sizeof(res->packet._1.pad10))
  826. return NULL;
  827. memcpy(&res->packet._1.pad10, mem, sizeof(res->packet._1.pad10));
  828. mem += sizeof(res->packet._1.pad10);
  829. len -= sizeof(res->packet._1.pad10);
  830. if (len < sizeof(res->packet._1.magic30))
  831. return NULL;
  832. memcpy(&res->packet._1.magic30, mem, sizeof(res->packet._1.magic30));
  833. mem += sizeof(res->packet._1.magic30);
  834. len -= sizeof(res->packet._1.magic30);
  835. if (len < sizeof(res->packet._1.pad15))
  836. return NULL;
  837. memcpy(res->packet._1.pad15, mem, sizeof(res->packet._1.pad15));
  838. mem += sizeof(res->packet._1.pad15);
  839. len -= sizeof(res->packet._1.pad15);
  840. if (len < sizeof(res->packet._1.magic35))
  841. return NULL;
  842. memcpy(&res->packet._1.magic35, mem, sizeof(res->packet._1.magic35));
  843. mem += sizeof(res->packet._1.magic35);
  844. len -= sizeof(res->packet._1.magic35);
  845. if (len < sizeof(res->packet._1.pad20))
  846. return NULL;
  847. memcpy(res->packet._1.pad20, mem, sizeof(res->packet._1.pad20));
  848. mem += sizeof(res->packet._1.pad20);
  849. len -= sizeof(res->packet._1.pad20);
  850. if (len < sizeof(res->packet._1.magic50))
  851. return NULL;
  852. memcpy(&res->packet._1.magic50, mem, sizeof(res->packet._1.magic50));
  853. mem += sizeof(res->packet._1.magic50);
  854. len -= sizeof(res->packet._1.magic50);
  855. if (len < sizeof(res->packet._1.pad30))
  856. return NULL;
  857. memcpy(res->packet._1.pad30, mem, sizeof(res->packet._1.pad30));
  858. mem += sizeof(res->packet._1.pad30);
  859. len -= sizeof(res->packet._1.pad30);
  860. if (len < sizeof(res->packet._1.magic60))
  861. return NULL;
  862. memcpy(&res->packet._1.magic60, mem, sizeof(res->packet._1.magic60));
  863. mem += sizeof(res->packet._1.magic60);
  864. len -= sizeof(res->packet._1.magic60);
  865. if (len < sizeof(res->packet._1.pad35))
  866. return NULL;
  867. memcpy(res->packet._1.pad35, mem, sizeof(res->packet._1.pad35));
  868. mem += sizeof(res->packet._1.pad35);
  869. len -= sizeof(res->packet._1.pad35);
  870. if (len < sizeof(res->packet._1.magic65))
  871. return NULL;
  872. memcpy(&res->packet._1.magic65, mem, sizeof(res->packet._1.magic65));
  873. mem += sizeof(res->packet._1.magic65);
  874. len -= sizeof(res->packet._1.magic65);
  875. if (len < sizeof(res->packet._1.pad40))
  876. return NULL;
  877. memcpy(res->packet._1.pad40, mem, sizeof(res->packet._1.pad40));
  878. mem += sizeof(res->packet._1.pad40);
  879. len -= sizeof(res->packet._1.pad40);
  880. if (len < sizeof(res->packet._1.udpport))
  881. return NULL;
  882. memcpy(&res->packet._1.udpport, mem, sizeof(res->packet._1.udpport));
  883. mem += sizeof(res->packet._1.udpport);
  884. len -= sizeof(res->packet._1.udpport);
  885. if (len < sizeof(res->packet._1.udpaddr))
  886. return NULL;
  887. memcpy(&res->packet._1.udpaddr, mem, sizeof(res->packet._1.udpaddr));
  888. mem += sizeof(res->packet._1.udpaddr);
  889. len -= sizeof(res->packet._1.udpaddr);
  890. break;
  891. case MSPROXY_USERINFO_ACK:
  892. if (len < sizeof(res->packet._2.pad5))
  893. return NULL;
  894. memcpy(res->packet._2.pad5, mem, sizeof(res->packet._2.pad5));
  895. mem += sizeof(res->packet._2.pad5);
  896. len -= sizeof(res->packet._2.pad5);
  897. if (len < sizeof(res->packet._2.magic5))
  898. return NULL;
  899. memcpy(&res->packet._2.magic5, mem, sizeof(res->packet._2.magic5));
  900. mem += sizeof(res->packet._2.magic5);
  901. len -= sizeof(res->packet._2.magic5);
  902. break;
  903. case MSPROXY_BIND_ACK:
  904. case MSPROXY_SOMETHING_1_ACK:
  905. if (len < sizeof(res->packet._3.pad1))
  906. return NULL;
  907. memcpy(res->packet._3.pad1, mem, sizeof(res->packet._3.pad1));
  908. mem += sizeof(res->packet._3.pad1);
  909. len -= sizeof(res->packet._3.pad1);
  910. if (len < sizeof(res->packet._3.magic10))
  911. return NULL;
  912. memcpy(&res->packet._3.magic10, mem, sizeof(res->packet._3.magic10));
  913. mem += sizeof(res->packet._3.magic10);
  914. len -= sizeof(res->packet._3.magic10);
  915. if (len < sizeof(res->packet._3.pad3))
  916. return NULL;
  917. memcpy(res->packet._3.pad3, mem, sizeof(res->packet._3.pad3));
  918. mem += sizeof(res->packet._3.pad3);
  919. len -= sizeof(res->packet._3.pad3);
  920. if (len < sizeof(res->packet._3.boundport))
  921. return NULL;
  922. memcpy(&res->packet._3.boundport, mem,
  923. sizeof(res->packet._3.boundport));
  924. mem += sizeof(res->packet._3.boundport);
  925. len -= sizeof(res->packet._3.boundport);
  926. if (len < sizeof(res->packet._3.boundaddr))
  927. return NULL;
  928. memcpy(&res->packet._3.boundaddr, mem,
  929. sizeof(res->packet._3.boundaddr));
  930. mem += sizeof(res->packet._3.boundaddr);
  931. len -= sizeof(res->packet._3.boundaddr);
  932. if (len < sizeof(res->packet._3.pad10))
  933. return NULL;
  934. memcpy(res->packet._3.pad10, mem, sizeof(res->packet._3.pad10));
  935. mem += sizeof(res->packet._3.pad10);
  936. len -= sizeof(res->packet._3.pad10);
  937. if (len < sizeof(res->packet._3.magic15))
  938. return NULL;
  939. memcpy(&res->packet._3.magic15, mem, sizeof(res->packet._3.magic15));
  940. mem += sizeof(res->packet._3.magic15);
  941. len -= sizeof(res->packet._3.magic15);
  942. if (len < sizeof(res->packet._3.pad15))
  943. return NULL;
  944. memcpy(res->packet._3.pad15, mem, sizeof(res->packet._3.pad15));
  945. mem += sizeof(res->packet._3.pad15);
  946. len -= sizeof(res->packet._3.pad15);
  947. if (len < sizeof(res->packet._3.NTLMSSP))
  948. return NULL;
  949. memcpy(res->packet._3.NTLMSSP, mem, sizeof(res->packet._3.NTLMSSP));
  950. mem += sizeof(res->packet._3.NTLMSSP);
  951. len -= sizeof(res->packet._3.NTLMSSP);
  952. if (len < sizeof(res->packet._3.magic50))
  953. return NULL;
  954. memcpy(&res->packet._3.magic50, mem, sizeof(res->packet._3.magic50));
  955. mem += sizeof(res->packet._3.magic50);
  956. len -= sizeof(res->packet._3.magic50);
  957. if (len < sizeof(res->packet._3.pad50))
  958. return NULL;
  959. memcpy(res->packet._3.pad50, mem, sizeof(res->packet._3.pad50));
  960. mem += sizeof(res->packet._3.pad50);
  961. len -= sizeof(res->packet._3.pad50);
  962. if (len < sizeof(res->packet._3.magic55))
  963. return NULL;
  964. memcpy(&res->packet._3.magic55, mem, sizeof(res->packet._3.magic55));
  965. mem += sizeof(res->packet._3.magic55);
  966. len -= sizeof(res->packet._3.magic55);
  967. if (len < sizeof(res->packet._3.magic60))
  968. return NULL;
  969. memcpy(&res->packet._3.magic60, mem, sizeof(res->packet._3.magic60));
  970. mem += sizeof(res->packet._3.magic60);
  971. len -= sizeof(res->packet._3.magic60);
  972. if (len < sizeof(res->packet._3.magic65))
  973. return NULL;
  974. memcpy(&res->packet._3.magic65, mem, sizeof(res->packet._3.magic65));
  975. mem += sizeof(res->packet._3.magic65);
  976. len -= sizeof(res->packet._3.magic65);
  977. if (len < sizeof(res->packet._3.pad60))
  978. return NULL;
  979. memcpy(res->packet._3.pad60, mem, sizeof(res->packet._3.pad60));
  980. mem += sizeof(res->packet._3.pad60);
  981. len -= sizeof(res->packet._3.pad60);
  982. if (len < sizeof(res->packet._3.magic70))
  983. return NULL;
  984. memcpy(&res->packet._3.magic70, mem, sizeof(res->packet._3.magic70));
  985. mem += sizeof(res->packet._3.magic70);
  986. len -= sizeof(res->packet._3.magic70);
  987. if (len < sizeof(res->packet._3.magic75))
  988. return NULL;
  989. memcpy(&res->packet._3.magic75, mem, sizeof(res->packet._3.magic75));
  990. mem += sizeof(res->packet._3.magic75);
  991. len -= sizeof(res->packet._3.magic75);
  992. if (len < sizeof(res->packet._3.pad70))
  993. return NULL;
  994. memcpy(res->packet._3.pad70, mem, sizeof(res->packet._3.pad70));
  995. mem += sizeof(res->packet._3.pad70);
  996. len -= sizeof(res->packet._3.pad70);
  997. if (len > sizeof(res->packet._3.ntdomain))
  998. swarnx("hmm, ntdomain length is %d, our max is %d?",
  999. len, sizeof(res->packet._3.ntdomain) - 1);
  1000. memcpy(res->packet._3.ntdomain, mem,
  1001. MIN(len, sizeof(res->packet._3.ntdomain) - 1));
  1002. res->packet._3.ntdomain[len] = NUL;
  1003. mem += len;
  1004. len -= len;
  1005. break;
  1006. case MSPROXY_SOMETHING_2_ACK:
  1007. case MSPROXY_SOMETHING_2_ACK2:
  1008. case MSPROXY_BIND2_ACK:
  1009. if (len < sizeof(res->packet._4.pad5))
  1010. return NULL;
  1011. memcpy(res->packet._4.pad5, mem, sizeof(res->packet._4.pad5));
  1012. mem += sizeof(res->packet._4.pad5);
  1013. len -= sizeof(res->packet._4.pad5);
  1014. break;
  1015. case MSPROXY_RESOLVE_ACK: {
  1016. if (len < sizeof(res->packet.resolve.addroffset))
  1017. return NULL;
  1018. memcpy(&res->packet.resolve.addroffset, mem,
  1019. sizeof(res->packet.resolve.addroffset));
  1020. mem += sizeof(res->packet.resolve.addroffset);
  1021. len -= sizeof(res->packet.resolve.addroffset);
  1022. if (len < sizeof(res->packet.resolve.pad5))
  1023. return NULL;
  1024. memcpy(res->packet.resolve.pad5, mem,
  1025. sizeof(res->packet.resolve.pad5));
  1026. mem += sizeof(res->packet.resolve.pad5);
  1027. len -= sizeof(res->packet.resolve.pad5);
  1028. mem += res->packet.resolve.addroffset;
  1029. if (len < sizeof(res->packet.resolve.hostaddr))
  1030. return NULL;
  1031. memcpy(&res->packet.resolve.hostaddr, mem,
  1032. sizeof(res->packet.resolve.hostaddr));
  1033. mem += sizeof(res->packet.resolve.hostaddr);
  1034. len -= sizeof(res->packet.resolve.hostaddr);
  1035. break;
  1036. }
  1037. case MSPROXY_CONNECT_ACK:
  1038. case MSPROXY_BINDINFO:
  1039. if (len < sizeof(res->packet._5.magic1))
  1040. return NULL;
  1041. memcpy(&res->packet._5.magic1, mem, sizeof(res->packet._5.magic1));
  1042. mem += sizeof(res->packet._5.magic1);
  1043. len -= sizeof(res->packet._5.magic1);
  1044. if (len < sizeof(res->packet._5.pad5))
  1045. return NULL;
  1046. memcpy(res->packet._5.pad5, mem, sizeof(res->packet._5.pad5));
  1047. mem += sizeof(res->packet._5.pad5);
  1048. len -= sizeof(res->packet._5.pad5);
  1049. if (len < sizeof(res->packet._5.clientport))
  1050. return NULL;
  1051. memcpy(&res->packet._5.clientport, mem,
  1052. sizeof(res->packet._5.clientport));
  1053. mem += sizeof(res->packet._5.clientport);
  1054. len -= sizeof(res->packet._5.clientport);
  1055. if (len < sizeof(res->packet._5.clientaddr))
  1056. return NULL;
  1057. memcpy(&res->packet._5.clientaddr, mem,
  1058. sizeof(res->packet._5.clientaddr));
  1059. mem += sizeof(res->packet._5.clientaddr);
  1060. len -= sizeof(res->packet._5.clientaddr);
  1061. if (len < sizeof(res->packet._5.magic10))
  1062. return NULL;
  1063. memcpy(&res->packet._5.magic10, mem, sizeof(res->packet._5.magic10));
  1064. mem += sizeof(res->packet._5.magic10);
  1065. len -= sizeof(res->packet._5.magic10);
  1066. if (len < sizeof(res->packet._5.magic15))
  1067. return NULL;
  1068. memcpy(&res->packet._5.magic15, mem, sizeof(res->packet._5.magic15));
  1069. mem += sizeof(res->packet._5.magic15);
  1070. len -= sizeof(res->packet._5.magic15);
  1071. if (len < sizeof(res->packet._5.serverport))
  1072. return NULL;
  1073. memcpy(&res->packet._5.serverport, mem,
  1074. sizeof(res->packet._5.serverport));
  1075. mem += sizeof(res->packet._5.serverport);
  1076. len -= sizeof(res->packet._5.serverport);
  1077. if (len < sizeof(res->packet._5.srcport))
  1078. return NULL;
  1079. memcpy(&res->packet._5.srcport, mem, sizeof(res->packet._5.srcport));
  1080. mem += sizeof(res->packet._5.srcport);
  1081. len -= sizeof(res->packet._5.srcport);
  1082. if (len < sizeof(res->packet._5.boundport))
  1083. return NULL;
  1084. memcpy(&res->packet._5.boundport, mem,
  1085. sizeof(res->packet._5.boundport));
  1086. mem += sizeof(res->packet._5.boundport);
  1087. len -= sizeof(res->packet._5.boundport);
  1088. if (len < sizeof(res->packet._5.boundaddr))
  1089. return NULL;
  1090. memcpy(&res->packet._5.boundaddr, mem,
  1091. sizeof(res->packet._5.boundaddr));
  1092. mem += sizeof(res->packet._5.boundaddr);
  1093. len -= sizeof(res->packet._5.boundaddr);
  1094. if (len < sizeof(res->packet._5.pad10))
  1095. return NULL;
  1096. memcpy(res->packet._5.pad10, mem, sizeof(res->packet._5.pad10));
  1097. mem += sizeof(res->packet._5.pad10);
  1098. len -= sizeof(res->packet._5.pad10);
  1099. break;
  1100. case MSPROXY_CONNECT_AUTHFAILED:
  1101. case MSPROXY_BIND_AUTHFAILED:
  1102. break;
  1103. default:
  1104. if (ntohs(res->command) >> 8 == MSPROXY_CONNREFUSED
  1105. ||  ntohs(res->command) >> 12 == MSPROXY_CONNREFUSED)
  1106. ;
  1107. else
  1108. slog(LOG_DEBUG, "%s: unknown command in msproxy reply: 0x%x",
  1109. function, ntohs(res->command));
  1110. }
  1111. return mem;
  1112. }
  1113. static char *
  1114. request2mem(req, mem)
  1115. const struct msproxy_request_t *req;
  1116. char *mem;
  1117. {
  1118. memcpy(mem, &req->clientid, sizeof(req->clientid));
  1119. mem += sizeof(req->clientid);
  1120. memcpy(mem, &req->magic25, sizeof(req->magic25));
  1121. mem += sizeof(req->magic25);
  1122. memcpy(mem, &req->serverid, sizeof(req->serverid));
  1123. mem += sizeof(req->serverid);
  1124. memcpy(mem, &req->serverack, sizeof(req->serverack));
  1125. mem += sizeof(req->serverack);
  1126. memcpy(mem, &req->pad10, sizeof(req->pad10));
  1127. mem += sizeof(req->pad10);
  1128. memcpy(mem, &req->sequence, sizeof(req->sequence));
  1129. mem += sizeof(req->sequence);
  1130. memcpy(mem, &req->pad11, sizeof(req->pad11));
  1131. mem += sizeof(req->pad11);
  1132. memcpy(mem, &req->RWSP, sizeof(req->RWSP));
  1133. mem += sizeof(req->RWSP);
  1134. memcpy(mem, &req->pad15, sizeof(req->pad15));
  1135. mem += sizeof(req->pad15);
  1136. memcpy(mem, &req->command, sizeof(req->command));
  1137. mem += sizeof(req->command);
  1138. switch (ntohs(req->command)) {
  1139. case MSPROXY_HELLO:
  1140. memcpy(mem, &req->packet._1.pad1, sizeof(req->packet._1.pad1));
  1141. mem += sizeof(req->packet._1.pad1);
  1142. memcpy(mem, &req->packet._1.magic3, sizeof(req->packet._1.magic3));
  1143. mem += sizeof(req->packet._1.magic3);
  1144. memcpy(mem, &req->packet._1.pad3, sizeof(req->packet._1.pad3));
  1145. mem += sizeof(req->packet._1.pad3);
  1146. memcpy(mem, &req->packet._1.magic5, sizeof(req->packet._1.magic5));
  1147. mem += sizeof(req->packet._1.magic5);
  1148. memcpy(mem, &req->packet._1.pad5, sizeof(req->packet._1.pad5));
  1149. mem += sizeof(req->packet._1.pad5);
  1150. memcpy(mem, &req->packet._1.magic10, sizeof(req->packet._1.magic10));
  1151. mem += sizeof(req->packet._1.magic10);
  1152. memcpy(mem, &req->packet._1.pad6, sizeof(req->packet._1.pad6));
  1153. mem += sizeof(req->packet._1.pad6);
  1154. memcpy(mem, &req->packet._1.magic15, sizeof(req->packet._1.magic15));
  1155. mem += sizeof(req->packet._1.magic15);
  1156. memcpy(mem, &req->packet._1.pad10, sizeof(req->packet._1.pad10));
  1157. mem += sizeof(req->packet._1.pad10);
  1158. memcpy(mem, &req->packet._1.magic20, sizeof(req->packet._1.magic20));
  1159. mem += sizeof(req->packet._1.magic20);
  1160. memcpy(mem, &req->packet._1.magic25, sizeof(req->packet._1.magic25));
  1161. mem += sizeof(req->packet._1.magic25);
  1162. memcpy(mem, &req->packet._1.magic30, sizeof(req->packet._1.magic30));
  1163. mem += sizeof(req->packet._1.magic30);
  1164. memcpy(mem, &req->packet._1.pad20, sizeof(req->packet._1.pad20));
  1165. mem += sizeof(req->packet._1.pad20);
  1166. memcpy(mem, &req->packet._1.magic35, sizeof(req->packet._1.magic35));
  1167. mem += sizeof(req->packet._1.magic35);
  1168. memcpy(mem, &req->packet._1.pad30, sizeof(req->packet._1.pad30));
  1169. mem += sizeof(req->packet._1.pad30);
  1170. memcpy(mem, &req->packet._1.magic40, sizeof(req->packet._1.magic40));
  1171. mem += sizeof(req->packet._1.magic40);
  1172. memcpy(mem, &req->packet._1.pad40, sizeof(req->packet._1.pad40));
  1173. mem += sizeof(req->packet._1.pad40);
  1174. memcpy(mem, &req->packet._1.magic45, sizeof(req->packet._1.magic45));
  1175. mem += sizeof(req->packet._1.magic45);
  1176. memcpy(mem, &req->packet._1.pad45, sizeof(req->packet._1.pad45));
  1177. mem += sizeof(req->packet._1.pad45);
  1178. memcpy(mem, &req->packet._1.magic50, sizeof(req->packet._1.magic50));
  1179. mem += sizeof(req->packet._1.magic50);
  1180. memcpy(mem, &req->packet._1.pad50, sizeof(req->packet._1.pad50));
  1181. mem += sizeof(req->packet._1.pad50);
  1182. strcpy(mem, req->username);
  1183. mem += strlen(req->username) + 1;
  1184. strcpy(mem, req->unknown);
  1185. mem += strlen(req->unknown) + 1;
  1186. strcpy(mem, req->executable);
  1187. mem += strlen(req->executable) + 1;
  1188. strcpy(mem, req->clienthost);
  1189. mem += strlen(req->clienthost) + 1;
  1190. break;
  1191. case MSPROXY_USERINFO:
  1192. memcpy(mem, &req->packet._2.pad1, sizeof(req->packet._2.pad1));
  1193. mem += sizeof(req->packet._2.pad1);
  1194. memcpy(mem, &req->packet._2.magic3, sizeof(req->packet._2.magic3));
  1195. mem += sizeof(req->packet._2.magic3);
  1196. memcpy(mem, &req->packet._2.pad3, sizeof(req->packet._2.pad3));
  1197. mem += sizeof(req->packet._2.pad3);
  1198. memcpy(mem, &req->packet._2.magic5, sizeof(req->packet._2.magic5));
  1199. mem += sizeof(req->packet._2.magic5);
  1200. memcpy(mem, &req->packet._2.pad5, sizeof(req->packet._2.pad5));
  1201. mem += sizeof(req->packet._2.pad5);
  1202. memcpy(mem, &req->packet._2.magic10, sizeof(req->packet._2.magic10));
  1203. mem += sizeof(req->packet._2.magic10);
  1204. memcpy(mem, &req->packet._2.pad10, sizeof(req->packet._2.pad10));
  1205. mem += sizeof(req->packet._2.pad10);
  1206. memcpy(mem, &req->packet._2.magic15, sizeof(req->packet._2.magic15));
  1207. mem += sizeof(req->packet._2.magic15);
  1208. memcpy(mem, &req->packet._2.pad15, sizeof(req->packet._2.pad15));
  1209. mem += sizeof(req->packet._2.pad15);
  1210. memcpy(mem, &req->packet._2.magic20, sizeof(req->packet._2.magic20));
  1211. mem += sizeof(req->packet._2.magic20);
  1212. memcpy(mem, &req->packet._2.magic25, sizeof(req->packet._2.magic25));
  1213. mem += sizeof(req->packet._2.magic25);
  1214. memcpy(mem, &req->packet._2.magic30, sizeof(req->packet._2.magic30));
  1215. mem += sizeof(req->packet._2.magic30);
  1216. memcpy(mem, &req->packet._2.pad20, sizeof(req->packet._2.pad20));
  1217. mem += sizeof(req->packet._2.pad20);
  1218. memcpy(mem, &req->packet._2.magic35, sizeof(req->packet._2.magic35));
  1219. mem += sizeof(req->packet._2.magic35);
  1220. memcpy(mem, &req->packet._2.pad25, sizeof(req->packet._2.pad25));
  1221. mem += sizeof(req->packet._2.pad25);
  1222. memcpy(mem, &req->packet._2.magic35, sizeof(req->packet._2.magic35));
  1223. mem += sizeof(req->packet._2.magic35);
  1224. memcpy(mem, &req->packet._2.pad25, sizeof(req->packet._2.pad25));
  1225. mem += sizeof(req->packet._2.pad25);
  1226. memcpy(mem, &req->packet._2.magic50, sizeof(req->packet._2.magic50));
  1227. mem += sizeof(req->packet._2.magic50);
  1228. memcpy(mem, &req->packet._2.pad50, sizeof(req->packet._2.pad50));
  1229. mem += sizeof(req->packet._2.pad50);
  1230. memcpy(mem, &req->packet._2.magic55, sizeof(req->packet._2.magic55));
  1231. mem += sizeof(req->packet._2.magic55);
  1232. memcpy(mem, &req->packet._2.pad55, sizeof(req->packet._2.pad55));
  1233. mem += sizeof(req->packet._2.pad55);
  1234. memcpy(mem, &req->packet._2.magic60, sizeof(req->packet._2.magic60));
  1235. mem += sizeof(req->packet._2.magic60);
  1236. strcpy(mem, req->username);
  1237. mem += strlen(req->username) + 1;
  1238. strcpy(mem, req->unknown);
  1239. mem += strlen(req->unknown) + 1;
  1240. strcpy(mem, req->executable);
  1241. mem += strlen(req->executable) + 1;
  1242. strcpy(mem, req->clienthost);
  1243. mem += strlen(req->clienthost) + 1;
  1244. break;
  1245. case MSPROXY_BIND:
  1246. case MSPROXY_SOMETHING:
  1247. memcpy(mem, &req->packet._3.pad1, sizeof(req->packet._3.pad1));
  1248. mem += sizeof(req->packet._3.pad1);
  1249. memcpy(mem, &req->packet._3.magic2, sizeof(req->packet._3.magic2));
  1250. mem += sizeof(req->packet._3.magic2);
  1251. memcpy(mem, &req->packet._3.pad10, sizeof(req->packet._3.pad10));
  1252. mem += sizeof(req->packet._3.pad10);
  1253. memcpy(mem, &req->packet._3.bindaddr, sizeof(req->packet._3.bindaddr));
  1254. mem += sizeof(req->packet._3.bindaddr);
  1255. memcpy(mem, &req->packet._3.bindport, sizeof(req->packet._3.bindport));
  1256. mem += sizeof(req->packet._3.bindport);
  1257. memcpy(mem, &req->packet._3.pad15, sizeof(req->packet._3.pad15));
  1258. mem += sizeof(req->packet._3.pad15);
  1259. memcpy(mem, &req->packet._3.magic3, sizeof(req->packet._3.magic3));
  1260. mem += sizeof(req->packet._3.magic3);
  1261. memcpy(mem, &req->packet._3.boundport,
  1262. sizeof(req->packet._3.boundport));
  1263. mem += sizeof(req->packet._3.boundport);
  1264. memcpy(mem, &req->packet._3.pad20, sizeof(req->packet._3.pad20));
  1265. mem += sizeof(req->packet._3.pad20);
  1266. memcpy(mem, &req->packet._3.NTLMSSP, sizeof(req->packet._3.NTLMSSP));
  1267. mem += sizeof(req->packet._3.NTLMSSP);
  1268. memcpy(mem, &req->packet._3.magic5, sizeof(req->packet._3.magic5));
  1269. mem += sizeof(req->packet._3.magic5);
  1270. memcpy(mem, &req->packet._3.pad25, sizeof(req->packet._3.pad25));
  1271. mem += sizeof(req->packet._3.pad25);
  1272. memcpy(mem, &req->packet._3.magic10, sizeof(req->packet._3.magic10));
  1273. mem += sizeof(req->packet._3.magic10);
  1274. memcpy(mem, &req->packet._3.magic15, sizeof(req->packet._3.magic15));
  1275. mem += sizeof(req->packet._3.magic15);
  1276. memcpy(mem, &req->packet._3.magic20, sizeof(req->packet._3.magic20));
  1277. mem += sizeof(req->packet._3.magic20);
  1278. memcpy(mem, &req->packet._3.pad30, sizeof(req->packet._3.pad30));
  1279. mem += sizeof(req->packet._3.pad30);
  1280. memcpy(mem, &req->packet._3.magic25, sizeof(req->packet._3.magic25));
  1281. mem += sizeof(req->packet._3.magic25);
  1282. memcpy(mem, &req->packet._3.magic30, sizeof(req->packet._3.magic30));
  1283. mem += sizeof(req->packet._3.magic30);
  1284. memcpy(mem, &req->packet._3.pad40, sizeof(req->packet._3.pad40));
  1285. mem += sizeof(req->packet._3.pad40);
  1286. memcpy(mem, &req->packet._3.magic50, sizeof(req->packet._3.magic50));
  1287. mem += sizeof(req->packet._3.magic50);
  1288. memcpy(mem, &req->packet._3.pad50, sizeof(req->packet._3.pad50));
  1289. mem += sizeof(req->packet._3.pad50);
  1290. memcpy(mem, &req->packet._3.magic55, sizeof(req->packet._3.magic55));
  1291. mem += sizeof(req->packet._3.magic55);
  1292. memcpy(mem, &req->packet._3.pad55, sizeof(req->packet._3.pad55));
  1293. mem += sizeof(req->packet._3.pad55);
  1294. break;
  1295. case MSPROXY_BIND2:
  1296. case MSPROXY_SOMETHING_2:
  1297. memcpy(mem, &req->packet._4.pad1, sizeof(req->packet._4.pad1));
  1298. mem += sizeof(req->packet._4.pad1);
  1299. memcpy(mem, &req->packet._4.magic1, sizeof(req->packet._4.magic1));
  1300. mem += sizeof(req->packet._4.magic1);
  1301. memcpy(mem, &req->packet._4.magic2, sizeof(req->packet._4.magic2));
  1302. mem += sizeof(req->packet._4.magic2);
  1303. memcpy(mem, &req->packet._4.pad2, sizeof(req->packet._4.pad2));
  1304. mem += sizeof(req->packet._4.pad2);
  1305. memcpy(mem, &req->packet._4.magic3, sizeof(req->packet._4.magic3));
  1306. mem += sizeof(req->packet._4.magic3);
  1307. memcpy(mem, &req->packet._4.pad3, sizeof(req->packet._4.pad3));
  1308. mem += sizeof(req->packet._4.pad3);
  1309. memcpy(mem, &req->packet._4.magic4, sizeof(req->packet._4.magic4));
  1310. mem += sizeof(req->packet._4.magic4);
  1311. memcpy(mem, &req->packet._4.boundport,
  1312. sizeof(req->packet._4.boundport));
  1313. mem += sizeof(req->packet._4.boundport);
  1314. memcpy(mem, &req->packet._4.pad4, sizeof(req->packet._4.pad4));
  1315. mem += sizeof(req->packet._4.pad4);
  1316. memcpy(mem, &req->packet._4.NTLMSSP, sizeof(req->packet._4.NTLMSSP));
  1317. mem += sizeof(req->packet._4.NTLMSSP);
  1318. memcpy(mem, &req->packet._4.magic5, sizeof(req->packet._4.magic5));
  1319. mem += sizeof(req->packet._4.magic5);
  1320. memcpy(mem, &req->packet._4.pad5, sizeof(req->packet._4.pad5));
  1321. mem += sizeof(req->packet._4.pad5);
  1322. memcpy(mem, &req->packet._4.magic10, sizeof(req->packet._4.magic10));
  1323. mem += sizeof(req->packet._4.magic10);
  1324. memcpy(mem, &req->packet._4.magic10, sizeof(req->packet._4.magic10));
  1325. mem += sizeof(req->packet._4.magic10);
  1326. memcpy(mem, &req->packet._4.magic20, sizeof(req->packet._4.magic20));
  1327. mem += sizeof(req->packet._4.magic20);
  1328. memcpy(mem, &req->packet._4.pad10, sizeof(req->packet._4.pad10));
  1329. mem += sizeof(req->packet._4.pad10);
  1330. memcpy(mem, &req->packet._4.magic30, sizeof(req->packet._4.magic30));
  1331. mem += sizeof(req->packet._4.magic30);
  1332. memcpy(mem, &req->packet._4.pad15, sizeof(req->packet._4.pad15));
  1333. mem += sizeof(req->packet._4.pad15);
  1334. memcpy(mem, &req->packet._4.magic35, sizeof(req->packet._4.magic35));
  1335. mem += sizeof(req->packet._4.magic35);
  1336. memcpy(mem, &req->packet._4.magic40, sizeof(req->packet._4.magic40));
  1337. mem += sizeof(req->packet._4.magic40);
  1338. memcpy(mem, &req->packet._4.magic45, sizeof(req->packet._4.magic45));
  1339. mem += sizeof(req->packet._4.magic45);
  1340. memcpy(mem, &req->packet._4.pad20, sizeof(req->packet._4.pad20));
  1341. mem += sizeof(req->packet._4.pad20);
  1342. memcpy(mem, &req->packet._4.magic50, sizeof(req->packet._4.magic50));
  1343. mem += sizeof(req->packet._4.magic50);
  1344. memcpy(mem, &req->packet._4.magic55, sizeof(req->packet._4.magic55));
  1345. mem += sizeof(req->packet._4.magic55);
  1346. memcpy(mem, &req->packet._4.magic60, sizeof(req->packet._4.magic60));
  1347. mem += sizeof(req->packet._4.magic60);
  1348. memcpy(mem, &req->packet._4.pad25, sizeof(req->packet._4.pad25));
  1349. mem += sizeof(req->packet._4.pad25);
  1350. memcpy(mem, &req->packet._4.magic65, sizeof(req->packet._4.magic65));
  1351. mem += sizeof(req->packet._4.magic65);
  1352. memcpy(mem, &req->packet._4.magic70, sizeof(req->packet._4.magic70));
  1353. mem += sizeof(req->packet._4.magic70);
  1354. memcpy(mem, &req->packet._4.magic75, sizeof(req->packet._4.magic75));
  1355. mem += sizeof(req->packet._4.magic75);
  1356. break;
  1357. case MSPROXY_RESOLVE:
  1358. memcpy(mem, &req->packet.resolve.hostlength,
  1359. sizeof(req->packet.resolve.hostlength));
  1360. mem += sizeof(req->packet.resolve.hostlength);
  1361. memcpy(mem, &req->packet.resolve.pad1,
  1362. sizeof(req->packet.resolve.pad1));
  1363. mem += sizeof(req->packet.resolve.pad1);
  1364. memcpy(mem, &req->packet.resolve.host,
  1365. (size_t)req->packet.resolve.hostlength);
  1366. mem += req->packet.resolve.hostlength;
  1367. break;
  1368. case MSPROXY_LISTEN:
  1369. case MSPROXY_CONNECT:
  1370. memcpy(mem, &req->packet._5.magic1, sizeof(req->packet._5.magic1));
  1371. mem += sizeof(req->packet._5.magic1);
  1372. memcpy(mem, &req->packet._5.pad1, sizeof(req->packet._5.pad1));
  1373. mem += sizeof(req->packet._5.pad1);
  1374. memcpy(mem, &req->packet._5.magic3, sizeof(req->packet._5.magic3));
  1375. mem += sizeof(req->packet._5.magic3);
  1376. memcpy(mem, &req->packet._5.pad5, sizeof(req->packet._5.pad5));
  1377. mem += sizeof(req->packet._5.pad5);
  1378. memcpy(mem, &req->packet._5.magic6, sizeof(req->packet._5.magic6));
  1379. mem += sizeof(req->packet._5.magic6);
  1380. memcpy(mem, &req->packet._5.destport, sizeof(req->packet._5.destport));
  1381. mem += sizeof(req->packet._5.destport);
  1382. memcpy(mem, &req->packet._5.destaddr, sizeof(req->packet._5.destaddr));
  1383. mem += sizeof(req->packet._5.destaddr);
  1384. memcpy(mem, &req->packet._5.pad10, sizeof(req->packet._5.pad10));
  1385. mem += sizeof(req->packet._5.pad10);
  1386. memcpy(mem, &req->packet._5.magic10, sizeof(req->packet._5.magic10));
  1387. mem += sizeof(req->packet._5.magic10);
  1388. memcpy(mem, &req->packet._5.pad15, sizeof(req->packet._5.pad15));
  1389. mem += sizeof(req->packet._5.pad15);
  1390. memcpy(mem, &req->packet._5.srcport, sizeof(req->packet._5.srcport));
  1391. mem += sizeof(req->packet._5.srcport);
  1392. memcpy(mem, &req->packet._5.pad20, sizeof(req->packet._5.pad20));
  1393. mem += sizeof(req->packet._5.pad20);
  1394. strcpy(mem, req->executable);
  1395. break;
  1396. case MSPROXY_BINDINFO_ACK:
  1397. case MSPROXY_CONNECTED:
  1398. memcpy(mem, &req->packet._6.magic1, sizeof(req->packet._6.magic1));
  1399. mem += sizeof(req->packet._6.magic1);
  1400. memcpy(mem, req->packet._6.pad5, sizeof(req->packet._6.pad5));
  1401. mem += sizeof(req->packet._6.pad5);
  1402. memcpy(mem, &req->packet._6.magic5, sizeof(req->packet._6.magic5));
  1403. mem += sizeof(req->packet._6.magic5);
  1404. memcpy(mem, &req->packet._6.magic10, sizeof(req->packet._6.magic10));
  1405. mem += sizeof(req->packet._6.magic10);
  1406. memcpy(mem, req->packet._6.pad10, sizeof(req->packet._6.pad10));
  1407. mem += sizeof(req->packet._6.pad10);
  1408. memcpy(mem, &req->packet._6.magic15, sizeof(req->packet._6.magic15));
  1409. mem += sizeof(req->packet._6.magic15);
  1410. memcpy(mem, &req->packet._6.magic16, sizeof(req->packet._6.magic16));
  1411. mem += sizeof(req->packet._6.magic16);
  1412. memcpy(mem, &req->packet._6.magic20, sizeof(req->packet._6.magic20));
  1413. mem += sizeof(req->packet._6.magic20);
  1414. memcpy(mem, &req->packet._6.clientport,
  1415. sizeof(req->packet._6.clientport));
  1416. mem += sizeof(req->packet._6.clientport);
  1417. memcpy(mem, &req->packet._6.clientaddr,
  1418. sizeof(req->packet._6.clientaddr));
  1419. mem += sizeof(req->packet._6.clientaddr);
  1420. memcpy(mem, &req->packet._6.magic30, sizeof(req->packet._6.magic30));
  1421. mem += sizeof(req->packet._6.magic30);
  1422. memcpy(mem, &req->packet._6.magic35, sizeof(req->packet._6.magic35));
  1423. mem += sizeof(req->packet._6.magic35);
  1424. memcpy(mem, &req->packet._6.serverport,
  1425. sizeof(req->packet._6.serverport));
  1426. mem += sizeof(req->packet._6.serverport);
  1427. memcpy(mem, &req->packet._6.srcport, sizeof(req->packet._6.srcport));
  1428. mem += sizeof(req->packet._6.srcport);
  1429. memcpy(mem, &req->packet._6.boundport,
  1430. sizeof(req->packet._6.boundport));
  1431. mem += sizeof(req->packet._6.boundport);
  1432. memcpy(mem, &req->packet._6.boundaddr,
  1433. sizeof(req->packet._6.boundaddr));
  1434. mem += sizeof(req->packet._6.boundaddr);
  1435. memcpy(mem, req->packet._6.pad30, sizeof(req->packet._6.pad30));
  1436. mem += sizeof(req->packet._6.pad30);
  1437. break;
  1438. case MSPROXY_SESSIONEND:
  1439. break;
  1440. default:
  1441. SERRX(req->command);
  1442. }
  1443. return mem;
  1444. }
  1445. void
  1446. msproxy_sessionsend(void)
  1447. {
  1448. const char *function = "msproxy_sessionsend()";
  1449. int i, max;
  1450. struct socksfd_t *socksfd;
  1451. slog(LOG_DEBUG, function);
  1452. for (i = 0, max = getdtablesize(); i < max; ++i) {
  1453. if ((socksfd = socks_getaddr((unsigned int)i)) == NULL)
  1454. continue;
  1455. if (socksfd->state.version != MSPROXY_V2)
  1456. continue;
  1457. msproxy_sessionend(socksfd->control, &socksfd->state.msproxy);
  1458. }
  1459. }
  1460. static void
  1461. msproxy_sessionend(s, msproxy)
  1462. int s;
  1463. struct msproxy_state_t *msproxy;
  1464. {
  1465. const char *function = "msproxy_sessionend()";
  1466. struct msproxy_request_t req;
  1467. slog(LOG_DEBUG, function);
  1468. bzero(&req, sizeof(req));
  1469. *req.username = NUL;
  1470. *req.unknown = NUL;
  1471. *req.executable = NUL;
  1472. *req.clienthost = NUL;
  1473. req.clientid = msproxy->clientid;
  1474. req.serverid = msproxy->serverid;
  1475. req.command = htons(MSPROXY_SESSIONEND);
  1476. send_msprequest(s, msproxy, &req);
  1477. }
  1478. /* ARGSUSED */
  1479. static void
  1480. msproxy_keepalive(sig)
  1481. int sig;
  1482. {
  1483. const char *function = "msproxy_keepalive()";
  1484. struct msproxy_request_t req;
  1485. struct msproxy_response_t res;
  1486. struct socksfd_t *socksfd;
  1487. int i, max;
  1488. slog(LOG_DEBUG, function);
  1489. for (i = 0, max = getdtablesize(); i < max; ++i) {
  1490. if ((socksfd = socks_getaddr((unsigned int)i)) == NULL)
  1491. continue;
  1492. if (socksfd->state.version != MSPROXY_V2
  1493. ||  socksfd->state.inprogress)
  1494. continue;
  1495. slog(LOG_DEBUG, "%s: sending keepalive packet", function);
  1496. bzero(&req, sizeof(req));
  1497. req.clientid = socksfd->state.msproxy.clientid;
  1498. req.serverid = socksfd->state.msproxy.serverid;
  1499. req.command = htons(MSPROXY_HELLO);
  1500. if (send_msprequest(socksfd->control, &socksfd->state.msproxy, &req)
  1501. == -1)
  1502. return;
  1503. if (recv_mspresponse(socksfd->control, &socksfd->state.msproxy, &res)
  1504. == -1)
  1505. return;
  1506. }
  1507. }