]> git.sur5r.net Git - openldap/blob - servers/ldapd/association.c
schema definitions from Active Directory.
[openldap] / servers / ldapd / association.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1990 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 #include "portable.h"
15
16 #include <stdio.h>
17
18 #include <ac/errno.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
21 #include <ac/time.h>
22
23 #include <quipu/commonarg.h>
24 #include <quipu/ds_error.h>
25 #include <quipu/dap.h>                  /* get dap_unbind() */
26 #if ISODEPACKAGE == IC
27 #include <ll/isoaddrs.h>
28 #else
29 #include <isoaddrs.h>
30 #endif
31
32 #include "lber.h"
33 #include "ldap.h"
34 #include "common.h"
35
36 #ifdef HAVE_SYS_IOCTL_H 
37 #include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_SYS_FILIO_H 
40 #include <sys/filio.h>
41 #endif
42
43 #ifdef __hpux
44 #define FIOGETOWN       FIOGSAIOOWN
45 #endif
46
47 struct conn     *conns;
48
49 struct conn *
50 conn_dup( struct conn *cn )
51 {
52         struct conn     *new;
53         if ( (new = (struct conn *) malloc( sizeof(struct conn) )) == NULL )
54                 return( NULL );
55
56         *new = *cn;
57         new->c_next = NULL;
58         new->c_time = 0L;
59         new->c_paddr = psap_cpy( cn->c_paddr );
60         new->c_dn = strdup( cn->c_dn );
61         if ( new->c_credlen > 0 ) {
62                 new->c_cred = (char *) malloc( cn->c_credlen );
63                 SAFEMEMCPY( new->c_cred, cn->c_cred, cn->c_credlen );
64         } else {
65                 new->c_cred = "";
66         }
67         new->c_credlen = cn->c_credlen;
68         new->c_refcnt = 1;
69
70         return( new );
71 }
72
73 int
74 conn_init( void )
75 {
76         struct PSAPaddr *addr;
77
78         if ( (conns = (struct conn *) malloc( sizeof(struct conn) )) == NULL ) {
79                 Debug( LDAP_DEBUG_ANY, "conn_init: malloc failed\n", 0, 0, 0 );
80                 return( -1 );
81         }
82
83         conns->c_ad = -1;
84         conns->c_dn = NULL;
85         conns->c_cred = NULL;
86         conns->c_credlen = 0;
87
88         if ( dsa_address == NULL || (addr = str2paddr( dsa_address ))
89             == NULLPA ) {
90                 conns->c_paddr = NULLPA;
91                 Debug( LDAP_DEBUG_ANY, "conn_init: bad DSA address (%s)\n",
92                     dsa_address ? dsa_address : "NULL", 0, 0 );
93         } else {
94             conns->c_paddr = psap_cpy( addr );
95         }
96
97         conns->c_refcnt = 1;    /* this conn is never deleted */
98         conns->c_next = NULL;
99
100         return( 0 );
101 }
102
103 void
104 conn_free( struct conn *conn )
105 {
106         struct timeval  tv;
107
108         Debug( LDAP_DEBUG_TRACE, "conn_free (%s): refcnt is %d\n",
109             paddr2str( conn->c_paddr, NULLNA ), conn->c_refcnt, 0 );
110
111         if ( --conn->c_refcnt > 0 )
112                 return;
113
114         gettimeofday( &tv, (struct timezone *)NULL );
115         if ( conn->c_time != 0L && (tv.tv_sec - conn->c_time)
116             < referral_connection_timeout ) {
117                 Debug( LDAP_DEBUG_TRACE, "conn_free: referral conn ttl is %d\n",
118                     referral_connection_timeout - (tv.tv_sec - conn->c_time),
119                     0, 0 );
120                 return;
121         }
122
123
124         conn_del( conn );
125
126         if ( conn->c_paddr )
127                 free( (char *) conn->c_paddr );
128         if ( conn->c_dn )
129                 free( conn->c_dn );
130         if ( conn->c_credlen > 0 )
131                 free( conn->c_cred );
132         free( conn );
133 }
134
135 void
136 conn_del( struct conn *conn )
137 {
138         struct conn     *tmp, *prev;
139
140         Debug( LDAP_DEBUG_TRACE, "conn_del (%s)\n",
141             paddr2str( conn->c_paddr, NULLNA ), 0, 0 );
142
143         prev = NULL;
144         for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
145                 if ( tmp == conn )
146                         break;
147                 prev = tmp;
148         }
149
150         if ( tmp == NULL ) {
151                 Debug( LDAP_DEBUG_ANY, "conn_del: cannot find conn\n", 0, 0,
152                     0 );
153                 return;
154         }
155
156         if ( prev == NULL ) {
157                 conns = conns->c_next;  /* delete head of list */
158         } else {
159                 prev->c_next = tmp->c_next;
160         }
161 }
162
163 void
164 conn_setfds( fd_set *fds )
165 {
166         struct conn     *tmp;
167
168         for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
169                 if ( tmp->c_ad != -1 )
170                         FD_SET( tmp->c_ad, fds );
171         }
172 }
173
174 void
175 conn_badfds( void )
176 {
177         struct conn     *tmp;
178
179         for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
180                 if ( isclosed( tmp->c_ad ) ) {
181                         Debug( LDAP_DEBUG_ANY, "conn_badfds: fd %d is bad\n",
182                             tmp->c_ad, 0, 0 );
183                         tmp->c_ad = -1;
184                 }
185         }
186 }
187
188 struct conn *
189 conn_getfd( fd_set *fds )
190 {
191         struct conn     *tmp;
192
193         for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
194                 if ( tmp->c_ad != -1 )
195                         if ( FD_ISSET( tmp->c_ad, fds ) )
196                                 return( tmp );
197         }
198
199         return( NULL );
200 }
201
202 void
203 conn_add( struct conn *new )
204 {
205         struct timeval  tv;
206
207 #ifdef LDAP_DEBUG
208         if ( ldap_debug & LDAP_DEBUG_CONNS ) {
209                 char    *str;
210
211                 str = paddr2str( new->c_paddr, NULLNA );
212                 Debug( LDAP_DEBUG_CONNS, "conn_add: (%s)\n", str, 0, 0 );
213         }
214 #endif
215
216         gettimeofday( &tv, (struct timezone *)NULL );
217         new->c_time = tv.tv_sec;
218         new->c_next = conns;
219         new->c_refcnt = 1;
220         conns = new;
221 }
222
223 static int
224 psap_cmp( struct PSAPaddr *a, struct PSAPaddr *b )
225 {
226         return( bcmp( (char *) a, (char *) b, sizeof(struct PSAPaddr) ) );
227 }
228
229 struct conn *
230 conn_find( struct conn *c )
231 {
232         struct conn     *tmp;
233
234 #ifdef LDAP_DEBUG
235         if ( ldap_debug & LDAP_DEBUG_CONNS ) {
236                 char    *str;
237
238                 str = paddr2str( c->c_paddr, NULLNA );
239                 Debug( LDAP_DEBUG_CONNS, "conn_find: (%s)\n", str, 0, 0 );
240         }
241 #endif
242         for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
243 #ifdef LDAP_DEBUG
244                 if ( ldap_debug & LDAP_DEBUG_CONNS ) {
245                         char    *str;
246
247                         str = paddr2str( tmp->c_paddr, NULLNA );
248                         Debug( LDAP_DEBUG_CONNS, "conn_find: compare to (%s)\n",
249                             str, 0, 0 );
250                 }
251 #endif
252                 if ( psap_cmp( tmp->c_paddr, c->c_paddr ) == 0
253                     && strcmp( tmp->c_dn, c->c_dn ) == 0
254                     && tmp->c_credlen == c->c_credlen
255                     && bcmp( tmp->c_cred, c->c_cred, c->c_credlen ) == 0 ) {
256                         Debug( LDAP_DEBUG_CONNS, "conn_find: found\n", 0,
257                             0, 0 );
258                         return( tmp );
259                 }
260         }
261
262         Debug( LDAP_DEBUG_CONNS, "conn_find: not found\n", 0, 0, 0 );
263         return( NULL );
264 }
265
266 void
267 conn_close( void )
268 {
269         struct conn     *tmp;
270
271         for ( tmp = conns; tmp != NULL; tmp = tmp->c_next ) {
272                 if ( tmp->c_ad != -1 )
273                         dap_unbind( tmp->c_ad );
274         }
275 }
276
277 int
278 isclosed( int ad )
279 {
280         int             o;
281
282         if ( ioctl( ad, FIOGETOWN, &o ) < 0 )
283                 return( errno == EBADF ? 1 : 0 );
284         else
285                 return( 0 );
286 }