Back to index

openldap  2.4.31
txn.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 2006-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* ACKNOWLEDGEMENTS:
00016  * This program was orignally developed by Kurt D. Zeilenga for inclusion
00017  * in OpenLDAP Software.
00018  */
00019 
00020 /*
00021  * LDAPv3 Transactions (draft-zeilenga-ldap-txn)
00022  */
00023 
00024 #include "portable.h"
00025 
00026 #include <stdio.h>
00027 #include <ac/stdlib.h>
00028 
00029 #include <ac/socket.h>
00030 #include <ac/string.h>
00031 #include <ac/time.h>
00032 
00033 #include "ldap-int.h"
00034 #include "ldap_log.h"
00035 
00036 #ifdef LDAP_X_TXN
00037 int
00038 ldap_txn_start(
00039        LDAP *ld,
00040        LDAPControl **sctrls,
00041        LDAPControl **cctrls,
00042        int *msgidp )
00043 {
00044        return ldap_extended_operation( ld, LDAP_EXOP_X_TXN_START,
00045               NULL, sctrls, cctrls, msgidp );
00046 }
00047 
00048 int
00049 ldap_txn_start_s(
00050        LDAP *ld,
00051        LDAPControl **sctrls,
00052        LDAPControl **cctrls,
00053        struct berval **txnid )
00054 {
00055        assert( txnid != NULL );
00056 
00057        return ldap_extended_operation_s( ld, LDAP_EXOP_X_TXN_START,
00058               NULL, sctrls, cctrls, NULL, txnid );
00059 }
00060 
00061 int
00062 ldap_txn_end(
00063        LDAP *ld,
00064        int commit,
00065        struct berval *txnid,
00066        LDAPControl **sctrls,
00067        LDAPControl **cctrls,
00068        int *msgidp )
00069 {
00070        int rc;
00071        BerElement *txnber = NULL;
00072        struct berval *txnval = NULL;
00073 
00074        assert( txnid != NULL );
00075 
00076        txnber = ber_alloc_t( LBER_USE_DER );
00077 
00078        if( commit ) {
00079               ber_printf( txnber, "{ON}", txnid );
00080        } else {
00081               ber_printf( txnber, "{bON}", commit, txnid );
00082        }
00083 
00084        ber_flatten( txnber, &txnval );
00085 
00086        rc = ldap_extended_operation( ld, LDAP_EXOP_X_TXN_END,
00087               txnval, sctrls, cctrls, msgidp );
00088 
00089        ber_free( txnber, 1 );
00090        return rc;
00091 }
00092 
00093 int
00094 ldap_txn_end_s(
00095        LDAP *ld,
00096        int commit,
00097        struct berval *txnid,
00098        LDAPControl **sctrls,
00099        LDAPControl **cctrls,
00100        int *retidp )
00101 {
00102        int rc;
00103        BerElement *txnber = NULL;
00104        struct berval *txnval = NULL;
00105        struct berval *retdata = NULL;
00106 
00107        if ( retidp != NULL ) *retidp = -1;
00108 
00109        txnber = ber_alloc_t( LBER_USE_DER );
00110 
00111        if( commit ) {
00112               ber_printf( txnber, "{ON}", txnid );
00113        } else {
00114               ber_printf( txnber, "{bON}", commit, txnid );
00115        }
00116 
00117        ber_flatten( txnber, &txnval );
00118 
00119        rc = ldap_extended_operation_s( ld, LDAP_EXOP_X_TXN_END,
00120               txnval, sctrls, cctrls, NULL, &retdata );
00121 
00122        ber_free( txnber, 1 );
00123 
00124        /* parse retdata */
00125        if( retdata != NULL ) {
00126               BerElement *ber;
00127               ber_tag_t tag;
00128               ber_int_t retid;
00129 
00130               if( retidp == NULL ) goto done;
00131 
00132               ber = ber_init( retdata );
00133 
00134               if( ber == NULL ) {
00135                      rc = ld->ld_errno = LDAP_NO_MEMORY;
00136                      goto done;
00137               }
00138 
00139               tag = ber_scanf( ber, "i", &retid );
00140               ber_free( ber, 1 );
00141 
00142               if ( tag != LBER_INTEGER ) {
00143                      rc = ld->ld_errno = LDAP_DECODING_ERROR;
00144                      goto done;
00145               }
00146 
00147               *retidp = (int) retid;
00148 
00149 done:
00150               ber_bvfree( retdata );
00151        }
00152 
00153        return rc;
00154 }
00155 #endif