Back to index

openldap  2.4.31
dds.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 2005-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 2005-2006 SysNet s.n.c.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was developed by Pierangelo Masarati for inclusion
00018  * in OpenLDAP Software */
00019 
00020 #include "portable.h"
00021 
00022 #include <stdio.h>
00023 #include <ac/stdlib.h>
00024 #include <ac/string.h>
00025 #include <ac/time.h>
00026 
00027 #include "ldap-int.h"
00028 
00029 int
00030 ldap_parse_refresh( LDAP *ld, LDAPMessage *res, ber_int_t *newttl )
00031 {
00032        int           rc;
00033        struct berval *retdata = NULL;
00034        ber_tag_t     tag;
00035        BerElement    *ber;
00036 
00037        assert( ld != NULL );
00038        assert( LDAP_VALID( ld ) );
00039        assert( res != NULL );
00040        assert( newttl != NULL );
00041 
00042        *newttl = 0;
00043 
00044        rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
00045 
00046        if ( rc != LDAP_SUCCESS ) {
00047               return rc;
00048        }
00049 
00050        if ( ld->ld_errno != LDAP_SUCCESS ) {
00051               return ld->ld_errno;
00052        }
00053 
00054        if ( retdata == NULL ) {
00055               rc = ld->ld_errno = LDAP_DECODING_ERROR;
00056               return rc;
00057        }
00058 
00059        ber = ber_init( retdata );
00060        if ( ber == NULL ) {
00061               rc = ld->ld_errno = LDAP_NO_MEMORY;
00062               goto done;
00063        }
00064 
00065        /* check the tag */
00066        tag = ber_scanf( ber, "{i}", newttl );
00067        ber_free( ber, 1 );
00068 
00069        if ( tag != LDAP_TAG_EXOP_REFRESH_RES_TTL ) {
00070               *newttl = 0;
00071               rc = ld->ld_errno = LDAP_DECODING_ERROR;
00072        }
00073 
00074 done:;
00075        if ( retdata ) {
00076               ber_bvfree( retdata );
00077        }
00078 
00079        return rc;
00080 }
00081 
00082 int
00083 ldap_refresh(
00084        LDAP          *ld,
00085        struct berval *dn,
00086        ber_int_t            ttl,
00087        LDAPControl   **sctrls,
00088        LDAPControl   **cctrls,
00089        int           *msgidp )
00090 {
00091        struct berval bv = { 0, NULL };
00092         BerElement   *ber = NULL;
00093        int           rc;
00094 
00095        assert( ld != NULL );
00096        assert( LDAP_VALID( ld ) );
00097        assert( dn != NULL );
00098        assert( msgidp != NULL );
00099 
00100        *msgidp = -1;
00101 
00102        ber = ber_alloc_t( LBER_USE_DER );
00103 
00104        if ( ber == NULL ) {
00105               ld->ld_errno = LDAP_NO_MEMORY;
00106               return ld->ld_errno;
00107        }
00108 
00109        ber_printf( ber, "{tOtiN}",
00110               LDAP_TAG_EXOP_REFRESH_REQ_DN, dn,
00111               LDAP_TAG_EXOP_REFRESH_REQ_TTL, ttl );
00112 
00113        rc = ber_flatten2( ber, &bv, 0 );
00114 
00115        if ( rc < 0 ) {
00116               rc = ld->ld_errno = LDAP_ENCODING_ERROR;
00117               goto done;
00118        }
00119 
00120        rc = ldap_extended_operation( ld, LDAP_EXOP_REFRESH, &bv,
00121               sctrls, cctrls, msgidp );
00122 
00123 done:;
00124         ber_free( ber, 1 );
00125 
00126        return rc;
00127 }
00128 
00129 int
00130 ldap_refresh_s(
00131        LDAP          *ld,
00132        struct berval *dn,
00133        ber_int_t            ttl,
00134        ber_int_t            *newttl,
00135        LDAPControl   **sctrls,
00136        LDAPControl   **cctrls )
00137 {
00138        int           rc;
00139        int           msgid;
00140        LDAPMessage   *res;
00141 
00142        rc = ldap_refresh( ld, dn, ttl, sctrls, cctrls, &msgid );
00143        if ( rc != LDAP_SUCCESS ) return rc;
00144        
00145        rc = ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *)NULL, &res );
00146        if( rc == -1 || !res ) return ld->ld_errno;
00147 
00148        rc = ldap_parse_refresh( ld, res, newttl );
00149        if( rc != LDAP_SUCCESS ) {
00150               ldap_msgfree( res );
00151               return rc;
00152        }
00153 
00154        return ldap_result2error( ld, res, 1 );
00155 }
00156