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"
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 */
38 /* full key path in normalized form */
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
) {
59 path
= talloc_strdup(mem_ctx
, key
);
64 path
= normalize_reg_path(mem_ctx
, path
);
69 if (strncmp(path
, KEY_CONTROL_PRINTERS
, strlen(KEY_CONTROL_PRINTERS
)) == 0) {
70 subkey
= reg_remaining_path(mem_ctx
, key
+ strlen(KEY_CONTROL_PRINTERS
));
74 return talloc_asprintf(mem_ctx
, "%s\\%s", KEY_WINNT_PRINTERS
, subkey
);
80 /*********************************************************************
81 *********************************************************************/
83 static int key_printers_fetch_keys( const char *key
, struct regsubkey_ctr
*subkeys
)
85 TALLOC_CTX
*ctx
= talloc_tos();
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();
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();
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();
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.
176 **********************************************************************/
178 static int match_registry_path(const char *key
)
182 TALLOC_CTX
*ctx
= talloc_tos();
187 path
= talloc_strdup(ctx
, key
);
191 path
= normalize_reg_path(ctx
, path
);
196 for ( i
=0; print_registry
[i
].path
; i
++ ) {
197 if (strncmp( path
, print_registry
[i
].path
, strlen(print_registry
[i
].path
) ) == 0 )
204 /***********************************************************************
205 **********************************************************************/
207 static int regprint_fetch_reg_keys( const char *key
, struct regsubkey_ctr
*subkeys
)
209 int i
= match_registry_path( key
);
214 if ( !print_registry
[i
].fetch_subkeys
)
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
);
230 if ( !print_registry
[i
].store_subkeys
)
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
);
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
)
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
);
265 if ( !print_registry
[i
].store_values
)
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
,