xref: /titanic_50/usr/src/head/nss_dbdefs.h (revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Database-specific definitions for the getXXXbyYYY routines
26  * (e.g getpwuid_r(), ether_ntohost()) that use the name-service switch.
27  * Database-independent definitions are in <nss_common.h>
28  *
29  * Ideally, this is the only switch header file one would add things
30  * to in order to support a new database.
31  *
32  * NOTE:  The interfaces documented in this file may change in a minor
33  *	  release.  It is intended that in the future a stronger committment
34  *	  will be made to these interface definitions which will guarantee
35  *	  them across minor releases.
36  */
37 
38 #ifndef _NSS_DBDEFS_H
39 #define	_NSS_DBDEFS_H
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 #include <sys/types.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <netdb.h>		/* MAXALIASES, MAXADDRS */
47 #include <limits.h>		/* LOGNAME_MAX */
48 #include <nss_common.h>
49 
50 #ifdef	__cplusplus
51 extern "C" {
52 #endif
53 
54 #ifndef	NSS_INCLUDE_UNSAFE
55 #define	NSS_INCLUDE_UNSAFE	1	/* Build old, MT-unsafe interfaces, */
56 #endif	/* NSS_INCLUDE_UNSAFE */	/*  e.g. getpwnam (c.f. getpwnam_r) */
57 
58 /*
59  * Names of the well-known databases.
60  */
61 
62 #define	NSS_DBNAM_ALIASES	"aliases"	/* E-mail aliases, that is */
63 #define	NSS_DBNAM_AUTOMOUNT	"automount"
64 #define	NSS_DBNAM_BOOTPARAMS	"bootparams"
65 #define	NSS_DBNAM_ETHERS	"ethers"
66 #define	NSS_DBNAM_GROUP		"group"
67 #define	NSS_DBNAM_HOSTS		"hosts"
68 #define	NSS_DBNAM_IPNODES	"ipnodes"
69 #define	NSS_DBNAM_NETGROUP	"netgroup"
70 #define	NSS_DBNAM_NETMASKS	"netmasks"
71 #define	NSS_DBNAM_NETWORKS	"networks"
72 #define	NSS_DBNAM_PASSWD	"passwd"
73 #define	NSS_DBNAM_PRINTERS	"printers"
74 #define	NSS_DBNAM_PROJECT	"project"
75 #define	NSS_DBNAM_PROTOCOLS	"protocols"
76 #define	NSS_DBNAM_PUBLICKEY	"publickey"
77 #define	NSS_DBNAM_RPC		"rpc"
78 #define	NSS_DBNAM_SERVICES	"services"
79 #define	NSS_DBNAM_AUDITUSER	"audit_user"
80 #define	NSS_DBNAM_AUTHATTR	"auth_attr"
81 #define	NSS_DBNAM_EXECATTR	"exec_attr"
82 #define	NSS_DBNAM_PROFATTR	"prof_attr"
83 #define	NSS_DBNAM_USERATTR	"user_attr"
84 
85 #define	NSS_DBNAM_TSOL_TP	"tnrhtp"
86 #define	NSS_DBNAM_TSOL_RH	"tnrhdb"
87 #define	NSS_DBNAM_TSOL_ZC	"tnzonecfg"
88 
89 /* getspnam() et al use the "passwd" config entry but the "shadow" backend */
90 #define	NSS_DBNAM_SHADOW	"shadow"
91 
92 /* The "compat" backend gets config entries for these pseudo-databases */
93 #define	NSS_DBNAM_PASSWD_COMPAT	"passwd_compat"
94 #define	NSS_DBNAM_GROUP_COMPAT	"group_compat"
95 
96 /*
97  * Default switch configuration, compiled into the front-ends.
98  *
99  * Absent good reasons to the contrary, this should be compatible with the
100  * default /etc/nsswitch.conf file.
101  */
102 #define	NSS_FILES_ONLY		"files"
103 #define	NSS_FILES_NS		"files nis"
104 #define	NSS_NS_FALLBACK		"nis [NOTFOUND=return] files"
105 #define	NSS_NS_ONLY		"nis"
106 #define	NSS_TSOL_FALLBACK	"files ldap"
107 
108 #define	NSS_DEFCONF_ALIASES	NSS_FILES_NS
109 #define	NSS_DEFCONF_AUTOMOUNT	NSS_FILES_NS
110 #define	NSS_DEFCONF_BOOTPARAMS	NSS_NS_FALLBACK
111 #define	NSS_DEFCONF_ETHERS	NSS_NS_FALLBACK
112 #define	NSS_DEFCONF_GROUP	NSS_FILES_NS
113 #define	NSS_DEFCONF_HOSTS	NSS_NS_FALLBACK
114 #define	NSS_DEFCONF_IPNODES	NSS_NS_FALLBACK
115 #define	NSS_DEFCONF_NETGROUP	NSS_NS_ONLY
116 #define	NSS_DEFCONF_NETMASKS	NSS_NS_FALLBACK
117 #define	NSS_DEFCONF_NETWORKS	NSS_NS_FALLBACK
118 #define	NSS_DEFCONF_PASSWD	NSS_FILES_NS
119 #define	NSS_DEFCONF_PRINTERS	"user files nis nisplus"
120 #define	NSS_DEFCONF_PROJECT	NSS_FILES_NS
121 #define	NSS_DEFCONF_PROTOCOLS	NSS_NS_FALLBACK
122 #define	NSS_DEFCONF_PUBLICKEY	NSS_FILES_NS
123 #define	NSS_DEFCONF_RPC		NSS_NS_FALLBACK
124 #define	NSS_DEFCONF_SERVICES	NSS_FILES_NS	/* speeds up byname() */
125 
126 #define	NSS_DEFCONF_GROUP_COMPAT	NSS_NS_ONLY
127 #define	NSS_DEFCONF_PASSWD_COMPAT	NSS_NS_ONLY
128 
129 #define	NSS_DEFCONF_ATTRDB	NSS_FILES_NS
130 
131 #define	NSS_DEFCONF_AUDITUSER	NSS_DEFCONF_PASSWD
132 #define	NSS_DEFCONF_USERATTR	NSS_DEFCONF_PASSWD
133 #define	NSS_DEFCONF_AUTHATTR	NSS_DEFCONF_ATTRDB
134 #define	NSS_DEFCONF_PROFATTR	NSS_DEFCONF_ATTRDB
135 #define	NSS_DEFCONF_EXECATTR	NSS_DEFCONF_PROFATTR
136 
137 #define	NSS_DEFCONF_TSOL_TP	NSS_TSOL_FALLBACK
138 #define	NSS_DEFCONF_TSOL_RH	NSS_TSOL_FALLBACK
139 #define	NSS_DEFCONF_TSOL_ZC	NSS_TSOL_FALLBACK
140 
141 /*
142  * Line-lengths that the "files" and "compat" backends will try to support.
143  * It may be reasonable (even advisable) to use smaller values than these.
144  */
145 
146 #define	NSS_BUFSIZ		1024
147 
148 #define	NSS_LINELEN_GROUP	((NSS_BUFSIZ) * 8)
149 #define	NSS_LINELEN_HOSTS	((NSS_BUFSIZ) * 8)
150 #define	NSS_LINELEN_IPNODES	((NSS_BUFSIZ) * 8)
151 #define	NSS_LINELEN_NETMASKS	NSS_BUFSIZ
152 #define	NSS_LINELEN_NETWORKS	NSS_BUFSIZ
153 #define	NSS_LINELEN_PASSWD	NSS_BUFSIZ
154 #define	NSS_LINELEN_PRINTERS	NSS_BUFSIZ
155 #define	NSS_LINELEN_PROJECT	((NSS_BUFSIZ) * 4)
156 #define	NSS_LINELEN_PROTOCOLS	NSS_BUFSIZ
157 #define	NSS_LINELEN_PUBLICKEY	NSS_BUFSIZ
158 #define	NSS_LINELEN_RPC		NSS_BUFSIZ
159 #define	NSS_LINELEN_SERVICES	NSS_BUFSIZ
160 #define	NSS_LINELEN_SHADOW	NSS_BUFSIZ
161 #define	NSS_LINELEN_ETHERS	NSS_BUFSIZ
162 #define	NSS_LINELEN_BOOTPARAMS	NSS_BUFSIZ
163 
164 #define	NSS_LINELEN_ATTRDB	NSS_BUFSIZ
165 
166 #define	NSS_LINELEN_AUDITUSER	NSS_LINELEN_ATTRDB
167 #define	NSS_LINELEN_AUTHATTR	NSS_LINELEN_ATTRDB
168 #define	NSS_LINELEN_EXECATTR	NSS_LINELEN_ATTRDB
169 #define	NSS_LINELEN_PROFATTR	NSS_LINELEN_ATTRDB
170 #define	NSS_LINELEN_USERATTR	NSS_LINELEN_ATTRDB
171 
172 #define	NSS_MMAPLEN_EXECATTR	NSS_LINELEN_EXECATTR * 8
173 
174 #define	NSS_LINELEN_TSOL	NSS_BUFSIZ
175 
176 #define	NSS_LINELEN_TSOL_TP	NSS_LINELEN_TSOL
177 #define	NSS_LINELEN_TSOL_RH	NSS_LINELEN_TSOL
178 #define	NSS_LINELEN_TSOL_ZC	NSS_LINELEN_TSOL
179 
180 /*
181  * Reasonable defaults for 'buflen' values passed to _r functions.  The BSD
182  * and SunOS 4.x implementations of the getXXXbyYYY() functions used hard-
183  * coded array sizes;  the values here are meant to handle anything that
184  * those implementations handled.
185  * === These might more reasonably go in <pwd.h>, <netdb.h> et al
186  */
187 
188 #define	NSS_BUFLEN_GROUP	NSS_LINELEN_GROUP
189 #define	NSS_BUFLEN_HOSTS	\
190 	(NSS_LINELEN_HOSTS + (MAXALIASES + MAXADDRS + 2) * sizeof (char *))
191 #define	NSS_BUFLEN_IPNODES	\
192 	(NSS_LINELEN_IPNODES + (MAXALIASES + MAXADDRS + 2) * sizeof (char *))
193 #define	NSS_BUFLEN_NETGROUP	(MAXHOSTNAMELEN * 2 + LOGNAME_MAX + 3)
194 #define	NSS_BUFLEN_NETWORKS	NSS_LINELEN_NETWORKS	/* === ?  + 35 * 4 */
195 #define	NSS_BUFLEN_PASSWD	NSS_LINELEN_PASSWD
196 #define	NSS_BUFLEN_PROJECT	(NSS_LINELEN_PROJECT + 800 * sizeof (char *))
197 #define	NSS_BUFLEN_PROTOCOLS	NSS_LINELEN_PROTOCOLS	/* === ?  + 35 * 4 */
198 #define	NSS_BUFLEN_PUBLICKEY	NSS_LINELEN_PUBLICKEY
199 #define	NSS_BUFLEN_RPC		NSS_LINELEN_RPC		/* === ?  + 35 * 4 */
200 #define	NSS_BUFLEN_SERVICES	NSS_LINELEN_SERVICES	/* === ?  + 35 * 4 */
201 #define	NSS_BUFLEN_SHADOW	NSS_LINELEN_SHADOW
202 #define	NSS_BUFLEN_ETHERS	NSS_LINELEN_ETHERS
203 #define	NSS_BUFLEN_BOOTPARAMS	NSS_LINELEN_BOOTPARAMS
204 
205 #define	NSS_BUFLEN_ATTRDB	NSS_LINELEN_ATTRDB
206 
207 #define	NSS_BUFLEN_AUDITUSER	NSS_BUFLEN_ATTRDB
208 #define	NSS_BUFLEN_AUTHATTR	NSS_BUFLEN_ATTRDB
209 #define	NSS_BUFLEN_EXECATTR	NSS_BUFLEN_ATTRDB
210 #define	NSS_BUFLEN_PROFATTR	NSS_BUFLEN_ATTRDB
211 #define	NSS_BUFLEN_USERATTR	((NSS_BUFLEN_ATTRDB) * 8)
212 
213 #define	NSS_BUFLEN_TSOL		NSS_LINELEN_TSOL
214 #define	NSS_BUFLEN_TSOL_TP	NSS_BUFLEN_TSOL
215 #define	NSS_BUFLEN_TSOL_RH	NSS_BUFLEN_TSOL
216 #define	NSS_BUFLEN_TSOL_ZC	NSS_BUFLEN_TSOL
217 
218 /*
219  * Default cache door buffer size (2x largest buffer)
220  */
221 
222 #define	NSS_BUFLEN_DOOR		((NSS_BUFSIZ) * 16)
223 
224 /*
225  * Arguments and results, passed between the frontends and backends for
226  * the well-known databases.  The getXbyY_r() and getXent_r() routines
227  * use a common format that is further described below;  other routines
228  * use their own formats.
229  */
230 
231 /*
232  * The nss_str2ent_t routine is the data marshaller for the nsswitch.
233  * it converts 'native files' format into 'entry' format as part of the
234  * return processing for a getXbyY interface.
235  *
236  * The nss_groupstr_t routine does the real work for any backend
237  * that can supply a netgroup entry as a string in /etc/group format
238  */
239 #if defined(__STDC__)
240 typedef int		(*nss_str2ent_t)(const char *in, int inlen,
241 				void *ent, char *buf, int buflen);
242 
243 struct nss_groupsbymem;		/* forward definition */
244 typedef nss_status_t	(*nss_groupstr_t)(const char *instr, int inlen,
245 				struct nss_groupsbymem *);
246 #else
247 typedef int		(*nss_str2ent_t)();
248 typedef nss_status_t	(*nss_groupstr_t)();
249 #endif
250 
251 /*
252  * The initgroups() function [see initgroups(3c)] needs to find all the
253  *   groups to which a given user belongs.  To do this it calls
254  *   _getgroupsbymember(), which is part of the frontend for the "group"
255  *   database.
256  * We want the same effect as if we used getgrent_r() to enumerate the
257  *   entire groups database (possibly from multiple sources), but getgrent_r()
258  *   is too inefficient.  Most backends can do better if they know they're
259  *   meant to scan all groups;  hence there's a separate backend operation,
260  *   NSS_DBOP_GROUP_BYMEMBER, which uses the nss_groupsbymem struct.
261  * Note that the normal return-value from such a backend, even when it
262  *   successfully finds matching group entries, is NSS_NOTFOUND, because
263  *   this tells the switch engine to keep searching in any more sources.
264  *   In fact, the backends only return NSS_SUCCESS if they find enough
265  *   matching entries that the gid_array is completely filled, in which
266  *   case the switch engine should stop searching.
267  * If the force_slow_way field is set, the backend should eschew any cached
268  *   information (e.g. the YP netid.byname map or the NIS+ cred.org_dir table)
269  *   and should instead grind its way through the group map/table/whatever.
270  */
271 
272 struct nss_groupsbymem {			/* For _getgroupsbymember() */
273 /* in: */
274 	const char	*username;
275 	gid_t		*gid_array;
276 	int		maxgids;
277 	int		force_slow_way;
278 	nss_str2ent_t	str2ent;
279 	nss_groupstr_t	process_cstr;
280 
281 /* in_out: */
282 	int		numgids;
283 };
284 
285 /*
286  * The netgroup routines are handled as follows:
287  *
288  *   Policy decision:
289  *	If netgroup A refers to netgroup B, both must occur in the same
290  *	source (other choices give very confusing semantics).  This
291  *	assumption is deeply embedded in the frontend and backends.
292  *
293  *    -	setnetgrent(), despite its name, is really a getXXXbyYYY operation:
294  *	it takes a name and finds a netgroup with that name (see the
295  *	nss_setnetgrent_args struct below).  The "result" that it returns
296  *	to the frontend is an nss_backend_t for a pseudo-backend that allows
297  *	one to enumerate the members of that netgroup.
298  *
299  *    -	getnetgrent() calls the 'getXXXent' function in the pseudo-backend;
300  *	it doesn't go through the switch engine at all.  It uses the
301  *	nss_getnetgrent_args struct below.
302  *
303  *    -	innetgr() is implemented on top of __multi_innetgr(), which replaces
304  *	each (char *) argument of innetgr() with a counted vector of (char *).
305  *	The semantics are the same as an OR of the results of innetgr()
306  *	operations on each possible 4-tuple picked from the arguments, but
307  *	it's possible to implement some cases more efficiently.  This is
308  *	important for mountd, which used to read YP netgroup.byhost directly
309  *	in order to determine efficiently whether a given host belonged to any
310  *	one of a long list of netgroups.  Wildcarded arguments are indicated
311  *	by a count of zero.
312  *
313  *    -	__multi_innetgr() uses the nss_innetgr_args struct.  A backend whose
314  *	source contains at least one of the groups listed in the 'groups'
315  *	vector will return NSS_SUCCESS and will set the 'status' field to
316  *	indicate whether any 4-tuple was satisfied.  A backend will only
317  *	return NSS_NOTFOUND if the source contained none of the groups
318  *	listed in the 'groups' vector.
319  */
320 
321 enum nss_netgr_argn {		/* We need (machine, user, domain) triples */
322 	NSS_NETGR_MACHINE = 0,
323 	NSS_NETGR_USER = 1,
324 	NSS_NETGR_DOMAIN = 2,
325 	NSS_NETGR_N = 3
326 };
327 
328 enum nss_netgr_status {		/* Status from setnetgrent, multi_innetgr */
329 	NSS_NETGR_FOUND = 0,
330 	NSS_NETGR_NO = 1,
331 	NSS_NETGR_NOMEM = 2
332 };
333 
334 struct nss_setnetgrent_args {
335 /* in: */
336 	const char		*netgroup;
337 /* out: */
338 	nss_backend_t		*iterator;	/* <==== Explain */
339 };
340 
341 struct nss_getnetgrent_args {
342 /* in: */
343 	char			*buffer;
344 	int			buflen;
345 /* out: */
346 	enum nss_netgr_status	status;
347 	char			*retp[NSS_NETGR_N];
348 };
349 
350 typedef unsigned	nss_innetgr_argc;    /* 0 means wildcard */
351 typedef char **		nss_innetgr_argv;    /* === Do we really need these? */
352 
353 struct nss_innetgr_1arg {
354 	nss_innetgr_argc	argc;
355 	nss_innetgr_argv	argv;
356 };
357 
358 struct nss_innetgr_args {
359 /* in: */
360 	struct nss_innetgr_1arg	arg[NSS_NETGR_N];
361 	struct nss_innetgr_1arg groups;
362 /* out: */
363 	enum nss_netgr_status	status;
364 };
365 
366 /*
367  * nss_XbyY_buf_t -- structure containing the generic arguments passwd to
368  *   getXXXbyYYY_r() and getXXXent_r() routines.  The (void *) value points to
369  *   a struct of the appropriate type, e.g. struct passwd or struct hostent.
370  *
371  * The functions that allocate and free these structures do no locking at
372  * all, since the routines that use them are inherently MT-unsafe anyway.
373  */
374 
375 typedef struct {
376 	void		*result;	/* "result" parameter to getXbyY_r() */
377 	char		*buffer;	/* "buffer"     "             "      */
378 	int		buflen;		/* "buflen"     "             "      */
379 } nss_XbyY_buf_t;
380 
381 #if defined(__STDC__)
382 extern nss_XbyY_buf_t	*_nss_XbyY_buf_alloc(int struct_size, int buffer_size);
383 extern void		 _nss_XbyY_buf_free(nss_XbyY_buf_t *);
384 #else
385 extern nss_XbyY_buf_t	*_nss_XbyY_buf_alloc();
386 extern void		 _nss_XbyY_buf_free();
387 #endif
388 
389 #define	NSS_XbyY_ALLOC(bufpp, str_size, buf_size)		(\
390 	(*bufpp) == 0						\
391 	? (*bufpp) = _nss_XbyY_buf_alloc(str_size, buf_size)	\
392 	: (*bufpp))
393 
394 #define	NSS_XbyY_FREE(bufpp)	(_nss_XbyY_buf_free(*bufpp), (*bufpp) = 0)
395 
396 /*
397  * The nss_XbyY_args_t struct contains all the information passed between
398  * frontends and backends for the getXbyY_r() and getXent() routines,
399  * including an nss_XbyY_buf_t and the lookup key (unused for getXXXent_r).
400  *
401  * The (*str2ent)() member converts a single XXXent from ASCII text to the
402  * appropriate struct, storing any pointer data (strings, in_addrs, arrays
403  * of these) in the buffer.  The ASCII text is a counted string (*not* a
404  * zero-terminated string) whose length is specified by the instr_len
405  * parameter.  The text is found at the address specified by instr and
406  * the string is treated as readonly. buffer and instr must be non-
407  * intersecting memory areas.
408  *
409  * With the exception of passwd, shadow and group, the text form for these
410  * databases allows trailing comments and arbitrary whitespace.  The
411  * corresponding str2ent routine assumes that comments, leading whitespace
412  * and trailing whitespace have been stripped (and thus assumes that entries
413  * consisting only of these have been discarded).
414  *
415  * The text entries for "rpc" and for the databases described in <netdb.h>
416  * follow a common format (a canonical name with a possibly empty list
417  * of aliases, and some other value), albeit with minor variations.
418  * The function _nss_netdb_aliases() does most of the generic work involved
419  * in parsing and marshalling these into the buffer.
420  */
421 
422 typedef union nss_XbyY_key {	/* No tag; backend should know what to expect */
423 	uid_t		uid;
424 	gid_t		gid;
425 	projid_t	projid;
426 	const char	*name;
427 	int		number;
428 	struct {
429 		int	net;
430 		int		type;
431 	}	netaddr;
432 	struct {
433 		const char	*addr;
434 		int		len;
435 		int		type;
436 	}	hostaddr;
437 	struct {
438 		union {
439 			const char	*name;
440 			int		port;
441 		}		serv;
442 		const char	*proto;
443 	}	serv;
444 	void *ether;
445 	struct {
446 		const char	*name;
447 		const char	*keytype;
448 	} pkey;
449 	struct {
450 		const char	*name;
451 		int		af_family;
452 		int		flags;
453 	}	ipnode;
454 	void *attrp;	/* for the new attr databases */
455 } nss_XbyY_key_t;
456 
457 
458 #if defined(__STDC__)
459 typedef int		(*nss_key2str_t)(void *buffer, size_t buflen,
460 				nss_XbyY_key_t *key, size_t *len);
461 #else
462 typedef int		(*nss_key2str_t)();
463 #endif
464 
465 
466 typedef struct nss_XbyY_args {
467 
468 /* IN */
469 	nss_XbyY_buf_t	buf;
470 	int		stayopen;
471 			/*
472 			 * Support for setXXXent(stayopen)
473 			 * Used only in hosts, protocols,
474 			 * networks, rpc, and services.
475 			 */
476 	nss_str2ent_t	str2ent;
477 	union nss_XbyY_key key;
478 
479 /* OUT */
480 	void		*returnval;
481 	int		erange;
482 	int		h_errno;	/* For gethost*_r() */
483 	nss_status_t	status;		/* from the backend last called */
484 /* NSS2 */
485 	nss_key2str_t	key2str;	/* IN */
486 	size_t		returnlen;	/* OUT */
487 
488 /* NSCD/DOOR data */
489 
490 /* ... buffer arena follows... */
491 } nss_XbyY_args_t;
492 
493 
494 
495 /*
496  * nss/nscd v2 interface, packed buffer format
497  *
498  * A key component of the v2 name service switch is the redirection
499  * of all activity to nscd for actual processing.  In the original
500  * switch most activity took place in each application, and the nscd
501  * cache component was an add-on optional interface.
502  *
503  * The nscd v1 format was a completely private interface that
504  * implemented specific bufferiing formats on a per getXbyY API basis.
505  *
506  * The nss/nscd v2 interface uses a common header and commonalizes
507  * the buffering format as consistently as possible.  The general rule
508  * of thumb is that backends are required to assemble their results in
509  * "files based" format [IE the format used on a per result basis as
510  * returned by the files backend] and then call the standard str2ent
511  * interface.  This is the original intended design as used in the files
512  * and nis backends.
513  *
514  * The benefit of this is that the application side library can assemble
515  * a request and provide a header and a variable length result buffer via
516  * a doors API, and then the nscd side switch can assemble a a getXbyY
517  * request providing the result buffer and a str2ent function that copies
518  * but does not unpack the result.
519  *
520  * This results is returned back via the door, and unpacked using the
521  * native library side str2ent interface.
522  *
523  * Additionally, the common header allows extensibility to add new
524  * getXbyYs, putXbyYs or other maintenance APIs to/from nscd without
525  * changing the existing "old style" backend interfaces.
526  *
527  * Finally new style getXbyY, putXbyY and backend interfaces can be
528  * by adding new operation requests to the header, while old style
529  * backwards compatability.
530  */
531 
532 /*
533  * nss/nscd v2 callnumber definitions
534  */
535 
536 /*
537  * callnumbers are separated by categories, such as:
538  * application to nscd requests, nscd to nscd requests,
539  * smf to nscd requests, etc.
540  */
541 
542 #define	NSCDV2CATMASK	(0xFF000000)
543 #define	NSCDV2CALLMASK	(0x00FFFFFF)
544 
545 /*
546  * nss/nscd v2 categories
547  */
548 
549 #define	NSCD_CALLCAT_APP	('a'<<24)
550 #define	NSCD_CALLCAT_N2N	('n'<<24)
551 
552 /* nscd v2 app-> nscd callnumbers */
553 
554 #define	NSCD_SEARCH	(NSCD_CALLCAT_APP|0x01)
555 #define	NSCD_SETENT	(NSCD_CALLCAT_APP|0x02)
556 #define	NSCD_GETENT	(NSCD_CALLCAT_APP|0x03)
557 #define	NSCD_ENDENT	(NSCD_CALLCAT_APP|0x04)
558 #define	NSCD_PUT	(NSCD_CALLCAT_APP|0x05)
559 #define	NSCD_GETHINTS	(NSCD_CALLCAT_APP|0x06)
560 
561 /* nscd v2 SETENT cookie markers */
562 
563 #define	NSCD_NEW_COOKIE		0
564 #define	NSCD_LOCAL_COOKIE	1
565 
566 /* nscd v2 header revision */
567 /* treated as 0xMMMMmmmm MMMM - Major Rev, mmmm - Minor Rev */
568 
569 #define	NSCD_HEADER_REV		0x00020000
570 
571 /*
572  * ptr/uint data type used to calculate shared nscd buffer struct sizes
573  * sizes/offsets are arbitrarily limited to 32 bits for 32/64 compatibility
574  * datatype is 64 bits for possible pointer storage and future use
575  */
576 
577 typedef uint64_t	nssuint_t;
578 
579 /*
580  * nscd v2 buffer layout overview
581  *
582  * The key interface to nscd moving forward is the doors interface
583  * between applications and nscd (NSCD_CALLCAT_APP), and nscd and
584  * it's children (NSCD_CALLCAT_N2N).
585  *
586  * Regardless of the interface used, the buffer layout is consistent.
587  * The General Layout is:
588  *   [nss_pheader_t][IN key][OUT data results]{extend results}
589  *
590  *   The header (nss_pheader_t) remains constant.
591  *   Keys and key layouts vary between call numbers/requests
592  *	NSCD_CALLCAT_APP use key layouts mimics/defines in nss_dbdefs.h
593  *	NSCD_CALLCAT_NSN use layouts defined by nscd headers
594  *   Data and data results vary between results
595  *	NSCD_CALLCAT_APP return "file standard format" output buffers
596  *	NSCD_CALLCAT_NSN return data defined by nscd headers
597  *   extended results are optional and vary
598  *
599  */
600 
601 /*
602  * nss_pheader_t -- buffer header structure that contains switch data
603  * "packed" by the client into a buffer suitable for transport over
604  * nscd's door, and that can be unpacked into a native form within
605  * nscd's switch.  Capable of packing and unpacking data ans results.
606  *
607  * NSCD_HEADER_REV: 0x00020000		16 x uint64 = (128 byte header)
608  */
609 
610 typedef struct {
611 	uint32_t	nsc_callnumber;		/* packed buffer request */
612 	uint32_t	nss_dbop;		/* old nss dbop */
613 	uint32_t	p_ruid;			/* real uid */
614 	uint32_t	p_euid;			/* effective uid */
615 	uint32_t	p_version;		/* 0xMMMMmmmm Major/minor */
616 	uint32_t	p_status;		/* nss_status_t */
617 	uint32_t	p_errno;		/* errno */
618 	uint32_t	p_herrno;		/* h_errno */
619 	nssuint_t	libpriv;		/* reserved (for lib/client) */
620 	nssuint_t	pbufsiz;		/* buffer size */
621 	nssuint_t	dbd_off;		/* IN: db desc off */
622 	nssuint_t	dbd_len;		/* IN: db desc len */
623 	nssuint_t	key_off;		/* IN: key off */
624 	nssuint_t	key_len;		/* IN: key len */
625 	nssuint_t	data_off;		/* OUT: data off */
626 	nssuint_t	data_len;		/* OUT: data len */
627 	nssuint_t	ext_off;		/* OUT: extended results off */
628 	nssuint_t	ext_len;		/* OUT: extended results len */
629 	nssuint_t	nscdpriv;		/* reserved (for nscd) */
630 	nssuint_t	reserved1;		/* reserved (TBD) */
631 } nss_pheader_t;
632 
633 /*
634  * nss_pnetgr_t -- packed offset structure for holding keys used
635  * by innetgr (__multi_innetgr) key
636  * Key format is:
637  *    nss_pnetgr_t
638  *     (nssuint_t)[machine_argc] offsets to strings
639  *     (nssuint_t)[user_argc] offsets to strings
640  *     (nssuint_t)[domain_argc] offsets to strings
641  *     (nssuint_t)[groups_argc] offsets to strings
642  *     machine,user,domain,groups strings
643  */
644 
645 typedef struct {
646 	uint32_t	machine_argc;
647 	uint32_t	user_argc;
648 	uint32_t	domain_argc;
649 	uint32_t	groups_argc;
650 	nssuint_t	machine_offv;
651 	nssuint_t	user_offv;
652 	nssuint_t	domain_offv;
653 	nssuint_t	groups_offv;
654 } nss_pnetgr_t;
655 
656 
657 /* status returned by the str2ent parsing routines */
658 #define	NSS_STR_PARSE_SUCCESS 0
659 #define	NSS_STR_PARSE_PARSE 1
660 #define	NSS_STR_PARSE_ERANGE 2
661 
662 #define	NSS_XbyY_INIT(str, res, bufp, len, func)	(\
663 	(str)->buf.result = (res),			\
664 	(str)->buf.buffer = (bufp),			\
665 	(str)->buf.buflen = (len),			\
666 	(str)->stayopen  = 0,				\
667 	(str)->str2ent  = (func),			\
668 	(str)->key2str  = NULL,				\
669 	(str)->returnval = 0,				\
670 	(str)->returnlen = 0,				\
671 	(str)->erange    = 0)
672 
673 #define	NSS_XbyY_INIT_EXT(str, res, bufp, len, func, kfunc)	(\
674 	(str)->buf.result = (res),			\
675 	(str)->buf.buffer = (bufp),			\
676 	(str)->buf.buflen = (len),			\
677 	(str)->stayopen  = 0,				\
678 	(str)->str2ent  = (func),			\
679 	(str)->key2str  = (kfunc),			\
680 	(str)->returnval = 0,				\
681 	(str)->returnlen = 0,				\
682 	(str)->erange    = 0)
683 
684 #define	NSS_XbyY_FINI(str)				(\
685 	(str)->returnval == 0 && (str)->erange && (errno = ERANGE), \
686 	(str)->returnval)
687 
688 #define	NSS_PACKED_CRED_CHECK(buf, ruid, euid)		(\
689 	((nss_pheader_t *)(buf))->p_ruid == (ruid) && \
690 	((nss_pheader_t *)(buf))->p_euid == (euid))
691 
692 #if defined(__STDC__)
693 extern char		**_nss_netdb_aliases(const char *, int, char *, int);
694 extern nss_status_t	nss_default_key2str(void *, size_t, nss_XbyY_args_t *,
695 					const char *, int, size_t *);
696 extern nss_status_t	nss_packed_arg_init(void *, size_t, nss_db_root_t *,
697 					nss_db_initf_t *, int *,
698 					nss_XbyY_args_t *);
699 extern nss_status_t	nss_packed_context_init(void *, size_t, nss_db_root_t *,
700 					nss_db_initf_t *, nss_getent_t **,
701 					nss_XbyY_args_t *);
702 extern void		nss_packed_set_status(void *, size_t, nss_status_t,
703 					nss_XbyY_args_t *);
704 extern nss_status_t	nss_packed_getkey(void *, size_t, char **, int *,
705 					nss_XbyY_args_t *);
706 #else
707 extern char		**_nss_netdb_aliases();
708 extern int		nss_default_key2str();
709 extern nss_status_t	nss_packed_arg_init();
710 extern nss_status_t	nss_packed_context_init();
711 extern void		nss_packed_set_status();
712 extern nss_status_t	nss_packed_getkey();
713 #endif
714 
715 /*
716  * nss_dbop_t values for searches with various keys;  values for
717  * destructor/endent/setent/getent are defined in <nss_common.h>
718  */
719 
720 /*
721  * These are part of the "Over the wire" IE app->nscd getXbyY
722  * op for well known getXbyY's.  Cannot use NSS_DBOP_X_Y directly
723  * because NSS_DBOP_next_iter is NOT an incrementing counter value
724  * it's a starting offset into an array value.
725  */
726 
727 #define	NSS_DBOP_X(x)			((x)<<16)
728 #define	NSS_DBOP_XY(x, y)		((x)|(y))
729 
730 #define	NSS_DBOP_ALIASES	NSS_DBOP_X(1)
731 #define	NSS_DBOP_AUTOMOUNT	NSS_DBOP_X(2)
732 #define	NSS_DBOP_BOOTPARAMS	NSS_DBOP_X(3)
733 #define	NSS_DBOP_ETHERS		NSS_DBOP_X(4)
734 #define	NSS_DBOP_GROUP		NSS_DBOP_X(5)
735 #define	NSS_DBOP_HOSTS		NSS_DBOP_X(6)
736 #define	NSS_DBOP_IPNODES	NSS_DBOP_X(7)
737 #define	NSS_DBOP_NETGROUP	NSS_DBOP_X(8)
738 #define	NSS_DBOP_NETMASKS	NSS_DBOP_X(9)
739 #define	NSS_DBOP_NETWORKS	NSS_DBOP_X(10)
740 #define	NSS_DBOP_PASSWD		NSS_DBOP_X(11)
741 #define	NSS_DBOP_PRINTERS	NSS_DBOP_X(12)
742 #define	NSS_DBOP_PROJECT	NSS_DBOP_X(13)
743 #define	NSS_DBOP_PROTOCOLS	NSS_DBOP_X(14)
744 #define	NSS_DBOP_PUBLICKEY	NSS_DBOP_X(15)
745 #define	NSS_DBOP_RPC		NSS_DBOP_X(16)
746 #define	NSS_DBOP_SERVICES	NSS_DBOP_X(17)
747 #define	NSS_DBOP_AUDITUSER	NSS_DBOP_X(18)
748 #define	NSS_DBOP_AUTHATTR	NSS_DBOP_X(19)
749 #define	NSS_DBOP_EXECATTR	NSS_DBOP_X(20)
750 #define	NSS_DBOP_PROFATTR	NSS_DBOP_X(21)
751 #define	NSS_DBOP_USERATTR	NSS_DBOP_X(22)
752 
753 #define	NSS_DBOP_GROUP_BYNAME		(NSS_DBOP_next_iter)
754 #define	NSS_DBOP_GROUP_BYGID		(NSS_DBOP_GROUP_BYNAME + 1)
755 #define	NSS_DBOP_GROUP_BYMEMBER		(NSS_DBOP_GROUP_BYGID  + 1)
756 
757 #define	NSS_DBOP_PASSWD_BYNAME		(NSS_DBOP_next_iter)
758 #define	NSS_DBOP_PASSWD_BYUID		(NSS_DBOP_PASSWD_BYNAME + 1)
759 
760 /* The "compat" backend requires that PASSWD_BYNAME == SHADOW_BYNAME */
761 /*   (it also requires that both use key.name to pass the username). */
762 #define	NSS_DBOP_SHADOW_BYNAME		(NSS_DBOP_PASSWD_BYNAME)
763 
764 #define	NSS_DBOP_PROJECT_BYNAME		(NSS_DBOP_next_iter)
765 #define	NSS_DBOP_PROJECT_BYID		(NSS_DBOP_PROJECT_BYNAME + 1)
766 
767 #define	NSS_DBOP_HOSTS_BYNAME		(NSS_DBOP_next_iter)
768 #define	NSS_DBOP_HOSTS_BYADDR		(NSS_DBOP_HOSTS_BYNAME + 1)
769 
770 #define	NSS_DBOP_IPNODES_BYNAME		(NSS_DBOP_next_iter)
771 #define	NSS_DBOP_IPNODES_BYADDR		(NSS_DBOP_IPNODES_BYNAME + 1)
772 
773 /*
774  * NSS_DBOP_NAME_2ADDR
775  * NSS_DBOP_ADDR_2NAME
776  *                                : are defines for ipv6 api's
777  */
778 
779 #define	NSS_DBOP_NAME_2ADDR		(NSS_DBOP_next_ipv6_iter)
780 #define	NSS_DBOP_ADDR_2NAME		(NSS_DBOP_NAME_2ADDR + 1)
781 
782 #define	NSS_DBOP_RPC_BYNAME		(NSS_DBOP_next_iter)
783 #define	NSS_DBOP_RPC_BYNUMBER		(NSS_DBOP_RPC_BYNAME + 1)
784 
785 #define	NSS_DBOP_NETWORKS_BYNAME		(NSS_DBOP_next_iter)
786 #define	NSS_DBOP_NETWORKS_BYADDR		(NSS_DBOP_NETWORKS_BYNAME + 1)
787 
788 #define	NSS_DBOP_SERVICES_BYNAME	(NSS_DBOP_next_iter)
789 #define	NSS_DBOP_SERVICES_BYPORT	(NSS_DBOP_SERVICES_BYNAME + 1)
790 
791 #define	NSS_DBOP_PROTOCOLS_BYNAME	(NSS_DBOP_next_iter)
792 #define	NSS_DBOP_PROTOCOLS_BYNUMBER	(NSS_DBOP_PROTOCOLS_BYNAME + 1)
793 
794 #define	NSS_DBOP_ETHERS_HOSTTON	(NSS_DBOP_next_noiter)
795 #define	NSS_DBOP_ETHERS_NTOHOST	(NSS_DBOP_ETHERS_HOSTTON + 1)
796 
797 #define	NSS_DBOP_BOOTPARAMS_BYNAME	(NSS_DBOP_next_noiter)
798 #define	NSS_DBOP_NETMASKS_BYNET	(NSS_DBOP_next_noiter)
799 
800 #define	NSS_DBOP_PRINTERS_BYNAME	(NSS_DBOP_next_iter)
801 
802 /*
803  * The "real" backend for netgroup (__multi_innetgr, setnetgrent)
804  */
805 #define	NSS_DBOP_NETGROUP_IN		(NSS_DBOP_next_iter)
806 #define	NSS_DBOP_NETGROUP_SET		(NSS_DBOP_NETGROUP_IN  + 1)
807 
808 /*
809  * The backend for getpublickey and getsecretkey (getkeys)
810  */
811 #define	NSS_DBOP_KEYS_BYNAME		(NSS_DBOP_next_iter)
812 
813 /*
814  * The pseudo-backend for netgroup (returned by setnetgrent) doesn't have
815  *   any getXXXbyYYY operations, just the usual destr/end/set/get ops,
816  *   so needs no definitions here.
817  */
818 
819 #define	NSS_DBOP_ATTRDB_BYNAME		(NSS_DBOP_next_iter)
820 
821 #define	NSS_DBOP_AUDITUSER_BYNAME	NSS_DBOP_ATTRDB_BYNAME
822 #define	NSS_DBOP_AUTHATTR_BYNAME	NSS_DBOP_ATTRDB_BYNAME
823 #define	NSS_DBOP_EXECATTR_BYNAME	NSS_DBOP_ATTRDB_BYNAME
824 #define	NSS_DBOP_EXECATTR_BYID		(NSS_DBOP_EXECATTR_BYNAME + 1)
825 #define	NSS_DBOP_EXECATTR_BYNAMEID	(NSS_DBOP_EXECATTR_BYID + 1)
826 #define	NSS_DBOP_PROFATTR_BYNAME	NSS_DBOP_ATTRDB_BYNAME
827 #define	NSS_DBOP_USERATTR_BYNAME	NSS_DBOP_ATTRDB_BYNAME
828 
829 #define	NSS_DBOP_TSOL_TP_BYNAME		(NSS_DBOP_next_iter)
830 #define	NSS_DBOP_TSOL_RH_BYADDR		(NSS_DBOP_next_iter)
831 #define	NSS_DBOP_TSOL_ZC_BYNAME		(NSS_DBOP_next_iter)
832 
833 /*
834  * Used all over in the switch code. The best home for it I can think of.
835  * Power-of-two alignments only.
836  */
837 #define	ROUND_DOWN(n, align)	(((uintptr_t)n) & ~((align) - 1l))
838 #define	ROUND_UP(n, align)	ROUND_DOWN(((uintptr_t)n) + (align) - 1l, \
839 				(align))
840 
841 #ifdef	__cplusplus
842 }
843 #endif
844 
845 #endif /* _NSS_DBDEFS_H */
846