Hash :
408808cf
Author :
Thomas de Grivel
Date :
2022-11-03T21:12:39
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 74 75 76 77 78 79 80 81 82 83 84
;; Adams - UNIX system administration tool written in Common Lisp
;; Copyright 2013-2022 Thomas de Grivel <thodg@kmx.io>
(in-package :adams)
(in-re-readtable)
(define-resource-class ssh-authorized-key ()
()
((probe-ssh-authorized-key :properties (:ensure :name :pubkey :type)))
((op-ssh-authorized-key :properties (:ensure :name :pubkey :type))))
(define-syntax ssh-public-key (type pubkey name)
#~|([^\s]+)\s+([^\s]+)\s+(.*)|)
(defun ssh-authorized-key (key)
(multiple-value-bind (type pubkey name) (parse-ssh-public-key key)
(resource 'ssh-authorized-key name
:type type
:pubkey pubkey
:name name)))
(defmethod probe-ssh-authorized-key ((res ssh-authorized-key)
(os os-unix))
(let* ((spec-type (the string (get-specified res :type)))
(spec-pubkey (the string (get-specified res :pubkey)))
(user *parent-resource*)
(home (resource-id (get-probed user :home)))
(path (str home "/.ssh/authorized_keys"))
(file (with-parent-resource *host*
(resource 'file path)))
(ensure :absent))
(multiple-value-bind (type pubkey name)
(unless (eq :absent (get-probed file :ensure))
(with-ssh-public-key (type pubkey name)
(run "cat " (sh-quote path))
(when (and (string= spec-type (the string type))
(string= spec-pubkey (the string pubkey)))
(setq ensure :present)
(return (values type pubkey name)))))
(properties* ensure type pubkey name))))
(defmethod op-ssh-authorized-key ((res ssh-authorized-key)
(os os-unix)
&key ensure type pubkey name)
(declare (type symbol ensure))
(let* ((user *parent-resource*)
(home (resource-id (get-probed user :home)))
(dot-ssh (str home "/.ssh"))
(ak (str dot-ssh "/authorized_keys"))
(sh-ak (sh-quote ak))
(sh-ak-tmp (sh-quote (str ak ".tmp"))))
(setf type (get-specified res :type)
pubkey (get-specified res :pubkey)
name (get-specified res :name))
(force-output)
(when (position ensure '(:absent nil))
(run "grep -v " (sh-quote pubkey) " " sh-ak " > " sh-ak-tmp)
(run "mv " sh-ak-tmp " " sh-ak))
(when (position ensure '(:present nil))
(run "echo " (sh-quote type " " pubkey " " name) " >> " sh-ak))))
(defmethod resource-additional-specs ((res ssh-authorized-key)
(os os-unix))
(format t "~& PARENT RESOURCE ~S~%" *parent-resource*)
(format t "~& HOME ~S~%" (get-specified *parent-resource* :home))
(force-output)
(let* ((user *parent-resource*)
(home (get-specified user :home))
(ssh-dir (str home "/.ssh"))
(ak (str ssh-dir "/authorized_keys"))
(owner (resource-id user))
(gid (get-probed user :gid)))
(with-parent-resource *host*
(resource 'directory ssh-dir
:ensure :present
:owner owner
:gid gid
:mode #o700)
(resource 'file ak
:ensure :present
:owner owner
:gid gid
:mode #o600))))