shell.c 27 KB

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