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