aboutsummaryrefslogtreecommitdiff
path: root/src/connection/setuid.lisp
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2021-03-23 21:23:40 -0700
committerSean Whitton <spwhitton@spwhitton.name>2021-03-23 21:51:35 -0700
commit1cd0000f281b33e9faecb61687a23195c5d1ae15 (patch)
treee77b3c15dd5bb5e4d474fe36a39618f6a6d2454a /src/connection/setuid.lisp
parentc32a1ee21df66606da67a09a58949dfe9e919191 (diff)
downloadconsfigurator-1cd0000f281b33e9faecb61687a23195c5d1ae15.tar.gz
add :SETUID connection type
Signed-off-by: Sean Whitton <spwhitton@spwhitton.name>
Diffstat (limited to 'src/connection/setuid.lisp')
-rw-r--r--src/connection/setuid.lisp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/connection/setuid.lisp b/src/connection/setuid.lisp
new file mode 100644
index 0000000..4ca12fc
--- /dev/null
+++ b/src/connection/setuid.lisp
@@ -0,0 +1,40 @@
+;;; 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)
+#+sbcl (eval-when (:compile-toplevel :load-toplevel :execute)
+ (require "sb-posix"))
+
+(defun setuid (uid)
+ #+sbcl (sb-posix:setuid uid)
+ #-(or sbcl) (foreign-funcall "setuid" :unsigned-int uid :int))
+
+(defun setgid (gid)
+ #+sbcl (sb-posix:setgid gid)
+ #-(or sbcl) (foreign-funcall "setgid" :unsigned-int uid :int))
+
+(defmethod establish-connection ((type (eql :setuid)) remaining &key to)
+ (informat 1 "~&SETUIDing to ~A" to)
+ (re:register-groups-bind ((#'parse-integer uid gid))
+ (#?/uid=([0-9]+).+gid=([0-9]+)/ (mrun "id" to))
+ (with-fork-connection (remaining)
+ (unless (zerop (setgid gid))
+ (error "setgid(2) failed; are you root?"))
+ (unless (zerop (setuid uid))
+ (error "setuid(2) failed; are you root?"))
+ (uiop:chdir (user:passwd-entry 5 uid)))))