1# Makefile to (re-)generate db versions of system database files.
2# Copyright (C) 1996-2022 Free Software Foundation, Inc.
3# This file is part of the GNU C Library.
4#
5
6# The GNU C Library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10
11# The GNU C Library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15
16# You should have received a copy of the GNU Lesser General Public
17# License along with the GNU C Library; if not, see
18# <https://www.gnu.org/licenses/>.
19
20DATABASES = $(wildcard /etc/passwd /etc/group /etc/ethers /etc/protocols \
21		       /etc/rpc /etc/services /etc/shadow /etc/gshadow \
22		       /etc/netgroup)
23
24VAR_DB = /var/db
25
26AWK = awk
27MAKEDB = makedb --quiet
28
29all: $(patsubst %,$(VAR_DB)/%.db,$(notdir $(DATABASES)))
30
31
32$(VAR_DB)/passwd.db: /etc/passwd
33	@printf %s "$(patsubst %.db,%,$(@F))... "
34	@$(AWK) 'BEGIN { FS=":"; OFS=":" } \
35		 /^[ \t]*$$/ { next } \
36		 /^[ \t]*#/ { next } \
37		 /^[^#]/ { printf ".%s ", $$1; print; \
38			   printf "=%s ", $$3; print }' $^ | \
39	$(MAKEDB) -o $@ -
40	@echo "done."
41
42$(VAR_DB)/group.db: /etc/group
43	@printf %s "$(patsubst %.db,%,$(@F))... "
44	@$(AWK) 'BEGIN { FS=":"; OFS=":" } \
45		 /^[ \t]*$$/ { next } \
46		 /^[ \t]*#/ { next } \
47		 /^[^#]/ { printf ".%s ", $$1; print; \
48			   printf "=%s ", $$3; print; \
49			   if ($$4 != "") { \
50			     split($$4, grmems, ","); \
51			     for (memidx in grmems) { \
52			       mem=grmems[memidx]; \
53			       if (members[mem] == "") \
54				 members[mem]=$$3; \
55			       else \
56				 members[mem]=members[mem] "," $$3; \
57			     } \
58			     delete grmems; } } \
59		 END { for (mem in members) \
60			 printf ":%s %s %s\n", mem, mem, members[mem]; }' $^ | \
61	$(MAKEDB) -o $@ -
62	@echo "done."
63
64$(VAR_DB)/ethers.db: /etc/ethers
65	@printf %s "$(patsubst %.db,%,$(@F))... "
66	@$(AWK) '/^[ \t]*$$/ { next } \
67		 /^[ \t]*#/ { next } \
68		 /^[^#]/ { printf ".%s ", $$1; print; \
69			   printf "=%s ", $$2; print }' $^ | \
70	$(MAKEDB) -o $@ -
71	@echo "done."
72
73$(VAR_DB)/protocols.db: /etc/protocols
74	@printf %s "$(patsubst %.db,%,$(@F))... "
75	@$(AWK) '/^[ \t]*$$/ { next } \
76		 /^[ \t]*#/ { next } \
77		 /^[^#]/ { printf ".%s ", $$1; print; \
78			   printf "=%s ", $$2; print; \
79			   for (i = 3; i <= NF && !($$i ~ /^#/); ++i) \
80			     { printf ".%s ", $$i; print } }' $^ | \
81	$(MAKEDB) -o $@ -
82	@echo "done."
83
84$(VAR_DB)/rpc.db: /etc/rpc
85	@printf %s "$(patsubst %.db,%,$(@F))... "
86	@$(AWK) '/^[ \t]*$$/ { next } \
87		 /^[ \t]*#/ { next } \
88		 /^[^#]/ { printf ".%s ", $$1; print; \
89			   printf "=%s ", $$2; print; \
90			   for (i = 3; i <= NF && !($$i ~ /^#/); ++i) \
91			     { printf ".%s ", $$i; print } }' $^ | \
92	$(MAKEDB) -o $@ -
93	@echo "done."
94
95$(VAR_DB)/services.db: /etc/services
96	@printf %s "$(patsubst %.db,%,$(@F))... "
97	@$(AWK) 'BEGIN { FS="[ \t/]+" } \
98		 /^[ \t]*$$/ { next } \
99		 /^[ \t]*#/ { next } \
100		 /^[^#]/ { sub(/[ \t]*#.*$$/, "");\
101			   printf ":%s/%s ", $$1, $$3; print; \
102			   printf ":%s/ ", $$1; print; \
103			   printf "=%s/%s ", $$2, $$3; print; \
104			   printf "=%s/ ", $$2; print; \
105			   for (i = 4; i <= NF && !($$i ~ /^#/); ++i) \
106			     { printf ":%s/%s ", $$i, $$3; print; \
107			       printf ":%s/ ", $$i; print } }' $^ | \
108	$(MAKEDB) -o $@ -
109	@echo "done."
110
111$(VAR_DB)/shadow.db: /etc/shadow
112	@printf %s "$(patsubst %.db,%,$(@F))... "
113	@$(AWK) 'BEGIN { FS=":"; OFS=":" } \
114		 /^[ \t]*$$/ { next } \
115		 /^[ \t]*#/ { next } \
116		 /^[^#]/ { printf ".%s ", $$1; print }' $^ | \
117	(umask 077 && $(MAKEDB) -o $@ -)
118	@echo "done."
119	@if chgrp shadow $@ 2>/dev/null; then \
120	  chmod g+r $@; \
121	else \
122	  chown 0 $@; chgrp 0 $@; chmod 600 $@; \
123	  echo; \
124	  echo "Warning: The shadow password database $@"; \
125	  echo "has been set to be readable only by root.  You may want"; \
126	  echo "to make it readable by the \`shadow' group depending"; \
127	  echo "on your configuration."; \
128	  echo; \
129	fi
130
131$(VAR_DB)/gshadow.db: /etc/gshadow
132	@printf %s "$(patsubst %.db,%,$(@F))... "
133	@$(AWK) 'BEGIN { FS=":"; OFS=":" } \
134		 /^[ \t]*$$/ { next } \
135		 /^[ \t]*#/ { next } \
136		 /^[^#]/ { printf ".%s ", $$1; print }' $^ | \
137	(umask 077 && $(MAKEDB) -o $@ -)
138	@echo "done."
139	@if chgrp shadow $@ 2>/dev/null; then \
140	  chmod g+r $@; \
141	else \
142	  chown 0 $@; chgrp 0 $@; chmod 600 $@; \
143	  echo; \
144	  echo "Warning: The shadow group database $@"; \
145	  echo "has been set to be readable only by root.  You may want"; \
146	  echo "to make it readable by the \`shadow' group depending"; \
147	  echo "on your configuration."; \
148	  echo; \
149	fi
150
151$(VAR_DB)/netgroup.db: /etc/netgroup
152	@printf %s "$(patsubst %.db,%,$(@F))... "
153	@$(AWK) 'BEGIN { ini=1 } \
154		 /^[ \t]*$$/ { next } \
155		 /^[ \t]*#/ { next } \
156		 /^[^#]/ { if (sub(/[ \t]*\\$$/, " ") == 0) end="\n"; \
157			   else end=""; \
158			   gsub(/[ \t]+/, " "); \
159			   sub(/^[ \t]*/, ""); \
160			   if (ini == 0) printf "%s%s", $$0, end; \
161			   else printf ".%s %s%s", $$1, $$0, end; \
162			   ini=end == "" ? 0 : 1; } \
163			   END { if (ini==0) printf "\n" }' $^ | \
164	$(MAKEDB) -o $@ -
165	@echo "done."
166