shell.c 22 KB

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