s3-spoolss: Added EN ISO 216, A0 and A1 to builtin forms.
[Samba.git] / source3 / printing / nt_printing.c
blobfdcf2d4ce07a5a1c04178c7c38c2d1be2e5a0a9c
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2000,
5 * Copyright (C) Jean François Micouleau 1998-2000.
6 * Copyright (C) Gerald Carter 2002-2005.
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 static TDB_CONTEXT *tdb_forms; /* used for forms files */
25 static TDB_CONTEXT *tdb_drivers; /* used for driver files */
26 static TDB_CONTEXT *tdb_printers; /* used for printers files */
28 #define FORMS_PREFIX "FORMS/"
29 #define DRIVERS_PREFIX "DRIVERS/"
30 #define DRIVER_INIT_PREFIX "DRIVER_INIT/"
31 #define PRINTERS_PREFIX "PRINTERS/"
32 #define SECDESC_PREFIX "SECDESC/"
33 #define GLOBAL_C_SETPRINTER "GLOBALS/c_setprinter"
35 #define NTDRIVERS_DATABASE_VERSION_1 1
36 #define NTDRIVERS_DATABASE_VERSION_2 2
37 #define NTDRIVERS_DATABASE_VERSION_3 3 /* little endian version of v2 */
38 #define NTDRIVERS_DATABASE_VERSION_4 4 /* fix generic bits in security descriptors */
39 #define NTDRIVERS_DATABASE_VERSION_5 5 /* normalize keys in ntprinters.tdb */
41 /* Map generic permissions to printer object specific permissions */
43 const struct generic_mapping printer_generic_mapping = {
44 PRINTER_READ,
45 PRINTER_WRITE,
46 PRINTER_EXECUTE,
47 PRINTER_ALL_ACCESS
50 const struct standard_mapping printer_std_mapping = {
51 PRINTER_READ,
52 PRINTER_WRITE,
53 PRINTER_EXECUTE,
54 PRINTER_ALL_ACCESS
57 /* Map generic permissions to print server object specific permissions */
59 const struct generic_mapping printserver_generic_mapping = {
60 SERVER_READ,
61 SERVER_WRITE,
62 SERVER_EXECUTE,
63 SERVER_ALL_ACCESS
66 const struct generic_mapping printserver_std_mapping = {
67 SERVER_READ,
68 SERVER_WRITE,
69 SERVER_EXECUTE,
70 SERVER_ALL_ACCESS
73 /* Map generic permissions to job object specific permissions */
75 const struct generic_mapping job_generic_mapping = {
76 JOB_READ,
77 JOB_WRITE,
78 JOB_EXECUTE,
79 JOB_ALL_ACCESS
82 /* We need one default form to support our default printer. Msoft adds the
83 forms it wants and in the ORDER it wants them (note: DEVMODE papersize is an
84 array index). Letter is always first, so (for the current code) additions
85 always put things in the correct order. */
86 static const nt_forms_struct default_forms[] = {
87 {"Letter",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
88 {"Letter Small",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
89 {"Tabloid",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
90 {"Ledger",0x1,0x696b8,0x44368,0x0,0x0,0x696b8,0x44368},
91 {"Legal",0x1,0x34b5c,0x56d10,0x0,0x0,0x34b5c,0x56d10},
92 {"Statement",0x1,0x221b4,0x34b5c,0x0,0x0,0x221b4,0x34b5c},
93 {"Executive",0x1,0x2cf56,0x411cc,0x0,0x0,0x2cf56,0x411cc},
94 {"A0",0x1,0xcd528,0x122488,0x0,0x0,0xcd528,0x122488},
95 {"A1",0x1,0x91050,0xcd528,0x0,0x0,0x91050,0xcd528},
96 {"A3",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
97 {"A4",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
98 {"A4 Small",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
99 {"A5",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
100 {"B4 (JIS)",0x1,0x3ebe8,0x58de0,0x0,0x0,0x3ebe8,0x58de0},
101 {"B5 (JIS)",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
102 {"Folio",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
103 {"Quarto",0x1,0x347d8,0x43238,0x0,0x0,0x347d8,0x43238},
104 {"10x14",0x1,0x3e030,0x56d10,0x0,0x0,0x3e030,0x56d10},
105 {"11x17",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
106 {"Note",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
107 {"Envelope #9",0x1,0x18079,0x37091,0x0,0x0,0x18079,0x37091},
108 {"Envelope #10",0x1,0x19947,0x3ae94,0x0,0x0,0x19947,0x3ae94},
109 {"Envelope #11",0x1,0x1be7c,0x40565,0x0,0x0,0x1be7c,0x40565},
110 {"Envelope #12",0x1,0x1d74a,0x44368,0x0,0x0,0x1d74a,0x44368},
111 {"Envelope #14",0x1,0x1f018,0x47504,0x0,0x0,0x1f018,0x47504},
112 {"C size sheet",0x1,0x696b8,0x886d0,0x0,0x0,0x696b8,0x886d0},
113 {"D size sheet",0x1,0x886d0,0xd2d70,0x0,0x0,0x886d0,0xd2d70},
114 {"E size sheet",0x1,0xd2d70,0x110da0,0x0,0x0,0xd2d70,0x110da0},
115 {"Envelope DL",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
116 {"Envelope C5",0x1,0x278d0,0x37e88,0x0,0x0,0x278d0,0x37e88},
117 {"Envelope C3",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
118 {"Envelope C4",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
119 {"Envelope C6",0x1,0x1bd50,0x278d0,0x0,0x0,0x1bd50,0x278d0},
120 {"Envelope C65",0x1,0x1bd50,0x37e88,0x0,0x0,0x1bd50,0x37e88},
121 {"Envelope B4",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
122 {"Envelope B5",0x1,0x2af80,0x3d090,0x0,0x0,0x2af80,0x3d090},
123 {"Envelope B6",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
124 {"Envelope",0x1,0x1adb0,0x38270,0x0,0x0,0x1adb0,0x38270},
125 {"Envelope Monarch",0x1,0x18079,0x2e824,0x0,0x0,0x18079,0x2e824},
126 {"6 3/4 Envelope",0x1,0x167ab,0x284ec,0x0,0x0,0x167ab,0x284ec},
127 {"US Std Fanfold",0x1,0x5c3e1,0x44368,0x0,0x0,0x5c3e1,0x44368},
128 {"German Std Fanfold",0x1,0x34b5c,0x4a6a0,0x0,0x0,0x34b5c,0x4a6a0},
129 {"German Legal Fanfold",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
130 {"B4 (ISO)",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
131 {"Japanese Postcard",0x1,0x186a0,0x24220,0x0,0x0,0x186a0,0x24220},
132 {"9x11",0x1,0x37cf8,0x44368,0x0,0x0,0x37cf8,0x44368},
133 {"10x11",0x1,0x3e030,0x44368,0x0,0x0,0x3e030,0x44368},
134 {"15x11",0x1,0x5d048,0x44368,0x0,0x0,0x5d048,0x44368},
135 {"Envelope Invite",0x1,0x35b60,0x35b60,0x0,0x0,0x35b60,0x35b60},
136 {"Reserved48",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
137 {"Reserved49",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
138 {"Letter Extra",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
139 {"Legal Extra",0x1,0x3ae94,0x5d048,0x0,0x0,0x3ae94,0x5d048},
140 {"Tabloid Extra",0x1,0x4a6a0,0x6f9f0,0x0,0x0,0x4a6a0,0x6f9f0},
141 {"A4 Extra",0x1,0x397c2,0x4eb16,0x0,0x0,0x397c2,0x4eb16},
142 {"Letter Transverse",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
143 {"A4 Transverse",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
144 {"Letter Extra Transverse",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
145 {"Super A",0x1,0x376b8,0x56ea0,0x0,0x0,0x376b8,0x56ea0},
146 {"Super B",0x1,0x4a768,0x76e58,0x0,0x0,0x4a768,0x76e58},
147 {"Letter Plus",0x1,0x34b5c,0x4eb16,0x0,0x0,0x34b5c,0x4eb16},
148 {"A4 Plus",0x1,0x33450,0x50910,0x0,0x0,0x33450,0x50910},
149 {"A5 Transverse",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
150 {"B5 (JIS) Transverse",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
151 {"A3 Extra",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
152 {"A5 Extra",0x1,0x2a7b0,0x395f8,0x0,0x0,0x2a7b0,0x395f8},
153 {"B5 (ISO) Extra",0x1,0x31128,0x43620,0x0,0x0,0x31128,0x43620},
154 {"A2",0x1,0x668a0,0x91050,0x0,0x0,0x668a0,0x91050},
155 {"A3 Transverse",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
156 {"A3 Extra Transverse",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
157 {"Japanese Double Postcard",0x1,0x30d40,0x24220,0x0,0x0,0x30d40,0x24220},
158 {"A6",0x1,0x19a28,0x24220,0x0,0x0,0x19a28,0x24220},
159 {"Japanese Envelope Kaku #2",0x1,0x3a980,0x510e0,0x0,0x0,0x3a980,0x510e0},
160 {"Japanese Envelope Kaku #3",0x1,0x34bc0,0x43a08,0x0,0x0,0x34bc0,0x43a08},
161 {"Japanese Envelope Chou #3",0x1,0x1d4c0,0x395f8,0x0,0x0,0x1d4c0,0x395f8},
162 {"Japanese Envelope Chou #4",0x1,0x15f90,0x320c8,0x0,0x0,0x15f90,0x320c8},
163 {"Letter Rotated",0x1,0x44368,0x34b5c,0x0,0x0,0x44368,0x34b5c},
164 {"A3 Rotated",0x1,0x668a0,0x48828,0x0,0x0,0x668a0,0x48828},
165 {"A4 Rotated",0x1,0x48828,0x33450,0x0,0x0,0x48828,0x33450},
166 {"A5 Rotated",0x1,0x33450,0x24220,0x0,0x0,0x33450,0x24220},
167 {"B4 (JIS) Rotated",0x1,0x58de0,0x3ebe8,0x0,0x0,0x58de0,0x3ebe8},
168 {"B5 (JIS) Rotated",0x1,0x3ebe8,0x2c6f0,0x0,0x0,0x3ebe8,0x2c6f0},
169 {"Japanese Postcard Rotated",0x1,0x24220,0x186a0,0x0,0x0,0x24220,0x186a0},
170 {"Double Japan Postcard Rotated",0x1,0x24220,0x30d40,0x0,0x0,0x24220,0x30d40},
171 {"A6 Rotated",0x1,0x24220,0x19a28,0x0,0x0,0x24220,0x19a28},
172 {"Japan Envelope Kaku #2 Rotated",0x1,0x510e0,0x3a980,0x0,0x0,0x510e0,0x3a980},
173 {"Japan Envelope Kaku #3 Rotated",0x1,0x43a08,0x34bc0,0x0,0x0,0x43a08, 0x34bc0},
174 {"Japan Envelope Chou #3 Rotated",0x1,0x395f8,0x1d4c0,0x0,0x0,0x395f8,0x1d4c0},
175 {"Japan Envelope Chou #4 Rotated",0x1,0x320c8,0x15f90,0x0,0x0,0x320c8,0x15f90},
176 {"B6 (JIS)",0x1,0x1f400,0x2c6f0,0x0,0x0,0x1f400,0x2c6f0},
177 {"B6 (JIS) Rotated",0x1,0x2c6f0,0x1f400,0x0,0x0,0x2c6f0,0x1f400},
178 {"12x11",0x1,0x4a724,0x443e1,0x0,0x0,0x4a724,0x443e1},
179 {"Japan Envelope You #4",0x1,0x19a28,0x395f8,0x0,0x0,0x19a28,0x395f8},
180 {"Japan Envelope You #4 Rotated",0x1,0x395f8,0x19a28,0x0,0x0,0x395f8,0x19a28},
181 {"PRC 16K",0x1,0x2de60,0x3f7a0,0x0,0x0,0x2de60,0x3f7a0},
182 {"PRC 32K",0x1,0x1fbd0,0x2cec0,0x0,0x0,0x1fbd0,0x2cec0},
183 {"PRC 32K(Big)",0x1,0x222e0,0x318f8,0x0,0x0,0x222e0,0x318f8},
184 {"PRC Envelope #1",0x1,0x18e70,0x28488,0x0,0x0,0x18e70,0x28488},
185 {"PRC Envelope #2",0x1,0x18e70,0x2af80,0x0,0x0,0x18e70,0x2af80},
186 {"PRC Envelope #3",0x1,0x1e848,0x2af80,0x0,0x0,0x1e848,0x2af80},
187 {"PRC Envelope #4",0x1,0x1adb0,0x32c80,0x0,0x0,0x1adb0,0x32c80},
188 {"PRC Envelope #5",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
189 {"PRC Envelope #6",0x1,0x1d4c0,0x38270,0x0,0x0,0x1d4c0,0x38270},
190 {"PRC Envelope #7",0x1,0x27100,0x38270,0x0,0x0,0x27100,0x38270},
191 {"PRC Envelope #8",0x1,0x1d4c0,0x4b708,0x0,0x0,0x1d4c0,0x4b708},
192 {"PRC Envelope #9",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
193 {"PRC Envelope #10",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
194 {"PRC 16K Rotated",0x1,0x3f7a0,0x2de60,0x0,0x0,0x3f7a0,0x2de60},
195 {"PRC 32K Rotated",0x1,0x2cec0,0x1fbd0,0x0,0x0,0x2cec0,0x1fbd0},
196 {"PRC 32K(Big) Rotated",0x1,0x318f8,0x222e0,0x0,0x0,0x318f8,0x222e0},
197 {"PRC Envelope #1 Rotated",0x1,0x28488,0x18e70,0x0,0x0,0x28488,0x18e70},
198 {"PRC Envelope #2 Rotated",0x1,0x2af80,0x18e70,0x0,0x0,0x2af80,0x18e70},
199 {"PRC Envelope #3 Rotated",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
200 {"PRC Envelope #4 Rotated",0x1,0x32c80,0x1adb0,0x0,0x0,0x32c80,0x1adb0},
201 {"PRC Envelope #5 Rotated",0x1,0x35b60,0x1adb0,0x0,0x0,0x35b60,0x1adb0},
202 {"PRC Envelope #6 Rotated",0x1,0x38270,0x1d4c0,0x0,0x0,0x38270,0x1d4c0},
203 {"PRC Envelope #7 Rotated",0x1,0x38270,0x27100,0x0,0x0,0x38270,0x27100},
204 {"PRC Envelope #8 Rotated",0x1,0x4b708,0x1d4c0,0x0,0x0,0x4b708,0x1d4c0},
205 {"PRC Envelope #9 Rotated",0x1,0x4f1a0,0x37e88,0x0,0x0,0x4f1a0,0x37e88},
206 {"PRC Envelope #10 Rotated",0x1,0x6fd10,0x4f1a0,0x0,0x0,0x6fd10,0x4f1a0}
209 static const struct print_architecture_table_node archi_table[]= {
211 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
212 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
213 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
214 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
215 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
216 {"Windows IA64", SPL_ARCH_IA64, 3 },
217 {"Windows x64", SPL_ARCH_X64, 3 },
218 {NULL, "", -1 }
222 /****************************************************************************
223 generate a new TDB_DATA key for storing a printer
224 ****************************************************************************/
226 static TDB_DATA make_printer_tdbkey(TALLOC_CTX *ctx, const char *sharename )
228 fstring share;
229 char *keystr = NULL;
230 TDB_DATA key;
232 fstrcpy(share, sharename);
233 strlower_m(share);
235 keystr = talloc_asprintf(ctx, "%s%s", PRINTERS_PREFIX, share);
236 key = string_term_tdb_data(keystr ? keystr : "");
238 return key;
241 /****************************************************************************
242 generate a new TDB_DATA key for storing a printer security descriptor
243 ****************************************************************************/
245 static TDB_DATA make_printers_secdesc_tdbkey(TALLOC_CTX *ctx,
246 const char* sharename )
248 fstring share;
249 char *keystr = NULL;
250 TDB_DATA key;
252 fstrcpy(share, sharename );
253 strlower_m(share);
255 keystr = talloc_asprintf(ctx, "%s%s", SECDESC_PREFIX, share);
256 key = string_term_tdb_data(keystr ? keystr : "");
258 return key;
261 /****************************************************************************
262 ****************************************************************************/
264 static bool upgrade_to_version_3(void)
266 TDB_DATA kbuf, newkey, dbuf;
268 DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
270 for (kbuf = tdb_firstkey(tdb_drivers); kbuf.dptr;
271 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
273 dbuf = tdb_fetch(tdb_drivers, kbuf);
275 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
276 DEBUG(0,("upgrade_to_version_3:moving form\n"));
277 if (tdb_store(tdb_forms, kbuf, dbuf, TDB_REPLACE) != 0) {
278 SAFE_FREE(dbuf.dptr);
279 DEBUG(0,("upgrade_to_version_3: failed to move form. Error (%s).\n", tdb_errorstr(tdb_forms)));
280 return False;
282 if (tdb_delete(tdb_drivers, kbuf) != 0) {
283 SAFE_FREE(dbuf.dptr);
284 DEBUG(0,("upgrade_to_version_3: failed to delete form. Error (%s)\n", tdb_errorstr(tdb_drivers)));
285 return False;
289 if (strncmp((const char *)kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
290 DEBUG(0,("upgrade_to_version_3:moving printer\n"));
291 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
292 SAFE_FREE(dbuf.dptr);
293 DEBUG(0,("upgrade_to_version_3: failed to move printer. Error (%s)\n", tdb_errorstr(tdb_printers)));
294 return False;
296 if (tdb_delete(tdb_drivers, kbuf) != 0) {
297 SAFE_FREE(dbuf.dptr);
298 DEBUG(0,("upgrade_to_version_3: failed to delete printer. Error (%s)\n", tdb_errorstr(tdb_drivers)));
299 return False;
303 if (strncmp((const char *)kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
304 DEBUG(0,("upgrade_to_version_3:moving secdesc\n"));
305 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
306 SAFE_FREE(dbuf.dptr);
307 DEBUG(0,("upgrade_to_version_3: failed to move secdesc. Error (%s)\n", tdb_errorstr(tdb_printers)));
308 return False;
310 if (tdb_delete(tdb_drivers, kbuf) != 0) {
311 SAFE_FREE(dbuf.dptr);
312 DEBUG(0,("upgrade_to_version_3: failed to delete secdesc. Error (%s)\n", tdb_errorstr(tdb_drivers)));
313 return False;
317 SAFE_FREE(dbuf.dptr);
320 return True;
323 /*******************************************************************
324 Fix an issue with security descriptors. Printer sec_desc must
325 use more than the generic bits that were previously used
326 in <= 3.0.14a. They must also have a owner and group SID assigned.
327 Otherwise, any printers than have been migrated to a Windows
328 host using printmig.exe will not be accessible.
329 *******************************************************************/
331 static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
332 TDB_DATA data, void *state )
334 NTSTATUS status;
335 SEC_DESC_BUF *sd_orig = NULL;
336 SEC_DESC_BUF *sd_new, *sd_store;
337 SEC_DESC *sec, *new_sec;
338 TALLOC_CTX *ctx = state;
339 int result, i;
340 uint32 sd_size;
341 size_t size_new_sec;
343 if (!data.dptr || data.dsize == 0) {
344 return 0;
347 if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) != 0 ) {
348 return 0;
351 /* upgrade the security descriptor */
353 status = unmarshall_sec_desc_buf(ctx, data.dptr, data.dsize, &sd_orig);
354 if (!NT_STATUS_IS_OK(status)) {
355 /* delete bad entries */
356 DEBUG(0,("sec_desc_upg_fn: Failed to parse original sec_desc for %si. Deleting....\n",
357 (const char *)key.dptr ));
358 tdb_delete( tdb_printers, key );
359 return 0;
362 if (!sd_orig) {
363 return 0;
365 sec = sd_orig->sd;
367 /* is this even valid? */
369 if ( !sec->dacl ) {
370 return 0;
373 /* update access masks */
375 for ( i=0; i<sec->dacl->num_aces; i++ ) {
376 switch ( sec->dacl->aces[i].access_mask ) {
377 case (GENERIC_READ_ACCESS | GENERIC_WRITE_ACCESS | GENERIC_EXECUTE_ACCESS):
378 sec->dacl->aces[i].access_mask = PRINTER_ACE_PRINT;
379 break;
381 case GENERIC_ALL_ACCESS:
382 sec->dacl->aces[i].access_mask = PRINTER_ACE_FULL_CONTROL;
383 break;
385 case READ_CONTROL_ACCESS:
386 sec->dacl->aces[i].access_mask = PRINTER_ACE_MANAGE_DOCUMENTS;
388 default: /* no change */
389 break;
393 /* create a new SEC_DESC with the appropriate owner and group SIDs */
395 new_sec = make_sec_desc( ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
396 &global_sid_Builtin_Administrators,
397 &global_sid_Builtin_Administrators,
398 NULL, NULL, &size_new_sec );
399 if (!new_sec) {
400 return 0;
402 sd_new = make_sec_desc_buf( ctx, size_new_sec, new_sec );
403 if (!sd_new) {
404 return 0;
407 if ( !(sd_store = sec_desc_merge( ctx, sd_new, sd_orig )) ) {
408 DEBUG(0,("sec_desc_upg_fn: Failed to update sec_desc for %s\n", key.dptr ));
409 return 0;
412 /* store it back */
414 sd_size = ndr_size_security_descriptor(sd_store->sd, NULL, 0)
415 + sizeof(SEC_DESC_BUF);
417 status = marshall_sec_desc_buf(ctx, sd_store, &data.dptr, &data.dsize);
418 if (!NT_STATUS_IS_OK(status)) {
419 DEBUG(0,("sec_desc_upg_fn: Failed to parse new sec_desc for %s\n", key.dptr ));
420 return 0;
423 result = tdb_store( tdb_printers, key, data, TDB_REPLACE );
425 /* 0 to continue and non-zero to stop traversal */
427 return (result == -1);
430 /*******************************************************************
431 *******************************************************************/
433 static bool upgrade_to_version_4(void)
435 TALLOC_CTX *ctx;
436 int result;
438 DEBUG(0,("upgrade_to_version_4: upgrading printer security descriptors\n"));
440 if ( !(ctx = talloc_init( "upgrade_to_version_4" )) )
441 return False;
443 result = tdb_traverse( tdb_printers, sec_desc_upg_fn, ctx );
445 talloc_destroy( ctx );
447 return ( result != -1 );
450 /*******************************************************************
451 Fix an issue with security descriptors. Printer sec_desc must
452 use more than the generic bits that were previously used
453 in <= 3.0.14a. They must also have a owner and group SID assigned.
454 Otherwise, any printers than have been migrated to a Windows
455 host using printmig.exe will not be accessible.
456 *******************************************************************/
458 static int normalize_printers_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
459 TDB_DATA data, void *state )
461 TALLOC_CTX *ctx = talloc_tos();
462 TDB_DATA new_key;
464 if (!data.dptr || data.dsize == 0)
465 return 0;
467 /* upgrade printer records and security descriptors */
469 if ( strncmp((const char *) key.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX) ) == 0 ) {
470 new_key = make_printer_tdbkey(ctx, (const char *)key.dptr+strlen(PRINTERS_PREFIX) );
472 else if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) == 0 ) {
473 new_key = make_printers_secdesc_tdbkey(ctx, (const char *)key.dptr+strlen(SECDESC_PREFIX) );
475 else {
476 /* ignore this record */
477 return 0;
480 /* delete the original record and store under the normalized key */
482 if ( tdb_delete( the_tdb, key ) != 0 ) {
483 DEBUG(0,("normalize_printers_fn: tdb_delete for [%s] failed!\n",
484 key.dptr));
485 return 1;
488 if ( tdb_store( the_tdb, new_key, data, TDB_REPLACE) != 0 ) {
489 DEBUG(0,("normalize_printers_fn: failed to store new record for [%s]!\n",
490 key.dptr));
491 return 1;
494 return 0;
497 /*******************************************************************
498 *******************************************************************/
500 static bool upgrade_to_version_5(void)
502 TALLOC_CTX *ctx;
503 int result;
505 DEBUG(0,("upgrade_to_version_5: normalizing printer keys\n"));
507 if ( !(ctx = talloc_init( "upgrade_to_version_5" )) )
508 return False;
510 result = tdb_traverse( tdb_printers, normalize_printers_fn, NULL );
512 talloc_destroy( ctx );
514 return ( result != -1 );
517 /****************************************************************************
518 Open the NT printing tdbs. Done once before fork().
519 ****************************************************************************/
521 bool nt_printing_init(struct messaging_context *msg_ctx)
523 const char *vstring = "INFO/version";
524 WERROR win_rc;
525 int32 vers_id;
527 if ( tdb_drivers && tdb_printers && tdb_forms )
528 return True;
530 if (tdb_drivers)
531 tdb_close(tdb_drivers);
532 tdb_drivers = tdb_open_log(state_path("ntdrivers.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
533 if (!tdb_drivers) {
534 DEBUG(0,("nt_printing_init: Failed to open nt drivers database %s (%s)\n",
535 state_path("ntdrivers.tdb"), strerror(errno) ));
536 return False;
539 if (tdb_printers)
540 tdb_close(tdb_printers);
541 tdb_printers = tdb_open_log(state_path("ntprinters.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
542 if (!tdb_printers) {
543 DEBUG(0,("nt_printing_init: Failed to open nt printers database %s (%s)\n",
544 state_path("ntprinters.tdb"), strerror(errno) ));
545 return False;
548 if (tdb_forms)
549 tdb_close(tdb_forms);
550 tdb_forms = tdb_open_log(state_path("ntforms.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
551 if (!tdb_forms) {
552 DEBUG(0,("nt_printing_init: Failed to open nt forms database %s (%s)\n",
553 state_path("ntforms.tdb"), strerror(errno) ));
554 return False;
557 /* handle a Samba upgrade */
559 vers_id = tdb_fetch_int32(tdb_drivers, vstring);
560 if (vers_id == -1) {
561 DEBUG(10, ("Fresh database\n"));
562 tdb_store_int32( tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5 );
563 vers_id = NTDRIVERS_DATABASE_VERSION_5;
566 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
568 if ((vers_id == NTDRIVERS_DATABASE_VERSION_1) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_1)) {
569 if (!upgrade_to_version_3())
570 return False;
571 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
572 vers_id = NTDRIVERS_DATABASE_VERSION_3;
575 if ((vers_id == NTDRIVERS_DATABASE_VERSION_2) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_2)) {
576 /* Written on a bigendian machine with old fetch_int code. Save as le. */
577 /* The only upgrade between V2 and V3 is to save the version in little-endian. */
578 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
579 vers_id = NTDRIVERS_DATABASE_VERSION_3;
582 if (vers_id == NTDRIVERS_DATABASE_VERSION_3 ) {
583 if ( !upgrade_to_version_4() )
584 return False;
585 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_4);
586 vers_id = NTDRIVERS_DATABASE_VERSION_4;
589 if (vers_id == NTDRIVERS_DATABASE_VERSION_4 ) {
590 if ( !upgrade_to_version_5() )
591 return False;
592 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5);
593 vers_id = NTDRIVERS_DATABASE_VERSION_5;
597 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
598 DEBUG(0,("nt_printing_init: Unknown printer database version [%d]\n", vers_id));
599 return False;
603 update_c_setprinter(True);
606 * register callback to handle updating printers as new
607 * drivers are installed
610 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
611 do_drv_upgrade_printer);
614 * register callback to handle updating printer data
615 * when a driver is initialized
618 messaging_register(msg_ctx, NULL, MSG_PRINTERDATA_INIT_RESET,
619 reset_all_printerdata);
621 /* of course, none of the message callbacks matter if you don't
622 tell messages.c that you interested in receiving PRINT_GENERAL
623 msgs. This is done in claim_connection() */
626 if ( lp_security() == SEC_ADS ) {
627 win_rc = check_published_printers();
628 if (!W_ERROR_IS_OK(win_rc))
629 DEBUG(0, ("nt_printing_init: error checking published printers: %s\n", win_errstr(win_rc)));
632 return True;
635 /*******************************************************************
636 Function to allow filename parsing "the old way".
637 ********************************************************************/
639 static NTSTATUS driver_unix_convert(connection_struct *conn,
640 const char *old_name,
641 struct smb_filename **smb_fname)
643 NTSTATUS status;
644 TALLOC_CTX *ctx = talloc_tos();
645 char *name = talloc_strdup(ctx, old_name);
647 if (!name) {
648 return NT_STATUS_NO_MEMORY;
650 unix_format(name);
651 name = unix_clean_name(ctx, name);
652 if (!name) {
653 return NT_STATUS_NO_MEMORY;
655 trim_string(name,"/","/");
657 status = unix_convert(ctx, conn, name, smb_fname, 0);
658 if (!NT_STATUS_IS_OK(status)) {
659 return NT_STATUS_NO_MEMORY;
662 return NT_STATUS_OK;
665 /*******************************************************************
666 tdb traversal function for counting printers.
667 ********************************************************************/
669 static int traverse_counting_printers(TDB_CONTEXT *t, TDB_DATA key,
670 TDB_DATA data, void *context)
672 int *printer_count = (int*)context;
674 if (memcmp(PRINTERS_PREFIX, key.dptr, sizeof(PRINTERS_PREFIX)-1) == 0) {
675 (*printer_count)++;
676 DEBUG(10,("traverse_counting_printers: printer = [%s] printer_count = %d\n", key.dptr, *printer_count));
679 return 0;
682 /*******************************************************************
683 Update the spooler global c_setprinter. This variable is initialized
684 when the parent smbd starts with the number of existing printers. It
685 is monotonically increased by the current number of printers *after*
686 each add or delete printer RPC. Only Microsoft knows why... JRR020119
687 ********************************************************************/
689 uint32 update_c_setprinter(bool initialize)
691 int32 c_setprinter;
692 int32 printer_count = 0;
694 tdb_lock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
696 /* Traverse the tdb, counting the printers */
697 tdb_traverse(tdb_printers, traverse_counting_printers, (void *)&printer_count);
699 /* If initializing, set c_setprinter to current printers count
700 * otherwise, bump it by the current printer count
702 if (!initialize)
703 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER) + printer_count;
704 else
705 c_setprinter = printer_count;
707 DEBUG(10,("update_c_setprinter: c_setprinter = %u\n", (unsigned int)c_setprinter));
708 tdb_store_int32(tdb_printers, GLOBAL_C_SETPRINTER, c_setprinter);
710 tdb_unlock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
712 return (uint32)c_setprinter;
715 /*******************************************************************
716 Get the spooler global c_setprinter, accounting for initialization.
717 ********************************************************************/
719 uint32 get_c_setprinter(void)
721 int32 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER);
723 if (c_setprinter == (int32)-1)
724 c_setprinter = update_c_setprinter(True);
726 DEBUG(10,("get_c_setprinter: c_setprinter = %d\n", c_setprinter));
728 return (uint32)c_setprinter;
731 /****************************************************************************
732 Get builtin form struct list.
733 ****************************************************************************/
735 int get_builtin_ntforms(nt_forms_struct **list)
737 *list = (nt_forms_struct *)memdup(&default_forms[0], sizeof(default_forms));
738 if (!*list) {
739 return 0;
741 return ARRAY_SIZE(default_forms);
744 /****************************************************************************
745 get a builtin form struct
746 ****************************************************************************/
748 bool get_a_builtin_ntform_by_string(const char *form_name, nt_forms_struct *form)
750 int i;
751 DEBUGADD(6,("Looking for builtin form %s \n", form_name));
752 for (i=0; i<ARRAY_SIZE(default_forms); i++) {
753 if (strequal(form_name,default_forms[i].name)) {
754 DEBUGADD(6,("Found builtin form %s \n", form_name));
755 memcpy(form,&default_forms[i],sizeof(*form));
756 return true;
760 return false;
763 /****************************************************************************
764 get a form struct list.
765 ****************************************************************************/
767 int get_ntforms(nt_forms_struct **list)
769 TDB_DATA kbuf, newkey, dbuf;
770 nt_forms_struct form;
771 int ret;
772 int i;
773 int n = 0;
775 *list = NULL;
777 for (kbuf = tdb_firstkey(tdb_forms);
778 kbuf.dptr;
779 newkey = tdb_nextkey(tdb_forms, kbuf), free(kbuf.dptr), kbuf=newkey)
781 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) != 0)
782 continue;
784 dbuf = tdb_fetch(tdb_forms, kbuf);
785 if (!dbuf.dptr)
786 continue;
788 fstrcpy(form.name, (const char *)kbuf.dptr+strlen(FORMS_PREFIX));
789 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "dddddddd",
790 &i, &form.flag, &form.width, &form.length, &form.left,
791 &form.top, &form.right, &form.bottom);
792 SAFE_FREE(dbuf.dptr);
793 if (ret != dbuf.dsize)
794 continue;
796 *list = SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1);
797 if (!*list) {
798 DEBUG(0,("get_ntforms: Realloc fail.\n"));
799 return 0;
801 (*list)[n] = form;
802 n++;
806 return n;
809 /****************************************************************************
810 write a form struct list
811 ****************************************************************************/
813 int write_ntforms(nt_forms_struct **list, int number)
815 TALLOC_CTX *ctx = talloc_tos();
816 char *buf = NULL;
817 char *key = NULL;
818 int len;
819 TDB_DATA dbuf;
820 int i;
822 for (i=0;i<number;i++) {
823 /* save index, so list is rebuilt in correct order */
824 len = tdb_pack(NULL, 0, "dddddddd",
825 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
826 (*list)[i].left, (*list)[i].top, (*list)[i].right,
827 (*list)[i].bottom);
828 if (!len) {
829 continue;
831 buf = TALLOC_ARRAY(ctx, char, len);
832 if (!buf) {
833 return 0;
835 len = tdb_pack((uint8 *)buf, len, "dddddddd",
836 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
837 (*list)[i].left, (*list)[i].top, (*list)[i].right,
838 (*list)[i].bottom);
839 key = talloc_asprintf(ctx, "%s%s", FORMS_PREFIX, (*list)[i].name);
840 if (!key) {
841 return 0;
843 dbuf.dsize = len;
844 dbuf.dptr = (uint8 *)buf;
845 if (tdb_store_bystring(tdb_forms, key, dbuf, TDB_REPLACE) != 0) {
846 TALLOC_FREE(key);
847 TALLOC_FREE(buf);
848 break;
850 TALLOC_FREE(key);
851 TALLOC_FREE(buf);
854 return i;
857 /****************************************************************************
858 add a form struct at the end of the list
859 ****************************************************************************/
860 bool add_a_form(nt_forms_struct **list, struct spoolss_AddFormInfo1 *form, int *count)
862 int n=0;
863 bool update;
866 * NT tries to add forms even when
867 * they are already in the base
868 * only update the values if already present
871 update=False;
873 for (n=0; n<*count; n++) {
874 if ( strequal((*list)[n].name, form->form_name) ) {
875 update=True;
876 break;
880 if (update==False) {
881 if((*list=SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1)) == NULL) {
882 DEBUG(0,("add_a_form: failed to enlarge forms list!\n"));
883 return False;
885 fstrcpy((*list)[n].name, form->form_name);
886 (*count)++;
889 (*list)[n].flag = form->flags;
890 (*list)[n].width = form->size.width;
891 (*list)[n].length = form->size.height;
892 (*list)[n].left = form->area.left;
893 (*list)[n].top = form->area.top;
894 (*list)[n].right = form->area.right;
895 (*list)[n].bottom = form->area.bottom;
897 DEBUG(6,("add_a_form: Successfully %s form [%s]\n",
898 update ? "updated" : "added", form->form_name));
900 return True;
903 /****************************************************************************
904 Delete a named form struct.
905 ****************************************************************************/
907 bool delete_a_form(nt_forms_struct **list, const char *del_name, int *count, WERROR *ret)
909 char *key = NULL;
910 int n=0;
912 *ret = WERR_OK;
914 for (n=0; n<*count; n++) {
915 if (!strncmp((*list)[n].name, del_name, strlen(del_name))) {
916 DEBUG(103, ("delete_a_form, [%s] in list\n", del_name));
917 break;
921 if (n == *count) {
922 DEBUG(10,("delete_a_form, [%s] not found\n", del_name));
923 *ret = WERR_INVALID_PARAM;
924 return False;
927 if (asprintf(&key, "%s%s", FORMS_PREFIX, (*list)[n].name) < 0) {
928 *ret = WERR_NOMEM;
929 return false;
931 if (tdb_delete_bystring(tdb_forms, key) != 0) {
932 SAFE_FREE(key);
933 *ret = WERR_NOMEM;
934 return False;
936 SAFE_FREE(key);
937 return true;
940 /****************************************************************************
941 Update a form struct.
942 ****************************************************************************/
944 void update_a_form(nt_forms_struct **list, struct spoolss_AddFormInfo1 *form, int count)
946 int n=0;
948 DEBUG(106, ("[%s]\n", form->form_name));
949 for (n=0; n<count; n++) {
950 DEBUGADD(106, ("n [%d]:[%s]\n", n, (*list)[n].name));
951 if (!strncmp((*list)[n].name, form->form_name, strlen(form->form_name)))
952 break;
955 if (n==count) return;
957 (*list)[n].flag = form->flags;
958 (*list)[n].width = form->size.width;
959 (*list)[n].length = form->size.height;
960 (*list)[n].left = form->area.left;
961 (*list)[n].top = form->area.top;
962 (*list)[n].right = form->area.right;
963 (*list)[n].bottom = form->area.bottom;
966 /****************************************************************************
967 Get the nt drivers list.
968 Traverse the database and look-up the matching names.
969 ****************************************************************************/
970 int get_ntdrivers(fstring **list, const char *architecture, uint32 version)
972 int total=0;
973 const char *short_archi;
974 char *key = NULL;
975 TDB_DATA kbuf, newkey;
977 short_archi = get_short_archi(architecture);
978 if (!short_archi) {
979 return 0;
982 if (asprintf(&key, "%s%s/%d/", DRIVERS_PREFIX,
983 short_archi, version) < 0) {
984 return 0;
987 for (kbuf = tdb_firstkey(tdb_drivers);
988 kbuf.dptr;
989 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
991 if (strncmp((const char *)kbuf.dptr, key, strlen(key)) != 0)
992 continue;
994 if((*list = SMB_REALLOC_ARRAY(*list, fstring, total+1)) == NULL) {
995 DEBUG(0,("get_ntdrivers: failed to enlarge list!\n"));
996 SAFE_FREE(key);
997 return -1;
1000 fstrcpy((*list)[total], (const char *)kbuf.dptr+strlen(key));
1001 total++;
1004 SAFE_FREE(key);
1005 return(total);
1008 /****************************************************************************
1009 Function to do the mapping between the long architecture name and
1010 the short one.
1011 ****************************************************************************/
1013 const char *get_short_archi(const char *long_archi)
1015 int i=-1;
1017 DEBUG(107,("Getting architecture dependant directory\n"));
1018 do {
1019 i++;
1020 } while ( (archi_table[i].long_archi!=NULL ) &&
1021 StrCaseCmp(long_archi, archi_table[i].long_archi) );
1023 if (archi_table[i].long_archi==NULL) {
1024 DEBUGADD(10,("Unknown architecture [%s] !\n", long_archi));
1025 return NULL;
1028 /* this might be client code - but shouldn't this be an fstrcpy etc? */
1030 DEBUGADD(108,("index: [%d]\n", i));
1031 DEBUGADD(108,("long architecture: [%s]\n", archi_table[i].long_archi));
1032 DEBUGADD(108,("short architecture: [%s]\n", archi_table[i].short_archi));
1034 return archi_table[i].short_archi;
1037 /****************************************************************************
1038 Version information in Microsoft files is held in a VS_VERSION_INFO structure.
1039 There are two case to be covered here: PE (Portable Executable) and NE (New
1040 Executable) files. Both files support the same INFO structure, but PE files
1041 store the signature in unicode, and NE files store it as !unicode.
1042 returns -1 on error, 1 on version info found, and 0 on no version info found.
1043 ****************************************************************************/
1045 static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32 *minor)
1047 int i;
1048 char *buf = NULL;
1049 ssize_t byte_count;
1051 if ((buf=(char *)SMB_MALLOC(DOS_HEADER_SIZE)) == NULL) {
1052 DEBUG(0,("get_file_version: PE file [%s] DOS Header malloc failed bytes = %d\n",
1053 fname, DOS_HEADER_SIZE));
1054 goto error_exit;
1057 if ((byte_count = vfs_read_data(fsp, buf, DOS_HEADER_SIZE)) < DOS_HEADER_SIZE) {
1058 DEBUG(3,("get_file_version: File [%s] DOS header too short, bytes read = %lu\n",
1059 fname, (unsigned long)byte_count));
1060 goto no_version_info;
1063 /* Is this really a DOS header? */
1064 if (SVAL(buf,DOS_HEADER_MAGIC_OFFSET) != DOS_HEADER_MAGIC) {
1065 DEBUG(6,("get_file_version: File [%s] bad DOS magic = 0x%x\n",
1066 fname, SVAL(buf,DOS_HEADER_MAGIC_OFFSET)));
1067 goto no_version_info;
1070 /* Skip OEM header (if any) and the DOS stub to start of Windows header */
1071 if (SMB_VFS_LSEEK(fsp, SVAL(buf,DOS_HEADER_LFANEW_OFFSET), SEEK_SET) == (SMB_OFF_T)-1) {
1072 DEBUG(3,("get_file_version: File [%s] too short, errno = %d\n",
1073 fname, errno));
1074 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1075 goto no_version_info;
1078 /* Note: DOS_HEADER_SIZE and NE_HEADER_SIZE are incidentally same */
1079 if ((byte_count = vfs_read_data(fsp, buf, NE_HEADER_SIZE)) < NE_HEADER_SIZE) {
1080 DEBUG(3,("get_file_version: File [%s] Windows header too short, bytes read = %lu\n",
1081 fname, (unsigned long)byte_count));
1082 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1083 goto no_version_info;
1086 /* The header may be a PE (Portable Executable) or an NE (New Executable) */
1087 if (IVAL(buf,PE_HEADER_SIGNATURE_OFFSET) == PE_HEADER_SIGNATURE) {
1088 unsigned int num_sections;
1089 unsigned int section_table_bytes;
1091 /* Just skip over optional header to get to section table */
1092 if (SMB_VFS_LSEEK(fsp,
1093 SVAL(buf,PE_HEADER_OPTIONAL_HEADER_SIZE)-(NE_HEADER_SIZE-PE_HEADER_SIZE),
1094 SEEK_CUR) == (SMB_OFF_T)-1) {
1095 DEBUG(3,("get_file_version: File [%s] Windows optional header too short, errno = %d\n",
1096 fname, errno));
1097 goto error_exit;
1100 /* get the section table */
1101 num_sections = SVAL(buf,PE_HEADER_NUMBER_OF_SECTIONS);
1102 section_table_bytes = num_sections * PE_HEADER_SECT_HEADER_SIZE;
1103 if (section_table_bytes == 0)
1104 goto error_exit;
1106 SAFE_FREE(buf);
1107 if ((buf=(char *)SMB_MALLOC(section_table_bytes)) == NULL) {
1108 DEBUG(0,("get_file_version: PE file [%s] section table malloc failed bytes = %d\n",
1109 fname, section_table_bytes));
1110 goto error_exit;
1113 if ((byte_count = vfs_read_data(fsp, buf, section_table_bytes)) < section_table_bytes) {
1114 DEBUG(3,("get_file_version: PE file [%s] Section header too short, bytes read = %lu\n",
1115 fname, (unsigned long)byte_count));
1116 goto error_exit;
1119 /* Iterate the section table looking for the resource section ".rsrc" */
1120 for (i = 0; i < num_sections; i++) {
1121 int sec_offset = i * PE_HEADER_SECT_HEADER_SIZE;
1123 if (strcmp(".rsrc", &buf[sec_offset+PE_HEADER_SECT_NAME_OFFSET]) == 0) {
1124 unsigned int section_pos = IVAL(buf,sec_offset+PE_HEADER_SECT_PTR_DATA_OFFSET);
1125 unsigned int section_bytes = IVAL(buf,sec_offset+PE_HEADER_SECT_SIZE_DATA_OFFSET);
1127 if (section_bytes == 0)
1128 goto error_exit;
1130 SAFE_FREE(buf);
1131 if ((buf=(char *)SMB_MALLOC(section_bytes)) == NULL) {
1132 DEBUG(0,("get_file_version: PE file [%s] version malloc failed bytes = %d\n",
1133 fname, section_bytes));
1134 goto error_exit;
1137 /* Seek to the start of the .rsrc section info */
1138 if (SMB_VFS_LSEEK(fsp, section_pos, SEEK_SET) == (SMB_OFF_T)-1) {
1139 DEBUG(3,("get_file_version: PE file [%s] too short for section info, errno = %d\n",
1140 fname, errno));
1141 goto error_exit;
1144 if ((byte_count = vfs_read_data(fsp, buf, section_bytes)) < section_bytes) {
1145 DEBUG(3,("get_file_version: PE file [%s] .rsrc section too short, bytes read = %lu\n",
1146 fname, (unsigned long)byte_count));
1147 goto error_exit;
1150 if (section_bytes < VS_VERSION_INFO_UNICODE_SIZE)
1151 goto error_exit;
1153 for (i=0; i<section_bytes-VS_VERSION_INFO_UNICODE_SIZE; i++) {
1154 /* Scan for 1st 3 unicoded bytes followed by word aligned magic value */
1155 if (buf[i] == 'V' && buf[i+1] == '\0' && buf[i+2] == 'S') {
1156 /* Align to next long address */
1157 int pos = (i + sizeof(VS_SIGNATURE)*2 + 3) & 0xfffffffc;
1159 if (IVAL(buf,pos) == VS_MAGIC_VALUE) {
1160 *major = IVAL(buf,pos+VS_MAJOR_OFFSET);
1161 *minor = IVAL(buf,pos+VS_MINOR_OFFSET);
1163 DEBUG(6,("get_file_version: PE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1164 fname, *major, *minor,
1165 (*major>>16)&0xffff, *major&0xffff,
1166 (*minor>>16)&0xffff, *minor&0xffff));
1167 SAFE_FREE(buf);
1168 return 1;
1175 /* Version info not found, fall back to origin date/time */
1176 DEBUG(10,("get_file_version: PE file [%s] has no version info\n", fname));
1177 SAFE_FREE(buf);
1178 return 0;
1180 } else if (SVAL(buf,NE_HEADER_SIGNATURE_OFFSET) == NE_HEADER_SIGNATURE) {
1181 if (CVAL(buf,NE_HEADER_TARGET_OS_OFFSET) != NE_HEADER_TARGOS_WIN ) {
1182 DEBUG(3,("get_file_version: NE file [%s] wrong target OS = 0x%x\n",
1183 fname, CVAL(buf,NE_HEADER_TARGET_OS_OFFSET)));
1184 /* At this point, we assume the file is in error. It still could be somthing
1185 * else besides a NE file, but it unlikely at this point. */
1186 goto error_exit;
1189 /* Allocate a bit more space to speed up things */
1190 SAFE_FREE(buf);
1191 if ((buf=(char *)SMB_MALLOC(VS_NE_BUF_SIZE)) == NULL) {
1192 DEBUG(0,("get_file_version: NE file [%s] malloc failed bytes = %d\n",
1193 fname, PE_HEADER_SIZE));
1194 goto error_exit;
1197 /* This is a HACK! I got tired of trying to sort through the messy
1198 * 'NE' file format. If anyone wants to clean this up please have at
1199 * it, but this works. 'NE' files will eventually fade away. JRR */
1200 while((byte_count = vfs_read_data(fsp, buf, VS_NE_BUF_SIZE)) > 0) {
1201 /* Cover case that should not occur in a well formed 'NE' .dll file */
1202 if (byte_count-VS_VERSION_INFO_SIZE <= 0) break;
1204 for(i=0; i<byte_count; i++) {
1205 /* Fast skip past data that can't possibly match */
1206 if (buf[i] != 'V') continue;
1208 /* Potential match data crosses buf boundry, move it to beginning
1209 * of buf, and fill the buf with as much as it will hold. */
1210 if (i>byte_count-VS_VERSION_INFO_SIZE) {
1211 int bc;
1213 memcpy(buf, &buf[i], byte_count-i);
1214 if ((bc = vfs_read_data(fsp, &buf[byte_count-i], VS_NE_BUF_SIZE-
1215 (byte_count-i))) < 0) {
1217 DEBUG(0,("get_file_version: NE file [%s] Read error, errno=%d\n",
1218 fname, errno));
1219 goto error_exit;
1222 byte_count = bc + (byte_count - i);
1223 if (byte_count<VS_VERSION_INFO_SIZE) break;
1225 i = 0;
1228 /* Check that the full signature string and the magic number that
1229 * follows exist (not a perfect solution, but the chances that this
1230 * occurs in code is, well, remote. Yes I know I'm comparing the 'V'
1231 * twice, as it is simpler to read the code. */
1232 if (strcmp(&buf[i], VS_SIGNATURE) == 0) {
1233 /* Compute skip alignment to next long address */
1234 int skip = -(SMB_VFS_LSEEK(fsp, 0, SEEK_CUR) - (byte_count - i) +
1235 sizeof(VS_SIGNATURE)) & 3;
1236 if (IVAL(buf,i+sizeof(VS_SIGNATURE)+skip) != 0xfeef04bd) continue;
1238 *major = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MAJOR_OFFSET);
1239 *minor = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MINOR_OFFSET);
1240 DEBUG(6,("get_file_version: NE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1241 fname, *major, *minor,
1242 (*major>>16)&0xffff, *major&0xffff,
1243 (*minor>>16)&0xffff, *minor&0xffff));
1244 SAFE_FREE(buf);
1245 return 1;
1250 /* Version info not found, fall back to origin date/time */
1251 DEBUG(0,("get_file_version: NE file [%s] Version info not found\n", fname));
1252 SAFE_FREE(buf);
1253 return 0;
1255 } else
1256 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1257 DEBUG(3,("get_file_version: File [%s] unknown file format, signature = 0x%x\n",
1258 fname, IVAL(buf,PE_HEADER_SIGNATURE_OFFSET)));
1260 no_version_info:
1261 SAFE_FREE(buf);
1262 return 0;
1264 error_exit:
1265 SAFE_FREE(buf);
1266 return -1;
1269 /****************************************************************************
1270 Drivers for Microsoft systems contain multiple files. Often, multiple drivers
1271 share one or more files. During the MS installation process files are checked
1272 to insure that only a newer version of a shared file is installed over an
1273 older version. There are several possibilities for this comparison. If there
1274 is no previous version, the new one is newer (obviously). If either file is
1275 missing the version info structure, compare the creation date (on Unix use
1276 the modification date). Otherwise chose the numerically larger version number.
1277 ****************************************************************************/
1279 static int file_version_is_newer(connection_struct *conn, fstring new_file, fstring old_file)
1281 bool use_version = true;
1283 uint32 new_major;
1284 uint32 new_minor;
1285 time_t new_create_time;
1287 uint32 old_major;
1288 uint32 old_minor;
1289 time_t old_create_time;
1291 struct smb_filename *smb_fname = NULL;
1292 files_struct *fsp = NULL;
1293 SMB_STRUCT_STAT st;
1295 NTSTATUS status;
1296 int ret;
1298 SET_STAT_INVALID(st);
1299 new_create_time = (time_t)0;
1300 old_create_time = (time_t)0;
1302 /* Get file version info (if available) for previous file (if it exists) */
1303 status = driver_unix_convert(conn, old_file, &smb_fname);
1304 if (!NT_STATUS_IS_OK(status)) {
1305 goto error_exit;
1308 status = SMB_VFS_CREATE_FILE(
1309 conn, /* conn */
1310 NULL, /* req */
1311 0, /* root_dir_fid */
1312 smb_fname, /* fname */
1313 FILE_GENERIC_READ, /* access_mask */
1314 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1315 FILE_OPEN, /* create_disposition*/
1316 0, /* create_options */
1317 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1318 INTERNAL_OPEN_ONLY, /* oplock_request */
1319 0, /* allocation_size */
1320 NULL, /* sd */
1321 NULL, /* ea_list */
1322 &fsp, /* result */
1323 NULL); /* pinfo */
1325 if (!NT_STATUS_IS_OK(status)) {
1326 /* Old file not found, so by definition new file is in fact newer */
1327 DEBUG(10,("file_version_is_newer: Can't open old file [%s], "
1328 "errno = %d\n", smb_fname_str_dbg(smb_fname),
1329 errno));
1330 ret = 1;
1331 goto done;
1333 } else {
1334 ret = get_file_version(fsp, old_file, &old_major, &old_minor);
1335 if (ret == -1) {
1336 goto error_exit;
1339 if (!ret) {
1340 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1341 old_file));
1342 use_version = false;
1343 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1344 goto error_exit;
1346 old_create_time = convert_timespec_to_time_t(st.st_ex_mtime);
1347 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n",
1348 (long)old_create_time));
1351 close_file(NULL, fsp, NORMAL_CLOSE);
1352 fsp = NULL;
1354 /* Get file version info (if available) for new file */
1355 status = driver_unix_convert(conn, new_file, &smb_fname);
1356 if (!NT_STATUS_IS_OK(status)) {
1357 goto error_exit;
1360 status = SMB_VFS_CREATE_FILE(
1361 conn, /* conn */
1362 NULL, /* req */
1363 0, /* root_dir_fid */
1364 smb_fname, /* fname */
1365 FILE_GENERIC_READ, /* access_mask */
1366 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1367 FILE_OPEN, /* create_disposition*/
1368 0, /* create_options */
1369 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1370 INTERNAL_OPEN_ONLY, /* oplock_request */
1371 0, /* allocation_size */
1372 NULL, /* sd */
1373 NULL, /* ea_list */
1374 &fsp, /* result */
1375 NULL); /* pinfo */
1377 if (!NT_STATUS_IS_OK(status)) {
1378 /* New file not found, this shouldn't occur if the caller did its job */
1379 DEBUG(3,("file_version_is_newer: Can't open new file [%s], "
1380 "errno = %d\n", smb_fname_str_dbg(smb_fname), errno));
1381 goto error_exit;
1383 } else {
1384 ret = get_file_version(fsp, new_file, &new_major, &new_minor);
1385 if (ret == -1) {
1386 goto error_exit;
1389 if (!ret) {
1390 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1391 new_file));
1392 use_version = false;
1393 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1394 goto error_exit;
1396 new_create_time = convert_timespec_to_time_t(st.st_ex_mtime);
1397 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n",
1398 (long)new_create_time));
1401 close_file(NULL, fsp, NORMAL_CLOSE);
1402 fsp = NULL;
1404 if (use_version && (new_major != old_major || new_minor != old_minor)) {
1405 /* Compare versions and choose the larger version number */
1406 if (new_major > old_major ||
1407 (new_major == old_major && new_minor > old_minor)) {
1409 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1410 ret = 1;
1411 goto done;
1413 else {
1414 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1415 ret = 0;
1416 goto done;
1419 } else {
1420 /* Compare modification time/dates and choose the newest time/date */
1421 if (new_create_time > old_create_time) {
1422 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1423 ret = 1;
1424 goto done;
1426 else {
1427 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1428 ret = 0;
1429 goto done;
1433 error_exit:
1434 if(fsp)
1435 close_file(NULL, fsp, NORMAL_CLOSE);
1436 ret = -1;
1437 done:
1438 TALLOC_FREE(smb_fname);
1439 return ret;
1442 /****************************************************************************
1443 Determine the correct cVersion associated with an architecture and driver
1444 ****************************************************************************/
1445 static uint32 get_correct_cversion(struct pipes_struct *p,
1446 const char *architecture,
1447 const char *driverpath_in,
1448 WERROR *perr)
1450 int cversion;
1451 NTSTATUS nt_status;
1452 struct smb_filename *smb_fname = NULL;
1453 char *driverpath = NULL;
1454 files_struct *fsp = NULL;
1455 connection_struct *conn = NULL;
1456 NTSTATUS status;
1457 char *oldcwd;
1458 fstring printdollar;
1459 int printdollar_snum;
1461 *perr = WERR_INVALID_PARAM;
1463 /* If architecture is Windows 95/98/ME, the version is always 0. */
1464 if (strcmp(architecture, SPL_ARCH_WIN40) == 0) {
1465 DEBUG(10,("get_correct_cversion: Driver is Win9x, cversion = 0\n"));
1466 *perr = WERR_OK;
1467 return 0;
1470 /* If architecture is Windows x64, the version is always 3. */
1471 if (strcmp(architecture, SPL_ARCH_X64) == 0) {
1472 DEBUG(10,("get_correct_cversion: Driver is x64, cversion = 3\n"));
1473 *perr = WERR_OK;
1474 return 3;
1477 fstrcpy(printdollar, "print$");
1479 printdollar_snum = find_service(printdollar);
1480 if (printdollar_snum == -1) {
1481 *perr = WERR_NO_SUCH_SHARE;
1482 return -1;
1485 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
1486 lp_pathname(printdollar_snum),
1487 p->server_info, &oldcwd);
1488 if (!NT_STATUS_IS_OK(nt_status)) {
1489 DEBUG(0,("get_correct_cversion: create_conn_struct "
1490 "returned %s\n", nt_errstr(nt_status)));
1491 *perr = ntstatus_to_werror(nt_status);
1492 return -1;
1495 /* Open the driver file (Portable Executable format) and determine the
1496 * deriver the cversion. */
1497 driverpath = talloc_asprintf(talloc_tos(),
1498 "%s/%s",
1499 architecture,
1500 driverpath_in);
1501 if (!driverpath) {
1502 *perr = WERR_NOMEM;
1503 goto error_exit;
1506 nt_status = driver_unix_convert(conn, driverpath, &smb_fname);
1507 if (!NT_STATUS_IS_OK(nt_status)) {
1508 *perr = ntstatus_to_werror(nt_status);
1509 goto error_exit;
1512 nt_status = vfs_file_exist(conn, smb_fname);
1513 if (!NT_STATUS_IS_OK(nt_status)) {
1514 *perr = WERR_BADFILE;
1515 goto error_exit;
1518 status = SMB_VFS_CREATE_FILE(
1519 conn, /* conn */
1520 NULL, /* req */
1521 0, /* root_dir_fid */
1522 smb_fname, /* fname */
1523 FILE_GENERIC_READ, /* access_mask */
1524 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1525 FILE_OPEN, /* create_disposition*/
1526 0, /* create_options */
1527 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1528 INTERNAL_OPEN_ONLY, /* oplock_request */
1529 0, /* allocation_size */
1530 NULL, /* sd */
1531 NULL, /* ea_list */
1532 &fsp, /* result */
1533 NULL); /* pinfo */
1535 if (!NT_STATUS_IS_OK(status)) {
1536 DEBUG(3,("get_correct_cversion: Can't open file [%s], errno = "
1537 "%d\n", smb_fname_str_dbg(smb_fname), errno));
1538 *perr = WERR_ACCESS_DENIED;
1539 goto error_exit;
1540 } else {
1541 uint32 major;
1542 uint32 minor;
1543 int ret;
1545 ret = get_file_version(fsp, smb_fname->base_name, &major, &minor);
1546 if (ret == -1) goto error_exit;
1548 if (!ret) {
1549 DEBUG(6,("get_correct_cversion: Version info not "
1550 "found [%s]\n",
1551 smb_fname_str_dbg(smb_fname)));
1552 goto error_exit;
1556 * This is a Microsoft'ism. See references in MSDN to VER_FILEVERSION
1557 * for more details. Version in this case is not just the version of the
1558 * file, but the version in the sense of kernal mode (2) vs. user mode
1559 * (3) drivers. Other bits of the version fields are the version info.
1560 * JRR 010716
1562 cversion = major & 0x0000ffff;
1563 switch (cversion) {
1564 case 2: /* WinNT drivers */
1565 case 3: /* Win2K drivers */
1566 break;
1568 default:
1569 DEBUG(6,("get_correct_cversion: cversion "
1570 "invalid [%s] cversion = %d\n",
1571 smb_fname_str_dbg(smb_fname),
1572 cversion));
1573 goto error_exit;
1576 DEBUG(10,("get_correct_cversion: Version info found [%s] major"
1577 " = 0x%x minor = 0x%x\n",
1578 smb_fname_str_dbg(smb_fname), major, minor));
1581 DEBUG(10,("get_correct_cversion: Driver file [%s] cversion = %d\n",
1582 smb_fname_str_dbg(smb_fname), cversion));
1584 goto done;
1586 error_exit:
1587 cversion = -1;
1588 done:
1589 TALLOC_FREE(smb_fname);
1590 if (fsp != NULL) {
1591 close_file(NULL, fsp, NORMAL_CLOSE);
1593 if (conn != NULL) {
1594 vfs_ChDir(conn, oldcwd);
1595 conn_free(conn);
1597 if (cversion != -1) {
1598 *perr = WERR_OK;
1600 return cversion;
1603 /****************************************************************************
1604 ****************************************************************************/
1606 #define strip_driver_path(_mem_ctx, _element) do { \
1607 if ((_p = strrchr((_element), '\\')) != NULL) { \
1608 (_element) = talloc_asprintf((_mem_ctx), "%s", _p+1); \
1609 W_ERROR_HAVE_NO_MEMORY((_element)); \
1611 } while (0);
1613 static WERROR clean_up_driver_struct_level(TALLOC_CTX *mem_ctx,
1614 struct pipes_struct *rpc_pipe,
1615 const char *architecture,
1616 const char **driver_path,
1617 const char **data_file,
1618 const char **config_file,
1619 const char **help_file,
1620 struct spoolss_StringArray *dependent_files,
1621 uint32_t *version)
1623 const char *short_architecture;
1624 int i;
1625 WERROR err;
1626 char *_p;
1628 /* clean up the driver name.
1629 * we can get .\driver.dll
1630 * or worse c:\windows\system\driver.dll !
1632 /* using an intermediate string to not have overlaping memcpy()'s */
1634 strip_driver_path(mem_ctx, *driver_path);
1635 strip_driver_path(mem_ctx, *data_file);
1636 strip_driver_path(mem_ctx, *config_file);
1637 strip_driver_path(mem_ctx, *help_file);
1639 if (dependent_files && dependent_files->string) {
1640 for (i=0; dependent_files->string[i]; i++) {
1641 strip_driver_path(mem_ctx, dependent_files->string[i]);
1645 short_architecture = get_short_archi(architecture);
1646 if (!short_architecture) {
1647 return WERR_UNKNOWN_PRINTER_DRIVER;
1650 /* jfm:7/16/2000 the client always sends the cversion=0.
1651 * The server should check which version the driver is by reading
1652 * the PE header of driver->driverpath.
1654 * For Windows 95/98 the version is 0 (so the value sent is correct)
1655 * For Windows NT (the architecture doesn't matter)
1656 * NT 3.1: cversion=0
1657 * NT 3.5/3.51: cversion=1
1658 * NT 4: cversion=2
1659 * NT2K: cversion=3
1662 *version = get_correct_cversion(rpc_pipe, short_architecture,
1663 *driver_path, &err);
1664 if (*version == -1) {
1665 return err;
1668 return WERR_OK;
1671 /****************************************************************************
1672 ****************************************************************************/
1674 WERROR clean_up_driver_struct(struct pipes_struct *rpc_pipe,
1675 struct spoolss_AddDriverInfoCtr *r)
1677 switch (r->level) {
1678 case 3:
1679 return clean_up_driver_struct_level(r, rpc_pipe,
1680 r->info.info3->architecture,
1681 &r->info.info3->driver_path,
1682 &r->info.info3->data_file,
1683 &r->info.info3->config_file,
1684 &r->info.info3->help_file,
1685 r->info.info3->dependent_files,
1686 &r->info.info3->version);
1687 case 6:
1688 return clean_up_driver_struct_level(r, rpc_pipe,
1689 r->info.info6->architecture,
1690 &r->info.info6->driver_path,
1691 &r->info.info6->data_file,
1692 &r->info.info6->config_file,
1693 &r->info.info6->help_file,
1694 r->info.info6->dependent_files,
1695 &r->info.info6->version);
1696 default:
1697 return WERR_NOT_SUPPORTED;
1701 /****************************************************************************
1702 This function sucks and should be replaced. JRA.
1703 ****************************************************************************/
1705 static void convert_level_6_to_level3(struct spoolss_AddDriverInfo3 *dst,
1706 const struct spoolss_AddDriverInfo6 *src)
1708 dst->version = src->version;
1710 dst->driver_name = src->driver_name;
1711 dst->architecture = src->architecture;
1712 dst->driver_path = src->driver_path;
1713 dst->data_file = src->data_file;
1714 dst->config_file = src->config_file;
1715 dst->help_file = src->help_file;
1716 dst->monitor_name = src->monitor_name;
1717 dst->default_datatype = src->default_datatype;
1718 dst->_ndr_size_dependent_files = src->_ndr_size_dependent_files;
1719 dst->dependent_files = src->dependent_files;
1722 /****************************************************************************
1723 This function sucks and should be replaced. JRA.
1724 ****************************************************************************/
1726 static void convert_level_8_to_level3(TALLOC_CTX *mem_ctx,
1727 struct spoolss_AddDriverInfo3 *dst,
1728 const struct spoolss_DriverInfo8 *src)
1730 dst->version = src->version;
1731 dst->driver_name = src->driver_name;
1732 dst->architecture = src->architecture;
1733 dst->driver_path = src->driver_path;
1734 dst->data_file = src->data_file;
1735 dst->config_file = src->config_file;
1736 dst->help_file = src->help_file;
1737 dst->monitor_name = src->monitor_name;
1738 dst->default_datatype = src->default_datatype;
1739 if (src->dependent_files) {
1740 dst->dependent_files = talloc_zero(mem_ctx, struct spoolss_StringArray);
1741 if (!dst->dependent_files) return;
1742 dst->dependent_files->string = src->dependent_files;
1743 } else {
1744 dst->dependent_files = NULL;
1748 /****************************************************************************
1749 ****************************************************************************/
1751 static WERROR move_driver_file_to_download_area(TALLOC_CTX *mem_ctx,
1752 connection_struct *conn,
1753 const char *driver_file,
1754 const char *short_architecture,
1755 uint32_t driver_version,
1756 uint32_t version)
1758 struct smb_filename *smb_fname_old = NULL;
1759 struct smb_filename *smb_fname_new = NULL;
1760 char *old_name = NULL;
1761 char *new_name = NULL;
1762 NTSTATUS status;
1763 WERROR ret;
1765 old_name = talloc_asprintf(mem_ctx, "%s/%s",
1766 short_architecture, driver_file);
1767 W_ERROR_HAVE_NO_MEMORY(old_name);
1769 new_name = talloc_asprintf(mem_ctx, "%s/%d/%s",
1770 short_architecture, driver_version, driver_file);
1771 if (new_name == NULL) {
1772 TALLOC_FREE(old_name);
1773 return WERR_NOMEM;
1776 if (version != -1 && (version = file_version_is_newer(conn, old_name, new_name)) > 0) {
1778 status = driver_unix_convert(conn, old_name, &smb_fname_old);
1779 if (!NT_STATUS_IS_OK(status)) {
1780 ret = WERR_NOMEM;
1781 goto out;
1784 /* Setup a synthetic smb_filename struct */
1785 smb_fname_new = TALLOC_ZERO_P(mem_ctx, struct smb_filename);
1786 if (!smb_fname_new) {
1787 ret = WERR_NOMEM;
1788 goto out;
1791 smb_fname_new->base_name = new_name;
1793 DEBUG(10,("move_driver_file_to_download_area: copying '%s' to "
1794 "'%s'\n", smb_fname_old->base_name,
1795 smb_fname_new->base_name));
1797 status = copy_file(mem_ctx, conn, smb_fname_old, smb_fname_new,
1798 OPENX_FILE_EXISTS_TRUNCATE |
1799 OPENX_FILE_CREATE_IF_NOT_EXIST,
1800 0, false);
1802 if (!NT_STATUS_IS_OK(status)) {
1803 DEBUG(0,("move_driver_file_to_download_area: Unable "
1804 "to rename [%s] to [%s]: %s\n",
1805 smb_fname_old->base_name, new_name,
1806 nt_errstr(status)));
1807 ret = WERR_ACCESS_DENIED;
1808 goto out;
1812 ret = WERR_OK;
1813 out:
1814 TALLOC_FREE(smb_fname_old);
1815 TALLOC_FREE(smb_fname_new);
1816 return ret;
1819 WERROR move_driver_to_download_area(struct pipes_struct *p,
1820 struct spoolss_AddDriverInfoCtr *r,
1821 WERROR *perr)
1823 struct spoolss_AddDriverInfo3 *driver;
1824 struct spoolss_AddDriverInfo3 converted_driver;
1825 const char *short_architecture;
1826 struct smb_filename *smb_dname = NULL;
1827 char *new_dir = NULL;
1828 connection_struct *conn = NULL;
1829 NTSTATUS nt_status;
1830 int i;
1831 TALLOC_CTX *ctx = talloc_tos();
1832 int ver = 0;
1833 char *oldcwd;
1834 fstring printdollar;
1835 int printdollar_snum;
1837 *perr = WERR_OK;
1839 switch (r->level) {
1840 case 3:
1841 driver = r->info.info3;
1842 break;
1843 case 6:
1844 convert_level_6_to_level3(&converted_driver, r->info.info6);
1845 driver = &converted_driver;
1846 break;
1847 default:
1848 DEBUG(0,("move_driver_to_download_area: Unknown info level (%u)\n", (unsigned int)r->level));
1849 return WERR_UNKNOWN_LEVEL;
1852 short_architecture = get_short_archi(driver->architecture);
1853 if (!short_architecture) {
1854 return WERR_UNKNOWN_PRINTER_DRIVER;
1857 fstrcpy(printdollar, "print$");
1859 printdollar_snum = find_service(printdollar);
1860 if (printdollar_snum == -1) {
1861 *perr = WERR_NO_SUCH_SHARE;
1862 return WERR_NO_SUCH_SHARE;
1865 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
1866 lp_pathname(printdollar_snum),
1867 p->server_info, &oldcwd);
1868 if (!NT_STATUS_IS_OK(nt_status)) {
1869 DEBUG(0,("move_driver_to_download_area: create_conn_struct "
1870 "returned %s\n", nt_errstr(nt_status)));
1871 *perr = ntstatus_to_werror(nt_status);
1872 return *perr;
1875 new_dir = talloc_asprintf(ctx,
1876 "%s/%d",
1877 short_architecture,
1878 driver->version);
1879 if (!new_dir) {
1880 *perr = WERR_NOMEM;
1881 goto err_exit;
1883 nt_status = driver_unix_convert(conn, new_dir, &smb_dname);
1884 if (!NT_STATUS_IS_OK(nt_status)) {
1885 *perr = WERR_NOMEM;
1886 goto err_exit;
1889 DEBUG(5,("Creating first directory: %s\n", smb_dname->base_name));
1891 create_directory(conn, NULL, smb_dname);
1893 /* For each driver file, archi\filexxx.yyy, if there is a duplicate file
1894 * listed for this driver which has already been moved, skip it (note:
1895 * drivers may list the same file name several times. Then check if the
1896 * file already exists in archi\version\, if so, check that the version
1897 * info (or time stamps if version info is unavailable) is newer (or the
1898 * date is later). If it is, move it to archi\version\filexxx.yyy.
1899 * Otherwise, delete the file.
1901 * If a file is not moved to archi\version\ because of an error, all the
1902 * rest of the 'unmoved' driver files are removed from archi\. If one or
1903 * more of the driver's files was already moved to archi\version\, it
1904 * potentially leaves the driver in a partially updated state. Version
1905 * trauma will most likely occur if an client attempts to use any printer
1906 * bound to the driver. Perhaps a rewrite to make sure the moves can be
1907 * done is appropriate... later JRR
1910 DEBUG(5,("Moving files now !\n"));
1912 if (driver->driver_path && strlen(driver->driver_path)) {
1914 *perr = move_driver_file_to_download_area(ctx,
1915 conn,
1916 driver->driver_path,
1917 short_architecture,
1918 driver->version,
1919 ver);
1920 if (!W_ERROR_IS_OK(*perr)) {
1921 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1922 ver = -1;
1924 goto err_exit;
1928 if (driver->data_file && strlen(driver->data_file)) {
1929 if (!strequal(driver->data_file, driver->driver_path)) {
1931 *perr = move_driver_file_to_download_area(ctx,
1932 conn,
1933 driver->data_file,
1934 short_architecture,
1935 driver->version,
1936 ver);
1937 if (!W_ERROR_IS_OK(*perr)) {
1938 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1939 ver = -1;
1941 goto err_exit;
1946 if (driver->config_file && strlen(driver->config_file)) {
1947 if (!strequal(driver->config_file, driver->driver_path) &&
1948 !strequal(driver->config_file, driver->data_file)) {
1950 *perr = move_driver_file_to_download_area(ctx,
1951 conn,
1952 driver->config_file,
1953 short_architecture,
1954 driver->version,
1955 ver);
1956 if (!W_ERROR_IS_OK(*perr)) {
1957 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1958 ver = -1;
1960 goto err_exit;
1965 if (driver->help_file && strlen(driver->help_file)) {
1966 if (!strequal(driver->help_file, driver->driver_path) &&
1967 !strequal(driver->help_file, driver->data_file) &&
1968 !strequal(driver->help_file, driver->config_file)) {
1970 *perr = move_driver_file_to_download_area(ctx,
1971 conn,
1972 driver->help_file,
1973 short_architecture,
1974 driver->version,
1975 ver);
1976 if (!W_ERROR_IS_OK(*perr)) {
1977 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1978 ver = -1;
1980 goto err_exit;
1985 if (driver->dependent_files && driver->dependent_files->string) {
1986 for (i=0; driver->dependent_files->string[i]; i++) {
1987 if (!strequal(driver->dependent_files->string[i], driver->driver_path) &&
1988 !strequal(driver->dependent_files->string[i], driver->data_file) &&
1989 !strequal(driver->dependent_files->string[i], driver->config_file) &&
1990 !strequal(driver->dependent_files->string[i], driver->help_file)) {
1991 int j;
1992 for (j=0; j < i; j++) {
1993 if (strequal(driver->dependent_files->string[i], driver->dependent_files->string[j])) {
1994 goto NextDriver;
1998 *perr = move_driver_file_to_download_area(ctx,
1999 conn,
2000 driver->dependent_files->string[i],
2001 short_architecture,
2002 driver->version,
2003 ver);
2004 if (!W_ERROR_IS_OK(*perr)) {
2005 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
2006 ver = -1;
2008 goto err_exit;
2011 NextDriver: ;
2015 err_exit:
2016 TALLOC_FREE(smb_dname);
2018 if (conn != NULL) {
2019 vfs_ChDir(conn, oldcwd);
2020 conn_free(conn);
2023 if (W_ERROR_EQUAL(*perr, WERR_OK)) {
2024 return WERR_OK;
2026 if (ver == -1) {
2027 return WERR_UNKNOWN_PRINTER_DRIVER;
2029 return (*perr);
2032 /****************************************************************************
2033 ****************************************************************************/
2035 static uint32 add_a_printer_driver_3(struct spoolss_AddDriverInfo3 *driver)
2037 TALLOC_CTX *ctx = talloc_tos();
2038 int len, buflen;
2039 const char *architecture;
2040 char *directory = NULL;
2041 char *key = NULL;
2042 uint8 *buf;
2043 int i, ret;
2044 TDB_DATA dbuf;
2046 architecture = get_short_archi(driver->architecture);
2047 if (!architecture) {
2048 return (uint32)-1;
2051 /* The names are relative. We store them in the form: \print$\arch\version\driver.xxx
2052 * \\server is added in the rpc server layer.
2053 * It does make sense to NOT store the server's name in the printer TDB.
2056 directory = talloc_asprintf(ctx, "\\print$\\%s\\%d\\",
2057 architecture, driver->version);
2058 if (!directory) {
2059 return (uint32)-1;
2062 #define gen_full_driver_unc_path(ctx, directory, file) \
2063 do { \
2064 if (file && strlen(file)) { \
2065 file = talloc_asprintf(ctx, "%s%s", directory, file); \
2066 } else { \
2067 file = talloc_strdup(ctx, ""); \
2069 if (!file) { \
2070 return (uint32_t)-1; \
2072 } while (0);
2074 /* .inf files do not always list a file for each of the four standard files.
2075 * Don't prepend a path to a null filename, or client claims:
2076 * "The server on which the printer resides does not have a suitable
2077 * <printer driver name> printer driver installed. Click OK if you
2078 * wish to install the driver on your local machine."
2081 gen_full_driver_unc_path(ctx, directory, driver->driver_path);
2082 gen_full_driver_unc_path(ctx, directory, driver->data_file);
2083 gen_full_driver_unc_path(ctx, directory, driver->config_file);
2084 gen_full_driver_unc_path(ctx, directory, driver->help_file);
2086 if (driver->dependent_files && driver->dependent_files->string) {
2087 for (i=0; driver->dependent_files->string[i]; i++) {
2088 gen_full_driver_unc_path(ctx, directory,
2089 driver->dependent_files->string[i]);
2093 key = talloc_asprintf(ctx, "%s%s/%d/%s", DRIVERS_PREFIX,
2094 architecture, driver->version, driver->driver_name);
2095 if (!key) {
2096 return (uint32)-1;
2099 DEBUG(5,("add_a_printer_driver_3: Adding driver with key %s\n", key ));
2101 buf = NULL;
2102 len = buflen = 0;
2104 again:
2105 len = 0;
2106 len += tdb_pack(buf+len, buflen-len, "dffffffff",
2107 driver->version,
2108 driver->driver_name,
2109 driver->architecture,
2110 driver->driver_path,
2111 driver->data_file,
2112 driver->config_file,
2113 driver->help_file,
2114 driver->monitor_name ? driver->monitor_name : "",
2115 driver->default_datatype ? driver->default_datatype : "");
2117 if (driver->dependent_files && driver->dependent_files->string) {
2118 for (i=0; driver->dependent_files->string[i]; i++) {
2119 len += tdb_pack(buf+len, buflen-len, "f",
2120 driver->dependent_files->string[i]);
2124 if (len != buflen) {
2125 buf = (uint8 *)SMB_REALLOC(buf, len);
2126 if (!buf) {
2127 DEBUG(0,("add_a_printer_driver_3: failed to enlarge buffer\n!"));
2128 ret = -1;
2129 goto done;
2131 buflen = len;
2132 goto again;
2135 dbuf.dptr = buf;
2136 dbuf.dsize = len;
2138 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
2140 done:
2141 if (ret)
2142 DEBUG(0,("add_a_printer_driver_3: Adding driver with key %s failed.\n", key ));
2144 SAFE_FREE(buf);
2145 return ret;
2148 /****************************************************************************
2149 ****************************************************************************/
2151 static uint32_t add_a_printer_driver_8(struct spoolss_DriverInfo8 *driver)
2153 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
2154 struct spoolss_AddDriverInfo3 info3;
2155 uint32_t ret;
2157 convert_level_8_to_level3(mem_ctx, &info3, driver);
2159 ret = add_a_printer_driver_3(&info3);
2160 talloc_free(mem_ctx);
2162 return ret;
2165 /****************************************************************************
2166 ****************************************************************************/
2168 static WERROR get_a_printer_driver_3_default(TALLOC_CTX *mem_ctx,
2169 struct spoolss_DriverInfo3 *info,
2170 const char *driver, const char *arch)
2172 info->driver_name = talloc_strdup(mem_ctx, driver);
2173 if (!info->driver_name) {
2174 return WERR_NOMEM;
2177 info->default_datatype = talloc_strdup(mem_ctx, "RAW");
2178 if (!info->default_datatype) {
2179 return WERR_NOMEM;
2182 info->driver_path = talloc_strdup(mem_ctx, "");
2183 info->data_file = talloc_strdup(mem_ctx, "");
2184 info->config_file = talloc_strdup(mem_ctx, "");
2185 info->help_file = talloc_strdup(mem_ctx, "");
2186 if (!info->driver_path || !info->data_file || !info->config_file || !info->help_file) {
2187 return WERR_NOMEM;
2190 return WERR_OK;
2193 /****************************************************************************
2194 ****************************************************************************/
2196 static WERROR get_a_printer_driver_3(TALLOC_CTX *mem_ctx,
2197 struct spoolss_DriverInfo3 *driver,
2198 const char *drivername, const char *arch,
2199 uint32_t version)
2201 TDB_DATA dbuf;
2202 const char *architecture;
2203 int len = 0;
2204 int i;
2205 char *key = NULL;
2206 fstring name, driverpath, environment, datafile, configfile, helpfile, monitorname, defaultdatatype;
2208 architecture = get_short_archi(arch);
2209 if ( !architecture ) {
2210 return WERR_UNKNOWN_PRINTER_DRIVER;
2213 /* Windows 4.0 (i.e. win9x) should always use a version of 0 */
2215 if ( strcmp( architecture, SPL_ARCH_WIN40 ) == 0 )
2216 version = 0;
2218 DEBUG(8,("get_a_printer_driver_3: [%s%s/%d/%s]\n", DRIVERS_PREFIX, architecture, version, drivername));
2220 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
2221 architecture, version, drivername) < 0) {
2222 return WERR_NOMEM;
2225 dbuf = tdb_fetch_bystring(tdb_drivers, key);
2226 if (!dbuf.dptr) {
2227 SAFE_FREE(key);
2228 return WERR_UNKNOWN_PRINTER_DRIVER;
2231 len += tdb_unpack(dbuf.dptr, dbuf.dsize, "dffffffff",
2232 &driver->version,
2233 name,
2234 environment,
2235 driverpath,
2236 datafile,
2237 configfile,
2238 helpfile,
2239 monitorname,
2240 defaultdatatype);
2242 driver->driver_name = talloc_strdup(mem_ctx, name);
2243 driver->architecture = talloc_strdup(mem_ctx, environment);
2244 driver->driver_path = talloc_strdup(mem_ctx, driverpath);
2245 driver->data_file = talloc_strdup(mem_ctx, datafile);
2246 driver->config_file = talloc_strdup(mem_ctx, configfile);
2247 driver->help_file = talloc_strdup(mem_ctx, helpfile);
2248 driver->monitor_name = talloc_strdup(mem_ctx, monitorname);
2249 driver->default_datatype = talloc_strdup(mem_ctx, defaultdatatype);
2251 i=0;
2253 while (len < dbuf.dsize) {
2255 fstring file;
2257 driver->dependent_files = talloc_realloc(mem_ctx, driver->dependent_files, const char *, i+2);
2258 if (!driver->dependent_files ) {
2259 DEBUG(0,("get_a_printer_driver_3: failed to enlarge buffer!\n"));
2260 break;
2263 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "f",
2264 &file);
2266 driver->dependent_files[i] = talloc_strdup(mem_ctx, file);
2268 i++;
2271 if (driver->dependent_files)
2272 driver->dependent_files[i] = NULL;
2274 SAFE_FREE(dbuf.dptr);
2275 SAFE_FREE(key);
2277 if (len != dbuf.dsize) {
2278 return get_a_printer_driver_3_default(mem_ctx, driver, drivername, arch);
2281 return WERR_OK;
2284 /****************************************************************************
2285 ****************************************************************************/
2286 int pack_devicemode(NT_DEVICEMODE *nt_devmode, uint8 *buf, int buflen)
2288 int len = 0;
2290 len += tdb_pack(buf+len, buflen-len, "p", nt_devmode);
2292 if (!nt_devmode)
2293 return len;
2295 len += tdb_pack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2296 nt_devmode->devicename,
2297 nt_devmode->formname,
2299 nt_devmode->specversion,
2300 nt_devmode->driverversion,
2301 nt_devmode->size,
2302 nt_devmode->driverextra,
2303 nt_devmode->orientation,
2304 nt_devmode->papersize,
2305 nt_devmode->paperlength,
2306 nt_devmode->paperwidth,
2307 nt_devmode->scale,
2308 nt_devmode->copies,
2309 nt_devmode->defaultsource,
2310 nt_devmode->printquality,
2311 nt_devmode->color,
2312 nt_devmode->duplex,
2313 nt_devmode->yresolution,
2314 nt_devmode->ttoption,
2315 nt_devmode->collate,
2316 nt_devmode->logpixels,
2318 nt_devmode->fields,
2319 nt_devmode->bitsperpel,
2320 nt_devmode->pelswidth,
2321 nt_devmode->pelsheight,
2322 nt_devmode->displayflags,
2323 nt_devmode->displayfrequency,
2324 nt_devmode->icmmethod,
2325 nt_devmode->icmintent,
2326 nt_devmode->mediatype,
2327 nt_devmode->dithertype,
2328 nt_devmode->reserved1,
2329 nt_devmode->reserved2,
2330 nt_devmode->panningwidth,
2331 nt_devmode->panningheight,
2332 nt_devmode->nt_dev_private);
2334 if (nt_devmode->nt_dev_private) {
2335 len += tdb_pack(buf+len, buflen-len, "B",
2336 nt_devmode->driverextra,
2337 nt_devmode->nt_dev_private);
2340 DEBUG(8,("Packed devicemode [%s]\n", nt_devmode->formname));
2342 return len;
2345 /****************************************************************************
2346 Pack all values in all printer keys
2347 ***************************************************************************/
2349 static int pack_values(NT_PRINTER_DATA *data, uint8 *buf, int buflen)
2351 int len = 0;
2352 int i, j;
2353 struct regval_blob *val;
2354 struct regval_ctr *val_ctr;
2355 char *path = NULL;
2356 int num_values;
2358 if ( !data )
2359 return 0;
2361 /* loop over all keys */
2363 for ( i=0; i<data->num_keys; i++ ) {
2364 val_ctr = data->keys[i].values;
2365 num_values = regval_ctr_numvals( val_ctr );
2367 /* pack the keyname followed by a empty value */
2369 len += tdb_pack(buf+len, buflen-len, "pPdB",
2370 &data->keys[i].name,
2371 data->keys[i].name,
2372 REG_NONE,
2374 NULL);
2376 /* now loop over all values */
2378 for ( j=0; j<num_values; j++ ) {
2379 /* pathname should be stored as <key>\<value> */
2381 val = regval_ctr_specific_value( val_ctr, j );
2382 if (asprintf(&path, "%s\\%s",
2383 data->keys[i].name,
2384 regval_name(val)) < 0) {
2385 return -1;
2388 len += tdb_pack(buf+len, buflen-len, "pPdB",
2389 val,
2390 path,
2391 regval_type(val),
2392 regval_size(val),
2393 regval_data_p(val) );
2395 DEBUG(8,("specific: [%s], len: %d\n", regval_name(val), regval_size(val)));
2396 SAFE_FREE(path);
2401 /* terminator */
2403 len += tdb_pack(buf+len, buflen-len, "p", NULL);
2405 return len;
2409 /****************************************************************************
2410 Delete a printer - this just deletes the printer info file, any open
2411 handles are not affected.
2412 ****************************************************************************/
2414 uint32 del_a_printer(const char *sharename)
2416 TDB_DATA kbuf;
2417 char *printdb_path = NULL;
2418 TALLOC_CTX *ctx = talloc_tos();
2420 kbuf = make_printer_tdbkey(ctx, sharename);
2421 tdb_delete(tdb_printers, kbuf);
2423 kbuf= make_printers_secdesc_tdbkey(ctx, sharename);
2424 tdb_delete(tdb_printers, kbuf);
2426 close_all_print_db();
2428 if (geteuid() == sec_initial_uid()) {
2429 if (asprintf(&printdb_path, "%s%s.tdb",
2430 cache_path("printing/"),
2431 sharename) < 0) {
2432 return (uint32)-1;
2434 unlink(printdb_path);
2435 SAFE_FREE(printdb_path);
2438 return 0;
2441 /****************************************************************************
2442 ****************************************************************************/
2443 static WERROR update_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info)
2445 uint8 *buf;
2446 int buflen, len;
2447 int retlen;
2448 WERROR ret;
2449 TDB_DATA kbuf, dbuf;
2452 * in addprinter: no servername and the printer is the name
2453 * in setprinter: servername is \\server
2454 * and printer is \\server\\printer
2456 * Samba manages only local printers.
2457 * we currently don't support things like i
2458 * path=\\other_server\printer
2460 * We only store the printername, not \\server\printername
2463 if ( info->servername[0] != '\0' ) {
2464 trim_string(info->printername, info->servername, NULL);
2465 trim_char(info->printername, '\\', '\0');
2466 info->servername[0]='\0';
2470 * JFM: one day I'll forget.
2471 * below that's info->portname because that's the SAMBA sharename
2472 * and I made NT 'thinks' it's the portname
2473 * the info->sharename is the thing you can name when you add a printer
2474 * that's the short-name when you create shared printer for 95/98
2475 * So I've made a limitation in SAMBA: you can only have 1 printer model
2476 * behind a SAMBA share.
2479 buf = NULL;
2480 buflen = 0;
2482 again:
2483 len = 0;
2484 len += tdb_pack(buf+len, buflen-len, "dddddddddddfffffPfffff",
2485 info->attributes,
2486 info->priority,
2487 info->default_priority,
2488 info->starttime,
2489 info->untiltime,
2490 info->status,
2491 info->cjobs,
2492 info->averageppm,
2493 info->changeid,
2494 info->c_setprinter,
2495 info->setuptime,
2496 info->servername,
2497 info->printername,
2498 info->sharename,
2499 info->portname,
2500 info->drivername,
2501 info->comment,
2502 info->location,
2503 info->sepfile,
2504 info->printprocessor,
2505 info->datatype,
2506 info->parameters);
2508 len += pack_devicemode(info->devmode, buf+len, buflen-len);
2509 retlen = pack_values( info->data, buf+len, buflen-len );
2510 if (retlen == -1) {
2511 ret = WERR_NOMEM;
2512 goto done;
2514 len += retlen;
2516 if (buflen != len) {
2517 buf = (uint8 *)SMB_REALLOC(buf, len);
2518 if (!buf) {
2519 DEBUG(0,("update_a_printer_2: failed to enlarge buffer!\n"));
2520 ret = WERR_NOMEM;
2521 goto done;
2523 buflen = len;
2524 goto again;
2527 kbuf = make_printer_tdbkey(talloc_tos(), info->sharename );
2529 dbuf.dptr = buf;
2530 dbuf.dsize = len;
2532 ret = (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) == 0? WERR_OK : WERR_NOMEM);
2534 done:
2535 if (!W_ERROR_IS_OK(ret))
2536 DEBUG(8, ("error updating printer to tdb on disk\n"));
2538 SAFE_FREE(buf);
2540 DEBUG(8,("packed printer [%s] with driver [%s] portname=[%s] len=%d\n",
2541 info->sharename, info->drivername, info->portname, len));
2543 return ret;
2547 /****************************************************************************
2548 Malloc and return an NT devicemode.
2549 ****************************************************************************/
2551 NT_DEVICEMODE *construct_nt_devicemode(const fstring default_devicename)
2554 char adevice[MAXDEVICENAME];
2555 NT_DEVICEMODE *nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE);
2557 if (nt_devmode == NULL) {
2558 DEBUG(0,("construct_nt_devicemode: malloc fail.\n"));
2559 return NULL;
2562 ZERO_STRUCTP(nt_devmode);
2564 slprintf(adevice, sizeof(adevice), "%s", default_devicename);
2565 fstrcpy(nt_devmode->devicename, adevice);
2567 fstrcpy(nt_devmode->formname, "Letter");
2569 nt_devmode->specversion = DMSPEC_NT4_AND_ABOVE;
2570 nt_devmode->driverversion = 0x0400;
2571 nt_devmode->size = 0x00DC;
2572 nt_devmode->driverextra = 0x0000;
2573 nt_devmode->fields = DEVMODE_FORMNAME |
2574 DEVMODE_TTOPTION |
2575 DEVMODE_PRINTQUALITY |
2576 DEVMODE_DEFAULTSOURCE |
2577 DEVMODE_COPIES |
2578 DEVMODE_SCALE |
2579 DEVMODE_PAPERSIZE |
2580 DEVMODE_ORIENTATION;
2581 nt_devmode->orientation = DMORIENT_PORTRAIT;
2582 nt_devmode->papersize = DMPAPER_LETTER;
2583 nt_devmode->paperlength = 0;
2584 nt_devmode->paperwidth = 0;
2585 nt_devmode->scale = 0x64;
2586 nt_devmode->copies = 1;
2587 nt_devmode->defaultsource = DMBIN_FORMSOURCE;
2588 nt_devmode->printquality = DMRES_HIGH; /* 0x0258 */
2589 nt_devmode->color = DMRES_MONOCHROME;
2590 nt_devmode->duplex = DMDUP_SIMPLEX;
2591 nt_devmode->yresolution = 0;
2592 nt_devmode->ttoption = DMTT_SUBDEV;
2593 nt_devmode->collate = DMCOLLATE_FALSE;
2594 nt_devmode->icmmethod = 0;
2595 nt_devmode->icmintent = 0;
2596 nt_devmode->mediatype = 0;
2597 nt_devmode->dithertype = 0;
2599 /* non utilisés par un driver d'imprimante */
2600 nt_devmode->logpixels = 0;
2601 nt_devmode->bitsperpel = 0;
2602 nt_devmode->pelswidth = 0;
2603 nt_devmode->pelsheight = 0;
2604 nt_devmode->displayflags = 0;
2605 nt_devmode->displayfrequency = 0;
2606 nt_devmode->reserved1 = 0;
2607 nt_devmode->reserved2 = 0;
2608 nt_devmode->panningwidth = 0;
2609 nt_devmode->panningheight = 0;
2611 nt_devmode->nt_dev_private = NULL;
2612 return nt_devmode;
2615 /****************************************************************************
2616 Clean up and deallocate a (maybe partially) allocated NT_DEVICEMODE.
2617 ****************************************************************************/
2619 void free_nt_devicemode(NT_DEVICEMODE **devmode_ptr)
2621 NT_DEVICEMODE *nt_devmode = *devmode_ptr;
2623 if(nt_devmode == NULL)
2624 return;
2626 DEBUG(106,("free_nt_devicemode: deleting DEVMODE\n"));
2628 SAFE_FREE(nt_devmode->nt_dev_private);
2629 SAFE_FREE(*devmode_ptr);
2632 /****************************************************************************
2633 Clean up and deallocate a (maybe partially) allocated NT_PRINTER_INFO_LEVEL_2.
2634 ****************************************************************************/
2636 static void free_nt_printer_info_level_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr)
2638 NT_PRINTER_INFO_LEVEL_2 *info = *info_ptr;
2640 if ( !info )
2641 return;
2643 free_nt_devicemode(&info->devmode);
2645 TALLOC_FREE( *info_ptr );
2649 /****************************************************************************
2650 ****************************************************************************/
2651 int unpack_devicemode(NT_DEVICEMODE **nt_devmode, const uint8 *buf, int buflen)
2653 int len = 0;
2654 int extra_len = 0;
2655 NT_DEVICEMODE devmode;
2657 ZERO_STRUCT(devmode);
2659 len += tdb_unpack(buf+len, buflen-len, "p", nt_devmode);
2661 if (!*nt_devmode) return len;
2663 len += tdb_unpack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2664 devmode.devicename,
2665 devmode.formname,
2667 &devmode.specversion,
2668 &devmode.driverversion,
2669 &devmode.size,
2670 &devmode.driverextra,
2671 &devmode.orientation,
2672 &devmode.papersize,
2673 &devmode.paperlength,
2674 &devmode.paperwidth,
2675 &devmode.scale,
2676 &devmode.copies,
2677 &devmode.defaultsource,
2678 &devmode.printquality,
2679 &devmode.color,
2680 &devmode.duplex,
2681 &devmode.yresolution,
2682 &devmode.ttoption,
2683 &devmode.collate,
2684 &devmode.logpixels,
2686 &devmode.fields,
2687 &devmode.bitsperpel,
2688 &devmode.pelswidth,
2689 &devmode.pelsheight,
2690 &devmode.displayflags,
2691 &devmode.displayfrequency,
2692 &devmode.icmmethod,
2693 &devmode.icmintent,
2694 &devmode.mediatype,
2695 &devmode.dithertype,
2696 &devmode.reserved1,
2697 &devmode.reserved2,
2698 &devmode.panningwidth,
2699 &devmode.panningheight,
2700 &devmode.nt_dev_private);
2702 if (devmode.nt_dev_private) {
2703 /* the len in tdb_unpack is an int value and
2704 * devmode.driverextra is only a short
2706 len += tdb_unpack(buf+len, buflen-len, "B", &extra_len, &devmode.nt_dev_private);
2707 devmode.driverextra=(uint16)extra_len;
2709 /* check to catch an invalid TDB entry so we don't segfault */
2710 if (devmode.driverextra == 0) {
2711 devmode.nt_dev_private = NULL;
2715 *nt_devmode = (NT_DEVICEMODE *)memdup(&devmode, sizeof(devmode));
2716 if (!*nt_devmode) {
2717 SAFE_FREE(devmode.nt_dev_private);
2718 return -1;
2721 DEBUG(8,("Unpacked devicemode [%s](%s)\n", devmode.devicename, devmode.formname));
2722 if (devmode.nt_dev_private)
2723 DEBUG(8,("with a private section of %d bytes\n", devmode.driverextra));
2725 return len;
2728 /****************************************************************************
2729 Allocate and initialize a new slot.
2730 ***************************************************************************/
2732 int add_new_printer_key( NT_PRINTER_DATA *data, const char *name )
2734 NT_PRINTER_KEY *d;
2735 int key_index;
2737 if ( !name || !data )
2738 return -1;
2740 /* allocate another slot in the NT_PRINTER_KEY array */
2742 if ( !(d = TALLOC_REALLOC_ARRAY( data, data->keys, NT_PRINTER_KEY, data->num_keys+1)) ) {
2743 DEBUG(0,("add_new_printer_key: Realloc() failed!\n"));
2744 return -1;
2747 data->keys = d;
2749 key_index = data->num_keys;
2751 /* initialze new key */
2753 data->keys[key_index].name = talloc_strdup( data, name );
2755 if ( !(data->keys[key_index].values = TALLOC_ZERO_P( data, struct regval_ctr )) )
2756 return -1;
2758 data->num_keys++;
2760 DEBUG(10,("add_new_printer_key: Inserted new data key [%s]\n", name ));
2762 return key_index;
2765 /****************************************************************************
2766 search for a registry key name in the existing printer data
2767 ***************************************************************************/
2769 int delete_printer_key( NT_PRINTER_DATA *data, const char *name )
2771 int i;
2773 for ( i=0; i<data->num_keys; i++ ) {
2774 if ( strequal( data->keys[i].name, name ) ) {
2776 /* cleanup memory */
2778 TALLOC_FREE( data->keys[i].name );
2779 TALLOC_FREE( data->keys[i].values );
2781 /* if not the end of the array, move remaining elements down one slot */
2783 data->num_keys--;
2784 if ( data->num_keys && (i < data->num_keys) )
2785 memmove( &data->keys[i], &data->keys[i+1], sizeof(NT_PRINTER_KEY)*(data->num_keys-i) );
2787 break;
2792 return data->num_keys;
2795 /****************************************************************************
2796 search for a registry key name in the existing printer data
2797 ***************************************************************************/
2799 int lookup_printerkey( NT_PRINTER_DATA *data, const char *name )
2801 int key_index = -1;
2802 int i;
2804 if ( !data || !name )
2805 return -1;
2807 DEBUG(12,("lookup_printerkey: Looking for [%s]\n", name));
2809 /* loop over all existing keys */
2811 for ( i=0; i<data->num_keys; i++ ) {
2812 if ( strequal(data->keys[i].name, name) ) {
2813 DEBUG(12,("lookup_printerkey: Found [%s]!\n", name));
2814 key_index = i;
2815 break;
2820 return key_index;
2823 /****************************************************************************
2824 ***************************************************************************/
2826 int get_printer_subkeys( NT_PRINTER_DATA *data, const char* key, fstring **subkeys )
2828 int i, j;
2829 int key_len;
2830 int num_subkeys = 0;
2831 char *p;
2832 fstring *subkeys_ptr = NULL;
2833 fstring subkeyname;
2835 *subkeys = NULL;
2837 if ( !data )
2838 return 0;
2840 if ( !key )
2841 return -1;
2843 /* special case of asking for the top level printer data registry key names */
2845 if ( strlen(key) == 0 ) {
2846 for ( i=0; i<data->num_keys; i++ ) {
2848 /* found a match, so allocate space and copy the name */
2850 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
2851 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
2852 num_subkeys+1));
2853 return -1;
2856 fstrcpy( subkeys_ptr[num_subkeys], data->keys[i].name );
2857 num_subkeys++;
2860 goto done;
2863 /* asking for the subkeys of some key */
2864 /* subkey paths are stored in the key name using '\' as the delimiter */
2866 for ( i=0; i<data->num_keys; i++ ) {
2867 if ( StrnCaseCmp(data->keys[i].name, key, strlen(key)) == 0 ) {
2869 /* if we found the exact key, then break */
2870 key_len = strlen( key );
2871 if ( strlen(data->keys[i].name) == key_len )
2872 break;
2874 /* get subkey path */
2876 p = data->keys[i].name + key_len;
2877 if ( *p == '\\' )
2878 p++;
2879 fstrcpy( subkeyname, p );
2880 if ( (p = strchr( subkeyname, '\\' )) )
2881 *p = '\0';
2883 /* don't add a key more than once */
2885 for ( j=0; j<num_subkeys; j++ ) {
2886 if ( strequal( subkeys_ptr[j], subkeyname ) )
2887 break;
2890 if ( j != num_subkeys )
2891 continue;
2893 /* found a match, so allocate space and copy the name */
2895 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
2896 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
2897 num_subkeys+1));
2898 return 0;
2901 fstrcpy( subkeys_ptr[num_subkeys], subkeyname );
2902 num_subkeys++;
2907 /* return error if the key was not found */
2909 if ( i == data->num_keys ) {
2910 SAFE_FREE(subkeys_ptr);
2911 return -1;
2914 done:
2915 /* tag off the end */
2917 if (num_subkeys)
2918 fstrcpy(subkeys_ptr[num_subkeys], "" );
2920 *subkeys = subkeys_ptr;
2922 return num_subkeys;
2925 #ifdef HAVE_ADS
2926 static void map_sz_into_ctr(struct regval_ctr *ctr, const char *val_name,
2927 const char *sz)
2929 regval_ctr_delvalue(ctr, val_name);
2930 regval_ctr_addvalue_sz(ctr, val_name, sz);
2933 static void map_dword_into_ctr(struct regval_ctr *ctr, const char *val_name,
2934 uint32 dword)
2936 regval_ctr_delvalue(ctr, val_name);
2937 regval_ctr_addvalue(ctr, val_name, REG_DWORD,
2938 (char *) &dword, sizeof(dword));
2941 static void map_bool_into_ctr(struct regval_ctr *ctr, const char *val_name,
2942 bool b)
2944 uint8 bin_bool = (b ? 1 : 0);
2945 regval_ctr_delvalue(ctr, val_name);
2946 regval_ctr_addvalue(ctr, val_name, REG_BINARY,
2947 (char *) &bin_bool, sizeof(bin_bool));
2950 static void map_single_multi_sz_into_ctr(struct regval_ctr *ctr, const char *val_name,
2951 const char *multi_sz)
2953 const char *a[2];
2955 a[0] = multi_sz;
2956 a[1] = NULL;
2958 regval_ctr_delvalue(ctr, val_name);
2959 regval_ctr_addvalue_multi_sz(ctr, val_name, a);
2962 /****************************************************************************
2963 * Map the NT_PRINTER_INFO_LEVEL_2 data into DsSpooler keys for publishing.
2965 * @param info2 NT_PRINTER_INFO_LEVEL_2 describing printer - gets modified
2966 * @return bool indicating success or failure
2967 ***************************************************************************/
2969 static bool map_nt_printer_info2_to_dsspooler(NT_PRINTER_INFO_LEVEL_2 *info2)
2971 struct regval_ctr *ctr = NULL;
2972 fstring longname;
2973 const char *dnssuffix;
2974 char *allocated_string = NULL;
2975 const char *ascii_str;
2976 int i;
2978 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
2979 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
2980 ctr = info2->data->keys[i].values;
2982 map_sz_into_ctr(ctr, SPOOL_REG_PRINTERNAME, info2->sharename);
2983 map_sz_into_ctr(ctr, SPOOL_REG_SHORTSERVERNAME, global_myname());
2985 /* we make the assumption that the netbios name is the same
2986 as the DNS name sinc ethe former will be what we used to
2987 join the domain */
2989 dnssuffix = get_mydnsdomname(talloc_tos());
2990 if (dnssuffix && *dnssuffix) {
2991 fstr_sprintf( longname, "%s.%s", global_myname(), dnssuffix );
2992 } else {
2993 fstrcpy( longname, global_myname() );
2996 map_sz_into_ctr(ctr, SPOOL_REG_SERVERNAME, longname);
2998 if (asprintf(&allocated_string, "\\\\%s\\%s", longname, info2->sharename) == -1) {
2999 return false;
3001 map_sz_into_ctr(ctr, SPOOL_REG_UNCNAME, allocated_string);
3002 SAFE_FREE(allocated_string);
3004 map_dword_into_ctr(ctr, SPOOL_REG_VERSIONNUMBER, 4);
3005 map_sz_into_ctr(ctr, SPOOL_REG_DRIVERNAME, info2->drivername);
3006 map_sz_into_ctr(ctr, SPOOL_REG_LOCATION, info2->location);
3007 map_sz_into_ctr(ctr, SPOOL_REG_DESCRIPTION, info2->comment);
3008 map_single_multi_sz_into_ctr(ctr, SPOOL_REG_PORTNAME, info2->portname);
3009 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSEPARATORFILE, info2->sepfile);
3010 map_dword_into_ctr(ctr, SPOOL_REG_PRINTSTARTTIME, info2->starttime);
3011 map_dword_into_ctr(ctr, SPOOL_REG_PRINTENDTIME, info2->untiltime);
3012 map_dword_into_ctr(ctr, SPOOL_REG_PRIORITY, info2->priority);
3014 map_bool_into_ctr(ctr, SPOOL_REG_PRINTKEEPPRINTEDJOBS,
3015 (info2->attributes &
3016 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
3018 switch (info2->attributes & 0x3) {
3019 case 0:
3020 ascii_str = SPOOL_REGVAL_PRINTWHILESPOOLING;
3021 break;
3022 case 1:
3023 ascii_str = SPOOL_REGVAL_PRINTAFTERSPOOLED;
3024 break;
3025 case 2:
3026 ascii_str = SPOOL_REGVAL_PRINTDIRECT;
3027 break;
3028 default:
3029 ascii_str = "unknown";
3031 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSPOOLING, ascii_str);
3033 return True;
3036 /*****************************************************************
3037 ****************************************************************/
3039 static void store_printer_guid(NT_PRINTER_INFO_LEVEL_2 *info2,
3040 struct GUID guid)
3042 int i;
3043 struct regval_ctr *ctr=NULL;
3045 /* find the DsSpooler key */
3046 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3047 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3048 ctr = info2->data->keys[i].values;
3050 regval_ctr_delvalue(ctr, "objectGUID");
3052 /* We used to store this as a REG_BINARY but that causes
3053 Vista to whine */
3055 regval_ctr_addvalue_sz(ctr, "objectGUID",
3056 GUID_string(talloc_tos(), &guid));
3059 static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
3060 NT_PRINTER_INFO_LEVEL *printer)
3062 ADS_STATUS ads_rc;
3063 LDAPMessage *res;
3064 char *prt_dn = NULL, *srv_dn, *srv_cn_0, *srv_cn_escaped, *sharename_escaped;
3065 char *srv_dn_utf8, **srv_cn_utf8;
3066 TALLOC_CTX *ctx;
3067 ADS_MODLIST mods;
3068 const char *attrs[] = {"objectGUID", NULL};
3069 struct GUID guid;
3070 WERROR win_rc = WERR_OK;
3071 size_t converted_size;
3073 /* build the ads mods */
3074 ctx = talloc_init("nt_printer_publish_ads");
3075 if (ctx == NULL) {
3076 return WERR_NOMEM;
3079 DEBUG(5, ("publishing printer %s\n", printer->info_2->printername));
3081 /* figure out where to publish */
3082 ads_find_machine_acct(ads, &res, global_myname());
3084 /* We use ldap_get_dn here as we need the answer
3085 * in utf8 to call ldap_explode_dn(). JRA. */
3087 srv_dn_utf8 = ldap_get_dn((LDAP *)ads->ldap.ld, (LDAPMessage *)res);
3088 if (!srv_dn_utf8) {
3089 TALLOC_FREE(ctx);
3090 return WERR_SERVER_UNAVAILABLE;
3092 ads_msgfree(ads, res);
3093 srv_cn_utf8 = ldap_explode_dn(srv_dn_utf8, 1);
3094 if (!srv_cn_utf8) {
3095 TALLOC_FREE(ctx);
3096 ldap_memfree(srv_dn_utf8);
3097 return WERR_SERVER_UNAVAILABLE;
3099 /* Now convert to CH_UNIX. */
3100 if (!pull_utf8_talloc(ctx, &srv_dn, srv_dn_utf8, &converted_size)) {
3101 TALLOC_FREE(ctx);
3102 ldap_memfree(srv_dn_utf8);
3103 ldap_memfree(srv_cn_utf8);
3104 return WERR_SERVER_UNAVAILABLE;
3106 if (!pull_utf8_talloc(ctx, &srv_cn_0, srv_cn_utf8[0], &converted_size)) {
3107 TALLOC_FREE(ctx);
3108 ldap_memfree(srv_dn_utf8);
3109 ldap_memfree(srv_cn_utf8);
3110 TALLOC_FREE(srv_dn);
3111 return WERR_SERVER_UNAVAILABLE;
3114 ldap_memfree(srv_dn_utf8);
3115 ldap_memfree(srv_cn_utf8);
3117 srv_cn_escaped = escape_rdn_val_string_alloc(srv_cn_0);
3118 if (!srv_cn_escaped) {
3119 TALLOC_FREE(ctx);
3120 return WERR_SERVER_UNAVAILABLE;
3122 sharename_escaped = escape_rdn_val_string_alloc(printer->info_2->sharename);
3123 if (!sharename_escaped) {
3124 SAFE_FREE(srv_cn_escaped);
3125 TALLOC_FREE(ctx);
3126 return WERR_SERVER_UNAVAILABLE;
3129 prt_dn = talloc_asprintf(ctx, "cn=%s-%s,%s", srv_cn_escaped, sharename_escaped, srv_dn);
3131 SAFE_FREE(srv_cn_escaped);
3132 SAFE_FREE(sharename_escaped);
3134 mods = ads_init_mods(ctx);
3136 if (mods == NULL) {
3137 SAFE_FREE(prt_dn);
3138 TALLOC_FREE(ctx);
3139 return WERR_NOMEM;
3142 get_local_printer_publishing_data(ctx, &mods, printer->info_2->data);
3143 ads_mod_str(ctx, &mods, SPOOL_REG_PRINTERNAME,
3144 printer->info_2->sharename);
3146 /* publish it */
3147 ads_rc = ads_mod_printer_entry(ads, prt_dn, ctx, &mods);
3148 if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT) {
3149 int i;
3150 for (i=0; mods[i] != 0; i++)
3152 mods[i] = (LDAPMod *)-1;
3153 ads_rc = ads_add_printer_entry(ads, prt_dn, ctx, &mods);
3156 if (!ADS_ERR_OK(ads_rc))
3157 DEBUG(3, ("error publishing %s: %s\n", printer->info_2->sharename, ads_errstr(ads_rc)));
3159 /* retreive the guid and store it locally */
3160 if (ADS_ERR_OK(ads_search_dn(ads, &res, prt_dn, attrs))) {
3161 ZERO_STRUCT(guid);
3162 ads_pull_guid(ads, res, &guid);
3163 ads_msgfree(ads, res);
3164 store_printer_guid(printer->info_2, guid);
3165 win_rc = mod_a_printer(printer, 2);
3167 TALLOC_FREE(ctx);
3169 return win_rc;
3172 static WERROR nt_printer_unpublish_ads(ADS_STRUCT *ads,
3173 NT_PRINTER_INFO_LEVEL *printer)
3175 ADS_STATUS ads_rc;
3176 LDAPMessage *res = NULL;
3177 char *prt_dn = NULL;
3179 DEBUG(5, ("unpublishing printer %s\n", printer->info_2->printername));
3181 /* remove the printer from the directory */
3182 ads_rc = ads_find_printer_on_server(ads, &res,
3183 printer->info_2->sharename, global_myname());
3185 if (ADS_ERR_OK(ads_rc) && res && ads_count_replies(ads, res)) {
3186 prt_dn = ads_get_dn(ads, talloc_tos(), res);
3187 if (!prt_dn) {
3188 ads_msgfree(ads, res);
3189 return WERR_NOMEM;
3191 ads_rc = ads_del_dn(ads, prt_dn);
3192 TALLOC_FREE(prt_dn);
3195 if (res) {
3196 ads_msgfree(ads, res);
3198 return WERR_OK;
3201 /****************************************************************************
3202 * Publish a printer in the directory
3204 * @param snum describing printer service
3205 * @return WERROR indicating status of publishing
3206 ***************************************************************************/
3208 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3210 ADS_STATUS ads_rc;
3211 ADS_STRUCT *ads = NULL;
3212 NT_PRINTER_INFO_LEVEL *printer = NULL;
3213 WERROR win_rc;
3215 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3216 if (!W_ERROR_IS_OK(win_rc))
3217 goto done;
3219 switch (action) {
3220 case DSPRINT_PUBLISH:
3221 case DSPRINT_UPDATE:
3222 /* set the DsSpooler info and attributes */
3223 if (!(map_nt_printer_info2_to_dsspooler(printer->info_2))) {
3224 win_rc = WERR_NOMEM;
3225 goto done;
3228 printer->info_2->attributes |= PRINTER_ATTRIBUTE_PUBLISHED;
3229 break;
3230 case DSPRINT_UNPUBLISH:
3231 printer->info_2->attributes ^= PRINTER_ATTRIBUTE_PUBLISHED;
3232 break;
3233 default:
3234 win_rc = WERR_NOT_SUPPORTED;
3235 goto done;
3238 win_rc = mod_a_printer(printer, 2);
3239 if (!W_ERROR_IS_OK(win_rc)) {
3240 DEBUG(3, ("err %d saving data\n", W_ERROR_V(win_rc)));
3241 goto done;
3244 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3245 if (!ads) {
3246 DEBUG(3, ("ads_init() failed\n"));
3247 win_rc = WERR_SERVER_UNAVAILABLE;
3248 goto done;
3250 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3251 SAFE_FREE(ads->auth.password);
3252 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3253 NULL, NULL);
3255 /* ads_connect() will find the DC for us */
3256 ads_rc = ads_connect(ads);
3257 if (!ADS_ERR_OK(ads_rc)) {
3258 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3259 win_rc = WERR_ACCESS_DENIED;
3260 goto done;
3263 switch (action) {
3264 case DSPRINT_PUBLISH:
3265 case DSPRINT_UPDATE:
3266 win_rc = nt_printer_publish_ads(ads, printer);
3267 break;
3268 case DSPRINT_UNPUBLISH:
3269 win_rc = nt_printer_unpublish_ads(ads, printer);
3270 break;
3273 done:
3274 free_a_printer(&printer, 2);
3275 ads_destroy(&ads);
3276 return win_rc;
3279 WERROR check_published_printers(void)
3281 ADS_STATUS ads_rc;
3282 ADS_STRUCT *ads = NULL;
3283 int snum;
3284 int n_services = lp_numservices();
3285 NT_PRINTER_INFO_LEVEL *printer = NULL;
3287 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3288 if (!ads) {
3289 DEBUG(3, ("ads_init() failed\n"));
3290 return WERR_SERVER_UNAVAILABLE;
3292 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3293 SAFE_FREE(ads->auth.password);
3294 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3295 NULL, NULL);
3297 /* ads_connect() will find the DC for us */
3298 ads_rc = ads_connect(ads);
3299 if (!ADS_ERR_OK(ads_rc)) {
3300 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3301 ads_destroy(&ads);
3302 ads_kdestroy("MEMORY:prtpub_cache");
3303 return WERR_ACCESS_DENIED;
3306 for (snum = 0; snum < n_services; snum++) {
3307 if (!(lp_snum_ok(snum) && lp_print_ok(snum)))
3308 continue;
3310 if (W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2,
3311 lp_servicename(snum))) &&
3312 (printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED))
3313 nt_printer_publish_ads(ads, printer);
3315 free_a_printer(&printer, 2);
3318 ads_destroy(&ads);
3319 ads_kdestroy("MEMORY:prtpub_cache");
3320 return WERR_OK;
3323 bool is_printer_published(Printer_entry *print_hnd, int snum,
3324 struct GUID *guid)
3326 NT_PRINTER_INFO_LEVEL *printer = NULL;
3327 struct regval_ctr *ctr;
3328 struct regval_blob *guid_val;
3329 WERROR win_rc;
3330 int i;
3331 bool ret = False;
3332 DATA_BLOB blob;
3334 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3336 if (!W_ERROR_IS_OK(win_rc) ||
3337 !(printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED) ||
3338 ((i = lookup_printerkey(printer->info_2->data, SPOOL_DSSPOOLER_KEY)) < 0) ||
3339 !(ctr = printer->info_2->data->keys[i].values) ||
3340 !(guid_val = regval_ctr_getvalue(ctr, "objectGUID")))
3342 free_a_printer(&printer, 2);
3343 return False;
3346 /* fetching printer guids really ought to be a separate function. */
3348 if ( guid ) {
3349 char *guid_str;
3351 /* We used to store the guid as REG_BINARY, then swapped
3352 to REG_SZ for Vista compatibility so check for both */
3354 switch ( regval_type(guid_val) ){
3355 case REG_SZ:
3356 blob = data_blob_const(regval_data_p(guid_val),
3357 regval_size(guid_val));
3358 pull_reg_sz(talloc_tos(), &blob, (const char **)&guid_str);
3359 ret = NT_STATUS_IS_OK(GUID_from_string( guid_str, guid ));
3360 talloc_free(guid_str);
3361 break;
3362 case REG_BINARY:
3363 if ( regval_size(guid_val) != sizeof(struct GUID) ) {
3364 ret = False;
3365 break;
3367 memcpy(guid, regval_data_p(guid_val), sizeof(struct GUID));
3368 break;
3369 default:
3370 DEBUG(0,("is_printer_published: GUID value stored as "
3371 "invaluid type (%d)\n", regval_type(guid_val) ));
3372 break;
3376 free_a_printer(&printer, 2);
3377 return ret;
3379 #else
3380 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3382 return WERR_OK;
3385 WERROR check_published_printers(void)
3387 return WERR_OK;
3390 bool is_printer_published(Printer_entry *print_hnd, int snum,
3391 struct GUID *guid)
3393 return False;
3395 #endif /* HAVE_ADS */
3397 /****************************************************************************
3398 ***************************************************************************/
3400 WERROR delete_all_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key )
3402 NT_PRINTER_DATA *data;
3403 int i;
3404 int removed_keys = 0;
3405 int empty_slot;
3407 data = p2->data;
3408 empty_slot = data->num_keys;
3410 if ( !key )
3411 return WERR_INVALID_PARAM;
3413 /* remove all keys */
3415 if ( !strlen(key) ) {
3417 TALLOC_FREE( data );
3419 p2->data = NULL;
3421 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from printer [%s]\n",
3422 p2->printername ));
3424 return WERR_OK;
3427 /* remove a specific key (and all subkeys) */
3429 for ( i=0; i<data->num_keys; i++ ) {
3430 if ( StrnCaseCmp( data->keys[i].name, key, strlen(key)) == 0 ) {
3431 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from key [%s]\n",
3432 data->keys[i].name));
3434 TALLOC_FREE( data->keys[i].name );
3435 TALLOC_FREE( data->keys[i].values );
3437 /* mark the slot as empty */
3439 ZERO_STRUCTP( &data->keys[i] );
3443 /* find the first empty slot */
3445 for ( i=0; i<data->num_keys; i++ ) {
3446 if ( !data->keys[i].name ) {
3447 empty_slot = i;
3448 removed_keys++;
3449 break;
3453 if ( i == data->num_keys )
3454 /* nothing was removed */
3455 return WERR_INVALID_PARAM;
3457 /* move everything down */
3459 for ( i=empty_slot+1; i<data->num_keys; i++ ) {
3460 if ( data->keys[i].name ) {
3461 memcpy( &data->keys[empty_slot], &data->keys[i], sizeof(NT_PRINTER_KEY) );
3462 ZERO_STRUCTP( &data->keys[i] );
3463 empty_slot++;
3464 removed_keys++;
3468 /* update count */
3470 data->num_keys -= removed_keys;
3472 /* sanity check to see if anything is left */
3474 if ( !data->num_keys ) {
3475 DEBUG(8,("delete_all_printer_data: No keys left for printer [%s]\n", p2->printername ));
3477 SAFE_FREE( data->keys );
3478 ZERO_STRUCTP( data );
3481 return WERR_OK;
3484 /****************************************************************************
3485 ***************************************************************************/
3487 WERROR delete_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3489 WERROR result = WERR_OK;
3490 int key_index;
3492 /* we must have names on non-zero length */
3494 if ( !key || !*key|| !value || !*value )
3495 return WERR_INVALID_NAME;
3497 /* find the printer key first */
3499 key_index = lookup_printerkey( p2->data, key );
3500 if ( key_index == -1 )
3501 return WERR_OK;
3503 /* make sure the value exists so we can return the correct error code */
3505 if ( !regval_ctr_getvalue( p2->data->keys[key_index].values, value ) )
3506 return WERR_BADFILE;
3508 regval_ctr_delvalue( p2->data->keys[key_index].values, value );
3510 DEBUG(8,("delete_printer_data: Removed key => [%s], value => [%s]\n",
3511 key, value ));
3513 return result;
3516 /****************************************************************************
3517 ***************************************************************************/
3519 WERROR add_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value,
3520 uint32 type, uint8 *data, int real_len )
3522 WERROR result = WERR_OK;
3523 int key_index;
3525 /* we must have names on non-zero length */
3527 if ( !key || !*key|| !value || !*value )
3528 return WERR_INVALID_NAME;
3530 /* find the printer key first */
3532 key_index = lookup_printerkey( p2->data, key );
3533 if ( key_index == -1 )
3534 key_index = add_new_printer_key( p2->data, key );
3536 if ( key_index == -1 )
3537 return WERR_NOMEM;
3539 regval_ctr_addvalue( p2->data->keys[key_index].values, value,
3540 type, (const char *)data, real_len );
3542 DEBUG(8,("add_printer_data: Added key => [%s], value => [%s], type=> [%d], size => [%d]\n",
3543 key, value, type, real_len ));
3545 return result;
3548 /****************************************************************************
3549 ***************************************************************************/
3551 struct regval_blob* get_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3553 int key_index;
3555 if ( (key_index = lookup_printerkey( p2->data, key )) == -1 )
3556 return NULL;
3558 DEBUG(8,("get_printer_data: Attempting to lookup key => [%s], value => [%s]\n",
3559 key, value ));
3561 return regval_ctr_getvalue( p2->data->keys[key_index].values, value );
3564 /****************************************************************************
3565 Unpack a list of registry values frem the TDB
3566 ***************************************************************************/
3568 static int unpack_values(NT_PRINTER_DATA *printer_data, const uint8 *buf, int buflen)
3570 int len = 0;
3571 uint32 type;
3572 fstring string;
3573 const char *valuename = NULL;
3574 const char *keyname = NULL;
3575 char *str;
3576 int size;
3577 uint8 *data_p;
3578 struct regval_blob *regval_p;
3579 int key_index;
3581 /* add the "PrinterDriverData" key first for performance reasons */
3583 add_new_printer_key( printer_data, SPOOL_PRINTERDATA_KEY );
3585 /* loop and unpack the rest of the registry values */
3587 while ( True ) {
3589 /* check to see if there are any more registry values */
3591 regval_p = NULL;
3592 len += tdb_unpack(buf+len, buflen-len, "p", &regval_p);
3593 if ( !regval_p )
3594 break;
3596 /* unpack the next regval */
3598 len += tdb_unpack(buf+len, buflen-len, "fdB",
3599 string,
3600 &type,
3601 &size,
3602 &data_p);
3604 /* lookup for subkey names which have a type of REG_NONE */
3605 /* there's no data with this entry */
3607 if ( type == REG_NONE ) {
3608 if ( (key_index=lookup_printerkey( printer_data, string)) == -1 )
3609 add_new_printer_key( printer_data, string );
3610 continue;
3614 * break of the keyname from the value name.
3615 * Valuenames can have embedded '\'s so be careful.
3616 * only support one level of keys. See the
3617 * "Konica Fiery S300 50C-K v1.1. enu" 2k driver.
3618 * -- jerry
3621 str = strchr_m( string, '\\');
3623 /* Put in "PrinterDriverData" is no key specified */
3625 if ( !str ) {
3626 keyname = SPOOL_PRINTERDATA_KEY;
3627 valuename = string;
3629 else {
3630 *str = '\0';
3631 keyname = string;
3632 valuename = str+1;
3635 /* see if we need a new key */
3637 if ( (key_index=lookup_printerkey( printer_data, keyname )) == -1 )
3638 key_index = add_new_printer_key( printer_data, keyname );
3640 if ( key_index == -1 ) {
3641 DEBUG(0,("unpack_values: Failed to allocate a new key [%s]!\n",
3642 keyname));
3643 break;
3646 DEBUG(8,("specific: [%s:%s], len: %d\n", keyname, valuename, size));
3648 /* Vista doesn't like unknown REG_BINARY values in DsSpooler.
3649 Thanks to Martin Zielinski for the hint. */
3651 if ( type == REG_BINARY &&
3652 strequal( keyname, SPOOL_DSSPOOLER_KEY ) &&
3653 strequal( valuename, "objectGUID" ) )
3655 struct GUID guid;
3657 /* convert the GUID to a UNICODE string */
3659 memcpy( &guid, data_p, sizeof(struct GUID) );
3661 regval_ctr_addvalue_sz(printer_data->keys[key_index].values,
3662 valuename,
3663 GUID_string(talloc_tos(), &guid));
3665 } else {
3666 /* add the value */
3668 regval_ctr_addvalue( printer_data->keys[key_index].values,
3669 valuename, type, (const char *)data_p,
3670 size );
3673 SAFE_FREE(data_p); /* 'B' option to tdbpack does a malloc() */
3677 return len;
3680 /****************************************************************************
3681 ***************************************************************************/
3683 static char *last_from;
3684 static char *last_to;
3686 static const char *get_last_from(void)
3688 if (!last_from) {
3689 return "";
3691 return last_from;
3694 static const char *get_last_to(void)
3696 if (!last_to) {
3697 return "";
3699 return last_to;
3702 static bool set_last_from_to(const char *from, const char *to)
3704 char *orig_from = last_from;
3705 char *orig_to = last_to;
3707 last_from = SMB_STRDUP(from);
3708 last_to = SMB_STRDUP(to);
3710 SAFE_FREE(orig_from);
3711 SAFE_FREE(orig_to);
3713 if (!last_from || !last_to) {
3714 SAFE_FREE(last_from);
3715 SAFE_FREE(last_to);
3716 return false;
3718 return true;
3721 static void map_to_os2_driver(fstring drivername)
3723 char *mapfile = lp_os2_driver_map();
3724 char **lines = NULL;
3725 int numlines = 0;
3726 int i;
3728 if (!strlen(drivername))
3729 return;
3731 if (!*mapfile)
3732 return;
3734 if (strequal(drivername,get_last_from())) {
3735 DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",
3736 drivername,get_last_to()));
3737 fstrcpy(drivername,get_last_to());
3738 return;
3741 lines = file_lines_load(mapfile, &numlines,0,NULL);
3742 if (numlines == 0 || lines == NULL) {
3743 DEBUG(0,("No entries in OS/2 driver map %s\n",mapfile));
3744 TALLOC_FREE(lines);
3745 return;
3748 DEBUG(4,("Scanning OS/2 driver map %s\n",mapfile));
3750 for( i = 0; i < numlines; i++) {
3751 char *nt_name = lines[i];
3752 char *os2_name = strchr(nt_name,'=');
3754 if (!os2_name)
3755 continue;
3757 *os2_name++ = 0;
3759 while (isspace(*nt_name))
3760 nt_name++;
3762 if (!*nt_name || strchr("#;",*nt_name))
3763 continue;
3766 int l = strlen(nt_name);
3767 while (l && isspace(nt_name[l-1])) {
3768 nt_name[l-1] = 0;
3769 l--;
3773 while (isspace(*os2_name))
3774 os2_name++;
3777 int l = strlen(os2_name);
3778 while (l && isspace(os2_name[l-1])) {
3779 os2_name[l-1] = 0;
3780 l--;
3784 if (strequal(nt_name,drivername)) {
3785 DEBUG(3,("Mapped windows driver %s to os2 driver%s\n",drivername,os2_name));
3786 set_last_from_to(drivername,os2_name);
3787 fstrcpy(drivername,os2_name);
3788 TALLOC_FREE(lines);
3789 return;
3793 TALLOC_FREE(lines);
3796 /****************************************************************************
3797 Get a default printer info 2 struct.
3798 ****************************************************************************/
3800 static WERROR get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 *info,
3801 const char *servername,
3802 const char* sharename,
3803 bool get_loc_com)
3805 int snum = lp_servicenumber(sharename);
3807 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
3808 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
3809 servername, sharename);
3810 fstrcpy(info->sharename, sharename);
3811 fstrcpy(info->portname, SAMBA_PRINTER_PORT_NAME);
3813 /* by setting the driver name to an empty string, a local NT admin
3814 can now run the **local** APW to install a local printer driver
3815 for a Samba shared printer in 2.2. Without this, drivers **must** be
3816 installed on the Samba server for NT clients --jerry */
3817 #if 0 /* JERRY --do not uncomment-- */
3818 if (!*info->drivername)
3819 fstrcpy(info->drivername, "NO DRIVER AVAILABLE FOR THIS PRINTER");
3820 #endif
3823 DEBUG(10,("get_a_printer_2_default: driver name set to [%s]\n", info->drivername));
3825 strlcpy(info->comment, "", sizeof(info->comment));
3826 fstrcpy(info->printprocessor, "winprint");
3827 fstrcpy(info->datatype, "RAW");
3829 #ifdef HAVE_CUPS
3830 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
3831 /* Pull the location and comment strings from cups if we don't
3832 already have one */
3833 if ( !strlen(info->location) || !strlen(info->comment) )
3834 cups_pull_comment_location( info );
3836 #endif
3838 info->attributes = PRINTER_ATTRIBUTE_SAMBA;
3840 info->starttime = 0; /* Minutes since 12:00am GMT */
3841 info->untiltime = 0; /* Minutes since 12:00am GMT */
3842 info->priority = 1;
3843 info->default_priority = 1;
3844 info->setuptime = (uint32)time(NULL);
3847 * I changed this as I think it is better to have a generic
3848 * DEVMODE than to crash Win2k explorer.exe --jerry
3849 * See the HP Deskjet 990c Win2k drivers for an example.
3851 * However the default devmode appears to cause problems
3852 * with the HP CLJ 8500 PCL driver. Hence the addition of
3853 * the "default devmode" parameter --jerry 22/01/2002
3856 if (lp_default_devmode(snum)) {
3857 if ((info->devmode = construct_nt_devicemode(info->printername)) == NULL) {
3858 goto fail;
3860 } else {
3861 info->devmode = NULL;
3864 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
3865 goto fail;
3868 info->data = TALLOC_ZERO_P(info, NT_PRINTER_DATA);
3869 if (!info->data) {
3870 goto fail;
3873 add_new_printer_key(info->data, SPOOL_PRINTERDATA_KEY);
3875 return WERR_OK;
3877 fail:
3878 if (info->devmode)
3879 free_nt_devicemode(&info->devmode);
3881 return WERR_ACCESS_DENIED;
3884 /****************************************************************************
3885 ****************************************************************************/
3887 static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info,
3888 const char *servername,
3889 const char *sharename,
3890 bool get_loc_com)
3892 int len = 0;
3893 int snum = lp_servicenumber(sharename);
3894 TDB_DATA kbuf, dbuf;
3895 fstring printername;
3896 char adevice[MAXDEVICENAME];
3897 char *comment = NULL;
3899 kbuf = make_printer_tdbkey(talloc_tos(), sharename);
3901 dbuf = tdb_fetch(tdb_printers, kbuf);
3902 if (!dbuf.dptr) {
3903 return get_a_printer_2_default(info, servername,
3904 sharename, get_loc_com);
3907 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "dddddddddddfffffPfffff",
3908 &info->attributes,
3909 &info->priority,
3910 &info->default_priority,
3911 &info->starttime,
3912 &info->untiltime,
3913 &info->status,
3914 &info->cjobs,
3915 &info->averageppm,
3916 &info->changeid,
3917 &info->c_setprinter,
3918 &info->setuptime,
3919 info->servername,
3920 info->printername,
3921 info->sharename,
3922 info->portname,
3923 info->drivername,
3924 &comment,
3925 info->location,
3926 info->sepfile,
3927 info->printprocessor,
3928 info->datatype,
3929 info->parameters);
3931 if (comment) {
3932 strlcpy(info->comment, comment, sizeof(info->comment));
3933 SAFE_FREE(comment);
3936 /* Samba has to have shared raw drivers. */
3937 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
3938 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
3940 /* Restore the stripped strings. */
3941 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
3943 if ( lp_force_printername(snum) ) {
3944 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, sharename );
3945 } else {
3946 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, info->printername);
3949 fstrcpy(info->printername, printername);
3951 #ifdef HAVE_CUPS
3952 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
3953 /* Pull the location and comment strings from cups if we don't
3954 already have one */
3955 if ( !strlen(info->location) || !strlen(info->comment) )
3956 cups_pull_comment_location( info );
3958 #endif
3960 len += unpack_devicemode(&info->devmode,dbuf.dptr+len, dbuf.dsize-len);
3963 * Some client drivers freak out if there is a NULL devmode
3964 * (probably the driver is not checking before accessing
3965 * the devmode pointer) --jerry
3967 * See comments in get_a_printer_2_default()
3970 if (lp_default_devmode(snum) && !info->devmode) {
3971 DEBUG(8,("get_a_printer_2: Constructing a default device mode for [%s]\n",
3972 printername));
3973 info->devmode = construct_nt_devicemode(printername);
3976 slprintf( adevice, sizeof(adevice), "%s", info->printername );
3977 if (info->devmode) {
3978 fstrcpy(info->devmode->devicename, adevice);
3981 if ( !(info->data = TALLOC_ZERO_P( info, NT_PRINTER_DATA )) ) {
3982 DEBUG(0,("unpack_values: talloc() failed!\n"));
3983 SAFE_FREE(dbuf.dptr);
3984 return WERR_NOMEM;
3986 len += unpack_values( info->data, dbuf.dptr+len, dbuf.dsize-len );
3988 /* This will get the current RPC talloc context, but we should be
3989 passing this as a parameter... fixme... JRA ! */
3991 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
3992 SAFE_FREE(dbuf.dptr);
3993 return WERR_NOMEM;
3996 /* Fix for OS/2 drivers. */
3998 if (get_remote_arch() == RA_OS2) {
3999 map_to_os2_driver(info->drivername);
4002 SAFE_FREE(dbuf.dptr);
4004 DEBUG(9,("Unpacked printer [%s] name [%s] running driver [%s]\n",
4005 sharename, info->printername, info->drivername));
4007 return WERR_OK;
4010 /****************************************************************************
4011 Debugging function, dump at level 6 the struct in the logs.
4012 ****************************************************************************/
4013 static uint32 dump_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4015 uint32 result;
4016 NT_PRINTER_INFO_LEVEL_2 *info2;
4018 DEBUG(106,("Dumping printer at level [%d]\n", level));
4020 switch (level) {
4021 case 2:
4023 if (printer->info_2 == NULL)
4024 result=5;
4025 else
4027 info2=printer->info_2;
4029 DEBUGADD(106,("attributes:[%d]\n", info2->attributes));
4030 DEBUGADD(106,("priority:[%d]\n", info2->priority));
4031 DEBUGADD(106,("default_priority:[%d]\n", info2->default_priority));
4032 DEBUGADD(106,("starttime:[%d]\n", info2->starttime));
4033 DEBUGADD(106,("untiltime:[%d]\n", info2->untiltime));
4034 DEBUGADD(106,("status:[%d]\n", info2->status));
4035 DEBUGADD(106,("cjobs:[%d]\n", info2->cjobs));
4036 DEBUGADD(106,("averageppm:[%d]\n", info2->averageppm));
4037 DEBUGADD(106,("changeid:[%d]\n", info2->changeid));
4038 DEBUGADD(106,("c_setprinter:[%d]\n", info2->c_setprinter));
4039 DEBUGADD(106,("setuptime:[%d]\n", info2->setuptime));
4041 DEBUGADD(106,("servername:[%s]\n", info2->servername));
4042 DEBUGADD(106,("printername:[%s]\n", info2->printername));
4043 DEBUGADD(106,("sharename:[%s]\n", info2->sharename));
4044 DEBUGADD(106,("portname:[%s]\n", info2->portname));
4045 DEBUGADD(106,("drivername:[%s]\n", info2->drivername));
4046 DEBUGADD(106,("comment:[%s]\n", info2->comment));
4047 DEBUGADD(106,("location:[%s]\n", info2->location));
4048 DEBUGADD(106,("sepfile:[%s]\n", info2->sepfile));
4049 DEBUGADD(106,("printprocessor:[%s]\n", info2->printprocessor));
4050 DEBUGADD(106,("datatype:[%s]\n", info2->datatype));
4051 DEBUGADD(106,("parameters:[%s]\n", info2->parameters));
4052 result=0;
4054 break;
4056 default:
4057 DEBUGADD(106,("dump_a_printer: Level %u not implemented\n", (unsigned int)level ));
4058 result=1;
4059 break;
4062 return result;
4065 /****************************************************************************
4066 Update the changeid time.
4067 This is SO NASTY as some drivers need this to change, others need it
4068 static. This value will change every second, and I must hope that this
4069 is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
4070 UTAH ! JRA.
4071 ****************************************************************************/
4073 static uint32 rev_changeid(void)
4075 struct timeval tv;
4077 get_process_uptime(&tv);
4079 #if 1 /* JERRY */
4080 /* Return changeid as msec since spooler restart */
4081 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
4082 #else
4084 * This setting seems to work well but is too untested
4085 * to replace the above calculation. Left in for experiementation
4086 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
4088 return tv.tv_sec * 10 + tv.tv_usec / 100000;
4089 #endif
4094 * The function below are the high level ones.
4095 * only those ones must be called from the spoolss code.
4096 * JFM.
4099 /****************************************************************************
4100 Modify a printer. This is called from SETPRINTERDATA/DELETEPRINTERDATA.
4101 ****************************************************************************/
4103 WERROR mod_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4105 WERROR result;
4107 dump_a_printer(printer, level);
4109 switch (level) {
4110 case 2:
4113 * Update the changestamp. Emperical tests show that the
4114 * ChangeID is always updated,but c_setprinter is
4115 * global spooler variable (not per printer).
4118 /* ChangeID **must** be increasing over the lifetime
4119 of client's spoolss service in order for the
4120 client's cache to show updates */
4122 printer->info_2->changeid = rev_changeid();
4125 * Because one day someone will ask:
4126 * NT->NT An admin connection to a remote
4127 * printer show changes imeediately in
4128 * the properities dialog
4130 * A non-admin connection will only show the
4131 * changes after viewing the properites page
4132 * 2 times. Seems to be related to a
4133 * race condition in the client between the spooler
4134 * updating the local cache and the Explorer.exe GUI
4135 * actually displaying the properties.
4137 * This is fixed in Win2k. admin/non-admin
4138 * connections both display changes immediately.
4140 * 14/12/01 --jerry
4143 result=update_a_printer_2(printer->info_2);
4144 break;
4146 default:
4147 result=WERR_UNKNOWN_LEVEL;
4148 break;
4151 return result;
4154 /****************************************************************************
4155 Initialize printer devmode & data with previously saved driver init values.
4156 ****************************************************************************/
4158 static bool set_driver_init_2( NT_PRINTER_INFO_LEVEL_2 *info_ptr )
4160 int len = 0;
4161 char *key = NULL;
4162 TDB_DATA dbuf;
4163 NT_PRINTER_INFO_LEVEL_2 info;
4166 ZERO_STRUCT(info);
4169 * Delete any printer data 'values' already set. When called for driver
4170 * replace, there will generally be some, but during an add printer, there
4171 * should not be any (if there are delete them).
4174 if ( info_ptr->data )
4175 delete_all_printer_data( info_ptr, "" );
4177 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX,
4178 info_ptr->drivername) < 0) {
4179 return false;
4182 dbuf = tdb_fetch_bystring(tdb_drivers, key);
4183 if (!dbuf.dptr) {
4185 * When changing to a driver that has no init info in the tdb, remove
4186 * the previous drivers init info and leave the new on blank.
4188 free_nt_devicemode(&info_ptr->devmode);
4189 SAFE_FREE(key);
4190 return false;
4193 SAFE_FREE(key);
4195 * Get the saved DEVMODE..
4198 len += unpack_devicemode(&info.devmode,dbuf.dptr+len, dbuf.dsize-len);
4201 * The saved DEVMODE contains the devicename from the printer used during
4202 * the initialization save. Change it to reflect the new printer.
4205 if ( info.devmode ) {
4206 ZERO_STRUCT(info.devmode->devicename);
4207 fstrcpy(info.devmode->devicename, info_ptr->printername);
4211 * NT/2k does not change out the entire DeviceMode of a printer
4212 * when changing the driver. Only the driverextra, private, &
4213 * driverversion fields. --jerry (Thu Mar 14 08:58:43 CST 2002)
4215 * Later examination revealed that Windows NT/2k does reset the
4216 * the printer's device mode, bit **only** when you change a
4217 * property of the device mode such as the page orientation.
4218 * --jerry
4222 /* Bind the saved DEVMODE to the new the printer */
4224 free_nt_devicemode(&info_ptr->devmode);
4225 info_ptr->devmode = info.devmode;
4227 DEBUG(10,("set_driver_init_2: Set printer [%s] init %s DEVMODE for driver [%s]\n",
4228 info_ptr->printername, info_ptr->devmode?"VALID":"NULL", info_ptr->drivername));
4230 /* Add the printer data 'values' to the new printer */
4232 if ( !(info_ptr->data = TALLOC_ZERO_P( info_ptr, NT_PRINTER_DATA )) ) {
4233 DEBUG(0,("set_driver_init_2: talloc() failed!\n"));
4234 return False;
4237 len += unpack_values( info_ptr->data, dbuf.dptr+len, dbuf.dsize-len );
4239 SAFE_FREE(dbuf.dptr);
4241 return true;
4244 /****************************************************************************
4245 Initialize printer devmode & data with previously saved driver init values.
4246 When a printer is created using AddPrinter, the drivername bound to the
4247 printer is used to lookup previously saved driver initialization info, which
4248 is bound to the new printer.
4249 ****************************************************************************/
4251 bool set_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4253 bool result = False;
4255 switch (level) {
4256 case 2:
4257 result = set_driver_init_2(printer->info_2);
4258 break;
4260 default:
4261 DEBUG(0,("set_driver_init: Programmer's error! Unknown driver_init level [%d]\n",
4262 level));
4263 break;
4266 return result;
4269 /****************************************************************************
4270 Delete driver init data stored for a specified driver
4271 ****************************************************************************/
4273 bool del_driver_init(const char *drivername)
4275 char *key;
4276 bool ret;
4278 if (!drivername || !*drivername) {
4279 DEBUG(3,("del_driver_init: No drivername specified!\n"));
4280 return false;
4283 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, drivername) < 0) {
4284 return false;
4287 DEBUG(6,("del_driver_init: Removing driver init data for [%s]\n",
4288 drivername));
4290 ret = (tdb_delete_bystring(tdb_drivers, key) == 0);
4291 SAFE_FREE(key);
4292 return ret;
4295 /****************************************************************************
4296 Pack up the DEVMODE and values for a printer into a 'driver init' entry
4297 in the tdb. Note: this is different from the driver entry and the printer
4298 entry. There should be a single driver init entry for each driver regardless
4299 of whether it was installed from NT or 2K. Technically, they should be
4300 different, but they work out to the same struct.
4301 ****************************************************************************/
4303 static uint32 update_driver_init_2(NT_PRINTER_INFO_LEVEL_2 *info)
4305 char *key = NULL;
4306 uint8 *buf;
4307 int buflen, len, ret;
4308 int retlen;
4309 TDB_DATA dbuf;
4311 buf = NULL;
4312 buflen = 0;
4314 again:
4315 len = 0;
4316 len += pack_devicemode(info->devmode, buf+len, buflen-len);
4318 retlen = pack_values( info->data, buf+len, buflen-len );
4319 if (retlen == -1) {
4320 ret = -1;
4321 goto done;
4323 len += retlen;
4325 if (buflen < len) {
4326 buf = (uint8 *)SMB_REALLOC(buf, len);
4327 if (!buf) {
4328 DEBUG(0, ("update_driver_init_2: failed to enlarge buffer!\n"));
4329 ret = -1;
4330 goto done;
4332 buflen = len;
4333 goto again;
4336 SAFE_FREE(key);
4337 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, info->drivername) < 0) {
4338 ret = (uint32)-1;
4339 goto done;
4342 dbuf.dptr = buf;
4343 dbuf.dsize = len;
4345 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
4347 done:
4348 if (ret == -1)
4349 DEBUG(8, ("update_driver_init_2: error updating printer init to tdb on disk\n"));
4351 SAFE_FREE(buf);
4353 DEBUG(10,("update_driver_init_2: Saved printer [%s] init DEVMODE & values for driver [%s]\n",
4354 info->sharename, info->drivername));
4356 return ret;
4359 /****************************************************************************
4360 Update (i.e. save) the driver init info (DEVMODE and values) for a printer
4361 ****************************************************************************/
4363 static uint32 update_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4365 uint32 result;
4367 dump_a_printer(printer, level);
4369 switch (level) {
4370 case 2:
4371 result = update_driver_init_2(printer->info_2);
4372 break;
4373 default:
4374 result = 1;
4375 break;
4378 return result;
4381 /****************************************************************************
4382 Convert the printer data value, a REG_BINARY array, into an initialization
4383 DEVMODE. Note: the array must be parsed as if it was a DEVMODE in an rpc...
4384 got to keep the endians happy :).
4385 ****************************************************************************/
4387 static bool convert_driver_init(TALLOC_CTX *mem_ctx, NT_DEVICEMODE *nt_devmode,
4388 const uint8_t *data, uint32_t data_len)
4390 struct spoolss_DeviceMode devmode;
4391 enum ndr_err_code ndr_err;
4392 DATA_BLOB blob;
4394 ZERO_STRUCT(devmode);
4396 blob = data_blob_const(data, data_len);
4398 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, NULL, &devmode,
4399 (ndr_pull_flags_fn_t)ndr_pull_spoolss_DeviceMode);
4400 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
4401 DEBUG(10,("convert_driver_init: error parsing spoolss_DeviceMode\n"));
4402 return false;
4405 return convert_devicemode("", &devmode, &nt_devmode);
4408 /****************************************************************************
4409 Set the DRIVER_INIT info in the tdb. Requires Win32 client code that:
4411 1. Use the driver's config DLL to this UNC printername and:
4412 a. Call DrvPrintEvent with PRINTER_EVENT_INITIALIZE
4413 b. Call DrvConvertDevMode with CDM_DRIVER_DEFAULT to get default DEVMODE
4414 2. Call SetPrinterData with the 'magic' key and the DEVMODE as data.
4416 The last step triggers saving the "driver initialization" information for
4417 this printer into the tdb. Later, new printers that use this driver will
4418 have this initialization information bound to them. This simulates the
4419 driver initialization, as if it had run on the Samba server (as it would
4420 have done on NT).
4422 The Win32 client side code requirement sucks! But until we can run arbitrary
4423 Win32 printer driver code on any Unix that Samba runs on, we are stuck with it.
4425 It would have been easier to use SetPrinter because all the UNMARSHALLING of
4426 the DEVMODE is done there, but 2K/XP clients do not set the DEVMODE... think
4427 about it and you will realize why. JRR 010720
4428 ****************************************************************************/
4430 static WERROR save_driver_init_2(NT_PRINTER_INFO_LEVEL *printer, uint8 *data, uint32 data_len )
4432 WERROR status = WERR_OK;
4433 TALLOC_CTX *ctx = NULL;
4434 NT_DEVICEMODE *nt_devmode = NULL;
4435 NT_DEVICEMODE *tmp_devmode = printer->info_2->devmode;
4438 * When the DEVMODE is already set on the printer, don't try to unpack it.
4440 DEBUG(8,("save_driver_init_2: Enter...\n"));
4442 if ( !printer->info_2->devmode && data_len ) {
4444 * Set devmode on printer info, so entire printer initialization can be
4445 * saved to tdb.
4448 if ((ctx = talloc_init("save_driver_init_2")) == NULL)
4449 return WERR_NOMEM;
4451 if ((nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE)) == NULL) {
4452 status = WERR_NOMEM;
4453 goto done;
4456 ZERO_STRUCTP(nt_devmode);
4459 * The DEVMODE is held in the 'data' component of the param in raw binary.
4460 * Convert it to to a devmode structure
4462 if ( !convert_driver_init( ctx, nt_devmode, data, data_len )) {
4463 DEBUG(10,("save_driver_init_2: error converting DEVMODE\n"));
4464 status = WERR_INVALID_PARAM;
4465 goto done;
4468 printer->info_2->devmode = nt_devmode;
4472 * Pack up and add (or update) the DEVMODE and any current printer data to
4473 * a 'driver init' element in the tdb
4477 if ( update_driver_init(printer, 2) != 0 ) {
4478 DEBUG(10,("save_driver_init_2: error updating DEVMODE\n"));
4479 status = WERR_NOMEM;
4480 goto done;
4484 * If driver initialization info was successfully saved, set the current
4485 * printer to match it. This allows initialization of the current printer
4486 * as well as the driver.
4488 status = mod_a_printer(printer, 2);
4489 if (!W_ERROR_IS_OK(status)) {
4490 DEBUG(10,("save_driver_init_2: error setting DEVMODE on printer [%s]\n",
4491 printer->info_2->printername));
4494 done:
4495 talloc_destroy(ctx);
4496 free_nt_devicemode( &nt_devmode );
4498 printer->info_2->devmode = tmp_devmode;
4500 return status;
4503 /****************************************************************************
4504 Update the driver init info (DEVMODE and specifics) for a printer
4505 ****************************************************************************/
4507 WERROR save_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level, uint8 *data, uint32 data_len)
4509 WERROR status = WERR_OK;
4511 switch (level) {
4512 case 2:
4513 status = save_driver_init_2( printer, data, data_len );
4514 break;
4515 default:
4516 status = WERR_UNKNOWN_LEVEL;
4517 break;
4520 return status;
4523 /****************************************************************************
4524 Get a NT_PRINTER_INFO_LEVEL struct. It returns malloced memory.
4526 Previously the code had a memory allocation problem because it always
4527 used the TALLOC_CTX from the Printer_entry*. This context lasts
4528 as a long as the original handle is open. So if the client made a lot
4529 of getprinter[data]() calls, the memory usage would climb. Now we use
4530 a short lived TALLOC_CTX for printer_info_2 objects returned. We
4531 still use the Printer_entry->ctx for maintaining the cache copy though
4532 since that object must live as long as the handle by definition.
4533 --jerry
4535 ****************************************************************************/
4537 static WERROR get_a_printer_internal( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level,
4538 const char *sharename, bool get_loc_com)
4540 WERROR result;
4541 fstring servername;
4543 DEBUG(10,("get_a_printer: [%s] level %u\n", sharename, (unsigned int)level));
4545 if ( !(*pp_printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
4546 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4547 return WERR_NOMEM;
4550 switch (level) {
4551 case 2:
4552 if ( !((*pp_printer)->info_2 = TALLOC_ZERO_P(*pp_printer, NT_PRINTER_INFO_LEVEL_2)) ) {
4553 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4554 TALLOC_FREE( *pp_printer );
4555 return WERR_NOMEM;
4558 if ( print_hnd )
4559 fstrcpy( servername, print_hnd->servername );
4560 else {
4561 fstrcpy( servername, "%L" );
4562 standard_sub_basic( "", "", servername,
4563 sizeof(servername)-1 );
4566 result = get_a_printer_2( (*pp_printer)->info_2,
4567 servername, sharename, get_loc_com);
4569 /* we have a new printer now. Save it with this handle */
4571 if ( !W_ERROR_IS_OK(result) ) {
4572 TALLOC_FREE( *pp_printer );
4573 DEBUG(10,("get_a_printer: [%s] level %u returning %s\n",
4574 sharename, (unsigned int)level, win_errstr(result)));
4575 return result;
4578 dump_a_printer( *pp_printer, level);
4580 break;
4582 default:
4583 TALLOC_FREE( *pp_printer );
4584 return WERR_UNKNOWN_LEVEL;
4587 return WERR_OK;
4590 WERROR get_a_printer( Printer_entry *print_hnd,
4591 NT_PRINTER_INFO_LEVEL **pp_printer,
4592 uint32 level,
4593 const char *sharename)
4595 return get_a_printer_internal(print_hnd, pp_printer, level,
4596 sharename, true);
4599 WERROR get_a_printer_search( Printer_entry *print_hnd,
4600 NT_PRINTER_INFO_LEVEL **pp_printer,
4601 uint32 level,
4602 const char *sharename)
4604 return get_a_printer_internal(print_hnd, pp_printer, level,
4605 sharename, false);
4608 /****************************************************************************
4609 Deletes a NT_PRINTER_INFO_LEVEL struct.
4610 ****************************************************************************/
4612 uint32 free_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level)
4614 NT_PRINTER_INFO_LEVEL *printer = *pp_printer;
4616 if ( !printer )
4617 return 0;
4619 switch (level) {
4620 case 2:
4621 if ( printer->info_2 )
4622 free_nt_printer_info_level_2(&printer->info_2);
4623 break;
4625 default:
4626 DEBUG(0,("free_a_printer: unknown level! [%d]\n", level ));
4627 return 1;
4630 TALLOC_FREE(*pp_printer);
4632 return 0;
4635 /****************************************************************************
4636 ****************************************************************************/
4638 uint32_t add_a_printer_driver(TALLOC_CTX *mem_ctx,
4639 struct spoolss_AddDriverInfoCtr *r,
4640 char **driver_name,
4641 uint32_t *version)
4643 struct spoolss_DriverInfo8 info8;
4645 ZERO_STRUCT(info8);
4647 DEBUG(10,("adding a printer at level [%d]\n", r->level));
4649 switch (r->level) {
4650 case 3:
4651 info8.version = r->info.info3->version;
4652 info8.driver_name = r->info.info3->driver_name;
4653 info8.architecture = r->info.info3->architecture;
4654 info8.driver_path = r->info.info3->driver_path;
4655 info8.data_file = r->info.info3->data_file;
4656 info8.config_file = r->info.info3->config_file;
4657 info8.help_file = r->info.info3->help_file;
4658 info8.monitor_name = r->info.info3->monitor_name;
4659 info8.default_datatype = r->info.info3->default_datatype;
4660 if (r->info.info3->dependent_files && r->info.info3->dependent_files->string) {
4661 info8.dependent_files = r->info.info3->dependent_files->string;
4663 break;
4664 case 6:
4665 info8.version = r->info.info6->version;
4666 info8.driver_name = r->info.info6->driver_name;
4667 info8.architecture = r->info.info6->architecture;
4668 info8.driver_path = r->info.info6->driver_path;
4669 info8.data_file = r->info.info6->data_file;
4670 info8.config_file = r->info.info6->config_file;
4671 info8.help_file = r->info.info6->help_file;
4672 info8.monitor_name = r->info.info6->monitor_name;
4673 info8.default_datatype = r->info.info6->default_datatype;
4674 if (r->info.info6->dependent_files && r->info.info6->dependent_files->string) {
4675 info8.dependent_files = r->info.info6->dependent_files->string;
4677 info8.driver_date = r->info.info6->driver_date;
4678 info8.driver_version = r->info.info6->driver_version;
4679 info8.manufacturer_name = r->info.info6->manufacturer_name;
4680 info8.manufacturer_url = r->info.info6->manufacturer_url;
4681 info8.hardware_id = r->info.info6->hardware_id;
4682 info8.provider = r->info.info6->provider;
4683 break;
4684 case 8:
4685 info8.version = r->info.info8->version;
4686 info8.driver_name = r->info.info8->driver_name;
4687 info8.architecture = r->info.info8->architecture;
4688 info8.driver_path = r->info.info8->driver_path;
4689 info8.data_file = r->info.info8->data_file;
4690 info8.config_file = r->info.info8->config_file;
4691 info8.help_file = r->info.info8->help_file;
4692 info8.monitor_name = r->info.info8->monitor_name;
4693 info8.default_datatype = r->info.info8->default_datatype;
4694 if (r->info.info8->dependent_files && r->info.info8->dependent_files->string) {
4695 info8.dependent_files = r->info.info8->dependent_files->string;
4697 if (r->info.info8->previous_names && r->info.info8->previous_names->string) {
4698 info8.previous_names = r->info.info8->previous_names->string;
4700 info8.driver_date = r->info.info8->driver_date;
4701 info8.driver_version = r->info.info8->driver_version;
4702 info8.manufacturer_name = r->info.info8->manufacturer_name;
4703 info8.manufacturer_url = r->info.info8->manufacturer_url;
4704 info8.hardware_id = r->info.info8->hardware_id;
4705 info8.provider = r->info.info8->provider;
4706 info8.print_processor = r->info.info8->print_processor;
4707 info8.vendor_setup = r->info.info8->vendor_setup;
4708 if (r->info.info8->color_profiles && r->info.info8->color_profiles->string) {
4709 info8.color_profiles = r->info.info8->color_profiles->string;
4711 info8.inf_path = r->info.info8->inf_path;
4712 info8.printer_driver_attributes = r->info.info8->printer_driver_attributes;
4713 if (r->info.info8->core_driver_dependencies && r->info.info8->core_driver_dependencies->string) {
4714 info8.core_driver_dependencies = r->info.info8->core_driver_dependencies->string;
4716 info8.min_inbox_driver_ver_date = r->info.info8->min_inbox_driver_ver_date;
4717 info8.min_inbox_driver_ver_version = r->info.info8->min_inbox_driver_ver_version;
4718 break;
4719 default:
4720 return -1;
4723 *driver_name = talloc_strdup(mem_ctx, info8.driver_name);
4724 if (!*driver_name) {
4725 return -1;
4727 *version = info8.version;
4729 return add_a_printer_driver_8(&info8);
4732 /****************************************************************************
4733 ****************************************************************************/
4735 WERROR get_a_printer_driver(TALLOC_CTX *mem_ctx,
4736 struct spoolss_DriverInfo8 **driver,
4737 const char *drivername, const char *architecture,
4738 uint32_t version)
4740 WERROR result;
4741 struct spoolss_DriverInfo3 info3;
4742 struct spoolss_DriverInfo8 *info8;
4744 ZERO_STRUCT(info3);
4746 /* Sometime we just want any version of the driver */
4748 if (version == DRIVER_ANY_VERSION) {
4749 /* look for Win2k first and then for NT4 */
4750 result = get_a_printer_driver_3(mem_ctx,
4751 &info3,
4752 drivername,
4753 architecture, 3);
4754 if (!W_ERROR_IS_OK(result)) {
4755 result = get_a_printer_driver_3(mem_ctx,
4756 &info3,
4757 drivername,
4758 architecture, 2);
4760 } else {
4761 result = get_a_printer_driver_3(mem_ctx,
4762 &info3,
4763 drivername,
4764 architecture,
4765 version);
4768 if (!W_ERROR_IS_OK(result)) {
4769 return result;
4772 info8 = talloc_zero(mem_ctx, struct spoolss_DriverInfo8);
4773 if (!info8) {
4774 return WERR_NOMEM;
4777 info8->version = info3.version;
4778 info8->driver_name = info3.driver_name;
4779 info8->architecture = info3.architecture;
4780 info8->driver_path = info3.driver_path;
4781 info8->data_file = info3.data_file;
4782 info8->config_file = info3.config_file;
4783 info8->help_file = info3.help_file;
4784 info8->dependent_files = info3.dependent_files;
4785 info8->monitor_name = info3.monitor_name;
4786 info8->default_datatype = info3.default_datatype;
4788 *driver = info8;
4790 return WERR_OK;
4793 /****************************************************************************
4794 ****************************************************************************/
4796 uint32_t free_a_printer_driver(struct spoolss_DriverInfo8 *driver)
4798 talloc_free(driver);
4799 return 0;
4803 /****************************************************************************
4804 Determine whether or not a particular driver is currently assigned
4805 to a printer
4806 ****************************************************************************/
4808 bool printer_driver_in_use(const struct spoolss_DriverInfo8 *r)
4810 int snum;
4811 int n_services = lp_numservices();
4812 NT_PRINTER_INFO_LEVEL *printer = NULL;
4813 bool in_use = False;
4815 if (!r) {
4816 return false;
4819 DEBUG(10,("printer_driver_in_use: Beginning search through ntprinters.tdb...\n"));
4821 /* loop through the printers.tdb and check for the drivername */
4823 for (snum=0; snum<n_services && !in_use; snum++) {
4824 if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
4825 continue;
4827 if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_servicename(snum))) )
4828 continue;
4830 if (strequal(r->driver_name, printer->info_2->drivername))
4831 in_use = True;
4833 free_a_printer( &printer, 2 );
4836 DEBUG(10,("printer_driver_in_use: Completed search through ntprinters.tdb...\n"));
4838 if ( in_use ) {
4839 struct spoolss_DriverInfo8 *d;
4840 WERROR werr;
4842 DEBUG(5,("printer_driver_in_use: driver \"%s\" is currently in use\n", r->driver_name));
4844 /* we can still remove the driver if there is one of
4845 "Windows NT x86" version 2 or 3 left */
4847 if (!strequal("Windows NT x86", r->architecture)) {
4848 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", DRIVER_ANY_VERSION);
4850 else {
4851 switch (r->version) {
4852 case 2:
4853 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", 3);
4854 break;
4855 case 3:
4856 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", 2);
4857 break;
4858 default:
4859 DEBUG(0,("printer_driver_in_use: ERROR! unknown driver version (%d)\n",
4860 r->version));
4861 werr = WERR_UNKNOWN_PRINTER_DRIVER;
4862 break;
4866 /* now check the error code */
4868 if ( W_ERROR_IS_OK(werr) ) {
4869 /* it's ok to remove the driver, we have other architctures left */
4870 in_use = False;
4871 free_a_printer_driver(d);
4875 /* report that the driver is not in use by default */
4877 return in_use;
4881 /**********************************************************************
4882 Check to see if a ogiven file is in use by *info
4883 *********************************************************************/
4885 static bool drv_file_in_use(const char *file, const struct spoolss_DriverInfo8 *info)
4887 int i = 0;
4889 if ( !info )
4890 return False;
4892 /* mz: skip files that are in the list but already deleted */
4893 if (!file || !file[0]) {
4894 return false;
4897 if (strequal(file, info->driver_path))
4898 return True;
4900 if (strequal(file, info->data_file))
4901 return True;
4903 if (strequal(file, info->config_file))
4904 return True;
4906 if (strequal(file, info->help_file))
4907 return True;
4909 /* see of there are any dependent files to examine */
4911 if (!info->dependent_files)
4912 return False;
4914 while (info->dependent_files[i] && *info->dependent_files[i]) {
4915 if (strequal(file, info->dependent_files[i]))
4916 return True;
4917 i++;
4920 return False;
4924 /**********************************************************************
4925 Utility function to remove the dependent file pointed to by the
4926 input parameter from the list
4927 *********************************************************************/
4929 static void trim_dependent_file(TALLOC_CTX *mem_ctx, const char **files, int idx)
4932 /* bump everything down a slot */
4934 while (files && files[idx+1]) {
4935 files[idx] = talloc_strdup(mem_ctx, files[idx+1]);
4936 idx++;
4939 files[idx] = NULL;
4941 return;
4944 /**********************************************************************
4945 Check if any of the files used by src are also used by drv
4946 *********************************************************************/
4948 static bool trim_overlap_drv_files(TALLOC_CTX *mem_ctx,
4949 struct spoolss_DriverInfo8 *src,
4950 const struct spoolss_DriverInfo8 *drv)
4952 bool in_use = False;
4953 int i = 0;
4955 if ( !src || !drv )
4956 return False;
4958 /* check each file. Remove it from the src structure if it overlaps */
4960 if (drv_file_in_use(src->driver_path, drv)) {
4961 in_use = True;
4962 DEBUG(10,("Removing driverfile [%s] from list\n", src->driver_path));
4963 src->driver_path = talloc_strdup(mem_ctx, "");
4964 if (!src->driver_path) { return false; }
4967 if (drv_file_in_use(src->data_file, drv)) {
4968 in_use = True;
4969 DEBUG(10,("Removing datafile [%s] from list\n", src->data_file));
4970 src->data_file = talloc_strdup(mem_ctx, "");
4971 if (!src->data_file) { return false; }
4974 if (drv_file_in_use(src->config_file, drv)) {
4975 in_use = True;
4976 DEBUG(10,("Removing configfile [%s] from list\n", src->config_file));
4977 src->config_file = talloc_strdup(mem_ctx, "");
4978 if (!src->config_file) { return false; }
4981 if (drv_file_in_use(src->help_file, drv)) {
4982 in_use = True;
4983 DEBUG(10,("Removing helpfile [%s] from list\n", src->help_file));
4984 src->help_file = talloc_strdup(mem_ctx, "");
4985 if (!src->help_file) { return false; }
4988 /* are there any dependentfiles to examine? */
4990 if (!src->dependent_files)
4991 return in_use;
4993 while (src->dependent_files[i] && *src->dependent_files[i]) {
4994 if (drv_file_in_use(src->dependent_files[i], drv)) {
4995 in_use = True;
4996 DEBUG(10,("Removing [%s] from dependent file list\n", src->dependent_files[i]));
4997 trim_dependent_file(mem_ctx, src->dependent_files, i);
4998 } else
4999 i++;
5002 return in_use;
5005 /****************************************************************************
5006 Determine whether or not a particular driver files are currently being
5007 used by any other driver.
5009 Return value is True if any files were in use by other drivers
5010 and False otherwise.
5012 Upon return, *info has been modified to only contain the driver files
5013 which are not in use
5015 Fix from mz:
5017 This needs to check all drivers to ensure that all files in use
5018 have been removed from *info, not just the ones in the first
5019 match.
5020 ****************************************************************************/
5022 bool printer_driver_files_in_use(TALLOC_CTX *mem_ctx,
5023 struct spoolss_DriverInfo8 *info)
5025 int i;
5026 int ndrivers;
5027 uint32 version;
5028 fstring *list = NULL;
5029 struct spoolss_DriverInfo8 *driver;
5030 bool in_use = false;
5032 if ( !info )
5033 return False;
5035 version = info->version;
5037 /* loop over all driver versions */
5039 DEBUG(5,("printer_driver_files_in_use: Beginning search through ntdrivers.tdb...\n"));
5041 /* get the list of drivers */
5043 list = NULL;
5044 ndrivers = get_ntdrivers(&list, info->architecture, version);
5046 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
5047 ndrivers, info->architecture, version));
5049 /* check each driver for overlap in files */
5051 for (i=0; i<ndrivers; i++) {
5052 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
5054 driver = NULL;
5056 if (!W_ERROR_IS_OK(get_a_printer_driver(talloc_tos(), &driver, list[i], info->architecture, version))) {
5057 SAFE_FREE(list);
5058 return True;
5061 /* check if d2 uses any files from d1 */
5062 /* only if this is a different driver than the one being deleted */
5064 if (!strequal(info->driver_name, driver->driver_name)) {
5065 if (trim_overlap_drv_files(mem_ctx, info, driver)) {
5066 /* mz: Do not instantly return -
5067 * we need to ensure this file isn't
5068 * also in use by other drivers. */
5069 in_use = true;
5073 free_a_printer_driver(driver);
5076 SAFE_FREE(list);
5078 DEBUG(5,("printer_driver_files_in_use: Completed search through ntdrivers.tdb...\n"));
5080 return in_use;
5083 static NTSTATUS driver_unlink_internals(connection_struct *conn,
5084 const char *name)
5086 struct smb_filename *smb_fname = NULL;
5087 NTSTATUS status;
5089 status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
5090 &smb_fname);
5091 if (!NT_STATUS_IS_OK(status)) {
5092 return status;
5095 status = unlink_internals(conn, NULL, 0, smb_fname, false);
5097 TALLOC_FREE(smb_fname);
5098 return status;
5101 /****************************************************************************
5102 Actually delete the driver files. Make sure that
5103 printer_driver_files_in_use() return False before calling
5104 this.
5105 ****************************************************************************/
5107 static bool delete_driver_files(struct pipes_struct *rpc_pipe,
5108 const struct spoolss_DriverInfo8 *r)
5110 int i = 0;
5111 char *s;
5112 const char *file;
5113 connection_struct *conn;
5114 NTSTATUS nt_status;
5115 char *oldcwd;
5116 fstring printdollar;
5117 int printdollar_snum;
5118 bool ret = false;
5120 if (!r) {
5121 return false;
5124 DEBUG(6,("delete_driver_files: deleting driver [%s] - version [%d]\n",
5125 r->driver_name, r->version));
5127 fstrcpy(printdollar, "print$");
5129 printdollar_snum = find_service(printdollar);
5130 if (printdollar_snum == -1) {
5131 return false;
5134 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
5135 lp_pathname(printdollar_snum),
5136 rpc_pipe->server_info, &oldcwd);
5137 if (!NT_STATUS_IS_OK(nt_status)) {
5138 DEBUG(0,("delete_driver_files: create_conn_struct "
5139 "returned %s\n", nt_errstr(nt_status)));
5140 return false;
5143 if ( !CAN_WRITE(conn) ) {
5144 DEBUG(3,("delete_driver_files: Cannot delete print driver when [print$] is read-only\n"));
5145 goto fail;
5148 /* now delete the files; must strip the '\print$' string from
5149 fron of path */
5151 if (r->driver_path && r->driver_path[0]) {
5152 if ((s = strchr(&r->driver_path[1], '\\')) != NULL) {
5153 file = s;
5154 DEBUG(10,("deleting driverfile [%s]\n", s));
5155 driver_unlink_internals(conn, file);
5159 if (r->config_file && r->config_file[0]) {
5160 if ((s = strchr(&r->config_file[1], '\\')) != NULL) {
5161 file = s;
5162 DEBUG(10,("deleting configfile [%s]\n", s));
5163 driver_unlink_internals(conn, file);
5167 if (r->data_file && r->data_file[0]) {
5168 if ((s = strchr(&r->data_file[1], '\\')) != NULL) {
5169 file = s;
5170 DEBUG(10,("deleting datafile [%s]\n", s));
5171 driver_unlink_internals(conn, file);
5175 if (r->help_file && r->help_file[0]) {
5176 if ((s = strchr(&r->help_file[1], '\\')) != NULL) {
5177 file = s;
5178 DEBUG(10,("deleting helpfile [%s]\n", s));
5179 driver_unlink_internals(conn, file);
5183 /* check if we are done removing files */
5185 if (r->dependent_files) {
5186 while (r->dependent_files[i] && r->dependent_files[i][0]) {
5187 char *p;
5189 /* bypass the "\print$" portion of the path */
5191 if ((p = strchr(r->dependent_files[i]+1, '\\')) != NULL) {
5192 file = p;
5193 DEBUG(10,("deleting dependent file [%s]\n", file));
5194 driver_unlink_internals(conn, file);
5197 i++;
5201 goto done;
5202 fail:
5203 ret = false;
5204 done:
5205 if (conn != NULL) {
5206 vfs_ChDir(conn, oldcwd);
5207 conn_free(conn);
5209 return ret;
5212 /****************************************************************************
5213 Remove a printer driver from the TDB. This assumes that the the driver was
5214 previously looked up.
5215 ***************************************************************************/
5217 WERROR delete_printer_driver(struct pipes_struct *rpc_pipe,
5218 const struct spoolss_DriverInfo8 *r,
5219 uint32 version, bool delete_files )
5221 char *key = NULL;
5222 const char *arch;
5223 TDB_DATA dbuf;
5225 /* delete the tdb data first */
5227 arch = get_short_archi(r->architecture);
5228 if (!arch) {
5229 return WERR_UNKNOWN_PRINTER_DRIVER;
5231 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
5232 arch, version, r->driver_name) < 0) {
5233 return WERR_NOMEM;
5236 DEBUG(5,("delete_printer_driver: key = [%s] delete_files = %s\n",
5237 key, delete_files ? "TRUE" : "FALSE" ));
5239 /* check if the driver actually exists for this environment */
5241 dbuf = tdb_fetch_bystring( tdb_drivers, key );
5242 if ( !dbuf.dptr ) {
5243 DEBUG(8,("delete_printer_driver: Driver unknown [%s]\n", key));
5244 SAFE_FREE(key);
5245 return WERR_UNKNOWN_PRINTER_DRIVER;
5248 SAFE_FREE( dbuf.dptr );
5250 /* ok... the driver exists so the delete should return success */
5252 if (tdb_delete_bystring(tdb_drivers, key) == -1) {
5253 DEBUG (0,("delete_printer_driver: fail to delete %s!\n", key));
5254 SAFE_FREE(key);
5255 return WERR_ACCESS_DENIED;
5259 * now delete any associated files if delete_files == True
5260 * even if this part failes, we return succes because the
5261 * driver doesn not exist any more
5264 if ( delete_files )
5265 delete_driver_files(rpc_pipe, r);
5267 DEBUG(5,("delete_printer_driver: driver delete successful [%s]\n", key));
5268 SAFE_FREE(key);
5270 return WERR_OK;
5273 /****************************************************************************
5274 Store a security desc for a printer.
5275 ****************************************************************************/
5277 WERROR nt_printing_setsec(const char *sharename, SEC_DESC_BUF *secdesc_ctr)
5279 SEC_DESC_BUF *new_secdesc_ctr = NULL;
5280 SEC_DESC_BUF *old_secdesc_ctr = NULL;
5281 TALLOC_CTX *mem_ctx = NULL;
5282 TDB_DATA kbuf;
5283 TDB_DATA dbuf;
5284 DATA_BLOB blob;
5285 WERROR status;
5286 NTSTATUS nt_status;
5288 mem_ctx = talloc_init("nt_printing_setsec");
5289 if (mem_ctx == NULL)
5290 return WERR_NOMEM;
5292 /* The old owner and group sids of the security descriptor are not
5293 present when new ACEs are added or removed by changing printer
5294 permissions through NT. If they are NULL in the new security
5295 descriptor then copy them over from the old one. */
5297 if (!secdesc_ctr->sd->owner_sid || !secdesc_ctr->sd->group_sid) {
5298 DOM_SID *owner_sid, *group_sid;
5299 SEC_ACL *dacl, *sacl;
5300 SEC_DESC *psd = NULL;
5301 size_t size;
5303 if (!nt_printing_getsec(mem_ctx, sharename, &old_secdesc_ctr)) {
5304 status = WERR_NOMEM;
5305 goto out;
5308 /* Pick out correct owner and group sids */
5310 owner_sid = secdesc_ctr->sd->owner_sid ?
5311 secdesc_ctr->sd->owner_sid :
5312 old_secdesc_ctr->sd->owner_sid;
5314 group_sid = secdesc_ctr->sd->group_sid ?
5315 secdesc_ctr->sd->group_sid :
5316 old_secdesc_ctr->sd->group_sid;
5318 dacl = secdesc_ctr->sd->dacl ?
5319 secdesc_ctr->sd->dacl :
5320 old_secdesc_ctr->sd->dacl;
5322 sacl = secdesc_ctr->sd->sacl ?
5323 secdesc_ctr->sd->sacl :
5324 old_secdesc_ctr->sd->sacl;
5326 /* Make a deep copy of the security descriptor */
5328 psd = make_sec_desc(mem_ctx, secdesc_ctr->sd->revision, secdesc_ctr->sd->type,
5329 owner_sid, group_sid,
5330 sacl,
5331 dacl,
5332 &size);
5334 if (!psd) {
5335 status = WERR_NOMEM;
5336 goto out;
5339 new_secdesc_ctr = make_sec_desc_buf(mem_ctx, size, psd);
5342 if (!new_secdesc_ctr) {
5343 new_secdesc_ctr = secdesc_ctr;
5346 /* Store the security descriptor in a tdb */
5348 nt_status = marshall_sec_desc_buf(mem_ctx, new_secdesc_ctr,
5349 &blob.data, &blob.length);
5350 if (!NT_STATUS_IS_OK(nt_status)) {
5351 status = ntstatus_to_werror(nt_status);
5352 goto out;
5355 kbuf = make_printers_secdesc_tdbkey(mem_ctx, sharename );
5357 dbuf.dptr = (unsigned char *)blob.data;
5358 dbuf.dsize = blob.length;
5360 if (tdb_trans_store(tdb_printers, kbuf, dbuf, TDB_REPLACE)==0) {
5361 status = WERR_OK;
5362 } else {
5363 DEBUG(1,("Failed to store secdesc for %s\n", sharename));
5364 status = WERR_BADFUNC;
5367 /* Free malloc'ed memory */
5368 talloc_free(blob.data);
5370 out:
5372 if (mem_ctx)
5373 talloc_destroy(mem_ctx);
5374 return status;
5377 /****************************************************************************
5378 Construct a default security descriptor buffer for a printer.
5379 ****************************************************************************/
5381 static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
5383 SEC_ACE ace[5]; /* max number of ace entries */
5384 int i = 0;
5385 uint32_t sa;
5386 SEC_ACL *psa = NULL;
5387 SEC_DESC_BUF *sdb = NULL;
5388 SEC_DESC *psd = NULL;
5389 DOM_SID adm_sid;
5390 size_t sd_size;
5392 /* Create an ACE where Everyone is allowed to print */
5394 sa = PRINTER_ACE_PRINT;
5395 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
5396 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5398 /* Add the domain admins group if we are a DC */
5400 if ( IS_DC ) {
5401 DOM_SID domadmins_sid;
5403 sid_copy(&domadmins_sid, get_global_sam_sid());
5404 sid_append_rid(&domadmins_sid, DOMAIN_GROUP_RID_ADMINS);
5406 sa = PRINTER_ACE_FULL_CONTROL;
5407 init_sec_ace(&ace[i++], &domadmins_sid,
5408 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5409 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5410 init_sec_ace(&ace[i++], &domadmins_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5411 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5413 else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
5414 sid_append_rid(&adm_sid, DOMAIN_USER_RID_ADMIN);
5416 sa = PRINTER_ACE_FULL_CONTROL;
5417 init_sec_ace(&ace[i++], &adm_sid,
5418 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5419 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5420 init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5421 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5424 /* add BUILTIN\Administrators as FULL CONTROL */
5426 sa = PRINTER_ACE_FULL_CONTROL;
5427 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5428 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5429 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5430 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5431 SEC_ACE_TYPE_ACCESS_ALLOWED,
5432 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5434 /* Make the security descriptor owned by the BUILTIN\Administrators */
5436 /* The ACL revision number in rpc_secdesc.h differs from the one
5437 created by NT when setting ACE entries in printer
5438 descriptors. NT4 complains about the property being edited by a
5439 NT5 machine. */
5441 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) != NULL) {
5442 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
5443 &global_sid_Builtin_Administrators,
5444 &global_sid_Builtin_Administrators,
5445 NULL, psa, &sd_size);
5448 if (!psd) {
5449 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
5450 return NULL;
5453 sdb = make_sec_desc_buf(ctx, sd_size, psd);
5455 DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
5456 (unsigned int)sd_size));
5458 return sdb;
5461 /****************************************************************************
5462 Get a security desc for a printer.
5463 ****************************************************************************/
5465 bool nt_printing_getsec(TALLOC_CTX *ctx, const char *sharename, SEC_DESC_BUF **secdesc_ctr)
5467 TDB_DATA kbuf;
5468 TDB_DATA dbuf;
5469 DATA_BLOB blob;
5470 char *temp;
5471 NTSTATUS status;
5473 if (strlen(sharename) > 2 && (temp = strchr(sharename + 2, '\\'))) {
5474 sharename = temp + 1;
5477 /* Fetch security descriptor from tdb */
5479 kbuf = make_printers_secdesc_tdbkey(ctx, sharename);
5481 dbuf = tdb_fetch(tdb_printers, kbuf);
5482 if (dbuf.dptr) {
5484 status = unmarshall_sec_desc_buf(ctx, dbuf.dptr, dbuf.dsize,
5485 secdesc_ctr);
5486 SAFE_FREE(dbuf.dptr);
5488 if (NT_STATUS_IS_OK(status)) {
5489 return true;
5493 *secdesc_ctr = construct_default_printer_sdb(ctx);
5494 if (!*secdesc_ctr) {
5495 return false;
5498 status = marshall_sec_desc_buf(ctx, *secdesc_ctr,
5499 &blob.data, &blob.length);
5500 if (NT_STATUS_IS_OK(status)) {
5501 dbuf.dptr = (unsigned char *)blob.data;
5502 dbuf.dsize = blob.length;
5503 tdb_trans_store(tdb_printers, kbuf, dbuf, TDB_REPLACE);
5504 talloc_free(blob.data);
5507 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
5508 this security descriptor has been created when winbindd was
5509 down. Take ownership of security descriptor. */
5511 if (sid_equal((*secdesc_ctr)->sd->owner_sid, &global_sid_World)) {
5512 DOM_SID owner_sid;
5514 /* Change sd owner to workgroup administrator */
5516 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
5517 SEC_DESC_BUF *new_secdesc_ctr = NULL;
5518 SEC_DESC *psd = NULL;
5519 size_t size;
5521 /* Create new sd */
5523 sid_append_rid(&owner_sid, DOMAIN_USER_RID_ADMIN);
5525 psd = make_sec_desc(ctx, (*secdesc_ctr)->sd->revision, (*secdesc_ctr)->sd->type,
5526 &owner_sid,
5527 (*secdesc_ctr)->sd->group_sid,
5528 (*secdesc_ctr)->sd->sacl,
5529 (*secdesc_ctr)->sd->dacl,
5530 &size);
5532 if (!psd) {
5533 return False;
5536 new_secdesc_ctr = make_sec_desc_buf(ctx, size, psd);
5537 if (!new_secdesc_ctr) {
5538 return False;
5541 /* Swap with other one */
5543 *secdesc_ctr = new_secdesc_ctr;
5545 /* Set it */
5547 nt_printing_setsec(sharename, *secdesc_ctr);
5551 if (DEBUGLEVEL >= 10) {
5552 SEC_ACL *the_acl = (*secdesc_ctr)->sd->dacl;
5553 int i;
5555 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5556 sharename, the_acl->num_aces));
5558 for (i = 0; i < the_acl->num_aces; i++) {
5559 DEBUG(10, ("%s %d %d 0x%08x\n",
5560 sid_string_dbg(&the_acl->aces[i].trustee),
5561 the_acl->aces[i].type, the_acl->aces[i].flags,
5562 the_acl->aces[i].access_mask));
5566 return True;
5569 /* error code:
5570 0: everything OK
5571 1: level not implemented
5572 2: file doesn't exist
5573 3: can't allocate memory
5574 4: can't free memory
5575 5: non existant struct
5579 A printer and a printer driver are 2 different things.
5580 NT manages them separatelly, Samba does the same.
5581 Why ? Simply because it's easier and it makes sense !
5583 Now explanation: You have 3 printers behind your samba server,
5584 2 of them are the same make and model (laser A and B). But laser B
5585 has an 3000 sheet feeder and laser A doesn't such an option.
5586 Your third printer is an old dot-matrix model for the accounting :-).
5588 If the /usr/local/samba/lib directory (default dir), you will have
5589 5 files to describe all of this.
5591 3 files for the printers (1 by printer):
5592 NTprinter_laser A
5593 NTprinter_laser B
5594 NTprinter_accounting
5595 2 files for the drivers (1 for the laser and 1 for the dot matrix)
5596 NTdriver_printer model X
5597 NTdriver_printer model Y
5599 jfm: I should use this comment for the text file to explain
5600 same thing for the forms BTW.
5601 Je devrais mettre mes commentaires en francais, ca serait mieux :-)
5605 /* Convert generic access rights to printer object specific access rights.
5606 It turns out that NT4 security descriptors use generic access rights and
5607 NT5 the object specific ones. */
5609 void map_printer_permissions(SEC_DESC *sd)
5611 int i;
5613 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5614 se_map_generic(&sd->dacl->aces[i].access_mask,
5615 &printer_generic_mapping);
5619 void map_job_permissions(SEC_DESC *sd)
5621 int i;
5623 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5624 se_map_generic(&sd->dacl->aces[i].access_mask,
5625 &job_generic_mapping);
5630 /****************************************************************************
5631 Check a user has permissions to perform the given operation. We use the
5632 permission constants defined in include/rpc_spoolss.h to check the various
5633 actions we perform when checking printer access.
5635 PRINTER_ACCESS_ADMINISTER:
5636 print_queue_pause, print_queue_resume, update_printer_sec,
5637 update_printer, spoolss_addprinterex_level_2,
5638 _spoolss_setprinterdata
5640 PRINTER_ACCESS_USE:
5641 print_job_start
5643 JOB_ACCESS_ADMINISTER:
5644 print_job_delete, print_job_pause, print_job_resume,
5645 print_queue_purge
5647 Try access control in the following order (for performance reasons):
5648 1) root and SE_PRINT_OPERATOR can do anything (easy check)
5649 2) check security descriptor (bit comparisons in memory)
5650 3) "printer admins" (may result in numerous calls to winbind)
5652 ****************************************************************************/
5653 bool print_access_check(struct auth_serversupplied_info *server_info, int snum,
5654 int access_type)
5656 SEC_DESC_BUF *secdesc = NULL;
5657 uint32 access_granted;
5658 NTSTATUS status;
5659 const char *pname;
5660 TALLOC_CTX *mem_ctx = NULL;
5661 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5663 /* If user is NULL then use the current_user structure */
5665 /* Always allow root or SE_PRINT_OPERATROR to do anything */
5667 if (server_info->utok.uid == sec_initial_uid()
5668 || user_has_privileges(server_info->ptok, &se_printop ) ) {
5669 return True;
5672 /* Get printer name */
5674 pname = PRINTERNAME(snum);
5676 if (!pname || !*pname) {
5677 errno = EACCES;
5678 return False;
5681 /* Get printer security descriptor */
5683 if(!(mem_ctx = talloc_init("print_access_check"))) {
5684 errno = ENOMEM;
5685 return False;
5688 if (!nt_printing_getsec(mem_ctx, pname, &secdesc)) {
5689 talloc_destroy(mem_ctx);
5690 errno = ENOMEM;
5691 return False;
5694 if (access_type == JOB_ACCESS_ADMINISTER) {
5695 SEC_DESC_BUF *parent_secdesc = secdesc;
5697 /* Create a child security descriptor to check permissions
5698 against. This is because print jobs are child objects
5699 objects of a printer. */
5701 status = se_create_child_secdesc_buf(mem_ctx, &secdesc, parent_secdesc->sd, False);
5703 if (!NT_STATUS_IS_OK(status)) {
5704 talloc_destroy(mem_ctx);
5705 errno = map_errno_from_nt_status(status);
5706 return False;
5709 map_job_permissions(secdesc->sd);
5710 } else {
5711 map_printer_permissions(secdesc->sd);
5714 /* Check access */
5715 status = se_access_check(secdesc->sd, server_info->ptok, access_type,
5716 &access_granted);
5718 DEBUG(4, ("access check was %s\n", NT_STATUS_IS_OK(status) ? "SUCCESS" : "FAILURE"));
5720 /* see if we need to try the printer admin list */
5722 if (!NT_STATUS_IS_OK(status) &&
5723 (token_contains_name_in_list(uidtoname(server_info->utok.uid),
5724 NULL, NULL, server_info->ptok,
5725 lp_printer_admin(snum)))) {
5726 talloc_destroy(mem_ctx);
5727 return True;
5730 talloc_destroy(mem_ctx);
5732 if (!NT_STATUS_IS_OK(status)) {
5733 errno = EACCES;
5736 return NT_STATUS_IS_OK(status);
5739 /****************************************************************************
5740 Check the time parameters allow a print operation.
5741 *****************************************************************************/
5743 bool print_time_access_check(const char *servicename)
5745 NT_PRINTER_INFO_LEVEL *printer = NULL;
5746 bool ok = False;
5747 time_t now = time(NULL);
5748 struct tm *t;
5749 uint32 mins;
5751 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
5752 return False;
5754 if (printer->info_2->starttime == 0 && printer->info_2->untiltime == 0)
5755 ok = True;
5757 t = gmtime(&now);
5758 mins = (uint32)t->tm_hour*60 + (uint32)t->tm_min;
5760 if (mins >= printer->info_2->starttime && mins <= printer->info_2->untiltime)
5761 ok = True;
5763 free_a_printer(&printer, 2);
5765 if (!ok)
5766 errno = EACCES;
5768 return ok;
5771 /****************************************************************************
5772 Fill in the servername sent in the _spoolss_open_printer_ex() call
5773 ****************************************************************************/
5775 char* get_server_name( Printer_entry *printer )
5777 return printer->servername;