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