R_API char *r_cons_hud_path(const char *path, int dir) { char *tmp = NULL, *ret = NULL; RList *files; if (path){ while (*path==' ') path++; tmp = (*path)? strdup(path): strdup ("./"); } else tmp = strdup ("./"); files = r_sys_dir (tmp); if (files) { ret = r_cons_hud (files, tmp); if (ret) { tmp = r_str_concat (tmp, "/"); tmp = r_str_concat (tmp, ret); ret = r_file_abspath (tmp); free (tmp); tmp = ret; if (r_file_is_directory (tmp)) { ret = r_cons_hud_path (tmp, dir); free (tmp); tmp = ret; } } r_list_free (files); } else eprintf ("No files found\n"); if (!ret) { free (tmp); return NULL; } return tmp; }
static char *projectScriptPath(RCore *core, const char *file) { const char *magic = "# r2 rdb project file"; char *data, *prjfile; if (r_file_is_abspath (file)) { prjfile = strdup (file); } else { if (!is_valid_project_name (file)) { return NULL; } prjfile = r_file_abspath (r_config_get ( core->config, "dir.projects")); prjfile = r_str_concat (prjfile, R_SYS_DIR); prjfile = r_str_concat (prjfile, file); if (!r_file_exists (prjfile) || r_file_is_directory (prjfile)) { prjfile = r_str_concat (prjfile, R_SYS_DIR "rc"); } } data = r_file_slurp (prjfile, NULL); if (data) { if (strncmp (data, magic, strlen (magic))) { R_FREE (prjfile); } } free (data); return prjfile; }
// TODO: support len for binary data? R_API char *r_core_rtr_cmds_query (RCore *core, const char *host, const char *port, const char *cmd) { int retries = 6; unsigned char buf[1024]; char *rbuf = NULL; const int timeout = 0; RSocket *s = r_socket_new (0); for (;retries>0; r_sys_usleep (10*1000)) { if (r_socket_connect (s, host, port, R_SOCKET_PROTO_TCP, timeout)) break; retries--; } if (retries>0) { rbuf = strdup (""); r_socket_write (s, (void*)cmd, strlen (cmd)); //r_socket_write (s, "px\n", 3); for (;;) { int ret = r_socket_read (s, buf, sizeof (buf)); if (ret<1) break; buf[ret] = 0; rbuf = r_str_concat (rbuf, (const char *)buf); } } else { eprintf ("Cannot connect\n"); } r_socket_free (s); return rbuf; }
R_API char *r_str_concatf(char *ptr, const char *fmt, ...) { int ret; char string[4096]; va_list ap; va_start (ap, fmt); ret = vsnprintf (string, sizeof (string), fmt, ap); if (ret>=sizeof (string)) { char *p = malloc (ret+2); if (!p) return NULL; vsnprintf (p, ret+1, fmt, ap); ptr = r_str_concat (ptr, p); free (p); } else ptr = r_str_concat (ptr, string); va_end (ap); return ptr; }
/* umf..this should probably be outside this file */ R_API char* r_anal_reflines_str(RAnal *anal, RAnalRefline *list, ut64 addr, int opts) { int l, linestyle = opts & R_ANAL_REFLINE_TYPE_STYLE; int dir = 0, wide = opts & R_ANAL_REFLINE_TYPE_WIDE; char ch = ' ', *str = NULL; struct list_head *pos; RAnalRefline *ref; if (!list) return NULL; str = r_str_concat (str, " "); for (pos = linestyle?(&(list->list))->next:(&(list->list))->prev; pos != (&(list->list)); pos = linestyle?pos->next:pos->prev) { ref = list_entry (pos, RAnalRefline, list); dir = (addr == ref->to)? 1: (addr == ref->from)? 2: dir; if (addr == ref->to) { str = r_str_concat (str, (ref->from>ref->to)?".":"`"); ch = '-'; } else if (addr == ref->from) { str = r_str_concat (str, (ref->from>ref->to)?"`":","); ch = '='; } else if (ref->from < ref->to) { if (addr > ref->from && addr < ref->to) { if (ch=='-'||ch=='=') str = r_str_concatch (str, ch); else str = r_str_concatch (str, '|'); } else str = r_str_concatch (str, ch); } else { if (addr < ref->from && addr > ref->to) { if (ch=='-'||ch=='=') str = r_str_concatch (str, ch); else str = r_str_concatch (str, '|'); } else str = r_str_concatch (str, ch); } if (wide) str = r_str_concatch (str, (ch=='='||ch=='-')?ch:' '); } str = r_str_concat (str, (dir==1)?"-> ":(dir==2)?"=< ":" "); if (anal->lineswidth>0) { l = strlen (str); if (l>anal->lineswidth) r_str_cpy (str, str+l-anal->lineswidth); } for (l = anal->lineswidth-strlen (str);l-->0;) str = r_str_prefix (str, " "); return str; }
R_API char *r_anal_value_to_string (RAnalValue *value) { char *out = NULL; if (value) { out = r_str_new (""); if (!value->base && !value->reg) { if (value->imm != -1LL) out = r_str_concatf (out, "0x%"PFMT64x, value->imm); else out = r_str_concat (out, "-1"); } else { if (value->memref) { switch (value->memref) { case 1: out = r_str_concat (out, "(char)"); break; case 2: out = r_str_concat (out, "(short)"); break; case 4: out = r_str_concat (out, "(word)"); break; case 8: out = r_str_concat (out, "(dword)"); break; } out = r_str_concat (out, "["); } if (value->mul) out = r_str_concatf (out, "%d*", value->mul); if (value->reg) out = r_str_concatf (out, "%s", value->reg->name); if (value->regdelta) out = r_str_concatf (out, "+%s", value->regdelta->name); if (value->base!=0) out = r_str_concatf (out, "0x%"PFMT64x, value->base); if (value->delta>0) out = r_str_concatf (out, "+0x%"PFMT64x, value->delta); else if (value->delta<0) out = r_str_concatf (out, "-0x%"PFMT64x, -value->delta); if (value->memref) out = r_str_concat (out, "]"); } } return out; }
R_API char *r_str_concatf(char *ptr, const char *fmt, ...) { char string[4096]; va_list ap; va_start (ap, fmt); vsnprintf (string, sizeof (string), fmt, ap); ptr = r_str_concat (ptr, string); va_end (ap); return ptr; }
static int r_core_is_project(RCore *core, const char *name) { int ret = 0; if (name && *name && *name!='.') { char *path = r_core_project_file (core, name); if (!path) return 0; path = r_str_concat (path, ".d"); if (r_file_is_directory (path)) ret = 1; free (path); } return ret; }
static char *r_core_project_file(RCore *core, const char *file) { const char *magic = "# r2 rdb project file"; char *data, *prjfile; //if (*file != R_SYS_DIR[0]) { if (r_file_is_abspath (file)) { prjfile = strdup (file); } else { if (!is_valid_project_name (file)) return NULL; prjfile = r_file_abspath (r_config_get ( core->config, "dir.projects")); prjfile = r_str_concat (prjfile, R_SYS_DIR); prjfile = r_str_concat (prjfile, file); } data = r_file_slurp (prjfile, NULL); if (data) { if (strncmp (data, magic, strlen (magic))) { R_FREE (prjfile); } } free (data); return prjfile; }
R_API int r_core_project_delete(RCore *core, const char *prjfile) { char *path; if (r_sandbox_enable (0)) { eprintf ("Cannot delete project in sandbox mode\n"); return 0; } path = r_core_project_file (core, prjfile); if (!path) { eprintf ("Invalid project name '%s'\n", prjfile); return false; } if (r_core_is_project (core, prjfile)) { // rm project file r_file_rm (path); eprintf ("rm %s\n", path); path = r_str_concat (path, ".d"); if (r_file_is_directory (path)) { char *f; RListIter *iter; RList *files = r_sys_dir (path); r_list_foreach (files, iter, f) { char *filepath = r_str_concat (strdup (path), R_SYS_DIR); filepath =r_str_concat (filepath, f); if (!r_file_is_directory (filepath)) { eprintf ("rm %s\n", filepath); r_file_rm (filepath); } free (filepath); } r_file_rm (path); eprintf ("rm %s\n", path); r_list_free (files); } // TODO: remove .d directory (BEWARE OF ROOT RIMRAFS!) // TODO: r_file_rmrf (path); }
- follow symlinks #endif static char *rtr_dir_files (const char *path) { char *ptr = strdup ("<html><body>\n"); const char *file; RListIter *iter; // list files RList *files = r_sys_dir (path); eprintf ("Listing directory %s\n", path); r_list_foreach (files, iter, file) { if (file[0] == '.') continue; ptr = r_str_concatf (ptr, "<a href=\"%s%s\">%s</a><br />\n", path, file, file); } r_list_free (files); return r_str_concat (ptr, "</body></html>\n"); }
static bool r_core_is_project(RCore *core, const char *name) { bool ret = false; if (name && *name && *name != '.') { char *path = projectScriptPath (core, name); if (!path) { return false; } if (r_str_endswith (path, "/rc") && r_file_exists (path)) { ret = true; } else { path = r_str_concat (path, ".d"); if (r_file_is_directory (path)) { ret = true; } } free (path); } return ret; }
R_API int r_reg_set_profile(RReg *reg, const char *profile) { int ret; char *base, *file; char *str = r_file_slurp (profile, NULL); if (!str) { base = r_sys_getenv (R_LIB_ENV); if (base) { file = r_str_concat (base, profile); str = r_file_slurp (file, NULL); free (file); } } if (!str) { eprintf ("r_reg_set_profile: Cannot find '%s'\n", profile); return false; } ret = r_reg_set_profile_string (reg, str); free (str); return ret; }
R_API int r_reg_set_profile(RReg *reg, const char *profile) { int ret; char *base, *file; char *str = r_file_slurp (profile, NULL); if (!str) { // XXX we must define this varname in r_lib.h /compiletime/ base = r_sys_getenv ("LIBR_PLUGINS"); if (base) { file = r_str_concat (base, profile); str = r_file_slurp (file, NULL); free (file); } } if (!str) { eprintf ("r_reg_set_profile: Cannot find '%s'\n", profile); return false; } ret = r_reg_set_profile_string (reg, str); free (str); return ret; }
R_API int r_sys_cmd_str_full(const char *cmd, const char *input, char **output, int *len, char **sterr) { char buffer[1024], *outputptr = NULL; char *inputptr = (char *)input; int pid, bytes = 0, status; int sh_in[2], sh_out[2], sh_err[2]; if (len) *len = 0; if (pipe (sh_in)) return R_FALSE; if (output) { if (pipe (sh_out)) { close (sh_in[0]); close (sh_in[1]); close (sh_out[0]); close (sh_out[1]); return R_FALSE; } } if (pipe (sh_err)) { close (sh_in[0]); close (sh_in[1]); return R_FALSE; } switch ((pid=fork ())) { case -1: return R_FALSE; case 0: dup2 (sh_in[0], 0); close (sh_in[0]); close (sh_in[1]); if (output) { dup2 (sh_out[1], 1); close (sh_out[0]); close (sh_out[1]); } if (sterr) dup2 (sh_err[1], 2); else close (2); close (sh_err[0]); close (sh_err[1]); exit (r_sandbox_system (cmd, 0)); default: outputptr = strdup (""); if (!outputptr) return R_FALSE; if (sterr) { *sterr = strdup (""); if (!*sterr) { free (outputptr); return R_FALSE; } } if (output) close (sh_out[1]); close (sh_err[1]); close (sh_in[0]); if (!inputptr || !*inputptr) close (sh_in[1]); for (;;) { fd_set rfds, wfds; int nfd; FD_ZERO (&rfds); FD_ZERO (&wfds); if (output) FD_SET (sh_out[0], &rfds); if (sterr) FD_SET (sh_err[0], &rfds); if (inputptr && *inputptr) FD_SET (sh_in[1], &wfds); memset (buffer, 0, sizeof (buffer)); nfd = select (sh_err[0] + 1, &rfds, &wfds, NULL, NULL); if (nfd < 0) break; if (output && FD_ISSET (sh_out[0], &rfds)) { if ((bytes = read (sh_out[0], buffer, sizeof (buffer)-1)) == 0) break; buffer[sizeof(buffer) - 1] = '\0'; if (len) *len += bytes; outputptr = r_str_concat (outputptr, buffer); } else if (FD_ISSET (sh_err[0], &rfds) && sterr) { if (read (sh_err[0], buffer, sizeof (buffer)-1) == 0) break; buffer[sizeof(buffer) - 1] = '\0'; *sterr = r_str_concat (*sterr, buffer); } else if (FD_ISSET (sh_in[1], &wfds) && inputptr && *inputptr) { bytes = write (sh_in[1], inputptr, strlen (inputptr)); inputptr += bytes; if (!*inputptr) { close (sh_in[1]); /* If neither stdout nor stderr should be captured, * abort now - nothing more to do for select(). */ if (!output && !sterr) break; } } } if (output) close (sh_out[0]); close (sh_err[0]); close (sh_in[1]); waitpid (pid, &status, 0); if (status != 0) { char *escmd = r_str_escape (cmd); eprintf ("%s: failed command '%s'\n", __func__, escmd); free (escmd); return R_FALSE; } if (output) *output = outputptr; else free (outputptr); return R_TRUE; } return R_FALSE; }
R_API char *r_str_concatch(char *x, char y) { char b[2] = {y, 0}; return r_str_concat (x,b); }
R_API char *r_str_concatlen(char *ptr, const char *string, int slen) { char *ret, *msg = r_str_newlen (string, slen); ret = r_str_concat (ptr, msg); free (msg); return ret; }
R_API int r_core_rtr_http(RCore *core, int launch, const char *path) { char buf[32]; RSocketHTTPRequest *rs; int iport, oldsandbox = -1; int timeout = r_config_get_i (core->config, "http.timeout"); int x = r_config_get_i (core->config, "scr.html"); int y = r_config_get_i (core->config, "scr.color"); int z = r_config_get_i (core->config, "asm.bytes"); int u = r_config_get_i (core->config, "scr.interactive"); int v = r_config_get_i (core->config, "asm.cmtright"); const char *port = r_config_get (core->config, "http.port"); char *allow = (char *)r_config_get (core->config, "http.allow"); if (core->http_up) { eprintf ("http server is already running\n"); return 1; } if (r_sandbox_enable (0)) { eprintf ("sandbox: connect disabled\n"); return 1; } if (path && atoi (path)) { port = path; path = NULL; } if (!strcmp (port, "0")) { r_num_irand (); iport = 1024+r_num_rand (45256); snprintf (buf, sizeof (buf), "%d", iport); port = buf; } s = r_socket_new (R_FALSE); s->local = !r_config_get_i (core->config, "http.public"); if (!r_socket_listen (s, port, NULL)) { eprintf ("Cannot listen on http.port\n"); return 1; } if (launch) { char cmd[128]; const char *browser = r_config_get (core->config, "http.browser"); snprintf (cmd, sizeof (cmd)-1, "%s http://localhost:%d/%s &", browser, atoi (port), path?path:""); r_sys_cmd (cmd); } r_config_set (core->config, "asm.cmtright", "false"); r_config_set (core->config, "scr.html", "true"); r_config_set (core->config, "scr.color", "false"); r_config_set (core->config, "asm.bytes", "false"); r_config_set (core->config, "scr.interactive", "false"); if (r_config_get_i (core->config, "http.sandbox")) { oldsandbox = r_config_get_i (core->config, "cfg.sandbox"); r_config_set (core->config, "cfg.sandbox", "true"); } eprintf ("Starting http server...\n"); eprintf ("http://localhost:%d/\n", atoi (port)); core->http_up = R_TRUE; while (!r_cons_singleton ()->breaked) { r_cons_break ((RConsBreak)http_break, core); rs = r_socket_http_accept (s, timeout); if (!rs) { if (!s) break; r_sys_usleep (100); continue; } if (allow && *allow) { int accepted = R_FALSE; const char *host; char *p, *peer = r_socket_to_string (rs->s); char *allows = strdup (allow); //eprintf ("Firewall (%s)\n", allows); int i, count = r_str_split (allows, ','); p = strchr (peer, ':'); if (p) *p = 0; for (i=0; i<count; i++) { host = r_str_word_get0 (allows, i); //eprintf ("--- (%s) (%s)\n", host, peer); if (!strcmp (host, peer)) { accepted = R_TRUE; break; } } free (peer); free (allows); if (!accepted) { r_socket_http_close (rs); continue; } } if (!rs->method || !rs->path) { eprintf ("Invalid http headers received from client\n"); r_socket_http_close (rs); continue; } char *dir = NULL; if (r_config_get_i (core->config, "http.dirlist")) if (r_file_is_directory (rs->path)) dir = strdup (rs->path); if (!strcmp (rs->method, "GET")) { if (!memcmp (rs->path, "/up", 3)) { if (r_config_get_i (core->config, "http.upget")) { const char *uproot = r_config_get (core->config, "http.uproot"); if (!rs->path[3] || (rs->path[3]=='/'&&!rs->path[4])) { char *ptr = rtr_dir_files (uproot); r_socket_http_response (rs, 200, ptr, 0, NULL); free (ptr); } else { char *path = r_file_root (uproot, rs->path + 4); if (r_file_exists (path)) { int sz = 0; char *f = r_file_slurp (path, &sz); if (f) { r_socket_http_response (rs, 200, f, sz, NULL); free (f); } else { r_socket_http_response (rs, 403, "Permission denied", 0, NULL); eprintf ("http: Cannot open '%s'\n", path); } } else { if (dir) { char *resp = rtr_dir_files (dir); r_socket_http_response (rs, 404, resp, 0, NULL); free (resp); } else { eprintf ("File '%s' not found\n", path); r_socket_http_response (rs, 404, "File not found\n", 0, NULL); } } free (path); } } else { r_socket_http_response (rs, 403, "Permission denied\n", 0, NULL); } } else if (!memcmp (rs->path, "/cmd/", 5)) { char *cmd = rs->path +5; char foo[32]; const char *httpcmd = r_config_get (core->config, "http.uri"); while (*cmd=='/') cmd++; if (httpcmd && *httpcmd) { int len; char *res; // do remote http query and proxy response snprintf (foo, sizeof (foo), "%s/%s", httpcmd, cmd); res = r_socket_http_get (foo, NULL, &len); if (res) { res[len]=0; r_cons_printf ("%s\n", res); } } else { char *out, *cmd = rs->path+5; r_str_uri_decode (cmd); // eprintf ("CMD (%s)\n", cmd); out = r_core_cmd_str_pipe (core, cmd); // eprintf ("\nOUT LEN = %d\n", strlen (out)); if (out) { char *res = r_str_uri_encode (out); r_socket_http_response (rs, 200, out, 0, "Content-Type: text/plain\n"); free (out); free (res); } else r_socket_http_response (rs, 200, "", 0, NULL); } } else { const char *root = r_config_get (core->config, "http.root"); char *path = r_file_root (root, rs->path); // FD IS OK HERE if (rs->path [strlen (rs->path)-1] == '/') { path = r_str_concat (path, "index.html"); //rs->path = r_str_concat (rs->path, "index.html"); } else { //snprintf (path, sizeof (path), "%s/%s", root, rs->path); if (r_file_is_directory (path)) { char res[128]; snprintf (res, sizeof (res), "Location: %s/\n", rs->path); r_socket_http_response (rs, 302, NULL, 0, res); r_socket_http_close (rs); free (path); free (dir); dir = NULL; continue; } } if (r_file_exists (path)) { int sz = 0; char *f = r_file_slurp (path, &sz); if (f) { const char *contenttype = NULL; if (strstr (path, ".js")) contenttype = "Content-Type: application/javascript\n"; if (strstr (path, ".css")) contenttype = "Content-Type: text/css\n"; if (strstr (path, ".html")) contenttype = "Content-Type: text/html\n"; r_socket_http_response (rs, 200, f, sz, contenttype); free (f); } else { r_socket_http_response (rs, 403, "Permission denied", 0, NULL); eprintf ("http: Cannot open '%s'\n", path); } } else { if (dir) { char *resp = rtr_dir_files (dir); eprintf ("Dirlisting %s\n", dir); r_socket_http_response (rs, 404, resp, 0, NULL); free (resp); } else { eprintf ("File '%s' not found\n", path); r_socket_http_response (rs, 404, "File not found\n", 0, NULL); } } free (path); } } else if (!strcmp (rs->method, "POST")) { ut8 *ret; int retlen; char buf[128]; if (r_config_get_i (core->config, "http.upload")) { ret = r_socket_http_handle_upload ( rs->data, rs->data_length, &retlen); if (ret) { ut64 size = r_config_get_i (core->config, "http.maxsize"); if (size && retlen > size) { r_socket_http_response (rs, 403, "403 File too big\n", 0, NULL); } else { char *filename = r_file_root ( r_config_get (core->config, "http.uproot"), rs->path + 4); eprintf ("UPLOADED '%s'\n", filename); r_file_dump (filename, ret, retlen); free (filename); snprintf (buf, sizeof (buf), "<html><body><h2>uploaded %d bytes. Thanks</h2>\n", retlen); r_socket_http_response (rs, 200, buf, 0, NULL); } free (ret); } } else { r_socket_http_response (rs, 403, "403 Forbidden\n", 0, NULL); } } else { r_socket_http_response (rs, 404, "Invalid protocol", 0, NULL); } r_socket_http_close (rs); free (dir); } core->http_up = R_FALSE; r_socket_free (s); r_cons_break_end (); r_config_set_i (core->config, "scr.html", x); r_config_set_i (core->config, "scr.color", y); r_config_set_i (core->config, "asm.bytes", z); r_config_set_i (core->config, "scr.interactive", u); r_config_set_i (core->config, "asm.cmtright", v); if (oldsandbox != -1) r_config_set_i (core->config, "cfg.sandbox", oldsandbox); return 0; }
// TODO: move into another file // TODO: this is TOO SLOW. do not iterate over all reflines or gtfo R_API char* r_anal_reflines_str(void *core, ut64 addr, int opts) { int l, linestyle = opts & R_ANAL_REFLINE_TYPE_STYLE; int dir = 0, wide = opts & R_ANAL_REFLINE_TYPE_WIDE; char ch = ' ', *str = NULL; struct list_head *pos; RAnalRefline *ref, *list = ((RCore*)core)->reflines; if (!list) return NULL; str = r_str_concat (str, " "); for (pos = linestyle?(&(list->list))->next:(&(list->list))->prev; pos != (&(list->list)); pos = linestyle?pos->next:pos->prev) { ref = list_entry (pos, RAnalRefline, list); dir = (addr == ref->to)? 1: (addr == ref->from)? 2: dir; if (addr == ref->to) { str = r_str_concat (str, (ref->from>ref->to)? "." : "`"); ch = '-'; } else if (addr == ref->from) { str = r_str_concat (str, (ref->from>ref->to)? "`" : "," ); ch = '='; } else if (ref->from < ref->to) { if (addr > ref->from && addr < ref->to) { if (ch=='-' || ch=='=') str = r_str_concatch (str, ch); //else str = r_str_concat (str, ((RCore*)core)->cons->vline[LINE_VERT]); else str = r_str_concatch (str, '|'); } else str = r_str_concatch (str, ch); } else { if (addr < ref->from && addr > ref->to) { if (ch=='-' || ch=='=') str = r_str_concatch (str, ch); //else str = r_str_concat (str, ((RCore*)core)->cons->vline[LINE_VERT]); else str = r_str_concatch (str, '|'); } else str = r_str_concatch (str, ch); } if (wide) str = r_str_concatch (str, (ch=='=' || ch=='-')? ch : ' '); } //str = r_str_concat (str, (dir==1)?"-> ":(dir==2)?"=< ":" "); str = r_str_concat (str, (dir==1)? "-> " :(dir==2)? "=< " : " "); if (((RCore*)core)->anal->lineswidth>0) { l = r_str_len_utf8 (str); if (l > ((RCore*)core)->anal->lineswidth) r_str_cpy (str, str + l - ((RCore*)core)->anal->lineswidth); } /* HACK */ if (((RCore*)core)->utf8 && ((RCore*)core)->cons->vline) { RCons *cons = ((RCore*)core)->cons; //str = r_str_replace (str, "=", "-", 1); str = r_str_replace (str, "<", cons->vline[ARROW_LEFT], 1); str = r_str_replace (str, ">", cons->vline[ARROW_RIGHT], 1); str = r_str_replace (str, "|", cons->vline[LINE_VERT], 1); str = r_str_replace (str, "=", cons->vline[LINE_HORIZ], 1); str = r_str_replace (str, "-", cons->vline[LINE_HORIZ], 1); //str = r_str_replace (str, ".", "\xe2\x94\x8c", 1); str = r_str_replace (str, ",", cons->vline[LUP_CORNER], 1); str = r_str_replace (str, ".", cons->vline[LUP_CORNER], 1); str = r_str_replace (str, "`", cons->vline[LDWN_CORNER], 1); } if (((RCore*)core)->anal->lineswidth>0) { char pfx[128]; int l = ((RCore*)core)->anal->lineswidth-r_str_len_utf8 (str); memset (pfx, ' ', sizeof (pfx)); if (l>=sizeof(pfx)) l = sizeof (pfx)-1; pfx[l] = 0; str = r_str_prefix (str, pfx); } return str; }
static char *r_core_project_file(RCore *core, const char *file) { char *ret = r_file_abspath (r_config_get ( core->config, "dir.projects")); ret = r_str_concat (ret, "/"); return r_str_concat (ret, file); }
- follow symlinks #endif R_API int r_core_rtr_http(RCore *core, int launch, const char *path) { RSocketHTTPRequest *rs; int oldsandbox = -1; int timeout = r_config_get_i (core->config, "http.timeout"); int x = r_config_get_i (core->config, "scr.html"); int y = r_config_get_i (core->config, "scr.color"); int z = r_config_get_i (core->config, "asm.bytes"); int u = r_config_get_i (core->config, "scr.interactive"); int v = r_config_get_i (core->config, "asm.cmtright"); const char *port = r_config_get (core->config, "http.port"); if (r_sandbox_enable (0)) { eprintf ("sandbox: connect disabled\n"); return 1; } s = r_socket_new (R_FALSE); s->local = !r_config_get_i (core->config, "http.public"); if (!r_socket_listen (s, port, NULL)) { eprintf ("Cannot listen on http.port\n"); return 1; } if (launch) { char cmd[128]; const char *browser = r_config_get (core->config, "http.browser"); snprintf (cmd, sizeof (cmd)-1, "%s http://localhost:%d/%s", browser, atoi (port), path?path:""); r_sys_cmd (cmd); } r_config_set (core->config, "asm.cmtright", "false"); r_config_set (core->config, "scr.html", "true"); r_config_set (core->config, "scr.color", "false"); r_config_set (core->config, "asm.bytes", "false"); r_config_set (core->config, "scr.interactive", "false"); if (r_config_get_i (core->config, "http.sandbox")) { oldsandbox = r_config_get_i (core->config, "cfg.sandbox"); r_config_set (core->config, "cfg.sandbox", "true"); } eprintf ("Starting http server...\n"); eprintf ("http://localhost:%d/\n", atoi (port)); while (!r_cons_singleton ()->breaked) { r_cons_break (http_break, core); rs = r_socket_http_accept (s, timeout); if (!rs) { if (!s) break; r_sys_usleep (200); continue; } if (!rs->method || !rs->path) { r_socket_http_close (rs); continue; } if (!strcmp (rs->method, "GET")) { if (!memcmp (rs->path, "/up", 3)) { if (r_config_get_i (core->config, "http.upget")) { const char *uproot = r_config_get (core->config, "http.uproot"); if (!rs->path[3] || (rs->path[3]=='/'&&!rs->path[4])) { char *ptr = strdup ("<html><body>\n"); const char *file; RListIter *iter; // list files RList *files = r_sys_dir (uproot); eprintf ("Listing directory %s\n", uproot); r_list_foreach (files, iter, file) { if (file[0] == '.') continue; ptr = r_str_concatf (ptr, "<a href=\"/up/%s\">%s</a><br />\n", file, file); } r_list_free (files); ptr = r_str_concat (ptr, "<html><body>\n"); r_socket_http_response (rs, 200, ptr, 0, NULL); } else { char *path = r_file_root (uproot, rs->path + 4); if (r_file_exists (path)) { int sz = 0; char *f = r_file_slurp (path, &sz); if (f) { r_socket_http_response (rs, 200, f, sz, NULL); free (f); } else { r_socket_http_response (rs, 403, "Permission denied", 0, NULL); eprintf ("http: Cannot open '%s'\n", path); } } else { eprintf ("File '%s' not found\n", path); r_socket_http_response (rs, 404, "File not found\n", 0, NULL); } free (path); } } else { r_socket_http_response (rs, 403, "Permission denied\n", 0, NULL); } } else if (!memcmp (rs->path, "/cmd/", 5)) {