xref: /titanic_51/usr/src/lib/libldap5/sources/ldap/common/spagectrl.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * Copyright (c) 2001 by Sun Microsystems, Inc.
3  * All rights reserved.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 #include <stdio.h>
9 #include <string.h>
10 
11 #ifdef MACOS
12 #include "macos.h"
13 #endif /* MACOS */
14 
15 #if !defined( MACOS ) && !defined( DOS )
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #endif
19 
20 #include "lber.h"
21 #include "ldap.h"
22 #include "ldap-int.h"
23 
24 int ldap_create_page_control(LDAP *ld, unsigned int pagesize, struct berval *cookie, char isCritical, LDAPControl **output)
25 {
26 	BerElement *ber;
27 	int rc;
28 
29 	if (NULL == ld || NULL == output)
30 		return (LDAP_PARAM_ERROR);
31 
32 	if ((ber = ber_alloc_t(LBER_USE_DER)) == NULLBER){
33 		return (LDAP_NO_MEMORY);
34 	}
35 
36 	if (ber_printf(ber, "{io}", pagesize,
37 			(cookie && cookie->bv_val) ? cookie->bv_val : "",
38 			(cookie && cookie->bv_val) ? cookie->bv_len : 0)
39 				 == LBER_ERROR) {
40 		ber_free(ber, 1);
41 		return (LDAP_ENCODING_ERROR);
42 	}
43 
44 	rc = nsldapi_build_control(LDAP_CONTROL_SIMPLE_PAGE, ber, 1, isCritical,
45 		output);
46 
47 	ld->ld_errno = rc;
48 	return (rc);
49 }
50 
51 int ldap_parse_page_control(LDAP *ld, LDAPControl **controls, unsigned int *totalcount, struct berval **cookie)
52 {
53 	int i, rc;
54 	BerElement *theBer;
55 	LDAPControl *listCtrlp;
56 
57 	for (i = 0; controls[i] != NULL; i++){
58 		if (strcmp(controls[i]->ldctl_oid, "1.2.840.113556.1.4.319") == 0) {
59 			listCtrlp = controls[i];
60 			if ((theBer = ber_init(&listCtrlp->ldctl_value)) == NULLBER){
61 				return (LDAP_NO_MEMORY);
62 			}
63 			if ((rc = ber_scanf(theBer, "{iO}", totalcount, cookie)) == LBER_ERROR){
64 				ber_free(theBer, 1);
65 				return (LDAP_DECODING_ERROR);
66 			}
67 			ber_free(theBer, 1);
68 			return (LDAP_SUCCESS);
69 		}
70 	}
71 	return (LDAP_CONTROL_NOT_FOUND);
72 }
73 
74