1 /*
2 *
3 * Copyright 1999 Sun Microsystems, Inc. All rights reserved.
4 * Use is subject to license terms.
5 *
6 *
7 * Comments:
8 *
9 */
10
11 #pragma ident "%Z%%M% %I% %E% SMI"
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #ifdef MACOS
17 #include "macos.h"
18 #endif /* MACOS */
19
20 #if !defined( MACOS ) && !defined( DOS )
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #endif
24
25 #include "lber.h"
26 #include "ldap.h"
27 #include "ldap-private.h"
28 #include "ldap-int.h"
29
ldap_build_extended_operation_req(LDAP * ld,char * exoid,struct berval * exdata,LDAPControl ** serverctrls)30 BerElement * ldap_build_extended_operation_req(LDAP *ld, char *exoid, struct berval *exdata, LDAPControl ** serverctrls)
31 {
32 BerElement *ber;
33 int rv;
34
35 /* an extended operation request looks like this:
36 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
37 * requestName [0] LDAPOID,
38 * requestValue [1] OCTECT STRING OPTIONAL
39 * }
40 */
41 if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
42 ld->ld_errno = LDAP_NO_MEMORY;
43 return( NULLBER );
44 }
45
46 if ( ber_printf( ber, "{it{ts", ++ld->ld_msgid, LDAP_REQ_EXTENDED, LDAP_TAG_EXT_NAME, exoid ) == -1 ) {
47 ld->ld_errno = LDAP_ENCODING_ERROR;
48 ber_free( ber, 1 );
49 return( NULLBER );
50 }
51
52 if (exdata && (ber_printf(ber, "to", LDAP_TAG_EXT_VAL, exdata->bv_val, exdata->bv_len) == -1 )) {
53 ld->ld_errno = LDAP_ENCODING_ERROR;
54 ber_free( ber, 1 );
55 return( NULLBER );
56 }
57
58 if ( ber_printf( ber, "}" ) == -1 ) {
59 ld->ld_errno = LDAP_ENCODING_ERROR;
60 ber_free( ber, 1 );
61 return( NULLBER );
62 }
63 /* LDAPv3 */
64 /* Code controls if any */
65 if (serverctrls && serverctrls[0]) {
66 if (ldap_controls_code(ber, serverctrls) != LDAP_SUCCESS){
67 ld->ld_errno = LDAP_ENCODING_ERROR;
68 ber_free( ber, 1 );
69 return( NULLBER );
70 }
71 } else if (ld->ld_srvctrls && ld->ld_srvctrls[0]) {
72 /* Otherwise, is there any global server ctrls ? */
73 if (ldap_controls_code(ber, ld->ld_srvctrls) != LDAP_SUCCESS){
74 ld->ld_errno = LDAP_ENCODING_ERROR;
75 ber_free( ber, 1 );
76 return( NULLBER );
77 }
78 }
79
80 if ( ber_printf( ber, "}" ) == -1 ) {
81 ld->ld_errno = LDAP_ENCODING_ERROR;
82 ber_free( ber, 1 );
83 return( NULLBER );
84 }
85
86 return (ber);
87 }
88
89 /* ldap_extended_operation - initiate an ldap extended operation.
90 * Parameters :
91 * ld : LDAP descriptor.
92 * exoid : OID of the request.
93 * exdata : Arbitrary data required by the operation.
94 * serverctrls : List of server controls.
95 * clientctrls : List of client controls.
96 * msgidp : msg id returned if operation succeeded.
97 * Returns LDAP_SUCCESS or error code.
98 */
99
ldap_extended_operation(LDAP * ld,char * exoid,struct berval * exdata,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int * msgidp)100 int ldap_extended_operation(LDAP *ld, char *exoid, struct berval *exdata,
101 LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp)
102 {
103 BerElement *ber;
104 int rv;
105
106 #ifdef _REENTRANT
107 LOCK_LDAP(ld);
108 #endif
109 Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 242, "ldap_extended_operation\n"), 0, 0, 0 );
110
111 if (( ber = ldap_build_extended_operation_req( ld, exoid, exdata, serverctrls)) == NULLBER ) {
112 rv = ld->ld_errno;
113 if (rv == LDAP_SUCCESS)
114 rv = LDAP_OTHER;
115 #ifdef _REENTRANT
116 UNLOCK_LDAP(ld);
117 #endif
118 return( rv);
119 }
120
121 /* send the message */
122 rv = send_initial_request( ld, LDAP_REQ_EXTENDED, NULL, ber );
123 if (rv == -1) {
124 rv = ld->ld_errno;
125 if (rv == LDAP_SUCCESS){
126 rv = LDAP_OTHER;
127 }
128 #ifdef _REENTRANT
129 UNLOCK_LDAP(ld);
130 #endif
131 return (rv);
132 }
133
134 *msgidp = rv;
135 #if _REENTRANT
136 UNLOCK_LDAP(ld);
137 #endif
138 return ( LDAP_SUCCESS );
139 }
140
141
ldap_extended_operation_s(LDAP * ld,char * exoid,struct berval * exdata,LDAPControl ** serverctrls,LDAPControl ** clientctrls,char ** retoidp,struct berval ** retdatap)142 int ldap_extended_operation_s(LDAP *ld, char *exoid, struct berval *exdata,
143 LDAPControl **serverctrls, LDAPControl **clientctrls,
144 char **retoidp, struct berval **retdatap)
145 {
146 int msgid;
147 int retcode;
148 LDAPMessage *res = NULL;
149
150 if ((retcode = ldap_extended_operation(ld, exoid, exdata, serverctrls, clientctrls, &msgid)) != LDAP_SUCCESS)
151 return (retcode);
152 if (ldap_result(ld, msgid, 1, (struct timeval *)NULL, &res ) == -1)
153 return (ld->ld_errno );
154
155 return (ldap_parse_extended_result(ld, res, retoidp, retdatap, 1));
156 }
157