1/*	$NetBSD: compare.c,v 1.1.1.3 2010/12/12 15:23:21 adam Exp $	*/
2
3/* compare.c - shell backend compare function */
4/* OpenLDAP: pkg/ldap/servers/slapd/back-shell/compare.c,v 1.28.2.5 2010/04/13 20:23:38 kurt Exp */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 1998-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/* Portions Copyright (c) 1995 Regents of the University of Michigan.
19 * All rights reserved.
20 *
21 * Redistribution and use in source and binary forms are permitted
22 * provided that this notice is preserved and that due credit is given
23 * to the University of Michigan at Ann Arbor. The name of the University
24 * may not be used to endorse or promote products derived from this
25 * software without specific prior written permission. This software
26 * is provided ``as is'' without express or implied warranty.
27 */
28/* ACKNOWLEDGEMENTS:
29 * This work was originally developed by the University of Michigan
30 * (as part of U-MICH LDAP).
31 */
32
33#include "portable.h"
34
35#include <stdio.h>
36
37#include <ac/string.h>
38#include <ac/socket.h>
39
40#include "slap.h"
41#include "shell.h"
42
43int
44shell_back_compare(
45    Operation	*op,
46    SlapReply	*rs )
47{
48	struct shellinfo	*si = (struct shellinfo *) op->o_bd->be_private;
49	AttributeDescription *entry = slap_schema.si_ad_entry;
50	Entry e;
51	FILE			*rfp, *wfp;
52
53	if ( si->si_compare == NULL ) {
54		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
55		    "compare not implemented" );
56		return( -1 );
57	}
58
59	e.e_id = NOID;
60	e.e_name = op->o_req_dn;
61	e.e_nname = op->o_req_ndn;
62	e.e_attrs = NULL;
63	e.e_ocflags = 0;
64	e.e_bv.bv_len = 0;
65	e.e_bv.bv_val = NULL;
66	e.e_private = NULL;
67
68	if ( ! access_allowed( op, &e,
69		entry, NULL, ACL_READ, NULL ) )
70	{
71		send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
72		return -1;
73	}
74
75	if ( forkandexec( si->si_compare, &rfp, &wfp ) == (pid_t)-1 ) {
76		send_ldap_error( op, rs, LDAP_OTHER,
77		    "could not fork/exec" );
78		return( -1 );
79	}
80
81	/*
82	 * FIX ME:  This should use LDIF routines so that binary
83	 *	values are properly dealt with
84	 */
85
86	/* write out the request to the compare process */
87	fprintf( wfp, "COMPARE\n" );
88	fprintf( wfp, "msgid: %ld\n", (long) op->o_msgid );
89	print_suffixes( wfp, op->o_bd );
90	fprintf( wfp, "dn: %s\n", op->o_req_dn.bv_val );
91	fprintf( wfp, "%s: %s\n",
92		op->oq_compare.rs_ava->aa_desc->ad_cname.bv_val,
93		op->oq_compare.rs_ava->aa_value.bv_val /* could be binary! */ );
94	fclose( wfp );
95
96	/* read in the result and send it along */
97	read_and_send_results( op, rs, rfp );
98
99	fclose( rfp );
100	return( 0 );
101}
102