summaryrefslogtreecommitdiff
path: root/source4/lib/registry/reg_backend_dir/reg_backend_dir.c
blob: baed39b4eb0f9cc3bfb2943e6a87b4eaf66140f3 (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
145
146
147
148
149
150
151
152
153
154
/* 
   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 DIR *reg_dir_dir(REG_HANDLE *h, const char *base, const char *name)
{
	char *path = NULL;
	DIR *d;
	asprintf(&path, "%s/%s/%s", h->location, base, name);
	path = reg_path_win2unix(path);
	
	d = opendir(path);
	if(!d) {
		printf("Unable to open '%s'\n", path);
		return NULL;
	}
	SAFE_FREE(path);
	return d;
}

static BOOL reg_dir_add_key(REG_KEY *parent, const char *name)
{
	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);
	free(path);
	return (ret == 0);
}

static BOOL reg_dir_del_key(REG_KEY *k)
{
	char *path;
	int ret;
	asprintf(&path, "%s/%s", k->handle->location, reg_key_get_path(k));
	path = reg_path_win2unix(path);
	ret = rmdir(path);
	free(path);
	return (ret == 0);
}

static REG_KEY *reg_dir_open_key(REG_HANDLE *h, const char *name)
{
	DIR *d;
	char *fullpath;
	if(!name) {
		DEBUG(0, ("NULL pointer passed as directory name!"));
		return NULL;
	}
	fullpath = reg_path_win2unix(strdup(name));
	d = reg_dir_dir(h, "", fullpath);
	free(fullpath);
	
	if(d) return reg_key_new_abs(name, h, d);
	return NULL;
}

static BOOL reg_dir_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***r)
{
	DIR *d = (DIR *)k->backend_data;
	struct dirent *e;
	int max = 200;
	REG_KEY **ar;
	if(!d) return False;
	rewinddir(d);
	(*count) = 0;
	ar = malloc(sizeof(REG_KEY *) * max);
	
	while((e = readdir(d))) {
		if(e->d_type == DT_DIR && 
		   strcmp(e->d_name, ".") &&
		   strcmp(e->d_name, "..")) {
			char *fullpath = reg_path_win2unix(strdup(k->path));
			ar[(*count)] = reg_key_new_rel(e->d_name, k, reg_dir_dir(k->handle, fullpath, e->d_name));
			free(fullpath);
			if(ar[(*count)])(*count)++;

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

static BOOL reg_dir_open(REG_HANDLE *h, const char *loc, BOOL try) {
	if(!loc) return False;
	return True;
}

static void dir_free(REG_KEY *k) 
{
	closedir((DIR *)k->backend_data);
}

static REG_VAL *reg_dir_add_value(REG_KEY *p, const char *name, int type, void *data, int len)
{
	REG_VAL *ret = reg_val_new(p, NULL);
	char *fullpath;
	FILE *fd;
	ret->name = name?strdup(name):NULL;
	fullpath = reg_path_win2unix(strdup(reg_val_get_path(ret)));
	
	fd = fopen(fullpath, "w+");
	
	/* FIXME */
	return NULL;
}

static BOOL reg_dir_del_value(REG_VAL *v)
{
	char *fullpath = reg_path_win2unix(strdup(reg_val_get_path(v)));
	return False;
}

static REG_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,
	.free_key_backend_data = dir_free
};

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