| | |
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| |
|
| | (define-module (system repl server) |
| | #:use-module (system repl repl) |
| | #:use-module (ice-9 threads) |
| | #:use-module (ice-9 rdelim) |
| | #:use-module (ice-9 match) |
| | #:use-module (ice-9 iconv) |
| | #:use-module (rnrs bytevectors) |
| | #:use-module (ice-9 binary-ports) |
| | #:use-module (srfi srfi-26) |
| | #:export (make-tcp-server-socket |
| | make-unix-domain-server-socket |
| | run-server |
| | spawn-server |
| | stop-server-and-clients!)) |
| |
|
| | |
| | |
| | |
| | (define *open-sockets* '()) |
| |
|
| | (define sockets-lock (make-mutex)) |
| |
|
| | |
| | |
| | (define (close-socket! s) |
| | (with-mutex sockets-lock |
| | (set! *open-sockets* (assq-remove! *open-sockets* s))) |
| | |
| | |
| | (close-port s)) |
| |
|
| | |
| | (define (add-open-socket! s force-close) |
| | (with-mutex sockets-lock |
| | (set! *open-sockets* (acons s force-close *open-sockets*)))) |
| |
|
| | (define (stop-server-and-clients!) |
| | (cond |
| | ((with-mutex sockets-lock |
| | (match *open-sockets* |
| | (() #f) |
| | (((s . force-close) . rest) |
| | (set! *open-sockets* rest) |
| | force-close))) |
| | => (lambda (force-close) |
| | (force-close) |
| | (stop-server-and-clients!))))) |
| |
|
| | (define* (make-tcp-server-socket #:key |
| | (host #f) |
| | (addr (if host |
| | (inet-pton AF_INET host) |
| | INADDR_LOOPBACK)) |
| | (port 37146)) |
| | (let ((sock (socket PF_INET SOCK_STREAM 0))) |
| | (setsockopt sock SOL_SOCKET SO_REUSEADDR 1) |
| | (bind sock AF_INET addr port) |
| | sock)) |
| |
|
| | (define* (make-unix-domain-server-socket #:key (path "/tmp/guile-socket")) |
| | (let ((sock (socket PF_UNIX SOCK_STREAM 0))) |
| | (setsockopt sock SOL_SOCKET SO_REUSEADDR 1) |
| | (bind sock AF_UNIX path) |
| | sock)) |
| |
|
| | (define* (run-server #:optional (server-socket (make-tcp-server-socket))) |
| | (run-server* server-socket serve-client)) |
| |
|
| | |
| | (define (run-server* server-socket serve-client) |
| | |
| | (define shutdown-pipes (pipe)) |
| | (define shutdown-read-pipe (car shutdown-pipes)) |
| | (define shutdown-write-pipe (cdr shutdown-pipes)) |
| |
|
| | |
| | (define (shutdown-server) |
| | (display #\! shutdown-write-pipe) |
| | (force-output shutdown-write-pipe)) |
| |
|
| | (define monitored-ports |
| | (list server-socket |
| | shutdown-read-pipe)) |
| |
|
| | (define (accept-new-client) |
| | (let ((ready-ports (car (select monitored-ports '() '())))) |
| | |
| | (and (not (memq shutdown-read-pipe ready-ports)) |
| | |
| | |
| | |
| | (or (false-if-exception (accept server-socket) |
| | #:warning "Failed to accept client:") |
| | (accept-new-client))))) |
| |
|
| | |
| | (fcntl server-socket F_SETFL |
| | (logior O_NONBLOCK |
| | (fcntl server-socket F_GETFL))) |
| |
|
| | (sigaction SIGPIPE SIG_IGN) |
| | (add-open-socket! server-socket shutdown-server) |
| | (listen server-socket 5) |
| | (let lp () |
| | (match (accept-new-client) |
| | (#f |
| | |
| | (close shutdown-write-pipe) |
| | (close shutdown-read-pipe) |
| | (close server-socket)) |
| | ((client-socket . client-addr) |
| | (make-thread serve-client client-socket client-addr) |
| | (lp))))) |
| |
|
| | (define* (spawn-server #:optional (server-socket (make-tcp-server-socket))) |
| | (make-thread run-server server-socket)) |
| |
|
| | (define (serve-client client addr) |
| |
|
| | (let ((thread (current-thread))) |
| | |
| | (add-open-socket! client (lambda () (cancel-thread thread)))) |
| |
|
| | (guard-against-http-request client) |
| |
|
| | (dynamic-wind |
| | (lambda () #f) |
| | (with-continuation-barrier |
| | (lambda () |
| | (parameterize ((current-input-port client) |
| | (current-output-port client) |
| | (current-error-port client) |
| | (current-warning-port client)) |
| | (with-fluids ((*repl-stack* '())) |
| | (start-repl))))) |
| | (lambda () (close-socket! client)))) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | (define (with-temporary-port-encoding port encoding thunk) |
| | "Call THUNK in a dynamic environment in which the encoding of PORT is |
| | temporarily set to ENCODING." |
| | (let ((saved-encoding #f)) |
| | (dynamic-wind |
| | (lambda () |
| | (unless (port-closed? port) |
| | (set! saved-encoding (port-encoding port)) |
| | (set-port-encoding! port encoding))) |
| | thunk |
| | (lambda () |
| | (unless (port-closed? port) |
| | (set! encoding (port-encoding port)) |
| | (set-port-encoding! port saved-encoding)))))) |
| |
|
| | (define (with-saved-port-line+column port thunk) |
| | "Save the line and column of PORT before entering THUNK, and restore |
| | their previous values upon normal or non-local exit from THUNK." |
| | (let ((saved-line #f) (saved-column #f)) |
| | (dynamic-wind |
| | (lambda () |
| | (unless (port-closed? port) |
| | (set! saved-line (port-line port)) |
| | (set! saved-column (port-column port)))) |
| | thunk |
| | (lambda () |
| | (unless (port-closed? port) |
| | (set-port-line! port saved-line) |
| | (set-port-column! port saved-column)))))) |
| |
|
| | (define (drain-input-and-close socket) |
| | "Drain input from SOCKET using ISO-8859-1 encoding until it would block, |
| | and then close it. Return the drained input as a string." |
| | (dynamic-wind |
| | (lambda () |
| | |
| | |
| | (setvbuf socket 'block)) |
| | (lambda () |
| | (let loop ((chunks '())) |
| | (let ((result (and (char-ready? socket) |
| | (get-bytevector-some socket)))) |
| | (if (bytevector? result) |
| | (loop (cons (bytevector->string result "ISO-8859-1") |
| | chunks)) |
| | (string-concatenate-reverse chunks))))) |
| | (lambda () |
| | |
| | (close-port socket)))) |
| |
|
| | (define permissive-http-request-line? |
| | |
| | |
| | (let ((cs (ucs-range->char-set #x20 #x7E)) |
| | (rx (make-regexp |
| | (string-append |
| | "^(OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT) " |
| | "[^ ]+ " |
| | "HTTP/[0123456789]+.[0123456789]+$")))) |
| | (lambda (line) |
| | "Return true if LINE might plausibly be an HTTP request-line, |
| | otherwise return #f." |
| | |
| | |
| | (and (string-every cs line) |
| | (regexp-exec rx line))))) |
| |
|
| | (define (check-for-http-request socket) |
| | "Check for a possible HTTP request in the initial input from SOCKET. |
| | If one is found, close the socket and print a report to STDERR (fdes 2). |
| | Otherwise, put back the bytes." |
| | |
| | |
| | |
| | |
| | (with-temporary-port-encoding socket "ISO-8859-1" |
| | (lambda () |
| | |
| | |
| | (with-saved-port-line+column socket |
| | (lambda () |
| | |
| | |
| | |
| | |
| | |
| | (match (read-delimited "\r\n" socket 'peek) |
| | ((? eof-object?) |
| | |
| | 'done) |
| |
|
| | ((? permissive-http-request-line? request-line) |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | (let ((void-port (%make-void-port "rw"))) |
| | (current-input-port void-port) |
| | (current-output-port void-port) |
| | (current-error-port void-port) |
| | (current-warning-port void-port)) |
| |
|
| | |
| | |
| | (let ((drained-input (drain-input-and-close socket))) |
| |
|
| | |
| | |
| | (call-with-port (dup->port 2 "w") |
| | (cut format <> " |
| | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| | @@ POSSIBLE BREAK-IN ATTEMPT ON THE REPL SERVER @@ |
| | @@ BY AN HTTP INTER-PROTOCOL EXPLOITATION ATTACK. See: @@ |
| | @@ <https://en.wikipedia.org/wiki/Inter-protocol_exploitation> @@ |
| | @@ Possible HTTP request received: ~S |
| | @@ The associated socket has been closed. @@ |
| | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" |
| | (string-append request-line |
| | drained-input))))) |
| |
|
| | (start-line |
| | |
| | |
| | (unread-string start-line socket)))))))) |
| |
|
| | (define (guard-against-http-request socket) |
| | "Arrange for the Guile REPL to check for an HTTP request in the |
| | initial input from SOCKET, in which case the socket will be closed. |
| | This guards against HTTP inter-protocol exploitation attacks, a scenario |
| | whereby an attacker can, via an HTML page, cause a web browser to send |
| | data to TCP servers listening on a loopback interface or private |
| | network." |
| | (%set-port-property! socket 'guard-against-http-request? #t)) |
| |
|
| | (define* (maybe-check-for-http-request |
| | #:optional (socket (current-input-port))) |
| | "Apply check-for-http-request to SOCKET if previously requested by |
| | guard-against-http-request. This procedure is intended to be added to |
| | before-read-hook." |
| | (when (%port-property socket 'guard-against-http-request?) |
| | (check-for-http-request socket) |
| | (unless (port-closed? socket) |
| | (%set-port-property! socket 'guard-against-http-request? #f)))) |
| |
|
| | |
| | (add-hook! before-read-hook |
| | maybe-check-for-http-request) |
| |
|
| | |
| | |
| | |
| | |
| |
|