1#!/bin/sh
2#
3# $NetBSD: include_ldap,v 1.1 2018/01/09 03:31:14 christos Exp $
4#
5
6# Modify this to suit your needs.  The "$1" is the map name, eg. "auto_master".
7# To debug, simply run this script with map name as the only parameter.  It's
8# supposed to output map contents ("key location" pairs) to standard output.
9SEARCHBASE="ou=$1,dc=example,dc=com"
10ENTRY_ATTRIBUTE="cn"
11VALUE_ATTRIBUTE="automountInformation"
12
13ldapsearch -LLL -x -o ldif-wrap=no -b "$SEARCHBASE" "$ENTRY_ATTRIBUTE" "$VALUE_ATTRIBUTE" | awk '
14$1 == "'$ENTRY_ATTRIBUTE':" {
15	key = $2
16}
17
18$1 == "'$VALUE_ATTRIBUTE':" {
19	for (i = 2; i <= NF; i++) {
20		value[i] = $(i)
21	}
22	nvalues = NF
23	b64 = 0
24}
25
26# Double colon after attribute name means the value is in Base64.
27$1 == "'$VALUE_ATTRIBUTE'::" {
28	for (i = 2; i <= NF; i++) {
29		value[i] = $(i)
30	}
31	nvalues = NF
32	b64 = 1
33}
34
35# Empty line - end of record.
36NF == 0 && key != "" && nvalues > 0 {
37	printf "%s%s", key, OFS
38	for (i = 2; i < nvalues; i++) {
39		printf "%s%s", value[i], OFS
40	}
41	if (b64 == 1) {
42		printf "%s", value[nvalues] | "b64decode -rp"
43		close("b64decode -rp")
44		printf "%s", ORS
45	} else {
46		printf "%s%s", value[nvalues], ORS
47	}
48}
49
50NF == 0 {
51	key = ""
52	nvalues = 0
53	delete value
54}
55'
56