shell.c 27 KB

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