X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=rags.git;a=blobdiff_plain;f=rags.scm;h=a06be1c626db8a20fa254d147e8f540b0b29ccad;hp=264f5537a66647d4f09d0a22cff39756b80c8687;hb=2f2d3ea456c393f1647c6778d4f58f4b4ede3564;hpb=f279ff3c0a93204a629e707fe54b0ee08921c579 diff --git a/rags.scm b/rags.scm index 264f553..a06be1c 100644 --- a/rags.scm +++ b/rags.scm @@ -1,3 +1,6 @@ +;; The Right-Awful Gemini Server +;; + (import (chicken io) (chicken port) (chicken file) @@ -7,15 +10,17 @@ (chicken time posix) (chicken process) (chicken process-context) - matchable srfi-13 + (chicken process-context posix) + matchable srfi-13 srfi-1 uri-common tcp6 openssl) (define-record config - root-dir host port certfile keyfile) + root-dir host port certfile keyfile uid gid) (define file-types '(("gmi" "text/gemini" "charset=utf-8") - ("txt" "text/plain" "charset=utf-8"))) + ("txt" "text/plain" "charset=utf-8") + ("xml" "text/xml" "charset=utf-8"))) (define (process-request config request-line) (let ((uri (uri-normalize-path-segments (absolute-uri request-line)))) @@ -30,18 +35,36 @@ (fail-permanent "Path must be absolute.")) ((not (document-available? config uri)) (fail-permanent "Document not found.")) + ((and (document-path-directory? config uri) + (uri-lacks-trailing-slash? uri)) + (redirect-permanent (uri-with-trailing-slash uri))) (else (serve-document config uri))))) (define (fail-permanent reason) (print "50 " reason "\r")) +(define (redirect-permanent new-uri) + (print "30 " (uri->string new-uri) "\r")) + +(define (uri-lacks-trailing-slash? uri) + (not (string-null? (last (uri-path uri))))) + +(define (uri-with-trailing-slash uri) + (update-uri uri path: (append (uri-path uri) '("")))) + (define (document-available? config uri) (file-exists? (document-path config uri))) +(define (document-path-directory? config uri) + (directory-exists? (document-path-raw config uri))) + +(define (document-path-raw config uri) + (let* ((crumbs (reverse (cons (config-root-dir config) (cdr (uri-path uri)))))) + (make-pathname (reverse (cdr crumbs)) (car crumbs)))) + (define (document-path config uri) - (let* ((crumbs (reverse (cons (config-root-dir config) (cdr (uri-path uri))))) - (path (make-pathname (reverse (cdr crumbs)) (car crumbs)))) + (let* ((path (document-path-raw config uri))) (if (directory-exists? path) (make-pathname path "index.gmi") path))) @@ -54,9 +77,8 @@ (mime-type (cadr mime))) (print "20 " (string-intersperse (cdr mime) ";") "\r") (cond - ((and (equal? mime-type "text/gemini") - (file-executable? path)) - (serve-text-dynamic path)) + ((file-executable? path) + (serve-text-dynamic path)) ; Binary-files can also be generated here, but the source is dynamic text ((string-prefix? "text/" mime-type) (serve-text-plain path)) (else (serve-binary path))))) @@ -117,10 +139,11 @@ (signal o))))) (define (run-server config) - (define listener (ssl-listen* hostname: (config-host config) - port: (config-port config) + (set-buffering-mode! (current-output-port) #:line) + (define listener (ssl-listen* port: (config-port config) certificate: (config-certfile config) - private-key: (config-keyfile config))) + private-key: (config-keyfile config) + protocol: 'tlsv12)) (print "Host: '" (config-host config) "'\n" "Port: '" (config-port config) "'\n" @@ -130,8 +153,18 @@ "\n" "Gemini server listening ...") + (drop-privs config) (server-loop listener config)) +(define (drop-privs config) + (let ((uid (config-uid config)) + (gid (config-gid config))) + (if gid ; Group first, since only root can switch groups. + (set! (current-group-id) gid)) + (if uid + (set! (current-user-id) uid)))) + + (define (server-loop listener config) (let-values (((in-port out-port) (ssl-accept listener))) (let-values (((local-ip remote-ip) (tcp-addresses (ssl-port->tcp-port in-port)))) @@ -152,11 +185,15 @@ (define (print-usage progname) - (print "Usage: " progname " [-h] [-p port] server-root-dir hostname certfile keyfile")) + (let ((indent-str (make-string (string-length progname) #\space))) + (print "Usage:\n" + progname " [-h/--help]\n" + progname " [-p/--port PORT] [-u/--user UID] [-g/--group GID]\n" + indent-str " server-root-dir hostname certfile keyfile"))) (define (main) (let* ((progname (pathname-file (car (argv)))) - (config (make-config #f #f 1965 #f #f))) + (config (make-config #f #f 1965 #f #f #f #f))) (if (null? (cdr (argv))) (print-usage progname) (let loop ((args (cdr (argv)))) @@ -169,7 +206,15 @@ (print-usage progname)) ((or (equal? this-arg "-p") (equal? this-arg "--port")) - (config-port-set! config (string->bumber (car rest-args))) + (config-port-set! config (string->number (car rest-args))) + (loop (cdr rest-args))) + ((or (equal? this-arg "-u") + (equal? this-arg "--user")) + (config-uid-set! config (string->number (car rest-args))) + (loop (cdr rest-args))) + ((or (equal? this-arg "-g") + (equal? this-arg "--group")) + (config-gid-set! config (string->number (car rest-args))) (loop (cdr rest-args))) (else (print-usage progname)))