summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/privs.awk
blob: 89e6f37665aebc4539398884091716dead2f951a (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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#ident	"%Z%%M%	%I%	%E% SMI"
#
# This file generates three different C files:
#
#	<sys/priv_const.h>
#		An implementation private set of manifest integer constant
#		for privileges and privilege sets and manifest constants for
#		set size, number of sets, number of privileges
#
#	os/priv_const.c
#		A C source file containing the set names, privilege names
#		arrays for the name <-> number mappings
#
#	<sys/priv_names.h>
#		A public header file containing the PRIV_* defines
#		that map to strings; these are for convenience.
#		(it's easy to misspell a string, harder to misspell a
#		manifest constant)
#
#	/etc/security/priv_names
#		A privilege name to explanation mapping.
#
#
# The files are output on the awk variable privhfile, pubhfile, cfile,
# and pnamesfile respectively
#
# The input file should contain a standard Sun comment and ident string
# which is copied verbatim and lines of
#
#	[keyword] privilege	PRIV_<privilege>
#	set			PRIV_<set>
#
# Which are converted to privileges and privilege sets
#


BEGIN	{
	# Number of privileges read
	npriv = 0

	# Number of privilege sets
	nset = 0

	# Length of all strings concatenated, including \0
	privbytes = 0
	setbytes = 0

	# Number of reserved privilege slots
	slack = 10

	privhcmt = \
	" * Privilege constant definitions; these constants are subject to\n" \
	" * change, including renumbering, without notice and should not be\n" \
	" * used in any code.  Privilege names must be used instead.\n" \
	" * Privileges and privilege sets must not be stored in binary\n" \
	" * form; privileges and privileges sets must be converted to\n" \
	" * textual representation before being committed to persistent store."

	ccmt = \
	" * Privilege name table and size definitions."

	pubhcmt = \
	" * Privilege constant definitions.  Privileges and privilege sets\n" \
	" * are only known by name and should be mapped at runtime."

	pnamescmt = \
	"#\n" \
	"# Privilege name explanation file\n" \
	"# The format of entries is a privilege name starting at the\n" \
	"# beginning of a line directly folowed by a new line followed\n" \
	"# by several lines of texts starting with white space terminated\n" \
	"# by a line with a single newline or not starting with white space\n" \
	"#\n"
}

#
# Privilege strings are represented as lower case strings;
# PRIV_ is stripped from the strings.
#
/^([A-Za-z]* )?privilege / {
	if (NF == 3) {
		key = toupper($1)
		priv = toupper($3)
		if (set[key] != "")
			set[key] = set[key] ";"
		set[key] = set[key] "\\\n\t\tPRIV_ASSERT((set), " priv ")"
	} else {
		priv = toupper($2);
	}
	privs[npriv] = tolower(substr(priv, 6));
	inset = 0
	inpriv = 1

	privind[npriv] = privbytes;

	tabs = (32 - length(priv) - 1)/8
	# length + \0 - PRIV_
	privbytes += length(priv) - 4
	pdef[npriv] = "#define\t" priv substr("\t\t\t\t\t", 1, tabs)

	npriv++
	next
}

#
# Set strings are represented as strings with an initial cap;
# PRIV_ is stripped from the strings.
#
/^set / {
	$2 = toupper($2)
	sets[nset] = toupper(substr($2, 6, 1)) tolower(substr($2, 7));
	inset = 1
	inpriv = 0

	setind[nset] = setbytes

	# length + \0 - PRIV_
	setbytes += length($2) - 4
	tabs = (32 - length($2) - 1)/8
	sdef[nset] = "#define\t" $2 substr("\t\t\t\t\t", 1, tabs)

	nset++
	next
}

/INSERT COMMENT/ {
	acmt = " *\n * THIS FILE WAS GENERATED; DO NOT EDIT"
	if (cfile) {
		print ccmt > cfile
		print acmt > cfile
	}
	if (privhfile) {
		print privhcmt > privhfile
		print acmt > privhfile
	}
	if (pubhfile) {
		print pubhcmt > pubhfile
		print acmt > pubhfile
	}
	next
}
/^#pragma/ {
	pragma = $0;
	if (pnamesfile) {
		print "#" substr($0, 9) > pnamesfile
	}
	next;
}

/^#/ && ! /^#pragma/{
	# Comments, ignore
	next
}

