shell.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * shell.c -- handles:
  3. *
  4. * All shell related functions
  5. * -shell_exec()
  6. * -botconfig parsing
  7. * -check_*()
  8. * -crontab functions
  9. *
  10. */
  11. #include "common.h"
  12. #include "shell.h"
  13. #include "chanprog.h"
  14. #include "cfg.h"
  15. #include "settings.h"
  16. #include "userrec.h"
  17. #include "flags.h"
  18. #include "tandem.h"
  19. #include "main.h"
  20. #include "dccutil.h"
  21. #include "misc.h"
  22. #include "misc_file.h"
  23. #include "bg.h"
  24. #include "stat.h"
  25. #include "users.h"
  26. #ifdef LEAF
  27. #include "src/mod/server.mod/server.h"
  28. #endif /* LEAF */
  29. #include <sys/types.h>
  30. #include <pwd.h>
  31. #include <signal.h>
  32. #ifndef CYGWIN_HACKS
  33. # include <sys/ptrace.h>
  34. #endif /* !CYGWIN_HACKS */
  35. #include <sys/wait.h>
  36. #include <sys/utsname.h>
  37. #include <pwd.h>
  38. #include <errno.h>
  39. #include <net/if.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/socket.h>
  42. #include <ctype.h>
  43. #include <fcntl.h>
  44. #include <sys/stat.h>
  45. #include <unistd.h>
  46. #include <dirent.h>
  47. #ifdef WIN32
  48. int
  49. my_system(const char *run)
  50. {
  51. int ret = -1;
  52. PROCESS_INFORMATION pinfo;
  53. STARTUPINFO sinfo;
  54. memset(&sinfo, 0, sizeof(STARTUPINFO));
  55. sinfo.cb = sizeof(sinfo);
  56. sinfo.wShowWindow = SW_HIDE;
  57. sinfo.dwFlags |= STARTF_USESTDHANDLES;
  58. sinfo.hStdInput =
  59. sinfo.hStdOutput =
  60. sinfo.hStdError =
  61. ret =
  62. CreateProcess(NULL, (char *) run, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS | DETACHED_PROCESS, NULL, NULL,
  63. &sinfo, &pinfo);
  64. if (ret == 0)
  65. return -1;
  66. else
  67. return 0;
  68. }
  69. #endif /* WIN32 */
  70. int clear_tmp()
  71. {
  72. DIR *tmp = NULL;
  73. struct dirent *dir_ent = NULL;
  74. if (!(tmp = opendir(tempdir))) return 1;
  75. while ((dir_ent = readdir(tmp))) {
  76. if (strncmp(dir_ent->d_name, ".pid.", 4) && strncmp(dir_ent->d_name, ".u", 2) && strcmp(dir_ent->d_name, ".bin.old")
  77. && strcmp(dir_ent->d_name, ".") && strcmp(dir_ent->d_name, ".un") && strcmp(dir_ent->d_name, "..")) {
  78. char *file = NULL;
  79. file = calloc(1, strlen(dir_ent->d_name) + strlen(tempdir) + 1);
  80. strcat(file, tempdir);
  81. strcat(file, dir_ent->d_name);
  82. file[strlen(file)] = 0;
  83. sdprintf("clear_tmp: %s", file);
  84. unlink(file);
  85. free(file);
  86. }
  87. }
  88. closedir(tmp);
  89. return 0;
  90. }
  91. #ifdef LEAF
  92. void check_mypid()
  93. {
  94. if (can_stat(conf.bot->pid_file) && (getpid() != checkpid(conf.bot->nick, NULL)))
  95. fatal(STR("getpid() does not match pid in file. Possible cloned process, exiting.."), 0);
  96. }
  97. #endif /* LEAF */
  98. #ifndef CYGWIN_HACKS
  99. char last_buf[128] = "";
  100. void check_last() {
  101. if (!strcmp((char *) CFG_LOGIN.ldata ? CFG_LOGIN.ldata : CFG_LOGIN.gdata ? CFG_LOGIN.gdata : "ignore", "ignore"))
  102. return;
  103. if (conf.username) {
  104. char *out = NULL, buf[50] = "";
  105. sprintf(buf, "last %s", conf.username);
  106. if (shell_exec(buf, NULL, &out, NULL)) {
  107. if (out) {
  108. char *p = NULL;
  109. p = strchr(out, '\n');
  110. if (p)
  111. *p = 0;
  112. if (strlen(out) > 10) {
  113. if (last_buf[0]) {
  114. if (strncmp(last_buf, out, sizeof(last_buf))) {
  115. char *work = NULL;
  116. work = calloc(1, strlen(out) + 7 + 2 + 1);
  117. sprintf(work, "Login: %s", out);
  118. detected(DETECT_LOGIN, work);
  119. free(work);
  120. }
  121. }
  122. strncpyz(last_buf, out, sizeof(last_buf));
  123. }
  124. free(out);
  125. }
  126. }
  127. }
  128. }
  129. void check_processes()
  130. {
  131. char *proclist = NULL, *out = NULL, *p = NULL, *np = NULL, *curp = NULL, buf[1024] = "", bin[128] = "";
  132. if (!strcmp((char *) CFG_BADPROCESS.ldata ? CFG_BADPROCESS.ldata : CFG_BADPROCESS.gdata ? CFG_BADPROCESS.gdata : "ignore", "ignore"))
  133. return;
  134. proclist = (char *) (CFG_PROCESSLIST.ldata && ((char *) CFG_PROCESSLIST.ldata)[0] ?
  135. CFG_PROCESSLIST.ldata : CFG_PROCESSLIST.gdata && ((char *) CFG_PROCESSLIST.gdata)[0] ? CFG_PROCESSLIST.gdata : NULL);
  136. if (!proclist)
  137. return;
  138. if (!shell_exec("ps x", NULL, &out, NULL))
  139. return;
  140. /* Get this binary's filename */
  141. strncpyz(buf, binname, sizeof(buf));
  142. p = strrchr(buf, '/');
  143. if (p) {
  144. p++;
  145. strncpyz(bin, p, sizeof(bin));
  146. } else {
  147. bin[0] = 0;
  148. }
  149. /* Fix up the "permitted processes" list */
  150. p = calloc(1, strlen(proclist) + strlen(bin) + 6);
  151. strcpy(p, proclist);
  152. strcat(p, " ");
  153. strcat(p, bin);
  154. strcat(p, " ");
  155. proclist = p;
  156. curp = out;
  157. while (curp) {
  158. np = strchr(curp, '\n');
  159. if (np)
  160. *np++ = 0;
  161. if (atoi(curp) > 0) {
  162. char *pid = NULL, *tty = NULL, *mystat = NULL, *mytime = NULL, cmd[512] = "", line[2048] = "";
  163. strncpyz(line, curp, sizeof(line));
  164. /* it's a process line */
  165. /* Assuming format: pid tty stat time cmd */
  166. pid = newsplit(&curp);
  167. tty = newsplit(&curp);
  168. mystat = newsplit(&curp);
  169. mytime = newsplit(&curp);
  170. strncpyz(cmd, curp, sizeof(cmd));
  171. /* skip any <defunct> procs "/bin/sh -c" crontab stuff and binname crontab stuff */
  172. if (!strstr(cmd, "<defunct>") && !strncmp(cmd, "/bin/sh -c", 10) && !strncmp(cmd, binname, strlen(binname))) {
  173. /* get rid of any args */
  174. if ((p = strchr(cmd, ' ')))
  175. *p = 0;
  176. /* remove [] or () */
  177. if (strlen(cmd)) {
  178. p = cmd + strlen(cmd) - 1;
  179. if (((cmd[0] == '(') && (*p == ')')) || ((cmd[0] == '[') && (*p == ']'))) {
  180. *p = 0;
  181. strcpy(buf, cmd + 1);
  182. strcpy(cmd, buf);
  183. }
  184. }
  185. /* remove path */
  186. if ((p = strrchr(cmd, '/'))) {
  187. p++;
  188. strcpy(buf, p);
  189. strcpy(cmd, buf);
  190. }
  191. /* skip "ps" */
  192. if (strcmp(cmd, "ps")) {
  193. /* see if proc's in permitted list */
  194. strcat(cmd, " ");
  195. if ((p = strstr(proclist, cmd))) {
  196. /* Remove from permitted list */
  197. while (*p != ' ')
  198. *p++ = 1;
  199. } else {
  200. char *work = NULL;
  201. size_t size = 0;
  202. size = strlen(line) + 22;
  203. work = calloc(1, size);
  204. egg_snprintf(work, size, "Unexpected process: %s", line);
  205. detected(DETECT_PROCESS, work);
  206. free(work);
  207. }
  208. }
  209. }
  210. }
  211. curp = np;
  212. }
  213. free(proclist);
  214. if (out)
  215. free(out);
  216. }
  217. void check_promisc()
  218. {
  219. #ifdef SIOCGIFCONF
  220. struct ifconf ifcnf;
  221. char *reqp = NULL, *end_req = NULL, buf[1024] = "";
  222. int sock;
  223. if (!strcmp((char *) CFG_PROMISC.ldata ? CFG_PROMISC.ldata : CFG_PROMISC.gdata ? CFG_PROMISC.gdata : "ignore", "ignore"))
  224. return;
  225. sock = socket(AF_INET, SOCK_DGRAM, 0);
  226. if (sock < 0)
  227. return;
  228. ifcnf.ifc_len = sizeof(buf);
  229. ifcnf.ifc_buf = buf;
  230. if (ioctl(sock, SIOCGIFCONF, &ifcnf) < 0) {
  231. close(sock);
  232. return;
  233. }
  234. reqp = buf; /* pointer to start of array */
  235. end_req = buf + ifcnf.ifc_len; /* pointer to end of array */
  236. while (reqp < end_req) {
  237. struct ifreq ifreq, *ifr = NULL;
  238. ifr = (struct ifreq *) reqp; /* start examining interface */
  239. ifreq = *ifr;
  240. if (!ioctl(sock, SIOCGIFFLAGS, &ifreq)) { /* we can read this interface! */
  241. /* sdprintf("Examing interface: %s", ifr->ifr_name); */
  242. if (ifreq.ifr_flags & IFF_PROMISC) {
  243. char which[101] = "";
  244. sprintf(which, "Detected promiscuous mode on interface: %s", ifr->ifr_name);
  245. ioctl(sock, SIOCSIFFLAGS, &ifreq); /* set flags */
  246. detected(DETECT_PROMISC, which);
  247. break;
  248. }
  249. }
  250. /* move pointer to next array element (next interface) */
  251. reqp += sizeof(ifr->ifr_name) + sizeof(ifr->ifr_addr);
  252. }
  253. close(sock);
  254. #endif /* SIOCGIFCONF */
  255. }
  256. int traced = 0;
  257. static void got_sigtrap(int z)
  258. {
  259. traced = 0;
  260. }
  261. void check_trace(int start)
  262. {
  263. int x, parent, i;
  264. if (!strcmp((char *) CFG_TRACE.ldata ? CFG_TRACE.ldata : CFG_TRACE.gdata ? CFG_TRACE.gdata : "ignore", "ignore"))
  265. return;
  266. parent = getpid();
  267. #ifdef __linux__
  268. /* we send ourselves a SIGTRAP, if we recieve, we're not being traced, otherwise we are. */
  269. signal(SIGTRAP, got_sigtrap);
  270. traced = 1;
  271. raise(SIGTRAP);
  272. /* no longer need this__asm__("INT3"); //SIGTRAP */
  273. signal(SIGTRAP, SIG_DFL);
  274. if (traced) {
  275. if (start) {
  276. kill(parent, SIGKILL);
  277. exit(1);
  278. } else
  279. detected(DETECT_TRACE, "I'm being traced!");
  280. } else {
  281. x = fork();
  282. if (x == -1)
  283. return;
  284. else if (x == 0) {
  285. i = ptrace(PTRACE_ATTACH, parent, 0, 0);
  286. if (i == (-1) && errno == EPERM) {
  287. if (start) {
  288. kill(parent, SIGKILL);
  289. exit(1);
  290. } else
  291. detected(DETECT_TRACE, "I'm being traced!");
  292. } else {
  293. waitpid(parent, &i, 0);
  294. kill(parent, SIGCHLD);
  295. ptrace(PTRACE_DETACH, parent, 0, 0);
  296. kill(parent, SIGCHLD);
  297. }
  298. exit(0);
  299. } else
  300. wait(&i);
  301. }
  302. #endif /* __linux__ */
  303. #ifdef BSD
  304. x = fork();
  305. if (x == -1)
  306. return;
  307. else if (x == 0) {
  308. i = ptrace(PT_ATTACH, parent, 0, 0);
  309. if (i == (-1) && errno == EBUSY) {
  310. if (start) {
  311. kill(parent, SIGKILL);
  312. exit(1);
  313. } else
  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 /* BSD */
  327. }
  328. #endif /* !CYGWIN_HACKS */
  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. if (code == DETECT_LOGIN)
  463. p = (char *) (CFG_LOGIN.ldata ? CFG_LOGIN.ldata : (CFG_LOGIN.gdata ? CFG_LOGIN.gdata : NULL));
  464. if (code == DETECT_TRACE)
  465. p = (char *) (CFG_TRACE.ldata ? CFG_TRACE.ldata : (CFG_TRACE.gdata ? CFG_TRACE.gdata : NULL));
  466. if (code == DETECT_PROMISC)
  467. p = (char *) (CFG_PROMISC.ldata ? CFG_PROMISC.ldata : (CFG_PROMISC.gdata ? CFG_PROMISC.gdata : NULL));
  468. if (code == DETECT_PROCESS)
  469. p = (char *) (CFG_BADPROCESS.ldata ? CFG_BADPROCESS.ldata : (CFG_BADPROCESS.gdata ? CFG_BADPROCESS.gdata : NULL));
  470. if (code == DETECT_SIGCONT)
  471. p = (char *) (CFG_HIJACK.ldata ? CFG_HIJACK.ldata : (CFG_HIJACK.gdata ? CFG_HIJACK.gdata : NULL));
  472. if (!p)
  473. act = DET_WARN;
  474. else if (!strcmp(p, "die"))
  475. act = DET_DIE;
  476. else if (!strcmp(p, "reject"))
  477. act = DET_REJECT;
  478. else if (!strcmp(p, "suicide"))
  479. act = DET_SUICIDE;
  480. else if (!strcmp(p, "ignore"))
  481. act = DET_IGNORE;
  482. else
  483. act = DET_WARN;
  484. switch (act) {
  485. case DET_IGNORE:
  486. break;
  487. case DET_WARN:
  488. putlog(LOG_WARN, "*", msg);
  489. break;
  490. case DET_REJECT:
  491. do_fork();
  492. putlog(LOG_WARN, "*", "Setting myself +d: %s", msg);
  493. sprintf(tmp, "+d: %s", msg);
  494. set_user(&USERENTRY_COMMENT, u, tmp);
  495. fr.global = USER_DEOP;
  496. fr.bot = 1;
  497. set_user_flagrec(u, &fr, 0);
  498. sleep(1);
  499. break;
  500. case DET_DIE:
  501. putlog(LOG_WARN, "*", "Dying: %s", msg);
  502. sprintf(tmp, "Dying: %s", msg);
  503. set_user(&USERENTRY_COMMENT, u, tmp);
  504. #ifdef LEAF
  505. nuke_server("BBL");
  506. #endif /* LEAF */
  507. sleep(1);
  508. fatal(msg, 0);
  509. break;
  510. case DET_SUICIDE:
  511. putlog(LOG_WARN, "*", "Comitting suicide: %s", msg);
  512. sprintf(tmp, "Suicide: %s", msg);
  513. set_user(&USERENTRY_COMMENT, u, tmp);
  514. #ifdef LEAF
  515. nuke_server("HARAKIRI!!");
  516. #endif /* LEAF */
  517. sleep(1);
  518. unlink(binname);
  519. #ifdef HUB
  520. unlink(userfile);
  521. sprintf(tmp, "%s~", userfile);
  522. unlink(tmp);
  523. #endif /* HUB */
  524. fatal(msg, 0);
  525. break;
  526. }
  527. }
  528. char *werr_tostr(int errnum)
  529. {
  530. switch (errnum) {
  531. case ERR_BINSTAT:
  532. return "Cannot access binary";
  533. case ERR_BADPASS:
  534. return "Incorrect password";
  535. case ERR_BINMOD:
  536. return "Cannot chmod() binary";
  537. case ERR_PASSWD:
  538. return "Cannot access the global passwd file";
  539. case ERR_WRONGBINDIR:
  540. return "Wrong directory/binary name";
  541. case ERR_CONFSTAT:
  542. #ifdef LEAF
  543. return STR("Cannot access config directory (~/.ssh/)");
  544. #else
  545. return "Cannot access config directory (./)";
  546. #endif /* LEAF */
  547. case ERR_TMPSTAT:
  548. #ifdef LEAF
  549. return STR("Cannot access tmp directory (~/.ssh/.../)");
  550. #else
  551. return STR("Cannot access config directory (./tmp/)");
  552. #endif /* LEAF */
  553. case ERR_CONFDIRMOD:
  554. #ifdef LEAF
  555. return STR("Cannot chmod() config directory (~/.ssh/)");
  556. #else
  557. return "Cannot chmod() config directory (./)";
  558. #endif /* LEAF */
  559. case ERR_CONFMOD:
  560. #ifdef LEAF
  561. return STR("Cannot chmod() config (~/.ssh/.known_hosts/)");
  562. #else
  563. return STR("Cannot chmod() config (./conf)");
  564. #endif /* LEAF */
  565. case ERR_TMPMOD:
  566. #ifdef LEAF
  567. return STR("Cannot chmod() tmp directory (~/.ssh/.../)");
  568. #else
  569. return STR("Cannot chmod() tmp directory (./tmp)");
  570. #endif /* LEAF */
  571. case ERR_NOCONF:
  572. #ifdef LEAF
  573. return STR("The local config is missing (~/.ssh/.known_hosts)");
  574. #else
  575. return STR("The local config is missing (./conf)");
  576. #endif /* LEAF */
  577. case ERR_CONFBADENC:
  578. return STR("Encryption in config is wrong/corrupt");
  579. case ERR_WRONGUID:
  580. return STR("UID in conf does not match geteuid()");
  581. case ERR_WRONGUNAME:
  582. return STR("Uname in conf does not match uname()");
  583. case ERR_BADCONF:
  584. return "Config file is incomplete";
  585. case ERR_BADBOT:
  586. return STR("No such botnick");
  587. case ERR_BOTDISABLED:
  588. return STR("Bot is disabled, remove '/' in config");
  589. case ERR_NOBOTS:
  590. return STR("There are no bots in the config! Please use ./binary -C to edit");
  591. default:
  592. return "Unforseen error";
  593. }
  594. }
  595. void werr(int errnum)
  596. {
  597. /* [1]+ Done ls --color=auto -A -CF
  598. [1]+ Killed bash
  599. */
  600. /*
  601. int x = 0;
  602. unsigned long job = randint(2) + 1; */
  603. /* printf("[%lu] %lu%lu%lu%lu%lu\n", job, randint(2) + 1, randint(8) + 1, randint(8) + 1, errnum); */
  604. /* printf("\n[%lu]+ Killed rm -rf /\r\n", job); */
  605. /*
  606. if (checkedpass) {
  607. printf("[%lu] %d\n", job, getpid());
  608. }
  609. */
  610. /* printf("\n[%lu]+ Stopped %s\r\n", job, basename(binname)); */
  611. sdprintf("Error %d: %s", errnum, werr_tostr(errnum));
  612. printf("*** Error code %d\n\n", errnum);
  613. printf(STR("Segmentation fault\n"));
  614. fatal("", 0);
  615. }
  616. int email(char *subject, char *msg, int who)
  617. {
  618. struct utsname un;
  619. char run[2048] = "", addrs[1024] = "";
  620. int mail = 0, sendmail = 0;
  621. FILE *f = NULL;
  622. uname(&un);
  623. if (is_file("/usr/sbin/sendmail"))
  624. sendmail++;
  625. else if (is_file("/usr/bin/mail"))
  626. mail++;
  627. else {
  628. putlog(LOG_WARN, "*", "I Have no usable mail client.");
  629. return 1;
  630. }
  631. if (who & EMAIL_OWNERS) {
  632. sprintf(addrs, "%s", replace(owneremail, ",", " "));
  633. }
  634. if (who & EMAIL_TEAM) {
  635. if (addrs[0])
  636. sprintf(addrs, "%s wraith@shatow.net", addrs);
  637. else
  638. sprintf(addrs, "wraith@shatow.net");
  639. }
  640. if (sendmail)
  641. sprintf(run, "/usr/sbin/sendmail -t");
  642. else if (mail)
  643. sprintf(run, "/usr/bin/mail %s -a \"From: %s@%s\" -s \"%s\" -a \"Content-Type: text/plain\"", addrs, conf.bot->nick ? conf.bot->nick : "none", un.nodename, subject);
  644. if ((f = popen(run, "w"))) {
  645. if (sendmail) {
  646. fprintf(f, "To: %s\n", addrs);
  647. fprintf(f, "From: %s@%s\n", origbotname[0] ? origbotname : (conf.username ? conf.username : "WRAITH"), un.nodename);
  648. fprintf(f, "Subject: %s\n", subject);
  649. fprintf(f, "Content-Type: text/plain\n");
  650. }
  651. fprintf(f, "%s\n", msg);
  652. if (fflush(f))
  653. return 1;
  654. if (pclose(f))
  655. return 1;
  656. } else
  657. return 1;
  658. return 0;
  659. }
  660. void baduname(char *confhas, char *myuname) {
  661. char *tmpFile = NULL;
  662. int tosend = 0, make = 0;
  663. tmpFile = calloc(1, strlen(tempdir) + 3 + 1);
  664. sprintf(tmpFile, "%s.un", tempdir);
  665. sdprintf("CHECKING %s", tmpFile);
  666. if (is_file(tmpFile)) {
  667. struct stat ss;
  668. time_t diff;
  669. stat(tmpFile, &ss);
  670. diff = now - ss.st_mtime;
  671. if (diff >= 86400) {
  672. tosend++; /* only send once a day */
  673. unlink(tmpFile); /* remove file */
  674. make++; /* make a new one at thie time. */
  675. }
  676. } else {
  677. make++;
  678. }
  679. if (make) {
  680. FILE *fp = NULL;
  681. if ((fp = fopen(tmpFile, "w"))) {
  682. fprintf(fp, "\n");
  683. fflush(fp);
  684. fclose(fp);
  685. tosend++; /* only send if we could write the file. */
  686. }
  687. }
  688. if (tosend) {
  689. struct utsname un;
  690. char msg[501] = "", subject[31] = "";
  691. uname(&un);
  692. egg_snprintf(subject, sizeof subject, "CONF/UNAME() mismatch notice");
  693. 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"),
  694. conffile.username ? conffile.username : (conf.username ? conf.username : "unknown"),
  695. confhas, myuname, un.nodename);
  696. email(subject, msg, EMAIL_OWNERS);
  697. }
  698. free(tmpFile);
  699. }
  700. char *homedir()
  701. {
  702. static char homedir_buf[DIRMAX] = "";
  703. if (!homedir_buf || (homedir_buf && !homedir_buf[0])) {
  704. char tmp[DIRMAX] = "";
  705. if (conf.homedir)
  706. egg_snprintf(tmp, sizeof tmp, "%s", conf.homedir);
  707. else {
  708. #ifdef CYGWIN_HACKS
  709. egg_snprintf(tmp, sizeof tmp, "%s", dirname(binname));
  710. #else /* !CYGWIN_HACKS */
  711. struct passwd *pw = NULL;
  712. ContextNote("getpwuid()");
  713. pw = getpwuid(myuid);
  714. egg_snprintf(tmp, sizeof tmp, "%s", pw->pw_dir);
  715. ContextNote("getpwuid(): Success");
  716. #endif /* CYGWIN_HACKS */
  717. }
  718. ContextNote("realpath()");
  719. realpath(tmp, homedir_buf); /* this will convert lame home dirs of /home/blah->/usr/home/blah */
  720. ContextNote("realpath(): Success");
  721. }
  722. return homedir_buf;
  723. }
  724. char *my_username()
  725. {
  726. static char username[DIRMAX] = "";
  727. if (!username || (username && !username[0])) {
  728. if (conf.username)
  729. egg_snprintf(username, sizeof username, "%s", conf.username);
  730. else {
  731. #ifdef CYGWIN_HACKS
  732. egg_snprintf(username, sizeof username, "cygwin");
  733. #else /* !CYGWIN_HACKS */
  734. struct passwd *pw = NULL;
  735. ContextNote("getpwuid()");
  736. pw = getpwuid(myuid);
  737. ContextNote("getpwuid(): Success");
  738. egg_snprintf(username, sizeof username, "%s", pw->pw_name);
  739. #endif /* CYGWIN_HACKS */
  740. }
  741. }
  742. return username;
  743. }
  744. char *confdir()
  745. {
  746. static char confdir_buf[DIRMAX] = "";
  747. if (!confdir_buf || (confdir_buf && !confdir_buf[0])) {
  748. #ifdef LEAF
  749. egg_snprintf(confdir_buf, sizeof confdir_buf, "%s/.ssh", homedir());
  750. #endif /* LEAF */
  751. #ifdef HUB
  752. egg_snprintf(confdir_buf, sizeof confdir_buf, "%s", dirname(binname));
  753. #endif /* HUB */
  754. #ifdef CYGWIN_HACKS
  755. egg_snprintf(confdir_buf, sizeof confdir_buf, "%s", homedir());
  756. #endif /* CYGWIN_HACKS */
  757. }
  758. return confdir_buf;
  759. }
  760. char *my_uname()
  761. {
  762. static char os_uname[250] = "";
  763. if (!os_uname || (os_uname && !os_uname[0])) {
  764. char *unix_n = NULL, *vers_n = NULL;
  765. struct utsname un;
  766. if (uname(&un) < 0) {
  767. unix_n = "*unkown*";
  768. vers_n = "";
  769. } else {
  770. unix_n = un.nodename;
  771. #ifdef __FreeBSD__
  772. vers_n = un.release;
  773. #else /* __linux__ */
  774. vers_n = un.version;
  775. #endif /* __FreeBSD__ */
  776. }
  777. egg_snprintf(os_uname, sizeof os_uname, "%s %s", unix_n, vers_n);
  778. }
  779. return os_uname;
  780. }
  781. #ifndef CYGWIN_HACKS
  782. void check_crontab()
  783. {
  784. int i = 0;
  785. #ifdef LEAF
  786. if (!conf.bot->localhub)
  787. fatal("something is wrong.", 0);
  788. #endif /* LEAF */
  789. if (!(i = crontab_exists())) {
  790. crontab_create(5);
  791. if (!(i = crontab_exists()))
  792. printf("* Error writing crontab entry.\n");
  793. }
  794. }
  795. void crontab_del() {
  796. char *tmpFile = NULL, *p = NULL, buf[2048] = "";
  797. tmpFile = calloc(1, strlen(binname) + 100);
  798. strcpy(tmpFile, binname);
  799. if (!(p = strrchr(tmpFile, '/')))
  800. return;
  801. p++;
  802. strcpy(p, ".ctb");
  803. sprintf(buf, "crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\" > %s", binname, tmpFile);
  804. if (shell_exec(buf, NULL, NULL, NULL)) {
  805. sprintf(buf, "crontab %s", tmpFile);
  806. shell_exec(buf, NULL, NULL, NULL);
  807. }
  808. unlink(tmpFile);
  809. }
  810. int crontab_exists() {
  811. char buf[2048] = "", *out = NULL;
  812. egg_snprintf(buf, sizeof buf, "crontab -l | grep \"%s\" | grep -v \"^#\"", binname);
  813. if (shell_exec(buf, NULL, &out, NULL)) {
  814. if (out && strstr(out, binname)) {
  815. free(out);
  816. return 1;
  817. } else {
  818. if (out)
  819. free(out);
  820. return 0;
  821. }
  822. } else
  823. return (-1);
  824. }
  825. void crontab_create(int interval) {
  826. char tmpFile[161] = "", buf[256] = "";
  827. FILE *f = NULL;
  828. int fd;
  829. egg_snprintf(tmpFile, sizeof tmpFile, "%s.crontab-XXXXXX", tempdir);
  830. if ((fd = mkstemp(tmpFile)) == -1) {
  831. unlink(tmpFile);
  832. return;
  833. }
  834. egg_snprintf(buf, sizeof buf, "crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\"> %s", binname, tmpFile);
  835. if (shell_exec(buf, NULL, NULL, NULL) && (f = fdopen(fd, "a")) != NULL) {
  836. buf[0] = 0;
  837. if (interval == 1)
  838. strcpy(buf, "*");
  839. else {
  840. int i = 1;
  841. int si = randint(interval);
  842. while (i < 60) {
  843. if (buf[0])
  844. sprintf(buf + strlen(buf), ",%i", (i + si) % 60);
  845. else
  846. sprintf(buf, "%i", (i + si) % 60);
  847. i += interval;
  848. }
  849. }
  850. egg_snprintf(buf + strlen(buf), sizeof buf, " * * * * %s > /dev/null 2>&1", binname);
  851. fseek(f, 0, SEEK_END);
  852. fprintf(f, "\n%s\n", buf);
  853. fclose(f);
  854. sprintf(buf, "crontab %s", tmpFile);
  855. shell_exec(buf, NULL, NULL, NULL);
  856. }
  857. close(fd);
  858. unlink(tmpFile);
  859. }
  860. #endif /* !CYGWIN_HACKS */
  861. #ifdef CRAZY_TRACE
  862. /* This code will attach a ptrace() to getpid() hence blocking process hijackers/tracers on the pid
  863. * only problem.. it just creates a new pid to be traced/hijacked :\
  864. */
  865. int attached = 0;
  866. void crazy_trace()
  867. {
  868. int parent = getpid();
  869. int x = fork();
  870. if (x == -1) {
  871. printf("Can't fork(): %s\n", strerror(errno));
  872. } else if (x == 0) {
  873. /* child */
  874. int i;
  875. i = ptrace(PTRACE_ATTACH, parent, (char *) 1, 0);
  876. if (i == (-1) && errno == EPERM) {
  877. printf("CANT PTRACE PARENT: errno: %d %s, i: %d\n", errno, strerror(errno), i);
  878. waitpid(parent, &i, 0);
  879. kill(parent, SIGCHLD);
  880. ptrace(PTRACE_DETACH, parent, 0, 0);
  881. kill(parent, SIGCHLD);
  882. exit(0);
  883. } else {
  884. printf("SUCCESSFUL ATTACH to %d: %d\n", parent, i);
  885. attached++;
  886. }
  887. } else {
  888. /* parent */
  889. printf("wait()\n");
  890. wait(&x);
  891. }
  892. printf("end\n");
  893. }
  894. #endif /* CRAZY_TRACE */
  895. /* need to account for this after reading in conf/spawning bots.
  896. ifdef LEAF
  897. if (updating)
  898. exit(0); let cron restart us.
  899. } localhub
  900. endif LEAF
  901. */