]> git.immae.eu Git - perso/Immae/Config/Nix.git/commitdiff
Migrate FTP access ssh keys
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 25 Nov 2023 23:00:56 +0000 (00:00 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 25 Nov 2023 23:10:19 +0000 (00:10 +0100)
deploy/flake.lock
flake.lock
flakes/flake.lock
systems/eldiron/ftp_sync.sh

index cda30082ba7035c87043ef5b3e5f4b88a27a1b15..f2517ef5d3785c2765baceed4ed307217ea0b1f6 100644 (file)
       },
       "locked": {
         "lastModified": 1,
-        "narHash": "sha256-uN1hah0fHMQoPHlU2DaCZSe6VEgbTbte//c7rLSwYQM=",
+        "narHash": "sha256-ICKEuT8YaVKsXgdau986YYMhcH+DfeOyD3XQBKUTMOM=",
         "path": "../flakes",
         "type": "path"
       },
       },
       "locked": {
         "lastModified": 1,
-        "narHash": "sha256-k4JkkQECOTq1uxe8nZe0Wmj+DfzX1KKF7lvDkEMK6vQ=",
+        "narHash": "sha256-PRhCKLyFpkmjr/RviVw7h3ZBWVHGmlwH1+z9gWIdXsI=",
         "path": "../systems/eldiron",
         "type": "path"
       },
index d1f5a88c3140368afbdf7de81c2c5bab94aad9a5..47a4bd660c00364575444954893bbe41c688f01c 100644 (file)
       },
       "locked": {
         "lastModified": 1,
-        "narHash": "sha256-uN1hah0fHMQoPHlU2DaCZSe6VEgbTbte//c7rLSwYQM=",
+        "narHash": "sha256-ICKEuT8YaVKsXgdau986YYMhcH+DfeOyD3XQBKUTMOM=",
         "path": "./flakes",
         "type": "path"
       },
       },
       "locked": {
         "lastModified": 1,
-        "narHash": "sha256-k4JkkQECOTq1uxe8nZe0Wmj+DfzX1KKF7lvDkEMK6vQ=",
+        "narHash": "sha256-PRhCKLyFpkmjr/RviVw7h3ZBWVHGmlwH1+z9gWIdXsI=",
         "path": "../systems/eldiron",
         "type": "path"
       },
index 64c9100114904dda08a5eecdbe46b8b07c5e140a..6dcee3da21b2f95925911096f596deb0c0fd2847 100644 (file)
       },
       "locked": {
         "lastModified": 1,
-        "narHash": "sha256-k4JkkQECOTq1uxe8nZe0Wmj+DfzX1KKF7lvDkEMK6vQ=",
+        "narHash": "sha256-PRhCKLyFpkmjr/RviVw7h3ZBWVHGmlwH1+z9gWIdXsI=",
         "path": "../systems/eldiron",
         "type": "path"
       },
index aff7178007032c9f20fec8c37c776e8fabb7c634..6760aab8c51ef0a09b9b2ddc3c4285210b882d96 100755 (executable)
@@ -7,41 +7,21 @@ LDAP_PASS=$(cat /etc/ssh/ldap_password)
 LDAP_HOST="ldap://ldap.immae.eu"
 LDAP_BASE="dc=immae,dc=eu"
 LDAP_FILTER="(memberOf=cn=users,cn=ftp,ou=services,dc=immae,dc=eu)"
+USER_LDAP_BASE="ou=users,dc=immae,dc=eu"
 
-handle_keys() {
-  uids="$1"
-  keys="$2"
-  if [ -n "$uids" ]; then
-    for uid in $uids; do
-      echo "$keys" | while read key; do
-        if [ -n "$key" ]; then
-          ssh-keygen -e -f <(echo "$key")
-        fi
-      done > /var/lib/proftpd/authorized_keys/$uid
-    done
-  fi
-}
+PSQL_BASE="immae"
+PSQL_HOST="localhost"
+PSQL_USER="immae_auth_read"
+PSQL_PASS=$(cat /etc/ssh/psql_password)
 
 mkdir -p /var/lib/proftpd/authorized_keys
 
-while read i; do
-  if [[ "$i" =~ ^dn: ]]; then
-    handle_keys "$uids" "$keys"
-    uids=""
-    keys=""
-  fi;
-  if [[ "$i" =~ ^uid: ]]; then
-    uids="$uids ${i#uid: }"
-  fi
-  if [[ "$i" =~ ^immaeSshKey: ]]; then
-    key="${i#immaeSshKey: }"
-    if [[ "$key" =~ ^ssh- ]]; then
-      keys="$keys
-$key"
-    elif echo "$key" | cut -d" " -f1 | grep -q "\bftp\b"; then
-      keys="$keys
-$(echo "$key" | cut -d" " -f2-)"
-    fi
-  fi
-done < <(ldapsearch -H "$LDAP_HOST" -ZZ -LLL -D "$LDAP_BIND" -w "$LDAP_PASS" -b "$LDAP_BASE" -x -o ldif-wrap=no "$LDAP_FILTER" uid immaeSshKey)
-handle_keys "$uids" "$keys"
+allowed_logins=$(ldapsearch -H "$LDAP_HOST" -ZZ -LLL -D "$LDAP_BIND" -w "$LDAP_PASS" -b "$LDAP_BASE" -x -o ldif-wrap=no "$LDAP_FILTER" '' \
+    | grep "^dn.*$USER_LDAP_BASE$" \
+    | sed -e "s/^dn: uid=\([^,]*\),.*$USER_LDAP_BASE$/'\1'/" \
+    | paste -sd,)
+
+PGPASSWORD="$PSQL_PASS" psql -U "$PSQL_USER" -h "$PSQL_HOST" -X -A -t -d "$PSQL_BASE" -c "SELECT login, key FROM ldap_users_ssh_keys WHERE realm = 'immae' AND 'ftp' = ANY(usage) AND login IN ($allowed_logins);" | while IFS='|' read user key; do
+  touch /var/lib/proftpd/authorized_keys/$user
+  ssh-keygen -e -f <(echo "$key") >> /var/lib/proftpd/authorized_keys/$user
+done