xref: /titanic_50/usr/src/cmd/ypcmd/yp2lscripts/ypmap2src.sh (revision 0e42dee69ed771bf604dd1789fca9d77b5bbe302)
1#! /usr/bin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# ident	"%Z%%M%	%I%	%E% SMI"
25#
26# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ypmap2src -- script to generate source files from YP maps.
30#
31
32
33# Please save a copy of this script before making any changes.
34
35
36usage()
37{
38echo "Usage: $PROG [-t] [[-c custom-map-name] ...] [-d domain] -o output-directory [[source-file] ...]"
39echo " t - Generate source files from TRADITIONAL NIS MAPS, default is NIS2LDAP maps."
40echo " c - Name of the custom map for which source file needs to be generated."
41echo " d - Specify a different domain, default is local system domain name."
42echo " o - Specify the output directory where source files can be generated."
43echo "source-file - The name of the source file for which needs to be generated."
44exit 0
45}
46
47parse_argument()
48{
49while getopts "tc:d:o:" ARG
50do
51  case $ARG in
52
53    t) N2LPREFIX=""
54       MAP_LIST="$NIS_ONLY_MAP_LIST"
55       ;;
56    c) CUST_LIST="$CUST_LIST $OPTARG"
57       ;;
58    d) DOMAIN=$OPTARG
59       MAPDIR=/var/yp/"$DOMAIN"
60       ;;
61    o) OUTDIR=$OPTARG
62       ;;
63    *) echo "ERROR : Invalid argument"
64       usage
65       exit 1
66       ;;
67  esac
68done
69
70# This is to handle if "-t" is supplied after "-c"
71for MAP in $CUST_LIST
72do
73  CUST_MAP_LIST="$CUST_MAP_LIST ${N2LPREFIX}$MAP"
74done
75
76if [ -z "$OUTDIR" ]; then
77  echo "ERROR : output directory has to be specified."
78  usage
79  exit 1
80fi
81
82# Set source list if supplied
83shift `expr $OPTIND - 1`
84CMDLINE_SRC_LIST="$@"
85
86[ $DEBUG -eq 1 ] && echo CMDLINE_SRC_LIST = $CMDLINE_SRC_LIST
87
88# If source(s) supplied on command line, then generate ONLY those file(s).
89
90if [ "$CMDLINE_SRC_LIST" != "" ]; then
91  MAP_LIST=""
92  CMDLINE_SRCS=1
93
94  for SRC in $CMDLINE_SRC_LIST
95  do
96    [ $DEBUG -eq 1 ] && echo Parsing Command line SRC = $SRC
97
98    case $SRC in
99      passwd )
100        MAP=${N2LPREFIX}passwd.byuid
101        MAP_LIST="$MAP_LIST $MAP"
102         ;;
103      group )
104        MAP=${N2LPREFIX}group.byname
105        MAP_LIST="$MAP_LIST $MAP"
106         ;;
107      hosts )
108        MAP=${N2LPREFIX}hosts.byaddr
109        MAP_LIST="$MAP_LIST $MAP"
110         ;;
111      ipnodes )
112        MAP=${N2LPREFIX}ipnodes.byaddr
113        MAP_LIST="$MAP_LIST $MAP"
114         ;;
115      ethers )
116        MAP=${N2LPREFIX}ethers.byname
117        MAP_LIST="$MAP_LIST $MAP"
118         ;;
119      networks )
120        MAP=${N2LPREFIX}networks.byaddr
121        MAP_LIST="$MAP_LIST $MAP"
122         ;;
123      rpc )
124        MAP=${N2LPREFIX}rpc.bynumber
125        MAP_LIST="$MAP_LIST $MAP"
126         ;;
127      services )
128        MAP=${N2LPREFIX}services.byname
129        MAP_LIST="$MAP_LIST $MAP"
130         ;;
131      protocols )
132        MAP=${N2LPREFIX}protocols.bynumber
133        MAP_LIST="$MAP_LIST $MAP"
134         ;;
135      netgroup )
136        MAP=${N2LPREFIX}netgroup
137        MAP_LIST="$MAP_LIST $MAP"
138         ;;
139      bootparams )
140        MAP=${N2LPREFIX}bootparams
141        MAP_LIST="$MAP_LIST $MAP"
142         ;;
143      aliases )
144        MAP=${N2LPREFIX}mail.aliases
145        MAP_LIST="$MAP_LIST $MAP"
146         ;;
147      publickey )
148        MAP=${N2LPREFIX}publickey.byname
149        MAP_LIST="$MAP_LIST $MAP"
150         ;;
151      netid )
152        MAP=${N2LPREFIX}netid.byname
153        MAP_LIST="$MAP_LIST $MAP"
154         ;;
155      netmasks )
156        MAP=${N2LPREFIX}netmasks.byaddr
157        MAP_LIST="$MAP_LIST $MAP"
158         ;;
159      passwd.adjunct )
160        MAP=${N2LPREFIX}passwd.adjunct.byname
161        MAP_LIST="$MAP_LIST $MAP"
162         ;;
163      group.adjunct )
164        MAP=${N2LPREFIX}group.adjunct.byname
165        MAP_LIST="$MAP_LIST $MAP"
166         ;;
167      timezone )
168        MAP=${N2LPREFIX}timezone.byname
169        MAP_LIST="$MAP_LIST $MAP"
170         ;;
171      auto.* )
172        MAP=${N2LPREFIX}${SRC}
173        MAP_LIST="$MAP_LIST $MAP"
174         ;;
175      auth_attr )
176        MAP=${N2LPREFIX}auth_attr
177        MAP_LIST="$MAP_LIST $MAP"
178         ;;
179      exec_attr )
180        MAP=${N2LPREFIX}exec_attr
181        MAP_LIST="$MAP_LIST $MAP"
182         ;;
183      prof_attr )
184        MAP=${N2LPREFIX}prof_attr
185        MAP_LIST="$MAP_LIST $MAP"
186         ;;
187      user_attr )
188        MAP=${N2LPREFIX}user_attr
189        MAP_LIST="$MAP_LIST $MAP"
190         ;;
191      audit_user )
192        MAP=${N2LPREFIX}audit_user
193        MAP_LIST="$MAP_LIST $MAP"
194         ;;
195     *) # Not a default source, could be a custom source.
196        # Then generate source files from all the available
197        # DBM files for this custom source.
198
199        MAPFOUND=0
200
201        for dbmfile in $MAPDIR/${N2LPREFIX}${SRC}.dir \
202            $MAPDIR/${N2LPREFIX}${SRC}.*.dir
203        do
204          MAP=`basename $dbmfile .dir`
205          if [ -f $MAPDIR/${MAP}.pag ]; then
206            MAPFOUND=1
207            CUST_MAP_LIST="$CUST_MAP_LIST $MAP"
208          fi
209        done
210
211        [ $MAPFOUND -eq 0 ] && \
212           echo ERROR : No maps found for $SRC. Skipping..
213        ;;
214   esac
215  done
216
217fi
218
219}
220
221
222is_root_user()
223{
224  case `id` in
225    uid=0\(root\)*) return 0
226                    ;;
227    * )             return 1
228                    ;;
229  esac
230}
231
232
233create_passwd()
234{
235SRCFILE=passwd
236SHADOW=shadow
237
238makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
239
240# Remove the YP operational lines
241grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
242  grep -v "YP_DOMAIN_NAME $DOMAIN" |
243  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
244
245# Remove the key
246cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
247
248# Sort the entries in ascending order of uid
249sort -n -t: -k3,3 $TMPDIR/${MAP}.cut > $TMPDIR/${MAP}.sort
250
251# If passwd.adjunct is used, the actual password is stored in
252# this map, and the passwd map contains "##<uid>" as the passwd.
253# In that case, do not generate the shadow file.
254
255UID=`head -1 $TMPDIR/${MAP}.sort | cut -f1 -d:`
256PSWD=`head -1 $TMPDIR/${MAP}.sort | cut -f2 -d:`
257if [ "$PSWD" != "##${UID}" ]; then
258
259  #Create the shadow file with blank passwd aging information
260  cut -f 1,2 -d: $TMPDIR/${MAP}.sort |
261      sed 's/$/:::::::/' > $OUTDIR/$SHADOW
262
263  #Make the shadow file readable to root only
264  chmod 400 $OUTDIR/$SHADOW
265
266  #Create the passwd file with "x" as the passwd
267  awk ' BEGIN { FS = ":"; OFS = ":"}
268        {$2 = "x"; print}' $TMPDIR/${MAP}.sort > $OUTDIR/$SRCFILE
269else
270  cp $TMPDIR/${MAP}.sort $OUTDIR/$SRCFILE
271fi
272
273}
274
275
276create_group()
277{
278SRCFILE=group
279
280makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
281
282# Remove the YP operational lines
283grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
284  grep -v "YP_DOMAIN_NAME $DOMAIN" |
285  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
286
287# Remove the key
288cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
289
290# Sort the entries in ascending order of gid
291sort -n -t: -k3,3 $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
292}
293
294
295create_hosts()
296{
297SRCFILE=hosts
298
299makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
300
301# Remove the YP operational lines
302grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
303  grep -v "YP_DOMAIN_NAME $DOMAIN" |
304  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
305
306# Remove the key
307cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
308
309# Sort the hosts ip addresses in ascending order
310sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4 $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
311}
312
313
314create_ipnodes()
315{
316SRCFILE=ipnodes
317
318makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
319
320# Remove the YP operational lines
321grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
322  grep -v "YP_DOMAIN_NAME $DOMAIN" |
323  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
324
325# Remove the key
326cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
327
328grep -v "::" $TMPDIR/${MAP}.cut >$TMPDIR/${MAP}.V4
329grep "::" $TMPDIR/${MAP}.cut >$TMPDIR/${MAP}.V6
330
331# Sort the ip addresses in ascending order
332sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4 $TMPDIR/${MAP}.V4 > $OUTDIR/$SRCFILE
333
334# V6 addresses due to hex chars, can't be sorted this way.
335# So just do the default string sort.
336sort $TMPDIR/${MAP}.V6 >> $OUTDIR/$SRCFILE
337}
338
339
340create_ethers()
341{
342SRCFILE=ethers
343
344makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
345
346# Remove the YP operational lines
347grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
348  grep -v "YP_DOMAIN_NAME $DOMAIN" |
349  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
350
351# Remove the key
352cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
353
354# Sort ethernet addresses based on host names
355sort -b -k2 $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
356}
357
358
359create_networks()
360{
361SRCFILE=networks
362
363makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
364
365# Remove the YP operational lines
366grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
367  grep -v "YP_DOMAIN_NAME $DOMAIN" |
368  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
369
370# Remove the key
371cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
372
373# Sort networks based on their names
374sort $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
375}
376
377
378create_rpc()
379{
380SRCFILE=rpc
381
382makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
383
384# Remove the YP operational lines
385grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
386  grep -v "YP_DOMAIN_NAME $DOMAIN" |
387  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
388
389# Remove the key
390cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
391
392# Sort entries in the increasing order of RPC number
393sort -n -k2 $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
394}
395
396
397create_services()
398{
399SRCFILE=services
400
401makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
402
403# Remove the YP operational lines
404grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
405  grep -v "YP_DOMAIN_NAME $DOMAIN" |
406  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
407
408# Remove the key
409cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
410
411# Sort entries in the increasing order of RPC number
412sort -n -k2 $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
413}
414
415
416create_protocols()
417{
418SRCFILE=protocols
419
420makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
421
422# Remove the YP operational lines
423grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
424  grep -v "YP_DOMAIN_NAME $DOMAIN" |
425  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
426
427# Remove the key
428cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
429
430# Sort entries in the increasing order of RPC number
431sort -n -k2 $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
432}
433
434
435create_netgroup()
436{
437SRCFILE=netgroup
438
439makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
440
441# Remove the YP operational lines
442grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
443  grep -v "YP_DOMAIN_NAME $DOMAIN" |
444  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
445
446cp $TMPDIR/${MAP}.grep $OUTDIR/$SRCFILE
447}
448
449
450create_bootparams()
451{
452SRCFILE=bootparams
453
454makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
455
456# Remove the YP operational lines
457grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
458  grep -v "YP_DOMAIN_NAME $DOMAIN" |
459  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
460
461# Sort the entries
462sort $TMPDIR/${MAP}.grep > $OUTDIR/$SRCFILE
463}
464
465
466create_aliases()
467{
468SRCFILE=aliases
469
470makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
471
472# Remove the YP operational lines
473grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
474  grep -v "YP_DOMAIN_NAME $DOMAIN" |
475  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
476
477# Replace first " " with ": " to make it similar to aliases
478sed 's/ /: /' $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.sed
479
480# Sort aliases entries alphabetically
481sort $TMPDIR/${MAP}.sed > $OUTDIR/$SRCFILE
482}
483
484
485create_publickey()
486{
487SRCFILE=publickey
488
489makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
490
491# Remove the YP operational lines
492grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
493  grep -v "YP_DOMAIN_NAME $DOMAIN" |
494  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
495
496# Sort entries alphabetically
497sort $TMPDIR/${MAP}.grep > $OUTDIR/$SRCFILE
498}
499
500
501create_netid()
502{
503SRCFILE=netid
504
505makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
506
507# Remove the YP operational lines
508grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
509  grep -v "YP_DOMAIN_NAME $DOMAIN" |
510  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
511
512# netid source files is used to add other domain
513# entries. So, filter out local domain entries
514grep -v "@${DOMAIN}" $TMPDIR/${MAP}.grep > $OUTDIR/$SRCFILE
515}
516
517
518create_netmasks()
519{
520SRCFILE=netmasks
521
522makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
523
524# Remove the YP operational lines
525grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
526  grep -v "YP_DOMAIN_NAME $DOMAIN" |
527  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
528
529# Sort the network numbers in ascending order
530sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4 $TMPDIR/${MAP}.grep > $OUTDIR/$SRCFILE
531}
532
533
534create_passwd_adjunct()
535{
536SRCFILE=passwd.adjunct
537
538makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
539
540# Remove the YP operational lines. It has three of them.
541grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
542  grep -v "YP_DOMAIN_NAME $DOMAIN" |
543  grep -v YP_MASTER_NAME | grep -v YP_SECURE > $TMPDIR/${MAP}.grep
544
545# Remove the key
546cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
547
548## Check if sorting is ok, or leave it as it is.
549# Sort the entries in alphabetical order
550sort $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
551}
552
553
554create_group_adjunct()
555{
556SRCFILE=group.adjunct
557
558makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
559
560# Remove the YP operational lines. It has three of them.
561grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
562  grep -v "YP_DOMAIN_NAME $DOMAIN" |
563  grep -v YP_MASTER_NAME | grep -v YP_SECURE > $TMPDIR/${MAP}.grep
564
565# Remove the key
566cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
567
568# Sort the entries in alphabetical order
569sort $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
570}
571
572
573create_timezone()
574{
575SRCFILE=timezone
576
577makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
578
579# Remove the YP operational lines
580grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
581  grep -v "YP_DOMAIN_NAME $DOMAIN" |
582  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
583
584# Remove the key
585cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
586
587# Sort the entries in alphabetical order
588sort $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
589}
590
591
592create_auto_src()
593{
594SRCFILE=$MAP
595
596makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
597
598# Remove the YP operational lines
599grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
600  grep -v "YP_DOMAIN_NAME $DOMAIN" |
601  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
602
603# Sort entries alphabetically
604sort $TMPDIR/${MAP}.grep > $OUTDIR/$SRCFILE
605}
606
607
608create_auth_attr()
609{
610SRCFILE=auth_attr
611
612makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
613
614# Remove the YP operational lines
615grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
616  grep -v "YP_DOMAIN_NAME $DOMAIN" |
617  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
618
619# Remove the key
620cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
621
622# Sort entries in the alphabetical order
623sort $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
624}
625
626
627create_exec_attr()
628{
629SRCFILE=exec_attr
630
631makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
632
633# Remove the YP operational lines
634grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
635  grep -v "YP_DOMAIN_NAME $DOMAIN" |
636  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
637
638# Remove the key which is made of three fields. space is part of key
639cut -f 3- -d ":" $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut1
640cut -f 2- -d " " $TMPDIR/${MAP}.cut1 > $TMPDIR/${MAP}.cut2
641
642# Sort entries in the alphabetical order
643sort $TMPDIR/${MAP}.cut2 > $OUTDIR/$SRCFILE
644}
645
646
647create_prof_attr()
648{
649SRCFILE=prof_attr
650
651makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
652
653# Remove the YP operational lines
654grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
655  grep -v "YP_DOMAIN_NAME $DOMAIN" |
656  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
657
658# Remove the key. It is difficult here as space is part of the key.
659# From the "key key" part, extract "key", and then paste it with
660# the rest of the entry.
661cut -f1 -d: $TMPDIR/${MAP}.grep |
662awk '{
663  STR = $1
664  for (i=2; i <= NF/2; i++) {
665    STR = STR  " " $i
666  }
667print STR
668}' > $TMPDIR/${MAP}.cut1
669
670cut -f2- -d: $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut2
671paste -d ":" $TMPDIR/${MAP}.cut1 $TMPDIR/${MAP}.cut2 > $TMPDIR/${MAP}.cut
672
673# Sort entries in the alphabetical order
674sort $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
675}
676
677
678create_user_attr()
679{
680SRCFILE=user_attr
681
682makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
683
684# Remove the YP operational lines
685grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
686  grep -v "YP_DOMAIN_NAME $DOMAIN" |
687  grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
688
689# Remove the key
690cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
691
692# Sort entries in the alphabetical order
693sort $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
694}
695
696
697create_audit_user()
698{
699SRCFILE=audit_user
700
701makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
702
703# Remove the YP operational lines. It has 3 of them.
704grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
705  grep -v "YP_DOMAIN_NAME $DOMAIN" |
706  grep -v YP_MASTER_NAME | grep -v YP_SECURE > $TMPDIR/${MAP}.grep
707
708# Remove the key
709cut -f 2- -d " " $TMPDIR/${MAP}.grep > $TMPDIR/${MAP}.cut
710
711# Sort entries in the alphabetical order
712sort $TMPDIR/${MAP}.cut > $OUTDIR/$SRCFILE
713}
714
715
716## MAIN ##
717
718PROG=`basename $0`
719
720# Only root can read the NIS maps, so no point allowing
721# non-root users to be able to run this script.
722is_root_user
723if [ $? -ne 0 ]; then
724  echo "ERROR : Only root can run $PROG"
725  exit 1
726fi
727
728# Prevent non-root users from reading/writing
729umask 077
730
731# Initialize default values.
732DOMAIN=`/usr/bin/domainname`
733MAPDIR=/var/yp/"$DOMAIN"	# Default to local domain
734N2LPREFIX=LDAP_
735
736NIS_ONLY_MAP_LIST="passwd.byuid
737                   group.byname
738                   hosts.byaddr
739                   ipnodes.byaddr
740                   ethers.byname
741                   networks.byaddr
742                   rpc.bynumber
743                   services.byname
744                   protocols.bynumber
745                   netgroup
746                   bootparams
747                   mail.aliases
748                   publickey.byname
749                   netid.byname
750                   netmasks.byaddr
751                   passwd.adjunct.byname
752                   group.adjunct.byname
753                   timezone.byname
754                   auth_attr
755                   exec_attr
756                   prof_attr
757                   user_attr
758                   audit_user"
759
760NIS2LDAP_MAP_LIST="${N2LPREFIX}passwd.byuid
761                   ${N2LPREFIX}group.byname
762                   ${N2LPREFIX}hosts.byaddr
763                   ${N2LPREFIX}ipnodes.byaddr
764                   ${N2LPREFIX}ethers.byname
765                   ${N2LPREFIX}networks.byaddr
766                   ${N2LPREFIX}rpc.bynumber
767                   ${N2LPREFIX}services.byname
768                   ${N2LPREFIX}protocols.bynumber
769                   ${N2LPREFIX}netgroup
770                   ${N2LPREFIX}bootparams
771                   ${N2LPREFIX}mail.aliases
772                   ${N2LPREFIX}publickey.byname
773                   ${N2LPREFIX}netid.byname
774                   ${N2LPREFIX}netmasks.byaddr
775                   ${N2LPREFIX}passwd.adjunct.byname
776                   ${N2LPREFIX}group.adjunct.byname
777                   ${N2LPREFIX}timezone.byname
778                   ${N2LPREFIX}auth_attr
779                   ${N2LPREFIX}exec_attr
780                   ${N2LPREFIX}prof_attr
781                   ${N2LPREFIX}user_attr
782                   ${N2LPREFIX}audit_user"
783
784
785# If auto maps exist, add them to the respective lists.
786for dbmfile in $MAPDIR/auto.*.dir
787do
788  MAP=`basename $dbmfile .dir`
789  if [ -f $MAPDIR/${MAP}.pag ]; then
790    NIS_ONLY_MAP_LIST="$NIS_ONLY_MAP_LIST $MAP"
791  fi
792done
793
794for dbmfile in $MAPDIR/${N2LPREFIX}auto.*.dir
795do
796  MAP=`basename $dbmfile .dir`
797  if [ -f $MAPDIR/${MAP}.pag ]; then
798    NIS2LDAP_MAP_LIST="$NIS2LDAP_MAP_LIST $MAP"
799  fi
800done
801
802# Default to N2L maps
803MAP_LIST="$NIS2LDAP_MAP_LIST"
804
805# Safe place to avoid anyone from reading sensitive data.
806TMPDIR="/var/tmp/ypmap2src"
807
808DEBUG=0			# Default to debug off
809DEBUG=1
810OUTDIR=""
811CUST_MAP_LIST=""
812CMDLINE_SRCS=0
813
814
815parse_argument $*
816
817[ $DEBUG -eq 1 ] && echo DOMAIN = $DOMAIN
818[ $DEBUG -eq 1 ] && echo OUTDIR = $OUTDIR
819[ $DEBUG -eq 1 ] && echo TMPDIR = $TMPDIR
820[ $DEBUG -eq 1 ] && echo CUST_MAP_LIST = $CUST_MAP_LIST
821[ $DEBUG -eq 1 ] && echo MAP_LIST = $MAP_LIST
822
823[ $DEBUG -eq 1 ] && echo MAPDIR = $MAPDIR
824if [ ! -d "$MAPDIR" ]; then
825  echo ERROR : NIS Map directory $MAPDIR does not exist.
826  exit 1
827fi
828
829if [ ! -d "$OUTDIR" ]; then
830  echo output directory $OUTDIR does not exist. Creating it.
831  mkdir -p $OUTDIR
832  if [ $? -ne 0 ]; then
833    echo ERROR : Failed to create output directory $OUTDIR
834    exit 1
835  fi
836fi
837
838# Cleanup if the temp directory has been leftover
839[ -d "$TMPDIR" ] && rm -rf $TMPDIR
840mkdir $TMPDIR
841if [ $? -ne 0 ]; then
842  echo ERROR : Failed to create temp directory $TMPDIR
843  exit 1
844fi
845
846
847for MAP in $MAP_LIST
848do
849  [ $DEBUG -eq 1 ] && echo Processing MAP = $MAP
850
851  if [ ! -f $MAPDIR/${MAP}.dir ] || [ ! -f $MAPDIR/${MAP}.pag ]; then
852
853    [ $CMDLINE_SRCS -ne 0 ] && \
854        echo ERROR : Missing DBM file for $MAP in $MAPDIR . Skipping..
855
856    [ $DEBUG -eq 1 ] && [ $CMDLINE_SRCS -eq 0 ] && \
857        echo No DBM file for $MAP in $MAPDIR . Skipping..
858    continue
859  fi
860
861  case $MAP in
862    ${N2LPREFIX}passwd.byuid )
863      create_passwd
864       ;;
865    ${N2LPREFIX}group.byname )
866      create_group
867       ;;
868    ${N2LPREFIX}hosts.byaddr )
869      create_hosts
870       ;;
871    ${N2LPREFIX}ipnodes.byaddr )
872      create_ipnodes
873       ;;
874    ${N2LPREFIX}ethers.byname )
875      create_ethers
876       ;;
877    ${N2LPREFIX}networks.byaddr )
878      create_networks
879       ;;
880    ${N2LPREFIX}rpc.bynumber )
881      create_rpc
882       ;;
883    ${N2LPREFIX}services.byname )
884      create_services
885       ;;
886    ${N2LPREFIX}protocols.bynumber )
887      create_protocols
888       ;;
889    ${N2LPREFIX}netgroup )
890      create_netgroup
891       ;;
892    ${N2LPREFIX}bootparams )
893      create_bootparams
894       ;;
895    ${N2LPREFIX}mail.aliases )
896      create_aliases
897       ;;
898    ${N2LPREFIX}publickey.byname )
899      create_publickey
900       ;;
901    ${N2LPREFIX}netid.byname )
902      create_netid
903       ;;
904    ${N2LPREFIX}netmasks.byaddr )
905      create_netmasks
906       ;;
907    ${N2LPREFIX}passwd.adjunct.byname )
908      create_passwd_adjunct
909       ;;
910    ${N2LPREFIX}group.adjunct.byname )
911      create_group_adjunct
912       ;;
913    ${N2LPREFIX}timezone.byname )
914      create_timezone
915       ;;
916    ${N2LPREFIX}auto.* )
917      create_auto_src
918       ;;
919    ${N2LPREFIX}auth_attr )
920      create_auth_attr
921       ;;
922    ${N2LPREFIX}exec_attr )
923      create_exec_attr
924       ;;
925    ${N2LPREFIX}prof_attr )
926      create_prof_attr
927       ;;
928    ${N2LPREFIX}user_attr )
929      create_user_attr
930       ;;
931    ${N2LPREFIX}audit_user )
932      create_audit_user
933       ;;
934   *) # Not a default map, could be a custom map.
935      CUST_MAP_LIST="$CUST_MAP_LIST $MAP"
936      ;;
937 esac
938done
939
940
941for MAP in $CUST_MAP_LIST
942do
943  [ $DEBUG -eq 1 ] && echo Processing Custom MAP = $MAP
944
945  if [ ! -f $MAPDIR/${MAP}.dir ] || [ ! -f $MAPDIR/${MAP}.pag ]; then
946    echo ERROR : Missing DBM file for $MAP in $MAPDIR . Skipping..
947    continue
948  fi
949
950  makedbm -u $MAPDIR/$MAP > $TMPDIR/$MAP
951
952# Remove the YP operational lines. Assuming each custom map
953# has only these entries (three in n2l mode as shown below, and
954# two in vanilla NIS mode as it does not have "YP_DOMAIN_NAME".
955# But that does not require any changes in the code). Modify it
956# appropriately in other cases.
957
958  grep -v YP_LAST_MODIFIED $TMPDIR/$MAP |
959    grep -v "YP_DOMAIN_NAME $DOMAIN" |
960    grep -v YP_MASTER_NAME > $TMPDIR/${MAP}.grep
961
962# If further processing (e.g., removing key, sorting etc.)
963# is required, then update the script appropriately.
964  cp $TMPDIR/${MAP}.grep $OUTDIR/$MAP
965
966done
967
968# Leave the temp directory if debug is set
969[ $DEBUG -eq 0 ] && rm -rf $TMPDIR
970
971exit 0
972