| 1 |
/* |
| 2 |
Unix SMB/CIFS implementation. |
| 3 |
test suite for iremotewinspool driver rpc operations |
| 4 |
|
| 5 |
Copyright (C) Justin Stephenson 2018 |
| 6 |
|
| 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. |
| 11 |
|
| 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. |
| 16 |
|
| 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/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <dirent.h> |
| 22 |
#include <talloc.h> |
| 23 |
#include <libgen.h> |
| 24 |
#include "includes.h" |
| 25 |
#include "torture/torture.h" |
| 26 |
#include "librpc/gen_ndr/ndr_winspool.h" |
| 27 |
#include "librpc/gen_ndr/ndr_winspool_c.h" |
| 28 |
#include "librpc/gen_ndr/ndr_spoolss_c.h" |
| 29 |
#include "librpc/gen_ndr/ndr_winreg_c.h" |
| 30 |
#include "torture/rpc/torture_rpc.h" |
| 31 |
#include "libcli/registry/util_reg.h" |
| 32 |
#include "torture/rpc/iremotewinspool_common.h" |
| 33 |
#include "libcli/libcli.h" |
| 34 |
#include "param/param.h" |
| 35 |
#include "lib/registry/registry.h" |
| 36 |
#include "libcli/libcli.h" |
| 37 |
#include "libcli/raw/raw_proto.h" |
| 38 |
#include "libcli/resolve/resolve.h" |
| 39 |
#include "libcli/smb2/smb2.h" |
| 40 |
#include "libcli/smb2/smb2_calls.h" |
| 41 |
#include "lib/cmdline/cmdline.h" |
| 42 |
#include "system/filesys.h" |
| 43 |
#include "lib/util/tftw.h" |
| 44 |
|
| 45 |
/* Connect to print driver share //server_name/share */ |
| 46 |
static bool smb_connect_print_share(struct torture_context *tctx, |
| 47 |
const char *server_name, |
| 48 |
const char *share_name, |
| 49 |
struct smbcli_state **cli) |
| 50 |
{ |
| 51 |
NTSTATUS status; |
| 52 |
bool ok = true; |
| 53 |
|
| 54 |
struct smbcli_options smb_options; |
| 55 |
struct smbcli_session_options smb_session_options; |
| 56 |
|
| 57 |
torture_comment(tctx, "Connecting to printer driver share '//%s/%s'\n", |
| 58 |
server_name, share_name); |
| 59 |
|
| 60 |
lpcfg_smbcli_options(tctx->lp_ctx, &smb_options); |
| 61 |
lpcfg_smbcli_session_options(tctx->lp_ctx, &smb_session_options); |
| 62 |
|
| 63 |
/* On Windows, SMB1 must be enabled! */ |
| 64 |
status = smbcli_full_connection(tctx, cli, server_name, |
| 65 |
share_name, NULL, |
| 66 |
lpcfg_socket_options(tctx->lp_ctx), |
| 67 |
samba_cmdline_get_creds(), |
| 68 |
tctx->lp_ctx, |
| 69 |
lpcfg_resolve_context(tctx->lp_ctx), |
| 70 |
tctx->ev, |
| 71 |
&smb_options, |
| 72 |
&smb_session_options, |
| 73 |
lpcfg_gensec_settings(tctx, tctx->lp_ctx)); |
| 74 |
|
| 75 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "Failed to connect to print$ share"); |
| 76 |
|
| 77 |
done: |
| 78 |
|
| 79 |
return ok; |
| 80 |
} |
| 81 |
|
| 82 |
/* Copy file to destination where dst_fpath is a smb share path, |
| 83 |
* files are either created or overwritten */ |
| 84 |
static bool smb_copy_files(TALLOC_CTX *tctx, |
| 85 |
const char *fpath, |
| 86 |
const char *dst_fpath, |
| 87 |
struct test_driver_info *dinfo) |
| 88 |
{ |
| 89 |
FILE *fp; |
| 90 |
int smbfp = 0; |
| 91 |
char *buffer = NULL; |
| 92 |
int maxwrite = 64512; |
| 93 |
size_t nread; |
| 94 |
ssize_t nwrote; |
| 95 |
bool ok = true; |
| 96 |
size_t total_read; |
| 97 |
|
| 98 |
fp = fopen(fpath, "r"); |
| 99 |
torture_assert_goto(tctx, fp, ok, done, "Failed to open local file\n"); |
| 100 |
|
| 101 |
smbfp = smbcli_open(dinfo->cli->tree, dst_fpath, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE); |
| 102 |
torture_assert_int_not_equal_goto(tctx, smbfp, -1, ok, done, "Failed to open dst file\n"); |
| 103 |
|
| 104 |
buffer = talloc_array(tctx, char, maxwrite); |
| 105 |
torture_assert_not_null_goto(tctx, buffer, ok, done, "Failed to allocate buffer\n"); |
| 106 |
|
| 107 |
total_read = 0; |
| 108 |
|
| 109 |
while (!feof(fp)) { |
| 110 |
nread = fread(buffer, 1, maxwrite, fp); |
| 111 |
if (ferror(fp)) { |
| 112 |
torture_warning(tctx, "Error reading file [%s]\n", fpath); |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
nwrote = smbcli_write(dinfo->cli->tree, smbfp, 0, buffer, total_read, nread); |
| 117 |
if (nwrote != nread) { |
| 118 |
torture_warning(tctx, "Not all data in stream written!\n"); |
| 119 |
} |
| 120 |
|
| 121 |
total_read += nread; |
| 122 |
} |
| 123 |
|
| 124 |
fclose(fp); |
| 125 |
smbcli_close(dinfo->cli->tree, smbfp); |
| 126 |
done: |
| 127 |
|
| 128 |
TALLOC_FREE(buffer); |
| 129 |
return ok; |
| 130 |
} |
| 131 |
|
| 132 |
/* Callback function provided to tftw() to |
| 133 |
* copy driver files to smb share */ |
| 134 |
static int copy_driver_files(TALLOC_CTX *tctx, |
| 135 |
const char *fpath, |
| 136 |
const struct stat *sb, |
| 137 |
enum tftw_flags_e flag, |
| 138 |
void *userdata) |
| 139 |
{ |
| 140 |
char *dst_fpath = NULL; |
| 141 |
struct test_driver_info *dinfo = userdata; |
| 142 |
char *path = NULL; |
| 143 |
NTSTATUS status; |
| 144 |
bool ok = true; |
| 145 |
|
| 146 |
path = talloc_strdup(tctx, fpath + dinfo->driver_path_len); |
| 147 |
torture_assert_not_null_goto(tctx, path, ok, done, "Cannot allocate memory"); |
| 148 |
|
| 149 |
string_replace(path, '/', '\\'); |
| 150 |
|
| 151 |
dst_fpath = talloc_asprintf(tctx, "%s%s", dinfo->print_upload_guid_dir, path); |
| 152 |
torture_assert_not_null_goto(tctx, dst_fpath, ok, done, "Cannot allocate memory"); |
| 153 |
|
| 154 |
switch (flag) { |
| 155 |
case TFTW_FLAG_FILE: |
| 156 |
ok = smb_copy_files(tctx, fpath, dst_fpath, dinfo); |
| 157 |
torture_assert_goto(tctx, ok, ok, done, "Failed to copy files over smb"); |
| 158 |
break; |
| 159 |
case TFTW_FLAG_DIR: |
| 160 |
status = smbcli_mkdir(dinfo->cli->tree, dst_fpath); |
| 161 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "Failed to create directories"); |
| 162 |
break; |
| 163 |
case TFTW_FLAG_SLINK: |
| 164 |
case TFTW_FLAG_DNR: |
| 165 |
case TFTW_FLAG_NSTAT: |
| 166 |
case TFTW_FLAG_SPEC: |
| 167 |
case TFTW_FLAG_DP: |
| 168 |
case TFTW_FLAG_SLN: |
| 169 |
torture_warning(tctx, "WARN: Unhandled typeflag [%s]\n", fpath); |
| 170 |
break; |
| 171 |
} |
| 172 |
|
| 173 |
done: |
| 174 |
TALLOC_FREE(path); |
| 175 |
TALLOC_FREE(dst_fpath); |
| 176 |
|
| 177 |
if (ok == true) { |
| 178 |
return 0; |
| 179 |
} else { |
| 180 |
return 1; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
static bool test_get_driver_torture_options(struct torture_context *tctx, |
| 185 |
const char **_local_driver_path, |
| 186 |
const char **_inf_file, |
| 187 |
const char **_driver_name, |
| 188 |
const char **_driver_arch, |
| 189 |
const char **_core_driver_inf) |
| 190 |
{ |
| 191 |
const char *local_driver_path = NULL; |
| 192 |
const char *inf_file = NULL; |
| 193 |
const char *driver_name = NULL; |
| 194 |
const char *driver_arch = NULL; |
| 195 |
const char *core_driver_inf = NULL; |
| 196 |
const char *arches_list[] = { |
| 197 |
SPOOLSS_ARCHITECTURE_x64, |
| 198 |
SPOOLSS_ARCHITECTURE_NT_X86, |
| 199 |
SPOOLSS_ARCHITECTURE_IA_64, |
| 200 |
SPOOLSS_ARCHITECTURE_ARM, |
| 201 |
SPOOLSS_ARCHITECTURE_4_0, |
| 202 |
NULL, |
| 203 |
}; |
| 204 |
const char **p; |
| 205 |
bool valid = false; |
| 206 |
bool ok = true; |
| 207 |
|
| 208 |
local_driver_path = torture_setting_string(tctx, "driver_path", NULL); |
| 209 |
if (local_driver_path == NULL) { |
| 210 |
torture_fail(tctx, |
| 211 |
"option --option=torture:driver_path=" |
| 212 |
"/full/path/to/local/driver/dir\n"); |
| 213 |
} |
| 214 |
|
| 215 |
inf_file = torture_setting_string(tctx, "inf_file", NULL); |
| 216 |
if (inf_file == NULL) { |
| 217 |
torture_fail(tctx, |
| 218 |
"option --option=torture:inf_file=" |
| 219 |
"filename.inf\n"); |
| 220 |
} |
| 221 |
|
| 222 |
driver_name = torture_setting_string(tctx, "driver_name", NULL); |
| 223 |
if (driver_name == NULL) { |
| 224 |
torture_fail(tctx, |
| 225 |
"option --option=torture:driver_name=" |
| 226 |
"driver name\n"); |
| 227 |
} |
| 228 |
|
| 229 |
driver_arch = torture_setting_string(tctx, "driver_arch", NULL); |
| 230 |
if (driver_arch == NULL) { |
| 231 |
torture_fail(tctx, |
| 232 |
"option --option=torture:driver_arch=" |
| 233 |
"driver arch\n"); |
| 234 |
} |
| 235 |
|
| 236 |
core_driver_inf = torture_setting_string(tctx, "core_driver_inf", NULL); |
| 237 |
|
| 238 |
for (p = arches_list; *p != NULL; p++) { |
| 239 |
if (strequal(*p, driver_arch) == 0) { |
| 240 |
valid = true; |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
torture_assert_goto(tctx, valid, ok, done, "Invalid driver arch provided"); |
| 245 |
|
| 246 |
*_local_driver_path = local_driver_path; |
| 247 |
*_inf_file = inf_file; |
| 248 |
*_driver_name = driver_name; |
| 249 |
*_driver_arch = driver_arch; |
| 250 |
*_core_driver_inf = core_driver_inf; |
| 251 |
done: |
| 252 |
return ok; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
static bool test_get_misc_driver_info(struct torture_context *tctx, |
| 257 |
struct test_driver_info *dinfo, |
| 258 |
const char **_abs_inf_path, |
| 259 |
size_t *_driver_path_len) |
| 260 |
{ |
| 261 |
const char *abs_inf_path; |
| 262 |
size_t driver_path_len; |
| 263 |
bool ok = true; |
| 264 |
|
| 265 |
driver_path_len = strlen(dinfo->local_driver_path); |
| 266 |
torture_assert_int_not_equal_goto(tctx, driver_path_len, 0, ok, done, "driver path length is 0"); |
| 267 |
|
| 268 |
abs_inf_path = talloc_asprintf(tctx, "%s/%s", dinfo->local_driver_path, dinfo->inf_file); |
| 269 |
torture_assert_not_null_goto(tctx, abs_inf_path, ok, done, "Cannot allocate memory"); |
| 270 |
|
| 271 |
*_abs_inf_path = abs_inf_path; |
| 272 |
*_driver_path_len = driver_path_len; |
| 273 |
done: |
| 274 |
|
| 275 |
return ok; |
| 276 |
} |
| 277 |
|
| 278 |
/* Uninstall the previously installed print driver */ |
| 279 |
static bool test_uninstall_printer_driver(struct torture_context *tctx, |
| 280 |
struct test_iremotewinspool_context *ctx) |
| 281 |
{ |
| 282 |
struct dcerpc_pipe *p = ctx->iremotewinspool_pipe; |
| 283 |
struct dcerpc_binding_handle *b = p->binding_handle; |
| 284 |
struct winspool_AsyncDeletePrinterDriverEx r; |
| 285 |
bool ok = true; |
| 286 |
NTSTATUS status; |
| 287 |
|
| 288 |
r.in.pName = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p)); |
| 289 |
|
| 290 |
r.in.pDriverName = talloc_strdup(tctx, ctx->dinfo->driver_name); |
| 291 |
torture_assert_not_null_goto(tctx, r.in.pDriverName, ok, done, "Cannot allocate memory"); |
| 292 |
|
| 293 |
r.in.pEnvironment = SPOOLSS_ARCHITECTURE_x64; |
| 294 |
|
| 295 |
r.in.dwDeleteFlag = 0; |
| 296 |
r.in.dwVersionNum = 0; |
| 297 |
|
| 298 |
status = dcerpc_winspool_AsyncDeletePrinterDriverEx_r(b, tctx, &r); |
| 299 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "AsyncDeletePrinterDriverEx failed"); |
| 300 |
|
| 301 |
torture_assert_werr_ok(tctx, r.out.result, "AsyncDeletePrinterDriverEx failed"); |
| 302 |
done: |
| 303 |
|
| 304 |
return ok; |
| 305 |
} |
| 306 |
|
| 307 |
/* Remove the leftover print driver package files from the driver store */ |
| 308 |
static bool test_remove_driver_package(struct torture_context *tctx, |
| 309 |
struct test_iremotewinspool_context *ctx) |
| 310 |
{ |
| 311 |
struct dcerpc_pipe *p = ctx->iremotewinspool_pipe; |
| 312 |
struct dcerpc_binding_handle *b = p->binding_handle; |
| 313 |
struct winspool_AsyncDeletePrinterDriverPackage r; |
| 314 |
bool ok = true; |
| 315 |
NTSTATUS status; |
| 316 |
|
| 317 |
r.in.pszServer = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p)); |
| 318 |
torture_assert_not_null_goto(tctx, r.in.pszServer, ok, done, "Cannot allocate memory"); |
| 319 |
|
| 320 |
r.in.pszInfPath = ctx->dinfo->uploaded_inf_path; |
| 321 |
|
| 322 |
r.in.pszEnvironment = SPOOLSS_ARCHITECTURE_x64; |
| 323 |
|
| 324 |
status = dcerpc_winspool_AsyncDeletePrinterDriverPackage_r(b, tctx, &r); |
| 325 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "AsyncDeletePrinterPackage failed"); |
| 326 |
|
| 327 |
torture_assert_hresult_ok(tctx, r.out.result, "AsyncDeletePrinterDriverPackage failed"); |
| 328 |
done: |
| 329 |
|
| 330 |
return ok; |
| 331 |
} |
| 332 |
|
| 333 |
static bool test_winreg_iremotewinspool_openhklm(struct torture_context *tctx, |
| 334 |
struct dcerpc_binding_handle *winreg_bh, |
| 335 |
struct policy_handle *_hklm_handle) |
| 336 |
{ |
| 337 |
struct winreg_OpenHKLM r; |
| 338 |
NTSTATUS status; |
| 339 |
bool ok = true; |
| 340 |
|
| 341 |
r.in.system_name = NULL; |
| 342 |
r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; |
| 343 |
r.out.handle = _hklm_handle; |
| 344 |
|
| 345 |
status = dcerpc_winreg_OpenHKLM_r(winreg_bh, tctx, &r); |
| 346 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "Failed to Open HKLM"); |
| 347 |
|
| 348 |
torture_assert_werr_ok(tctx, r.out.result, "Failed to Open HKLM"); |
| 349 |
done: |
| 350 |
|
| 351 |
return ok; |
| 352 |
} |
| 353 |
|
| 354 |
static bool test_winreg_iremotewinspool_openkey(struct torture_context *tctx, |
| 355 |
struct dcerpc_binding_handle *winreg_bh, |
| 356 |
struct policy_handle *hklm_handle, |
| 357 |
const char *keyname, |
| 358 |
struct policy_handle *_key_handle) |
| 359 |
{ |
| 360 |
struct winreg_OpenKey r; |
| 361 |
NTSTATUS status; |
| 362 |
bool ok = true; |
| 363 |
|
| 364 |
r.in.parent_handle = hklm_handle; |
| 365 |
init_winreg_String(&r.in.keyname, keyname); |
| 366 |
r.in.options = REG_OPTION_NON_VOLATILE; |
| 367 |
r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; |
| 368 |
r.out.handle = _key_handle; |
| 369 |
|
| 370 |
status = dcerpc_winreg_OpenKey_r(winreg_bh, tctx, &r); |
| 371 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "OpenKey failed"); |
| 372 |
|
| 373 |
torture_assert_werr_ok(tctx, r.out.result, "OpenKey failed"); |
| 374 |
done: |
| 375 |
|
| 376 |
return ok; |
| 377 |
} |
| 378 |
|
| 379 |
static bool test_winreg_iremotewinspool_queryvalue(struct torture_context *tctx, |
| 380 |
struct dcerpc_binding_handle *b, |
| 381 |
struct policy_handle *key_handle, |
| 382 |
const char *value_name, |
| 383 |
const char **_valuestr) |
| 384 |
{ |
| 385 |
struct winreg_QueryValue r; |
| 386 |
enum winreg_Type type = REG_NONE; |
| 387 |
struct winreg_String valuename; |
| 388 |
DATA_BLOB blob; |
| 389 |
const char *str; |
| 390 |
uint32_t data_size = 0; |
| 391 |
uint32_t data_length = 0; |
| 392 |
uint8_t *data = NULL; |
| 393 |
NTSTATUS status; |
| 394 |
bool ok = true; |
| 395 |
|
| 396 |
init_winreg_String(&valuename, value_name); |
| 397 |
|
| 398 |
data = talloc_zero_array(tctx, uint8_t, 0); |
| 399 |
|
| 400 |
r.in.handle = key_handle; |
| 401 |
r.in.value_name = &valuename; |
| 402 |
r.in.type = &type; |
| 403 |
r.in.data_size = &data_size; |
| 404 |
r.in.data_length = &data_length; |
| 405 |
r.in.data = data; |
| 406 |
|
| 407 |
r.out.type = &type; |
| 408 |
r.out.data = data; |
| 409 |
r.out.data_size = &data_size; |
| 410 |
r.out.data_length = &data_length; |
| 411 |
|
| 412 |
status = dcerpc_winreg_QueryValue_r(b, tctx, &r); |
| 413 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "winreg_QueryValue failure"); |
| 414 |
|
| 415 |
torture_assert_ntstatus_ok(tctx, dcerpc_winreg_QueryValue_r(b, tctx, &r), "QueryValue failed"); |
| 416 |
if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) { |
| 417 |
*r.in.data_size = *r.out.data_size; |
| 418 |
data = talloc_zero_array(tctx, uint8_t, *r.in.data_size); |
| 419 |
r.in.data = data; |
| 420 |
r.out.data = data; |
| 421 |
status = dcerpc_winreg_QueryValue_r(b, tctx, &r); |
| 422 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "QueryValue failed"); |
| 423 |
} |
| 424 |
torture_assert_werr_ok(tctx, r.out.result, "QueryValue failed"); |
| 425 |
|
| 426 |
torture_assert_int_equal_goto(tctx, *r.out.type, REG_SZ, ok, done, "unexpected type"); |
| 427 |
blob = data_blob(r.out.data, *r.out.data_size); |
| 428 |
str = reg_val_data_string(tctx, REG_SZ, blob); |
| 429 |
|
| 430 |
*_valuestr = str; |
| 431 |
done: |
| 432 |
|
| 433 |
return ok; |
| 434 |
} |
| 435 |
|
| 436 |
/* Validate the installed driver subkey exists, and the InfPath |
| 437 |
* value matches the pszDestInfPath from test_UploadPrinterDriverPackage */ |
| 438 |
static bool test_winreg_validate_driver(struct torture_context *tctx, |
| 439 |
struct dcerpc_pipe *winreg_pipe, |
| 440 |
struct test_driver_info *dinfo) |
| 441 |
{ |
| 442 |
struct policy_handle hklm_handle; |
| 443 |
struct policy_handle key_handle; |
| 444 |
char *driver_key = NULL; |
| 445 |
const char *val_name = NULL; |
| 446 |
const char *val_str = NULL; |
| 447 |
bool ok = true; |
| 448 |
|
| 449 |
struct dcerpc_binding_handle *winreg_bh; |
| 450 |
struct spoolss_AddDriverInfo8 *parsed_dinfo; |
| 451 |
|
| 452 |
winreg_bh = winreg_pipe->binding_handle; |
| 453 |
parsed_dinfo = dinfo->info; |
| 454 |
|
| 455 |
/* OpenHKLM */ |
| 456 |
ok = test_winreg_iremotewinspool_openhklm(tctx, winreg_bh, &hklm_handle); |
| 457 |
torture_assert_goto(tctx, ok, ok, done, "Failed to perform winreg OpenHKLM"); |
| 458 |
|
| 459 |
/* Open registry subkey for the installed print driver */ |
| 460 |
driver_key = talloc_asprintf(tctx, "%s\\Environments\\%s\\Drivers\\Version-%d\\%s", |
| 461 |
REG_DRIVER_CONTROL_KEY, |
| 462 |
parsed_dinfo->architecture, |
| 463 |
parsed_dinfo->version, |
| 464 |
parsed_dinfo->driver_name); |
| 465 |
torture_assert_not_null_goto(tctx, driver_key, ok, done, "Cannot allocate driver_key string"); |
| 466 |
ok = test_winreg_iremotewinspool_openkey(tctx, winreg_bh, &hklm_handle, |
| 467 |
driver_key, |
| 468 |
&key_handle); |
| 469 |
torture_assert_goto(tctx, ok, ok, done, "Failed to perform winreg OpenKey"); |
| 470 |
|
| 471 |
/* Read infpath value and validate this matches what was uploaded */ |
| 472 |
val_name = "InfPath"; |
| 473 |
ok = test_winreg_iremotewinspool_queryvalue(tctx, winreg_bh, &key_handle, val_name, |
| 474 |
&val_str); |
| 475 |
torture_assert_goto(tctx, ok, ok, done, "QueryValue failed"); |
| 476 |
|
| 477 |
torture_assert_casestr_equal(tctx, val_str, |
| 478 |
dinfo->uploaded_inf_path, |
| 479 |
"InfPath does not match uploaded inf"); |
| 480 |
done: |
| 481 |
|
| 482 |
return ok; |
| 483 |
} |
| 484 |
|
| 485 |
static bool test_init_iremotewinspool_conn(struct torture_context *tctx, |
| 486 |
struct test_iremotewinspool_context *t) |
| 487 |
{ |
| 488 |
struct dcerpc_binding *binding = {0}; |
| 489 |
bool ok = true; |
| 490 |
NTSTATUS status; |
| 491 |
|
| 492 |
status = GUID_from_string(IREMOTEWINSPOOL_OBJECT_GUID, &t->object_uuid); |
| 493 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "failed to parse GUID"); |
| 494 |
|
| 495 |
status = torture_rpc_binding(tctx, &binding); |
| 496 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "failed to retrieve torture binding"); |
| 497 |
|
| 498 |
status = dcerpc_binding_set_object(binding, t->object_uuid); |
| 499 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "failed to set object_uuid"); |
| 500 |
|
| 501 |
status = torture_rpc_connection_with_binding(tctx, binding, &t->iremotewinspool_pipe, |
| 502 |
&ndr_table_iremotewinspool); |
| 503 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "Error connecting to server"); |
| 504 |
|
| 505 |
done: |
| 506 |
|
| 507 |
return ok; |
| 508 |
|
| 509 |
} |
| 510 |
|
| 511 |
static bool test_init_iremotewinspool_openprinter(struct torture_context *tctx, |
| 512 |
struct test_iremotewinspool_context *t) |
| 513 |
{ |
| 514 |
struct spoolss_UserLevel1 client_info = {0}; |
| 515 |
char *printer_name = NULL; |
| 516 |
bool ok = true; |
| 517 |
|
| 518 |
printer_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(t->iremotewinspool_pipe)); |
| 519 |
torture_assert_not_null_goto(tctx, printer_name, ok, done, "Cannot allocate memory"); |
| 520 |
|
| 521 |
client_info = test_get_client_info(tctx, WIN_7, 3, SPOOLSS_MINOR_VERSION_0, |
| 522 |
"testclient_machine", "testclient_user"); |
| 523 |
|
| 524 |
ok = test_AsyncOpenPrinter_byprinter(tctx, t, t->iremotewinspool_pipe, printer_name, |
| 525 |
client_info, &t->server_handle); |
| 526 |
torture_assert_goto(tctx, ok, ok, done, "failed to open printserver"); |
| 527 |
|
| 528 |
ok = test_get_environment(tctx, t->iremotewinspool_pipe->binding_handle, |
| 529 |
&t->server_handle, &t->environment); |
| 530 |
torture_assert_goto(tctx, ok, ok, done, "failed to get environment"); |
| 531 |
|
| 532 |
done: |
| 533 |
TALLOC_FREE(printer_name); |
| 534 |
|
| 535 |
return ok; |
| 536 |
} |
| 537 |
|
| 538 |
static bool test_init_driver_info(struct torture_context *tctx, |
| 539 |
struct test_iremotewinspool_context *t) |
| 540 |
{ |
| 541 |
bool ok = true; |
| 542 |
const char *abs_inf_path; |
| 543 |
struct test_driver_info *drv_info = {0}; |
| 544 |
|
| 545 |
drv_info = talloc_zero(tctx, struct test_driver_info); |
| 546 |
torture_assert_not_null_goto(tctx, drv_info, ok, done, "Cannot allocate memory"); |
| 547 |
|
| 548 |
t->dinfo = drv_info; |
| 549 |
|
| 550 |
ok = test_get_driver_torture_options(tctx, |
| 551 |
&drv_info->local_driver_path, |
| 552 |
&drv_info->inf_file, |
| 553 |
&drv_info->driver_name, |
| 554 |
&drv_info->driver_arch, |
| 555 |
&drv_info->core_driver_inf); |
| 556 |
torture_assert_goto(tctx, ok, ok, done, "Failed to get driver torture options"); |
| 557 |
|
| 558 |
ok = test_get_misc_driver_info(tctx, drv_info, |
| 559 |
&abs_inf_path, |
| 560 |
&drv_info->driver_path_len); |
| 561 |
torture_assert_goto(tctx, ok, ok, done, "Failed to get misc driver info"); |
| 562 |
|
| 563 |
ok = parse_inf_driver(tctx, drv_info->driver_name, abs_inf_path, drv_info->driver_arch, |
| 564 |
drv_info->core_driver_inf, &drv_info->info); |
| 565 |
torture_assert_goto(tctx, ok, ok, done, "Failed to parse inf driver"); |
| 566 |
|
| 567 |
/* Ensure that we are trying to install the correct device class: |
| 568 |
* https://docs.microsoft.com/en-us/windows-hardware/drivers/install/system-defined-device-setup-classes-available-to-vendors |
| 569 |
*/ |
| 570 |
if (!(drv_info->info->printer_driver_attributes & PRINTER_DRIVER_CLASS)) { |
| 571 |
ok = false; |
| 572 |
torture_fail_goto(tctx, done, "Inf file Class value must be Printer"); |
| 573 |
} |
| 574 |
done: |
| 575 |
return ok; |
| 576 |
|
| 577 |
} |
| 578 |
|
| 579 |
static bool test_init_server_and_share_info(struct torture_context *tctx, |
| 580 |
struct test_iremotewinspool_context *t) |
| 581 |
{ |
| 582 |
struct GUID guid; |
| 583 |
bool ok = true; |
| 584 |
|
| 585 |
t->dinfo->server_name = talloc_asprintf(tctx, "%s", dcerpc_server_name(t->iremotewinspool_pipe)); |
| 586 |
torture_assert_not_null_goto(tctx, t->dinfo->server_name, ok, done, "Cannot allocate memory"); |
| 587 |
|
| 588 |
t->dinfo->share_name = talloc_strdup(tctx, "print$"); |
| 589 |
torture_assert_not_null_goto(tctx, t->dinfo->share_name, ok, done, "Cannot allocate memory"); |
| 590 |
|
| 591 |
guid = GUID_random(); |
| 592 |
t->dinfo->print_upload_guid_dir = GUID_string2(tctx, &guid); |
| 593 |
done: |
| 594 |
return ok; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
static bool torture_rpc_iremotewinspool_drv_setup_common(struct torture_context *tctx, |
| 599 |
struct test_iremotewinspool_context *t) |
| 600 |
{ |
| 601 |
bool ok = true; |
| 602 |
int ret = 0; |
| 603 |
|
| 604 |
ok = test_init_driver_info(tctx, t); |
| 605 |
torture_assert_goto(tctx, ok, ok, done, "failed to init driver info"); |
| 606 |
|
| 607 |
ok = test_init_iremotewinspool_conn(tctx, t); |
| 608 |
torture_assert_goto(tctx, ok, ok, done, "failed to init iremotewinspool conn"); |
| 609 |
|
| 610 |
ok = test_init_iremotewinspool_openprinter(tctx, t); |
| 611 |
torture_assert_goto(tctx, ok, ok, done, "failed to init iremotewinspool openprinter"); |
| 612 |
|
| 613 |
ok = test_init_server_and_share_info(tctx, t); |
| 614 |
torture_assert_goto(tctx, ok, ok, done, "failed to init server and share info"); |
| 615 |
|
| 616 |
ret = smb_connect_print_share(tctx, t->dinfo->server_name, t->dinfo->share_name, &t->dinfo->cli); |
| 617 |
torture_assert_goto(tctx, ret, ok, done, "Failed to connect to print share"); |
| 618 |
|
| 619 |
done: |
| 620 |
|
| 621 |
return ok; |
| 622 |
} |
| 623 |
|
| 624 |
static bool torture_rpc_iremotewinspool_drv_setup(struct torture_context *tctx, |
| 625 |
void **data) |
| 626 |
{ |
| 627 |
struct test_iremotewinspool_context *t; |
| 628 |
|
| 629 |
*data = t = talloc_zero(tctx, struct test_iremotewinspool_context); |
| 630 |
|
| 631 |
return torture_rpc_iremotewinspool_drv_setup_common(tctx, t); |
| 632 |
} |
| 633 |
|
| 634 |
static bool torture_rpc_iremotewinspool_drv_teardown_common(struct torture_context *tctx, |
| 635 |
struct test_iremotewinspool_context *t) |
| 636 |
{ |
| 637 |
smbcli_deltree(t->dinfo->cli->tree, t->dinfo->print_upload_guid_dir); |
| 638 |
smb_raw_exit(t->dinfo->cli->session); |
| 639 |
|
| 640 |
test_uninstall_printer_driver(tctx, t); |
| 641 |
test_remove_driver_package(tctx, t); |
| 642 |
|
| 643 |
test_AsyncClosePrinter_byhandle(tctx, t, t->iremotewinspool_pipe, &t->server_handle); |
| 644 |
|
| 645 |
return true; |
| 646 |
} |
| 647 |
|
| 648 |
static bool torture_rpc_iremotewinspool_drv_teardown(struct torture_context *tctx, |
| 649 |
void *data) |
| 650 |
{ |
| 651 |
struct test_iremotewinspool_context *t = talloc_get_type(data, struct test_iremotewinspool_context); |
| 652 |
bool ret; |
| 653 |
|
| 654 |
ret = torture_rpc_iremotewinspool_drv_teardown_common(tctx, t); |
| 655 |
talloc_free(t); |
| 656 |
|
| 657 |
return ret; |
| 658 |
} |
| 659 |
|
| 660 |
/* Creates {GUID} directory inside //server/print$ then copies driver files |
| 661 |
* and directories from torture option driver_path to this directory over smb */ |
| 662 |
static bool test_CopyDriverFiles(struct torture_context *tctx, |
| 663 |
void *private_data) |
| 664 |
{ |
| 665 |
struct test_iremotewinspool_context *ctx = |
| 666 |
talloc_get_type_abort(private_data, struct test_iremotewinspool_context); |
| 667 |
|
| 668 |
bool ret = false; |
| 669 |
bool ok = true; |
| 670 |
NTSTATUS status; |
| 671 |
|
| 672 |
status = smbcli_mkdir(ctx->dinfo->cli->tree, ctx->dinfo->print_upload_guid_dir); |
| 673 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "Failed to create upload directory"); |
| 674 |
|
| 675 |
/* Walk the provided torture option driver_path file tree, creating the directory hierarchy and |
| 676 |
* copying all files to print$/{GUID}/ share */ |
| 677 |
ret = tftw(tctx, ctx->dinfo->local_driver_path, copy_driver_files, TFTW_MAX_DEPTH, ctx->dinfo); |
| 678 |
torture_assert_int_equal_goto(tctx, ret, 0, ok, done, "Failed to copy driver files to print$/{GUID}/ dir"); |
| 679 |
|
| 680 |
done: |
| 681 |
|
| 682 |
return ok; |
| 683 |
} |
| 684 |
|
| 685 |
/* |
| 686 |
* Upload print driver package files and inf file, preparing the print server |
| 687 |
* for driver installation |
| 688 |
*/ |
| 689 |
static bool test_UploadPrinterDriverPackage(struct torture_context *tctx, |
| 690 |
void *private_data) |
| 691 |
{ |
| 692 |
struct test_iremotewinspool_context *ctx = |
| 693 |
talloc_get_type_abort(private_data, struct test_iremotewinspool_context); |
| 694 |
|
| 695 |
struct dcerpc_pipe *p = ctx->iremotewinspool_pipe; |
| 696 |
struct dcerpc_binding_handle *b = p->binding_handle; |
| 697 |
|
| 698 |
struct spoolss_AddDriverInfo8 *parsed_dinfo; |
| 699 |
struct winspool_AsyncUploadPrinterDriverPackage r; |
| 700 |
uint32_t pcchDestInfPath = 0; |
| 701 |
NTSTATUS status; |
| 702 |
bool ok = true; |
| 703 |
|
| 704 |
parsed_dinfo = ctx->dinfo->info; |
| 705 |
|
| 706 |
r.in.pszServer = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p)); |
| 707 |
torture_assert_not_null_goto(tctx, r.in.pszServer, ok, done, "Cannot allocate memory"); |
| 708 |
|
| 709 |
r.in.pszInfPath = talloc_asprintf(tctx, "\\\\%s\\%s\\%s\\%s", ctx->dinfo->server_name, |
| 710 |
ctx->dinfo->share_name, |
| 711 |
ctx->dinfo->print_upload_guid_dir, |
| 712 |
ctx->dinfo->inf_file); |
| 713 |
torture_assert_not_null_goto(tctx, r.in.pszInfPath, ok, done, "Cannot allocate memory"); |
| 714 |
|
| 715 |
r.in.pszEnvironment = parsed_dinfo->architecture; |
| 716 |
/* Upload driver package files even if the driver package is already present |
| 717 |
* on the print server */ |
| 718 |
r.in.dwFlags = UPDP_UPLOAD_ALWAYS; |
| 719 |
pcchDestInfPath = 260; |
| 720 |
r.in.pszDestInfPath = NULL; |
| 721 |
r.in.pcchDestInfPath = &pcchDestInfPath; |
| 722 |
r.out.pszDestInfPath = NULL; |
| 723 |
r.out.pcchDestInfPath = &pcchDestInfPath; |
| 724 |
|
| 725 |
r.in.pszDestInfPath = talloc_zero(tctx, const char); |
| 726 |
torture_assert_not_null_goto(tctx, r.in.pszDestInfPath, ok, done, "Cannot allocate memory"); |
| 727 |
r.out.pszDestInfPath = talloc_zero(tctx, const char); |
| 728 |
torture_assert_not_null_goto(tctx, r.out.pszDestInfPath, ok, done, "Cannot allocate memory"); |
| 729 |
|
| 730 |
status = dcerpc_winspool_AsyncUploadPrinterDriverPackage_r(b, tctx, &r); |
| 731 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "AsyncUploadPrinterDriverPackage failed"); |
| 732 |
|
| 733 |
torture_assert_hresult_ok(tctx, r.out.result, "AsyncUploadPrinterDriverPackage failed"); |
| 734 |
|
| 735 |
ctx->dinfo->uploaded_inf_path = talloc_strdup(tctx, r.out.pszDestInfPath); |
| 736 |
torture_assert_not_null_goto(tctx, ctx->dinfo->uploaded_inf_path, ok, done, "Cannot allocate memory"); |
| 737 |
|
| 738 |
done: |
| 739 |
|
| 740 |
return ok; |
| 741 |
} |
| 742 |
|
| 743 |
/* Install the driver that was successfully uploaded to the printer driver |
| 744 |
* store, note that Windows validates the pszDriverName as mentioned below */ |
| 745 |
static bool test_InstallPrinterDriverFromPackage(struct torture_context *tctx, |
| 746 |
void *private_data) |
| 747 |
{ |
| 748 |
struct test_iremotewinspool_context *ctx = |
| 749 |
talloc_get_type_abort(private_data, struct test_iremotewinspool_context); |
| 750 |
|
| 751 |
struct dcerpc_pipe *p = ctx->iremotewinspool_pipe; |
| 752 |
struct dcerpc_binding_handle *b = p->binding_handle; |
| 753 |
|
| 754 |
char *abs_inf_path = NULL; |
| 755 |
struct spoolss_AddDriverInfo8 *parsed_dinfo; |
| 756 |
struct winspool_AsyncInstallPrinterDriverFromPackage r; |
| 757 |
bool ok = true; |
| 758 |
NTSTATUS status; |
| 759 |
|
| 760 |
parsed_dinfo = ctx->dinfo->info; |
| 761 |
|
| 762 |
r.in.pszServer = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p)); |
| 763 |
torture_assert_not_null_goto(tctx, r.in.pszServer, ok, done, "Cannot allocate memory"); |
| 764 |
|
| 765 |
/* output string(pszDestInfPath) from test_UploadPrinterDriverPackage() */ |
| 766 |
r.in.pszInfPath = talloc_strdup(tctx, ctx->dinfo->uploaded_inf_path); |
| 767 |
torture_assert_not_null_goto(tctx, r.in.pszInfPath, ok, done, "Cannot allocate memory"); |
| 768 |
|
| 769 |
abs_inf_path = talloc_asprintf(tctx, "%s/%s", ctx->dinfo->local_driver_path, ctx->dinfo->inf_file); |
| 770 |
torture_assert_not_null_goto(tctx, abs_inf_path, ok, done, "Cannot allocate memory"); |
| 771 |
|
| 772 |
r.in.pszEnvironment = parsed_dinfo->architecture; |
| 773 |
torture_assert_not_null_goto(tctx, r.in.pszEnvironment, ok, done, "Cannot allocate memory"); |
| 774 |
|
| 775 |
/* Windows validates the print driver name by checking the pszDriverName input against the inf file: |
| 776 |
* 1) "DriverName" value |
| 777 |
* 2) "CompatName" value |
| 778 |
* 3) left-hand-side value under the [Model] section |
| 779 |
* otherwise ERROR_UNKNOWN_PRINTER_DRIVER is returned */ |
| 780 |
r.in.pszDriverName = parsed_dinfo->driver_name; |
| 781 |
torture_assert_not_null_goto(tctx, r.in.pszDriverName, ok, done, "Cannot allocate memory"); |
| 782 |
|
| 783 |
/* All files should be installed, even if doing so would overwrite some newer |
| 784 |
* versions */ |
| 785 |
r.in.dwFlags = IPDFP_COPY_ALL_FILES; |
| 786 |
|
| 787 |
status = dcerpc_winspool_AsyncInstallPrinterDriverFromPackage_r(b, tctx, &r); |
| 788 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "AsyncInstallPrinterDriverFromPackage failed"); |
| 789 |
|
| 790 |
torture_assert_hresult_ok(tctx, r.out.result, "AsyncInstallPrinterDriverFromPackage failed"); |
| 791 |
done: |
| 792 |
TALLOC_FREE(abs_inf_path); |
| 793 |
|
| 794 |
return ok; |
| 795 |
} |
| 796 |
|
| 797 |
/* Check the registry to validate the print driver installed successfully */ |
| 798 |
static bool test_ValidatePrinterDriverInstalled(struct torture_context *tctx, |
| 799 |
void *private_data) |
| 800 |
{ |
| 801 |
struct test_iremotewinspool_context *ctx = |
| 802 |
talloc_get_type_abort(private_data, struct test_iremotewinspool_context); |
| 803 |
|
| 804 |
struct dcerpc_pipe *winreg_pipe = NULL; |
| 805 |
NTSTATUS status; |
| 806 |
bool ok = true; |
| 807 |
|
| 808 |
/* winreg is not available over ncacn_ip_tcp */ |
| 809 |
status = torture_rpc_connection_transport(tctx, &winreg_pipe, &ndr_table_winreg, NCACN_NP, 0, 0); |
| 810 |
if (NT_STATUS_EQUAL(status, NT_STATUS_PIPE_NOT_AVAILABLE)) { |
| 811 |
/* retry */ |
| 812 |
status = torture_rpc_connection_transport(tctx, &winreg_pipe, &ndr_table_winreg, NCACN_NP, 0, 0); |
| 813 |
} |
| 814 |
torture_assert_ntstatus_ok_goto(tctx, status, ok, done, "Failed to connect to winreg"); |
| 815 |
|
| 816 |
ok = test_winreg_validate_driver(tctx, winreg_pipe, ctx->dinfo); |
| 817 |
torture_assert_goto(tctx, ok, ok, done, "Failed to validate driver with winreg"); |
| 818 |
|
| 819 |
done: |
| 820 |
TALLOC_FREE(winreg_pipe); |
| 821 |
|
| 822 |
return ok; |
| 823 |
} |
| 824 |
|
| 825 |
struct torture_suite *torture_rpc_iremotewinspool_drv(TALLOC_CTX *mem_ctx) |
| 826 |
{ |
| 827 |
struct torture_suite *suite = torture_suite_create(mem_ctx, "iremotewinspool_driver"); |
| 828 |
struct torture_tcase *tcase = torture_suite_add_tcase(suite, "drivers"); |
| 829 |
|
| 830 |
torture_tcase_set_fixture(tcase, |
| 831 |
torture_rpc_iremotewinspool_drv_setup, |
| 832 |
torture_rpc_iremotewinspool_drv_teardown); |
| 833 |
|
| 834 |
torture_tcase_add_simple_test(tcase, "CopyDriverFiles", test_CopyDriverFiles); |
| 835 |
torture_tcase_add_simple_test(tcase, "UploadPrinterDriverPackage", test_UploadPrinterDriverPackage); |
| 836 |
torture_tcase_add_simple_test(tcase, "InstallPrinterDriverFromPackage", test_InstallPrinterDriverFromPackage); |
| 837 |
torture_tcase_add_simple_test(tcase, "ValidatePrinterDriverInstalled", test_ValidatePrinterDriverInstalled); |
| 838 |
|
| 839 |
return suite; |
| 840 |
} |