]> git.sur5r.net Git - openldap/blob - libraries/librewrite/session.c
0cffaeea3af9c3a5ca7838b84aa5111c97a6fdb7
[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 <ac/string.h>
28
29 #include "rewrite-int.h"
30
31 /*
32  * Compares two cookies
33  */
34 static int
35 rewrite_cookie_cmp(
36                 const void *c1,
37                 const void *c2
38 )
39 {
40         struct rewrite_session *s1, *s2;
41
42         s1 = ( struct rewrite_session * )c1;
43         s2 = ( struct rewrite_session * )c2;
44
45         assert( s1 != NULL );
46         assert( s2 != NULL );
47         assert( s1->ls_cookie != NULL );
48         assert( s2->ls_cookie != NULL );
49         
50         return ( ( s1->ls_cookie < s2->ls_cookie ) ? -1 :
51                         ( ( s1->ls_cookie > s2->ls_cookie ) ? 1 : 0 ) );
52 }
53
54 /*
55  * Duplicate cookies?
56  */
57 static int
58 rewrite_cookie_dup(
59                 void *c1,
60                 void *c2
61 )
62 {
63         struct rewrite_session *s1, *s2;
64
65         s1 = ( struct rewrite_session * )c1;
66         s2 = ( struct rewrite_session * )c2;
67         
68         assert( s1 != NULL );
69         assert( s2 != NULL );
70         assert( s1->ls_cookie != NULL );
71         assert( s2->ls_cookie != NULL );
72         
73         return ( ( s1->ls_cookie == s2->ls_cookie ) ? -1 : 0 );
74 }
75
76 /*
77  * Inits a session
78  */
79 struct rewrite_session *
80 rewrite_session_init(
81                 struct rewrite_info *info,
82                 const void *cookie
83 )
84 {
85         struct rewrite_session *session;
86         int rc;
87
88         assert( info != NULL );
89         assert( cookie != NULL );
90         
91         session = calloc( sizeof( struct rewrite_session ), 1 );
92         if ( session == NULL ) {
93                 return NULL;
94         }
95         session->ls_cookie = ( void * )cookie;
96         
97 #ifdef USE_REWRITE_LDAP_PVT_THREADS
98         if ( ldap_pvt_thread_rdwr_init( &session->ls_vars_mutex ) ) {
99                 free( session );
100                 return NULL;
101         }
102         ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
103 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
104
105         rc = avl_insert( &info->li_cookies, ( caddr_t )session,
106                         rewrite_cookie_cmp, rewrite_cookie_dup );
107         info->li_num_cookies++;
108
109 #ifdef USE_REWRITE_LDAP_PVT_THREADS
110         ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
111 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
112         
113         if ( rc != 0 ) {
114                 free( session );
115                 return NULL;
116         }
117         
118         return session;
119 }
120
121 /*
122  * Fetches a session
123  */
124 struct rewrite_session *
125 rewrite_session_find(
126                 struct rewrite_info *info,
127                 const void *cookie
128 )
129 {
130         struct rewrite_session *session, tmp;
131
132         assert( info != NULL );
133         assert( cookie != NULL );
134         
135         tmp.ls_cookie = ( void * )cookie;
136 #ifdef USE_REWRITE_LDAP_PVT_THREADS
137         ldap_pvt_thread_rdwr_rlock( &info->li_cookies_mutex );
138 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
139         session = ( struct rewrite_session * )avl_find( info->li_cookies,
140                         ( caddr_t )&tmp, rewrite_cookie_cmp );
141 #ifdef USE_REWRITE_LDAP_PVT_THREADS
142         ldap_pvt_thread_rdwr_runlock( &info->li_cookies_mutex );
143 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
144
145         return session;
146                 
147 }
148
149 /*
150  * Defines and inits a var with session scope
151  */
152 int
153 rewrite_session_var_set(
154                 struct rewrite_info *info,
155                 const void *cookie,
156                 const char *name,
157                 const char *value
158 )
159 {
160         struct rewrite_session *session;
161         struct rewrite_var *var;
162
163         assert( info != NULL );
164         assert( cookie != NULL );
165         assert( name != NULL );
166         assert( value != NULL );
167
168         session = rewrite_session_find( info, cookie );
169         if ( session == NULL ) {
170                 session = rewrite_session_init( info, cookie );
171         }
172
173 #ifdef USE_REWRITE_LDAP_PVT_THREADS
174         ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
175 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
176
177         var = rewrite_var_find( session->ls_vars, name );
178         if ( var != NULL ) {
179                 assert( var->lv_value.bv_val != NULL );
180                 free( var->lv_value.bv_val );
181                 var->lv_value.bv_val = strdup( value );
182                 var->lv_value.bv_len = strlen( value );
183         } else {
184                 var = rewrite_var_insert( &session->ls_vars, name, value );
185                 if ( var == NULL ) {
186 #ifdef USE_REWRITE_LDAP_PVT_THREADS
187                         ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
188 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
189                         return REWRITE_ERR;
190                 }
191         }       
192         
193 #ifdef USE_REWRITE_LDAP_PVT_THREADS
194         ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
195 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
196
197         return REWRITE_SUCCESS;
198 }
199
200 /*
201  * Gets a var with session scope
202  */
203 int
204 rewrite_session_var_get(
205                 struct rewrite_info *info,
206                 const void *cookie,
207                 const char *name,
208                 struct berval *value
209 )
210 {
211         struct rewrite_session *session;
212         struct rewrite_var *var;
213
214         assert( info != NULL );
215         assert( cookie != NULL );
216         assert( name != NULL );
217         assert( value != NULL );
218
219         value->bv_val = NULL;
220         value->bv_len = 0;
221         
222         if ( cookie == NULL ) {
223                 return REWRITE_ERR;
224         }
225
226         session = rewrite_session_find( info, cookie );
227         if ( session == NULL ) {
228                 return REWRITE_ERR;
229         }
230
231 #ifdef USE_REWRITE_LDAP_PVT_THREADS
232         ldap_pvt_thread_rdwr_rlock( &session->ls_vars_mutex );
233 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
234         
235         var = rewrite_var_find( session->ls_vars, name );
236         if ( var == NULL ) {
237                 
238 #ifdef USE_REWRITE_LDAP_PVT_THREADS
239                 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
240 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
241                 
242                 return REWRITE_ERR;
243         } else {
244                 value->bv_val = strdup( var->lv_value.bv_val );
245                 value->bv_len = var->lv_value.bv_len;
246         }
247         
248 #ifdef USE_REWRITE_LDAP_PVT_THREADS
249         ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
250 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
251         
252         return REWRITE_SUCCESS;
253 }
254
255 /*
256  * Deletes a session
257  */
258 int
259 rewrite_session_delete(
260                 struct rewrite_info *info,
261                 const void *cookie
262 )
263 {
264         struct rewrite_session *session, tmp;
265
266         assert( info != NULL );
267         assert( cookie != NULL );
268
269         tmp.ls_cookie = ( void * )cookie;
270         
271         session = rewrite_session_find( info, cookie );
272
273         if ( session != NULL ) {
274 #ifdef USE_REWRITE_LDAP_PVT_THREADS
275                 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
276 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
277                 rewrite_var_delete( session->ls_vars );
278 #ifdef USE_REWRITE_LDAP_PVT_THREADS
279                 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
280 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
281         }
282
283 #ifdef USE_REWRITE_LDAP_PVT_THREADS
284         ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
285 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
286
287         assert( info->li_num_cookies > 0 );
288         info->li_num_cookies--;
289         
290         /*
291          * There is nothing to delete in the return value
292          */
293         avl_delete( &info->li_cookies, ( caddr_t )&tmp, rewrite_cookie_cmp );
294 #ifdef USE_REWRITE_LDAP_PVT_THREADS
295         ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
296 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
297
298         return REWRITE_SUCCESS;
299 }
300
301 /*
302  * Destroys the cookie tree
303  */
304 int
305 rewrite_session_destroy(
306                 struct rewrite_info *info
307 )
308 {
309         int count;
310
311         assert( info != NULL );
312         
313 #ifdef USE_REWRITE_LDAP_PVT_THREADS
314         ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
315 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
316
317         /*
318          * Should call per-session destruction routine ...
319          */
320         
321         count = avl_free( info->li_cookies, NULL );
322         info->li_cookies = NULL;
323         assert( count == info->li_num_cookies );
324         info->li_num_cookies = 0;
325
326 #ifdef USE_REWRITE_LDAP_PVT_THREADS
327         ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
328 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
329
330         return REWRITE_SUCCESS;
331 }
332