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