shell.c 28 KB

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