summaryrefslogtreecommitdiffstats
path: root/doc/migrate-nis.sh
blob: d8dcb63d39396da2eb3245ceb6891ad98287810f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/sh

domain=`domainname`
server=`ypwhich -d $domain`
suffix=dc=example,dc=com
people=cn=Users
groups=cn=Group
ipa=false
realm=`echo "$domain" | tr '[a-z]' '[A-Z]'`
rfc2307bis=false
mergegroups=true
maps=
automap=false
help=false
email=false
containers=false
entries=true

object_from_attr()
{
	case "$1" in
	cn)
		containerobject=nsContainer
		;;
	dc)
		containerobject=domain
		;;
	ou)
		containerobject=organizationalUnit
		;;
	*)
		containerobject=extensibleObject
		;;
	esac
	echo $containerobject
}

migrate_passwd() {
	if $containers ; then
		nameattr=`echo "$people" | cut -f1 -d=`
		nameval=`echo "$people" | cut -f2- -d=`
		containerclass=`object_from_attr "$nameattr"`
		grep -v '^$' <<- EOF
		dn: $people,$suffix
		${nameattr}: ${nameval}
		objectClass: $containerclass
		EOF
		echo
	fi
	while read key value ; do
		if ! $entries ; then
			continue
		fi
		uid=`echo "$value" | cut -d: -f1`
		userpassword=`echo "$value" | cut -d: -f2`
		uidnumber=`echo "$value" | cut -d: -f3`
		gidnumber=`echo "$value" | cut -d: -f4`
		gecos=`echo "$value" | cut -d: -f5`
		homedirectory=`echo "$value" | cut -d: -f6`
		loginshell=`echo "$value" | cut -d: -f7`
		cn=`echo "$gecos" | cut -d, -f1`
		givenname=`echo "$gecos" | awk '{print $1}'`
		sn=`echo "$gecos" | awk '{print $NF}'`
		grep -v '^$' <<- EOF
			dn: uid=$uid,$people,$suffix
			objectClass: posixAccount
			uid: $uid
			uidNumber: $uidnumber
			gidNumber: $gidnumber
			homeDirectory: $homedirectory
			${userpassword:+userPassword: "{CRYPT}"$userpassword}
			${loginshell:+loginShell: $loginshell}
		EOF
		if $rfc2307bis || $ipa || $email ; then
			grep -v '^$' <<- EOF
			objectClass: inetOrgPerson
			objectClass: inetUser
			objectClass: organizationalPerson
			objectClass: person
			cn: ${cn:-$uid}
			sn: ${sn:-$uid}
			givenName: ${givenname:-$uid}
			mail: ${uid}@${domain}
			EOF
		fi
		if $ipa ; then
			grep -v '^$' <<- EOF
				objectClass: krbprincipalaux
				krbPrincipalName: $uid@$realm
			EOF
		fi
		echo
	done
}

migrate_group() {
	if $containers ; then
		nameattr=`echo "$groups" | cut -f1 -d=`
		nameval=`echo "$groups" | cut -f2- -d=`
		containerclass=`object_from_attr "$nameattr"`
		grep -v '^$' <<- EOF
		dn: $groups,$suffix
		${nameattr}: ${nameval}
		objectClass: $containerclass
		EOF
		echo
	fi
	while read key value ; do
		if ! $entries ; then
			continue
		fi
		gid=`echo "$value" | cut -d: -f1`
		userpassword=`echo "$value" | cut -d: -f2`
		gidnumber=`echo "$value" | cut -d: -f3`
		members=`echo "$value" | cut -d: -f4`
		grep -v '^$' <<- EOF
			dn: cn=$gid,$groups,$suffix
			objectClass: posixGroup
			cn: $gid
			gidNumber: $gidnumber
			${userpassword:+userPassword: "{CRYPT}"$userpassword}
		EOF
		if $rfc2307bis || $ipa ; then
			grep -v '^$' <<- EOF
				objectClass: groupOfNames
			EOF
			for member in `echo "$members" | sed 's:,: :g'` ; do
				echo member: uid=$member,$people,$suffix
			done
		else
			for member in `echo "$members" | sed 's:,: :g'` ; do
				echo memberUid: $member
			done
		fi
		echo
	done
}

migrate_automount() {
	if $containers ; then
		grep -v '^$' <<- EOF
			dn: automountMapName=$1,$suffix
			objectClass: automountMap
			automountMapName: $1
		EOF
		echo
	fi
	while read key value ; do
		if ! $entries ; then
			continue
		fi
		grep -v '^$' <<- EOF
			dn: automountKey=$key,automountMap=$1,$suffix
			objectClass: automount
			automountKey: $key
			automountInformation: $value
		EOF
		echo
	done
}

migrate_nis() {
	if $containers ; then
		grep -v '^$' <<- EOF
			dn: nisMapName=$1,$suffix
			objectClass: nisMap
			automountMapName: $1
		EOF
		echo
	fi
	while read key value ; do
		if ! $entries ; then
			continue
		fi
		grep -v '^$' <<- EOF
			dn: cn=$key,automountMap=$1,$suffix
			objectClass: nisObject
			nisMapName: $1
			cn: $key
			nisEntry: $value
		EOF
		echo
	done
}

