1270096Strasz#!/bin/sh
2270096Strasz#
3270096Strasz# $FreeBSD$
4270096Strasz#
5270096Strasz
6270096Strasz# Modify this to suit your needs.  The "$1" is the map name, eg. "auto_master".
7270096Strasz# To debug, simply run this script with map name as the only parameter.  It's
8270096Strasz# supposed to output map contents ("key location" pairs) to standard output.
9270096StraszSEARCHBASE="ou=$1,dc=example,dc=com"
10270096StraszENTRY_ATTRIBUTE="cn"
11270096StraszVALUE_ATTRIBUTE="automountInformation"
12270096Strasz
13270096Strasz/usr/local/bin/ldapsearch -LLL -x -o ldif-wrap=no -b "$SEARCHBASE" "$ENTRY_ATTRIBUTE" "$VALUE_ATTRIBUTE" | awk '
14270096Strasz$1 == "'$ENTRY_ATTRIBUTE':" {
15270096Strasz	key = $2
16270096Strasz}
17270096Strasz
18282961Strasz$1 == "'$VALUE_ATTRIBUTE':" {
19282961Strasz	for (i = 2; i <= NF; i++) {
20282961Strasz		value[i] = $(i)
21270096Strasz	}
22282961Strasz	nvalues = NF
23282961Strasz	b64 = 0
24270096Strasz}
25270096Strasz
26270096Strasz# Double colon after attribute name means the value is in Base64.
27282961Strasz$1 == "'$VALUE_ATTRIBUTE'::" {
28282961Strasz	for (i = 2; i <= NF; i++) {
29282961Strasz		value[i] = $(i)
30282961Strasz	}
31282961Strasz	nvalues = NF
32282961Strasz	b64 = 1
33282961Strasz}
34282961Strasz
35282961Strasz# Empty line - end of record.
36282961StraszNF == 0 && key != "" && nvalues > 0 {
37270096Strasz	printf "%s%s", key, OFS
38282961Strasz	for (i = 2; i < nvalues; i++) {
39282961Strasz		printf "%s%s", value[i], OFS
40270096Strasz	}
41282961Strasz	if (b64 == 1) {
42282961Strasz		printf "%s", value[nvalues] | "b64decode -rp"
43282961Strasz		close("b64decode -rp")
44282961Strasz		printf "%s", ORS
45282961Strasz	} else {
46282961Strasz		printf "%s%s", value[nvalues], ORS
47282961Strasz	}
48270096Strasz}
49282961Strasz
50282961StraszNF == 0 {
51282961Strasz	key = ""
52282961Strasz	nvalues = 0
53282961Strasz	delete value
54282961Strasz}
55270096Strasz'
56