r4132: - Bunch of rather large fixes in the registry
[gd/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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "registry.h"
23 #include "system/dir.h"
24
25 static WERROR reg_dir_add_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, SEC_DESC *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(struct registry_key *k)
38 {
39         return (rmdir((char *)k->backend_data) == 0)?WERR_OK:WERR_GENERAL_FAILURE;
40 }
41
42 static WERROR reg_dir_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **subkey)
43 {
44         DIR *d;
45         char *fullpath;
46         struct registry_key *ret;
47         
48         if(!name) {
49                 DEBUG(0, ("NULL pointer passed as directory name!"));
50                 return WERR_INVALID_PARAM;
51         }
52
53         
54         fullpath = talloc_asprintf(mem_ctx, "%s%s", h->location, name);
55         fullpath = reg_path_win2unix(fullpath);
56         
57         d = opendir(fullpath);
58         if(!d) {
59                 DEBUG(3,("Unable to open '%s': %s\n", fullpath, strerror(errno)));
60                 return WERR_BADFILE;
61         }
62         closedir(d);
63         ret = talloc_p(mem_ctx, struct registry_key);
64         ret->hive = h;
65         ret->path = fullpath;
66         *subkey = ret;
67         return WERR_OK;
68 }
69
70 static WERROR reg_dir_key_by_index(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **key)
71 {
72         struct dirent *e;
73         char *fullpath = k->backend_data;
74         int i = 0;
75         DIR *d;
76
77         d = opendir(fullpath);
78
79         if(!d) return WERR_INVALID_PARAM;
80         
81         while((e = readdir(d))) {
82                 if( strcmp(e->d_name, ".") &&
83                    strcmp(e->d_name, "..")) {
84                         struct stat stbuf;
85                         char *thispath;
86                         
87                         /* Check if file is a directory */
88                         asprintf(&thispath, "%s/%s", fullpath, e->d_name);
89                         stat(thispath, &stbuf);
90
91                         if(S_ISDIR(stbuf.st_mode)) {
92                                 i++;
93                                 if(i == idx) {
94                                         (*key) = talloc_p(mem_ctx, struct registry_key);
95                                         (*key)->name = e->d_name;
96                                         (*key)->path = NULL;
97                                         (*key)->backend_data = talloc_strdup(mem_ctx, thispath);
98                                         SAFE_FREE(thispath);
99                                         closedir(d);
100                                         return WERR_OK;
101                                 }
102                         }
103
104                         SAFE_FREE(thispath);
105                 }
106         }
107
108         closedir(d);
109
110         return WERR_NO_MORE_ITEMS;
111 }
112
113 static WERROR reg_dir_open(struct registry_hive *h, struct registry_key **key)
114 {
115         if(!h->location) return WERR_INVALID_PARAM;
116
117         *key = talloc_p(h, struct registry_key);
118         (*key)->backend_data = talloc_strdup(*key, h->location);
119         return WERR_OK;
120 }
121
122 static WERROR reg_dir_set_value(struct registry_key *p, const char *name, int type, void *data, int len)
123 {
124         /* FIXME */
125         return WERR_NOT_SUPPORTED;
126 }
127
128 static WERROR reg_dir_del_value(struct registry_value *v)
129 {
130         /* FIXME*/
131         return WERR_NOT_SUPPORTED;
132 }
133
134 static struct hive_operations reg_backend_dir = {
135         .name = "dir",
136         .open_hive = reg_dir_open,
137         .open_key = reg_dir_open_key,
138         .add_key = reg_dir_add_key,
139         .del_key = reg_dir_del_key,
140         .get_subkey_by_index = reg_dir_key_by_index,
141         .set_value = reg_dir_set_value,
142         .del_value = reg_dir_del_value,
143 };
144
145 NTSTATUS registry_dir_init(void)
146 {
147         return registry_register(&reg_backend_dir);
148 }