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