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