091d62d41584d9f58b8ca9e57e042832e3719d7f
[metze/samba/wip.git] / lib / ldb / ldb_key_value / ldb_kv_cache.c
1 /*
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldb key value cache functions
28  *
29  *  Description: cache special records in a ldb/tdb
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb_kv.h"
35 #include "ldb_private.h"
36
37 #define LDB_KV_FLAG_CASE_INSENSITIVE (1<<0)
38 #define LDB_KV_FLAG_INTEGER          (1<<1)
39 #define LDB_KV_FLAG_UNIQUE_INDEX     (1<<2)
40 #define LDB_KV_FLAG_ORDERED_INTEGER  (1<<3)
41
42 /* valid attribute flags */
43 static const struct {
44         const char *name;
45         int value;
46 } ldb_kv_valid_attr_flags[] = {
47         { "CASE_INSENSITIVE", LDB_KV_FLAG_CASE_INSENSITIVE },
48         { "INTEGER", LDB_KV_FLAG_INTEGER },
49         { "ORDERED_INTEGER", LDB_KV_FLAG_ORDERED_INTEGER },
50         { "HIDDEN", 0 },
51         { "UNIQUE_INDEX",  LDB_KV_FLAG_UNIQUE_INDEX},
52         { "NONE", 0 },
53         { NULL, 0 }
54 };
55
56 /*
57   de-register any special handlers for @ATTRIBUTES
58 */
59 static void ldb_kv_attributes_unload(struct ldb_module *module)
60 {
61         struct ldb_context *ldb = ldb_module_get_ctx(module);
62
63         ldb_schema_attribute_remove_flagged(ldb, LDB_ATTR_FLAG_FROM_DB);
64
65 }
66
67 /*
68   add up the attrib flags for a @ATTRIBUTES element
69 */
70 static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
71 {
72         unsigned int i;
73         unsigned value = 0;
74         for (i=0;i<el->num_values;i++) {
75                 unsigned int j;
76                 for (j = 0; ldb_kv_valid_attr_flags[j].name; j++) {
77                         if (strcmp(ldb_kv_valid_attr_flags[j].name,
78                                    (char *)el->values[i].data) == 0) {
79                                 value |= ldb_kv_valid_attr_flags[j].value;
80                                 break;
81                         }
82                 }
83                 if (ldb_kv_valid_attr_flags[j].name == NULL) {
84                         return -1;
85                 }
86         }
87         *v = value;
88         return 0;
89 }
90
91 static int ldb_schema_attribute_compare(const void *p1, const void *p2)
92 {
93         const struct ldb_schema_attribute *sa1 = (const struct ldb_schema_attribute *)p1;
94         const struct ldb_schema_attribute *sa2 = (const struct ldb_schema_attribute *)p2;
95         return ldb_attr_cmp(sa1->name, sa2->name);
96 }
97
98 /*
99   register any special handlers from @ATTRIBUTES
100 */
101 static int ldb_kv_attributes_load(struct ldb_module *module)
102 {
103         struct ldb_schema_attribute *attrs;
104         struct ldb_context *ldb;
105         struct ldb_message *attrs_msg = NULL;
106         struct ldb_dn *dn;
107         unsigned int i;
108         unsigned int num_loaded_attrs = 0;
109         int r;
110
111         ldb = ldb_module_get_ctx(module);
112
113         if (ldb->schema.attribute_handler_override) {
114                 /* we skip loading the @ATTRIBUTES record when a module is supplying
115                    its own attribute handling */
116                 return 0;
117         }
118
119         attrs_msg = ldb_msg_new(module);
120         if (attrs_msg == NULL) {
121                 goto failed;
122         }
123
124         dn = ldb_dn_new(module, ldb, LDB_KV_ATTRIBUTES);
125         if (dn == NULL) goto failed;
126
127         r = ldb_kv_search_dn1(module,
128                               dn,
129                               attrs_msg,
130                               LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
131                                   LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
132                                   LDB_UNPACK_DATA_FLAG_NO_DN);
133         talloc_free(dn);
134         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
135                 goto failed;
136         }
137         if (r == LDB_ERR_NO_SUCH_OBJECT || attrs_msg->num_elements == 0) {
138                 TALLOC_FREE(attrs_msg);
139                 return 0;
140         }
141
142         attrs = talloc_array(attrs_msg,
143                              struct ldb_schema_attribute,
144                              attrs_msg->num_elements
145                              + ldb->schema.num_attributes);
146         if (attrs == NULL) {
147                 goto failed;
148         }
149
150         memcpy(attrs,
151                ldb->schema.attributes,
152                sizeof(ldb->schema.attributes[0]) * ldb->schema.num_attributes);
153
154         /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
155            but its close enough for now */
156         for (i=0;i<attrs_msg->num_elements;i++) {
157                 unsigned flags = 0, attr_flags = 0;
158                 const char *syntax;
159                 const struct ldb_schema_syntax *s;
160                 const struct ldb_schema_attribute *a =
161                         ldb_schema_attribute_by_name(ldb,
162                                                      attrs_msg->elements[i].name);
163                 if (a != NULL && a->flags & LDB_ATTR_FLAG_FIXED) {
164                         /* Must already be set in the array, and kept */
165                         continue;
166                 }
167
168                 if (ldb_kv_attributes_flags(&attrs_msg->elements[i], &flags) !=
169                     0) {
170                         ldb_debug(ldb, LDB_DEBUG_ERROR,
171                                   "Invalid @ATTRIBUTES element for '%s'",
172                                   attrs_msg->elements[i].name);
173                         goto failed;
174                 }
175
176                 if (flags & LDB_KV_FLAG_UNIQUE_INDEX) {
177                         attr_flags = LDB_ATTR_FLAG_UNIQUE_INDEX;
178                 }
179                 flags &= ~LDB_KV_FLAG_UNIQUE_INDEX;
180
181                 /* These are not currently flags, each is exclusive */
182                 if (flags == LDB_KV_FLAG_CASE_INSENSITIVE) {
183                         syntax = LDB_SYNTAX_DIRECTORY_STRING;
184                 } else if (flags == LDB_KV_FLAG_INTEGER) {
185                         syntax = LDB_SYNTAX_INTEGER;
186                 } else if (flags == LDB_KV_FLAG_ORDERED_INTEGER) {
187                         syntax = LDB_SYNTAX_ORDERED_INTEGER;
188                 } else if (flags == 0) {
189                         syntax = LDB_SYNTAX_OCTET_STRING;
190                 } else {
191                         ldb_debug(ldb, LDB_DEBUG_ERROR,
192                                   "Invalid flag combination 0x%x for '%s' "
193                                   "in @ATTRIBUTES",
194                                   flags, attrs_msg->elements[i].name);
195                         goto failed;
196                 }
197
198                 s = ldb_standard_syntax_by_name(ldb, syntax);
199                 if (s == NULL) {
200                         ldb_debug(ldb, LDB_DEBUG_ERROR,
201                                   "Invalid attribute syntax '%s' for '%s' "
202                                   "in @ATTRIBUTES",
203                                   syntax, attrs_msg->elements[i].name);
204                         goto failed;
205                 }
206
207                 attr_flags |= LDB_ATTR_FLAG_ALLOCATED | LDB_ATTR_FLAG_FROM_DB;
208
209                 r = ldb_schema_attribute_fill_with_syntax(ldb,
210                                                           attrs,
211                                                           attrs_msg->elements[i].name,
212                                                           attr_flags, s,
213                                                           &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
214                 if (r != 0) {
215                         goto failed;
216                 }
217                 num_loaded_attrs++;
218         }
219
220         attrs = talloc_realloc(attrs_msg,
221                                attrs, struct ldb_schema_attribute,
222                                num_loaded_attrs + ldb->schema.num_attributes);
223         if (attrs == NULL) {
224                 goto failed;
225         }
226         TYPESAFE_QSORT(attrs, num_loaded_attrs + ldb->schema.num_attributes,
227                        ldb_schema_attribute_compare);
228         talloc_unlink(ldb, ldb->schema.attributes);
229         ldb->schema.attributes = talloc_steal(ldb, attrs);
230         ldb->schema.num_attributes = num_loaded_attrs + ldb->schema.num_attributes;
231         TALLOC_FREE(attrs_msg);
232
233         return 0;
234 failed:
235         TALLOC_FREE(attrs_msg);
236         return -1;
237 }
238
239 /*
240   register any index records we find for the DB
241 */
242 static int ldb_kv_index_load(struct ldb_module *module,
243                              struct ldb_kv_private *ldb_kv)
244 {
245         struct ldb_context *ldb = ldb_module_get_ctx(module);
246         struct ldb_dn *indexlist_dn;
247         int r, lmdb_subdb_version;
248
249         if (ldb->schema.index_handler_override) {
250                 /*
251                  * we skip loading the @INDEXLIST record when a module is
252                  * supplying its own attribute handling
253                  */
254                 ldb_kv->cache->attribute_indexes = true;
255                 ldb_kv->cache->one_level_indexes =
256                     ldb->schema.one_level_indexes;
257                 ldb_kv->cache->GUID_index_attribute =
258                     ldb->schema.GUID_index_attribute;
259                 ldb_kv->cache->GUID_index_dn_component =
260                     ldb->schema.GUID_index_dn_component;
261                 return 0;
262         }
263
264         talloc_free(ldb_kv->cache->indexlist);
265
266         ldb_kv->cache->indexlist = ldb_msg_new(ldb_kv->cache);
267         if (ldb_kv->cache->indexlist == NULL) {
268                 return -1;
269         }
270         ldb_kv->cache->one_level_indexes = false;
271         ldb_kv->cache->attribute_indexes = false;
272
273         indexlist_dn = ldb_dn_new(ldb_kv, ldb, LDB_KV_INDEXLIST);
274         if (indexlist_dn == NULL) {
275                 return -1;
276         }
277
278         r = ldb_kv_search_dn1(module,
279                               indexlist_dn,
280                               ldb_kv->cache->indexlist,
281                               LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
282                                   LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
283                                   LDB_UNPACK_DATA_FLAG_NO_DN);
284         TALLOC_FREE(indexlist_dn);
285
286         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
287                 return -1;
288         }
289
290         if (ldb_msg_find_element(ldb_kv->cache->indexlist, LDB_KV_IDXONE) !=
291             NULL) {
292                 ldb_kv->cache->one_level_indexes = true;
293         }
294         if (ldb_msg_find_element(ldb_kv->cache->indexlist, LDB_KV_IDXATTR) !=
295             NULL) {
296                 ldb_kv->cache->attribute_indexes = true;
297         }
298         ldb_kv->cache->GUID_index_attribute = ldb_msg_find_attr_as_string(
299             ldb_kv->cache->indexlist, LDB_KV_IDXGUID, NULL);
300         ldb_kv->cache->GUID_index_dn_component = ldb_msg_find_attr_as_string(
301             ldb_kv->cache->indexlist, LDB_KV_IDX_DN_GUID, NULL);
302
303         lmdb_subdb_version = ldb_msg_find_attr_as_int(
304             ldb_kv->cache->indexlist, LDB_KV_IDX_LMDB_SUBDB, 0);
305
306         if (lmdb_subdb_version != 0) {
307                 ldb_set_errstring(ldb,
308                                   "FATAL: This ldb_mdb database has "
309                                   "been written in a new version of LDB "
310                                   "using a sub-database index that "
311                                   "is not understood by ldb "
312                                   LDB_VERSION);
313                 return -1;
314         }
315
316         return 0;
317 }
318
319 /*
320   initialise the baseinfo record
321 */
322 static int ldb_kv_baseinfo_init(struct ldb_module *module)
323 {
324         struct ldb_context *ldb;
325         void *data = ldb_module_get_private(module);
326         struct ldb_kv_private *ldb_kv =
327             talloc_get_type(data, struct ldb_kv_private);
328         struct ldb_message *msg;
329         struct ldb_message_element el;
330         struct ldb_val val;
331         int ret;
332         /* the initial sequence number must be different from the one
333            set in ltdb_cache_free(). Thanks to Jon for pointing this
334            out. */
335         const char *initial_sequence_number = "1";
336
337         ldb = ldb_module_get_ctx(module);
338
339         ldb_kv->sequence_number = atof(initial_sequence_number);
340
341         msg = ldb_msg_new(ldb_kv);
342         if (msg == NULL) {
343                 goto failed;
344         }
345
346         msg->num_elements = 1;
347         msg->elements = &el;
348         msg->dn = ldb_dn_new(msg, ldb, LDB_KV_BASEINFO);
349         if (!msg->dn) {
350                 goto failed;
351         }
352         el.name = talloc_strdup(msg, LDB_KV_SEQUENCE_NUMBER);
353         if (!el.name) {
354                 goto failed;
355         }
356         el.values = &val;
357         el.num_values = 1;
358         el.flags = 0;
359         val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
360         if (!val.data) {
361                 goto failed;
362         }
363         val.length = 1;
364
365         ret = ldb_kv_store(module, msg, TDB_INSERT);
366
367         talloc_free(msg);
368
369         return ret;
370
371 failed:
372         talloc_free(msg);
373         errno = ENOMEM;
374         return LDB_ERR_OPERATIONS_ERROR;
375 }
376
377 /*
378   free any cache records
379  */
380 static void ldb_kv_cache_free(struct ldb_module *module)
381 {
382         void *data = ldb_module_get_private(module);
383         struct ldb_kv_private *ldb_kv =
384             talloc_get_type(data, struct ldb_kv_private);
385
386         ldb_kv->sequence_number = 0;
387         talloc_free(ldb_kv->cache);
388         ldb_kv->cache = NULL;
389 }
390
391 /*
392   force a cache reload
393 */
394 int ldb_kv_cache_reload(struct ldb_module *module)
395 {
396         ldb_kv_attributes_unload(module);
397         ldb_kv_cache_free(module);
398         return ldb_kv_cache_load(module);
399 }
400
401 /*
402   load the cache records
403 */
404 int ldb_kv_cache_load(struct ldb_module *module)
405 {
406         struct ldb_context *ldb;
407         void *data = ldb_module_get_private(module);
408         struct ldb_kv_private *ldb_kv =
409             talloc_get_type(data, struct ldb_kv_private);
410         struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
411         uint64_t seq;
412         struct ldb_message *baseinfo = NULL, *options = NULL;
413         const struct ldb_schema_attribute *a;
414         bool have_write_txn = false;
415         int r;
416
417         ldb = ldb_module_get_ctx(module);
418
419         /* a very fast check to avoid extra database reads */
420         if (ldb_kv->cache != NULL && !ldb_kv->kv_ops->has_changed(ldb_kv)) {
421                 return 0;
422         }
423
424         if (ldb_kv->cache == NULL) {
425                 ldb_kv->cache = talloc_zero(ldb_kv, struct ldb_kv_cache);
426                 if (ldb_kv->cache == NULL)
427                         goto failed;
428         }
429
430         baseinfo = ldb_msg_new(ldb_kv->cache);
431         if (baseinfo == NULL) goto failed;
432
433         baseinfo_dn = ldb_dn_new(baseinfo, ldb, LDB_KV_BASEINFO);
434         if (baseinfo_dn == NULL) goto failed;
435
436         r = ldb_kv->kv_ops->lock_read(module);
437         if (r != LDB_SUCCESS) {
438                 goto failed;
439         }
440         r = ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0);
441         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
442                 goto failed_and_unlock;
443         }
444
445         /* possibly initialise the baseinfo */
446         if (r == LDB_ERR_NO_SUCH_OBJECT) {
447
448                 /* Give up the read lock, try again with a write lock */
449                 r = ldb_kv->kv_ops->unlock_read(module);
450                 if (r != LDB_SUCCESS) {
451                         goto failed;
452                 }
453
454                 if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
455                         goto failed;
456                 }
457
458                 have_write_txn = true;
459
460                 /* error handling for ltdb_baseinfo_init() is by
461                    looking for the record again. */
462                 ldb_kv_baseinfo_init(module);
463
464                 if (ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0) !=
465                     LDB_SUCCESS) {
466                         goto failed_and_unlock;
467                 }
468
469         }
470
471         /* Ignore the result, and update the sequence number */
472         ldb_kv->kv_ops->has_changed(ldb_kv);
473
474         /* if the current internal sequence number is the same as the one
475            in the database then assume the rest of the cache is OK */
476         seq = ldb_msg_find_attr_as_uint64(baseinfo, LDB_KV_SEQUENCE_NUMBER, 0);
477         if (seq == ldb_kv->sequence_number) {
478                 goto done;
479         }
480         ldb_kv->sequence_number = seq;
481
482         /* Read an interpret database options */
483
484         options = ldb_msg_new(ldb_kv->cache);
485         if (options == NULL) goto failed_and_unlock;
486
487         options_dn = ldb_dn_new(options, ldb, LDB_KV_OPTIONS);
488         if (options_dn == NULL) goto failed_and_unlock;
489
490         r = ldb_kv_search_dn1(module, options_dn, options, 0);
491         talloc_free(options_dn);
492         if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
493                 goto failed_and_unlock;
494         }
495
496         /* set flags if they do exist */
497         if (r == LDB_SUCCESS) {
498                 ldb_kv->check_base =
499                     ldb_msg_find_attr_as_bool(options, LDB_KV_CHECK_BASE, false);
500                 ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
501                     options, LDB_KV_DISALLOW_DN_FILTER, false);
502         } else {
503                 ldb_kv->check_base = false;
504                 ldb_kv->disallow_dn_filter = false;
505         }
506
507         /*
508          * ltdb_attributes_unload() calls internally talloc_free() on
509          * any non-fixed elemnts in ldb->schema.attributes.
510          *
511          * NOTE WELL: This is per-ldb, not per module, so overwrites
512          * the handlers across all databases when used under Samba's
513          * partition module.
514          */
515         ldb_kv_attributes_unload(module);
516
517         if (ldb_kv_index_load(module, ldb_kv) == -1) {
518                 goto failed_and_unlock;
519         }
520
521         /*
522          * NOTE WELL: This is per-ldb, not per module, so overwrites
523          * the handlers across all databases when used under Samba's
524          * partition module.
525          */
526         if (ldb_kv_attributes_load(module) == -1) {
527                 goto failed_and_unlock;
528         }
529
530         ldb_kv->GUID_index_syntax = NULL;
531         if (ldb_kv->cache->GUID_index_attribute != NULL) {
532                 /*
533                  * Now the attributes are loaded, set the guid_index_syntax.
534                  * This can't fail, it will return a default at worst
535                  */
536                 a = ldb_schema_attribute_by_name(
537                     ldb, ldb_kv->cache->GUID_index_attribute);
538                 ldb_kv->GUID_index_syntax = a->syntax;
539         }
540
541 done:
542         if (have_write_txn) {
543                 if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
544                         goto failed;
545                 }
546         } else {
547                 ldb_kv->kv_ops->unlock_read(module);
548         }
549
550         talloc_free(options);
551         talloc_free(baseinfo);
552         return 0;
553
554 failed_and_unlock:
555         if (have_write_txn) {
556                 ldb_kv->kv_ops->abort_write(ldb_kv);
557         } else {
558                 ldb_kv->kv_ops->unlock_read(module);
559         }
560
561 failed:
562         talloc_free(options);
563         talloc_free(baseinfo);
564         return -1;
565 }
566
567
568 /*
569   increase the sequence number to indicate a database change
570 */
571 int ldb_kv_increase_sequence_number(struct ldb_module *module)
572 {
573         struct ldb_context *ldb;
574         void *data = ldb_module_get_private(module);
575         struct ldb_kv_private *ldb_kv =
576             talloc_get_type(data, struct ldb_kv_private);
577         struct ldb_message *msg;
578         struct ldb_message_element el[2];
579         struct ldb_val val;
580         struct ldb_val val_time;
581         time_t t = time(NULL);
582         char *s = NULL;
583         int ret;
584
585         ldb = ldb_module_get_ctx(module);
586
587         msg = ldb_msg_new(ldb_kv);
588         if (msg == NULL) {
589                 errno = ENOMEM;
590                 return LDB_ERR_OPERATIONS_ERROR;
591         }
592
593         s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number + 1);
594         if (!s) {
595                 talloc_free(msg);
596                 errno = ENOMEM;
597                 return LDB_ERR_OPERATIONS_ERROR;
598         }
599
600         msg->num_elements = ARRAY_SIZE(el);
601         msg->elements = el;
602         msg->dn = ldb_dn_new(msg, ldb, LDB_KV_BASEINFO);
603         if (msg->dn == NULL) {
604                 talloc_free(msg);
605                 errno = ENOMEM;
606                 return LDB_ERR_OPERATIONS_ERROR;
607         }
608         el[0].name = talloc_strdup(msg, LDB_KV_SEQUENCE_NUMBER);
609         if (el[0].name == NULL) {
610                 talloc_free(msg);
611                 errno = ENOMEM;
612                 return LDB_ERR_OPERATIONS_ERROR;
613         }
614         el[0].values = &val;
615         el[0].num_values = 1;
616         el[0].flags = LDB_FLAG_MOD_REPLACE;
617         val.data = (uint8_t *)s;
618         val.length = strlen(s);
619
620         el[1].name = talloc_strdup(msg, LDB_KV_MOD_TIMESTAMP);
621         if (el[1].name == NULL) {
622                 talloc_free(msg);
623                 errno = ENOMEM;
624                 return LDB_ERR_OPERATIONS_ERROR;
625         }
626         el[1].values = &val_time;
627         el[1].num_values = 1;
628         el[1].flags = LDB_FLAG_MOD_REPLACE;
629
630         s = ldb_timestring(msg, t);
631         if (s == NULL) {
632                 talloc_free(msg);
633                 return LDB_ERR_OPERATIONS_ERROR;
634         }
635
636         val_time.data = (uint8_t *)s;
637         val_time.length = strlen(s);
638
639         ret = ldb_kv_modify_internal(module, msg, NULL);
640
641         talloc_free(msg);
642
643         if (ret == LDB_SUCCESS) {
644                 ldb_kv->sequence_number += 1;
645         }
646
647         /* updating the tdb_seqnum here avoids us reloading the cache
648            records due to our own modification */
649         ldb_kv->kv_ops->has_changed(ldb_kv);
650
651         return ret;
652 }
653
654 int ldb_kv_check_at_attributes_values(const struct ldb_val *value)
655 {
656         unsigned int i;
657
658         for (i = 0; ldb_kv_valid_attr_flags[i].name != NULL; i++) {
659                 if ((strcmp(ldb_kv_valid_attr_flags[i].name,
660                             (char *)value->data) == 0)) {
661                         return 0;
662                 }
663         }
664
665         return -1;
666 }