shell.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 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. /*
  21. * shell.c -- handles:
  22. *
  23. * All shell related functions
  24. * -shell_exec()
  25. * -botconfig parsing
  26. * -check_*()
  27. * -crontab functions
  28. *
  29. */
  30. #include "common.h"
  31. #include "shell.h"
  32. #include "chanprog.h"
  33. #include "set.h"
  34. #include "settings.h"
  35. #include "userrec.h"
  36. #include "net.h"
  37. #include "flags.h"
  38. #include "tandem.h"
  39. #include "main.h"
  40. #include "dccutil.h"
  41. #include "misc.h"
  42. #include "misc_file.h"
  43. #include "bg.h"
  44. #include "stat.h"
  45. #include "users.h"
  46. #include "botnet.h"
  47. #include "src/mod/server.mod/server.h"
  48. #include <bdlib/src/String.h>
  49. #include <bdlib/src/Stream.h>
  50. #include <sys/types.h>
  51. #include <pwd.h>
  52. #include <signal.h>
  53. #ifdef HAVE_SYS_PRCTL_H
  54. #include <sys/prctl.h>
  55. #endif
  56. #ifdef HAVE_SYS_PTRACE_H
  57. # include <sys/ptrace.h>
  58. #endif /* HAVE_SYS_PTRACE_H */
  59. #include <sys/wait.h>
  60. #include <sys/utsname.h>
  61. #include <pwd.h>
  62. #include <errno.h>
  63. #include <net/if.h>
  64. #include <sys/ioctl.h>
  65. #include <sys/socket.h>
  66. #include <ctype.h>
  67. #include <fcntl.h>
  68. #include <sys/stat.h>
  69. #include <unistd.h>
  70. #include <dirent.h>
  71. #include "botmsg.h"
  72. bool clear_tmpdir = 0;
  73. void clear_tmp()
  74. {
  75. if (!clear_tmpdir)
  76. return;
  77. DIR *tmp = NULL;
  78. if (!(tmp = opendir(tempdir)))
  79. return;
  80. struct dirent *dir_ent = NULL;
  81. char *file = NULL;
  82. size_t flen = 0;
  83. while ((dir_ent = readdir(tmp))) {
  84. if (strncmp(dir_ent->d_name, ".pid.", 4) &&
  85. strncmp(dir_ent->d_name, ".u", 2) &&
  86. strcmp(dir_ent->d_name, ".bin.old") &&
  87. strncmp(dir_ent->d_name, STR(".socks-"), 7) &&
  88. strcmp(dir_ent->d_name, ".") &&
  89. strcmp(dir_ent->d_name, "..")) {
  90. flen = strlen(dir_ent->d_name) + strlen(tempdir) + 1;
  91. file = (char *) my_calloc(1, flen);
  92. strlcat(file, tempdir, flen);
  93. strlcat(file, dir_ent->d_name, flen);
  94. file[strlen(file)] = 0;
  95. sdprintf("clear_tmp: %s", file);
  96. unlink(file);
  97. free(file);
  98. }
  99. }
  100. closedir(tmp);
  101. return;
  102. }
  103. void check_maxfiles()
  104. {
  105. int sock = -1, sock1 = -1 , bogus = 0, failed_close = 0;
  106. #ifdef USE_IPV6
  107. sock1 = getsock(0, AF_INET); /* fill up any lower avail */
  108. sock = getsock(0, AF_INET);
  109. #else
  110. sock1 = getsock(0);
  111. sock = getsock(0);
  112. #endif
  113. if (sock1 != -1)
  114. killsock(sock1);
  115. if (sock == -1) {
  116. return;
  117. } else
  118. killsock(sock);
  119. bogus = sock - socks_total - 4; //4 for stdin/stdout/stderr/dns
  120. if (unlikely(bogus >= 50)) { /* Attempt to close them */
  121. sdprintf("SOCK: %d BOGUS: %d SOCKS_TOTAL: %d", sock, bogus, socks_total);
  122. for (int i = 10; i < sock; i++) /* dont close lower sockets, they're probably legit */
  123. if (!findanysnum(i)) {
  124. if ((close(i)) == -1) /* try to close the BOGUS fd (likely a KQUEUE) */
  125. failed_close++;
  126. else
  127. bogus--;
  128. }
  129. if (bogus >= 150 || failed_close >= 50) {
  130. if (tands > 0) {
  131. botnet_send_chat(-1, conf.bot->nick, "Max FD reached, restarting...");
  132. botnet_send_bye("Max FD reached, restarting...");
  133. }
  134. nuke_server("brb");
  135. cycle_time = 0;
  136. restart(-1);
  137. } else if (bogus >= 100 && (bogus % 10) == 0) {
  138. putlog(LOG_WARN, "*", "* WARNING: $b%d$b bogus file descriptors detected, auto restart at 150", bogus);
  139. }
  140. }
  141. }
  142. void check_mypid()
  143. {
  144. pid_t pid = 0;
  145. if (can_stat(conf.bot->pid_file))
  146. pid = checkpid(conf.bot->nick, NULL);
  147. if (pid && (pid != getpid()))
  148. fatal(STR("getpid() does not match pid in file. Possible cloned process, exiting.."), 0);
  149. }
  150. char last_buf[128] = "";
  151. void check_last() {
  152. if (login == DET_IGNORE)
  153. return;
  154. if (conf.username) {
  155. char *out = NULL, buf[50] = "";
  156. simple_snprintf(buf, sizeof(buf), STR("last -10 %s"), conf.username);
  157. if (shell_exec(buf, NULL, &out, NULL, 1)) {
  158. if (out) {
  159. char *p = NULL;
  160. p = strchr(out, '\n');
  161. if (p)
  162. *p = 0;
  163. if (strlen(out) > 10) {
  164. if (last_buf[0]) {
  165. if (strncmp(last_buf, out, sizeof(last_buf))) {
  166. char *work = NULL;
  167. size_t siz = strlen(out) + 7 + 2 + 1;
  168. work = (char *) my_calloc(1, siz);
  169. simple_snprintf(work, siz, STR("Login: %s"), out);
  170. detected(DETECT_LOGIN, work);
  171. free(work);
  172. }
  173. }
  174. strlcpy(last_buf, out, sizeof(last_buf));
  175. }
  176. free(out);
  177. }
  178. }
  179. }
  180. }
  181. void check_promisc()
  182. {
  183. #ifdef SIOCGIFCONF
  184. if (promisc == DET_IGNORE)
  185. return;
  186. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  187. if (sock < 0)
  188. return;
  189. struct ifconf ifcnf;
  190. char buf[1024] = "";
  191. ifcnf.ifc_len = sizeof(buf);
  192. ifcnf.ifc_buf = buf;
  193. if (ioctl(sock, SIOCGIFCONF, &ifcnf) < 0) {
  194. close(sock);
  195. return;
  196. }
  197. char *reqp = NULL, *end_req = NULL;
  198. reqp = buf; /* pointer to start of array */
  199. end_req = buf + ifcnf.ifc_len; /* pointer to end of array */
  200. while (reqp < end_req) {
  201. struct ifreq ifreq, *ifr = NULL;
  202. ifr = reinterpret_cast<struct ifreq *>(reqp); /* start examining interface */
  203. ifreq = *ifr;
  204. if (!ioctl(sock, SIOCGIFFLAGS, &ifreq)) { /* we can read this interface! */
  205. /* sdprintf("Examing interface: %s", ifr->ifr_name); */
  206. if (unlikely(ifreq.ifr_flags & IFF_PROMISC) && strncmp(ifr->ifr_name, "pflog", 5) && strncmp(ifr->ifr_name, "ipfw", 4)) {
  207. char which[101] = "";
  208. simple_snprintf(which, sizeof(which), STR("Detected promiscuous mode on interface: %s"), ifr->ifr_name);
  209. ioctl(sock, SIOCSIFFLAGS, &ifreq); /* set flags */
  210. detected(DETECT_PROMISC, which);
  211. break;
  212. }
  213. }
  214. /* move pointer to next array element (next interface) */
  215. reqp += sizeof(ifr->ifr_name) + sizeof(ifr->ifr_addr);
  216. }
  217. close(sock);
  218. #endif /* SIOCGIFCONF */
  219. }
  220. bool traced = 0;
  221. static void got_sigtrap(int z)
  222. {
  223. traced = 0;
  224. }
  225. void check_trace(int start)
  226. {
  227. if (trace == DET_IGNORE || trace == DET_WARN)
  228. trace = DET_DIE;
  229. // return;
  230. #ifdef DEBUG
  231. trace = DET_IGNORE;
  232. #endif /* DEBUG */
  233. if (trace == DET_IGNORE)
  234. return;
  235. pid_t parent = getpid();
  236. /* we send ourselves a SIGTRAP, if we recieve, we're not being traced, otherwise we are. */
  237. signal(SIGTRAP, got_sigtrap);
  238. traced = 1;
  239. raise(SIGTRAP);
  240. /* no longer need this__asm__("INT3"); //SIGTRAP */
  241. signal(SIGTRAP, SIG_DFL);
  242. if (!traced) {
  243. signal(SIGINT, got_sigtrap);
  244. traced = 1;
  245. raise(SIGINT);
  246. signal(SIGINT, SIG_DFL);
  247. }
  248. if (traced) {
  249. if (start) {
  250. kill(parent, SIGKILL);
  251. exit(1);
  252. } else
  253. detected(DETECT_TRACE, STR("I'm being traced!"));
  254. } else {
  255. if (!start)
  256. return;
  257. #ifndef __sun__
  258. int x, i;
  259. /* now, let's attempt to ptrace ourself */
  260. switch ((x = fork())) {
  261. case -1:
  262. return;
  263. case 0: //child
  264. i = ptrace(PT_ATTACH, parent, 0, 0);
  265. /* EPERM is given on fbsd when security.bsd.unprivileged_proc_debug=0 */
  266. if (i == -1 && errno != EPERM && errno != EINVAL) {
  267. if (start) {
  268. kill(parent, SIGKILL);
  269. exit(1);
  270. } else
  271. detected(DETECT_TRACE, STR("I'm being traced!"));
  272. } else {
  273. waitpid(parent, NULL, 0);
  274. ptrace(PT_DETACH, parent, (char *) 1, 0);
  275. kill(parent, SIGCHLD);
  276. }
  277. exit(0);
  278. default: //parent
  279. waitpid(x, NULL, 0);
  280. }
  281. #endif
  282. }
  283. }
  284. int shell_exec(char *cmdline, char *input, char **output, char **erroutput, bool simple)
  285. {
  286. if (!cmdline)
  287. return 0;
  288. Tempfile *in = NULL, *out = NULL, *err = NULL;
  289. int x;
  290. int parent = getpid();
  291. /* Set up temp files */
  292. in = new Tempfile("in");
  293. if (!in || in->error) {
  294. // putlog(LOG_ERRORS, "*" , "exec: Couldn't open '%s': %s", in->file, strerror(errno));
  295. delete in;
  296. return 0;
  297. }
  298. if (input) {
  299. if (fwrite(input, strlen(input), 1, in->f) != 1) {
  300. // putlog(LOG_ERRORS, "*", "exec: Couldn't write to '%s': %s", in->file, strerror(errno));
  301. delete in;
  302. return 0;
  303. }
  304. fseek(in->f, 0, SEEK_SET);
  305. }
  306. err = new Tempfile("err");
  307. if (!err || err->error) {
  308. // putlog(LOG_ERRORS, "*", "exec: Couldn't open '%s': %s", err->file, strerror(errno));
  309. delete in;
  310. delete err;
  311. return 0;
  312. }
  313. out = new Tempfile("out");
  314. if (!out || out->error) {
  315. // putlog(LOG_ERRORS, "*", "exec: Couldn't open '%s': %s", out->file, strerror(errno));
  316. delete in;
  317. delete err;
  318. delete out;
  319. return 0;
  320. }
  321. x = fork();
  322. if (x == -1) {
  323. putlog(LOG_ERRORS, "*", "exec: fork() failed: %s", strerror(errno));
  324. delete in;
  325. delete err;
  326. delete out;
  327. return 0;
  328. }
  329. if (x) { /* Parent: wait for the child to complete */
  330. int st = 0;
  331. size_t fs = 0;
  332. #ifdef PR_SET_PTRACER
  333. // Allow the child to debug the parent on Ubuntu
  334. // https://wiki.ubuntu.com/SecurityTeam/Roadmap/KernelHardening#ptrace
  335. prctl(PR_SET_PTRACER, x, 0, 0, 0);
  336. #endif
  337. waitpid(x, &st, 0);
  338. /* child is now complete, read the files into buffers */
  339. delete in;
  340. fflush(out->f);
  341. fflush(err->f);
  342. if (erroutput) {
  343. char *buf = NULL;
  344. fseek(err->f, 0, SEEK_END);
  345. fs = ftell(err->f);
  346. if (fs == 0) {
  347. (*erroutput) = NULL;
  348. } else {
  349. buf = (char *) my_calloc(1, fs + 1);
  350. fseek(err->f, 0, SEEK_SET);
  351. if (!fread(buf, 1, fs, err->f))
  352. fs = 0;
  353. buf[fs] = 0;
  354. (*erroutput) = buf;
  355. }
  356. }
  357. delete err;
  358. if (output) {
  359. char *buf = NULL;
  360. fseek(out->f, 0, SEEK_END);
  361. fs = ftell(out->f);
  362. if (fs == 0) {
  363. (*output) = NULL;
  364. } else {
  365. buf = (char *) my_calloc(1, fs + 1);
  366. fseek(out->f, 0, SEEK_SET);
  367. if (!fread(buf, 1, fs, out->f))
  368. fs = 0;
  369. buf[fs] = 0;
  370. (*output) = buf;
  371. }
  372. }
  373. delete out;
  374. return 1;
  375. } else {
  376. /* Child: make fd's and set them up as std* */
  377. // int ind, outd, errd;
  378. // ind = fileno(inpFile);
  379. // outd = fileno(outFile);
  380. // errd = fileno(errFile);
  381. if (dup2(in->fd, STDIN_FILENO) == (-1)) {
  382. kill(parent, SIGCHLD);
  383. exit(1);
  384. }
  385. if (dup2(out->fd, STDOUT_FILENO) == (-1)) {
  386. kill(parent, SIGCHLD);
  387. exit(1);
  388. }
  389. if (dup2(err->fd, STDERR_FILENO) == (-1)) {
  390. kill(parent, SIGCHLD);
  391. exit(1);
  392. }
  393. // Close all sockets
  394. for (int fd = 3; fd < MAX_SOCKETS; ++fd) close(fd);
  395. char *argv[15];
  396. if (simple) {
  397. char *p = NULL;
  398. int n = 0;
  399. char *mycmdline = strdup(cmdline);
  400. while (mycmdline[0] && (p = newsplit(&mycmdline)))
  401. argv[n++] = p;
  402. argv[n] = NULL;
  403. } else {
  404. argv[0] = "/bin/sh";
  405. argv[1] = "-c";
  406. argv[2] = cmdline;
  407. argv[3] = NULL;
  408. }
  409. execvp(argv[0], &argv[0]);
  410. kill(parent, SIGCHLD);
  411. exit(1);
  412. }
  413. }
  414. int simple_exec(const char* argv[]) {
  415. pid_t pid, savedpid;
  416. int status;
  417. switch ((pid = fork())) {
  418. case -1:
  419. break;
  420. case 0: //child
  421. // Close all sockets
  422. for (int fd = 3; fd < MAX_SOCKETS; ++fd) close(fd);
  423. execvp(argv[0], (char* const*) &argv[0]);
  424. _exit(127);
  425. default: //parent
  426. savedpid = pid;
  427. do {
  428. pid = wait4(savedpid, &status, 0, (struct rusage *)0);
  429. } while (pid == -1 && errno == EINTR);
  430. break;
  431. }
  432. return(pid == -1 ? -1 : status);
  433. }
  434. void suicide(const char *msg)
  435. {
  436. char tmp[512] = "";
  437. if (!conf.bot->localhub) {
  438. //im not a localhub, ask the localhub to suicide
  439. simple_snprintf(tmp, sizeof(tmp), STR("suicide %s"), msg);
  440. putbot(conf.localhub, tmp);
  441. return;
  442. } else {
  443. //im the localhub, loop thru bots and kill 'em
  444. putlog(LOG_WARN, "*", STR("Comitting suicide: %s"), msg);
  445. crontab_del();
  446. conf_bot *bot = NULL;
  447. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  448. if (!strcmp(conf.bot->nick, bot->nick))
  449. continue; //skip myself or i wont be able to remove the rest
  450. bot->pid = checkpid(bot->nick, bot);
  451. conf_killbot(conf.bots, NULL, bot, SIGKILL);
  452. unlink(bot->pid_file);
  453. deluser(bot->nick);
  454. }
  455. }
  456. if (!conf.bot->hub) {
  457. nuke_server(STR("kill the infidels!"));
  458. sleep(1);
  459. } else {
  460. unlink(userfile);
  461. simple_snprintf(tmp, sizeof(tmp), STR("%s~new"), userfile);
  462. unlink(tmp);
  463. simple_snprintf(tmp, sizeof(tmp), STR("%s~"), userfile);
  464. unlink(tmp);
  465. simple_snprintf(tmp, sizeof(tmp), STR("%s/%s~"), conf.datadir, userfile);
  466. unlink(tmp);
  467. simple_snprintf(tmp, sizeof(tmp), STR("%s/.u.0"), conf.datadir);
  468. unlink(tmp);
  469. simple_snprintf(tmp, sizeof(tmp), STR("%s/.u.1"), conf.datadir);
  470. unlink(tmp);
  471. }
  472. unlink(binname);
  473. //Not recursively clearing these dirs as they may be ~USER/ ..
  474. unlink(conf.datadir); //Probably will fail, shrug
  475. unlink(tempdir); //Probably will fail too, oh well
  476. //now deal with myself after the rest of the conf.bots are gone
  477. deluser(conf.bot->nick);
  478. unlink(conf.bot->pid_file);
  479. //and die in agony!
  480. fatal(msg, 0);
  481. }
  482. void detected(int code, const char *msg)
  483. {
  484. char tmp[512] = "";
  485. struct flag_record fr = { FR_GLOBAL, 0, 0, 0 };
  486. int act = DET_WARN, do_fatal = 0, killbots = 0;
  487. if (code == DETECT_LOGIN)
  488. act = login;
  489. if (code == DETECT_TRACE)
  490. act = trace;
  491. if (code == DETECT_PROMISC)
  492. act = promisc;
  493. if (code == DETECT_HIJACK)
  494. act = hijack;
  495. switch (act) {
  496. case DET_IGNORE:
  497. break;
  498. case DET_WARN:
  499. putlog(LOG_WARN, "*", "%s", msg);
  500. break;
  501. case DET_REJECT:
  502. do_fork();
  503. putlog(LOG_WARN, "*", STR("Setting myself +d: %s"), msg);
  504. simple_snprintf(tmp, sizeof(tmp), "+d: %s", msg);
  505. set_user(&USERENTRY_COMMENT, conf.bot->u, tmp);
  506. fr.global = USER_DEOP;
  507. fr.bot = 1;
  508. set_user_flagrec(conf.bot->u, &fr, 0);
  509. sleep(1);
  510. break;
  511. case DET_DIE:
  512. putlog(LOG_WARN, "*", STR("Dying: %s"), msg);
  513. simple_snprintf(tmp, sizeof(tmp), STR("Dying: %s"), msg);
  514. set_user(&USERENTRY_COMMENT, conf.bot->u, tmp);
  515. if (!conf.bot->hub)
  516. nuke_server(STR("BBL"));
  517. sleep(1);
  518. killbots++;
  519. do_fatal++;
  520. break;
  521. case DET_SUICIDE:
  522. suicide(msg);
  523. break;
  524. }
  525. if (killbots && conf.bot->localhub) {
  526. conf_checkpids(conf.bots);
  527. conf_killbot(conf.bots, NULL, NULL, SIGKILL);
  528. }
  529. if (do_fatal)
  530. fatal(msg, 0);
  531. }
  532. const char *werr_tostr(int errnum)
  533. {
  534. switch (errnum) {
  535. case ERR_BINSTAT:
  536. return STR("Cannot access binary");
  537. case ERR_BADPASS:
  538. return STR("Incorrect password");
  539. case ERR_PASSWD:
  540. return STR("Cannot access the global passwd file");
  541. case ERR_WRONGBINDIR:
  542. return STR("Wrong directory/binary name");
  543. case ERR_DATADIR:
  544. return STR("Cannot access datadir.");
  545. case ERR_TMPSTAT:
  546. return STR("Cannot access tmp directory.");
  547. case ERR_TMPMOD:
  548. return STR("Cannot chmod() tmp directory.");
  549. case ERR_WRONGUID:
  550. return STR("UID in binary does not match geteuid()");
  551. case ERR_WRONGUNAME:
  552. return STR("Uname in binary does not match uname()");
  553. case ERR_BADCONF:
  554. return STR("Config file is incomplete");
  555. case ERR_BADBOT:
  556. return STR("No such botnick");
  557. case ERR_BOTDISABLED:
  558. return STR("Bot is disabled, remove '/' in config");
  559. case ERR_NOBOTS:
  560. return STR("There are no bots in the binary! Please use ./binary -C to edit");
  561. case ERR_NOHOMEDIR:
  562. return STR("There is no homedir set. Please set one in the binary config with ./binary -C");
  563. case ERR_NOUSERNAME:
  564. return STR("There is no username set. Please set one in the binary config with ./binary -C");
  565. case ERR_NOBOT:
  566. return STR("I have no bot record but received -B???");
  567. case ERR_NOTINIT:
  568. return STR("Binary data is not initialized; try ./binary -C");
  569. case ERR_TOOMANYBOTS:
  570. return STR("Too many bots defined. 5 max. Too many will lead to klines.\nSpread out into multiple accounts/shells/ip ranges.");
  571. case ERR_LIBS:
  572. return STR("Failed to load required libraries.\nEnsure that 32bit compat libs are installed, and openssl.\nLinux: ia32-libs\nFreeBSD: misc/compat8x");
  573. default:
  574. return STR("Unforseen error");
  575. }
  576. }
  577. void werr(int errnum)
  578. {
  579. /* [1]+ Done ls --color=auto -A -CF
  580. [1]+ Killed bash
  581. */
  582. /*
  583. int x = 0;
  584. unsigned long job = randint(2) + 1; */
  585. /* printf("[%lu] %lu%lu%lu%lu%lu\n", job, randint(2) + 1, randint(8) + 1, randint(8) + 1, errnum); */
  586. /* printf("\n[%lu]+ Killed rm -rf /\r\n", job); */
  587. /*
  588. if (checkedpass) {
  589. printf("[%lu] %d\n", job, getpid());
  590. }
  591. */
  592. /* printf("\n[%lu]+ Stopped %s\r\n", job, basename(binname)); */
  593. #ifdef OBSCURE_ERRORS
  594. sdprintf(STR("Error %d: %s"), errnum, werr_tostr(errnum));
  595. printf(STR("*** Error code %d\n\n"), errnum);
  596. printf(STR("Segmentation fault\n"));
  597. #else
  598. fprintf(stderr, STR("Error %d: %s\n"), errnum, werr_tostr(errnum));
  599. #endif
  600. fatal("", 0);
  601. exit(1); // This is never reached, done for gcc() warnings
  602. }
  603. char *homedir(bool useconf)
  604. {
  605. static char homedir_buf[PATH_MAX] = "";
  606. if (!homedir_buf[0]) {
  607. if (conf.homedir && useconf)
  608. simple_snprintf(homedir_buf, sizeof homedir_buf, "%s", conf.homedir);
  609. else {
  610. char *home = getenv("HOME");
  611. if (home && strlen(home))
  612. strlcpy(homedir_buf, home, sizeof(homedir_buf));
  613. }
  614. }
  615. return homedir_buf[0] ? homedir_buf : NULL;
  616. }
  617. char *my_username()
  618. {
  619. static char username[DIRMAX] = "";
  620. if (!username[0]) {
  621. char *user = getenv("USER");
  622. if (user && strlen(user))
  623. strlcpy(username, user, sizeof(username));
  624. }
  625. return username[0] ? username : NULL;
  626. }
  627. int mkdir_p(const char *dir) {
  628. char *p = NULL, *path = NULL;
  629. path = p = strdup(dir);
  630. do {
  631. p = strchr(p + 1, '/');
  632. if (p)
  633. *p = '\0';
  634. if (can_stat(path) && !is_dir(path))
  635. unlink(path);
  636. if (!can_stat(path)) {
  637. if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
  638. unlink(path);
  639. mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
  640. }
  641. }
  642. if (p)
  643. *p = '/';
  644. } while(p);
  645. int couldStat = can_stat(path);
  646. free(path);
  647. return couldStat;
  648. }
  649. void expand_tilde(char **ptr)
  650. {
  651. if (!conf.homedir || !conf.homedir[0])
  652. return;
  653. char *str = ptr ? *ptr : NULL;
  654. if (str && strchr(str, '~')) {
  655. char *p = NULL;
  656. size_t siz = strlen(str);
  657. if (str[siz - 1] == '/')
  658. str[siz - 1] = 0;
  659. if ((p = replace(str, "~", conf.homedir)))
  660. str_redup(ptr, p);
  661. else
  662. fatal("Unforseen error expanding '~'", 0);
  663. }
  664. }
  665. void check_crontab()
  666. {
  667. int i = 0;
  668. if (!conf.bot->hub && !conf.bot->localhub)
  669. fatal(STR("something is wrong."), 0);
  670. if (!(i = crontab_exists())) {
  671. crontab_create(5);
  672. if (!(i = crontab_exists()))
  673. printf(STR("* Error writing crontab entry.\n"));
  674. }
  675. }
  676. static void crontab_install(bd::Stream& crontab) {
  677. // Write out the new crontab
  678. Tempfile new_crontab = Tempfile("crontab");
  679. crontab.writeFile(new_crontab.fd);
  680. // Install new crontab
  681. const char* argv[] = {"crontab", new_crontab.file, 0};
  682. simple_exec(argv);
  683. }
  684. void crontab_del() {
  685. bd::Stream crontab;
  686. if (crontab_exists(&crontab, 1) == 1)
  687. crontab_install(crontab);
  688. }
  689. int crontab_exists(bd::Stream* crontab, bool excludeSelf) {
  690. char *out = NULL;
  691. int ret = -1;
  692. if (shell_exec("crontab -l", NULL, &out, NULL, 1)) {
  693. if (out) {
  694. bd::Stream stream(out);
  695. bd::String line;
  696. ret = 0;
  697. while (stream.tell() < stream.length()) {
  698. line = stream.getline();
  699. if (line[0] != '#' && line.find(binname) != bd::String::npos) {
  700. ret = 1;
  701. // Need to continue if the existing crontab is requested
  702. if (crontab && !excludeSelf)
  703. (*crontab) << line;
  704. else if (!crontab)
  705. break;
  706. } else if (crontab)
  707. (*crontab) << line;
  708. }
  709. free(out);
  710. } else
  711. ret = 0;
  712. }
  713. return ret;
  714. }
  715. char s1_2[3] = "",s1_8[3] = "",s2_5[3] = "";
  716. void crontab_create(int interval) {
  717. bd::Stream crontab;
  718. if (crontab_exists(&crontab) == 1)
  719. return;
  720. char buf[1024] = "";
  721. if (interval == 1)
  722. strlcpy(buf, "*", 2);
  723. else {
  724. int i = 1;
  725. int si = randint(interval);
  726. while (i < 60) {
  727. if (buf[0])
  728. strlcat(buf, ",", sizeof(buf));
  729. simple_snprintf(&buf[strlen(buf)], sizeof(buf) - strlen(buf), "%i", (i + si) % 60);
  730. i += interval;
  731. }
  732. }
  733. simple_snprintf(buf + strlen(buf), sizeof buf, STR(" * * * * %s > /dev/null 2>&1\n"), shell_escape(binname));
  734. crontab << buf;
  735. crontab_install(crontab);
  736. }
  737. int det_translate(const char *word)
  738. {
  739. if (word && word[0]) {
  740. if (!strcasecmp(word, STR("ignore")))
  741. return DET_IGNORE;
  742. else if (!strcasecmp(word, STR("warn")))
  743. return DET_WARN;
  744. else if (!strcasecmp(word, STR("reject")))
  745. return DET_REJECT;
  746. else if (!strcasecmp(word, STR("die")))
  747. return DET_DIE;
  748. else if (!strcasecmp(word, STR("suicide")))
  749. return DET_SUICIDE;
  750. }
  751. return DET_IGNORE;
  752. }
  753. const char *det_translate_num(int num)
  754. {
  755. switch (num) {
  756. case DET_IGNORE: return STR("ignore");
  757. case DET_WARN: return STR("warn");
  758. case DET_REJECT: return STR("reject");
  759. case DET_DIE: return STR("die");
  760. case DET_SUICIDE:return STR("suicide");
  761. default: return STR("ignore");
  762. }
  763. return STR("ignore");
  764. }
  765. char *shell_escape(const char *path)
  766. {
  767. static char ret1[DIRMAX] = "", ret2[DIRMAX] = "", *ret = NULL;
  768. static bool alt = 0;
  769. char *c = NULL;
  770. if (alt) {
  771. alt = 0;
  772. ret = ret1;
  773. } else {
  774. alt = 1;
  775. ret = ret2;
  776. }
  777. ret[0] = 0;
  778. for (c = (char *) path; c && *c; ++c) {
  779. if (strchr(ESCAPESHELL, *c))
  780. simple_snprintf(&ret[strlen(ret)], sizeof(ret1) - strlen(ret), "\\%c", *c);
  781. else
  782. simple_snprintf(&ret[strlen(ret)], sizeof(ret1) - strlen(ret), "%c", *c);
  783. }
  784. return ret;
  785. }
  786. /* vim: set sts=2 sw=2 ts=8 et: */