Merge commit 'origin/v4-0-test' into 4-0-local
[samba.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 loadparm_context *lp_ctx,
31                               struct hive_key **root)
32 {
33         int fd, num;
34         char peek[20];
35
36         /* Check for directory */
37         if (directory_exist(location)) {
38                 return reg_open_directory(parent_ctx, location, root);
39         }
40
41         fd = open(location, O_RDWR);
42         if (fd == -1) {
43                 if (errno == ENOENT)
44                         return WERR_BADFILE;
45                 return WERR_BADFILE;
46         }
47
48         num = read(fd, peek, 20);
49         if (num == -1) {
50                 return WERR_BADFILE;
51         }
52
53         if (!strncmp(peek, "regf", 4)) {
54                 close(fd);
55                 return reg_open_regf_file(parent_ctx, location, lp_ctx, root);
56         } else if (!strncmp(peek, "TDB file", 8)) {
57                 close(fd);
58                 return reg_open_ldb_file(parent_ctx, location, session_info,
59                                          credentials, lp_ctx, root);
60         }
61
62         return WERR_BADFILE;
63 }
64
65 _PUBLIC_ WERROR hive_key_get_info(TALLOC_CTX *mem_ctx,
66                                   const struct hive_key *key,
67                                   const char **classname, uint32_t *num_subkeys,
68                                   uint32_t *num_values,
69                                   NTTIME *last_change_time,
70                                   uint32_t *max_subkeynamelen,
71                                   uint32_t *max_valnamelen,
72                                   uint32_t *max_valbufsize)
73 {
74         return key->ops->get_key_info(mem_ctx, key, classname, num_subkeys,
75                                       num_values, last_change_time,
76                                       max_subkeynamelen,
77                                       max_valnamelen, max_valbufsize);
78 }
79
80 _PUBLIC_ WERROR hive_key_add_name(TALLOC_CTX *ctx,
81                                   const struct hive_key *parent_key,
82                                   const char *name, const char *classname,
83                                   struct security_descriptor *desc,
84                                   struct hive_key **key)
85 {
86         SMB_ASSERT(strchr(name, '\\') == NULL);
87
88         return parent_key->ops->add_key(ctx, parent_key, name, classname,
89                                         desc, key);
90 }
91
92 _PUBLIC_ WERROR hive_key_del(const struct hive_key *key, const char *name)
93 {
94         return key->ops->del_key(key, name);
95 }
96
97 _PUBLIC_ WERROR hive_get_key_by_name(TALLOC_CTX *mem_ctx,
98                                      const struct hive_key *key,
99                                      const char *name,
100                                      struct hive_key **subkey)
101 {
102         return key->ops->get_key_by_name(mem_ctx, key, name, subkey);
103 }
104
105 WERROR hive_enum_key(TALLOC_CTX *mem_ctx,
106                      const struct hive_key *key, uint32_t idx,
107                      const char **name,
108                      const char **classname,
109                      NTTIME *last_mod_time)
110 {
111         return key->ops->enum_key(mem_ctx, key, idx, name, classname,
112                                   last_mod_time);
113 }
114
115 WERROR hive_key_set_value(struct hive_key *key, const char *name, uint32_t type,
116                                           const DATA_BLOB data)
117 {
118         if (key->ops->set_value == NULL)
119                 return WERR_NOT_SUPPORTED;
120
121         return key->ops->set_value(key, name, type, data);
122 }
123
124 WERROR hive_get_value(TALLOC_CTX *mem_ctx,
125                       struct hive_key *key, const char *name,
126                       uint32_t *type, DATA_BLOB *data)
127 {
128         if (key->ops->get_value_by_name == NULL)
129                 return WERR_NOT_SUPPORTED;
130
131         return key->ops->get_value_by_name(mem_ctx, key, name, type, data);
132 }
133
134 WERROR hive_get_value_by_index(TALLOC_CTX *mem_ctx,
135                                struct hive_key *key, uint32_t idx,
136                                const char **name,
137                                uint32_t *type, DATA_BLOB *data)
138 {
139         if (key->ops->enum_value == NULL)
140                 return WERR_NOT_SUPPORTED;
141
142         return key->ops->enum_value(mem_ctx, key, idx, name, type, data);
143 }
144
145
146 WERROR hive_key_del_value(struct hive_key *key, const char *name)
147 {
148         if (key->ops->delete_value == NULL)
149                 return WERR_NOT_SUPPORTED;
150
151         return key->ops->delete_value(key, name);
152 }
153
154 WERROR hive_key_flush(struct hive_key *key)
155 {
156         if (key->ops->flush_key == NULL)
157                 return WERR_OK;
158
159         return key->ops->flush_key(key);
160 }