]> git.sur5r.net Git - openldap/blob - libraries/libldap/getattr.c
Merged ldap_get_values_ber into ldap_get_attribute_ber.
[openldap] / libraries / libldap / getattr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 #ifdef NEW_LOGGING
34         LDAP_LOG ( OPERATION, ENTRY, "ldap_first_attribute\n", 0, 0, 0 );
35 #else
36         Debug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 );
37 #endif
38
39         assert( ld != NULL );
40         assert( LDAP_VALID( ld ) );
41         assert( entry != NULL );
42         assert( berout != NULL );
43
44         *berout = NULL;
45
46         ber = ldap_alloc_ber_with_options( ld );
47         if( ber == NULL ) {
48                 return NULL;
49         }
50
51         *ber = *entry->lm_ber;
52
53         /* 
54          * Skip past the sequence, dn, sequence of sequence leaving
55          * us at the first attribute.
56          */
57
58         tag = ber_scanf( ber, "{xl{" /*}}*/, &len );
59         if( tag == LBER_ERROR ) {
60                 ld->ld_errno = LDAP_DECODING_ERROR;
61                 ber_free( ber, 0 );
62                 return NULL;
63         }
64
65         /* set the length to avoid overrun */
66         rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
67         if( rc != LBER_OPT_SUCCESS ) {
68                 ld->ld_errno = LDAP_LOCAL_ERROR;
69                 ber_free( ber, 0 );
70                 return NULL;
71         }
72
73         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
74                 assert( len == 0 );
75                 ber_free( ber, 0 );
76                 return NULL;
77         }
78         assert( len != 0 );
79
80         /* snatch the first attribute */
81         tag = ber_scanf( ber, "{ax}", &attr );
82         if( tag == LBER_ERROR ) {
83                 ld->ld_errno = LDAP_DECODING_ERROR;
84                 ber_free( ber, 0 );
85                 return NULL;
86         }
87
88         *berout = ber;
89         return attr;
90 }
91
92 /* ARGSUSED */
93 char *
94 ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
95 {
96         ber_tag_t tag;
97         char *attr;
98
99 #ifdef NEW_LOGGING
100         LDAP_LOG ( OPERATION, ENTRY, "ldap_next_attribute\n", 0, 0, 0 );
101 #else
102         Debug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 );
103 #endif
104
105         assert( ld != NULL );
106         assert( LDAP_VALID( ld ) );
107         assert( entry != NULL );
108         assert( ber != NULL );
109
110         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
111                 return NULL;
112         }
113
114         /* skip sequence, snarf attribute type, skip values */
115         tag = ber_scanf( ber, "{ax}", &attr ); 
116         if( tag == LBER_ERROR ) {
117                 ld->ld_errno = LDAP_DECODING_ERROR;
118                 return NULL;
119         }
120
121         return attr;
122 }
123
124 /* Fetch attribute type and optionally fetch values */
125 /* ARGSUSED */
126 int
127 ldap_get_attribute_ber( LDAP *ld, LDAPMessage *entry, BerElement *ber,
128         BerValue *attr, BerVarray *vals )
129 {
130         ber_tag_t tag;
131         int rc = LDAP_SUCCESS;
132
133 #ifdef NEW_LOGGING
134         LDAP_LOG ( OPERATION, ENTRY, "ldap_get_attribute_ber\n", 0, 0, 0 );
135 #else
136         Debug( LDAP_DEBUG_TRACE, "ldap_get_attribute_ber\n", 0, 0, 0 );
137 #endif
138
139         assert( ld != NULL );
140         assert( LDAP_VALID( ld ) );
141         assert( entry != NULL );
142         assert( ber != NULL );
143         assert( attr != NULL );
144
145         attr->bv_val = NULL;
146         attr->bv_len = 0;
147
148         if ( ber_pvt_ber_remaining( ber ) ) {
149                 /* skip sequence, snarf attribute type */
150                 tag = ber_scanf( ber, vals ? "{mW}" : "{mx}", attr, vals ); 
151                 if( tag == LBER_ERROR ) {
152                         rc = ld->ld_errno = LDAP_DECODING_ERROR;
153                 }
154         }
155
156         return rc;
157 }