]> git.sur5r.net Git - openldap/blob - libraries/libldap/getattr.c
Fix ITS#1843, don't deref NULL string in ldap_pvt_str2upper
[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 (( "getattr", LDAP_LEVEL_ENTRY, "ldap_first_attribute\n" ));
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 (( "getattr", LDAP_LEVEL_ENTRY, "ldap_next_attribute\n" ));
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 }