]> git.sur5r.net Git - openldap/blob - servers/ldapd/util.c
ITS#2762: header include fix
[openldap] / servers / ldapd / util.c
1 /*
2  * Copyright (c) 1990 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <sys/errno.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <quipu/commonarg.h>
21 #include <quipu/ds_error.h>
22 #include "lber.h"
23 #include "ldap.h"
24 #include "common.h"
25
26 /*
27  * Print arbitrary stuff, for debugging.
28  */
29
30
31 #define BPLEN   48
32
33 void
34 bprint( char *data, int len )
35 {
36     static char hexdig[] = "0123456789abcdef";
37     char        out[ BPLEN ];
38     int         i = 0;
39
40     (void) memset( out, 0, BPLEN );
41     for ( ;; ) {
42         if ( len < 1 ) {
43             printf( "\t%s\n", ( i == 0 ) ? "(end)" : out );
44             break;
45         }
46
47         if ( isgraph( (unsigned char)*data )) {
48             out[ i ] = ' ';
49             out[ i+1 ] = *data;
50         } else {
51             out[ i ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
52             out[ i+1 ] = hexdig[ *data & 0x0f ];
53         }
54         i += 2;
55         len--;
56         data++;
57
58         if ( i > BPLEN - 2 ) {
59             printf( "\t%s\n", out );
60             (void) memset( out, 0, BPLEN );
61             i = 0;
62             continue;
63         }
64         out[ i++ ] = ' ';
65     }
66 }
67
68 void charlist_free( char **cl )
69 {
70         int     i;
71
72         if ( cl == NULL )
73                 return;
74
75         for ( i = 0; cl[i] != NULL; i++ )
76                 free( cl[i] );
77         free( (char *) cl );
78 }
79
80 int
81 get_ava( BerElement *ber, AVA *tava )
82 {
83         char                    *type, *value;
84         extern short            ldap_dn_syntax;
85
86         Debug( LDAP_DEBUG_TRACE, "get_ava\n", 0, 0, 0 );
87
88         /*
89          * An AVA looks like this:
90          *      AttributeValueAsertion ::= SEQUENCE {
91          *              attributeType   AttributeType,
92          *              attributeValue  AttributeValue
93          *      }
94          */
95
96         if ( ber_scanf( ber, "{aa}", &type, &value ) == LBER_ERROR )
97                 return( LDAP_PROTOCOL_ERROR );
98
99         if ( (tava->ava_type = str2AttrT( type )) == NULLAttrT ) {
100                 free( type );
101                 free( value );
102                 return( LDAP_UNDEFINED_TYPE );
103         }
104
105         if ( (tava->ava_value = ldap_str2AttrV( value,
106             tava->ava_type->oa_syntax )) == NULLAttrV ) {
107                 free( type );
108                 free( value );
109                 return( LDAP_INVALID_SYNTAX );
110         }
111
112         free( type );
113         free( value );
114
115         return( 0 );
116 }
117
118 int
119 chase_referral(
120     Sockbuf             *clientsb,
121     struct msg          *m,
122     struct DSError      *err,
123     char                **matched
124 )
125 {
126         ContinuationRef         cr;
127         struct access_point     *ap;
128         int                     rc, bound;
129         struct conn             *save, *dup, *found;
130         struct PSAPaddr         *psap_cpy();
131
132         Debug( LDAP_DEBUG_TRACE, "chase_referral\n", 0, 0, 0 );
133
134         save = m->m_conn;
135         dup = conn_dup( m->m_conn );
136         m->m_conn = dup;
137         m->m_conn->c_ad = -1;
138
139         /* for each dsa candidate */
140         rc = LDAP_OTHER;
141         for ( cr = err->ERR_REFERRAL.DSE_ref_candidates;
142             cr != NULLCONTINUATIONREF; cr = cr->cr_next ) {
143
144                 /* for each access point listed for the dsa */
145                 for ( ap = cr->cr_accesspoints; ap != NULLACCESSPOINT;
146                     ap = ap->ap_next ) {
147 #ifdef LDAP_DEBUG
148                         if ( ldap_debug & LDAP_DEBUG_ARGS ) {
149                                 char    *str;
150
151                                 str = paddr2str( ap->ap_address, NULLNA );
152                                 fprintf( stderr, "Referring to (%s)...\n",
153                                     str );
154                         }
155 #endif
156
157                         if ( m->m_conn->c_paddr )
158                                 free( (char *) m->m_conn->c_paddr );
159                         m->m_conn->c_paddr = psap_cpy( ap->ap_address );
160
161                         if ( (found = conn_find( m->m_conn )) != NULL ) {
162                                 conn_free( m->m_conn );
163                                 m->m_conn = found;
164                                 m->m_conn->c_refcnt++;
165                                 conn_free( save );
166                                 return( LDAP_SUCCESS );
167                         }
168
169                         rc = do_bind_real( m->m_conn, &bound, matched );
170
171                         if ( rc == LDAP_SUCCESS ) {
172                                 conn_free( save );
173                                 conn_add( m->m_conn );
174                                 return( LDAP_SUCCESS );
175                         }
176                 }
177
178         }
179
180         /* so the conn can be found and freed later */
181         conn_free( m->m_conn );
182         m->m_conn = save;
183
184         return( rc );
185 }