Skip to content

Instantly share code, notes, and snippets.

@NickCrew
Last active November 6, 2021 18:26
Show Gist options
  • Save NickCrew/264a1ba1f460a883d4e0e9c7e3822029 to your computer and use it in GitHub Desktop.
Save NickCrew/264a1ba1f460a883d4e0e9c7e3822029 to your computer and use it in GitHub Desktop.
Synology AutoFS
#!/bin/bash
# Copyright (c) 2000-2014 Synology Inc. All rights reserved.
################### NOTE ###################
#
# Adjust following settings to match your LDAP server.
#
# If using TLS or ldaps://, you have to append LDAP server's certificate to
# your client (in the file specified by tls_cacert in ldap.conf).
#
############################################
# LDAP server URI, must starts with "ldap://" or "ldaps://".
LDAP_URI="ldap://syno720.lan.piggah.xyz"
# Search base of LDAP server.
BASE_DN="dc=lan,dc=piggah,dc=xyz"
# "yes" or "no" means use TLS for LDAP connections or not.
USE_TLS="no"
# User-defined domain name (login suffix).
DOMAIN_NAME="lan.piggah.xyz"
# NFS server, default use LDAP_URI.
NFS_SERVER="syno720.lan.piggah.xyz"
# NFS folder for home, default use /var/services/homes.
NFS_FOLDER="/volume1"
ToUpper() { ### <string>
echo "$1" | tr [:lower:] [:upper:]
}
ResolveDomainName() { ### <base_dn>
local token= domain=
for token in ${1//,/ }; do
domain="$domain.${token#*=}"
done
echo "`ToUpper ${domain:1}`"
}
key="$1"
opt="-LLLxH"
if [ "$USE_TLS" = "yes" ]; then
opt="-Z $opt"
fi
if [ -z "$key" ]; then
exit 0
fi
if [ -z "$NFS_SERVER" ]; then
# Use pre-defined NFS server if any.
NFS_SERVER="${LDAP_URI#ldap*://}"
fi
if [ -n "$NFS_FOLDER" ]; then
# Use pre-defined NFS folder if any.
echo "-fstype=nfs $NFS_SERVER:${NFS_FOLDER// /\\ }/${key// /\\ }"
exit 0
fi
if [ -z "$LDAP_URI" -o -z "$BASE_DN" ]; then
>&2 echo "empty LDAP_URI or BASE_DN, please check your '$0'"
exit 1
fi
if [ -z "$DOMAIN_NAME" ]; then
domain="`ResolveDomainName $BASE_DN`"
else
# Use pre-defined domain name (login suffix) if any.
domain="`ToUpper $DOMAIN_NAME`"
fi
uid="`ldapsearch $opt "$LDAP_URI" -b "$BASE_DN" "uid=$key" uidNumber | grep 'uidNumber:' | awk '{print $2}'`" if [ -z "$uid" ]; then
>&2 echo "no uid found for user '$key'"
exit 1
fi
if ! [[ "$uid" =~ ^[0-9]+ ]]; then
>&2 echo "invalid uid '$uid'"
exit 1
fi
num=$(( $uid >> 14 ))
echo "-fstype=nfs $NFS_SERVER:/var/services/homes/@LH-$domain/$num/${key// /\\ }-$uid"
# vim:ft=sh ts=4 sts=4 sw=4 et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment