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 #include "ldap-int.h"
38 
39 /* ldap_create_sort_control:
40 
41    Parameters are
42 
43    ld              LDAP pointer to the desired connection
44 
45    sortKeyList     an array of sortkeys
46 
47    ctl_iscritical  Indicates whether the control is critical of not. If
48                    this field is non-zero, the operation will only be car-
49                    ried out if the control is recognized by the server
50                    and/or client
51 
52    ctrlp           the address of a place to put the constructed control
53 */
54 
55 int
56 LDAP_CALL
ldap_create_sort_control(LDAP * ld,LDAPsortkey ** sortKeyList,const char ctl_iscritical,LDAPControl ** ctrlp)57 ldap_create_sort_control (
58      LDAP *ld,
59      LDAPsortkey **sortKeyList,
60      const char ctl_iscritical,
61      LDAPControl **ctrlp
62 )
63 {
64 	BerElement		*ber;
65 	int				i, rc;
66 
67 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
68 		return( LDAP_PARAM_ERROR );
69 	}
70 
71 	if ( sortKeyList == NULL || ctrlp == NULL ) {
72 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
73 		return ( LDAP_PARAM_ERROR );
74 	}
75 
76 	/* create a ber package to hold the controlValue */
77 	if ( ( nsldapi_alloc_ber_with_options( ld, &ber ) ) != LDAP_SUCCESS ) {
78 		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
79 		return( LDAP_NO_MEMORY );
80 	}
81 
82 	/* encode the start of the sequence of sequences into the ber */
83 	if ( ber_printf( ber, "{" ) == -1 ) {
84 		goto encoding_error_exit;
85 	}
86 
87 	/* the sort control value will be encoded as a sequence of sequences
88 	   which are each encoded as one of the following: {s} or {sts} or {stb} or {ststb}
89 	   since the orderingRule and reverseOrder flag are both optional */
90 	for ( i = 0; sortKeyList[i] != NULL; i++ ) {
91 
92 		/* encode the attributeType into the ber */
93 		if ( ber_printf( ber, "{s", (sortKeyList[i])->sk_attrtype  )
94 		    == -1 ) {
95 			goto encoding_error_exit;
96 		}
97 
98 		/* encode the optional orderingRule into the ber */
99 		if ( (sortKeyList[i])->sk_matchruleoid != NULL ) {
100 			if ( ber_printf( ber, "ts", LDAP_TAG_SK_MATCHRULE,
101 			    (sortKeyList[i])->sk_matchruleoid )
102 			    == -1 ) {
103 				goto encoding_error_exit;
104 			}
105 		}
106 
107 		/* Encode the optional reverseOrder flag into the ber. */
108 		/* If the flag is false, it should be absent. */
109 		if ( (sortKeyList[i])->sk_reverseorder ) {
110 			if ( ber_printf( ber, "tb}", LDAP_TAG_SK_REVERSE,
111 			    (sortKeyList[i])->sk_reverseorder ) == -1 ) {
112 				goto encoding_error_exit;
113 			}
114 		} else {
115 			if ( ber_printf( ber, "}" ) == -1 ) {
116 				goto encoding_error_exit;
117 			}
118 		}
119 	}
120 
121 	/* encode the end of the sequence of sequences into the ber */
122 	if ( ber_printf( ber, "}" ) == -1 ) {
123 		goto encoding_error_exit;
124 	}
125 
126 	rc = nsldapi_build_control( LDAP_CONTROL_SORTREQUEST, ber, 1,
127 	    ctl_iscritical, ctrlp );
128 
129 	LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
130 	return( rc );
131 
132 encoding_error_exit:
133 	LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
134 	ber_free( ber, 1 );
135 	return( LDAP_ENCODING_ERROR );
136 }
137 
138 /* ldap_parse_sort_control:
139 
140    Parameters are
141 
142    ld              LDAP pointer to the desired connection
143 
144    ctrlp           An array of controls obtained from calling
145                    ldap_parse_result on the set of results returned by
146                    the server
147 
148    result          the address of a place to put the result code
149 
150    attribute       the address of a place to put the name of the
151                    attribute which cause the operation to fail, optionally
152                    returned by the server */
153 
154 int
155 LDAP_CALL
ldap_parse_sort_control(LDAP * ld,LDAPControl ** ctrlp,ber_int_t * result,char ** attribute)156 ldap_parse_sort_control (
157      LDAP *ld,
158      LDAPControl **ctrlp,
159      ber_int_t *result,
160      char **attribute
161 )
162 {
163 	BerElement *ber;
164 	int			i, foundSortControl;
165 	LDAPControl *sortCtrlp;
166 	ber_len_t	len;
167 	ber_tag_t	tag;
168 	char		*attr;
169 
170 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || result == NULL ||
171 		attribute == NULL ) {
172 	    return( LDAP_PARAM_ERROR );
173 	}
174 
175 
176 	/* find the sortControl in the list of controls if it exists */
177 	if ( ctrlp == NULL ) {
178 		LDAP_SET_LDERRNO( ld, LDAP_CONTROL_NOT_FOUND, NULL, NULL );
179 		return ( LDAP_CONTROL_NOT_FOUND );
180 	}
181 	foundSortControl = 0;
182 	for ( i = 0; (( ctrlp[i] != NULL ) && ( !foundSortControl )); i++ ) {
183 		foundSortControl = !strcmp( ctrlp[i]->ldctl_oid, LDAP_CONTROL_SORTRESPONSE );
184 	}
185 	if ( !foundSortControl ) {
186 		LDAP_SET_LDERRNO( ld, LDAP_CONTROL_NOT_FOUND, NULL, NULL );
187 		return ( LDAP_CONTROL_NOT_FOUND );
188 	} else {
189 		/* let local var point to the sortControl */
190 		sortCtrlp = ctrlp[i-1];
191 	}
192 
193 	/*  allocate a Ber element with the contents of the sort_control's struct berval */
194 	if ( ( ber = ber_init( &sortCtrlp->ldctl_value ) ) == NULL ) {
195 		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
196 		return( LDAP_NO_MEMORY );
197 	}
198 
199 	/* decode the result from the Berelement */
200 	if ( ber_scanf( ber, "{i", result ) == LBER_ERROR ) {
201 		LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
202 		ber_free( ber, 1 );
203 		return( LDAP_DECODING_ERROR );
204 	}
205 
206 	/* if the server returned one, decode the attribute from the Ber element */
207 	if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SR_ATTRTYPE ) {
208 		if ( ber_scanf( ber, "ta", &tag, &attr ) == LBER_ERROR ) {
209 			LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
210 			ber_free( ber, 1 );
211 			return( LDAP_DECODING_ERROR );
212 		}
213 		*attribute = attr;
214 	} else {
215 		*attribute = NULL;
216 	}
217 
218 	if ( ber_scanf( ber, "}" ) == LBER_ERROR ) {
219 		LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
220 		ber_free( ber, 1 );
221 		return( LDAP_DECODING_ERROR );
222 	}
223 
224 	/* the ber encoding is no longer needed */
225 	ber_free(ber,1);
226 
227 	return( LDAP_SUCCESS );
228 }
229 
230 /* Routines for the manipulation of string-representations of sort control keylists */
231 
count_tokens(const char * s)232 static int count_tokens(const char *s)
233 {
234 	int count = 0;
235 	const char *p = s;
236 	int whitespace = 1;
237 	/* Loop along the string counting the number of times we see the
238 	 * beginning of non-whitespace. This tells us
239 	 * the number of tokens in the string
240 	 */
241 	while (*p != '\0') {
242 		if (whitespace) {
243 			if (!isspace(*p)) {
244 				whitespace = 0;
245 				count++;
246 			}
247 		} else {
248 			if (isspace(*p)) {
249 				whitespace = 1;
250 			}
251 		}
252 		p++;
253 	}
254 	return count;
255 }
256 
257 
read_next_token(const char ** s,LDAPsortkey ** key)258 static int read_next_token(const char **s,LDAPsortkey **key)
259 {
260 	char c = 0;
261 	const char *pos = *s;
262 	int retval = 0;
263 	LDAPsortkey *new_key = NULL;
264 
265 	const char *matchrule_source = NULL;
266 	int matchrule_size = 0;
267 	const char *attrdesc_source = NULL;
268 	int attrdesc_size = 0;
269 	int reverse = 0;
270 
271 	int state = 0;
272 
273 	while ( ((c = *pos++) != '\0') && (state != 4) ) {
274 		switch (state) {
275 		case 0:
276 		/* case where we've not seen the beginning of the attr yet */
277 			/* If we still see whitespace, nothing to do */
278 			if (!isspace(c)) {
279 				/* Otherwise, something to look at */
280 				/* Is it a minus sign ? */
281 				if ('-' == c) {
282 					reverse = 1;
283 				} else {
284 					attrdesc_source = pos - 1;
285 					state = 1;
286 				}
287 			}
288 			break;
289 		case 1:
290 		/* case where we've seen the beginning of the attr, but not the end */
291 			/* Is this char either whitespace or a ';' ? */
292 			if ( isspace(c) || (':' == c)) {
293 				attrdesc_size = (pos - attrdesc_source) - 1;
294 				if (':' == c) {
295 					state = 2;
296 				} else {
297 					state = 4;
298 				}
299 			}
300 			break;
301 		case 2:
302 		/* case where we've seen the end of the attr and want the beginning of match rule */
303 			if (!isspace(c)) {
304 				matchrule_source = pos - 1;
305 				state = 3;
306 			} else {
307 				state = 4;
308 			}
309 			break;
310 		case 3:
311 		/* case where we've seen the beginning of match rule and want to find the end */
312 			if (isspace(c)) {
313 				matchrule_size = (pos - matchrule_source) - 1;
314 				state = 4;
315 			}
316 			break;
317 		default:
318 			break;
319 		}
320 	}
321 
322 	if (3 == state) {
323 		/* means we fell off the end of the string looking for the end of the marching rule */
324 		matchrule_size = (pos - matchrule_source) - 1;
325 	}
326 
327 	if (1 == state) {
328 		/* means we fell of the end of the string looking for the end of the attribute */
329 		attrdesc_size = (pos - attrdesc_source) - 1;
330 	}
331 
332 	if (NULL == attrdesc_source)  {
333 		/* Didn't find anything */
334 		return -1;
335 	}
336 
337 	new_key = (LDAPsortkey*)NSLDAPI_MALLOC(sizeof(LDAPsortkey));
338 	if (0 == new_key) {
339 		return LDAP_NO_MEMORY;
340 	}
341 
342 	/* Allocate the strings */
343 	new_key->sk_attrtype = (char *)NSLDAPI_MALLOC(attrdesc_size + 1);
344 	if (NULL != matchrule_source) {
345 		new_key->sk_matchruleoid = (char *)NSLDAPI_MALLOC(
346 		    matchrule_size + 1);
347 	} else {
348 		new_key->sk_matchruleoid = NULL;
349 	}
350 	/* Copy over the strings */
351 	memcpy(new_key->sk_attrtype,attrdesc_source,attrdesc_size);
352 	*(new_key->sk_attrtype + attrdesc_size) = '\0';
353 	if (NULL != matchrule_source) {
354 		memcpy(new_key->sk_matchruleoid,matchrule_source,matchrule_size);
355 		*(new_key->sk_matchruleoid + matchrule_size) = '\0';
356 	}
357 
358 	new_key->sk_reverseorder = reverse;
359 
360 	*s = pos - 1;
361 	*key = new_key;
362 	return retval;
363 }
364 
365 int
366 LDAP_CALL
ldap_create_sort_keylist(LDAPsortkey *** sortKeyList,const char * string_rep)367 ldap_create_sort_keylist (
368 	LDAPsortkey ***sortKeyList,
369 	const char *string_rep
370 )
371 {
372 	int count = 0;
373 	LDAPsortkey **pointer_array = NULL;
374 	const char *current_position = NULL;
375 	int retval = 0;
376 	int i = 0;
377 
378 	/* Figure out how many there are */
379 	if (NULL == string_rep) {
380 		return LDAP_PARAM_ERROR;
381 	}
382 	if (NULL == sortKeyList) {
383 		return LDAP_PARAM_ERROR;
384 	}
385 	count = count_tokens(string_rep);
386 	if (0 == count) {
387 		*sortKeyList = NULL;
388 		return LDAP_PARAM_ERROR;
389 	}
390 	/* Allocate enough memory for the pointers */
391 	pointer_array = (LDAPsortkey**)NSLDAPI_MALLOC(sizeof(LDAPsortkey*)
392 	    * (count + 1) );
393 	if (NULL == pointer_array) {
394 		return LDAP_NO_MEMORY;
395 	}
396 	/* Now walk along the string, allocating and filling in the LDAPsearchkey structure */
397 	current_position = string_rep;
398 
399 	for (i = 0; i < count; i++) {
400 		if (0 != (retval = read_next_token(&current_position,&(pointer_array[i])))) {
401 			pointer_array[count] = NULL;
402 			ldap_free_sort_keylist(pointer_array);
403 			*sortKeyList = NULL;
404 			return retval;
405 		}
406 	}
407 	pointer_array[count] = NULL;
408 	*sortKeyList = pointer_array;
409 	return LDAP_SUCCESS;
410 }
411 
412 void
413 LDAP_CALL
ldap_free_sort_keylist(LDAPsortkey ** sortKeyList)414 ldap_free_sort_keylist (
415 	LDAPsortkey **sortKeyList
416 )
417 {
418 	LDAPsortkey *this_one = NULL;
419 	int i = 0;
420 
421 	if ( NULL == sortKeyList ) {
422 		return;
423 	}
424 
425 	/* Walk down the list freeing the LDAPsortkey structures */
426 	for (this_one = sortKeyList[0]; this_one ; this_one = sortKeyList[++i]) {
427 		/* Free the strings, if present */
428 		if (NULL != this_one->sk_attrtype) {
429 			NSLDAPI_FREE(this_one->sk_attrtype);
430 		}
431 		if (NULL != this_one->sk_matchruleoid) {
432 			NSLDAPI_FREE(this_one->sk_matchruleoid);
433 		}
434 		NSLDAPI_FREE(this_one);
435 	}
436 	/* Free the pointer list */
437 	NSLDAPI_FREE(sortKeyList);
438 }
439