shell.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. #ifdef __linux__
  294. bool traced = 0;
  295. static void got_sigtrap(int z)
  296. {
  297. traced = 0;
  298. }
  299. #endif
  300. void check_trace(int start)
  301. {
  302. if (trace == DET_IGNORE || trace == DET_WARN)
  303. trace = DET_DIE;
  304. // return;
  305. #ifdef DEBUG
  306. trace = DET_IGNORE;
  307. #endif /* DEBUG */
  308. int x, i;
  309. pid_t parent = getpid();
  310. #ifdef __linux__
  311. /* we send ourselves a SIGTRAP, if we recieve, we're not being traced, otherwise we are. */
  312. signal(SIGTRAP, got_sigtrap);
  313. traced = 1;
  314. raise(SIGTRAP);
  315. /* no longer need this__asm__("INT3"); //SIGTRAP */
  316. signal(SIGTRAP, SIG_DFL);
  317. if (traced) {
  318. if (start) {
  319. kill(parent, SIGKILL);
  320. exit(1);
  321. } else
  322. detected(DETECT_TRACE, "I'm being traced!");
  323. } else {
  324. x = fork();
  325. if (x == -1)
  326. return;
  327. else if (x == 0) {
  328. i = ptrace(PTRACE_ATTACH, parent, 0, 0);
  329. if (i == (-1) && errno == EPERM) {
  330. if (start) {
  331. kill(parent, SIGKILL);
  332. exit(1);
  333. } else
  334. detected(DETECT_TRACE, "I'm being traced!");
  335. } else {
  336. waitpid(parent, &i, 0);
  337. kill(parent, SIGCHLD);
  338. ptrace(PTRACE_DETACH, parent, 0, 0);
  339. kill(parent, SIGCHLD);
  340. }
  341. exit(0);
  342. } else
  343. wait(&i);
  344. }
  345. #endif /* __linux__ */
  346. #ifdef BSD
  347. x = fork();
  348. if (x == -1)
  349. return;
  350. else if (x == 0) {
  351. i = ptrace(PT_ATTACH, parent, 0, 0);
  352. if (i == (-1) && errno == EBUSY) {
  353. if (start) {
  354. kill(parent, SIGKILL);
  355. exit(1);
  356. } else
  357. detected(DETECT_TRACE, "I'm being traced");
  358. } else {
  359. wait(&i);
  360. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  361. kill(parent, SIGCHLD);
  362. wait(&i);
  363. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  364. wait(&i);
  365. }
  366. exit(0);
  367. } else
  368. waitpid(x, NULL, 0);
  369. #endif /* BSD */
  370. }
  371. #endif /* !CYGWIN_HACKS */
  372. int shell_exec(char *cmdline, char *input, char **output, char **erroutput)
  373. {
  374. if (!cmdline)
  375. return 0;
  376. Tempfile *in = NULL, *out = NULL, *err = NULL;
  377. int x;
  378. int parent = getpid();
  379. /* Set up temp files */
  380. in = new Tempfile("in");
  381. if (!in || in->error) {
  382. // putlog(LOG_ERRORS, "*" , "exec: Couldn't open '%s': %s", in->file, strerror(errno));
  383. if (in)
  384. delete in;
  385. return 0;
  386. }
  387. if (input) {
  388. if (fwrite(input, strlen(input), 1, in->f) != 1) {
  389. // putlog(LOG_ERRORS, "*", "exec: Couldn't write to '%s': %s", in->file, strerror(errno));
  390. delete in;
  391. return 0;
  392. }
  393. fseek(in->f, 0, SEEK_SET);
  394. }
  395. err = new Tempfile("err");
  396. if (!err || err->error) {
  397. // putlog(LOG_ERRORS, "*", "exec: Couldn't open '%s': %s", err->file, strerror(errno));
  398. delete in;
  399. if (err)
  400. delete err;
  401. return 0;
  402. }
  403. out = new Tempfile("out");
  404. if (!out || out->error) {
  405. // putlog(LOG_ERRORS, "*", "exec: Couldn't open '%s': %s", out->file, strerror(errno));
  406. delete in;
  407. delete err;
  408. if (out)
  409. delete out;
  410. return 0;
  411. }
  412. x = fork();
  413. if (x == -1) {
  414. putlog(LOG_ERRORS, "*", "exec: fork() failed: %s", strerror(errno));
  415. delete in;
  416. delete err;
  417. delete out;
  418. return 0;
  419. }
  420. if (x) { /* Parent: wait for the child to complete */
  421. int st = 0;
  422. size_t fs = 0;
  423. waitpid(x, &st, 0);
  424. /* child is now complete, read the files into buffers */
  425. delete in;
  426. fflush(out->f);
  427. fflush(err->f);
  428. if (erroutput) {
  429. char *buf = NULL;
  430. fseek(err->f, 0, SEEK_END);
  431. fs = ftell(err->f);
  432. if (fs == 0) {
  433. (*erroutput) = NULL;
  434. } else {
  435. buf = (char *) my_calloc(1, fs + 1);
  436. fseek(err->f, 0, SEEK_SET);
  437. fread(buf, 1, fs, err->f);
  438. buf[fs] = 0;
  439. (*erroutput) = buf;
  440. }
  441. }
  442. delete err;
  443. if (output) {
  444. char *buf = NULL;
  445. fseek(out->f, 0, SEEK_END);
  446. fs = ftell(out->f);
  447. if (fs == 0) {
  448. (*output) = NULL;
  449. } else {
  450. buf = (char *) my_calloc(1, fs + 1);
  451. fseek(out->f, 0, SEEK_SET);
  452. fread(buf, 1, fs, out->f);
  453. buf[fs] = 0;
  454. (*output) = buf;
  455. }
  456. }
  457. delete out;
  458. return 1;
  459. } else {
  460. /* Child: make fd's and set them up as std* */
  461. // int ind, outd, errd;
  462. char *argv[4] = { NULL, NULL, NULL, NULL };
  463. // ind = fileno(inpFile);
  464. // outd = fileno(outFile);
  465. // errd = fileno(errFile);
  466. if (dup2(in->fd, STDIN_FILENO) == (-1)) {
  467. kill(parent, SIGCHLD);
  468. exit(1);
  469. }
  470. if (dup2(out->fd, STDOUT_FILENO) == (-1)) {
  471. kill(parent, SIGCHLD);
  472. exit(1);
  473. }
  474. if (dup2(err->fd, STDERR_FILENO) == (-1)) {
  475. kill(parent, SIGCHLD);
  476. exit(1);
  477. }
  478. argv[0] = "sh";
  479. argv[1] = "-c";
  480. argv[2] = cmdline;
  481. argv[3] = NULL;
  482. execvp(argv[0], &argv[0]);
  483. kill(parent, SIGCHLD);
  484. exit(1);
  485. }
  486. }
  487. void suicide(const char *msg)
  488. {
  489. char tmp[512] = "";
  490. putlog(LOG_WARN, "*", "Comitting suicide: %s", msg);
  491. simple_sprintf(tmp, "Suicide: %s", msg);
  492. set_user(&USERENTRY_COMMENT, conf.bot->u, tmp);
  493. if (!conf.bot->hub) {
  494. nuke_server("HARAKIRI!!");
  495. sleep(1);
  496. } else {
  497. unlink(userfile);
  498. simple_sprintf(tmp, "%s~", userfile);
  499. unlink(tmp);
  500. }
  501. unlink(binname);
  502. if (conf.bot->localhub) {
  503. conf_checkpids();
  504. conf_killbot(NULL, NULL, SIGKILL);
  505. }
  506. fatal(msg, 0);
  507. }
  508. void detected(int code, char *msg)
  509. {
  510. char tmp[512] = "";
  511. struct flag_record fr = { FR_GLOBAL, 0, 0, 0 };
  512. int act = DET_WARN, do_fatal = 0, killbots = 0;
  513. if (code == DETECT_LOGIN)
  514. act = login;
  515. if (code == DETECT_TRACE)
  516. act = trace;
  517. if (code == DETECT_PROMISC)
  518. act = promisc;
  519. if (code == DETECT_PROCESS)
  520. act = badprocess;
  521. if (code == DETECT_SIGCONT)
  522. act = hijack;
  523. switch (act) {
  524. case DET_IGNORE:
  525. break;
  526. case DET_WARN:
  527. putlog(LOG_WARN, "*", msg);
  528. break;
  529. case DET_REJECT:
  530. do_fork();
  531. putlog(LOG_WARN, "*", "Setting myself +d: %s", msg);
  532. simple_sprintf(tmp, "+d: %s", msg);
  533. set_user(&USERENTRY_COMMENT, conf.bot->u, tmp);
  534. fr.global = USER_DEOP;
  535. fr.bot = 1;
  536. set_user_flagrec(conf.bot->u, &fr, 0);
  537. sleep(1);
  538. break;
  539. case DET_DIE:
  540. putlog(LOG_WARN, "*", "Dying: %s", msg);
  541. simple_sprintf(tmp, "Dying: %s", msg);
  542. set_user(&USERENTRY_COMMENT, conf.bot->u, tmp);
  543. if (!conf.bot->hub)
  544. nuke_server("BBL");
  545. sleep(1);
  546. killbots++;
  547. do_fatal++;
  548. break;
  549. case DET_SUICIDE:
  550. suicide(msg);
  551. break;
  552. }
  553. if (killbots && conf.bot->localhub) {
  554. conf_checkpids();
  555. conf_killbot(NULL, NULL, SIGKILL);
  556. }
  557. if (do_fatal)
  558. fatal(msg, 0);
  559. }
  560. char *werr_tostr(int errnum)
  561. {
  562. switch (errnum) {
  563. case ERR_BINSTAT:
  564. return "Cannot access binary";
  565. case ERR_BADPASS:
  566. return "Incorrect password";
  567. case ERR_BINMOD:
  568. return "Cannot chmod() binary";
  569. case ERR_PASSWD:
  570. return "Cannot access the global passwd file";
  571. case ERR_WRONGBINDIR:
  572. return "Wrong directory/binary name";
  573. case ERR_TMPSTAT:
  574. return STR("Cannot access tmp directory.");
  575. case ERR_TMPMOD:
  576. return STR("Cannot chmod() tmp directory.");
  577. case ERR_WRONGUID:
  578. return STR("UID in binary does not match geteuid()");
  579. case ERR_WRONGUNAME:
  580. return STR("Uname in binary does not match uname()");
  581. case ERR_BADCONF:
  582. return "Config file is incomplete";
  583. case ERR_BADBOT:
  584. return STR("No such botnick");
  585. case ERR_BOTDISABLED:
  586. return STR("Bot is disabled, remove '/' in config");
  587. case ERR_NOBOTS:
  588. return STR("There are no bots in the binary! Please use ./binary -C to edit");
  589. case ERR_NOBOT:
  590. return STR("I have no bot record but received -B???");
  591. default:
  592. return "Unforseen error";
  593. }
  594. }
  595. void werr(int errnum)
  596. {
  597. /* [1]+ Done ls --color=auto -A -CF
  598. [1]+ Killed bash
  599. */
  600. /*
  601. int x = 0;
  602. unsigned long job = randint(2) + 1; */
  603. /* printf("[%lu] %lu%lu%lu%lu%lu\n", job, randint(2) + 1, randint(8) + 1, randint(8) + 1, errnum); */
  604. /* printf("\n[%lu]+ Killed rm -rf /\r\n", job); */
  605. /*
  606. if (checkedpass) {
  607. printf("[%lu] %d\n", job, getpid());
  608. }
  609. */
  610. /* printf("\n[%lu]+ Stopped %s\r\n", job, basename(binname)); */
  611. sdprintf("Error %d: %s", errnum, werr_tostr(errnum));
  612. printf("*** Error code %d\n\n", errnum);
  613. printf(STR("Segmentation fault\n"));
  614. fatal("", 0);
  615. exit(0); //gcc is stupid :)
  616. }
  617. int email(char *subject, char *msg, int who)
  618. {
  619. struct utsname un;
  620. char run[2048] = "", addrs[1024] = "";
  621. int mail = 0, sendmail = 0;
  622. FILE *f = NULL;
  623. uname(&un);
  624. if (is_file("/usr/sbin/sendmail"))
  625. sendmail++;
  626. else if (is_file("/usr/bin/mail"))
  627. mail++;
  628. else {
  629. putlog(LOG_WARN, "*", "I Have no usable mail client.");
  630. return 1;
  631. }
  632. if (who & EMAIL_OWNERS) {
  633. simple_sprintf(addrs, "%s", replace(settings.owneremail, ",", " "));
  634. }
  635. if (who & EMAIL_TEAM) {
  636. if (addrs[0])
  637. simple_sprintf(addrs, "%s wraith@shatow.net", addrs);
  638. else
  639. simple_sprintf(addrs, "wraith@shatow.net");
  640. }
  641. if (sendmail)
  642. simple_sprintf(run, "/usr/sbin/sendmail -t");
  643. else if (mail)
  644. 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);
  645. if ((f = popen(run, "w"))) {
  646. if (sendmail) {
  647. fprintf(f, "To: %s\n", addrs);
  648. fprintf(f, "From: %s@%s\n", origbotname[0] ? origbotname : (conf.username ? conf.username : "WRAITH"), un.nodename);
  649. fprintf(f, "Subject: %s\n", subject);
  650. fprintf(f, "Content-Type: text/plain\n");
  651. }
  652. fprintf(f, "%s\n", msg);
  653. if (fflush(f))
  654. return 1;
  655. if (pclose(f))
  656. return 1;
  657. } else
  658. return 1;
  659. return 0;
  660. }
  661. void baduname(char *confhas, char *myuname) {
  662. char *tmpFile = NULL;
  663. int tosend = 0, make = 0;
  664. tmpFile = (char *) my_calloc(1, strlen(tempdir) + 3 + 1);
  665. simple_sprintf(tmpFile, "%s.un", tempdir);
  666. sdprintf("CHECKING %s", tmpFile);
  667. if (is_file(tmpFile)) {
  668. struct stat ss;
  669. time_t diff;
  670. stat(tmpFile, &ss);
  671. diff = now - ss.st_mtime;
  672. if (diff >= 86400) {
  673. tosend++; /* only send once a day */
  674. unlink(tmpFile); /* remove file */
  675. make++; /* make a new one at thie time. */
  676. }
  677. } else {
  678. make++;
  679. }
  680. if (make) {
  681. FILE *fp = NULL;
  682. if ((fp = fopen(tmpFile, "w"))) {
  683. fprintf(fp, "\n");
  684. fflush(fp);
  685. fclose(fp);
  686. tosend++; /* only send if we could write the file. */
  687. }
  688. }
  689. if (tosend) {
  690. struct utsname un;
  691. char msg[1024] = "", subject[31] = "";
  692. uname(&un);
  693. simple_snprintf(subject, sizeof subject, "CONF/UNAME() mismatch notice");
  694. 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"),
  695. conf.username ? conf.username : "unknown",
  696. binname,
  697. confhas, myuname, un.nodename);
  698. email(subject, msg, EMAIL_OWNERS);
  699. }
  700. free(tmpFile);
  701. }
  702. char *homedir(bool useconf)
  703. {
  704. static char homedir_buf[DIRMAX] = "";
  705. if (!homedir_buf || (homedir_buf && !homedir_buf[0])) {
  706. char tmp[DIRMAX] = "";
  707. if (conf.homedir && useconf)
  708. simple_snprintf(tmp, sizeof tmp, "%s", conf.homedir);
  709. else {
  710. #ifdef CYGWIN_HACKS
  711. simple_snprintf(tmp, sizeof tmp, "%s", dirname(binname));
  712. #else /* !CYGWIN_HACKS */
  713. struct passwd *pw = NULL;
  714. ContextNote("getpwuid()");
  715. pw = getpwuid(myuid);
  716. simple_snprintf(tmp, sizeof tmp, "%s", pw->pw_dir);
  717. ContextNote("getpwuid(): Success");
  718. #endif /* CYGWIN_HACKS */
  719. }
  720. ContextNote("realpath()");
  721. realpath(tmp, homedir_buf); /* this will convert lame home dirs of /home/blah->/usr/home/blah */
  722. ContextNote("realpath(): Success");
  723. }
  724. return homedir_buf;
  725. }
  726. char *my_username()
  727. {
  728. static char username[DIRMAX] = "";
  729. if (!username || (username && !username[0])) {
  730. #ifdef CYGWIN_HACKS
  731. simple_snprintf(username, sizeof username, "cygwin");
  732. #else /* !CYGWIN_HACKS */
  733. struct passwd *pw = NULL;
  734. ContextNote("getpwuid()");
  735. pw = getpwuid(myuid);
  736. ContextNote("getpwuid(): Success");
  737. simple_snprintf(username, sizeof username, "%s", pw->pw_name);
  738. #endif /* CYGWIN_HACKS */
  739. }
  740. return username;
  741. }
  742. void fix_tilde(char **binptr)
  743. {
  744. char *binpath = binptr ? *binptr : NULL;
  745. if (binpath && strchr(binpath, '~')) {
  746. char *p = NULL;
  747. if (binpath[strlen(binpath) - 1] == '/')
  748. binpath[strlen(binpath) - 1] = 0;
  749. if ((p = replace(binpath, "~", homedir())))
  750. str_redup(binptr, p);
  751. else
  752. fatal("Unforseen error expanding '~'", 0);
  753. }
  754. }
  755. char *my_uname()
  756. {
  757. static char os_uname[250] = "";
  758. if (!os_uname || (os_uname && !os_uname[0])) {
  759. char *unix_n = NULL, *vers_n = NULL;
  760. struct utsname un;
  761. if (uname(&un) < 0) {
  762. unix_n = "*unkown*";
  763. vers_n = "";
  764. } else {
  765. unix_n = un.nodename;
  766. #ifdef __FreeBSD__
  767. vers_n = un.release;
  768. #else /* __linux__ */
  769. vers_n = un.version;
  770. #endif /* __FreeBSD__ */
  771. }
  772. simple_snprintf(os_uname, sizeof os_uname, "%s %s", unix_n, vers_n);
  773. }
  774. return os_uname;
  775. }
  776. #ifndef CYGWIN_HACKS
  777. char *move_bin(const char *ipath, const char *file, bool run)
  778. {
  779. char *path = strdup(ipath);
  780. fix_tilde(&path);
  781. /* move the binary to the correct place */
  782. static char newbin[DIRMAX] = "";
  783. char real[DIRMAX] = "";
  784. simple_snprintf(newbin, sizeof newbin, "%s%s%s", path, path[strlen(path) - 1] == '/' ? "" : "/", file);
  785. ContextNote("realpath()");
  786. realpath(binname, real); /* get the realpath of binname */
  787. ContextNote("realpath(): Success");
  788. /* running from wrong dir, or wrong bin name.. lets try to fix that :) */
  789. sdprintf("binname: %s", binname);
  790. sdprintf("newbin: %s", newbin);
  791. sdprintf("real: %s", real);
  792. if (strcmp(binname, newbin) && strcmp(newbin, real)) { /* if wrong path and new path != current */
  793. bool ok = 1;
  794. sdprintf("wrong dir, is: %s :: %s", binname, newbin);
  795. unlink(newbin);
  796. if (copyfile(binname, newbin))
  797. ok = 0;
  798. if (ok && !can_stat(newbin)) {
  799. unlink(newbin);
  800. ok = 0;
  801. }
  802. if (ok && fixmod(newbin)) {
  803. unlink(newbin);
  804. ok = 0;
  805. }
  806. if (ok) {
  807. sdprintf("Binary successfully moved to: %s", newbin);
  808. unlink(binname);
  809. if (run) {
  810. system(newbin);
  811. sdprintf("exiting to let new binary run...");
  812. exit(0);
  813. }
  814. } else {
  815. if (run)
  816. werr(ERR_WRONGBINDIR);
  817. sdprintf("Binary move failed to: %s", newbin);
  818. return binname;
  819. }
  820. }
  821. return newbin;
  822. }
  823. void check_crontab()
  824. {
  825. int i = 0;
  826. if (!conf.bot->hub && !conf.bot->localhub)
  827. fatal("something is wrong.", 0);
  828. if (!(i = crontab_exists())) {
  829. crontab_create(5);
  830. if (!(i = crontab_exists()))
  831. printf("* Error writing crontab entry.\n");
  832. }
  833. }
  834. void crontab_del() {
  835. char *tmpFile = NULL, *p = NULL, buf[2048] = "";
  836. tmpFile = (char *) my_calloc(1, strlen(binname) + 100);
  837. strcpy(tmpFile, binname);
  838. if (!(p = strrchr(tmpFile, '/')))
  839. return;
  840. p++;
  841. strcpy(p, ".ctb");
  842. simple_sprintf(buf, "crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\" > %s", binname, tmpFile);
  843. if (shell_exec(buf, NULL, NULL, NULL)) {
  844. simple_sprintf(buf, "crontab %s", tmpFile);
  845. shell_exec(buf, NULL, NULL, NULL);
  846. }
  847. unlink(tmpFile);
  848. }
  849. int crontab_exists() {
  850. char buf[2048] = "", *out = NULL;
  851. simple_snprintf(buf, sizeof buf, "crontab -l | grep \"%s\" | grep -v \"^#\"", binname);
  852. if (shell_exec(buf, NULL, &out, NULL)) {
  853. if (out && strstr(out, binname)) {
  854. free(out);
  855. return 1;
  856. } else {
  857. if (out)
  858. free(out);
  859. return 0;
  860. }
  861. } else
  862. return (-1);
  863. }
  864. void crontab_create(int interval) {
  865. char tmpFile[161] = "";
  866. FILE *f = NULL;
  867. int fd;
  868. simple_snprintf(tmpFile, sizeof tmpFile, "%s.crontab-XXXXXX", tempdir);
  869. if ((fd = mkstemp(tmpFile)) == -1) {
  870. unlink(tmpFile);
  871. return;
  872. }
  873. char buf[256] = "";
  874. simple_snprintf(buf, sizeof buf, "crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\"> %s", binname, tmpFile);
  875. if (shell_exec(buf, NULL, NULL, NULL) && (f = fdopen(fd, "a")) != NULL) {
  876. buf[0] = 0;
  877. if (interval == 1)
  878. strcpy(buf, "*");
  879. else {
  880. int i = 1;
  881. int si = randint(interval);
  882. while (i < 60) {
  883. if (buf[0])
  884. simple_sprintf(buf + strlen(buf), ",%i", (i + si) % 60);
  885. else
  886. simple_sprintf(buf, "%i", (i + si) % 60);
  887. i += interval;
  888. }
  889. }
  890. simple_snprintf(buf + strlen(buf), sizeof buf, " * * * * %s > /dev/null 2>&1", binname);
  891. fseek(f, 0, SEEK_END);
  892. fprintf(f, "\n%s\n", buf);
  893. fclose(f);
  894. simple_sprintf(buf, "crontab %s", tmpFile);
  895. shell_exec(buf, NULL, NULL, NULL);
  896. }
  897. close(fd);
  898. unlink(tmpFile);
  899. }
  900. #endif /* !CYGWIN_HACKS */
  901. #ifdef CRAZY_TRACE
  902. /* This code will attach a ptrace() to getpid() hence blocking process hijackers/tracers on the pid
  903. * only problem.. it just creates a new pid to be traced/hijacked :\
  904. */
  905. int attached = 0;
  906. void crazy_trace()
  907. {
  908. pid_t parent = getpid();
  909. int x = fork();
  910. if (x == -1) {
  911. printf("Can't fork(): %s\n", strerror(errno));
  912. } else if (x == 0) {
  913. /* child */
  914. int i;
  915. i = ptrace(PTRACE_ATTACH, parent, (char *) 1, 0);
  916. if (i == (-1) && errno == EPERM) {
  917. printf("CANT PTRACE PARENT: errno: %d %s, i: %d\n", errno, strerror(errno), i);
  918. waitpid(parent, &i, 0);
  919. kill(parent, SIGCHLD);
  920. ptrace(PTRACE_DETACH, parent, 0, 0);
  921. kill(parent, SIGCHLD);
  922. exit(0);
  923. } else {
  924. printf("SUCCESSFUL ATTACH to %d: %d\n", parent, i);
  925. attached++;
  926. }
  927. } else {
  928. /* parent */
  929. printf("wait()\n");
  930. wait(&x);
  931. }
  932. printf("end\n");
  933. }
  934. #endif /* CRAZY_TRACE */
  935. int det_translate(const char *word)
  936. {
  937. if (word && word[0]) {
  938. if (!egg_strcasecmp(word, "ignore"))
  939. return DET_IGNORE;
  940. else if (!egg_strcasecmp(word, "warn"))
  941. return DET_WARN;
  942. else if (!egg_strcasecmp(word, "reject"))
  943. return DET_REJECT;
  944. else if (!egg_strcasecmp(word, "die"))
  945. return DET_DIE;
  946. else if (!egg_strcasecmp(word, "suicide"))
  947. return DET_SUICIDE;
  948. }
  949. return DET_IGNORE;
  950. }
  951. const char *det_translate_num(int num)
  952. {
  953. switch (num) {
  954. case DET_IGNORE: return "ignore";
  955. case DET_WARN: return "warn";
  956. case DET_REJECT: return "reject";
  957. case DET_DIE: return "die";
  958. case DET_SUICIDE:return "suicide";
  959. default: return "ignore";
  960. }
  961. return "ignore";
  962. }