1/* search.c - shell backend search 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/socket.h>
36#include <ac/string.h>
37
38#include "slap.h"
39#include "shell.h"
40
41int
42shell_back_search(
43    Operation	*op,
44    SlapReply	*rs )
45{
46	struct shellinfo	*si = (struct shellinfo *) op->o_bd->be_private;
47	FILE			*rfp, *wfp;
48	AttributeName		*an;
49
50	if ( si->si_search == NULL ) {
51		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
52		    "search not implemented" );
53		return( -1 );
54	}
55
56	if ( forkandexec( si->si_search, &rfp, &wfp ) == (pid_t)-1 ) {
57		send_ldap_error( op, rs, LDAP_OTHER,
58		    "could not fork/exec" );
59		return( -1 );
60	}
61
62	/* write out the request to the search process */
63	fprintf( wfp, "SEARCH\n" );
64	fprintf( wfp, "msgid: %ld\n", (long) op->o_msgid );
65	print_suffixes( wfp, op->o_bd );
66	fprintf( wfp, "base: %s\n", op->o_req_dn.bv_val );
67	fprintf( wfp, "scope: %d\n", op->oq_search.rs_scope );
68	fprintf( wfp, "deref: %d\n", op->oq_search.rs_deref );
69	fprintf( wfp, "sizelimit: %d\n", op->oq_search.rs_slimit );
70	fprintf( wfp, "timelimit: %d\n", op->oq_search.rs_tlimit );
71	fprintf( wfp, "filter: %s\n", op->oq_search.rs_filterstr.bv_val );
72	fprintf( wfp, "attrsonly: %d\n", op->oq_search.rs_attrsonly ? 1 : 0 );
73	fprintf( wfp, "attrs:%s", op->oq_search.rs_attrs == NULL ? " all" : "" );
74	for ( an = op->oq_search.rs_attrs; an && an->an_name.bv_val; an++ ) {
75		fprintf( wfp, " %s", an->an_name.bv_val );
76	}
77	fprintf( wfp, "\n" );
78	fclose( wfp );
79
80	/* read in the results and send them along */
81	rs->sr_attrs = op->oq_search.rs_attrs;
82	read_and_send_results( op, rs, rfp );
83
84	fclose( rfp );
85	return( 0 );
86}
87