]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - flakes/private/ssh/ldap_authorized_keys.sh
Squash changes containing private information
[perso/Immae/Config/Nix.git] / flakes / private / ssh / ldap_authorized_keys.sh
CommitLineData
5566d26d
IB
1#!/usr/bin/env bash
2
3LDAPSEARCH=ldapsearch
4KEY="immaeSshKey"
5LDAP_BIND="cn=ssh,ou=services,dc=immae,dc=eu"
ea7bf00c 6LDAP_PASS=$(cat /etc/ssh/ldap_password)
1a64deeb 7LDAP_HOST="ldap://ldap.immae.eu"
5566d26d 8LDAP_BASE="dc=immae,dc=eu"
1a64deeb
IB
9USER_LDAP_BASE="ou=users,dc=immae,dc=eu"
10
11PSQL_BASE="immae"
12PSQL_HOST="localhost"
13PSQL_USER="immae_auth_read"
14PSQL_PASS=$(cat /etc/ssh/psql_password)
5566d26d
IB
15
16suitable_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
35clean_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
49ldap_search() {
1a64deeb
IB
50 $LDAPSEARCH -H $LDAP_HOST -ZZ -b $LDAP_BASE -D $LDAP_BIND -w "$LDAP_PASS" -x -o ldif-wrap=no -LLL "$@"
51}
52
53psql_search() {
54 PGPASSWORD="$PSQL_PASS" psql -U "$PSQL_USER" -h "$PSQL_HOST" -X -A -t -d "$PSQL_BASE" -c "$@"
5566d26d
IB
55}
56
57ldap_keys() {
58 user=$1;
1b9150a5 59 @snippets@
5566d26d
IB
60}
61
62ldap_keys $@