aboutsummaryrefslogtreecommitdiff
path: root/src/connection/setuid.lisp
blob: 5c210206a9b0e1b4c61d3785c5f6a04c1659bf85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;;; Consfigurator -- Lisp declarative configuration management system

;;; Copyright (C) 2021  Sean Whitton <spwhitton@spwhitton.name>

;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3, or (at your option)
;;; any later version.

;;; This file is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.

;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(in-package :consfigurator.connection.setuid)
(named-readtables:in-readtable :consfigurator)

(defclass setuid-connection (rehome-connection fork-connection) ())

(defmethod establish-connection ((type (eql :setuid)) remaining &key user)
  (unless (and (lisp-connection-p) (zerop (nix:geteuid)))
    (error "~&SETUIDing requires a Lisp image running as root"))
  (informat 1 "~&SETUIDing to ~A" user)
  (let* ((ent
           (or (user:user-info user)
               (failed-change "~&Could not look up user info for ~A." user)))
	 (xdg-cache-home
           (ensure-directory-pathname
            (stripln
             ;; su(1) is not POSIX but very likely to be present.  Note that
             ;; the -c argument here is to the user's login shell, not the -c
             ;; argument to su(1) on, e.g., FreeBSD.  So should be fairly
             ;; portable.
             (mrun "su" (cdr (assoc :name ent))
		   "-c" "echo ${XDG_CACHE_HOME:-$HOME/.cache}"))))
         (cache (merge-pathnames "consfigurator/" xdg-cache-home))
         (datadir (merge-pathnames "data/" cache)))
    (dolist (dir (list xdg-cache-home cache datadir))
      (unless (directory-exists-p dir)
        (nix:chown (ensure-directories-exist dir)
                   (cdr (assoc :user-id ent)) (cdr (assoc :group-id ent)))))
    (continue-connection
     (make-instance
      'setuid-connection
      :rehome-datadir datadir
      :connattrs `(:remote-uid ,(cdr (assoc :user-id ent))
                   :remote-gid ,(cdr (assoc :group-id ent))
                   :remote-user ,(cdr (assoc :name ent))
                   :remote-home ,(ensure-directory-pathname
                                  (cdr (assoc :home ent)))
                   :consfigurator-cache ,cache
                   :XDG_CACHE_HOME ,xdg-cache-home))
     remaining)))

(defmethod post-fork ((connection setuid-connection))
  (let ((uid (connection-connattr connection :remote-uid))
        (gid (connection-connattr connection :remote-gid))
        (user (connection-connattr connection :remote-user)))
    (run-program
     (list "chown" "-R"
           (format nil "~A:~A" uid gid)
           (unix-namestring (slot-value connection 'rehome-datadir))))
    (posix-login-environment
     uid user (connection-connattr connection :remote-home))
    ;; We are privileged, so this sets the real, effective and saved IDs.
    (nix:setgid gid) (nix:initgroups user gid) (nix:setuid uid)))

(defmethod propagate-connattr
    ((type (eql :no-services)) connattr (connection setuid-connection))
  connattr)