From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:59242) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gf9D6-0001xe-8Q for guix-patches@gnu.org; Thu, 03 Jan 2019 15:03:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gf9Cv-0006C1-DS for guix-patches@gnu.org; Thu, 03 Jan 2019 15:03:15 -0500 Received: from debbugs.gnu.org ([208.118.235.43]:52330) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gf9Cp-00068J-Q0 for guix-patches@gnu.org; Thu, 03 Jan 2019 15:03:05 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gf9Cp-0007DK-Lr for guix-patches@gnu.org; Thu, 03 Jan 2019 15:03:03 -0500 Subject: [bug#33966] fcgiwrap: additional options for logging and unix domain sockets Resent-Message-ID: Received: from eggs.gnu.org ([208.118.235.92]:59143) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gf9CZ-0001uD-E3 for guix-patches@gnu.org; Thu, 03 Jan 2019 15:02:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gf9CV-0005tg-9v for guix-patches@gnu.org; Thu, 03 Jan 2019 15:02:47 -0500 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:37457) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gf9CU-0005sm-V0 for guix-patches@gnu.org; Thu, 03 Jan 2019 15:02:43 -0500 Received: by mail-wm1-x32f.google.com with SMTP id g67so31332383wmd.2 for ; Thu, 03 Jan 2019 12:02:42 -0800 (PST) Received: from [192.168.178.64] (p508876EC.dip0.t-ipconnect.de. [80.136.118.236]) by smtp.gmail.com with ESMTPSA id l6sm42366499wrv.70.2019.01.03.12.02.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 03 Jan 2019 12:02:40 -0800 (PST) From: Florian Dold Message-ID: <624ba072-d5fe-b159-46af-61e79caf22f1@gmail.com> Date: Thu, 3 Jan 2019 21:02:38 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------0EC48590587DD7B3CAB39C78" Content-Language: en-US-large List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: 33966@debbugs.gnu.org This is a multi-part message in MIME format. --------------0EC48590587DD7B3CAB39C78 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Hi Guix, this patch adds additional options to the fcgiwrap service. In particular it allows 1. writing the output of the fcgi process to a file (with the 'log-file' option) 2. arranging for a directory to be created so that the fcgiwrap process can create its listening socket without running into permission problems (with the 'ensure-socket-dir?' option) 3. adjusting the permissions on the listening unix domain socket, typically so that users in the fcgiwrap group have read and write access to that socket (with the 'adjusted-socket-permissions' option) Additionally, a potentially left-over fcgiwrap socket is cleaned up before starting the service, which would otherwise lead to the process refusing to run. The documentation is also changed to address a potential security issue, now recommending against running fcgiwrap as root. The configuration defaults are not ideal (a tcp socket with unrestricted access from any local user), but impossible to change without breaking existing system definitions. - Florian --------------0EC48590587DD7B3CAB39C78 Content-Type: text/x-patch; name="0001-services-fcgiwrap-Implement-additional-options.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="0001-services-fcgiwrap-Implement-additional-options.patch" =46rom 3ac9c6fa536faff23291b21d4e649b85386fedfc Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 3 Jan 2019 14:22:49 +0100 Subject: [PATCH] services: fcgiwrap: Implement additional options The fcgiwrap service now supports logging and can be run on a unix domain socket as unprivileged user. * doc/guix.texi (Web Services): Document new options and replace dangerous advice about running fcgiwrap as root. * gnu/services/web.scm: Add the options 'log-file', 'adjusted-socket-permissions' and 'ensure-socket-dir?'. --- doc/guix.texi | 26 +++++++--- gnu/services/web.scm | 119 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 120 insertions(+), 25 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index fcb5b8c08..608dd26ca 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -17756,12 +17756,26 @@ The user and group names, as strings, under whi= ch to run the the user asks for the specific user or group names @code{fcgiwrap} that the corresponding user and/or group is present on the system. =20 -It is possible to configure a FastCGI-backed web service to pass HTTP -authentication information from the front-end to the back-end, and to -allow @code{fcgiwrap} to run the back-end process as a corresponding -local user. To enable this capability on the back-end., run -@code{fcgiwrap} as the @code{root} user and group. Note that this -capability also has to be configured on the front-end as well. +Note that whoever can write to the fcgiwrap socket is effectively able t= o +execute programs as the user/group running the fcgiwrap process. It is = thus +strongly discouraged to run fcgiwrap as the @code{root} user or group. + +@item @code{log-file} (default: @code{#f}) +File where @command{fcgiwrap}'s output is written, or @code{#f} to not +store the output. + +@item @code{adjusted-socket-permissions} (default: @code{#f}) +Only applies to @code{unix} sockets. Adjusts the permissions of the soc= ket +after it has been created. If set to an integer, it is interpreted as a= +numeric file mode. If set to @code{#t}, it is interpreted as mode @code= {#o660} +(read and write permissions for user and group). If set to the default +@code{#f}, no adjustments are made. + +@item @code{ensure-socket-dir?} (default: @code{#f}) +Only applies to @code{unix} sockets. If set to @code{#t} and the direct= ory +component of the socket path in @code{socket} does not exist yet, the +directory is created with ownership set to the user and group running th= e +fcgiwrap process. @end table @end deftp =20 diff --git a/gnu/services/web.scm b/gnu/services/web.scm index d71fed20e..a3d435489 100644 --- a/gnu/services/web.scm +++ b/gnu/services/web.scm @@ -39,6 +39,7 @@ #:use-module (guix records) #:use-module (guix modules) #:use-module (guix gexp) + #:use-module (guix i18n) #:use-module ((guix store) #:select (text-file)) #:use-module ((guix utils) #:select (version-major)) #:use-module ((guix packages) #:select (package-version)) @@ -696,14 +697,21 @@ of index files." (define-record-type* fcgiwrap-configuration make-fcgiwrap-configuration fcgiwrap-configuration? - (package fcgiwrap-configuration-package ; - (default fcgiwrap)) - (socket fcgiwrap-configuration-socket - (default "tcp:127.0.0.1:9000")) - (user fcgiwrap-configuration-user - (default "fcgiwrap")) - (group fcgiwrap-configuration-group - (default "fcgiwrap"))) + (package fcgiwrap-configuration-package ; + (default fcgiwrap)) + (socket fcgiwrap-configuration-socket + (default "tcp:127.0.0.1:9000")) + (user fcgiwrap-configuration-user + (default "fcgiwrap")) + (group fcgiwrap-configuration-group + (default "fcgiwrap")) + (log-file fcgiwrap-log-file + (default #f)) + ;; boolean or octal mode integer + (adjusted-socket-permissions fcgiwrap-adjusted-socket-permissions? + (default #f)) + (ensure-socket-dir? fcgiwrap-ensure-socket-dir? + (default #f))) =20 (define fcgiwrap-accounts (match-lambda @@ -723,25 +731,98 @@ of index files." (home-directory "/var/empty") (shell (file-append shadow "/sbin/nologin"))))))))) =20 +(define (parse-fcgiwrap-socket s) + "Parse a fcgiwrap socket specification string into '(type args ...)" + (cond + ((string-prefix? "unix:" s) + (list 'unix (substring s 5))) + ((string-prefix? "tcp:" s) + (match (string-match "^tcp:([.0-9]+):([0-9]+)$" s) + ((? regexp-match? m) + (list + 'tcp + (match:substring m 1) + (string->number (match:substring m 2)))) + (_ (error "invalid tcp socket address")))) + ((string-prefix? "tcp6:" s) + (match (string-match "^tcp6:\\[(.*)\\]:([0-9]+)$" s) + ((? regexp-match? m) + (list + 'tcp6 + (match:substring m 1) + (string->number (match:substring m 2)))) + (_ (error "invalid tcp6 socket address")))) + (else (error "unrecognized socket protocol")))) + (define fcgiwrap-shepherd-service (match-lambda - (($ package socket user group) - (list (shepherd-service - (provision '(fcgiwrap)) - (documentation "Run the fcgiwrap daemon.") - (requirement '(networking)) - (start #~(make-forkexec-constructor - '(#$(file-append package "/sbin/fcgiwrap") - "-s" #$socket) - #:user #$user #:group #$group)) - (stop #~(make-kill-destructor))))))) + (($ package socket user group log-file perm= ensure-dir?) + (define parsed-socket (parse-fcgiwrap-socket socket)) + (list + (shepherd-service + (provision '(fcgiwrap)) + (documentation "Run the fcgiwrap daemon.") + (requirement '(networking)) + (modules `((shepherd support) (ice-9 match) ,@%default-modules)) + (start + #~(lambda args + (define (clean-up file) + (catch 'system-error + (lambda () + (delete-file file)) + (lambda args + (unless (=3D ENOENT (system-error-errno args)) + (apply throw args))))) + (define* (wait-for-file file #:key (max-delay 5)) + (define start (current-time)) + (let loop () + (cond + ((file-exists? file) #t) + ((< (current-time) (+ start max-delay)) + (sleep 1) + (loop)) + (else #f)))) + (define (adjust-permissions file mode) + (match mode + (#t (chmod file #o660)) + (n (chmod file n)) + (#f 0))) + (define (ensure-socket-dir dir user group) + (unless (file-exists? dir) + (mkdir dir) ; FIXME: use mkdir-p instead? + (let ((uid (passwd:uid (getpwnam user))) + (gid (group:gid (getgrnam group)))) + (chown dir uid gid)))) + (define start-fcgiwrap + (make-forkexec-constructor + '(#$(file-append package "/sbin/fcgiwrap") + "-s" #$socket) + #:user #$user + #:group #$group + #:log-file #$log-file)) + (match '#$parsed-socket + (('unix path) + ;; Clean up socket, otherwise fcgiwrap might not start pr= operly. + (clean-up path) + (when #$ensure-dir? + (ensure-socket-dir (dirname path) #$user #$group)) + (let ((pid (start-fcgiwrap)) + (socket-exists? (wait-for-file path))) + (if socket-exists? + (adjust-permissions path #$perm) + (local-output + #$(G_ "fcgiwrap: warning: waiting for socket ~s failed") + path)) + pid)) + (_ (start-fcgiwrap))))) + (stop #~(make-kill-destructor))))))) =20 (define fcgiwrap-service-type (service-type (name 'fcgiwrap) (extensions (list (service-extension shepherd-root-service-type fcgiwrap-shepherd-service) - (service-extension account-service-type + (service-extension account-service-type fcgiwrap-accounts))) (default-value (fcgiwrap-configuration)))) =20 --=20 2.20.1 --------------0EC48590587DD7B3CAB39C78--