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