summaryrefslogtreecommitdiff
path: root/source3/rpc_server/srv_spoolss_util.c
blob: 4837c1c3db7808b4d16f4bf97f3a75b535fb42ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
 *  Unix SMB/CIFS implementation.
 *
 *  SPOOLSS RPC Pipe server / winreg client routines
 *
 *  Copyright (c) 2010      Andreas Schneider <asn@samba.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "srv_spoolss_util.h"
#include "../librpc/gen_ndr/srv_winreg.h"
#include "../librpc/gen_ndr/cli_winreg.h"

/********************************************************************
 static helper functions
********************************************************************/

/**
 * @internal
 *
 * @brief Connect to the interal winreg server and open the given printer key.
 *
 * The function will create the needed subkeys if they don't exist.
 *
 * @param[in]  mem_ctx       The memory context to use.
 *
 * @param[in]  server_info   The supplied server info.
 *
 * @param[out] winreg_pipe   A pointer for the winreg rpc client pipe.
 *
 * @param[in]  name          The name of the printer.
 *
 * @param[in]  key           The key to open.
 *
 * @param[in]  create_key    Set to true if the key should be created if it
 *                           doesn't exist.
 *
 * @param[in]  access_mask   The access mask to open the key.
 *
 * @param[out] hive_handle   A policy handle for the opened hive.
 *
 * @param[out] key_handle    A policy handle for the opened key.
 *
 * @return                   WERR_OK on success, the corresponding DOS error
 *                           code if something gone wrong.
 */
static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
			      struct auth_serversupplied_info *server_info,
			      struct rpc_pipe_client **winreg_pipe,
			      const char *name,
			      const char *key,
			      bool create_key,
			      uint32_t access_mask,
			      struct policy_handle *hive_handle,
			      struct policy_handle *key_handle)
{
	struct rpc_pipe_client *pipe_handle;
	struct winreg_String wkey, wkeyclass;
	char *keyname;
	NTSTATUS status;
	WERROR result = WERR_OK;

	/* create winreg connection */
	status = rpc_pipe_open_internal(mem_ctx,
					&ndr_table_winreg.syntax_id,
					rpc_winreg_dispatch,
					server_info,
					&pipe_handle);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("winreg_printer_openkey: Could not connect to winreg_pipe: %s\n",
			  nt_errstr(status)));
		return ntstatus_to_werror(status);
	}

	status = rpccli_winreg_OpenHKLM(pipe_handle,
					mem_ctx,
					NULL,
					access_mask,
					hive_handle,
					&result);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
			  nt_errstr(status)));
		talloc_free(pipe_handle);
		if (!W_ERROR_IS_OK(result)) {
			return result;
		}
		return ntstatus_to_werror(status);
	}

	if (key && *key) {
		keyname = talloc_asprintf(mem_ctx,
				    "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\%s\\%s",
				    name,
				    key);
	} else {
		keyname = talloc_asprintf(mem_ctx,
				    "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\%s",
				    name);
	}
	if (keyname == NULL) {
		talloc_free(pipe_handle);
		return WERR_NOMEM;
	}

	ZERO_STRUCT(wkey);
	wkey.name = keyname;

	if (create_key) {
		enum winreg_CreateAction action = REG_ACTION_NONE;

		ZERO_STRUCT(wkeyclass);
		wkeyclass.name = "";

		status = rpccli_winreg_CreateKey(pipe_handle,
						 mem_ctx,
						 hive_handle,
						 wkey,
						 wkeyclass,
						 0,
						 access_mask,
						 NULL,
						 key_handle,
						 &action,
						 &result);
		switch (action) {
			case REG_ACTION_NONE:
				DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
				break;
			case REG_CREATED_NEW_KEY:
				DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
				break;
			case REG_OPENED_EXISTING_KEY:
				DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
				break;
		}
	} else {
		status = rpccli_winreg_OpenKey(pipe_handle,
					       mem_ctx,
					       hive_handle,
					       wkey,
					       0,
					       access_mask,
					       key_handle,
					       &result);
	}
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(pipe_handle);
		if (!W_ERROR_IS_OK(result)) {
			return result;
		}
		return ntstatus_to_werror(status);
	}

	*winreg_pipe = pipe_handle;

	return WERR_OK;
}

