Merge commit 'release-4-0-0alpha1' into v4-0-test
[ira/wip.git] / source4 / lib / registry / hive.c
1
2 /*
3    Unix SMB/CIFS implementation.
4    Registry hive interface
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "hive.h"
24 #include "system/filesys.h"
25
26 /** Open a registry file/host/etc */
27 _PUBLIC_ WERROR reg_open_hive(TALLOC_CTX *parent_ctx, const char *location,
28                               struct auth_session_info *session_info,
29                               struct cli_credentials *credentials,
30                               struct hive_key **root)
31 {
32         int fd, num;
33         char peek[20];
34
35         /* Check for directory */
36         if (directory_exist(location)) {
37                 return reg_open_directory(parent_ctx, location, root);
38         }
39
40         fd = open(location, O_RDWR);
41         if (fd == -1) {
42                 if (errno == ENOENT)
43                         return WERR_NOT_FOUND;
44                 return WERR_BADFILE;
45         }
46
47         num = read(fd, peek, 20);
48         if (num == -1) {
49                 return WERR_BADFILE;
50         }
51
52         if (!strncmp(peek, "regf", 4)) {
53                 close(fd);
54                 return reg_open_regf_file(parent_ctx, location, root);
55         } else if (!strncmp(peek, "TDB file", 8)) {
56                 close(fd);
57                 return reg_open_ldb_file(parent_ctx, location, session_info,
58                                          credentials, root);
59         }
60
61         return WERR_BADFILE;
62 }
63
64 _PUBLIC_ WERROR hive_key_get_info(TALLOC_CTX *mem_ctx,
65                                   const struct hive_key *key,
66                                   const char **classname, uint32_t *num_subkeys,
67                                   uint32_t *num_values,
68                                   NTTIME *last_change_time)
69 {
70         return key->ops->get_key_info(mem_ctx, key, classname, num_subkeys,
71                                       num_values, last_change_time);
72 }
73
74 _PUBLIC_ WERROR hive_key_add_name(TALLOC_CTX *ctx,
75                                   const struct hive_key *parent_key,
76                                   const char *name, const char *classname,
77                                   struct security_descriptor *desc,
78                                   struct hive_key **key)
79 {
80         SMB_ASSERT(strchr(name, '\\') == NULL);
81
82         return parent_key->ops->add_key(ctx, parent_key, name, classname,
83                                         desc, key);
84 }
85
86 _PUBLIC_ WERROR hive_key_del(const struct hive_key *key, const char *name)
87 {
88         return key->ops->del_key(key, name);
89 }
90
91 _PUBLIC_ WERROR hive_get_key_by_name(TALLOC_CTX *mem_ctx,
92                                      const struct hive_key *key,
93                                      const char *name,
94                                      struct hive_key **subkey)
95 {
96         return key->ops->get_key_by_name(mem_ctx, key, name, subkey);
97 }
98
99 WERROR hive_enum_key(TALLOC_CTX *mem_ctx,
100                      const struct hive_key *key, uint32_t idx,
101                      const char **name,
102                      const char **classname,
103                      NTTIME *last_mod_time)
104 {
105         return key->ops->enum_key(mem_ctx, key, idx, name, classname,
106                                   last_mod_time);
107 }
108
109 WERROR hive_set_value(struct hive_key *key, const char *name, uint32_t type,
110                                           const DATA_BLOB data)
111 {
112         if (key->ops->set_value == NULL)
113                 return WERR_NOT_SUPPORTED;
114
115         return key->ops->set_value(key, name, type, data);
116 }
117
118 WERROR hive_get_value(TALLOC_CTX *mem_ctx,
119                       struct hive_key *key, const char *name,
120                       uint32_t *type, DATA_BLOB *data)
121 {
122         if (key->ops->get_value_by_name == NULL)
123                 return WERR_NOT_SUPPORTED;
124
125         return key->ops->get_value_by_name(mem_ctx, key, name, type, data);
126 }
127
128 WERROR hive_get_value_by_index(TALLOC_CTX *mem_ctx,
129                                struct hive_key *key, uint32_t idx,
130                                const char **name,
131                                uint32_t *type, DATA_BLOB *data)
132 {
133         if (key->ops->enum_value == NULL)
134                 return WERR_NOT_SUPPORTED;
135
136         return key->ops->enum_value(mem_ctx, key, idx, name, type, data);
137 }
138
139
140 WERROR hive_del_value(struct hive_key *key, const char *name)
141 {
142         if (key->ops->delete_value == NULL)
143                 return WERR_NOT_SUPPORTED;
144
145         return key->ops->delete_value(key, name);
146 }
147
148 WERROR hive_key_flush(struct hive_key *key)
149 {
150         if (key->ops->flush_key == NULL)
151                 return WERR_OK;
152
153         return key->ops->flush_key(key);
154 }