Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap / getattr.c
bloba3832fd463cc693f433dd8e8b656fb50412bfda8
1 /* $OpenLDAP: pkg/ldap/libraries/libldap/getattr.c,v 1.35.2.3 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
5 * All rights reserved.
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.
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>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
19 #include "portable.h"
21 #include <stdio.h>
22 #include <ac/stdlib.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
28 #include "ldap-int.h"
30 char *
31 ldap_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **berout )
33 int rc;
34 ber_tag_t tag;
35 ber_len_t len = 0;
36 char *attr;
37 BerElement *ber;
39 Debug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 );
41 assert( ld != NULL );
42 assert( LDAP_VALID( ld ) );
43 assert( entry != NULL );
44 assert( berout != NULL );
46 *berout = NULL;
48 ber = ldap_alloc_ber_with_options( ld );
49 if( ber == NULL ) {
50 return NULL;
53 *ber = *entry->lm_ber;
55 /*
56 * Skip past the sequence, dn, sequence of sequence leaving
57 * us at the first attribute.
60 tag = ber_scanf( ber, "{xl{" /*}}*/, &len );
61 if( tag == LBER_ERROR ) {
62 ld->ld_errno = LDAP_DECODING_ERROR;
63 ber_free( ber, 0 );
64 return NULL;
67 /* set the length to avoid overrun */
68 rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
69 if( rc != LBER_OPT_SUCCESS ) {
70 ld->ld_errno = LDAP_LOCAL_ERROR;
71 ber_free( ber, 0 );
72 return NULL;
75 if ( ber_pvt_ber_remaining( ber ) == 0 ) {
76 assert( len == 0 );
77 ber_free( ber, 0 );
78 return NULL;
80 assert( len != 0 );
82 /* snatch the first attribute */
83 tag = ber_scanf( ber, "{ax}", &attr );
84 if( tag == LBER_ERROR ) {
85 ld->ld_errno = LDAP_DECODING_ERROR;
86 ber_free( ber, 0 );
87 return NULL;
90 *berout = ber;
91 return attr;
94 /* ARGSUSED */
95 char *
96 ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
98 ber_tag_t tag;
99 char *attr;
101 Debug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 );
103 assert( ld != NULL );
104 assert( LDAP_VALID( ld ) );
105 assert( entry != NULL );
106 assert( ber != NULL );
108 if ( ber_pvt_ber_remaining( ber ) == 0 ) {
109 return NULL;
112 /* skip sequence, snarf attribute type, skip values */
113 tag = ber_scanf( ber, "{ax}", &attr );
114 if( tag == LBER_ERROR ) {
115 ld->ld_errno = LDAP_DECODING_ERROR;
116 return NULL;
119 return attr;
122 /* Fetch attribute type and optionally fetch values. The type
123 * and values are referenced in-place from the BerElement, they are
124 * not dup'd into malloc'd memory.
126 /* ARGSUSED */
128 ldap_get_attribute_ber( LDAP *ld, LDAPMessage *entry, BerElement *ber,
129 BerValue *attr, BerVarray *vals )
131 ber_tag_t tag;
132 int rc = LDAP_SUCCESS;
134 Debug( LDAP_DEBUG_TRACE, "ldap_get_attribute_ber\n", 0, 0, 0 );
136 assert( ld != NULL );
137 assert( LDAP_VALID( ld ) );
138 assert( entry != NULL );
139 assert( ber != NULL );
140 assert( attr != NULL );
142 attr->bv_val = NULL;
143 attr->bv_len = 0;
145 if ( ber_pvt_ber_remaining( ber ) ) {
146 ber_len_t siz = sizeof( BerValue );
148 /* skip sequence, snarf attribute type */
149 tag = ber_scanf( ber, vals ? "{mM}" : "{mx}", attr, vals,
150 &siz, 0 );
151 if( tag == LBER_ERROR ) {
152 rc = ld->ld_errno = LDAP_DECODING_ERROR;
156 return rc;