/**
 * @internal
 *
 * @brief Enumerate values of an opened key handle and retrieve the data.
 *
 * @param[in]  mem_ctx  The memory context to use.
 *
 * @param[in]  pipe_handle The pipe handle for the rpc connection.
 *
 * @param[in]  key_hnd  The opened key handle.
 *
 * @param[out] pnum_values A pointer to store he number of values found.
 *
 * @param[out] pnum_values A pointer to store the number of values we found.
 *
 * @return                   WERR_OK on success, the corresponding DOS error
 *                           code if something gone wrong.
 */
static WERROR winreg_printer_enumvalues(TALLOC_CTX *mem_ctx,
					struct rpc_pipe_client *pipe_handle,
					struct policy_handle *key_hnd,
					uint32_t *pnum_values,
					struct spoolss_PrinterEnumValues **penum_values)
{
	TALLOC_CTX *tmp_ctx;
	uint32_t num_subkeys, max_subkeylen, max_classlen;
	uint32_t num_values, max_valnamelen, max_valbufsize;
	uint32_t secdescsize;
	uint32_t i;
	NTTIME last_changed_time;
	struct winreg_String classname;

	struct spoolss_PrinterEnumValues *enum_values;

	WERROR result = WERR_OK;
	NTSTATUS status;

	tmp_ctx = talloc_new(mem_ctx);
	if (tmp_ctx == NULL) {
		return WERR_NOMEM;
	}

	ZERO_STRUCT(classname);

	status = rpccli_winreg_QueryInfoKey(pipe_handle,
					    tmp_ctx,
					    key_hnd,
					    &classname,
					    &num_subkeys,
					    &max_subkeylen,
					    &max_classlen,
					    &num_values,
					    &max_valnamelen,
					    &max_valbufsize,
					    &secdescsize,
					    &last_changed_time,
					    &result);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("winreg_printer_enumvalues: Could not query info: %s\n",
			  nt_errstr(status)));
		if (!W_ERROR_IS_OK(result)) {
			goto error;
		}
		result = ntstatus_to_werror(status);
		goto error;
	}

	if (num_values == 0) {
		*pnum_values = 0;
		TALLOC_FREE(tmp_ctx);
		return WERR_OK;
	}

	enum_values = TALLOC_ARRAY(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
	if (enum_values == NULL) {
		result = WERR_NOMEM;
		goto error;
	}

	for (i = 0; i < num_values; i++) {
		struct spoolss_PrinterEnumValues val;
		struct winreg_ValNameBuf name_buf;
		enum winreg_Type type = REG_NONE;
		uint8_t *data = NULL;
		uint32_t data_size;
		uint32_t length;
		char n = '\0';;

		name_buf.name = &n;
		name_buf.size = max_valnamelen + 2;
		name_buf.length = 0;

		data_size = max_valbufsize;
		data = (uint8_t *) TALLOC(tmp_ctx, data_size);
		length = 0;

		status = rpccli_winreg_EnumValue(pipe_handle,
						 tmp_ctx,
						 key_hnd,
						 i,
						 &name_buf,
						 &type,
						 data,
						 &data_size,
						 &length,
						 &result);
		if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
			result = WERR_OK;
			status = NT_STATUS_OK;
			break;
		}

		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0, ("winreg_printer_enumvalues: Could not enumerate values: %s\n",
				  nt_errstr(status)));
			if (!W_ERROR_IS_OK(result)) {
				goto error;
			}
			result = ntstatus_to_werror(status);
			goto error;
		}

		if (name_buf.name == NULL) {
			result = WERR_INVALID_PARAMETER;
			goto error;
		}

		val.value_name = talloc_strdup(enum_values, name_buf.name);
		if (val.value_name == NULL) {
			result = WERR_NOMEM;
			goto error;
		}
		val.value_name_len = strlen_m_term(val.value_name) * 2;

		val.type = type;
		val.data_length = data_size;
		if (val.data_length) {
			val.data = talloc(enum_values, DATA_BLOB);
			if (val.data == NULL) {
				result = WERR_NOMEM;
				goto error;
			}
			*val.data = data_blob_talloc(enum_values, data, data_size);
		}

		enum_values[i] = val;
	}

	*pnum_values = num_values;
	if (penum_values) {
		*penum_values = talloc_move(mem_ctx, &enum_values);
	}

	result = WERR_OK;

 error:
	TALLOC_FREE(tmp_ctx);
	return result;
}

