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 */
25 #include "reg_util_internal.h"
26 #include "reg_backend_db.h"
27 #include "reg_objects.h"
28 #include "../librpc/gen_ndr/ndr_spoolss.h"
31 #define DBGC_CLASS DBGC_REGISTRY
33 /* registry paths used in the print_registry[] */
34 #define KEY_CONTROL_PRINTERS "HKLM\\SYSTEM\\CURRENTCONTROLSET\\CONTROL\\PRINT\\PRINTERS"
35 #define KEY_WINNT_PRINTERS "HKLM\\SOFTWARE\\MICROSOFT\\WINDOWS NT\\CURRENTVERSION\\PRINT\\PRINTERS"
37 /* callback table for various registry paths below the ones we service in this module */
40 /* full key path in normalized form */
43 /* callbscks for fetch/store operations */
44 int ( *fetch_subkeys) ( const char *path, struct regsubkey_ctr *subkeys );
45 bool (*store_subkeys) ( const char *path, struct regsubkey_ctr *subkeys );
46 int (*fetch_values) ( const char *path, struct regval_ctr *values );
47 bool (*store_values) ( const char *path, struct regval_ctr *values );
50 /*********************************************************************
51 *********************************************************************
52 ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/PRINTERS"
53 ** "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT/PRINTERS"
54 *********************************************************************
55 *********************************************************************/
57 static char *create_printer_registry_path(TALLOC_CTX *mem_ctx, const char *key) {
61 path = talloc_strdup(mem_ctx, key);
66 path = normalize_reg_path(mem_ctx, path);
71 if (strncmp(path, KEY_CONTROL_PRINTERS, strlen(KEY_CONTROL_PRINTERS)) == 0) {
72 subkey = reg_remaining_path(mem_ctx, key + strlen(KEY_CONTROL_PRINTERS));
76 return talloc_asprintf(mem_ctx, "%s\\%s", KEY_WINNT_PRINTERS, subkey);
82 /*********************************************************************
83 *********************************************************************/
85 static int key_printers_fetch_keys( const char *key, struct regsubkey_ctr *subkeys )
87 TALLOC_CTX *ctx = talloc_tos();
90 printers_key = create_printer_registry_path(ctx, key);
91 if (printers_key == NULL) {
92 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
93 return regdb_fetch_keys(KEY_WINNT_PRINTERS, subkeys);
96 return regdb_fetch_keys(printers_key, subkeys);
99 /**********************************************************************
100 *********************************************************************/
102 static bool key_printers_store_keys( const char *key, struct regsubkey_ctr *subkeys )
104 TALLOC_CTX *ctx = talloc_tos();
107 printers_key = create_printer_registry_path(ctx, key);
108 if (printers_key == NULL) {
109 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
110 return regdb_store_keys(KEY_WINNT_PRINTERS, subkeys);
113 return regdb_store_keys(printers_key, subkeys);
116 /**********************************************************************
117 *********************************************************************/
119 static int key_printers_fetch_values(const char *key, struct regval_ctr *values)
121 TALLOC_CTX *ctx = talloc_tos();
124 printers_key = create_printer_registry_path(ctx, key);
125 if (printers_key == NULL) {
126 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
127 return regdb_fetch_values(KEY_WINNT_PRINTERS, values);
130 return regdb_fetch_values(printers_key, values);
133 /**********************************************************************
134 *********************************************************************/
136 static bool key_printers_store_values(const char *key, struct regval_ctr *values)
138 TALLOC_CTX *ctx = talloc_tos();
141 printers_key = create_printer_registry_path(ctx, key);
142 if (printers_key == NULL) {
143 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
144 return regdb_store_values(KEY_WINNT_PRINTERS, values);
147 return regdb_store_values(printers_key, values);
150 /**********************************************************************
151 *********************************************************************
152 ** Structure to hold dispatch table of ops for various printer keys.
153 ** Make sure to always store deeper keys along the same path first so
154 ** we ge a more specific match.
155 *********************************************************************
156 *********************************************************************/
158 static struct reg_dyn_tree print_registry[] = {
159 { KEY_CONTROL_PRINTERS,
160 &key_printers_fetch_keys,
161 &key_printers_store_keys,
162 &key_printers_fetch_values,
163 &key_printers_store_values },
165 { NULL, NULL, NULL, NULL, NULL }
169 /**********************************************************************
170 *********************************************************************
171 ** Main reg_printing interface functions
172 *********************************************************************
173 *********************************************************************/
175 /***********************************************************************
176 Lookup a key in the print_registry table, returning its index.
178 **********************************************************************/
180 static int match_registry_path(const char *key)
184 TALLOC_CTX *ctx = talloc_tos();
189 path = talloc_strdup(ctx, key);
193 path = normalize_reg_path(ctx, path);
198 for ( i=0; print_registry[i].path; i++ ) {
199 if (strncmp( path, print_registry[i].path, strlen(print_registry[i].path) ) == 0 )
206 /***********************************************************************
207 **********************************************************************/
209 static int regprint_fetch_reg_keys( const char *key, struct regsubkey_ctr *subkeys )
211 int i = match_registry_path( key );
216 if ( !print_registry[i].fetch_subkeys )
219 return print_registry[i].fetch_subkeys( key, subkeys );
222 /**********************************************************************
223 *********************************************************************/
225 static bool regprint_store_reg_keys( const char *key, struct regsubkey_ctr *subkeys )
227 int i = match_registry_path( key );
232 if ( !print_registry[i].store_subkeys )
235 return print_registry[i].store_subkeys( key, subkeys );
238 /**********************************************************************
239 *********************************************************************/
241 static int regprint_fetch_reg_values(const char *key, struct regval_ctr *values)
243 int i = match_registry_path( key );
248 /* return 0 values by default since we know the key had
249 to exist because the client opened a handle */
251 if ( !print_registry[i].fetch_values )
254 return print_registry[i].fetch_values( key, values );
257 /**********************************************************************
258 *********************************************************************/
260 static bool regprint_store_reg_values(const char *key, struct regval_ctr *values)
262 int i = match_registry_path( key );
267 if ( !print_registry[i].store_values )
270 return print_registry[i].store_values( key, values );
274 * Table of function pointers for accessing printing data
277 struct registry_ops printing_ops = {
278 .fetch_subkeys = regprint_fetch_reg_keys,
279 .fetch_values = regprint_fetch_reg_values,
280 .store_subkeys = regprint_store_reg_keys,
281 .store_values = regprint_store_reg_values,