r24665: Close file handles properly.
[sfrench/samba-autobuild/.git] / source4 / lib / registry / reg_backend_dir.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) Jelmer Vernooij                                          2004.
5    
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.
10    
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.
15    
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/>.
18 */
19
20 #include "includes.h"
21 #include "registry.h"
22 #include "system/dir.h"
23 #include "system/filesys.h"
24
25 static WERROR reg_dir_add_key(TALLOC_CTX *mem_ctx, const struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *desc, struct registry_key **result)
26 {
27         char *path;
28         int ret;
29         asprintf(&path, "%s%s\\%s", parent->hive->location, parent->path, name);
30         path = reg_path_win2unix(path);
31         ret = mkdir(path, 0700);
32         SAFE_FREE(path);
33         if(ret == 0)return WERR_OK; /* FIXME */
34         return WERR_INVALID_PARAM;
35 }
36
37 static WERROR reg_dir_del_key(const struct registry_key *k, const char *name)
38 {
39         char *child = talloc_asprintf(NULL, "%s/%s", (char *)k->backend_data, name);
40         WERROR ret;
41
42         if (rmdir(child) == 0) ret = WERR_OK; else ret = WERR_GENERAL_FAILURE;
43
44         talloc_free(child);
45
46         return ret;
47 }
48
49 static WERROR reg_dir_open_key(TALLOC_CTX *mem_ctx, const struct registry_key *p, const char *name, struct registry_key **subkey)
50 {
51         DIR *d;
52         char *fullpath, *unixpath;
53         struct registry_key *ret;
54         
55         if(!name) {
56                 DEBUG(0, ("NULL pointer passed as directory name!"));
57                 return WERR_INVALID_PARAM;
58         }
59
60         
61         fullpath = talloc_asprintf(mem_ctx, "%s/%s", (char *)p->backend_data, name);
62         unixpath = reg_path_win2unix(fullpath);
63         
64         d = opendir(unixpath);
65         if(!d) {
66                 DEBUG(3,("Unable to open '%s': %s\n", unixpath, strerror(errno)));
67                 return WERR_BADFILE;
68         }
69         closedir(d);
70         ret = talloc(mem_ctx, struct registry_key);
71         ret->hive = p->hive;
72         ret->path = fullpath;
73         ret->backend_data = unixpath;
74         *subkey = ret;
75         return WERR_OK;
76 }
77
78 static WERROR reg_dir_key_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *k, int idx, struct registry_key **key)
79 {
80         struct dirent *e;
81         char *fullpath = k->backend_data;
82         int i = 0;
83         DIR *d;
84
85         d = opendir(fullpath);
86
87         if(!d) return WERR_INVALID_PARAM;
88         
89         while((e = readdir(d))) {
90                 if(!ISDOT(e->d_name) && !ISDOTDOT(e->d_name)) {
91                         struct stat stbuf;
92                         char *thispath;
93                         
94                         /* Check if file is a directory */
95                         asprintf(&thispath, "%s/%s", fullpath, e->d_name);
96                         stat(thispath, &stbuf);
97
98                         if(S_ISDIR(stbuf.st_mode)) {
99                                 if(i == idx) {
100                                         (*key) = talloc(mem_ctx, struct registry_key);
101                                         (*key)->name = talloc_strdup(*key, e->d_name);
102                                         (*key)->path = NULL;
103                                         (*key)->backend_data = talloc_strdup(*key, thispath);
104                                         SAFE_FREE(thispath);
105                                         closedir(d);
106                                         return WERR_OK;
107                                 }
108                                 i++;
109                         }
110
111                         SAFE_FREE(thispath);
112                 }
113         }
114
115         closedir(d);
116
117         return WERR_NO_MORE_ITEMS;
118 }
119
120 static WERROR reg_dir_open(struct registry_hive *h, struct registry_key **key)
121 {
122         if(!h->location) return WERR_INVALID_PARAM;
123
124         *key = talloc(h, struct registry_key);
125         (*key)->backend_data = talloc_strdup(*key, h->location);
126         return WERR_OK;
127 }
128
129 static struct hive_operations reg_backend_dir = {
130         .name = "dir",
131         .open_hive = reg_dir_open,
132         .open_key = reg_dir_open_key,
133         .add_key = reg_dir_add_key,
134         .del_key = reg_dir_del_key,
135         .get_subkey_by_index = reg_dir_key_by_index
136 };
137
138 NTSTATUS registry_dir_init(void)
139 {
140         return registry_register(&reg_backend_dir);
141 }