2 Unix SMB/CIFS implementation.
4 Copyright (C) Jelmer Vernooij 2004-2007.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/dir.h"
23 #include "system/filesys.h"
30 static struct hive_operations reg_backend_dir;
32 static WERROR reg_dir_add_key(TALLOC_CTX *mem_ctx,
33 const struct hive_key *parent,
34 const char *name, const char *classname,
35 struct security_descriptor *desc,
36 struct hive_key **result)
38 struct dir_key *dk = talloc_get_type(parent, struct dir_key);
42 path = talloc_asprintf(mem_ctx, "%s/%s", dk->path, name);
43 ret = mkdir(path, 0700);
45 struct dir_key *key = talloc(mem_ctx, struct dir_key);
46 key->key.ops = ®_backend_dir;
47 key->path = talloc_steal(key, path);
48 *result = (struct hive_key *)key;
53 return WERR_ALREADY_EXISTS;
54 printf("FAILED %s BECAUSE: %s\n", path, strerror(errno));
55 return WERR_GENERAL_FAILURE;
58 static WERROR reg_dir_del_key(const struct hive_key *k, const char *name)
60 struct dir_key *dk = talloc_get_type(k, struct dir_key);
61 char *child = talloc_asprintf(NULL, "%s/%s", dk->path, name);
64 if (rmdir(child) == 0)
66 else if (errno == ENOENT)
69 ret = WERR_GENERAL_FAILURE;
76 static WERROR reg_dir_open_key(TALLOC_CTX *mem_ctx,
77 const struct hive_key *parent,
78 const char *name, struct hive_key **subkey)
82 const struct dir_key *p = talloc_get_type(parent, struct dir_key);
86 DEBUG(0, ("NULL pointer passed as directory name!"));
87 return WERR_INVALID_PARAM;
90 fullpath = talloc_asprintf(mem_ctx, "%s/%s", p->path, name);
92 d = opendir(fullpath);
94 DEBUG(3,("Unable to open '%s': %s\n", fullpath,
99 ret = talloc(mem_ctx, struct dir_key);
100 ret->key.ops = ®_backend_dir;
101 ret->path = talloc_steal(ret, fullpath);
102 *subkey = (struct hive_key *)ret;
106 static WERROR reg_dir_key_by_index(TALLOC_CTX *mem_ctx,
107 const struct hive_key *k, uint32_t idx,
109 const char **classname,
110 NTTIME *last_mod_time)
113 const struct dir_key *dk = talloc_get_type(k, struct dir_key);
117 d = opendir(dk->path);
120 return WERR_INVALID_PARAM;
122 while((e = readdir(d))) {
123 if(!ISDOT(e->d_name) && !ISDOTDOT(e->d_name)) {
127 /* Check if file is a directory */
128 asprintf(&thispath, "%s/%s", dk->path, e->d_name);
129 stat(thispath, &stbuf);
131 if (!S_ISDIR(stbuf.st_mode)) {
138 *name = talloc_strdup(mem_ctx, e->d_name);
141 unix_to_nt_time(last_mod_time, st.st_mtime);
154 return WERR_NO_MORE_ITEMS;
157 WERROR reg_open_directory(TALLOC_CTX *parent_ctx,
158 const char *location, struct hive_key **key)
162 if (location == NULL)
163 return WERR_INVALID_PARAM;
165 dk = talloc(parent_ctx, struct dir_key);
166 dk->key.ops = ®_backend_dir;
167 dk->path = talloc_strdup(dk, location);
168 *key = (struct hive_key *)dk;
172 WERROR reg_create_directory(TALLOC_CTX *parent_ctx,
173 const char *location, struct hive_key **key)
175 if (mkdir(location, 0700) != 0) {
177 return WERR_GENERAL_FAILURE;
180 return reg_open_directory(parent_ctx, location, key);
183 static WERROR reg_dir_get_info(TALLOC_CTX *ctx, const struct hive_key *key,
184 const char **classname,
185 uint32_t *num_subkeys,
186 uint32_t *num_values,
190 const struct dir_key *dk = talloc_get_type(key, struct dir_key);
194 SMB_ASSERT(key != NULL);
196 if (classname != NULL)
199 d = opendir(dk->path);
201 return WERR_INVALID_PARAM;
203 if (num_subkeys != NULL)
206 if (num_values != NULL)
209 while((e = readdir(d))) {
210 if(!ISDOT(e->d_name) && !ISDOTDOT(e->d_name)) {
211 char *path = talloc_asprintf(ctx, "%s/%s",
212 dk->path, e->d_name);
214 if (stat(path, &st) < 0) {
215 DEBUG(0, ("Error statting %s: %s\n", path,
220 if (S_ISDIR(st.st_mode) && num_subkeys != NULL)
223 if (!S_ISDIR(st.st_mode) && num_values != NULL)
237 static WERROR reg_dir_set_value(struct hive_key *key, const char *name,
238 uint32_t type, const DATA_BLOB data)
240 const struct dir_key *dk = talloc_get_type(key, struct dir_key);
241 char *path = talloc_asprintf(dk, "%s/%s", dk->path, name);
243 if (!file_save(path, data.data, data.length))
244 return WERR_GENERAL_FAILURE;
251 static WERROR reg_dir_get_value(TALLOC_CTX *mem_ctx,
252 struct hive_key *key, const char *name,
253 uint32_t *type, DATA_BLOB *data)
255 const struct dir_key *dk = talloc_get_type(key, struct dir_key);
256 char *path = talloc_asprintf(mem_ctx, "%s/%s", dk->path, name);
260 contents = file_load(path, &size, mem_ctx);
262 if (contents == NULL)
263 return WERR_NOT_FOUND;
266 *type = 4; /* FIXME */
268 data->data = (uint8_t *)contents;
274 static WERROR reg_dir_enum_value(TALLOC_CTX *mem_ctx,
275 const struct hive_key *key, int idx,
277 uint32_t *type, DATA_BLOB *data)
279 const struct dir_key *dk = talloc_get_type(key, struct dir_key);
284 d = opendir(dk->path);
286 DEBUG(3,("Unable to open '%s': %s\n", dk->path,
292 while((e = readdir(d))) {
293 if (ISDOT(e->d_name) || ISDOTDOT(e->d_name))
298 *name = talloc_strdup(mem_ctx, e->d_name);
299 W_ERROR_NOT_OK_RETURN(reg_dir_get_value(mem_ctx, key,
309 return WERR_NO_MORE_ITEMS;
313 static WERROR reg_dir_del_value (struct hive_key *key, const char *name)
315 const struct dir_key *dk = talloc_get_type(key, struct dir_key);
316 char *path = talloc_asprintf(key, "%s/%s", dk->path, name);
317 if (unlink(path) < 0) {
320 return WERR_NOT_FOUND;
321 return WERR_GENERAL_FAILURE;
328 static struct hive_operations reg_backend_dir = {
330 .get_key_by_name = reg_dir_open_key,
331 .get_key_info = reg_dir_get_info,
332 .add_key = reg_dir_add_key,
333 .del_key = reg_dir_del_key,
334 .enum_key = reg_dir_key_by_index,
335 .set_value = reg_dir_set_value,
336 .get_value_by_name = reg_dir_get_value,
337 .enum_value = reg_dir_enum_value,
338 .delete_value = reg_dir_del_value,