1
0

shell.c 27 KB

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