c6e8d3767110a03fe1edff2256832cffdafea3df
[gd/samba-autobuild/.git] / source4 / lib / ldb / common / ldb.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 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 core API
29  *
30  *  Description: core API routines interfacing to ldb backends
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36
37 /* 
38  connect to a database. The URL can either be one of the following forms
39    ldb://path
40    ldapi://path
41
42    flags is made up of LDB_FLG_*
43
44    the options are passed uninterpreted to the backend, and are
45    backend specific
46 */
47 struct ldb_context *ldb_connect(const char *url, unsigned int flags,
48                                 const char *options[])
49 {
50
51         if (strncmp(url, "tdb:", 4) == 0 ||
52             strchr(url, ':') == NULL) {
53                 return ltdb_connect(url, flags, options);
54         }
55
56 #if HAVE_LDAP
57         if (strncmp(url, "ldap", 4) == 0) {
58                 return lldb_connect(url, flags, options);
59         }
60 #endif
61
62         errno = EINVAL;
63         return NULL;
64 }
65
66 /*
67   close the connection to the database
68 */
69 int ldb_close(struct ldb_context *ldb)
70 {
71         ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_close");
72         return ldb->ops->close(ldb);
73 }
74
75
76 /*
77   search the database given a LDAP-like search expression
78
79   return the number of records found, or -1 on error
80 */
81 int ldb_search(struct ldb_context *ldb, 
82                const char *base,
83                enum ldb_scope scope,
84                const char *expression,
85                const char * const *attrs, struct ldb_message ***res)
86 {
87         int ret;
88         ret = ldb->ops->search(ldb, base, scope, expression, attrs, res);
89
90         ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_search(%s) -> %d records\n", 
91                   expression, ret);
92         return ret;
93 }
94
95 /* 
96    free a set of messages returned by ldb_search
97 */
98 int ldb_search_free(struct ldb_context *ldb, struct ldb_message **msgs)
99 {
100         return ldb->ops->search_free(ldb, msgs);
101 }
102
103
104 /*
105   add a record to the database. Will fail if a record with the given class and key
106   already exists
107 */
108 int ldb_add(struct ldb_context *ldb, 
109             const struct ldb_message *message)
110 {
111         int ret;
112         ret = ldb->ops->add_record(ldb, message);
113         ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_add(%s) -> %d\n", message->dn, ret);
114         return ret;
115 }
116
117 /*
118   modify the specified attributes of a record
119 */
120 int ldb_modify(struct ldb_context *ldb, 
121                const struct ldb_message *message)
122 {
123         int ret;
124         ret = ldb->ops->modify_record(ldb, message);
125         ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_modify(%s) -> %d\n", 
126                   message->dn, ret);
127         return ret;
128 }
129
130
131 /*
132   delete a record from the database
133 */
134 int ldb_delete(struct ldb_context *ldb, const char *dn)
135 {
136         int ret;
137         ret = ldb->ops->delete_record(ldb, dn);
138         ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_delete(%s) -> %d\n", dn, ret);
139         return ret;
140 }
141
142 /*
143   return extended error information 
144 */
145 const char *ldb_errstring(struct ldb_context *ldb)
146 {
147         return ldb->ops->errstring(ldb);
148 }