2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
5 * Copyright (c) Andreas Schneider 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/>.
22 #include "system/filesys.h"
23 #include "printing/nt_printing_migrate.h"
24 #include "printing/nt_printing_migrate_internal.h"
26 #include "rpc_client/rpc_client.h"
27 #include "librpc/gen_ndr/ndr_spoolss_c.h"
28 #include "librpc/gen_ndr/ndr_winreg.h"
29 #include "rpc_server/rpc_ncacn_np.h"
33 #define FORMS_PREFIX "FORMS/"
34 #define DRIVERS_PREFIX "DRIVERS/"
35 #define PRINTERS_PREFIX "PRINTERS/"
36 #define SECDESC_PREFIX "SECDESC/"
38 static int rename_file_with_suffix(TALLOC_CTX *mem_ctx,
45 dst_path = talloc_asprintf(mem_ctx, "%s%s", path, suffix);
46 if (dst_path == NULL) {
47 DEBUG(3, ("error out of memory\n"));
51 rc = (rename(path, dst_path) != 0);
54 DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
55 } else if (errno == ENOENT) {
56 DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
59 DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
63 TALLOC_FREE(dst_path);
67 static NTSTATUS migrate_internal(TALLOC_CTX *mem_ctx,
69 struct rpc_pipe_client *winreg_pipe)
71 const char *backup_suffix = ".bak";
72 TDB_DATA kbuf, newkey, dbuf;
77 tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDONLY, 0600);
78 if (tdb == NULL && errno == ENOENT) {
79 /* if we have no printers database then migration is
80 considered successful */
81 DEBUG(4, ("No printers database to migrate in %s\n", tdb_path));
85 DEBUG(2, ("Failed to open tdb file: %s\n", tdb_path));
86 return NT_STATUS_NO_SUCH_FILE;
89 for (kbuf = tdb_firstkey(tdb);
91 newkey = tdb_nextkey(tdb, kbuf), free(kbuf.dptr), kbuf = newkey)
93 dbuf = tdb_fetch(tdb, kbuf);
98 if (strncmp((const char *) kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
99 status = printing_tdb_migrate_form(mem_ctx,
101 (const char *) kbuf.dptr + strlen(FORMS_PREFIX),
104 SAFE_FREE(dbuf.dptr);
105 if (!NT_STATUS_IS_OK(status)) {
112 if (strncmp((const char *) kbuf.dptr, DRIVERS_PREFIX, strlen(DRIVERS_PREFIX)) == 0) {
113 status = printing_tdb_migrate_driver(mem_ctx,
115 (const char *) kbuf.dptr + strlen(DRIVERS_PREFIX),
119 SAFE_FREE(dbuf.dptr);
120 if (!NT_STATUS_IS_OK(status)) {
127 if (strncmp((const char *) kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
128 const char *printer_name = (const char *)(kbuf.dptr
129 + strlen(PRINTERS_PREFIX));
130 status = printing_tdb_migrate_printer(mem_ctx,
136 SAFE_FREE(dbuf.dptr);
137 if (!NT_STATUS_IS_OK(status)) {
143 SAFE_FREE(dbuf.dptr);
146 for (kbuf = tdb_firstkey(tdb);
148 newkey = tdb_nextkey(tdb, kbuf), free(kbuf.dptr), kbuf = newkey)
150 dbuf = tdb_fetch(tdb, kbuf);
155 if (strncmp((const char *) kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
156 const char *secdesc_name = (const char *)(kbuf.dptr
157 + strlen(SECDESC_PREFIX));
158 status = printing_tdb_migrate_secdesc(mem_ctx,
163 SAFE_FREE(dbuf.dptr);
164 if (NT_STATUS_EQUAL(status, werror_to_ntstatus(WERR_FILE_NOT_FOUND))) {
165 DEBUG(2, ("Skipping secdesc migration for non-existent "
166 "printer: %s\n", secdesc_name));
167 } else if (!NT_STATUS_IS_OK(status)) {
173 SAFE_FREE(dbuf.dptr);
178 rc = rename_file_with_suffix(mem_ctx, tdb_path, backup_suffix);
180 DEBUG(0, ("Error moving tdb to '%s%s'\n",
181 tdb_path, backup_suffix));
187 bool nt_printing_tdb_migrate(struct messaging_context *msg_ctx)
189 const char *drivers_path;
190 const char *printers_path;
191 const char *forms_path;
193 bool printers_exists;
195 struct auth_session_info *session_info;
196 struct rpc_pipe_client *winreg_pipe = NULL;
197 TALLOC_CTX *tmp_ctx = talloc_stackframe();
200 /* paths talloced on new stackframe */
201 drivers_path = state_path(talloc_tos(), "ntdrivers.tdb");
202 printers_path = state_path(talloc_tos(), "ntprinters.tdb");
203 forms_path = state_path(talloc_tos(), "ntforms.tdb");
204 if ((drivers_path == NULL) || (printers_path == NULL)
205 || (forms_path == NULL)) {
206 talloc_free(tmp_ctx);
209 drivers_exists = file_exist(drivers_path);
210 printers_exists = file_exist(printers_path);
211 forms_exists = file_exist(forms_path);
213 if (!drivers_exists && !printers_exists && !forms_exists) {
214 talloc_free(tmp_ctx);
218 status = make_session_info_system(tmp_ctx, &session_info);
219 if (!NT_STATUS_IS_OK(status)) {
220 DEBUG(0, ("Couldn't create session_info: %s\n",
222 talloc_free(tmp_ctx);
226 status = rpc_pipe_open_interface(tmp_ctx,
233 if (!NT_STATUS_IS_OK(status)) {
234 DEBUG(0, ("Couldn't open internal winreg pipe: %s\n",
236 talloc_free(tmp_ctx);
240 if (drivers_exists) {
241 status = migrate_internal(tmp_ctx, drivers_path, winreg_pipe);
242 if (!NT_STATUS_IS_OK(status)) {
243 DEBUG(0, ("Couldn't migrate drivers tdb file: %s\n",
245 talloc_free(tmp_ctx);
250 if (printers_exists) {
251 status = migrate_internal(tmp_ctx, printers_path, winreg_pipe);
252 if (!NT_STATUS_IS_OK(status)) {
253 DEBUG(0, ("Couldn't migrate printers tdb file: %s\n",
255 talloc_free(tmp_ctx);
261 status = migrate_internal(tmp_ctx, forms_path, winreg_pipe);
262 if (!NT_STATUS_IS_OK(status)) {
263 DEBUG(0, ("Couldn't migrate forms tdb file: %s\n",
265 talloc_free(tmp_ctx);
270 talloc_free(tmp_ctx);