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