enclink.c 9.0 KB

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