shell.c 28 KB

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