shell.c 24 KB

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