2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-2000,
6 * Copyright (C) Jean François Micouleau 1998-2000.
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.
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.
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.
25 extern int DEBUGLEVEL;
26 extern pstring global_myname;
28 static TDB_CONTEXT *tdb; /* used for driver files */
30 #define FORMS_PREFIX "FORMS/"
31 #define DRIVERS_PREFIX "DRIVERS/"
32 #define PRINTERS_PREFIX "PRINTERS/"
34 #define DATABASE_VERSION 1
36 /* we need to have a small set of default forms to support our
38 static nt_forms_struct default_forms[] = {
39 {"Letter", 0x20, 0x34b5b, 0x44367, 0x0, 0x0, 0x34b5b, 0x44367},
40 {"A4", 0xb0, 0x3354f, 0x4884e, 0x0, 0x0, 0x3354f, 0x4884e}
44 /****************************************************************************
45 open the NT printing tdb
46 ****************************************************************************/
47 BOOL nt_printing_init(void)
49 static pid_t local_pid;
51 if (tdb && local_pid == sys_getpid()) return True;
52 tdb = tdb_open(lock_path("ntdrivers.tdb"), 0, 0, O_RDWR|O_CREAT, 0600);
54 DEBUG(0,("Failed to open nt drivers database\n"));
58 local_pid = sys_getpid();
60 /* handle a Samba upgrade */
62 if (tdb_fetch_int(tdb, "INFO/version") != DATABASE_VERSION) {
63 tdb_traverse(tdb, (tdb_traverse_func)tdb_delete, NULL);
64 tdb_store_int(tdb, "INFO/version", DATABASE_VERSION);
72 /****************************************************************************
73 get a form struct list
74 ****************************************************************************/
75 int get_ntforms(nt_forms_struct **list)
77 TDB_DATA kbuf, newkey, dbuf;
82 for (kbuf = tdb_firstkey(tdb);
84 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
85 if (strncmp(kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) != 0) continue;
87 dbuf = tdb_fetch(tdb, kbuf);
88 if (!dbuf.dptr) continue;
90 fstrcpy(form.name, kbuf.dptr+strlen(FORMS_PREFIX));
91 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddddddd",
92 &form.flag, &form.width, &form.length, &form.left,
93 &form.top, &form.right, &form.bottom);
95 if (ret != dbuf.dsize) continue;
97 *list = Realloc(*list, sizeof(nt_forms_struct)*(n+1));
102 /* we should never return a null forms list or NT gets unhappy */
104 *list = (nt_forms_struct *)memdup(&default_forms[0], sizeof(default_forms));
105 n = sizeof(default_forms) / sizeof(default_forms[0]);
112 /****************************************************************************
113 write a form struct list
114 ****************************************************************************/
115 int write_ntforms(nt_forms_struct **list, int number)
122 for (i=0;i<number;i++) {
123 len = tdb_pack(buf, sizeof(buf), "ddddddd",
124 (*list)[i].flag, (*list)[i].width, (*list)[i].length,
125 (*list)[i].left, (*list)[i].top, (*list)[i].right,
127 if (len > sizeof(buf)) break;
128 slprintf(key, sizeof(key), "%s%s", FORMS_PREFIX, (*list)[i].name);
129 kbuf.dsize = strlen(key)+1;
133 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) break;
139 /****************************************************************************
140 add a form struct at the end of the list
141 ****************************************************************************/
142 BOOL add_a_form(nt_forms_struct **list, const FORM *form, int *count)
149 * NT tries to add forms even when
150 * they are already in the base
151 * only update the values if already present
156 unistr2_to_ascii(form_name, &(form->name), sizeof(form_name)-1);
157 for (n=0; n<*count && update==False; n++)
159 if (!strncmp((*list)[n].name, form_name, strlen(form_name)))
161 DEBUG(103, ("NT workaround, [%s] already exists\n", form_name));
168 if((*list=Realloc(*list, (n+1)*sizeof(nt_forms_struct))) == NULL)
170 unistr2_to_ascii((*list)[n].name, &(form->name), sizeof((*list)[n].name)-1);
174 (*list)[n].flag=form->flags;
175 (*list)[n].width=form->size_x;
176 (*list)[n].length=form->size_y;
177 (*list)[n].left=form->left;
178 (*list)[n].top=form->top;
179 (*list)[n].right=form->right;
180 (*list)[n].bottom=form->bottom;
185 /****************************************************************************
187 ****************************************************************************/
188 void update_a_form(nt_forms_struct **list, const FORM *form, int count)
192 unistr2_to_ascii(form_name, &(form->name), sizeof(form_name)-1);
194 DEBUG(106, ("[%s]\n", form_name));
195 for (n=0; n<count; n++)
197 DEBUGADD(106, ("n [%d]:[%s]\n", n, (*list)[n].name));
198 if (!strncmp((*list)[n].name, form_name, strlen(form_name)))
202 if (n==count) return;
204 (*list)[n].flag=form->flags;
205 (*list)[n].width=form->size_x;
206 (*list)[n].length=form->size_y;
207 (*list)[n].left=form->left;
208 (*list)[n].top=form->top;
209 (*list)[n].right=form->right;
210 (*list)[n].bottom=form->bottom;
213 /****************************************************************************
214 get the nt drivers list
216 traverse the database and look-up the matching names
217 ****************************************************************************/
218 int get_ntdrivers(fstring **list, char *architecture)
223 TDB_DATA kbuf, newkey;
225 get_short_archi(short_archi, architecture);
226 slprintf(key, sizeof(key), "%s%s/", DRIVERS_PREFIX, short_archi);
228 for (kbuf = tdb_firstkey(tdb);
230 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
231 if (strncmp(kbuf.dptr, key, strlen(key)) != 0) continue;
233 if((*list = Realloc(*list, sizeof(fstring)*(total+1))) == NULL)
236 fstrcpy((*list)[total], kbuf.dptr+strlen(key));
243 /****************************************************************************
244 function to do the mapping between the long architecture name and
246 ****************************************************************************/
247 void get_short_archi(char *short_archi, char *long_archi)
254 struct table archi_table[]=
256 {"Windows 4.0", "WIN40" },
257 {"Windows NT x86", "W32X86" },
258 {"Windows NT R4000", "W32mips" },
259 {"Windows NT Alpha_AXP", "W32alpha" },
260 {"Windows NT PowerPC", "W32ppc" },
266 DEBUG(107,("Getting architecture dependant directory\n"));
269 } while ( (archi_table[i].long_archi!=NULL ) && strncmp(long_archi, archi_table[i].long_archi, strlen(long_archi)) );
271 if (archi_table[i].long_archi==NULL)
273 DEBUGADD(107,("Unknown architecture [%s] !\n", long_archi));
275 StrnCpy (short_archi, archi_table[i].short_archi, strlen(archi_table[i].short_archi));
277 DEBUGADD(108,("index: [%d]\n", i));
278 DEBUGADD(108,("long architecture: [%s]\n", long_archi));
279 DEBUGADD(108,("short architecture: [%s]\n", short_archi));
282 /****************************************************************************
283 ****************************************************************************/
284 static uint32 add_a_printer_driver_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver)
287 fstring architecture;
293 get_short_archi(architecture, driver->environment);
294 slprintf(key, sizeof(key), "%s%s/%s", DRIVERS_PREFIX, architecture, driver->name);
297 * cversion must be 2.
298 * when adding a printer ON the SERVER
299 * rpcAddPrinterDriver defines it to zero
311 len += tdb_pack(buf+len, buflen-len, "dffffffff",
320 driver->defaultdatatype);
322 if (driver->dependentfiles) {
323 for (i=0; *driver->dependentfiles[i]; i++) {
324 len += tdb_pack(buf+len, buflen-len, "f",
325 driver->dependentfiles[i]);
330 buf = (char *)Realloc(buf, len);
337 kbuf.dsize = strlen(key)+1;
341 ret = tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
347 /****************************************************************************
348 ****************************************************************************/
349 static uint32 add_a_printer_driver_6(NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver)
351 NT_PRINTER_DRIVER_INFO_LEVEL_3 info3;
354 info3.cversion = driver->version;
355 fstrcpy(info3.environment,driver->environment);
356 fstrcpy(info3.driverpath,driver->driverpath);
357 fstrcpy(info3.datafile,driver->datafile);
358 fstrcpy(info3.configfile,driver->configfile);
359 fstrcpy(info3.helpfile,driver->helpfile);
360 fstrcpy(info3.monitorname,driver->monitorname);
361 fstrcpy(info3.defaultdatatype,driver->defaultdatatype);
362 info3.dependentfiles = driver->dependentfiles;
364 return add_a_printer_driver_3(&info3);
368 /****************************************************************************
369 ****************************************************************************/
370 static uint32 get_a_printer_driver_3_default(NT_PRINTER_DRIVER_INFO_LEVEL_3 **info_ptr, fstring in_prt, fstring in_arch)
372 NT_PRINTER_DRIVER_INFO_LEVEL_3 info;
376 fstrcpy(info.name, in_prt);
377 fstrcpy(info.defaultdatatype, "RAW");
379 fstrcpy(info.driverpath, "");
380 fstrcpy(info.datafile, "");
381 fstrcpy(info.configfile, "");
382 fstrcpy(info.helpfile, "");
384 if ((info.dependentfiles=(fstring *)malloc(2*sizeof(fstring))) == NULL)
385 return ERROR_NOT_ENOUGH_MEMORY;
387 memset(info.dependentfiles, '\0', 2*sizeof(fstring));
388 fstrcpy(info.dependentfiles[0], "");
390 *info_ptr = memdup(&info, sizeof(info));
395 /****************************************************************************
396 ****************************************************************************/
397 static uint32 get_a_printer_driver_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 **info_ptr, fstring in_prt, fstring in_arch)
399 NT_PRINTER_DRIVER_INFO_LEVEL_3 driver;
401 fstring architecture;
408 get_short_archi(architecture, in_arch);
409 slprintf(key, sizeof(key), "%s%s/%s", DRIVERS_PREFIX, architecture, in_prt);
412 kbuf.dsize = strlen(key)+1;
414 dbuf = tdb_fetch(tdb, kbuf);
415 if (!dbuf.dptr) return get_a_printer_driver_3_default(info_ptr, in_prt, in_arch);
417 len += tdb_unpack(dbuf.dptr, dbuf.dsize, "dffffffff",
426 driver.defaultdatatype);
429 while (len < dbuf.dsize) {
430 driver.dependentfiles = (fstring *)Realloc(driver.dependentfiles,
431 sizeof(fstring)*(i+2));
432 if (driver.dependentfiles == NULL)
435 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "f",
436 &driver.dependentfiles[i]);
439 if (driver.dependentfiles != NULL)
440 fstrcpy(driver.dependentfiles[i], "");
442 safe_free(dbuf.dptr);
444 if (len != dbuf.dsize) {
445 if (driver.dependentfiles != NULL)
446 safe_free(driver.dependentfiles);
448 return get_a_printer_driver_3_default(info_ptr, in_prt, in_arch);
451 *info_ptr = (NT_PRINTER_DRIVER_INFO_LEVEL_3 *)memdup(&driver, sizeof(driver));
456 /****************************************************************************
457 debugging function, dump at level 6 the struct in the logs
458 ****************************************************************************/
459 static uint32 dump_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
462 NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3;
465 DEBUG(106,("Dumping printer driver at level [%d]\n", level));
471 if (driver.info_3 == NULL)
476 DEBUGADD(106,("version:[%d]\n", info3->cversion));
477 DEBUGADD(106,("name:[%s]\n", info3->name));
478 DEBUGADD(106,("environment:[%s]\n", info3->environment));
479 DEBUGADD(106,("driverpath:[%s]\n", info3->driverpath));
480 DEBUGADD(106,("datafile:[%s]\n", info3->datafile));
481 DEBUGADD(106,("configfile:[%s]\n", info3->configfile));
482 DEBUGADD(106,("helpfile:[%s]\n", info3->helpfile));
483 DEBUGADD(106,("monitorname:[%s]\n", info3->monitorname));
484 DEBUGADD(106,("defaultdatatype:[%s]\n", info3->defaultdatatype));
486 for (i=0; info3->dependentfiles &&
487 *info3->dependentfiles[i]; i++) {
488 DEBUGADD(106,("dependentfile:[%s]\n",
489 info3->dependentfiles[i]));
496 DEBUGADD(1,("Level not implemented\n"));
504 /****************************************************************************
505 ****************************************************************************/
506 static int pack_devicemode(NT_DEVICEMODE *nt_devmode, char *buf, int buflen)
510 len += tdb_pack(buf+len, buflen-len, "p", nt_devmode);
512 if (!nt_devmode) return len;
514 len += tdb_pack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
515 nt_devmode->devicename,
516 nt_devmode->formname,
518 nt_devmode->specversion,
519 nt_devmode->driverversion,
521 nt_devmode->driverextra,
522 nt_devmode->orientation,
523 nt_devmode->papersize,
524 nt_devmode->paperlength,
525 nt_devmode->paperwidth,
528 nt_devmode->defaultsource,
529 nt_devmode->printquality,
532 nt_devmode->yresolution,
533 nt_devmode->ttoption,
535 nt_devmode->logpixels,
538 nt_devmode->bitsperpel,
539 nt_devmode->pelswidth,
540 nt_devmode->pelsheight,
541 nt_devmode->displayflags,
542 nt_devmode->displayfrequency,
543 nt_devmode->icmmethod,
544 nt_devmode->icmintent,
545 nt_devmode->mediatype,
546 nt_devmode->dithertype,
547 nt_devmode->reserved1,
548 nt_devmode->reserved2,
549 nt_devmode->panningwidth,
550 nt_devmode->panningheight,
551 nt_devmode->private);
554 if (nt_devmode->private) {
555 len += tdb_pack(buf+len, buflen-len, "B",
556 nt_devmode->driverextra,
557 nt_devmode->private);
560 DEBUG(8,("Packed devicemode [%s]\n", nt_devmode->formname));
565 /****************************************************************************
566 ****************************************************************************/
567 static int pack_specifics(NT_PRINTER_PARAM *param, char *buf, int buflen)
571 while (param != NULL) {
572 len += tdb_pack(buf+len, buflen-len, "pfdB",
581 len += tdb_pack(buf+len, buflen-len, "p", param);
587 /****************************************************************************
588 delete a printer - this just deletes the printer info file, any open
589 handles are not affected
590 ****************************************************************************/
591 uint32 del_a_printer(char *portname)
596 slprintf(key, sizeof(key), "%s%s",
597 PRINTERS_PREFIX, portname);
600 kbuf.dsize=strlen(key)+1;
602 tdb_delete(tdb, kbuf);
606 /****************************************************************************
607 ****************************************************************************/
608 static uint32 add_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info)
612 int buflen, len, ret;
618 * in addprinter: no servername and the printer is the name
619 * in setprinter: servername is \\server
620 * and printer is \\server\\printer
622 * Samba manages only local printers.
623 * we currently don't support things like path=\\other_server\printer
625 if (info->servername[0]!='\0')
627 trim_string(info->printername, info->servername, NULL);
628 trim_string(info->printername, "\\", NULL);
629 info->servername[0]='\0';
633 * JFM: one day I'll forget.
634 * below that's info->portname because that's the SAMBA sharename
635 * and I made NT 'thinks' it's the portname
636 * the info->sharename is the thing you can name when you add a printer
637 * that's the short-name when you create shared printer for 95/98
638 * So I've made a limitation in SAMBA: you can only have 1 printer model
639 * behind a SAMBA share.
642 unix_to_nt_time(&time_nt, time_unix);
643 info->changeid=time_nt.low;
644 info->c_setprinter++;
651 len += tdb_pack(buf+len, buflen-len, "dddddddddddffffffffff",
654 info->default_priority,
670 info->printprocessor,
674 len += pack_devicemode(info->devmode, buf+len, buflen-len);
675 len += pack_specifics(info->specific, buf+len, buflen-len);
678 buf = (char *)Realloc(buf, len);
684 slprintf(key, sizeof(key), "%s%s",
685 PRINTERS_PREFIX, info->sharename);
688 kbuf.dsize = strlen(key)+1;
692 ret = tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
695 DEBUG(8, ("error updating printer to tdb on disk\n"));
699 DEBUG(8,("packed printer [%s] with driver [%s] portname=[%s] len=%d\n",
700 info->portname, info->drivername, info->portname, len));
706 /****************************************************************************
707 ****************************************************************************/
708 BOOL add_a_specific_param(NT_PRINTER_INFO_LEVEL_2 *info_2, NT_PRINTER_PARAM *param)
710 NT_PRINTER_PARAM *current;
712 DEBUG(108,("add_a_specific_param\n"));
716 if (info_2->specific == NULL)
718 info_2->specific=param;
722 current=info_2->specific;
723 while (current->next != NULL) {
724 current=current->next;
731 /****************************************************************************
732 ****************************************************************************/
733 BOOL unlink_specific_param_if_exist(NT_PRINTER_INFO_LEVEL_2 *info_2, NT_PRINTER_PARAM *param)
735 NT_PRINTER_PARAM *current;
736 NT_PRINTER_PARAM *previous;
738 current=info_2->specific;
741 if (current==NULL) return (False);
743 if ( !strcmp(current->value, param->value) &&
744 (strlen(current->value)==strlen(param->value)) ) {
745 DEBUG(109,("deleting first value\n"));
746 info_2->specific=current->next;
747 safe_free(current->data);
749 DEBUG(109,("deleted first value\n"));
753 current=previous->next;
755 while ( current!=NULL ) {
756 if (!strcmp(current->value, param->value) &&
757 strlen(current->value)==strlen(param->value) ) {
758 DEBUG(109,("deleting current value\n"));
759 previous->next=current->next;
760 safe_free(current->data);
762 DEBUG(109,("deleted current value\n"));
766 previous=previous->next;
767 current=current->next;
772 /****************************************************************************
773 Clean up and deallocate a (maybe partially) allocated NT_PRINTER_PARAM.
774 ****************************************************************************/
775 static void free_nt_printer_param(NT_PRINTER_PARAM **param_ptr)
777 NT_PRINTER_PARAM *param = *param_ptr;
782 DEBUG(106,("free_nt_printer_param: deleting param [%s]\n", param->value));
785 safe_free(param->data);
791 /****************************************************************************
792 Malloc and return an NT devicemode.
793 ****************************************************************************/
795 NT_DEVICEMODE *construct_nt_devicemode(const fstring default_devicename)
798 * should I init this ones ???
799 nt_devmode->devicename
803 NT_DEVICEMODE *nt_devmode = (NT_DEVICEMODE *)malloc(sizeof(NT_DEVICEMODE));
805 if (nt_devmode == NULL) {
806 DEBUG(0,("construct_nt_devicemode: malloc fail.\n"));
810 ZERO_STRUCTP(nt_devmode);
812 snprintf(adevice, sizeof(adevice), "\\\\%s\\%s", global_myname, default_devicename);
813 fstrcpy(nt_devmode->devicename, adevice);
816 fstrcpy(nt_devmode->formname, "Letter");
818 nt_devmode->specversion = 0x0401;
819 nt_devmode->driverversion = 0x0400;
820 nt_devmode->size = 0x00DC;
821 nt_devmode->driverextra = 0x0000;
822 nt_devmode->fields = FORMNAME | TTOPTION | PRINTQUALITY |
823 DEFAULTSOURCE | COPIES | SCALE |
824 PAPERSIZE | ORIENTATION;
825 nt_devmode->orientation = 1;
826 nt_devmode->papersize = PAPER_LETTER;
827 nt_devmode->paperlength = 0;
828 nt_devmode->paperwidth = 0;
829 nt_devmode->scale = 0x64;
830 nt_devmode->copies = 01;
831 nt_devmode->defaultsource = BIN_FORMSOURCE;
832 nt_devmode->printquality = 0x0258;
833 nt_devmode->color = COLOR_MONOCHROME;
834 nt_devmode->duplex = DUP_SIMPLEX;
835 nt_devmode->yresolution = 0;
836 nt_devmode->ttoption = TT_SUBDEV;
837 nt_devmode->collate = COLLATE_FALSE;
838 nt_devmode->icmmethod = 0;
839 nt_devmode->icmintent = 0;
840 nt_devmode->mediatype = 0;
841 nt_devmode->dithertype = 0;
843 /* non utilisés par un driver d'imprimante */
844 nt_devmode->logpixels = 0;
845 nt_devmode->bitsperpel = 0;
846 nt_devmode->pelswidth = 0;
847 nt_devmode->pelsheight = 0;
848 nt_devmode->displayflags = 0;
849 nt_devmode->displayfrequency = 0;
850 nt_devmode->reserved1 = 0;
851 nt_devmode->reserved2 = 0;
852 nt_devmode->panningwidth = 0;
853 nt_devmode->panningheight = 0;
855 nt_devmode->private=NULL;
860 /****************************************************************************
861 Deepcopy an NT devicemode.
862 ****************************************************************************/
864 NT_DEVICEMODE *dup_nt_devicemode(NT_DEVICEMODE *nt_devicemode)
866 NT_DEVICEMODE *new_nt_devicemode = NULL;
868 if ((new_nt_devicemode = (NT_DEVICEMODE *)memdup(nt_devicemode, sizeof(NT_DEVICEMODE))) == NULL) {
869 DEBUG(0,("dup_nt_devicemode: malloc fail.\n"));
873 new_nt_devicemode->private = NULL;
874 if (nt_devicemode->private != NULL) {
875 if ((new_nt_devicemode->private = memdup(nt_devicemode->private, nt_devicemode->driverextra)) == NULL) {
876 safe_free(new_nt_devicemode);
877 DEBUG(0,("dup_nt_devicemode: malloc fail.\n"));
882 return new_nt_devicemode;
885 /****************************************************************************
886 Clean up and deallocate a (maybe partially) allocated NT_DEVICEMODE.
887 ****************************************************************************/
889 void free_nt_devicemode(NT_DEVICEMODE **devmode_ptr)
891 NT_DEVICEMODE *nt_devmode = *devmode_ptr;
893 if(nt_devmode == NULL)
896 DEBUG(106,("free_nt_devicemode: deleting DEVMODE\n"));
898 if(nt_devmode->private)
899 safe_free(nt_devmode->private);
901 safe_free(nt_devmode);
905 /****************************************************************************
906 Clean up and deallocate a (maybe partially) allocated NT_PRINTER_INFO_LEVEL_2.
907 ****************************************************************************/
908 static void free_nt_printer_info_level_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr)
910 NT_PRINTER_INFO_LEVEL_2 *info = *info_ptr;
911 NT_PRINTER_PARAM *param_ptr;
916 DEBUG(106,("free_nt_printer_info_level_2: deleting info\n"));
918 free_nt_devicemode(&info->devmode);
919 free_sec_desc_buf(&info->secdesc_buf);
921 for(param_ptr = info->specific; param_ptr; ) {
922 NT_PRINTER_PARAM *tofree = param_ptr;
924 param_ptr = param_ptr->next;
925 free_nt_printer_param(&tofree);
928 safe_free(*info_ptr);
933 /****************************************************************************
934 ****************************************************************************/
935 static int unpack_devicemode(NT_DEVICEMODE **nt_devmode, char *buf, int buflen)
938 NT_DEVICEMODE devmode;
940 ZERO_STRUCT(devmode);
942 len += tdb_unpack(buf+len, buflen-len, "p", nt_devmode);
944 if (!*nt_devmode) return len;
946 len += tdb_unpack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
950 &devmode.specversion,
951 &devmode.driverversion,
953 &devmode.driverextra,
954 &devmode.orientation,
956 &devmode.paperlength,
960 &devmode.defaultsource,
961 &devmode.printquality,
964 &devmode.yresolution,
973 &devmode.displayflags,
974 &devmode.displayfrequency,
981 &devmode.panningwidth,
982 &devmode.panningheight,
986 len += tdb_unpack(buf+len, buflen-len, "B", &devmode.driverextra, &devmode.private);
988 *nt_devmode = (NT_DEVICEMODE *)memdup(&devmode, sizeof(devmode));
990 DEBUG(8,("Unpacked devicemode [%s](%s)\n", devmode.devicename, devmode.formname));
992 DEBUG(8,("with a private section of %d bytes\n", devmode.driverextra));
997 /****************************************************************************
998 ****************************************************************************/
999 static int unpack_specifics(NT_PRINTER_PARAM **list, char *buf, int buflen)
1002 NT_PRINTER_PARAM param, *p;
1007 len += tdb_unpack(buf+len, buflen-len, "p", &p);
1010 len += tdb_unpack(buf+len, buflen-len, "fdB",
1016 *list = memdup(¶m, sizeof(param));
1023 /****************************************************************************
1024 get a default printer info 2 struct
1025 ****************************************************************************/
1026 static uint32 get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 **info_ptr, fstring sharename)
1028 extern pstring global_myname;
1030 NT_PRINTER_INFO_LEVEL_2 info;
1034 snum = lp_servicenumber(sharename);
1036 fstrcpy(info.servername, global_myname);
1037 fstrcpy(info.printername, sharename);
1038 fstrcpy(info.portname, sharename);
1039 fstrcpy(info.drivername, lp_printerdriver(snum));
1040 fstrcpy(info.printprocessor, "winprint");
1041 fstrcpy(info.datatype, "RAW");
1043 info.attributes = PRINTER_ATTRIBUTE_SHARED \
1044 | PRINTER_ATTRIBUTE_LOCAL \
1045 | PRINTER_ATTRIBUTE_RAW_ONLY ; /* attributes */
1047 info.starttime = 0; /* Minutes since 12:00am GMT */
1048 info.untiltime = 0; /* Minutes since 12:00am GMT */
1050 info.default_priority = 1;
1052 if ((info.devmode = construct_nt_devicemode(info.printername)) == NULL)
1055 if (!nt_printing_getsec(sharename, &info.secdesc_buf))
1058 *info_ptr = (NT_PRINTER_INFO_LEVEL_2 *)memdup(&info, sizeof(info));
1060 DEBUG(0,("get_a_printer_2_default: malloc fail.\n"));
1069 free_nt_devicemode(&info.devmode);
1070 if (info.secdesc_buf)
1071 free_sec_desc_buf(&info.secdesc_buf);
1075 /****************************************************************************
1076 ****************************************************************************/
1077 static uint32 get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr, fstring sharename)
1080 NT_PRINTER_INFO_LEVEL_2 info;
1082 TDB_DATA kbuf, dbuf;
1086 slprintf(key, sizeof(key), "%s%s",
1087 PRINTERS_PREFIX, sharename);
1090 kbuf.dsize = strlen(key)+1;
1092 dbuf = tdb_fetch(tdb, kbuf);
1094 if (!dbuf.dptr) return get_a_printer_2_default(info_ptr, sharename);
1096 if (!dbuf.dptr) return 1;
1099 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "dddddddddddffffffffff",
1102 &info.default_priority,
1118 info.printprocessor,
1122 info.attributes |= PRINTER_ATTRIBUTE_RAW_ONLY; /* Samba has to have raw drivers. */
1124 len += unpack_devicemode(&info.devmode,dbuf.dptr+len, dbuf.dsize-len);
1125 len += unpack_specifics(&info.specific,dbuf.dptr+len, dbuf.dsize-len);
1128 nt_printing_getsec(sharename, &info.secdesc_buf);
1129 #endif /* JRATEST */
1131 fstrcpy(info.sharename, "");
1133 safe_free(dbuf.dptr);
1134 *info_ptr=memdup(&info, sizeof(info));
1136 DEBUG(9,("Unpacked printer [%s] running drier [%s]\n",
1137 sharename, info.drivername));
1143 /****************************************************************************
1144 debugging function, dump at level 6 the struct in the logs
1145 ****************************************************************************/
1146 static uint32 dump_a_printer(NT_PRINTER_INFO_LEVEL printer, uint32 level)
1149 NT_PRINTER_INFO_LEVEL_2 *info2;
1151 DEBUG(106,("Dumping printer at level [%d]\n", level));
1157 if (printer.info_2 == NULL)
1161 info2=printer.info_2;
1163 DEBUGADD(106,("attributes:[%d]\n", info2->attributes));
1164 DEBUGADD(106,("priority:[%d]\n", info2->priority));
1165 DEBUGADD(106,("default_priority:[%d]\n", info2->default_priority));
1166 DEBUGADD(106,("starttime:[%d]\n", info2->starttime));
1167 DEBUGADD(106,("untiltime:[%d]\n", info2->untiltime));
1168 DEBUGADD(106,("status:[%d]\n", info2->status));
1169 DEBUGADD(106,("cjobs:[%d]\n", info2->cjobs));
1170 DEBUGADD(106,("averageppm:[%d]\n", info2->averageppm));
1171 DEBUGADD(106,("changeid:[%d]\n", info2->changeid));
1172 DEBUGADD(106,("c_setprinter:[%d]\n", info2->c_setprinter));
1173 DEBUGADD(106,("setuptime:[%d]\n", info2->setuptime));
1175 DEBUGADD(106,("servername:[%s]\n", info2->servername));
1176 DEBUGADD(106,("printername:[%s]\n", info2->printername));
1177 DEBUGADD(106,("sharename:[%s]\n", info2->sharename));
1178 DEBUGADD(106,("portname:[%s]\n", info2->portname));
1179 DEBUGADD(106,("drivername:[%s]\n", info2->drivername));
1180 DEBUGADD(106,("location:[%s]\n", info2->location));
1181 DEBUGADD(106,("sepfile:[%s]\n", info2->sepfile));
1182 DEBUGADD(106,("printprocessor:[%s]\n", info2->printprocessor));
1183 DEBUGADD(106,("datatype:[%s]\n", info2->datatype));
1184 DEBUGADD(106,("parameters:[%s]\n", info2->parameters));
1190 DEBUGADD(1,("Level not implemented\n"));
1199 * The function below are the high level ones.
1200 * only those ones must be called from the spoolss code.
1205 /****************************************************************************
1206 ****************************************************************************/
1207 uint32 add_a_printer(NT_PRINTER_INFO_LEVEL printer, uint32 level)
1211 dump_a_printer(printer, level);
1217 success=add_a_printer_2(printer.info_2);
1228 /****************************************************************************
1229 Get a NT_PRINTER_INFO_LEVEL struct. It returns malloced memory.
1230 ****************************************************************************/
1232 uint32 get_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level, fstring sharename)
1235 NT_PRINTER_INFO_LEVEL *printer = NULL;
1239 DEBUG(10,("get_a_printer: [%s] level %u\n", sharename, (unsigned int)level));
1245 if ((printer = (NT_PRINTER_INFO_LEVEL *)malloc(sizeof(NT_PRINTER_INFO_LEVEL))) == NULL) {
1246 DEBUG(0,("get_a_printer: malloc fail.\n"));
1249 ZERO_STRUCTP(printer);
1250 success=get_a_printer_2(&printer->info_2, sharename);
1252 dump_a_printer(*printer, level);
1253 *pp_printer = printer;
1264 DEBUG(10,("get_a_printer: [%s] level %u returning %u\n", sharename, (unsigned int)level, (unsigned int)success));
1269 /****************************************************************************
1270 Deletes a NT_PRINTER_INFO_LEVEL struct.
1271 ****************************************************************************/
1273 uint32 free_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level)
1276 NT_PRINTER_INFO_LEVEL *printer = *pp_printer;
1278 DEBUG(104,("freeing a printer at level [%d]\n", level));
1280 if (printer == NULL)
1287 if (printer->info_2 != NULL)
1289 free_nt_printer_info_level_2(&printer->info_2);
1308 /****************************************************************************
1309 ****************************************************************************/
1310 uint32 add_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
1313 DEBUG(104,("adding a printer at level [%d]\n", level));
1314 dump_a_printer_driver(driver, level);
1320 success=add_a_printer_driver_3(driver.info_3);
1326 success=add_a_printer_driver_6(driver.info_6);
1336 /****************************************************************************
1337 ****************************************************************************/
1338 uint32 get_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL *driver, uint32 level,
1339 fstring printername, fstring architecture)
1347 success=get_a_printer_driver_3(&(driver->info_3),
1357 if (success == 0) dump_a_printer_driver(*driver, level);
1361 /****************************************************************************
1362 ****************************************************************************/
1363 uint32 free_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
1371 NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3;
1372 if (driver.info_3 != NULL)
1374 info3=driver.info_3;
1375 safe_free(info3->dependentfiles);
1376 ZERO_STRUCTP(info3);
1388 NT_PRINTER_DRIVER_INFO_LEVEL_6 *info6;
1389 if (driver.info_6 != NULL)
1391 info6=driver.info_6;
1392 safe_free(info6->dependentfiles);
1393 safe_free(info6->previousnames);
1394 ZERO_STRUCTP(info6);
1411 /****************************************************************************
1412 ****************************************************************************/
1413 BOOL get_specific_param_by_index(NT_PRINTER_INFO_LEVEL printer, uint32 level, uint32 param_index,
1414 fstring value, uint8 **data, uint32 *type, uint32 *len)
1416 /* right now that's enough ! */
1417 NT_PRINTER_PARAM *param;
1420 param=printer.info_2->specific;
1422 while (param != NULL && i < param_index) {
1430 /* exited because it exist */
1432 StrnCpy(value, param->value, sizeof(fstring)-1);
1433 *data=(uint8 *)malloc(param->data_len*sizeof(uint8));
1436 ZERO_STRUCTP(*data);
1437 memcpy(*data, param->data, param->data_len);
1438 *len=param->data_len;
1442 /****************************************************************************
1443 ****************************************************************************/
1444 BOOL get_specific_param(NT_PRINTER_INFO_LEVEL printer, uint32 level,
1445 fstring value, uint8 **data, uint32 *type, uint32 *len)
1447 /* right now that's enough ! */
1448 NT_PRINTER_PARAM *param;
1450 DEBUG(105, ("get_specific_param\n"));
1452 param=printer.info_2->specific;
1454 while (param != NULL)
1456 if ( !strcmp(value, param->value)
1457 && strlen(value)==strlen(param->value))
1463 DEBUG(106, ("found one param\n"));
1466 /* exited because it exist */
1469 *data=(uint8 *)malloc(param->data_len*sizeof(uint8));
1472 memcpy(*data, param->data, param->data_len);
1473 *len=param->data_len;
1475 DEBUG(106, ("exit of get_specific_param:true\n"));
1478 DEBUG(106, ("exit of get_specific_param:false\n"));
1483 /****************************************************************************
1484 store a security desc for a printer
1485 ****************************************************************************/
1486 uint32 nt_printing_setsec(char *printername, struct current_user *user,
1487 SEC_DESC_BUF *secdesc_ctr)
1489 SEC_DESC_BUF *new_secdesc_ctr = NULL;
1490 SEC_DESC_BUF *old_secdesc_ctr = NULL;
1493 uint32 acc_granted, status;
1495 /* Get old security descriptor */
1497 if (!nt_printing_getsec(printername, &old_secdesc_ctr)) {
1498 DEBUG(3, ("could not get old security descriptor for "
1499 "printer %s", printername));
1500 return ERROR_INVALID_FUNCTION;
1503 /* Check the user has permissions to change the security
1504 descriptor. By experimentation with two NT machines, the user
1505 requires Full Access to the printer to change security
1508 if (!se_access_check(old_secdesc_ctr->sec, user->uid, user->gid,
1509 user->ngroups, user->groups,
1510 PRINTER_ACE_FULL_CONTROL, &acc_granted,
1512 DEBUG(3, ("security descriptor change denied by existing "
1513 "security descriptor\n"));
1514 free_sec_desc_buf(&old_secdesc_ctr);
1518 /* The old owner and group sids of the security descriptor are not
1519 present when new ACEs are added or removed by changing printer
1520 permissions through NT. If they are NULL in the new security
1521 descriptor then copy them over from the old one. */
1523 if (!secdesc_ctr->sec->owner_sid || !secdesc_ctr->sec->grp_sid) {
1524 DOM_SID *owner_sid, *group_sid;
1525 SEC_DESC *psd = NULL;
1528 /* Pick out correct owner and group sids */
1530 owner_sid = secdesc_ctr->sec->owner_sid ?
1531 secdesc_ctr->sec->owner_sid :
1532 old_secdesc_ctr->sec->owner_sid;
1534 group_sid = secdesc_ctr->sec->grp_sid ?
1535 secdesc_ctr->sec->grp_sid :
1536 old_secdesc_ctr->sec->grp_sid;
1538 /* Make a deep copy of the security descriptor */
1540 psd = make_sec_desc(secdesc_ctr->sec->revision,
1541 secdesc_ctr->sec->type,
1542 owner_sid, group_sid,
1543 secdesc_ctr->sec->sacl,
1544 secdesc_ctr->sec->dacl,
1547 new_secdesc_ctr = make_sec_desc_buf(size, psd);
1549 /* Free up memory */
1551 free_sec_desc(&psd);
1554 if (!new_secdesc_ctr) {
1555 new_secdesc_ctr = secdesc_ctr;
1558 /* Store the security descriptor in a tdb */
1560 prs_init(&ps, (uint32)sec_desc_size(new_secdesc_ctr->sec) +
1561 sizeof(SEC_DESC_BUF), 4, MARSHALL);
1563 if (!sec_io_desc_buf("nt_printing_setsec", &new_secdesc_ctr,
1565 status = ERROR_INVALID_FUNCTION;
1569 slprintf(key, sizeof(key), "SECDESC/%s", printername);
1571 if (tdb_prs_store(tdb, key, &ps)==0) {
1574 DEBUG(1,("Failed to store secdesc for %s\n", printername));
1575 status = ERROR_INVALID_FUNCTION;
1578 /* Free mallocated memory */
1581 free_sec_desc_buf(&old_secdesc_ctr);
1583 if (new_secdesc_ctr != secdesc_ctr) {
1584 free_sec_desc_buf(&new_secdesc_ctr);
1591 /****************************************************************************
1592 Construct a default security descriptor buffer for a printer.
1593 ****************************************************************************/
1595 static SEC_DESC_BUF *construct_default_printer_sdb(void)
1597 extern DOM_SID global_sid_World;
1600 SEC_ACL *psa = NULL;
1601 SEC_DESC_BUF *sdb = NULL;
1602 SEC_DESC *psd = NULL;
1607 /* Create an ACE where Everyone is allowed to print */
1609 init_sec_access(&sa, PRINTER_ACE_PRINT);
1610 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
1611 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
1613 /* Make the security descriptor owned by the Administrators group
1614 on the PDC of the domain. */
1616 if (!winbind_lookup_name("Administrator", &owner_sid, &name_type)) {
1618 * Backup - make owner the everyone sid. This may be a security
1619 * hole for print control .... check. JRA.
1621 sid_copy( &owner_sid, &global_sid_World);
1624 /* The ACL revision number in rpc_secdesc.h differs from the one
1625 created by NT when setting ACE entries in printer
1626 descriptors. NT4 complains about the property being edited by a
1629 #define NT4_ACL_REVISION 0x2
1631 if ((psa = make_sec_acl(NT4_ACL_REVISION, 1, &ace)) != NULL) {
1632 psd = make_sec_desc(SEC_DESC_REVISION,
1633 SEC_DESC_SELF_RELATIVE |
1634 SEC_DESC_DACL_PRESENT,
1636 NULL, psa, &sd_size);
1641 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
1645 sdb = make_sec_desc_buf(sd_size, psd);
1647 DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
1648 (unsigned int)sd_size));
1650 free_sec_desc(&psd);
1654 /****************************************************************************
1655 Get a security desc for a printer.
1656 ****************************************************************************/
1658 BOOL nt_printing_getsec(char *printername, SEC_DESC_BUF **secdesc_ctr)
1663 slprintf(key, sizeof(key), "SECDESC/%s", printername);
1665 if (tdb_prs_fetch(tdb, key, &ps)!=0 ||
1666 !sec_io_desc_buf("nt_printing_getsec", secdesc_ctr, &ps, 1)) {
1668 DEBUG(4,("using default secdesc for %s\n", printername));
1670 if (!(*secdesc_ctr = construct_default_printer_sdb()))
1682 1: level not implemented
1683 2: file doesn't exist
1684 3: can't allocate memory
1685 4: can't free memory
1686 5: non existant struct
1690 A printer and a printer driver are 2 different things.
1691 NT manages them separatelly, Samba does the same.
1692 Why ? Simply because it's easier and it makes sense !
1694 Now explanation: You have 3 printers behind your samba server,
1695 2 of them are the same make and model (laser A and B). But laser B
1696 has an 3000 sheet feeder and laser A doesn't such an option.
1697 Your third printer is an old dot-matrix model for the accounting :-).
1699 If the /usr/local/samba/lib directory (default dir), you will have
1700 5 files to describe all of this.
1702 3 files for the printers (1 by printer):
1705 NTprinter_accounting
1706 2 files for the drivers (1 for the laser and 1 for the dot matrix)
1707 NTdriver_printer model X
1708 NTdriver_printer model Y
1710 jfm: I should use this comment for the text file to explain
1711 same thing for the forms BTW.
1712 Je devrais mettre mes commentaires en francais, ca serait mieux :-)
1716 /* Check a user has permissions to perform the given operation */
1718 BOOL print_access_check(struct current_user *user, int snum,
1719 uint32 required_access)
1721 SEC_DESC_BUF *secdesc = NULL;
1722 uint32 access_granted, status;
1727 /* Get printer name */
1729 pname = PRINTERNAME(snum);
1730 if (!pname || !*pname) pname = SERVICE(snum);
1732 /* Get printer security descriptor */
1734 nt_printing_getsec(pname, &secdesc);
1736 /* The ACE for Full Control in a printer security descriptor
1737 doesn't seem to map properly to the access checking model. For
1738 it to work properly it should be the logical OR of all the other
1739 values, i.e PRINTER_ACE_MANAGE_DOCUMENTS | PRINTER_ACE_PRINT.
1740 This would cause the access check to simply fall out when we
1741 check against any subset of these bits. To get things to work,
1742 change every ACE mask of PRINTER_ACE_FULL_CONTROL to
1743 PRINTER_ACE_MANAGE_DOCUMENTS | PRINTER_ACE_PRINT before
1744 performing the access check. I'm sure there is a better way to
1747 if (secdesc && secdesc->sec && secdesc->sec->dacl &&
1748 secdesc->sec->dacl->ace) {
1749 for(i = 0; i < secdesc->sec->dacl->num_aces; i++) {
1750 if (secdesc->sec->dacl->ace[i].info.mask ==
1751 PRINTER_ACE_FULL_CONTROL) {
1752 secdesc->sec->dacl->ace[i].info.mask =
1753 PRINTER_ACE_MANAGE_DOCUMENTS |
1761 result = se_access_check(secdesc->sec, user->uid, user->gid,
1762 user->ngroups, user->groups,
1763 required_access, &access_granted, &status);
1765 DEBUG(4, ("access check was %s\n", result ? "SUCCESS" : "FAILURE"));
1767 /* Free mallocated memory */
1769 free_sec_desc_buf(&secdesc);