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