X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=vm_remote;h=cf899d72592be1b6a39fdeac4a8b82f0f054d76e;hb=fcd70fe078a04a9bfe068a8c68d3e52c0ee6c8b2;hp=9b37839800975501713c515d4b7b8169e900974d;hpb=910b5358868be624ddf79e5c8e8dd46b0d403b4b;p=lhc%2Fateliers.git diff --git a/vm_remote b/vm_remote index 9b37839..cf899d7 100755 --- a/vm_remote +++ b/vm_remote @@ -1,7 +1,7 @@ #!/bin/sh set -e -f ${DRY_RUN:+-n} -u tool=${0%/*} -. "$tool"/lib/functions.sh +. "$tool"/lib/rule.sh . "$tool"/etc/vm.sh rule_help () { # SYNTAX: [--hidden] @@ -10,7 +10,8 @@ rule_help () { # SYNTAX: [--hidden] DESCRIPTION: ce script regroupe des règles pour administrer la VM ($vm_fqdn) _depuis_ une machine distante ; - il sert à la fois d'outil et de documentation. + il sert à la fois d'outil (aisément bidouillable) + et de documentation (préçise). Voir \`$tool/vm_host' pour les règles côté machine hôte ($vm_host). Voir \`$tool/vm_hosted' pour les règles côté VM hébergée ($vm_fqdn). SYNTAX: $0 \$RULE \${RULE}_SYNTAX @@ -22,24 +23,32 @@ rule_help () { # SYNTAX: [--hidden] EOF } -rule_git_config () { # DESCRIPTION: configure le .git local pour bien pousser +rule_git_configure () { # DESCRIPTION: configure ./.git correctement ( cd "$tool" git remote rm host || true git remote add host $vm_host:tool/vm - git config --replace remote.host.push HEAD:refs/heads/origin + git config --replace remote.host.push HEAD:refs/remotes/master git remote rm hosted || true git remote add hosted root@$vm_fqdn:tool/vm - git config --replace remote.hosted.push HEAD:refs/heads/origin + git config --replace remote.hosted.push HEAD:refs/remotes/master + git submodule update --init ) } rule_git_push () { # SYNTAX: {host|hosted} $git_push_options + ( + cd "$tool" local remote=${1#remote=}; shift - GIT_SSH=./vm_ssh git push -v "$remote" "$@" + GIT_SSH=./lib/ssh git push -v "$remote" "$@" + info "penser à faire : vm_hosted git_reset" + ) } rule_ssh () { - "$tool"/vm_ssh $vm_fqdn "$@" + "$tool"/lib/ssh $vm_fqdn "$@" + } +rule_mosh () { + mosh --ssh="$tool/lib/ssh $*" $vm_fqdn } rule__ssh_known_hosts_update () { rule ssh \ @@ -48,29 +57,79 @@ rule__ssh_known_hosts_update () { -o HashKnownHosts=no \ whoami } -rule_key_disk_send () { - gpg --decrypt var/lib/luks/$vm_fqdn.key.gpg | - "$tool"/vm_ssh root@$vm_fqdn "$@" \ + +rule__x509_service_key_send_deciphered () { # SYNTAX: $service $remote_destination ${ssh_options-} + local service="$1"; shift + local remote_destination="$1"; shift + gpg --decrypt "var/sec/x509/service/$service/key.pass.gpg" | + openssl rsa -passin 'stdin' \ + -in "var/sec/x509/service/$service/key.pem" \ + -out '/dev/stdout' | + rule ssh "$@" ' \ + install -m 400 -o root -g root \ + /dev/stdin \ + "'"$remote_destination"'" \ + ' + } + +rule_luks_key_send () { # DESCRIPTION: envoie la clef de déchiffrement des partitions au démarrage de la VM. + gpg --decrypt var/sec/luks/$vm_fqdn.key.gpg | + "$tool"/lib/ssh root@$vm_fqdn "$@" \ -o CheckHostIP=no \ -o HostKeyAlias=init.$vm_fqdn \ tee /lib/cryptsetup/passfifo \>/dev/null } -rule_key_disk_backup () { +rule_luks_key_backup () { # SYNTAX: ${gpg_options:---recipient $USER@} DESCRIPTION: sauvegarde localement les entêtes des partitions chiffrées. + test "${*+set}" || set -- --recipient "$USER@" for part in root var home do - mkdir -p var/lib/luks + mkdir -p var/sec/luks rule ssh -l root ' \ - tmp=$(mktemp) - cryptsetup luksHeaderBackup \ - /dev/$vm_lvm_vg/${vm_lvm_lv}_${part} \ - --header-backup-file "$tmp" \ - cat "$tmp" - shred --remove "$tmp" + set -e -f -u; + exec 2>/dev/null; + tmp=$(mktemp -t "luks.'"$part"'.XXXXXXXX.tmp" --dry-run); + cryptsetup luksHeaderBackup >/dev/null \ + /dev/'"$vm_lvm_vg"'/'"$vm_lvm_lv"'_'"$part"' \ + --header-backup-file "$tmp"; \ + cat "$tmp"; + shred >/dev/null --remove "$tmp"; \ ' | - gpg --encrypt --recipient $USER@ \ - -o var/lib/luks/${vm_lvm_lv}_${part}.luks.gpg + gpg "$@" --encrypt \ + -o var/sec/luks/${vm_lvm_lv}_${part}.luks.gpg done } +rule_dovecot_key_send () { + rule ssh -l root ' \ + sudo install -d -m 770 -o root -g root \ + /etc/dovecot/'"$vm_domainname"'/ \ + /etc/dovecot/'"$vm_domainname"'/imap \ + /etc/dovecot/'"$vm_domainname"'/imap/x509 ; \ + sudo install -m 644 -o root -g root /dev/stdin \ + /etc/dovecot/'"$vm_domainname"'/imap/x509/.gitignore <<-EOF + key.pem + EOF + ' + rule _x509_service_key_send_deciphered imap \ + /etc/dovecot/$vm_domainname/imap/x509/key.pem -l root "$@" + } +rule_postfix_key_send () { + rule ssh -l root ' \ + sudo install -d -m 770 -o root -g root \ + /etc/postfix/'"$vm_domainname"'/ \ + /etc/postfix/'"$vm_domainname"'/smptd \ + /etc/postfix/'"$vm_domainname"'/smptd/x509; \ + sudo install -m 644 -o root -g root /dev/stdin \ + /etc/postfix/'"$vm_domainname"'/smtp/x509/.gitignore <<-EOF + key.pem + EOF + sudo install -m 644 -o root -g root /dev/stdin \ + /etc/postfix/'"$vm_domainname"'/smtpd/x509/.gitignore <<-EOF + key.pem + EOF + ' + rule _x509_service_key_send_deciphered smtpd \ + /etc/postfix/$vm_domainname/smtpd/x509/key.pem -l root "$@" + } rule=${1:-help} ${1+shift}