]> git.sur5r.net Git - openldap/blob - libraries/libldap/getattr.c
ITS#791: fix SASL ctx close
[openldap] / libraries / libldap / getattr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  getattr.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <ac/stdlib.h>
17
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include "ldap-int.h"
23
24 char *
25 ldap_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **berout )
26 {
27 #if LBER_SEQORSET_AVOID_OVERRUN
28         int rc;
29 #endif
30         ber_tag_t tag;
31         ber_len_t len;
32         char *attr;
33         BerElement *ber;
34
35         Debug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 );
36
37         assert( ld != NULL );
38         assert( LDAP_VALID( ld ) );
39         assert( entry != NULL );
40         assert( berout != NULL );
41
42         *berout = NULL;
43
44         ber = ldap_alloc_ber_with_options( ld );
45         if( ber == NULL ) {
46                 return NULL;
47         }
48
49         *ber = *entry->lm_ber;
50
51         /* 
52          * Skip past the sequence, dn, sequence of sequence leaving
53          * us at the first attribute.
54          */
55
56         tag = ber_scanf( ber, "{xl{" /*}}*/, &attr, &len );
57         if( tag == LBER_ERROR ) {
58                 ld->ld_errno = LDAP_DECODING_ERROR;
59                 ber_free( ber, 0 );
60                 return  NULL;
61         }
62
63 #if LDAP_SEQORSET_BAILOUT
64         if( len == 0 ) {
65                 return NULL;
66         }
67 #endif
68         
69 #if LBER_SEQORSET_AVOID_OVERRUN
70         /* set the length to avoid overrun */
71         rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
72         if( rc != LBER_OPT_SUCCESS ) {
73                 ld->ld_errno = LDAP_LOCAL_ERROR;
74                 ber_free( ber, 0 );
75                 return NULL;
76         }
77 #endif
78
79         /* snatch the first attribute */
80         tag = ber_scanf( ber, "{ax}", &attr );
81         if( tag == LBER_ERROR ) {
82                 ld->ld_errno = LDAP_DECODING_ERROR;
83                 ber_free( ber, 0 );
84                 return NULL;
85         }
86
87         *berout = ber;
88         return attr;
89 }
90
91 /* ARGSUSED */
92 char *
93 ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
94 {
95         ber_tag_t tag;
96         char *attr;
97
98         Debug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 );
99
100         assert( ld != NULL );
101         assert( LDAP_VALID( ld ) );
102         assert( entry != NULL );
103         assert( ber != NULL );
104
105 #if LDAP_SEQORSET_BAILOUT
106         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
107                 return NULL;
108         }
109 #endif
110
111         /* skip sequence, snarf attribute type, skip values */
112         tag = ber_scanf( ber, "{ax}", &attr ); 
113         if( tag == LBER_ERROR ) {
114                 ld->ld_errno = LDAP_DECODING_ERROR;
115                 return NULL;
116         }
117
118         return attr;
119 }