examples/VFS: fix skel_transparent.c in reference to shadow_copy changes
[Samba/gebeck_regimport.git] / source3 / registry / reg_backend_printing.c
blobf76495253e13c9378a59bf4ce01c21b1d475d7ee
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (c) Andreas Schneider <asn@samba.org> 2010
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Implementation of registry virtual views for printing information */
23 #include "includes.h"
24 #include "registry.h"
25 #include "reg_util_internal.h"
26 #include "reg_backend_db.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_REGISTRY
31 /* registry paths used in the print_registry[] */
32 #define KEY_CONTROL_PRINTERS "HKLM\\SYSTEM\\CURRENTCONTROLSET\\CONTROL\\PRINT\\PRINTERS"
33 #define KEY_WINNT_PRINTERS "HKLM\\SOFTWARE\\MICROSOFT\\WINDOWS NT\\CURRENTVERSION\\PRINT\\PRINTERS"
35 /* callback table for various registry paths below the ones we service in this module */
37 struct reg_dyn_tree {
38 /* full key path in normalized form */
39 const char *path;
41 /* callbscks for fetch/store operations */
42 int ( *fetch_subkeys) ( const char *path, struct regsubkey_ctr *subkeys );
43 bool (*store_subkeys) ( const char *path, struct regsubkey_ctr *subkeys );
44 int (*fetch_values) ( const char *path, struct regval_ctr *values );
45 bool (*store_values) ( const char *path, struct regval_ctr *values );
48 /*********************************************************************
49 *********************************************************************
50 ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/PRINTERS"
51 ** "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT/PRINTERS"
52 *********************************************************************
53 *********************************************************************/
55 static char *create_printer_registry_path(TALLOC_CTX *mem_ctx, const char *key) {
56 char *path;
57 char *subkey = NULL;
59 path = talloc_strdup(mem_ctx, key);
60 if (path == NULL) {
61 return NULL;
64 path = normalize_reg_path(mem_ctx, path);
65 if (path == NULL) {
66 return NULL;
69 if (strncmp(path, KEY_CONTROL_PRINTERS, strlen(KEY_CONTROL_PRINTERS)) == 0) {
70 subkey = reg_remaining_path(mem_ctx, key + strlen(KEY_CONTROL_PRINTERS));
71 if (subkey == NULL) {
72 return NULL;
74 return talloc_asprintf(mem_ctx, "%s\\%s", KEY_WINNT_PRINTERS, subkey);
77 return NULL;
80 /*********************************************************************
81 *********************************************************************/
83 static int key_printers_fetch_keys( const char *key, struct regsubkey_ctr *subkeys )
85 TALLOC_CTX *ctx = talloc_tos();
86 char *printers_key;
88 printers_key = create_printer_registry_path(ctx, key);
89 if (printers_key == NULL) {
90 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
91 return regdb_fetch_keys(KEY_WINNT_PRINTERS, subkeys);
94 return regdb_fetch_keys(printers_key, subkeys);
97 /**********************************************************************
98 *********************************************************************/
100 static bool key_printers_store_keys( const char *key, struct regsubkey_ctr *subkeys )
102 TALLOC_CTX *ctx = talloc_tos();
103 char *printers_key;
105 printers_key = create_printer_registry_path(ctx, key);
106 if (printers_key == NULL) {
107 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
108 return regdb_store_keys(KEY_WINNT_PRINTERS, subkeys);
111 return regdb_store_keys(printers_key, subkeys);
114 /**********************************************************************
115 *********************************************************************/
117 static int key_printers_fetch_values(const char *key, struct regval_ctr *values)
119 TALLOC_CTX *ctx = talloc_tos();
120 char *printers_key;
122 printers_key = create_printer_registry_path(ctx, key);
123 if (printers_key == NULL) {
124 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
125 return regdb_fetch_values(KEY_WINNT_PRINTERS, values);
128 return regdb_fetch_values(printers_key, values);
131 /**********************************************************************
132 *********************************************************************/
134 static bool key_printers_store_values(const char *key, struct regval_ctr *values)
136 TALLOC_CTX *ctx = talloc_tos();
137 char *printers_key;
139 printers_key = create_printer_registry_path(ctx, key);
140 if (printers_key == NULL) {
141 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
142 return regdb_store_values(KEY_WINNT_PRINTERS, values);
145 return regdb_store_values(printers_key, values);
148 /**********************************************************************
149 *********************************************************************
150 ** Structure to hold dispatch table of ops for various printer keys.
151 ** Make sure to always store deeper keys along the same path first so
152 ** we ge a more specific match.
153 *********************************************************************
154 *********************************************************************/
156 static struct reg_dyn_tree print_registry[] = {
157 { KEY_CONTROL_PRINTERS,
158 &key_printers_fetch_keys,
159 &key_printers_store_keys,
160 &key_printers_fetch_values,
161 &key_printers_store_values },
163 { NULL, NULL, NULL, NULL, NULL }
167 /**********************************************************************
168 *********************************************************************
169 ** Main reg_printing interface functions
170 *********************************************************************
171 *********************************************************************/
173 /***********************************************************************
174 Lookup a key in the print_registry table, returning its index.
175 -1 on failure
176 **********************************************************************/
178 static int match_registry_path(const char *key)
180 int i;
181 char *path = NULL;
182 TALLOC_CTX *ctx = talloc_tos();
184 if ( !key )
185 return -1;
187 path = talloc_strdup(ctx, key);
188 if (!path) {
189 return -1;
191 path = normalize_reg_path(ctx, path);
192 if (!path) {
193 return -1;
196 for ( i=0; print_registry[i].path; i++ ) {
197 if (strncmp( path, print_registry[i].path, strlen(print_registry[i].path) ) == 0 )
198 return i;
201 return -1;
204 /***********************************************************************
205 **********************************************************************/
207 static int regprint_fetch_reg_keys( const char *key, struct regsubkey_ctr *subkeys )
209 int i = match_registry_path( key );
211 if ( i == -1 )
212 return -1;
214 if ( !print_registry[i].fetch_subkeys )
215 return -1;
217 return print_registry[i].fetch_subkeys( key, subkeys );
220 /**********************************************************************
221 *********************************************************************/
223 static bool regprint_store_reg_keys( const char *key, struct regsubkey_ctr *subkeys )
225 int i = match_registry_path( key );
227 if ( i == -1 )
228 return False;
230 if ( !print_registry[i].store_subkeys )
231 return False;
233 return print_registry[i].store_subkeys( key, subkeys );
236 /**********************************************************************
237 *********************************************************************/
239 static int regprint_fetch_reg_values(const char *key, struct regval_ctr *values)
241 int i = match_registry_path( key );
243 if ( i == -1 )
244 return -1;
246 /* return 0 values by default since we know the key had
247 to exist because the client opened a handle */
249 if ( !print_registry[i].fetch_values )
250 return 0;
252 return print_registry[i].fetch_values( key, values );
255 /**********************************************************************
256 *********************************************************************/
258 static bool regprint_store_reg_values(const char *key, struct regval_ctr *values)
260 int i = match_registry_path( key );
262 if ( i == -1 )
263 return False;
265 if ( !print_registry[i].store_values )
266 return False;
268 return print_registry[i].store_values( key, values );
272 * Table of function pointers for accessing printing data
275 struct registry_ops printing_ops = {
276 .fetch_subkeys = regprint_fetch_reg_keys,
277 .fetch_values = regprint_fetch_reg_values,
278 .store_subkeys = regprint_store_reg_keys,
279 .store_values = regprint_store_reg_values,