shell.c 26 KB

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