r17525: This is a merge from the Google Summer of Code 2006 project by Martin Kühl
[gd/samba/.git] / source4 / lib / ldb / modules / ldb_map.h
1 /* 
2    ldb database library - map backend
3
4    Copyright (C) Jelmer Vernooij 2005
5    Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
6         Development sponsored by the Google Summer of Code program
7
8      ** NOTE! The following LGPL license applies to the ldb
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11    
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 2 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 */
26
27 #ifndef __LDB_MAP_H__
28 #define __LDB_MAP_H__
29
30 /* ldb_map is a skeleton LDB module that can be used for any other modules
31  * that need to map attributes.
32  *
33  * The term 'remote' in this header refers to the connection where the 
34  * original schema is used on while 'local' means the local connection 
35  * that any upper layers will use.
36  *
37  * All local attributes will have to have a definition. Not all remote 
38  * attributes need a definition as LDB is a lot less strict than LDAP 
39  * (in other words, sending unknown attributes to an LDAP server hurts us, 
40  * while returning too many attributes in ldb_search() doesn't)
41  */
42
43
44 /* Name of the internal attribute pointing from the local to the
45  * remote part of a record */
46 #define IS_MAPPED "isMapped"
47
48
49 struct ldb_map_context;
50
51 /* convert a local ldb_val to a remote ldb_val */
52 typedef struct ldb_val (*ldb_map_convert_func) (struct ldb_module *module, void *mem_ctx, const struct ldb_val *val);
53
54 #define LDB_MAP_MAX_REMOTE_NAMES 10
55
56 /* map from local to remote attribute */
57 struct ldb_map_attribute {
58         const char *local_name; /* local name */
59
60         enum ldb_map_attr_type { 
61                 MAP_IGNORE, /* Ignore this local attribute. Doesn't exist remotely.  */
62                 MAP_KEEP,   /* Keep as is. Same name locally and remotely. */
63                 MAP_RENAME, /* Simply rename the attribute. Name changes, data is the same */
64                 MAP_CONVERT, /* Rename + convert data */
65                 MAP_GENERATE /* Use generate function for generating new name/data. 
66                                                 Used for generating attributes based on 
67                                                 multiple remote attributes. */
68         } type;
69         
70         /* if set, will be called for search expressions that contain this attribute */
71         struct ldb_parse_tree *(*convert_operator)(const struct ldb_map_context *, TALLOC_CTX *ctx, const struct ldb_parse_tree *);
72
73         union { 
74                 struct {
75                         const char *remote_name;
76                 } rename;
77                 
78                 struct {
79                         const char *remote_name;
80
81                         /* Convert local to remote data */
82                         ldb_map_convert_func convert_local;
83
84                         /* Convert remote to local data */
85                         /* an entry can have convert_remote set to NULL, as long as there as an entry with the same local_name 
86                          * that is non-NULL before it. */
87                         ldb_map_convert_func convert_remote;
88                 } convert;
89         
90                 struct {
91                         /* Generate the local attribute from remote message */
92                         struct ldb_message_element *(*generate_local)(struct ldb_module *, TALLOC_CTX *mem_ctx, const char *remote_attr, const struct ldb_message *remote);
93
94                         /* Update remote message with information from local message */
95                         void (*generate_remote)(struct ldb_module *, const char *local_attr, const struct ldb_message *old, struct ldb_message *remote, struct ldb_message *local);
96
97                         /* Name(s) for this attribute on the remote server. This is an array since 
98                          * one local attribute's data can be split up into several attributes 
99                          * remotely */
100                         const char *remote_names[LDB_MAP_MAX_REMOTE_NAMES];
101
102                         /* Names of additional remote attributes
103                          * required for the generation.  NULL
104                          * indicates that `local_attr' suffices. */
105                         /*
106 #define LDB_MAP_MAX_SELF_ATTRIBUTES 10
107                         const char *self_attrs[LDB_MAP_MAX_SELF_ATTRIBUTES];
108                         */
109                 } generate;
110         } u;
111 };
112
113
114 #define LDB_MAP_MAX_SUBCLASSES  10
115 #define LDB_MAP_MAX_MUSTS               10
116 #define LDB_MAP_MAX_MAYS                50
117
118 /* map from local to remote objectClass */
119 struct ldb_map_objectclass {
120         const char *local_name;
121         const char *remote_name;
122         const char *base_classes[LDB_MAP_MAX_SUBCLASSES];
123         const char *musts[LDB_MAP_MAX_MUSTS];
124         const char *mays[LDB_MAP_MAX_MAYS];
125 };
126
127
128 /* private context data */
129 struct ldb_map_context {
130         struct ldb_map_attribute *attribute_maps;
131         /* NOTE: Always declare base classes first here */
132         const struct ldb_map_objectclass *objectclass_maps;
133         /* struct ldb_context *mapped_ldb; */
134         const struct ldb_dn *local_base_dn;
135         const struct ldb_dn *remote_base_dn;
136 };
137
138 /* initialization function */
139 int
140 ldb_map_init(struct ldb_module *module,
141              const struct ldb_map_attribute *attrs,
142              const struct ldb_map_objectclass *ocls,
143              const char *name);
144
145 /* get copy of map_ops */
146 struct ldb_module_ops
147 ldb_map_get_ops(void);
148
149 #endif /* __LDB_MAP_H__ */