static NTSTATUS printing_migrate_internal(struct net_context *c, const struct dom_sid *domain_sid, const char *domain_name, struct cli_state *cli, struct rpc_pipe_client *winreg_pipe, TALLOC_CTX *mem_ctx, int argc, const char **argv) { TALLOC_CTX *tmp_ctx; TDB_CONTEXT *tdb; TDB_DATA kbuf, newkey, dbuf; NTSTATUS status; tmp_ctx = talloc_new(mem_ctx); if (tmp_ctx == NULL) { return NT_STATUS_NO_MEMORY; } tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0600); if (tdb == NULL) { d_fprintf(stderr, _("failed to open tdb file: %s\n"), argv[0]); status = NT_STATUS_NO_SUCH_FILE; goto done; } for (kbuf = tdb_firstkey(tdb); kbuf.dptr; newkey = tdb_nextkey(tdb, kbuf), free(kbuf.dptr), kbuf = newkey) { dbuf = tdb_fetch(tdb, kbuf); if (!dbuf.dptr) { continue; } if (strncmp((const char *) kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) { printing_tdb_migrate_form(tmp_ctx, winreg_pipe, (const char *) kbuf.dptr + strlen(FORMS_PREFIX), dbuf.dptr, dbuf.dsize); SAFE_FREE(dbuf.dptr); continue; } if (strncmp((const char *) kbuf.dptr, DRIVERS_PREFIX, strlen(DRIVERS_PREFIX)) == 0) { printing_tdb_migrate_driver(tmp_ctx, winreg_pipe, (const char *) kbuf.dptr + strlen(DRIVERS_PREFIX), dbuf.dptr, dbuf.dsize); SAFE_FREE(dbuf.dptr); continue; } if (strncmp((const char *) kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) { printing_tdb_migrate_printer(tmp_ctx, winreg_pipe, (const char *) kbuf.dptr + strlen(PRINTERS_PREFIX), dbuf.dptr, dbuf.dsize); SAFE_FREE(dbuf.dptr); continue; } SAFE_FREE(dbuf.dptr); } for (kbuf = tdb_firstkey(tdb); kbuf.dptr; newkey = tdb_nextkey(tdb, kbuf), free(kbuf.dptr), kbuf = newkey) { dbuf = tdb_fetch(tdb, kbuf); if (!dbuf.dptr) { continue; } if (strncmp((const char *) kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) { printing_tdb_migrate_secdesc(tmp_ctx, winreg_pipe, (const char *) kbuf.dptr + strlen(SECDESC_PREFIX), dbuf.dptr, dbuf.dsize); SAFE_FREE(dbuf.dptr); continue; } SAFE_FREE(dbuf.dptr); } status = NT_STATUS_OK; done: talloc_free(tmp_ctx); return status; }
static NTSTATUS migrate_internal(TALLOC_CTX *mem_ctx, const char *tdb_path, struct rpc_pipe_client *winreg_pipe) { const char *backup_suffix = ".bak"; TDB_DATA kbuf, newkey, dbuf; TDB_CONTEXT *tdb; NTSTATUS status; int rc; tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDONLY, 0600); if (tdb == NULL && errno == ENOENT) { /* if we have no printers database then migration is considered successful */ DEBUG(4, ("No printers database to migrate in %s\n", tdb_path)); return NT_STATUS_OK; } if (tdb == NULL) { DEBUG(2, ("Failed to open tdb file: %s\n", tdb_path)); return NT_STATUS_NO_SUCH_FILE; } for (kbuf = tdb_firstkey(tdb); kbuf.dptr; newkey = tdb_nextkey(tdb, kbuf), free(kbuf.dptr), kbuf = newkey) { dbuf = tdb_fetch(tdb, kbuf); if (!dbuf.dptr) { continue; } if (strncmp((const char *) kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) { status = printing_tdb_migrate_form(mem_ctx, winreg_pipe, (const char *) kbuf.dptr + strlen(FORMS_PREFIX), dbuf.dptr, dbuf.dsize); SAFE_FREE(dbuf.dptr); if (!NT_STATUS_IS_OK(status)) { tdb_close(tdb); return status; } continue; } if (strncmp((const char *) kbuf.dptr, DRIVERS_PREFIX, strlen(DRIVERS_PREFIX)) == 0) { status = printing_tdb_migrate_driver(mem_ctx, winreg_pipe, (const char *) kbuf.dptr + strlen(DRIVERS_PREFIX), dbuf.dptr, dbuf.dsize, false); SAFE_FREE(dbuf.dptr); if (!NT_STATUS_IS_OK(status)) { tdb_close(tdb); return status; } continue; } if (strncmp((const char *) kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) { const char *printer_name = (const char *)(kbuf.dptr + strlen(PRINTERS_PREFIX)); status = printing_tdb_migrate_printer(mem_ctx, winreg_pipe, printer_name, dbuf.dptr, dbuf.dsize, false); SAFE_FREE(dbuf.dptr); if (!NT_STATUS_IS_OK(status)) { tdb_close(tdb); return status; } continue; } SAFE_FREE(dbuf.dptr); } for (kbuf = tdb_firstkey(tdb); kbuf.dptr; newkey = tdb_nextkey(tdb, kbuf), free(kbuf.dptr), kbuf = newkey) { dbuf = tdb_fetch(tdb, kbuf); if (!dbuf.dptr) { continue; } if (strncmp((const char *) kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) { const char *secdesc_name = (const char *)(kbuf.dptr + strlen(SECDESC_PREFIX)); status = printing_tdb_migrate_secdesc(mem_ctx, winreg_pipe, secdesc_name, dbuf.dptr, dbuf.dsize); SAFE_FREE(dbuf.dptr); if (NT_STATUS_EQUAL(status, werror_to_ntstatus(WERR_FILE_NOT_FOUND))) { DEBUG(2, ("Skipping secdesc migration for non-existent " "printer: %s\n", secdesc_name)); } else if (!NT_STATUS_IS_OK(status)) { tdb_close(tdb); return status; } continue; } SAFE_FREE(dbuf.dptr); } tdb_close(tdb); rc = rename_file_with_suffix(mem_ctx, tdb_path, backup_suffix); if (rc != 0) { DEBUG(0, ("Error moving tdb to '%s%s'\n", tdb_path, backup_suffix)); } return NT_STATUS_OK; }