]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sock/result.c
back-sock by Brian Candler (B.Candler@pobox.com) ITS#4094 (untested)
[openldap] / servers / slapd / back-sock / result.c
1 /* result.c - sock backend result reading function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/errno.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
24 #include <ac/unistd.h>
25
26 #include "slap.h"
27 #include "back-sock.h"
28
29 /*
30  * FIXME: make a RESULT section compulsory from the socket response.
31  * Otherwise, a partial/aborted response is treated as 'success'.
32  * This is a divergence from the back-shell protocol, but makes things
33  * more robust.
34  */
35
36 int
37 sock_read_and_send_results(
38     Operation   *op,
39     SlapReply   *rs,
40     FILE        *fp )
41 {
42         int     bsize, len;
43         char    *buf, *bp;
44         char    line[BUFSIZ];
45         char    ebuf[128];
46
47         /* read in the result and send it along */
48         buf = (char *) ch_malloc( BUFSIZ );
49         buf[0] = '\0';
50         bsize = BUFSIZ;
51         bp = buf;
52         while ( !feof(fp) ) {
53                 errno = 0;
54                 if ( fgets( line, sizeof(line), fp ) == NULL ) {
55                         if ( errno == EINTR ) continue;
56
57                         Debug( LDAP_DEBUG_ANY, "sock: fgets failed: %s (%d)\n",
58                                 AC_STRERROR_R(errno, ebuf, sizeof ebuf), errno, 0 ); 
59                         break;
60                 }
61
62                 Debug( LDAP_DEBUG_SHELL, "sock search reading line (%s)\n",
63                     line, 0, 0 );
64
65                 /* ignore lines beginning with # (LDIFv1 comments) */
66                 if ( *line == '#' ) {
67                         continue;
68                 }
69
70                 /* ignore lines beginning with DEBUG: */
71                 if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
72                         continue;
73                 }
74
75                 len = strlen( line );
76                 while ( bp + len - buf > bsize ) {
77                         size_t offset = bp - buf;
78                         bsize += BUFSIZ;
79                         buf = (char *) ch_realloc( buf, bsize );
80                         bp = &buf[offset];
81                 }
82                 strcpy( bp, line );
83                 bp += len;
84
85                 /* line marked the end of an entry or result */
86                 if ( *line == '\n' ) {
87                         if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
88                                 break;
89                         }
90
91                         if ( (rs->sr_entry = str2entry( buf )) == NULL ) {
92                                 Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
93                                     buf, 0, 0 );
94                         } else {
95                                 rs->sr_attrs = op->oq_search.rs_attrs;
96                                 rs->sr_flags = REP_ENTRY_MODIFIABLE;
97                                 send_search_entry( op, rs );
98                                 entry_free( rs->sr_entry );
99                         }
100
101                         bp = buf;
102                 }
103         }
104         (void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text );
105
106         /* otherwise, front end will send this result */
107         if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) {
108                 send_ldap_result( op, rs );
109         }
110
111         free( buf );
112
113         return( rs->sr_err );
114 }
115
116 void
117 sock_print_suffixes(
118     FILE        *fp,
119     Backend     *be
120 )
121 {
122         int     i;
123
124         for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) {
125                 fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val );
126         }
127 }
128
129 void
130 sock_print_conn(
131     FILE        *fp,
132     Connection  *conn,
133     struct sockinfo *si
134 )
135 {
136         if ( conn == NULL ) return;
137
138         if( si->si_extensions & SOCK_EXT_BINDDN ) {
139                 fprintf( fp, "binddn: %s\n",
140                         conn->c_dn.bv_len ? conn->c_dn.bv_val : "" );
141         }
142         if( si->si_extensions & SOCK_EXT_PEERNAME ) {
143                 fprintf( fp, "peername: %s\n",
144                         conn->c_peer_name.bv_len ? conn->c_peer_name.bv_val : "" );
145         }
146         if( si->si_extensions & SOCK_EXT_SSF ) {
147                 fprintf( fp, "ssf: %d\n", conn->c_ssf );
148         }
149 }