shell.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2013 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. return;
  228. if (trace == DET_IGNORE || trace == DET_WARN)
  229. trace = DET_DIE;
  230. // return;
  231. #ifdef DEBUG
  232. trace = DET_IGNORE;
  233. #endif /* DEBUG */
  234. if (trace == DET_IGNORE)
  235. return;
  236. pid_t parent = getpid();
  237. /* we send ourselves a SIGTRAP, if we recieve, we're not being traced, otherwise we are. */
  238. signal(SIGTRAP, got_sigtrap);
  239. traced = 1;
  240. raise(SIGTRAP);
  241. /* no longer need this__asm__("INT3"); //SIGTRAP */
  242. signal(SIGTRAP, SIG_DFL);
  243. if (!traced) {
  244. signal(SIGINT, got_sigtrap);
  245. traced = 1;
  246. raise(SIGINT);
  247. signal(SIGINT, got_int);
  248. }
  249. if (traced) {
  250. if (start) {
  251. kill(parent, SIGKILL);
  252. exit(1);
  253. } else
  254. detected(DETECT_TRACE, STR("I'm being traced!"));
  255. } else {
  256. if (!start)
  257. return;
  258. #ifndef __sun__
  259. int x, i;
  260. /* now, let's attempt to ptrace ourself */
  261. switch ((x = fork())) {
  262. case -1:
  263. return;
  264. case 0: //child
  265. i = ptrace(PT_ATTACH, parent, 0, 0);
  266. /* EPERM is given on fbsd when security.bsd.unprivileged_proc_debug=0 */
  267. if (i == -1 && errno != EPERM && errno != EINVAL) {
  268. if (start) {
  269. kill(parent, SIGKILL);
  270. exit(1);
  271. } else
  272. detected(DETECT_TRACE, STR("I'm being traced!"));
  273. } else {
  274. waitpid(parent, NULL, 0);
  275. ptrace(PT_DETACH, parent, (char *) 1, 0);
  276. kill(parent, SIGCHLD);
  277. }
  278. exit(0);
  279. default: //parent
  280. waitpid(x, NULL, 0);
  281. }
  282. #endif
  283. }
  284. }
  285. int shell_exec(char *cmdline, char *input, char **output, char **erroutput, bool simple)
  286. {
  287. if (!cmdline)
  288. return 0;
  289. Tempfile *in = NULL, *out = NULL, *err = NULL;
  290. int x;
  291. int parent = getpid();
  292. /* Set up temp files */
  293. in = new Tempfile("in");
  294. if (!in || in->error) {
  295. // putlog(LOG_ERRORS, "*" , "exec: Couldn't open '%s': %s", in->file, strerror(errno));
  296. if (in)
  297. delete in;
  298. return 0;
  299. }
  300. if (input) {
  301. if (fwrite(input, strlen(input), 1, in->f) != 1) {
  302. // putlog(LOG_ERRORS, "*", "exec: Couldn't write to '%s': %s", in->file, strerror(errno));
  303. delete in;
  304. return 0;
  305. }
  306. fseek(in->f, 0, SEEK_SET);
  307. }
  308. err = new Tempfile("err");
  309. if (!err || err->error) {
  310. // putlog(LOG_ERRORS, "*", "exec: Couldn't open '%s': %s", err->file, strerror(errno));
  311. delete in;
  312. if (err)
  313. delete err;
  314. return 0;
  315. }
  316. out = new Tempfile("out");
  317. if (!out || out->error) {
  318. // putlog(LOG_ERRORS, "*", "exec: Couldn't open '%s': %s", out->file, strerror(errno));
  319. delete in;
  320. delete err;
  321. if (out)
  322. delete out;
  323. return 0;
  324. }
  325. x = fork();
  326. if (x == -1) {
  327. putlog(LOG_ERRORS, "*", "exec: fork() failed: %s", strerror(errno));
  328. delete in;
  329. delete err;
  330. delete out;
  331. return 0;
  332. }
  333. if (x) { /* Parent: wait for the child to complete */
  334. int st = 0;
  335. size_t fs = 0;
  336. #ifdef PR_SET_PTRACER
  337. // Allow the child to debug the parent on Ubuntu
  338. // https://wiki.ubuntu.com/SecurityTeam/Roadmap/KernelHardening#ptrace
  339. prctl(PR_SET_PTRACER, x, 0, 0, 0);
  340. #endif
  341. waitpid(x, &st, 0);
  342. /* child is now complete, read the files into buffers */
  343. delete in;
  344. fflush(out->f);
  345. fflush(err->f);
  346. if (erroutput) {
  347. char *buf = NULL;
  348. fseek(err->f, 0, SEEK_END);
  349. fs = ftell(err->f);
  350. if (fs == 0) {
  351. (*erroutput) = NULL;
  352. } else {
  353. buf = (char *) my_calloc(1, fs + 1);
  354. fseek(err->f, 0, SEEK_SET);
  355. if (!fread(buf, 1, fs, err->f))
  356. fs = 0;
  357. buf[fs] = 0;
  358. (*erroutput) = buf;
  359. }
  360. }
  361. delete err;
  362. if (output) {
  363. char *buf = NULL;
  364. fseek(out->f, 0, SEEK_END);
  365. fs = ftell(out->f);
  366. if (fs == 0) {
  367. (*output) = NULL;
  368. } else {
  369. buf = (char *) my_calloc(1, fs + 1);
  370. fseek(out->f, 0, SEEK_SET);
  371. if (!fread(buf, 1, fs, out->f))
  372. fs = 0;
  373. buf[fs] = 0;
  374. (*output) = buf;
  375. }
  376. }
  377. delete out;
  378. return 1;
  379. } else {
  380. /* Child: make fd's and set them up as std* */
  381. // int ind, outd, errd;
  382. // ind = fileno(inpFile);
  383. // outd = fileno(outFile);
  384. // errd = fileno(errFile);
  385. if (dup2(in->fd, STDIN_FILENO) == (-1)) {
  386. kill(parent, SIGCHLD);
  387. exit(1);
  388. }
  389. if (dup2(out->fd, STDOUT_FILENO) == (-1)) {
  390. kill(parent, SIGCHLD);
  391. exit(1);
  392. }
  393. if (dup2(err->fd, STDERR_FILENO) == (-1)) {
  394. kill(parent, SIGCHLD);
  395. exit(1);
  396. }
  397. // Close all sockets
  398. for (int fd = 3; fd < MAX_SOCKETS; ++fd) close(fd);
  399. char *argv[15];
  400. if (simple) {
  401. char *p = NULL;
  402. int n = 0;
  403. char *mycmdline = strdup(cmdline);
  404. while (mycmdline[0] && (p = newsplit(&mycmdline)))
  405. argv[n++] = p;
  406. argv[n] = NULL;
  407. } else {
  408. argv[0] = "/bin/sh";
  409. argv[1] = "-c";
  410. argv[2] = cmdline;
  411. argv[3] = NULL;
  412. }
  413. execvp(argv[0], &argv[0]);
  414. kill(parent, SIGCHLD);
  415. exit(1);
  416. }
  417. }
  418. int simple_exec(const char* argv[]) {
  419. pid_t pid, savedpid;
  420. int status;
  421. switch ((pid = fork())) {
  422. case -1:
  423. break;
  424. case 0: //child
  425. // Close all sockets
  426. for (int fd = 3; fd < MAX_SOCKETS; ++fd) close(fd);
  427. execvp(argv[0], (char* const*) &argv[0]);
  428. _exit(127);
  429. default: //parent
  430. savedpid = pid;
  431. do {
  432. pid = wait4(savedpid, &status, 0, (struct rusage *)0);
  433. } while (pid == -1 && errno == EINTR);
  434. break;
  435. }
  436. return(pid == -1 ? -1 : status);
  437. }
  438. void suicide(const char *msg)
  439. {
  440. char tmp[512] = "";
  441. if (!conf.bot->localhub) {
  442. //im not a localhub, ask the localhub to suicide
  443. simple_snprintf(tmp, sizeof(tmp), STR("suicide %s"), msg);
  444. putbot(conf.localhub, tmp);
  445. return;
  446. } else {
  447. //im the localhub, loop thru bots and kill 'em
  448. putlog(LOG_WARN, "*", STR("Comitting suicide: %s"), msg);
  449. crontab_del();
  450. conf_bot *bot = NULL;
  451. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  452. if (!strcmp(conf.bot->nick, bot->nick))
  453. continue; //skip myself or i wont be able to remove the rest
  454. bot->pid = checkpid(bot->nick, bot);
  455. conf_killbot(conf.bots, NULL, bot, SIGKILL);
  456. unlink(bot->pid_file);
  457. deluser(bot->nick);
  458. }
  459. }
  460. if (!conf.bot->hub) {
  461. nuke_server(STR("kill the infidels!"));
  462. sleep(1);
  463. } else {
  464. unlink(userfile);
  465. simple_snprintf(tmp, sizeof(tmp), STR("%s~new"), userfile);
  466. unlink(tmp);
  467. simple_snprintf(tmp, sizeof(tmp), STR("%s~"), userfile);
  468. unlink(tmp);
  469. simple_snprintf(tmp, sizeof(tmp), STR("%s/%s~"), conf.datadir, userfile);
  470. unlink(tmp);
  471. simple_snprintf(tmp, sizeof(tmp), STR("%s/.u.0"), conf.datadir);
  472. unlink(tmp);
  473. simple_snprintf(tmp, sizeof(tmp), STR("%s/.u.1"), conf.datadir);
  474. unlink(tmp);
  475. }
  476. unlink(binname);
  477. //Not recursively clearing these dirs as they may be ~USER/ ..
  478. unlink(conf.datadir); //Probably will fail, shrug
  479. unlink(tempdir); //Probably will fail too, oh well
  480. //now deal with myself after the rest of the conf.bots are gone
  481. deluser(conf.bot->nick);
  482. unlink(conf.bot->pid_file);
  483. //and die in agony!
  484. fatal(msg, 0);
  485. }
  486. void detected(int code, const char *msg)
  487. {
  488. char tmp[512] = "";
  489. struct flag_record fr = { FR_GLOBAL, 0, 0, 0 };
  490. int act = DET_WARN, do_fatal = 0, killbots = 0;
  491. if (code == DETECT_LOGIN)
  492. act = login;
  493. if (code == DETECT_TRACE)
  494. act = trace;
  495. if (code == DETECT_PROMISC)
  496. act = promisc;
  497. if (code == DETECT_HIJACK)
  498. act = hijack;
  499. switch (act) {
  500. case DET_IGNORE:
  501. break;
  502. case DET_WARN:
  503. putlog(LOG_WARN, "*", "%s", msg);
  504. break;
  505. case DET_REJECT:
  506. do_fork();
  507. putlog(LOG_WARN, "*", STR("Setting myself +d: %s"), msg);
  508. simple_snprintf(tmp, sizeof(tmp), "+d: %s", msg);
  509. set_user(&USERENTRY_COMMENT, conf.bot->u, tmp);
  510. fr.global = USER_DEOP;
  511. fr.bot = 1;
  512. set_user_flagrec(conf.bot->u, &fr, 0);
  513. sleep(1);
  514. break;
  515. case DET_DIE:
  516. putlog(LOG_WARN, "*", STR("Dying: %s"), msg);
  517. simple_snprintf(tmp, sizeof(tmp), STR("Dying: %s"), msg);
  518. set_user(&USERENTRY_COMMENT, conf.bot->u, tmp);
  519. if (!conf.bot->hub)
  520. nuke_server(STR("BBL"));
  521. sleep(1);
  522. killbots++;
  523. do_fatal++;
  524. break;
  525. case DET_SUICIDE:
  526. suicide(msg);
  527. break;
  528. }
  529. if (killbots && conf.bot->localhub) {
  530. conf_checkpids(conf.bots);
  531. conf_killbot(conf.bots, NULL, NULL, SIGKILL);
  532. }
  533. if (do_fatal)
  534. fatal(msg, 0);
  535. }
  536. const char *werr_tostr(int errnum)
  537. {
  538. switch (errnum) {
  539. case ERR_BINSTAT:
  540. return STR("Cannot access binary");
  541. case ERR_BADPASS:
  542. return STR("Incorrect password");
  543. case ERR_PASSWD:
  544. return STR("Cannot access the global passwd file");
  545. case ERR_WRONGBINDIR:
  546. return STR("Wrong directory/binary name");
  547. case ERR_DATADIR:
  548. return STR("Cannot access datadir.");
  549. case ERR_TMPSTAT:
  550. return STR("Cannot access tmp directory.");
  551. case ERR_TMPMOD:
  552. return STR("Cannot chmod() tmp directory.");
  553. case ERR_WRONGUID:
  554. return STR("UID in binary does not match geteuid()");
  555. case ERR_WRONGUNAME:
  556. return STR("Uname in binary does not match uname()");
  557. case ERR_BADCONF:
  558. return STR("Config file is incomplete");
  559. case ERR_BADBOT:
  560. return STR("No such botnick");
  561. case ERR_BOTDISABLED:
  562. return STR("Bot is disabled, remove '/' in config");
  563. case ERR_NOBOTS:
  564. return STR("There are no bots in the binary! Please use ./binary -C to edit");
  565. case ERR_NOHOMEDIR:
  566. return STR("There is no homedir set. Please set one in the binary config with ./binary -C");
  567. case ERR_NOUSERNAME:
  568. return STR("There is no username set. Please set one in the binary config with ./binary -C");
  569. case ERR_NOBOT:
  570. return STR("I have no bot record but received -B???");
  571. case ERR_NOTINIT:
  572. return STR("Binary data is not initialized; try ./binary -C");
  573. case ERR_TOOMANYBOTS:
  574. return STR("Too many bots defined. 5 max. Too many will lead to klines.\nSpread out into multiple accounts/shells/ip ranges.");
  575. case ERR_LIBS:
  576. return STR("Failed to load required libraries.\nEnsure that 32bit compat libs are installed, and openssl.\nLinux: ia32-libs\nFreeBSD: misc/compat8x");
  577. default:
  578. return STR("Unforseen error");
  579. }
  580. }
  581. void werr(int errnum)
  582. {
  583. /* [1]+ Done ls --color=auto -A -CF
  584. [1]+ Killed bash
  585. */
  586. /*
  587. int x = 0;
  588. unsigned long job = randint(2) + 1; */
  589. /* printf("[%lu] %lu%lu%lu%lu%lu\n", job, randint(2) + 1, randint(8) + 1, randint(8) + 1, errnum); */
  590. /* printf("\n[%lu]+ Killed rm -rf /\r\n", job); */
  591. /*
  592. if (checkedpass) {
  593. printf("[%lu] %d\n", job, getpid());
  594. }
  595. */
  596. /* printf("\n[%lu]+ Stopped %s\r\n", job, basename(binname)); */
  597. #ifdef OBSCURE_ERRORS
  598. sdprintf(STR("Error %d: %s"), errnum, werr_tostr(errnum));
  599. printf(STR("*** Error code %d\n\n"), errnum);
  600. printf(STR("Segmentation fault\n"));
  601. #else
  602. fprintf(stderr, STR("Error %d: %s\n"), errnum, werr_tostr(errnum));
  603. #endif
  604. fatal("", 0);
  605. exit(1); // This is never reached, done for gcc() warnings
  606. }
  607. char *homedir(bool useconf)
  608. {
  609. static char homedir_buf[PATH_MAX] = "";
  610. if (!homedir_buf[0]) {
  611. if (conf.homedir && useconf)
  612. simple_snprintf(homedir_buf, sizeof homedir_buf, "%s", conf.homedir);
  613. else {
  614. char *home = getenv("HOME");
  615. if (home && strlen(home))
  616. strlcpy(homedir_buf, home, sizeof(homedir_buf));
  617. }
  618. }
  619. return homedir_buf[0] ? homedir_buf : NULL;
  620. }
  621. char *my_username()
  622. {
  623. static char username[DIRMAX] = "";
  624. if (!username[0]) {
  625. char *user = getenv("USER");
  626. if (user && strlen(user))
  627. strlcpy(username, user, sizeof(username));
  628. }
  629. return username[0] ? username : NULL;
  630. }
  631. int mkdir_p(const char *dir) {
  632. char *p = NULL, *path = NULL;
  633. path = p = strdup(dir);
  634. do {
  635. p = strchr(p + 1, '/');
  636. if (p)
  637. *p = '\0';
  638. if (can_stat(path) && !is_dir(path))
  639. unlink(path);
  640. if (!can_stat(path)) {
  641. if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
  642. unlink(path);
  643. mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
  644. }
  645. }
  646. if (p)
  647. *p = '/';
  648. } while(p);
  649. int couldStat = can_stat(path);
  650. free(path);
  651. return couldStat;
  652. }
  653. void expand_tilde(char **ptr)
  654. {
  655. if (!conf.homedir || !conf.homedir[0])
  656. return;
  657. char *str = ptr ? *ptr : NULL;
  658. if (str && strchr(str, '~')) {
  659. char *p = NULL;
  660. size_t siz = strlen(str);
  661. if (str[siz - 1] == '/')
  662. str[siz - 1] = 0;
  663. if ((p = replace(str, "~", conf.homedir)))
  664. str_redup(ptr, p);
  665. else
  666. fatal("Unforseen error expanding '~'", 0);
  667. }
  668. }
  669. void check_crontab()
  670. {
  671. int i = 0;
  672. if (!conf.bot->hub && !conf.bot->localhub)
  673. fatal(STR("something is wrong."), 0);
  674. if (!(i = crontab_exists())) {
  675. crontab_create(5);
  676. if (!(i = crontab_exists()))
  677. printf(STR("* Error writing crontab entry.\n"));
  678. }
  679. }
  680. static void crontab_install(bd::Stream& crontab) {
  681. // Write out the new crontab
  682. Tempfile new_crontab = Tempfile("crontab");
  683. crontab.writeFile(new_crontab.fd);
  684. // Install new crontab
  685. const char* argv[] = {"crontab", new_crontab.file, 0};
  686. simple_exec(argv);
  687. }
  688. void crontab_del() {
  689. bd::Stream crontab;
  690. if (crontab_exists(&crontab, 1) == 1)
  691. crontab_install(crontab);
  692. }
  693. int crontab_exists(bd::Stream* crontab, bool excludeSelf) {
  694. char *out = NULL;
  695. int ret = -1;
  696. if (shell_exec("crontab -l", NULL, &out, NULL, 1)) {
  697. if (out) {
  698. bd::Stream stream(out);
  699. bd::String line;
  700. ret = 0;
  701. while (stream.tell() < stream.length()) {
  702. line = stream.getline();
  703. if (line[0] != '#' && line.find(binname) != bd::String::npos) {
  704. ret = 1;
  705. // Need to continue if the existing crontab is requested
  706. if (crontab && !excludeSelf)
  707. (*crontab) << line;
  708. else if (!crontab)
  709. break;
  710. } else if (crontab)
  711. (*crontab) << line;
  712. }
  713. free(out);
  714. } else
  715. ret = 0;
  716. }
  717. return ret;
  718. }
  719. char s1_2[3] = "",s1_8[3] = "",s2_5[3] = "";
  720. void crontab_create(int interval) {
  721. bd::Stream crontab;
  722. if (crontab_exists(&crontab) == 1)
  723. return;
  724. char buf[1024] = "";
  725. if (interval == 1)
  726. strlcpy(buf, "*", 2);
  727. else {
  728. int i = 1;
  729. int si = randint(interval);
  730. while (i < 60) {
  731. if (buf[0])
  732. strlcat(buf, ",", sizeof(buf));
  733. simple_snprintf(&buf[strlen(buf)], sizeof(buf) - strlen(buf), "%i", (i + si) % 60);
  734. i += interval;
  735. }
  736. }
  737. simple_snprintf(buf + strlen(buf), sizeof buf, STR(" * * * * %s > /dev/null 2>&1\n"), shell_escape(binname));
  738. crontab << buf;
  739. crontab_install(crontab);
  740. }
  741. int det_translate(const char *word)
  742. {
  743. if (word && word[0]) {
  744. if (!strcasecmp(word, STR("ignore")))
  745. return DET_IGNORE;
  746. else if (!strcasecmp(word, STR("warn")))
  747. return DET_WARN;
  748. else if (!strcasecmp(word, STR("reject")))
  749. return DET_REJECT;
  750. else if (!strcasecmp(word, STR("die")))
  751. return DET_DIE;
  752. else if (!strcasecmp(word, STR("suicide")))
  753. return DET_SUICIDE;
  754. }
  755. return DET_IGNORE;
  756. }
  757. const char *det_translate_num(int num)
  758. {
  759. switch (num) {
  760. case DET_IGNORE: return STR("ignore");
  761. case DET_WARN: return STR("warn");
  762. case DET_REJECT: return STR("reject");
  763. case DET_DIE: return STR("die");
  764. case DET_SUICIDE:return STR("suicide");
  765. default: return STR("ignore");
  766. }
  767. return STR("ignore");
  768. }
  769. char *shell_escape(const char *path)
  770. {
  771. static char ret1[DIRMAX] = "", ret2[DIRMAX] = "", *ret = NULL;
  772. static bool alt = 0;
  773. char *c = NULL;
  774. if (alt) {
  775. alt = 0;
  776. ret = ret1;
  777. } else {
  778. alt = 1;
  779. ret = ret2;
  780. }
  781. ret[0] = 0;
  782. for (c = (char *) path; c && *c; ++c) {
  783. if (strchr(ESCAPESHELL, *c))
  784. simple_snprintf(&ret[strlen(ret)], sizeof(ret1) - strlen(ret), "\\%c", *c);
  785. else
  786. simple_snprintf(&ret[strlen(ret)], sizeof(ret1) - strlen(ret), "%c", *c);
  787. }
  788. return ret;
  789. }