]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
Add validation.
[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 static int
45 local_time( const struct tm *ztm, long delta, char *buf, size_t len );
46
47 int
48 monitor_subsys_time_init(
49         BackendDB               *be
50 )
51 {
52         struct monitorinfo      *mi;
53         
54         Entry                   *e, *e_tmp, *e_time;
55         struct monitorentrypriv *mp;
56         char                    buf[1024], ztmbuf[20], ltmbuf[20];
57         struct tm               *ztm, *ltm;
58
59         /*
60          * Note: ltmbuf, ltm are used only if HACK_LOCAL_TIME is defined
61          */
62
63         assert( be != NULL );
64
65         mi = ( struct monitorinfo * )be->be_private;
66
67         if ( monitor_cache_get( mi,
68                         &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
69 #ifdef NEW_LOGGING
70                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
71                         "monitor_subsys_time_init: "
72                         "unable to get entry '%s'\n",
73                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
74 #else
75                 Debug( LDAP_DEBUG_ANY,
76                         "monitor_subsys_time_init: "
77                         "unable to get entry '%s'\n%s%s",
78                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 
79                         "", "" );
80 #endif
81                 return( -1 );
82         }
83
84         e_tmp = NULL;
85
86         /*
87          * Start
88          */
89         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
90         ztm = gmtime( &starttime );
91         strftime( ztmbuf, sizeof(ztmbuf), "%Y%m%d%H%M%SZ", ztm );
92 #ifdef HACK_LOCAL_TIME
93         ltm = localtime( &starttime );
94         local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
95 #endif /* HACK_LOCAL_TIME */
96         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
97         snprintf( buf, sizeof( buf ),
98                         "dn: cn=Start,%s\n"
99                         SLAPD_MONITOR_OBJECTCLASSES
100                         "cn: Start\n"
101                         "description: %s\n"
102 #ifdef HACK_LOCAL_TIME
103                         "description;lang-x-local: %s"
104 #endif /* HACK_LOCAL_TIME */
105                         , monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
106                         ztmbuf
107 #ifdef HACK_LOCAL_TIME
108                         , ltmbuf 
109 #endif /* HACK_LOCAL_TIME */
110                         );
111
112         e = str2entry( buf );
113         if ( e == NULL ) {
114 #ifdef NEW_LOGGING
115                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
116                         "monitor_subsys_time_init: "
117                         "unable to create entry 'cn=Start,%s'\n",
118                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
119 #else
120                 Debug( LDAP_DEBUG_ANY,
121                         "monitor_subsys_time_init: "
122                         "unable to create entry 'cn=Start,%s'\n%s%s",
123                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
124                         "", "" );
125 #endif
126                 return( -1 );
127         }
128         
129         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
130         e->e_private = ( void * )mp;
131         mp->mp_next = e_tmp;
132         mp->mp_children = NULL;
133         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
134         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
135                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
136
137         if ( monitor_cache_add( mi, e ) ) {
138 #ifdef NEW_LOGGING
139                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
140                         "monitor_subsys_time_init: "
141                         "unable to add entry 'cn=Start,%s'\n",
142                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
143 #else
144                 Debug( LDAP_DEBUG_ANY,
145                         "monitor_subsys_time_init: "
146                         "unable to add entry 'cn=Start,%s'\n%s%s",
147                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
148                         "", "" );
149 #endif
150                 return( -1 );
151         }
152         
153         e_tmp = e;
154
155         /*
156          * Current
157          */
158         snprintf( buf, sizeof( buf ),
159                         "dn: cn=Current,%s\n"
160                         SLAPD_MONITOR_OBJECTCLASSES
161                         "cn: Current\n"
162                         "description: %s\n"
163 #ifdef HACK_LOCAL_TIME 
164                         "description;lang-x-local: %s"
165 #endif /* HACK_LOCAL_TIME */
166                         , monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
167                         ztmbuf
168 #ifdef HACK_LOCAL_TIME
169                         , ltmbuf
170 #endif /* HACK_LOCAL_TIME */
171                         );
172
173         e = str2entry( buf );
174         if ( e == NULL ) {
175 #ifdef NEW_LOGGING
176                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
177                         "monitor_subsys_time_init: "
178                         "unable to create entry 'cn=Current,%s'\n",
179                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
180 #else
181                 Debug( LDAP_DEBUG_ANY,
182                         "monitor_subsys_time_init: "
183                         "unable to create entry 'cn=Current,%s'\n%s%s",
184                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
185                         "", "" );
186 #endif
187                 return( -1 );
188         }
189         
190         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
191         e->e_private = ( void * )mp;
192         mp->mp_next = e_tmp;
193         mp->mp_children = NULL;
194         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
195         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
196                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
197
198         if ( monitor_cache_add( mi, e ) ) {
199 #ifdef NEW_LOGGING
200                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
201                         "monitor_subsys_time_init: "
202                         "unable to add entry 'cn=Current,%s'\n",
203                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
204 #else
205                 Debug( LDAP_DEBUG_ANY,
206                         "monitor_subsys_time_init: "
207                         "unable to add entry 'cn=Current,%s'\n%s%s",
208                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
209                         "", "" );
210 #endif
211                 return( -1 );
212         }
213         
214         e_tmp = e;
215
216         mp = ( struct monitorentrypriv * )e_time->e_private;
217         mp->mp_children = e_tmp;
218
219         monitor_cache_release( mi, e_time );
220
221         return( 0 );
222 }
223
224 int
225 monitor_subsys_time_update(
226         struct monitorinfo      *mi,
227         Entry                   *e
228 )
229 {
230         char            ztmbuf[20], ltmbuf[20];
231         struct tm       *ztm, *ltm;
232         time_t          currenttime;
233         Attribute       *a;
234         static AttributeDescription     *ad_local = NULL;
235         const char      *text = NULL;
236         ber_len_t       len;
237
238         static int      init_start = 0;
239
240         /*
241          * Note: ltmbuf, ltm, ad_local, text are used only if HACK_LOCAL_TIME
242          *       is defined
243          */
244
245         assert( mi );
246         assert( e );
247         
248         if ( init_start == 0 && strncmp( e->e_nname.bv_val, "cn=START",
249                                 sizeof("cn=START")-1 ) == 0 ) {
250                 currenttime = starttime;
251                 init_start = 1;
252
253         } else if ( strncmp( e->e_nname.bv_val, "cn=CURRENT",
254                                 sizeof("cn=CURRENT")-1 ) == 0 ) {
255                 currenttime = slap_get_time();
256
257         } else {
258                 return( 0 );
259         }
260
261         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
262         ztm = gmtime( &currenttime );
263         strftime( ztmbuf, sizeof( ztmbuf ), "%Y%m%d%H%M%SZ", ztm );
264 #ifdef HACK_LOCAL_TIME
265         ltm = localtime( &currenttime );
266         local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
267 #endif /* HACK_LOCAL_TIME */
268         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
269
270         a = attr_find( e->e_attrs, monitor_ad_desc );
271         if ( a == NULL ) {
272                 return( -1 );
273         }
274
275         len = strlen( ztmbuf );
276         assert( len == a->a_vals[0].bv_len );
277         AC_MEMCPY( a->a_vals[0].bv_val, ztmbuf, len );
278
279 #ifdef HACK_LOCAL_TIME
280         if ( ad_local == NULL ) {
281                 if ( slap_str2ad( "description;lang-x-local", 
282                                         &ad_local, &text ) != LDAP_SUCCESS ) {
283                         return( -1 );
284                 }
285         }
286         a = attr_find( e->e_attrs, ad_local );
287         if ( a == NULL ) {
288                 return( -1 );
289         }
290
291         len = strlen( ltmbuf );
292         assert( len == a->a_vals[0].bv_len );
293         AC_MEMCPY( a->a_vals[0].bv_val, ltmbuf, len );
294 #endif /* HACK_LOCAL_TIME */
295
296         return( 0 );
297 }
298
299 /*
300  * assumes gmtime_mutex is locked
301  */
302 static int
303 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
304 {
305         char *p;
306
307         if ( len < 20 ) {
308                 return -1;
309         }
310         strftime( buf, len, "%Y%m%d%H%M%S", ltm );
311
312         p = buf + 14;
313
314         if ( delta < 0 ) {
315                 p[ 0 ] = '-';
316                 delta = -delta;
317         } else {
318                 p[ 0 ] = '+';
319         }
320         p++;
321
322         snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );
323         
324         return 0;
325 }
326