aboutsummaryrefslogtreecommitdiff
path: root/modules/private/ftp_sync.sh
blob: 8b0d9c5f2736f69d10f45b3d177f37983fc246fb (plain) (blame)
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
#!/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"