1 /*	$NetBSD: search.c,v 1.1.1.3 2010/12/12 15:23:22 adam Exp $	*/
2 
3 /* search.c - sock backend search function */
4 /* OpenLDAP: pkg/ldap/servers/slapd/back-sock/search.c,v 1.3.2.3 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/socket.h>
28 #include <ac/string.h>
29 
30 #include "slap.h"
31 #include "back-sock.h"
32 
33 /*
34  * FIXME: add a filterSearchResults option like back-perl has
35  */
36 
37 int
38 sock_back_search(
39     Operation	*op,
40     SlapReply	*rs )
41 {
42 	struct sockinfo	*si = (struct sockinfo *) op->o_bd->be_private;
43 	FILE			*fp;
44 	AttributeName		*an;
45 
46 	if ( (fp = opensock( si->si_sockpath )) == NULL ) {
47 		send_ldap_error( op, rs, LDAP_OTHER,
48 		    "could not open socket" );
49 		return( -1 );
50 	}
51 
52 	/* write out the request to the search process */
53 	fprintf( fp, "SEARCH\n" );
54 	fprintf( fp, "msgid: %ld\n", (long) op->o_msgid );
55 	sock_print_conn( fp, op->o_conn, si );
56 	sock_print_suffixes( fp, op->o_bd );
57 	fprintf( fp, "base: %s\n", op->o_req_dn.bv_val );
58 	fprintf( fp, "scope: %d\n", op->oq_search.rs_scope );
59 	fprintf( fp, "deref: %d\n", op->oq_search.rs_deref );
60 	fprintf( fp, "sizelimit: %d\n", op->oq_search.rs_slimit );
61 	fprintf( fp, "timelimit: %d\n", op->oq_search.rs_tlimit );
62 	fprintf( fp, "filter: %s\n", op->oq_search.rs_filterstr.bv_val );
63 	fprintf( fp, "attrsonly: %d\n", op->oq_search.rs_attrsonly ? 1 : 0 );
64 	fprintf( fp, "attrs:%s", op->oq_search.rs_attrs == NULL ? " all" : "" );
65 	for ( an = op->oq_search.rs_attrs; an && an->an_name.bv_val; an++ ) {
66 		fprintf( fp, " %s", an->an_name.bv_val );
67 	}
68 	fprintf( fp, "\n\n" );  /* end of attr line plus blank line */
69 
70 	/* read in the results and send them along */
71 	rs->sr_attrs = op->oq_search.rs_attrs;
72 	sock_read_and_send_results( op, rs, fp );
73 
74 	fclose( fp );
75 	return( 0 );
76 }
77