cc44f877bba68bfcf4a1cf23cd5dd47aa220048b
[ira/wip.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, struct regsubkey_ctr *subkeys );
46         bool (*store_subkeys) ( const char *path, struct regsubkey_ctr *subkeys );
47         int  (*fetch_values)  ( const char *path, struct regval_ctr *values );
48         bool (*store_values)  ( const char *path, struct 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, struct 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, struct 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, struct 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( struct 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, struct 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, struct regval_ctr *values)
387 {
388         struct spoolss_DeviceMode *devmode;
389         char            *p;
390         uint32 printer_status = PRINTER_STATUS_OK;
391         
392         regval_ctr_addvalue( values, "Attributes",       REG_DWORD, (char*)&info2->attributes,       sizeof(info2->attributes) );
393         regval_ctr_addvalue( values, "Priority",         REG_DWORD, (char*)&info2->priority,         sizeof(info2->attributes) );
394         regval_ctr_addvalue( values, "ChangeID",         REG_DWORD, (char*)&info2->changeid,         sizeof(info2->changeid) );
395         regval_ctr_addvalue( values, "Default Priority", REG_DWORD, (char*)&info2->default_priority, sizeof(info2->default_priority) );
396         
397         /* lie and say everything is ok since we don't want to call print_queue_length() to get the real status */
398         regval_ctr_addvalue( values, "Status",           REG_DWORD, (char*)&printer_status,          sizeof(info2->status) );
399
400         regval_ctr_addvalue( values, "StartTime",        REG_DWORD, (char*)&info2->starttime,        sizeof(info2->starttime) );
401         regval_ctr_addvalue( values, "UntilTime",        REG_DWORD, (char*)&info2->untiltime,        sizeof(info2->untiltime) );
402
403         /* strip the \\server\ from this string */
404         if ( !(p = strrchr( info2->printername, '\\' ) ) )
405                 p = info2->printername;
406         else
407                 p++;
408
409         regval_ctr_addvalue_sz(values, "Name", p);
410         regval_ctr_addvalue_sz(values, "Location", info2->location);
411         regval_ctr_addvalue_sz(values, "Description", info2->comment);
412         regval_ctr_addvalue_sz(values, "Parameters", info2->parameters);
413         regval_ctr_addvalue_sz(values, "Port", info2->portname);
414         regval_ctr_addvalue_sz(values, "Share Name", info2->sharename);
415         regval_ctr_addvalue_sz(values, "Printer Driver", info2->drivername);
416         regval_ctr_addvalue_sz(values, "Separator File", info2->sepfile);
417         regval_ctr_addvalue_sz(values, "Print Processor", "WinPrint");
418         regval_ctr_addvalue_sz(values, "Datatype", "RAW");
419
420         /* stream the device mode */
421
422         devmode = construct_dev_mode(values,info2->sharename);
423         if (devmode) {
424                 DATA_BLOB blob;
425                 enum ndr_err_code ndr_err;
426
427                 ndr_err = ndr_push_struct_blob(&blob, values, NULL, devmode,
428                                 (ndr_push_flags_fn_t)ndr_push_spoolss_DeviceMode);
429
430                 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
431                         regval_ctr_addvalue(values, "Default Devmode", REG_BINARY,
432                                             (const char *)blob.data, blob.length);
433                 }
434         }
435
436         /* stream the printer security descriptor */
437
438         if (info2->secdesc_buf &&
439             info2->secdesc_buf->sd &&
440             info2->secdesc_buf->sd_size)
441         {
442                 NTSTATUS status;
443                 DATA_BLOB blob;
444
445                 status = marshall_sec_desc(values, info2->secdesc_buf->sd,
446                                            &blob.data, &blob.length);
447                 if (NT_STATUS_IS_OK(status)) {
448                         regval_ctr_addvalue(values, "Security", REG_BINARY,
449                                             (const char *)blob.data, blob.length);
450                 }
451         }
452
453         return;
454 }
455
456 /**********************************************************************
457  *********************************************************************/
458
459 static int key_printers_fetch_values(const char *key, struct regval_ctr *values)
460 {
461         int             num_values;
462         char            *printers_key;
463         char            *printername, *printerdatakey;
464         NT_PRINTER_INFO_LEVEL   *printer = NULL;
465         NT_PRINTER_DATA *p_data;
466         int             i, key_index;
467         
468         printers_key = strip_printers_prefix( key );    
469         
470         /* top level key values stored in the registry has no values */
471         
472         if ( !printers_key ) {
473                 /* normalize to the 'HKLM\SOFTWARE\...\Print\Printers' key */
474                 return regdb_fetch_values( KEY_WINNT_PRINTERS, values );
475         }
476         
477         /* lookup the printer object */
478         
479         if (!reg_split_path( printers_key, &printername, &printerdatakey )) {
480                 return -1;
481         }
482         
483         if ( !W_ERROR_IS_OK( get_a_printer(NULL, &printer, 2, printername) ) )
484                 goto done;
485                 
486         if ( !printerdatakey ) {
487                 fill_in_printer_values( printer->info_2, values );
488                 goto done;
489         }
490                 
491         /* iterate over all printer data keys and fill the regval container */
492         
493         p_data = printer->info_2->data;
494         if ( (key_index = lookup_printerkey( p_data, printerdatakey )) == -1  ) {
495                 /* failure....should never happen if the client has a valid open handle first */
496                 DEBUG(10,("key_printers_fetch_values: Unknown keyname [%s]\n", printerdatakey));
497                 free_a_printer( &printer, 2 );
498                 return -1;
499         }
500         
501         num_values = regval_ctr_numvals( p_data->keys[key_index].values );      
502         for ( i=0; i<num_values; i++ )
503                 regval_ctr_copyvalue( values, regval_ctr_specific_value(p_data->keys[key_index].values, i) );
504                         
505
506 done:
507         if ( printer )
508                 free_a_printer( &printer, 2 );
509                 
510         return regval_ctr_numvals( values );
511 }
512
513 /**********************************************************************
514  *********************************************************************/
515
516 #define REG_IDX_ATTRIBUTES              1
517 #define REG_IDX_PRIORITY                2
518 #define REG_IDX_DEFAULT_PRIORITY        3
519 #define REG_IDX_CHANGEID                4
520 #define REG_IDX_STATUS                  5
521 #define REG_IDX_STARTTIME               6
522 #define REG_IDX_NAME                    7
523 #define REG_IDX_LOCATION                8
524 #define REG_IDX_DESCRIPTION             9
525 #define REG_IDX_PARAMETERS              10
526 #define REG_IDX_PORT                    12
527 #define REG_IDX_SHARENAME               13
528 #define REG_IDX_DRIVER                  14
529 #define REG_IDX_SEP_FILE                15
530 #define REG_IDX_PRINTPROC               16
531 #define REG_IDX_DATATYPE                17
532 #define REG_IDX_DEVMODE                 18
533 #define REG_IDX_SECDESC                 19
534 #define REG_IDX_UNTILTIME               20
535
536 struct {
537         const char *name;
538         int index;      
539 } printer_values_map[] = {
540         { "Attributes",         REG_IDX_ATTRIBUTES },
541         { "Priority",           REG_IDX_PRIORITY },
542         { "Default Priority",   REG_IDX_DEFAULT_PRIORITY },
543         { "ChangeID",           REG_IDX_CHANGEID },
544         { "Status",             REG_IDX_STATUS },
545         { "StartTime",          REG_IDX_STARTTIME },
546         { "UntilTime",          REG_IDX_UNTILTIME },
547         { "Name",               REG_IDX_NAME },
548         { "Location",           REG_IDX_LOCATION },
549         { "Description",        REG_IDX_DESCRIPTION },
550         { "Parameters",         REG_IDX_PARAMETERS },
551         { "Port",               REG_IDX_PORT },
552         { "Share Name",         REG_IDX_SHARENAME },
553         { "Printer Driver",     REG_IDX_DRIVER },
554         { "Separator File",     REG_IDX_SEP_FILE },
555         { "Print Processor",    REG_IDX_PRINTPROC },
556         { "Datatype",           REG_IDX_DATATYPE },
557         { "Default Devmode",    REG_IDX_DEVMODE },
558         { "Security",           REG_IDX_SECDESC },
559         { NULL, -1 }
560 };
561
562
563 static int find_valuename_index( const char *valuename )
564 {
565         int i;
566         
567         for ( i=0; printer_values_map[i].name; i++ ) {
568                 if ( strequal( valuename, printer_values_map[i].name ) )
569                         return printer_values_map[i].index;
570         }
571         
572         return -1;
573 }
574
575 /**********************************************************************
576  *********************************************************************/
577
578 static void convert_values_to_printer_info_2(NT_PRINTER_INFO_LEVEL_2 *printer2, struct regval_ctr *values)
579 {
580         int num_values = regval_ctr_numvals( values );
581         uint32 value_index;
582         struct regval_blob *val;
583         int i;
584         
585         for ( i=0; i<num_values; i++ ) {
586                 val = regval_ctr_specific_value( values, i );
587                 value_index = find_valuename_index( regval_name( val ) );
588                 
589                 switch( value_index ) {
590                         case REG_IDX_ATTRIBUTES:
591                                 printer2->attributes = (uint32)(*regval_data_p(val));
592                                 break;
593                         case REG_IDX_PRIORITY:
594                                 printer2->priority = (uint32)(*regval_data_p(val));
595                                 break;
596                         case REG_IDX_DEFAULT_PRIORITY:
597                                 printer2->default_priority = (uint32)(*regval_data_p(val));
598                                 break;
599                         case REG_IDX_CHANGEID:
600                                 printer2->changeid = (uint32)(*regval_data_p(val));
601                                 break;
602                         case REG_IDX_STARTTIME:
603                                 printer2->starttime = (uint32)(*regval_data_p(val));
604                                 break;
605                         case REG_IDX_UNTILTIME:
606                                 printer2->untiltime = (uint32)(*regval_data_p(val));
607                                 break;
608                         case REG_IDX_NAME:
609                                 rpcstr_pull( printer2->printername, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
610                                 break;
611                         case REG_IDX_LOCATION:
612                                 rpcstr_pull( printer2->location, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
613                                 break;
614                         case REG_IDX_DESCRIPTION:
615                                 rpcstr_pull( printer2->comment, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
616                                 break;
617                         case REG_IDX_PARAMETERS:
618                                 rpcstr_pull( printer2->parameters, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
619                                 break;
620                         case REG_IDX_PORT:
621                                 rpcstr_pull( printer2->portname, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
622                                 break;
623                         case REG_IDX_SHARENAME:
624                                 rpcstr_pull( printer2->sharename, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
625                                 break;
626                         case REG_IDX_DRIVER:
627                                 rpcstr_pull( printer2->drivername, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
628                                 break;
629                         case REG_IDX_SEP_FILE:
630                                 rpcstr_pull( printer2->sepfile, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
631                                 break;
632                         case REG_IDX_PRINTPROC:
633                                 rpcstr_pull( printer2->printprocessor, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
634                                 break;
635                         case REG_IDX_DATATYPE:
636                                 rpcstr_pull( printer2->datatype, regval_data_p(val), sizeof(fstring), regval_size(val), 0 );
637                                 break;
638                         case REG_IDX_DEVMODE:
639                                 break;
640                         case REG_IDX_SECDESC:
641                                 break;          
642                         default:
643                                 /* unsupported value...throw away */
644                                 DEBUG(8,("convert_values_to_printer_info_2: Unsupported registry value [%s]\n", 
645                                         regval_name( val ) ));
646                 }
647         }
648         
649         return;
650 }       
651
652 /**********************************************************************
653  *********************************************************************/
654
655 static bool key_printers_store_values(const char *key, struct regval_ctr *values)
656 {
657         char *printers_key;
658         char *printername, *keyname;
659         NT_PRINTER_INFO_LEVEL   *printer = NULL;
660         WERROR result;
661         
662         printers_key = strip_printers_prefix( key );
663         
664         /* values in the top level key get stored in the registry */
665
666         if ( !printers_key ) {
667                 /* normalize on the 'HKLM\SOFTWARE\....\Print\Printers' key */
668                 return regdb_store_values( KEY_WINNT_PRINTERS, values );
669         }
670         
671         if (!reg_split_path( printers_key, &printername, &keyname )) {
672                 return False;
673         }
674
675         if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, printername) ) )
676                 return False;
677
678         /* deal with setting values directly under the printername */
679
680         if ( !keyname ) {
681                 convert_values_to_printer_info_2( printer->info_2, values );
682         }
683         else {
684                 int num_values = regval_ctr_numvals( values );
685                 int i;
686                 struct regval_blob *val;
687                 
688                 delete_printer_key( printer->info_2->data, keyname );
689                 
690                 /* deal with any subkeys */
691                 for ( i=0; i<num_values; i++ ) {
692                         val = regval_ctr_specific_value( values, i );
693                         result = set_printer_dataex( printer, keyname, 
694                                 regval_name( val ),
695                                 regval_type( val ),
696                                 regval_data_p( val ),
697                                 regval_size( val ) );
698                         if ( !W_ERROR_IS_OK(result) ) {
699                                 DEBUG(0,("key_printers_store_values: failed to set printer data [%s]!\n",
700                                         keyname));
701                                 free_a_printer( &printer, 2 );
702                                 return False;
703                         }
704                 }
705         }
706
707         result = mod_a_printer( printer, 2 );
708
709         free_a_printer( &printer, 2 );
710
711         return W_ERROR_IS_OK(result);
712 }
713
714 /*********************************************************************
715  *********************************************************************
716  ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT/ENVIRONMENTS"
717  *********************************************************************
718  *********************************************************************/
719
720 static int key_driver_fetch_keys( const char *key, struct regsubkey_ctr *subkeys )
721 {
722         const char *environments[] = {
723                 "Windows 4.0",
724                 "Windows NT x86",
725                 "Windows NT R4000",
726                 "Windows NT Alpha_AXP",
727                 "Windows NT PowerPC",
728                 "Windows IA64",
729                 "Windows x64",
730                 NULL };
731         fstring *drivers = NULL;
732         int i, env_index, num_drivers;
733         char *keystr, *base, *subkeypath;
734         char *key2 = NULL;
735         int num_subkeys = -1;
736         int version;
737         TALLOC_CTX *ctx = talloc_tos();
738
739         DEBUG(10,("key_driver_fetch_keys key=>[%s]\n", key ? key : "NULL" ));
740
741         keystr = reg_remaining_path(ctx, key + strlen(KEY_ENVIRONMENTS) );
742
743         /* list all possible architectures */
744
745         if ( !keystr ) {
746                 for ( num_subkeys=0; environments[num_subkeys]; num_subkeys++ )
747                         regsubkey_ctr_addkey( subkeys,  environments[num_subkeys] );
748
749                 return num_subkeys;
750         }
751
752         /* we are dealing with a subkey of "Environments */
753         key2 = talloc_strdup(ctx, keystr);
754         if (!key2) {
755                 return -1;
756         }
757         keystr = key2;
758         if (!reg_split_path(keystr, &base, &subkeypath )) {
759                 return -1;
760         }
761
762         /* sanity check */
763
764         for ( env_index=0; environments[env_index]; env_index++ ) {
765                 if ( strequal( environments[env_index], base ) )
766                         break;
767         }
768         if ( !environments[env_index] )
769                 return -1;
770
771         /* ...\Print\Environements\...\ */
772
773         if ( !subkeypath ) {
774                 regsubkey_ctr_addkey( subkeys, "Drivers" );
775                 regsubkey_ctr_addkey( subkeys, "Print Processors" );
776
777                 return 2;
778         }
779
780         /* more of the key path to process */
781
782         keystr = subkeypath;
783         if (!reg_split_path( keystr, &base, &subkeypath )) {
784                 return -1;
785         }
786
787         /* ...\Print\Environements\...\Drivers\ */
788
789         if ( !subkeypath ) {
790                 if ( strequal(base, "Drivers") ) {
791                         switch ( env_index ) {
792                                 case 0: /* Win9x */
793                                         regsubkey_ctr_addkey( subkeys, "Version-0" );
794                                         break;
795                                 default: /* Windows NT based systems */
796                                         regsubkey_ctr_addkey( subkeys, "Version-2" );
797                                         regsubkey_ctr_addkey( subkeys, "Version-3" );
798                                         break;
799                         }
800
801                         return regsubkey_ctr_numkeys( subkeys );
802                 } else if ( strequal(base, "Print Processors") ) {
803                         if ( env_index == 1 || env_index == 5 || env_index == 6 )
804
805
806                         return regsubkey_ctr_numkeys( subkeys );
807                 } else
808                         return -1;      /* bad path */
809         }
810
811         /* we finally get to enumerate the drivers */
812
813         /* only one possible subkey below PrintProc key */
814
815         if ( strequal(base, "Print Processors") ) {
816                 keystr = subkeypath;
817                 if (!reg_split_path( keystr, &base, &subkeypath )) {
818                         return -1;
819                 }
820
821                 /* no subkeys below this point */
822
823                 if ( subkeypath )
824                         return -1;
825
826                 /* only allow one keyname here -- 'winprint' */
827
828                 return strequal( base, "winprint" ) ? 0 : -1;
829         }
830
831         /* only dealing with drivers from here on out */
832
833         keystr = subkeypath;
834         if (!reg_split_path( keystr, &base, &subkeypath )) {
835                 return -1;
836         }
837
838         version = atoi(&base[strlen(base)-1]);
839
840         switch (env_index) {
841         case 0:
842                 if ( version != 0 )
843                         return -1;
844                 break;
845         default:
846                 if ( version != 2 && version != 3 )
847                         return -1;
848                 break;
849         }
850
851
852         if ( !subkeypath ) {
853                 num_drivers = get_ntdrivers( &drivers, environments[env_index], version );
854                 for ( i=0; i<num_drivers; i++ )
855                         regsubkey_ctr_addkey( subkeys, drivers[i] );
856
857                 return regsubkey_ctr_numkeys( subkeys );
858         }
859
860         /* if anything else left, just say if has no subkeys */
861
862         DEBUG(1,("key_driver_fetch_keys unhandled key [%s] (subkey == %s)\n",
863                 key, subkeypath ));
864
865         return 0;
866 }
867
868
869 /**********************************************************************
870  *********************************************************************/
871
872 static void fill_in_driver_values(NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3, struct regval_ctr *values)
873 {
874         char *buffer = NULL;
875         int buffer_size = 0;
876         int i, length;
877         const char *filename;
878         DATA_BLOB data;
879
880         filename = dos_basename( info3->driverpath );
881         regval_ctr_addvalue_sz(values, "Driver", filename);
882
883         filename = dos_basename( info3->configfile );
884         regval_ctr_addvalue_sz(values, "Configuration File", filename);
885
886         filename = dos_basename( info3->datafile );
887         regval_ctr_addvalue_sz(values, "Data File", filename);
888
889         filename = dos_basename( info3->helpfile );
890         regval_ctr_addvalue_sz(values, "Help File", filename);
891
892         regval_ctr_addvalue_sz(values, "Data Type", info3->defaultdatatype);
893
894         regval_ctr_addvalue( values, "Version", REG_DWORD, (char*)&info3->cversion, 
895                 sizeof(info3->cversion) );
896
897         if ( info3->dependentfiles ) {
898                 /* place the list of dependent files in a single
899                    character buffer, separating each file name by
900                    a NULL */
901
902                 for ( i=0; strcmp(info3->dependentfiles[i], ""); i++ ) {
903                         /* strip the path to only the file's base name */
904
905                         filename = dos_basename( info3->dependentfiles[i] );
906
907                         length = strlen(filename);
908
909                         buffer = (char *)SMB_REALLOC( buffer, buffer_size + (length + 1)*sizeof(uint16) );
910                         if ( !buffer ) {
911                                 break;
912                         }
913
914                         push_reg_sz(talloc_tos(), &data, filename);
915                         memcpy( buffer+buffer_size, (char*)data.data, data.length);
916
917                         buffer_size += (length + 1)*sizeof(uint16);
918                 }
919
920                 /* terminated by double NULL.  Add the final one here */
921
922                 buffer = (char *)SMB_REALLOC( buffer, buffer_size + 2 );
923                 if ( !buffer ) {
924                         buffer_size = 0;
925                 } else {
926                         buffer[buffer_size++] = '\0';
927                         buffer[buffer_size++] = '\0';
928                 }
929         }
930
931         regval_ctr_addvalue( values, "Dependent Files",    REG_MULTI_SZ, buffer, buffer_size );
932
933         SAFE_FREE( buffer );
934
935         return;
936 }
937
938 /**********************************************************************
939  *********************************************************************/
940
941 static int driver_arch_fetch_values(char *key, struct regval_ctr *values)
942 {
943         char            *keystr, *base, *subkeypath;
944         fstring         arch_environment;
945         fstring         driver;
946         int             version;
947         NT_PRINTER_DRIVER_INFO_LEVEL    driver_ctr;
948         WERROR          w_result;
949
950         if (!reg_split_path( key, &base, &subkeypath )) {
951                 return -1;
952         }
953
954         /* no values in 'Environments\Drivers\Windows NT x86' */
955
956         if ( !subkeypath )
957                 return 0;
958
959         /* We have the Architecture string and some subkey name:
960            Currently we only support
961            * Drivers
962            * Print Processors
963            Anything else is an error.
964            */
965
966         fstrcpy( arch_environment, base );
967
968         keystr = subkeypath;
969         if (!reg_split_path( keystr, &base, &subkeypath )) {
970                 return -1;
971         }
972
973         if ( strequal(base, "Print Processors") )
974                 return 0;
975
976         /* only Drivers key can be left */
977
978         if ( !strequal(base, "Drivers") )
979                 return -1;
980
981         if ( !subkeypath )
982                 return 0;
983
984         /* We know that we have Architechure\Drivers with some subkey name
985            The subkey name has to be Version-XX */
986
987         keystr = subkeypath;
988         if (!reg_split_path( keystr, &base, &subkeypath )) {
989                 return -1;
990         }
991
992         if ( !subkeypath )
993                 return 0;
994
995         version = atoi(&base[strlen(base)-1]);
996
997         /* BEGIN PRINTER DRIVER NAME BLOCK */
998
999         keystr = subkeypath;
1000         if (!reg_split_path( keystr, &base, &subkeypath )) {
1001                 return -1;
1002         }
1003
1004         /* don't go any deeper for now */
1005
1006         fstrcpy( driver, base );
1007
1008         w_result = get_a_printer_driver( &driver_ctr, 3, driver, arch_environment, version );
1009
1010         if ( !W_ERROR_IS_OK(w_result) )
1011                 return -1;
1012
1013         fill_in_driver_values( driver_ctr.info_3, values );
1014
1015         free_a_printer_driver( driver_ctr, 3 );
1016
1017         /* END PRINTER DRIVER NAME BLOCK */
1018
1019
1020         DEBUG(8,("key_driver_fetch_values: Exit\n"));
1021
1022         return regval_ctr_numvals( values );
1023 }
1024
1025 /**********************************************************************
1026  *********************************************************************/
1027
1028 static int key_driver_fetch_values(const char *key, struct regval_ctr *values)
1029 {
1030         char *keystr = NULL;
1031         char *subkey = NULL;
1032         TALLOC_CTX *ctx = talloc_tos();
1033
1034         DEBUG(8,("key_driver_fetch_values: Enter key => [%s]\n", key ? key : "NULL"));
1035
1036         /* no values in the Environments key */
1037
1038         if (!(keystr = reg_remaining_path(ctx, key + strlen(KEY_ENVIRONMENTS))))
1039                 return 0;
1040
1041         subkey = talloc_strdup(ctx, keystr);
1042         if (!subkey) {
1043                 return 0;
1044         }
1045
1046         /* pass off to handle subkeys */
1047
1048         return driver_arch_fetch_values( subkey, values );
1049 }
1050
1051 /*********************************************************************
1052  *********************************************************************
1053  ** "HKLM/SYSTEM/CURRENTCONTROLSET/CONTROL/PRINT"
1054  *********************************************************************
1055  *********************************************************************/
1056
1057 static int key_print_fetch_keys( const char *key, struct regsubkey_ctr *subkeys )
1058 {
1059         int key_len = strlen(key);
1060
1061         /* no keys below 'Print' handled here */
1062
1063         if ( (key_len != strlen(KEY_CONTROL_PRINT)) && (key_len != strlen(KEY_WINNT_PRINT)) )
1064                 return -1;
1065
1066         regsubkey_ctr_addkey( subkeys, "Environments" );
1067         regsubkey_ctr_addkey( subkeys, "Monitors" );
1068         regsubkey_ctr_addkey( subkeys, "Forms" );
1069         regsubkey_ctr_addkey( subkeys, "Printers" );
1070
1071         return regsubkey_ctr_numkeys( subkeys );
1072 }
1073
1074 /**********************************************************************
1075  *********************************************************************
1076  ** Structure to hold dispatch table of ops for various printer keys.
1077  ** Make sure to always store deeper keys along the same path first so
1078  ** we ge a more specific match.
1079  *********************************************************************
1080  *********************************************************************/
1081
1082 static struct reg_dyn_tree print_registry[] = {
1083 /* just pass the monitor onto the registry tdb */
1084 { KEY_MONITORS,
1085         &regdb_fetch_keys,
1086         &regdb_store_keys,
1087         &regdb_fetch_values,
1088         &regdb_store_values },
1089 { KEY_FORMS,
1090         &key_forms_fetch_keys,
1091         NULL,
1092         &key_forms_fetch_values,
1093         NULL },
1094 { KEY_CONTROL_PRINTERS,
1095         &key_printers_fetch_keys,
1096         &key_printers_store_keys,
1097         &key_printers_fetch_values,
1098         &key_printers_store_values },
1099 { KEY_ENVIRONMENTS,
1100         &key_driver_fetch_keys,
1101         NULL,
1102         &key_driver_fetch_values,
1103         NULL },
1104 { KEY_CONTROL_PRINT,
1105         &key_print_fetch_keys,
1106         NULL,
1107         NULL,
1108         NULL },
1109 { KEY_WINNT_PRINTERS,
1110         &key_printers_fetch_keys,
1111         &key_printers_store_keys,
1112         &key_printers_fetch_values,
1113         &key_printers_store_values },
1114 { KEY_PORTS,
1115         &regdb_fetch_keys,
1116         &regdb_store_keys,
1117         &regdb_fetch_values,
1118         &regdb_store_values },
1119
1120 { NULL, NULL, NULL, NULL, NULL }
1121 };
1122
1123
1124 /**********************************************************************
1125  *********************************************************************
1126  ** Main reg_printing interface functions
1127  *********************************************************************
1128  *********************************************************************/
1129
1130 /***********************************************************************
1131  Lookup a key in the print_registry table, returning its index.
1132  -1 on failure
1133  **********************************************************************/
1134
1135 static int match_registry_path(const char *key)
1136 {
1137         int i;
1138         char *path = NULL;
1139         TALLOC_CTX *ctx = talloc_tos();
1140
1141         if ( !key )
1142                 return -1;
1143
1144         path = talloc_strdup(ctx, key);
1145         if (!path) {
1146                 return -1;
1147         }
1148         path = normalize_reg_path(ctx, path);
1149         if (!path) {
1150                 return -1;
1151         }
1152
1153         for ( i=0; print_registry[i].path; i++ ) {
1154                 if (strncmp( path, print_registry[i].path, strlen(print_registry[i].path) ) == 0 )
1155                         return i;
1156         }
1157
1158         return -1;
1159 }
1160
1161 /***********************************************************************
1162  **********************************************************************/
1163
1164 static int regprint_fetch_reg_keys( const char *key, struct regsubkey_ctr *subkeys )
1165 {
1166         int i = match_registry_path( key );
1167
1168         if ( i == -1 )
1169                 return -1;
1170
1171         if ( !print_registry[i].fetch_subkeys )
1172                 return -1;
1173
1174         return print_registry[i].fetch_subkeys( key, subkeys );
1175 }
1176
1177 /**********************************************************************
1178  *********************************************************************/
1179
1180 static bool regprint_store_reg_keys( const char *key, struct regsubkey_ctr *subkeys )
1181 {
1182         int i = match_registry_path( key );
1183
1184         if ( i == -1 )
1185                 return False;
1186
1187         if ( !print_registry[i].store_subkeys )
1188                 return False;
1189
1190         return print_registry[i].store_subkeys( key, subkeys );
1191 }
1192
1193 /**********************************************************************
1194  *********************************************************************/
1195
1196 static int regprint_fetch_reg_values(const char *key, struct regval_ctr *values)
1197 {
1198         int i = match_registry_path( key );
1199
1200         if ( i == -1 )
1201                 return -1;
1202
1203         /* return 0 values by default since we know the key had
1204            to exist because the client opened a handle */
1205
1206         if ( !print_registry[i].fetch_values )
1207                 return 0;
1208
1209         return print_registry[i].fetch_values( key, values );
1210 }
1211
1212 /**********************************************************************
1213  *********************************************************************/
1214
1215 static bool regprint_store_reg_values(const char *key, struct regval_ctr *values)
1216 {
1217         int i = match_registry_path( key );
1218
1219         if ( i == -1 )
1220                 return False;
1221
1222         if ( !print_registry[i].store_values )
1223                 return False;
1224
1225         return print_registry[i].store_values( key, values );
1226 }
1227
1228 /*
1229  * Table of function pointers for accessing printing data
1230  */
1231
1232 struct registry_ops printing_ops = {
1233         .fetch_subkeys = regprint_fetch_reg_keys,
1234         .fetch_values = regprint_fetch_reg_values,
1235         .store_subkeys = regprint_store_reg_keys,
1236         .store_values = regprint_store_reg_values,
1237 };