1/* result.c - shell backend result reading function */
2/* $OpenLDAP$ */
3/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 1998-2011 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/* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
25 */
26/* ACKNOWLEDGEMENTS:
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
29 */
30
31#include "portable.h"
32
33#include <stdio.h>
34
35#include <ac/errno.h>
36#include <ac/string.h>
37#include <ac/socket.h>
38#include <ac/unistd.h>
39
40#include "slap.h"
41#include "shell.h"
42
43int
44read_and_send_results(
45    Operation	*op,
46    SlapReply	*rs,
47    FILE	*fp )
48{
49	int	bsize, len;
50	char	*buf, *bp;
51	char	line[BUFSIZ];
52	char	ebuf[128];
53
54	/* read in the result and send it along */
55	buf = (char *) ch_malloc( BUFSIZ );
56	buf[0] = '\0';
57	bsize = BUFSIZ;
58	bp = buf;
59	while ( !feof(fp) ) {
60		errno = 0;
61		if ( fgets( line, sizeof(line), fp ) == NULL ) {
62			if ( errno == EINTR ) continue;
63
64			Debug( LDAP_DEBUG_ANY, "shell: fgets failed: %s (%d)\n",
65				AC_STRERROR_R(errno, ebuf, sizeof ebuf), errno, 0 );
66			break;
67		}
68
69		Debug( LDAP_DEBUG_SHELL, "shell search reading line (%s)\n",
70		    line, 0, 0 );
71
72		/* ignore lines beginning with # (LDIFv1 comments) */
73		if ( *line == '#' ) {
74			continue;
75		}
76
77		/* ignore lines beginning with DEBUG: */
78		if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
79			continue;
80		}
81
82		len = strlen( line );
83		while ( bp + len + 1 - buf > bsize ) {
84			size_t offset = bp - buf;
85			bsize += BUFSIZ;
86			buf = (char *) ch_realloc( buf, bsize );
87			bp = &buf[offset];
88		}
89		strcpy( bp, line );
90		bp += len;
91
92		/* line marked the end of an entry or result */
93		if ( *line == '\n' ) {
94			if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
95				break;
96			}
97
98			if ( (rs->sr_entry = str2entry( buf )) == NULL ) {
99				Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
100				    buf, 0, 0 );
101			} else {
102				rs->sr_attrs = op->oq_search.rs_attrs;
103				rs->sr_flags = REP_ENTRY_MODIFIABLE;
104				send_search_entry( op, rs );
105				entry_free( rs->sr_entry );
106				rs->sr_attrs = NULL;
107			}
108
109			bp = buf;
110		}
111	}
112	(void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text );
113
114	/* otherwise, front end will send this result */
115	if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) {
116		send_ldap_result( op, rs );
117	}
118
119	free( buf );
120
121	return( rs->sr_err );
122}
123
124void
125print_suffixes(
126    FILE	*fp,
127    Backend	*be
128)
129{
130	int	i;
131
132	for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) {
133		fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val );
134	}
135}
136