enclink.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /* enclink.c
  21. *
  22. */
  23. #include "common.h"
  24. #include "enclink.h"
  25. #include "dcc.h"
  26. #include "net.h"
  27. #include "misc.h"
  28. #include <stdarg.h>
  29. static void ghost_link_case(int idx, direction_t direction)
  30. {
  31. int snum = findanysnum(dcc[idx].sock);
  32. if (snum >= 0) {
  33. char initkey[33] = "", *tmp2 = NULL;
  34. char tmp[70] = "";
  35. char *keyp = NULL, *nick1 = NULL, *nick2 = NULL;
  36. port_t port = 0;
  37. const char salt1[] = SALT1;
  38. const char salt2[] = SALT2;
  39. if (direction == TO) {
  40. keyp = socklist[snum].ikey;
  41. nick1 = strdup(dcc[idx].nick);
  42. nick2 = strdup(conf.bot->nick);
  43. port = htons(dcc[idx].port);
  44. } else if (direction == FROM) {
  45. keyp = socklist[snum].okey;
  46. nick1 = strdup(conf.bot->nick);
  47. nick2 = strdup(dcc[idx].nick);
  48. struct sockaddr_in sa;
  49. socklen_t socklen = sizeof(sa);
  50. egg_bzero(&sa, socklen);
  51. getsockname(socklist[snum].sock, (struct sockaddr *) &sa, &socklen);
  52. port = sa.sin_port;
  53. }
  54. /* initkey-gen */
  55. /* salt1 salt2 port mynick conf.bot->nick */
  56. sprintf(tmp, STR("%s@%s@%4x@%s@%s"), salt1, salt2, port, strtoupper(nick1), strtoupper(nick2));
  57. free(nick1);
  58. free(nick2);
  59. strlcpy(keyp, SHA1(tmp), ENC_KEY_LEN + 1);
  60. #ifdef DEBUG
  61. putlog(LOG_DEBUG, "@", "Link hash for %s: %s", dcc[idx].nick, tmp);
  62. putlog(LOG_DEBUG, "@", "outkey (%zu): %s", strlen(keyp), keyp);
  63. #endif
  64. OPENSSL_cleanse(tmp, sizeof(tmp));
  65. SHA1(NULL);
  66. if (direction == FROM) {
  67. make_rand_str(initkey, 32); /* set the initial out/in link key to random chars. */
  68. socklist[snum].oseed = random();
  69. socklist[snum].iseed = socklist[snum].oseed;
  70. tmp2 = encrypt_string(salt2, initkey);
  71. putlog(LOG_BOTS, "*", STR("Sending encrypted link handshake to %s..."), dcc[idx].nick);
  72. socklist[snum].encstatus = 1;
  73. socklist[snum].gz = 1;
  74. link_send(idx, STR("elink %s %d\n"), tmp2, socklist[snum].oseed);
  75. free(tmp2);
  76. strlcpy(socklist[snum].okey, initkey, ENC_KEY_LEN + 1);
  77. strlcpy(socklist[snum].ikey, initkey, ENC_KEY_LEN + 1);
  78. OPENSSL_cleanse(initkey, sizeof(initkey));
  79. } else {
  80. socklist[snum].encstatus = 1;
  81. socklist[snum].gz = 1;
  82. }
  83. } else {
  84. putlog(LOG_MISC, "*", STR("Couldn't find socket for %s connection?? Shouldn't happen :/"), dcc[idx].nick);
  85. killsock(dcc[idx].sock);
  86. lostdcc(idx);
  87. }
  88. }
  89. static int
  90. prand(int *seed, int range)
  91. {
  92. long long i1 = *seed;
  93. i1 = (i1 * 0x08088405 + 1) & 0xFFFFFFFF;
  94. *seed = i1;
  95. return ((i1 * range) >> 32);
  96. }
  97. static int ghost_read(int snum, char *src, size_t *len)
  98. {
  99. char *line = decrypt_string(socklist[snum].ikey, src);
  100. strcpy(src, line);
  101. OPENSSL_cleanse(line, strlen(line) + 1);
  102. free(line);
  103. if (socklist[snum].iseed) {
  104. *(dword *) & socklist[snum].ikey[0] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  105. *(dword *) & socklist[snum].ikey[4] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  106. *(dword *) & socklist[snum].ikey[8] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  107. *(dword *) & socklist[snum].ikey[12] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  108. if (!socklist[snum].iseed)
  109. socklist[snum].iseed++;
  110. }
  111. // *len = strlen(src);
  112. return OK;
  113. }
  114. static char *ghost_write(int snum, char *src, size_t *len)
  115. {
  116. char *srcbuf = NULL, *buf = NULL, *line = NULL, *eol = NULL, *eline = NULL;
  117. size_t bufpos = 0;
  118. const size_t bufsiz = *len + 9 + 1;
  119. srcbuf = (char *) my_calloc(1, bufsiz);
  120. strlcpy(srcbuf, src, bufsiz);
  121. line = srcbuf;
  122. eol = strchr(line, '\n');
  123. while (eol) {
  124. *eol++ = 0;
  125. eline = encrypt_string(socklist[snum].okey, line);
  126. if (socklist[snum].oseed) {
  127. *(dword *) & socklist[snum].okey[0] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  128. *(dword *) & socklist[snum].okey[4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  129. *(dword *) & socklist[snum].okey[8] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  130. *(dword *) & socklist[snum].okey[12] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  131. if (!socklist[snum].oseed)
  132. socklist[snum].oseed++;
  133. }
  134. buf = (char *) my_realloc(buf, bufpos + strlen(eline) + 1 + 9);
  135. strcpy((char *) &buf[bufpos], eline);
  136. free(eline);
  137. strcat(buf, "\n");
  138. bufpos = strlen(buf);
  139. line = eol;
  140. eol = strchr(line, '\n');
  141. }
  142. if (line[0]) {
  143. eline = encrypt_string(socklist[snum].okey, line);
  144. if (socklist[snum].oseed) {
  145. *(dword *) & socklist[snum].okey[0] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  146. *(dword *) & socklist[snum].okey[4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  147. *(dword *) & socklist[snum].okey[8] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  148. *(dword *) & socklist[snum].okey[12] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  149. if (!socklist[snum].oseed)
  150. socklist[snum].oseed++;
  151. }
  152. buf = (char *) my_realloc(buf, bufpos + strlen(eline) + 1 + 9);
  153. strcpy((char *) &buf[bufpos], eline);
  154. free(eline);
  155. strcat(buf, "\n");
  156. }
  157. OPENSSL_cleanse(srcbuf, bufsiz);
  158. free(srcbuf);
  159. *len = strlen(buf);
  160. return buf;
  161. }
  162. void ghost_parse(int idx, int snum, char *buf)
  163. {
  164. /* putlog(LOG_DEBUG, "*", "Got elink: %s %s", code, buf); */
  165. /* Set the socket key and we're linked */
  166. char *code = newsplit(&buf);
  167. if (!egg_strcasecmp(code, STR("elink"))) {
  168. const char salt2[] = SALT2;
  169. char *tmp = decrypt_string(salt2, newsplit(&buf));
  170. strlcpy(socklist[snum].okey, tmp, ENC_KEY_LEN + 1);
  171. OPENSSL_cleanse(tmp, strlen(tmp));
  172. free(tmp);
  173. strlcpy(socklist[snum].ikey, socklist[snum].okey, ENC_KEY_LEN + 1);
  174. socklist[snum].iseed = atoi(buf);
  175. socklist[snum].oseed = atoi(buf);
  176. putlog(LOG_BOTS, "*", STR("Handshake with %s succeeded, we're linked."), dcc[idx].nick);
  177. link_done(idx);
  178. }
  179. }
  180. #ifdef no
  181. static int binary_read(int snum, char *src, size_t *len)
  182. {
  183. char *line = NULL;
  184. line = decrypt_binary(socklist[snum].ikey, (unsigned char *) src, len);
  185. strlcpy(src, line, SGRAB + 10);
  186. free(line);
  187. if (socklist[snum].iseed) {
  188. *(dword *) & socklist[snum].ikey[0] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  189. *(dword *) & socklist[snum].ikey[4] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  190. *(dword *) & socklist[snum].ikey[8] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  191. *(dword *) & socklist[snum].ikey[12] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  192. if (!socklist[snum].iseed)
  193. socklist[snum].iseed++;
  194. }
  195. // *len = strlen(src);
  196. return OK;
  197. }
  198. static char *binary_write(int snum, char *src, size_t *len)
  199. {
  200. char *srcbuf = NULL, *buf = NULL, *line = NULL, *eol = NULL, *eline = NULL;
  201. size_t bufpos = 0, bufsiz = *len + 9 + 1;
  202. srcbuf = (char *) my_calloc(1, bufsiz);
  203. strcpy(srcbuf, src);
  204. line = srcbuf;
  205. eol = strchr(line, '\n');
  206. while (eol) {
  207. *eol++ = 0;
  208. eline = encrypt_binary(socklist[snum.okey, (unsigned char *) line, len);
  209. if (socklist[snum].oseed) {
  210. *(dword *) & socklist[snum].okey[0] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  211. *(dword *) & socklist[snum].okey[4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  212. *(dword *) & socklist[snum].okey[8] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  213. *(dword *) & socklist[snum].okey[12] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  214. if (!socklist[snum].oseed)
  215. socklist[snum].oseed++;
  216. }
  217. buf = (char *) my_realloc(buf, bufpos + len + 1 + 9);
  218. strcpy((char *) &buf[bufpos], eline);
  219. free(eline);
  220. strcat(buf, "\n");
  221. bufpos = strlen(buf);
  222. line = eol;
  223. eol = strchr(line, '\n');
  224. }
  225. if (line[0]) {
  226. eline = encrypt_string(socklist[snum].okey, line);
  227. if (socklist[snum].oseed) {
  228. *(dword *) & socklist[snum].okey[0] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  229. *(dword *) & socklist[snum].okey[4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  230. *(dword *) & socklist[snum].okey[8] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  231. *(dword *) & socklist[snum].okey[12] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  232. if (!socklist[snum].oseed)
  233. socklist[snum].oseed++;
  234. }
  235. buf = (char *) my_realloc(buf, bufpos + strlen(eline) + 1 + 9);
  236. strcpy((char *) &buf[bufpos], eline);
  237. free(eline);
  238. strcat(buf, "\n");
  239. }
  240. OPENSSL_cleanse(srcbuf, bufsiz);
  241. free(srcbuf);
  242. *len = strlen(buf);
  243. return buf;
  244. }
  245. #endif
  246. void link_send(int idx, const char *format, ...)
  247. {
  248. char s[2001] = "";
  249. va_list va;
  250. va_start(va, format);
  251. egg_vsnprintf(s, sizeof(s) - 1, format, va);
  252. va_end(va);
  253. remove_crlf(s);
  254. dprintf(-dcc[idx].sock, STR("neg! %s\n"), s);
  255. }
  256. void link_done(int idx)
  257. {
  258. dprintf(-dcc[idx].sock, STR("neg.\n"));
  259. }
  260. int link_find_by_type(int type)
  261. {
  262. int i = 0;
  263. for (i = 0; enclink[i].name; i++)
  264. if (type == enclink[i].type)
  265. return i;
  266. return -1;
  267. }
  268. void link_link(int idx, int type, int i, direction_t direction)
  269. {
  270. if (i == -1 && type != -1) {
  271. for (i = 0; enclink[i].name; i++) {
  272. if (enclink[i].type == type)
  273. break;
  274. }
  275. }
  276. if (i != -1 && enclink[i].link)
  277. (enclink[i].link) (idx, direction);
  278. else if (direction == TO) /* problem finding function, just assume we're done */
  279. link_done(idx);
  280. return;
  281. }
  282. int link_read(int snum, char *buf, size_t *len)
  283. {
  284. int i = socklist[snum].enclink;
  285. if (i != -1 && enclink[i].read)
  286. return (enclink[i].read) (snum, buf, len);
  287. return -1;
  288. }
  289. char *link_write(int snum, char *buf, size_t *len)
  290. {
  291. int i = socklist[snum].enclink;
  292. if (i != -1 && enclink[i].write)
  293. return ((enclink[i].write) (snum, buf, len));
  294. return buf;
  295. }
  296. void link_hash(int idx, char *rand)
  297. {
  298. char hash[60] = "";
  299. /* nothing fancy, just something simple that can stop people from playing */
  300. simple_snprintf(hash, sizeof(hash), STR("enclink%s"), rand);
  301. strlcpy(dcc[idx].shahash, SHA1(hash), SHA_HASH_LENGTH + 1);
  302. SHA1(NULL);
  303. OPENSSL_cleanse(hash, sizeof(hash));
  304. return;
  305. }
  306. void link_parse(int idx, char *buf)
  307. {
  308. int snum = findanysnum(dcc[idx].sock);
  309. int i = socklist[snum].enclink;
  310. if (i >= 0 && enclink[i].parse)
  311. (enclink[i].parse) (idx, snum, buf);
  312. return;
  313. }
  314. void link_get_method(int idx)
  315. {
  316. if (!dcc[idx].type)
  317. return;
  318. int n = dcc[idx].u.enc->method_number;
  319. if (enclink[n].name)
  320. dcc[idx].u.enc->method = &(enclink[n]);
  321. dcc[idx].u.enc->method_number++;
  322. }
  323. /* the order of entries here determines which will be picked */
  324. struct enc_link enclink[] = {
  325. { "ghost+case3", LINK_GHOSTCASE3, ghost_link_case, ghost_write, ghost_read, ghost_parse },
  326. { "cleartext", LINK_CLEARTEXT, NULL, NULL, NULL, NULL },
  327. { NULL, 0, NULL, NULL, NULL, NULL }
  328. };