static RIODesc *haret__open(struct r_io_t *io, const char *pathname, int rw, int mode) { char *port, *ptr, buf[1024]; RSocket *s; strncpy (buf, pathname, sizeof (buf)-1); if (haret__plugin_open (io, pathname, 0)) { ptr = buf + 8; if (!(port = strchr (ptr, ':'))) { eprintf ("haret: wrong url\n"); return NULL; } if (!r_sandbox_enable (0)) { eprintf ("sandbox: cannot use network\n"); return NULL; } *port++ = 0; if ((s = r_socket_new (R_FALSE)) == NULL) { eprintf ("Cannot create new socket\n"); return NULL; } if (!r_socket_connect_tcp (s, ptr, port, 30)) { eprintf ("Cannot connect to '%s' (%s)\n", ptr, port); return NULL; } else eprintf ("Connected to: %s at port %s\n", ptr, port); haret_wait_until_prompt (s); //return r_io_desc_new (&r_io_plugin_haret, s->fd, pathname, rw, mode, (void*)s); RETURN_IO_DESC_NEW (&r_io_plugin_haret, s->fd, pathname, rw, mode, (void*)s); } return NULL; }
int qnxr_connect (libqnxr_t *g, const char *host, int port) { char tmp[255]; int ret; if (!g || !host || g->connected) return -1; g->data_len = 0; g->read_len = 0; g->read_ptr = 0; g->sock = r_socket_new (0); g->connected = 0; g->mid = 0; memmove (g->host, host, strlen (host) + 1); g->port = port; ret = snprintf (tmp, sizeof (tmp) - 1, "%d", port); if (!ret) return -1; ret = r_socket_connect_tcp (g->sock, host, tmp, 200); if (!ret) return -1; g->connected = 1; qnxr_send_ch_reset (g); nto_send_init (g, DStMsg_connect, 0, SET_CHANNEL_DEBUG); g->tran.pkt.connect.major = HOST_QNX_PROTOVER_MAJOR; g->tran.pkt.connect.minor = HOST_QNX_PROTOVER_MINOR; nto_send (g, sizeof (g->tran.pkt.connect), 0); if (g->recv.pkt.hdr.cmd == DSrMsg_err) { eprintf ("%s: connection failed: %lld\n", __func__, EXTRACT_SIGNED_INTEGER (&g->recv.pkt.err.err, 4)); return -1; } /* Try to query pdebug for their version of the protocol */ nto_send_init (g, DStMsg_protover, 0, SET_CHANNEL_DEBUG); g->tran.pkt.protover.major = HOST_QNX_PROTOVER_MAJOR; g->tran.pkt.protover.minor = HOST_QNX_PROTOVER_MINOR; nto_send (g, sizeof (g->tran.pkt.protover), 0); if ((g->recv.pkt.hdr.cmd == DSrMsg_err) && (EXTRACT_SIGNED_INTEGER (&g->recv.pkt.err.err, 4) == EINVAL)) { g->target_proto_major = 0; g->target_proto_minor = 0; } else if (g->recv.pkt.hdr.cmd == DSrMsg_okstatus) { g->target_proto_major = EXTRACT_SIGNED_INTEGER (&g->recv.pkt.okstatus.status, 4); g->target_proto_minor = EXTRACT_SIGNED_INTEGER (&g->recv.pkt.okstatus.status, 4); g->target_proto_major = (g->target_proto_major >> 8) & DSMSG_PROTOVER_MAJOR; g->target_proto_minor = g->target_proto_minor & DSMSG_PROTOVER_MINOR; } else {
R_API char *r_socket_http_post (const char *url, const char *data, int *code, int *rlen) { RSocket *s; int ssl = !memcmp (url, "https://", 8); char *response, *host, *path, *port = "80"; char *uri = strdup (url); if (!uri) return NULL; host = strstr (uri, "://"); if (!host) { free (uri); printf ("Invalid URI"); return NULL; } host += 3; port = strchr (host, ':'); if (!port) port = (ssl)?"443":"80"; else *port++ = 0; path = strchr (host, '/'); if (!path) path = ""; else *path++ = 0; s = r_socket_new (ssl); if (!s) { printf ("Cannot create socket\n"); free (uri); return NULL; } if (!r_socket_connect_tcp (s, host, port, 0)) { eprintf ("Cannot connect to %s:%s\n", host, port); free (uri); return NULL; } /* Send */ r_socket_printf (s, "POST /%s HTTP/1.0\r\n" "User-Agent: radare2 "R2_VERSION"\r\n" "Accept: */*\r\n" "Host: %s\r\n" "Content-Length: %i\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "\r\n", path, host, strlen (data)); r_socket_write (s, (void *)data, strlen (data)); response = r_socket_http_answer (s, code, rlen); free (uri); return response; }
R_API char *r_socket_http_get (const char *url, int *code, int *rlen) { RSocket *s; int ssl = !memcmp (url, "https://", 8); char *response, *host, *path, *port = "80"; char *uri = strdup (url); if (!uri) return NULL; if (code) *code = 0; if (rlen) *rlen = 0; host = strstr (uri, "://"); if (!host) { free (uri); eprintf ("r_socket_http_get: Invalid URI"); return NULL; } host += 3; port = strchr (host, ':'); if (!port) { port = (ssl)?"443":"80"; path = host; } else { *port++ = 0; path = port; } path = strchr (path, '/'); if (!path) path = ""; else *path++ = 0; s = r_socket_new (ssl); if (!s) { eprintf ("r_socket_http_get: Cannot create socket\n"); free (uri); return NULL; } if (r_socket_connect_tcp (s, host, port, 0)) { r_socket_printf (s, "GET /%s HTTP/1.1\r\n" "User-Agent: radare2 "R2_VERSION"\r\n" "Accept: */*\r\n" "Host: %s:%s\r\n" "\r\n", path, host, port); response = r_socket_http_answer (s, code, rlen); } else { eprintf ("Cannot connect to %s:%s\n", host, port); response = NULL; } free (uri); r_socket_free (s); return response; }
int gdbr_connect(libgdbr_t* g, const char* host, int port) { int ret; char tmp[255]; ret = snprintf (tmp, 255, "%d", port); if (!ret) return -1; ret = r_socket_connect_tcp (g->sock, host, tmp, 200); if (!ret) return -1; g->connected = 1; // TODO add config possibility here char* message = "qSupported:multiprocess+;qRelocInsn+"; ret = send_command(g, message); if (ret < 0) return ret; read_packet(g); return handle_connect(g); }
int main (int argc, char ** argv) { ut8 buf [MAX_LINE+1]; memset (buf, 0, MAX_LINE+1); RSocket *s = r_socket_new (true); if (s == NULL) { fprintf (stderr, "Error, cannot create new socket \n"); return 1; } if (!r_socket_connect_tcp (s, SERVER, PORT)) { fprintf (stderr, "Error, cannot connect to "SERVER"\n"); return 1; } printf ("%i\n",r_socket_puts (s, "GET /\r\n\r\n")); while(r_socket_read (s, buf, MAX_LINE)>0) printf ("%s", buf); r_socket_free (s); return 0; }
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) { char host[128], *port, *p; RSocket *_fd; RIOGdb *riog; if (!__plugin_open (io, file, 0)) return NULL; strncpy (host, file+6, sizeof (host)-1); port = strchr (host , ':'); if (!port) { eprintf ("Port not specified. Please use gdb://[host]:[port]\n"); return NULL; } *port = '\0'; port++; p = strchr (port, '/'); if (p) *p=0; if (r_sandbox_enable (0)) { eprintf ("sandbox: Cannot use network\n"); return NULL; } _fd = r_socket_new (R_FALSE); if (_fd && r_socket_connect_tcp (_fd, host, port, 3)) { riog = R_NEW (RIOGdb); riog->fd = _fd; riog->desc = gdbwrap_init (_fd->fd, NUM_REGS, 4); if (!riog->desc) { r_socket_free (_fd); free (riog); return NULL; } #if __WINDOWS__ // XXX: bypass lazylinking RETURN_IO_DESC_NEW (&r_io_plugin_gdb, _fd->fd, file, rw, mode, riog); #else return r_io_desc_new (&r_io_plugin_gdb, _fd->fd, file, rw, mode, riog); #endif } eprintf ("gdb.io.open: Cannot connect to host.\n"); return NULL; }
static RIODesc *rap__open(RIO *io, const char *pathname, int rw, int mode) { int i, p, listenmode; char *file, *port; const char *ptr; RSocket *rap_fd; char buf[1024]; RIORap *rior; if (!rap__plugin_open (io, pathname, 0)) { return NULL; } bool is_ssl = (!strncmp (pathname, "raps://", 7)); ptr = pathname + (is_ssl? 7: 6); if (!(port = strchr (ptr, ':'))) { eprintf ("rap: wrong uri\n"); return NULL; } listenmode = (*ptr == ':'); *port++ = 0; if (!*port) { return NULL; } p = atoi (port); if ((file = strchr (port + 1, '/'))) { *file = 0; file++; } if (r_sandbox_enable (0)) { eprintf ("sandbox: Cannot use network\n"); return NULL; } if (listenmode) { if (p <= 0) { eprintf ("rap: cannot listen here. Try rap://:9999\n"); return NULL; } //TODO: Handle ^C signal (SIGINT, exit); // ??? eprintf ("rap: listening at port %s ssl %s\n", port, (is_ssl)?"on":"off"); rior = R_NEW0 (RIORap); rior->listener = true; rior->client = rior->fd = r_socket_new (is_ssl); if (!rior->fd) { free (rior); return NULL; } if (is_ssl) { if (file && *file) { if (!r_socket_listen (rior->fd, port, file)) { free (rior); return NULL; } } else { free (rior); return NULL; } } else { if (!r_socket_listen (rior->fd, port, NULL)) { return NULL; } } return r_io_desc_new (&r_io_plugin_rap, rior->fd->fd, pathname, rw, mode, rior); } if (!(rap_fd = r_socket_new (is_ssl))) { eprintf ("Cannot create new socket\n"); return NULL; } if (r_socket_connect_tcp (rap_fd, ptr, port, 30) == false) { eprintf ("Cannot connect to '%s' (%d)\n", ptr, p); r_socket_free (rap_fd); return NULL; } eprintf ("Connected to: %s at port %s\n", ptr, port); rior = R_NEW0 (RIORap); rior->listener = false; rior->client = rior->fd = rap_fd; if (file && *file) { // send buf[0] = RMT_OPEN; buf[1] = rw; buf[2] = (ut8)strlen (file); memcpy (buf + 3, file, buf[2]); r_socket_write (rap_fd, buf, buf[2] + 3); r_socket_flush (rap_fd); // read eprintf ("waiting... "); buf[0] = 0; r_socket_read_block (rap_fd, (ut8*)buf, 5); if (buf[0] != (char)(RMT_OPEN | RMT_REPLY)) { eprintf ("rap: Expecting OPEN|REPLY packet. got %02x\n", buf[0]); r_socket_free (rap_fd); free (rior); return NULL; } i = r_read_at_be32 (buf, 1); if (i > 0) { eprintf ("ok\n"); } #if 0 /* Read meta info */ r_socket_read (rap_fd, (ut8 *)&buf, 4); r_mem_copyendian ((ut8 *)&i, (ut8*)buf, 4, ENDIAN); while (i>0) { int n = r_socket_read (rap_fd, (ut8 *)&buf, i); if (n<1) break; buf[i] = 0; io->core_cmd_cb (io->user, buf); n = r_socket_read (rap_fd, (ut8 *)&buf, 4); if (n<1) break; r_mem_copyendian ((ut8 *)&i, (ut8*)buf, 4, ENDIAN); i -= n; } #endif } else { // r_socket_free (rap_fd); // free (rior); //return NULL; } //r_socket_free (rap_fd); return r_io_desc_new (&r_io_plugin_rap, rior->fd->fd, pathname, rw, mode, rior); }
int gdbr_connect(libgdbr_t *g, const char *host, int port) { const char *message = "qSupported:multiprocess+;qRelocInsn+;xmlRegisters=i386"; RStrBuf tmp; r_strbuf_init (&tmp); int ret; if (!g || !host) { return -1; } // Initial max_packet_size for remote target (minimum so far for AVR = 64) g->stub_features.pkt_sz = 64; char *env_pktsz_str; ut32 env_pktsz; if ((env_pktsz_str = getenv ("R2_GDB_PKTSZ"))) { if ((env_pktsz = (ut32) strtoul (env_pktsz_str, NULL, 10))) { g->stub_features.pkt_sz = R_MAX (env_pktsz, 64); } } ret = snprintf (tmp.buf, sizeof (tmp.buf) - 1, "%d", port); if (!ret) { return -1; } if (*host == '/') { ret = r_socket_connect_serial (g->sock, host, port, 1); } else { ret = r_socket_connect_tcp (g->sock, host, tmp.buf, 200); } if (!ret) { return -1; } if (send_ack (g) < 0) { return -1; } read_packet (g); g->connected = 1; // TODO add config possibility here ret = send_msg (g, message); if (ret < 0) { return ret; } read_packet (g); ret = handle_qSupported (g); if (ret < 0) { return ret; } if (env_pktsz > 0) { g->stub_features.pkt_sz = R_MAX (R_MIN (env_pktsz, g->stub_features.pkt_sz), 64); } // If no-ack supported, enable no-ack mode (should speed up things) if (g->stub_features.QStartNoAckMode) { if (send_msg (g, "QStartNoAckMode") < 0) { return -1; } read_packet (g); if (!strncmp (g->data, "OK", 2)) { // Just in case, send ack send_ack (g); g->no_ack = true; } } if (g->remote_type == GDB_REMOTE_TYPE_LLDB) { return gdbr_connect_lldb (g); } // Query the thread / process id g->stub_features.qC = true; g->pid = g->tid = 0; ret = send_msg (g, "qC"); if (ret < 0) { return ret; } read_packet (g); ret = handle_qC (g); if (ret < 0) { g->stub_features.qC = false; } // Check if vCont is supported gdbr_check_vcont (g); // Set pid/thread for operations other than "step" and "continue" if (g->stub_features.multiprocess) { snprintf (tmp.buf, sizeof (tmp.buf) - 1, "Hgp%x.0", (ut32) g->pid); #if 0 if (g->tid < 0) { snprintf (tmp.buf, sizeof (tmp.buf) - 1, "Hgp%x.-1", (ut32) g->pid); } else { snprintf (tmp.buf, sizeof (tmp.buf) - 1, "Hgp%x.%x", (ut32) g->pid, (ut32) g->tid); } #endif } else { snprintf (tmp.buf, sizeof (tmp.buf) - 1, "Hg0"); #if 0 if (g->tid < 0) { snprintf (tmp.buf, sizeof (tmp.buf) - 1, "Hg-1"); } else { snprintf (tmp.buf, sizeof (tmp.buf) - 1, "Hg%x", (ut32) g->tid); } #endif } ret = send_msg (g, tmp.buf); if (ret < 0) { return ret; } read_packet (g); ret = send_ack (g); if (strncmp (g->data, "OK", 2)) { // return -1; } // Set thread for "step" and "continue" operations snprintf (tmp.buf, sizeof (tmp.buf) - 1, "Hc-1"); ret = send_msg (g, tmp.buf); if (ret < 0) { return ret; } read_packet (g); ret = send_ack (g); if (strncmp (g->data, "OK", 2)) { // return -1; } if (g->stub_features.qXfer_features_read) { gdbr_read_target_xml (g); } reg_cache_init (g); return ret; }
static int runfile () { if (!_program && !_system) { printf ("No program or system rule defined\n"); return 1; } if (_stdin) { int f = open (_stdin, O_RDONLY); close (0); dup2 (f, 0); } if (_stdout) { int f = open (_stdout, O_WRONLY); close (1); dup2 (f, 1); } if (_stderr) { int f = open (_stderr, O_WRONLY); close (2); dup2 (f, 2); } if (_connect) { char *p = strchr (_connect, ':'); if (p) { RSocket *fd = r_socket_new (0); *p=0; if (!r_socket_connect_tcp (fd, _connect, p+1, 30)) { eprintf ("Cannot connect\n"); return 1; } eprintf ("connected\n"); close (0); close (1); close (2); dup2 (fd->fd, 0); dup2 (fd->fd, 1); dup2 (fd->fd, 2); } else { eprintf ("Invalid format for connect. missing ':'\n"); return 1; } } if (_listen) { RSocket *child, *fd = r_socket_new (0); if (!r_socket_listen (fd, _listen, NULL)) { eprintf ("rarun2: cannot listen\n"); return 1; } child = r_socket_accept (fd); if (child) { eprintf ("connected\n"); close (0); close (1); close (2); dup2 (child->fd, 0); dup2 (child->fd, 1); dup2 (child->fd, 2); } } if (_chgdir) chdir (_chgdir); if (_chroot) chdir (_chroot); #if __UNIX__ if (_chroot) { if (chroot (".")) { eprintf ("rarun2: cannot chroot\n"); return 1; } } if (_setuid) setuid (atoi (_setuid)); if (_seteuid) seteuid (atoi (_seteuid)); if (_setgid) setgid (atoi (_setgid)); if (_input) { int f2[2]; pipe (f2); close (0); dup2 (f2[0], 0); parseinput (_input); write (f2[1], _input, strlen (_input)); } #endif if (_r2preload) { if (_preload) { eprintf ("WARNING: Only one library can be opened at a time\n"); } _preload = R2_LIBDIR"/libr2."R_LIB_EXT; } if (_libpath) { #if __WINDOWS__ eprintf ("rarun2: libpath unsupported for this platform\n"); #elif __APPLE__ r_sys_setenv ("DYLD_LIBRARY_PATH", _libpath); #else r_sys_setenv ("LD_LIBRARY_PATH", _libpath); #endif } if (_preload) { #if __APPLE__ // 10.6 r_sys_setenv ("DYLD_PRELOAD", _preload); r_sys_setenv ("DYLD_INSERT_LIBRARIES", _preload); // 10.8 r_sys_setenv ("DYLD_FORCE_FLAT_NAMESPACE", "1"); #else r_sys_setenv ("LD_PRELOAD", _preload); #endif } if (_timeout) { #if __UNIX__ int mypid = getpid (); if (!fork ()) { sleep (_timeout); if (!kill (mypid, 0)) eprintf ("\nrarun2: Interrupted by timeout\n"); kill (mypid, SIGKILL); exit (0); } #else eprintf ("timeout not supported for this platform\n"); #endif } if (_system) { exit (r_sys_cmd (_system)); } if (!r_file_exists (_program)) { eprintf ("rarun2: %s: file not found\n", _program); return 1; } exit (execv (_program, _args)); }
R_API int r_run_start(RRunProfile *p) { #if __APPLE__ posix_spawnattr_t attr = {0}; pid_t pid = -1; #endif int ret; if (!p->_program && !p->_system) { printf ("No program or system rule defined\n"); return 1; } if (p->_stdin) { int f = open (p->_stdin, O_RDONLY); if (f < 0) return 1; close (0); dup2 (f, 0); } if (p->_stdout) { int f = open (p->_stdout, O_WRONLY); if (f < 0) return 1; close (1); dup2 (f, 1); } if (p->_stderr) { int f = open (p->_stderr, O_WRONLY); if (f < 0) return 1; close (2); dup2 (f, 2); } if (p->_aslr != -1) setASLR (p->_aslr); #if __UNIX__ set_limit (p->_docore, RLIMIT_CORE, RLIM_INFINITY); if (p->_maxfd) set_limit (p->_maxfd, RLIMIT_NOFILE, p->_maxfd); #ifdef RLIMIT_NPROC if (p->_maxproc) set_limit (p->_maxproc, RLIMIT_NPROC, p->_maxproc); #endif if (p->_maxstack) set_limit (p->_maxstack, RLIMIT_STACK, p->_maxstack); #else if (p->_docore || p->_maxfd || p->_maxproc || p->_maxstack) eprintf ("Warning: setrlimits not supported for this platform\n"); #endif if (p->_connect) { char *q = strchr (p->_connect, ':'); if (q) { RSocket *fd = r_socket_new (0); *q = 0; if (!r_socket_connect_tcp (fd, p->_connect, q+1, 30)) { eprintf ("Cannot connect\n"); return 1; } eprintf ("connected\n"); close (0); close (1); close (2); dup2 (fd->fd, 0); dup2 (fd->fd, 1); dup2 (fd->fd, 2); } else { eprintf ("Invalid format for connect. missing ':'\n"); return 1; } } if (p->_listen) { RSocket *child, *fd = r_socket_new (0); if (!r_socket_listen (fd, p->_listen, NULL)) { eprintf ("rarun2: cannot listen\n"); r_socket_free (fd); return 1; } child = r_socket_accept (fd); if (child) { eprintf ("connected\n"); close (0); close (1); close (2); dup2 (child->fd, 0); dup2 (child->fd, 1); dup2 (child->fd, 2); } } if (p->_r2sleep != 0) { r_sys_sleep (p->_r2sleep); } if (p->_chgdir) { ret = chdir (p->_chgdir); if (ret < 0) return 1; } if (p->_chroot) { ret = chdir (p->_chroot); if (ret < 0) return 1; } #if __UNIX__ if (p->_chroot) { if (chroot (p->_chroot)) { eprintf ("rarun2: cannot chroot\n"); return 1; } chdir("/"); } if (p->_setuid) { ret = setgroups(0, NULL); if (ret < 0) return 1; ret = setuid (atoi (p->_setuid)); if (ret < 0) return 1; } if (p->_seteuid) { ret = seteuid (atoi (p->_seteuid)); if (ret < 0) return 1; } if (p->_setgid) { ret = setgid (atoi (p->_setgid)); if (ret < 0) return 1; } if (p->_input) { int f2[2]; pipe (f2); close (0); dup2 (f2[0], 0); parseinput (p->_input); write (f2[1], p->_input, strlen (p->_input)); } #endif if (p->_r2preload) { if (p->_preload) { eprintf ("WARNING: Only one library can be opened at a time\n"); } p->_preload = R2_LIBDIR"/libr2."R_LIB_EXT; } if (p->_libpath) { #if __WINDOWS__ eprintf ("rarun2: libpath unsupported for this platform\n"); #elif __HAIKU__ r_sys_setenv ("LIBRARY_PATH", p->_libpath); #elif __APPLE__ r_sys_setenv ("DYLD_LIBRARY_PATH", p->_libpath); #else r_sys_setenv ("LD_LIBRARY_PATH", p->_libpath); #endif } if (p->_preload) { #if __APPLE__ // 10.6 r_sys_setenv ("DYLD_PRELOAD", p->_preload); r_sys_setenv ("DYLD_INSERT_LIBRARIES", p->_preload); // 10.8 r_sys_setenv ("DYLD_FORCE_FLAT_NAMESPACE", "1"); #else r_sys_setenv ("LD_PRELOAD", p->_preload); #endif } if (p->_timeout) { #if __UNIX__ int mypid = getpid (); if (!fork ()) { sleep (p->_timeout); if (!kill (mypid, 0)) eprintf ("\nrarun2: Interrupted by timeout\n"); kill (mypid, SIGKILL); exit (0); } #else eprintf ("timeout not supported for this platform\n"); #endif } #if __APPLE__ posix_spawnattr_init (&attr); if (p->_args[0]) { char **envp = r_sys_get_environ(); ut32 spflags = 0; //POSIX_SPAWN_START_SUSPENDED; spflags |= POSIX_SPAWN_SETEXEC; if (p->_aslr == 0) { #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 spflags |= _POSIX_SPAWN_DISABLE_ASLR; } (void)posix_spawnattr_setflags (&attr, spflags); if (p->_bits) { size_t copied = 1; cpu_type_t cpu; #if __i386__ || __x86_64__ cpu = CPU_TYPE_I386; if (p->_bits == 64) cpu |= CPU_ARCH_ABI64; #else cpu = CPU_TYPE_ANY; #endif posix_spawnattr_setbinpref_np ( &attr, 1, &cpu, &copied); } ret = posix_spawnp (&pid, p->_args[0], NULL, &attr, p->_args, envp); switch (ret) { case 0: break; case 22: eprintf ("posix_spawnp: Invalid argument\n"); break; case 86: eprintf ("posix_spawnp: Unsupported architecture\n"); break; default: eprintf ("posix_spawnp: unknown error %d\n", ret); perror ("posix_spawnp"); break; } exit (ret); } #endif if (p->_system) { if (p->_pid) { eprintf ("PID: Cannot determine pid with 'system' directive. Use 'program'.\n"); } exit (r_sys_cmd (p->_system)); } if (p->_program) { if (!r_file_exists (p->_program)) { char *progpath = r_file_path (p->_program); if (progpath && *progpath) { free (p->_program); p->_program = progpath; } else { free (progpath); eprintf ("rarun2: %s: file not found\n", p->_program); return 1; } } #if __UNIX__ // XXX HACK close all non-tty fds { int i; for (i=3; i<10; i++) close (i); } // TODO: use posix_spawn if (p->_setgid) { ret = setgid (atoi (p->_setgid)); if (ret < 0) return 1; } if (p->_pid) { eprintf ("PID: %d\n", getpid ()); } if (p->_pidfile) { char pidstr[32]; snprintf (pidstr, sizeof (pidstr), "%d\n", getpid ()); r_file_dump (p->_pidfile, (const ut8*)pidstr, strlen (pidstr), 0); } #endif if (p->_nice) { #if __UNIX__ errno = 0; ret = nice(p->_nice); if (ret == -1) { if (errno != 0) { return 1; } } #else eprintf ("nice not supported for this platform\n"); #endif } exit (execv (p->_program, (char* const*)p->_args)); } return 0; }
R_API void r_core_rtr_add(RCore *core, const char *_input) { char *port, input[1024], *host = NULL, *file = NULL, *ptr = NULL, buf[1024]; int proto, i; RSocket *fd; strncpy (input, _input, sizeof (input)-4); /* Parse uri */ if ((ptr = strstr(input, "tcp://"))) { proto = RTR_PROT_TCP; host = ptr+6; } else if ((ptr = strstr(input, "udp://"))) { proto = RTR_PROT_UDP; host = ptr+6; } else if ((ptr = strstr(input, "rap://"))) { proto = RTR_PROT_RAP; host = ptr+6; } else { proto = RTR_PROT_RAP; host = input; } while (*host&&iswhitechar(*host)) host++; if (!(ptr = strchr (host, ':'))) { eprintf ("Error: Port is not specified\n"); return; } ptr[0] = '\0'; ptr = ptr+1; if (!(file = strchr (ptr, '/'))) { eprintf("Error: Missing '/'\n"); return; } file[0] = '\0'; file = file+1; port = ptr; fd = r_socket_new (R_FALSE); if (!fd) { eprintf ("Error: Cannot create new socket\n"); return; } switch (proto) { case RTR_PROT_RAP: if (!r_socket_connect_tcp (fd, host, port)) { //TODO: Use rap.ssl eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port); return; } eprintf ("Connected to: %s at port %s\n", host, port); /* send */ buf[0] = RTR_RAP_OPEN; buf[1] = 0; buf[2] = (ut8)(strlen (file)+1); memcpy (buf+3, file, buf[2]); r_socket_write(fd, buf, 3+buf[2]); /* read */ eprintf ("waiting... "); fflush(stdout); r_socket_read (fd, (ut8*)buf, 5); r_mem_copyendian ((ut8 *)&i, (ut8*)buf+1, 4, core->assembler->big_endian); if (buf[0] != (char)(RTR_RAP_OPEN|RTR_RAP_REPLY) || i<= 0) { eprintf ("Error: Wrong reply\n"); return; } eprintf ("ok\n"); break; case RTR_PROT_TCP: if (!r_socket_connect_tcp (fd, host, port)) { //TODO: Use rap.ssl eprintf("Error: Cannot connect to '%s' (%s)\n", host, port); return; } eprintf ("Connected to: %s at port %s\n", host, port); break; case RTR_PROT_UDP: if (!r_socket_connect_udp(fd, host, port)) { //TODO: Use rap.ssl eprintf("Error: Cannot connect to '%s' (%s)\n", host, port); return; } eprintf("Connected to: %s at port %s\n", host, port); break; } for (i = 0; i < RTR_MAX_HOSTS; i++) if (!rtr_host[i].fd) { rtr_host[i].proto = proto; memcpy (rtr_host[i].host, host, 512); rtr_host[i].port = atoi(port); memcpy (rtr_host[i].file, file, 1024); rtr_host[i].fd = fd; rtr_n = i; break; } r_core_rtr_list (core); }
R_API int r_run_config_env(RRunProfile *p) { int ret; if (!p->_program && !p->_system && !p->_runlib) { printf ("No program, system or runlib rule defined\n"); return 1; } // when IO is redirected to a process, handle them together if (handle_redirection (p->_stdio, true, true, false) != 0) { return 1; } if (handle_redirection (p->_stdin, true, false, false) != 0) { return 1; } if (handle_redirection (p->_stdout, false, true, false) != 0) { return 1; } if (handle_redirection (p->_stderr, false, false, true) != 0) { return 1; } if (p->_aslr != -1) { setASLR (p, p->_aslr); } #if __UNIX__ set_limit (p->_docore, RLIMIT_CORE, RLIM_INFINITY); if (p->_maxfd) { set_limit (p->_maxfd, RLIMIT_NOFILE, p->_maxfd); } #ifdef RLIMIT_NPROC if (p->_maxproc) { set_limit (p->_maxproc, RLIMIT_NPROC, p->_maxproc); } #endif if (p->_maxstack) { set_limit (p->_maxstack, RLIMIT_STACK, p->_maxstack); } #else if (p->_docore || p->_maxfd || p->_maxproc || p->_maxstack) eprintf ("Warning: setrlimits not supported for this platform\n"); #endif if (p->_connect) { char *q = strchr (p->_connect, ':'); if (q) { RSocket *fd = r_socket_new (0); *q = 0; if (!r_socket_connect_tcp (fd, p->_connect, q+1, 30)) { eprintf ("Cannot connect\n"); return 1; } if (p->_pty) { if (redirect_socket_to_pty (fd) != 0) { eprintf ("socket redirection failed\n"); r_socket_free (fd); return 1; } } else { redirect_socket_to_stdio (fd); } } else { eprintf ("Invalid format for connect. missing ':'\n"); return 1; } } if (p->_listen) { RSocket *child, *fd = r_socket_new (0); bool is_child = false; if (!r_socket_listen (fd, p->_listen, NULL)) { eprintf ("rarun2: cannot listen\n"); r_socket_free (fd); return 1; } while (true) { child = r_socket_accept (fd); if (child) { is_child = true; if (p->_dofork && !p->_dodebug) { #ifdef _MSC_VER int child_pid = r_sys_fork (); #else pid_t child_pid = r_sys_fork (); #endif if (child_pid == -1) { eprintf("rarun2: cannot fork\n"); r_socket_free (child); r_socket_free (fd); return 1; } else if (child_pid != 0){ // parent code is_child = false; } } if (is_child) { r_socket_close_fd (fd); eprintf ("connected\n"); if (p->_pty) { if (redirect_socket_to_pty (child) != 0) { eprintf ("socket redirection failed\n"); r_socket_free (child); r_socket_free (fd); return 1; } } else { redirect_socket_to_stdio (child); } break; } else { r_socket_close_fd (child); } } } if (!is_child) { r_socket_free (child); } r_socket_free (fd); } if (p->_r2sleep != 0) { r_sys_sleep (p->_r2sleep); } #if __UNIX__ if (p->_chroot) { if (chdir (p->_chroot) == -1) { eprintf ("Cannot chdir to chroot in %s\n", p->_chroot); return 1; } else { if (chroot (".") == -1) { eprintf ("Cannot chroot to %s\n", p->_chroot); return 1; } else { // Silenting pedantic meson flags... if (chdir ("/") == -1) { eprintf ("Cannot chdir to /\n"); return 1; } if (p->_chgdir) { if (chdir (p->_chgdir) == -1) { eprintf ("Cannot chdir after chroot to %s\n", p->_chgdir); return 1; } } } } } else if (p->_chgdir) { if (chdir (p->_chgdir) == -1) { eprintf ("Cannot chdir after chroot to %s\n", p->_chgdir); return 1; } } #else if (p->_chgdir) { ret = chdir (p->_chgdir); if (ret < 0) { return 1; } } if (p->_chroot) { ret = chdir (p->_chroot); if (ret < 0) { return 1; } } #endif #if __UNIX__ if (p->_setuid) { ret = setgroups (0, NULL); if (ret < 0) { return 1; } ret = setuid (atoi (p->_setuid)); if (ret < 0) { return 1; } } if (p->_seteuid) { ret = seteuid (atoi (p->_seteuid)); if (ret < 0) { return 1; } } if (p->_setgid) { ret = setgid (atoi (p->_setgid)); if (ret < 0) { return 1; } } if (p->_input) { char *inp; int f2[2]; if (pipe (f2) != -1) { close (0); dup2 (f2[0], 0); } else { eprintf ("[ERROR] rarun2: Cannot create pipe\n"); return 1; } inp = getstr (p->_input); if (inp) { size_t inpl = strlen (inp); if (write (f2[1], inp, inpl) != inpl) { eprintf ("[ERROR] rarun2: Cannot write to the pipe\n"); } close (f2[1]); free (inp); } else { eprintf ("Invalid input\n"); } } #endif if (p->_r2preload) { if (p->_preload) { eprintf ("WARNING: Only one library can be opened at a time\n"); } p->_preload = R2_LIBDIR"/libr2."R_LIB_EXT; } if (p->_libpath) { #if __WINDOWS__ eprintf ("rarun2: libpath unsupported for this platform\n"); #elif __HAIKU__ r_sys_setenv ("LIBRARY_PATH", p->_libpath); #elif __APPLE__ r_sys_setenv ("DYLD_LIBRARY_PATH", p->_libpath); #else r_sys_setenv ("LD_LIBRARY_PATH", p->_libpath); #endif } if (p->_preload) { #if __APPLE__ // 10.6 #ifndef __MAC_10_7 r_sys_setenv ("DYLD_PRELOAD", p->_preload); #endif r_sys_setenv ("DYLD_INSERT_LIBRARIES", p->_preload); // 10.8 r_sys_setenv ("DYLD_FORCE_FLAT_NAMESPACE", "1"); #else r_sys_setenv ("LD_PRELOAD", p->_preload); #endif } if (p->_timeout) { #if __UNIX__ int mypid = getpid (); if (!r_sys_fork ()) { int use_signal = p->_timeout_sig; if (use_signal < 1) { use_signal = SIGKILL; } sleep (p->_timeout); if (!kill (mypid, 0)) { eprintf ("\nrarun2: Interrupted by timeout\n"); } kill (mypid, use_signal); exit (0); } #else eprintf ("timeout not supported for this platform\n"); #endif } return 0; }
static int runfile () { if (!_program) { printf ("No program rule defined\n"); return 1; } if (_stdin) { int f = open (_stdin, O_RDONLY); close (0); dup2 (f, 0); } if (_stdout) { int f = open (_stdout, O_RDONLY); close (1); dup2 (f, 1); } if (_stderr) { int f = open (_stderr, O_RDONLY); close (2); dup2 (f, 2); } if (_connect) { char *p = strchr (_connect, ':'); if (p) { RSocket *fd = r_socket_new (0); *p=0; if (!r_socket_connect_tcp (fd, _connect, p+1, 30)) { eprintf ("Cannot connect\n"); return 1; } eprintf ("connected\n"); close (0); close (1); close (2); dup2 (fd->fd, 0); dup2 (fd->fd, 1); dup2 (fd->fd, 2); } else { eprintf ("Invalid format for connect. missing ':'\n"); return 1; } } if (_listen) { RSocket *child, *fd = r_socket_new (0); if (!r_socket_listen (fd, _listen, NULL)) { eprintf ("rarun2: cannot listen\n"); return 1; } child = r_socket_accept (fd); if (child) { eprintf ("connected\n"); close (0); close (1); close (2); dup2 (child->fd, 0); dup2 (child->fd, 1); dup2 (child->fd, 2); } } if (_chgdir) chdir (_chgdir); if (_chroot) chdir (_chroot); #if __UNIX__ if (_chroot) { if (chroot (".")) { eprintf ("rarun2: cannot chroot\n"); return 1; } } if (_setuid) setuid (atoi (_setuid)); if (_seteuid) seteuid (atoi (_seteuid)); if (_setgid) setgid (atoi (_setgid)); if (_input) { int f2[2]; pipe (f2); close (0); dup2 (f2[0], 0); parseinput (_input); write (f2[1], _input, strlen (_input)); } #endif if (_preload) { #if __APPLE__ r_sys_setenv ("DYLD_PRELOAD", _preload); #else r_sys_setenv ("LD_PRELOAD", _preload); #endif } if (_timeout) { #if __UNIX__ int mypid = getpid (); if (!fork ()) { sleep (_timeout); if (!kill (mypid, 0)) fprintf (stderr, "\nrarun2: Interrupted by timeout\n"); kill (mypid, SIGKILL); exit (0); } #else eprintf ("timeout not supported for this platform\n"); #endif } if (!r_file_exists (_program)) { eprintf ("rarun2: %s: file not found\n", _program); return 1; } exit (execv (_program, _args)); }
R_API void r_core_rtr_add(RCore *core, const char *_input) { char *port, input[1024], *host = NULL, *file = NULL, *ptr = NULL, buf[1024]; int proto, i, timeout, ret; RSocket *fd; timeout = r_config_get_i (core->config, "http.timeout"); strncpy (input, _input, sizeof (input)-4); /* Parse uri */ if ((ptr = strstr (input, "tcp://"))) { proto = RTR_PROT_TCP; host = ptr+6; } else if ((ptr = strstr(input, "http://"))) { proto = RTR_PROT_HTTP; host = ptr+7; } else if ((ptr = strstr(input, "udp://"))) { proto = RTR_PROT_UDP; host = ptr+6; } else if ((ptr = strstr(input, "rap://"))) { proto = RTR_PROT_RAP; host = ptr+6; } else { proto = RTR_PROT_RAP; host = input; } while (*host && iswhitechar (*host)) host++; if (!(ptr = strchr (host, ':'))) { ptr = host; port = "80"; } else { *ptr++ = '\0'; port = ptr; } if (!(file = strchr (ptr, '/'))) { eprintf("Error: Missing '/'\n"); return; } *file++ = 0; port = r_str_chop (port); while (*file==' ') file++; if (r_sandbox_enable (0)) { eprintf ("sandbox: connect disabled\n"); return; } fd = r_socket_new (R_FALSE); if (!fd) { eprintf ("Error: Cannot create new socket\n"); return; } switch (proto) { case RTR_PROT_HTTP: { char uri[1024], prompt[64]; int len; char *str, *res; if (file[strlen (file)-1]=='/') { snprintf (prompt, sizeof (prompt), "[http://%s:%s/%s]> ", host, port, file); r_line_set_prompt (prompt); for (;;) { char *ptr, *str = r_line_readline (); if (!str || !*str) break; if (*str == 'q') break; ptr = r_str_uri_encode (str); if (ptr) str = ptr; snprintf (uri, sizeof (uri), "http://%s:%s/%s%s", host, port, file, str); if (ptr == str) free (ptr); str = r_socket_http_get (uri, NULL, &len); if (str) { str[len] = 0; res = strstr (str, "\n\n"); if (res) res = strstr (res+1, "\n\n"); if (res) res += 2; else res = str; printf ("%s%s", res, (res[strlen (res)-1]=='\n')?"":"\n"); r_line_hist_add (str); free (str); } } r_socket_free (fd); return; } snprintf (uri, sizeof (uri), "http://%s:%s/%s", host, port, file); str = r_socket_http_get (uri, NULL, &len); if (str) { str[len] = 0; res = strstr (str, "\n\n"); if (res) res = strstr (res+1, "\n\n"); if (res) res += 2; else res = str; printf ("%s", res); free (str); } else eprintf ("HTTP connection has failed\n"); // do not add connection. wtf return; } break; case RTR_PROT_RAP: if (!r_socket_connect_tcp (fd, host, port, timeout)) { //TODO: Use rap.ssl eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port); return; } eprintf ("Connected to %s at port %s\n", host, port); /* send */ buf[0] = RTR_RAP_OPEN; buf[1] = 0; buf[2] = (ut8)(strlen (file)+1); memcpy (buf+3, file, buf[2]); r_socket_write(fd, buf, 3+buf[2]); /* read */ eprintf ("waiting... "); fflush(stdout); r_socket_read (fd, (ut8*)buf, 5); r_mem_copyendian ((ut8 *)&i, (ut8*)buf+1, 4, core->assembler->big_endian); if (buf[0] != (char)(RTR_RAP_OPEN|RTR_RAP_REPLY) || i<= 0) { eprintf ("Error: Wrong reply\n"); return; } eprintf ("ok\n"); break; case RTR_PROT_TCP: if (!r_socket_connect_tcp (fd, host, port, timeout)) { //TODO: Use rap.ssl core->num->value = 1; eprintf("Error: Cannot connect to '%s' (%s)\n", host, port); return; } core->num->value = 0; eprintf ("Connected to: %s at port %s\n", host, port); break; case RTR_PROT_UDP: if (!r_socket_connect_udp (fd, host, port, timeout)) { //TODO: Use rap.ssl core->num->value = 1; eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port); return; } core->num->value = 0; eprintf("Connected to: %s at port %s\n", host, port); break; } ret = core->num->value; for (i = 0; i < RTR_MAX_HOSTS; i++) if (!rtr_host[i].fd) { rtr_host[i].proto = proto; memcpy (rtr_host[i].host, host, 512); rtr_host[i].port = r_num_get (core->num, port); memcpy (rtr_host[i].file, file, 1024); rtr_host[i].fd = fd; rtr_n = i; break; } core->num->value = ret; r_socket_free(fd); //r_core_rtr_list (core); }