]> git.sur5r.net Git - openldap/blob - libraries/libldap/getattr.c
ITS#897 Internal connection that is closed on one end and about to
[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         int rc;
28         ber_tag_t tag;
29         ber_len_t len = 0;
30         char *attr;
31         BerElement *ber;
32
33         Debug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 );
34
35         assert( ld != NULL );
36         assert( LDAP_VALID( ld ) );
37         assert( entry != NULL );
38         assert( berout != NULL );
39
40         *berout = NULL;
41
42         ber = ldap_alloc_ber_with_options( ld );
43         if( ber == NULL ) {
44                 return NULL;
45         }
46
47         *ber = *entry->lm_ber;
48
49         /* 
50          * Skip past the sequence, dn, sequence of sequence leaving
51          * us at the first attribute.
52          */
53
54         tag = ber_scanf( ber, "{xl{" /*}}*/, &len );
55         if( tag == LBER_ERROR ) {
56                 ld->ld_errno = LDAP_DECODING_ERROR;
57                 ber_free( ber, 0 );
58                 return NULL;
59         }
60
61         /* set the length to avoid overrun */
62         rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
63         if( rc != LBER_OPT_SUCCESS ) {
64                 ld->ld_errno = LDAP_LOCAL_ERROR;
65                 ber_free( ber, 0 );
66                 return NULL;
67         }
68
69         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
70                 assert( len == 0 );
71                 return NULL;
72         }
73         assert( len != 0 );
74
75         /* snatch the first attribute */
76         tag = ber_scanf( ber, "{ax}", &attr );
77         if( tag == LBER_ERROR ) {
78                 ld->ld_errno = LDAP_DECODING_ERROR;
79                 ber_free( ber, 0 );
80                 return NULL;
81         }
82
83         *berout = ber;
84         return attr;
85 }
86
87 /* ARGSUSED */
88 char *
89 ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
90 {
91         ber_tag_t tag;
92         char *attr;
93
94         Debug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 );
95
96         assert( ld != NULL );
97         assert( LDAP_VALID( ld ) );
98         assert( entry != NULL );
99         assert( ber != NULL );
100
101         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
102                 return NULL;
103         }
104
105         /* skip sequence, snarf attribute type, skip values */
106         tag = ber_scanf( ber, "{ax}", &attr ); 
107         if( tag == LBER_ERROR ) {
108                 ld->ld_errno = LDAP_DECODING_ERROR;
109                 return NULL;
110         }
111
112         return attr;
113 }