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