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