shell.c 28 KB

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