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