c7ed95d80f28dda226b672d6b063aeff9263c91e
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / reg_backend_dir / 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 "lib/registry/common/registry.h"
23
24 static WERROR reg_dir_add_key(REG_KEY *parent, const char *name, uint32 access_mask, SEC_DESC *desc, REG_KEY **result)
25 {
26         char *path;
27         int ret;
28         asprintf(&path, "%s%s\\%s", parent->handle->location, reg_key_get_path(parent), name);
29         path = reg_path_win2unix(path);
30         ret = mkdir(path, 0700);
31         SAFE_FREE(path);
32         if(ret == 0)return WERR_OK; /* FIXME */
33         return WERR_INVALID_PARAM;
34 }
35
36 static WERROR reg_dir_del_key(REG_KEY *k)
37 {
38         return (rmdir((char *)k->backend_data) == 0)?WERR_OK:WERR_GENERAL_FAILURE;
39 }
40
41 static WERROR reg_dir_open_key(REG_HANDLE *h, int hive, const char *name, REG_KEY **subkey)
42 {
43         DIR *d;
44         char *fullpath;
45         REG_KEY *ret;
46         TALLOC_CTX *mem_ctx;
47         
48         if(hive != 0) return WERR_NO_MORE_ITEMS;
49         
50         if(!name) {
51                 DEBUG(0, ("NULL pointer passed as directory name!"));
52                 return WERR_INVALID_PARAM;
53         }
54
55         
56         mem_ctx = talloc_init("tmp");
57         fullpath = talloc_asprintf(mem_ctx, "%s%s", h->location, name);
58         fullpath = reg_path_win2unix(fullpath);
59         
60         d = opendir(fullpath);
61         if(!d) {
62                 DEBUG(3,("Unable to open '%s': %s\n", fullpath, strerror(errno)));
63                 talloc_destroy(mem_ctx);
64                 return WERR_BADFILE;
65         }
66         closedir(d);
67         ret = reg_key_new_abs(name, h, fullpath);
68         talloc_steal(mem_ctx, ret->mem_ctx, fullpath);
69         talloc_destroy(mem_ctx);
70         *subkey = ret;
71         return WERR_OK;
72 }
73
74 static WERROR reg_dir_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***r)
75 {
76         struct dirent *e;
77         int max = 200;
78         char *fullpath = k->backend_data;
79         REG_KEY **ar;
80         DIR *d;
81         (*count) = 0;
82         ar = talloc(k->mem_ctx, sizeof(REG_KEY *) * max);
83
84         d = opendir(fullpath);
85
86         if(!d) return WERR_INVALID_PARAM;
87         
88         while((e = readdir(d))) {
89                 if( strcmp(e->d_name, ".") &&
90                    strcmp(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                                 ar[(*count)] = reg_key_new_rel(e->d_name, k, NULL);
100                                 ar[(*count)]->backend_data = talloc_strdup(ar[*count]->mem_ctx, thispath);
101                                 if(ar[(*count)])(*count)++;
102
103                                 if((*count) == max) {
104                                         max+=200;
105                                         ar = realloc(ar, sizeof(REG_KEY *) * max);
106                                 }
107                         }
108
109                         SAFE_FREE(thispath);
110                 }
111         }
112
113         closedir(d);
114
115         *r = ar;
116         return WERR_OK;
117 }
118
119 static WERROR reg_dir_open(REG_HANDLE *h, const char *loc, const char *credentials) {
120         if(!loc) return WERR_INVALID_PARAM;
121         return WERR_OK;
122 }
123
124 static WERROR reg_dir_add_value(REG_KEY *p, const char *name, int type, void *data, int len)
125 {
126         /* FIXME */
127         return WERR_NOT_SUPPORTED;
128 }
129
130 static WERROR reg_dir_get_hive(REG_HANDLE *h, int hive, REG_KEY **key)
131 {
132         if(hive != 0) return WERR_NO_MORE_ITEMS;
133         *key = reg_key_new_abs("", h, NULL);
134         (*key)->backend_data = talloc_strdup((*key)->mem_ctx, h->location);
135         return WERR_OK;
136 }
137
138 static WERROR reg_dir_del_value(REG_VAL *v)
139 {
140         /* FIXME*/
141         return WERR_NOT_SUPPORTED;
142 }
143
144 static struct registry_ops reg_backend_dir = {
145         .name = "dir",
146         .open_registry = reg_dir_open,
147         .open_key = reg_dir_open_key,
148         .get_hive = reg_dir_get_hive,
149         .fetch_subkeys = reg_dir_fetch_subkeys,
150         .add_key = reg_dir_add_key,
151         .del_key = reg_dir_del_key,
152         .add_value = reg_dir_add_value,
153         .del_value = reg_dir_del_value,
154 };
155
156 NTSTATUS registry_dir_init(void)
157 {
158         return register_backend("registry", &reg_backend_dir);
159 }