s4:registry: add a TALLOC_CTX argument to reg_open_remote()
[mat/samba.git] / source4 / lib / registry / registry.h
1 /*
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) Gerald Carter                        2002.
5    Copyright (C) Jelmer Vernooij                        2003-2007.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef _REGISTRY_H /* _REGISTRY_H */
22 #define _REGISTRY_H
23
24 struct registry_context;
25 struct loadparm_context;
26
27 #include <talloc.h>
28 #include "libcli/util/werror.h"
29 #include "librpc/gen_ndr/security.h"
30 #include "libcli/util/ntstatus.h"
31 #include "../lib/util/time.h"
32 #include "../lib/util/data_blob.h"
33
34 /**
35  * The hive API. This API is generally used for
36  * reading a specific file that contains just one hive.
37  *
38  * Good examples are .DAT (NTUSER.DAT) files.
39  *
40  * This API does not have any notification support (that
41  * should be provided by the registry implementation), nor
42  * does it understand what predefined keys are.
43  */
44
45 struct hive_key {
46         const struct hive_operations *ops;
47 };
48
49 struct hive_operations {
50         const char *name;
51
52         /**
53          * Open a specific subkey
54          */
55         WERROR (*enum_key) (TALLOC_CTX *mem_ctx,
56                             const struct hive_key *key, uint32_t idx,
57                             const char **name,
58                             const char **classname,
59                             NTTIME *last_mod_time);
60
61         /**
62          * Open a subkey by name
63          */
64         WERROR (*get_key_by_name) (TALLOC_CTX *mem_ctx,
65                                    const struct hive_key *key, const char *name,
66                                    struct hive_key **subkey);
67
68         /**
69          * Add a new key.
70          */
71         WERROR (*add_key) (TALLOC_CTX *ctx,
72                            const struct hive_key *parent_key, const char *path,
73                            const char *classname,
74                            struct security_descriptor *desc,
75                            struct hive_key **key);
76         /**
77          * Remove an existing key.
78          */
79         WERROR (*del_key) (TALLOC_CTX *mem_ctx,
80                            const struct hive_key *key, const char *name);
81
82         /**
83          * Force write of a key to disk.
84          */
85         WERROR (*flush_key) (struct hive_key *key);
86
87         /**
88          * Retrieve a registry value with a specific index.
89          */
90         WERROR (*enum_value) (TALLOC_CTX *mem_ctx,
91                               struct hive_key *key, uint32_t idx,
92                               const char **name, uint32_t *type,
93                               DATA_BLOB *data);
94
95         /**
96          * Retrieve a registry value with the specified name
97          */
98         WERROR (*get_value_by_name) (TALLOC_CTX *mem_ctx,
99                                      struct hive_key *key, const char *name,
100                                      uint32_t *type, DATA_BLOB *data);
101
102         /**
103          * Set a value on the specified registry key.
104          */
105         WERROR (*set_value) (struct hive_key *key, const char *name,
106                              uint32_t type, const DATA_BLOB data);
107
108         /**
109          * Remove a value.
110          */
111         WERROR (*delete_value) (TALLOC_CTX *mem_ctx,
112                                 struct hive_key *key, const char *name);
113
114         /* Security Descriptors */
115
116         /**
117          * Change the security descriptor on a registry key.
118          *
119          * This should return WERR_NOT_SUPPORTED if the underlying
120          * format does not have a mechanism for storing
121          * security descriptors.
122          */
123         WERROR (*set_sec_desc) (struct hive_key *key,
124                                 const struct security_descriptor *desc);
125
126         /**
127          * Retrieve the security descriptor on a registry key.
128          *
129          * This should return WERR_NOT_SUPPORTED if the underlying
130          * format does not have a mechanism for storing
131          * security descriptors.
132          */
133         WERROR (*get_sec_desc) (TALLOC_CTX *ctx,
134                                 const struct hive_key *key,
135                                 struct security_descriptor **desc);
136
137         /**
138          * Retrieve general information about a key.
139          */
140         WERROR (*get_key_info) (TALLOC_CTX *mem_ctx,
141                                 const struct hive_key *key,
142                                 const char **classname,
143                                 uint32_t *num_subkeys,
144                                 uint32_t *num_values,
145                                 NTTIME *last_change_time,
146                                 uint32_t *max_subkeynamelen,
147                                 uint32_t *max_valnamelen,
148                                 uint32_t *max_valbufsize);
149 };
150
151 struct cli_credentials;
152 struct auth_session_info;
153 struct tevent_context;
154
155 WERROR reg_open_hive(TALLOC_CTX *parent_ctx, const char *location,
156                      struct auth_session_info *session_info,
157                      struct cli_credentials *credentials,
158                      struct tevent_context *ev_ctx,
159                      struct loadparm_context *lp_ctx,
160                      struct hive_key **root);
161 WERROR hive_key_get_info(TALLOC_CTX *mem_ctx, const struct hive_key *key,
162                          const char **classname, uint32_t *num_subkeys,
163                          uint32_t *num_values, NTTIME *last_change_time,
164                          uint32_t *max_subkeynamelen,
165                          uint32_t *max_valnamelen, uint32_t *max_valbufsize);
166 WERROR hive_key_add_name(TALLOC_CTX *ctx, const struct hive_key *parent_key,
167                          const char *name, const char *classname,
168                          struct security_descriptor *desc,
169                          struct hive_key **key);
170 WERROR hive_key_del(TALLOC_CTX *mem_ctx,
171                     const struct hive_key *key, const char *name);
172 WERROR hive_get_key_by_name(TALLOC_CTX *mem_ctx,
173                             const struct hive_key *key, const char *name,
174                             struct hive_key **subkey);
175 WERROR hive_enum_key(TALLOC_CTX *mem_ctx,
176                      const struct hive_key *key, uint32_t idx,
177                      const char **name,
178                      const char **classname,
179                      NTTIME *last_mod_time);
180
181 WERROR hive_key_set_value(struct hive_key *key, const char *name,
182                       uint32_t type, const DATA_BLOB data);
183
184 WERROR hive_get_value(TALLOC_CTX *mem_ctx,
185                       struct hive_key *key, const char *name,
186                       uint32_t *type, DATA_BLOB *data);
187 WERROR hive_get_value_by_index(TALLOC_CTX *mem_ctx,
188                                struct hive_key *key, uint32_t idx,
189                                const char **name,
190                                uint32_t *type, DATA_BLOB *data);
191 WERROR hive_get_sec_desc(TALLOC_CTX *mem_ctx,
192                          struct hive_key *key,
193                          struct security_descriptor **security);
194
195 WERROR hive_set_sec_desc(struct hive_key *key, 
196                          const struct security_descriptor *security);
197
198 WERROR hive_key_del_value(TALLOC_CTX *mem_ctx,
199                           struct hive_key *key, const char *name);
200
201 WERROR hive_key_flush(struct hive_key *key);
202
203
204 /* Individual backends */
205 WERROR reg_open_directory(TALLOC_CTX *parent_ctx,
206                           const char *location, struct hive_key **key);
207 WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx,
208                           const char *location, struct hive_key **key);
209 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
210                          struct auth_session_info *session_info,
211                          struct cli_credentials *credentials,
212                          struct tevent_context *ev_ctx,
213                          struct loadparm_context *lp_ctx,
214                          struct hive_key **k);
215
216
217 WERROR reg_create_directory(TALLOC_CTX *parent_ctx,
218                             const char *location, struct hive_key **key);
219 WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx,
220                             const char *location,
221                             int major_version,
222                             struct hive_key **key);
223
224
225
226 /* Handles for the predefined keys */
227 #define HKEY_CLASSES_ROOT               0x80000000
228 #define HKEY_CURRENT_USER               0x80000001
229 #define HKEY_LOCAL_MACHINE              0x80000002
230 #define HKEY_USERS                      0x80000003
231 #define HKEY_PERFORMANCE_DATA           0x80000004
232 #define HKEY_CURRENT_CONFIG             0x80000005
233 #define HKEY_DYN_DATA                   0x80000006
234 #define HKEY_PERFORMANCE_TEXT           0x80000050
235 #define HKEY_PERFORMANCE_NLSTEXT        0x80000060
236
237 #define HKEY_FIRST              HKEY_CLASSES_ROOT
238 #define HKEY_LAST               HKEY_PERFORMANCE_NLSTEXT
239
240 struct reg_predefined_key {
241         uint32_t handle;
242         const char *name;
243 };
244
245 extern const struct reg_predefined_key reg_predefined_keys[];
246
247 #define REG_DELETE              -1
248
249 /*
250  * The general idea here is that every backend provides a 'hive'. Combining
251  * various hives gives you a complete registry like windows has
252  */
253
254 #define REGISTRY_INTERFACE_VERSION 1
255
256 struct reg_key_operations;
257
258 /* structure to store the registry handles */
259 struct registry_key
260 {
261         struct registry_context *context;
262 };
263
264 struct registry_value
265 {
266         const char *name;
267         unsigned int data_type;
268         DATA_BLOB data;
269 };
270
271 /* FIXME */
272 typedef void (*reg_key_notification_function) (void);
273 typedef void (*reg_value_notification_function) (void);
274
275 struct cli_credentials;
276
277 struct registry_operations {
278         const char *name;
279
280         WERROR (*get_key_info) (TALLOC_CTX *mem_ctx,
281                                 const struct registry_key *key,
282                                 const char **classname,
283                                 uint32_t *numsubkeys,
284                                 uint32_t *numvalues,
285                                 NTTIME *last_change_time,
286                                 uint32_t *max_subkeynamelen,
287                                 uint32_t *max_valnamelen,
288                                 uint32_t *max_valbufsize);
289
290         WERROR (*flush_key) (struct registry_key *key);
291
292         WERROR (*get_predefined_key) (struct registry_context *ctx,
293                                       uint32_t key_id,
294                                       struct registry_key **key);
295
296         WERROR (*open_key) (TALLOC_CTX *mem_ctx,
297                             struct registry_key *parent,
298                             const char *path,
299                             struct registry_key **key);
300
301         WERROR (*create_key) (TALLOC_CTX *mem_ctx,
302                               struct registry_key *parent,
303                               const char *name,
304                               const char *key_class,
305                               struct security_descriptor *security,
306                               struct registry_key **key);
307
308         WERROR (*delete_key) (TALLOC_CTX *mem_ctx,
309                               struct registry_key *key, const char *name);
310
311         WERROR (*delete_value) (TALLOC_CTX *mem_ctx,
312                                 struct registry_key *key, const char *name);
313
314         WERROR (*enum_key) (TALLOC_CTX *mem_ctx,
315                             const struct registry_key *key, uint32_t idx,
316                             const char **name,
317                             const char **keyclass,
318                             NTTIME *last_changed_time);
319
320         WERROR (*enum_value) (TALLOC_CTX *mem_ctx,
321                               const struct registry_key *key, uint32_t idx,
322                               const char **name,
323                               uint32_t *type,
324                               DATA_BLOB *data);
325
326         WERROR (*get_sec_desc) (TALLOC_CTX *mem_ctx,
327                                 const struct registry_key *key,
328                                 struct security_descriptor **security);
329
330         WERROR (*set_sec_desc) (struct registry_key *key,
331                                 const struct security_descriptor *security);
332
333         WERROR (*load_key) (struct registry_key *key,
334                             const char *key_name,
335                             const char *path);
336
337         WERROR (*unload_key) (struct registry_key *key, const char *name);
338
339         WERROR (*notify_value_change) (struct registry_key *key,
340                                        reg_value_notification_function fn);
341
342         WERROR (*get_value) (TALLOC_CTX *mem_ctx,
343                              const struct registry_key *key,
344                              const char *name,
345                              uint32_t *type,
346                              DATA_BLOB *data);
347
348         WERROR (*set_value) (struct registry_key *key,
349                              const char *name,
350                              uint32_t type,
351                              const DATA_BLOB data);
352 };
353
354 /**
355  * Handle to a full registry
356  * contains zero or more hives
357  */
358 struct registry_context {
359         const struct registry_operations *ops;
360 };
361
362 struct auth_session_info;
363 struct tevent_context;
364 struct loadparm_context;
365
366 /**
367  * Open the locally defined registry.
368  */
369 WERROR reg_open_local(TALLOC_CTX *mem_ctx,
370                       struct registry_context **ctx);
371
372 WERROR reg_open_samba(TALLOC_CTX *mem_ctx,
373                       struct registry_context **ctx,
374                       struct tevent_context *ev_ctx,
375                       struct loadparm_context *lp_ctx,
376                       struct auth_session_info *session_info,
377                       struct cli_credentials *credentials);
378
379 /**
380  * Open the registry on a remote machine.
381  */
382 WERROR reg_open_remote(TALLOC_CTX *mem_ctx,
383                        struct registry_context **ctx,
384                        struct auth_session_info *session_info,
385                        struct cli_credentials *credentials,
386                        struct loadparm_context *lp_ctx,
387                        const char *location, struct tevent_context *ev);
388
389 WERROR reg_open_wine(struct registry_context **ctx, const char *path);
390
391 const char *reg_get_predef_name(uint32_t hkey);
392 WERROR reg_get_predefined_key_by_name(struct registry_context *ctx,
393                                       const char *name,
394                                       struct registry_key **key);
395 WERROR reg_get_predefined_key(struct registry_context *ctx,
396                               uint32_t hkey,
397                               struct registry_key **key);
398
399 WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent,
400                     const char *name, struct registry_key **result);
401
402 WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx,
403                                   const struct registry_key *key, uint32_t idx,
404                                   const char **name,
405                                   uint32_t *type,
406                                   DATA_BLOB *data);
407 WERROR reg_key_get_info(TALLOC_CTX *mem_ctx,
408                         const struct registry_key *key,
409                         const char **class_name,
410                         uint32_t *num_subkeys,
411                         uint32_t *num_values,
412                         NTTIME *last_change_time,
413                         uint32_t *max_subkeynamelen,
414                         uint32_t *max_valnamelen,
415                         uint32_t *max_valbufsize);
416 WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx,
417                                    const struct registry_key *key,
418                                    uint32_t idx,
419                                    const char **name,
420                                    const char **classname,
421                                    NTTIME *last_mod_time);
422 WERROR reg_key_get_subkey_by_name(TALLOC_CTX *mem_ctx,
423                                   const struct registry_key *key,
424                                   const char *name,
425                                   struct registry_key **subkey);
426 WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx,
427                                  const struct registry_key *key,
428                                  const char *name,
429                                  uint32_t *type,
430                                  DATA_BLOB *data);
431 WERROR reg_key_del(TALLOC_CTX *mem_ctx,
432                    struct registry_key *parent, const char *name);
433 WERROR reg_key_add_name(TALLOC_CTX *mem_ctx,
434                         struct registry_key *parent, const char *name,
435                         const char *classname,
436                         struct security_descriptor *desc,
437                         struct registry_key **newkey);
438 WERROR reg_val_set(struct registry_key *key, const char *value,
439                    uint32_t type, DATA_BLOB data);
440 WERROR reg_get_sec_desc(TALLOC_CTX *ctx, const struct registry_key *key,
441                         struct security_descriptor **secdesc);
442 WERROR reg_del_value(TALLOC_CTX *mem_ctx,
443                      struct registry_key *key, const char *valname);
444 WERROR reg_key_flush(struct registry_key *key);
445 WERROR reg_create_key(TALLOC_CTX *mem_ctx,
446                       struct registry_key *parent,
447                       const char *name,
448                       const char *key_class,
449                       struct security_descriptor *security,
450                       struct registry_key **key);
451
452 /* Utility functions */
453 const char *str_regtype(int type);
454 bool push_reg_sz(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *s);
455 bool push_reg_multi_sz(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char **a);
456 bool pull_reg_sz(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, const char **s);
457 bool pull_reg_multi_sz(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, const char ***a);
458 int regtype_by_string(const char *str);
459 char *reg_val_data_string(TALLOC_CTX *mem_ctx, uint32_t type, const DATA_BLOB data);
460 char *reg_val_description(TALLOC_CTX *mem_ctx, const char *name,
461                           uint32_t type, const DATA_BLOB data);
462 bool reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str,
463                        const char *data_str, uint32_t *type, DATA_BLOB *data);
464 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
465                         const char *name, struct registry_key **result);
466 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path);
467 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
468                        const char *path, uint32_t access_mask,
469                        struct security_descriptor *sec_desc,
470                        struct registry_key **result);
471 WERROR reg_load_key(struct registry_context *ctx, struct registry_key *key,
472                     const char *name, const char *filename);
473
474 WERROR reg_mount_hive(struct registry_context *rctx,
475                       struct hive_key *hive_key,
476                       uint32_t key_id,
477                       const char **elements);
478
479 struct registry_key *reg_import_hive_key(struct registry_context *ctx,
480                                          struct hive_key *hive,
481                                          uint32_t predef_key,
482                                          const char **elements);
483 WERROR reg_set_sec_desc(struct registry_key *key,
484                         const struct security_descriptor *security);
485
486 struct reg_diff_callbacks {
487         WERROR (*add_key) (void *callback_data, const char *key_name);
488         WERROR (*set_value) (void *callback_data, const char *key_name,
489                              const char *value_name, uint32_t value_type,
490                              DATA_BLOB value);
491         WERROR (*del_value) (void *callback_data, const char *key_name,
492                              const char *value_name);
493         WERROR (*del_key) (void *callback_data, const char *key_name);
494         WERROR (*del_all_values) (void *callback_data, const char *key_name);
495         WERROR (*done) (void *callback_data);
496 };
497
498 WERROR reg_diff_apply(struct registry_context *ctx, 
499                                           const char *filename);
500
501 WERROR reg_generate_diff(struct registry_context *ctx1,
502                          struct registry_context *ctx2,
503                          const struct reg_diff_callbacks *callbacks,
504                          void *callback_data);
505 WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
506                             struct reg_diff_callbacks **callbacks,
507                             void **callback_data);
508 WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
509                           struct reg_diff_callbacks **callbacks,
510                           void **callback_data);
511 WERROR reg_generate_diff_key(struct registry_key *oldkey,
512                              struct registry_key *newkey,
513                              const char *path,
514                              const struct reg_diff_callbacks *callbacks,
515                              void *callback_data);
516 WERROR reg_diff_load(const char *filename,
517                      const struct reg_diff_callbacks *callbacks,
518                      void *callback_data);
519
520 WERROR reg_dotreg_diff_load(int fd,
521                                      const struct reg_diff_callbacks *callbacks,
522                                      void *callback_data);
523
524 WERROR reg_preg_diff_load(int fd,
525                                    const struct reg_diff_callbacks *callbacks,
526                                    void *callback_data);
527
528 WERROR local_get_predefined_key(struct registry_context *ctx,
529                                 uint32_t key_id, struct registry_key **key);
530
531
532 #endif /* _REGISTRY_H */