1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Mozilla Communicator client code, released
15 * March 31, 1998.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-1999
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38 /* tmpltest.c - implements a test/config templates. */
39 #include <stdio.h>
40 #include <sys/types.h>
41
42 #ifdef _WINDOWS
43 # include <windows.h>
44 #endif
45
46 #include "ldap-int.h"
47 #include "disptmpl.h"
48 #include "srchpref.h"
49
50 #ifdef MACOS
51 # include <stdlib.h>
52 # include <console.h>
53 #endif /* MACOS */
54
55 #ifdef NEEDPROTOS
56 void dump_tmpl(struct ldap_disptmpl* tmpl);
57 void dump_srchpref(struct ldap_searchobj* sp);
58 #else /* NEEDPROTOS */
59 void dump_tmpl();
60 void dump_srchpref();
61 #endif /* NEEDPROTOS */
62
63 #define NULLSTRINGIFNULL(s) (s == NULL ? "(null)" : s)
64
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66 struct ldap_disptmpl *templates, *dtp;
67 struct ldap_searchobj *so, *sop;
68 int err;
69
70 #ifdef MACOS
71 ccommand(&argv);
72 for (argc = 0; argv[argc] != NULL; ++argc) {
73 ;
74 }
75 cshow(stdout);
76 #endif /* MACOS */
77
78 if ((err = ldap_init_templates("ldaptemplates.conf", &templates)) != 0) {
79 fprintf(stderr, "ldap_init_templates failed (%d)\n", err);
80 exit(1);
81 }
82
83 if ((err = ldap_init_searchprefs("ldapsearchprefs.conf", &so)) != 0) {
84 fprintf(stderr, "ldap_init_searchprefs failed (%d)\n", err);
85 exit(1);
86 }
87
88 if (argc == 1) {
89 printf("*** Display Templates:\n");
90 for (dtp = ldap_first_disptmpl(templates); dtp != NULLDISPTMPL;
91 dtp = ldap_next_disptmpl(templates, dtp)) {
92 dump_tmpl(dtp);
93 printf("\n\n");
94 }
95
96 printf("\n\n*** Search Objects:\n");
97 for (sop = ldap_first_searchobj(so); sop != NULLSEARCHOBJ;
98 sop = ldap_next_searchobj(so, sop)) {
99 dump_srchpref(sop);
100 printf("\n\n");
101 }
102
103 } else {
104 if ((dtp = ldap_oc2template(++argv, templates)) == NULL) {
105 fprintf(stderr, "no matching template found\n");
106 } else {
107 dump_tmpl(dtp);
108 }
109 }
110
111 ldap_free_templates(templates);
112 ldap_free_searchprefs(so);
113
114 exit(0);
115 }
116
117 static char* syn_name[] = {
118 "?", "CIS", "MLS", "DN", "BOOL", "JPEG",
119 "JPEGBTN", "FAX", "FAXBTN", "AUDIOBTN", "TIME", "DATE",
120 "URL", "SEARCHACT", "LINKACT", "ADDDNACT", "VERIFYACT",
121 };
122
123 static char* syn_type[] = {"?", "txt", "img", "?", "bool", "?",
124 "?", "?", "btn", "?", "?", "?",
125 "?", "?", "?", "?", "action", "?"};
126
127 static char* includeattrs[] = {"objectClass", "sn", NULL};
128
129 static char* item_opts[] = {"ro", "sort", "1val", "hide",
130 "required", "hideiffalse", NULL};
131
132 static unsigned long item_opt_vals[] = {
133 LDAP_DITEM_OPT_READONLY, LDAP_DITEM_OPT_SORTVALUES,
134 LDAP_DITEM_OPT_SINGLEVALUED, LDAP_DITEM_OPT_HIDEIFEMPTY,
135 LDAP_DITEM_OPT_VALUEREQUIRED, LDAP_DITEM_OPT_HIDEIFFALSE,
136 };
137
dump_tmpl(struct ldap_disptmpl * tmpl)138 void dump_tmpl(struct ldap_disptmpl* tmpl) {
139 struct ldap_tmplitem *rowp, *colp;
140 int i, rowcnt, colcnt;
141 char** fetchattrs;
142 struct ldap_oclist* ocp;
143 struct ldap_adddeflist* adp;
144
145 printf("** Template \"%s\" (plural \"%s\", icon \"%s\")\n",
146 NULLSTRINGIFNULL(tmpl->dt_name), NULLSTRINGIFNULL(tmpl->dt_pluralname),
147 NULLSTRINGIFNULL(tmpl->dt_iconname));
148
149 printf("object class list:\n");
150 for (ocp = tmpl->dt_oclist; ocp != NULL; ocp = ocp->oc_next) {
151 for (i = 0; ocp->oc_objclasses[i] != NULL; ++i) {
152 printf("%s%s", i == 0 ? " " : " & ",
153 NULLSTRINGIFNULL(ocp->oc_objclasses[i]));
154 }
155 putchar('\n');
156 }
157 putchar('\n');
158
159 printf("template options: ");
160 if (tmpl->dt_options == 0L) {
161 printf("NONE\n");
162 } else {
163 printf("%s %s %s\n",
164 LDAP_IS_DISPTMPL_OPTION_SET(tmpl, LDAP_DTMPL_OPT_ADDABLE) ? "addable"
165 : "",
166 LDAP_IS_DISPTMPL_OPTION_SET(tmpl, LDAP_DTMPL_OPT_ALLOWMODRDN)
167 ? "modrdn"
168 : "",
169 LDAP_IS_DISPTMPL_OPTION_SET(tmpl, LDAP_DTMPL_OPT_ALTVIEW) ? "altview"
170 : "");
171 }
172
173 printf("authenticate as attribute: %s\n",
174 tmpl->dt_authattrname != NULL ? tmpl->dt_authattrname : "<default>");
175
176 printf("default RDN attribute: %s\n",
177 tmpl->dt_defrdnattrname != NULL ? tmpl->dt_defrdnattrname : "NONE");
178
179 printf("default add location: %s\n",
180 tmpl->dt_defaddlocation != NULL ? tmpl->dt_defaddlocation : "NONE");
181
182 printf("\nnew entry value default rules:\n");
183 for (adp = tmpl->dt_adddeflist; adp != NULL; adp = adp->ad_next) {
184 if (adp->ad_source == LDAP_ADSRC_CONSTANTVALUE) {
185 printf(" attribute %s <-- constant value \"%s\"\n",
186 NULLSTRINGIFNULL(adp->ad_attrname),
187 NULLSTRINGIFNULL(adp->ad_value));
188 } else {
189 printf(" attribute %s <-- adder's DN\n",
190 NULLSTRINGIFNULL(adp->ad_attrname));
191 }
192 }
193 putchar('\n');
194
195 printf("\nfetch attributes & values:\n");
196 if ((fetchattrs =
197 ldap_tmplattrs(tmpl, includeattrs, 1, LDAP_SYN_OPT_DEFER)) == NULL) {
198 printf(" <none>\n");
199 } else {
200 for (i = 0; fetchattrs[i] != NULL; ++i) {
201 printf(" %s\n", fetchattrs[i]);
202 free(fetchattrs[i]);
203 }
204 free((char*)fetchattrs);
205 }
206
207 printf("\nfetch attributes only:\n");
208 if ((fetchattrs = ldap_tmplattrs(tmpl, NULL, 0, LDAP_SYN_OPT_DEFER)) ==
209 NULL) {
210 printf(" <none>\n");
211 } else {
212 for (i = 0; fetchattrs[i] != NULL; ++i) {
213 printf(" %s\n", fetchattrs[i]);
214 free(fetchattrs[i]);
215 }
216 free((char*)fetchattrs);
217 }
218
219 printf("\ntemplate items:\n");
220 rowcnt = 0;
221 for (rowp = ldap_first_tmplrow(tmpl); rowp != NULLTMPLITEM;
222 rowp = ldap_next_tmplrow(tmpl, rowp)) {
223 ++rowcnt;
224 colcnt = 0;
225 for (colp = ldap_first_tmplcol(tmpl, rowp); colp != NULLTMPLITEM;
226 colp = ldap_next_tmplcol(tmpl, rowp, colp)) {
227 ++colcnt;
228 printf(
229 " %2d-%d: %s (%s%s", rowcnt, colcnt,
230 syn_name[colp->ti_syntaxid & 0x0000FFFF],
231 syn_type[LDAP_GET_SYN_TYPE(colp->ti_syntaxid) >> 24],
232 ((LDAP_GET_SYN_OPTIONS(colp->ti_syntaxid) & LDAP_SYN_OPT_DEFER) != 0)
233 ? ",defer"
234 : "");
235
236 for (i = 0; item_opts[i] != NULL; ++i) {
237 if (LDAP_IS_TMPLITEM_OPTION_SET(colp, item_opt_vals[i])) {
238 printf(",%s", NULLSTRINGIFNULL(item_opts[i]));
239 }
240 }
241
242 printf("), %s, %s", NULLSTRINGIFNULL(colp->ti_attrname),
243 NULLSTRINGIFNULL(colp->ti_label));
244 if (colp->ti_args != NULL) {
245 printf(",args=");
246 for (i = 0; colp->ti_args[i] != NULL; ++i) {
247 printf("<%s>", NULLSTRINGIFNULL(colp->ti_args[i]));
248 }
249 }
250
251 putchar('\n');
252 }
253 }
254 }
255
dump_srchpref(struct ldap_searchobj * so)256 void dump_srchpref(struct ldap_searchobj* so) {
257 int i;
258 struct ldap_searchattr* sa;
259 struct ldap_searchmatch* sm;
260
261 printf("Object type prompt: %s\n", NULLSTRINGIFNULL(so->so_objtypeprompt));
262 printf("Options: %s\n",
263 LDAP_IS_SEARCHOBJ_OPTION_SET(so, LDAP_SEARCHOBJ_OPT_INTERNAL)
264 ? "internal"
265 : "NONE");
266 printf("Prompt: %s\n", NULLSTRINGIFNULL(so->so_prompt));
267 printf("Scope: ");
268 switch (so->so_defaultscope) {
269 case LDAP_SCOPE_BASE:
270 printf("LDAP_SCOPE_BASE");
271 break;
272 case LDAP_SCOPE_ONELEVEL:
273 printf("LDAP_SCOPE_ONELEVEL");
274 break;
275 case LDAP_SCOPE_SUBTREE:
276 printf("LDAP_SCOPE_SUBTREE");
277 break;
278 default:
279 printf("*** unknown!");
280 }
281 puts("\n");
282 printf("Filter prefix: %s\n", NULLSTRINGIFNULL(so->so_filterprefix));
283 printf("Filter tag: %s\n", NULLSTRINGIFNULL(so->so_filtertag));
284 printf("Default select attr: %s\n",
285 NULLSTRINGIFNULL(so->so_defaultselectattr));
286 printf("Default select text: %s\n",
287 NULLSTRINGIFNULL(so->so_defaultselecttext));
288 printf("Searchable attributes ---- \n");
289 for (sa = so->so_salist; sa != NULL; sa = sa->sa_next) {
290 printf(" Label: %s\n", NULLSTRINGIFNULL(sa->sa_attrlabel));
291 printf(" Attribute: %s\n", NULLSTRINGIFNULL(sa->sa_attr));
292 printf(" Select attr: %s\n", NULLSTRINGIFNULL(sa->sa_selectattr));
293 printf(" Select text: %s\n", NULLSTRINGIFNULL(sa->sa_selecttext));
294 printf(" Match types ---- \n");
295 for (i = 0, sm = so->so_smlist; sm != NULL; i++, sm = sm->sm_next) {
296 if ((sa->sa_matchtypebitmap >> i) & 1) {
297 printf(" %s (%s)\n", NULLSTRINGIFNULL(sm->sm_matchprompt),
298 NULLSTRINGIFNULL(sm->sm_filter));
299 }
300 }
301 }
302 }
303