]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
UNDO LAST COMMIT.
[openldap] / servers / slapd / result.c
1 /* result.c - routines to send ldap results, errors, and referrals */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/errno.h>
8 #include <ac/signal.h>
9 #include <ac/socket.h>
10 #include <ac/string.h>
11 #include <ac/time.h>
12 #include <ac/unistd.h>          /* get close() */
13
14 #include "slap.h"
15
16 #ifdef HAVE_WINSOCK
17 #define EWOULDBLOCK WSAEWOULDBLOCK
18 #endif
19
20 static void
21 send_ldap_result2(
22     Connection  *conn,
23     Operation   *op,
24     int         err,
25     char        *matched,
26     char        *text,
27     int         nentries
28 )
29 {
30         BerElement      *ber;
31         int             rc, tmp;
32         unsigned long   tag, bytes;
33
34         if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
35                 err = LDAP_NO_SUCH_OBJECT;
36
37         Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n", err, matched ?
38             matched : "", text ? text : "" );
39
40         switch ( op->o_tag ) {
41         case LBER_DEFAULT:
42                 tag = LBER_SEQUENCE;
43                 break;
44
45         case LDAP_REQ_SEARCH:
46                 tag = LDAP_RES_SEARCH_RESULT;
47                 break;
48
49         case LDAP_REQ_DELETE:
50                 tag = LDAP_RES_DELETE;
51                 break;
52
53         default:
54                 tag = op->o_tag + 1;
55                 break;
56         }
57
58
59 #ifdef LDAP_COMPAT30
60         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
61             == NULLBER )
62 #else
63         if ( (ber = der_alloc()) == NULLBER )
64 #endif
65         {
66                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
67                 return;
68         }
69
70 #ifdef LDAP_CONNECTIONLESS
71         if ( op->o_cldap ) {
72                 rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
73                     err, matched ? matched : "", text ? text : "" );
74         } else
75 #endif
76 #ifdef LDAP_COMPAT30
77         if ( conn->c_version == 30 ) {
78                 rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
79                     matched ? matched : "", text ? text : "" );
80         } else
81 #endif
82                 rc = ber_printf( ber, "{it{ess}}", op->o_msgid, tag, err,
83                     matched ? matched : "", text ? text : "" );
84
85         if ( rc == -1 ) {
86                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
87                 return;
88         }
89
90         /* write only one pdu at a time - wait til it's our turn */
91         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
92
93         /* lock the connection */
94         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
95
96         /* write the pdu */
97         bytes = ber->ber_ptr - ber->ber_buf;
98
99         while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
100                 /*
101                  * we got an error.  if it's ewouldblock, we need to
102                  * wait on the socket being writable.  otherwise, figure
103                  * it's a hard error and return.
104                  */
105
106                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
107                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
108                     : "unknown", 0 );
109
110                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
111                         connection_closing( conn );
112
113                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
114                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
115                         return;
116                 }
117
118                 /* wait for socket to be write-ready */
119                 conn->c_writewaiter = 1;
120                 slapd_set_write( conn->c_sb.sb_sd, 1 );
121
122                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
123                 conn->c_writewaiter = 0;
124         }
125
126         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
127         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
128
129         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
130         num_bytes_sent += bytes;
131         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
132
133         Statslog( LDAP_DEBUG_STATS,
134             "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
135             op->o_opid, err, tag, nentries );
136
137         return;
138 }
139
140 void
141 send_ldap_result(
142     Connection  *conn,
143     Operation   *op,
144     int         err,
145     char        *matched,
146     char        *text
147 )
148 {
149 #ifdef LDAP_CONNECTIONLESS
150         if ( op->o_cldap ) {
151                 lber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
152                 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
153                     inet_ntoa(((struct sockaddr_in *)
154                     &op->o_clientaddr)->sin_addr ),
155                     ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
156                     0 );
157         }
158 #endif
159         send_ldap_result2( conn, op, err, matched, text, 0 );
160 }
161
162 void
163 send_ldap_search_result(
164     Connection  *conn,
165     Operation   *op,
166     int         err,
167     char        *matched,
168     char        *text,
169     int         nentries
170 )
171 {
172         send_ldap_result2( conn, op, err, matched, text, nentries );
173 }
174
175 int
176 send_search_entry(
177     Backend     *be,
178     Connection  *conn,
179     Operation   *op,
180     Entry       *e,
181     char        **attrs,
182     int         attrsonly
183 )
184 {
185         BerElement      *ber;
186         Attribute       *a;
187         int             i, rc=-1, bytes;
188         struct acl      *acl;
189         char            *edn;
190
191         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
192
193         if ( ! access_allowed( be, conn, op, e,
194                 "entry", NULL, ACL_READ ) )
195         {
196                 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
197                     0, 0, 0 );
198                 return( 1 );
199         }
200
201         edn = e->e_ndn;
202
203 #ifdef LDAP_COMPAT30
204         if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
205                 == NULLBER )
206 #else
207         if ( (ber = der_alloc()) == NULLBER )
208 #endif
209         {
210                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
211                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
212                         "ber_alloc" );
213                 goto error_return;
214         }
215
216 #ifdef LDAP_COMPAT30
217         if ( conn->c_version == 30 ) {
218                 rc = ber_printf( ber, "{it{{s{", op->o_msgid,
219                     LDAP_RES_SEARCH_ENTRY, e->e_dn );
220         } else
221 #endif
222         {
223                 rc = ber_printf( ber, "{it{s{", op->o_msgid,
224                         LDAP_RES_SEARCH_ENTRY, e->e_dn );
225         }
226
227         if ( rc == -1 ) {
228                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
229                 ber_free( ber, 1 );
230                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
231                     "ber_printf dn" );
232                 goto error_return;
233         }
234
235         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
236                 regmatch_t       matches[MAXREMATCHES];
237
238                 if ( attrs != NULL && ! charray_inlist( attrs, a->a_type ) ) {
239                         continue;
240                 }
241
242                 /* the lastmod attributes are ignored by ACL checking */
243                 if ( strcasecmp( a->a_type, "modifiersname" ) == 0 ||
244                         strcasecmp( a->a_type, "modifytimestamp" ) == 0 ||
245                         strcasecmp( a->a_type, "creatorsname" ) == 0 ||
246                         strcasecmp( a->a_type, "createtimestamp" ) == 0 ) 
247                 {
248                         Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access DEFAULT\n",
249                                 a->a_type, 0, 0 );
250                         acl = NULL;
251                 } else {
252                         acl = acl_get_applicable( be, op, e, a->a_type,
253                                 MAXREMATCHES, matches );
254                 }
255
256                 if ( ! acl_access_allowed( acl, be, conn, e,
257                         NULL, op, ACL_READ, edn, matches ) ) 
258                 {
259                         continue;
260                 }
261
262                 if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
263                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
264                         ber_free( ber, 1 );
265                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
266                             NULL, "ber_printf type" );
267                         goto error_return;
268                 }
269
270                 if ( ! attrsonly ) {
271                         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
272                                 if ( a->a_syntax & SYNTAX_DN && 
273                                         ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
274                                                 ACL_READ, edn, matches) )
275                                 {
276                                         continue;
277                                 }
278
279                                 if (( rc = ber_printf( ber, "o",
280                                     a->a_vals[i]->bv_val,
281                                     a->a_vals[i]->bv_len )) == -1 )
282                                 {
283                                         Debug( LDAP_DEBUG_ANY,
284                                             "ber_printf failed\n", 0, 0, 0 );
285                                         ber_free( ber, 1 );
286                                         send_ldap_result( conn, op,
287                                             LDAP_OPERATIONS_ERROR, NULL,
288                                             "ber_printf value" );
289                                         goto error_return;
290                                 }
291                         }
292                 }
293
294                 if (( rc = ber_printf( ber, "]}" )) == -1 ) {
295                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
296                         ber_free( ber, 1 );
297                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
298                             NULL, "ber_printf type end" );
299                         goto error_return;
300                 }
301         }
302
303 #ifdef LDAP_COMPAT30
304         if ( conn->c_version == 30 ) {
305                 rc = ber_printf( ber, "}}}}" );
306         } else
307 #endif
308                 rc = ber_printf( ber, "}}}" );
309
310         if ( rc == -1 ) {
311                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
312                 ber_free( ber, 1 );
313                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
314                     "ber_printf entry end" );
315                 return( 1 );
316         }
317
318         bytes = ber->ber_ptr - ber->ber_buf;
319
320         /* write only one pdu at a time - wait til it's our turn */
321         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
322
323         /* lock the connection */ 
324         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
325
326         /* write the pdu */
327         while ( ber_flush( &conn->c_sb, ber, 1 ) != 0 ) {
328                 /*
329                  * we got an error.  if it's ewouldblock, we need to
330                  * wait on the socket being writable.  otherwise, figure
331                  * it's a hard error and return.
332                  */
333
334                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
335                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno]
336                     : "unknown", 0 );
337
338                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
339                         connection_closing( conn );
340
341                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
342                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
343                         return( -1 );
344                 }
345
346                 /* wait for socket to be write-ready */
347                 conn->c_writewaiter = 1;
348                 slapd_set_write( conn->c_sb.sb_sd, 1 );
349
350                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
351                 conn->c_writewaiter = 0;
352         }
353
354         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
355         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
356
357         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
358         num_bytes_sent += bytes;
359         num_entries_sent++;
360         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
361
362         Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
363             conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
364
365         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
366
367         rc = 0;
368
369 error_return:;
370         return( rc );
371 }
372
373 int
374 str2result(
375     char        *s,
376     int         *code,
377     char        **matched,
378     char        **info
379 )
380 {
381         int     rc;
382         char    *c;
383
384         *code = LDAP_SUCCESS;
385         *matched = NULL;
386         *info = NULL;
387
388         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
389                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
390                     s, 0, 0 );
391
392                 return( -1 );
393         }
394
395         rc = 0;
396         while ( (s = strchr( s, '\n' )) != NULL ) {
397                 *s++ = '\0';
398                 if ( *s == '\0' ) {
399                         break;
400                 }
401                 if ( (c = strchr( s, ':' )) != NULL ) {
402                         c++;
403                 }
404
405                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
406                         if ( c != NULL ) {
407                                 *code = atoi( c );
408                         }
409                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
410                         if ( c != NULL ) {
411                                 *matched = c;
412                         }
413                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
414                         if ( c != NULL ) {
415                                 *info = c;
416                         }
417                 } else {
418                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
419                             s, 0, 0 );
420                         rc = -1;
421                 }
422         }
423
424         return( rc );
425 }