summaryrefslogtreecommitdiffstats
path: root/source3/registry/reg_backend_printing.c
blob: 3b5e7bfb838c7ec07aee66b52757e85e9e3bf3fe (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 *  Unix SMB/CIFS implementation.
 *  Virtual Windows Registry Layer
 *  Copyright (C) Gerald Carter                     2002-2005
 *  Copyright (c) Andreas Schneider <asn@samba.org> 2010
 *
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

/* Implementation of registry virtual views for printing information */

#include "includes.h"
#include "registry.h"
#include "reg_util_internal.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_REGISTRY

extern struct registry_ops regdb_ops;

/* registry paths used in the print_registry[] */
#define KEY_CONTROL_PRINTERS	"HKLM\\SYSTEM\\CURRENTCONTROLSET\\CONTROL\\PRINT\\PRINTERS"
#define KEY_WINNT_PRINTERS	"HKLM\\SOFTWARE\\MICROSOFT\\WINDOWS NT\\CURRENTVERSION\\PRINT\\PRINTERS"

/* callback table for various registry paths below the ones we service in this module */

struct reg_dyn_tree {
	/* full key path in normalized form */
	const char *path;

	/* callbscks for fetch/store operations */
	int ( *fetch_subkeys) ( const char *path, struct regsubkey_ctr *subkeys );
	bool (*store_subkeys) ( const char *path, struct regsubkey_ctr *subkeys );
	int  (*fetch_values)  ( const char *path, struct regval_ctr *values );
	bool (*store_values)  ( const char *path, struct regval_ctr *values );
};

/*********************************************************************
 *********************************************************************
 ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/PRINTERS"
 ** "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT/PRINTERS"
 *********************************************************************
 *********************************************************************/

static char *create_printer_registry_path(TALLOC_CTX *mem_ctx, const char *key) {
	char *path;
	char *subkey = NULL;

	path = talloc_strdup(mem_ctx, key);
	if (path == NULL) {
		return NULL;
	}

	path = normalize_reg_path(mem_ctx, path);
	if (path == NULL) {
		return NULL;
	}

	if (strncmp(path, KEY_CONTROL_PRINTERS, strlen(KEY_CONTROL_PRINTERS)) == 0) {
		subkey = reg_remaining_path(mem_ctx, key + strlen(KEY_CONTROL_PRINTERS));
		if (subkey == NULL) {
			return NULL;
		}
		return talloc_asprintf(mem_ctx, "%s\\%s", KEY_WINNT_PRINTERS, subkey);
	}

	return NULL;
}

/*********************************************************************
 *********************************************************************/

static int key_printers_fetch_keys( const char *key, struct regsubkey_ctr *subkeys )
{
	TALLOC_CTX *ctx = talloc_tos();
	char *printers_key;

	printers_key = create_printer_registry_path(ctx, key);
	if (printers_key == NULL) {
		/* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
		return regdb_ops.fetch_subkeys(KEY_WINNT_PRINTERS, subkeys);
	}

	return regdb_ops.fetch_subkeys(printers_key, subkeys);
}

/**********************************************************************
 *********************************************************************/

static bool key_printers_store_keys( const char *key, struct regsubkey_ctr *subkeys )
{
	TALLOC_CTX *ctx = talloc_tos();
	char *printers_key;

	printers_key = create_printer_registry_path(ctx, key);
	if (printers_key == NULL) {
		/* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
		return regdb_ops.store_subkeys(KEY_WINNT_PRINTERS, subkeys);
	}

	return regdb_ops.store_subkeys(printers_key, subkeys);
}

/**********************************************************************
 *********************************************************************/

static int key_printers_fetch_values(const char *key, struct regval_ctr *values)
{
	TALLOC_CTX *ctx = talloc_tos();
	char *printers_key;

	printers_key = create_printer_registry_path(ctx, key);
	if (printers_key == NULL) {
		/* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
		return regdb_ops.fetch_values(KEY_WINNT_PRINTERS, values);
	}

	return regdb_ops.fetch_values(printers_key, values);
}

/**********************************************************************
 *********************************************************************/

static bool key_printers_store_values(const char *key, struct regval_ctr *values)
{
	TALLOC_CTX *ctx = talloc_tos();
	char *printers_key;

	printers_key = create_printer_registry_path(ctx, key);
	if (printers_key == NULL) {
		/* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
		return regdb_ops.store_values(KEY_WINNT_PRINTERS, values);
	}

	return regdb_ops.store_values(printers_key, values);
}

/**********************************************************************
 *********************************************************************
 ** Structure to hold dispatch table of ops for various printer keys.
 ** Make sure to always store deeper keys along the same path first so
 ** we ge a more specific match.
 *********************************************************************
 *********************************************************************/

static struct reg_dyn_tree print_registry[] = {
{ KEY_CONTROL_PRINTERS,
	&key_printers_fetch_keys,
	&key_printers_store_keys,
	&key_printers_fetch_values,
	&key_printers_store_values },

{ NULL, NULL, NULL, NULL, NULL }
};


/**********************************************************************
 *********************************************************************
 ** Main reg_printing interface functions
 *********************************************************************
 *********************************************************************/

/***********************************************************************
 Lookup a key in the print_registry table, returning its index.
 -1 on failure
 **********************************************************************/

static int match_registry_path(const char *key)
{
	int i;
	char *path = NULL;
	TALLOC_CTX *ctx = talloc_tos();

	if ( !key )
		return -1;

	path = talloc_strdup(ctx, key);
	if (!path) {
		return -1;
	}
	path = normalize_reg_path(ctx, path);
	if (!path) {
		return -1;
	}

	for ( i=0; print_registry[i].path; i++ ) {
		if (strncmp( path, print_registry[i].path, strlen(print_registry[i].path) ) == 0 )
			return i;
	}

	return -1;
}

/***********************************************************************
 **********************************************************************/

static int regprint_fetch_reg_keys( const char *key, struct regsubkey_ctr *subkeys )
{
	int i = match_registry_path( key );

	if ( i == -1 )
		return -1;

	if ( !print_registry[i].fetch_subkeys )
		return -1;

	return print_registry[i].fetch_subkeys( key, subkeys );
}

/**********************************************************************
 *********************************************************************/

static bool regprint_store_reg_keys( const char *key, struct regsubkey_ctr *subkeys )
{
	int i = match_registry_path( key );

	if ( i == -1 )
		return False;

	if ( !print_registry[i].store_subkeys )
		return False;

	return print_registry[i].store_subkeys( key, subkeys );
}

/**********************************************************************
 *********************************************************************/

static int regprint_fetch_reg_values(const char *key, struct regval_ctr *values)
{
	int i = match_registry_path( key );

	if ( i == -1 )
		return -1;

	/* return 0 values by default since we know the key had
	   to exist because the client opened a handle */

	if ( !print_registry[i].fetch_values )
		return 0;

	return print_registry[i].fetch_values( key, values );
}

/**********************************************************************
 *********************************************************************/

static bool regprint_store_reg_values(const char *key, struct regval_ctr *values)
{
	int i = match_registry_path( key );

	if ( i == -1 )
		return False;

	if ( !print_registry[i].store_values )
		return False;

	return print_registry[i].store_values( key, values );
}

/*
 * Table of function pointers for accessing printing data
 */

struct registry_ops printing_ops = {
	.fetch_subkeys = regprint_fetch_reg_keys,
	.fetch_values = regprint_fetch_reg_values,
	.store_subkeys = regprint_store_reg_keys,
	.store_values = regprint_store_reg_values,
};