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