xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-sock/result.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: result.c,v 1.1.1.7 2018/02/06 01:53:17 christos Exp $	*/
2 
3 /* result.c - sock backend result reading function */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 2007-2017 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 <sys/cdefs.h>
24 __RCSID("$NetBSD: result.c,v 1.1.1.7 2018/02/06 01:53:17 christos Exp $");
25 
26 #include "portable.h"
27 
28 #include <stdio.h>
29 
30 #include <ac/errno.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/unistd.h>
34 
35 #include "slap.h"
36 #include "back-sock.h"
37 
38 /*
39  * FIXME: make a RESULT section compulsory from the socket response.
40  * Otherwise, a partial/aborted response is treated as 'success'.
41  * This is a divergence from the back-shell protocol, but makes things
42  * more robust.
43  */
44 
45 int
46 sock_read_and_send_results(
47     Operation	*op,
48     SlapReply	*rs,
49     FILE	*fp )
50 {
51 	int	bsize, len;
52 	char	*buf, *bp;
53 	char	line[BUFSIZ];
54 	char	ebuf[128];
55 
56 	(void) fflush(fp);
57 	/* read in the result and send it along */
58 	buf = (char *) ch_malloc( BUFSIZ );
59 	buf[0] = '\0';
60 	bsize = BUFSIZ;
61 	bp = buf;
62 	while ( !feof(fp) ) {
63 		errno = 0;
64 		if ( fgets( line, sizeof(line), fp ) == NULL ) {
65 			if ( errno == EINTR ) continue;
66 
67 			Debug( LDAP_DEBUG_ANY, "sock: fgets failed: %s (%d)\n",
68 				AC_STRERROR_R(errno, ebuf, sizeof ebuf), errno, 0 );
69 			break;
70 		}
71 
72 		Debug( LDAP_DEBUG_SHELL, "sock search reading line (%s)\n",
73 		    line, 0, 0 );
74 
75 		/* ignore lines beginning with # (LDIFv1 comments) */
76 		if ( *line == '#' ) {
77 			continue;
78 		}
79 
80 		/* ignore lines beginning with DEBUG: */
81 		if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
82 			continue;
83 		}
84 
85 		if ( strncasecmp( line, "CONTINUE", 8 ) == 0 ) {
86 			struct sockinfo	*si = (struct sockinfo *) op->o_bd->be_private;
87 			/* Only valid when operating as an overlay! */
88 			assert( si->si_ops != 0 );
89 			rs->sr_err = SLAP_CB_CONTINUE;
90 			goto skip;
91 		}
92 
93 		len = strlen( line );
94 		while ( bp + len + 1 - buf > bsize ) {
95 			size_t offset = bp - buf;
96 			bsize += BUFSIZ;
97 			buf = (char *) ch_realloc( buf, bsize );
98 			bp = &buf[offset];
99 		}
100 		strcpy( bp, line );
101 		bp += len;
102 
103 		/* line marked the end of an entry or result */
104 		if ( *line == '\n' ) {
105 			if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
106 				break;
107 			}
108 
109 			if ( (rs->sr_entry = str2entry( buf )) == NULL ) {
110 				Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
111 				    buf, 0, 0 );
112 			} else {
113 				rs->sr_attrs = op->oq_search.rs_attrs;
114 				rs->sr_flags = REP_ENTRY_MODIFIABLE;
115 				send_search_entry( op, rs );
116 				entry_free( rs->sr_entry );
117 				rs->sr_attrs = NULL;
118 			}
119 
120 			bp = buf;
121 		}
122 	}
123 	(void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text );
124 
125 	/* otherwise, front end will send this result */
126 	if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) {
127 		send_ldap_result( op, rs );
128 	}
129 
130 skip:
131 	ch_free( buf );
132 
133 	return( rs->sr_err );
134 }
135 
136 void
137 sock_print_suffixes(
138     FILE	*fp,
139     Backend	*be
140 )
141 {
142 	int	i;
143 
144 	for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) {
145 		fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val );
146 	}
147 }
148 
149 void
150 sock_print_conn(
151     FILE	*fp,
152     Connection	*conn,
153     struct sockinfo *si
154 )
155 {
156 	if ( conn == NULL ) return;
157 
158 	if( si->si_extensions & SOCK_EXT_BINDDN ) {
159 		fprintf( fp, "binddn: %s\n",
160 			conn->c_dn.bv_len ? conn->c_dn.bv_val : "" );
161 	}
162 	if( si->si_extensions & SOCK_EXT_PEERNAME ) {
163 		fprintf( fp, "peername: %s\n",
164 			conn->c_peer_name.bv_len ? conn->c_peer_name.bv_val : "" );
165 	}
166 	if( si->si_extensions & SOCK_EXT_SSF ) {
167 		fprintf( fp, "ssf: %d\n", conn->c_ssf );
168 	}
169 	if( si->si_extensions & SOCK_EXT_CONNID ) {
170 		fprintf( fp, "connid: %lu\n", conn->c_connid );
171 	}
172 }
173