r7558: added support in ldb for extended ldap search requests. These are
[metze/samba/wip.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 QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...)        \
52     do {                                                        \
53             if (query_norows(lsqlite3, sql) != 0) {             \
54                 if (bRollbackOnError) {                         \
55                         query_norows(lsqlite3,                  \
56                                        "ROLLBACK;");            \
57                 }                                               \
58                 return -1;                                      \
59         }                                                       \
60     } while (0)
61
62 #define QUERY_INT(lsqlite3, result_var, bRollbackOnError, sql...)       \
63     do {                                                                \
64             if (query_int(lsqlite3, &result_var, sql) != 0) {           \
65                     if (bRollbackOnError) {                             \
66                             query_norows(lsqlite3,                      \
67                                                   "ROLLBACK;");         \
68                     }                                                   \
69                     return -1;                                          \
70             }                                                           \
71     } while (0)
72
73
74 /*
75  * Forward declarations
76  */
77 static int
78 lsqlite3_rename(struct ldb_module * module,
79                 const char * olddn,
80                 const char * newdn);
81
82 static int
83 lsqlite3_delete(struct ldb_module *module,
84                 const char *dn);
85
86 static int
87 lsqlite3_search(struct ldb_module * module,
88                 const char * pBaseDN,
89                 enum ldb_scope scope,
90                 const char * pExpression,
91                 const char * const attrs[],
92                 struct ldb_message *** res);
93
94 static int
95 lsqlite3_add(struct ldb_module *module,
96              const struct ldb_message *msg);
97
98 static int
99 lsqlite3_modify(struct ldb_module *module,
100                 const struct ldb_message *msg);
101
102 static int
103 lsqlite3_lock(struct ldb_module *module,
104               const char *lockname);
105
106 static int
107 lsqlite3_unlock(struct ldb_module *module,
108                 const char *lockname);
109
110 static const char *
111 lsqlite3_errstring(struct ldb_module *module);
112
113 static int
114 initialize(struct lsqlite3_private *lsqlite3,
115            const char *url);
116
117 static int
118 destructor(void *p);
119
120 static int
121 query_norows(const struct lsqlite3_private *lsqlite3,
122              const char *pSql,
123              ...);
124
125 static int
126 query_int(const struct lsqlite3_private * lsqlite3,
127           long long * pRet,
128           const char * pSql,
129           ...);
130
131 static int case_fold_attr_required(void * hUserData,
132                                    char *attr);
133
134 static char *
135 parsetree_to_sql(struct ldb_module *module,
136                           char * hTalloc,
137                  const struct ldb_parse_tree *t);
138
139 static char *
140 parsetree_to_tablelist(struct ldb_module *module,
141                        char * hTalloc,
142                        const struct ldb_parse_tree *t);
143
144 static int
145 msg_to_sql(struct ldb_module * module,
146            const struct ldb_message * msg,
147            long long eid,
148            int use_flags);
149
150 static int
151 new_dn(struct ldb_module * module,
152        char * pDN,
153        long long * pEID);
154
155 static int
156 new_attr(struct ldb_module * module,
157          char * pAttrName);
158
159
160 /*
161  * Table of operations for the sqlite3 backend
162  */
163 static const struct ldb_module_ops lsqlite3_ops = {
164         .name          = "sqlite",
165         .search        = lsqlite3_search,
166         .search_bytree = lsqlite3_search_bytree,
167         .add_record    = lsqlite3_add,
168         .modify_record = lsqlite3_modify,
169         .delete_record = lsqlite3_delete,
170         .rename_record = lsqlite3_rename,
171         .named_lock    = lsqlite3_lock,
172         .named_unlock  = lsqlite3_unlock,
173         .errstring     = lsqlite3_errstring
174 };
175
176
177
178
179 /*
180  * Public functions
181  */
182
183
184 /*
185  * connect to the database
186  */
187 struct ldb_context *
188 lsqlite3_connect(const char *url, 
189                  unsigned int flags, 
190                  const char *options[])
191 {
192         int                         i;
193         int                         ret;
194         struct ldb_context *        ldb = NULL;
195         struct lsqlite3_private *   lsqlite3 = NULL;
196
197         ldb = talloc(NULL, struct ldb_context);
198         if (!ldb) {
199                 errno = ENOMEM;
200                 goto failed;
201         }
202
203         lsqlite3 = talloc(ldb, struct lsqlite3_private);
204         if (!lsqlite3) {
205                 errno = ENOMEM;
206                 goto failed;
207         }
208
209         lsqlite3->sqlite = NULL;
210         lsqlite3->options = NULL;
211         lsqlite3->lock_count = 0;
212
213         ret = initialize(lsqlite3, url);
214         if (ret != SQLITE_OK) {
215                 goto failed;
216         }
217
218         talloc_set_destructor(lsqlite3, destructor);
219
220         ldb->modules = talloc(ldb, struct ldb_module);
221         if (!ldb->modules) {
222                 errno = ENOMEM;
223                 goto failed;
224         }
225         ldb->modules->ldb = ldb;
226         ldb->modules->prev = ldb->modules->next = NULL;
227         ldb->modules->private_data = lsqlite3;
228         ldb->modules->ops = &lsqlite3_ops;
229
230         if (options) {
231                 /*
232                  * take a copy of the options array, so we don't have to rely
233                  * on the caller keeping it around (it might be dynamic)
234                  */
235                 for (i=0;options[i];i++) ;
236
237                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
238                 if (!lsqlite3->options) {
239                         goto failed;
240                 }
241                 
242                 for (i=0;options[i];i++) {
243
244                         lsqlite3->options[i+1] = NULL;
245                         lsqlite3->options[i] =
246                                 talloc_strdup(lsqlite3->options, options[i]);
247                         if (!lsqlite3->options[i]) {
248                                 goto failed;
249                         }
250                 }
251         }
252
253         return ldb;
254
255 failed:
256         if (lsqlite3->sqlite != NULL) {
257                 (void) sqlite3_close(lsqlite3->sqlite);
258         }
259         talloc_free(ldb);
260         return NULL;
261 }
262
263
264 /*
265  * Interface functions referenced by lsqlite3_ops
266  */
267
268 /* rename a record */
269 static int
270 lsqlite3_rename(struct ldb_module * module,
271                 const char * olddn,
272                 const char * newdn)
273 {
274         /* ignore ltdb specials */
275         if (olddn[0] == '@' ||newdn[0] == '@') {
276                 return 0;
277         }
278
279 #warning "lsqlite3_rename() is not yet supported"
280         return -1;
281 }
282
283 /* delete a record */
284 static int
285 lsqlite3_delete(struct ldb_module *module,
286                 const char *dn)
287 {
288         /* ignore ltdb specials */
289         if (dn[0] == '@') {
290                 return 0;
291         }
292         
293 #warning "lsqlite3_delete() is not yet supported"
294         return -1;
295 }
296
297 /* search for matching records */
298 static int
299 lsqlite3_search(struct ldb_module * module,
300                 const char * pBaseDN,
301                 enum ldb_scope scope,
302                 const char * pExpression,
303                 const char * const attrs[],
304                 struct ldb_message *** res)
305 {
306         int                         ret;
307         int                         bLoop;
308         long long                   eid = 0;
309         long long                   prevEID;
310         char *                      pSql = NULL;
311         char *                      pSqlConstraints;
312         char *                      pTableList;
313         char *                      hTalloc;
314         const char *                pDN;
315         const char *                pAttrName;
316         const char *                pAttrValue;
317         sqlite3_stmt *              pStmt;
318         struct ldb_parse_tree *     pTree;
319         struct lsqlite3_private *   lsqlite3 = module->private_data;
320         
321         if (pBaseDN == NULL) {
322                 pBaseDN = "";
323         }
324
325         /* Begin a transaction */
326         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN IMMEDIATE;");
327
328         /*
329          * Obtain the eid of the base DN
330          */
331         QUERY_INT(lsqlite3,
332                   eid,
333                   TRUE,
334                   "SELECT eid "
335                   "  FROM ldb_attr_dn "
336                   "  WHERE attr_value = %Q;",
337                   pBaseDN);
338
339         /* Parse the filter expression into a tree we can work with */
340         if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
341                 return -1;
342         }
343         
344         /* Allocate a temporary talloc context */
345         hTalloc = talloc_new(module->ldb);
346
347         /* Move the parse tree to our temporary context */
348         talloc_steal(hTalloc, pTree);
349         
350         /* Convert filter into a series of SQL statements (constraints) */
351         pSqlConstraints = parsetree_to_sql(module, hTalloc, pTree);
352         
353         /* Get the list of attribute names to use as our extra table list */
354         pTableList = parsetree_to_tablelist(module, hTalloc, pTree);
355
356         switch(scope) {
357         case LDB_SCOPE_DEFAULT:
358         case LDB_SCOPE_SUBTREE:
359                 pSql = sqlite3_mprintf(
360                         "SELECT entry.eid,\n"
361                         "       entry.dn,\n"
362                         "       av.attr_name,\n"
363                         "       av.attr_value\n"
364                         "  FROM ldb_entry AS entry,\n"
365                         "       ldb_attribute_values AS av\n"
366                         "  WHERE entry.eid IN\n"
367                         "    (SELECT DISTINCT ldb_entry.eid\n"
368                         "       FROM ldb_entry,\n"
369                         "            ldb_descendants,\n"
370                         "            %q\n"
371                         "       WHERE ldb_descendants.aeid = %lld\n"
372                         "         AND ldb_entry.eid = ldb_descendants.deid\n"
373                         "         AND ldap_entry.eid IN\n%s\n"
374                         "    ) "
375                         "    AND av.eid = entry.eid "
376                         "  ORDER BY av.eid, av.attr_name;",
377                         pTableList,
378                         eid,
379                         pSqlConstraints);
380                 break;
381
382         case LDB_SCOPE_BASE:
383                 pSql = sqlite3_mprintf(
384                         "SELECT entry.eid,\n"
385                         "       entry.dn,\n"
386                         "       av.attr_name,\n"
387                         "       av.attr_value\n"
388                         "  FROM ldb_entry AS entry,\n"
389                         "       ldb_attribute_values AS av\n"
390                         "  WHERE entry.eid IN\n"
391                         "    (SELECT DISTINCT ldb_entry.eid\n"
392                         "       FROM %q\n"
393                         "       WHERE ldb_entry.eid = %lld\n"
394                         "         AND ldb_entry.eid IN\n%s\n"
395                         "    ) "
396                         "    AND av.eid = entry.eid "
397                         "  ORDER BY av.eid, av.attr_name;",
398                         pTableList,
399                         eid,
400                         pSqlConstraints);
401                 break;
402
403         case LDB_SCOPE_ONELEVEL:
404                 pSql = sqlite3_mprintf(
405                         "SELECT entry.eid,\n"
406                         "       entry.dn,\n"
407                         "       av.attr_name,\n"
408                         "       av.attr_value\n"
409                         "  FROM ldb_entry AS entry,\n"
410                         "       ldb_attribute_values AS av\n"
411                         "  WHERE entry.eid IN\n"
412                         "    (SELECT DISTINCT ldb_entry.eid\n"
413                         "       FROM ldb_entry AS pchild, "
414                         "            %q\n"
415                         "       WHERE ldb_entry.eid = pchild.eid "
416                         "         AND pchild.peid = %lld "
417                         "         AND ldb_entry.eid IN\n%s\n"
418                         "    ) "
419                         "    AND av.eid = entry.eid "
420                         "  ORDER BY av.eid, av.attr_name;",
421                         pTableList,
422                         eid,
423                         pSqlConstraints);
424                 break;
425         }
426
427         /* Initially, we have no old eid */
428         prevEID = -1;
429
430         /*
431          * Prepare and execute the SQL statement.  Loop allows retrying on
432          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
433          * requiring retrying the operation.
434          */
435         for (bLoop = TRUE; bLoop; ) {
436
437                 /* Compile the SQL statement into sqlite virtual machine */
438                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
439                                            pSql,
440                                            -1,
441                                            &pStmt,
442                                            NULL)) == SQLITE_SCHEMA) {
443                         continue;
444                 } else if (ret != SQLITE_OK) {
445                         ret = -1;
446                         break;
447                 }
448                 
449                 /* Loop through the returned rows */
450                 do {
451                         /* Get the next row */
452                         if ((ret = sqlite3_step(pStmt)) == SQLITE_ROW) {
453
454                                 /* Get the values from this row */
455                                 eid = sqlite3_column_int64(pStmt, 0);
456                                 pDN = sqlite3_column_text(pStmt, 1);
457                                 pAttrName = sqlite3_column_text(pStmt, 2);
458                                 pAttrValue = sqlite3_column_text(pStmt, 3);
459                                 
460                                 /* Add this row data to the return results */
461 #warning "finish returning the result set of the search here"
462                         }
463                 } while (ret == SQLITE_ROW);
464
465                 if (ret == SQLITE_SCHEMA) {
466                         (void) sqlite3_finalize(pStmt);
467                         continue;
468                 } else if (ret != SQLITE_DONE) {
469                         (void) sqlite3_finalize(pStmt);
470                         ret = -1;
471                         break;
472                 }
473
474                 /* Free the virtual machine */
475                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
476                         (void) sqlite3_finalize(pStmt);
477                         continue;
478                 } else if (ret != SQLITE_OK) {
479                         (void) sqlite3_finalize(pStmt);
480                         ret = -1;
481                         break;
482                 }
483
484                 /*
485                  * Normal condition is only one time through loop.  Loop is
486                  * rerun in error conditions, via "continue", above.
487                  */
488                 ret = 0;
489                 bLoop = FALSE;
490         }
491
492
493         /* End the transaction */
494         QUERY_NOROWS(lsqlite3, FALSE, "END TRANSACTION;");
495
496         /* We're alll done with this query */
497         sqlite3_free(pSql);
498
499         return ret;
500 }
501
502
503 /* add a record */
504 static int
505 lsqlite3_add(struct ldb_module *module,
506              const struct ldb_message *msg)
507 {
508         long long                   eid;
509         struct lsqlite3_private *   lsqlite3 = module->private_data;
510
511         /* ignore ltdb specials */
512         if (msg->dn[0] == '@') {
513                 return 0;
514         }
515
516         /* Begin a transaction */
517         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
518
519         /*
520          * Build any portions of the directory tree that don't exist.  If the
521          * final component already exists, it's an error.
522          */
523         if (new_dn(module, msg->dn, &eid) != 0) {
524                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
525                 return -1;
526         }
527
528         /* Add attributes to this new entry */
529         if (msg_to_sql(module, msg, eid, FALSE) != 0) {
530                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
531                 return -1;
532         }
533
534         /* Everything worked.  Commit it! */
535         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
536         return 0;
537 }
538
539
540 /* modify a record */
541 static int
542 lsqlite3_modify(struct ldb_module *module,
543                 const struct ldb_message *msg)
544 {
545         struct lsqlite3_private *   lsqlite3 = module->private_data;
546
547         /* ignore ltdb specials */
548         if (msg->dn[0] == '@') {
549                 return 0;
550         }
551
552         /* Begin a transaction */
553         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
554
555         /* Everything worked.  Commit it! */
556         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
557         return 0 ;
558 }
559
560 /* obtain a named lock */
561 static int
562 lsqlite3_lock(struct ldb_module *module,
563               const char *lockname)
564 {
565         if (lockname == NULL) {
566                 return -1;
567         }
568
569         /* TODO implement a local locking mechanism here */
570
571         return 0;
572 }
573
574 /* release a named lock */
575 static int
576 lsqlite3_unlock(struct ldb_module *module,
577                 const char *lockname)
578 {
579         if (lockname == NULL) {
580                 return -1;
581         }
582
583         /* TODO implement a local locking mechanism here */
584
585         return 0;
586 }
587
588 /* return extended error information */
589 static const char *
590 lsqlite3_errstring(struct ldb_module *module)
591 {
592         struct lsqlite3_private *   lsqlite3 = module->private_data;
593
594         return sqlite3_errmsg(lsqlite3->sqlite);
595 }
596
597
598
599
600 /*
601  * Static functions
602  */
603
604 static int
605 initialize(struct lsqlite3_private *lsqlite3,
606            const char *url)
607 {
608         int             ret;
609         long long       queryInt;
610         const char *    pTail;
611         sqlite3_stmt *  stmt;
612         const char *    schema =       
613                 "-- ------------------------------------------------------"
614
615                 "PRAGMA auto_vacuum=1;"
616
617                 "-- ------------------------------------------------------"
618
619                 "BEGIN EXCLUSIVE;"
620
621                 "-- ------------------------------------------------------"
622
623                 "CREATE TABLE ldb_info AS"
624                 "  SELECT 'LDB' AS database_type,"
625                 "         '1.0' AS version;"
626
627                 "-- ------------------------------------------------------"
628                 "-- Schema"
629
630                 "/*"
631                 " * The entry table holds the information about an entry. "
632                 " * This table is used to obtain the EID of the entry and to "
633                 " * support scope=one and scope=base.  The parent and child"
634                 " * table is included in the entry table since all the other"
635                 " * attributes are dependent on EID."
636                 " */"
637                 "CREATE TABLE ldb_entry"
638                 "("
639                 "  -- Unique identifier of this LDB entry"
640                 "  eid                   INTEGER PRIMARY KEY,"
641
642                 "  -- Unique identifier of the parent LDB entry"
643                 "  peid                  INTEGER REFERENCES ldb_entry,"
644
645                 "  -- Distinguished name of this entry"
646                 "  dn                    TEXT,"
647
648                 "  -- Time when the entry was created"
649                 "  create_timestamp      INTEGER,"
650
651                 "  -- Time when the entry was last modified"
652                 "  modify_timestamp      INTEGER"
653                 ");"
654
655
656                 "/*"
657                 " * The purpose of the descendant table is to support the"
658                 " * subtree search feature.  For each LDB entry with a unique"
659                 " * ID (AEID), this table contains the unique identifiers"
660                 " * (DEID) of the descendant entries."
661                 " *"
662                 " * For evern entry in the directory, a row exists in this"
663                 " * table for each of its ancestors including itself.  The "
664                 " * size of the table depends on the depth of each entry.  In "
665                 " * the worst case, if all the entries were at the same "
666                 " * depth, the number of rows in the table is O(nm) where "
667                 " * n is the number of nodes in the directory and m is the "
668                 " * depth of the tree. "
669                 " */"
670                 "CREATE TABLE ldb_descendants"
671                 "("
672                 "  -- The unique identifier of the ancestor LDB entry"
673                 "  aeid                  INTEGER REFERENCES ldb_entry,"
674
675                 "  -- The unique identifier of the descendant LDB entry"
676                 "  deid                  INTEGER REFERENCES ldb_entry"
677                 ");"
678
679
680                 "CREATE TABLE ldb_object_classes"
681                 "("
682                 "  -- Object classes are inserted into this table to track"
683                 "  -- their class hierarchy.  'top' is the top-level class"
684                 "  -- of which all other classes are subclasses."
685                 "  class_name            TEXT PRIMARY KEY,"
686
687                 "  -- tree_key tracks the position of the class in"
688                 "  -- the hierarchy"
689                 "  tree_key              TEXT UNIQUE"
690                 ");"
691
692                 "/*"
693                 " * We keep a full listing of attribute/value pairs here"
694                 " */"
695                 "CREATE TABLE ldb_attribute_values"
696                 "("
697                 "  eid                   INTEGER REFERENCES ldb_entry,"
698                 "  attr_name             TEXT, -- see ldb_attr_ATTRIBUTE_NAME"
699                 "  attr_value            TEXT"
700                 ");"
701
702                 "/*"
703                 " * There is one attribute table per searchable attribute."
704                 " */"
705                 "/*"
706                 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
707                 "("
708                 "  -- The unique identifier of the LDB entry"
709                 "  eid                   INTEGER REFERENCES ldb_entry,"
710
711                 "  -- Normalized attribute value"
712                 "  attr_value            TEXT"
713                 ");"
714                 "*/"
715
716
717                 "-- ------------------------------------------------------"
718                 "-- Indexes"
719
720
721                 "-- ------------------------------------------------------"
722                 "-- Triggers"
723
724                 "CREATE TRIGGER ldb_entry_insert_tr"
725                 "  AFTER INSERT"
726                 "  ON ldb_entry"
727                 "  FOR EACH ROW"
728                 "    BEGIN"
729                 "      UPDATE ldb_entry"
730                 "        SET create_timestamp = strftime('%s', 'now'),"
731                 "            modify_timestamp = strftime('%s', 'now')"
732                 "        WHERE eid = new.eid;"
733                 "    END;"
734
735                 "CREATE TRIGGER ldb_entry_update_tr"
736                 "  AFTER UPDATE"
737                 "  ON ldb_entry"
738                 "  FOR EACH ROW"
739                 "    BEGIN"
740                 "      UPDATE ldb_entry"
741                 "        SET modify_timestamp = strftime('%s', 'now')"
742                 "        WHERE eid = old.eid;"
743                 "    END;"
744
745                 "-- ------------------------------------------------------"
746                 "-- Table initialization"
747
748                 "/* We need an implicit 'top' level object class */"
749                 "INSERT INTO ldb_attributes (attr_name,"
750                 "                            parent_tree_key)"
751                 "  SELECT 'top', '';"
752
753                 "-- ------------------------------------------------------"
754
755                 "COMMIT;"
756
757                 "-- ------------------------------------------------------"
758                 ;
759         
760         /* Skip protocol indicator of url  */
761         if (strncmp(url, "sqlite://", 9) != 0) {
762                 return SQLITE_MISUSE;
763         }
764
765         /* Update pointer to just after the protocol indicator */
766         url += 9;
767                 
768         /* Try to open the (possibly empty/non-existent) database */
769         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
770                 return ret;
771         }
772
773         /* Begin a transaction */
774         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
775
776         /* Determine if this is a new database.  No tables means it is. */
777         QUERY_INT(lsqlite3,
778                   queryInt,
779                   TRUE,
780                   "SELECT COUNT(*) "
781                   "  FROM sqlite_master "
782                   "  WHERE type = 'table';");
783
784         if (queryInt == 0) {
785                 /*
786                  * Create the database schema
787                  */
788                 for (pTail = discard_const_p(char, schema); pTail != NULL; ) {
789
790                         if ((ret = sqlite3_prepare(
791                                      lsqlite3->sqlite,
792                                      pTail,
793                                      -1,
794                                      &stmt,
795                                      &pTail)) != SQLITE_OK ||
796                             (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
797                             (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
798
799                                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
800                                 (void) sqlite3_close(lsqlite3->sqlite);
801                                 return ret;
802                         }
803                 }
804         } else {
805                 /*
806                  * Ensure that the database we opened is one of ours
807                  */
808                 if (query_int(lsqlite3,
809                               &queryInt,
810                               "SELECT "
811                               "  (SELECT COUNT(*) = 3"
812                               "     FROM sqlite_master "
813                               "     WHERE type = 'table' "
814                               "       AND name IN "
815                               "         ("
816                               "           'ldb_entry', "
817                               "           'ldb_descendants', "
818                               "           'ldb_object_classes' "
819                               "         ) "
820                               "  ) "
821                               "  AND "
822                               "  (SELECT 1 "
823                               "     FROM ldb_info "
824                               "     WHERE database_type = 'LDB' "
825                               "       AND version = '1.0'"
826                               "  );") != 0 ||
827                     queryInt != 1) {
828                 
829                         /* It's not one that we created.  See ya! */
830                         QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
831                         (void) sqlite3_close(lsqlite3->sqlite);
832                         return SQLITE_MISUSE;
833                 }
834         }
835
836         /* Commit the transaction */
837         QUERY_NOROWS(lsqlite3, FALSE, "COMMIT;");
838
839         return SQLITE_OK;
840 }
841
842 static int
843 destructor(void *p)
844 {
845         struct lsqlite3_private *   lsqlite3 = p;
846
847         (void) sqlite3_close(lsqlite3->sqlite);
848         return 0;
849 }
850
851
852 /*
853  * query_norows()
854  *
855  * This function is used for queries that are not expected to return any rows,
856  * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
857  * There are no provisions here for returning data from rows in a table, so do
858  * not pass SELECT queries to this function.
859  */
860 static int
861 query_norows(const struct lsqlite3_private *lsqlite3,
862              const char *pSql,
863              ...)
864 {
865         int             ret;
866         int             bLoop;
867         char *          p;
868         const char *    pTail;
869         sqlite3_stmt *  pStmt;
870         va_list         args;
871         
872         /* Begin access to variable argument list */
873         va_start(args, pSql);
874
875         /* Format the query */
876         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
877                 return -1;
878         }
879
880         /*
881          * Prepare and execute the SQL statement.  Loop allows retrying on
882          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
883          * requiring retrying the operation.
884          */
885         for (bLoop = TRUE; bLoop; ) {
886
887                 /* Compile the SQL statement into sqlite virtual machine */
888                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
889                                            pTail,
890                                            -1,
891                                            &pStmt,
892                                            &pTail)) == SQLITE_SCHEMA) {
893                         continue;
894                 } else if (ret != SQLITE_OK) {
895                         ret = -1;
896                         break;
897                 }
898                 
899                 /* No rows expected, so just step through machine code once */
900                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
901                         (void) sqlite3_finalize(pStmt);
902                         continue;
903                 } else if (ret != SQLITE_DONE) {
904                         (void) sqlite3_finalize(pStmt);
905                         ret = -1;
906                         break;
907                 }
908
909                 /* Free the virtual machine */
910                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
911                         (void) sqlite3_finalize(pStmt);
912                         continue;
913                 } else if (ret != SQLITE_OK) {
914                         (void) sqlite3_finalize(pStmt);
915                         ret = -1;
916                         break;
917                 }
918
919                 /*
920                  * Normal condition is only one time through loop.  Loop is
921                  * rerun in error conditions, via "continue", above.
922                  */
923                 ret = 0;
924                 bLoop = FALSE;
925         }
926
927         /* All done with variable argument list */
928         va_end(args);
929
930         /* Free the memory we allocated for our query string */
931         sqlite3_free(p);
932
933         return ret;
934 }
935
936
937 /*
938  * query_int()
939  *
940  * This function is used for the common case of queries that return a single
941  * integer value.
942  *
943  * NOTE: If more than one value is returned by the query, all but the first
944  * one will be ignored.
945  */
946 static int
947 query_int(const struct lsqlite3_private * lsqlite3,
948           long long * pRet,
949           const char * pSql,
950           ...)
951 {
952         int             ret;
953         int             bLoop;
954         char *          p;
955         const char *    pTail;
956         sqlite3_stmt *  pStmt;
957         va_list         args;
958         
959         /* Begin access to variable argument list */
960         va_start(args, pSql);
961
962         /* Format the query */
963         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
964                 return -1;
965         }
966
967         /*
968          * Prepare and execute the SQL statement.  Loop allows retrying on
969          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
970          * requiring retrying the operation.
971          */
972         for (bLoop = TRUE; bLoop; ) {
973
974                 /* Compile the SQL statement into sqlite virtual machine */
975                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
976                                            pTail,
977                                            -1,
978                                            &pStmt,
979                                            &pTail)) == SQLITE_SCHEMA) {
980                         continue;
981                 } else if (ret != SQLITE_OK) {
982                         ret = -1;
983                         break;
984                 }
985                 
986                 /* One row expected */
987                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
988                         (void) sqlite3_finalize(pStmt);
989                         continue;
990                 } else if (ret != SQLITE_ROW) {
991                         (void) sqlite3_finalize(pStmt);
992                         ret = -1;
993                         break;
994                 }
995
996                 /* Get the value to be returned */
997                 *pRet = sqlite3_column_int64(pStmt, 0);
998
999                 /* Free the virtual machine */
1000                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1001                         (void) sqlite3_finalize(pStmt);
1002                         continue;
1003                 } else if (ret != SQLITE_OK) {
1004                         (void) sqlite3_finalize(pStmt);
1005                         ret = -1;
1006                         break;
1007                 }
1008
1009                 /*
1010                  * Normal condition is only one time through loop.  Loop is
1011                  * rerun in error conditions, via "continue", above.
1012                  */
1013                 ret = 0;
1014                 bLoop = FALSE;
1015         }
1016
1017         /* All done with variable argument list */
1018         va_end(args);
1019
1020         /* Free the memory we allocated for our query string */
1021         sqlite3_free(p);
1022
1023         return ret;
1024 }
1025
1026
1027 /*
1028   callback function used in call to ldb_dn_fold() for determining whether an
1029   attribute type requires case folding.
1030 */
1031 static int
1032 case_fold_attr_required(void * hUserData,
1033                         char *attr)
1034 {
1035 //        struct ldb_module * module = hUserData;
1036         
1037 #warning "currently, all attributes require case folding"
1038         return TRUE;
1039 }
1040
1041
1042 /*
1043  * add a single set of ldap message values to a ldb_message
1044  */
1045
1046 #warning "add_msg_attr() not yet implemented or used"
1047 #if 0
1048 static int
1049 add_msg_attr(struct ldb_context *ldb,
1050                       struct ldb_message *msg, 
1051                       const char *attr,
1052                       struct berval **bval)
1053 {
1054         int                          i;
1055         int                          count;
1056         struct ldb_message_element * el;
1057
1058         count = ldap_count_values_len(bval);
1059
1060         if (count <= 0) {
1061                 return -1;
1062         }
1063
1064         el = talloc_realloc(msg, msg->elements, struct ldb_message_element, 
1065                               msg->num_elements + 1);
1066         if (!el) {
1067                 errno = ENOMEM;
1068                 return -1;
1069         }
1070
1071         msg->elements = el;
1072
1073         el = &msg->elements[msg->num_elements];
1074
1075         el->name = talloc_strdup(msg->elements, attr);
1076         if (!el->name) {
1077                 errno = ENOMEM;
1078                 return -1;
1079         }
1080         el->flags = 0;
1081
1082         el->num_values = 0;
1083         el->values = talloc_array(msg->elements, struct ldb_val, count);
1084         if (!el->values) {
1085                 errno = ENOMEM;
1086                 return -1;
1087         }
1088
1089         for (i=0;i<count;i++) {
1090                 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
1091                 if (!el->values[i].data) {
1092                         return -1;
1093                 }
1094                 el->values[i].length = bval[i]->bv_len;
1095                 el->num_values++;
1096         }
1097
1098         msg->num_elements++;
1099
1100         return 0;
1101 }
1102 #endif
1103
1104 static char *
1105 parsetree_to_sql(struct ldb_module *module,
1106                           char * hTalloc,
1107                           const struct ldb_parse_tree *t)
1108 {
1109         int                     i;
1110         char *                  child;
1111         char *                  p;
1112         char *                  ret = NULL;
1113         char *                  pAttrName;
1114         
1115
1116         switch(t->operation) {
1117                 case LDB_OP_SIMPLE:
1118                         break;
1119
1120                 case LDB_OP_EXTENDED:
1121 #warning  "derrell, you'll need to work out how to handle bitops"
1122                         return NULL;
1123
1124                 case LDB_OP_AND:
1125                         ret = parsetree_to_sql(module,
1126                                                hTalloc,
1127                                                t->u.list.elements[0]);
1128
1129                         for (i = 1; i < t->u.list.num_elements; i++) {
1130                                 child =
1131                                         parsetree_to_sql(
1132                                                 module,
1133                                                 hTalloc,
1134                                                 t->u.list.elements[i]);
1135                                 ret = talloc_asprintf_append(ret,
1136                                                              "INTERSECT\n"
1137                                                              "%s\n",
1138                                                              child);
1139                                 talloc_free(child);
1140                         }
1141
1142                         child = ret;
1143                         ret = talloc_asprintf("(\n"
1144                                               "%s\n"
1145                                               ")\n",
1146                                               child);
1147                         talloc_free(child);
1148                         return ret;
1149
1150                 case LDB_OP_OR:
1151                         child =
1152                                 parsetree_to_sql(
1153                                         module,
1154                                         hTalloc,
1155                                         t->u.list.elements[0]);
1156
1157                         for (i = 1; i < t->u.list.num_elements; i++) {
1158                                 child =
1159                                         parsetree_to_sql(
1160                                                 module,
1161                                                 hTalloc,
1162                                                 t->u.list.elements[i]);
1163                                 ret = talloc_asprintf_append(ret,
1164                                                              "UNION\n"
1165                                                              "%s\n",
1166                                                              child);
1167                                 talloc_free(child);
1168                         }
1169                         child = ret;
1170                         ret = talloc_asprintf("(\n"
1171                                               "%s\n"
1172                                               ")\n",
1173                                               child);
1174                         talloc_free(child);
1175                         return ret;
1176
1177                 case LDB_OP_NOT:
1178                         child =
1179                                 parsetree_to_sql(
1180                                         module,
1181                                         hTalloc,
1182                                         t->u.not.child);
1183                         ret = talloc_asprintf(hTalloc,
1184                                               "(\n"
1185                                               "  SELECT eid\n"
1186                                               "    FROM ldb_entry\n"
1187                                               "    WHERE eid NOT IN %s\n"
1188                                               ")\n",
1189                                               child);
1190                         talloc_free(child);
1191                         return ret;
1192
1193                 default:
1194                         /* should never occur */
1195                         abort();
1196         };
1197         
1198         /* Get a case-folded copy of the attribute name */
1199         pAttrName = ldb_casefold((struct ldb_context *) module,
1200                                  t->u.simple.attr);
1201
1202         /*
1203          * For simple searches, we want to retrieve the list of EIDs that
1204          * match the criteria.  We accomplish this by searching the
1205          * appropriate table, ldb_attr_<attributeName>, for the eid
1206          * corresponding to all matching values.
1207          */
1208         if (t->u.simple.value.length == 1 &&
1209             (*(const char *) t->u.simple.value.data) == '*') {
1210                 /*
1211                  * Special case for "attr_name=*".  In this case, we want the
1212                  * eid corresponding to all values in the specified attribute
1213                  * table.
1214                  */
1215                 if ((p = sqlite3_mprintf("(\n"
1216                                          "  SELECT eid\n"
1217                                          "    FROM ldb_attr_%q\n"
1218                                          ")\n",
1219                                          pAttrName)) == NULL) {
1220                         return NULL;
1221                 }
1222
1223                 ret = talloc_strdup(hTalloc, p);
1224                 sqlite3_free(p);
1225
1226         } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
1227                 /*
1228                  * For object classes, we want to search for all objectclasses
1229                  * that are subclasses as well.
1230                  */
1231                 if ((p = sqlite3_mprintf(
1232                              "(\n"
1233                              "  SELECT eid\n"
1234                              "    FROM ldb_attr_objectclass\n"
1235                              "    WHERE attr_name IN\n"
1236                              "      (SELECT class_name\n"
1237                              "         FROM ldb_objectclasses\n"
1238                              "         WHERE tree_key GLOB\n"
1239                              "           (SELECT tree_key\n"
1240                              "              FROM ldb_objectclasses\n"
1241                              "              WHERE class_name = %Q) || '*')\n"
1242                              ")\n",
1243                              t->u.simple.value.data)) == NULL) {
1244                         return NULL;
1245                 }
1246
1247                 ret = talloc_strdup(hTalloc, p);
1248                 sqlite3_free(p);
1249
1250         } else {
1251                 /* A normal query. */
1252                 if ((p = sqlite3_mprintf("(\n"
1253                                          "  SELECT eid\n"
1254                                          "    FROM ldb_attr_%q\n"
1255                                          "    WHERE attr_value = %Q\n"
1256                                          ")\n",
1257                                          pAttrName,
1258                                          t->u.simple.value.data)) == NULL) {
1259                         return NULL;
1260                 }
1261
1262                 ret = talloc_strdup(hTalloc, p);
1263                 sqlite3_free(p);
1264         }
1265         return ret;
1266 }
1267
1268
1269 static char *
1270 parsetree_to_tablelist(struct ldb_module *module,
1271                                 char * hTalloc,
1272                                 const struct ldb_parse_tree *t)
1273 {
1274 #warning "obtain talloc'ed array of attribute names for table list"
1275         return NULL;
1276 }
1277
1278
1279 /*
1280  * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
1281  * requests in the ldb_message
1282  */
1283 static int
1284 msg_to_sql(struct ldb_module * module,
1285            const struct ldb_message * msg,
1286            long long eid,
1287            int use_flags)
1288 {
1289         int                         flags;
1290         char *                      pAttrName;
1291         unsigned int                i;
1292         unsigned int                j;
1293         struct lsqlite3_private *   lsqlite3 = module->private_data;
1294
1295         for (i = 0; i < msg->num_elements; i++) {
1296                 const struct ldb_message_element *el = &msg->elements[i];
1297
1298                 if (! use_flags) {
1299                         flags = LDB_FLAG_MOD_ADD;
1300                 } else {
1301                         flags = el->flags & LDB_FLAG_MOD_MASK;
1302                 }
1303
1304                 /* Get a case-folded copy of the attribute name */
1305                 pAttrName = ldb_casefold((struct ldb_context *) module,
1306                                          el->name);
1307
1308                 if (flags == LDB_FLAG_MOD_ADD) {
1309                         /* Create the attribute table if it doesn't exist */
1310                         if (new_attr(module, pAttrName) != 0) {
1311                                 return -1;
1312                         }
1313                 }
1314
1315                 /* For each value of the specified attribute name... */
1316                 for (j = 0; j < el->num_values; j++) {
1317
1318                         /* ... bind the attribute value, if necessary */
1319                         switch (flags) {
1320                         case LDB_FLAG_MOD_ADD:
1321                                 QUERY_NOROWS(lsqlite3,
1322                                              FALSE,
1323                                              "INSERT INTO ldb_attr_%q "
1324                                              "    (eid, attr_value) "
1325                                              "  VALUES "
1326                                              "    (%lld, %Q);",
1327                                              pAttrName,
1328                                              eid, el->values[j].data);
1329                                 QUERY_NOROWS(lsqlite3,
1330                                              FALSE,
1331                                              "UPDATE ldb_entry "
1332                                              "  SET entry_data = "
1333                                              "        add_attr(entry_data, "
1334                                              "                 %Q, %Q) "
1335                                              "  WHERE eid = %lld;",
1336                                              el->name, el->values[j].data,
1337                                              eid);
1338                                       
1339                                 break;
1340
1341                         case LDB_FLAG_MOD_REPLACE:
1342                                 QUERY_NOROWS(lsqlite3,
1343                                              FALSE,
1344                                              "UPDATE ldb_attr_%q "
1345                                              "  SET attr_value = %Q "
1346                                              "  WHERE eid = %lld;",
1347                                              pAttrName,
1348                                              el->values[j].data,
1349                                              eid);
1350                                 QUERY_NOROWS(lsqlite3,
1351                                              FALSE,
1352                                              "UPDATE ldb_entry "
1353                                              "  SET entry_data = "
1354                                              "        mod_attr(entry_data, "
1355                                              "                 %Q, %Q) "
1356                                              "  WHERE eid = %lld;",
1357                                              el->name, el->values[j].data,
1358                                              eid);
1359                                 break;
1360
1361                         case LDB_FLAG_MOD_DELETE:
1362                                 /* No additional parameters to this query */
1363                                 QUERY_NOROWS(lsqlite3,
1364                                              FALSE,
1365                                              "DELETE FROM ldb_attr_%q "
1366                                              "  WHERE eid = %lld "
1367                                              "    AND attr_value = %Q;",
1368                                              pAttrName,
1369                                              eid,
1370                                              el->values[j].data);
1371                                 QUERY_NOROWS(lsqlite3,
1372                                              FALSE,
1373                                              "UPDATE ldb_entry "
1374                                              "  SET entry_data = "
1375                                              "        del_attr(entry_data, "
1376                                              "                 %Q, %Q) "
1377                                              "  WHERE eid = %lld;",
1378                                              el->name, el->values[j].data,
1379                                              eid);
1380                                 break;
1381                         }
1382                 }
1383         }
1384
1385         return 0;
1386 }
1387
1388
1389
1390 static int
1391 new_dn(struct ldb_module * module,
1392        char * pDN,
1393        long long * pEID)
1394 {
1395         int                         nComponent;
1396         int                         bFirst;
1397         char *                      p;
1398         char *                      pPartialDN;
1399         long long                   eid;
1400         struct ldb_dn *             pExplodedDN;
1401         struct ldb_dn_component *   pComponent;
1402         struct ldb_context *        ldb = module->ldb;
1403         struct lsqlite3_private *   lsqlite3 = module->private_data;
1404
1405         /* Explode and normalize the DN */
1406         if ((pExplodedDN =
1407              ldb_explode_dn(ldb,
1408                             pDN,
1409                             ldb,
1410                             case_fold_attr_required)) == NULL) {
1411                 return -1;
1412         }
1413
1414         /* Allocate a string to hold the partial DN of each component */
1415         if ((pPartialDN = talloc_strdup(ldb, "")) == NULL) {
1416                 return -1;
1417         }
1418
1419         /* For each component of the DN (starting with the last one)... */
1420         eid = 0;
1421         for (nComponent = pExplodedDN->comp_num - 1, bFirst = TRUE;
1422              nComponent >= 0;
1423              nComponent--, bFirst = FALSE) {
1424                 
1425                 /* Point to the component */
1426                 pComponent = pExplodedDN->components[nComponent];
1427
1428                 /* Add this component on to the partial DN to date */
1429                 if ((p = talloc_asprintf(ldb,
1430                                          "%s%s%s",
1431                                          pComponent->component,
1432                                          bFirst ? "" : ",",
1433                                          pPartialDN)) == NULL) {
1434                         return -1;
1435                 }
1436
1437                 /* No need for the old partial DN any more */
1438                 talloc_free(pPartialDN);
1439
1440                 /* Save the new partial DN */
1441                 pPartialDN = p;
1442
1443                 /*
1444                  * Ensure that an entry is in the ldb_entry table for this
1445                  * component.  Any component other than the last one
1446                  * (component 0) may already exist.  It is an error if
1447                  * component 0 (the full DN requested to be be inserted)
1448                  * already exists.
1449                  */
1450                 if (bFirst) {
1451                         /* This is a top-level entry.  Parent EID is null. */
1452                         QUERY_NOROWS(lsqlite3,
1453                                      FALSE,
1454                                      "INSERT %s INTO ldb_entry "
1455                                      "    (peid, dn) "
1456                                      "  VALUES "
1457                                      "    (NULL, %q);",
1458                                      nComponent == 0 ? "" : "OR IGNORE",
1459                                      pPartialDN);
1460                 } else {
1461                         QUERY_NOROWS(lsqlite3,
1462                                      FALSE,
1463                                      "INSERT %s INTO ldb_entry "
1464                                      "    (peid, dn) "
1465                                      "  VALUES "
1466                                      "    (%lld, %q);",
1467                                      nComponent == 0 ? "" : "OR IGNORE",
1468                                      eid, pPartialDN);
1469                 }
1470
1471                 /* Get the EID of the just inserted row (the next parent) */
1472                 eid = sqlite3_last_insert_rowid(lsqlite3->sqlite);
1473         }
1474
1475         /* Give 'em what they came for! */
1476         *pEID = eid;
1477
1478         return 0;
1479 }
1480
1481
1482 static int
1483 new_attr(struct ldb_module * module,
1484                   char * pAttrName)
1485 {
1486         long long                   bExists;
1487         struct lsqlite3_private *   lsqlite3 = module->private_data;
1488
1489         /*
1490          * NOTE:
1491          *   pAttrName is assumed to already be case-folded here!
1492          */
1493
1494         /* See if the table already exists */
1495         QUERY_INT(lsqlite3,
1496                   bExists,
1497                   FALSE,
1498                   "SELECT COUNT(*) <> 0"
1499                   "  FROM sqlite_master "
1500                   "  WHERE type = 'table' "
1501                   "    AND tbl_name = %Q;",
1502                   pAttrName);
1503
1504         /* Did it exist? */
1505         if (! bExists) {
1506                 /* Nope.  Create the table */
1507                 QUERY_NOROWS(lsqlite3,
1508                              FALSE,
1509                              "CREATE TABLE ldb_attr_%q "
1510                              "("
1511                              "  eid        INTEGER REFERENCES ldb_entry, "
1512                              "  attr_value TEXT"
1513                              ");",
1514                              pAttrName);
1515         }
1516
1517         return 0;
1518 }
1519
1520