xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/assertion.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: assertion.c,v 1.3 2021/08/14 16:14:55 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2021 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.3 2021/08/14 16:14:55 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
ldap_create_assertion_control_value(LDAP * ld,char * assertion,struct berval * value)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 	ld->ld_errno = LDAP_SUCCESS;
40 
41 	if ( assertion == NULL || assertion[ 0 ] == '\0' ) {
42 		ld->ld_errno = LDAP_PARAM_ERROR;
43 		return ld->ld_errno;
44 	}
45 
46 	if ( value == NULL ) {
47 		ld->ld_errno = LDAP_PARAM_ERROR;
48 		return ld->ld_errno;
49 	}
50 
51 	BER_BVZERO( value );
52 
53 	ber = ldap_alloc_ber_with_options( ld );
54 	if ( ber == NULL ) {
55 		ld->ld_errno = LDAP_NO_MEMORY;
56 		return ld->ld_errno;
57 	}
58 
59 	err = ldap_pvt_put_filter( ber, assertion );
60 	if ( err < 0 ) {
61 		ld->ld_errno = LDAP_ENCODING_ERROR;
62 		goto done;
63 	}
64 
65 	err = ber_flatten2( ber, value, 1 );
66 	if ( err < 0 ) {
67 		ld->ld_errno = LDAP_NO_MEMORY;
68 		goto done;
69 	}
70 
71 done:;
72 	if ( ber != NULL ) {
73 		ber_free( ber, 1 );
74 	}
75 
76 	return ld->ld_errno;
77 }
78 
79 int
ldap_create_assertion_control(LDAP * ld,char * assertion,int iscritical,LDAPControl ** ctrlp)80 ldap_create_assertion_control(
81 	LDAP		*ld,
82 	char		*assertion,
83 	int		iscritical,
84 	LDAPControl	**ctrlp )
85 {
86 	struct berval	value;
87 
88 	if ( ctrlp == NULL ) {
89 		ld->ld_errno = LDAP_PARAM_ERROR;
90 		return ld->ld_errno;
91 	}
92 
93 	ld->ld_errno = ldap_create_assertion_control_value( ld,
94 		assertion, &value );
95 	if ( ld->ld_errno == LDAP_SUCCESS ) {
96 		ld->ld_errno = ldap_control_create( LDAP_CONTROL_ASSERT,
97 			iscritical, &value, 0, ctrlp );
98 		if ( ld->ld_errno != LDAP_SUCCESS ) {
99 			LDAP_FREE( value.bv_val );
100 		}
101 	}
102 
103 	return ld->ld_errno;
104 }
105 
106