1 /*	$NetBSD: result.c,v 1.3 2021/08/14 16:15:01 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-2021 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.3 2021/08/14 16:15:01 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
sock_read_and_send_results(Operation * op,SlapReply * rs,FILE * fp)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 			int saved_errno = errno;
66 			if ( errno == EINTR ) continue;
67 
68 			Debug( LDAP_DEBUG_ANY, "sock: fgets failed: %s (%d)\n",
69 				AC_STRERROR_R(saved_errno, ebuf, sizeof ebuf), saved_errno );
70 			break;
71 		}
72 
73 		Debug( LDAP_DEBUG_SHELL, "sock search reading line (%s)\n",
74 		    line );
75 
76 		/* ignore lines beginning with # (LDIFv1 comments) */
77 		if ( *line == '#' ) {
78 			continue;
79 		}
80 
81 		/* ignore lines beginning with DEBUG: */
82 		if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
83 			continue;
84 		}
85 
86 		if ( strncasecmp( line, "CONTINUE", 8 ) == 0 ) {
87 			struct sockinfo	*si = (struct sockinfo *) op->o_bd->be_private;
88 			/* Only valid when operating as an overlay! */
89 			assert( si->si_ops != 0 );
90 			rs->sr_err = SLAP_CB_CONTINUE;
91 			goto skip;
92 		}
93 
94 		len = strlen( line );
95 		while ( bp + len + 1 - buf > bsize ) {
96 			size_t offset = bp - buf;
97 			bsize += BUFSIZ;
98 			buf = (char *) ch_realloc( buf, bsize );
99 			bp = &buf[offset];
100 		}
101 		strcpy( bp, line );
102 		bp += len;
103 
104 		/* line marked the end of an entry or result */
105 		if ( *line == '\n' ) {
106 			if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
107 				break;
108 			}
109 
110 			if ( (rs->sr_entry = str2entry( buf )) == NULL ) {
111 				Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
112 				    buf );
113 			} else {
114 				rs->sr_attrs = op->oq_search.rs_attrs;
115 				rs->sr_flags = REP_ENTRY_MODIFIABLE;
116 				send_search_entry( op, rs );
117 				entry_free( rs->sr_entry );
118 				rs->sr_attrs = NULL;
119 			}
120 
121 			bp = buf;
122 		}
123 	}
124 	(void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text );
125 
126 	/* otherwise, front end will send this result */
127 	if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) {
128 		send_ldap_result( op, rs );
129 	}
130 
131 skip:
132 	ch_free( buf );
133 
134 	return( rs->sr_err );
135 }
136 
137 void
sock_print_suffixes(FILE * fp,Backend * be)138 sock_print_suffixes(
139     FILE	*fp,
140     Backend	*be
141 )
142 {
143 	int	i;
144 
145 	for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) {
146 		fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val );
147 	}
148 }
149 
150 void
sock_print_conn(FILE * fp,Connection * conn,struct sockinfo * si)151 sock_print_conn(
152     FILE	*fp,
153     Connection	*conn,
154     struct sockinfo *si
155 )
156 {
157 	if ( conn == NULL ) return;
158 
159 	if( si->si_extensions & SOCK_EXT_BINDDN ) {
160 		fprintf( fp, "binddn: %s\n",
161 			conn->c_dn.bv_len ? conn->c_dn.bv_val : "" );
162 	}
163 	if( si->si_extensions & SOCK_EXT_PEERNAME ) {
164 		fprintf( fp, "peername: %s\n",
165 			conn->c_peer_name.bv_len ? conn->c_peer_name.bv_val : "" );
166 	}
167 	if( si->si_extensions & SOCK_EXT_SSF ) {
168 		fprintf( fp, "ssf: %d\n", conn->c_ssf );
169 	}
170 	if( si->si_extensions & SOCK_EXT_CONNID ) {
171 		fprintf( fp, "connid: %lu\n", conn->c_connid );
172 	}
173 }
174