1 /*	$NetBSD: assertion.c,v 1.1.1.3 2010/12/12 15:21:29 adam Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/libraries/libldap/assertion.c,v 1.1.2.3 2010/04/13 20:22:55 kurt Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2010 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 "portable.h"
19 
20 #include <stdio.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24 
25 #include "ldap-int.h"
26 
27 int
28 ldap_create_assertion_control_value(
29 	LDAP		*ld,
30 	char		*assertion,
31 	struct berval	*value )
32 {
33 	BerElement		*ber = NULL;
34 	int			err;
35 
36 	if ( assertion == NULL || assertion[ 0 ] == '\0' ) {
37 		ld->ld_errno = LDAP_PARAM_ERROR;
38 		return ld->ld_errno;
39 	}
40 
41 	if ( value == NULL ) {
42 		ld->ld_errno = LDAP_PARAM_ERROR;
43 		return ld->ld_errno;
44 	}
45 
46 	BER_BVZERO( value );
47 
48 	ber = ldap_alloc_ber_with_options( ld );
49 	if ( ber == NULL ) {
50 		ld->ld_errno = LDAP_NO_MEMORY;
51 		return ld->ld_errno;
52 	}
53 
54 	err = ldap_pvt_put_filter( ber, assertion );
55 	if ( err < 0 ) {
56 		ld->ld_errno = LDAP_ENCODING_ERROR;
57 		goto done;
58 	}
59 
60 	err = ber_flatten2( ber, value, 1 );
61 	if ( err < 0 ) {
62 		ld->ld_errno = LDAP_NO_MEMORY;
63 		goto done;
64 	}
65 
66 done:;
67 	if ( ber != NULL ) {
68 		ber_free( ber, 1 );
69 	}
70 
71 	return ld->ld_errno;
72 }
73 
74 int
75 ldap_create_assertion_control(
76 	LDAP		*ld,
77 	char		*assertion,
78 	int		iscritical,
79 	LDAPControl	**ctrlp )
80 {
81 	struct berval	value;
82 
83 	if ( ctrlp == NULL ) {
84 		ld->ld_errno = LDAP_PARAM_ERROR;
85 		return ld->ld_errno;
86 	}
87 
88 	ld->ld_errno = ldap_create_assertion_control_value( ld,
89 		assertion, &value );
90 	if ( ld->ld_errno == LDAP_SUCCESS ) {
91 		ld->ld_errno = ldap_control_create( LDAP_CONTROL_ASSERT,
92 			iscritical, &value, 0, ctrlp );
93 		if ( ld->ld_errno != LDAP_SUCCESS ) {
94 			LDAP_FREE( value.bv_val );
95 		}
96 	}
97 
98 	return ld->ld_errno;
99 }
100 
101