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