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