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 #include "ldap_scheme.h"
28*7c478bd9Sstevel@tonic-gate #include "ldap_util.h"
29*7c478bd9Sstevel@tonic-gate #include "ldap_nisdbquery.h"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * Input:  A db_query where the 'which_index' fields refer to the schema
34*7c478bd9Sstevel@tonic-gate  *         columns.
35*7c478bd9Sstevel@tonic-gate  * Output: A db_query where the 'which_index' fields refer to the table
36*7c478bd9Sstevel@tonic-gate  *         columns.
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate db_query *
schemeQuery2Query(db_query * qin,db_scheme * s)39*7c478bd9Sstevel@tonic-gate schemeQuery2Query(db_query *qin, db_scheme *s) {
40*7c478bd9Sstevel@tonic-gate 	db_query	*q;
41*7c478bd9Sstevel@tonic-gate 	int		i;
42*7c478bd9Sstevel@tonic-gate 	char		*myself = "schemeQuery2Query";
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	q = cloneQuery(qin, 0);
45*7c478bd9Sstevel@tonic-gate 	if (q == 0 || s == 0)
46*7c478bd9Sstevel@tonic-gate 		return (q);
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < q->components.components_len; i++) {
49*7c478bd9Sstevel@tonic-gate 		int	index = q->components.components_val[i].which_index;
50*7c478bd9Sstevel@tonic-gate 		if (index >= s->keys.keys_len) {
51*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
52*7c478bd9Sstevel@tonic-gate 				"%s: query index %d out-of-range (%d)",
53*7c478bd9Sstevel@tonic-gate 				myself, index, s->keys.keys_len-1);
54*7c478bd9Sstevel@tonic-gate 			freeQuery(q);
55*7c478bd9Sstevel@tonic-gate 			return (0);
56*7c478bd9Sstevel@tonic-gate 		}
57*7c478bd9Sstevel@tonic-gate 		q->components.components_val[i].which_index =
58*7c478bd9Sstevel@tonic-gate 			s->keys.keys_val[index].column_number - 1;
59*7c478bd9Sstevel@tonic-gate 	}
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	return (q);
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static const char	*dirCol = "name";
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * Input:  A db_query where the 'which_index' fields refer to the scheme
68*7c478bd9Sstevel@tonic-gate  *         columns, space for a nis_attr array with at least q->components->
69*7c478bd9Sstevel@tonic-gate  *	   components_len elements, a scheme, and a __nis_table_mapping_t
70*7c478bd9Sstevel@tonic-gate  *	   (for column names).
71*7c478bd9Sstevel@tonic-gate  * Output: A nis_attr structure with the searchable columns.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate nis_attr *
schemeQuery2nisAttr(db_query * q,nis_attr * space,db_scheme * s,__nis_table_mapping_t * t,int * numAttr)74*7c478bd9Sstevel@tonic-gate schemeQuery2nisAttr(db_query *q, nis_attr *space, db_scheme *s,
75*7c478bd9Sstevel@tonic-gate 		__nis_table_mapping_t *t, int *numAttr) {
76*7c478bd9Sstevel@tonic-gate 	nis_attr	*a;
77*7c478bd9Sstevel@tonic-gate 	int		na, i, nc;
78*7c478bd9Sstevel@tonic-gate 	char		**col;
79*7c478bd9Sstevel@tonic-gate 	char		*myself = "schemeQuery2nisAttr";
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (q == 0 || space == 0 || s == 0 || t == 0 || numAttr == 0)
82*7c478bd9Sstevel@tonic-gate 		return (0);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	/*
85*7c478bd9Sstevel@tonic-gate 	 * A table will have the column names stored in the mapping
86*7c478bd9Sstevel@tonic-gate 	 * structure, while a directory only has a single column
87*7c478bd9Sstevel@tonic-gate 	 * called "name". The latter isn't stored in the mapping,
88*7c478bd9Sstevel@tonic-gate 	 * so we create a column name array for a directory.
89*7c478bd9Sstevel@tonic-gate 	 */
90*7c478bd9Sstevel@tonic-gate 	if (t->numColumns > 0) {
91*7c478bd9Sstevel@tonic-gate 		col = t->column;
92*7c478bd9Sstevel@tonic-gate 		nc = t->numColumns;
93*7c478bd9Sstevel@tonic-gate 	} else {
94*7c478bd9Sstevel@tonic-gate 		if (t->objType == NIS_DIRECTORY_OBJ) {
95*7c478bd9Sstevel@tonic-gate 			col = (char **)&dirCol;
96*7c478bd9Sstevel@tonic-gate 			nc = 1;
97*7c478bd9Sstevel@tonic-gate 		} else {
98*7c478bd9Sstevel@tonic-gate 			return (0);
99*7c478bd9Sstevel@tonic-gate 		}
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	a = space;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	for (i = 0, na = 0; i < q->components.components_len; i++) {
105*7c478bd9Sstevel@tonic-gate 		int	index;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 		if (q->components.components_val[i].which_index >=
108*7c478bd9Sstevel@tonic-gate 				s->keys.keys_len) {
109*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
110*7c478bd9Sstevel@tonic-gate 				"%s: query index %d out-of-range (%d)",
111*7c478bd9Sstevel@tonic-gate 				myself,
112*7c478bd9Sstevel@tonic-gate 				q->components.components_val[i].which_index,
113*7c478bd9Sstevel@tonic-gate 				s->keys.keys_len-1);
114*7c478bd9Sstevel@tonic-gate 			return (0);
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		index = s->keys.keys_val[i].column_number - 1;
118*7c478bd9Sstevel@tonic-gate 		if (index >= nc) {
119*7c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
120*7c478bd9Sstevel@tonic-gate 				"%s: column index out-of-range (%d >= %d)",
121*7c478bd9Sstevel@tonic-gate 				myself, index, nc);
122*7c478bd9Sstevel@tonic-gate 			return (0);
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 		a[na].zattr_ndx = col[index];
126*7c478bd9Sstevel@tonic-gate 		a[na].zattr_val.zattr_val_val =	q->components.
127*7c478bd9Sstevel@tonic-gate 			components_val[i].index_value->itemvalue.itemvalue_val;
128*7c478bd9Sstevel@tonic-gate 		a[na].zattr_val.zattr_val_len = q->components.
129*7c478bd9Sstevel@tonic-gate 			components_val[i].index_value->itemvalue.itemvalue_len;
130*7c478bd9Sstevel@tonic-gate 		na++;
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	*numAttr = na;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	return (a);
136*7c478bd9Sstevel@tonic-gate }
137