r10328: Add more emacs python-mode markers.
[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( strcmp(e->d_name, ".") &&
92                    strcmp(e->d_name, "..")) {
93                         struct stat stbuf;
94                         char *thispath;
95                         
96                         /* Check if file is a directory */
97                         asprintf(&thispath, "%s/%s", fullpath, e->d_name);
98                         stat(thispath, &stbuf);
99
100                         if(S_ISDIR(stbuf.st_mode)) {
101                                 if(i == idx) {
102                                         (*key) = talloc(mem_ctx, struct registry_key);
103                                         (*key)->name = e->d_name;
104                                         (*key)->path = NULL;
105                                         (*key)->backend_data = talloc_strdup(mem_ctx, thispath);
106                                         SAFE_FREE(thispath);
107                                         closedir(d);
108                                         return WERR_OK;
109                                 }
110                                 i++;
111                         }
112
113                         SAFE_FREE(thispath);
114                 }
115         }
116
117         closedir(d);
118
119         return WERR_NO_MORE_ITEMS;
120 }
121
122 static WERROR reg_dir_open(struct registry_hive *h, struct registry_key **key)
123 {
124         if(!h->location) return WERR_INVALID_PARAM;
125
126         *key = talloc(h, struct registry_key);
127         (*key)->backend_data = talloc_strdup(*key, h->location);
128         return WERR_OK;
129 }
130
131 static struct hive_operations reg_backend_dir = {
132         .name = "dir",
133         .open_hive = reg_dir_open,
134         .open_key = reg_dir_open_key,
135         .add_key = reg_dir_add_key,
136         .del_key = reg_dir_del_key,
137         .get_subkey_by_index = reg_dir_key_by_index
138 };
139
140 NTSTATUS registry_dir_init(void)
141 {
142         return registry_register(&reg_backend_dir);
143 }