/********************************************************************
 Public winreg function for spoolss
********************************************************************/

/* Set printer data over the winreg pipe. */
WERROR winreg_set_printer_dataex(struct pipes_struct *p,
				 const char *printer,
				 const char *key,
				 const char *value,
				 enum winreg_Type type,
				 uint8_t *data,
				 uint32_t data_size)
{
	uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	struct rpc_pipe_client *winreg_pipe = NULL;
	struct policy_handle hive_hnd, key_hnd;
	struct winreg_String wvalue;
	WERROR result = WERR_OK;
	NTSTATUS status;
	TALLOC_CTX *tmp_ctx;

	tmp_ctx = talloc_new(p->mem_ctx);
	if (tmp_ctx == NULL) {
		return WERR_NOMEM;
	}

	ZERO_STRUCT(hive_hnd);
	ZERO_STRUCT(key_hnd);

	DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
			key, value, access_mask, printer));
	result = winreg_printer_openkey(tmp_ctx,
					p->server_info,
					&winreg_pipe,
					printer,
					key,
					true,
					access_mask,
					&hive_hnd,
					&key_hnd);
	if (!W_ERROR_IS_OK(result)) {
		DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
			  key, win_errstr(result)));
		goto done;
	}

	wvalue.name = value;
	status = rpccli_winreg_SetValue(winreg_pipe,
					tmp_ctx,
					&key_hnd,
					wvalue,
					type,
					data,
					data_size,
					&result);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
			  value, nt_errstr(status)));
		if (!W_ERROR_IS_OK(result)) {
			goto done;
		}
		result = ntstatus_to_werror(status);
		goto done;
	}

	result = WERR_OK;
done:
	if (winreg_pipe != NULL) {
		if (is_valid_policy_hnd(&key_hnd)) {
			rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
		}
		if (is_valid_policy_hnd(&hive_hnd)) {
			rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
		}
	}

	TALLOC_FREE(tmp_ctx);
	return result;
}

/* Get printer data over a winreg pipe. */
WERROR winreg_get_printer_dataex(struct pipes_struct *p,
				 const char *printer,
				 const char *key,
				 const char *value,
				 enum winreg_Type *type,
				 uint8_t **data,
				 uint32_t *data_size)
{
	uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	struct rpc_pipe_client *winreg_pipe = NULL;
	struct policy_handle hive_hnd, key_hnd;
	struct winreg_String wvalue;
	enum winreg_Type type_in;
	uint8_t *data_in;
	uint32_t data_in_size = 0;
	uint32_t value_len = 0;
	WERROR result = WERR_OK;
	NTSTATUS status;
	TALLOC_CTX *tmp_ctx;

	tmp_ctx = talloc_new(p->mem_ctx);
	if (tmp_ctx == NULL) {
		return WERR_NOMEM;
	}

	ZERO_STRUCT(hive_hnd);
	ZERO_STRUCT(key_hnd);

	result = winreg_printer_openkey(tmp_ctx,
					p->server_info,
					&winreg_pipe,
					printer,
					key,
					false,
					access_mask,
					&hive_hnd,
					&key_hnd);
	if (!W_ERROR_IS_OK(result)) {
		DEBUG(0, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
			  key, win_errstr(result)));
		goto done;
	}

	wvalue.name = value;

	/*
	 * call QueryValue once with data == NULL to get the
	 * needed memory size to be allocated, then allocate
	 * data buffer and call again.
	 */
	status = rpccli_winreg_QueryValue(winreg_pipe,
					  tmp_ctx,
					  &key_hnd,
					  &wvalue,
					  &type_in,
					  NULL,
					  &data_in_size,
					  &value_len,
					  &result);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
			  value, nt_errstr(status)));
		if (!W_ERROR_IS_OK(result)) {
			goto done;
		}
		result = ntstatus_to_werror(status);
		goto done;
	}

	data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
	if (data_in == NULL) {
		result = WERR_NOMEM;
		goto done;
	}
	value_len = 0;

	status = rpccli_winreg_QueryValue(winreg_pipe,
					  tmp_ctx,
					  &key_hnd,
					  &wvalue,
					  &type_in,
					  data_in,
					  &data_in_size,
					  &value_len,
					  &result);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
			  value, nt_errstr(status)));
		if (!W_ERROR_IS_OK(result)) {
			result = ntstatus_to_werror(status);
		}
		goto done;
	}

	*type = type_in;
	*data_size = data_in_size;
	if (data_in_size) {
		*data = talloc_move(p->mem_ctx, &data_in);
	}

	result = WERR_OK;