mergegroups() {
	if $mergegroups ; then
		awk -F: '
		BEGIN { OFS=":" }
		{ 
			if ((length(NAMES[$3]) == 0) ||
			    (length(NAMES[$3]) > length($1))) {
				NAMES[$3] = $1
			}
			GIDS[$3] = $3
			PASS[$3] = $2
			if (length(MEMBERS[$3]) > 0) {
				MEMBERS[$3] = MEMBERS[$3] "," $4
			} else {
				MEMBERS[$3] = $4
			}
		}
		END {
			for (GID in GIDS) {
				print NAMES[GID],PASS[GID],GID,MEMBERS[GID]
			}
		}'
	else
		cat
	fi
}

get_map() {
	case "$1" in
	passwd*)
		ypcat -k ${server:+-h $server} ${domain:+-d $domain} passwd.byname | sort
		;;
	group*)
		ypcat -k ${server:+-h $server} ${domain:+-d $domain} group.byname | mergegroups | sort
		;;
	*)
		ypcat -k ${server:+-h $server} ${domain:+-d $domain} "$1" | sort
		;;
	esac
}

migrate_map() {
	case "$1" in
	passwd*)
		(get_map "$1" || echo) | migrate_passwd
		;;
	group*)
		(get_map "$1" || echo) | migrate_group
		;;
	auto.*|auto_*)
		(get_map "$1" || echo) | migrate_automount "$1"
		;;
	*)
		(get_map "$1" || echo) | migrate_nis "$1"
		;;
	esac
}

while test $# -gt 0 ; do
	case "$1" in
	--domain=*)
		domain=`echo "$1" | cut -f2- -d=`
		automap=false
		;;
	--domain)
		shift
		domain="$1"
		automap=false
		;;
	--server=*)
		server=`echo "$1" | cut -f2- -d=`
		automap=false
		;;
	--server)
		shift
		server="$1"
		automap=false
		;;
	--suffix=*)
		suffix=`echo "$1" | cut -f2- -d=`
		;;
	--suffix)
		shift
		suffix="$1"
		;;
	--people=*)
		people=`echo "$1" | cut -f2- -d=`
		;;
	--people)
		shift
		people="$1"
		;;
	--groups=*)
		groups=`echo "$1" | cut -f2- -d=`
		;;
	--groups)
		shift
		groups="$1"
		;;
	--nomergegroups)
		mergegroups=false
		;;
	--rfc2307bis)
		rfc2307bis=true
		;;
	--ipa)
		ipa=true
		;;
	--email)
		email=true
		;;
	--realm=*)
		realm=`echo "$1" | cut -f2- -d= | tr '[a-z]' '[A-Z]'`
		automap=false
		;;
	--realm)
		shift
		realm=`echo "$1"                | tr '[a-z]' '[A-Z]'`
		automap=false
		;;
	-a|--all)
		automap=true
		;;
	--containers)
		containers=true
		;;
	--just-containers)
		containers=true
		entries=false
		;;
	-*|-h|--help)
		help=true
		;;
	*)
		maps="${maps:+$maps }$1"
		;;
	esac
	shift
done

if $automap && test -z "$maps" ; then
	maps=`./ypmaplist.py`
fi
if $help || test -z "$maps" ; then
	echo `basename $0`: create LDIF from NIS maps
	echo Usage: `basename $0` "[options] [mapname [...]]"
	cat <<- EOF
	Options:
	-h --help		Print this text.
	--domain		Query maps for a non-default domain (default is
	 			"$domain").
	--server		Query a non-default server (default is
	 			"$server").
	--suffix		Store entries under a non-default suffix (default is
	 			"$suffix").
	--people		Store account entries under a non-default container
	 			under the suffix (default is "$people").
	--groups		Store group entries under a non-default container
	 			under the suffix (default is "$groups").
	--nomergegroups		Don't merge group entries which have the same GID.
	--rfc2307bis		Use groupOfNames groups, create user account
	 			entries which are also inetOrgPerson entries.
	--ipa			Use groupOfNames groups, create user account
	 			entries which are also inetOrgPerson and Kerberos
	 			user entries.
	--realm			Use a non-default Kerberos realm name (default is
	 			"$realm").
	--email			Add email addresses by default (default domain for
	 			mail addresses is "$domain").
	-a --all		Attempt to migrate all maps in the local domain.
	 			(Can not be used with either the --server or
	 			the --domain options.)
	--containers		Create containers for maps in addition to entries.
	--just-containers	Create containers for maps, but not for entries.
	EOF
else
	seen_passwd=false
	seen_group=false
	for map in $maps ; do
		seen_before=false
		case "$map" in
		*.by*)
			base=`echo "$map" | sed 's,\.by.*,,g'`
			case $base in
			passwd)
				if $seen_passwd ; then
					seen_before=true
				fi
				seen_passwd=true
				;;
			group)
				if $seen_group ; then
					seen_before=true
				fi
				seen_group=true
				;;
			esac
			;;
		esac
		$seen_before || migrate_map "$map"
	done
fi