]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_auth.c
Big cleanup of the C auth app from Scott McKellar:
[Evergreen.git] / Open-ILS / src / c-apps / oils_auth.c
1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "opensrf/osrf_settings.h"
4 #include "objson/object.h"
5 #include "opensrf/log.h"
6 #include "openils/oils_utils.h"
7 #include "openils/oils_constants.h"
8 #include "openils/oils_event.h"
9
10 #define OILS_AUTH_CACHE_PRFX "oils_auth_"
11
12 #define MODULENAME "open-ils.auth"
13
14 #define OILS_AUTH_OPAC "opac"
15 #define OILS_AUTH_STAFF "staff"
16 #define OILS_AUTH_TEMP "temp"
17
18 int osrfAppInitialize();
19 int osrfAppChildInit();
20
21 static int _oilsAuthOPACTimeout = 0;
22 static int _oilsAuthStaffTimeout = 0;
23 static int _oilsAuthOverrideTimeout = 0;
24
25
26 int osrfAppInitialize() {
27
28         osrfLogInfo(OSRF_LOG_MARK, "Initializing Auth Server...");
29
30     /* load and parse the IDL */
31         if (!oilsInitIDL(NULL)) return 1; /* return non-zero to indicate error */
32
33         osrfAppRegisterMethod( 
34                 MODULENAME, 
35                 "open-ils.auth.authenticate.init", 
36                 "oilsAuthInit", 
37                 "Start the authentication process and returns the intermediate authentication seed"
38                 " PARAMS( username )", 1, 0 );
39
40         osrfAppRegisterMethod( 
41                 MODULENAME, 
42                 "open-ils.auth.authenticate.complete", 
43                 "oilsAuthComplete", 
44                 "Completes the authentication process.  Returns an object like so: "
45                 "{authtoken : <token>, authtime:<time>}, where authtoken is the login "
46                 "token and authtime is the number of seconds the session will be active"
47                 "PARAMS(username, md5sum( seed + password ), type, org_id ) "
48                 "type can be one of 'opac','staff', or 'temp' and it defaults to 'staff' "
49                 "org_id is the location at which the login should be considered "
50                 "active for login timeout purposes"     , 1, 0 );
51
52         osrfAppRegisterMethod( 
53                 MODULENAME, 
54                 "open-ils.auth.session.retrieve", 
55                 "oilsAuthSessionRetrieve", 
56                 "Pass in the auth token and this retrieves the user object.  The auth "
57                 "timeout is reset when this call is made "
58                 "Returns the user object (password blanked) for the given login session "
59                 "PARAMS( authToken )", 1, 0 );
60
61         osrfAppRegisterMethod( 
62                 MODULENAME, 
63                 "open-ils.auth.session.delete", 
64                 "oilsAuthSessionDelete", 
65                 "Destroys the given login session "
66                 "PARAMS( authToken )",  1, 0 );
67
68         osrfAppRegisterMethod(
69                 MODULENAME,
70                 "open-ils.auth.session.reset_timeout",
71                 "oilsAuthResetTimeout",
72                 "Resets the login timeout for the given session "
73                 "Returns an ILS Event with payload = session_timeout of session "
74                 "if found, otherwise returns the NO_SESSION event"
75                 "PARAMS( authToken )", 1, 0 );
76
77         return 0;
78 }
79
80 int osrfAppChildInit() {
81         return 0;
82 }
83
84 int oilsAuthInit( osrfMethodContext* ctx ) {
85         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
86
87         jsonObject* resp;
88
89         char* username = NULL;
90         char* seed              = NULL;
91         char* md5seed   = NULL;
92         char* key               = NULL;
93
94         if( (username = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0))) ) {
95
96                 if( strchr( username, ' ' ) ) {
97
98                         /* spaces are not allowed */
99                         resp = jsonNewObject("x");       /* 'x' will never be a valid seed */
100                         osrfAppRespondComplete( ctx, resp );
101
102                 } else {
103
104                         seed = va_list_to_string( "%d.%ld.%s", time(NULL), (long) getpid(), username );
105                         key = va_list_to_string( "%s%s", OILS_AUTH_CACHE_PRFX, username );
106         
107                         md5seed = md5sum(seed);
108                         osrfCachePutString( key, md5seed, 30 );
109         
110                         osrfLogDebug( OSRF_LOG_MARK, "oilsAuthInit(): has seed %s and key %s", md5seed, key );
111         
112                         resp = jsonNewObject(md5seed);  
113                         osrfAppRespondComplete( ctx, resp );
114         
115                         free(seed);
116                         free(md5seed);
117                         free(key);
118                 }
119
120                 jsonObjectFree(resp);
121                 free(username);
122                 return 0;
123         }
124
125         return -1;
126 }
127
128 /** Verifies that the user has permission to login with the 
129  * given type.  If the permission fails, an oilsEvent is returned
130  * to the caller.
131  * @return -1 if the permission check failed, 0 if ther permission
132  * is granted
133  */
134 static int oilsAuthCheckLoginPerm( 
135                 osrfMethodContext* ctx, const jsonObject* userObj, const char* type ) {
136
137         if(!(userObj && type)) return -1;
138         oilsEvent* perm = NULL;
139
140         if(!strcasecmp(type, OILS_AUTH_OPAC)) {
141                 char* permissions[] = { "OPAC_LOGIN" };
142                 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
143
144         } else if(!strcasecmp(type, OILS_AUTH_STAFF)) {
145                 char* permissions[] = { "STAFF_LOGIN" };
146                 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
147
148         } else if(!strcasecmp(type, OILS_AUTH_TEMP)) {
149                 char* permissions[] = { "STAFF_LOGIN" };
150                 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
151         }
152
153         if(perm) {
154                 osrfAppRespondComplete( ctx, oilsEventToJSON(perm) ); 
155                 oilsEventFree(perm);
156                 return -1;
157         }
158
159         return 0;
160 }
161
162 /**
163  * Returns 1 if the password provided matches the user's real password
164  * Returns 0 otherwise
165  * Returns -1 on error
166  */
167 static int oilsAuthVerifyPassword( const osrfMethodContext* ctx,
168                 const jsonObject* userObj, const char* uname, const char* password ) {
169
170         int ret = 0;
171         char* realPassword = oilsFMGetString( userObj, "passwd" ); /**/
172         char* seed = osrfCacheGetString( "%s%s", OILS_AUTH_CACHE_PRFX, uname ); /**/
173
174         if(!seed) {
175                 return osrfAppRequestRespondException( ctx->session,
176                         ctx->request, "No authentication seed found. "
177                         "open-ils.auth.authenticate.init must be called first");
178         }
179
180         osrfLogInternal(OSRF_LOG_MARK, "oilsAuth retrieved real password: [%s]", realPassword);
181         osrfLogDebug(OSRF_LOG_MARK,  "oilsAuth retrieved seed from cache: %s", seed );
182         char* maskedPw = md5sum( "%s%s", seed, realPassword );
183         if(!maskedPw) {
184                 free(realPassword);
185                 free(seed);
186                 return -1;
187         }
188         osrfLogDebug(OSRF_LOG_MARK,  "oilsAuth generated masked password %s. "
189                         "Testing against provided password %s", maskedPw, password );
190
191         if( !strcmp( maskedPw, password ) ) ret = 1;
192
193         free(realPassword);
194         free(seed);
195         free(maskedPw);
196
197         return ret;
198 }
199
200 /**
201  * Calculates the login timeout
202  * 1. If orgloc is 1 or greater and has a timeout specified as an 
203  * org unit setting, it is used
204  * 2. If orgloc is not valid, we check the org unit auth timeout 
205  * setting for the home org unit of the user logging in
206  * 3. If that setting is not defined, we use the configured defaults
207  */
208 static double oilsAuthGetTimeout( const jsonObject* userObj, const char* type, double orgloc ) {
209
210         if(!_oilsAuthOPACTimeout) { /* Load the default timeouts */
211
212                 _oilsAuthOPACTimeout =
213                         jsonObjectGetNumber( 
214                                 osrf_settings_host_value_object( 
215                                         "/apps/open-ils.auth/app_settings/default_timeout/opac"));
216
217                 _oilsAuthStaffTimeout =
218                         jsonObjectGetNumber( 
219                                 osrf_settings_host_value_object( 
220                                         "/apps/open-ils.auth/app_settings/default_timeout/staff" ));
221
222                 _oilsAuthOverrideTimeout =
223                         jsonObjectGetNumber( 
224                                 osrf_settings_host_value_object( 
225                                         "/apps/open-ils.auth/app_settings/default_timeout/temp" ));
226
227
228                 osrfLogInfo(OSRF_LOG_MARK, "Set default auth timetouts: opac => %d : staff => %d : temp => %d",
229                                 _oilsAuthOPACTimeout, _oilsAuthStaffTimeout, _oilsAuthOverrideTimeout );
230         }
231
232         char* setting = NULL;
233
234         double home_ou = jsonObjectGetNumber( oilsFMGetObject( userObj, "home_ou" ) );
235         if(orgloc < 1) orgloc = (int) home_ou;
236
237         if(!strcmp(type, OILS_AUTH_OPAC)) 
238                 setting = OILS_ORG_SETTING_OPAC_TIMEOUT;
239         else if(!strcmp(type, OILS_AUTH_STAFF)) 
240                 setting = OILS_ORG_SETTING_STAFF_TIMEOUT;
241         else if(!strcmp(type, OILS_AUTH_TEMP)) 
242                 setting = OILS_ORG_SETTING_TEMP_TIMEOUT;
243
244         char* timeout = oilsUtilsFetchOrgSetting( orgloc, setting );
245
246         if(!timeout) {
247                 if( orgloc != home_ou ) {
248                         osrfLogDebug(OSRF_LOG_MARK, "Auth timeout not defined for org %d, "
249                                                                 "trying home_ou %d", orgloc, home_ou );
250                         timeout = oilsUtilsFetchOrgSetting( (int) home_ou, setting );
251                 }
252                 if(!timeout) {
253                         if(!strcmp(type, OILS_AUTH_STAFF)) return _oilsAuthStaffTimeout;
254                         if(!strcmp(type, OILS_AUTH_TEMP)) return _oilsAuthOverrideTimeout;
255                         return _oilsAuthOPACTimeout;
256                 }
257         }
258
259         double t = atof(timeout);
260         free(timeout);
261         return t ;
262 }
263
264 /* Adds the authentication token to the user cache.  The timeout for the 
265  * auth token is based on the type of login as well as (if type=='opac') 
266  * the org location id.
267  * Returns the event that should be returned to the user.  
268  * Event must be freed
269  */
270 static oilsEvent* oilsAuthHandleLoginOK( jsonObject* userObj, const char* uname,
271                 const char* type, double orgloc, const char* workstation ) {
272                 
273         oilsEvent* response;
274
275         double timeout;
276         char* wsorg = jsonObjectToSimpleString(oilsFMGetObject(userObj, "ws_ou"));
277         if(wsorg) { /* if there is a workstation, use it for the timeout */
278                 osrfLogDebug( OSRF_LOG_MARK, 
279                                 "Auth session trying workstation id %d for auth timeout", atoi(wsorg));
280                 timeout = oilsAuthGetTimeout( userObj, type, atoi(wsorg) );
281                 free(wsorg);
282         } else {
283                 osrfLogDebug( OSRF_LOG_MARK, 
284                                 "Auth session trying org from param [%d] for auth timeout", orgloc );
285                 timeout = oilsAuthGetTimeout( userObj, type, orgloc );
286         }
287         osrfLogDebug(OSRF_LOG_MARK, "Auth session timeout for %s: %f", uname, timeout );
288
289         char* string = va_list_to_string( 
290                         "%d.%ld.%s", (long) getpid(), time(NULL), uname ); 
291         char* authToken = md5sum(string); 
292         char* authKey = va_list_to_string( 
293                         "%s%s", OILS_AUTH_CACHE_PRFX, authToken ); 
294
295         const char* ws = (workstation) ? workstation : "";
296         osrfLogActivity(OSRF_LOG_MARK,  
297                 "successful login: username=%s, authtoken=%s, workstation=%s", uname, authToken, ws );
298
299         oilsFMSetString( userObj, "passwd", "" );
300         jsonObject* cacheObj = jsonParseStringFmt("{\"authtime\": %f}", timeout);
301         jsonObjectSetKey( cacheObj, "userobj", jsonObjectClone(userObj));
302
303         osrfCachePutObject( authKey, cacheObj, timeout ); 
304         jsonObjectFree(cacheObj);
305         osrfLogInternal(OSRF_LOG_MARK, "oilsAuthComplete(): Placed user object into cache");
306         jsonObject* payload = jsonParseStringFmt(
307                 "{ \"authtoken\": \"%s\", \"authtime\": %f }", authToken, timeout );
308
309         response = oilsNewEvent2( OSRF_LOG_MARK, OILS_EVENT_SUCCESS, payload );
310         free(string); free(authToken); free(authKey);
311         jsonObjectFree(payload);
312
313         return response;
314 }
315
316 static oilsEvent* oilsAuthVerifyWorkstation( 
317                 const osrfMethodContext* ctx, jsonObject* userObj, const char* ws ) {
318         osrfLogInfo(OSRF_LOG_MARK, "Attaching workstation to user at login: %s", ws);
319         jsonObject* workstation = oilsUtilsFetchWorkstationByName(ws);
320         if(!workstation) return oilsNewEvent(OSRF_LOG_MARK, "WORKSTATION_NOT_FOUND");
321         long wsid = oilsFMGetObjectId(workstation);
322         LONG_TO_STRING(wsid);
323         char* orgid = oilsFMGetString(workstation, "owning_lib");
324         oilsFMSetString(userObj, "wsid", LONGSTR);
325         oilsFMSetString(userObj, "ws_ou", orgid);
326         free(orgid);
327         jsonObjectFree(workstation);
328         return NULL;
329 }
330
331
332
333 /* see if the card used to login is marked as barred */
334 static oilsEvent* oilsAuthCheckCard( const char* barcode ) {
335         if(!barcode) return NULL;
336         osrfLogDebug(OSRF_LOG_MARK, "Checking to see if barcode %s is active", barcode);
337
338         jsonObject* params = jsonParseStringFmt("{\"barcode\":\"%s\"}", barcode);
339         jsonObject* card = oilsUtilsQuickReq(
340                 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
341         jsonObjectFree(params);
342
343         char* active = oilsFMGetString(card, "active");
344         jsonObjectFree(card);
345
346         oilsEvent* return_event = NULL;
347         if( ! oilsUtilsIsDBTrue(active) ) {
348                 osrfLogInfo(OSRF_LOG_MARK, "barcode %s is not active, returning event", barcode);
349                 return_event = oilsNewEvent(OSRF_LOG_MARK, "PATRON_CARD_INACTIVE");
350         }
351
352         free(active);
353         return return_event;
354 }
355
356
357
358 int oilsAuthComplete( osrfMethodContext* ctx ) {
359         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
360
361         const jsonObject* args  = jsonObjectGetIndex(ctx->params, 0);
362
363         const char* uname               = jsonObjectGetString(jsonObjectGetKeyConst(args, "username"));
364         const char* password    = jsonObjectGetString(jsonObjectGetKeyConst(args, "password"));
365         const char* type                = jsonObjectGetString(jsonObjectGetKeyConst(args, "type"));
366         double orgloc                   = jsonObjectGetNumber(jsonObjectGetKeyConst(args, "org"));
367         const char* workstation = jsonObjectGetString(jsonObjectGetKeyConst(args, "workstation"));
368         char* barcode                   = jsonObjectToSimpleString(jsonObjectGetKeyConst(args, "barcode"));
369
370         const char* ws = (workstation) ? workstation : "";
371
372
373         if(!type) type = OILS_AUTH_STAFF;
374
375         if( !( (uname || barcode) && password) ) {
376                 free(barcode);
377                 return osrfAppRequestRespondException( ctx->session, ctx->request, 
378                         "username/barcode and password required for method: %s", ctx->method->name );
379         }
380
381         oilsEvent* response = NULL;
382         jsonObject* userObj = NULL;
383
384         if(uname) userObj = oilsUtilsFetchUserByUsername( uname ); 
385         else if(barcode) userObj = oilsUtilsFetchUserByBarcode( barcode );
386         
387         if(!userObj) { 
388                 response = oilsNewEvent( OSRF_LOG_MARK, OILS_EVENT_AUTH_FAILED );
389                 osrfLogInfo(OSRF_LOG_MARK,  "failed login: username=%s, barcode=%s, workstation=%s", uname, barcode, ws );
390                 osrfAppRespondComplete( ctx, oilsEventToJSON(response) ); 
391                 oilsEventFree(response);
392                 free(barcode);
393                 return 0;
394         }
395
396         /* first let's see if they have the right credentials */
397         int passOK = -1;
398         if(uname) passOK = oilsAuthVerifyPassword( ctx, userObj, uname, password );
399         else if (barcode) 
400                 passOK = oilsAuthVerifyPassword( ctx, userObj, barcode, password );
401
402         if( passOK < 0 ) {
403                 jsonObjectFree(userObj);
404                 free(barcode);
405                 return passOK;
406         }
407
408         /* first see if their account is inactive */
409         char* active = oilsFMGetString(userObj, "active");
410         if( !oilsUtilsIsDBTrue(active) ) {
411                 response = oilsNewEvent(OSRF_LOG_MARK, "PATRON_INACTIVE");
412                 osrfAppRespondComplete( ctx, oilsEventToJSON(response) ); 
413                 oilsEventFree(response);
414                 jsonObjectFree(userObj);
415                 free(barcode);
416                 free(active);
417                 return 0;
418         }
419         free(active);
420
421         /* then see if the barcode they used is active */
422         if( barcode && ctx && userObj && (response = oilsAuthCheckCard( barcode )) ) {
423                 osrfAppRespondComplete( ctx, oilsEventToJSON(response) ); 
424                 oilsEventFree(response);
425                 jsonObjectFree(userObj);
426                 free(barcode);
427                 return 0;
428         }
429
430
431         /* check to see if the user is even allowed to login */
432         if( oilsAuthCheckLoginPerm( ctx, userObj, type ) == -1 ) {
433                 jsonObjectFree(userObj);
434                 free(barcode);
435                 return 0;
436         }
437         
438
439         /* if a workstation is defined, flesh the user with the workstation info */
440         if( workstation != NULL ) {
441                 osrfLogDebug(OSRF_LOG_MARK, "Workstation is %s", workstation);
442                 response = oilsAuthVerifyWorkstation( ctx, userObj, workstation );
443                 if(response) {
444                         jsonObjectFree(userObj);
445                         osrfAppRespondComplete( ctx, oilsEventToJSON(response) ); 
446                         oilsEventFree(response);
447                         free(barcode);
448                         return 0;
449                 }
450
451         } else {
452                 /* otherwise, use the home org as the workstation org on the user */
453                 char* orgid = oilsFMGetString(userObj, "home_ou");
454                 oilsFMSetString(userObj, "ws_ou", orgid);
455                 free(orgid);
456         }
457
458         char* freeable_uname = NULL;
459         if(!uname) {
460                 uname = freeable_uname = oilsFMGetString( userObj, "usrname" );
461         }
462
463         if( passOK ) {
464                 response = oilsAuthHandleLoginOK( userObj, uname, type, orgloc, workstation );
465
466         } else {
467                 response = oilsNewEvent( OSRF_LOG_MARK, OILS_EVENT_AUTH_FAILED );
468                 osrfLogInfo(OSRF_LOG_MARK,  "failed login: username=%s, barcode=%s, workstation=%s", uname, barcode, ws );
469         }
470
471         jsonObjectFree(userObj);
472         osrfAppRespondComplete( ctx, oilsEventToJSON(response) ); 
473         oilsEventFree(response);
474         free(barcode);
475
476         if(freeable_uname) free(freeable_uname);
477
478         return 0;
479 }
480
481
482
483 int oilsAuthSessionDelete( osrfMethodContext* ctx ) {
484         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
485
486         const char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
487         jsonObject* resp = NULL;
488
489         if( authToken ) {
490                 osrfLogDebug(OSRF_LOG_MARK, "Removing auth session: %s", authToken );
491                 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
492                 osrfCacheRemove(key);
493                 resp = jsonNewObject(authToken); /**/
494                 free(key);
495         }
496
497         osrfAppRespondComplete( ctx, resp );
498         jsonObjectFree(resp);
499         return 0;
500 }
501
502 /** Resets the auth login timeout
503  * @return The event object, OILS_EVENT_SUCCESS, or OILS_EVENT_NO_SESSION
504  */
505 static oilsEvent*  _oilsAuthResetTimeout( const char* authToken ) {
506         if(!authToken) return NULL;
507
508         oilsEvent* evt = NULL;
509         double timeout;
510
511         osrfLogDebug(OSRF_LOG_MARK, "Resetting auth timeout for session %s", authToken);
512         char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); 
513         jsonObject* cacheObj = osrfCacheGetObject( key ); 
514
515         if(!cacheObj) {
516                 osrfLogInfo(OSRF_LOG_MARK, "No user in the cache exists with key %s", key);
517                 evt = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
518
519         } else {
520
521                 timeout = jsonObjectGetNumber( jsonObjectGetKeyConst( cacheObj, "authtime"));
522                 osrfCacheSetExpire( timeout, key );
523                 jsonObject* payload = jsonNewNumberObject(timeout);
524                 evt = oilsNewEvent2(OSRF_LOG_MARK, OILS_EVENT_SUCCESS, payload);
525                 jsonObjectFree(payload);
526                 jsonObjectFree(cacheObj);
527         }
528
529         free(key);
530         return evt;
531 }
532
533 int oilsAuthResetTimeout( osrfMethodContext* ctx ) {
534         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
535         const char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0));
536         oilsEvent* evt = _oilsAuthResetTimeout(authToken);
537         osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );
538         oilsEventFree(evt);
539         return 0;
540 }
541
542
543 int oilsAuthSessionRetrieve( osrfMethodContext* ctx ) {
544         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
545
546         const char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0));
547         jsonObject* cacheObj = NULL;
548         oilsEvent* evt = NULL;
549
550         if( authToken ){
551
552                 evt = _oilsAuthResetTimeout(authToken);
553
554                 if( evt && strcmp(evt->event, OILS_EVENT_SUCCESS) ) {
555                         osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );
556
557                 } else {
558
559                         osrfLogDebug(OSRF_LOG_MARK, "Retrieving auth session: %s", authToken);
560                         char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); 
561                         cacheObj = osrfCacheGetObject( key ); 
562                         if(cacheObj) {
563                                 osrfAppRespondComplete( ctx, jsonObjectGetKeyConst( cacheObj, "userobj"));
564                                 jsonObjectFree(cacheObj);
565                         } else {
566                                 oilsEvent* evt2 = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
567                                 osrfAppRespondComplete( ctx, oilsEventToJSON(evt2) ); /* should be event.. */
568                                 oilsEventFree(evt2);
569                         }
570                         free(key);
571                 }
572
573         } else {
574
575                 evt = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
576                 osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );
577         }
578
579         if(evt)
580                 oilsEventFree(evt);
581
582         return 0;
583 }
584
585
586