1 /*	$NetBSD: apitest.c,v 1.1.1.3 2010/12/12 15:21:29 adam Exp $	*/
2 
3 /* apitest.c -- OpenLDAP API Test Program */
4 /* OpenLDAP: pkg/ldap/libraries/libldap/apitest.c,v 1.25.2.5 2010/04/13 20:22:55 kurt Exp */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2010 The OpenLDAP Foundation.
8  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* ACKNOWLEDGEMENTS:
20  * This program was orignally developed by Kurt D. Zeilenga for inclusion in
21  * OpenLDAP Software.
22  */
23 #include "portable.h"
24 
25 #include <ac/stdlib.h>
26 
27 #include <stdio.h>
28 
29 #include <ldap.h>
30 
31 int
32 main(int argc, char **argv)
33 {
34 	LDAPAPIInfo api;
35 	int ival;
36 	char *sval;
37 
38 	printf("Compile time API Information\n");
39 
40 #ifdef LDAP_API_INFO_VERSION
41 	api.ldapai_info_version = LDAP_API_INFO_VERSION;
42 	printf("  API Info version:  %d\n", (int) api.ldapai_info_version);
43 #else
44 	api.ldapai_info_version = 1;
45 	printf("  API Info version:  unknown\n");
46 #endif
47 
48 #ifdef LDAP_FEATURE_INFO_VERSION
49 	printf("  Feature Info version:  %d\n", (int) LDAP_FEATURE_INFO_VERSION);
50 #else
51 	printf("  Feature Info version:  unknown\n");
52 	api.ldapai_info_version = 1;
53 #endif
54 
55 #ifdef LDAP_API_VERSION
56 	printf("  API version:       %d\n", (int) LDAP_API_VERSION);
57 #else
58 	printf("  API version:       unknown\n");
59 #endif
60 
61 #ifdef LDAP_VERSION
62 	printf("  Protocol Version:  %d\n", (int) LDAP_VERSION);
63 #else
64 	printf("  Protocol Version:  unknown\n");
65 #endif
66 #ifdef LDAP_VERSION_MIN
67 	printf("  Protocol Min:      %d\n", (int) LDAP_VERSION_MIN);
68 #else
69 	printf("  Protocol Min:      unknown\n");
70 #endif
71 #ifdef LDAP_VERSION_MAX
72 	printf("  Protocol Max:      %d\n", (int) LDAP_VERSION_MAX);
73 #else
74 	printf("  Protocol Max:      unknown\n");
75 #endif
76 #ifdef LDAP_VENDOR_NAME
77 	printf("  Vendor Name:       %s\n", LDAP_VENDOR_NAME);
78 #else
79 	printf("  Vendor Name:       unknown\n");
80 #endif
81 #ifdef LDAP_VENDOR_VERSION
82 	printf("  Vendor Version:    %d\n", (int) LDAP_VENDOR_VERSION);
83 #else
84 	printf("  Vendor Version:    unknown\n");
85 #endif
86 
87 	if(ldap_get_option(NULL, LDAP_OPT_API_INFO, &api) != LDAP_SUCCESS) {
88 		fprintf(stderr, "%s: ldap_get_option(API_INFO) failed\n", argv[0]);
89 		return EXIT_FAILURE;
90 	}
91 
92 	printf("\nExecution time API Information\n");
93 	printf("  API Info version:  %d\n", api.ldapai_info_version);
94 
95 	if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
96 		printf(" API INFO version mismatch: got %d, expected %d\n",
97 			api.ldapai_info_version, LDAP_API_INFO_VERSION);
98 		return EXIT_FAILURE;
99 	}
100 
101 	printf("  API Version:       %d\n", api.ldapai_api_version);
102 	printf("  Protocol Max:      %d\n", api.ldapai_protocol_version);
103 
104 	if(api.ldapai_extensions == NULL) {
105 		printf("  Extensions:        none\n");
106 
107 	} else {
108 		int i;
109 		for(i=0; api.ldapai_extensions[i] != NULL; i++) /* empty */;
110 		printf("  Extensions:        %d\n", i);
111 		for(i=0; api.ldapai_extensions[i] != NULL; i++) {
112 #ifdef LDAP_OPT_API_FEATURE_INFO
113 			LDAPAPIFeatureInfo fi;
114 			fi.ldapaif_info_version = LDAP_FEATURE_INFO_VERSION;
115 			fi.ldapaif_name = api.ldapai_extensions[i];
116 			fi.ldapaif_version = 0;
117 
118 			if( ldap_get_option(NULL, LDAP_OPT_API_FEATURE_INFO, &fi) == LDAP_SUCCESS ) {
119 				if(fi.ldapaif_info_version != LDAP_FEATURE_INFO_VERSION) {
120 					printf("                     %s feature info mismatch: got %d, expected %d\n",
121 						api.ldapai_extensions[i],
122 						LDAP_FEATURE_INFO_VERSION,
123 						fi.ldapaif_info_version);
124 
125 				} else {
126 					printf("                     %s: version %d\n",
127 						fi.ldapaif_name,
128 						fi.ldapaif_version);
129 				}
130 
131 			} else {
132 				printf("                     %s (NO FEATURE INFO)\n",
133 					api.ldapai_extensions[i]);
134 			}
135 
136 #else
137 			printf("                     %s\n",
138 				api.ldapai_extensions[i]);
139 #endif
140 
141 			ldap_memfree(api.ldapai_extensions[i]);
142 		}
143 		ldap_memfree(api.ldapai_extensions);
144 	}
145 
146 	printf("  Vendor Name:       %s\n", api.ldapai_vendor_name);
147 	ldap_memfree(api.ldapai_vendor_name);
148 
149 	printf("  Vendor Version:    %d\n", api.ldapai_vendor_version);
150 
151 	printf("\nExecution time Default Options\n");
152 
153 	if(ldap_get_option(NULL, LDAP_OPT_DEREF, &ival) != LDAP_SUCCESS) {
154 		fprintf(stderr, "%s: ldap_get_option(api) failed\n", argv[0]);
155 		return EXIT_FAILURE;
156 	}
157 	printf("  DEREF:             %d\n", ival);
158 
159 	if(ldap_get_option(NULL, LDAP_OPT_SIZELIMIT, &ival) != LDAP_SUCCESS) {
160 		fprintf(stderr, "%s: ldap_get_option(sizelimit) failed\n", argv[0]);
161 		return EXIT_FAILURE;
162 	}
163 	printf("  SIZELIMIT:         %d\n", ival);
164 
165 	if(ldap_get_option(NULL, LDAP_OPT_TIMELIMIT, &ival) != LDAP_SUCCESS) {
166 		fprintf(stderr, "%s: ldap_get_option(timelimit) failed\n", argv[0]);
167 		return EXIT_FAILURE;
168 	}
169 	printf("  TIMELIMIT:         %d\n", ival);
170 
171 	if(ldap_get_option(NULL, LDAP_OPT_REFERRALS, &ival) != LDAP_SUCCESS) {
172 		fprintf(stderr, "%s: ldap_get_option(referrals) failed\n", argv[0]);
173 		return EXIT_FAILURE;
174 	}
175 	printf("  REFERRALS:         %s\n", ival ? "on" : "off");
176 
177 	if(ldap_get_option(NULL, LDAP_OPT_RESTART, &ival) != LDAP_SUCCESS) {
178 		fprintf(stderr, "%s: ldap_get_option(restart) failed\n", argv[0]);
179 		return EXIT_FAILURE;
180 	}
181 	printf("  RESTART:           %s\n", ival ? "on" : "off");
182 
183 	if(ldap_get_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ival) != LDAP_SUCCESS) {
184 		fprintf(stderr, "%s: ldap_get_option(protocol version) failed\n", argv[0]);
185 		return EXIT_FAILURE;
186 	}
187 	printf("  PROTOCOL VERSION:  %d\n", ival);
188 
189 	if(ldap_get_option(NULL, LDAP_OPT_HOST_NAME, &sval) != LDAP_SUCCESS) {
190 		fprintf(stderr, "%s: ldap_get_option(host name) failed\n", argv[0]);
191 		return EXIT_FAILURE;
192 	}
193 	if( sval != NULL ) {
194 		printf("  HOST NAME:         %s\n", sval);
195 		ldap_memfree(sval);
196 	} else {
197 		puts("  HOST NAME:         <not set>");
198 	}
199 
200 #if 0
201 	/* API tests */
202 	{	/* bindless unbind */
203 		LDAP *ld;
204 		int rc;
205 
206 		ld = ldap_init( "localhost", 389 );
207 		if( ld == NULL ) {
208 			perror("ldap_init");
209 			return EXIT_FAILURE;
210 		}
211 
212 		rc = ldap_unbind( ld );
213 		if( rc != LDAP_SUCCESS ) {
214 			perror("ldap_unbind");
215 			return EXIT_FAILURE;
216 		}
217 	}
218 	{	/* bindless unbind */
219 		LDAP *ld;
220 		int rc;
221 
222 		ld = ldap_init( "localhost", 389 );
223 		if( ld == NULL ) {
224 			perror("ldap_init");
225 			return EXIT_FAILURE;
226 		}
227 
228 		rc = ldap_abandon_ext( ld, 0, NULL, NULL );
229 		if( rc != LDAP_SERVER_DOWN ) {
230 			ldap_perror( ld, "ldap_abandon");
231 			return EXIT_FAILURE;
232 		}
233 
234 		rc = ldap_unbind( ld );
235 		if( rc != LDAP_SUCCESS ) {
236 			perror("ldap_unbind");
237 			return EXIT_FAILURE;
238 		}
239 	}
240 #endif
241 
242 	return EXIT_SUCCESS;
243 }
244