r7851: We are case preserving let the DN be returned the same the user put it into.
[kai/samba.git] / source / lib / ldb / ldb_sqlite3 / ldb_sqlite3.c
1 /* 
2    ldb database library
3    
4    Copyright (C) Derrell Lipman  2005
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 2 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, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb sqlite3 backend
29  *
30  *  Description: core files for SQLITE3 backend
31  *
32  *  Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
33  */
34
35 #include <stdarg.h>
36 #include "includes.h"
37 #include "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "ldb/include/ldb_explode_dn.h"
40 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
41
42 /*
43  * Macros used throughout
44  */
45
46 #ifndef FALSE
47 # define FALSE  (0)
48 # define TRUE   (! FALSE)
49 #endif
50
51 #define FILTER_ATTR_TABLE       "temp_filter_attrs"
52 #define RESULT_ATTR_TABLE       "temp_result_attrs"
53
54 //#define TEMPTAB                 /* for testing, create non-temporary table */
55 #define TEMPTAB                 "TEMPORARY"
56
57 //#define DEBUG_LOCKS
58
59 #ifndef DEBUG_LOCKS
60 # define LOCK_DB(mod, name)      lsqlite3_lock(mod, name)
61 # define UNLOCK_DB(mod, name)    lsqlite3_unlock(mod, name)
62 #else
63 # define LOCK_DB(mod, name)      lock_debug(mod, name, __FILE__, __LINE__)
64 # define UNLOCK_DB(mod, name)    unlock_debug(mod, name, __FILE__, __LINE__)
65 #endif
66
67 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...)                \
68         do {                                                            \
69                 if (query_norows(lsqlite3, sql) != 0) {                 \
70                         if (bRollbackOnError) {                         \
71                                 UNLOCK_DB(module, "rollback");          \
72                         }                                               \
73                         return -1;                                      \
74                 }                                                       \
75         } while (0)
76
77 #define QUERY_INT(lsqlite3, result_var, bRollbackOnError, sql...)       \
78         do {                                                            \
79                 if (query_int(lsqlite3, &result_var, sql) != 0) {       \
80                         if (bRollbackOnError) {                         \
81                                 UNLOCK_DB(module, "rollback");          \
82                         }                                               \
83                         return -1;                                      \
84                 }                                                       \
85         } while (0)
86
87
88 #define SQLITE3_DEBUG_QUERY     (1 << 0)
89 #define SQLITE3_DEBUG_INIT      (1 << 1)
90 #define SQLITE3_DEBUG_ADD       (1 << 2)
91 #define SQLITE3_DEBUG_NEWDN     (1 << 3)
92 #define SQLITE3_DEBUG_SEARCH    (1 << 4)
93
94 /*
95  * Static variables
96  */
97 static int      lsqlite3_debug = FALSE;
98
99 /*
100  * Forward declarations
101  */
102 static int
103 lsqlite3_rename(struct ldb_module * module,
104                 const char * olddn,
105                 const char * newdn);
106
107 static int
108 lsqlite3_delete(struct ldb_module *module,
109                 const char *dn);
110
111 static int
112 lsqlite3_search_bytree(struct ldb_module * module,
113                        const char * pBaseDN,
114                        enum ldb_scope scope,
115                        struct ldb_parse_tree * pTree,
116                        const char * const * attrs,
117                        struct ldb_message *** pppRes);
118
119 static int
120 lsqlite3_search(struct ldb_module * module,
121                 const char * pBaseDN,
122                 enum ldb_scope scope,
123                 const char * pExpression,
124                 const char * const attrs[],
125                 struct ldb_message *** pppRes);
126
127 static int
128 lsqlite3_add(struct ldb_module *module,
129              const struct ldb_message *msg);
130
131 static int
132 lsqlite3_modify(struct ldb_module *module,
133                 const struct ldb_message *msg);
134
135 static int
136 lsqlite3_lock(struct ldb_module *module,
137               const char *lockname);
138
139 static int
140 lsqlite3_unlock(struct ldb_module *module,
141                 const char *lockname);
142
143 static const char *
144 lsqlite3_errstring(struct ldb_module *module);
145
146 static int
147 initialize(struct lsqlite3_private *lsqlite3,
148            const char *url);
149
150 static int
151 destructor(void *p);
152
153 static int
154 query_norows(const struct lsqlite3_private *lsqlite3,
155              const char *pSql,
156              ...);
157
158 static int
159 query_int(const struct lsqlite3_private * lsqlite3,
160           long long * pRet,
161           const char * pSql,
162           ...);
163
164 static int case_fold_attr_required(void * hUserData,
165                                    char *attr);
166
167 static int case_fold_attr_not_required(void * hUserData,
168                                    char *attr);
169
170 static int
171 add_msg_attr(void * hTalloc,
172              long long eid,
173              const char * pDN,
174              const char * pAttrName,
175              const char * pAttrValue,
176              long long prevEID,
177              int * pAllocated,
178              struct ldb_message *** pppRes);
179
180 static char *
181 parsetree_to_sql(struct ldb_module *module,
182                  char * hTalloc,
183                  const struct ldb_parse_tree *t);
184
185 static int
186 parsetree_to_attrlist(struct ldb_module *module,
187                       const struct ldb_parse_tree * t);
188
189 static int
190 msg_to_sql(struct ldb_module * module,
191            const struct ldb_message * msg,
192            long long eid,
193            int use_flags);
194
195 static int
196 new_dn(struct ldb_module * module,
197        char * pDN,
198        long long * pEID);
199
200 static void
201 base160_sql(sqlite3_context * hContext,
202             int argc,
203             sqlite3_value ** argv);
204
205 static void
206 base160next_sql(sqlite3_context * hContext,
207                 int argc,
208                 sqlite3_value ** argv);
209
210 #ifdef DEBUG_LOCKS
211 static int lock_debug(struct ldb_module * module,
212                       const char * lockname,
213                       const char * pFileName,
214                       int linenum);
215
216 static int unlock_debug(struct ldb_module * module,
217                         const char * lockname,
218                         const char * pFileName,
219                         int linenum);
220 #endif
221
222
223 /*
224  * Table of operations for the sqlite3 backend
225  */
226 static const struct ldb_module_ops lsqlite3_ops = {
227         .name          = "sqlite",
228         .search        = lsqlite3_search,
229         .search_bytree = lsqlite3_search_bytree,
230         .add_record    = lsqlite3_add,
231         .modify_record = lsqlite3_modify,
232         .delete_record = lsqlite3_delete,
233         .rename_record = lsqlite3_rename,
234         .named_lock    = lsqlite3_lock,
235         .named_unlock  = lsqlite3_unlock,
236         .errstring     = lsqlite3_errstring
237 };
238
239
240
241
242 /*
243  * Public functions
244  */
245
246
247 /*
248  * connect to the database
249  */
250 int lsqlite3_connect(struct ldb_context *ldb,
251                      const char *url, 
252                      unsigned int flags, 
253                      const char *options[])
254 {
255         int                         i;
256         int                         ret;
257         struct lsqlite3_private *   lsqlite3 = NULL;
258         
259         lsqlite3 = talloc(ldb, struct lsqlite3_private);
260         if (!lsqlite3) {
261                 goto failed;
262         }
263         
264         lsqlite3->sqlite = NULL;
265         lsqlite3->options = NULL;
266         lsqlite3->lock_count = 0;
267         
268         ret = initialize(lsqlite3, url);
269         if (ret != SQLITE_OK) {
270                 goto failed;
271         }
272         
273         talloc_set_destructor(lsqlite3, destructor);
274         
275         ldb->modules = talloc(ldb, struct ldb_module);
276         if (!ldb->modules) {
277                 goto failed;
278         }
279         ldb->modules->ldb = ldb;
280         ldb->modules->prev = ldb->modules->next = NULL;
281         ldb->modules->private_data = lsqlite3;
282         ldb->modules->ops = &lsqlite3_ops;
283         
284         if (options) {
285                 /*
286                  * take a copy of the options array, so we don't have to rely
287                  * on the caller keeping it around (it might be dynamic)
288                  */
289                 for (i=0;options[i];i++) ;
290                 
291                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
292                 if (!lsqlite3->options) {
293                         goto failed;
294                 }
295                 
296                 for (i=0;options[i];i++) {
297                         
298                         lsqlite3->options[i+1] = NULL;
299                         lsqlite3->options[i] =
300                                 talloc_strdup(lsqlite3->options, options[i]);
301                         if (!lsqlite3->options[i]) {
302                                 goto failed;
303                         }
304                 }
305         }
306         
307         return 0;
308         
309 failed:
310         if (lsqlite3->sqlite != NULL) {
311                 (void) sqlite3_close(lsqlite3->sqlite);
312         }
313         talloc_free(lsqlite3);
314         return -1;
315 }
316
317
318 /*
319  * Interface functions referenced by lsqlite3_ops
320  */
321
322 /* rename a record */
323 static int
324 lsqlite3_rename(struct ldb_module * module,
325                 const char * pOldDN,
326                 const char * pNewDN)
327 {
328         const char *pOldNormalizedDN;
329         const char *pNewNormalizedDN;
330         long long                   eid;
331         struct lsqlite3_private *   lsqlite3 = module->private_data;
332
333         /* ignore ltdb specials */
334         if (*pOldDN == '@' || *pNewDN == '@') {
335                 return 0;
336         }
337
338         /* Case-fold each of the DNs */
339         pOldNormalizedDN = ldb_dn_fold(module->ldb, pOldDN,
340                              module, case_fold_attr_required);
341         pNewNormalizedDN = ldb_dn_fold(module->ldb, pNewDN,
342                              module, case_fold_attr_required);
343
344         /* Begin a transaction */
345         if (LOCK_DB(module, "transaction") < 0) {
346                 return -1;
347         }
348
349         /* Determine the eid of the DN being renamed */
350         QUERY_INT(lsqlite3,
351                   eid,
352                   TRUE,
353                   "SELECT eid\n"
354                   "  FROM ldb_entry\n"
355                   "  WHERE normalized_dn = %Q;",
356                   pOldNormalizedDN);
357         
358         QUERY_NOROWS(lsqlite3,
359                      TRUE,
360                      "UPDATE ldb_entry "
361                      "  SET dn = %Q, "
362                      "  normalized_dn = %Q "
363                      "  WHERE eid = %lld;",
364                      pNewDN, pNewNormalizedDN, eid);
365
366         QUERY_NOROWS(lsqlite3,
367                      TRUE,
368                      "UPDATE ldb_attribute_values "
369                      "  SET attr_value = %Q, "
370                      "      attr_value_normalized = %Q "
371                      "  WHERE eid = %lld "
372                      "    AND attr_name = 'DN';",
373                      pNewDN,
374                      pNewNormalizedDN,
375                      eid);
376
377         /* Commit the transaction */
378         if (UNLOCK_DB(module, "transaction") < 0) {
379                 UNLOCK_DB(module, "rollback");
380                 return -1;
381         }
382         
383         return 0;
384 }
385
386 /* delete a record */
387 static int
388 lsqlite3_delete(struct ldb_module * module,
389                 const char * pDN)
390 {
391         char *pNormalizedDN;
392         long long                   eid;
393         struct lsqlite3_private *   lsqlite3 = module->private_data;
394
395         /* ignore ltdb specials */
396         if (*pDN == '@') {
397                 return 0;
398         }
399
400         /* Begin a transaction */
401         if (LOCK_DB(module, "transaction") < 0) {
402                 return -1;
403         }
404
405         /* Case-fold the DNs */
406         pNormalizedDN = ldb_dn_fold(module->ldb, pDN, module, case_fold_attr_required);
407
408         /* Determine the eid of the DN being deleted */
409         QUERY_INT(lsqlite3,
410                   eid,
411                   TRUE,
412                   "SELECT eid\n"
413                   "  FROM ldb_attribute_values\n"
414                   "  WHERE attr_name = 'DN'\n"
415                   "    AND attr_value_normalized = %Q;",
416                   pNormalizedDN);
417         
418         /* Delete attribute/value table entries pertaining to this DN */
419         QUERY_NOROWS(lsqlite3,
420                      TRUE,
421                      "DELETE FROM ldb_attribute_values "
422                      "  WHERE eid = %lld;",
423                      eid);
424
425         /* Delete this entry */
426         QUERY_NOROWS(lsqlite3,
427                      TRUE,
428                      "DELETE FROM ldb_entry "
429                      "  WHERE eid = %lld;",
430                      eid);
431
432         /* Commit the transaction */
433         if (UNLOCK_DB(module, "transaction") < 0) {
434                 UNLOCK_DB(module, "rollback");
435                 return -1;
436         }
437         
438         return 0;
439 }
440
441 /* search for matching records, by tree */
442 static int
443 lsqlite3_search_bytree(struct ldb_module * module,
444                        const char * pBaseDN,
445                        enum ldb_scope scope,
446                        struct ldb_parse_tree * pTree,
447                        const char * const * attrs,
448                        struct ldb_message *** pppRes)
449 {
450         int                         ret;
451         int                         allocated;
452         int                         bLoop;
453         long long                   eid = 0;
454         long long                   prevEID;
455         char *                      pSql = NULL;
456         char *                      pSqlConstraints;
457         char *                      hTalloc = NULL;
458         const char *                pDN;
459         const char *                pNormalizedBaseDN;
460         const char *                pAttrName;
461         const char *                pAttrValue;
462         const char *                pResultAttrList;
463         const char * const *        pRequestedAttrs;
464         sqlite3_stmt *              pStmt;
465         struct lsqlite3_private *   lsqlite3 = module->private_data;
466         
467         /* Allocate a temporary talloc context */
468         if ((hTalloc = talloc_new(module->ldb)) == NULL) {
469                 return -1;
470         }
471         
472         /* Case-fold the base DN */
473         if ((pNormalizedBaseDN = ldb_dn_fold(hTalloc, pBaseDN?pBaseDN:"",
474                                    module, case_fold_attr_required)) == NULL) {
475                 talloc_free(hTalloc);
476                 return -1;
477             }
478
479         /* Begin a transaction */
480         if (LOCK_DB(module, "transaction") < 0) {
481                 return -1;
482         }
483         
484         /*
485          * Obtain the eid of the base DN
486          */
487         if ((ret = query_int(lsqlite3,
488                              &eid,
489                              "SELECT eid\n"
490                              "  FROM ldb_entry\n"
491                              "  WHERE normalized_dn = %Q;",
492                              pNormalizedBaseDN)) == SQLITE_DONE) {
493                 UNLOCK_DB(module, "rollback");
494                 talloc_free(hTalloc);
495                 return 0;
496         } else if (ret != SQLITE_OK) {
497                 UNLOCK_DB(module, "rollback");
498                 talloc_free(hTalloc);
499                 return -1;
500         }
501         
502         /* Convert filter into a series of SQL conditions (constraints) */
503         pSqlConstraints = parsetree_to_sql(module, hTalloc, pTree);
504         
505         /* Ensure we're starting with an empty result attribute table */
506         QUERY_NOROWS(lsqlite3,
507                      FALSE,
508                      "DELETE FROM " RESULT_ATTR_TABLE "\n"
509                      "  WHERE 1;");/* avoid a schema change with WHERE 1 */
510         
511         /* Initially, we don't know what the requested attributes are */
512         pResultAttrList = NULL;
513
514         /* Insert the list of requested attributes into this table */
515         for (pRequestedAttrs = (const char * const *) attrs;
516              pRequestedAttrs != NULL && *pRequestedAttrs != NULL;
517              pRequestedAttrs++) {
518                 
519                 /* If any attribute in the list is "*" then... */
520                 if (strcmp(*pRequestedAttrs, "*") == 0) {
521                         /* we want all attribute types */
522                         pResultAttrList = "";
523                         break;
524                         
525                 } else {
526                         /* otherwise, add this name to the resuult list */
527                         QUERY_NOROWS(lsqlite3,
528                                      FALSE,
529                                      "INSERT OR IGNORE\n"
530                                      "  INTO " RESULT_ATTR_TABLE "\n"
531                                      "    (attr_name)\n"
532                                      "  VALUES\n"
533                                      "    (%Q);",
534                                      *pRequestedAttrs);
535                 }
536         }
537         
538         /* If we didn't get a "*" for all attributes in the result list... */
539         if (pResultAttrList == NULL) {
540                 /* ... then we'll use the result attribute table */
541                 pResultAttrList =
542                         "    AND upper(av.attr_name) IN\n"
543                         "          (SELECT attr_name\n"
544                         "             FROM " RESULT_ATTR_TABLE ") ";
545         }
546
547         /* Ensure we're starting with an empty filter attribute table */
548         QUERY_NOROWS(lsqlite3,
549                      FALSE,
550                      "DELETE FROM " FILTER_ATTR_TABLE "\n"
551                      "  WHERE 1;");/* avoid a schema change with WHERE 1 */
552         
553         /*
554          * Create a table of unique attribute names for our extra table list
555          */
556         if ((ret = parsetree_to_attrlist(module, pTree)) != 0) {
557                 ret = -1;
558                 goto cleanup;
559         }
560         
561         switch(scope) {
562         case LDB_SCOPE_DEFAULT:
563         case LDB_SCOPE_SUBTREE:
564                 pSql = sqlite3_mprintf(
565                         "SELECT entry.eid,\n"
566                         "       entry.dn,\n"
567                         "       av.attr_name,\n"
568                         "       av.attr_value\n"
569                         "  FROM ldb_entry AS entry\n"
570
571                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
572                         "    ON av.eid = entry.eid\n"
573                         "       %s\n"
574
575                         "  WHERE entry.eid IN\n"
576                         "    (SELECT DISTINCT ldb_entry.eid\n"
577                         "       FROM ldb_entry\n"
578                         "       WHERE ldb_entry.tree_key >=\n"
579                         "               (SELECT tree_key\n"
580                         "                  FROM ldb_entry\n"
581                         "                  WHERE eid = %lld)\n"
582                         "         AND ldb_entry.tree_key <\n"
583                         "               (SELECT base160_next(tree_key)\n"
584                         "                  FROM ldb_entry\n"
585                         "                  WHERE eid = %lld)\n"
586                         "         AND ldb_entry.eid IN\n(%s)\n"
587                         "    )\n"
588                         "  ORDER BY entry.tree_key DESC,\n"
589                         "           COALESCE(av.attr_name, '');",
590                         pResultAttrList,
591                         eid,
592                         eid,
593                         pSqlConstraints);
594                 break;
595                 
596         case LDB_SCOPE_BASE:
597                 pSql = sqlite3_mprintf(
598                         "SELECT entry.eid,\n"
599                         "       entry.dn,\n"
600                         "       av.attr_name,\n"
601                         "       av.attr_value\n"
602                         "  FROM ldb_entry AS entry\n"
603
604                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
605                         "    ON av.eid = entry.eid\n"
606                         "       %s\n"
607
608                         "  WHERE entry.eid IN\n"
609                         "    (SELECT DISTINCT ldb_entry.eid\n"
610                         "       FROM ldb_entry\n"
611                         "       WHERE ldb_entry.eid = %lld\n"
612                         "         AND ldb_entry.eid IN\n(%s)\n"
613                         "    )\n"
614                         "  ORDER BY entry.tree_key DESC,\n"
615                         "           COALESCE(av.attr_name, '');",
616                         pResultAttrList,
617                         eid,
618                         pSqlConstraints);
619                 break;
620                 
621         case LDB_SCOPE_ONELEVEL:
622                 pSql = sqlite3_mprintf(
623                         "SELECT entry.eid,\n"
624                         "       entry.dn,\n"
625                         "       av.attr_name,\n"
626                         "       av.attr_value\n"
627                         "  FROM ldb_entry AS entry\n"
628
629                         "  LEFT OUTER JOIN ldb_attribute_values AS av\n"
630                         "    ON av.eid = entry.eid\n"
631                         "       %s\n"
632
633                         "  WHERE entry.eid IN\n"
634                         "    (SELECT DISTINCT ldb_entry.eid\n"
635                         "       FROM ldb_entry\n"
636                         "       WHERE ldb_entry.tree_key >=\n"
637                         "               (SELECT tree_key\n"
638                         "                  FROM ldb_entry\n"
639                         "                  WHERE eid = %lld)\n"
640                         "         AND ldb_entry.tree_key <\n"
641                         "               (SELECT base160_next(tree_key)\n"
642                         "                  FROM ldb_entry\n"
643                         "                  WHERE eid = %lld)\n"
644                         "         AND length(ldb_entry.tree_key) =\n"
645                         "               (SELECT length(tree_key) + 4\n"
646                         "                  FROM ldb_entry\n"
647                         "                  WHERE eid = %lld)\n"
648                         "         AND ldb_entry.eid IN\n(%s)\n"
649                         "    )\n"
650
651                         "  ORDER BY entry.tree_key DESC,\n"
652                         "           COALESCE(av.attr_name, '');\n",
653                         pResultAttrList,
654                         eid,
655                         eid,
656                         eid,
657                         pSqlConstraints);
658                 break;
659         }
660         
661         if (pSql == NULL) {
662                 ret = -1;
663                 goto cleanup;
664         }
665
666         if (lsqlite3_debug & SQLITE3_DEBUG_SEARCH) {
667                 printf("%s\n", pSql);
668         }
669
670         /*
671          * Prepare and execute the SQL statement.  Loop allows retrying on
672          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
673          * requiring retrying the operation.
674          */
675         for (bLoop = TRUE; bLoop; ) {
676                 /* There are no allocate message structures yet */
677                 allocated = 0;
678                 if (pppRes != NULL) {
679                         *pppRes = NULL;
680                 }
681                 
682                 /* Compile the SQL statement into sqlite virtual machine */
683                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
684                                            pSql,
685                                            -1,
686                                            &pStmt,
687                                            NULL)) == SQLITE_SCHEMA) {
688                         if (pppRes != NULL && *pppRes != NULL) {
689                                 talloc_free(*pppRes);
690                         }
691                         continue;
692                 } else if (ret != SQLITE_OK) {
693                         ret = -1;
694                         break;
695                 }
696                 
697                 /* Initially, we have no previous eid */
698                 prevEID = -1;
699                 
700                 /* Loop through the returned rows */
701                 for (ret = SQLITE_ROW; ret == SQLITE_ROW; ) {
702                         
703                         /* Get the next row */
704                         if ((ret = sqlite3_step(pStmt)) == SQLITE_ROW) {
705                                 
706                                 /* Get the values from this row */
707                                 eid = sqlite3_column_int64(pStmt, 0);
708                                 pDN = sqlite3_column_text(pStmt, 1);
709                                 pAttrName = sqlite3_column_text(pStmt, 2);
710                                 pAttrValue = sqlite3_column_text(pStmt, 3);
711                                 
712                                 /* Add this result to the result set */
713                                 if (add_msg_attr(hTalloc,
714                                                  eid,
715                                                  pDN,
716                                                  pAttrName,
717                                                  pAttrValue,
718                                                  prevEID,
719                                                  &allocated,
720                                                  pppRes) != 0) {
721                                         
722                                         (void) sqlite3_finalize(pStmt);
723                                         ret = -1;
724                                         break;
725                                 }
726
727                                 /* Save the most recent EID */
728                                 prevEID = eid;
729                         }
730                 }
731                 
732                 if (ret == SQLITE_SCHEMA) {
733                         (void) sqlite3_finalize(pStmt);
734                         if (pppRes != NULL && *pppRes != NULL) {
735                                 talloc_free(*pppRes);
736                         }
737                         continue;
738                 } else if (ret != SQLITE_DONE) {
739                         (void) sqlite3_finalize(pStmt);
740                         ret = -1;
741                         break;
742                 }
743                 
744                 /* Free the virtual machine */
745                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
746                         (void) sqlite3_finalize(pStmt);
747                         if (pppRes != NULL && *pppRes != NULL) {
748                                 talloc_free(*pppRes);
749                         }
750                         continue;
751                 } else if (ret != SQLITE_OK) {
752                         (void) sqlite3_finalize(pStmt);
753                         ret = -1;
754                         break;
755                 }
756                 
757                 /*
758                  * Normal condition is only one time through loop.  Loop is
759                  * rerun in error conditions, via "continue", above.
760                  */
761                 ret = 0;
762                 bLoop = FALSE;
763         }
764         
765         /* We're alll done with this query */
766         sqlite3_free(pSql);
767         
768         /* End the transaction */
769         UNLOCK_DB(module, "rollback");
770         
771         /* Were there any results? */
772         if (ret != 0 || allocated == 0) {
773                 /* Nope.  We can free the results. */
774                 if (pppRes != NULL && *pppRes != NULL) {
775                         talloc_free(*pppRes);
776                 }
777         }
778         
779 cleanup:
780         /* Clean up our temporary tables */
781         QUERY_NOROWS(lsqlite3,
782                      FALSE,
783                      "DELETE FROM " RESULT_ATTR_TABLE "\n"
784                      "  WHERE 1;");/* avoid a schema change with WHERE 1 */
785         
786         QUERY_NOROWS(lsqlite3,
787                      FALSE,
788                      "DELETE FROM " FILTER_ATTR_TABLE "\n"
789                      "  WHERE 1;");/* avoid a schema change with WHERE 1 */
790         
791         
792         if (hTalloc != NULL) {
793                 talloc_free(hTalloc);
794         }
795         
796         /* If error, return error code; otherwise return number of results */
797         return ret == 0 ? allocated : ret;
798 }
799
800 /* search for matching records, by expression */
801 static int
802 lsqlite3_search(struct ldb_module * module,
803                 const char * pBaseDN,
804                 enum ldb_scope scope,
805                 const char * pExpression,
806                 const char * const * attrs,
807                 struct ldb_message *** pppRes)
808 {
809         int                     ret;
810         struct ldb_parse_tree * pTree;
811         
812         /* Handle tdb specials */
813         if (pBaseDN != NULL && *pBaseDN == '@') {
814 #warning "handle tdb specials"
815                 return 0;
816         }
817
818 #if 0 
819 /* (|(objectclass=*)(dn=*)) is  passed by the command line tool now instead */
820         /* Handle the special case of requesting all */
821         if (pExpression != NULL && *pExpression == '\0') {
822                 pExpression = "dn=*";
823         }
824 #endif
825
826         /* Parse the filter expression into a tree we can work with */
827         if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
828                 return -1;
829         }
830         
831         /* Now use the bytree function for the remainder of processing */
832         ret = lsqlite3_search_bytree(module, pBaseDN, scope,
833                                      pTree, attrs, pppRes);
834         
835         /* Free the parse tree */
836         talloc_free(pTree);
837         
838         /* All done. */
839         return ret;
840 }
841
842
843 /* add a record */
844 static int
845 lsqlite3_add(struct ldb_module *module,
846              const struct ldb_message *msg)
847 {
848         long long                   eid;
849         
850         /* See if this is an ltdb special */
851         if (*msg->dn == '@') {
852                 /* Yup.  We handle a few of these and ignore others */
853                 if (strcmp(msg->dn, "@SUBCLASSES") == 0) {
854 #warning "insert subclasses into object class tree"
855                 }
856
857                 if (strcmp(msg->dn, "@INDEXLIST") == 0) {
858                         /* explicitly ignored */
859                         return 0;
860                 }
861
862                 /* Others are implicitly ignored */
863                 return 0;
864         }
865
866         /* Begin a transaction */
867         if (LOCK_DB(module, "transaction") < 0) {
868                 return -1;
869         }
870         
871         /*
872          * Build any portions of the directory tree that don't exist.  If the
873          * final component already exists, it's an error.
874          */
875         if (new_dn(module, msg->dn, &eid) != 0) {
876                 UNLOCK_DB(module, "rollback");
877                 return -1;
878         }
879         
880         /* Add attributes to this new entry */
881         if (msg_to_sql(module, msg, eid, FALSE) != 0) {
882                 UNLOCK_DB(module, "rollback");
883                 return -1;
884         }
885         
886         /* Everything worked.  Commit it! */
887         if (UNLOCK_DB(module, "transaction") < 0) {
888                 UNLOCK_DB(module, "rollback");
889                 return -1;
890         }
891         return 0;
892 }
893
894
895 /* modify a record */
896 static int
897 lsqlite3_modify(struct ldb_module * module,
898                 const struct ldb_message * msg)
899 {
900         char *                      pNormalizedDN;
901         long long                   eid;
902         struct lsqlite3_private *   lsqlite3 = module->private_data;
903         
904         /* ignore ltdb specials */
905         if (*msg->dn == '@') {
906                 return 0;
907         }
908
909         /* Begin a transaction */
910         if (LOCK_DB(module, "transaction") < 0) {
911                 return -1;
912         }
913         
914         /* Case-fold the DN so we can compare it to what's in the database */
915         pNormalizedDN = ldb_dn_fold(module->ldb, msg->dn,
916                           module, case_fold_attr_required);
917
918         /* Determine the eid of the DN being deleted */
919         QUERY_INT(lsqlite3,
920                   eid,
921                   TRUE,
922                   "SELECT eid\n"
923                   "  FROM ldb_entry\n"
924                   "  WHERE normalized_dn = %Q;",
925                   pNormalizedDN);
926         
927         /* Apply the message attributes */
928         if (msg_to_sql(module, msg, eid, TRUE) != 0) {
929                 UNLOCK_DB(module, "rollback");
930                 return -1;
931         }
932         
933
934         /* Everything worked.  Commit it! */
935         if (UNLOCK_DB(module, "transaction") < 0) {
936                 UNLOCK_DB(module, "rollback");
937                 return -1;
938         }
939         return 0 ;
940 }
941
942 /* obtain a named lock */
943 static int
944 lsqlite3_lock(struct ldb_module * module,
945               const char * lockname)
946 {
947         struct lsqlite3_private *   lsqlite3 = module->private_data;
948
949         if (lockname == NULL) {
950                 return -1;
951         }
952         
953         if (strcmp(lockname, "transaction") == 0) {
954                 if (lsqlite3->lock_count == 0) {
955                         if (query_norows(lsqlite3, "BEGIN EXCLUSIVE;") != 0) {
956                                 return -1;
957                         }
958                 }
959                 ++lsqlite3->lock_count;
960         }
961         
962         return 0;
963 }
964
965 /* release a named lock */
966 static int
967 lsqlite3_unlock(struct ldb_module *module,
968                 const char *lockname)
969 {
970         struct lsqlite3_private *   lsqlite3 = module->private_data;
971
972         if (lockname == NULL) {
973                 return -1;
974         }
975         
976         if (strcmp(lockname, "transaction") == 0) {
977                 if (lsqlite3->lock_count == 1) {
978                         if (query_norows(lsqlite3, "COMMIT;") != 0) {
979                                 query_norows(lsqlite3, "ROLLBACK;");
980                         }
981                 } else if (lsqlite3->lock_count > 0) {
982                         --lsqlite3->lock_count;
983                 }
984         } else if (strcmp(lockname, "rollback") == 0) {
985                 query_norows(lsqlite3, "ROLLBACK;");
986         }
987         
988         return 0;
989 }
990
991 /* return extended error information */
992 static const char *
993 lsqlite3_errstring(struct ldb_module *module)
994 {
995         struct lsqlite3_private *   lsqlite3 = module->private_data;
996         
997         return sqlite3_errmsg(lsqlite3->sqlite);
998 }
999
1000
1001
1002
1003 /*
1004  * Static functions
1005  */
1006
1007 static int
1008 initialize(struct lsqlite3_private *lsqlite3,
1009            const char *url)
1010 {
1011         int             ret;
1012         long long       queryInt;
1013         const char *    pTail;
1014         sqlite3_stmt *  stmt;
1015         const char *    schema =       
1016                 
1017                 
1018                 "CREATE TABLE ldb_info AS "
1019                 "  SELECT 'LDB' AS database_type,"
1020                 "         '1.0' AS version;"
1021                 
1022                 /*
1023                  * The entry table holds the information about an entry. 
1024                  * This table is used to obtain the EID of the entry and to 
1025                  * support scope=one and scope=base.  The parent and child
1026                  * table is included in the entry table since all the other
1027                  * attributes are dependent on EID.
1028                  */
1029                 "CREATE TABLE ldb_entry "
1030                 "("
1031                 "  eid                   INTEGER PRIMARY KEY,"
1032                 "  peid                  INTEGER REFERENCES ldb_entry,"
1033                 "  dn                    TEXT UNIQUE NOT NULL,"
1034                 "  normalized_dn         TEXT UNIQUE NOT NULL,"
1035                 "  tree_key              TEXT UNIQUE,"
1036                 "  max_child_num         INTEGER DEFAULT 0,"
1037                 "  create_timestamp      INTEGER,"
1038                 "  modify_timestamp      INTEGER"
1039                 ");"
1040                 
1041
1042                 "CREATE TABLE ldb_object_classes"
1043                 "("
1044                 "  class_name            TEXT PRIMARY KEY,"
1045                 "  parent_class_name     TEXT,"
1046                 "  tree_key              TEXT UNIQUE,"
1047                 "  max_child_num         INTEGER DEFAULT 0"
1048                 ");"
1049                 
1050                 /*
1051                  * We keep a full listing of attribute/value pairs here
1052                  */
1053                 "CREATE TABLE ldb_attribute_values"
1054                 "("
1055                 "  eid                   INTEGER REFERENCES ldb_entry,"
1056                 "  attr_name             TEXT,"
1057                 "  attr_value            TEXT,"
1058                 "  attr_value_normalized TEXT "
1059                 ");"
1060                 
1061                
1062                 /*
1063                  * Indexes
1064                  */
1065                 "CREATE INDEX ldb_entry_tree_key_idx "
1066                 "  ON ldb_entry (tree_key);"
1067
1068                 "CREATE INDEX ldb_attribute_values_eid_idx "
1069                 "  ON ldb_attribute_values (eid);"
1070                 
1071                 
1072
1073                 /*
1074                  * Triggers
1075                  */
1076                 
1077                 "CREATE TRIGGER ldb_entry_insert_tr"
1078                 "  AFTER INSERT"
1079                 "  ON ldb_entry"
1080                 "  FOR EACH ROW"
1081                 "    BEGIN"
1082
1083                 "      UPDATE ldb_entry"
1084                 "        SET create_timestamp = strftime('%s', 'now'),"
1085                 "            modify_timestamp = strftime('%s', 'now')"
1086                 "           ,"
1087                 "            tree_key = COALESCE(tree_key, "
1088                 "              ("
1089                 "                SELECT tree_key || "
1090                 "                       (SELECT base160(max_child_num + 1)"
1091                 "                                FROM ldb_entry"
1092                 "                                WHERE eid = new.peid)"
1093                 "                  FROM ldb_entry "
1094                 "                  WHERE eid = new.peid "
1095                 "              ));"
1096                 "      UPDATE ldb_entry "
1097                 "        SET max_child_num = max_child_num + 1"
1098                 "        WHERE eid = new.peid;"
1099                 "    END;"
1100                 
1101                 "CREATE TRIGGER ldb_entry_update_tr"
1102                 "  AFTER UPDATE"
1103                 "  ON ldb_entry"
1104                 "  FOR EACH ROW"
1105                 "    BEGIN"
1106                 "      UPDATE ldb_entry"
1107                 "        SET modify_timestamp = strftime('%s', 'now')"
1108                 "        WHERE eid = old.eid;"
1109                 "    END;"
1110                 
1111                 "CREATE TRIGGER ldb_object_classes_insert_tr"
1112                 "  AFTER INSERT"
1113                 "  ON ldb_object_classes"
1114                 "  FOR EACH ROW"
1115                 "    BEGIN"
1116                 "      UPDATE ldb_object_classes"
1117                 "        SET tree_key = COALESCE(tree_key, "
1118                 "              ("
1119                 "                SELECT tree_key || "
1120                 "                       (SELECT base160(max_child_num + 1)"
1121                 "                                FROM ldb_object_classes"
1122                 "                                WHERE class_name = "
1123                 "                                      new.parent_class_name)"
1124                 "                  FROM ldb_object_classes "
1125                 "                  WHERE class_name = new.parent_class_name "
1126                 "              ));"
1127                 "      UPDATE ldb_object_classes "
1128                 "        SET max_child_num = max_child_num + 1"
1129                 "        WHERE class_name = new.parent_class_name;"
1130                 "    END;"
1131                 
1132                 /*
1133                  * Table initialization
1134                  */
1135
1136                 /* The root node */
1137                 "INSERT INTO ldb_entry "
1138                 "    (eid, peid, dn, normalized_dn, tree_key) "
1139                 "  VALUES "
1140                 "    (0, NULL, '', '', '0001');"
1141
1142                 /* And the root node "dn" attribute */
1143                 "INSERT INTO ldb_attribute_values "
1144                 "    (eid, attr_name, attr_value, attr_value_normalized) "
1145                 "  VALUES "
1146                 "    (0, 'DN', '', '');"
1147
1148                 "INSERT INTO ldb_object_classes "
1149                 "    (class_name, tree_key) "
1150                 "  VALUES "
1151                 "    ('TOP', '0001');"
1152
1153                 ;
1154         
1155         /* Skip protocol indicator of url  */
1156         if (strncmp(url, "sqlite://", 9) != 0) {
1157                 return SQLITE_MISUSE;
1158         }
1159         
1160         /* Update pointer to just after the protocol indicator */
1161         url += 9;
1162         
1163         /* Try to open the (possibly empty/non-existent) database */
1164         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1165                 return ret;
1166         }
1167         
1168         /* In case this is a new database, enable auto_vacuum */
1169         if (query_norows(lsqlite3, "PRAGMA auto_vacuum=1;") != 0) {
1170                         return -1;
1171         }
1172         
1173         /* Establish a busy timeout of 30 seconds */
1174         if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1175                                         30000)) != SQLITE_OK) {
1176                 return ret;
1177         }
1178
1179         /* Create a function, callable from sql, to increment a tree_key */
1180         if ((ret =
1181              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1182                                      "base160_next",  /* function name */
1183                                      1,               /* number of args */
1184                                      SQLITE_ANY,      /* preferred text type */
1185                                      NULL,            /* user data */
1186                                      base160next_sql, /* called func */
1187                                      NULL,            /* step func */
1188                                      NULL             /* final func */
1189                      )) != SQLITE_OK) {
1190                 return ret;
1191         }
1192
1193         /* Create a function, callable from sql, to convert int to base160 */
1194         if ((ret =
1195              sqlite3_create_function(lsqlite3->sqlite,/* handle */
1196                                      "base160",       /* function name */
1197                                      1,               /* number of args */
1198                                      SQLITE_ANY,      /* preferred text type */
1199                                      NULL,            /* user data */
1200                                      base160_sql,     /* called func */
1201                                      NULL,            /* step func */
1202                                      NULL             /* final func */
1203                      )) != SQLITE_OK) {
1204                 return ret;
1205         }
1206
1207         /* Begin a transaction */
1208         if ((ret = query_norows(lsqlite3, "BEGIN EXCLUSIVE;")) != 0) {
1209                         return ret;
1210         }
1211         
1212         /* Determine if this is a new database.  No tables means it is. */
1213         if (query_int(lsqlite3,
1214                       &queryInt,
1215                       "SELECT COUNT(*)\n"
1216                       "  FROM sqlite_master\n"
1217                       "  WHERE type = 'table';") != 0) {
1218                 query_norows(lsqlite3, "ROLLBACK;");
1219                 return -1;
1220         }
1221         
1222         if (queryInt == 0) {
1223                 /*
1224                  * Create the database schema
1225                  */
1226                 for (pTail = discard_const_p(char, schema);
1227                      pTail != NULL && *pTail != '\0';
1228                         ) {
1229                         
1230                         if (lsqlite3_debug & SQLITE3_DEBUG_INIT) {
1231                                 printf("Execute first query in:\n%s\n", pTail);
1232                         }
1233                         
1234                         if ((ret = sqlite3_prepare(
1235                                      lsqlite3->sqlite,
1236                                      pTail,
1237                                      -1,
1238                                      &stmt,
1239                                      &pTail)) != SQLITE_OK ||
1240                             (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
1241                             (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
1242                                 
1243                                 if (lsqlite3_debug & SQLITE3_DEBUG_INIT) {
1244                                         printf("%s\n",
1245                                                sqlite3_errmsg(lsqlite3->sqlite));
1246                                         printf("pTail = [%s]\n", pTail);
1247                                 }
1248                                         
1249                                 query_norows(lsqlite3, "ROLLBACK;");
1250                                 (void) sqlite3_close(lsqlite3->sqlite);
1251                                 return ret;
1252                         }
1253                 }
1254         } else {
1255                 /*
1256                  * Ensure that the database we opened is one of ours
1257                  */
1258                 if (query_int(lsqlite3,
1259                               &queryInt,
1260                               "SELECT "
1261                               "  (SELECT COUNT(*) = 2"
1262                               "     FROM sqlite_master "
1263                               "     WHERE type = 'table' "
1264                               "       AND name IN "
1265                               "         ("
1266                               "           'ldb_entry', "
1267                               "           'ldb_object_classes' "
1268                               "         ) "
1269                               "  ) "
1270                               "  AND "
1271                               "  (SELECT 1 "
1272                               "     FROM ldb_info "
1273                               "     WHERE database_type = 'LDB' "
1274                               "       AND version = '1.0'"
1275                               "  );") != 0 ||
1276                     queryInt != 1) {
1277                         
1278                         /* It's not one that we created.  See ya! */
1279                         query_norows(lsqlite3, "ROLLBACK;");
1280                         (void) sqlite3_close(lsqlite3->sqlite);
1281                         return SQLITE_MISUSE;
1282                 }
1283         }
1284         
1285         /*
1286          * Create a temporary table to hold attributes requested in the result
1287          * set of a search.
1288          */
1289         query_norows(lsqlite3, "DROP TABLE " RESULT_ATTR_TABLE ";\n");
1290         if ((ret =
1291              query_norows(lsqlite3,
1292                           "CREATE " TEMPTAB " TABLE " RESULT_ATTR_TABLE "\n"
1293                           " (\n"
1294                           "  attr_name TEXT PRIMARY KEY\n"
1295                           " );")) != 0) {
1296                 query_norows(lsqlite3, "ROLLBACK;");
1297                 return ret;
1298         }
1299
1300         /*
1301          * Create a temporary table to hold the attributes used by filters
1302          * during a search.
1303          */
1304         query_norows(lsqlite3, "DROP TABLE " FILTER_ATTR_TABLE ";\n");
1305         if ((ret =
1306              query_norows(lsqlite3,
1307                           "CREATE " TEMPTAB " TABLE " FILTER_ATTR_TABLE "\n"
1308                           " (\n"
1309                           "  attr_name TEXT PRIMARY KEY\n"
1310                           " );")) != 0) {
1311                 query_norows(lsqlite3, "ROLLBACK;");
1312                 return ret;
1313         }
1314
1315         /* Commit the transaction */
1316         if ((ret = query_norows(lsqlite3, "COMMIT;")) != 0) {
1317                 query_norows(lsqlite3, "ROLLBACK;");
1318                 return ret;
1319         }
1320         
1321         return SQLITE_OK;
1322 }
1323
1324 static int
1325 destructor(void *p)
1326 {
1327         struct lsqlite3_private *   lsqlite3 = p;
1328         
1329         if (lsqlite3->sqlite) {
1330                 sqlite3_close(lsqlite3->sqlite);
1331         }
1332         return 0;
1333 }
1334
1335
1336 /*
1337  * query_norows()
1338  *
1339  * This function is used for queries that are not expected to return any rows,
1340  * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
1341  * There are no provisions here for returning data from rows in a table, so do
1342  * not pass SELECT queries to this function.
1343  */
1344 static int
1345 query_norows(const struct lsqlite3_private *lsqlite3,
1346              const char *pSql,
1347              ...)
1348 {
1349         int             ret;
1350         int             bLoop;
1351         char *          p;
1352         sqlite3_stmt *  pStmt;
1353         va_list         args;
1354         double          t0;
1355         double          t1;
1356         struct timeval  tv;
1357         struct timezone tz;
1358         
1359         if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1360                 gettimeofday(&tv, &tz);
1361                 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1362         }
1363
1364         /* Begin access to variable argument list */
1365         va_start(args, pSql);
1366         
1367         /* Format the query */
1368         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
1369                 return -1;
1370         }
1371         
1372         /*
1373          * Prepare and execute the SQL statement.  Loop allows retrying on
1374          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1375          * requiring retrying the operation.
1376          */
1377         for (bLoop = TRUE; bLoop; ) {
1378                 
1379                 /* Compile the SQL statement into sqlite virtual machine */
1380                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1381                                            p,
1382                                            -1,
1383                                            &pStmt,
1384                                            NULL)) == SQLITE_SCHEMA) {
1385                         continue;
1386                 } else if (ret != SQLITE_OK) {
1387                         ret = -1;
1388                         break;
1389                 }
1390                 
1391                 /* No rows expected, so just step through machine code once */
1392                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
1393                         (void) sqlite3_finalize(pStmt);
1394                         continue;
1395                 } else if (ret != SQLITE_DONE) {
1396                         (void) sqlite3_finalize(pStmt);
1397                         ret = -1;
1398                         break;
1399                 }
1400                 
1401                 /* Free the virtual machine */
1402                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1403                         (void) sqlite3_finalize(pStmt);
1404                         continue;
1405                 } else if (ret != SQLITE_OK) {
1406                         (void) sqlite3_finalize(pStmt);
1407                         ret = -1;
1408                         break;
1409                 }
1410                 
1411                 /*
1412                  * Normal condition is only one time through loop.  Loop is
1413                  * rerun in error conditions, via "continue", above.
1414                  */
1415                 ret = 0;
1416                 bLoop = FALSE;
1417         }
1418         
1419         /* All done with variable argument list */
1420         va_end(args);
1421         
1422         if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1423                 gettimeofday(&tv, NULL);
1424                 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1425                 printf("%1.6lf %s\n%s\n\n", t1 - t0,
1426                        ret == 0 ? "SUCCESS" : "FAIL",
1427                        p);
1428         }
1429
1430         /* Free the memory we allocated for our query string */
1431         sqlite3_free(p);
1432         
1433         return ret;
1434 }
1435
1436
1437 /*
1438  * query_int()
1439  *
1440  * This function is used for the common case of queries that return a single
1441  * integer value.
1442  *
1443  * NOTE: If more than one value is returned by the query, all but the first
1444  * one will be ignored.
1445  */
1446 static int
1447 query_int(const struct lsqlite3_private * lsqlite3,
1448           long long * pRet,
1449           const char * pSql,
1450           ...)
1451 {
1452         int             ret;
1453         int             bLoop;
1454         char *          p;
1455         sqlite3_stmt *  pStmt;
1456         va_list         args;
1457         double          t0;
1458         double          t1;
1459         struct timeval  tv;
1460         struct timezone tz;
1461         
1462         if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1463                 gettimeofday(&tv, &tz);
1464                 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1465         }
1466
1467         /* Begin access to variable argument list */
1468         va_start(args, pSql);
1469         
1470         /* Format the query */
1471         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
1472                 return SQLITE_NOMEM;
1473         }
1474         
1475         if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1476                 printf("%s\n", p);
1477         }
1478
1479         /*
1480          * Prepare and execute the SQL statement.  Loop allows retrying on
1481          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1482          * requiring retrying the operation.
1483          */
1484         for (bLoop = TRUE; bLoop; ) {
1485                 
1486                 /* Compile the SQL statement into sqlite virtual machine */
1487                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1488                                            p,
1489                                            -1,
1490                                            &pStmt,
1491                                            NULL)) == SQLITE_SCHEMA) {
1492                         continue;
1493                 } else if (ret != SQLITE_OK) {
1494                         break;
1495                 }
1496                 
1497                 /* One row expected */
1498                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
1499                         (void) sqlite3_finalize(pStmt);
1500                         continue;
1501                 } else if (ret != SQLITE_ROW) {
1502                         (void) sqlite3_finalize(pStmt);
1503                         break;
1504                 }
1505                 
1506                 /* Get the value to be returned */
1507                 *pRet = sqlite3_column_int64(pStmt, 0);
1508                 
1509                 /* Free the virtual machine */
1510                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1511                         (void) sqlite3_finalize(pStmt);
1512                         continue;
1513                 } else if (ret != SQLITE_OK) {
1514                         (void) sqlite3_finalize(pStmt);
1515                         break;
1516                 }
1517                 
1518                 /*
1519                  * Normal condition is only one time through loop.  Loop is
1520                  * rerun in error conditions, via "continue", above.
1521                  */
1522                 bLoop = FALSE;
1523         }
1524         
1525         /* All done with variable argument list */
1526         va_end(args);
1527         
1528
1529         if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1530                 gettimeofday(&tv, NULL);
1531                 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1532                 printf("%1.6lf %s\n%s\n\n", t1 - t0,
1533                        ret == 0 ? "SUCCESS" : "FAIL",
1534                        p);
1535         }
1536
1537         /* Free the memory we allocated for our query string */
1538         sqlite3_free(p);
1539         
1540         return ret;
1541 }
1542
1543
1544 /*
1545   callback function used in call to ldb_dn_fold() for determining whether an
1546   attribute type requires case folding.
1547 */
1548 static int
1549 case_fold_attr_required(void * hUserData,
1550                         char *attr)
1551 {
1552 //        struct ldb_module * module = hUserData;
1553         
1554         return TRUE;
1555 }
1556
1557 static int
1558 case_fold_attr_not_required(void * hUserData,
1559                         char *attr)
1560 {
1561 //        struct ldb_module * module = hUserData;
1562         
1563         return FALSE;
1564 }
1565
1566
1567 /*
1568  * add a single set of ldap message values to a ldb_message
1569  */
1570
1571 static int
1572 add_msg_attr(void * hTalloc,
1573              long long eid,
1574              const char * pDN,
1575              const char * pAttrName,
1576              const char * pAttrValue,
1577              long long prevEID,
1578              int * pAllocated,
1579              struct ldb_message *** pppRes)
1580 {
1581         void *                       x;
1582         struct ldb_message *         msg;
1583         struct ldb_message_element * el;
1584         
1585         /* Is this a different EID than the previous one? */
1586         if (eid != prevEID) {
1587                 /* Yup.  Add another result to the result array */
1588                 if ((x = talloc_realloc(hTalloc,
1589                                         *pAllocated == 0 ? NULL : *pppRes,
1590                                         struct ldb_message *,
1591                                         *pAllocated + 1)) == NULL) {
1592                         
1593                         return -1;
1594                 }
1595                 
1596                 /* Save the new result list */
1597                 *pppRes = x;
1598
1599                 /* Allocate a new result structure */
1600                 if ((x = talloc(*pppRes, struct ldb_message)) == NULL) {
1601                         return -1;
1602                 }
1603
1604                 /* Save the new result */
1605                 (*pppRes)[*pAllocated] = x;
1606
1607                 /* Steal the initial result and put it in its own context */
1608                 talloc_steal(NULL, *pppRes);
1609
1610                 /* We've allocated one more result */
1611                 ++*pAllocated;
1612                 
1613                 /* Ensure that the message is initialized */
1614                 msg = x;
1615                 if ((msg->dn = talloc_strdup(msg, pDN)) == NULL) {
1616                         return -1;
1617                 }
1618                 msg->num_elements = 0;
1619                 msg->elements = NULL;
1620                 msg->private_data = NULL;
1621         } else {
1622                 /* Same EID.  Point to the previous most-recent message */
1623                 msg = (*pppRes)[*pAllocated - 1];
1624         }
1625         
1626         if (pAttrName != NULL && pAttrValue != NULL) {
1627             /*
1628              * Point to the most recent previous element.  (If there are none,
1629              * this will point to non-allocated memory, but the pointer will
1630              * never be dereferenced.)
1631              */
1632             el = &msg->elements[msg->num_elements - 1];
1633         
1634             /*
1635              * See if the most recent previous element has the same attr_name
1636              */
1637             if (msg->num_elements == 0 || strcmp(el->name, pAttrName) != 0) {
1638                 
1639                 /* It's a new attr_name.  Allocate another message element */
1640                 if ((el = talloc_realloc(msg,
1641                                          msg->elements,
1642                                          struct ldb_message_element, 
1643                                          msg->num_elements + 1)) == NULL) {
1644                     return -1;
1645                 }
1646                 
1647                 /* Save the new element */
1648                 msg->elements = el;
1649                 
1650                 /* Save the attribute name */
1651                 if ((el->name =
1652                      talloc_strdup(msg->elements, pAttrName)) == NULL) {
1653                         
1654                     return -1;
1655                 }
1656                 
1657                 /* There's now one additional element */
1658                 msg->num_elements++;
1659                 
1660                 /* No flags */
1661                 el->flags = 0;
1662                 
1663                 /* Initialize number of attribute values for this type */
1664                 el->num_values = 0;
1665                 el->values = NULL;
1666             }
1667         
1668             /* Increase the value array size by 1 */
1669             if ((el->values =
1670                  talloc_realloc(el,
1671                                 el->num_values == 0 ? NULL : el->values,
1672                                 struct ldb_val,
1673                                 el->num_values + 1)) == NULL) {
1674                 return -1;
1675             }
1676         
1677             /* Save the new attribute value length */
1678             el->values[el->num_values].length = strlen(pAttrValue);
1679         
1680             /* Copy the new attribute value */
1681             if ((el->values[el->num_values].data =
1682                  talloc_memdup(el->values,
1683                                pAttrValue,
1684                                el->values[el->num_values].length)) == NULL) {
1685                 return -1;
1686             }
1687         
1688             /* We now have one additional value of this type */
1689             el->num_values++;
1690         }
1691         
1692         return 0;
1693 }
1694
1695 static char *
1696 parsetree_to_sql(struct ldb_module *module,
1697                  char * hTalloc,
1698                  const struct ldb_parse_tree *t)
1699 {
1700         int                     i;
1701         char *                  pNormalizedDN;
1702         char *                  child;
1703         char *                  p;
1704         char *                  ret = NULL;
1705         char *                  pAttrName;
1706         
1707         
1708         switch(t->operation) {
1709         case LDB_OP_SIMPLE:
1710                 break;
1711                 
1712         case LDB_OP_EXTENDED:
1713 #warning  "work out how to handle bitops"
1714                 return NULL;
1715
1716         case LDB_OP_AND:
1717                 ret = parsetree_to_sql(module,
1718                                        hTalloc,
1719                                        t->u.list.elements[0]);
1720                 
1721                 for (i = 1; i < t->u.list.num_elements; i++) {
1722                         child =
1723                                 parsetree_to_sql(
1724                                         module,
1725                                         hTalloc,
1726                                         t->u.list.elements[i]);
1727                         ret = talloc_asprintf_append(ret,
1728                                                      "INTERSECT\n"
1729                                                      "%s\n",
1730                                                      child);
1731                         talloc_free(child);
1732                 }
1733                 
1734                 child = ret;
1735                 ret = talloc_asprintf(hTalloc,
1736                                       "SELECT * FROM (\n"
1737                                       "%s\n"
1738                                       ")\n",
1739                                       child);
1740                 talloc_free(child);
1741                 return ret;
1742                 
1743         case LDB_OP_OR:
1744                 ret = parsetree_to_sql(module,
1745                                        hTalloc,
1746                                        t->u.list.elements[0]);
1747                 
1748                 for (i = 1; i < t->u.list.num_elements; i++) {
1749                         child =
1750                                 parsetree_to_sql(
1751                                         module,
1752                                         hTalloc,
1753                                         t->u.list.elements[i]);
1754                         ret = talloc_asprintf_append(ret,
1755                                                      "UNION\n"
1756                                                      "%s\n",
1757                                                      child);
1758                         talloc_free(child);
1759                 }
1760                 child = ret;
1761                 ret = talloc_asprintf(hTalloc,
1762                                       "SELECT * FROM (\n"
1763                                       "%s\n"
1764                                       ")\n",
1765                                       child);
1766                 talloc_free(child);
1767                 return ret;
1768                 
1769         case LDB_OP_NOT:
1770                 child =
1771                         parsetree_to_sql(
1772                                 module,
1773                                 hTalloc,
1774                                 t->u.not.child);
1775                 ret = talloc_asprintf(hTalloc,
1776                                       "  SELECT eid\n"
1777                                       "    FROM ldb_entry\n"
1778                                       "    WHERE eid NOT IN (%s)\n",
1779                                       child);
1780                 talloc_free(child);
1781                 return ret;
1782                 
1783         default:
1784                 /* should never occur */
1785                 abort();
1786         };
1787         
1788         /* Get a case-folded copy of the attribute name */
1789         pAttrName = ldb_casefold((struct ldb_context *) module,
1790                                  t->u.simple.attr);
1791         
1792         /*
1793          * For simple searches, we want to retrieve the list of EIDs that
1794          * match the criteria.
1795          */
1796         if (t->u.simple.value.length == 1 &&
1797             (*(const char *) t->u.simple.value.data) == '*') {
1798                 /*
1799                  * Special case for "attr_name=*".  In this case, we want the
1800                  * eid corresponding to all values in the specified attribute
1801                  * table.
1802                  */
1803                 if ((p = sqlite3_mprintf("  SELECT eid\n"
1804                                          "    FROM ldb_attribute_values\n"
1805                                          "    WHERE attr_name = %Q",
1806                                      pAttrName)) == NULL) {
1807                         return NULL;
1808                 }
1809                 
1810                 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1811                         printf("%s\n", p);
1812                 }
1813
1814                 ret = talloc_strdup(hTalloc, p);
1815                 sqlite3_free(p);
1816
1817         } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
1818                 /*
1819                  * For object classes, we want to search for all objectclasses
1820                  * that are subclasses as well.
1821                  */
1822                 if ((p = sqlite3_mprintf(
1823                              "  SELECT eid\n"
1824                              "    FROM ldb_attribute_values\n"
1825                              "    WHERE attr_name = 'OBJECTCLASS' "
1826                              "      AND attr_value_normalized IN\n"
1827                              "      (SELECT class_name\n"
1828                              "         FROM ldb_object_classes\n"
1829                              "         WHERE tree_key GLOB\n"
1830                              "           (SELECT tree_key\n"
1831                              "              FROM ldb_object_classes\n"
1832                              "              WHERE class_name = upper(%Q)) "
1833                              "           || '*')\n",
1834                              t->u.simple.value.data)) == NULL) {
1835                         return NULL;
1836                 }
1837                 
1838                 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1839                         printf("%s\n", p);
1840                 }
1841
1842                 ret = talloc_strdup(hTalloc, p);
1843                 sqlite3_free(p);
1844                 
1845         } else if (strcasecmp(t->u.simple.attr, "dn") == 0) {
1846                 pNormalizedDN = ldb_dn_fold(module->ldb, t->u.simple.value.data,
1847                                   module, case_fold_attr_required);
1848                 if ((p = sqlite3_mprintf(
1849                              "  SELECT eid\n"
1850                              "    FROM ldb_attribute_values\n"
1851                              "    WHERE attr_name = %Q\n"
1852                              "      AND attr_value_normalized = %Q\n",
1853                              pAttrName,
1854                              pNormalizedDN)) == NULL) {
1855                         return NULL;
1856                 }
1857                 
1858                 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1859                         printf("%s\n", p);
1860                 }
1861
1862                 ret = talloc_strdup(hTalloc, p);
1863                 sqlite3_free(p);
1864         } else {
1865                 /* A normal query. */
1866                 if ((p = sqlite3_mprintf(
1867                              "  SELECT eid\n"
1868                              "    FROM ldb_attribute_values\n"
1869                              "    WHERE attr_name = %Q\n"
1870                              "      AND attr_value_normalized = upper(%Q)\n",
1871                              pAttrName,
1872                              t->u.simple.value.data)) == NULL) {
1873                         return NULL;
1874                 }
1875                 
1876                 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1877                         printf("%s\n", p);
1878                 }
1879
1880                 ret = talloc_strdup(hTalloc, p);
1881                 sqlite3_free(p);
1882         }
1883
1884         return ret;
1885 }
1886
1887
1888 static int
1889 parsetree_to_attrlist(struct ldb_module *module,
1890                       const struct ldb_parse_tree * t)
1891 {
1892         int                         i;
1893         struct lsqlite3_private *   lsqlite3 = module->private_data;
1894         
1895         switch(t->operation) {
1896         case LDB_OP_SIMPLE:
1897                 break;
1898                 
1899         case LDB_OP_EXTENDED:
1900 #warning  "work out how to handle bitops"
1901                 return -1;
1902
1903         case LDB_OP_AND:
1904                 if (parsetree_to_attrlist(
1905                             module,
1906                             t->u.list.elements[0]) != 0) {
1907                         return -1;
1908                 }
1909                 
1910                 for (i = 1; i < t->u.list.num_elements; i++) {
1911                         if (parsetree_to_attrlist(
1912                                     module,
1913                                     t->u.list.elements[i]) != 0) {
1914                                 return -1;
1915                         }
1916                 }
1917                 
1918                 return 0;
1919                 
1920         case LDB_OP_OR:
1921                 if (parsetree_to_attrlist(
1922                             module,
1923                             t->u.list.elements[0]) != 0) {
1924                         return -1;
1925                 }
1926                 
1927                 for (i = 1; i < t->u.list.num_elements; i++) {
1928                         if (parsetree_to_attrlist(
1929                                     module,
1930                                     t->u.list.elements[i]) != 0) {
1931                                 return -1;
1932                         }
1933                 }
1934                 
1935                 return 0;
1936                 
1937         case LDB_OP_NOT:
1938                 if (parsetree_to_attrlist(module,
1939                                           t->u.not.child) != 0) {
1940                         return -1;
1941                 }
1942                 
1943                 return 0;
1944                 
1945         default:
1946                 /* should never occur */
1947                 abort();
1948         };
1949         
1950         QUERY_NOROWS(lsqlite3,
1951                      FALSE,
1952                      "INSERT OR IGNORE INTO " FILTER_ATTR_TABLE "\n"
1953                      "    (attr_name)\n"
1954                      "  VALUES\n"
1955                      "    (%Q);",
1956                      t->u.simple.attr);
1957         return 0;
1958 }
1959
1960
1961 /*
1962  * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
1963  * requests in the ldb_message
1964  */
1965 static int
1966 msg_to_sql(struct ldb_module * module,
1967            const struct ldb_message * msg,
1968            long long eid,
1969            int use_flags)
1970 {
1971         int                         flags;
1972         char *                      pAttrName;
1973         unsigned int                i;
1974         unsigned int                j;
1975         struct lsqlite3_private *   lsqlite3 = module->private_data;
1976         
1977         for (i = 0; i < msg->num_elements; i++) {
1978                 const struct ldb_message_element *el = &msg->elements[i];
1979                 
1980                 if (! use_flags) {
1981                         flags = LDB_FLAG_MOD_ADD;
1982                 } else {
1983                         flags = el->flags & LDB_FLAG_MOD_MASK;
1984                 }
1985                 
1986                 /* Get a case-folded copy of the attribute name */
1987                 pAttrName = ldb_casefold((struct ldb_context *) module,
1988                                          el->name);
1989                 
1990                 /* For each value of the specified attribute name... */
1991                 for (j = 0; j < el->num_values; j++) {
1992                         
1993                         /* ... bind the attribute value, if necessary */
1994                         switch (flags) {
1995                         case LDB_FLAG_MOD_ADD:
1996                                 QUERY_NOROWS(
1997                                         lsqlite3,
1998                                         FALSE,
1999                                         "INSERT INTO ldb_attribute_values\n"
2000                                         "    (eid,\n"
2001                                         "     attr_name,\n"
2002                                         "     attr_value,\n"
2003                                         "     attr_value_normalized)\n"
2004                                         "  VALUES\n"
2005                                         "    (%lld, %Q, %Q, upper(%Q));",
2006                                         eid,
2007                                         pAttrName,
2008                                         el->values[j].data, /* FIX ME */
2009                                         el->values[j].data);
2010
2011                                 /* Is this a special "objectclass"? */
2012                                 if (strcasecmp(pAttrName,
2013                                                "objectclass") != 0) {
2014                                         /* Nope. */
2015                                         break;
2016                                 }
2017
2018                                 /* Handle special "objectclass" type */
2019                                 QUERY_NOROWS(lsqlite3,
2020                                              FALSE,
2021                                              "INSERT OR IGNORE "
2022                                              "  INTO ldb_object_classes "
2023                                              "    (class_name, "
2024                                              "     parent_class_name) "
2025                                              "  VALUES "
2026                                              "    (upper(%Q), 'TOP');",
2027                                              ldb_casefold(module,
2028                                                           el->values[j].data));
2029                                 break;
2030                                 
2031                         case LDB_FLAG_MOD_REPLACE:
2032                                 QUERY_NOROWS(
2033                                         lsqlite3,
2034                                         FALSE,
2035                                         "UPDATE ldb_attribute_values\n"
2036                                         "  SET attr_value = %Q,\n"
2037                                         "      attr_value_normalized =\n"
2038                                         "          upper(%Q)\n"
2039                                         "  WHERE eid = %lld\n"
2040                                         "    AND attr_name = %Q;",
2041                                         el->values[j].data, /* FIX ME */
2042                                         el->values[j].data,
2043                                         eid,
2044                                         pAttrName);
2045                                 break;
2046                                 
2047                         case LDB_FLAG_MOD_DELETE:
2048                                 /* No additional parameters to this query */
2049                                 QUERY_NOROWS(
2050                                         lsqlite3,
2051                                         FALSE,
2052                                         "DELETE FROM ldb_attribute_values"
2053                                         "  WHERE eid = %lld "
2054                                         "    AND attr_name = %Q "
2055                                         "    AND attr_value_normalized =\n"
2056                                         "            upper(%Q);",
2057                                         eid,
2058                                         el->name,
2059                                         el->values[j].data);
2060                                 break;
2061                         }
2062                 }
2063         }
2064         
2065         return 0;
2066 }
2067
2068
2069
2070 static int
2071 new_dn(struct ldb_module * module,
2072        char * pDN,
2073        long long * pEID)
2074 {
2075         int                         nComponent;
2076         int                         bFirst;
2077         char *                      p;
2078         char *                      pPartialDN;
2079         char *                      pPartialNormalizedDN;
2080         long long                   eid;
2081         long long                   peid;
2082         double                      t0 = 0;
2083         double                      t1 = 0;
2084         struct timeval              tv;
2085         struct timezone             tz;
2086         struct ldb_dn *             pExplodedDN;
2087         struct ldb_dn_component *   pComponent;
2088         struct ldb_context *        ldb = module->ldb;
2089         struct lsqlite3_private *   lsqlite3 = module->private_data;
2090         
2091         if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2092                 gettimeofday(&tv, &tz);
2093                 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
2094         }
2095
2096         /* Explode the DN */
2097         if ((pExplodedDN =
2098              ldb_explode_dn(ldb,
2099                             pDN,
2100                             ldb,
2101                             case_fold_attr_not_required)) == NULL) {
2102                 return -1;
2103         }
2104         
2105         if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2106                 gettimeofday(&tv, NULL);
2107                 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
2108                 printf("%1.6lf loc 1\n", t1 - t0);
2109                 t0 = t1;
2110         }
2111
2112         /* Allocate a string to hold the partial DN of each component */
2113         if ((pPartialDN = talloc_strdup(ldb, "")) == NULL) {
2114                 return -1;
2115         }
2116         
2117         if ((pPartialNormalizedDN = talloc_strdup(pPartialDN, "")) == NULL) {
2118                 return -1;
2119         }
2120         
2121         /* For each component of the DN (starting with the last one)... */
2122 #warning "convert this loop to recursive, and search backwards instead"
2123         eid = 0;
2124
2125         for (nComponent = pExplodedDN->comp_num - 1, bFirst = TRUE;
2126              nComponent >= 0;
2127              nComponent--, bFirst = FALSE) {
2128                 
2129                 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2130                         gettimeofday(&tv, NULL);
2131                         t1 = ((double) tv.tv_sec +
2132                               ((double) tv.tv_usec / 1000000.0));
2133                         printf("%1.6lf loc 2\n", t1 - t0);
2134                         t0 = t1;
2135                 }
2136                 
2137                 /* Point to the component */
2138                 pComponent = pExplodedDN->components[nComponent];
2139                 
2140                 /* Add this component on to the partial DN to date */
2141                 if ((p = talloc_asprintf(ldb,
2142                                          "%s%s%s",
2143                                          pComponent->component,
2144                                          bFirst ? "" : ",",
2145                                          pPartialDN)) == NULL) {
2146                         return -1;
2147                 }
2148                 
2149                 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2150                         gettimeofday(&tv, NULL);
2151                         t1 = ((double) tv.tv_sec +
2152                               ((double) tv.tv_usec / 1000000.0));
2153                         printf("%1.6lf loc 3\n", t1 - t0);
2154                         t0 = t1;
2155                 }
2156                 
2157                 /* No need for the old partial DN any more */
2158                 talloc_free(pPartialDN);
2159                 
2160                 /* Save the new partial DN */
2161                 pPartialDN = p;
2162                 pPartialNormalizedDN = ldb_dn_fold(pPartialDN, p, module, case_fold_attr_required);
2163                 
2164                 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2165                         gettimeofday(&tv, NULL);
2166                         t1 = ((double) tv.tv_sec +
2167                               ((double) tv.tv_usec / 1000000.0));
2168                         printf("%1.6lf loc 4\n", t1 - t0);
2169                         t0 = t1;
2170                 }
2171                 
2172                 /*
2173                  * Ensure that an entry is in the ldb_entry table for this
2174                  * component.  Any component other than the last one
2175                  * (component 0) may already exist.  It is an error if
2176                  * component 0 (the full DN requested to be be inserted)
2177                  * already exists.
2178                  */
2179                 QUERY_NOROWS(lsqlite3,
2180                              FALSE,
2181                              "INSERT %s INTO ldb_entry\n"
2182                              "    (peid, dn, normalized_dn)\n"
2183                              "  VALUES\n"
2184                              "    (%lld, %Q, %Q);",
2185                              nComponent == 0 ? "" : "OR IGNORE",
2186                              eid, pPartialDN, pPartialNormalizedDN);
2187                 
2188                 /* Save the parent EID */
2189                 peid = eid;
2190                 
2191                 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2192                         gettimeofday(&tv, NULL);
2193                         t1 = ((double) tv.tv_sec +
2194                               ((double) tv.tv_usec / 1000000.0));
2195                         printf("%1.6lf loc 5\n", t1 - t0);
2196                         t0 = t1;
2197                 }
2198                 
2199                 /* Get the EID of the just inserted row */
2200                 QUERY_INT(lsqlite3,
2201                           eid,
2202                           FALSE,
2203                           "SELECT eid "
2204                           "  FROM ldb_entry "
2205                           "  WHERE normalized_dn = %Q;",
2206                           pPartialNormalizedDN);
2207
2208                 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2209                         gettimeofday(&tv, NULL);
2210                         t1 = ((double) tv.tv_sec +
2211                               ((double) tv.tv_usec / 1000000.0));
2212                         printf("%1.6lf loc 8\n", t1 - t0);
2213                         t0 = t1;
2214                 }
2215                 
2216                 /* Also add DN attribute */
2217                 QUERY_NOROWS(lsqlite3,
2218                              FALSE,
2219                              "INSERT %s INTO ldb_attribute_values\n"
2220                              "    (eid,\n"
2221                              "     attr_name,\n"
2222                              "     attr_value,\n"
2223                              "     attr_value_normalized) "
2224                              "  VALUES "
2225                              "    (%lld, 'DN', %Q, %Q);",
2226                              nComponent == 0 ? "" : "OR IGNORE",
2227                              eid,
2228                              pPartialDN, /* FIX ME */
2229                              pPartialNormalizedDN);
2230         }
2231         
2232         if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2233                 gettimeofday(&tv, NULL);
2234                 t1 = ((double) tv.tv_sec +
2235                       ((double) tv.tv_usec / 1000000.0));
2236                 printf("%1.6lf loc 9\n", t1 - t0);
2237                 t0 = t1;
2238         }
2239                 
2240         /* Give 'em what they came for! */
2241         *pEID = eid;
2242         
2243         return 0;
2244 }
2245
2246
2247 static unsigned char        base160tab[161] = {
2248         48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
2249         58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
2250         73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
2251         83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
2252         99 ,100,101,102,103,104,105,106,107,108, /* c-l */
2253         109,110,111,112,113,114,115,116,117,118, /* m-v */
2254         119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
2255         166,167,168,169,170,171,172,173,174,175, /* latin1 */
2256         176,177,178,179,180,181,182,183,184,185, /* latin1 */
2257         186,187,188,189,190,191,192,193,194,195, /* latin1 */
2258         196,197,198,199,200,201,202,203,204,205, /* latin1 */
2259         206,207,208,209,210,211,212,213,214,215, /* latin1 */
2260         216,217,218,219,220,221,222,223,224,225, /* latin1 */
2261         226,227,228,229,230,231,232,233,234,235, /* latin1 */
2262         236,237,238,239,240,241,242,243,244,245, /* latin1 */
2263         246,247,248,249,250,251,252,253,254,255, /* latin1 */
2264         '\0'
2265 };
2266
2267
2268 /*
2269  * base160()
2270  *
2271  * Convert an unsigned long integer into a base160 representation of the
2272  * number.
2273  *
2274  * Parameters:
2275  *   val --
2276  *     value to be converted
2277  *
2278  *   result --
2279  *     character array, 5 bytes long, into which the base160 representation
2280  *     will be placed.  The result will be a four-digit representation of the
2281  *     number (with leading zeros prepended as necessary), and null
2282  *     terminated.
2283  *
2284  * Returns:
2285  *   Nothing
2286  */
2287 static void
2288 base160_sql(sqlite3_context * hContext,
2289             int argc,
2290             sqlite3_value ** argv)
2291 {
2292     int             i;
2293     long long       val;
2294     char            result[5];
2295
2296     val = sqlite3_value_int64(argv[0]);
2297
2298     for (i = 3; i >= 0; i--) {
2299         
2300         result[i] = base160tab[val % 160];
2301         val /= 160;
2302     }
2303
2304     result[4] = '\0';
2305
2306     sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
2307 }
2308
2309
2310 /*
2311  * base160next_sql()
2312  *
2313  * This function enhances sqlite by adding a "base160_next()" function which is
2314  * accessible via queries.
2315  *
2316  * Retrieve the next-greater number in the base160 sequence for the terminal
2317  * tree node (the last four digits).  Only one tree level (four digits) is
2318  * operated on.
2319  *
2320  * Input:
2321  *   A character string: either an empty string (in which case no operation is
2322  *   performed), or a string of base160 digits with a length of a multiple of
2323  *   four digits.
2324  *
2325  * Output:
2326  *   Upon return, the trailing four digits (one tree level) will have been
2327  *   incremented by 1.
2328  */
2329 static void
2330 base160next_sql(sqlite3_context * hContext,
2331                 int argc,
2332                 sqlite3_value ** argv)
2333 {
2334         int                         i;
2335         int                         len;
2336         unsigned char *             pTab;
2337         unsigned char *             pBase160 =
2338                 strdup(sqlite3_value_text(argv[0]));
2339         unsigned char *             pStart = pBase160;
2340
2341         /*
2342          * We need a minimum of four digits, and we will always get a multiple
2343          * of four digits.
2344          */
2345         if (pBase160 != NULL &&
2346             (len = strlen(pBase160)) >= 4 &&
2347             len % 4 == 0) {
2348
2349                 if (pBase160 == NULL) {
2350
2351                         sqlite3_result_null(hContext);
2352                         return;
2353                 }
2354
2355                 pBase160 += strlen(pBase160) - 1;
2356
2357                 /* We only carry through four digits: one level in the tree */
2358                 for (i = 0; i < 4; i++) {
2359
2360                         /* What base160 value does this digit have? */
2361                         pTab = strchr(base160tab, *pBase160);
2362
2363                         /* Is there a carry? */
2364                         if (pTab < base160tab + sizeof(base160tab) - 1) {
2365
2366                                 /*
2367                                  * Nope.  Just increment this value and we're
2368                                  * done.
2369                                  */
2370                                 *pBase160 = *++pTab;
2371                                 break;
2372                         } else {
2373
2374                                 /*
2375                                  * There's a carry.  This value gets
2376                                  * base160tab[0], we decrement the buffer
2377                                  * pointer to get the next higher-order digit,
2378                                  * and continue in the loop.
2379                                  */
2380                                 *pBase160-- = base160tab[0];
2381                         }
2382                 }
2383
2384                 sqlite3_result_text(hContext,
2385                                     pStart,
2386                                     strlen(pStart),
2387                                     free);
2388         } else {
2389                 sqlite3_result_value(hContext, argv[0]);
2390                 if (pBase160 != NULL) {
2391                         free(pBase160);
2392                 }
2393         }
2394 }
2395
2396
2397 #ifdef DEBUG_LOCKS
2398 static int lock_debug(struct ldb_module * module,
2399                       const char * lockname,
2400                       const char * pFileName,
2401                       int linenum)
2402 {
2403         int                         ret;
2404         struct lsqlite3_private *   lsqlite3 = module->private_data;
2405
2406         printf("%s(%d): LOCK (%d) ",
2407                pFileName, linenum, lsqlite3->lock_count);
2408         ret = lsqlite3_lock(module, lockname);
2409         printf("got %d\n", ret);
2410
2411         return ret;
2412 }
2413                       
2414
2415 static int unlock_debug(struct ldb_module * module,
2416                         const char * lockname,
2417                         const char * pFileName,
2418                         int linenum)
2419 {
2420         int                         ret;
2421         struct lsqlite3_private *   lsqlite3 = module->private_data;
2422
2423         ret = lsqlite3_unlock(module, lockname);
2424         printf("%s(%d): UNLOCK (%d) got %d\n",
2425                pFileName, linenum, lsqlite3->lock_count, ret);
2426
2427         return ret;
2428 }
2429 #endif