shell.cc 23 KB

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