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