1 /*	$NetBSD: dds.c,v 1.1.1.3 2010/12/12 15:21:30 adam Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/libraries/libldap/dds.c,v 1.2.2.5 2010/04/13 20:22:56 kurt Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 2005-2010 The OpenLDAP Foundation.
7  * Portions Copyright 2005-2006 SysNet s.n.c.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was developed by Pierangelo Masarati for inclusion
20  * in OpenLDAP Software */
21 
22 #include "portable.h"
23 
24 #include <stdio.h>
25 #include <ac/stdlib.h>
26 #include <ac/string.h>
27 #include <ac/time.h>
28 
29 #include "ldap-int.h"
30 
31 int
32 ldap_parse_refresh( LDAP *ld, LDAPMessage *res, ber_int_t *newttl )
33 {
34 	int		rc;
35 	struct berval	*retdata = NULL;
36 	ber_tag_t	tag;
37 	BerElement	*ber;
38 
39 	assert( ld != NULL );
40 	assert( LDAP_VALID( ld ) );
41 	assert( res != NULL );
42 	assert( newttl != NULL );
43 
44 	*newttl = 0;
45 
46 	rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
47 
48 	if ( rc != LDAP_SUCCESS ) {
49 		return rc;
50 	}
51 
52 	if ( ld->ld_errno != LDAP_SUCCESS ) {
53 		return ld->ld_errno;
54 	}
55 
56 	if ( retdata == NULL ) {
57 		rc = ld->ld_errno = LDAP_DECODING_ERROR;
58 		return rc;
59 	}
60 
61 	ber = ber_init( retdata );
62 	if ( ber == NULL ) {
63 		rc = ld->ld_errno = LDAP_NO_MEMORY;
64 		goto done;
65 	}
66 
67 	/* check the tag */
68 	tag = ber_scanf( ber, "{i}", newttl );
69 	ber_free( ber, 1 );
70 
71 	if ( tag != LDAP_TAG_EXOP_REFRESH_RES_TTL ) {
72 		*newttl = 0;
73 		rc = ld->ld_errno = LDAP_DECODING_ERROR;
74 	}
75 
76 done:;
77 	if ( retdata ) {
78 		ber_bvfree( retdata );
79 	}
80 
81 	return rc;
82 }
83 
84 int
85 ldap_refresh(
86 	LDAP		*ld,
87 	struct berval	*dn,
88 	ber_int_t		ttl,
89 	LDAPControl	**sctrls,
90 	LDAPControl	**cctrls,
91 	int		*msgidp )
92 {
93 	struct berval	bv = { 0, NULL };
94         BerElement	*ber = NULL;
95 	int		rc;
96 
97 	assert( ld != NULL );
98 	assert( LDAP_VALID( ld ) );
99 	assert( dn != NULL );
100 	assert( msgidp != NULL );
101 
102 	*msgidp = -1;
103 
104 	ber = ber_alloc_t( LBER_USE_DER );
105 
106 	if ( ber == NULL ) {
107 		ld->ld_errno = LDAP_NO_MEMORY;
108 		return ld->ld_errno;
109 	}
110 
111 	ber_printf( ber, "{tOtiN}",
112 		LDAP_TAG_EXOP_REFRESH_REQ_DN, dn,
113 		LDAP_TAG_EXOP_REFRESH_REQ_TTL, ttl );
114 
115 	rc = ber_flatten2( ber, &bv, 0 );
116 
117 	if ( rc < 0 ) {
118 		rc = ld->ld_errno = LDAP_ENCODING_ERROR;
119 		goto done;
120 	}
121 
122 	rc = ldap_extended_operation( ld, LDAP_EXOP_REFRESH, &bv,
123 		sctrls, cctrls, msgidp );
124 
125 done:;
126         ber_free( ber, 1 );
127 
128 	return rc;
129 }
130 
131 int
132 ldap_refresh_s(
133 	LDAP		*ld,
134 	struct berval	*dn,
135 	ber_int_t		ttl,
136 	ber_int_t		*newttl,
137 	LDAPControl	**sctrls,
138 	LDAPControl	**cctrls )
139 {
140 	int		rc;
141 	int		msgid;
142 	LDAPMessage	*res;
143 
144 	rc = ldap_refresh( ld, dn, ttl, sctrls, cctrls, &msgid );
145 	if ( rc != LDAP_SUCCESS ) return rc;
146 
147 	rc = ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *)NULL, &res );
148 	if( rc == -1 || !res ) return ld->ld_errno;
149 
150 	rc = ldap_parse_refresh( ld, res, newttl );
151 	if( rc != LDAP_SUCCESS ) {
152 		ldap_msgfree( res );
153 		return rc;
154 	}
155 
156 	return ldap_result2error( ld, res, 1 );
157 }
158 
159