shell.c 22 KB

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