r18984: Removing ads_gpo_get_sysvol_gpt_version() which was just doing stupid
[Samba/nascimento.git] / source3 / registry / reg_printing.c
blobba79092ea5b3fbf6a64fbce0e1450bdeaa0c3ce1
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* Implementation of registry virtual views for printing information */
23 #include "includes.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
28 /* registrt paths used in the print_registry[] */
30 #define KEY_MONITORS "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/MONITORS"
31 #define KEY_FORMS "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/FORMS"
32 #define KEY_CONTROL_PRINTERS "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/PRINTERS"
33 #define KEY_ENVIRONMENTS "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/ENVIRONMENTS"
34 #define KEY_CONTROL_PRINT "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT"
35 #define KEY_WINNT_PRINTERS "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT/PRINTERS"
36 #define KEY_WINNT_PRINT "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT"
37 #define KEY_PORTS "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PORTS"
39 /* callback table for various registry paths below the ones we service in this module */
41 struct reg_dyn_tree {
42 /* full key path in normalized form */
43 const char *path;
45 /* callbscks for fetch/store operations */
46 int ( *fetch_subkeys) ( const char *path, REGSUBKEY_CTR *subkeys );
47 BOOL (*store_subkeys) ( const char *path, REGSUBKEY_CTR *subkeys );
48 int (*fetch_values) ( const char *path, REGVAL_CTR *values );
49 BOOL (*store_values) ( const char *path, REGVAL_CTR *values );
52 /*********************************************************************
53 *********************************************************************
54 ** Utility Functions
55 *********************************************************************
56 *********************************************************************/
58 /***********************************************************************
59 simple function to prune a pathname down to the basename of a file
60 **********************************************************************/
62 static char* dos_basename ( char *path )
64 char *p;
66 if ( !(p = strrchr( path, '\\' )) )
67 p = path;
68 else
69 p++;
71 return p;
74 /*********************************************************************
75 *********************************************************************
76 ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/FORMS"
77 *********************************************************************
78 *********************************************************************/
80 static int key_forms_fetch_keys( const char *key, REGSUBKEY_CTR *subkeys )
82 char *p = reg_remaining_path( key + strlen(KEY_FORMS) );
84 /* no keys below Forms */
86 if ( p )
87 return -1;
89 return 0;
92 /**********************************************************************
93 *********************************************************************/
95 static int key_forms_fetch_values( const char *key, REGVAL_CTR *values )
97 uint32 data[8];
98 int i, num_values, form_index = 1;
99 nt_forms_struct *forms_list = NULL;
100 nt_forms_struct *form;
102 DEBUG(10,("print_values_forms: key=>[%s]\n", key ? key : "NULL" ));
104 num_values = get_ntforms( &forms_list );
106 DEBUG(10,("hive_forms_fetch_values: [%d] user defined forms returned\n",
107 num_values));
109 /* handle user defined forms */
111 for ( i=0; i<num_values; i++ ) {
112 form = &forms_list[i];
114 data[0] = form->width;
115 data[1] = form->length;
116 data[2] = form->left;
117 data[3] = form->top;
118 data[4] = form->right;
119 data[5] = form->bottom;
120 data[6] = form_index++;
121 data[7] = form->flag;
123 regval_ctr_addvalue( values, form->name, REG_BINARY, (char*)data, sizeof(data) );
126 SAFE_FREE( forms_list );
127 forms_list = NULL;
129 /* handle built-on forms */
131 num_values = get_builtin_ntforms( &forms_list );
133 DEBUG(10,("print_subpath_values_forms: [%d] built-in forms returned\n",
134 num_values));
136 for ( i=0; i<num_values; i++ ) {
137 form = &forms_list[i];
139 data[0] = form->width;
140 data[1] = form->length;
141 data[2] = form->left;
142 data[3] = form->top;
143 data[4] = form->right;
144 data[5] = form->bottom;
145 data[6] = form_index++;
146 data[7] = form->flag;
148 regval_ctr_addvalue( values, form->name, REG_BINARY, (char*)data, sizeof(data) );
151 SAFE_FREE( forms_list );
153 return regval_ctr_numvals( values );
156 /*********************************************************************
157 *********************************************************************
158 ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/PRINTERS"
159 ** "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT/PRINTERS"
160 *********************************************************************
161 *********************************************************************/
163 /*********************************************************************
164 strip off prefix for printers key. DOes return a pointer to static
165 memory.
166 *********************************************************************/
168 static char* strip_printers_prefix( const char *key )
170 char *subkeypath;
171 pstring path;
173 pstrcpy( path, key );
174 normalize_reg_path( path );
176 /* normalizing the path does not change length, just key delimiters and case */
178 if ( strncmp( path, KEY_WINNT_PRINTERS, strlen(KEY_WINNT_PRINTERS) ) == 0 )
179 subkeypath = reg_remaining_path( key + strlen(KEY_WINNT_PRINTERS) );
180 else
181 subkeypath = reg_remaining_path( key + strlen(KEY_CONTROL_PRINTERS) );
183 return subkeypath;
186 /*********************************************************************
187 *********************************************************************/
189 static int key_printers_fetch_keys( const char *key, REGSUBKEY_CTR *subkeys )
191 int n_services = lp_numservices();
192 int snum;
193 fstring sname;
194 int i;
195 int num_subkeys = 0;
196 char *printers_key;
197 char *printername, *printerdatakey;
198 NT_PRINTER_INFO_LEVEL *printer = NULL;
199 fstring *subkey_names = NULL;
201 DEBUG(10,("key_printers_fetch_keys: key=>[%s]\n", key ? key : "NULL" ));
203 printers_key = strip_printers_prefix( key );
205 if ( !printers_key ) {
206 /* enumerate all printers */
208 for (snum=0; snum<n_services; snum++) {
209 if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
210 continue;
212 /* don't report the [printers] share */
214 if ( strequal( lp_servicename(snum), PRINTERS_NAME ) )
215 continue;
217 fstrcpy( sname, lp_servicename(snum) );
219 regsubkey_ctr_addkey( subkeys, sname );
222 num_subkeys = regsubkey_ctr_numkeys( subkeys );
223 goto done;
226 /* get information for a specific printer */
228 if (!reg_split_path( printers_key, &printername, &printerdatakey )) {
229 return -1;
232 /* validate the printer name */
234 for (snum=0; snum<n_services; snum++) {
235 if ( !lp_snum_ok(snum) || !lp_print_ok(snum) )
236 continue;
237 if (strequal( lp_servicename(snum), printername ) )
238 break;
241 if ( snum>=n_services
242 || !W_ERROR_IS_OK( get_a_printer(NULL, &printer, 2, printername) ) )
244 return -1;
247 num_subkeys = get_printer_subkeys( printer->info_2->data, printerdatakey?printerdatakey:"", &subkey_names );
249 for ( i=0; i<num_subkeys; i++ )
250 regsubkey_ctr_addkey( subkeys, subkey_names[i] );
252 free_a_printer( &printer, 2 );
254 /* no other subkeys below here */
256 done:
257 SAFE_FREE( subkey_names );
259 return num_subkeys;
262 /**********************************************************************
263 Take a list of names and call add_printer_hook() if necessary
264 Note that we do this a little differently from Windows since the
265 keyname is the sharename and not the printer name.
266 *********************************************************************/
268 static BOOL add_printers_by_registry( REGSUBKEY_CTR *subkeys )
270 int i, num_keys, snum;
271 char *printername;
272 NT_PRINTER_INFO_LEVEL_2 info2;
273 NT_PRINTER_INFO_LEVEL printer;
275 ZERO_STRUCT( info2 );
276 printer.info_2 = &info2;
278 num_keys = regsubkey_ctr_numkeys( subkeys );
280 become_root();
281 for ( i=0; i<num_keys; i++ ) {
282 printername = regsubkey_ctr_specific_key( subkeys, i );
283 snum = find_service( printername );
285 /* just verify a valied snum for now */
286 if ( snum == -1 ) {
287 fstrcpy( info2.printername, printername );
288 fstrcpy( info2.sharename, printername );
289 if ( !add_printer_hook( NULL, &printer ) ) {
290 DEBUG(0,("add_printers_by_registry: Failed to add printer [%s]\n",
291 printername));
295 unbecome_root();
297 return True;
300 /**********************************************************************
301 *********************************************************************/
303 static BOOL key_printers_store_keys( const char *key, REGSUBKEY_CTR *subkeys )
305 char *printers_key;
306 char *printername, *printerdatakey;
307 NT_PRINTER_INFO_LEVEL *printer = NULL;
308 int i, num_subkeys, num_existing_keys;
309 char *subkeyname;
310 fstring *existing_subkeys = NULL;
312 printers_key = strip_printers_prefix( key );
314 if ( !printers_key ) {
315 /* have to deal with some new or deleted printer */
316 return add_printers_by_registry( subkeys );
319 if (!reg_split_path( printers_key, &printername, &printerdatakey )) {
320 return False;
323 /* lookup the printer */
325 if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, printername)) ) {
326 DEBUG(0,("key_printers_store_keys: Tried to store subkey for bad printername %s\n",
327 printername));
328 return False;
331 /* get the top level printer keys */
333 num_existing_keys = get_printer_subkeys( printer->info_2->data, "", &existing_subkeys );
335 for ( i=0; i<num_existing_keys; i++ ) {
337 /* remove the key if it has been deleted */
339 if ( !regsubkey_ctr_key_exists( subkeys, existing_subkeys[i] ) ) {
340 DEBUG(5,("key_printers_store_keys: deleting key %s\n",
341 existing_subkeys[i]));
342 delete_printer_key( printer->info_2->data, existing_subkeys[i] );
346 num_subkeys = regsubkey_ctr_numkeys( subkeys );
347 for ( i=0; i<num_subkeys; i++ ) {
348 subkeyname = regsubkey_ctr_specific_key(subkeys, i);
349 /* add any missing printer keys */
350 if ( lookup_printerkey(printer->info_2->data, subkeyname) == -1 ) {
351 DEBUG(5,("key_printers_store_keys: adding key %s\n",
352 existing_subkeys[i]));
353 if ( add_new_printer_key( printer->info_2->data, subkeyname ) == -1 ) {
354 SAFE_FREE( existing_subkeys );
355 return False;
360 /* write back to disk */
362 mod_a_printer( printer, 2 );
364 /* cleanup */
366 if ( printer )
367 free_a_printer( &printer, 2 );
369 SAFE_FREE( existing_subkeys );
371 return True;
374 /**********************************************************************
375 *********************************************************************/
377 static void fill_in_printer_values( NT_PRINTER_INFO_LEVEL_2 *info2, REGVAL_CTR *values )
379 DEVICEMODE *devmode;
380 prs_struct prs;
381 uint32 offset;
382 UNISTR2 data;
383 char *p;
384 uint32 printer_status = PRINTER_STATUS_OK;
386 regval_ctr_addvalue( values, "Attributes", REG_DWORD, (char*)&info2->attributes, sizeof(info2->attributes) );
387 regval_ctr_addvalue( values, "Priority", REG_DWORD, (char*)&info2->priority, sizeof(info2->attributes) );
388 regval_ctr_addvalue( values, "ChangeID", REG_DWORD, (char*)&info2->changeid, sizeof(info2->changeid) );
389 regval_ctr_addvalue( values, "Default Priority", REG_DWORD, (char*)&info2->default_priority, sizeof(info2->default_priority) );
391 /* lie and say everything is ok since we don't want to call print_queue_length() to get the real status */
392 regval_ctr_addvalue( values, "Status", REG_DWORD, (char*)&printer_status, sizeof(info2->status) );
394 regval_ctr_addvalue( values, "StartTime", REG_DWORD, (char*)&info2->starttime, sizeof(info2->starttime) );
395 regval_ctr_addvalue( values, "UntilTime", REG_DWORD, (char*)&info2->untiltime, sizeof(info2->untiltime) );
397 /* strip the \\server\ from this string */
398 if ( !(p = strrchr( info2->printername, '\\' ) ) )
399 p = info2->printername;
400 else
401 p++;
402 init_unistr2( &data, p, UNI_STR_TERMINATE);
403 regval_ctr_addvalue( values, "Name", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
405 init_unistr2( &data, info2->location, UNI_STR_TERMINATE);
406 regval_ctr_addvalue( values, "Location", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
408 init_unistr2( &data, info2->comment, UNI_STR_TERMINATE);
409 regval_ctr_addvalue( values, "Description", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
411 init_unistr2( &data, info2->parameters, UNI_STR_TERMINATE);
412 regval_ctr_addvalue( values, "Parameters", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
414 init_unistr2( &data, info2->portname, UNI_STR_TERMINATE);
415 regval_ctr_addvalue( values, "Port", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
417 init_unistr2( &data, info2->sharename, UNI_STR_TERMINATE);
418 regval_ctr_addvalue( values, "Share Name", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
420 init_unistr2( &data, info2->drivername, UNI_STR_TERMINATE);
421 regval_ctr_addvalue( values, "Printer Driver", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
423 init_unistr2( &data, info2->sepfile, UNI_STR_TERMINATE);
424 regval_ctr_addvalue( values, "Separator File", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
426 init_unistr2( &data, "WinPrint", UNI_STR_TERMINATE);
427 regval_ctr_addvalue( values, "Print Processor", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
429 init_unistr2( &data, "RAW", UNI_STR_TERMINATE);
430 regval_ctr_addvalue( values, "Datatype", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
433 /* use a prs_struct for converting the devmode and security
434 descriptor to REG_BINARY */
436 prs_init( &prs, RPC_MAX_PDU_FRAG_LEN, values, MARSHALL);
438 /* stream the device mode */
440 if ( (devmode = construct_dev_mode( info2->sharename )) != NULL ) {
441 if ( spoolss_io_devmode( "devmode", &prs, 0, devmode ) ) {
442 offset = prs_offset( &prs );
443 regval_ctr_addvalue( values, "Default Devmode", REG_BINARY, prs_data_p(&prs), offset );
447 prs_mem_clear( &prs );
448 prs_set_offset( &prs, 0 );
450 /* stream the printer security descriptor */
452 if ( info2->secdesc_buf && info2->secdesc_buf->sd ) {
453 if ( sec_io_desc("sec_desc", &info2->secdesc_buf->sd, &prs, 0 ) ) {
454 offset = prs_offset( &prs );
455 regval_ctr_addvalue( values, "Security", REG_BINARY, prs_data_p(&prs), offset );
459 prs_mem_free( &prs );
461 return;
464 /**********************************************************************
465 *********************************************************************/
467 static int key_printers_fetch_values( const char *key, REGVAL_CTR *values )
469 int num_values;
470 char *printers_key;
471 char *printername, *printerdatakey;
472 NT_PRINTER_INFO_LEVEL *printer = NULL;
473 NT_PRINTER_DATA *p_data;
474 int i, key_index;
476 printers_key = strip_printers_prefix( key );
478 /* top level key values stored in the registry has no values */
480 if ( !printers_key ) {
481 /* normalize to the 'HKLM\SOFTWARE\...\Print\Printers' ket */
482 return regdb_fetch_values( KEY_WINNT_PRINTERS, values );
485 /* lookup the printer object */
487 if (!reg_split_path( printers_key, &printername, &printerdatakey )) {
488 return -1;
491 if ( !W_ERROR_IS_OK( get_a_printer(NULL, &printer, 2, printername) ) )
492 goto done;
494 if ( !printerdatakey ) {
495 fill_in_printer_values( printer->info_2, values );
496 goto done;
499 /* iterate over all printer data keys and fill the regval container */
501 p_data = printer->info_2->data;
502 if ( (key_index = lookup_printerkey( p_data, printerdatakey )) == -1 ) {
503 /* failure....should never happen if the client has a valid open handle first */
504 DEBUG(10,("key_printers_fetch_values: Unknown keyname [%s]\n", printerdatakey));
505 if ( printer )
506 free_a_printer( &printer, 2 );
507 return -1;
510 num_values = regval_ctr_numvals( p_data->keys[key_index].values );
511 for ( i=0; i<num_values; i++ )
512 regval_ctr_copyvalue( values, regval_ctr_specific_value(p_data->keys[key_index].values, i) );
515 done:
516 if ( printer )
517 free_a_printer( &printer, 2 );
519 return regval_ctr_numvals( values );
522 /**********************************************************************
523 *********************************************************************/
525 #define REG_IDX_ATTRIBUTES 1
526 #define REG_IDX_PRIORITY 2
527 #define REG_IDX_DEFAULT_PRIORITY 3
528 #define REG_IDX_CHANGEID 4
529 #define REG_IDX_STATUS 5
530 #define REG_IDX_STARTTIME 6
531 #define REG_IDX_NAME 7
532 #define REG_IDX_LOCATION 8
533 #define REG_IDX_DESCRIPTION 9
534 #define REG_IDX_PARAMETERS 10
535 #define REG_IDX_PORT 12
536 #define REG_IDX_SHARENAME 13
537 #define REG_IDX_DRIVER 14
538 #define REG_IDX_SEP_FILE 15
539 #define REG_IDX_PRINTPROC 16
540 #define REG_IDX_DATATYPE 17
541 #define REG_IDX_DEVMODE 18
542 #define REG_IDX_SECDESC 19
543 #define REG_IDX_UNTILTIME 20
545 struct {
546 const char *name;
547 int index;
548 } printer_values_map[] = {
549 { "Attributes", REG_IDX_ATTRIBUTES },
550 { "Priority", REG_IDX_PRIORITY },
551 { "Default Priority", REG_IDX_DEFAULT_PRIORITY },
552 { "ChangeID", REG_IDX_CHANGEID },
553 { "Status", REG_IDX_STATUS },
554 { "StartTime", REG_IDX_STARTTIME },
555 { "UntilTime", REG_IDX_UNTILTIME },
556 { "Name", REG_IDX_NAME },
557 { "Location", REG_IDX_LOCATION },
558 { "Descrioption", REG_IDX_DESCRIPTION },
559 { "Parameters", REG_IDX_PARAMETERS },
560 { "Port", REG_IDX_PORT },
561 { "Share Name", REG_IDX_SHARENAME },
562 { "Printer Driver", REG_IDX_DRIVER },
563 { "Separator File", REG_IDX_SEP_FILE },
564 { "Print Processor", REG_IDX_PRINTPROC },
565 { "Datatype", REG_IDX_DATATYPE },
566 { "Default Devmode", REG_IDX_DEVMODE },
567 { "Security", REG_IDX_SECDESC },
568 { NULL, -1 }
572 static int find_valuename_index( const char *valuename )
574 int i;
576 for ( i=0; printer_values_map[i].name; i++ ) {
577 if ( strequal( valuename, printer_values_map[i].name ) )
578 return printer_values_map[i].index;
581 return -1;
584 /**********************************************************************
585 *********************************************************************/
587 static void convert_values_to_printer_info_2( NT_PRINTER_INFO_LEVEL_2 *printer2, REGVAL_CTR *values )
589 int num_values = regval_ctr_numvals( values );
590 uint32 value_index;
591 REGISTRY_VALUE *val;
592 int i;
594 for ( i=0; i<num_values; i++ ) {
595 val = regval_ctr_specific_value( values, i );
596 value_index = find_valuename_index( regval_name( val ) );
598 switch( value_index ) {
599 case REG_IDX_ATTRIBUTES:
600 printer2->attributes = (uint32)(*regval_data_p(val));
601 break;
602 case REG_IDX_PRIORITY:
603 printer2->priority = (uint32)(*regval_data_p(val));
604 break;
605 case REG_IDX_DEFAULT_PRIORITY:
606 printer2->default_priority = (uint32)(*regval_data_p(val));
607 break;
608 case REG_IDX_CHANGEID:
609 printer2->changeid = (uint32)(*regval_data_p(val));
610 break;
611 case REG_IDX_STARTTIME:
612 printer2->starttime = (uint32)(*regval_data_p(val));
613 break;
614 case REG_IDX_UNTILTIME:
615 printer2->untiltime = (uint32)(*regval_data_p(val));
616 break;
617 case REG_IDX_NAME:
618 rpcstr_pull( printer2->printername, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
619 break;
620 case REG_IDX_LOCATION:
621 rpcstr_pull( printer2->location, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
622 break;
623 case REG_IDX_DESCRIPTION:
624 rpcstr_pull( printer2->comment, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
625 break;
626 case REG_IDX_PARAMETERS:
627 rpcstr_pull( printer2->parameters, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
628 break;
629 case REG_IDX_PORT:
630 rpcstr_pull( printer2->portname, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
631 break;
632 case REG_IDX_SHARENAME:
633 rpcstr_pull( printer2->sharename, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
634 break;
635 case REG_IDX_DRIVER:
636 rpcstr_pull( printer2->drivername, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
637 break;
638 case REG_IDX_SEP_FILE:
639 rpcstr_pull( printer2->sepfile, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
640 break;
641 case REG_IDX_PRINTPROC:
642 rpcstr_pull( printer2->printprocessor, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
643 break;
644 case REG_IDX_DATATYPE:
645 rpcstr_pull( printer2->datatype, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
646 break;
647 case REG_IDX_DEVMODE:
648 break;
649 case REG_IDX_SECDESC:
650 break;
651 default:
652 /* unsupported value...throw away */
653 DEBUG(8,("convert_values_to_printer_info_2: Unsupported registry value [%s]\n",
654 regval_name( val ) ));
658 return;
661 /**********************************************************************
662 *********************************************************************/
664 static BOOL key_printers_store_values( const char *key, REGVAL_CTR *values )
666 char *printers_key;
667 char *printername, *keyname;
668 NT_PRINTER_INFO_LEVEL *printer = NULL;
669 WERROR result;
671 printers_key = strip_printers_prefix( key );
673 /* values in the top level key get stored in the registry */
675 if ( !printers_key ) {
676 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
677 return regdb_store_values( KEY_WINNT_PRINTERS, values );
680 if (!reg_split_path( printers_key, &printername, &keyname )) {
681 return False;
684 if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, printername) ) )
685 return False;
687 /* deal with setting values directly under the printername */
689 if ( !keyname ) {
690 convert_values_to_printer_info_2( printer->info_2, values );
692 else {
693 int num_values = regval_ctr_numvals( values );
694 int i;
695 REGISTRY_VALUE *val;
697 delete_printer_key( printer->info_2->data, keyname );
699 /* deal with any subkeys */
700 for ( i=0; i<num_values; i++ ) {
701 val = regval_ctr_specific_value( values, i );
702 result = set_printer_dataex( printer, keyname,
703 regval_name( val ),
704 regval_type( val ),
705 regval_data_p( val ),
706 regval_size( val ) );
707 if ( !W_ERROR_IS_OK(result) ) {
708 DEBUG(0,("key_printers_store_values: failed to set printer data [%s]!\n",
709 keyname));
710 free_a_printer( &printer, 2 );
711 return False;
716 result = mod_a_printer( printer, 2 );
718 free_a_printer( &printer, 2 );
720 return W_ERROR_IS_OK(result);
723 /*********************************************************************
724 *********************************************************************
725 ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/ENVIRONMENTS"
726 *********************************************************************
727 *********************************************************************/
729 static int key_driver_fetch_keys( const char *key, REGSUBKEY_CTR *subkeys )
731 const char *environments[] = {
732 "Windows 4.0",
733 "Windows NT x86",
734 "Windows NT R4000",
735 "Windows NT Alpha_AXP",
736 "Windows NT PowerPC",
737 "Windows IA64",
738 "Windows x64",
739 NULL };
740 fstring *drivers = NULL;
741 int i, env_index, num_drivers;
742 char *keystr, *base, *subkeypath;
743 pstring key2;
744 int num_subkeys = -1;
745 int version;
747 DEBUG(10,("key_driver_fetch_keys key=>[%s]\n", key ? key : "NULL" ));
749 keystr = reg_remaining_path( key + strlen(KEY_ENVIRONMENTS) );
751 /* list all possible architectures */
753 if ( !keystr ) {
754 for ( num_subkeys=0; environments[num_subkeys]; num_subkeys++ )
755 regsubkey_ctr_addkey( subkeys, environments[num_subkeys] );
757 return num_subkeys;
760 /* we are dealing with a subkey of "Environments */
762 pstrcpy( key2, keystr );
763 keystr = key2;
764 if (!reg_split_path( keystr, &base, &subkeypath )) {
765 return -1;
768 /* sanity check */
770 for ( env_index=0; environments[env_index]; env_index++ ) {
771 if ( strequal( environments[env_index], base ) )
772 break;
774 if ( !environments[env_index] )
775 return -1;
777 /* ...\Print\Environements\...\ */
779 if ( !subkeypath ) {
780 regsubkey_ctr_addkey( subkeys, "Drivers" );
781 regsubkey_ctr_addkey( subkeys, "Print Processors" );
783 return 2;
786 /* more of the key path to process */
788 keystr = subkeypath;
789 if (!reg_split_path( keystr, &base, &subkeypath )) {
790 return -1;
793 /* ...\Print\Environements\...\Drivers\ */
795 if ( !subkeypath ) {
796 if ( strequal(base, "Drivers") ) {
797 switch ( env_index ) {
798 case 0: /* Win9x */
799 regsubkey_ctr_addkey( subkeys, "Version-0" );
800 break;
801 default: /* Windows NT based systems */
802 regsubkey_ctr_addkey( subkeys, "Version-2" );
803 regsubkey_ctr_addkey( subkeys, "Version-3" );
804 break;
807 return regsubkey_ctr_numkeys( subkeys );
808 } else if ( strequal(base, "Print Processors") ) {
809 if ( env_index == 1 || env_index == 5 || env_index == 6 )
810 regsubkey_ctr_addkey( subkeys, "winprint" );
812 return regsubkey_ctr_numkeys( subkeys );
813 } else
814 return -1; /* bad path */
817 /* we finally get to enumerate the drivers */
819 /* only one possible subkey below PrintProc key */
821 if ( strequal(base, "Print Processors") ) {
822 keystr = subkeypath;
823 if (!reg_split_path( keystr, &base, &subkeypath )) {
824 return -1;
827 /* no subkeys below this point */
829 if ( subkeypath )
830 return -1;
832 /* only allow one keyname here -- 'winprint' */
834 return strequal( base, "winprint" ) ? 0 : -1;
837 /* only dealing with drivers from here on out */
839 keystr = subkeypath;
840 if (!reg_split_path( keystr, &base, &subkeypath )) {
841 return -1;
844 version = atoi(&base[strlen(base)-1]);
846 switch (env_index) {
847 case 0:
848 if ( version != 0 )
849 return -1;
850 break;
851 default:
852 if ( version != 2 && version != 3 )
853 return -1;
854 break;
858 if ( !subkeypath ) {
859 num_drivers = get_ntdrivers( &drivers, environments[env_index], version );
860 for ( i=0; i<num_drivers; i++ )
861 regsubkey_ctr_addkey( subkeys, drivers[i] );
863 return regsubkey_ctr_numkeys( subkeys );
866 /* if anything else left, just say if has no subkeys */
868 DEBUG(1,("key_driver_fetch_keys unhandled key [%s] (subkey == %s\n",
869 key, subkeypath ));
871 return 0;
875 /**********************************************************************
876 *********************************************************************/
878 static void fill_in_driver_values( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3, REGVAL_CTR *values )
880 char *buffer = NULL;
881 int buffer_size = 0;
882 int i, length;
883 char *filename;
884 UNISTR2 data;
886 filename = dos_basename( info3->driverpath );
887 init_unistr2( &data, filename, UNI_STR_TERMINATE);
888 regval_ctr_addvalue( values, "Driver", REG_SZ, (char*)data.buffer,
889 data.uni_str_len*sizeof(uint16) );
891 filename = dos_basename( info3->configfile );
892 init_unistr2( &data, filename, UNI_STR_TERMINATE);
893 regval_ctr_addvalue( values, "Configuration File", REG_SZ, (char*)data.buffer,
894 data.uni_str_len*sizeof(uint16) );
896 filename = dos_basename( info3->datafile );
897 init_unistr2( &data, filename, UNI_STR_TERMINATE);
898 regval_ctr_addvalue( values, "Data File", REG_SZ, (char*)data.buffer,
899 data.uni_str_len*sizeof(uint16) );
901 filename = dos_basename( info3->helpfile );
902 init_unistr2( &data, filename, UNI_STR_TERMINATE);
903 regval_ctr_addvalue( values, "Help File", REG_SZ, (char*)data.buffer,
904 data.uni_str_len*sizeof(uint16) );
906 init_unistr2( &data, info3->defaultdatatype, UNI_STR_TERMINATE);
907 regval_ctr_addvalue( values, "Data Type", REG_SZ, (char*)data.buffer,
908 data.uni_str_len*sizeof(uint16) );
910 regval_ctr_addvalue( values, "Version", REG_DWORD, (char*)&info3->cversion,
911 sizeof(info3->cversion) );
913 if ( info3->dependentfiles ) {
914 /* place the list of dependent files in a single
915 character buffer, separating each file name by
916 a NULL */
918 for ( i=0; strcmp(info3->dependentfiles[i], ""); i++ ) {
919 /* strip the path to only the file's base name */
921 filename = dos_basename( info3->dependentfiles[i] );
923 length = strlen(filename);
925 buffer = (char *)SMB_REALLOC( buffer, buffer_size + (length + 1)*sizeof(uint16) );
926 if ( !buffer ) {
927 break;
930 init_unistr2( &data, filename, UNI_STR_TERMINATE);
931 memcpy( buffer+buffer_size, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
933 buffer_size += (length + 1)*sizeof(uint16);
936 /* terminated by double NULL. Add the final one here */
938 buffer = (char *)SMB_REALLOC( buffer, buffer_size + 2 );
939 if ( !buffer ) {
940 buffer_size = 0;
941 } else {
942 buffer[buffer_size++] = '\0';
943 buffer[buffer_size++] = '\0';
947 regval_ctr_addvalue( values, "Dependent Files", REG_MULTI_SZ, buffer, buffer_size );
949 SAFE_FREE( buffer );
951 return;
954 /**********************************************************************
955 *********************************************************************/
957 static int driver_arch_fetch_values( char *key, REGVAL_CTR *values )
959 char *keystr, *base, *subkeypath;
960 fstring arch_environment;
961 fstring driver;
962 int version;
963 NT_PRINTER_DRIVER_INFO_LEVEL driver_ctr;
964 WERROR w_result;
966 if (!reg_split_path( key, &base, &subkeypath )) {
967 return -1;
970 /* no values in 'Environments\Drivers\Windows NT x86' */
972 if ( !subkeypath )
973 return 0;
975 /* We have the Architecture string and some subkey name:
976 Currently we only support
977 * Drivers
978 * Print Processors
979 Anything else is an error.
982 fstrcpy( arch_environment, base );
984 keystr = subkeypath;
985 if (!reg_split_path( keystr, &base, &subkeypath )) {
986 return -1;
989 if ( strequal(base, "Print Processors") )
990 return 0;
992 /* only Drivers key can be left */
994 if ( !strequal(base, "Drivers") )
995 return -1;
997 if ( !subkeypath )
998 return 0;
1000 /* We know that we have Architechure\Drivers with some subkey name
1001 The subkey name has to be Version-XX */
1003 keystr = subkeypath;
1004 if (!reg_split_path( keystr, &base, &subkeypath )) {
1005 return -1;
1008 if ( !subkeypath )
1009 return 0;
1011 version = atoi(&base[strlen(base)-1]);
1013 /* BEGIN PRINTER DRIVER NAME BLOCK */
1015 keystr = subkeypath;
1016 if (!reg_split_path( keystr, &base, &subkeypath )) {
1017 return -1;
1020 /* don't go any deeper for now */
1022 fstrcpy( driver, base );
1024 w_result = get_a_printer_driver( &driver_ctr, 3, driver, arch_environment, version );
1026 if ( !W_ERROR_IS_OK(w_result) )
1027 return -1;
1029 fill_in_driver_values( driver_ctr.info_3, values );
1031 free_a_printer_driver( driver_ctr, 3 );
1033 /* END PRINTER DRIVER NAME BLOCK */
1036 DEBUG(8,("key_driver_fetch_values: Exit\n"));
1038 return regval_ctr_numvals( values );
1041 /**********************************************************************
1042 *********************************************************************/
1044 static int key_driver_fetch_values( const char *key, REGVAL_CTR *values )
1046 char *keystr;
1047 pstring subkey;
1049 DEBUG(8,("key_driver_fetch_values: Enter key => [%s]\n", key ? key : "NULL"));
1051 /* no values in the Environments key */
1053 if ( !(keystr = reg_remaining_path( key + strlen(KEY_ENVIRONMENTS) )) )
1054 return 0;
1056 pstrcpy( subkey, keystr);
1058 /* pass off to handle subkeys */
1060 return driver_arch_fetch_values( subkey, values );
1063 /*********************************************************************
1064 *********************************************************************
1065 ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT"
1066 *********************************************************************
1067 *********************************************************************/
1069 static int key_print_fetch_keys( const char *key, REGSUBKEY_CTR *subkeys )
1071 int key_len = strlen(key);
1073 /* no keys below 'Print' handled here */
1075 if ( (key_len != strlen(KEY_CONTROL_PRINT)) && (key_len != strlen(KEY_WINNT_PRINT)) )
1076 return -1;
1078 regsubkey_ctr_addkey( subkeys, "Environments" );
1079 regsubkey_ctr_addkey( subkeys, "Monitors" );
1080 regsubkey_ctr_addkey( subkeys, "Forms" );
1081 regsubkey_ctr_addkey( subkeys, "Printers" );
1083 return regsubkey_ctr_numkeys( subkeys );
1086 /**********************************************************************
1087 *********************************************************************
1088 ** Structure to hold dispatch table of ops for various printer keys.
1089 ** Make sure to always store deeper keys along the same path first so
1090 ** we ge a more specific match.
1091 *********************************************************************
1092 *********************************************************************/
1094 static struct reg_dyn_tree print_registry[] = {
1095 /* just pass the monitor onto the registry tdb */
1096 { KEY_MONITORS,
1097 &regdb_fetch_keys,
1098 &regdb_store_keys,
1099 &regdb_fetch_values,
1100 &regdb_store_values },
1101 { KEY_FORMS,
1102 &key_forms_fetch_keys,
1103 NULL,
1104 &key_forms_fetch_values,
1105 NULL },
1106 { KEY_CONTROL_PRINTERS,
1107 &key_printers_fetch_keys,
1108 &key_printers_store_keys,
1109 &key_printers_fetch_values,
1110 &key_printers_store_values },
1111 { KEY_ENVIRONMENTS,
1112 &key_driver_fetch_keys,
1113 NULL,
1114 &key_driver_fetch_values,
1115 NULL },
1116 { KEY_CONTROL_PRINT,
1117 &key_print_fetch_keys,
1118 NULL,
1119 NULL,
1120 NULL },
1121 { KEY_WINNT_PRINTERS,
1122 &key_printers_fetch_keys,
1123 &key_printers_store_keys,
1124 &key_printers_fetch_values,
1125 &key_printers_store_values },
1126 { KEY_PORTS,
1127 &regdb_fetch_keys,
1128 &regdb_store_keys,
1129 &regdb_fetch_values,
1130 &regdb_store_values },
1132 { NULL, NULL, NULL, NULL, NULL }
1136 /**********************************************************************
1137 *********************************************************************
1138 ** Main reg_printing interface functions
1139 *********************************************************************
1140 *********************************************************************/
1142 /***********************************************************************
1143 Lookup a key in the print_registry table, returning its index.
1144 -1 on failure
1145 **********************************************************************/
1147 static int match_registry_path( const char *key )
1149 int i;
1150 pstring path;
1152 if ( !key )
1153 return -1;
1155 pstrcpy( path, key );
1156 normalize_reg_path( path );
1158 for ( i=0; print_registry[i].path; i++ ) {
1159 if ( strncmp( path, print_registry[i].path, strlen(print_registry[i].path) ) == 0 )
1160 return i;
1163 return -1;
1166 /***********************************************************************
1167 **********************************************************************/
1169 static int regprint_fetch_reg_keys( const char *key, REGSUBKEY_CTR *subkeys )
1171 int i = match_registry_path( key );
1173 if ( i == -1 )
1174 return -1;
1176 if ( !print_registry[i].fetch_subkeys )
1177 return -1;
1179 return print_registry[i].fetch_subkeys( key, subkeys );
1182 /**********************************************************************
1183 *********************************************************************/
1185 static BOOL regprint_store_reg_keys( const char *key, REGSUBKEY_CTR *subkeys )
1187 int i = match_registry_path( key );
1189 if ( i == -1 )
1190 return False;
1192 if ( !print_registry[i].store_subkeys )
1193 return False;
1195 return print_registry[i].store_subkeys( key, subkeys );
1198 /**********************************************************************
1199 *********************************************************************/
1201 static int regprint_fetch_reg_values( const char *key, REGVAL_CTR *values )
1203 int i = match_registry_path( key );
1205 if ( i == -1 )
1206 return -1;
1208 /* return 0 values by default since we know the key had
1209 to exist because the client opened a handle */
1211 if ( !print_registry[i].fetch_values )
1212 return 0;
1214 return print_registry[i].fetch_values( key, values );
1217 /**********************************************************************
1218 *********************************************************************/
1220 static BOOL regprint_store_reg_values( const char *key, REGVAL_CTR *values )
1222 int i = match_registry_path( key );
1224 if ( i == -1 )
1225 return False;
1227 if ( !print_registry[i].store_values )
1228 return False;
1230 return print_registry[i].store_values( key, values );
1234 * Table of function pointers for accessing printing data
1237 REGISTRY_OPS printing_ops = {
1238 regprint_fetch_reg_keys,
1239 regprint_fetch_reg_values,
1240 regprint_store_reg_keys,
1241 regprint_store_reg_values,
1242 NULL