]> git.sur5r.net Git - openldap/blob - libraries/libldap/getattr.c
slashpath from HEAD
[openldap] / libraries / libldap / getattr.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22 #include <ac/stdlib.h>
23
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27
28 #include "ldap-int.h"
29
30 char *
31 ldap_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **berout )
32 {
33         int rc;
34         ber_tag_t tag;
35         ber_len_t len = 0;
36         char *attr;
37         BerElement *ber;
38
39 #ifdef NEW_LOGGING
40         LDAP_LOG ( OPERATION, ENTRY, "ldap_first_attribute\n", 0, 0, 0 );
41 #else
42         Debug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 );
43 #endif
44
45         assert( ld != NULL );
46         assert( LDAP_VALID( ld ) );
47         assert( entry != NULL );
48         assert( berout != NULL );
49
50         *berout = NULL;
51
52         ber = ldap_alloc_ber_with_options( ld );
53         if( ber == NULL ) {
54                 return NULL;
55         }
56
57         *ber = *entry->lm_ber;
58
59         /* 
60          * Skip past the sequence, dn, sequence of sequence leaving
61          * us at the first attribute.
62          */
63
64         tag = ber_scanf( ber, "{xl{" /*}}*/, &len );
65         if( tag == LBER_ERROR ) {
66                 ld->ld_errno = LDAP_DECODING_ERROR;
67                 ber_free( ber, 0 );
68                 return NULL;
69         }
70
71         /* set the length to avoid overrun */
72         rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
73         if( rc != LBER_OPT_SUCCESS ) {
74                 ld->ld_errno = LDAP_LOCAL_ERROR;
75                 ber_free( ber, 0 );
76                 return NULL;
77         }
78
79         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
80                 assert( len == 0 );
81                 ber_free( ber, 0 );
82                 return NULL;
83         }
84         assert( len != 0 );
85
86         /* snatch the first attribute */
87         tag = ber_scanf( ber, "{ax}", &attr );
88         if( tag == LBER_ERROR ) {
89                 ld->ld_errno = LDAP_DECODING_ERROR;
90                 ber_free( ber, 0 );
91                 return NULL;
92         }
93
94         *berout = ber;
95         return attr;
96 }
97
98 /* ARGSUSED */
99 char *
100 ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
101 {
102         ber_tag_t tag;
103         char *attr;
104
105 #ifdef NEW_LOGGING
106         LDAP_LOG ( OPERATION, ENTRY, "ldap_next_attribute\n", 0, 0, 0 );
107 #else
108         Debug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 );
109 #endif
110
111         assert( ld != NULL );
112         assert( LDAP_VALID( ld ) );
113         assert( entry != NULL );
114         assert( ber != NULL );
115
116         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
117                 return NULL;
118         }
119
120         /* skip sequence, snarf attribute type, skip values */
121         tag = ber_scanf( ber, "{ax}", &attr ); 
122         if( tag == LBER_ERROR ) {
123                 ld->ld_errno = LDAP_DECODING_ERROR;
124                 return NULL;
125         }
126
127         return attr;
128 }
129
130 /* Fetch attribute type and optionally fetch values. The type
131  * and values are referenced in-place from the BerElement, they are
132  * not dup'd into malloc'd memory.
133  */
134 /* ARGSUSED */
135 int
136 ldap_get_attribute_ber( LDAP *ld, LDAPMessage *entry, BerElement *ber,
137         BerValue *attr, BerVarray *vals )
138 {
139         ber_tag_t tag;
140         int rc = LDAP_SUCCESS;
141
142 #ifdef NEW_LOGGING
143         LDAP_LOG ( OPERATION, ENTRY, "ldap_get_attribute_ber\n", 0, 0, 0 );
144 #else
145         Debug( LDAP_DEBUG_TRACE, "ldap_get_attribute_ber\n", 0, 0, 0 );
146 #endif
147
148         assert( ld != NULL );
149         assert( LDAP_VALID( ld ) );
150         assert( entry != NULL );
151         assert( ber != NULL );
152         assert( attr != NULL );
153
154         attr->bv_val = NULL;
155         attr->bv_len = 0;
156
157         if ( ber_pvt_ber_remaining( ber ) ) {
158                 ber_len_t siz = sizeof( BerValue );
159
160                 /* skip sequence, snarf attribute type */
161                 tag = ber_scanf( ber, vals ? "{mM}" : "{mx}", attr, vals,
162                         &siz, 0 ); 
163                 if( tag == LBER_ERROR ) {
164                         rc = ld->ld_errno = LDAP_DECODING_ERROR;
165                 }
166         }
167
168         return rc;
169 }