]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/private/ftp_sync.sh
Migrate to proftpd
[perso/Immae/Config/Nix.git] / modules / private / ftp_sync.sh
diff --git a/modules/private/ftp_sync.sh b/modules/private/ftp_sync.sh
new file mode 100755 (executable)
index 0000000..8b0d9c5
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+
+LDAPSEARCH=ldapsearch
+
+LDAP_BIND="cn=ssh,ou=services,dc=immae,dc=eu"
+LDAP_PASS=$(cat /etc/ssh/ldap_password)
+LDAP_HOST="ldap.immae.eu"
+LDAP_BASE="dc=immae,dc=eu"
+LDAP_FILTER="(memberOf=cn=users,cn=ftp,ou=services,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
+}
+
+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"