1 /*        $NetBSD: dds.c,v 1.3 2021/08/14 16:14:55 christos Exp $     */
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 2005-2021 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 <sys/cdefs.h>
23 __RCSID("$NetBSD: dds.c,v 1.3 2021/08/14 16:14:55 christos Exp $");
24 
25 #include "portable.h"
26 
27 #include <stdio.h>
28 #include <ac/stdlib.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31 
32 #include "ldap-int.h"
33 
34 int
ldap_parse_refresh(LDAP * ld,LDAPMessage * res,ber_int_t * newttl)35 ldap_parse_refresh( LDAP *ld, LDAPMessage *res, ber_int_t *newttl )
36 {
37           int                 rc;
38           struct berval       *retdata = NULL;
39           ber_tag_t tag;
40           BerElement          *ber;
41 
42           assert( ld != NULL );
43           assert( LDAP_VALID( ld ) );
44           assert( res != NULL );
45           assert( newttl != NULL );
46 
47           *newttl = 0;
48 
49           rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
50 
51           if ( rc != LDAP_SUCCESS ) {
52                     return rc;
53           }
54 
55           if ( ld->ld_errno != LDAP_SUCCESS ) {
56                     return ld->ld_errno;
57           }
58 
59           if ( retdata == NULL ) {
60                     rc = ld->ld_errno = LDAP_DECODING_ERROR;
61                     return rc;
62           }
63 
64           ber = ber_init( retdata );
65           if ( ber == NULL ) {
66                     rc = ld->ld_errno = LDAP_NO_MEMORY;
67                     goto done;
68           }
69 
70           /* check the tag */
71           tag = ber_scanf( ber, "{i}", newttl );
72           ber_free( ber, 1 );
73 
74           if ( tag != LDAP_TAG_EXOP_REFRESH_RES_TTL ) {
75                     *newttl = 0;
76                     rc = ld->ld_errno = LDAP_DECODING_ERROR;
77           }
78 
79 done:;
80           if ( retdata ) {
81                     ber_bvfree( retdata );
82           }
83 
84           return rc;
85 }
86 
87 int
ldap_refresh(LDAP * ld,struct berval * dn,ber_int_t ttl,LDAPControl ** sctrls,LDAPControl ** cctrls,int * msgidp)88 ldap_refresh(
89           LDAP                *ld,
90           struct berval       *dn,
91           ber_int_t           ttl,
92           LDAPControl         **sctrls,
93           LDAPControl         **cctrls,
94           int                 *msgidp )
95 {
96           struct berval       bv = { 0, NULL };
97         BerElement  *ber = NULL;
98           int                 rc;
99 
100           assert( ld != NULL );
101           assert( LDAP_VALID( ld ) );
102           assert( dn != NULL );
103           assert( msgidp != NULL );
104 
105           *msgidp = -1;
106 
107           ber = ber_alloc_t( LBER_USE_DER );
108 
109           if ( ber == NULL ) {
110                     ld->ld_errno = LDAP_NO_MEMORY;
111                     return ld->ld_errno;
112           }
113 
114           ber_printf( ber, "{tOtiN}",
115                     LDAP_TAG_EXOP_REFRESH_REQ_DN, dn,
116                     LDAP_TAG_EXOP_REFRESH_REQ_TTL, ttl );
117 
118           rc = ber_flatten2( ber, &bv, 0 );
119 
120           if ( rc < 0 ) {
121                     rc = ld->ld_errno = LDAP_ENCODING_ERROR;
122                     goto done;
123           }
124 
125           rc = ldap_extended_operation( ld, LDAP_EXOP_REFRESH, &bv,
126                     sctrls, cctrls, msgidp );
127 
128 done:;
129         ber_free( ber, 1 );
130 
131           return rc;
132 }
133 
134 int
ldap_refresh_s(LDAP * ld,struct berval * dn,ber_int_t ttl,ber_int_t * newttl,LDAPControl ** sctrls,LDAPControl ** cctrls)135 ldap_refresh_s(
136           LDAP                *ld,
137           struct berval       *dn,
138           ber_int_t           ttl,
139           ber_int_t           *newttl,
140           LDAPControl         **sctrls,
141           LDAPControl         **cctrls )
142 {
143           int                 rc;
144           int                 msgid;
145           LDAPMessage         *res;
146 
147           rc = ldap_refresh( ld, dn, ttl, sctrls, cctrls, &msgid );
148           if ( rc != LDAP_SUCCESS ) return rc;
149 
150           rc = ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *)NULL, &res );
151           if( rc == -1 || !res ) return ld->ld_errno;
152 
153           rc = ldap_parse_refresh( ld, res, newttl );
154           if( rc != LDAP_SUCCESS ) {
155                     ldap_msgfree( res );
156                     return rc;
157           }
158 
159           return ldap_result2error( ld, res, 1 );
160 }
161 
162