X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=scratchy.git;a=blobdiff_plain;f=scratchy.scm;h=409047d0caeec3ffa143a3b69bfba8616475f202;hp=0b81523080ea1d9f46373f31f9dc68be81a407d6;hb=045c10a05d15f3a19bddfa8be26979e73f150cd0;hpb=843f30f3749a5a47d12446d93c2e364b2956e5a7 diff --git a/scratchy.scm b/scratchy.scm index 0b81523..409047d 100644 --- a/scratchy.scm +++ b/scratchy.scm @@ -20,7 +20,7 @@ ;;; Global constants -(define scratchy-version "1.2.1") +(define scratchy-version "1.4.1") (define scratchy-footer (conc "\n" @@ -38,7 +38,7 @@ ;; we genuinely don't expect a huge flood of gopher traffic. :-( (define-record config - root-dir host port display-footer user group) + root-dir host port display-footer user group blacklist blacklist-resp) (define (run-server config) (set-buffering-mode! (current-output-port) #:line) @@ -60,25 +60,38 @@ (let-values (((in-port out-port) (tcp-accept listener))) (let-values (((local-ip remote-ip) (tcp-addresses in-port))) (print "Accepted connection from " remote-ip - " on " (seconds->string))) - (condition-case - (let* ((line (read-line in-port)) - (selector-raw (string-trim-both line)) - (selector (if (= (string-length selector-raw) 0) - "/" - selector-raw))) - (condition-case + " on " (seconds->string)) + (condition-case + (if (or (null? (config-blacklist config)) + (not (member remote-ip + (with-input-from-file + (config-blacklist config) + read)))) + (let* ((line (read-line in-port)) + (selector (string-trim-both line))) + (condition-case + (begin + (with-output-to-port out-port + (lambda () + (serve-selector selector config))) + (print "... served selector '" selector "'. Closing connection.")) + (o (exn) + (print-error-message o out-port) + (print-error-message o) + (print "Error while attempting to serve selector " selector ".")))) (begin + (print "Connection from blacklisted IP. Closing.") (with-output-to-port out-port (lambda () - (serve-selector selector config))) - (print "... served selector '" selector "'. Closing connection.")) - (o (exn) - (print-error-message o out-port) - (print-error-message o) - (print "Error while attempting to serve selector " selector ".")))) - (o (exn) - (print-error-message o))) + (print* "Refusing to serve to IP " remote-ip ".\r\n") + (unless (null? (config-blacklist-resp config)) + (for-each (lambda (l) (print* l "\r\n")) + (with-input-from-file + (config-blacklist-resp config) + read-lines))) + (print* ".\r\n"))))) + (o (exn) + (print-error-message o)))) (close-input-port in-port) (close-output-port out-port)) (server-loop listener config)) @@ -117,19 +130,23 @@ ;;; Selector retrieval (define (serve-selector raw-selector config) - (let* ((selector-list (string-split raw-selector "\t")) + (let* ((selector-list (string-split raw-selector "\t" #t)) (selector (car selector-list)) (arguments (cdr selector-list))) - (if (string-contains selector "|") - (let ((l (string-split selector "|" #t))) - (serve-script (car l) (cdr l) config)) - (case (infer-selector-type selector) - ((1) (serve-directory-file selector config)) - ((7) (let ((l (string-split selector "?" #t))) - (serve-script (car l) arguments config))) - ((0) (serve-text-file selector config)) - ((h) (serve-url selector config)) - (else (serve-binary-file selector config)))))) + (cond + ((string-contains selector "|") + (let ((l (string-split selector "|" #t))) + (serve-script (car l) (cdr l) config))) + ((legal-filename? (directory-index-filename selector config) config) + (serve-directory-file selector config)) + (else + (case (infer-selector-type selector) + ((1) (error "Invalid directory selector.")) + ((7) (let ((l (string-split selector "?" #t))) + (serve-script (car l) arguments config))) + ((0) (serve-text-file selector config)) + ((h) (serve-url selector config)) + (else (serve-binary-file selector config))))))) (define (legal-filename? filename config) (and (string-prefix? (config-root-dir config) @@ -143,9 +160,13 @@ (string-suffix? ".scm" filename) (file-executable? filename))) +(define (directory-index-filename selector config) + (make-pathname (list (config-root-dir config) + selector) + gopher-index-filename)) + (define (serve-directory-file selector config) - (let ((filename (make-pathname (list (config-root-dir config) selector) - gopher-index-filename))) + (let ((filename (directory-index-filename selector config))) (if (legal-filename? filename config) (begin (with-input-from-file filename @@ -213,7 +234,7 @@ (if (legal-script-filename? filename config) (let* ((sexp (with-input-from-file filename read)) (script-result (with-selector-dir - selector config + (pathname-directory selector) config (lambda () (apply (eval sexp) arguments))))) (when (pair? script-result) @@ -306,24 +327,29 @@ (define (with-selector-dir selector config thunk) (with-current-working-directory - (make-pathname (config-root-dir config) - (pathname-directory selector)) thunk)) + (make-pathname (config-root-dir config) selector) + thunk)) ;;; Main (define (print-usage progname) - (print "Usage:\n" - progname " -h/--help\n" - progname " [-n/--no-footer] [-u/--user UID] [-g/--group GID] root-dir hostname [port]\n" - "\n" - "The -n option tells the server to not display a directory footer.\n" - "The -u and -g can be used to set the UID and GID of the process following\n" - "the creation of the TCP port listener (which often requires root).")) + (let ((indent-str (make-string (string-length progname) #\space))) + (print "Usage:\n" + progname " -h/--help\n" + progname " [-n/--no-footer] [-u/--user UID] [-g/--group GID]\n" + indent-str " [-b/--blacklist FILE] [-r/--blacklist-resp RESPFILE] root-dir hostname [port]\n" + "\n" + "The -n option tells the server to not display a directory footer.\n" + "The -u and -g can be used to set the UID and GID of the process following\n" + "the creation of the TCP port listener (which often requires root).\n" + "The -b option can be used to specify a FILE containing a list of IP addresses\n" + "to block from the server. If a connection from a blocked address is served,\n" + "the response file RESPFILE is served instead, if this is provided."))) (define (main) (let* ((progname (pathname-file (car (argv)))) - (config (make-config '() '() 70 #t '() '()))) + (config (make-config '() '() 70 #t '() '() '() '()))) (if (null? (cdr (argv))) (print-usage progname) (let loop ((args (cdr (argv)))) @@ -346,6 +372,14 @@ (equal? this-arg "--group")) (config-group-set! config (string->number (car rest-args))) (loop (cdr rest-args))) + ((or (equal? this-arg "-b") + (equal? this-arg "--blacklist")) + (config-blacklist-set! config (car rest-args)) + (loop (cdr rest-args))) + ((or (equal? this-arg "-r") + (equal? this-arg "--blacklist-resp")) + (config-blacklist-resp-set! config (car rest-args)) + (loop (cdr rest-args))) (else (print-usage progname))) (begin @@ -358,6 +392,6 @@ (main) ;; (define (test) -;; (run-server (make-config "gopher-root" "localhost" 70 #t '() '()))) +;; (run-server (make-config "gopher-root" "localhost" 70 #t '() '() '() '()))) ;; (test)