]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
Reengineered ldappasswd(1). Uses extended operation to set
[openldap] / servers / slapd / extended.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999 The OpenLDAP Foundation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11
12 /*
13  * LDAPv3 Extended Operation Request
14  *      ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
15  *              requestName      [0] LDAPOID,
16  *              requestValue     [1] OCTET STRING OPTIONAL
17  *      }
18  *
19  * LDAPv3 Extended Operation Response
20  *      ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
21  *              COMPONENTS OF LDAPResult,
22  *              responseName     [10] LDAPOID OPTIONAL,
23  *              response         [11] OCTET STRING OPTIONAL
24  *      }
25  *
26  */
27
28 #include "portable.h"
29
30 #include <stdio.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34
35 #define MAX_OID_LENGTH  128
36
37 typedef struct extop_list_t {
38         struct extop_list_t *next;
39         char *oid;
40         SLAP_EXTOP_MAIN_FN ext_main;
41 } extop_list_t;
42
43 extop_list_t *supp_ext_list = NULL;
44
45 static extop_list_t *find_extop( extop_list_t *list, char *oid );
46
47 static int extop_callback(
48         Connection *conn, Operation *op,
49         int msg, int arg, void *argp);
50
51 char *
52 get_supported_extop (int index)
53 {
54         extop_list_t *ext;
55
56         /* linear scan is slow, but this way doesn't force a
57          * big change on root_dse.c, where this routine is used.
58          */
59         for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) ;
60         if (ext == NULL)
61                 return(NULL);
62         return(ext->oid);
63 }
64
65 int
66 do_extended(
67     Connection  *conn,
68     Operation   *op
69 )
70 {
71         int rc = LDAP_SUCCESS;
72         char* oid;
73         struct berval *reqdata;
74         ber_tag_t tag;
75         ber_len_t len;
76         extop_list_t *ext;
77         char *text;
78         struct berval *rspdata;
79
80         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
81
82         oid = NULL;
83         reqdata = NULL;
84
85         if( op->o_protocol < LDAP_VERSION3 ) {
86                 Debug( LDAP_DEBUG_ANY, "do_extended: protocol version (%d) too low\n",
87                         op->o_protocol, 0 ,0 );
88                 send_ldap_disconnect( conn, op,
89                         LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
90                 rc = -1;
91                 goto done;
92         }
93
94         if ( ber_scanf( op->o_ber, "{a" /*}*/, &oid ) == LBER_ERROR ) {
95                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
96                 send_ldap_disconnect( conn, op,
97                         LDAP_PROTOCOL_ERROR, "decoding error" );
98                 rc = -1;
99                 goto done;
100         }
101
102         if( !(ext = find_extop(supp_ext_list, oid)) ) {
103                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
104                         oid, 0 ,0 );
105                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
106                         NULL, "unsupported extended operation", NULL, NULL );
107                 goto done;
108         }
109
110         tag = ber_peek_tag( op->o_ber, &len );
111         
112         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
113                 if( ber_scanf( op->o_ber, "O", &reqdata ) == LBER_ERROR ) {
114                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
115                         send_ldap_disconnect( conn, op,
116                                 LDAP_PROTOCOL_ERROR, "decoding error" );
117                         rc = -1;
118                         goto done;
119                 }
120         }
121
122         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
123                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
124                 return rc;
125         } 
126
127         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", oid, 0 ,0 );
128
129         rspdata = NULL;
130         text = NULL;
131
132         rc = (ext->ext_main)( extop_callback, conn, op,
133                 oid, reqdata, &rspdata, &text );
134
135         if( rc != SLAPD_ABANDON ) {
136                 send_ldap_extended( conn, op, rc, NULL, text,
137                         oid, rspdata );
138         }
139
140         if ( rspdata != NULL )
141                 ber_bvfree( rspdata );
142
143         if ( text != NULL )
144                 free(text);
145
146 done:
147         if ( reqdata != NULL ) {
148                 ber_bvfree( reqdata );
149         }
150         if ( oid != NULL ) {
151                 free( oid );
152         }
153
154         return rc;
155 }
156
157 int
158 load_extop(
159         const char *ext_oid,
160         SLAP_EXTOP_MAIN_FN ext_main )
161 {
162         extop_list_t *ext;
163         int rc;
164
165         if( ext_oid == NULL || *ext_oid == '\0' ) return -1; 
166         if(!ext_main) return -1; 
167
168         ext = ch_calloc(1, sizeof(extop_list_t));
169         if (ext == NULL)
170                 return(-1);
171
172         ext->oid = ch_strdup( ext_oid );
173         if (ext->oid == NULL) {
174                 free(ext);
175                 return(-1);
176         }
177
178         ext->ext_main = ext_main;
179         ext->next = supp_ext_list;
180
181         supp_ext_list = ext;
182
183         return(0);
184 }
185
186
187 static extop_list_t *
188 find_extop( extop_list_t *list, char *oid )
189 {
190         extop_list_t *ext;
191
192         for (ext = list; ext; ext = ext->next) {
193                 if (strcmp(ext->oid, oid) == 0)
194                         return(ext);
195         }
196         return(NULL);
197 }
198
199 int
200 extop_callback(
201         Connection *conn, Operation *op,
202         int msg, int arg, void *argp)
203 {
204         if (argp == NULL)
205                 return(-1);
206
207         switch (msg) {
208         case SLAPD_EXTOP_GETVERSION:
209                 *(int *)argp = 1;
210                 return(0);
211
212         case SLAPD_EXTOP_GETPROTO:
213                 *(int *)argp = op->o_protocol;
214                 return(0);
215         
216         case SLAPD_EXTOP_GETAUTH:
217                 *(int *)argp = op->o_authtype;
218                 return(0);
219         
220         case SLAPD_EXTOP_GETDN:
221                 *(char **)argp = op->o_dn;
222                 return(0);
223         
224         case SLAPD_EXTOP_GETCLIENT:
225                 if (conn->c_peer_domain != NULL && *conn->c_peer_domain != 0) {
226                         *(char **)argp = conn->c_peer_domain;
227                         return(0);
228                 }
229                 if (conn->c_peer_name != NULL && *conn->c_peer_name != 0) {
230                         *(char **)argp = conn->c_peer_name;
231                         return(0);
232                 }
233                 break;
234         
235         default:
236                 break;
237         }
238         return(-1);
239 }