registry: check for existence of key in regdb_get_secdesc() before proceeding.
[kai/samba.git] / source3 / registry / reg_backend_printing.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Gerald Carter                     2002-2005
5  *
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 3 of the License, or
9  *  (at your option) any later version.
10  *
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.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /* Implementation of registry virtual views for printing information */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
26
27 /* registrt paths used in the print_registry[] */
28
29 #define KEY_MONITORS            "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/MONITORS"
30 #define KEY_FORMS               "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/FORMS"
31 #define KEY_CONTROL_PRINTERS    "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/PRINTERS"
32 #define KEY_ENVIRONMENTS        "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/ENVIRONMENTS"
33 #define KEY_CONTROL_PRINT       "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT"
34 #define KEY_WINNT_PRINTERS      "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT/PRINTERS"
35 #define KEY_WINNT_PRINT         "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT"
36 #define KEY_PORTS               "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PORTS"
37
38 /* callback table for various registry paths below the ones we service in this module */
39
40 struct reg_dyn_tree {
41         /* full key path in normalized form */
42         const char *path;
43
44         /* callbscks for fetch/store operations */
45         int ( *fetch_subkeys) ( const char *path, REGSUBKEY_CTR *subkeys );
46         bool (*store_subkeys) ( const char *path, REGSUBKEY_CTR *subkeys );
47         int  (*fetch_values)  ( const char *path, REGVAL_CTR *values );
48         bool (*store_values)  ( const char *path, REGVAL_CTR *values );
49 };
50
51 /*********************************************************************
52  *********************************************************************
53  ** Utility Functions
54  *********************************************************************
55  *********************************************************************/
56
57 /***********************************************************************
58  simple function to prune a pathname down to the basename of a file
59  **********************************************************************/
60
61 static const char *dos_basename(const char *path)
62 {
63         const char *p;
64
65         if (!(p = strrchr( path, '\\'))) {
66                 p = path;
67         } else {
68                 p++;
69         }
70
71         return p;
72 }
73
74 /*********************************************************************
75  *********************************************************************
76  ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/FORMS"
77  *********************************************************************
78  *********************************************************************/
79
80 static int key_forms_fetch_keys(const char *key, REGSUBKEY_CTR *subkeys)
81 {
82         char *p = reg_remaining_path(talloc_tos(), key + strlen(KEY_FORMS));
83
84         /* no keys below Forms */
85
86         if (p) {
87                 return -1;
88         }
89
90         return 0;
91 }
92
93 /**********************************************************************
94  *********************************************************************/
95
96 static int key_forms_fetch_values( const char *key, REGVAL_CTR *values )
97 {
98         uint32          data[8];
99         int             i, num_values, form_index = 1;
100         nt_forms_struct *forms_list = NULL;
101         nt_forms_struct *form;
102
103         DEBUG(10,("print_values_forms: key=>[%s]\n", key ? key : "NULL" ));
104
105         num_values = get_ntforms( &forms_list );
106
107         DEBUG(10,("hive_forms_fetch_values: [%d] user defined forms returned\n",
108                 num_values));
109
110         /* handle user defined forms */
111
112         for ( i=0; i<num_values; i++ ) {
113                 form = &forms_list[i];
114
115                 data[0] = form->width;
116                 data[1] = form->length;
117                 data[2] = form->left;
118                 data[3] = form->top;
119                 data[4] = form->right;
120                 data[5] = form->bottom;
121                 data[6] = form_index++;
122                 data[7] = form->flag;
123
124                 regval_ctr_addvalue( values, form->name, REG_BINARY, (char*)data, sizeof(data) );       
125         }
126
127         SAFE_FREE( forms_list );
128         forms_list = NULL;
129
130         /* handle built-on forms */
131
132         num_values = get_builtin_ntforms( &forms_list );
133
134         DEBUG(10,("print_subpath_values_forms: [%d] built-in forms returned\n",
135                 num_values));
136
137         for ( i=0; i<num_values; i++ ) {
138                 form = &forms_list[i];
139
140                 data[0] = form->width;
141                 data[1] = form->length;
142                 data[2] = form->left;
143                 data[3] = form->top;
144                 data[4] = form->right;
145                 data[5] = form->bottom;
146                 data[6] = form_index++;
147                 data[7] = form->flag;
148
149                 regval_ctr_addvalue(values, form->name, REG_BINARY, (char*)data, sizeof(data) );
150         }
151
152         SAFE_FREE(forms_list);
153
154         return regval_ctr_numvals(values);
155 }
156
157 /*********************************************************************
158  *********************************************************************
159  ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/PRINTERS"
160  ** "HKLM/SOFTWARE/MICROSOFT/WINDOWS NT/CURRENTVERSION/PRINT/PRINTERS"
161  *********************************************************************
162  *********************************************************************/
163
164 /*********************************************************************
165  strip off prefix for printers key.  DOes return a pointer to static
166  memory.
167  *********************************************************************/
168
169 static char *strip_printers_prefix(const char *key)
170 {
171         char *subkeypath = NULL;
172         char *path = NULL;
173         TALLOC_CTX *ctx = talloc_tos();
174
175         path = talloc_strdup(ctx, key);
176         if (!path) {
177                 return NULL;
178         }
179         path = normalize_reg_path(ctx, path);
180         if (!path) {
181                 return NULL;
182         }
183
184         /* normalizing the path does not change length, just key delimiters and case */
185
186         if (strncmp(path, KEY_WINNT_PRINTERS, strlen(KEY_WINNT_PRINTERS)) == 0) {
187                 subkeypath = reg_remaining_path(ctx, key + strlen(KEY_WINNT_PRINTERS));
188         } else {
189                 subkeypath = reg_remaining_path(ctx, key + strlen(KEY_CONTROL_PRINTERS));
190         }
191
192         TALLOC_FREE(path);
193         return subkeypath;
194 }
195
196 /*********************************************************************
197  *********************************************************************/
198
199 static int key_printers_fetch_keys( const char *key, REGSUBKEY_CTR *subkeys )
200 {
201         int n_services = lp_numservices();
202         int snum;
203         fstring sname;
204         int i;
205         int num_subkeys = 0;
206         char *printers_key;
207         char *printername, *printerdatakey;
208         NT_PRINTER_INFO_LEVEL *printer = NULL;
209         fstring *subkey_names = NULL;
210
211         DEBUG(10,("key_printers_fetch_keys: key=>[%s]\n", key ? key : "NULL" ));
212
213         printers_key = strip_printers_prefix( key );
214
215         if ( !printers_key ) {
216                 /* enumerate all printers */
217
218                 for (snum=0; snum<n_services; snum++) {
219                         if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
220                                 continue;
221
222                         /* don't report the [printers] share */
223
224                         if ( strequal( lp_servicename(snum), PRINTERS_NAME ) )
225                                 continue;
226
227                         fstrcpy( sname, lp_servicename(snum) );
228
229                         regsubkey_ctr_addkey( subkeys, sname );
230                 }
231
232                 num_subkeys = regsubkey_ctr_numkeys( subkeys );
233                 goto done;
234         }
235
236         /* get information for a specific printer */
237
238         if (!reg_split_path( printers_key, &printername, &printerdatakey )) {
239                 return -1;
240         }
241
242         /* validate the printer name */
243
244         for (snum=0; snum<n_services; snum++) {
245                 if ( !lp_snum_ok(snum) || !lp_print_ok(snum) )
246                         continue;
247                 if (strequal( lp_servicename(snum), printername ) )
248                         break;
249         }
250
251         if ( snum>=n_services
252                 || !W_ERROR_IS_OK( get_a_printer(NULL, &printer, 2, printername) ) ) 
253         {
254                 return -1;
255         }
256
257         num_subkeys = get_printer_subkeys( printer->info_2->data, printerdatakey?printerdatakey:"", &subkey_names );
258         
259         for ( i=0; i<num_subkeys; i++ )
260                 regsubkey_ctr_addkey( subkeys, subkey_names[i] );
261         
262         free_a_printer( &printer, 2 );
263                         
264         /* no other subkeys below here */
265
266 done:   
267         SAFE_FREE( subkey_names );
268         
269         return num_subkeys;
270 }
271
272 /**********************************************************************
273  Take a list of names and call add_printer_hook() if necessary
274  Note that we do this a little differently from Windows since the 
275  keyname is the sharename and not the printer name.
276  *********************************************************************/
277
278 static bool add_printers_by_registry( REGSUBKEY_CTR *subkeys )
279 {
280         int i, num_keys, snum;
281         char *printername;
282         NT_PRINTER_INFO_LEVEL_2 info2;
283         NT_PRINTER_INFO_LEVEL printer;
284         
285         ZERO_STRUCT( info2 );
286         printer.info_2 = &info2;
287         
288         num_keys = regsubkey_ctr_numkeys( subkeys );
289         
290         become_root();
291         for ( i=0; i<num_keys; i++ ) {
292                 printername = regsubkey_ctr_specific_key( subkeys, i );
293                 snum = find_service( printername );
294                 
295                 /* just verify a valied snum for now */
296                 if ( snum == -1 ) {
297                         fstrcpy( info2.printername, printername );
298                         fstrcpy( info2.sharename, printername );
299                         if ( !add_printer_hook(talloc_tos(), NULL, &printer ) ) {
300                                 DEBUG(0,("add_printers_by_registry: Failed to add printer [%s]\n",
301                                         printername));
302                         }       
303                 }
304         }
305         unbecome_root();
306
307         return True;
308 }
309
310 /**********************************************************************
311  *********************************************************************/
312
313 static bool key_printers_store_keys( const char *key, REGSUBKEY_CTR *subkeys )
314 {
315         char *printers_key;
316         char *printername, *printerdatakey;
317         NT_PRINTER_INFO_LEVEL *printer = NULL;
318         int i, num_subkeys, num_existing_keys;
319         char *subkeyname;
320         fstring *existing_subkeys = NULL;
321         
322         printers_key = strip_printers_prefix( key );
323         
324         if ( !printers_key ) {
325                 /* have to deal with some new or deleted printer */
326                 return add_printers_by_registry( subkeys );
327         }
328         
329         if (!reg_split_path( printers_key, &printername, &printerdatakey )) {
330                 return False;
331         }
332         
333         /* lookup the printer */
334         
335         if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, printername)) ) {
336                 DEBUG(0,("key_printers_store_keys: Tried to store subkey for bad printername %s\n", 
337                         printername));
338                 return False;
339         }
340         
341         /* get the top level printer keys */
342         
343         num_existing_keys = get_printer_subkeys( printer->info_2->data, "", &existing_subkeys );
344         
345         for ( i=0; i<num_existing_keys; i++ ) {
346         
347                 /* remove the key if it has been deleted */
348                 
349                 if ( !regsubkey_ctr_key_exists( subkeys, existing_subkeys[i] ) ) {
350                         DEBUG(5,("key_printers_store_keys: deleting key %s\n", 
351                                 existing_subkeys[i]));
352                         delete_printer_key( printer->info_2->data, existing_subkeys[i] );
353                 }
354         }
355
356         num_subkeys = regsubkey_ctr_numkeys( subkeys );
357         for ( i=0; i<num_subkeys; i++ ) {
358                 subkeyname = regsubkey_ctr_specific_key(subkeys, i);
359                 /* add any missing printer keys */
360                 if ( lookup_printerkey(printer->info_2->data, subkeyname) == -1 ) {
361                         DEBUG(5,("key_printers_store_keys: adding key %s\n", 
362                                 existing_subkeys[i]));
363                         if ( add_new_printer_key( printer->info_2->data, subkeyname ) == -1 ) {
364                                 SAFE_FREE( existing_subkeys );
365                                 return False;
366                         }
367                 }
368         }
369         
370         /* write back to disk */
371         
372         mod_a_printer( printer, 2 );
373         
374         /* cleanup */
375         
376         free_a_printer( &printer, 2 );
377
378         SAFE_FREE( existing_subkeys );
379
380         return True;
381 }
382
383 /**********************************************************************
384  *********************************************************************/
385
386 static void fill_in_printer_values( NT_PRINTER_INFO_LEVEL_2 *info2, REGVAL_CTR *values )
387 {
388         DEVICEMODE      *devmode;
389         prs_struct      prs;
390         uint32          offset;
391         UNISTR2         data;
392         char            *p;
393         uint32 printer_status = PRINTER_STATUS_OK;
394         
395         regval_ctr_addvalue( values, "Attributes",       REG_DWORD, (char*)&info2->attributes,       sizeof(info2->attributes) );
396         regval_ctr_addvalue( values, "Priority",         REG_DWORD, (char*)&info2->priority,         sizeof(info2->attributes) );
397         regval_ctr_addvalue( values, "ChangeID",         REG_DWORD, (char*)&info2->changeid,         sizeof(info2->changeid) );
398         regval_ctr_addvalue( values, "Default Priority", REG_DWORD, (char*)&info2->default_priority, sizeof(info2->default_priority) );
399         
400         /* lie and say everything is ok since we don't want to call print_queue_length() to get the real status */
401         regval_ctr_addvalue( values, "Status",           REG_DWORD, (char*)&printer_status,          sizeof(info2->status) );
402
403         regval_ctr_addvalue( values, "StartTime",        REG_DWORD, (char*)&info2->starttime,        sizeof(info2->starttime) );
404         regval_ctr_addvalue( values, "UntilTime",        REG_DWORD, (char*)&info2->untiltime,        sizeof(info2->untiltime) );
405
406         /* strip the \\server\ from this string */
407         if ( !(p = strrchr( info2->printername, '\\' ) ) )
408                 p = info2->printername;
409         else
410                 p++;
411         init_unistr2( &data, p, UNI_STR_TERMINATE);
412         regval_ctr_addvalue( values, "Name", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
413
414         init_unistr2( &data, info2->location, UNI_STR_TERMINATE);
415         regval_ctr_addvalue( values, "Location", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
416
417         init_unistr2( &data, info2->comment, UNI_STR_TERMINATE);
418         regval_ctr_addvalue( values, "Description", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
419
420         init_unistr2( &data, info2->parameters, UNI_STR_TERMINATE);
421         regval_ctr_addvalue( values, "Parameters", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
422
423         init_unistr2( &data, info2->portname, UNI_STR_TERMINATE);
424         regval_ctr_addvalue( values, "Port", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
425
426         init_unistr2( &data, info2->sharename, UNI_STR_TERMINATE);
427         regval_ctr_addvalue( values, "Share Name", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
428
429         init_unistr2( &data, info2->drivername, UNI_STR_TERMINATE);
430         regval_ctr_addvalue( values, "Printer Driver", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
431
432         init_unistr2( &data, info2->sepfile, UNI_STR_TERMINATE);
433         regval_ctr_addvalue( values, "Separator File", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
434
435         init_unistr2( &data, "WinPrint", UNI_STR_TERMINATE);
436         regval_ctr_addvalue( values, "Print Processor",  REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
437
438         init_unistr2( &data, "RAW", UNI_STR_TERMINATE);
439         regval_ctr_addvalue( values, "Datatype", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
440
441                 
442         /* use a prs_struct for converting the devmode and security 
443            descriptor to REG_BINARY */
444         
445         if (!prs_init( &prs, RPC_MAX_PDU_FRAG_LEN, values, MARSHALL))
446                 return;
447
448         /* stream the device mode */
449                 
450         if ( (devmode = construct_dev_mode( info2->sharename )) != NULL ) {
451                 if ( spoolss_io_devmode( "devmode", &prs, 0, devmode ) ) {
452                         offset = prs_offset( &prs );
453                         regval_ctr_addvalue( values, "Default Devmode", REG_BINARY, prs_data_p(&prs), offset );
454                 }
455         }
456                 
457         prs_mem_clear( &prs );
458         prs_set_offset( &prs, 0 );
459                 
460         /* stream the printer security descriptor */
461         
462         if ( info2->secdesc_buf &&
463              info2->secdesc_buf->sd &&
464              info2->secdesc_buf->sd_size )  
465         {
466                 if ( sec_io_desc("sec_desc", &info2->secdesc_buf->sd, &prs, 0 ) ) {
467                         offset = prs_offset( &prs );
468                         regval_ctr_addvalue( values, "Security", REG_BINARY, prs_data_p(&prs), offset );
469                 }
470         }
471
472         prs_mem_free( &prs );
473
474         return;         
475 }
476
477 /**********************************************************************
478  *********************************************************************/
479
480 static int key_printers_fetch_values( const char *key, REGVAL_CTR *values )
481 {
482         int             num_values;
483         char            *printers_key;
484         char            *printername, *printerdatakey;
485         NT_PRINTER_INFO_LEVEL   *printer = NULL;
486         NT_PRINTER_DATA *p_data;
487         int             i, key_index;
488         
489         printers_key = strip_printers_prefix( key );    
490         
491         /* top level key values stored in the registry has no values */
492         
493         if ( !printers_key ) {
494                 /* normalize to the 'HKLM\SOFTWARE\...\Print\Printers' key */
495                 return regdb_fetch_values( KEY_WINNT_PRINTERS, values );
496         }
497         
498         /* lookup the printer object */
499         
500         if (!reg_split_path( printers_key, &printername, &printerdatakey )) {
501                 return -1;
502         }
503         
504         if ( !W_ERROR_IS_OK( get_a_printer(NULL, &printer, 2, printername) ) )
505                 goto done;
506                 
507         if ( !printerdatakey ) {
508                 fill_in_printer_values( printer->info_2, values );
509                 goto done;
510         }
511                 
512         /* iterate over all printer data keys and fill the regval container */
513         
514         p_data = printer->info_2->data;
515         if ( (key_index = lookup_printerkey( p_data, printerdatakey )) == -1  ) {
516                 /* failure....should never happen if the client has a valid open handle first */
517                 DEBUG(10,("key_printers_fetch_values: Unknown keyname [%s]\n", printerdatakey));
518                 free_a_printer( &printer, 2 );
519                 return -1;
520         }
521         
522         num_values = regval_ctr_numvals( p_data->keys[key_index].values );      
523         for ( i=0; i<num_values; i++ )
524                 regval_ctr_copyvalue( values, regval_ctr_specific_value(p_data->keys[key_index].values, i) );
525                         
526
527 done:
528         if ( printer )
529                 free_a_printer( &printer, 2 );
530                 
531         return regval_ctr_numvals( values );
532 }
533
534 /**********************************************************************
535  *********************************************************************/
536
537 #define REG_IDX_ATTRIBUTES              1
538 #define REG_IDX_PRIORITY                2
539 #define REG_IDX_DEFAULT_PRIORITY        3
540 #define REG_IDX_CHANGEID                4
541 #define REG_IDX_STATUS                  5
542 #define REG_IDX_STARTTIME               6
543 #define REG_IDX_NAME                    7
544 #define REG_IDX_LOCATION                8
545 #define REG_IDX_DESCRIPTION             9
546 #define REG_IDX_PARAMETERS              10
547 #define REG_IDX_PORT                    12
548 #define REG_IDX_SHARENAME               13
549 #define REG_IDX_DRIVER                  14
550 #define REG_IDX_SEP_FILE                15
551 #define REG_IDX_PRINTPROC               16
552 #define REG_IDX_DATATYPE                17
553 #define REG_IDX_DEVMODE                 18
554 #define REG_IDX_SECDESC                 19
555 #define REG_IDX_UNTILTIME               20
556
557 struct {
558         const char *name;
559         int index;      
560 } printer_values_map[] = {
561         { "Attributes",         REG_IDX_ATTRIBUTES },
562         { "Priority",           REG_IDX_PRIORITY },
563         { "Default Priority",   REG_IDX_DEFAULT_PRIORITY },
564         { "ChangeID",           REG_IDX_CHANGEID },
565         { "Status",             REG_IDX_STATUS },
566         { "StartTime",          REG_IDX_STARTTIME },
567         { "UntilTime",          REG_IDX_UNTILTIME },
568         { "Name",               REG_IDX_NAME },
569         { "Location",           REG_IDX_LOCATION },
570         { "Description",        REG_IDX_DESCRIPTION },
571         { "Parameters",         REG_IDX_PARAMETERS },
572         { "Port",               REG_IDX_PORT },
573         { "Share Name",         REG_IDX_SHARENAME },
574         { "Printer Driver",     REG_IDX_DRIVER },
575         { "Separator File",     REG_IDX_SEP_FILE },
576         { "Print Processor",    REG_IDX_PRINTPROC },
577         { "Datatype",           REG_IDX_DATATYPE },
578         { "Default Devmode",    REG_IDX_DEVMODE },
579         { "Security",           REG_IDX_SECDESC },
580         { NULL, -1 }
581 };
582
583
584 static int find_valuename_index( const char *valuename )
585 {
586         int i;
587         
588         for ( i=0; printer_values_map[i].name; i++ ) {
589                 if ( strequal( valuename, printer_values_map[i].name ) )
590                         return printer_values_map[i].index;
591         }
592         
593         return -1;
594 }
595
596 /**********************************************************************
597  *********************************************************************/
598
599 static void convert_values_to_printer_info_2( NT_PRINTER_INFO_LEVEL_2 *printer2, REGVAL_CTR *values )
600 {
601         int num_values = regval_ctr_numvals( values );
602         uint32 value_index;
603         REGISTRY_VALUE *val;
604         int i;
605         
606         for ( i=0; i<num_values; i++ ) {
607                 val = regval_ctr_specific_value( values, i );
608                 value_index = find_valuename_index( regval_name( val ) );
609                 
610                 switch( value_index ) {
611                         case REG_IDX_ATTRIBUTES:
612                                 printer2->attributes = (uint32)(*regval_data_p(val));
613                                 break;
614                         case REG_IDX_PRIORITY:
615                                 printer2->priority = (uint32)(*regval_data_p(val));
616                                 break;
617                         case REG_IDX_DEFAULT_PRIORITY:
618                                 printer2->default_priority = (uint32)(*regval_data_p(val));
619                                 break;
620                         case REG_IDX_CHANGEID:
621                                 printer2->changeid = (uint32)(*regval_data_p(val));
622                                 break;
623                         case REG_IDX_STARTTIME:
624                                 printer2->starttime = (uint32)(*regval_data_p(val));
625                                 break;
626                         case REG_IDX_UNTILTIME:
627                                 printer2->untiltime = (uint32)(*regval_data_p(val));
628                                 break;
629                         case REG_IDX_NAME:
630                                 rpcstr_pull( printer2->printername, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
631                                 break;
632                         case REG_IDX_LOCATION:
633                                 rpcstr_pull( printer2->location, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
634                                 break;
635                         case REG_IDX_DESCRIPTION:
636                                 rpcstr_pull( printer2->comment, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
637                                 break;
638                         case REG_IDX_PARAMETERS:
639                                 rpcstr_pull( printer2->parameters, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
640                                 break;
641                         case REG_IDX_PORT:
642                                 rpcstr_pull( printer2->portname, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
643                                 break;
644                         case REG_IDX_SHARENAME:
645                                 rpcstr_pull( printer2->sharename, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
646                                 break;
647                         case REG_IDX_DRIVER:
648                                 rpcstr_pull( printer2->drivername, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
649                                 break;
650                         case REG_IDX_SEP_FILE:
651                                 rpcstr_pull( printer2->sepfile, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
652                                 break;
653                         case REG_IDX_PRINTPROC:
654                                 rpcstr_pull( printer2->printprocessor, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
655                                 break;
656                         case REG_IDX_DATATYPE:
657                                 rpcstr_pull( printer2->datatype, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
658                                 break;
659                         case REG_IDX_DEVMODE:
660                                 break;
661                         case REG_IDX_SECDESC:
662                                 break;          
663                         default:
664                                 /* unsupported value...throw away */
665                                 DEBUG(8,("convert_values_to_printer_info_2: Unsupported registry value [%s]\n", 
666                                         regval_name( val ) ));
667                 }
668         }
669         
670         return;
671 }       
672
673 /**********************************************************************
674  *********************************************************************/
675
676 static bool key_printers_store_values( const char *key, REGVAL_CTR *values )
677 {
678         char *printers_key;
679         char *printername, *keyname;
680         NT_PRINTER_INFO_LEVEL   *printer = NULL;
681         WERROR result;
682         
683         printers_key = strip_printers_prefix( key );
684         
685         /* values in the top level key get stored in the registry */
686
687         if ( !printers_key ) {
688                 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
689                 return regdb_store_values( KEY_WINNT_PRINTERS, values );
690         }
691         
692         if (!reg_split_path( printers_key, &printername, &keyname )) {
693                 return False;
694         }
695
696         if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, printername) ) )
697                 return False;
698
699         /* deal with setting values directly under the printername */
700
701         if ( !keyname ) {
702                 convert_values_to_printer_info_2( printer->info_2, values );
703         }
704         else {
705                 int num_values = regval_ctr_numvals( values );
706                 int i;
707                 REGISTRY_VALUE *val;
708                 
709                 delete_printer_key( printer->info_2->data, keyname );
710                 
711                 /* deal with any subkeys */
712                 for ( i=0; i<num_values; i++ ) {
713                         val = regval_ctr_specific_value( values, i );
714                         result = set_printer_dataex( printer, keyname, 
715                                 regval_name( val ),
716                                 regval_type( val ),
717                                 regval_data_p( val ),
718                                 regval_size( val ) );
719                         if ( !W_ERROR_IS_OK(result) ) {
720                                 DEBUG(0,("key_printers_store_values: failed to set printer data [%s]!\n",
721                                         keyname));
722                                 free_a_printer( &printer, 2 );
723                                 return False;
724                         }
725                 }
726         }
727
728         result = mod_a_printer( printer, 2 );
729
730         free_a_printer( &printer, 2 );
731
732         return W_ERROR_IS_OK(result);
733 }
734
735 /*********************************************************************
736  *********************************************************************
737  ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/ENVIRONMENTS"
738  *********************************************************************
739  *********************************************************************/
740
741 static int key_driver_fetch_keys( const char *key, REGSUBKEY_CTR *subkeys )
742 {
743         const char *environments[] = {
744                 "Windows 4.0",
745                 "Windows NT x86",
746                 "Windows NT R4000",
747                 "Windows NT Alpha_AXP",
748                 "Windows NT PowerPC",
749                 "Windows IA64",
750                 "Windows x64",
751                 NULL };
752         fstring *drivers = NULL;
753         int i, env_index, num_drivers;
754         char *keystr, *base, *subkeypath;
755         char *key2 = NULL;
756         int num_subkeys = -1;
757         int version;
758         TALLOC_CTX *ctx = talloc_tos();
759
760         DEBUG(10,("key_driver_fetch_keys key=>[%s]\n", key ? key : "NULL" ));
761
762         keystr = reg_remaining_path(ctx, key + strlen(KEY_ENVIRONMENTS) );
763
764         /* list all possible architectures */
765
766         if ( !keystr ) {
767                 for ( num_subkeys=0; environments[num_subkeys]; num_subkeys++ )
768                         regsubkey_ctr_addkey( subkeys,  environments[num_subkeys] );
769
770                 return num_subkeys;
771         }
772
773         /* we are dealing with a subkey of "Environments */
774         key2 = talloc_strdup(ctx, keystr);
775         if (!key2) {
776                 return -1;
777         }
778         keystr = key2;
779         if (!reg_split_path(keystr, &base, &subkeypath )) {
780                 return -1;
781         }
782
783         /* sanity check */
784
785         for ( env_index=0; environments[env_index]; env_index++ ) {
786                 if ( strequal( environments[env_index], base ) )
787                         break;
788         }
789         if ( !environments[env_index] )
790                 return -1;
791
792         /* ...\Print\Environements\...\ */
793
794         if ( !subkeypath ) {
795                 regsubkey_ctr_addkey( subkeys, "Drivers" );
796                 regsubkey_ctr_addkey( subkeys, "Print Processors" );
797
798                 return 2;
799         }
800
801         /* more of the key path to process */
802
803         keystr = subkeypath;
804         if (!reg_split_path( keystr, &base, &subkeypath )) {
805                 return -1;
806         }
807
808         /* ...\Print\Environements\...\Drivers\ */
809
810         if ( !subkeypath ) {
811                 if ( strequal(base, "Drivers") ) {
812                         switch ( env_index ) {
813                                 case 0: /* Win9x */
814                                         regsubkey_ctr_addkey( subkeys, "Version-0" );
815                                         break;
816                                 default: /* Windows NT based systems */
817                                         regsubkey_ctr_addkey( subkeys, "Version-2" );
818                                         regsubkey_ctr_addkey( subkeys, "Version-3" );
819                                         break;
820                         }
821
822                         return regsubkey_ctr_numkeys( subkeys );
823                 } else if ( strequal(base, "Print Processors") ) {
824                         if ( env_index == 1 || env_index == 5 || env_index == 6 )
825
826
827                         return regsubkey_ctr_numkeys( subkeys );
828                 } else
829                         return -1;      /* bad path */
830         }
831
832         /* we finally get to enumerate the drivers */
833
834         /* only one possible subkey below PrintProc key */
835
836         if ( strequal(base, "Print Processors") ) {
837                 keystr = subkeypath;
838                 if (!reg_split_path( keystr, &base, &subkeypath )) {
839                         return -1;
840                 }
841
842                 /* no subkeys below this point */
843
844                 if ( subkeypath )
845                         return -1;
846
847                 /* only allow one keyname here -- 'winprint' */
848
849                 return strequal( base, "winprint" ) ? 0 : -1;
850         }
851
852         /* only dealing with drivers from here on out */
853
854         keystr = subkeypath;
855         if (!reg_split_path( keystr, &base, &subkeypath )) {
856                 return -1;
857         }
858
859         version = atoi(&base[strlen(base)-1]);
860
861         switch (env_index) {
862         case 0:
863                 if ( version != 0 )
864                         return -1;
865                 break;
866         default:
867                 if ( version != 2 && version != 3 )
868                         return -1;
869                 break;
870         }
871
872
873         if ( !subkeypath ) {
874                 num_drivers = get_ntdrivers( &drivers, environments[env_index], version );
875                 for ( i=0; i<num_drivers; i++ )
876                         regsubkey_ctr_addkey( subkeys, drivers[i] );
877
878                 return regsubkey_ctr_numkeys( subkeys );
879         }
880
881         /* if anything else left, just say if has no subkeys */
882
883         DEBUG(1,("key_driver_fetch_keys unhandled key [%s] (subkey == %s\n",
884                 key, subkeypath ));
885
886         return 0;
887 }
888
889
890 /**********************************************************************
891  *********************************************************************/
892
893 static void fill_in_driver_values( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3, REGVAL_CTR *values )
894 {
895         char *buffer = NULL;
896         int buffer_size = 0;
897         int i, length;
898         const char *filename;
899         UNISTR2 data;
900
901         filename = dos_basename( info3->driverpath );
902         init_unistr2( &data, filename, UNI_STR_TERMINATE);
903         regval_ctr_addvalue( values, "Driver", REG_SZ, (char*)data.buffer,
904                 data.uni_str_len*sizeof(uint16) );
905
906         filename = dos_basename( info3->configfile );
907         init_unistr2( &data, filename, UNI_STR_TERMINATE);
908         regval_ctr_addvalue( values, "Configuration File", REG_SZ, (char*)data.buffer, 
909                 data.uni_str_len*sizeof(uint16) );
910
911         filename = dos_basename( info3->datafile );
912         init_unistr2( &data, filename, UNI_STR_TERMINATE);
913         regval_ctr_addvalue( values, "Data File", REG_SZ, (char*)data.buffer,
914                 data.uni_str_len*sizeof(uint16) );
915
916         filename = dos_basename( info3->helpfile );
917         init_unistr2( &data, filename, UNI_STR_TERMINATE);
918         regval_ctr_addvalue( values, "Help File", REG_SZ, (char*)data.buffer,
919                 data.uni_str_len*sizeof(uint16) );
920
921         init_unistr2( &data, info3->defaultdatatype, UNI_STR_TERMINATE);
922         regval_ctr_addvalue( values, "Data Type", REG_SZ, (char*)data.buffer,
923                 data.uni_str_len*sizeof(uint16) );
924
925         regval_ctr_addvalue( values, "Version", REG_DWORD, (char*)&info3->cversion, 
926                 sizeof(info3->cversion) );
927
928         if ( info3->dependentfiles ) {
929                 /* place the list of dependent files in a single
930                    character buffer, separating each file name by
931                    a NULL */
932
933                 for ( i=0; strcmp(info3->dependentfiles[i], ""); i++ ) {
934                         /* strip the path to only the file's base name */
935
936                         filename = dos_basename( info3->dependentfiles[i] );
937
938                         length = strlen(filename);
939
940                         buffer = (char *)SMB_REALLOC( buffer, buffer_size + (length + 1)*sizeof(uint16) );
941                         if ( !buffer ) {
942                                 break;
943                         }
944
945                         init_unistr2( &data, filename, UNI_STR_TERMINATE);
946                         memcpy( buffer+buffer_size, (char*)data.buffer, data.uni_str_len*sizeof(uint16) );
947
948                         buffer_size += (length + 1)*sizeof(uint16);
949                 }
950
951                 /* terminated by double NULL.  Add the final one here */
952
953                 buffer = (char *)SMB_REALLOC( buffer, buffer_size + 2 );
954                 if ( !buffer ) {
955                         buffer_size = 0;
956                 } else {
957                         buffer[buffer_size++] = '\0';
958                         buffer[buffer_size++] = '\0';
959                 }
960         }
961
962         regval_ctr_addvalue( values, "Dependent Files",    REG_MULTI_SZ, buffer, buffer_size );
963
964         SAFE_FREE( buffer );
965
966         return;
967 }
968
969 /**********************************************************************
970  *********************************************************************/
971
972 static int driver_arch_fetch_values( char *key, REGVAL_CTR *values )
973 {
974         char            *keystr, *base, *subkeypath;
975         fstring         arch_environment;
976         fstring         driver;
977         int             version;
978         NT_PRINTER_DRIVER_INFO_LEVEL    driver_ctr;
979         WERROR          w_result;
980
981         if (!reg_split_path( key, &base, &subkeypath )) {
982                 return -1;
983         }
984
985         /* no values in 'Environments\Drivers\Windows NT x86' */
986
987         if ( !subkeypath )
988                 return 0;
989
990         /* We have the Architecture string and some subkey name:
991            Currently we only support
992            * Drivers
993            * Print Processors
994            Anything else is an error.
995            */
996
997         fstrcpy( arch_environment, base );
998
999         keystr = subkeypath;
1000         if (!reg_split_path( keystr, &base, &subkeypath )) {
1001                 return -1;
1002         }
1003
1004         if ( strequal(base, "Print Processors") )
1005                 return 0;
1006
1007         /* only Drivers key can be left */
1008
1009         if ( !strequal(base, "Drivers") )
1010                 return -1;
1011
1012         if ( !subkeypath )
1013                 return 0;
1014
1015         /* We know that we have Architechure\Drivers with some subkey name
1016            The subkey name has to be Version-XX */
1017
1018         keystr = subkeypath;
1019         if (!reg_split_path( keystr, &base, &subkeypath )) {
1020                 return -1;
1021         }
1022
1023         if ( !subkeypath )
1024                 return 0;
1025
1026         version = atoi(&base[strlen(base)-1]);
1027
1028         /* BEGIN PRINTER DRIVER NAME BLOCK */
1029
1030         keystr = subkeypath;
1031         if (!reg_split_path( keystr, &base, &subkeypath )) {
1032                 return -1;
1033         }
1034
1035         /* don't go any deeper for now */
1036
1037         fstrcpy( driver, base );
1038
1039         w_result = get_a_printer_driver( &driver_ctr, 3, driver, arch_environment, version );
1040
1041         if ( !W_ERROR_IS_OK(w_result) )
1042                 return -1;
1043
1044         fill_in_driver_values( driver_ctr.info_3, values );
1045
1046         free_a_printer_driver( driver_ctr, 3 );
1047
1048         /* END PRINTER DRIVER NAME BLOCK */
1049
1050
1051         DEBUG(8,("key_driver_fetch_values: Exit\n"));
1052
1053         return regval_ctr_numvals( values );
1054 }
1055
1056 /**********************************************************************
1057  *********************************************************************/
1058
1059 static int key_driver_fetch_values( const char *key, REGVAL_CTR *values )
1060 {
1061         char *keystr = NULL;
1062         char *subkey = NULL;
1063         TALLOC_CTX *ctx = talloc_tos();
1064
1065         DEBUG(8,("key_driver_fetch_values: Enter key => [%s]\n", key ? key : "NULL"));
1066
1067         /* no values in the Environments key */
1068
1069         if (!(keystr = reg_remaining_path(ctx, key + strlen(KEY_ENVIRONMENTS))))
1070                 return 0;
1071
1072         subkey = talloc_strdup(ctx, keystr);
1073         if (!subkey) {
1074                 return 0;
1075         }
1076
1077         /* pass off to handle subkeys */
1078
1079         return driver_arch_fetch_values( subkey, values );
1080 }
1081
1082 /*********************************************************************
1083  *********************************************************************
1084  ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT"
1085  *********************************************************************
1086  *********************************************************************/
1087
1088 static int key_print_fetch_keys( const char *key, REGSUBKEY_CTR *subkeys )
1089 {
1090         int key_len = strlen(key);
1091
1092         /* no keys below 'Print' handled here */
1093
1094         if ( (key_len != strlen(KEY_CONTROL_PRINT)) && (key_len != strlen(KEY_WINNT_PRINT)) )
1095                 return -1;
1096
1097         regsubkey_ctr_addkey( subkeys, "Environments" );
1098         regsubkey_ctr_addkey( subkeys, "Monitors" );
1099         regsubkey_ctr_addkey( subkeys, "Forms" );
1100         regsubkey_ctr_addkey( subkeys, "Printers" );
1101
1102         return regsubkey_ctr_numkeys( subkeys );
1103 }
1104
1105 /**********************************************************************
1106  *********************************************************************
1107  ** Structure to hold dispatch table of ops for various printer keys.
1108  ** Make sure to always store deeper keys along the same path first so
1109  ** we ge a more specific match.
1110  *********************************************************************
1111  *********************************************************************/
1112
1113 static struct reg_dyn_tree print_registry[] = {
1114 /* just pass the monitor onto the registry tdb */
1115 { KEY_MONITORS,
1116         &regdb_fetch_keys,
1117         &regdb_store_keys,
1118         &regdb_fetch_values,
1119         &regdb_store_values },
1120 { KEY_FORMS,
1121         &key_forms_fetch_keys,
1122         NULL,
1123         &key_forms_fetch_values,
1124         NULL },
1125 { KEY_CONTROL_PRINTERS,
1126         &key_printers_fetch_keys,
1127         &key_printers_store_keys,
1128         &key_printers_fetch_values,
1129         &key_printers_store_values },
1130 { KEY_ENVIRONMENTS,
1131         &key_driver_fetch_keys,
1132         NULL,
1133         &key_driver_fetch_values,
1134         NULL },
1135 { KEY_CONTROL_PRINT,
1136         &key_print_fetch_keys,
1137         NULL,
1138         NULL,
1139         NULL },
1140 { KEY_WINNT_PRINTERS,
1141         &key_printers_fetch_keys,
1142         &key_printers_store_keys,
1143         &key_printers_fetch_values,
1144         &key_printers_store_values },
1145 { KEY_PORTS,
1146         &regdb_fetch_keys,
1147         &regdb_store_keys,
1148         &regdb_fetch_values,
1149         &regdb_store_values },
1150
1151 { NULL, NULL, NULL, NULL, NULL }
1152 };
1153
1154
1155 /**********************************************************************
1156  *********************************************************************
1157  ** Main reg_printing interface functions
1158  *********************************************************************
1159  *********************************************************************/
1160
1161 /***********************************************************************
1162  Lookup a key in the print_registry table, returning its index.
1163  -1 on failure
1164  **********************************************************************/
1165
1166 static int match_registry_path(const char *key)
1167 {
1168         int i;
1169         char *path = NULL;
1170         TALLOC_CTX *ctx = talloc_tos();
1171
1172         if ( !key )
1173                 return -1;
1174
1175         path = talloc_strdup(ctx, key);
1176         if (!path) {
1177                 return -1;
1178         }
1179         path = normalize_reg_path(ctx, path);
1180         if (!path) {
1181                 return -1;
1182         }
1183
1184         for ( i=0; print_registry[i].path; i++ ) {
1185                 if (strncmp( path, print_registry[i].path, strlen(print_registry[i].path) ) == 0 )
1186                         return i;
1187         }
1188
1189         return -1;
1190 }
1191
1192 /***********************************************************************
1193  **********************************************************************/
1194
1195 static int regprint_fetch_reg_keys( const char *key, REGSUBKEY_CTR *subkeys )
1196 {
1197         int i = match_registry_path( key );
1198
1199         if ( i == -1 )
1200                 return -1;
1201
1202         if ( !print_registry[i].fetch_subkeys )
1203                 return -1;
1204
1205         return print_registry[i].fetch_subkeys( key, subkeys );
1206 }
1207
1208 /**********************************************************************
1209  *********************************************************************/
1210
1211 static bool regprint_store_reg_keys( const char *key, REGSUBKEY_CTR *subkeys )
1212 {
1213         int i = match_registry_path( key );
1214
1215         if ( i == -1 )
1216                 return False;
1217
1218         if ( !print_registry[i].store_subkeys )
1219                 return False;
1220
1221         return print_registry[i].store_subkeys( key, subkeys );
1222 }
1223
1224 /**********************************************************************
1225  *********************************************************************/
1226
1227 static int regprint_fetch_reg_values( const char *key, REGVAL_CTR *values )
1228 {
1229         int i = match_registry_path( key );
1230
1231         if ( i == -1 )
1232                 return -1;
1233
1234         /* return 0 values by default since we know the key had
1235            to exist because the client opened a handle */
1236
1237         if ( !print_registry[i].fetch_values )
1238                 return 0;
1239
1240         return print_registry[i].fetch_values( key, values );
1241 }
1242
1243 /**********************************************************************
1244  *********************************************************************/
1245
1246 static bool regprint_store_reg_values( const char *key, REGVAL_CTR *values )
1247 {
1248         int i = match_registry_path( key );
1249
1250         if ( i == -1 )
1251                 return False;
1252
1253         if ( !print_registry[i].store_values )
1254                 return False;
1255
1256         return print_registry[i].store_values( key, values );
1257 }
1258
1259 /*
1260  * Table of function pointers for accessing printing data
1261  */
1262
1263 REGISTRY_OPS printing_ops = {
1264         .fetch_subkeys = regprint_fetch_reg_keys,
1265         .fetch_values = regprint_fetch_reg_values,
1266         .store_subkeys = regprint_store_reg_keys,
1267         .store_values = regprint_store_reg_values,
1268 };