aboutsummaryrefslogblamecommitdiff
path: root/systems/eldiron/ftp_sync.sh
blob: aff7178007032c9f20fec8c37c776e8fabb7c634 (plain) (tree)
1
2
3
4
5
6
7





                                             
                                





































                                                                   
                                                                                                                                               
                           
#!/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://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"