More memory corruption (leaks etc.) fixes.
[kai/samba.git] / source3 / printing / nt_printing.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-2000,
6  *  Copyright (C) Jean François Micouleau      1998-2000.
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include "includes.h"
24
25 extern int DEBUGLEVEL;
26
27 static TDB_CONTEXT *tdb; /* used for driver files */
28
29 #define FORMS_PREFIX "FORMS/"
30 #define DRIVERS_PREFIX "DRIVERS/"
31 #define PRINTERS_PREFIX "PRINTERS/"
32
33 #define DATABASE_VERSION 1
34
35 /****************************************************************************
36 open the NT printing tdb
37 ****************************************************************************/
38 BOOL nt_printing_init(void)
39 {
40         static pid_t local_pid;
41
42         if (tdb && local_pid == sys_getpid()) return True;
43         tdb = tdb_open(lock_path("ntdrivers.tdb"), 0, 0, O_RDWR|O_CREAT, 0600);
44         if (!tdb) {
45                 DEBUG(0,("Failed to open nt drivers database\n"));
46                 return False;
47         }
48
49         local_pid = sys_getpid();
50
51         /* handle a Samba upgrade */
52         tdb_writelock(tdb);
53         if (tdb_fetch_int(tdb, "INFO/version") != DATABASE_VERSION) {
54                 tdb_traverse(tdb, (tdb_traverse_func)tdb_delete, NULL);
55                 tdb_store_int(tdb, "INFO/version", DATABASE_VERSION);
56         }
57         tdb_writeunlock(tdb);
58
59         return True;
60 }
61
62   
63 /****************************************************************************
64 get a form struct list
65 ****************************************************************************/
66 int get_ntforms(nt_forms_struct **list)
67 {
68         TDB_DATA kbuf, newkey, dbuf;
69         nt_forms_struct form;
70         int ret;
71         int n = 0;
72
73         for (kbuf = tdb_firstkey(tdb); 
74              kbuf.dptr; 
75              newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
76                 if (strncmp(kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) != 0) continue;
77                 
78                 dbuf = tdb_fetch(tdb, kbuf);
79                 if (!dbuf.dptr) continue;
80
81                 fstrcpy(form.name, kbuf.dptr+strlen(FORMS_PREFIX));
82                 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddddddd",
83                                  &form.flag, &form.width, &form.length, &form.left,
84                                  &form.top, &form.right, &form.bottom);
85                 safe_free(dbuf.dptr);
86                 if (ret != dbuf.dsize) continue;
87
88                 *list = Realloc(*list, sizeof(nt_forms_struct)*(n+1));
89                 (*list)[n] = form;
90                 n++;
91         }
92
93         return n;
94 }
95
96 /****************************************************************************
97 write a form struct list
98 ****************************************************************************/
99 int write_ntforms(nt_forms_struct **list, int number)
100 {
101         pstring buf, key;
102         int len;
103         TDB_DATA kbuf,dbuf;
104         int i;
105
106         for (i=0;i<number;i++) {
107                 len = tdb_pack(buf, sizeof(buf), "ddddddd", 
108                                (*list)[i].flag, (*list)[i].width, (*list)[i].length,
109                                (*list)[i].left, (*list)[i].top, (*list)[i].right, 
110                                (*list)[i].bottom);
111                 if (len > sizeof(buf)) break;
112                 slprintf(key, sizeof(key), "%s/%s", FORMS_PREFIX, (*list)[i].name);
113                 kbuf.dsize = strlen(key)+1;
114                 kbuf.dptr = key;
115                 dbuf.dsize = len;
116                 dbuf.dptr = buf;
117                 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) break;
118        }
119
120        return i;
121 }
122
123 /****************************************************************************
124 add a form struct at the end of the list
125 ****************************************************************************/
126 BOOL add_a_form(nt_forms_struct **list, const FORM *form, int *count)
127 {
128         int n=0;
129         BOOL update;
130         fstring form_name;
131
132         /* 
133          * NT tries to add forms even when 
134          * they are already in the base
135          * only update the values if already present
136          */
137
138         update=False;
139         
140         unistr2_to_ascii(form_name, &(form->name), sizeof(form_name)-1);
141         for (n=0; n<*count && update==False; n++)
142         {
143                 if (!strncmp((*list)[n].name, form_name, strlen(form_name)))
144                 {
145                         DEBUG(103, ("NT workaround, [%s] already exists\n", form_name));
146                         update=True;
147                 }
148         }
149
150         if (update==False)
151         {
152                 if((*list=Realloc(*list, (n+1)*sizeof(nt_forms_struct))) == NULL)
153                         return False;
154                 unistr2_to_ascii((*list)[n].name, &(form->name), sizeof((*list)[n].name)-1);
155                 (*count)++;
156         }
157         
158         (*list)[n].flag=form->flags;
159         (*list)[n].width=form->size_x;
160         (*list)[n].length=form->size_y;
161         (*list)[n].left=form->left;
162         (*list)[n].top=form->top;
163         (*list)[n].right=form->right;
164         (*list)[n].bottom=form->bottom;
165
166         return True;
167 }
168
169 /****************************************************************************
170 update a form struct 
171 ****************************************************************************/
172 void update_a_form(nt_forms_struct **list, const FORM *form, int count)
173 {
174         int n=0;
175         fstring form_name;
176         unistr2_to_ascii(form_name, &(form->name), sizeof(form_name)-1);
177
178         DEBUG(106, ("[%s]\n", form_name));
179         for (n=0; n<count; n++)
180         {
181                 DEBUGADD(106, ("n [%d]:[%s]\n", n, (*list)[n].name));
182                 if (!strncmp((*list)[n].name, form_name, strlen(form_name)))
183                         break;
184         }
185
186         if (n==count) return;
187
188         (*list)[n].flag=form->flags;
189         (*list)[n].width=form->size_x;
190         (*list)[n].length=form->size_y;
191         (*list)[n].left=form->left;
192         (*list)[n].top=form->top;
193         (*list)[n].right=form->right;
194         (*list)[n].bottom=form->bottom;
195 }
196  
197 /****************************************************************************
198 get the nt drivers list
199
200 traverse the database and look-up the matching names
201 ****************************************************************************/
202 int get_ntdrivers(fstring **list, char *architecture)
203 {
204         int total=0;
205         fstring short_archi;
206         pstring key;
207         TDB_DATA kbuf, newkey;
208
209         get_short_archi(short_archi, architecture);
210         slprintf(key, sizeof(key), "%s/%s/", DRIVERS_PREFIX, short_archi);
211
212         for (kbuf = tdb_firstkey(tdb); 
213              kbuf.dptr; 
214              newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
215                 if (strncmp(kbuf.dptr, key, strlen(key)) != 0) continue;
216                 
217                 if((*list = Realloc(*list, sizeof(fstring)*(total+1))) == NULL)
218                         return -1;
219
220                 fstrcpy((*list)[total], kbuf.dptr+strlen(key));
221                 total++;
222         }
223
224         return(total);
225 }
226
227 /****************************************************************************
228 function to do the mapping between the long architecture name and
229 the short one.
230 ****************************************************************************/
231 void get_short_archi(char *short_archi, char *long_archi)
232 {
233         struct table {
234                 char *long_archi;
235                 char *short_archi;
236         };
237         
238         struct table archi_table[]=
239         {
240                 {"Windows 4.0",          "WIN40"    },
241                 {"Windows NT x86",       "W32X86"   },
242                 {"Windows NT R4000",     "W32mips"  },
243                 {"Windows NT Alpha_AXP", "W32alpha" },
244                 {"Windows NT PowerPC",   "W32ppc"   },
245                 {NULL,                   ""         }
246         };
247         
248         int i=-1;
249
250         DEBUG(107,("Getting architecture dependant directory\n"));
251         do {
252                 i++;
253         } while ( (archi_table[i].long_archi!=NULL ) && strncmp(long_archi, archi_table[i].long_archi, strlen(long_archi)) );
254
255         if (archi_table[i].long_archi==NULL)
256         {
257                 DEBUGADD(107,("Unknown architecture [%s] !\n", long_archi));
258         }
259         StrnCpy (short_archi, archi_table[i].short_archi, strlen(archi_table[i].short_archi));
260
261         DEBUGADD(108,("index: [%d]\n", i));
262         DEBUGADD(108,("long architecture: [%s]\n", long_archi));
263         DEBUGADD(108,("short architecture: [%s]\n", short_archi));
264 }
265
266 /****************************************************************************
267 ****************************************************************************/
268 static uint32 add_a_printer_driver_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver)
269 {
270         int len, buflen;
271         fstring architecture;
272         pstring key;
273         char *buf;
274         int i, ret;
275         TDB_DATA kbuf, dbuf;
276
277         get_short_archi(architecture, driver->environment);
278         slprintf(key, sizeof(key), "%s/%s/%s", DRIVERS_PREFIX, architecture, driver->name);
279
280         /*
281          * cversion must be 2.
282          * when adding a printer ON the SERVER
283          * rpcAddPrinterDriver defines it to zero
284          * which is wrong !!!
285          *
286          * JFM, 4/14/99
287          */
288         driver->cversion=2;
289         
290         buf = NULL;
291         len = buflen = 0;
292
293  again:
294         len = 0;
295         len += tdb_pack(buf+len, buflen-len, "dffffffff", 
296                         driver->cversion,
297                         driver->name,
298                         driver->environment,
299                         driver->driverpath,
300                         driver->datafile,
301                         driver->configfile,
302                         driver->helpfile,
303                         driver->monitorname,
304                         driver->defaultdatatype);
305         
306         if (driver->dependentfiles) {
307                 for (i=0; *driver->dependentfiles[i]; i++) {
308                         len += tdb_pack(buf+len, buflen-len, "f", 
309                                         driver->dependentfiles[i]);
310                 }
311         }
312
313         if (len != buflen) {
314                 buf = (char *)Realloc(buf, len);
315                 buflen = len;
316                 goto again;
317         }
318
319
320         kbuf.dptr = key;
321         kbuf.dsize = strlen(key)+1;
322         dbuf.dptr = buf;
323         dbuf.dsize = len;
324         
325         ret = tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
326
327         safe_free(buf);
328         return ret;
329 }
330
331 /****************************************************************************
332 ****************************************************************************/
333 static uint32 add_a_printer_driver_6(NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver)
334 {
335         NT_PRINTER_DRIVER_INFO_LEVEL_3 info3;
336
337         info3.cversion = driver->version;
338         fstrcpy(info3.environment,driver->environment);
339         fstrcpy(info3.driverpath,driver->driverpath);
340         fstrcpy(info3.datafile,driver->datafile);
341         fstrcpy(info3.configfile,driver->configfile);
342         fstrcpy(info3.helpfile,driver->helpfile);
343         fstrcpy(info3.monitorname,driver->monitorname);
344         fstrcpy(info3.defaultdatatype,driver->defaultdatatype);
345         info3.dependentfiles = driver->dependentfiles;
346
347         return add_a_printer_driver_3(&info3);
348 }
349
350
351 /****************************************************************************
352 ****************************************************************************/
353 static uint32 get_a_printer_driver_3_default(NT_PRINTER_DRIVER_INFO_LEVEL_3 **info_ptr, fstring in_prt, fstring in_arch)
354 {
355         NT_PRINTER_DRIVER_INFO_LEVEL_3 info;
356         int snum;
357
358         ZERO_STRUCT(info);
359
360         snum = lp_servicenumber(in_prt);
361
362         fstrcpy(info.name, lp_printerdriver(snum));
363         fstrcpy(info.defaultdatatype, "RAW");
364         
365         if ((info.dependentfiles=(char *)malloc(sizeof(fstring))) == NULL)
366                 return ERROR_NOT_ENOUGH_MEMORY;
367
368         fstrcpy(info.dependentfiles[0], "");
369
370         *info_ptr = memdup(&info, sizeof(info));
371         
372         return 0;       
373 }
374
375 /****************************************************************************
376 ****************************************************************************/
377 static uint32 get_a_printer_driver_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 **info_ptr, fstring in_prt, fstring in_arch)
378 {
379         NT_PRINTER_DRIVER_INFO_LEVEL_3 driver;
380         TDB_DATA kbuf, dbuf;
381         fstring architecture;
382         int len = 0;
383         int i;
384         pstring key;
385
386         ZERO_STRUCT(driver);
387
388         get_short_archi(architecture, in_arch);
389         slprintf(key, sizeof(key), "%s/%s/%s", DRIVERS_PREFIX, architecture, in_prt);
390
391         kbuf.dptr = key;
392         kbuf.dsize = strlen(key)+1;
393         
394         dbuf = tdb_fetch(tdb, kbuf);
395         if (!dbuf.dptr) return get_a_printer_driver_3_default(info_ptr, in_prt, in_arch);
396
397         len += tdb_unpack(dbuf.dptr, dbuf.dsize, "dffffffff", 
398                           &driver.cversion,
399                           driver.name,
400                           driver.environment,
401                           driver.driverpath,
402                           driver.datafile,
403                           driver.configfile,
404                           driver.helpfile,
405                           driver.monitorname,
406                           driver.defaultdatatype);
407
408         i=0;
409         while (len < dbuf.dsize) {
410                 driver.dependentfiles = (fstring *)Realloc(driver.dependentfiles,
411                                                          sizeof(fstring)*(i+2));
412                 if (driver.dependentfiles == NULL)
413                         break;
414
415                 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "f", 
416                                   &driver.dependentfiles[i]);
417                 i++;
418         }
419         if (driver.dependentfiles != NULL)
420                 fstrcpy(driver.dependentfiles[i], "");
421
422         safe_free(dbuf.dptr);
423
424         if (len != dbuf.dsize) {
425                 if (driver.dependentfiles != NULL)
426                         safe_free(driver.dependentfiles);
427
428                 return get_a_printer_driver_3_default(info_ptr, in_prt, in_arch);
429         }
430
431         *info_ptr = (NT_PRINTER_DRIVER_INFO_LEVEL_3 *)memdup(&driver, sizeof(driver));
432
433         return 0;
434 }
435
436 /****************************************************************************
437 debugging function, dump at level 6 the struct in the logs
438 ****************************************************************************/
439 static uint32 dump_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
440 {
441         uint32 success;
442         NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3;
443         int i;
444         
445         DEBUG(106,("Dumping printer driver at level [%d]\n", level));
446         
447         switch (level)
448         {
449                 case 3: 
450                 {
451                         if (driver.info_3 == NULL)
452                                 success=5;
453                         else {
454                                 info3=driver.info_3;
455                         
456                                 DEBUGADD(106,("version:[%d]\n",         info3->cversion));
457                                 DEBUGADD(106,("name:[%s]\n",            info3->name));
458                                 DEBUGADD(106,("environment:[%s]\n",     info3->environment));
459                                 DEBUGADD(106,("driverpath:[%s]\n",      info3->driverpath));
460                                 DEBUGADD(106,("datafile:[%s]\n",        info3->datafile));
461                                 DEBUGADD(106,("configfile:[%s]\n",      info3->configfile));
462                                 DEBUGADD(106,("helpfile:[%s]\n",        info3->helpfile));
463                                 DEBUGADD(106,("monitorname:[%s]\n",     info3->monitorname));
464                                 DEBUGADD(106,("defaultdatatype:[%s]\n", info3->defaultdatatype));
465                                 
466                                 for (i=0; info3->dependentfiles &&
467                                           *info3->dependentfiles[i]; i++) {
468                                         DEBUGADD(106,("dependentfile:[%s]\n", 
469                                                       info3->dependentfiles[i]));
470                                 }
471                                 success=0;
472                         }
473                         break;
474                 }
475                 default:
476                         DEBUGADD(1,("Level not implemented\n"));
477                         success=1;
478                         break;
479         }
480         
481         return (success);
482 }
483
484 /****************************************************************************
485 ****************************************************************************/
486 static int pack_devicemode(NT_DEVICEMODE *nt_devmode, char *buf, int buflen)
487 {
488         int len = 0;
489
490         len += tdb_pack(buf+len, buflen-len, "p", nt_devmode);
491
492         if (!nt_devmode) return len;
493
494         len += tdb_pack(buf+len, buflen-len, "fddddddddddddddddddddddp",
495                        nt_devmode->formname,
496                        nt_devmode->specversion,
497                        nt_devmode->driverversion,
498                        nt_devmode->size,
499                        nt_devmode->driverextra,
500                        nt_devmode->fields,
501                        nt_devmode->orientation,
502                        nt_devmode->papersize,
503                        nt_devmode->paperlength,
504                        nt_devmode->paperwidth,
505                        nt_devmode->scale,
506                        nt_devmode->copies,
507                        nt_devmode->defaultsource,
508                        nt_devmode->printquality,
509                        nt_devmode->color,
510                        nt_devmode->duplex,
511                        nt_devmode->yresolution,
512                        nt_devmode->ttoption,
513                        nt_devmode->collate,
514                        nt_devmode->icmmethod,
515                        nt_devmode->icmintent,
516                        nt_devmode->mediatype,
517                        nt_devmode->dithertype,
518                        nt_devmode->private);
519         
520         if (nt_devmode->private) {
521                 len += tdb_pack(buf+len, buflen-len, "B",
522                                 nt_devmode->driverextra,
523                                 nt_devmode->private);
524         }
525
526         return len;
527 }
528
529 /****************************************************************************
530 ****************************************************************************/
531 static int pack_specifics(NT_PRINTER_PARAM *param, char *buf, int buflen)
532 {
533         int len = 0;
534
535         while (param != NULL) {
536                 len += tdb_pack(buf+len, buflen-len, "pfdB",
537                                 param,
538                                 param->value, 
539                                 param->type, 
540                                 param->data_len,
541                                 param->data);
542                 param=param->next;      
543         }
544
545         len += tdb_pack(buf+len, buflen-len, "p", param);
546
547         return len;
548 }
549
550
551 /****************************************************************************
552 delete a printer - this just deletes the printer info file, any open
553 handles are not affected
554 ****************************************************************************/
555 uint32 del_a_printer(char *portname)
556 {
557         pstring key;
558         TDB_DATA kbuf;
559
560         slprintf(key, sizeof(key), "%s/%s",
561                  PRINTERS_PREFIX, portname);
562
563         kbuf.dptr=key;
564         kbuf.dsize=strlen(key)+1;
565
566         tdb_delete(tdb, kbuf);
567         return 0;
568 }
569
570 /****************************************************************************
571 ****************************************************************************/
572 static uint32 add_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info)
573 {
574         pstring key;
575         char *buf;
576         int buflen, len, ret;
577         TDB_DATA kbuf, dbuf;
578         
579         /* 
580          * in addprinter: no servername and the printer is the name
581          * in setprinter: servername is \\server
582          *                and printer is \\server\\printer
583          *
584          * Samba manages only local printers.
585          * we currently don't support things like path=\\other_server\printer
586          */
587         if (info->servername[0]!='\0')
588         {
589                 trim_string(info->printername, info->servername, NULL);
590                 trim_string(info->printername, "\\", NULL);
591                 info->servername[0]='\0';
592         }
593
594         /*
595          * JFM: one day I'll forget.
596          * below that's info->portname because that's the SAMBA sharename
597          * and I made NT 'thinks' it's the portname
598          * the info->sharename is the thing you can name when you add a printer
599          * that's the short-name when you create shared printer for 95/98
600          * So I've made a limitation in SAMBA: you can only have 1 printer model
601          * behind a SAMBA share.
602          */
603
604
605         buf = NULL;
606         buflen = 0;
607
608  again: 
609         len = 0;
610         len += tdb_pack(buf+len, buflen-len, "dddddddddddffffffffff",
611                         info->attributes,
612                         info->priority,
613                         info->default_priority,
614                         info->starttime,
615                         info->untiltime,
616                         info->status,
617                         info->cjobs,
618                         info->averageppm,
619                         info->changeid,
620                         info->c_setprinter,
621                         info->setuptime,
622                         info->servername,
623                         info->printername,
624                         info->sharename,
625                         info->portname,
626                         info->drivername,
627                         info->location,
628                         info->sepfile,
629                         info->printprocessor,
630                         info->datatype,
631                         info->parameters);
632
633         len += pack_devicemode(info->devmode, buf+len, buflen-len);
634         len += pack_specifics(info->specific, buf+len, buflen-len);
635
636         if (buflen != len) {
637                 buf = (char *)Realloc(buf, len);
638                 buflen = len;
639                 goto again;
640         }
641         
642
643         slprintf(key, sizeof(key), "%s/%s",
644                  PRINTERS_PREFIX, info->portname);
645
646         kbuf.dptr = key;
647         kbuf.dsize = strlen(key)+1;
648         dbuf.dptr = buf;
649         dbuf.dsize = len;
650
651         ret = tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
652
653         safe_free(buf);
654
655         return ret;
656 }
657
658
659 /****************************************************************************
660 ****************************************************************************/
661 BOOL add_a_specific_param(NT_PRINTER_INFO_LEVEL_2 *info_2, NT_PRINTER_PARAM *param)
662 {
663         NT_PRINTER_PARAM *current;
664         
665         DEBUG(108,("add_a_specific_param\n"));  
666
667         param->next=NULL;
668         
669         if (info_2->specific == NULL)
670         {
671                 info_2->specific=param;
672         }
673         else
674         {
675                 current=info_2->specific;               
676                 while (current->next != NULL) {
677                         current=current->next;
678                 }               
679                 current->next=param;
680         }
681         return (True);
682 }
683
684 /****************************************************************************
685 ****************************************************************************/
686 BOOL unlink_specific_param_if_exist(NT_PRINTER_INFO_LEVEL_2 *info_2, NT_PRINTER_PARAM *param)
687 {
688         NT_PRINTER_PARAM *current;
689         NT_PRINTER_PARAM *previous;
690         
691         current=info_2->specific;
692         previous=current;
693         
694         if (current==NULL) return (False);
695         
696         if ( !strcmp(current->value, param->value) && 
697             (strlen(current->value)==strlen(param->value)) )
698         {
699                 DEBUG(109,("deleting first value\n"));
700                 info_2->specific=current->next;
701                 safe_free(current->data);
702                 safe_free(current);
703                 DEBUG(109,("deleted first value\n"));
704                 return (True);
705         }
706
707         current=previous->next;
708                 
709         while ( current!=NULL )
710         {
711                 if (!strcmp(current->value, param->value) &&
712                     strlen(current->value)==strlen(param->value) )
713                 {
714                         DEBUG(109,("deleting current value\n"));
715                         previous->next=current->next;
716                         safe_free(current);
717                         DEBUG(109,("deleted current value\n"));
718                         return(True);
719                 }
720                 
721                 previous=previous->next;
722                 current=current->next;
723         }
724         return (False);
725 }
726
727 /****************************************************************************
728  Clean up and deallocate a (maybe partially) allocated NT_PRINTER_PARAM.
729 ****************************************************************************/
730 static void free_nt_printer_param(NT_PRINTER_PARAM **param_ptr)
731 {
732         NT_PRINTER_PARAM *param = *param_ptr;
733
734         if(param == NULL)
735                 return;
736
737         DEBUG(106,("free_nt_printer_param: deleting param [%s]\n", param->value));
738
739         if(param->data)
740                 safe_free(param->data);
741
742         safe_free(param);
743         *param_ptr = NULL;
744 }
745
746 /****************************************************************************
747  Malloc and return an NT devicemode.
748 ****************************************************************************/
749
750 NT_DEVICEMODE *construct_nt_devicemode(void)
751 {
752 /*
753  * should I init this ones ???
754         nt_devmode->devicename
755 */
756
757         NT_DEVICEMODE *nt_devmode = (NT_DEVICEMODE *)malloc(sizeof(NT_DEVICEMODE));
758
759         if (nt_devmode == NULL) {
760                 DEBUG(0,("construct_nt_devicemode: malloc fail.\n"));
761                 return NULL;
762         }
763
764         ZERO_STRUCTP(nt_devmode);
765
766         fstrcpy(nt_devmode->formname, "A4");
767
768         nt_devmode->specversion      = 0x0401;
769         nt_devmode->driverversion    = 0x0400;
770         nt_devmode->size             = 0x00DC;
771         nt_devmode->driverextra      = 0x0000;
772         nt_devmode->fields           = FORMNAME | TTOPTION | PRINTQUALITY | 
773                                        DEFAULTSOURCE | COPIES | SCALE | 
774                                        PAPERSIZE | ORIENTATION;
775         nt_devmode->orientation      = 1;
776         nt_devmode->papersize        = PAPER_A4;
777         nt_devmode->paperlength      = 0;
778         nt_devmode->paperwidth       = 0;
779         nt_devmode->scale            = 0x64;
780         nt_devmode->copies           = 01;
781         nt_devmode->defaultsource    = BIN_FORMSOURCE;
782         nt_devmode->printquality     = 0x0258;
783         nt_devmode->color            = COLOR_MONOCHROME;
784         nt_devmode->duplex           = DUP_SIMPLEX;
785         nt_devmode->yresolution      = 0;
786         nt_devmode->ttoption         = TT_SUBDEV;
787         nt_devmode->collate          = COLLATE_FALSE;
788         nt_devmode->icmmethod        = 0;
789         nt_devmode->icmintent        = 0;
790         nt_devmode->mediatype        = 0;
791         nt_devmode->dithertype       = 0;
792
793         /* non utilisés par un driver d'imprimante */
794         nt_devmode->logpixels        = 0;
795         nt_devmode->bitsperpel       = 0;
796         nt_devmode->pelswidth        = 0;
797         nt_devmode->pelsheight       = 0;
798         nt_devmode->displayflags     = 0;
799         nt_devmode->displayfrequency = 0;
800         nt_devmode->reserved1        = 0;
801         nt_devmode->reserved2        = 0;
802         nt_devmode->panningwidth     = 0;
803         nt_devmode->panningheight    = 0;
804         
805         nt_devmode->private=NULL;
806
807         return nt_devmode;
808 }
809
810 /****************************************************************************
811  Deepcopy an NT devicemode.
812 ****************************************************************************/
813
814 NT_DEVICEMODE *dup_nt_devicemode(NT_DEVICEMODE *nt_devicemode)
815 {
816         NT_DEVICEMODE *new_nt_devicemode = NULL;
817
818         if ((new_nt_devicemode = (NT_DEVICEMODE *)memdup(nt_devicemode, sizeof(NT_DEVICEMODE))) == NULL) {
819                 DEBUG(0,("dup_nt_devicemode: malloc fail.\n"));
820                 return NULL;
821         }
822
823         new_nt_devicemode->private = NULL;
824         if (nt_devicemode->private != NULL) {
825                 if ((new_nt_devicemode->private = memdup(nt_devicemode->private, nt_devicemode->driverextra)) == NULL) {
826                         safe_free(new_nt_devicemode);
827                         DEBUG(0,("dup_nt_devicemode: malloc fail.\n"));
828                         return NULL;
829         }
830         }
831
832         return new_nt_devicemode;
833 }
834
835 /****************************************************************************
836  Clean up and deallocate a (maybe partially) allocated NT_DEVICEMODE.
837 ****************************************************************************/
838
839 void free_nt_devicemode(NT_DEVICEMODE **devmode_ptr)
840 {
841         NT_DEVICEMODE *nt_devmode = *devmode_ptr;
842
843         if(nt_devmode == NULL)
844                 return;
845
846         DEBUG(106,("free_nt_devicemode: deleting DEVMODE\n"));
847
848         if(nt_devmode->private)
849                 safe_free(nt_devmode->private);
850
851         safe_free(nt_devmode);
852         *devmode_ptr = NULL;
853 }
854
855 /****************************************************************************
856  Clean up and deallocate a (maybe partially) allocated NT_PRINTER_INFO_LEVEL_2.
857 ****************************************************************************/
858 static void free_nt_printer_info_level_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr)
859 {
860         NT_PRINTER_INFO_LEVEL_2 *info = *info_ptr;
861         NT_PRINTER_PARAM *param_ptr;
862
863         if(info == NULL)
864                 return;
865
866         DEBUG(106,("free_nt_printer_info_level_2: deleting info\n"));
867
868         free_nt_devicemode(&info->devmode);
869         free_sec_desc_buf(&info->secdesc_buf);
870
871         for(param_ptr = info->specific; param_ptr; ) {
872                 NT_PRINTER_PARAM *tofree = param_ptr;
873
874                 param_ptr = param_ptr->next;
875                 free_nt_printer_param(&tofree);
876         }
877
878         safe_free(*info_ptr);
879         *info_ptr = NULL;
880 }
881
882
883 /****************************************************************************
884 ****************************************************************************/
885 static int unpack_devicemode(NT_DEVICEMODE **nt_devmode, char *buf, int buflen)
886 {
887         int len = 0;
888         NT_DEVICEMODE devmode;
889
890         ZERO_STRUCT(devmode);
891
892         len += tdb_unpack(buf+len, buflen-len, "p", nt_devmode);
893
894         if (!*nt_devmode) return len;
895
896         len += tdb_unpack(buf+len, buflen-len, "fddddddddddddddddddddddp",
897                        devmode.formname,
898                        &devmode.specversion,
899                        &devmode.driverversion,
900                        &devmode.size,
901                        &devmode.driverextra,
902                        &devmode.fields,
903                        &devmode.orientation,
904                        &devmode.papersize,
905                        &devmode.paperlength,
906                        &devmode.paperwidth,
907                        &devmode.scale,
908                        &devmode.copies,
909                        &devmode.defaultsource,
910                        &devmode.printquality,
911                        &devmode.color,
912                        &devmode.duplex,
913                        &devmode.yresolution,
914                        &devmode.ttoption,
915                        &devmode.collate,
916                        &devmode.icmmethod,
917                        &devmode.icmintent,
918                        &devmode.mediatype,
919                        &devmode.dithertype,
920                        &devmode.private);
921         
922         if (devmode.private) {
923                 devmode.private = (uint8 *)malloc(devmode.driverextra);
924                 if (!devmode.private) return 2;
925                 len += tdb_unpack(buf+len, buflen-len, "B",
926                                   devmode.driverextra,
927                                   devmode.private);
928         }
929
930         *nt_devmode = (NT_DEVICEMODE *)memdup(&devmode, sizeof(devmode));
931
932         return len;
933 }
934
935 /****************************************************************************
936 ****************************************************************************/
937 static int unpack_specifics(NT_PRINTER_PARAM **list, char *buf, int buflen)
938 {
939         int len = 0;
940         NT_PRINTER_PARAM param, *p;
941
942         *list = NULL;
943
944         while (1) {
945                 len += tdb_unpack(buf+len, buflen-len, "p", &p);
946                 if (!p) break;
947
948                 len += tdb_unpack(buf+len, buflen-len, "fdB",
949                                   param.value, 
950                                   &param.type, 
951                                   &param.data_len,
952                                   &param.data);
953                 param.next = *list;
954                 *list = memdup(&param, sizeof(param));
955         }
956
957         return len;
958 }
959
960
961 /****************************************************************************
962 get a default printer info 2 struct
963 ****************************************************************************/
964 static uint32 get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 **info_ptr, fstring sharename)
965 {
966         extern pstring global_myname;
967         int snum;
968         NT_PRINTER_INFO_LEVEL_2 info;
969
970         ZERO_STRUCT(info);
971
972         snum = lp_servicenumber(sharename);
973
974         fstrcpy(info.servername, global_myname);
975         fstrcpy(info.printername, sharename);
976         fstrcpy(info.portname, sharename);
977         fstrcpy(info.drivername, lp_printerdriver(snum));
978         fstrcpy(info.printprocessor, "winprint");
979         fstrcpy(info.datatype, "RAW");
980
981         if ((info.devmode = construct_nt_devicemode()) == NULL)
982                 goto fail;
983
984         if (!nt_printing_getsec(sharename, &info.secdesc_buf))
985                 goto fail;
986
987         *info_ptr = (NT_PRINTER_INFO_LEVEL_2 *)memdup(&info, sizeof(info));
988         if (! *info_ptr) {
989                 DEBUG(0,("get_a_printer_2_default: malloc fail.\n"));
990                 goto fail;
991         }
992
993         return (0);     
994
995   fail:
996
997         if (info.devmode)
998                 free_nt_devicemode(&info.devmode);
999         if (info.secdesc_buf)
1000                 free_sec_desc_buf(&info.secdesc_buf);
1001         return 2;
1002 }
1003
1004 /****************************************************************************
1005 ****************************************************************************/
1006 static uint32 get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr, fstring sharename)
1007 {
1008         pstring key;
1009         NT_PRINTER_INFO_LEVEL_2 info;
1010         int len = 0;
1011         TDB_DATA kbuf, dbuf;
1012                 
1013         ZERO_STRUCT(info);
1014
1015         slprintf(key, sizeof(key), "%s/%s",
1016                  PRINTERS_PREFIX, sharename);
1017
1018         kbuf.dptr = key;
1019         kbuf.dsize = strlen(key)+1;
1020
1021         dbuf = tdb_fetch(tdb, kbuf);
1022         if (!dbuf.dptr) return get_a_printer_2_default(info_ptr, sharename);
1023
1024         len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "dddddddddddffffffffff",
1025                         &info.attributes,
1026                         &info.priority,
1027                         &info.default_priority,
1028                         &info.starttime,
1029                         &info.untiltime,
1030                         &info.status,
1031                         &info.cjobs,
1032                         &info.averageppm,
1033                         &info.changeid,
1034                         &info.c_setprinter,
1035                         &info.setuptime,
1036                         info.servername,
1037                         info.printername,
1038                         info.sharename,
1039                         info.portname,
1040                         info.drivername,
1041                         info.location,
1042                         info.sepfile,
1043                         info.printprocessor,
1044                         info.datatype,
1045                         info.parameters);
1046
1047         len += unpack_devicemode(&info.devmode,dbuf.dptr+len, dbuf.dsize-len);
1048         len += unpack_specifics(&info.specific,dbuf.dptr+len, dbuf.dsize-len);
1049
1050         nt_printing_getsec(sharename, &info.secdesc_buf);
1051
1052         safe_free(dbuf.dptr);
1053         *info_ptr=memdup(&info, sizeof(info));
1054         
1055         return 0;       
1056 }
1057
1058 /****************************************************************************
1059 debugging function, dump at level 6 the struct in the logs
1060 ****************************************************************************/
1061 static uint32 dump_a_printer(NT_PRINTER_INFO_LEVEL printer, uint32 level)
1062 {
1063         uint32 success;
1064         NT_PRINTER_INFO_LEVEL_2 *info2;
1065         
1066         DEBUG(106,("Dumping printer at level [%d]\n", level));
1067         
1068         switch (level)
1069         {
1070                 case 2: 
1071                 {
1072                         if (printer.info_2 == NULL)
1073                                 success=5;
1074                         else
1075                         {
1076                                 info2=printer.info_2;
1077                         
1078                                 DEBUGADD(106,("attributes:[%d]\n", info2->attributes));
1079                                 DEBUGADD(106,("priority:[%d]\n", info2->priority));
1080                                 DEBUGADD(106,("default_priority:[%d]\n", info2->default_priority));
1081                                 DEBUGADD(106,("starttime:[%d]\n", info2->starttime));
1082                                 DEBUGADD(106,("untiltime:[%d]\n", info2->untiltime));
1083                                 DEBUGADD(106,("status:[%d]\n", info2->status));
1084                                 DEBUGADD(106,("cjobs:[%d]\n", info2->cjobs));
1085                                 DEBUGADD(106,("averageppm:[%d]\n", info2->averageppm));
1086                                 DEBUGADD(106,("changeid:[%d]\n", info2->changeid));
1087                                 DEBUGADD(106,("c_setprinter:[%d]\n", info2->c_setprinter));
1088                                 DEBUGADD(106,("setuptime:[%d]\n", info2->setuptime));
1089
1090                                 DEBUGADD(106,("servername:[%s]\n", info2->servername));
1091                                 DEBUGADD(106,("printername:[%s]\n", info2->printername));
1092                                 DEBUGADD(106,("sharename:[%s]\n", info2->sharename));
1093                                 DEBUGADD(106,("portname:[%s]\n", info2->portname));
1094                                 DEBUGADD(106,("drivername:[%s]\n", info2->drivername));
1095                                 DEBUGADD(106,("location:[%s]\n", info2->location));
1096                                 DEBUGADD(106,("sepfile:[%s]\n", info2->sepfile));
1097                                 DEBUGADD(106,("printprocessor:[%s]\n", info2->printprocessor));
1098                                 DEBUGADD(106,("datatype:[%s]\n", info2->datatype));
1099                                 DEBUGADD(106,("parameters:[%s]\n", info2->parameters));
1100                                 success=0;
1101                         }
1102                         break;
1103                 }
1104                 default:
1105                         DEBUGADD(1,("Level not implemented\n"));
1106                         success=1;
1107                         break;
1108         }
1109         
1110         return (success);
1111 }
1112
1113 /*
1114  * The function below are the high level ones.
1115  * only those ones must be called from the spoolss code.
1116  * JFM.
1117  */
1118
1119
1120 /****************************************************************************
1121 ****************************************************************************/
1122 uint32 add_a_printer(NT_PRINTER_INFO_LEVEL printer, uint32 level)
1123 {
1124         uint32 success;
1125         
1126         dump_a_printer(printer, level); 
1127         
1128         switch (level)
1129         {
1130                 case 2: 
1131                 {
1132                         success=add_a_printer_2(printer.info_2);
1133                         break;
1134                 }
1135                 default:
1136                         success=1;
1137                         break;
1138         }
1139         
1140         return (success);
1141 }
1142
1143 /****************************************************************************
1144  Get a NT_PRINTER_INFO_LEVEL struct. It returns malloced memory.
1145 ****************************************************************************/
1146
1147 uint32 get_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level, fstring sharename)
1148 {
1149         uint32 success;
1150         NT_PRINTER_INFO_LEVEL *printer = NULL;
1151         
1152         *pp_printer = NULL;
1153
1154         DEBUG(10,("get_a_printer: [%s] level %u\n", sharename, (unsigned int)level));
1155
1156         switch (level)
1157         {
1158                 case 2: 
1159                 {
1160                         if ((printer = (NT_PRINTER_INFO_LEVEL *)malloc(sizeof(NT_PRINTER_INFO_LEVEL))) == NULL) {
1161                                 DEBUG(0,("get_a_printer: malloc fail.\n"));
1162                                 return 1;
1163                         }
1164                         ZERO_STRUCTP(printer);
1165                         success=get_a_printer_2(&printer->info_2, sharename);
1166                         if (success == 0) {
1167                                 dump_a_printer(*printer, level);
1168                                 *pp_printer = printer;
1169                         } else {
1170                                 safe_free(printer);
1171                         }
1172                         break;
1173                 }
1174                 default:
1175                         success=1;
1176                         break;
1177         }
1178         
1179         DEBUG(10,("get_a_printer: [%s] level %u returning %u\n", sharename, (unsigned int)level, (unsigned int)success));
1180
1181         return (success);
1182 }
1183
1184 /****************************************************************************
1185  Deletes a NT_PRINTER_INFO_LEVEL struct.
1186 ****************************************************************************/
1187
1188 uint32 free_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level)
1189 {
1190         uint32 success;
1191         NT_PRINTER_INFO_LEVEL *printer = *pp_printer;
1192
1193         DEBUG(104,("freeing a printer at level [%d]\n", level));
1194
1195         if (printer == NULL)
1196                 return 0;
1197         
1198         switch (level)
1199         {
1200                 case 2: 
1201                 {
1202                         if (printer->info_2 != NULL)
1203                         {
1204                                 free_nt_printer_info_level_2(&printer->info_2);
1205                                 success=0;
1206                         }
1207                         else
1208                         {
1209                                 success=4;
1210                         }
1211                         break;
1212                 }
1213                 default:
1214                         success=1;
1215                         break;
1216         }
1217
1218         safe_free(printer);
1219         *pp_printer = NULL;
1220         return (success);
1221 }
1222
1223 /****************************************************************************
1224 ****************************************************************************/
1225 uint32 add_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
1226 {
1227         uint32 success;
1228         DEBUG(104,("adding a printer at level [%d]\n", level));
1229         dump_a_printer_driver(driver, level);
1230         
1231         switch (level)
1232         {
1233                 case 3: 
1234                 {
1235                         success=add_a_printer_driver_3(driver.info_3);
1236                         break;
1237                 }
1238
1239                 case 6: 
1240                 {
1241                         success=add_a_printer_driver_6(driver.info_6);
1242                         break;
1243                 }
1244                 default:
1245                         success=1;
1246                         break;
1247         }
1248         
1249         return (success);
1250 }
1251 /****************************************************************************
1252 ****************************************************************************/
1253 uint32 get_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL *driver, uint32 level, 
1254                             fstring printername, fstring architecture)
1255 {
1256         uint32 success;
1257         
1258         switch (level)
1259         {
1260                 case 3: 
1261                 {
1262                         success=get_a_printer_driver_3(&(driver->info_3), 
1263                                                        printername,
1264                                                        architecture);
1265                         break;
1266                 }
1267                 default:
1268                         success=1;
1269                         break;
1270         }
1271         
1272         if (success == 0) dump_a_printer_driver(*driver, level);
1273         return (success);
1274 }
1275
1276 /****************************************************************************
1277 ****************************************************************************/
1278 uint32 free_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
1279 {
1280         uint32 success;
1281         
1282         switch (level)
1283         {
1284                 case 3: 
1285                 {
1286                         NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3;
1287                         if (driver.info_3 != NULL)
1288                         {
1289                                 info3=driver.info_3;
1290                                 safe_free(info3->dependentfiles);
1291                                 ZERO_STRUCTP(info3);
1292                                 safe_free(info3);
1293                                 success=0;
1294                         }
1295                         else
1296                         {
1297                                 success=4;
1298                         }
1299                         break;
1300                 }
1301                 case 6: 
1302                 {
1303                         NT_PRINTER_DRIVER_INFO_LEVEL_3 *info6;
1304                         if (driver.info_6 != NULL)
1305                         {
1306                                 info6=driver.info_6;
1307                                 safe_free(info6->dependentfiles);
1308                                 ZERO_STRUCTP(info6);
1309                                 safe_free(info6);
1310                                 success=0;
1311                         }
1312                         else
1313                         {
1314                                 success=4;
1315                         }
1316                         break;
1317                 }
1318                 default:
1319                         success=1;
1320                         break;
1321         }
1322         return (success);
1323 }
1324
1325 /****************************************************************************
1326 ****************************************************************************/
1327 BOOL get_specific_param_by_index(NT_PRINTER_INFO_LEVEL printer, uint32 level, uint32 param_index,
1328                                  fstring value, uint8 **data, uint32 *type, uint32 *len)
1329 {
1330         /* right now that's enough ! */ 
1331         NT_PRINTER_PARAM *param;
1332         int i=0;
1333         
1334         param=printer.info_2->specific;
1335         
1336         while (param != NULL && i <= param_index)
1337         {
1338                 param=param->next;
1339                 i++;
1340         }
1341         
1342         if (param == NULL)
1343                 return False;
1344
1345         /* exited because it exist */
1346         *type=param->type;              
1347         StrnCpy(value, param->value, sizeof(fstring)-1);
1348         *data=(uint8 *)malloc(param->data_len*sizeof(uint8));
1349         if(*data == NULL)
1350                 return False;
1351         memcpy(*data, param->data, param->data_len);
1352         *len=param->data_len;
1353         return True;
1354 }
1355
1356 /****************************************************************************
1357 ****************************************************************************/
1358 BOOL get_specific_param(NT_PRINTER_INFO_LEVEL printer, uint32 level, 
1359                         fstring value, uint8 **data, uint32 *type, uint32 *len)
1360 {
1361         /* right now that's enough ! */ 
1362         NT_PRINTER_PARAM *param;
1363         
1364         DEBUG(105, ("get_specific_param\n"));
1365         
1366         param=printer.info_2->specific;
1367                 
1368         while (param != NULL)
1369         {
1370                 if ( !strcmp(value, param->value) 
1371                     && strlen(value)==strlen(param->value))
1372                         break;
1373                         
1374                 param=param->next;
1375         }
1376         
1377         DEBUG(106, ("found one param\n"));
1378         if (param != NULL)
1379         {
1380                 /* exited because it exist */
1381                 *type=param->type;      
1382                 
1383                 *data=(uint8 *)malloc(param->data_len*sizeof(uint8));
1384                 if(*data == NULL)
1385                         return False;
1386                 memcpy(*data, param->data, param->data_len);
1387                 *len=param->data_len;
1388
1389                 DEBUG(106, ("exit of get_specific_param:true\n"));
1390                 return (True);
1391         }
1392         DEBUG(106, ("exit of get_specific_param:false\n"));
1393         return (False);
1394 }
1395
1396
1397 /****************************************************************************
1398 store a security desc for a printer
1399 ****************************************************************************/
1400 uint32 nt_printing_setsec(char *printername, SEC_DESC_BUF *secdesc_ctr)
1401 {
1402         prs_struct ps;
1403         fstring key;
1404         uint32 status;
1405
1406         prs_init(&ps, (uint32)sec_desc_size(secdesc_ctr->sec), 4, MARSHALL);
1407
1408         if (!sec_io_desc_buf("nt_printing_setsec", &secdesc_ctr, &ps, 1)) {
1409                 status = ERROR_INVALID_FUNCTION;
1410                 goto out;
1411         }
1412
1413         slprintf(key, sizeof(key), "SECDESC/%s", printername);
1414
1415         if (tdb_prs_store(tdb, key, &ps)==0) {
1416                 status = 0;
1417         } else {
1418                 DEBUG(1,("Failed to store secdesc for %s\n", printername));
1419                 status = ERROR_INVALID_FUNCTION;
1420         }
1421
1422  out:
1423         prs_mem_free(&ps);
1424         return status;
1425 }
1426
1427 /****************************************************************************
1428  Construct a default security descriptor buffer for a printer.
1429 ****************************************************************************/
1430
1431 static SEC_DESC_BUF *construct_default_printer_sdb(void)
1432 {
1433         extern DOM_SID global_sid_World; 
1434         SEC_DESC_BUF *sdb = NULL;
1435         size_t sd_size;
1436         SEC_DESC *psd = make_sec_desc(1, SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
1437                                                                 &global_sid_World, &global_sid_World,
1438                                                                 NULL, NULL, &sd_size);
1439
1440         if (!psd) {
1441                 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
1442                 return NULL;
1443         }
1444
1445         sdb = make_sec_desc_buf(sd_size, psd);
1446
1447         free_sec_desc(&psd);
1448         return sdb;
1449 }
1450
1451 /****************************************************************************
1452  Get a security desc for a printer.
1453 ****************************************************************************/
1454
1455 BOOL nt_printing_getsec(char *printername, SEC_DESC_BUF **secdesc_ctr)
1456 {
1457         prs_struct ps;
1458         fstring key;
1459
1460         slprintf(key, sizeof(key), "SECDESC/%s", printername);
1461
1462         if (tdb_prs_fetch(tdb, key, &ps)!=0 ||
1463             !sec_io_desc_buf("nt_printing_getsec", secdesc_ctr, &ps, 1)) {
1464
1465                 DEBUG(4,("using default secdesc for %s\n", printername));
1466
1467                 if ((*secdesc_ctr = construct_default_printer_sdb()))
1468                         return False;
1469
1470                 return True;
1471         }
1472
1473         prs_mem_free(&ps);
1474         return True;
1475 }
1476
1477 /* error code:
1478         0: everything OK
1479         1: level not implemented
1480         2: file doesn't exist
1481         3: can't allocate memory
1482         4: can't free memory
1483         5: non existant struct
1484 */
1485
1486 /*
1487         A printer and a printer driver are 2 different things.
1488         NT manages them separatelly, Samba does the same.
1489         Why ? Simply because it's easier and it makes sense !
1490         
1491         Now explanation: You have 3 printers behind your samba server,
1492         2 of them are the same make and model (laser A and B). But laser B 
1493         has an 3000 sheet feeder and laser A doesn't such an option.
1494         Your third printer is an old dot-matrix model for the accounting :-).
1495         
1496         If the /usr/local/samba/lib directory (default dir), you will have
1497         5 files to describe all of this.
1498         
1499         3 files for the printers (1 by printer):
1500                 NTprinter_laser A
1501                 NTprinter_laser B
1502                 NTprinter_accounting
1503         2 files for the drivers (1 for the laser and 1 for the dot matrix)
1504                 NTdriver_printer model X
1505                 NTdriver_printer model Y
1506
1507 jfm: I should use this comment for the text file to explain 
1508         same thing for the forms BTW.
1509         Je devrais mettre mes commentaires en francais, ca serait mieux :-)
1510
1511 */
1512
1513