enclink.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 *keyp = NULL, *nick1 = NULL, *nick2 = NULL;
  35. port_t port = 0;
  36. const char salt1[] = SALT1;
  37. const char salt2[] = SALT2;
  38. if (direction == TO) {
  39. keyp = socklist[snum].ikey;
  40. nick1 = strdup(dcc[idx].nick);
  41. nick2 = strdup(conf.bot->nick);
  42. port = htons(dcc[idx].port);
  43. } else if (direction == FROM) {
  44. keyp = socklist[snum].okey;
  45. nick1 = strdup(conf.bot->nick);
  46. nick2 = strdup(dcc[idx].nick);
  47. struct sockaddr_in sa;
  48. socklen_t socklen = sizeof(sa);
  49. bzero(&sa, socklen);
  50. getsockname(socklist[snum].sock, (struct sockaddr *) &sa, &socklen);
  51. if (sa.sin_family == AF_UNIX)
  52. port = 0;
  53. else
  54. port = sa.sin_port;
  55. }
  56. /* initkey-gen */
  57. /* salt1 salt2 port mynick conf.bot->nick */
  58. char tmp[SALT1LEN + 1 + SALT2LEN + 1 + 4 + 1 + HANDLEN + 1 + HANDLEN + 1] = "";
  59. simple_snprintf(tmp, sizeof(tmp), STR("%s@%s@%4x@%s@%s"), salt1, salt2, port, strtoupper(nick1), strtoupper(nick2));
  60. free(nick1);
  61. free(nick2);
  62. strlcpy(keyp, SHA1(tmp), ENC_KEY_LEN + 1);
  63. #ifdef DEBUG
  64. putlog(LOG_DEBUG, "@", "Link hash for %s: %s", dcc[idx].nick, tmp);
  65. putlog(LOG_DEBUG, "@", "outkey (%zu): %s", strlen(keyp), keyp);
  66. #endif
  67. OPENSSL_cleanse(tmp, sizeof(tmp));
  68. SHA1(NULL);
  69. if (direction == FROM) {
  70. make_rand_str(initkey, 32); /* set the initial out/in link key to random chars. */
  71. socklist[snum].oseed = random();
  72. socklist[snum].iseed = socklist[snum].oseed;
  73. tmp2 = encrypt_string(salt2, initkey);
  74. putlog(LOG_BOTS, "*", STR("Sending encrypted link handshake to %s..."), dcc[idx].nick);
  75. socklist[snum].encstatus = 1;
  76. socklist[snum].gz = 1;
  77. link_send(idx, STR("elink %s %d\n"), tmp2, socklist[snum].oseed);
  78. free(tmp2);
  79. strlcpy(socklist[snum].okey, initkey, ENC_KEY_LEN + 1);
  80. strlcpy(socklist[snum].ikey, initkey, ENC_KEY_LEN + 1);
  81. OPENSSL_cleanse(initkey, sizeof(initkey));
  82. } else {
  83. socklist[snum].encstatus = 1;
  84. socklist[snum].gz = 1;
  85. }
  86. } else {
  87. putlog(LOG_MISC, "*", STR("Couldn't find socket for %s connection?? Shouldn't happen :/"), dcc[idx].nick);
  88. killsock(dcc[idx].sock);
  89. lostdcc(idx);
  90. }
  91. }
  92. static int
  93. prand(int *seed, int range)
  94. {
  95. long long i1 = *seed;
  96. i1 = (i1 * 0x08088405 + 1) & 0xFFFFFFFF;
  97. *seed = i1;
  98. return ((i1 * range) >> 32);
  99. }
  100. static int ghost_read(int snum, char *src, size_t *len)
  101. {
  102. char *line = decrypt_string(socklist[snum].ikey, src);
  103. strcpy(src, line);
  104. OPENSSL_cleanse(line, strlen(line) + 1);
  105. free(line);
  106. if (socklist[snum].iseed) {
  107. *(dword *) & socklist[snum].ikey[0] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  108. *(dword *) & socklist[snum].ikey[4] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  109. *(dword *) & socklist[snum].ikey[8] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  110. *(dword *) & socklist[snum].ikey[12] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  111. if (!socklist[snum].iseed)
  112. socklist[snum].iseed++;
  113. }
  114. // *len = strlen(src);
  115. return OK;
  116. }
  117. static const char *ghost_write(int snum, const char *src, size_t *len)
  118. {
  119. static char buf[SGRAB + 14] = "";
  120. char *srcbuf = NULL, *line = NULL, *eol = NULL, *eline = NULL;
  121. size_t bufpos = 0;
  122. const size_t bufsiz = *len + 9 + 1;
  123. srcbuf = (char *) my_calloc(1, bufsiz);
  124. strlcpy(srcbuf, src, bufsiz);
  125. line = srcbuf;
  126. eol = strchr(line, '\n');
  127. while (eol) {
  128. *eol++ = 0;
  129. eline = encrypt_string(socklist[snum].okey, line);
  130. if (socklist[snum].oseed) {
  131. *(dword *) & socklist[snum].okey[0] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  132. *(dword *) & socklist[snum].okey[4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  133. *(dword *) & socklist[snum].okey[8] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  134. *(dword *) & socklist[snum].okey[12] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  135. if (!socklist[snum].oseed)
  136. socklist[snum].oseed++;
  137. }
  138. // buf = (char *) my_realloc(buf, bufpos + strlen(eline) + 1 + 9);
  139. strcpy((char *) &buf[bufpos], eline);
  140. free(eline);
  141. strcat(buf, "\n");
  142. bufpos = strlen(buf);
  143. line = eol;
  144. eol = strchr(line, '\n');
  145. }
  146. if (line[0]) {
  147. eline = encrypt_string(socklist[snum].okey, line);
  148. if (socklist[snum].oseed) {
  149. *(dword *) & socklist[snum].okey[0] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  150. *(dword *) & socklist[snum].okey[4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  151. *(dword *) & socklist[snum].okey[8] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  152. *(dword *) & socklist[snum].okey[12] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  153. if (!socklist[snum].oseed)
  154. socklist[snum].oseed++;
  155. }
  156. // buf = (char *) my_realloc(buf, bufpos + strlen(eline) + 1 + 9);
  157. strcpy((char *) &buf[bufpos], eline);
  158. free(eline);
  159. strcat(buf, "\n");
  160. }
  161. OPENSSL_cleanse(srcbuf, bufsiz);
  162. free(srcbuf);
  163. *len = strlen(buf);
  164. return buf;
  165. }
  166. void ghost_parse(int idx, int snum, char *buf)
  167. {
  168. /* putlog(LOG_DEBUG, "*", "Got elink: %s %s", code, buf); */
  169. /* Set the socket key and we're linked */
  170. char *code = newsplit(&buf);
  171. if (!strcasecmp(code, STR("elink"))) {
  172. const char salt2[] = SALT2;
  173. char *tmp = decrypt_string(salt2, newsplit(&buf));
  174. strlcpy(socklist[snum].okey, tmp, ENC_KEY_LEN + 1);
  175. OPENSSL_cleanse(tmp, strlen(tmp));
  176. free(tmp);
  177. strlcpy(socklist[snum].ikey, socklist[snum].okey, ENC_KEY_LEN + 1);
  178. socklist[snum].iseed = atoi(buf);
  179. socklist[snum].oseed = atoi(buf);
  180. putlog(LOG_BOTS, "*", STR("Handshake with %s succeeded, we're linked."), dcc[idx].nick);
  181. link_done(idx);
  182. }
  183. }
  184. void link_send(int idx, const char *format, ...)
  185. {
  186. char s[2001] = "";
  187. va_list va;
  188. va_start(va, format);
  189. egg_vsnprintf(s, sizeof(s) - 1, format, va);
  190. va_end(va);
  191. remove_crlf(s);
  192. dprintf(-dcc[idx].sock, STR("neg! %s\n"), s);
  193. }
  194. void link_done(int idx)
  195. {
  196. dprintf(-dcc[idx].sock, STR("neg.\n"));
  197. }
  198. int link_find_by_type(int type)
  199. {
  200. int i = 0;
  201. for (i = 0; enclink[i].name; i++)
  202. if (type == enclink[i].type)
  203. return i;
  204. return -1;
  205. }
  206. void link_link(int idx, int type, int i, direction_t direction)
  207. {
  208. if (i == -1 && type != -1) {
  209. for (i = 0; enclink[i].name; i++) {
  210. if (enclink[i].type == type)
  211. break;
  212. }
  213. }
  214. if (i != -1 && enclink[i].link)
  215. (enclink[i].link) (idx, direction);
  216. else if (direction == TO) /* problem finding function, just assume we're done */
  217. link_done(idx);
  218. return;
  219. }
  220. int link_read(int snum, char *buf, size_t *len)
  221. {
  222. int i = socklist[snum].enclink;
  223. if (i != -1 && enclink[i].read)
  224. return (enclink[i].read) (snum, buf, len);
  225. return -1;
  226. }
  227. const char *link_write(int snum, const char *buf, size_t *len)
  228. {
  229. int i = socklist[snum].enclink;
  230. if (i != -1 && enclink[i].write)
  231. return ((enclink[i].write) (snum, buf, len));
  232. return buf;
  233. }
  234. void link_hash(int idx, char *rand)
  235. {
  236. char hash[60] = "";
  237. /* nothing fancy, just something simple that can stop people from playing */
  238. simple_snprintf(hash, sizeof(hash), STR("enclink%s"), rand);
  239. strlcpy(dcc[idx].shahash, SHA1(hash), SHA_HASH_LENGTH + 1);
  240. SHA1(NULL);
  241. OPENSSL_cleanse(hash, sizeof(hash));
  242. return;
  243. }
  244. void link_parse(int idx, char *buf)
  245. {
  246. int snum = findanysnum(dcc[idx].sock);
  247. int i = socklist[snum].enclink;
  248. if (i >= 0 && enclink[i].parse)
  249. (enclink[i].parse) (idx, snum, buf);
  250. return;
  251. }
  252. void link_get_method(int idx)
  253. {
  254. if (!dcc[idx].type)
  255. return;
  256. int n = dcc[idx].u.enc->method_number;
  257. if (enclink[n].name)
  258. dcc[idx].u.enc->method = &(enclink[n]);
  259. dcc[idx].u.enc->method_number++;
  260. }
  261. /* the order of entries here determines which will be picked */
  262. struct enc_link enclink[] = {
  263. { "ghost+case3", LINK_GHOSTCASE3, ghost_link_case, ghost_write, ghost_read, ghost_parse },
  264. { "cleartext", LINK_CLEARTEXT, NULL, NULL, NULL, NULL },
  265. { NULL, 0, NULL, NULL, NULL, NULL }
  266. };