xref: /titanic_50/usr/src/lib/libnisdb/ldap_print.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
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 2001-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "ldap_util.h"
30 #include "ldap_print.h"
31 
32 
33 void
34 printMappingFormat(__nis_mapping_format_t *f) {
35 	__nis_value_t	*val = getMappingFormat(f, 0, fa_any, 0, 0);
36 	int		i;
37 	char		*myself = "printMappingFormat";
38 
39 	if (val == 0)
40 		return;
41 
42 	for (i = 0; i < val->numVals; i++) {
43 		c2buf(myself, val->val[i].value, val->val[i].length);
44 	}
45 	freeValue(val, 1);
46 }
47 
48 void
49 printMappingFormatArray(__nis_mapping_format_t *a) {
50 	__nis_value_t	*val = getMappingFormatArray(a, 0, fa_any, 0, 0);
51 	char		*myself = "printMappingFormatArray";
52 
53 	if (val != 0) {
54 		if (val->type == vt_string) {
55 			int	i;
56 
57 			if (a[0].type != mmt_begin)
58 				p2buf(myself, "\"");
59 			for (i = 0; i < val->numVals; i++) {
60 				sc2buf(myself, val->val[i].value,
61 					val->val[i].length);
62 			}
63 		} else {
64 			p2buf(myself, "<illegal>");
65 		}
66 		freeValue(val, 1);
67 	} else {
68 		p2buf(myself, "<novals>");
69 	}
70 }
71 
72 void
73 printIndex(__nis_index_t *i) {
74 	int	len = 0;
75 	char	*str = getIndex(i, &len);
76 	char	*myself = "printIndex";
77 
78 	sc2buf(myself, str, len);
79 	sfree(str);
80 }
81 
82 void
83 printObjSpec(__nis_obj_spec_t *o) {
84 	int	len = 0;
85 	char	*str = getObjSpec(o, &len);
86 	char	*myself = "printObjSpec";
87 
88 	sc2buf(myself, str, len);
89 	sfree(str);
90 }
91 
92 void
93 printSearchTriple(__nis_search_triple_t *s) {
94 	int	len = 0;
95 	char	*str = getSearchTriple(s, &len);
96 	char	*myself = "printSearchTriple";
97 
98 	sc2buf(myself, str, len);
99 	sfree(str);
100 }
101 
102 void
103 printMappingItem(__nis_mapping_item_t *i, __nis_mapping_item_type_t native) {
104 	__nis_value_t	*val = getMappingItem(i, native, 0, 0, NULL);
105 	int		j;
106 	char		*myself = "printMappingItem";
107 
108 	if (val == 0)
109 		return;
110 
111 	if (i->repeat)
112 		p2buf(myself, "(");
113 	for (j = 0; j < val->numVals; j++) {
114 		c2buf(myself, val->val[j].value, val->val[j].length);
115 	}
116 	if (i->repeat)
117 		p2buf(myself, ")");
118 	freeValue(val, 1);
119 }
120 
121 void
122 printMappingSubElement(__nis_mapping_sub_element_t *e,
123 			__nis_mapping_item_type_t native) {
124 	int	i;
125 	char	*myself = "printMappingSubElement";
126 
127 	switch (e->type) {
128 	case me_item:
129 		printMappingItem(&e->element.item, native);
130 		break;
131 	case me_print:
132 		p2buf(myself, "(");
133 		printMappingFormatArray(e->element.print.fmt);
134 		for (i = 0; i < e->element.print.numItems; i++) {
135 			p2buf(myself, ", ");
136 			printMappingItem(&e->element.print.item[i], native);
137 		}
138 		if (e->element.print.doElide) {
139 			p2buf(myself, ", \"%c\"", e->element.print.elide);
140 		}
141 		p2buf(myself, ")");
142 		break;
143 	case me_split:
144 		p2buf(myself, "(");
145 		printMappingItem(&e->element.split.item, native);
146 		p2buf(myself, ", \"%c\")", e->element.split.delim);
147 		break;
148 	case me_match:
149 		p2buf(myself, "<me_match>");
150 		break;
151 	case me_extract:
152 		p2buf(myself, "(");
153 		printMappingItem(&e->element.extract.item, native);
154 		p2buf(myself, ", ");
155 		printMappingFormatArray(e->element.extract.fmt);
156 		p2buf(myself, ")");
157 		break;
158 	default:
159 		p2buf(myself, "(<unknown>)");
160 		break;
161 	}
162 }
163 
164 void
165 printMappingElement(__nis_mapping_element_t *e,
166 			__nis_mapping_item_type_t native) {
167 	int	i;
168 	char	*myself = "printMappingElement";
169 
170 	switch (e->type) {
171 	case me_item:
172 		printMappingItem(&e->element.item, native);
173 		break;
174 	case me_print:
175 		p2buf(myself, "(");
176 		printMappingFormatArray(e->element.print.fmt);
177 		for (i = 0; i < e->element.print.numSubElements; i++) {
178 			p2buf(myself, ", ");
179 			printMappingSubElement(
180 				&e->element.print.subElement[i], native);
181 		}
182 		if (e->element.print.doElide) {
183 			p2buf(myself, ", \"%c\"", e->element.print.elide);
184 		}
185 		p2buf(myself, ")");
186 		break;
187 	case me_split:
188 		p2buf(myself, "(");
189 		printMappingItem(&e->element.split.item, native);
190 		p2buf(myself, ", \"%c\")", e->element.split.delim);
191 		break;
192 	case me_match:
193 		p2buf(myself, "(");
194 		printMappingFormatArray(e->element.match.fmt);
195 		for (i = 0; i < e->element.match.numItems; i++) {
196 			p2buf(myself, ", ");
197 			printMappingItem(&e->element.match.item[i], native);
198 		}
199 		p2buf(myself, ")");
200 		break;
201 	case me_extract:
202 		p2buf(myself, "(");
203 		printMappingItem(&e->element.extract.item, native);
204 		p2buf(myself, ", ");
205 		printMappingFormatArray(e->element.extract.fmt);
206 		p2buf(myself, ")");
207 		break;
208 	default:
209 		p2buf(myself, "(<unknown>)");
210 		break;
211 	}
212 }
213 
214 void
215 printMappingRLHS(__nis_mapping_rlhs_t *m, __nis_mapping_item_type_t native) {
216 	int	i;
217 	char	*myself = "printMappingRLHS";
218 
219 	if (m->numElements > 1)
220 		p2buf(myself, "(");
221 	for (i = 0; i < m->numElements; i++) {
222 		printMappingElement(&m->element[i], native);
223 	}
224 	if (m->numElements > 1)
225 		p2buf(myself, ")");
226 }
227 
228 void
229 printMappingRule(__nis_mapping_rule_t *r,
230 		__nis_mapping_item_type_t nativeLhs,
231 		__nis_mapping_item_type_t nativeRhs) {
232 	char		*myself = "printMappingRule";
233 
234 	printMappingRLHS(&r->lhs, nativeLhs);
235 	p2buf(myself, "=");
236 	printMappingRLHS(&r->rhs, nativeRhs);
237 }
238 
239 void
240 printObjName(__nis_index_t *index, char *name) {
241 	char		*myself = "printObjName";
242 
243 	printIndex(index);
244 	p2buf(myself, "%s", NIL(name));
245 }
246 
247 void
248 printobjectDN(__nis_object_dn_t *o) {
249 	char		*myself = "printobjectDN";
250 	int		i;
251 
252 	p2buf(myself, "\t");
253 	printSearchTriple(&o->read);
254 	p2buf(myself, ":\n\t");
255 	printSearchTriple(&o->write);
256 	switch (o->delDisp) {
257 	case dd_always:
258 		p2buf(myself, ":\n\t\talways");
259 		break;
260 	case dd_perDbId:
261 		p2buf(myself, ":\n\t\tdbid=%s\n", NIL(o->dbIdName));
262 		for (i = 0; i < o->numDbIds; i++) {
263 			p2buf(myself, "\t\t\t");
264 			printMappingRule(o->dbId[i], mit_ldap, mit_nisplus);
265 		}
266 		break;
267 	case dd_never:
268 		p2buf(myself, ":\n\t\tnever");
269 		break;
270 	default:
271 		p2buf(myself, ":\n\t\t<unknown>");
272 	}
273 }
274 
275 void
276 printTableMapping(__nis_table_mapping_t *t) {
277 	__nis_object_dn_t	*o;
278 	int			i;
279 	char			*myself = "printTableMapping";
280 
281 	p2buf(myself, "\n%s:", NIL(t->dbId));
282 	printObjName(&t->index, t->objName);
283 	p2buf(myself, "\n\t%s \t%s", NIL(t->objName), NIL(t->objPath));
284 	p2buf(myself, "\n\tTTL = (%d - %d) -> %d\n",
285 		t->initTtlLo, t->initTtlHi, t->ttl);
286 
287 	for (o = t->objectDN; o != 0; o = o->next) {
288 		printobjectDN(o);
289 		p2buf(myself, "\n");
290 	}
291 
292 	p2buf(myself, "\tLDAP -> NIS+\n");
293 	p2buf(myself, "\tRules:\n");
294 	for (i = 0; i < t->numRulesFromLDAP; i++) {
295 		p2buf(myself, "\t\t");
296 		printMappingRule(t->ruleFromLDAP[i], mit_nisplus, mit_ldap);
297 		p2buf(myself, "\n");
298 	}
299 
300 	p2buf(myself, "\tNIS+ -> LDAP\n");
301 	p2buf(myself, "\tRules:\n");
302 	for (i = 0; i < t->numRulesToLDAP; i++) {
303 		p2buf(myself, "\t\t");
304 		printMappingRule(t->ruleToLDAP[i], mit_ldap, mit_nisplus);
305 		p2buf(myself, "\n");
306 	}
307 }
308 
309 void
310 printRuleValue(__nis_rule_value_t *rv) {
311 	int		i, j;
312 	__nis_buffer_t	b = {0, 0};
313 	char		*myself = "printRuleValue";
314 
315 	if (rv == 0)
316 		return;
317 
318 	if (rv->colName != 0) {
319 		bp2buf(myself, &b, "Columns:\n");
320 		for (i = 0; i < rv->numColumns; i++) {
321 			bp2buf(myself, &b, "\t%s", NIL(rv->colName[i]));
322 			if (rv->colVal[i].numVals == 1) {
323 				bp2buf(myself, &b, "=");
324 				if (rv->colVal[i].type == vt_string)
325 					sbc2buf(myself,
326 						rv->colVal[i].val[0].value,
327 					rv->colVal[i].val[0].length, &b);
328 				else
329 					bc2buf(myself,
330 						rv->colVal[i].val[0].value,
331 					rv->colVal[i].val[0].length, &b);
332 				bp2buf(myself, &b, "\n");
333 			} else {
334 				bp2buf(myself, &b, "\n");
335 				for (j = 0; j < rv->colVal[i].numVals; j++) {
336 					bp2buf(myself, &b, "\t\t");
337 					if (rv->colVal[i].type == vt_string)
338 						sbc2buf(myself,
339 						rv->colVal[i].val[j].value,
340 						rv->colVal[i].val[j].length,
341 						&b);
342 					else
343 						bc2buf(myself,
344 						rv->colVal[i].val[j].value,
345 						rv->colVal[i].val[j].length,
346 						&b);
347 					bp2buf(myself, &b, "\n");
348 				}
349 			}
350 		}
351 	}
352 
353 	if (rv->attrName != 0) {
354 		bp2buf(myself, &b, "Attributes:\n");
355 		for (i = 0; i < rv->numAttrs; i++) {
356 			bp2buf(myself, &b, "\t%s", NIL(rv->attrName[i]));
357 			if (rv->attrVal[i].numVals == 1) {
358 				bp2buf(myself, &b, "=");
359 				if (rv->attrVal[i].type == vt_string)
360 					sbc2buf(myself,
361 						rv->attrVal[i].val[0].value,
362 						rv->attrVal[i].val[0].length,
363 						&b);
364 				else
365 					bc2buf(myself,
366 						rv->attrVal[i].val[0].value,
367 						rv->attrVal[i].val[0].length,
368 						&b);
369 				bp2buf(myself, &b, "\n");
370 			} else {
371 				bp2buf(myself, &b, "\n");
372 				for (j = 0; j < rv->attrVal[i].numVals; j++) {
373 					bp2buf(myself, &b, "\t\t");
374 					if (rv->attrVal[i].type == vt_string)
375 						sbc2buf(myself,
376 						rv->attrVal[i].val[j].value,
377 						rv->attrVal[i].val[j].length,
378 						&b);
379 					else
380 						bc2buf(myself,
381 						rv->attrVal[i].val[j].value,
382 						rv->attrVal[i].val[j].length,
383 						&b);
384 					bp2buf(myself, &b, "\n");
385 				}
386 			}
387 		}
388 	}
389 
390 	c2buf(myself, b.buf, b.len);
391 	sfree(b.buf);
392 	printbuf();
393 }
394 
395 void
396 printLdapMod(LDAPMod **mods, __nis_buffer_t *b) {
397 	LDAPMod		*m;
398 	char		*s;
399 	char		*myself = "printLdapMod";
400 
401 	if (mods == 0)
402 		return;
403 
404 	if (b == 0)
405 		b = &pb;
406 
407 	while ((m = *mods) != 0) {
408 		if ((m->mod_op & LDAP_MOD_ADD) != 0 ||
409 				(m->mod_op & ~LDAP_MOD_BVALUES) == 0) {
410 			s = "ADD    ";
411 		} else if ((m->mod_op & LDAP_MOD_DELETE) != 0) {
412 			s = "DELETE ";
413 		} else if ((m->mod_op & LDAP_MOD_REPLACE) != 0) {
414 			s = "REPLACE";
415 		} else {
416 			s = "UNKNOWN";
417 		}
418 		bp2buf(myself, b, "%s: %s\n", s, m->mod_type);
419 		if ((m->mod_op & LDAP_MOD_BVALUES) == 0) {
420 			char	**v = m->mod_values;
421 
422 			if (v != 0) {
423 				while (*v != 0) {
424 					bp2buf(myself, b, "\t%s\n", *v);
425 					v++;
426 				}
427 			}
428 		} else {
429 			struct berval	**bv = m->mod_bvalues;
430 
431 			if (bv != 0) {
432 				while (*bv != 0) {
433 					bp2buf(myself, b, "\t<ber> %d bytes\n",
434 						(*bv)->bv_len);
435 					bv++;
436 				}
437 			}
438 		}
439 		mods++;
440 	}
441 }
442 
443 static void
444 printObjRights(char *msg, void *access) {
445 	uchar_t	*a = access;
446 	int	i;
447 
448 	if (a == 0)
449 		return;
450 
451 	for (i = 0; i < 4; i++) {
452 		p2buf(msg, "%s", (a[i] & NIS_READ_ACC) ? "r" : "-");
453 		p2buf(msg, "%s", (a[i] & NIS_MODIFY_ACC) ? "m" : "-");
454 		p2buf(msg, "%s", (a[i] & NIS_CREATE_ACC) ? "c" : "-");
455 		p2buf(msg, "%s", (a[i] & NIS_DESTROY_ACC) ? "d" : "-");
456 	}
457 }
458 
459 void
460 printObjAttr(__nis_obj_attr_t *attr) {
461 	char	*myself = "printObjAttr";
462 
463 	if (attr == 0)
464 		return;
465 
466 	p2buf(myself, "\tzo_owner  = %s\n", NIL(attr->zo_owner));
467 	p2buf(myself, "\tzo_group  = %s\n", NIL(attr->zo_group));
468 	p2buf(myself, "\tzo_domain = %s\n", NIL(attr->zo_domain));
469 	p2buf(myself, "\tzo_access = ");
470 	printObjRights(myself, &attr->zo_access);
471 	p2buf(myself, " (0x%08x)\n", attr->zo_access);
472 	p2buf(myself, "\tzo_ttl    = %d\n", attr->zo_ttl);
473 }
474