]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - flakes/private/ssh/ldap_authorized_keys.sh
Add config for CI
[perso/Immae/Config/Nix.git] / flakes / private / ssh / ldap_authorized_keys.sh
1 #!/usr/bin/env bash
2
3 LDAPSEARCH=ldapsearch
4 KEY="immaeSshKey"
5 LDAP_BIND="cn=ssh,ou=services,dc=immae,dc=eu"
6 LDAP_PASS=$(cat /etc/ssh/ldap_password)
7 LDAP_HOST="ldap://ldap.immae.eu"
8 LDAP_BASE="dc=immae,dc=eu"
9 USER_LDAP_BASE="ou=users,dc=immae,dc=eu"
10
11 PSQL_BASE="immae"
12 PSQL_HOST="localhost"
13 PSQL_USER="immae_auth_read"
14 PSQL_PASS=$(cat /etc/ssh/psql_password)
15
16 suitable_for() {
17 type_for="$1"
18 key="$2"
19
20 if [[ $key != *$'\n'* ]] && [[ $key == ssh-* ]]; then
21 echo "$key"
22 else
23 key_type=$(cut -d " " -f 1 <<< "$key")
24
25 if grep -q "\b-$type_for\b" <<< "$key_type"; then
26 echo ""
27 elif grep -q "\b$type_for\b" <<< "$key_type"; then
28 echo $(sed -e "s/^[^ ]* //g" <<< "$key")
29 else
30 echo ""
31 fi
32 fi
33 }
34
35 clean_key_line() {
36 type_for="$1"
37 line="$2"
38
39 if [[ "$line" == $KEY::* ]]; then
40 # base64 keys should't happen, unless wrong copy-pasting
41 key=""
42 else
43 key=$(sed -e "s/^$KEY: *//" -e "s/ *$//" <<< "$line")
44 fi
45
46 suitable_for "$type_for" "$key"
47 }
48
49 ldap_search() {
50 $LDAPSEARCH -H $LDAP_HOST -ZZ -b $LDAP_BASE -D $LDAP_BIND -w "$LDAP_PASS" -x -o ldif-wrap=no -LLL "$@"
51 }
52
53 psql_search() {
54 PGPASSWORD="$PSQL_PASS" psql -U "$PSQL_USER" -h "$PSQL_HOST" -X -A -t -d "$PSQL_BASE" -c "$@"
55 }
56
57 ldap_keys() {
58 user=$1;
59 @snippets@
60 }
61
62 ldap_keys $@