2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Local printing tdb migration interface
6 Copyright (C) Guenther Deschner 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
24 #include "utils/net.h"
25 #include "rpc_client/rpc_client.h"
26 #include "rpc_client/cli_pipe.h"
27 #include "librpc/gen_ndr/ndr_ntprinting.h"
28 #include "librpc/gen_ndr/ndr_spoolss.h"
29 #include "../libcli/security/security.h"
30 #include "../librpc/gen_ndr/ndr_security.h"
31 #include "../librpc/gen_ndr/ndr_winreg.h"
33 #include "printing/nt_printing_migrate.h"
35 #define FORMS_PREFIX "FORMS/"
36 #define DRIVERS_PREFIX "DRIVERS/"
37 #define PRINTERS_PREFIX "PRINTERS/"
38 #define SECDESC_PREFIX "SECDESC/"
40 static void dump_form(TALLOC_CTX *mem_ctx,
45 enum ndr_err_code ndr_err;
48 struct ntprinting_form r;
50 printf("found form: %s\n", key_name);
52 blob = data_blob_const(data, length);
56 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
57 (ndr_pull_flags_fn_t)ndr_pull_ntprinting_form);
58 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
59 d_fprintf(stderr, _("form pull failed: %s\n"),
64 s = NDR_PRINT_STRUCT_STRING(mem_ctx, ntprinting_form, &r);
70 static void dump_driver(TALLOC_CTX *mem_ctx,
75 enum ndr_err_code ndr_err;
78 struct ntprinting_driver r;
80 printf("found driver: %s\n", key_name);
82 blob = data_blob_const(data, length);
86 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
87 (ndr_pull_flags_fn_t)ndr_pull_ntprinting_driver);
88 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
89 d_fprintf(stderr, _("driver pull failed: %s\n"),
94 s = NDR_PRINT_STRUCT_STRING(mem_ctx, ntprinting_driver, &r);
100 static void dump_printer(TALLOC_CTX *mem_ctx,
101 const char *key_name,
105 enum ndr_err_code ndr_err;
108 struct ntprinting_printer r;
110 printf("found printer: %s\n", key_name);
112 blob = data_blob_const(data, length);
116 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
117 (ndr_pull_flags_fn_t)ndr_pull_ntprinting_printer);
118 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
119 d_fprintf(stderr, _("printer pull failed: %s\n"),
120 ndr_errstr(ndr_err));
124 s = NDR_PRINT_STRUCT_STRING(mem_ctx, ntprinting_printer, &r);
130 static void dump_sd(TALLOC_CTX *mem_ctx,
131 const char *key_name,
135 enum ndr_err_code ndr_err;
138 struct sec_desc_buf r;
140 printf("found security descriptor: %s\n", key_name);
142 blob = data_blob_const(data, length);
146 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
147 (ndr_pull_flags_fn_t)ndr_pull_sec_desc_buf);
148 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
149 d_fprintf(stderr, _("security descriptor pull failed: %s\n"),
150 ndr_errstr(ndr_err));
154 s = NDR_PRINT_STRUCT_STRING(mem_ctx, sec_desc_buf, &r);
161 static int net_printing_dump(struct net_context *c, int argc,
165 TALLOC_CTX *ctx = talloc_stackframe();
169 if (argc < 1 || c->display_usage) {
170 d_fprintf(stderr, "%s\nnet printing dump <file.tdb>\n",
175 tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0600);
177 d_fprintf(stderr, _("failed to open tdb file: %s\n"), argv[0]);
181 for (kbuf = tdb_firstkey_compat(tdb);
183 kbuf = tdb_nextkey_compat(tdb, kbuf))
185 dbuf = tdb_fetch_compat(tdb, kbuf);
190 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
191 dump_form(ctx, (const char *)kbuf.dptr+strlen(FORMS_PREFIX), dbuf.dptr, dbuf.dsize);
192 SAFE_FREE(dbuf.dptr);
196 if (strncmp((const char *)kbuf.dptr, DRIVERS_PREFIX, strlen(DRIVERS_PREFIX)) == 0) {
197 dump_driver(ctx, (const char *)kbuf.dptr+strlen(DRIVERS_PREFIX), dbuf.dptr, dbuf.dsize);
198 SAFE_FREE(dbuf.dptr);
202 if (strncmp((const char *)kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
203 dump_printer(ctx, (const char *)kbuf.dptr+strlen(PRINTERS_PREFIX), dbuf.dptr, dbuf.dsize);
204 SAFE_FREE(dbuf.dptr);
208 if (strncmp((const char *)kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
209 dump_sd(ctx, (const char *)kbuf.dptr+strlen(SECDESC_PREFIX), dbuf.dptr, dbuf.dsize);
210 SAFE_FREE(dbuf.dptr);
223 static NTSTATUS printing_migrate_internal(struct net_context *c,
224 const struct dom_sid *domain_sid,
225 const char *domain_name,
226 struct cli_state *cli,
227 struct rpc_pipe_client *winreg_pipe,
237 tmp_ctx = talloc_new(mem_ctx);
238 if (tmp_ctx == NULL) {
239 return NT_STATUS_NO_MEMORY;
242 tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0600);
244 d_fprintf(stderr, _("failed to open tdb file: %s\n"), argv[0]);
245 status = NT_STATUS_NO_SUCH_FILE;
249 for (kbuf = tdb_firstkey_compat(tdb);
251 kbuf = tdb_nextkey_compat(tdb, kbuf))
253 dbuf = tdb_fetch_compat(tdb, kbuf);
258 if (strncmp((const char *) kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
259 printing_tdb_migrate_form(tmp_ctx,
261 (const char *) kbuf.dptr + strlen(FORMS_PREFIX),
264 SAFE_FREE(dbuf.dptr);
268 if (strncmp((const char *) kbuf.dptr, DRIVERS_PREFIX, strlen(DRIVERS_PREFIX)) == 0) {
269 printing_tdb_migrate_driver(tmp_ctx,
271 (const char *) kbuf.dptr + strlen(DRIVERS_PREFIX),
274 SAFE_FREE(dbuf.dptr);
278 if (strncmp((const char *) kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
279 printing_tdb_migrate_printer(tmp_ctx,
281 (const char *) kbuf.dptr + strlen(PRINTERS_PREFIX),
284 SAFE_FREE(dbuf.dptr);
287 SAFE_FREE(dbuf.dptr);
290 for (kbuf = tdb_firstkey_compat(tdb);
292 kbuf = tdb_nextkey_compat(tdb, kbuf))
294 dbuf = tdb_fetch_compat(tdb, kbuf);
299 if (strncmp((const char *) kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
300 printing_tdb_migrate_secdesc(tmp_ctx,
302 (const char *) kbuf.dptr + strlen(SECDESC_PREFIX),
305 SAFE_FREE(dbuf.dptr);
308 SAFE_FREE(dbuf.dptr);
312 status = NT_STATUS_OK;
315 talloc_free(tmp_ctx);
319 static int net_printing_migrate(struct net_context *c,
323 if (argc < 1 || c->display_usage) {
325 "net printing migrate <file.tdb>\n"
328 _("Migrate tdb printing files to new storage"));
332 return run_rpc_command(c,
336 printing_migrate_internal,
341 * 'net printing' entrypoint.
342 * @param argc Standard main() style argc.
343 * @param argv Standard main() style argv. Initial components are already
347 int net_printing(struct net_context *c, int argc, const char **argv)
351 struct functable func[] = {
356 N_("Dump printer databases"),
357 N_("net printing dump\n"
358 " Dump tdb printing file")
363 net_printing_migrate,
364 NET_TRANSPORT_LOCAL | NET_TRANSPORT_RPC,
365 N_("Migrate printer databases"),
366 N_("net printing migrate\n"
367 " Migrate tdb printing files to new storage")
370 { NULL, NULL, 0, NULL, NULL }
373 ret = net_run_function(c, argc, argv, "net printing", func);