done:
	if (winreg_pipe != NULL) {
		if (is_valid_policy_hnd(&key_hnd)) {
			rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
		}
		if (is_valid_policy_hnd(&hive_hnd)) {
			rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
		}
	}

	TALLOC_FREE(tmp_ctx);
	return result;
}

/* Enumerate on the values of a given key and provide the data. */
WERROR winreg_enum_printer_dataex(struct pipes_struct *p,
				  const char *printer,
				  const char *key,
				  uint32_t *pnum_values,
				  struct spoolss_PrinterEnumValues **penum_values)
{
	uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	struct rpc_pipe_client *winreg_pipe = NULL;
	struct policy_handle hive_hnd, key_hnd;

	struct spoolss_PrinterEnumValues *enum_values = NULL;
	uint32_t num_values = 0;

	WERROR result = WERR_OK;

	TALLOC_CTX *tmp_ctx;

	tmp_ctx = talloc_new(p->mem_ctx);
	if (tmp_ctx == NULL) {
		return WERR_NOMEM;
	}

	result = winreg_printer_openkey(tmp_ctx,
					p->server_info,
					&winreg_pipe,
					printer,
					key,
					false,
					access_mask,
					&hive_hnd,
					&key_hnd);
	if (!W_ERROR_IS_OK(result)) {
		DEBUG(0, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
			  key, win_errstr(result)));
		goto done;
	}

	result = winreg_printer_enumvalues(tmp_ctx,
					   winreg_pipe,
					   &key_hnd,
					   &num_values,
					   &enum_values);
	if (!W_ERROR_IS_OK(result)) {
		DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
			  key, win_errstr(result)));
		goto done;
	}

	*pnum_values = num_values;
	if (penum_values) {
		*penum_values = talloc_move(p->mem_ctx, &enum_values);
	}

	result = WERR_OK;
done:
	if (winreg_pipe != NULL) {
		if (is_valid_policy_hnd(&key_hnd)) {
			rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
		}
		if (is_valid_policy_hnd(&hive_hnd)) {
			rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
		}
	}

	TALLOC_FREE(tmp_ctx);
	return result;
}

/* Delete printer data over a winreg pipe. */
WERROR winreg_delete_printer_dataex(struct pipes_struct *p,
				    const char *printer,
				    const char *key,
				    const char *value)
{
	uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	struct rpc_pipe_client *winreg_pipe = NULL;
	struct policy_handle hive_hnd, key_hnd;
	struct winreg_String wvalue;
	WERROR result = WERR_OK;
	NTSTATUS status;

	TALLOC_CTX *tmp_ctx;

	tmp_ctx = talloc_new(p->mem_ctx);
	if (tmp_ctx == NULL) {
		return WERR_NOMEM;
	}

	ZERO_STRUCT(hive_hnd);
	ZERO_STRUCT(key_hnd);

	result = winreg_printer_openkey(tmp_ctx,
					p->server_info,
					&winreg_pipe,
					printer,
					key,
					false,
					access_mask,
					&hive_hnd,
					&key_hnd);
	if (!W_ERROR_IS_OK(result)) {
		DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
			  key, win_errstr(result)));
		goto done;
	}

	wvalue.name = value;
	status = rpccli_winreg_DeleteValue(winreg_pipe,
					   tmp_ctx,
					   &key_hnd,
					   wvalue,
					   &result);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
			  value, nt_errstr(status)));
		if (!W_ERROR_IS_OK(result)) {
			goto done;
		}
		result = ntstatus_to_werror(status);
		goto done;
	}

	result = WERR_OK;
done:
	if (winreg_pipe != NULL) {
		if (is_valid_policy_hnd(&key_hnd)) {
			rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
		}
		if (is_valid_policy_hnd(&hive_hnd)) {
			rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
		}
	}

	TALLOC_FREE(tmp_ctx);
	return result;
}