summaryrefslogtreecommitdiff
path: root/source4/lib/registry/reg_backend_dir/reg_backend_dir.c
blob: 960d5f3e0494d8a90c1223a103c391582297c9ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* 
   Unix SMB/CIFS implementation.
   Registry interface
   Copyright (C) Jelmer Vernooij					  2004.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "lib/registry/common/registry.h"

static WERROR reg_dir_add_key(REG_KEY *parent, const char *name, uint32 access_mask, SEC_DESC *desc, REG_KEY **result)
{
	char *path;
	int ret;
	asprintf(&path, "%s%s\\%s", parent->handle->location, reg_key_get_path(parent), name);
	path = reg_path_win2unix(path);
	ret = mkdir(path, 0700);
	SAFE_FREE(path);
	if(ret == 0)return WERR_OK; /* FIXME */
	return WERR_INVALID_PARAM;
}

static WERROR reg_dir_del_key(REG_KEY *k)
{
	return (rmdir((char *)k->backend_data) == 0)?WERR_OK:WERR_GENERAL_FAILURE;
}

static WERROR reg_dir_open_key(REG_HANDLE *h, const char *name, REG_KEY **subkey)
{
	DIR *d;
	char *fullpath;
	REG_KEY *ret;
	TALLOC_CTX *mem_ctx = talloc_init("tmp");
	if(!name) {
		DEBUG(0, ("NULL pointer passed as directory name!"));
		return WERR_INVALID_PARAM;
	}
	fullpath = talloc_asprintf(mem_ctx, "%s%s", h->location, name);
	fullpath = reg_path_win2unix(fullpath);
	
	d = opendir(fullpath);
	if(!d) {
		DEBUG(3,("Unable to open '%s': %s\n", fullpath, strerror(errno)));
		talloc_destroy(mem_ctx);
		return WERR_BADFILE;
	}
	closedir(d);
	ret = reg_key_new_abs(name, h, fullpath);
	talloc_steal(mem_ctx, ret->mem_ctx, fullpath);
	talloc_destroy(mem_ctx);
	*subkey = ret;
	return WERR_OK;
}

static WERROR reg_dir_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***r)
{
	struct dirent *e;
	int max = 200;
	char *fullpath = k->backend_data;
	REG_KEY **ar;
	DIR *d;
	(*count) = 0;
	ar = talloc(k->mem_ctx, sizeof(REG_KEY *) * max);

	d = opendir(fullpath);

	if(!d) return WERR_INVALID_PARAM;
	
	while((e = readdir(d))) {
		if( strcmp(e->d_name, ".") &&
		   strcmp(e->d_name, "..")) {
			struct stat stbuf;
			char *thispath;
			
			/* Check if file is a directory */
			asprintf(&thispath, "%s/%s", fullpath, e->d_name);
			stat(thispath, &stbuf);

			if(S_ISDIR(stbuf.st_mode)) {
				ar[(*count)] = reg_key_new_rel(e->d_name, k, NULL);
				ar[(*count)]->backend_data = talloc_strdup(ar[*count]->mem_ctx, thispath);
				if(ar[(*count)])(*count)++;

				if((*count) == max) {
					max+=200;
					ar = realloc(ar, sizeof(REG_KEY *) * max);
				}
			}

			SAFE_FREE(thispath);
		}
	}

	closedir(d);

	*r = ar;
	return WERR_OK;
}

static WERROR reg_dir_open(REG_HANDLE *h, const char *loc, const char *credentials) {
	if(!loc) return WERR_INVALID_PARAM;
	return WERR_OK;
}

static WERROR reg_dir_add_value(REG_KEY *p, const char *name, int type, void *data, int len)
{
	/* FIXME */
	return WERR_NOT_SUPPORTED;
}

static WERROR reg_dir_del_value(REG_VAL *v)
{
	/* FIXME*/
	return WERR_NOT_SUPPORTED;
}

static struct registry_ops reg_backend_dir = {
	.name = "dir",
	.open_registry = reg_dir_open,
	.open_key = reg_dir_open_key,
	.fetch_subkeys = reg_dir_fetch_subkeys,
	.add_key = reg_dir_add_key,
	.del_key = reg_dir_del_key,
	.add_value = reg_dir_add_value,
	.del_value = reg_dir_del_value,
};

NTSTATUS reg_dir_init(void)
{
	return register_backend("registry", &reg_backend_dir);
}