tclmisc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #include <sys/stat.h>
  2. #include "main.h"
  3. #include "modules.h"
  4. #include "tandem.h"
  5. #include "md5/md5.h"
  6. #ifdef HAVE_UNAME
  7. #include <sys/utsname.h>
  8. #endif
  9. extern p_tcl_bind_list bind_table_list;
  10. extern tcl_timer_t *timer, *utimer;
  11. extern struct dcc_t *dcc;
  12. extern char origbotname[], botnetnick[], quit_msg[];
  13. extern struct userrec *userlist;
  14. extern time_t now;
  15. extern module_entry *module_list;
  16. extern int max_logs, timesync;
  17. extern log_t *logs;
  18. extern Tcl_Interp *interp;
  19. extern char myipv6host[120];
  20. static int tcl_myip6 STDVAR
  21. {
  22. char s[120];
  23. getmyip ();
  24. BADARGS (1, 1, "");
  25. s[0] = 0;
  26. if (strlen (myipv6host) < 120)
  27. strcpy (s, myipv6host);
  28. Tcl_AppendResult (irp, s, NULL);
  29. return TCL_OK;
  30. }
  31. int
  32. expmem_tclmisc ()
  33. {
  34. int i, tot = 0;
  35. for (i = 0; i < max_logs; i++)
  36. if (logs[i].filename != NULL)
  37. {
  38. tot += strlen (logs[i].filename) + 1;
  39. tot += strlen (logs[i].chname) + 1;
  40. }
  41. return tot;
  42. }
  43. #ifdef HUB
  44. static int tcl_logfile STDVAR
  45. {
  46. int i;
  47. char s[151];
  48. BADARGS (1, 4, " ?logModes channel logFile?");
  49. if (argc == 1)
  50. {
  51. for (i = 0; i < max_logs; i++)
  52. if (logs[i].filename != NULL)
  53. {
  54. strcpy (s, masktype (logs[i].mask));
  55. strcat (s, " ");
  56. strcat (s, logs[i].chname);
  57. strcat (s, " ");
  58. strcat (s, logs[i].filename);
  59. Tcl_AppendElement (interp, s);
  60. }
  61. return TCL_OK;
  62. }
  63. BADARGS (4, 4, " ?logModes channel logFile?");
  64. for (i = 0; i < max_logs; i++)
  65. if ((logs[i].filename != NULL) && (!strcmp (logs[i].filename, argv[3])))
  66. {
  67. logs[i].flags &= ~LF_EXPIRING;
  68. logs[i].mask = logmodes (argv[1]);
  69. nfree (logs[i].chname);
  70. logs[i].chname = NULL;
  71. if (!logs[i].mask)
  72. {
  73. nfree (logs[i].filename);
  74. logs[i].filename = NULL;
  75. if (logs[i].f != NULL)
  76. {
  77. fclose (logs[i].f);
  78. logs[i].f = NULL;
  79. }
  80. logs[i].flags = 0;
  81. }
  82. else
  83. {
  84. logs[i].chname = (char *) nmalloc (strlen (argv[2]) + 1);
  85. strcpy (logs[i].chname, argv[2]);
  86. } Tcl_AppendResult (interp, argv[3], NULL);
  87. return TCL_OK;
  88. }
  89. if (!logmodes (argv[1]))
  90. {
  91. Tcl_AppendResult (interp, "can't remove \"", argv[3],
  92. "\" from list: no such logfile", NULL);
  93. return TCL_ERROR;
  94. }
  95. for (i = 0; i < max_logs; i++)
  96. if (logs[i].filename == NULL)
  97. {
  98. logs[i].flags = 0;
  99. logs[i].mask = logmodes (argv[1]);
  100. logs[i].filename = (char *) nmalloc (strlen (argv[3]) + 1);
  101. strcpy (logs[i].filename, argv[3]);
  102. logs[i].chname = (char *) nmalloc (strlen (argv[2]) + 1);
  103. strcpy (logs[i].chname, argv[2]);
  104. Tcl_AppendResult (interp, argv[3], NULL);
  105. return TCL_OK;
  106. }
  107. Tcl_AppendResult (interp, "reached max # of logfiles", NULL);
  108. return TCL_ERROR;
  109. }
  110. #endif
  111. static int tcl_putlog STDVAR
  112. {
  113. char logtext[501];
  114. BADARGS (2, 2, " text");
  115. strncpyz (logtext, argv[1], sizeof logtext);
  116. putlog (LOG_MISC, "*", "%s", logtext);
  117. return TCL_OK;
  118. }
  119. static int tcl_putlogl STDVAR
  120. {
  121. char logtext[501];
  122. BADARGS (2, 2, " text");
  123. strncpyz (logtext, argv[1], sizeof logtext);
  124. putlog (LOG_MISC, "@", "%s", logtext);
  125. return TCL_OK;
  126. }
  127. static int tcl_putcmdlog STDVAR
  128. {
  129. char logtext[501];
  130. BADARGS (2, 2, " text");
  131. strncpyz (logtext, argv[1], sizeof logtext);
  132. putlog (LOG_CMDS, "*", "%s", logtext);
  133. return TCL_OK;
  134. }
  135. static int tcl_putcmdlogl STDVAR
  136. {
  137. char logtext[501];
  138. BADARGS (2, 2, " text");
  139. strncpyz (logtext, argv[1], sizeof logtext);
  140. putlog (LOG_CMDS, "@", "%s", logtext);
  141. return TCL_OK;
  142. }
  143. static int tcl_putxferlog STDVAR
  144. {
  145. char logtext[501];
  146. BADARGS (2, 2, " text");
  147. strncpyz (logtext, argv[1], sizeof logtext);
  148. putlog (LOG_FILES, "*", "%s", logtext);
  149. return TCL_OK;
  150. }
  151. static int tcl_putloglev STDVAR
  152. {
  153. int lev = 0;
  154. char logtext[501];
  155. BADARGS (4, 4, " level channel text");
  156. lev = logmodes (argv[1]);
  157. if (!lev)
  158. {
  159. Tcl_AppendResult (irp, "No valid log-level given", NULL);
  160. return TCL_ERROR;
  161. }
  162. strncpyz (logtext, argv[3], sizeof logtext);
  163. putlog (lev, argv[2], "%s", logtext);
  164. return TCL_OK;
  165. }
  166. static int tcl_binds STDVAR
  167. {
  168. tcl_bind_list_t *tl, *tl_kind;
  169. tcl_bind_mask_t *tm;
  170. tcl_cmd_t *tc;
  171. char *g, flg[100], hits[11];
  172. int matching = 0;
  173. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  174. CONST char *list[5];
  175. #else
  176. char *list[5];
  177. #endif
  178. BADARGS (1, 2, " ?type/mask?");
  179. if (argv[1])
  180. tl_kind = find_bind_table (argv[1]);
  181. else
  182. tl_kind = NULL;
  183. if (!tl_kind && argv[1])
  184. matching = 1;
  185. for (tl = tl_kind ? tl_kind : bind_table_list; tl;
  186. tl = tl_kind ? 0 : tl->next)
  187. {
  188. if (tl->flags & HT_DELETED)
  189. continue;
  190. for (tm = tl->first; tm; tm = tm->next)
  191. {
  192. if (tm->flags & TBM_DELETED)
  193. continue;
  194. for (tc = tm->first; tc; tc = tc->next)
  195. {
  196. if (tc->attributes & TC_DELETED)
  197. continue;
  198. if (matching && !wild_match_per (argv[1], tl->name)
  199. && !wild_match_per (argv[1], tm->mask)
  200. && !wild_match_per (argv[1], tc->func_name))
  201. continue;
  202. build_flags (flg, &(tc->flags), NULL);
  203. egg_snprintf (hits, sizeof hits, "%i", (int) tc->hits);
  204. list[0] = tl->name;
  205. list[1] = flg;
  206. list[2] = tm->mask;
  207. list[3] = hits;
  208. list[4] = tc->func_name;
  209. g = Tcl_Merge (5, list);
  210. Tcl_AppendElement (irp, g);
  211. Tcl_Free ((char *) g);
  212. }}} return TCL_OK;
  213. }
  214. static int tcl_timer STDVAR
  215. {
  216. unsigned long x;
  217. char s[16];
  218. BADARGS (3, 3, " minutes command");
  219. if (atoi (argv[1]) < 0)
  220. {
  221. Tcl_AppendResult (irp, "time value must be positive", NULL);
  222. return TCL_ERROR;
  223. }
  224. if (argv[2][0] != '#')
  225. {
  226. x = add_timer (&timer, atoi (argv[1]), argv[2], 0L);
  227. egg_snprintf (s, sizeof s, "timer%lu", x);
  228. Tcl_AppendResult (irp, s, NULL);
  229. }
  230. return TCL_OK;
  231. }
  232. static int tcl_ischanhub STDVAR
  233. {
  234. #ifdef HUB
  235. Tcl_AppendResult (irp, "0", NULL);
  236. return TCL_OK;
  237. #endif
  238. if (ischanhub ())
  239. Tcl_AppendResult (irp, "1", NULL);
  240. else
  241. Tcl_AppendResult (irp, "0", NULL);
  242. return TCL_OK;
  243. }
  244. static int tcl_issechub STDVAR
  245. {
  246. #ifdef HUB
  247. Tcl_AppendResult (irp, "0", NULL);
  248. return TCL_OK;
  249. #endif
  250. if (issechub ())
  251. Tcl_AppendResult (irp, "1", NULL);
  252. else
  253. Tcl_AppendResult (irp, "0", NULL);
  254. return TCL_OK;
  255. }
  256. static int tcl_utimer STDVAR
  257. {
  258. unsigned long x;
  259. char s[16];
  260. BADARGS (3, 3, " seconds command");
  261. if (atoi (argv[1]) < 0)
  262. {
  263. Tcl_AppendResult (irp, "time value must be positive", NULL);
  264. return TCL_ERROR;
  265. }
  266. if (argv[2][0] != '#')
  267. {
  268. x = add_timer (&utimer, atoi (argv[1]), argv[2], 0L);
  269. egg_snprintf (s, sizeof s, "timer%lu", x);
  270. Tcl_AppendResult (irp, s, NULL);
  271. }
  272. return TCL_OK;
  273. }
  274. static int tcl_killtimer STDVAR
  275. {
  276. BADARGS (2, 2, " timerID");
  277. if (strncmp (argv[1], "timer", 5))
  278. {
  279. Tcl_AppendResult (irp, "argument is not a timerID", NULL);
  280. return TCL_ERROR;
  281. }
  282. if (remove_timer (&timer, atol (&argv[1][5])))
  283. return TCL_OK;
  284. Tcl_AppendResult (irp, "invalid timerID", NULL);
  285. return TCL_ERROR;
  286. }
  287. static int tcl_killutimer STDVAR
  288. {
  289. BADARGS (2, 2, " timerID");
  290. if (strncmp (argv[1], "timer", 5))
  291. {
  292. Tcl_AppendResult (irp, "argument is not a timerID", NULL);
  293. return TCL_ERROR;
  294. }
  295. if (remove_timer (&utimer, atol (&argv[1][5])))
  296. return TCL_OK;
  297. Tcl_AppendResult (irp, "invalid timerID", NULL);
  298. return TCL_ERROR;
  299. }
  300. static int tcl_timers STDVAR
  301. {
  302. BADARGS (1, 1, "");
  303. list_timers (irp, timer);
  304. return TCL_OK;
  305. }
  306. static int tcl_utimers STDVAR
  307. {
  308. BADARGS (1, 1, "");
  309. list_timers (irp, utimer);
  310. return TCL_OK;
  311. }
  312. static int tcl_duration STDVAR
  313. {
  314. char s[70];
  315. unsigned long sec, tmp;
  316. BADARGS (2, 2, " seconds");
  317. if (atol (argv[1]) <= 0)
  318. {
  319. Tcl_AppendResult (irp, "0 seconds", NULL);
  320. return TCL_OK;
  321. }
  322. sec = atol (argv[1]);
  323. s[0] = 0;
  324. if (sec >= 31536000)
  325. {
  326. tmp = (sec / 31536000);
  327. sprintf (s, "%lu year%s ", tmp, (tmp == 1) ? "" : "s");
  328. sec -= (tmp * 31536000);
  329. }
  330. if (sec >= 604800)
  331. {
  332. tmp = (sec / 604800);
  333. sprintf (&s[strlen (s)], "%lu week%s ", tmp, (tmp == 1) ? "" : "s");
  334. sec -= (tmp * 604800);
  335. }
  336. if (sec >= 86400)
  337. {
  338. tmp = (sec / 86400);
  339. sprintf (&s[strlen (s)], "%lu day%s ", tmp, (tmp == 1) ? "" : "s");
  340. sec -= (tmp * 86400);
  341. }
  342. if (sec >= 3600)
  343. {
  344. tmp = (sec / 3600);
  345. sprintf (&s[strlen (s)], "%lu hour%s ", tmp, (tmp == 1) ? "" : "s");
  346. sec -= (tmp * 3600);
  347. }
  348. if (sec >= 60)
  349. {
  350. tmp = (sec / 60);
  351. sprintf (&s[strlen (s)], "%lu minute%s ", tmp, (tmp == 1) ? "" : "s");
  352. sec -= (tmp * 60);
  353. }
  354. if (sec > 0)
  355. {
  356. tmp = (sec);
  357. sprintf (&s[strlen (s)], "%lu second%s", tmp, (tmp == 1) ? "" : "s");
  358. }
  359. Tcl_AppendResult (irp, s, NULL);
  360. return TCL_OK;
  361. }
  362. static int tcl_unixtime STDVAR
  363. {
  364. char s[11];
  365. BADARGS (1, 1, "");
  366. egg_snprintf (s, sizeof s, "%lu", (unsigned long) now);
  367. Tcl_AppendResult (irp, s, NULL);
  368. return TCL_OK;
  369. }
  370. static int tcl_ctime STDVAR
  371. {
  372. time_t tt;
  373. char s[25];
  374. BADARGS (2, 2, " unixtime");
  375. tt = (time_t) atol (argv[1]);
  376. strncpyz (s, ctime (&tt), sizeof s);
  377. Tcl_AppendResult (irp, s, NULL);
  378. return TCL_OK;
  379. }
  380. static int tcl_strftime STDVAR
  381. {
  382. char buf[512];
  383. struct tm *tm1;
  384. time_t t;
  385. BADARGS (2, 3, " format ?time?");
  386. if (argc == 3)
  387. t = atol (argv[2]);
  388. else
  389. t = now;
  390. tm1 = localtime (&t);
  391. if (egg_strftime (buf, sizeof (buf) - 1, argv[1], tm1))
  392. {
  393. Tcl_AppendResult (irp, buf, NULL);
  394. return TCL_OK;
  395. }
  396. Tcl_AppendResult (irp, " error with strftime", NULL);
  397. return TCL_ERROR;
  398. }
  399. static int tcl_myip STDVAR
  400. {
  401. char s[16];
  402. BADARGS (1, 1, "");
  403. egg_snprintf (s, sizeof s, "%lu", iptolong (getmyip ()));
  404. Tcl_AppendResult (irp, s, NULL);
  405. return TCL_OK;
  406. }
  407. static int tcl_rand STDVAR
  408. {
  409. unsigned long x;
  410. char s[11];
  411. BADARGS (2, 2, " limit");
  412. if (atol (argv[1]) <= 0)
  413. {
  414. Tcl_AppendResult (irp, "random limit must be greater than zero", NULL);
  415. return TCL_ERROR;
  416. }
  417. x = random () % (unsigned long) (atol (argv[1]));
  418. egg_snprintf (s, sizeof s, "%lu", x);
  419. Tcl_AppendResult (irp, s, NULL);
  420. return TCL_OK;
  421. }
  422. static int tcl_sendnote STDVAR
  423. {
  424. char s[5], from[NOTENAMELEN + 1], to[NOTENAMELEN + 1], msg[451];
  425. BADARGS (4, 4, " from to message");
  426. strncpyz (from, argv[1], sizeof from);
  427. strncpyz (to, argv[2], sizeof to);
  428. strncpyz (msg, argv[3], sizeof msg);
  429. egg_snprintf (s, sizeof s, "%d", add_note (to, from, msg, -1, 0));
  430. Tcl_AppendResult (irp, s, NULL);
  431. return TCL_OK;
  432. }
  433. #ifdef HUB
  434. static int tcl_backup STDVAR
  435. {
  436. BADARGS (1, 1, "");
  437. call_hook (HOOK_BACKUP);
  438. return TCL_OK;
  439. }
  440. #endif
  441. static int tcl_timesync STDVAR
  442. {
  443. char buf[20];
  444. sprintf (buf, "%li %li %li", (now + timesync), now, timesync);
  445. Tcl_AppendResult (irp, buf, NULL);
  446. return TCL_OK;
  447. }
  448. static int tcl_die STDVAR
  449. {
  450. char s[1024];
  451. BADARGS (1, 2, " ?reason?");
  452. if (argc == 2)
  453. {
  454. egg_snprintf (s, sizeof s, "BOT SHUTDOWN (%s)", argv[1]);
  455. strncpyz (quit_msg, argv[1], 1024);
  456. }
  457. else
  458. {
  459. strncpyz (s, "BOT SHUTDOWN (No reason)", sizeof s);
  460. quit_msg[0] = 0;
  461. }
  462. kill_bot (s, quit_msg[0] ? quit_msg : "EXIT");
  463. return TCL_OK;
  464. }
  465. static int tcl_loadmodule STDVAR
  466. {
  467. const char *p;
  468. BADARGS (2, 2, " module-name");
  469. p = module_load (argv[1]);
  470. if (p && strcmp (p, MOD_ALREADYLOAD) && !strcmp (argv[0], "loadmodule"))
  471. putlog (LOG_MISC, "*", "%s %s: %s", MOD_CANTLOADMOD, argv[1], p);
  472. Tcl_AppendResult (irp, p, NULL);
  473. return TCL_OK;
  474. }
  475. static int tcl_unloadmodule STDVAR
  476. {
  477. BADARGS (2, 2, " module-name");
  478. Tcl_AppendResult (irp, module_unload (argv[1], botnetnick), NULL);
  479. return TCL_OK;
  480. }
  481. static int tcl_unames STDVAR
  482. {
  483. char *unix_n, *vers_n;
  484. #ifdef HAVE_UNAME
  485. struct utsname un;
  486. if (uname (&un) < 0)
  487. {
  488. #endif
  489. unix_n = "*unkown*";
  490. vers_n = "";
  491. #ifdef HAVE_UNAME
  492. }
  493. else
  494. {
  495. unix_n = un.sysname;
  496. vers_n = un.release;
  497. }
  498. #endif
  499. Tcl_AppendResult (irp, unix_n, " ", vers_n, NULL);
  500. return TCL_OK;
  501. }
  502. static int tcl_callevent STDVAR
  503. {
  504. BADARGS (2, 2, " event");
  505. check_tcl_event (argv[1]);
  506. return TCL_OK;
  507. }
  508. #if (TCL_MAJOR_VERSION >= 8)
  509. static int
  510. tcl_md5 (cd, irp, objc, objv)
  511. ClientData cd;
  512. Tcl_Interp *irp;
  513. int objc;
  514. Tcl_Obj *CONST objv[];
  515. {
  516. #else
  517. static int tcl_md5 STDVAR
  518. {
  519. #endif
  520. MD5_CTX md5context;
  521. char digest_string[33], *string;
  522. unsigned char digest[16];
  523. int i, len;
  524. #if (TCL_MAJOR_VERSION >= 8)
  525. if (objc != 2)
  526. {
  527. Tcl_WrongNumArgs (irp, 1, objv, "string");
  528. return TCL_ERROR;
  529. }
  530. #if (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION < 1)
  531. string = Tcl_GetStringFromObj (objv[1], &len);
  532. #else
  533. string = Tcl_GetByteArrayFromObj (objv[1], &len);
  534. #endif
  535. #else
  536. BADARGS (2, 2, " string");
  537. string = argv[1];
  538. len = strlen (argv[1]);
  539. #endif
  540. MD5_Init (&md5context);
  541. MD5_Update (&md5context, (unsigned char *) string, len);
  542. MD5_Final (digest, &md5context);
  543. for (i = 0; i < 16; i++)
  544. sprintf (digest_string + (i * 2), "%.2x", digest[i]);
  545. Tcl_AppendResult (irp, digest_string, NULL);
  546. return TCL_OK;
  547. }
  548. tcl_cmds tclmisc_objcmds[] = {
  549. #if (TCL_MAJOR_VERSION >= 8)
  550. {"md5", tcl_md5}
  551. ,
  552. #endif
  553. {NULL, NULL}
  554. };
  555. tcl_cmds tclmisc_cmds[] = {
  556. #ifdef HUB
  557. {"logfile", tcl_logfile}
  558. ,
  559. #endif
  560. {"putlog", tcl_putlog}
  561. , {"putlogl", tcl_putlogl}
  562. , {"putcmdlog", tcl_putcmdlog}
  563. , {"putcmdlogl", tcl_putcmdlogl}
  564. , {"putxferlog", tcl_putxferlog}
  565. , {"putloglev", tcl_putloglev}
  566. , {"timer", tcl_timer}
  567. , {"utimer", tcl_utimer}
  568. , {"killtimer", tcl_killtimer}
  569. , {"killutimer", tcl_killutimer}
  570. , {"timers", tcl_timers}
  571. , {"utimers", tcl_utimers}
  572. , {"unixtime", tcl_unixtime}
  573. , {"strftime", tcl_strftime}
  574. , {"ctime", tcl_ctime}
  575. , {"myip", tcl_myip}
  576. , {"myip6", tcl_myip6}
  577. , {"rand", tcl_rand}
  578. , {"sendnote", tcl_sendnote}
  579. ,
  580. #ifdef HUB
  581. {"backup", tcl_backup}
  582. ,
  583. #endif
  584. {"ischanhub", tcl_ischanhub}
  585. , {"issechub", tcl_issechub}
  586. , {"timesync", tcl_timesync}
  587. , {"exit", tcl_die}
  588. , {"die", tcl_die}
  589. , {"unames", tcl_unames}
  590. , {"unloadmodule", tcl_unloadmodule}
  591. , {"loadmodule", tcl_loadmodule}
  592. , {"checkmodule", tcl_loadmodule}
  593. , {"duration", tcl_duration}
  594. ,
  595. #if (TCL_MAJOR_VERSION < 8)
  596. {"md5", tcl_md5}
  597. ,
  598. #endif
  599. {"binds", tcl_binds}
  600. , {"callevent", tcl_callevent}
  601. , {NULL, NULL}
  602. };