]> git.sur5r.net Git - openldap/blob - libraries/librewrite/session.c
More contrib cleanout
[openldap] / libraries / librewrite / session.c
1 /******************************************************************************
2  *
3  * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
4  * All rights reserved.
5  *
6  * Permission is granted to anyone to use this software for any purpose
7  * on any computer system, and to alter it and redistribute it, subject
8  * to the following restrictions:
9  *
10  * 1. The author is not responsible for the consequences of use of this
11  * software, no matter how awful, even if they arise from flaws in it.
12  *
13  * 2. The origin of this software must not be misrepresented, either by
14  * explicit claim or by omission.  Since few users ever read sources,
15  * credits should appear in the documentation.
16  *
17  * 3. Altered versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.  Since few users
19  * ever read sources, credits should appear in the documentation.
20  * 
21  * 4. This notice may not be removed or altered.
22  *
23  ******************************************************************************/
24
25 #include <portable.h>
26
27 #include "rewrite-int.h"
28
29 /*
30  * Compares two cookies
31  */
32 static int
33 rewrite_cookie_cmp(
34                 const void *c1,
35                 const void *c2
36 )
37 {
38         struct rewrite_session *s1, *s2;
39
40         s1 = ( struct rewrite_session * )c1;
41         s2 = ( struct rewrite_session * )c2;
42
43         assert( s1 != NULL );
44         assert( s2 != NULL );
45         assert( s1->ls_cookie != NULL );
46         assert( s2->ls_cookie != NULL );
47         
48         return ( ( s1->ls_cookie < s2->ls_cookie ) ? -1 :
49                         ( ( s1->ls_cookie > s2->ls_cookie ) ? 1 : 0 ) );
50 }
51
52 /*
53  * Duplicate cookies?
54  */
55 static int
56 rewrite_cookie_dup(
57                 void *c1,
58                 void *c2
59 )
60 {
61         struct rewrite_session *s1, *s2;
62
63         s1 = ( struct rewrite_session * )c1;
64         s2 = ( struct rewrite_session * )c2;
65         
66         assert( s1 != NULL );
67         assert( s2 != NULL );
68         assert( s1->ls_cookie != NULL );
69         assert( s2->ls_cookie != NULL );
70         
71         return ( ( s1->ls_cookie == s2->ls_cookie ) ? -1 : 0 );
72 }
73
74 /*
75  * Inits a session
76  */
77 struct rewrite_session *
78 rewrite_session_init(
79                 struct rewrite_info *info,
80                 const void *cookie
81 )
82 {
83         struct rewrite_session *session;
84         int rc;
85
86         assert( info != NULL );
87         assert( cookie != NULL );
88         
89         session = calloc( sizeof( struct rewrite_session ), 1 );
90         if ( session == NULL ) {
91                 return NULL;
92         }
93         session->ls_cookie = ( void * )cookie;
94         
95 #ifdef USE_REWRITE_LDAP_PVT_THREADS
96         if ( ldap_pvt_thread_rdwr_init( &session->ls_vars_mutex ) ) {
97                 free( session );
98                 return NULL;
99         }
100         ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
101 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
102
103         rc = avl_insert( &info->li_cookies, ( caddr_t )session,
104                         rewrite_cookie_cmp, rewrite_cookie_dup );
105         info->li_num_cookies++;
106
107 #ifdef USE_REWRITE_LDAP_PVT_THREADS
108         ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
109 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
110         
111         if ( rc != 0 ) {
112                 free( session );
113                 return NULL;
114         }
115         
116         return session;
117 }
118
119 /*
120  * Fetches a session
121  */
122 struct rewrite_session *
123 rewrite_session_find(
124                 struct rewrite_info *info,
125                 const void *cookie
126 )
127 {
128         struct rewrite_session *session, tmp;
129
130         assert( info != NULL );
131         assert( cookie != NULL );
132         
133         tmp.ls_cookie = ( void * )cookie;
134 #ifdef USE_REWRITE_LDAP_PVT_THREADS
135         ldap_pvt_thread_rdwr_rlock( &info->li_cookies_mutex );
136 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
137         session = ( struct rewrite_session * )avl_find( info->li_cookies,
138                         ( caddr_t )&tmp, rewrite_cookie_cmp );
139 #ifdef USE_REWRITE_LDAP_PVT_THREADS
140         ldap_pvt_thread_rdwr_runlock( &info->li_cookies_mutex );
141 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
142
143         return session;
144                 
145 }
146
147 /*
148  * Defines and inits a var with session scope
149  */
150 int
151 rewrite_session_var_set(
152                 struct rewrite_info *info,
153                 const void *cookie,
154                 const char *name,
155                 const char *value
156 )
157 {
158         struct rewrite_session *session;
159         struct rewrite_var *var;
160
161         assert( info != NULL );
162         assert( cookie != NULL );
163         assert( name != NULL );
164         assert( value != NULL );
165
166         session = rewrite_session_find( info, cookie );
167         if ( session == NULL ) {
168                 session = rewrite_session_init( info, cookie );
169         }
170
171 #ifdef USE_REWRITE_LDAP_PVT_THREADS
172         ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
173 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
174
175         var = rewrite_var_find( session->ls_vars, name );
176         if ( var != NULL ) {
177                 assert( var->lv_value.bv_val != NULL );
178                 free( var->lv_value.bv_val );
179                 var->lv_value.bv_val = strdup( value );
180                 var->lv_value.bv_len = strlen( value );
181         } else {
182                 var = rewrite_var_insert( &session->ls_vars, name, value );
183                 if ( var == NULL ) {
184 #ifdef USE_REWRITE_LDAP_PVT_THREADS
185                         ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
186 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
187                         return REWRITE_ERR;
188                 }
189         }       
190         
191 #ifdef USE_REWRITE_LDAP_PVT_THREADS
192         ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
193 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
194
195         return REWRITE_SUCCESS;
196 }
197
198 /*
199  * Gets a var with session scope
200  */
201 int
202 rewrite_session_var_get(
203                 struct rewrite_info *info,
204                 const void *cookie,
205                 const char *name,
206                 struct berval *value
207 )
208 {
209         struct rewrite_session *session;
210         struct rewrite_var *var;
211
212         assert( info != NULL );
213         assert( cookie != NULL );
214         assert( name != NULL );
215         assert( value != NULL );
216
217         value->bv_val = NULL;
218         value->bv_len = 0;
219         
220         if ( cookie == NULL ) {
221                 return REWRITE_ERR;
222         }
223
224         session = rewrite_session_find( info, cookie );
225         if ( session == NULL ) {
226                 return REWRITE_ERR;
227         }
228
229 #ifdef USE_REWRITE_LDAP_PVT_THREADS
230         ldap_pvt_thread_rdwr_rlock( &session->ls_vars_mutex );
231 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
232         
233         var = rewrite_var_find( session->ls_vars, name );
234         if ( var == NULL ) {
235                 
236 #ifdef USE_REWRITE_LDAP_PVT_THREADS
237                 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
238 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
239                 
240                 return REWRITE_ERR;
241         } else {
242                 value->bv_val = strdup( var->lv_value.bv_val );
243                 value->bv_len = var->lv_value.bv_len;
244         }
245         
246 #ifdef USE_REWRITE_LDAP_PVT_THREADS
247         ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
248 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
249         
250         return REWRITE_SUCCESS;
251 }
252
253 /*
254  * Deletes a session
255  */
256 int
257 rewrite_session_delete(
258                 struct rewrite_info *info,
259                 const void *cookie
260 )
261 {
262         struct rewrite_session *session, tmp;
263
264         assert( info != NULL );
265         assert( cookie != NULL );
266
267         tmp.ls_cookie = ( void * )cookie;
268         
269         session = rewrite_session_find( info, cookie );
270
271         if ( session != NULL ) {
272 #ifdef USE_REWRITE_LDAP_PVT_THREADS
273                 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
274 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
275                 rewrite_var_delete( session->ls_vars );
276 #ifdef USE_REWRITE_LDAP_PVT_THREADS
277                 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
278 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
279         }
280
281 #ifdef USE_REWRITE_LDAP_PVT_THREADS
282         ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
283 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
284
285         assert( info->li_num_cookies > 0 );
286         info->li_num_cookies--;
287         
288         /*
289          * There is nothing to delete in the return value
290          */
291         avl_delete( &info->li_cookies, ( caddr_t )&tmp, rewrite_cookie_cmp );
292 #ifdef USE_REWRITE_LDAP_PVT_THREADS
293         ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
294 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
295
296         return REWRITE_SUCCESS;
297 }
298
299 /*
300  * Destroys the cookie tree
301  */
302 int
303 rewrite_session_destroy(
304                 struct rewrite_info *info
305 )
306 {
307         int count;
308
309         assert( info != NULL );
310         
311 #ifdef USE_REWRITE_LDAP_PVT_THREADS
312         ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
313 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
314
315         /*
316          * Should call per-session destruction routine ...
317          */
318         
319         count = avl_free( info->li_cookies, NULL );
320         info->li_cookies = NULL;
321         assert( count == info->li_num_cookies );
322         info->li_num_cookies = 0;
323
324 #ifdef USE_REWRITE_LDAP_PVT_THREADS
325         ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
326 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
327
328         return REWRITE_SUCCESS;
329 }
330