{
	#
	# Comments describing privileges and sets follow the definitions.
	#
	if (inset || inpriv) {
		sub("^[ 	]*", "")
		sub("[ 	]*$", "")
		if (/^$/) next;
	}
	if (inset) {
		setcmt[nset - 1] = setcmt[nset - 1] " * " $0 "\n"
		next
	} else if (inpriv) {
		sub("^[ 	]*", "")
		privcmt[npriv - 1] = privcmt[npriv - 1] " * " $0 "\n"
		privncmt[npriv - 1] = privncmt[npriv - 1] "\t" $0 "\n"
		next
	}

	if (cfile)
		print > cfile
	if (privhfile)
		print > privhfile
	if (pubhfile)
		print > pubhfile
	if (pnamesfile) {
		sub("^/\\*", "#")
		sub("^ \\*/", "")
		sub("^ \\*", "#")
		if (/^$/) next;
		print > pnamesfile
	}
}

END	{

	if (!pubhfile && !privhfile && !cfile && !pnamesfile) {
		print "Output file parameter not set" > "/dev/stderr"
		exit 1
	}

	setsize = int((npriv + slack)/(8 * 4)) + 1
	maxnpriv = setsize * 8 * 4
	# Assume allocated privileges are on average "NSDQ" bytes larger.
	maxprivbytes = int((privbytes / npriv + 5.5)) * (maxnpriv - npriv)
	maxprivbytes += privbytes

	if (cfile) {
		print "\n" > cfile
		print pragma "\n"> cfile
		print "#include <sys/types.h>" > cfile
		print "#include <sys/priv_const.h>" > cfile
		print "#include <sys/priv_impl.h>" > cfile
		print "#include <sys/priv.h>" > cfile
		print "#include <sys/sysmacros.h>" > cfile
		print "\n" > cfile
		#
		# Create the entire priv info structure here.
		# When adding privileges, the kernel needs to update
		# too many fields as the number of privileges is kept in
		# many places.
		#
		print \
		    "static struct _info {\n" \
		    "	priv_impl_info_t	impl_info;\n" \
		    "	priv_info_t		settype;\n" \
		    "	int			nsets;\n" \
		    "	const char		sets[" setbytes "];\n" \
		    "	priv_info_t		privtype;\n" \
		    "	int			nprivs;\n" \
		    "	char			privs[" maxprivbytes "];\n" \
		    "	priv_info_t		sysset;\n" \
		    "	priv_set_t		basicset;\n" \
		    "} info = {\n" \
		    "	{ sizeof (priv_impl_info_t), 0, PRIV_NSET, " \
			"PRIV_SETSIZE, " npriv ",\n" \
			"\t\tsizeof (priv_info_uint_t),\n" \
			"\t\tsizeof (info) - sizeof (info.impl_info)},\n" \
		    "	{ PRIV_INFO_SETNAMES,\n" \
		    "	    offsetof(struct _info, privtype) - " \
		    "offsetof(struct _info, settype)},\n\tPRIV_NSET," > cfile

		sep = "\t\""
		len = 9;
		for (i = 0; i < nset; i++) {
			if (len + length(sets[i]) > 80) {
				sep = "\\0\"\n\t\""
				len = 9
			}
			printf sep sets[i]  > cfile
			len += length(sets[i]) + length(sep);
			sep = "\\0"
		}
		print "\\0\"," > cfile

		print "\t{ PRIV_INFO_PRIVNAMES,\n\t    " \
			"offsetof(struct _info, sysset) - " \
			"offsetof(struct _info, privtype)},\n\t" npriv "," \
			> cfile

		sep = "\t\""
		len = 9;
		for (i = 0; i < npriv; i++) {
			if (len + length(privs[i]) > 80) {
				sep = "\\0\"\n\t\""
				len = 9
			}
			printf sep privs[i]  > cfile
			len += length(privs[i]) + length(sep);
			sep = "\\0"
		}
		print "\\0\"," > cfile

		print "\t{ PRIV_INFO_BASICPRIVS, sizeof (info) - " \
			"offsetof(struct _info, sysset)},"  > cfile

		print "};\n" > cfile

		print "\nconst char *priv_names[" maxnpriv "] =\n{" > cfile
		for (i = 0; i < npriv; i++)
			print "\t&info.privs[" privind[i] "]," > cfile

		print "};\n" > cfile

		print "\nconst char *priv_setnames[" nset "] =\n{" > cfile
		for (i = 0; i < nset; i++)
			print "\t&info.sets[" setind[i] "]," > cfile

		print "};\n" > cfile

		print "int nprivs = " npriv ";" > cfile
		print "int privbytes = " privbytes ";" > cfile
		print "int maxprivbytes = " maxprivbytes ";" > cfile
		print "size_t privinfosize = sizeof (info);" > cfile
		print "char *priv_str = info.privs;" > cfile
		print "priv_set_t *priv_basic = &info.basicset;" > cfile
		print "priv_impl_info_t *priv_info = &info.impl_info;" > cfile
		print "priv_info_names_t *priv_ninfo = " \
			"(priv_info_names_t *)&info.privtype;" > cfile
		close(cfile)
	}

	# Kernel private
	if (privhfile) {
		print "#ifndef _SYS_PRIV_CONST_H" > privhfile
		print "#define\t_SYS_PRIV_CONST_H\n" > privhfile
		print pragma "\n"> privhfile
		print "\n#include <sys/types.h>\n\n" > privhfile
		print "#ifdef __cplusplus\nextern \"C\" {\n#endif\n" > privhfile

		print "#if defined(_KERNEL) || defined(_KMEMUSER)" > privhfile
		print "#define\tPRIV_NSET\t\t\t  " nset > privhfile
		print "#define\tPRIV_SETSIZE\t\t\t  " setsize > privhfile
		print "#endif\n\n#ifdef _KERNEL" > privhfile
		print "#define\t__PRIV_CONST_IMPL\n" > privhfile
		print "extern const char *priv_names[];" > privhfile
		print "extern const char *priv_setnames[];" > privhfile

		print "extern int nprivs;" > privhfile
		print "extern int privbytes;" > privhfile
		print "extern int maxprivbytes;" > privhfile
		print "extern size_t privinfosize;" > privhfile
		print "extern char *priv_str;" > privhfile
		print "extern struct priv_set *priv_basic;" > privhfile
		print "extern struct priv_impl_info *priv_info;" > privhfile
		print "extern struct priv_info_names *priv_ninfo;" > privhfile

		print "\n/* Privileges */" > privhfile
		 
		for (i = 0; i < npriv; i++)
			print pdef[i] sprintf("%3d", i) > privhfile

		print "\n/* Privilege sets */" > privhfile
		for (i = 0; i < nset; i++)
			print sdef[i] sprintf("%3d", i) > privhfile

		print "\n#define\tMAX_PRIVILEGE\t\t\t "  setsize * 32 \
			> privhfile

		# Special privilege categories.
		for (s in set)
			print "\n#define\tPRIV_" s "_ASSERT(set)" set[s] \
				> privhfile

		print "\n#endif /* _KERNEL */" > privhfile
		print "\n#ifdef __cplusplus\n}\n#endif" > privhfile
		print "\n#endif /* _SYS_PRIV_CONST_H */" > privhfile
		close(privhfile)
	}

	if (pubhfile) {
		cast="((const char *)"
		print "#ifndef _SYS_PRIV_NAMES_H" > pubhfile
		print "#define\t_SYS_PRIV_NAMES_H\n" > pubhfile

		print pragma "\n" > pubhfile
		print "#ifdef __cplusplus\nextern \"C\" {\n#endif\n" > pubhfile

		print "#ifndef __PRIV_CONST_IMPL" > pubhfile
		print "/*\n * Privilege names\n */" > pubhfile
		for (i = 0; i < npriv; i++) {
			print "/*\n" privcmt[i] " */" > pubhfile
			print pdef[i] cast "\"" privs[i] "\")\n" > pubhfile
		}

		print "" > pubhfile

		print "/*\n * Privilege set names\n */" > pubhfile
		for (i = 0; i < nset; i++) {
			print "/*\n" setcmt[i] " */" > pubhfile
			print sdef[i] cast "\"" sets[i] "\")\n" > pubhfile
		}

		print "\n#endif /* __PRIV_CONST_IMPL */" > pubhfile
		print "\n#ifdef __cplusplus\n}\n#endif" > pubhfile
		print "\n#endif /* _SYS_PRIV_NAMES_H */" > pubhfile
		close(pubhfile)
	}

	if (pnamesfile) {
		print pnamescmt > pnamesfile
		for (i = 0; i < npriv; i++) {
			print privs[i] > pnamesfile
			print privncmt[i] > pnamesfile
		}
	}

}