]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
Changed ldap_pvt_tls_get_my_dn and ldap_pvt_tls_get_peer_dn to store result
[openldap] / servers / slapd / back-monitor / time.c
1 /* time.c - deal with time subsystem */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37 #include <ac/string.h>
38 #include <ac/time.h>
39
40 #include "slap.h"
41 #include "proto-slap.h"
42 #include "back-monitor.h"
43
44 #ifdef HACK_LOCAL_TIME
45 static int
46 local_time( const struct tm *ztm, long delta, char *buf, size_t len );
47 #endif /* HACK_LOCAL_TIME */
48
49 int
50 monitor_subsys_time_init(
51         BackendDB               *be
52 )
53 {
54         struct monitorinfo      *mi;
55         
56         Entry                   *e, *e_tmp, *e_time;
57         struct monitorentrypriv *mp;
58         char                    buf[1024], ztmbuf[20];
59         struct tm               *ztm;
60 #ifdef HACK_LOCAL_TIME
61         struct tm               *ltm;
62         char                    ltmbuf[20];
63 #endif
64
65         /*
66          * Note: ltmbuf, ltm are used only if HACK_LOCAL_TIME is defined
67          */
68
69         assert( be != NULL );
70
71         mi = ( struct monitorinfo * )be->be_private;
72
73         if ( monitor_cache_get( mi,
74                         &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
75 #ifdef NEW_LOGGING
76                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
77                         "monitor_subsys_time_init: "
78                         "unable to get entry '%s'\n",
79                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
80 #else
81                 Debug( LDAP_DEBUG_ANY,
82                         "monitor_subsys_time_init: "
83                         "unable to get entry '%s'\n%s%s",
84                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 
85                         "", "" );
86 #endif
87                 return( -1 );
88         }
89
90         e_tmp = NULL;
91
92         /*
93          * Start
94          */
95         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
96         ztm = gmtime( &starttime );
97         strftime( ztmbuf, sizeof(ztmbuf), "%Y%m%d%H%M%SZ", ztm );
98 #ifdef HACK_LOCAL_TIME
99         ltm = localtime( &starttime );
100         local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
101 #endif /* HACK_LOCAL_TIME */
102         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
103         snprintf( buf, sizeof( buf ),
104                         "dn: cn=Start,%s\n"
105                         SLAPD_MONITOR_OBJECTCLASSES
106                         "cn: Start\n"
107                         "description: %s\n"
108 #ifdef HACK_LOCAL_TIME
109                         "description;lang-x-local: %s"
110 #endif /* HACK_LOCAL_TIME */
111                         , monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
112                         ztmbuf
113 #ifdef HACK_LOCAL_TIME
114                         , ltmbuf 
115 #endif /* HACK_LOCAL_TIME */
116                         );
117
118         e = str2entry( buf );
119         if ( e == NULL ) {
120 #ifdef NEW_LOGGING
121                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
122                         "monitor_subsys_time_init: "
123                         "unable to create entry 'cn=Start,%s'\n",
124                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
125 #else
126                 Debug( LDAP_DEBUG_ANY,
127                         "monitor_subsys_time_init: "
128                         "unable to create entry 'cn=Start,%s'\n%s%s",
129                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
130                         "", "" );
131 #endif
132                 return( -1 );
133         }
134         
135         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
136         e->e_private = ( void * )mp;
137         mp->mp_next = e_tmp;
138         mp->mp_children = NULL;
139         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
140         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
141                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
142
143         if ( monitor_cache_add( mi, e ) ) {
144 #ifdef NEW_LOGGING
145                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
146                         "monitor_subsys_time_init: "
147                         "unable to add entry 'cn=Start,%s'\n",
148                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
149 #else
150                 Debug( LDAP_DEBUG_ANY,
151                         "monitor_subsys_time_init: "
152                         "unable to add entry 'cn=Start,%s'\n%s%s",
153                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
154                         "", "" );
155 #endif
156                 return( -1 );
157         }
158         
159         e_tmp = e;
160
161         /*
162          * Current
163          */
164         snprintf( buf, sizeof( buf ),
165                         "dn: cn=Current,%s\n"
166                         SLAPD_MONITOR_OBJECTCLASSES
167                         "cn: Current\n"
168                         "description: %s\n"
169 #ifdef HACK_LOCAL_TIME 
170                         "description;lang-x-local: %s"
171 #endif /* HACK_LOCAL_TIME */
172                         , monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
173                         ztmbuf
174 #ifdef HACK_LOCAL_TIME
175                         , ltmbuf
176 #endif /* HACK_LOCAL_TIME */
177                         );
178
179         e = str2entry( buf );
180         if ( e == NULL ) {
181 #ifdef NEW_LOGGING
182                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
183                         "monitor_subsys_time_init: "
184                         "unable to create entry 'cn=Current,%s'\n",
185                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
186 #else
187                 Debug( LDAP_DEBUG_ANY,
188                         "monitor_subsys_time_init: "
189                         "unable to create entry 'cn=Current,%s'\n%s%s",
190                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
191                         "", "" );
192 #endif
193                 return( -1 );
194         }
195         
196         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
197         e->e_private = ( void * )mp;
198         mp->mp_next = e_tmp;
199         mp->mp_children = NULL;
200         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
201         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
202                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
203
204         if ( monitor_cache_add( mi, e ) ) {
205 #ifdef NEW_LOGGING
206                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
207                         "monitor_subsys_time_init: "
208                         "unable to add entry 'cn=Current,%s'\n",
209                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
210 #else
211                 Debug( LDAP_DEBUG_ANY,
212                         "monitor_subsys_time_init: "
213                         "unable to add entry 'cn=Current,%s'\n%s%s",
214                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
215                         "", "" );
216 #endif
217                 return( -1 );
218         }
219         
220         e_tmp = e;
221
222         mp = ( struct monitorentrypriv * )e_time->e_private;
223         mp->mp_children = e_tmp;
224
225         monitor_cache_release( mi, e_time );
226
227         return( 0 );
228 }
229
230 int
231 monitor_subsys_time_update(
232         struct monitorinfo      *mi,
233         Entry                   *e
234 )
235 {
236         char            ztmbuf[20];
237         struct tm       *ztm;
238 #ifdef HACK_LOCAL_TIME
239         char            ltmbuf[20];
240         struct tm       *
241 #endif /* HACK_LOCAL_TIME */
242         time_t          currenttime;
243         Attribute       *a;
244 #ifdef HACK_LOCAL_TIME
245         static AttributeDescription     *ad_local = NULL;
246         const char      *text = NULL;
247 #endif /* HACK_LOCAL_TIME */
248         ber_len_t       len;
249
250         static int      init_start = 0;
251
252         /*
253          * Note: ltmbuf, ltm, ad_local, text are used only if HACK_LOCAL_TIME
254          *       is defined
255          */
256
257         assert( mi );
258         assert( e );
259         
260         if ( init_start == 0 && strncmp( e->e_nname.bv_val, "cn=START",
261                                 sizeof("cn=START")-1 ) == 0 ) {
262                 currenttime = starttime;
263                 init_start = 1;
264
265         } else if ( strncmp( e->e_nname.bv_val, "cn=CURRENT",
266                                 sizeof("cn=CURRENT")-1 ) == 0 ) {
267                 currenttime = slap_get_time();
268
269         } else {
270                 return( 0 );
271         }
272
273         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
274         ztm = gmtime( &currenttime );
275         strftime( ztmbuf, sizeof( ztmbuf ), "%Y%m%d%H%M%SZ", ztm );
276 #ifdef HACK_LOCAL_TIME
277         ltm = localtime( &currenttime );
278         local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
279 #endif /* HACK_LOCAL_TIME */
280         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
281
282         a = attr_find( e->e_attrs, monitor_ad_desc );
283         if ( a == NULL ) {
284                 return( -1 );
285         }
286
287         len = strlen( ztmbuf );
288         assert( len == a->a_vals[0].bv_len );
289         AC_MEMCPY( a->a_vals[0].bv_val, ztmbuf, len );
290
291 #ifdef HACK_LOCAL_TIME
292         if ( ad_local == NULL ) {
293                 if ( slap_str2ad( "description;lang-x-local", 
294                                         &ad_local, &text ) != LDAP_SUCCESS ) {
295                         return( -1 );
296                 }
297         }
298         a = attr_find( e->e_attrs, ad_local );
299         if ( a == NULL ) {
300                 return( -1 );
301         }
302
303         len = strlen( ltmbuf );
304         assert( len == a->a_vals[0].bv_len );
305         AC_MEMCPY( a->a_vals[0].bv_val, ltmbuf, len );
306 #endif /* HACK_LOCAL_TIME */
307
308         return( 0 );
309 }
310
311 #ifdef HACK_LOCAL_TIME
312 /*
313  * assumes gmtime_mutex is locked
314  */
315 static int
316 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
317 {
318         char *p;
319
320         if ( len < 20 ) {
321                 return -1;
322         }
323         strftime( buf, len, "%Y%m%d%H%M%S", ltm );
324
325         p = buf + 14;
326
327         if ( delta < 0 ) {
328                 p[ 0 ] = '-';
329                 delta = -delta;
330         } else {
331                 p[ 0 ] = '+';
332         }
333         p++;
334
335         snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );
336         
337         return 0;
338 }
339 #endif /* HACK_LOCAL_TIME */
340