xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/assertion.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: assertion.c,v 1.1.1.6 2018/02/06 01:53:08 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2017 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 
18 #include <sys/cdefs.h>
19 __RCSID("$NetBSD: assertion.c,v 1.1.1.6 2018/02/06 01:53:08 christos Exp $");
20 
21 #include "portable.h"
22 
23 #include <stdio.h>
24 #include <ac/stdlib.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27 
28 #include "ldap-int.h"
29 
30 int
31 ldap_create_assertion_control_value(
32 	LDAP		*ld,
33 	char		*assertion,
34 	struct berval	*value )
35 {
36 	BerElement		*ber = NULL;
37 	int			err;
38 
39 	if ( assertion == NULL || assertion[ 0 ] == '\0' ) {
40 		ld->ld_errno = LDAP_PARAM_ERROR;
41 		return ld->ld_errno;
42 	}
43 
44 	if ( value == NULL ) {
45 		ld->ld_errno = LDAP_PARAM_ERROR;
46 		return ld->ld_errno;
47 	}
48 
49 	BER_BVZERO( value );
50 
51 	ber = ldap_alloc_ber_with_options( ld );
52 	if ( ber == NULL ) {
53 		ld->ld_errno = LDAP_NO_MEMORY;
54 		return ld->ld_errno;
55 	}
56 
57 	err = ldap_pvt_put_filter( ber, assertion );
58 	if ( err < 0 ) {
59 		ld->ld_errno = LDAP_ENCODING_ERROR;
60 		goto done;
61 	}
62 
63 	err = ber_flatten2( ber, value, 1 );
64 	if ( err < 0 ) {
65 		ld->ld_errno = LDAP_NO_MEMORY;
66 		goto done;
67 	}
68 
69 done:;
70 	if ( ber != NULL ) {
71 		ber_free( ber, 1 );
72 	}
73 
74 	return ld->ld_errno;
75 }
76 
77 int
78 ldap_create_assertion_control(
79 	LDAP		*ld,
80 	char		*assertion,
81 	int		iscritical,
82 	LDAPControl	**ctrlp )
83 {
84 	struct berval	value;
85 
86 	if ( ctrlp == NULL ) {
87 		ld->ld_errno = LDAP_PARAM_ERROR;
88 		return ld->ld_errno;
89 	}
90 
91 	ld->ld_errno = ldap_create_assertion_control_value( ld,
92 		assertion, &value );
93 	if ( ld->ld_errno == LDAP_SUCCESS ) {
94 		ld->ld_errno = ldap_control_create( LDAP_CONTROL_ASSERT,
95 			iscritical, &value, 0, ctrlp );
96 		if ( ld->ld_errno != LDAP_SUCCESS ) {
97 			LDAP_FREE( value.bv_val );
98 		}
99 	}
100 
101 	return ld->ld_errno;
102 }
103 
104