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