1#!/bin/sh
2
3#
4# Make passwd.db, group.db, etc.
5#
6
7VAR_DB=/var/db
8
9# Use make if available
10if [ -x /usr/bin/make -o -x /bin/make ]; then
11	make -C $VAR_DB
12	exit 0
13fi
14
15# No make available, do it in hard way
16
17# passwd.db
18if [ -e /etc/passwd ]; then
19target=$VAR_DB/passwd.db
20echo -n "passwd... "
21awk 'BEGIN { FS=":"; OFS=":" } \
22 /^[ \t]*$$/ { next } \
23 /^[ \t]*#/ { next } \
24 /^[^#]/ { printf ".%s ", $$1; print; \
25	   printf "=%s ", $$3; print }' /etc/passwd | \
26makedb --quiet -o $target -
27echo "done."
28fi
29
30# group.db
31if [ -e /etc/group ]; then
32target=$VAR_DB/group.db
33echo -n "group... "
34awk 'BEGIN { FS=":"; OFS=":" } \
35 /^[ \t]*$$/ { next } \
36 /^[ \t]*#/ { next } \
37 /^[^#]/ { printf ".%s ", $$1; print; \
38	   printf "=%s ", $$3; print; \
39	   if ($$4 != "") { \
40	     split($$4, grmems, ","); \
41	     for (memidx in grmems) { \
42	       mem=grmems[memidx]; \
43	       if (members[mem] == "") \
44		 members[mem]=$$3; \
45	       else \
46		 members[mem]=members[mem] "," $$3; \
47	     } \
48	     delete grmems; } } \
49 END { for (mem in members) \
50	 printf ":%s %s %s\n", mem, mem, members[mem]; }' /etc/group | \
51makedb --quiet -o $target -
52echo "done."
53fi
54
55# ethers.db
56if [ -e /etc/ethers ]; then
57target=$VAR_DB/ethers.db
58echo -n "ethers... "
59awk '/^[ \t]*$$/ { next } \
60 /^[ \t]*#/ { next } \
61 /^[^#]/ { printf ".%s ", $$1; print; \
62	   printf "=%s ", $$2; print }' /etc/ethers | \
63makedb --quiet -o $target -
64echo "done."
65fi
66
67# protocols.db
68if [ -e /etc/protocols ]; then
69target=$VAR_DB/protocols.db
70echo -n "protocols... "
71awk '/^[ \t]*$$/ { next } \
72 /^[ \t]*#/ { next } \
73 /^[^#]/ { printf ".%s ", $$1; print; \
74	   printf "=%s ", $$2; print; \
75	   for (i = 3; i <= NF && !($$i ~ /^#/); ++i) \
76	     { printf ".%s ", $$i; print } }' /etc/protocols | \
77makedb --quiet -o $target -
78echo "done."
79fi
80
81# rpc.db
82if [ -e /etc/rpc ]; then
83target=$VAR_DB/rpc.db
84echo -n "rpc... "
85awk '/^[ \t]*$$/ { next } \
86 /^[ \t]*#/ { next } \
87 /^[^#]/ { printf ".%s ", $$1; print; \
88	   printf "=%s ", $$2; print; \
89	   for (i = 3; i <= NF && !($$i ~ /^#/); ++i) \
90	     { printf ".%s ", $$i; print } }' /etc/rpc | \
91makedb --quiet -o $target -
92echo "done."
93fi
94
95# services.db
96if [ -e /etc/services ]; then
97target=$VAR_DB/services.db
98echo -n "services... "
99awk 'BEGIN { FS="[ \t/]+" } \
100 /^[ \t]*$$/ { next } \
101 /^[ \t]*#/ { next } \
102 /^[^#]/ { sub(/[ \t]*#.*$$/, "");\
103	   printf ":%s/%s ", $$1, $$3; print; \
104	   printf ":%s/ ", $$1; print; \
105	   printf "=%s/%s ", $$2, $$3; print; \
106	   printf "=%s/ ", $$2; print; \
107	   for (i = 4; i <= NF && !($$i ~ /^#/); ++i) \
108	     { printf ":%s/%s ", $$i, $$3; print; \
109	       printf ":%s/ ", $$i; print } }' /etc/services | \
110makedb --quiet -o $target -
111echo "done."
112fi
113
114# shadow.db
115if [ -e /etc/shadow ]; then
116target=$VAR_DB/shadow.db
117echo -n "shadow... "
118awk 'BEGIN { FS=":"; OFS=":" } \
119 /^[ \t]*$$/ { next } \
120 /^[ \t]*#/ { next } \
121 /^[^#]/ { printf ".%s ", $$1; print }' /etc/shadow | \
122(umask 077 && makedb --quiet -o $target -)
123echo "done."
124if chgrp shadow $target 2>/dev/null; then
125	chmod g+r $target
126else
127	chown 0 $target; chgrp 0 $target; chmod 600 $target;
128	echo
129	echo "Warning: The shadow password database $target"
130	echo "has been set to be readable only by root.  You may want"
131	echo "to make it readable by the \`shadow' group depending"
132	echo "on your configuration."
133	echo
134fi
135fi
136
137# gshadow.db
138if [ -e /etc/gshadow ]; then
139target=$VAR_DB/gshadow.db
140echo -n "gshadow... "
141awk 'BEGIN { FS=":"; OFS=":" } \
142 /^[ \t]*$$/ { next } \
143 /^[ \t]*#/ { next } \
144 /^[^#]/ { printf ".%s ", $$1; print }' /etc/gshadow | \
145(umask 077 && makedb --quiet -o $target -)
146echo "done."
147if chgrp shadow $target 2>/dev/null; then
148	chmod g+r $target
149else
150	chown 0 $target; chgrp 0 $target; chmod 600 $target
151	echo
152	echo "Warning: The shadow group database $target"
153	echo "has been set to be readable only by root.  You may want"
154	echo "to make it readable by the \`shadow' group depending"
155	echo "on your configuration."
156	echo
157fi
158fi
159
160# netgroup.db
161if [ -e /etc/netgroup ]; then
162target=$VAR_DB/netgroup.db
163echo -n "netgroup... "
164awk 'BEGIN { ini=1 } \
165 /^[ \t]*$$/ { next } \
166 /^[ \t]*#/ { next } \
167 /^[^#]/ { if (sub(/[ \t]*\\$$/, " ") == 0) end="\n"; \
168	   else end=""; \
169	   gsub(/[ \t]+/, " "); \
170	   sub(/^[ \t]*/, ""); \
171	   if (ini == 0) printf "%s%s", $$0, end; \
172	   else printf ".%s %s%s", $$1, $$0, end; \
173	   ini=end == "" ? 0 : 1; } \
174 END { if (ini==0) printf "\n" }' /etc/netgroup | \
175makedb --quiet -o $target
176echo "done."
177fi
178