xref: /titanic_52/usr/src/lib/libnisdb/ldap_scheme.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "ldap_scheme.h"
30*7c478bd9Sstevel@tonic-gate #include "ldap_util.h"
31*7c478bd9Sstevel@tonic-gate #include "ldap_nisdbquery.h"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * Input:  A db_query where the 'which_index' fields refer to the schema
36*7c478bd9Sstevel@tonic-gate  *         columns.
37*7c478bd9Sstevel@tonic-gate  * Output: A db_query where the 'which_index' fields refer to the table
38*7c478bd9Sstevel@tonic-gate  *         columns.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate db_query *
41*7c478bd9Sstevel@tonic-gate schemeQuery2Query(db_query *qin, db_scheme *s) {
42*7c478bd9Sstevel@tonic-gate 	db_query	*q;
43*7c478bd9Sstevel@tonic-gate 	int		i;
44*7c478bd9Sstevel@tonic-gate 	char		*myself = "schemeQuery2Query";
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	q = cloneQuery(qin, 0);
47*7c478bd9Sstevel@tonic-gate 	if (q == 0 || s == 0)
48*7c478bd9Sstevel@tonic-gate 		return (q);
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < q->components.components_len; i++) {
51*7c478bd9Sstevel@tonic-gate 		int	index = q->components.components_val[i].which_index;
52*7c478bd9Sstevel@tonic-gate 		if (index >= s->keys.keys_len) {
53*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
54*7c478bd9Sstevel@tonic-gate 				"%s: query index %d out-of-range (%d)",
55*7c478bd9Sstevel@tonic-gate 				myself, index, s->keys.keys_len-1);
56*7c478bd9Sstevel@tonic-gate 			freeQuery(q);
57*7c478bd9Sstevel@tonic-gate 			return (0);
58*7c478bd9Sstevel@tonic-gate 		}
59*7c478bd9Sstevel@tonic-gate 		q->components.components_val[i].which_index =
60*7c478bd9Sstevel@tonic-gate 			s->keys.keys_val[index].column_number - 1;
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	return (q);
64*7c478bd9Sstevel@tonic-gate }
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate static const char	*dirCol = "name";
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * Input:  A db_query where the 'which_index' fields refer to the scheme
70*7c478bd9Sstevel@tonic-gate  *         columns, space for a nis_attr array with at least q->components->
71*7c478bd9Sstevel@tonic-gate  *	   components_len elements, a scheme, and a __nis_table_mapping_t
72*7c478bd9Sstevel@tonic-gate  *	   (for column names).
73*7c478bd9Sstevel@tonic-gate  * Output: A nis_attr structure with the searchable columns.
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate nis_attr *
76*7c478bd9Sstevel@tonic-gate schemeQuery2nisAttr(db_query *q, nis_attr *space, db_scheme *s,
77*7c478bd9Sstevel@tonic-gate 		__nis_table_mapping_t *t, int *numAttr) {
78*7c478bd9Sstevel@tonic-gate 	nis_attr	*a;
79*7c478bd9Sstevel@tonic-gate 	int		na, i, nc;
80*7c478bd9Sstevel@tonic-gate 	char		**col;
81*7c478bd9Sstevel@tonic-gate 	char		*myself = "schemeQuery2nisAttr";
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	if (q == 0 || space == 0 || s == 0 || t == 0 || numAttr == 0)
84*7c478bd9Sstevel@tonic-gate 		return (0);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/*
87*7c478bd9Sstevel@tonic-gate 	 * A table will have the column names stored in the mapping
88*7c478bd9Sstevel@tonic-gate 	 * structure, while a directory only has a single column
89*7c478bd9Sstevel@tonic-gate 	 * called "name". The latter isn't stored in the mapping,
90*7c478bd9Sstevel@tonic-gate 	 * so we create a column name array for a directory.
91*7c478bd9Sstevel@tonic-gate 	 */
92*7c478bd9Sstevel@tonic-gate 	if (t->numColumns > 0) {
93*7c478bd9Sstevel@tonic-gate 		col = t->column;
94*7c478bd9Sstevel@tonic-gate 		nc = t->numColumns;
95*7c478bd9Sstevel@tonic-gate 	} else {
96*7c478bd9Sstevel@tonic-gate 		if (t->objType == NIS_DIRECTORY_OBJ) {
97*7c478bd9Sstevel@tonic-gate 			col = (char **)&dirCol;
98*7c478bd9Sstevel@tonic-gate 			nc = 1;
99*7c478bd9Sstevel@tonic-gate 		} else {
100*7c478bd9Sstevel@tonic-gate 			return (0);
101*7c478bd9Sstevel@tonic-gate 		}
102*7c478bd9Sstevel@tonic-gate 	}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	a = space;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	for (i = 0, na = 0; i < q->components.components_len; i++) {
107*7c478bd9Sstevel@tonic-gate 		int	index;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		if (q->components.components_val[i].which_index >=
110*7c478bd9Sstevel@tonic-gate 				s->keys.keys_len) {
111*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
112*7c478bd9Sstevel@tonic-gate 				"%s: query index %d out-of-range (%d)",
113*7c478bd9Sstevel@tonic-gate 				myself,
114*7c478bd9Sstevel@tonic-gate 				q->components.components_val[i].which_index,
115*7c478bd9Sstevel@tonic-gate 				s->keys.keys_len-1);
116*7c478bd9Sstevel@tonic-gate 			return (0);
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		index = s->keys.keys_val[i].column_number - 1;
120*7c478bd9Sstevel@tonic-gate 		if (index >= nc) {
121*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
122*7c478bd9Sstevel@tonic-gate 				"%s: column index out-of-range (%d >= %d)",
123*7c478bd9Sstevel@tonic-gate 				myself, index, nc);
124*7c478bd9Sstevel@tonic-gate 			return (0);
125*7c478bd9Sstevel@tonic-gate 		}
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 		a[na].zattr_ndx = col[index];
128*7c478bd9Sstevel@tonic-gate 		a[na].zattr_val.zattr_val_val =	q->components.
129*7c478bd9Sstevel@tonic-gate 			components_val[i].index_value->itemvalue.itemvalue_val;
130*7c478bd9Sstevel@tonic-gate 		a[na].zattr_val.zattr_val_len = q->components.
131*7c478bd9Sstevel@tonic-gate 			components_val[i].index_value->itemvalue.itemvalue_len;
132*7c478bd9Sstevel@tonic-gate 		na++;
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	*numAttr = na;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	return (a);
138*7c478bd9Sstevel@tonic-gate }
139