1 /*	$NetBSD: str2filter.c,v 1.3 2021/08/14 16:14:58 christos Exp $	*/
2 
3 /* str2filter.c - parse an RFC 4515 string filter */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-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 /* 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 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: str2filter.c,v 1.3 2021/08/14 16:14:58 christos Exp $");
31 
32 #include "portable.h"
33 
34 #include <stdio.h>
35 
36 #include <ac/string.h>
37 #include <ac/ctype.h>
38 #include <ac/socket.h>
39 
40 #include "slap.h"
41 
42 
43 Filter *
str2filter_x(Operation * op,const char * str)44 str2filter_x( Operation *op, const char *str )
45 {
46 	int rc;
47 	Filter	*f = NULL;
48 	BerElementBuffer berbuf;
49 	BerElement *ber = (BerElement *)&berbuf;
50 	const char *text = NULL;
51 
52 	Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str );
53 
54 	if ( str == NULL || *str == '\0' ) {
55 		return NULL;
56 	}
57 
58 	ber_init2( ber, NULL, LBER_USE_DER );
59 	if ( op->o_tmpmemctx ) {
60 		ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
61 	}
62 
63 	rc = ldap_pvt_put_filter( ber, str );
64 	if( rc < 0 ) {
65 		goto done;
66 	}
67 
68 	ber_reset( ber, 1 );
69 
70 	rc = get_filter( op, ber, &f, &text );
71 
72 done:
73 	ber_free_buf( ber );
74 
75 	return f;
76 }
77 
78 Filter *
str2filter(const char * str)79 str2filter( const char *str )
80 {
81 	Operation op = {0};
82 	Opheader ohdr = {0};
83 
84 	op.o_hdr = &ohdr;
85 	op.o_tmpmemctx = NULL;
86 	op.o_tmpmfuncs = &ch_mfuncs;
87 
88 	return str2filter_x( &op, str );
89 }
90