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/>.
23 #include "librpc/gen_ndr/messaging.h"
24 #include "printing/pcap.h"
27 static TDB_CONTEXT *tdb_forms; /* used for forms files */
28 static TDB_CONTEXT *tdb_drivers; /* used for driver files */
29 static TDB_CONTEXT *tdb_printers; /* used for printers files */
31 #define FORMS_PREFIX "FORMS/"
32 #define DRIVERS_PREFIX "DRIVERS/"
33 #define DRIVER_INIT_PREFIX "DRIVER_INIT/"
34 #define PRINTERS_PREFIX "PRINTERS/"
35 #define SECDESC_PREFIX "SECDESC/"
36 #define GLOBAL_C_SETPRINTER "GLOBALS/c_setprinter"
38 #define NTDRIVERS_DATABASE_VERSION_1 1
39 #define NTDRIVERS_DATABASE_VERSION_2 2
40 #define NTDRIVERS_DATABASE_VERSION_3 3 /* little endian version of v2 */
41 #define NTDRIVERS_DATABASE_VERSION_4 4 /* fix generic bits in security descriptors */
42 #define NTDRIVERS_DATABASE_VERSION_5 5 /* normalize keys in ntprinters.tdb */
44 /* Map generic permissions to printer object specific permissions */
46 const struct generic_mapping printer_generic_mapping = {
53 const struct standard_mapping printer_std_mapping = {
60 /* Map generic permissions to print server object specific permissions */
62 const struct generic_mapping printserver_generic_mapping = {
69 const struct generic_mapping printserver_std_mapping = {
76 /* Map generic permissions to job object specific permissions */
78 const struct generic_mapping job_generic_mapping = {
85 /* We need one default form to support our default printer. Msoft adds the
86 forms it wants and in the ORDER it wants them (note: DEVMODE papersize is an
87 array index). Letter is always first, so (for the current code) additions
88 always put things in the correct order. */
89 static const nt_forms_struct default_forms[] = {
90 {"Letter",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
91 {"Letter Small",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
92 {"Tabloid",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
93 {"Ledger",0x1,0x696b8,0x44368,0x0,0x0,0x696b8,0x44368},
94 {"Legal",0x1,0x34b5c,0x56d10,0x0,0x0,0x34b5c,0x56d10},
95 {"Statement",0x1,0x221b4,0x34b5c,0x0,0x0,0x221b4,0x34b5c},
96 {"Executive",0x1,0x2cf56,0x411cc,0x0,0x0,0x2cf56,0x411cc},
97 {"A3",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
98 {"A4",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
99 {"A4 Small",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
100 {"A5",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
101 {"B4 (JIS)",0x1,0x3ebe8,0x58de0,0x0,0x0,0x3ebe8,0x58de0},
102 {"B5 (JIS)",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
103 {"Folio",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
104 {"Quarto",0x1,0x347d8,0x43238,0x0,0x0,0x347d8,0x43238},
105 {"10x14",0x1,0x3e030,0x56d10,0x0,0x0,0x3e030,0x56d10},
106 {"11x17",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
107 {"Note",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
108 {"Envelope #9",0x1,0x18079,0x37091,0x0,0x0,0x18079,0x37091},
109 {"Envelope #10",0x1,0x19947,0x3ae94,0x0,0x0,0x19947,0x3ae94},
110 {"Envelope #11",0x1,0x1be7c,0x40565,0x0,0x0,0x1be7c,0x40565},
111 {"Envelope #12",0x1,0x1d74a,0x44368,0x0,0x0,0x1d74a,0x44368},
112 {"Envelope #14",0x1,0x1f018,0x47504,0x0,0x0,0x1f018,0x47504},
113 {"C size sheet",0x1,0x696b8,0x886d0,0x0,0x0,0x696b8,0x886d0},
114 {"D size sheet",0x1,0x886d0,0xd2d70,0x0,0x0,0x886d0,0xd2d70},
115 {"E size sheet",0x1,0xd2d70,0x110da0,0x0,0x0,0xd2d70,0x110da0},
116 {"Envelope DL",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
117 {"Envelope C5",0x1,0x278d0,0x37e88,0x0,0x0,0x278d0,0x37e88},
118 {"Envelope C3",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
119 {"Envelope C4",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
120 {"Envelope C6",0x1,0x1bd50,0x278d0,0x0,0x0,0x1bd50,0x278d0},
121 {"Envelope C65",0x1,0x1bd50,0x37e88,0x0,0x0,0x1bd50,0x37e88},
122 {"Envelope B4",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
123 {"Envelope B5",0x1,0x2af80,0x3d090,0x0,0x0,0x2af80,0x3d090},
124 {"Envelope B6",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
125 {"Envelope",0x1,0x1adb0,0x38270,0x0,0x0,0x1adb0,0x38270},
126 {"Envelope Monarch",0x1,0x18079,0x2e824,0x0,0x0,0x18079,0x2e824},
127 {"6 3/4 Envelope",0x1,0x167ab,0x284ec,0x0,0x0,0x167ab,0x284ec},
128 {"US Std Fanfold",0x1,0x5c3e1,0x44368,0x0,0x0,0x5c3e1,0x44368},
129 {"German Std Fanfold",0x1,0x34b5c,0x4a6a0,0x0,0x0,0x34b5c,0x4a6a0},
130 {"German Legal Fanfold",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
131 {"B4 (ISO)",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
132 {"Japanese Postcard",0x1,0x186a0,0x24220,0x0,0x0,0x186a0,0x24220},
133 {"9x11",0x1,0x37cf8,0x44368,0x0,0x0,0x37cf8,0x44368},
134 {"10x11",0x1,0x3e030,0x44368,0x0,0x0,0x3e030,0x44368},
135 {"15x11",0x1,0x5d048,0x44368,0x0,0x0,0x5d048,0x44368},
136 {"Envelope Invite",0x1,0x35b60,0x35b60,0x0,0x0,0x35b60,0x35b60},
137 {"Reserved48",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
138 {"Reserved49",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
139 {"Letter Extra",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
140 {"Legal Extra",0x1,0x3ae94,0x5d048,0x0,0x0,0x3ae94,0x5d048},
141 {"Tabloid Extra",0x1,0x4a6a0,0x6f9f0,0x0,0x0,0x4a6a0,0x6f9f0},
142 {"A4 Extra",0x1,0x397c2,0x4eb16,0x0,0x0,0x397c2,0x4eb16},
143 {"Letter Transverse",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
144 {"A4 Transverse",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
145 {"Letter Extra Transverse",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
146 {"Super A",0x1,0x376b8,0x56ea0,0x0,0x0,0x376b8,0x56ea0},
147 {"Super B",0x1,0x4a768,0x76e58,0x0,0x0,0x4a768,0x76e58},
148 {"Letter Plus",0x1,0x34b5c,0x4eb16,0x0,0x0,0x34b5c,0x4eb16},
149 {"A4 Plus",0x1,0x33450,0x50910,0x0,0x0,0x33450,0x50910},
150 {"A5 Transverse",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
151 {"B5 (JIS) Transverse",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
152 {"A3 Extra",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
153 {"A5 Extra",0x1,0x2a7b0,0x395f8,0x0,0x0,0x2a7b0,0x395f8},
154 {"B5 (ISO) Extra",0x1,0x31128,0x43620,0x0,0x0,0x31128,0x43620},
155 {"A2",0x1,0x668a0,0x91050,0x0,0x0,0x668a0,0x91050},
156 {"A3 Transverse",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
157 {"A3 Extra Transverse",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
158 {"Japanese Double Postcard",0x1,0x30d40,0x24220,0x0,0x0,0x30d40,0x24220},
159 {"A6",0x1,0x19a28,0x24220,0x0,0x0,0x19a28,0x24220},
160 {"Japanese Envelope Kaku #2",0x1,0x3a980,0x510e0,0x0,0x0,0x3a980,0x510e0},
161 {"Japanese Envelope Kaku #3",0x1,0x34bc0,0x43a08,0x0,0x0,0x34bc0,0x43a08},
162 {"Japanese Envelope Chou #3",0x1,0x1d4c0,0x395f8,0x0,0x0,0x1d4c0,0x395f8},
163 {"Japanese Envelope Chou #4",0x1,0x15f90,0x320c8,0x0,0x0,0x15f90,0x320c8},
164 {"Letter Rotated",0x1,0x44368,0x34b5c,0x0,0x0,0x44368,0x34b5c},
165 {"A3 Rotated",0x1,0x668a0,0x48828,0x0,0x0,0x668a0,0x48828},
166 {"A4 Rotated",0x1,0x48828,0x33450,0x0,0x0,0x48828,0x33450},
167 {"A5 Rotated",0x1,0x33450,0x24220,0x0,0x0,0x33450,0x24220},
168 {"B4 (JIS) Rotated",0x1,0x58de0,0x3ebe8,0x0,0x0,0x58de0,0x3ebe8},
169 {"B5 (JIS) Rotated",0x1,0x3ebe8,0x2c6f0,0x0,0x0,0x3ebe8,0x2c6f0},
170 {"Japanese Postcard Rotated",0x1,0x24220,0x186a0,0x0,0x0,0x24220,0x186a0},
171 {"Double Japan Postcard Rotated",0x1,0x24220,0x30d40,0x0,0x0,0x24220,0x30d40},
172 {"A6 Rotated",0x1,0x24220,0x19a28,0x0,0x0,0x24220,0x19a28},
173 {"Japan Envelope Kaku #2 Rotated",0x1,0x510e0,0x3a980,0x0,0x0,0x510e0,0x3a980},
174 {"Japan Envelope Kaku #3 Rotated",0x1,0x43a08,0x34bc0,0x0,0x0,0x43a08, 0x34bc0},
175 {"Japan Envelope Chou #3 Rotated",0x1,0x395f8,0x1d4c0,0x0,0x0,0x395f8,0x1d4c0},
176 {"Japan Envelope Chou #4 Rotated",0x1,0x320c8,0x15f90,0x0,0x0,0x320c8,0x15f90},
177 {"B6 (JIS)",0x1,0x1f400,0x2c6f0,0x0,0x0,0x1f400,0x2c6f0},
178 {"B6 (JIS) Rotated",0x1,0x2c6f0,0x1f400,0x0,0x0,0x2c6f0,0x1f400},
179 {"12x11",0x1,0x4a724,0x443e1,0x0,0x0,0x4a724,0x443e1},
180 {"Japan Envelope You #4",0x1,0x19a28,0x395f8,0x0,0x0,0x19a28,0x395f8},
181 {"Japan Envelope You #4 Rotated",0x1,0x395f8,0x19a28,0x0,0x0,0x395f8,0x19a28},
182 {"PRC 16K",0x1,0x2de60,0x3f7a0,0x0,0x0,0x2de60,0x3f7a0},
183 {"PRC 32K",0x1,0x1fbd0,0x2cec0,0x0,0x0,0x1fbd0,0x2cec0},
184 {"PRC 32K(Big)",0x1,0x222e0,0x318f8,0x0,0x0,0x222e0,0x318f8},
185 {"PRC Envelope #1",0x1,0x18e70,0x28488,0x0,0x0,0x18e70,0x28488},
186 {"PRC Envelope #2",0x1,0x18e70,0x2af80,0x0,0x0,0x18e70,0x2af80},
187 {"PRC Envelope #3",0x1,0x1e848,0x2af80,0x0,0x0,0x1e848,0x2af80},
188 {"PRC Envelope #4",0x1,0x1adb0,0x32c80,0x0,0x0,0x1adb0,0x32c80},
189 {"PRC Envelope #5",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
190 {"PRC Envelope #6",0x1,0x1d4c0,0x38270,0x0,0x0,0x1d4c0,0x38270},
191 {"PRC Envelope #7",0x1,0x27100,0x38270,0x0,0x0,0x27100,0x38270},
192 {"PRC Envelope #8",0x1,0x1d4c0,0x4b708,0x0,0x0,0x1d4c0,0x4b708},
193 {"PRC Envelope #9",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
194 {"PRC Envelope #10",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
195 {"PRC 16K Rotated",0x1,0x3f7a0,0x2de60,0x0,0x0,0x3f7a0,0x2de60},
196 {"PRC 32K Rotated",0x1,0x2cec0,0x1fbd0,0x0,0x0,0x2cec0,0x1fbd0},
197 {"PRC 32K(Big) Rotated",0x1,0x318f8,0x222e0,0x0,0x0,0x318f8,0x222e0},
198 {"PRC Envelope #1 Rotated",0x1,0x28488,0x18e70,0x0,0x0,0x28488,0x18e70},
199 {"PRC Envelope #2 Rotated",0x1,0x2af80,0x18e70,0x0,0x0,0x2af80,0x18e70},
200 {"PRC Envelope #3 Rotated",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
201 {"PRC Envelope #4 Rotated",0x1,0x32c80,0x1adb0,0x0,0x0,0x32c80,0x1adb0},
202 {"PRC Envelope #5 Rotated",0x1,0x35b60,0x1adb0,0x0,0x0,0x35b60,0x1adb0},
203 {"PRC Envelope #6 Rotated",0x1,0x38270,0x1d4c0,0x0,0x0,0x38270,0x1d4c0},
204 {"PRC Envelope #7 Rotated",0x1,0x38270,0x27100,0x0,0x0,0x38270,0x27100},
205 {"PRC Envelope #8 Rotated",0x1,0x4b708,0x1d4c0,0x0,0x0,0x4b708,0x1d4c0},
206 {"PRC Envelope #9 Rotated",0x1,0x4f1a0,0x37e88,0x0,0x0,0x4f1a0,0x37e88},
207 {"PRC Envelope #10 Rotated",0x1,0x6fd10,0x4f1a0,0x0,0x0,0x6fd10,0x4f1a0}
210 static const struct print_architecture_table_node archi_table[]= {
212 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
213 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
214 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
215 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
216 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
217 {"Windows IA64", SPL_ARCH_IA64, 3 },
218 {"Windows x64", SPL_ARCH_X64, 3 },
223 /****************************************************************************
224 generate a new TDB_DATA key for storing a printer
225 ****************************************************************************/
227 static TDB_DATA make_printer_tdbkey(TALLOC_CTX *ctx, const char *sharename )
233 fstrcpy(share, sharename);
236 keystr = talloc_asprintf(ctx, "%s%s", PRINTERS_PREFIX, share);
237 key = string_term_tdb_data(keystr ? keystr : "");
242 /****************************************************************************
243 generate a new TDB_DATA key for storing a printer security descriptor
244 ****************************************************************************/
246 static TDB_DATA make_printers_secdesc_tdbkey(TALLOC_CTX *ctx,
247 const char* sharename )
253 fstrcpy(share, sharename );
256 keystr = talloc_asprintf(ctx, "%s%s", SECDESC_PREFIX, share);
257 key = string_term_tdb_data(keystr ? keystr : "");
262 /****************************************************************************
263 ****************************************************************************/
265 static bool upgrade_to_version_3(void)
267 TDB_DATA kbuf, newkey, dbuf;
269 DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
271 for (kbuf = tdb_firstkey(tdb_drivers); kbuf.dptr;
272 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
274 dbuf = tdb_fetch(tdb_drivers, kbuf);
276 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
277 DEBUG(0,("upgrade_to_version_3:moving form\n"));
278 if (tdb_store(tdb_forms, kbuf, dbuf, TDB_REPLACE) != 0) {
279 SAFE_FREE(dbuf.dptr);
280 DEBUG(0,("upgrade_to_version_3: failed to move form. Error (%s).\n", tdb_errorstr(tdb_forms)));
283 if (tdb_delete(tdb_drivers, kbuf) != 0) {
284 SAFE_FREE(dbuf.dptr);
285 DEBUG(0,("upgrade_to_version_3: failed to delete form. Error (%s)\n", tdb_errorstr(tdb_drivers)));
290 if (strncmp((const char *)kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
291 DEBUG(0,("upgrade_to_version_3:moving printer\n"));
292 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
293 SAFE_FREE(dbuf.dptr);
294 DEBUG(0,("upgrade_to_version_3: failed to move printer. Error (%s)\n", tdb_errorstr(tdb_printers)));
297 if (tdb_delete(tdb_drivers, kbuf) != 0) {
298 SAFE_FREE(dbuf.dptr);
299 DEBUG(0,("upgrade_to_version_3: failed to delete printer. Error (%s)\n", tdb_errorstr(tdb_drivers)));
304 if (strncmp((const char *)kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
305 DEBUG(0,("upgrade_to_version_3:moving secdesc\n"));
306 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
307 SAFE_FREE(dbuf.dptr);
308 DEBUG(0,("upgrade_to_version_3: failed to move secdesc. Error (%s)\n", tdb_errorstr(tdb_printers)));
311 if (tdb_delete(tdb_drivers, kbuf) != 0) {
312 SAFE_FREE(dbuf.dptr);
313 DEBUG(0,("upgrade_to_version_3: failed to delete secdesc. Error (%s)\n", tdb_errorstr(tdb_drivers)));
318 SAFE_FREE(dbuf.dptr);
324 /*******************************************************************
325 Fix an issue with security descriptors. Printer sec_desc must
326 use more than the generic bits that were previously used
327 in <= 3.0.14a. They must also have a owner and group SID assigned.
328 Otherwise, any printers than have been migrated to a Windows
329 host using printmig.exe will not be accessible.
330 *******************************************************************/
332 static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
333 TDB_DATA data, void *state )
336 struct sec_desc_buf *sd_orig = NULL;
337 struct sec_desc_buf *sd_new, *sd_store;
338 struct security_descriptor *sec, *new_sec;
339 TALLOC_CTX *ctx = state;
344 if (!data.dptr || data.dsize == 0) {
348 if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) != 0 ) {
352 /* upgrade the security descriptor */
354 status = unmarshall_sec_desc_buf(ctx, data.dptr, data.dsize, &sd_orig);
355 if (!NT_STATUS_IS_OK(status)) {
356 /* delete bad entries */
357 DEBUG(0,("sec_desc_upg_fn: Failed to parse original sec_desc for %si. Deleting....\n",
358 (const char *)key.dptr ));
359 tdb_delete( tdb_printers, key );
368 /* is this even valid? */
374 /* update access masks */
376 for ( i=0; i<sec->dacl->num_aces; i++ ) {
377 switch ( sec->dacl->aces[i].access_mask ) {
378 case (GENERIC_READ_ACCESS | GENERIC_WRITE_ACCESS | GENERIC_EXECUTE_ACCESS):
379 sec->dacl->aces[i].access_mask = PRINTER_ACE_PRINT;
382 case GENERIC_ALL_ACCESS:
383 sec->dacl->aces[i].access_mask = PRINTER_ACE_FULL_CONTROL;
386 case READ_CONTROL_ACCESS:
387 sec->dacl->aces[i].access_mask = PRINTER_ACE_MANAGE_DOCUMENTS;
389 default: /* no change */
394 /* create a new struct security_descriptor with the appropriate owner and group SIDs */
396 new_sec = make_sec_desc( ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
397 &global_sid_Builtin_Administrators,
398 &global_sid_Builtin_Administrators,
399 NULL, NULL, &size_new_sec );
403 sd_new = make_sec_desc_buf( ctx, size_new_sec, new_sec );
408 if ( !(sd_store = sec_desc_merge_buf( ctx, sd_new, sd_orig )) ) {
409 DEBUG(0,("sec_desc_upg_fn: Failed to update sec_desc for %s\n", key.dptr ));
415 sd_size = ndr_size_security_descriptor(sd_store->sd, 0)
416 + sizeof(struct sec_desc_buf);
418 status = marshall_sec_desc_buf(ctx, sd_store, &data.dptr, &data.dsize);
419 if (!NT_STATUS_IS_OK(status)) {
420 DEBUG(0,("sec_desc_upg_fn: Failed to parse new sec_desc for %s\n", key.dptr ));
424 result = tdb_store( tdb_printers, key, data, TDB_REPLACE );
426 /* 0 to continue and non-zero to stop traversal */
428 return (result == -1);
431 /*******************************************************************
432 *******************************************************************/
434 static bool upgrade_to_version_4(void)
439 DEBUG(0,("upgrade_to_version_4: upgrading printer security descriptors\n"));
441 if ( !(ctx = talloc_init( "upgrade_to_version_4" )) )
444 result = tdb_traverse( tdb_printers, sec_desc_upg_fn, ctx );
446 talloc_destroy( ctx );
448 return ( result != -1 );
451 /*******************************************************************
452 Fix an issue with security descriptors. Printer sec_desc must
453 use more than the generic bits that were previously used
454 in <= 3.0.14a. They must also have a owner and group SID assigned.
455 Otherwise, any printers than have been migrated to a Windows
456 host using printmig.exe will not be accessible.
457 *******************************************************************/
459 static int normalize_printers_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
460 TDB_DATA data, void *state )
462 TALLOC_CTX *ctx = talloc_tos();
465 if (!data.dptr || data.dsize == 0)
468 /* upgrade printer records and security descriptors */
470 if ( strncmp((const char *) key.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX) ) == 0 ) {
471 new_key = make_printer_tdbkey(ctx, (const char *)key.dptr+strlen(PRINTERS_PREFIX) );
473 else if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) == 0 ) {
474 new_key = make_printers_secdesc_tdbkey(ctx, (const char *)key.dptr+strlen(SECDESC_PREFIX) );
477 /* ignore this record */
481 /* delete the original record and store under the normalized key */
483 if ( tdb_delete( the_tdb, key ) != 0 ) {
484 DEBUG(0,("normalize_printers_fn: tdb_delete for [%s] failed!\n",
489 if ( tdb_store( the_tdb, new_key, data, TDB_REPLACE) != 0 ) {
490 DEBUG(0,("normalize_printers_fn: failed to store new record for [%s]!\n",
498 /*******************************************************************
499 *******************************************************************/
501 static bool upgrade_to_version_5(void)
506 DEBUG(0,("upgrade_to_version_5: normalizing printer keys\n"));
508 if ( !(ctx = talloc_init( "upgrade_to_version_5" )) )
511 result = tdb_traverse( tdb_printers, normalize_printers_fn, NULL );
513 talloc_destroy( ctx );
515 return ( result != -1 );
518 /****************************************************************************
519 Open the NT printing tdbs. Done once before fork().
520 ****************************************************************************/
522 bool nt_printing_init(struct messaging_context *msg_ctx)
524 const char *vstring = "INFO/version";
528 if ( tdb_drivers && tdb_printers && tdb_forms )
532 tdb_close(tdb_drivers);
533 tdb_drivers = tdb_open_log(state_path("ntdrivers.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
535 DEBUG(0,("nt_printing_init: Failed to open nt drivers database %s (%s)\n",
536 state_path("ntdrivers.tdb"), strerror(errno) ));
541 tdb_close(tdb_printers);
542 tdb_printers = tdb_open_log(state_path("ntprinters.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
544 DEBUG(0,("nt_printing_init: Failed to open nt printers database %s (%s)\n",
545 state_path("ntprinters.tdb"), strerror(errno) ));
550 tdb_close(tdb_forms);
551 tdb_forms = tdb_open_log(state_path("ntforms.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
553 DEBUG(0,("nt_printing_init: Failed to open nt forms database %s (%s)\n",
554 state_path("ntforms.tdb"), strerror(errno) ));
558 /* handle a Samba upgrade */
560 vers_id = tdb_fetch_int32(tdb_drivers, vstring);
562 DEBUG(10, ("Fresh database\n"));
563 tdb_store_int32( tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5 );
564 vers_id = NTDRIVERS_DATABASE_VERSION_5;
567 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
569 if ((vers_id == NTDRIVERS_DATABASE_VERSION_1) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_1)) {
570 if (!upgrade_to_version_3())
572 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
573 vers_id = NTDRIVERS_DATABASE_VERSION_3;
576 if ((vers_id == NTDRIVERS_DATABASE_VERSION_2) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_2)) {
577 /* Written on a bigendian machine with old fetch_int code. Save as le. */
578 /* The only upgrade between V2 and V3 is to save the version in little-endian. */
579 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
580 vers_id = NTDRIVERS_DATABASE_VERSION_3;
583 if (vers_id == NTDRIVERS_DATABASE_VERSION_3 ) {
584 if ( !upgrade_to_version_4() )
586 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_4);
587 vers_id = NTDRIVERS_DATABASE_VERSION_4;
590 if (vers_id == NTDRIVERS_DATABASE_VERSION_4 ) {
591 if ( !upgrade_to_version_5() )
593 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5);
594 vers_id = NTDRIVERS_DATABASE_VERSION_5;
598 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
599 DEBUG(0,("nt_printing_init: Unknown printer database version [%d]\n", vers_id));
604 update_c_setprinter(True);
607 * register callback to handle updating printers as new
608 * drivers are installed
611 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
612 do_drv_upgrade_printer);
615 * register callback to handle updating printer data
616 * when a driver is initialized
619 messaging_register(msg_ctx, NULL, MSG_PRINTERDATA_INIT_RESET,
620 reset_all_printerdata);
622 /* of course, none of the message callbacks matter if you don't
623 tell messages.c that you interested in receiving PRINT_GENERAL
624 msgs. This is done in serverid_register() */
627 if ( lp_security() == SEC_ADS ) {
628 win_rc = check_published_printers();
629 if (!W_ERROR_IS_OK(win_rc))
630 DEBUG(0, ("nt_printing_init: error checking published printers: %s\n", win_errstr(win_rc)));
636 /*******************************************************************
637 Function to allow filename parsing "the old way".
638 ********************************************************************/
640 static NTSTATUS driver_unix_convert(connection_struct *conn,
641 const char *old_name,
642 struct smb_filename **smb_fname)
645 TALLOC_CTX *ctx = talloc_tos();
646 char *name = talloc_strdup(ctx, old_name);
649 return NT_STATUS_NO_MEMORY;
652 name = unix_clean_name(ctx, name);
654 return NT_STATUS_NO_MEMORY;
656 trim_string(name,"/","/");
658 status = unix_convert(ctx, conn, name, smb_fname, 0);
659 if (!NT_STATUS_IS_OK(status)) {
660 return NT_STATUS_NO_MEMORY;
666 /*******************************************************************
667 tdb traversal function for counting printers.
668 ********************************************************************/
670 static int traverse_counting_printers(TDB_CONTEXT *t, TDB_DATA key,
671 TDB_DATA data, void *context)
673 int *printer_count = (int*)context;
675 if (memcmp(PRINTERS_PREFIX, key.dptr, sizeof(PRINTERS_PREFIX)-1) == 0) {
677 DEBUG(10,("traverse_counting_printers: printer = [%s] printer_count = %d\n", key.dptr, *printer_count));
683 /*******************************************************************
684 Update the spooler global c_setprinter. This variable is initialized
685 when the parent smbd starts with the number of existing printers. It
686 is monotonically increased by the current number of printers *after*
687 each add or delete printer RPC. Only Microsoft knows why... JRR020119
688 ********************************************************************/
690 uint32 update_c_setprinter(bool initialize)
693 int32 printer_count = 0;
695 tdb_lock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
697 /* Traverse the tdb, counting the printers */
698 tdb_traverse(tdb_printers, traverse_counting_printers, (void *)&printer_count);
700 /* If initializing, set c_setprinter to current printers count
701 * otherwise, bump it by the current printer count
704 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER) + printer_count;
706 c_setprinter = printer_count;
708 DEBUG(10,("update_c_setprinter: c_setprinter = %u\n", (unsigned int)c_setprinter));
709 tdb_store_int32(tdb_printers, GLOBAL_C_SETPRINTER, c_setprinter);
711 tdb_unlock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
713 return (uint32)c_setprinter;
716 /*******************************************************************
717 Get the spooler global c_setprinter, accounting for initialization.
718 ********************************************************************/
720 uint32 get_c_setprinter(void)
722 int32 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER);
724 if (c_setprinter == (int32)-1)
725 c_setprinter = update_c_setprinter(True);
727 DEBUG(10,("get_c_setprinter: c_setprinter = %d\n", c_setprinter));
729 return (uint32)c_setprinter;
732 /****************************************************************************
733 Get builtin form struct list.
734 ****************************************************************************/
736 int get_builtin_ntforms(nt_forms_struct **list)
738 *list = (nt_forms_struct *)memdup(&default_forms[0], sizeof(default_forms));
742 return ARRAY_SIZE(default_forms);
745 /****************************************************************************
746 get a builtin form struct
747 ****************************************************************************/
749 bool get_a_builtin_ntform_by_string(const char *form_name, nt_forms_struct *form)
752 DEBUGADD(6,("Looking for builtin form %s \n", form_name));
753 for (i=0; i<ARRAY_SIZE(default_forms); i++) {
754 if (strequal(form_name,default_forms[i].name)) {
755 DEBUGADD(6,("Found builtin form %s \n", form_name));
756 memcpy(form,&default_forms[i],sizeof(*form));
764 /****************************************************************************
765 get a form struct list.
766 ****************************************************************************/
768 int get_ntforms(nt_forms_struct **list)
770 TDB_DATA kbuf, newkey, dbuf;
771 nt_forms_struct form;
778 for (kbuf = tdb_firstkey(tdb_forms);
780 newkey = tdb_nextkey(tdb_forms, kbuf), free(kbuf.dptr), kbuf=newkey)
782 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) != 0)
785 dbuf = tdb_fetch(tdb_forms, kbuf);
789 fstrcpy(form.name, (const char *)kbuf.dptr+strlen(FORMS_PREFIX));
790 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "dddddddd",
791 &i, &form.flag, &form.width, &form.length, &form.left,
792 &form.top, &form.right, &form.bottom);
793 SAFE_FREE(dbuf.dptr);
794 if (ret != dbuf.dsize)
797 *list = SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1);
799 DEBUG(0,("get_ntforms: Realloc fail.\n"));
810 /****************************************************************************
811 write a form struct list
812 ****************************************************************************/
814 int write_ntforms(nt_forms_struct **list, int number)
816 TALLOC_CTX *ctx = talloc_tos();
823 for (i=0;i<number;i++) {
824 /* save index, so list is rebuilt in correct order */
825 len = tdb_pack(NULL, 0, "dddddddd",
826 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
827 (*list)[i].left, (*list)[i].top, (*list)[i].right,
832 buf = TALLOC_ARRAY(ctx, char, len);
836 len = tdb_pack((uint8 *)buf, len, "dddddddd",
837 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
838 (*list)[i].left, (*list)[i].top, (*list)[i].right,
840 key = talloc_asprintf(ctx, "%s%s", FORMS_PREFIX, (*list)[i].name);
845 dbuf.dptr = (uint8 *)buf;
846 if (tdb_store_bystring(tdb_forms, key, dbuf, TDB_REPLACE) != 0) {
858 /****************************************************************************
859 add a form struct at the end of the list
860 ****************************************************************************/
861 bool add_a_form(nt_forms_struct **list, struct spoolss_AddFormInfo1 *form, int *count)
867 * NT tries to add forms even when
868 * they are already in the base
869 * only update the values if already present
874 for (n=0; n<*count; n++) {
875 if ( strequal((*list)[n].name, form->form_name) ) {
882 if((*list=SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1)) == NULL) {
883 DEBUG(0,("add_a_form: failed to enlarge forms list!\n"));
886 fstrcpy((*list)[n].name, form->form_name);
890 (*list)[n].flag = form->flags;
891 (*list)[n].width = form->size.width;
892 (*list)[n].length = form->size.height;
893 (*list)[n].left = form->area.left;
894 (*list)[n].top = form->area.top;
895 (*list)[n].right = form->area.right;
896 (*list)[n].bottom = form->area.bottom;
898 DEBUG(6,("add_a_form: Successfully %s form [%s]\n",
899 update ? "updated" : "added", form->form_name));
904 /****************************************************************************
905 Delete a named form struct.
906 ****************************************************************************/
908 bool delete_a_form(nt_forms_struct **list, const char *del_name, int *count, WERROR *ret)
915 for (n=0; n<*count; n++) {
916 if (!strncmp((*list)[n].name, del_name, strlen(del_name))) {
917 DEBUG(103, ("delete_a_form, [%s] in list\n", del_name));
923 DEBUG(10,("delete_a_form, [%s] not found\n", del_name));
924 *ret = WERR_INVALID_FORM_NAME;
928 if (asprintf(&key, "%s%s", FORMS_PREFIX, (*list)[n].name) < 0) {
932 if (tdb_delete_bystring(tdb_forms, key) != 0) {
941 /****************************************************************************
942 Update a form struct.
943 ****************************************************************************/
945 void update_a_form(nt_forms_struct **list, struct spoolss_AddFormInfo1 *form, int count)
949 DEBUG(106, ("[%s]\n", form->form_name));
950 for (n=0; n<count; n++) {
951 DEBUGADD(106, ("n [%d]:[%s]\n", n, (*list)[n].name));
952 if (!strncmp((*list)[n].name, form->form_name, strlen(form->form_name)))
956 if (n==count) return;
958 (*list)[n].flag = form->flags;
959 (*list)[n].width = form->size.width;
960 (*list)[n].length = form->size.height;
961 (*list)[n].left = form->area.left;
962 (*list)[n].top = form->area.top;
963 (*list)[n].right = form->area.right;
964 (*list)[n].bottom = form->area.bottom;
967 /****************************************************************************
968 Get the nt drivers list.
969 Traverse the database and look-up the matching names.
970 ****************************************************************************/
971 int get_ntdrivers(fstring **list, const char *architecture, uint32 version)
974 const char *short_archi;
976 TDB_DATA kbuf, newkey;
978 short_archi = get_short_archi(architecture);
983 if (asprintf(&key, "%s%s/%d/", DRIVERS_PREFIX,
984 short_archi, version) < 0) {
988 for (kbuf = tdb_firstkey(tdb_drivers);
990 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
992 if (strncmp((const char *)kbuf.dptr, key, strlen(key)) != 0)
995 if((*list = SMB_REALLOC_ARRAY(*list, fstring, total+1)) == NULL) {
996 DEBUG(0,("get_ntdrivers: failed to enlarge list!\n"));
1001 fstrcpy((*list)[total], (const char *)kbuf.dptr+strlen(key));
1009 /****************************************************************************
1010 Function to do the mapping between the long architecture name and
1012 ****************************************************************************/
1014 const char *get_short_archi(const char *long_archi)
1018 DEBUG(107,("Getting architecture dependant directory\n"));
1021 } while ( (archi_table[i].long_archi!=NULL ) &&
1022 StrCaseCmp(long_archi, archi_table[i].long_archi) );
1024 if (archi_table[i].long_archi==NULL) {
1025 DEBUGADD(10,("Unknown architecture [%s] !\n", long_archi));
1029 /* this might be client code - but shouldn't this be an fstrcpy etc? */
1031 DEBUGADD(108,("index: [%d]\n", i));
1032 DEBUGADD(108,("long architecture: [%s]\n", archi_table[i].long_archi));
1033 DEBUGADD(108,("short architecture: [%s]\n", archi_table[i].short_archi));
1035 return archi_table[i].short_archi;
1038 /****************************************************************************
1039 Version information in Microsoft files is held in a VS_VERSION_INFO structure.
1040 There are two case to be covered here: PE (Portable Executable) and NE (New
1041 Executable) files. Both files support the same INFO structure, but PE files
1042 store the signature in unicode, and NE files store it as !unicode.
1043 returns -1 on error, 1 on version info found, and 0 on no version info found.
1044 ****************************************************************************/
1046 static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32 *minor)
1052 if ((buf=(char *)SMB_MALLOC(DOS_HEADER_SIZE)) == NULL) {
1053 DEBUG(0,("get_file_version: PE file [%s] DOS Header malloc failed bytes = %d\n",
1054 fname, DOS_HEADER_SIZE));
1058 if ((byte_count = vfs_read_data(fsp, buf, DOS_HEADER_SIZE)) < DOS_HEADER_SIZE) {
1059 DEBUG(3,("get_file_version: File [%s] DOS header too short, bytes read = %lu\n",
1060 fname, (unsigned long)byte_count));
1061 goto no_version_info;
1064 /* Is this really a DOS header? */
1065 if (SVAL(buf,DOS_HEADER_MAGIC_OFFSET) != DOS_HEADER_MAGIC) {
1066 DEBUG(6,("get_file_version: File [%s] bad DOS magic = 0x%x\n",
1067 fname, SVAL(buf,DOS_HEADER_MAGIC_OFFSET)));
1068 goto no_version_info;
1071 /* Skip OEM header (if any) and the DOS stub to start of Windows header */
1072 if (SMB_VFS_LSEEK(fsp, SVAL(buf,DOS_HEADER_LFANEW_OFFSET), SEEK_SET) == (SMB_OFF_T)-1) {
1073 DEBUG(3,("get_file_version: File [%s] too short, errno = %d\n",
1075 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1076 goto no_version_info;
1079 /* Note: DOS_HEADER_SIZE and NE_HEADER_SIZE are incidentally same */
1080 if ((byte_count = vfs_read_data(fsp, buf, NE_HEADER_SIZE)) < NE_HEADER_SIZE) {
1081 DEBUG(3,("get_file_version: File [%s] Windows header too short, bytes read = %lu\n",
1082 fname, (unsigned long)byte_count));
1083 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1084 goto no_version_info;
1087 /* The header may be a PE (Portable Executable) or an NE (New Executable) */
1088 if (IVAL(buf,PE_HEADER_SIGNATURE_OFFSET) == PE_HEADER_SIGNATURE) {
1089 unsigned int num_sections;
1090 unsigned int section_table_bytes;
1092 /* Just skip over optional header to get to section table */
1093 if (SMB_VFS_LSEEK(fsp,
1094 SVAL(buf,PE_HEADER_OPTIONAL_HEADER_SIZE)-(NE_HEADER_SIZE-PE_HEADER_SIZE),
1095 SEEK_CUR) == (SMB_OFF_T)-1) {
1096 DEBUG(3,("get_file_version: File [%s] Windows optional header too short, errno = %d\n",
1101 /* get the section table */
1102 num_sections = SVAL(buf,PE_HEADER_NUMBER_OF_SECTIONS);
1103 section_table_bytes = num_sections * PE_HEADER_SECT_HEADER_SIZE;
1104 if (section_table_bytes == 0)
1108 if ((buf=(char *)SMB_MALLOC(section_table_bytes)) == NULL) {
1109 DEBUG(0,("get_file_version: PE file [%s] section table malloc failed bytes = %d\n",
1110 fname, section_table_bytes));
1114 if ((byte_count = vfs_read_data(fsp, buf, section_table_bytes)) < section_table_bytes) {
1115 DEBUG(3,("get_file_version: PE file [%s] Section header too short, bytes read = %lu\n",
1116 fname, (unsigned long)byte_count));
1120 /* Iterate the section table looking for the resource section ".rsrc" */
1121 for (i = 0; i < num_sections; i++) {
1122 int sec_offset = i * PE_HEADER_SECT_HEADER_SIZE;
1124 if (strcmp(".rsrc", &buf[sec_offset+PE_HEADER_SECT_NAME_OFFSET]) == 0) {
1125 unsigned int section_pos = IVAL(buf,sec_offset+PE_HEADER_SECT_PTR_DATA_OFFSET);
1126 unsigned int section_bytes = IVAL(buf,sec_offset+PE_HEADER_SECT_SIZE_DATA_OFFSET);
1128 if (section_bytes == 0)
1132 if ((buf=(char *)SMB_MALLOC(section_bytes)) == NULL) {
1133 DEBUG(0,("get_file_version: PE file [%s] version malloc failed bytes = %d\n",
1134 fname, section_bytes));
1138 /* Seek to the start of the .rsrc section info */
1139 if (SMB_VFS_LSEEK(fsp, section_pos, SEEK_SET) == (SMB_OFF_T)-1) {
1140 DEBUG(3,("get_file_version: PE file [%s] too short for section info, errno = %d\n",
1145 if ((byte_count = vfs_read_data(fsp, buf, section_bytes)) < section_bytes) {
1146 DEBUG(3,("get_file_version: PE file [%s] .rsrc section too short, bytes read = %lu\n",
1147 fname, (unsigned long)byte_count));
1151 if (section_bytes < VS_VERSION_INFO_UNICODE_SIZE)
1154 for (i=0; i<section_bytes-VS_VERSION_INFO_UNICODE_SIZE; i++) {
1155 /* Scan for 1st 3 unicoded bytes followed by word aligned magic value */
1156 if (buf[i] == 'V' && buf[i+1] == '\0' && buf[i+2] == 'S') {
1157 /* Align to next long address */
1158 int pos = (i + sizeof(VS_SIGNATURE)*2 + 3) & 0xfffffffc;
1160 if (IVAL(buf,pos) == VS_MAGIC_VALUE) {
1161 *major = IVAL(buf,pos+VS_MAJOR_OFFSET);
1162 *minor = IVAL(buf,pos+VS_MINOR_OFFSET);
1164 DEBUG(6,("get_file_version: PE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1165 fname, *major, *minor,
1166 (*major>>16)&0xffff, *major&0xffff,
1167 (*minor>>16)&0xffff, *minor&0xffff));
1176 /* Version info not found, fall back to origin date/time */
1177 DEBUG(10,("get_file_version: PE file [%s] has no version info\n", fname));
1181 } else if (SVAL(buf,NE_HEADER_SIGNATURE_OFFSET) == NE_HEADER_SIGNATURE) {
1182 if (CVAL(buf,NE_HEADER_TARGET_OS_OFFSET) != NE_HEADER_TARGOS_WIN ) {
1183 DEBUG(3,("get_file_version: NE file [%s] wrong target OS = 0x%x\n",
1184 fname, CVAL(buf,NE_HEADER_TARGET_OS_OFFSET)));
1185 /* At this point, we assume the file is in error. It still could be somthing
1186 * else besides a NE file, but it unlikely at this point. */
1190 /* Allocate a bit more space to speed up things */
1192 if ((buf=(char *)SMB_MALLOC(VS_NE_BUF_SIZE)) == NULL) {
1193 DEBUG(0,("get_file_version: NE file [%s] malloc failed bytes = %d\n",
1194 fname, PE_HEADER_SIZE));
1198 /* This is a HACK! I got tired of trying to sort through the messy
1199 * 'NE' file format. If anyone wants to clean this up please have at
1200 * it, but this works. 'NE' files will eventually fade away. JRR */
1201 while((byte_count = vfs_read_data(fsp, buf, VS_NE_BUF_SIZE)) > 0) {
1202 /* Cover case that should not occur in a well formed 'NE' .dll file */
1203 if (byte_count-VS_VERSION_INFO_SIZE <= 0) break;
1205 for(i=0; i<byte_count; i++) {
1206 /* Fast skip past data that can't possibly match */
1207 if (buf[i] != 'V') continue;
1209 /* Potential match data crosses buf boundry, move it to beginning
1210 * of buf, and fill the buf with as much as it will hold. */
1211 if (i>byte_count-VS_VERSION_INFO_SIZE) {
1214 memcpy(buf, &buf[i], byte_count-i);
1215 if ((bc = vfs_read_data(fsp, &buf[byte_count-i], VS_NE_BUF_SIZE-
1216 (byte_count-i))) < 0) {
1218 DEBUG(0,("get_file_version: NE file [%s] Read error, errno=%d\n",
1223 byte_count = bc + (byte_count - i);
1224 if (byte_count<VS_VERSION_INFO_SIZE) break;
1229 /* Check that the full signature string and the magic number that
1230 * follows exist (not a perfect solution, but the chances that this
1231 * occurs in code is, well, remote. Yes I know I'm comparing the 'V'
1232 * twice, as it is simpler to read the code. */
1233 if (strcmp(&buf[i], VS_SIGNATURE) == 0) {
1234 /* Compute skip alignment to next long address */
1235 int skip = -(SMB_VFS_LSEEK(fsp, 0, SEEK_CUR) - (byte_count - i) +
1236 sizeof(VS_SIGNATURE)) & 3;
1237 if (IVAL(buf,i+sizeof(VS_SIGNATURE)+skip) != 0xfeef04bd) continue;
1239 *major = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MAJOR_OFFSET);
1240 *minor = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MINOR_OFFSET);
1241 DEBUG(6,("get_file_version: NE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1242 fname, *major, *minor,
1243 (*major>>16)&0xffff, *major&0xffff,
1244 (*minor>>16)&0xffff, *minor&0xffff));
1251 /* Version info not found, fall back to origin date/time */
1252 DEBUG(0,("get_file_version: NE file [%s] Version info not found\n", fname));
1257 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1258 DEBUG(3,("get_file_version: File [%s] unknown file format, signature = 0x%x\n",
1259 fname, IVAL(buf,PE_HEADER_SIGNATURE_OFFSET)));
1270 /****************************************************************************
1271 Drivers for Microsoft systems contain multiple files. Often, multiple drivers
1272 share one or more files. During the MS installation process files are checked
1273 to insure that only a newer version of a shared file is installed over an
1274 older version. There are several possibilities for this comparison. If there
1275 is no previous version, the new one is newer (obviously). If either file is
1276 missing the version info structure, compare the creation date (on Unix use
1277 the modification date). Otherwise chose the numerically larger version number.
1278 ****************************************************************************/
1280 static int file_version_is_newer(connection_struct *conn, fstring new_file, fstring old_file)
1282 bool use_version = true;
1286 time_t new_create_time;
1290 time_t old_create_time;
1292 struct smb_filename *smb_fname = NULL;
1293 files_struct *fsp = NULL;
1299 SET_STAT_INVALID(st);
1300 new_create_time = (time_t)0;
1301 old_create_time = (time_t)0;
1303 /* Get file version info (if available) for previous file (if it exists) */
1304 status = driver_unix_convert(conn, old_file, &smb_fname);
1305 if (!NT_STATUS_IS_OK(status)) {
1309 status = SMB_VFS_CREATE_FILE(
1312 0, /* root_dir_fid */
1313 smb_fname, /* fname */
1314 FILE_GENERIC_READ, /* access_mask */
1315 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1316 FILE_OPEN, /* create_disposition*/
1317 0, /* create_options */
1318 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1319 INTERNAL_OPEN_ONLY, /* oplock_request */
1320 0, /* allocation_size */
1321 0, /* private_flags */
1327 if (!NT_STATUS_IS_OK(status)) {
1328 /* Old file not found, so by definition new file is in fact newer */
1329 DEBUG(10,("file_version_is_newer: Can't open old file [%s], "
1330 "errno = %d\n", smb_fname_str_dbg(smb_fname),
1336 ret = get_file_version(fsp, old_file, &old_major, &old_minor);
1342 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1344 use_version = false;
1345 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1348 old_create_time = convert_timespec_to_time_t(st.st_ex_mtime);
1349 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n",
1350 (long)old_create_time));
1353 close_file(NULL, fsp, NORMAL_CLOSE);
1356 /* Get file version info (if available) for new file */
1357 status = driver_unix_convert(conn, new_file, &smb_fname);
1358 if (!NT_STATUS_IS_OK(status)) {
1362 status = SMB_VFS_CREATE_FILE(
1365 0, /* root_dir_fid */
1366 smb_fname, /* fname */
1367 FILE_GENERIC_READ, /* access_mask */
1368 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1369 FILE_OPEN, /* create_disposition*/
1370 0, /* create_options */
1371 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1372 INTERNAL_OPEN_ONLY, /* oplock_request */
1373 0, /* allocation_size */
1374 0, /* private_flags */
1380 if (!NT_STATUS_IS_OK(status)) {
1381 /* New file not found, this shouldn't occur if the caller did its job */
1382 DEBUG(3,("file_version_is_newer: Can't open new file [%s], "
1383 "errno = %d\n", smb_fname_str_dbg(smb_fname), errno));
1387 ret = get_file_version(fsp, new_file, &new_major, &new_minor);
1393 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1395 use_version = false;
1396 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1399 new_create_time = convert_timespec_to_time_t(st.st_ex_mtime);
1400 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n",
1401 (long)new_create_time));
1404 close_file(NULL, fsp, NORMAL_CLOSE);
1407 if (use_version && (new_major != old_major || new_minor != old_minor)) {
1408 /* Compare versions and choose the larger version number */
1409 if (new_major > old_major ||
1410 (new_major == old_major && new_minor > old_minor)) {
1412 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1417 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1423 /* Compare modification time/dates and choose the newest time/date */
1424 if (new_create_time > old_create_time) {
1425 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1430 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1438 close_file(NULL, fsp, NORMAL_CLOSE);
1441 TALLOC_FREE(smb_fname);
1445 /****************************************************************************
1446 Determine the correct cVersion associated with an architecture and driver
1447 ****************************************************************************/
1448 static uint32 get_correct_cversion(struct pipes_struct *p,
1449 const char *architecture,
1450 const char *driverpath_in,
1455 struct smb_filename *smb_fname = NULL;
1456 char *driverpath = NULL;
1457 files_struct *fsp = NULL;
1458 connection_struct *conn = NULL;
1461 fstring printdollar;
1462 int printdollar_snum;
1464 *perr = WERR_INVALID_PARAM;
1466 /* If architecture is Windows 95/98/ME, the version is always 0. */
1467 if (strcmp(architecture, SPL_ARCH_WIN40) == 0) {
1468 DEBUG(10,("get_correct_cversion: Driver is Win9x, cversion = 0\n"));
1473 /* If architecture is Windows x64, the version is always 3. */
1474 if (strcmp(architecture, SPL_ARCH_X64) == 0) {
1475 DEBUG(10,("get_correct_cversion: Driver is x64, cversion = 3\n"));
1480 fstrcpy(printdollar, "print$");
1482 printdollar_snum = find_service(printdollar);
1483 if (printdollar_snum == -1) {
1484 *perr = WERR_NO_SUCH_SHARE;
1488 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
1489 lp_pathname(printdollar_snum),
1490 p->server_info, &oldcwd);
1491 if (!NT_STATUS_IS_OK(nt_status)) {
1492 DEBUG(0,("get_correct_cversion: create_conn_struct "
1493 "returned %s\n", nt_errstr(nt_status)));
1494 *perr = ntstatus_to_werror(nt_status);
1498 /* Open the driver file (Portable Executable format) and determine the
1499 * deriver the cversion. */
1500 driverpath = talloc_asprintf(talloc_tos(),
1509 nt_status = driver_unix_convert(conn, driverpath, &smb_fname);
1510 if (!NT_STATUS_IS_OK(nt_status)) {
1511 *perr = ntstatus_to_werror(nt_status);
1515 nt_status = vfs_file_exist(conn, smb_fname);
1516 if (!NT_STATUS_IS_OK(nt_status)) {
1517 *perr = WERR_BADFILE;
1521 status = SMB_VFS_CREATE_FILE(
1524 0, /* root_dir_fid */
1525 smb_fname, /* fname */
1526 FILE_GENERIC_READ, /* access_mask */
1527 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1528 FILE_OPEN, /* create_disposition*/
1529 0, /* create_options */
1530 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1531 INTERNAL_OPEN_ONLY, /* oplock_request */
1532 0, /* private_flags */
1533 0, /* allocation_size */
1539 if (!NT_STATUS_IS_OK(status)) {
1540 DEBUG(3,("get_correct_cversion: Can't open file [%s], errno = "
1541 "%d\n", smb_fname_str_dbg(smb_fname), errno));
1542 *perr = WERR_ACCESS_DENIED;
1549 ret = get_file_version(fsp, smb_fname->base_name, &major, &minor);
1550 if (ret == -1) goto error_exit;
1553 DEBUG(6,("get_correct_cversion: Version info not "
1555 smb_fname_str_dbg(smb_fname)));
1560 * This is a Microsoft'ism. See references in MSDN to VER_FILEVERSION
1561 * for more details. Version in this case is not just the version of the
1562 * file, but the version in the sense of kernal mode (2) vs. user mode
1563 * (3) drivers. Other bits of the version fields are the version info.
1566 cversion = major & 0x0000ffff;
1568 case 2: /* WinNT drivers */
1569 case 3: /* Win2K drivers */
1573 DEBUG(6,("get_correct_cversion: cversion "
1574 "invalid [%s] cversion = %d\n",
1575 smb_fname_str_dbg(smb_fname),
1580 DEBUG(10,("get_correct_cversion: Version info found [%s] major"
1581 " = 0x%x minor = 0x%x\n",
1582 smb_fname_str_dbg(smb_fname), major, minor));
1585 DEBUG(10,("get_correct_cversion: Driver file [%s] cversion = %d\n",
1586 smb_fname_str_dbg(smb_fname), cversion));
1593 TALLOC_FREE(smb_fname);
1595 close_file(NULL, fsp, NORMAL_CLOSE);
1598 vfs_ChDir(conn, oldcwd);
1601 if (cversion != -1) {
1607 /****************************************************************************
1608 ****************************************************************************/
1610 #define strip_driver_path(_mem_ctx, _element) do { \
1611 if (_element && ((_p = strrchr((_element), '\\')) != NULL)) { \
1612 (_element) = talloc_asprintf((_mem_ctx), "%s", _p+1); \
1613 W_ERROR_HAVE_NO_MEMORY((_element)); \
1617 static WERROR clean_up_driver_struct_level(TALLOC_CTX *mem_ctx,
1618 struct pipes_struct *rpc_pipe,
1619 const char *architecture,
1620 const char **driver_path,
1621 const char **data_file,
1622 const char **config_file,
1623 const char **help_file,
1624 struct spoolss_StringArray *dependent_files,
1627 const char *short_architecture;
1632 if (!*driver_path || !*data_file || !*config_file) {
1633 return WERR_INVALID_PARAM;
1636 /* clean up the driver name.
1637 * we can get .\driver.dll
1638 * or worse c:\windows\system\driver.dll !
1640 /* using an intermediate string to not have overlaping memcpy()'s */
1642 strip_driver_path(mem_ctx, *driver_path);
1643 strip_driver_path(mem_ctx, *data_file);
1644 strip_driver_path(mem_ctx, *config_file);
1646 strip_driver_path(mem_ctx, *help_file);
1649 if (dependent_files && dependent_files->string) {
1650 for (i=0; dependent_files->string[i]; i++) {
1651 strip_driver_path(mem_ctx, dependent_files->string[i]);
1655 short_architecture = get_short_archi(architecture);
1656 if (!short_architecture) {
1657 return WERR_UNKNOWN_PRINTER_DRIVER;
1660 /* jfm:7/16/2000 the client always sends the cversion=0.
1661 * The server should check which version the driver is by reading
1662 * the PE header of driver->driverpath.
1664 * For Windows 95/98 the version is 0 (so the value sent is correct)
1665 * For Windows NT (the architecture doesn't matter)
1666 * NT 3.1: cversion=0
1667 * NT 3.5/3.51: cversion=1
1672 *version = get_correct_cversion(rpc_pipe, short_architecture,
1673 *driver_path, &err);
1674 if (*version == -1) {
1681 /****************************************************************************
1682 ****************************************************************************/
1684 WERROR clean_up_driver_struct(struct pipes_struct *rpc_pipe,
1685 struct spoolss_AddDriverInfoCtr *r)
1689 return clean_up_driver_struct_level(r, rpc_pipe,
1690 r->info.info3->architecture,
1691 &r->info.info3->driver_path,
1692 &r->info.info3->data_file,
1693 &r->info.info3->config_file,
1694 &r->info.info3->help_file,
1695 r->info.info3->dependent_files,
1696 &r->info.info3->version);
1698 return clean_up_driver_struct_level(r, rpc_pipe,
1699 r->info.info6->architecture,
1700 &r->info.info6->driver_path,
1701 &r->info.info6->data_file,
1702 &r->info.info6->config_file,
1703 &r->info.info6->help_file,
1704 r->info.info6->dependent_files,
1705 &r->info.info6->version);
1707 return WERR_NOT_SUPPORTED;
1711 /****************************************************************************
1712 This function sucks and should be replaced. JRA.
1713 ****************************************************************************/
1715 static void convert_level_6_to_level3(struct spoolss_AddDriverInfo3 *dst,
1716 const struct spoolss_AddDriverInfo6 *src)
1718 dst->version = src->version;
1720 dst->driver_name = src->driver_name;
1721 dst->architecture = src->architecture;
1722 dst->driver_path = src->driver_path;
1723 dst->data_file = src->data_file;
1724 dst->config_file = src->config_file;
1725 dst->help_file = src->help_file;
1726 dst->monitor_name = src->monitor_name;
1727 dst->default_datatype = src->default_datatype;
1728 dst->_ndr_size_dependent_files = src->_ndr_size_dependent_files;
1729 dst->dependent_files = src->dependent_files;
1732 /****************************************************************************
1733 This function sucks and should be replaced. JRA.
1734 ****************************************************************************/
1736 static void convert_level_8_to_level3(TALLOC_CTX *mem_ctx,
1737 struct spoolss_AddDriverInfo3 *dst,
1738 const struct spoolss_DriverInfo8 *src)
1740 dst->version = src->version;
1741 dst->driver_name = src->driver_name;
1742 dst->architecture = src->architecture;
1743 dst->driver_path = src->driver_path;
1744 dst->data_file = src->data_file;
1745 dst->config_file = src->config_file;
1746 dst->help_file = src->help_file;
1747 dst->monitor_name = src->monitor_name;
1748 dst->default_datatype = src->default_datatype;
1749 if (src->dependent_files) {
1750 dst->dependent_files = talloc_zero(mem_ctx, struct spoolss_StringArray);
1751 if (!dst->dependent_files) return;
1752 dst->dependent_files->string = src->dependent_files;
1754 dst->dependent_files = NULL;
1758 /****************************************************************************
1759 ****************************************************************************/
1761 static WERROR move_driver_file_to_download_area(TALLOC_CTX *mem_ctx,
1762 connection_struct *conn,
1763 const char *driver_file,
1764 const char *short_architecture,
1765 uint32_t driver_version,
1768 struct smb_filename *smb_fname_old = NULL;
1769 struct smb_filename *smb_fname_new = NULL;
1770 char *old_name = NULL;
1771 char *new_name = NULL;
1775 old_name = talloc_asprintf(mem_ctx, "%s/%s",
1776 short_architecture, driver_file);
1777 W_ERROR_HAVE_NO_MEMORY(old_name);
1779 new_name = talloc_asprintf(mem_ctx, "%s/%d/%s",
1780 short_architecture, driver_version, driver_file);
1781 if (new_name == NULL) {
1782 TALLOC_FREE(old_name);
1786 if (version != -1 && (version = file_version_is_newer(conn, old_name, new_name)) > 0) {
1788 status = driver_unix_convert(conn, old_name, &smb_fname_old);
1789 if (!NT_STATUS_IS_OK(status)) {
1794 /* Setup a synthetic smb_filename struct */
1795 smb_fname_new = TALLOC_ZERO_P(mem_ctx, struct smb_filename);
1796 if (!smb_fname_new) {
1801 smb_fname_new->base_name = new_name;
1803 DEBUG(10,("move_driver_file_to_download_area: copying '%s' to "
1804 "'%s'\n", smb_fname_old->base_name,
1805 smb_fname_new->base_name));
1807 status = copy_file(mem_ctx, conn, smb_fname_old, smb_fname_new,
1808 OPENX_FILE_EXISTS_TRUNCATE |
1809 OPENX_FILE_CREATE_IF_NOT_EXIST,
1812 if (!NT_STATUS_IS_OK(status)) {
1813 DEBUG(0,("move_driver_file_to_download_area: Unable "
1814 "to rename [%s] to [%s]: %s\n",
1815 smb_fname_old->base_name, new_name,
1816 nt_errstr(status)));
1817 ret = WERR_ACCESS_DENIED;
1824 TALLOC_FREE(smb_fname_old);
1825 TALLOC_FREE(smb_fname_new);
1829 WERROR move_driver_to_download_area(struct pipes_struct *p,
1830 struct spoolss_AddDriverInfoCtr *r,
1833 struct spoolss_AddDriverInfo3 *driver;
1834 struct spoolss_AddDriverInfo3 converted_driver;
1835 const char *short_architecture;
1836 struct smb_filename *smb_dname = NULL;
1837 char *new_dir = NULL;
1838 connection_struct *conn = NULL;
1841 TALLOC_CTX *ctx = talloc_tos();
1844 fstring printdollar;
1845 int printdollar_snum;
1851 driver = r->info.info3;
1854 convert_level_6_to_level3(&converted_driver, r->info.info6);
1855 driver = &converted_driver;
1858 DEBUG(0,("move_driver_to_download_area: Unknown info level (%u)\n", (unsigned int)r->level));
1859 return WERR_UNKNOWN_LEVEL;
1862 short_architecture = get_short_archi(driver->architecture);
1863 if (!short_architecture) {
1864 return WERR_UNKNOWN_PRINTER_DRIVER;
1867 fstrcpy(printdollar, "print$");
1869 printdollar_snum = find_service(printdollar);
1870 if (printdollar_snum == -1) {
1871 *perr = WERR_NO_SUCH_SHARE;
1872 return WERR_NO_SUCH_SHARE;
1875 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
1876 lp_pathname(printdollar_snum),
1877 p->server_info, &oldcwd);
1878 if (!NT_STATUS_IS_OK(nt_status)) {
1879 DEBUG(0,("move_driver_to_download_area: create_conn_struct "
1880 "returned %s\n", nt_errstr(nt_status)));
1881 *perr = ntstatus_to_werror(nt_status);
1885 new_dir = talloc_asprintf(ctx,
1893 nt_status = driver_unix_convert(conn, new_dir, &smb_dname);
1894 if (!NT_STATUS_IS_OK(nt_status)) {
1899 DEBUG(5,("Creating first directory: %s\n", smb_dname->base_name));
1901 create_directory(conn, NULL, smb_dname);
1903 /* For each driver file, archi\filexxx.yyy, if there is a duplicate file
1904 * listed for this driver which has already been moved, skip it (note:
1905 * drivers may list the same file name several times. Then check if the
1906 * file already exists in archi\version\, if so, check that the version
1907 * info (or time stamps if version info is unavailable) is newer (or the
1908 * date is later). If it is, move it to archi\version\filexxx.yyy.
1909 * Otherwise, delete the file.
1911 * If a file is not moved to archi\version\ because of an error, all the
1912 * rest of the 'unmoved' driver files are removed from archi\. If one or
1913 * more of the driver's files was already moved to archi\version\, it
1914 * potentially leaves the driver in a partially updated state. Version
1915 * trauma will most likely occur if an client attempts to use any printer
1916 * bound to the driver. Perhaps a rewrite to make sure the moves can be
1917 * done is appropriate... later JRR
1920 DEBUG(5,("Moving files now !\n"));
1922 if (driver->driver_path && strlen(driver->driver_path)) {
1924 *perr = move_driver_file_to_download_area(ctx,
1926 driver->driver_path,
1930 if (!W_ERROR_IS_OK(*perr)) {
1931 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1938 if (driver->data_file && strlen(driver->data_file)) {
1939 if (!strequal(driver->data_file, driver->driver_path)) {
1941 *perr = move_driver_file_to_download_area(ctx,
1947 if (!W_ERROR_IS_OK(*perr)) {
1948 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1956 if (driver->config_file && strlen(driver->config_file)) {
1957 if (!strequal(driver->config_file, driver->driver_path) &&
1958 !strequal(driver->config_file, driver->data_file)) {
1960 *perr = move_driver_file_to_download_area(ctx,
1962 driver->config_file,
1966 if (!W_ERROR_IS_OK(*perr)) {
1967 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1975 if (driver->help_file && strlen(driver->help_file)) {
1976 if (!strequal(driver->help_file, driver->driver_path) &&
1977 !strequal(driver->help_file, driver->data_file) &&
1978 !strequal(driver->help_file, driver->config_file)) {
1980 *perr = move_driver_file_to_download_area(ctx,
1986 if (!W_ERROR_IS_OK(*perr)) {
1987 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1995 if (driver->dependent_files && driver->dependent_files->string) {
1996 for (i=0; driver->dependent_files->string[i]; i++) {
1997 if (!strequal(driver->dependent_files->string[i], driver->driver_path) &&
1998 !strequal(driver->dependent_files->string[i], driver->data_file) &&
1999 !strequal(driver->dependent_files->string[i], driver->config_file) &&
2000 !strequal(driver->dependent_files->string[i], driver->help_file)) {
2002 for (j=0; j < i; j++) {
2003 if (strequal(driver->dependent_files->string[i], driver->dependent_files->string[j])) {
2008 *perr = move_driver_file_to_download_area(ctx,
2010 driver->dependent_files->string[i],
2014 if (!W_ERROR_IS_OK(*perr)) {
2015 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
2026 TALLOC_FREE(smb_dname);
2029 vfs_ChDir(conn, oldcwd);
2033 if (W_ERROR_EQUAL(*perr, WERR_OK)) {
2037 return WERR_UNKNOWN_PRINTER_DRIVER;
2042 /****************************************************************************
2043 ****************************************************************************/
2045 static uint32 add_a_printer_driver_3(struct spoolss_AddDriverInfo3 *driver)
2047 TALLOC_CTX *ctx = talloc_tos();
2049 const char *architecture;
2050 char *directory = NULL;
2056 architecture = get_short_archi(driver->architecture);
2057 if (!architecture) {
2061 /* The names are relative. We store them in the form: \print$\arch\version\driver.xxx
2062 * \\server is added in the rpc server layer.
2063 * It does make sense to NOT store the server's name in the printer TDB.
2066 directory = talloc_asprintf(ctx, "\\print$\\%s\\%d\\",
2067 architecture, driver->version);
2072 #define gen_full_driver_unc_path(ctx, directory, file) \
2074 if (file && strlen(file)) { \
2075 file = talloc_asprintf(ctx, "%s%s", directory, file); \
2077 file = talloc_strdup(ctx, ""); \
2080 return (uint32_t)-1; \
2084 /* .inf files do not always list a file for each of the four standard files.
2085 * Don't prepend a path to a null filename, or client claims:
2086 * "The server on which the printer resides does not have a suitable
2087 * <printer driver name> printer driver installed. Click OK if you
2088 * wish to install the driver on your local machine."
2091 gen_full_driver_unc_path(ctx, directory, driver->driver_path);
2092 gen_full_driver_unc_path(ctx, directory, driver->data_file);
2093 gen_full_driver_unc_path(ctx, directory, driver->config_file);
2094 gen_full_driver_unc_path(ctx, directory, driver->help_file);
2096 if (driver->dependent_files && driver->dependent_files->string) {
2097 for (i=0; driver->dependent_files->string[i]; i++) {
2098 gen_full_driver_unc_path(ctx, directory,
2099 driver->dependent_files->string[i]);
2103 key = talloc_asprintf(ctx, "%s%s/%d/%s", DRIVERS_PREFIX,
2104 architecture, driver->version, driver->driver_name);
2109 DEBUG(5,("add_a_printer_driver_3: Adding driver with key %s\n", key ));
2116 len += tdb_pack(buf+len, buflen-len, "dffffffff",
2118 driver->driver_name,
2119 driver->architecture,
2120 driver->driver_path,
2122 driver->config_file,
2124 driver->monitor_name ? driver->monitor_name : "",
2125 driver->default_datatype ? driver->default_datatype : "");
2127 if (driver->dependent_files && driver->dependent_files->string) {
2128 for (i=0; driver->dependent_files->string[i]; i++) {
2129 len += tdb_pack(buf+len, buflen-len, "f",
2130 driver->dependent_files->string[i]);
2134 if (len != buflen) {
2135 buf = (uint8 *)SMB_REALLOC(buf, len);
2137 DEBUG(0,("add_a_printer_driver_3: failed to enlarge buffer\n!"));
2148 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
2152 DEBUG(0,("add_a_printer_driver_3: Adding driver with key %s failed.\n", key ));
2158 /****************************************************************************
2159 ****************************************************************************/
2161 static uint32_t add_a_printer_driver_8(struct spoolss_DriverInfo8 *driver)
2163 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
2164 struct spoolss_AddDriverInfo3 info3;
2167 convert_level_8_to_level3(mem_ctx, &info3, driver);
2169 ret = add_a_printer_driver_3(&info3);
2170 talloc_free(mem_ctx);
2175 /****************************************************************************
2176 ****************************************************************************/
2178 static WERROR get_a_printer_driver_3_default(TALLOC_CTX *mem_ctx,
2179 struct spoolss_DriverInfo3 *info,
2180 const char *driver, const char *arch)
2182 info->driver_name = talloc_strdup(mem_ctx, driver);
2183 if (!info->driver_name) {
2187 info->default_datatype = talloc_strdup(mem_ctx, "RAW");
2188 if (!info->default_datatype) {
2192 info->driver_path = talloc_strdup(mem_ctx, "");
2193 info->data_file = talloc_strdup(mem_ctx, "");
2194 info->config_file = talloc_strdup(mem_ctx, "");
2195 info->help_file = talloc_strdup(mem_ctx, "");
2196 if (!info->driver_path || !info->data_file || !info->config_file || !info->help_file) {
2203 /****************************************************************************
2204 ****************************************************************************/
2206 static WERROR get_a_printer_driver_3(TALLOC_CTX *mem_ctx,
2207 struct spoolss_DriverInfo3 *driver,
2208 const char *drivername, const char *arch,
2212 const char *architecture;
2216 fstring name, driverpath, environment, datafile, configfile, helpfile, monitorname, defaultdatatype;
2218 architecture = get_short_archi(arch);
2219 if ( !architecture ) {
2220 return WERR_UNKNOWN_PRINTER_DRIVER;
2223 /* Windows 4.0 (i.e. win9x) should always use a version of 0 */
2225 if ( strcmp( architecture, SPL_ARCH_WIN40 ) == 0 )
2228 DEBUG(8,("get_a_printer_driver_3: [%s%s/%d/%s]\n", DRIVERS_PREFIX, architecture, version, drivername));
2230 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
2231 architecture, version, drivername) < 0) {
2235 dbuf = tdb_fetch_bystring(tdb_drivers, key);
2238 return WERR_UNKNOWN_PRINTER_DRIVER;
2241 len += tdb_unpack(dbuf.dptr, dbuf.dsize, "dffffffff",
2252 driver->driver_name = talloc_strdup(mem_ctx, name);
2253 driver->architecture = talloc_strdup(mem_ctx, environment);
2254 driver->driver_path = talloc_strdup(mem_ctx, driverpath);
2255 driver->data_file = talloc_strdup(mem_ctx, datafile);
2256 driver->config_file = talloc_strdup(mem_ctx, configfile);
2257 driver->help_file = talloc_strdup(mem_ctx, helpfile);
2258 driver->monitor_name = talloc_strdup(mem_ctx, monitorname);
2259 driver->default_datatype = talloc_strdup(mem_ctx, defaultdatatype);
2263 while (len < dbuf.dsize) {
2267 driver->dependent_files = talloc_realloc(mem_ctx, driver->dependent_files, const char *, i+2);
2268 if (!driver->dependent_files ) {
2269 DEBUG(0,("get_a_printer_driver_3: failed to enlarge buffer!\n"));
2273 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "f",
2276 driver->dependent_files[i] = talloc_strdup(mem_ctx, file);
2281 if (driver->dependent_files)
2282 driver->dependent_files[i] = NULL;
2284 SAFE_FREE(dbuf.dptr);
2287 if (len != dbuf.dsize) {
2288 return get_a_printer_driver_3_default(mem_ctx, driver, drivername, arch);
2294 /****************************************************************************
2295 ****************************************************************************/
2296 int pack_devicemode(NT_DEVICEMODE *nt_devmode, uint8 *buf, int buflen)
2300 len += tdb_pack(buf+len, buflen-len, "p", nt_devmode);
2305 len += tdb_pack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2306 nt_devmode->devicename,
2307 nt_devmode->formname,
2309 nt_devmode->specversion,
2310 nt_devmode->driverversion,
2312 nt_devmode->driverextra,
2313 nt_devmode->orientation,
2314 nt_devmode->papersize,
2315 nt_devmode->paperlength,
2316 nt_devmode->paperwidth,
2319 nt_devmode->defaultsource,
2320 nt_devmode->printquality,
2323 nt_devmode->yresolution,
2324 nt_devmode->ttoption,
2325 nt_devmode->collate,
2326 nt_devmode->logpixels,
2329 nt_devmode->bitsperpel,
2330 nt_devmode->pelswidth,
2331 nt_devmode->pelsheight,
2332 nt_devmode->displayflags,
2333 nt_devmode->displayfrequency,
2334 nt_devmode->icmmethod,
2335 nt_devmode->icmintent,
2336 nt_devmode->mediatype,
2337 nt_devmode->dithertype,
2338 nt_devmode->reserved1,
2339 nt_devmode->reserved2,
2340 nt_devmode->panningwidth,
2341 nt_devmode->panningheight,
2342 nt_devmode->nt_dev_private);
2344 if (nt_devmode->nt_dev_private) {
2345 len += tdb_pack(buf+len, buflen-len, "B",
2346 nt_devmode->driverextra,
2347 nt_devmode->nt_dev_private);
2350 DEBUG(8,("Packed devicemode [%s]\n", nt_devmode->formname));
2355 /****************************************************************************
2356 Pack all values in all printer keys
2357 ***************************************************************************/
2359 static int pack_values(NT_PRINTER_DATA *data, uint8 *buf, int buflen)
2363 struct regval_blob *val;
2364 struct regval_ctr *val_ctr;
2371 /* loop over all keys */
2373 for ( i=0; i<data->num_keys; i++ ) {
2374 val_ctr = data->keys[i].values;
2375 num_values = regval_ctr_numvals( val_ctr );
2377 /* pack the keyname followed by a empty value */
2379 len += tdb_pack(buf+len, buflen-len, "pPdB",
2380 &data->keys[i].name,
2386 /* now loop over all values */
2388 for ( j=0; j<num_values; j++ ) {
2389 /* pathname should be stored as <key>\<value> */
2391 val = regval_ctr_specific_value( val_ctr, j );
2392 if (asprintf(&path, "%s\\%s",
2394 regval_name(val)) < 0) {
2398 len += tdb_pack(buf+len, buflen-len, "pPdB",
2403 regval_data_p(val) );
2405 DEBUG(8,("specific: [%s], len: %d\n", regval_name(val), regval_size(val)));
2413 len += tdb_pack(buf+len, buflen-len, "p", NULL);
2419 /****************************************************************************
2420 Delete a printer - this just deletes the printer info file, any open
2421 handles are not affected.
2422 ****************************************************************************/
2424 uint32 del_a_printer(const char *sharename)
2427 char *printdb_path = NULL;
2428 TALLOC_CTX *ctx = talloc_tos();
2430 kbuf = make_printer_tdbkey(ctx, sharename);
2431 tdb_delete(tdb_printers, kbuf);
2433 kbuf= make_printers_secdesc_tdbkey(ctx, sharename);
2434 tdb_delete(tdb_printers, kbuf);
2436 close_all_print_db();
2438 if (geteuid() == sec_initial_uid()) {
2439 if (asprintf(&printdb_path, "%s%s.tdb",
2440 cache_path("printing/"),
2444 unlink(printdb_path);
2445 SAFE_FREE(printdb_path);
2451 /****************************************************************************
2452 ****************************************************************************/
2453 static WERROR update_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info)
2459 TDB_DATA kbuf, dbuf;
2462 * in addprinter: no servername and the printer is the name
2463 * in setprinter: servername is \\server
2464 * and printer is \\server\\printer
2466 * Samba manages only local printers.
2467 * we currently don't support things like i
2468 * path=\\other_server\printer
2470 * We only store the printername, not \\server\printername
2473 if ( info->servername[0] != '\0' ) {
2474 trim_string(info->printername, info->servername, NULL);
2475 trim_char(info->printername, '\\', '\0');
2476 info->servername[0]='\0';
2480 * JFM: one day I'll forget.
2481 * below that's info->portname because that's the SAMBA sharename
2482 * and I made NT 'thinks' it's the portname
2483 * the info->sharename is the thing you can name when you add a printer
2484 * that's the short-name when you create shared printer for 95/98
2485 * So I've made a limitation in SAMBA: you can only have 1 printer model
2486 * behind a SAMBA share.
2494 len += tdb_pack(buf+len, buflen-len, "dddddddddddfffffPfffff",
2497 info->default_priority,
2514 info->printprocessor,
2518 len += pack_devicemode(info->devmode, buf+len, buflen-len);
2519 retlen = pack_values( info->data, buf+len, buflen-len );
2526 if (buflen != len) {
2527 buf = (uint8 *)SMB_REALLOC(buf, len);
2529 DEBUG(0,("update_a_printer_2: failed to enlarge buffer!\n"));
2537 kbuf = make_printer_tdbkey(talloc_tos(), info->sharename );
2542 ret = (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) == 0? WERR_OK : WERR_NOMEM);
2545 if (!W_ERROR_IS_OK(ret))
2546 DEBUG(8, ("error updating printer to tdb on disk\n"));
2550 DEBUG(8,("packed printer [%s] with driver [%s] portname=[%s] len=%d\n",
2551 info->sharename, info->drivername, info->portname, len));
2556 /****************************************************************************
2557 Create and allocate a default devicemode.
2558 ****************************************************************************/
2560 WERROR spoolss_create_default_devmode(TALLOC_CTX *mem_ctx,
2561 const char *devicename,
2562 struct spoolss_DeviceMode **devmode)
2564 struct spoolss_DeviceMode *dm;
2567 dm = talloc_zero(mem_ctx, struct spoolss_DeviceMode);
2572 dname = talloc_asprintf(dm, "%s", devicename);
2573 if (dname == NULL) {
2576 if (strlen(dname) > MAXDEVICENAME) {
2577 dname[MAXDEVICENAME] = '\0';
2579 dm->devicename = dname;
2581 dm->formname = talloc_strdup(dm, "Letter");
2582 if (dm->formname == NULL) {
2586 dm->specversion = DMSPEC_NT4_AND_ABOVE;
2587 dm->driverversion = 0x0400;
2589 dm->__driverextra_length = 0;
2590 dm->fields = DEVMODE_FORMNAME |
2592 DEVMODE_PRINTQUALITY |
2593 DEVMODE_DEFAULTSOURCE |
2597 DEVMODE_ORIENTATION;
2598 dm->orientation = DMORIENT_PORTRAIT;
2599 dm->papersize = DMPAPER_LETTER;
2600 dm->paperlength = 0;
2604 dm->defaultsource = DMBIN_FORMSOURCE;
2605 dm->printquality = DMRES_HIGH; /* 0x0258 */
2606 dm->color = DMRES_MONOCHROME;
2607 dm->duplex = DMDUP_SIMPLEX;
2608 dm->yresolution = 0;
2609 dm->ttoption = DMTT_SUBDEV;
2610 dm->collate = DMCOLLATE_FALSE;
2620 dm->displayflags = 0;
2621 dm->displayfrequency = 0;
2624 dm->panningwidth = 0;
2625 dm->panningheight = 0;
2627 dm->driverextra_data.data = NULL;
2628 dm->driverextra_data.length = 0;
2634 WERROR spoolss_create_default_secdesc(TALLOC_CTX *mem_ctx,
2635 struct spoolss_security_descriptor **secdesc)
2637 struct security_ace ace[5]; /* max number of ace entries */
2640 struct security_acl *psa = NULL;
2641 struct security_descriptor *psd = NULL;
2645 /* Create an ACE where Everyone is allowed to print */
2647 sa = PRINTER_ACE_PRINT;
2648 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
2649 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
2651 /* Add the domain admins group if we are a DC */
2654 DOM_SID domadmins_sid;
2656 sid_compose(&domadmins_sid, get_global_sam_sid(),
2659 sa = PRINTER_ACE_FULL_CONTROL;
2660 init_sec_ace(&ace[i++], &domadmins_sid,
2661 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
2662 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
2663 init_sec_ace(&ace[i++], &domadmins_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
2664 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
2666 else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
2667 sid_append_rid(&adm_sid, DOMAIN_RID_ADMINISTRATOR);
2669 sa = PRINTER_ACE_FULL_CONTROL;
2670 init_sec_ace(&ace[i++], &adm_sid,
2671 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
2672 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
2673 init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
2674 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
2677 /* add BUILTIN\Administrators as FULL CONTROL */
2679 sa = PRINTER_ACE_FULL_CONTROL;
2680 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
2681 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
2682 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
2683 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
2684 SEC_ACE_TYPE_ACCESS_ALLOWED,
2685 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
2687 /* Make the security descriptor owned by the BUILTIN\Administrators */
2689 /* The ACL revision number in rpc_secdesc.h differs from the one
2690 created by NT when setting ACE entries in printer
2691 descriptors. NT4 complains about the property being edited by a
2694 if ((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, i, ace)) != NULL) {
2695 psd = make_sec_desc(mem_ctx,
2697 SEC_DESC_SELF_RELATIVE,
2698 &global_sid_Builtin_Administrators,
2699 &global_sid_Builtin_Administrators,
2706 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
2710 DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
2711 (unsigned int)sd_size));
2718 /****************************************************************************
2719 Malloc and return an NT devicemode.
2720 ****************************************************************************/
2722 NT_DEVICEMODE *construct_nt_devicemode(const fstring default_devicename)
2725 char adevice[MAXDEVICENAME];
2726 NT_DEVICEMODE *nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE);
2728 if (nt_devmode == NULL) {
2729 DEBUG(0,("construct_nt_devicemode: malloc fail.\n"));
2733 ZERO_STRUCTP(nt_devmode);
2735 slprintf(adevice, sizeof(adevice), "%s", default_devicename);
2736 fstrcpy(nt_devmode->devicename, adevice);
2738 fstrcpy(nt_devmode->formname, "Letter");
2740 nt_devmode->specversion = DMSPEC_NT4_AND_ABOVE;
2741 nt_devmode->driverversion = 0x0400;
2742 nt_devmode->size = 0x00DC;
2743 nt_devmode->driverextra = 0x0000;
2744 nt_devmode->fields = DEVMODE_FORMNAME |
2746 DEVMODE_PRINTQUALITY |
2747 DEVMODE_DEFAULTSOURCE |
2751 DEVMODE_ORIENTATION;
2752 nt_devmode->orientation = DMORIENT_PORTRAIT;
2753 nt_devmode->papersize = DMPAPER_LETTER;
2754 nt_devmode->paperlength = 0;
2755 nt_devmode->paperwidth = 0;
2756 nt_devmode->scale = 0x64;
2757 nt_devmode->copies = 1;
2758 nt_devmode->defaultsource = DMBIN_FORMSOURCE;
2759 nt_devmode->printquality = DMRES_HIGH; /* 0x0258 */
2760 nt_devmode->color = DMRES_MONOCHROME;
2761 nt_devmode->duplex = DMDUP_SIMPLEX;
2762 nt_devmode->yresolution = 0;
2763 nt_devmode->ttoption = DMTT_SUBDEV;
2764 nt_devmode->collate = DMCOLLATE_FALSE;
2765 nt_devmode->icmmethod = 0;
2766 nt_devmode->icmintent = 0;
2767 nt_devmode->mediatype = 0;
2768 nt_devmode->dithertype = 0;
2770 /* non utilisés par un driver d'imprimante */
2771 nt_devmode->logpixels = 0;
2772 nt_devmode->bitsperpel = 0;
2773 nt_devmode->pelswidth = 0;
2774 nt_devmode->pelsheight = 0;
2775 nt_devmode->displayflags = 0;
2776 nt_devmode->displayfrequency = 0;
2777 nt_devmode->reserved1 = 0;
2778 nt_devmode->reserved2 = 0;
2779 nt_devmode->panningwidth = 0;
2780 nt_devmode->panningheight = 0;
2782 nt_devmode->nt_dev_private = NULL;
2786 /****************************************************************************
2787 Clean up and deallocate a (maybe partially) allocated NT_DEVICEMODE.
2788 ****************************************************************************/
2790 void free_nt_devicemode(NT_DEVICEMODE **devmode_ptr)
2792 NT_DEVICEMODE *nt_devmode = *devmode_ptr;
2794 if(nt_devmode == NULL)
2797 DEBUG(106,("free_nt_devicemode: deleting DEVMODE\n"));
2799 SAFE_FREE(nt_devmode->nt_dev_private);
2800 SAFE_FREE(*devmode_ptr);
2803 /****************************************************************************
2804 Clean up and deallocate a (maybe partially) allocated NT_PRINTER_INFO_LEVEL_2.
2805 ****************************************************************************/
2807 static void free_nt_printer_info_level_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr)
2809 NT_PRINTER_INFO_LEVEL_2 *info = *info_ptr;
2814 free_nt_devicemode(&info->devmode);
2816 TALLOC_FREE( *info_ptr );
2820 /****************************************************************************
2821 ****************************************************************************/
2822 int unpack_devicemode(NT_DEVICEMODE **nt_devmode, const uint8 *buf, int buflen)
2826 NT_DEVICEMODE devmode;
2828 ZERO_STRUCT(devmode);
2830 len += tdb_unpack(buf+len, buflen-len, "p", nt_devmode);
2832 if (!*nt_devmode) return len;
2834 len += tdb_unpack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2838 &devmode.specversion,
2839 &devmode.driverversion,
2841 &devmode.driverextra,
2842 &devmode.orientation,
2844 &devmode.paperlength,
2845 &devmode.paperwidth,
2848 &devmode.defaultsource,
2849 &devmode.printquality,
2852 &devmode.yresolution,
2858 &devmode.bitsperpel,
2860 &devmode.pelsheight,
2861 &devmode.displayflags,
2862 &devmode.displayfrequency,
2866 &devmode.dithertype,
2869 &devmode.panningwidth,
2870 &devmode.panningheight,
2871 &devmode.nt_dev_private);
2873 if (devmode.nt_dev_private) {
2874 /* the len in tdb_unpack is an int value and
2875 * devmode.driverextra is only a short
2877 len += tdb_unpack(buf+len, buflen-len, "B", &extra_len, &devmode.nt_dev_private);
2878 devmode.driverextra=(uint16)extra_len;
2880 /* check to catch an invalid TDB entry so we don't segfault */
2881 if (devmode.driverextra == 0) {
2882 devmode.nt_dev_private = NULL;
2886 *nt_devmode = (NT_DEVICEMODE *)memdup(&devmode, sizeof(devmode));
2888 SAFE_FREE(devmode.nt_dev_private);
2892 DEBUG(8,("Unpacked devicemode [%s](%s)\n", devmode.devicename, devmode.formname));
2893 if (devmode.nt_dev_private)
2894 DEBUG(8,("with a private section of %d bytes\n", devmode.driverextra));
2899 /****************************************************************************
2900 Allocate and initialize a new slot.
2901 ***************************************************************************/
2903 int add_new_printer_key( NT_PRINTER_DATA *data, const char *name )
2908 if ( !name || !data )
2911 /* allocate another slot in the NT_PRINTER_KEY array */
2913 if ( !(d = TALLOC_REALLOC_ARRAY( data, data->keys, NT_PRINTER_KEY, data->num_keys+1)) ) {
2914 DEBUG(0,("add_new_printer_key: Realloc() failed!\n"));
2920 key_index = data->num_keys;
2922 /* initialze new key */
2924 data->keys[key_index].name = talloc_strdup( data, name );
2926 if ( !(data->keys[key_index].values = TALLOC_ZERO_P( data, struct regval_ctr )) )
2931 DEBUG(10,("add_new_printer_key: Inserted new data key [%s]\n", name ));
2936 /****************************************************************************
2937 search for a registry key name in the existing printer data
2938 ***************************************************************************/
2940 int delete_printer_key( NT_PRINTER_DATA *data, const char *name )
2944 for ( i=0; i<data->num_keys; i++ ) {
2945 if ( strequal( data->keys[i].name, name ) ) {
2947 /* cleanup memory */
2949 TALLOC_FREE( data->keys[i].name );
2950 TALLOC_FREE( data->keys[i].values );
2952 /* if not the end of the array, move remaining elements down one slot */
2955 if ( data->num_keys && (i < data->num_keys) )
2956 memmove( &data->keys[i], &data->keys[i+1], sizeof(NT_PRINTER_KEY)*(data->num_keys-i) );
2963 return data->num_keys;
2966 /****************************************************************************
2967 search for a registry key name in the existing printer data
2968 ***************************************************************************/
2970 int lookup_printerkey( NT_PRINTER_DATA *data, const char *name )
2975 if ( !data || !name )
2978 DEBUG(12,("lookup_printerkey: Looking for [%s]\n", name));
2980 /* loop over all existing keys */
2982 for ( i=0; i<data->num_keys; i++ ) {
2983 if ( strequal(data->keys[i].name, name) ) {
2984 DEBUG(12,("lookup_printerkey: Found [%s]!\n", name));
2994 /****************************************************************************
2995 ***************************************************************************/
2997 int get_printer_subkeys( NT_PRINTER_DATA *data, const char* key, fstring **subkeys )
3001 int num_subkeys = 0;
3003 fstring *subkeys_ptr = NULL;
3014 /* special case of asking for the top level printer data registry key names */
3016 if ( strlen(key) == 0 ) {
3017 for ( i=0; i<data->num_keys; i++ ) {
3019 /* found a match, so allocate space and copy the name */
3021 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
3022 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
3027 fstrcpy( subkeys_ptr[num_subkeys], data->keys[i].name );
3034 /* asking for the subkeys of some key */
3035 /* subkey paths are stored in the key name using '\' as the delimiter */
3037 for ( i=0; i<data->num_keys; i++ ) {
3038 if ( StrnCaseCmp(data->keys[i].name, key, strlen(key)) == 0 ) {
3040 /* if we found the exact key, then break */
3041 key_len = strlen( key );
3042 if ( strlen(data->keys[i].name) == key_len )
3045 /* get subkey path */
3047 p = data->keys[i].name + key_len;
3050 fstrcpy( subkeyname, p );
3051 if ( (p = strchr( subkeyname, '\\' )) )
3054 /* don't add a key more than once */
3056 for ( j=0; j<num_subkeys; j++ ) {
3057 if ( strequal( subkeys_ptr[j], subkeyname ) )
3061 if ( j != num_subkeys )
3064 /* found a match, so allocate space and copy the name */
3066 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
3067 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
3072 fstrcpy( subkeys_ptr[num_subkeys], subkeyname );
3078 /* return error if the key was not found */
3080 if ( i == data->num_keys ) {
3081 SAFE_FREE(subkeys_ptr);
3086 /* tag off the end */
3089 fstrcpy(subkeys_ptr[num_subkeys], "" );
3091 *subkeys = subkeys_ptr;
3097 static void map_sz_into_ctr(struct regval_ctr *ctr, const char *val_name,
3100 regval_ctr_delvalue(ctr, val_name);
3101 regval_ctr_addvalue_sz(ctr, val_name, sz);
3104 static void map_dword_into_ctr(struct regval_ctr *ctr, const char *val_name,
3107 regval_ctr_delvalue(ctr, val_name);
3108 regval_ctr_addvalue(ctr, val_name, REG_DWORD,
3109 (char *) &dword, sizeof(dword));
3112 static void map_bool_into_ctr(struct regval_ctr *ctr, const char *val_name,
3115 uint8 bin_bool = (b ? 1 : 0);
3116 regval_ctr_delvalue(ctr, val_name);
3117 regval_ctr_addvalue(ctr, val_name, REG_BINARY,
3118 (char *) &bin_bool, sizeof(bin_bool));
3121 static void map_single_multi_sz_into_ctr(struct regval_ctr *ctr, const char *val_name,
3122 const char *multi_sz)
3129 regval_ctr_delvalue(ctr, val_name);
3130 regval_ctr_addvalue_multi_sz(ctr, val_name, a);
3133 /****************************************************************************
3134 * Map the NT_PRINTER_INFO_LEVEL_2 data into DsSpooler keys for publishing.
3136 * @param info2 NT_PRINTER_INFO_LEVEL_2 describing printer - gets modified
3137 * @return bool indicating success or failure
3138 ***************************************************************************/
3140 static bool map_nt_printer_info2_to_dsspooler(NT_PRINTER_INFO_LEVEL_2 *info2)
3142 struct regval_ctr *ctr = NULL;
3144 const char *dnssuffix;
3145 char *allocated_string = NULL;
3146 const char *ascii_str;
3149 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3150 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3151 ctr = info2->data->keys[i].values;
3153 map_sz_into_ctr(ctr, SPOOL_REG_PRINTERNAME, info2->sharename);
3154 map_sz_into_ctr(ctr, SPOOL_REG_SHORTSERVERNAME, global_myname());
3156 /* we make the assumption that the netbios name is the same
3157 as the DNS name sinc ethe former will be what we used to
3160 dnssuffix = get_mydnsdomname(talloc_tos());
3161 if (dnssuffix && *dnssuffix) {
3162 fstr_sprintf( longname, "%s.%s", global_myname(), dnssuffix );
3164 fstrcpy( longname, global_myname() );
3167 map_sz_into_ctr(ctr, SPOOL_REG_SERVERNAME, longname);
3169 if (asprintf(&allocated_string, "\\\\%s\\%s", longname, info2->sharename) == -1) {
3172 map_sz_into_ctr(ctr, SPOOL_REG_UNCNAME, allocated_string);
3173 SAFE_FREE(allocated_string);
3175 map_dword_into_ctr(ctr, SPOOL_REG_VERSIONNUMBER, 4);
3176 map_sz_into_ctr(ctr, SPOOL_REG_DRIVERNAME, info2->drivername);
3177 map_sz_into_ctr(ctr, SPOOL_REG_LOCATION, info2->location);
3178 map_sz_into_ctr(ctr, SPOOL_REG_DESCRIPTION, info2->comment);
3179 map_single_multi_sz_into_ctr(ctr, SPOOL_REG_PORTNAME, info2->portname);
3180 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSEPARATORFILE, info2->sepfile);
3181 map_dword_into_ctr(ctr, SPOOL_REG_PRINTSTARTTIME, info2->starttime);
3182 map_dword_into_ctr(ctr, SPOOL_REG_PRINTENDTIME, info2->untiltime);
3183 map_dword_into_ctr(ctr, SPOOL_REG_PRIORITY, info2->priority);
3185 map_bool_into_ctr(ctr, SPOOL_REG_PRINTKEEPPRINTEDJOBS,
3186 (info2->attributes &
3187 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
3189 switch (info2->attributes & 0x3) {
3191 ascii_str = SPOOL_REGVAL_PRINTWHILESPOOLING;
3194 ascii_str = SPOOL_REGVAL_PRINTAFTERSPOOLED;
3197 ascii_str = SPOOL_REGVAL_PRINTDIRECT;
3200 ascii_str = "unknown";
3202 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSPOOLING, ascii_str);
3207 /*****************************************************************
3208 ****************************************************************/
3210 static void store_printer_guid(NT_PRINTER_INFO_LEVEL_2 *info2,
3214 struct regval_ctr *ctr=NULL;
3216 /* find the DsSpooler key */
3217 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3218 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3219 ctr = info2->data->keys[i].values;
3221 regval_ctr_delvalue(ctr, "objectGUID");
3223 /* We used to store this as a REG_BINARY but that causes
3226 regval_ctr_addvalue_sz(ctr, "objectGUID",
3227 GUID_string(talloc_tos(), &guid));
3230 static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
3231 NT_PRINTER_INFO_LEVEL *printer)
3235 char *prt_dn = NULL, *srv_dn, *srv_cn_0, *srv_cn_escaped, *sharename_escaped;
3236 char *srv_dn_utf8, **srv_cn_utf8;
3239 const char *attrs[] = {"objectGUID", NULL};
3241 WERROR win_rc = WERR_OK;
3242 size_t converted_size;
3244 /* build the ads mods */
3245 ctx = talloc_init("nt_printer_publish_ads");
3250 DEBUG(5, ("publishing printer %s\n", printer->info_2->printername));
3252 /* figure out where to publish */
3253 ads_find_machine_acct(ads, &res, global_myname());
3255 /* We use ldap_get_dn here as we need the answer
3256 * in utf8 to call ldap_explode_dn(). JRA. */
3258 srv_dn_utf8 = ldap_get_dn((LDAP *)ads->ldap.ld, (LDAPMessage *)res);
3261 return WERR_SERVER_UNAVAILABLE;
3263 ads_msgfree(ads, res);
3264 srv_cn_utf8 = ldap_explode_dn(srv_dn_utf8, 1);
3267 ldap_memfree(srv_dn_utf8);
3268 return WERR_SERVER_UNAVAILABLE;
3270 /* Now convert to CH_UNIX. */
3271 if (!pull_utf8_talloc(ctx, &srv_dn, srv_dn_utf8, &converted_size)) {
3273 ldap_memfree(srv_dn_utf8);
3274 ldap_memfree(srv_cn_utf8);
3275 return WERR_SERVER_UNAVAILABLE;
3277 if (!pull_utf8_talloc(ctx, &srv_cn_0, srv_cn_utf8[0], &converted_size)) {
3279 ldap_memfree(srv_dn_utf8);
3280 ldap_memfree(srv_cn_utf8);
3281 TALLOC_FREE(srv_dn);
3282 return WERR_SERVER_UNAVAILABLE;
3285 ldap_memfree(srv_dn_utf8);
3286 ldap_memfree(srv_cn_utf8);
3288 srv_cn_escaped = escape_rdn_val_string_alloc(srv_cn_0);
3289 if (!srv_cn_escaped) {
3291 return WERR_SERVER_UNAVAILABLE;
3293 sharename_escaped = escape_rdn_val_string_alloc(printer->info_2->sharename);
3294 if (!sharename_escaped) {
3295 SAFE_FREE(srv_cn_escaped);
3297 return WERR_SERVER_UNAVAILABLE;
3300 prt_dn = talloc_asprintf(ctx, "cn=%s-%s,%s", srv_cn_escaped, sharename_escaped, srv_dn);
3302 SAFE_FREE(srv_cn_escaped);
3303 SAFE_FREE(sharename_escaped);
3305 mods = ads_init_mods(ctx);
3313 get_local_printer_publishing_data(ctx, &mods, printer->info_2->data);
3314 ads_mod_str(ctx, &mods, SPOOL_REG_PRINTERNAME,
3315 printer->info_2->sharename);
3318 ads_rc = ads_mod_printer_entry(ads, prt_dn, ctx, &mods);
3319 if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT) {
3321 for (i=0; mods[i] != 0; i++)
3323 mods[i] = (LDAPMod *)-1;
3324 ads_rc = ads_add_printer_entry(ads, prt_dn, ctx, &mods);
3327 if (!ADS_ERR_OK(ads_rc))
3328 DEBUG(3, ("error publishing %s: %s\n", printer->info_2->sharename, ads_errstr(ads_rc)));
3330 /* retreive the guid and store it locally */
3331 if (ADS_ERR_OK(ads_search_dn(ads, &res, prt_dn, attrs))) {
3333 ads_pull_guid(ads, res, &guid);
3334 ads_msgfree(ads, res);
3335 store_printer_guid(printer->info_2, guid);
3336 win_rc = mod_a_printer(printer, 2);
3343 static WERROR nt_printer_unpublish_ads(ADS_STRUCT *ads,
3344 NT_PRINTER_INFO_LEVEL *printer)
3347 LDAPMessage *res = NULL;
3348 char *prt_dn = NULL;
3350 DEBUG(5, ("unpublishing printer %s\n", printer->info_2->printername));
3352 /* remove the printer from the directory */
3353 ads_rc = ads_find_printer_on_server(ads, &res,
3354 printer->info_2->sharename, global_myname());
3356 if (ADS_ERR_OK(ads_rc) && res && ads_count_replies(ads, res)) {
3357 prt_dn = ads_get_dn(ads, talloc_tos(), res);
3359 ads_msgfree(ads, res);
3362 ads_rc = ads_del_dn(ads, prt_dn);
3363 TALLOC_FREE(prt_dn);
3367 ads_msgfree(ads, res);
3372 /****************************************************************************
3373 * Publish a printer in the directory
3375 * @param snum describing printer service
3376 * @return WERROR indicating status of publishing
3377 ***************************************************************************/
3379 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3382 ADS_STRUCT *ads = NULL;
3383 NT_PRINTER_INFO_LEVEL *printer = NULL;
3386 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3387 if (!W_ERROR_IS_OK(win_rc))
3391 case DSPRINT_PUBLISH:
3392 case DSPRINT_UPDATE:
3393 /* set the DsSpooler info and attributes */
3394 if (!(map_nt_printer_info2_to_dsspooler(printer->info_2))) {
3395 win_rc = WERR_NOMEM;
3399 printer->info_2->attributes |= PRINTER_ATTRIBUTE_PUBLISHED;
3401 case DSPRINT_UNPUBLISH:
3402 printer->info_2->attributes ^= PRINTER_ATTRIBUTE_PUBLISHED;
3405 win_rc = WERR_NOT_SUPPORTED;
3409 win_rc = mod_a_printer(printer, 2);
3410 if (!W_ERROR_IS_OK(win_rc)) {
3411 DEBUG(3, ("err %d saving data\n", W_ERROR_V(win_rc)));
3415 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3417 DEBUG(3, ("ads_init() failed\n"));
3418 win_rc = WERR_SERVER_UNAVAILABLE;
3421 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3422 SAFE_FREE(ads->auth.password);
3423 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3426 /* ads_connect() will find the DC for us */
3427 ads_rc = ads_connect(ads);
3428 if (!ADS_ERR_OK(ads_rc)) {
3429 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3430 win_rc = WERR_ACCESS_DENIED;
3435 case DSPRINT_PUBLISH:
3436 case DSPRINT_UPDATE:
3437 win_rc = nt_printer_publish_ads(ads, printer);
3439 case DSPRINT_UNPUBLISH:
3440 win_rc = nt_printer_unpublish_ads(ads, printer);
3445 free_a_printer(&printer, 2);
3450 WERROR check_published_printers(void)
3453 ADS_STRUCT *ads = NULL;
3455 int n_services = lp_numservices();
3456 NT_PRINTER_INFO_LEVEL *printer = NULL;
3458 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3460 DEBUG(3, ("ads_init() failed\n"));
3461 return WERR_SERVER_UNAVAILABLE;
3463 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3464 SAFE_FREE(ads->auth.password);
3465 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3468 /* ads_connect() will find the DC for us */
3469 ads_rc = ads_connect(ads);
3470 if (!ADS_ERR_OK(ads_rc)) {
3471 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3473 ads_kdestroy("MEMORY:prtpub_cache");
3474 return WERR_ACCESS_DENIED;
3477 for (snum = 0; snum < n_services; snum++) {
3478 if (!(lp_snum_ok(snum) && lp_print_ok(snum)))
3481 if (W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2,
3482 lp_servicename(snum))) &&
3483 (printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED))
3484 nt_printer_publish_ads(ads, printer);
3486 free_a_printer(&printer, 2);
3490 ads_kdestroy("MEMORY:prtpub_cache");
3494 bool is_printer_published(Printer_entry *print_hnd, int snum,
3497 NT_PRINTER_INFO_LEVEL *printer = NULL;
3498 struct regval_ctr *ctr;
3499 struct regval_blob *guid_val;
3505 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3507 if (!W_ERROR_IS_OK(win_rc) ||
3508 !(printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED) ||
3509 ((i = lookup_printerkey(printer->info_2->data, SPOOL_DSSPOOLER_KEY)) < 0) ||
3510 !(ctr = printer->info_2->data->keys[i].values) ||
3511 !(guid_val = regval_ctr_getvalue(ctr, "objectGUID")))
3513 free_a_printer(&printer, 2);
3517 /* fetching printer guids really ought to be a separate function. */
3522 /* We used to store the guid as REG_BINARY, then swapped
3523 to REG_SZ for Vista compatibility so check for both */
3525 switch ( regval_type(guid_val) ){
3527 blob = data_blob_const(regval_data_p(guid_val),
3528 regval_size(guid_val));
3529 pull_reg_sz(talloc_tos(), &blob, (const char **)&guid_str);
3530 ret = NT_STATUS_IS_OK(GUID_from_string( guid_str, guid ));
3531 talloc_free(guid_str);
3534 if ( regval_size(guid_val) != sizeof(struct GUID) ) {
3538 memcpy(guid, regval_data_p(guid_val), sizeof(struct GUID));
3541 DEBUG(0,("is_printer_published: GUID value stored as "
3542 "invaluid type (%d)\n", regval_type(guid_val) ));
3547 free_a_printer(&printer, 2);
3551 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3556 WERROR check_published_printers(void)
3561 bool is_printer_published(Printer_entry *print_hnd, int snum,
3566 #endif /* HAVE_ADS */
3568 /****************************************************************************
3569 ***************************************************************************/
3571 WERROR delete_all_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key )
3573 NT_PRINTER_DATA *data;
3575 int removed_keys = 0;
3579 empty_slot = data->num_keys;
3582 return WERR_INVALID_PARAM;
3584 /* remove all keys */
3586 if ( !strlen(key) ) {
3588 TALLOC_FREE( data );
3592 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from printer [%s]\n",
3598 /* remove a specific key (and all subkeys) */
3600 for ( i=0; i<data->num_keys; i++ ) {
3601 if ( StrnCaseCmp( data->keys[i].name, key, strlen(key)) == 0 ) {
3602 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from key [%s]\n",
3603 data->keys[i].name));
3605 TALLOC_FREE( data->keys[i].name );
3606 TALLOC_FREE( data->keys[i].values );
3608 /* mark the slot as empty */
3610 ZERO_STRUCTP( &data->keys[i] );
3614 /* find the first empty slot */
3616 for ( i=0; i<data->num_keys; i++ ) {
3617 if ( !data->keys[i].name ) {
3624 if ( i == data->num_keys )
3625 /* nothing was removed */
3626 return WERR_INVALID_PARAM;
3628 /* move everything down */
3630 for ( i=empty_slot+1; i<data->num_keys; i++ ) {
3631 if ( data->keys[i].name ) {
3632 memcpy( &data->keys[empty_slot], &data->keys[i], sizeof(NT_PRINTER_KEY) );
3633 ZERO_STRUCTP( &data->keys[i] );
3641 data->num_keys -= removed_keys;
3643 /* sanity check to see if anything is left */
3645 if ( !data->num_keys ) {
3646 DEBUG(8,("delete_all_printer_data: No keys left for printer [%s]\n", p2->printername ));
3648 SAFE_FREE( data->keys );
3649 ZERO_STRUCTP( data );
3655 /****************************************************************************
3656 ***************************************************************************/
3658 WERROR delete_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3660 WERROR result = WERR_OK;
3663 /* we must have names on non-zero length */
3665 if ( !key || !*key|| !value || !*value )
3666 return WERR_INVALID_NAME;
3668 /* find the printer key first */
3670 key_index = lookup_printerkey( p2->data, key );
3671 if ( key_index == -1 )
3674 /* make sure the value exists so we can return the correct error code */
3676 if ( !regval_ctr_getvalue( p2->data->keys[key_index].values, value ) )
3677 return WERR_BADFILE;
3679 regval_ctr_delvalue( p2->data->keys[key_index].values, value );
3681 DEBUG(8,("delete_printer_data: Removed key => [%s], value => [%s]\n",
3687 /****************************************************************************
3688 ***************************************************************************/
3690 WERROR add_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value,
3691 uint32 type, uint8 *data, int real_len )
3693 WERROR result = WERR_OK;
3696 /* we must have names on non-zero length */
3698 if ( !key || !*key|| !value || !*value )
3699 return WERR_INVALID_NAME;
3701 /* find the printer key first */
3703 key_index = lookup_printerkey( p2->data, key );
3704 if ( key_index == -1 )
3705 key_index = add_new_printer_key( p2->data, key );
3707 if ( key_index == -1 )
3710 regval_ctr_addvalue( p2->data->keys[key_index].values, value,
3711 type, (const char *)data, real_len );
3713 DEBUG(8,("add_printer_data: Added key => [%s], value => [%s], type=> [%d], size => [%d]\n",
3714 key, value, type, real_len ));
3719 /****************************************************************************
3720 ***************************************************************************/
3722 struct regval_blob* get_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3726 if ( (key_index = lookup_printerkey( p2->data, key )) == -1 )
3729 DEBUG(8,("get_printer_data: Attempting to lookup key => [%s], value => [%s]\n",
3732 return regval_ctr_getvalue( p2->data->keys[key_index].values, value );
3735 /****************************************************************************
3736 Unpack a list of registry values frem the TDB
3737 ***************************************************************************/
3739 static int unpack_values(NT_PRINTER_DATA *printer_data, const uint8 *buf, int buflen)
3744 const char *valuename = NULL;
3745 const char *keyname = NULL;
3749 struct regval_blob *regval_p;
3752 /* add the "PrinterDriverData" key first for performance reasons */
3754 add_new_printer_key( printer_data, SPOOL_PRINTERDATA_KEY );
3756 /* loop and unpack the rest of the registry values */
3760 /* check to see if there are any more registry values */
3763 len += tdb_unpack(buf+len, buflen-len, "p", ®val_p);
3767 /* unpack the next regval */
3769 len += tdb_unpack(buf+len, buflen-len, "fdB",
3775 /* lookup for subkey names which have a type of REG_NONE */
3776 /* there's no data with this entry */
3778 if ( type == REG_NONE ) {
3779 if ( (key_index=lookup_printerkey( printer_data, string)) == -1 )
3780 add_new_printer_key( printer_data, string );
3785 * break of the keyname from the value name.
3786 * Valuenames can have embedded '\'s so be careful.
3787 * only support one level of keys. See the
3788 * "Konica Fiery S300 50C-K v1.1. enu" 2k driver.
3792 str = strchr_m( string, '\\');
3794 /* Put in "PrinterDriverData" is no key specified */
3797 keyname = SPOOL_PRINTERDATA_KEY;
3806 /* see if we need a new key */
3808 if ( (key_index=lookup_printerkey( printer_data, keyname )) == -1 )
3809 key_index = add_new_printer_key( printer_data, keyname );
3811 if ( key_index == -1 ) {
3812 DEBUG(0,("unpack_values: Failed to allocate a new key [%s]!\n",
3817 DEBUG(8,("specific: [%s:%s], len: %d\n", keyname, valuename, size));
3819 /* Vista doesn't like unknown REG_BINARY values in DsSpooler.
3820 Thanks to Martin Zielinski for the hint. */
3822 if ( type == REG_BINARY &&
3823 strequal( keyname, SPOOL_DSSPOOLER_KEY ) &&
3824 strequal( valuename, "objectGUID" ) )
3828 /* convert the GUID to a UNICODE string */
3830 memcpy( &guid, data_p, sizeof(struct GUID) );
3832 regval_ctr_addvalue_sz(printer_data->keys[key_index].values,
3834 GUID_string(talloc_tos(), &guid));
3839 regval_ctr_addvalue( printer_data->keys[key_index].values,
3840 valuename, type, (const char *)data_p,
3844 SAFE_FREE(data_p); /* 'B' option to tdbpack does a malloc() */
3851 /****************************************************************************
3852 ***************************************************************************/
3854 static char *last_from;
3855 static char *last_to;
3857 static const char *get_last_from(void)
3865 static const char *get_last_to(void)
3873 static bool set_last_from_to(const char *from, const char *to)
3875 char *orig_from = last_from;
3876 char *orig_to = last_to;
3878 last_from = SMB_STRDUP(from);
3879 last_to = SMB_STRDUP(to);
3881 SAFE_FREE(orig_from);
3884 if (!last_from || !last_to) {
3885 SAFE_FREE(last_from);
3892 static void map_to_os2_driver(fstring drivername)
3894 char *mapfile = lp_os2_driver_map();
3895 char **lines = NULL;
3899 if (!strlen(drivername))
3905 if (strequal(drivername,get_last_from())) {
3906 DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",
3907 drivername,get_last_to()));
3908 fstrcpy(drivername,get_last_to());
3912 lines = file_lines_load(mapfile, &numlines,0,NULL);
3913 if (numlines == 0 || lines == NULL) {
3914 DEBUG(0,("No entries in OS/2 driver map %s\n",mapfile));
3919 DEBUG(4,("Scanning OS/2 driver map %s\n",mapfile));
3921 for( i = 0; i < numlines; i++) {
3922 char *nt_name = lines[i];
3923 char *os2_name = strchr(nt_name,'=');
3930 while (isspace(*nt_name))
3933 if (!*nt_name || strchr("#;",*nt_name))
3937 int l = strlen(nt_name);
3938 while (l && isspace(nt_name[l-1])) {
3944 while (isspace(*os2_name))
3948 int l = strlen(os2_name);
3949 while (l && isspace(os2_name[l-1])) {
3955 if (strequal(nt_name,drivername)) {
3956 DEBUG(3,("Mapped windows driver %s to os2 driver%s\n",drivername,os2_name));
3957 set_last_from_to(drivername,os2_name);
3958 fstrcpy(drivername,os2_name);
3967 /****************************************************************************
3968 Get a default printer info 2 struct.
3969 ****************************************************************************/
3971 static WERROR get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 *info,
3972 const char *servername,
3973 const char* sharename,
3976 int snum = lp_servicenumber(sharename);
3978 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
3979 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
3980 servername, sharename);
3981 fstrcpy(info->sharename, sharename);
3982 fstrcpy(info->portname, SAMBA_PRINTER_PORT_NAME);
3984 /* by setting the driver name to an empty string, a local NT admin
3985 can now run the **local** APW to install a local printer driver
3986 for a Samba shared printer in 2.2. Without this, drivers **must** be
3987 installed on the Samba server for NT clients --jerry */
3988 #if 0 /* JERRY --do not uncomment-- */
3989 if (!*info->drivername)
3990 fstrcpy(info->drivername, "NO DRIVER AVAILABLE FOR THIS PRINTER");
3994 DEBUG(10,("get_a_printer_2_default: driver name set to [%s]\n", info->drivername));
3996 strlcpy(info->comment, "", sizeof(info->comment));
3997 fstrcpy(info->printprocessor, "winprint");
3998 fstrcpy(info->datatype, "RAW");
4001 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
4002 /* Pull the location and comment strings from cups if we don't
4004 if ( !strlen(info->location) || !strlen(info->comment) ) {
4005 char *comment = NULL;
4006 char *location = NULL;
4007 if (cups_pull_comment_location(info, info->sharename,
4008 &comment, &location)) {
4009 strlcpy(info->comment, comment, sizeof(info->comment));
4010 fstrcpy(info->location, location);
4011 TALLOC_FREE(comment);
4012 TALLOC_FREE(location);
4018 info->attributes = PRINTER_ATTRIBUTE_SAMBA;
4020 info->starttime = 0; /* Minutes since 12:00am GMT */
4021 info->untiltime = 0; /* Minutes since 12:00am GMT */
4023 info->default_priority = 1;
4024 info->setuptime = (uint32)time(NULL);
4027 * I changed this as I think it is better to have a generic
4028 * DEVMODE than to crash Win2k explorer.exe --jerry
4029 * See the HP Deskjet 990c Win2k drivers for an example.
4031 * However the default devmode appears to cause problems
4032 * with the HP CLJ 8500 PCL driver. Hence the addition of
4033 * the "default devmode" parameter --jerry 22/01/2002
4036 if (lp_default_devmode(snum)) {
4037 if ((info->devmode = construct_nt_devicemode(info->printername)) == NULL) {
4041 info->devmode = NULL;
4044 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
4048 info->data = TALLOC_ZERO_P(info, NT_PRINTER_DATA);
4053 add_new_printer_key(info->data, SPOOL_PRINTERDATA_KEY);
4059 free_nt_devicemode(&info->devmode);
4061 return WERR_ACCESS_DENIED;
4064 /****************************************************************************
4065 ****************************************************************************/
4067 static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info,
4068 const char *servername,
4069 const char *sharename,
4073 int snum = lp_servicenumber(sharename);
4074 TDB_DATA kbuf, dbuf;
4075 fstring printername;
4076 char adevice[MAXDEVICENAME];
4077 char *comment = NULL;
4079 kbuf = make_printer_tdbkey(talloc_tos(), sharename);
4081 dbuf = tdb_fetch(tdb_printers, kbuf);
4083 return get_a_printer_2_default(info, servername,
4084 sharename, get_loc_com);
4087 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "dddddddddddfffffPfffff",
4090 &info->default_priority,
4097 &info->c_setprinter,
4107 info->printprocessor,
4112 strlcpy(info->comment, comment, sizeof(info->comment));
4116 /* Samba has to have shared raw drivers. */
4117 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
4118 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
4120 /* Restore the stripped strings. */
4121 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
4123 if ( lp_force_printername(snum) ) {
4124 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, sharename );
4126 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, info->printername);
4129 fstrcpy(info->printername, printername);
4132 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
4133 /* Pull the location and comment strings from cups if we don't
4135 if ( !strlen(info->location) || !strlen(info->comment) ) {
4136 char *location = NULL;
4138 if (cups_pull_comment_location(info, info->sharename,
4139 &comment, &location)) {
4140 strlcpy(info->comment, comment, sizeof(info->comment));
4141 fstrcpy(info->location, location);
4142 TALLOC_FREE(comment);
4143 TALLOC_FREE(location);
4149 len += unpack_devicemode(&info->devmode,dbuf.dptr+len, dbuf.dsize-len);
4152 * Some client drivers freak out if there is a NULL devmode
4153 * (probably the driver is not checking before accessing
4154 * the devmode pointer) --jerry
4156 * See comments in get_a_printer_2_default()
4159 if (lp_default_devmode(snum) && !info->devmode) {
4160 DEBUG(8,("get_a_printer_2: Constructing a default device mode for [%s]\n",
4162 info->devmode = construct_nt_devicemode(printername);
4165 slprintf( adevice, sizeof(adevice), "%s", info->printername );
4166 if (info->devmode) {
4167 fstrcpy(info->devmode->devicename, adevice);
4170 if ( !(info->data = TALLOC_ZERO_P( info, NT_PRINTER_DATA )) ) {
4171 DEBUG(0,("unpack_values: talloc() failed!\n"));
4172 SAFE_FREE(dbuf.dptr);
4175 len += unpack_values( info->data, dbuf.dptr+len, dbuf.dsize-len );
4177 /* This will get the current RPC talloc context, but we should be
4178 passing this as a parameter... fixme... JRA ! */
4180 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
4181 SAFE_FREE(dbuf.dptr);
4185 /* Fix for OS/2 drivers. */
4187 if (get_remote_arch() == RA_OS2) {
4188 map_to_os2_driver(info->drivername);
4191 SAFE_FREE(dbuf.dptr);
4193 DEBUG(9,("Unpacked printer [%s] name [%s] running driver [%s]\n",
4194 sharename, info->printername, info->drivername));
4199 /****************************************************************************
4200 Debugging function, dump at level 6 the struct in the logs.
4201 ****************************************************************************/
4202 static uint32 dump_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4205 NT_PRINTER_INFO_LEVEL_2 *info2;
4207 DEBUG(106,("Dumping printer at level [%d]\n", level));
4212 if (printer->info_2 == NULL)
4216 info2=printer->info_2;
4218 DEBUGADD(106,("attributes:[%d]\n", info2->attributes));
4219 DEBUGADD(106,("priority:[%d]\n", info2->priority));
4220 DEBUGADD(106,("default_priority:[%d]\n", info2->default_priority));
4221 DEBUGADD(106,("starttime:[%d]\n", info2->starttime));
4222 DEBUGADD(106,("untiltime:[%d]\n", info2->untiltime));
4223 DEBUGADD(106,("status:[%d]\n", info2->status));
4224 DEBUGADD(106,("cjobs:[%d]\n", info2->cjobs));
4225 DEBUGADD(106,("averageppm:[%d]\n", info2->averageppm));
4226 DEBUGADD(106,("changeid:[%d]\n", info2->changeid));
4227 DEBUGADD(106,("c_setprinter:[%d]\n", info2->c_setprinter));
4228 DEBUGADD(106,("setuptime:[%d]\n", info2->setuptime));
4230 DEBUGADD(106,("servername:[%s]\n", info2->servername));
4231 DEBUGADD(106,("printername:[%s]\n", info2->printername));
4232 DEBUGADD(106,("sharename:[%s]\n", info2->sharename));
4233 DEBUGADD(106,("portname:[%s]\n", info2->portname));
4234 DEBUGADD(106,("drivername:[%s]\n", info2->drivername));
4235 DEBUGADD(106,("comment:[%s]\n", info2->comment));
4236 DEBUGADD(106,("location:[%s]\n", info2->location));
4237 DEBUGADD(106,("sepfile:[%s]\n", info2->sepfile));
4238 DEBUGADD(106,("printprocessor:[%s]\n", info2->printprocessor));
4239 DEBUGADD(106,("datatype:[%s]\n", info2->datatype));
4240 DEBUGADD(106,("parameters:[%s]\n", info2->parameters));
4246 DEBUGADD(106,("dump_a_printer: Level %u not implemented\n", (unsigned int)level ));
4254 /****************************************************************************
4255 Update the changeid time.
4256 This is SO NASTY as some drivers need this to change, others need it
4257 static. This value will change every second, and I must hope that this
4258 is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
4260 ****************************************************************************/
4262 static uint32 rev_changeid(void)
4266 get_process_uptime(&tv);
4269 /* Return changeid as msec since spooler restart */
4270 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
4273 * This setting seems to work well but is too untested
4274 * to replace the above calculation. Left in for experiementation
4275 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
4277 return tv.tv_sec * 10 + tv.tv_usec / 100000;
4283 * The function below are the high level ones.
4284 * only those ones must be called from the spoolss code.
4288 /****************************************************************************
4289 Modify a printer. This is called from SETPRINTERDATA/DELETEPRINTERDATA.
4290 ****************************************************************************/
4292 WERROR mod_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4296 dump_a_printer(printer, level);
4302 * Update the changestamp. Emperical tests show that the
4303 * ChangeID is always updated,but c_setprinter is
4304 * global spooler variable (not per printer).
4307 /* ChangeID **must** be increasing over the lifetime
4308 of client's spoolss service in order for the
4309 client's cache to show updates */
4311 printer->info_2->changeid = rev_changeid();
4314 * Because one day someone will ask:
4315 * NT->NT An admin connection to a remote
4316 * printer show changes imeediately in
4317 * the properities dialog
4319 * A non-admin connection will only show the
4320 * changes after viewing the properites page
4321 * 2 times. Seems to be related to a
4322 * race condition in the client between the spooler
4323 * updating the local cache and the Explorer.exe GUI
4324 * actually displaying the properties.
4326 * This is fixed in Win2k. admin/non-admin
4327 * connections both display changes immediately.
4332 result=update_a_printer_2(printer->info_2);
4336 result=WERR_UNKNOWN_LEVEL;
4343 /****************************************************************************
4344 Initialize printer devmode & data with previously saved driver init values.
4345 ****************************************************************************/
4347 static bool set_driver_init_2( NT_PRINTER_INFO_LEVEL_2 *info_ptr )
4352 NT_PRINTER_INFO_LEVEL_2 info;
4358 * Delete any printer data 'values' already set. When called for driver
4359 * replace, there will generally be some, but during an add printer, there
4360 * should not be any (if there are delete them).
4363 if ( info_ptr->data )
4364 delete_all_printer_data( info_ptr, "" );
4366 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX,
4367 info_ptr->drivername) < 0) {
4371 dbuf = tdb_fetch_bystring(tdb_drivers, key);
4374 * When changing to a driver that has no init info in the tdb, remove
4375 * the previous drivers init info and leave the new on blank.
4377 free_nt_devicemode(&info_ptr->devmode);
4384 * Get the saved DEVMODE..
4387 len += unpack_devicemode(&info.devmode,dbuf.dptr+len, dbuf.dsize-len);
4390 * The saved DEVMODE contains the devicename from the printer used during
4391 * the initialization save. Change it to reflect the new printer.
4394 if ( info.devmode ) {
4395 ZERO_STRUCT(info.devmode->devicename);
4396 fstrcpy(info.devmode->devicename, info_ptr->printername);
4400 * NT/2k does not change out the entire DeviceMode of a printer
4401 * when changing the driver. Only the driverextra, private, &
4402 * driverversion fields. --jerry (Thu Mar 14 08:58:43 CST 2002)
4404 * Later examination revealed that Windows NT/2k does reset the
4405 * the printer's device mode, bit **only** when you change a
4406 * property of the device mode such as the page orientation.
4411 /* Bind the saved DEVMODE to the new the printer */
4413 free_nt_devicemode(&info_ptr->devmode);
4414 info_ptr->devmode = info.devmode;
4416 DEBUG(10,("set_driver_init_2: Set printer [%s] init %s DEVMODE for driver [%s]\n",
4417 info_ptr->printername, info_ptr->devmode?"VALID":"NULL", info_ptr->drivername));
4419 /* Add the printer data 'values' to the new printer */
4421 if ( !(info_ptr->data = TALLOC_ZERO_P( info_ptr, NT_PRINTER_DATA )) ) {
4422 DEBUG(0,("set_driver_init_2: talloc() failed!\n"));
4426 len += unpack_values( info_ptr->data, dbuf.dptr+len, dbuf.dsize-len );
4428 SAFE_FREE(dbuf.dptr);
4433 /****************************************************************************
4434 Initialize printer devmode & data with previously saved driver init values.
4435 When a printer is created using AddPrinter, the drivername bound to the
4436 printer is used to lookup previously saved driver initialization info, which
4437 is bound to the new printer.
4438 ****************************************************************************/
4440 bool set_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4442 bool result = False;
4446 result = set_driver_init_2(printer->info_2);
4450 DEBUG(0,("set_driver_init: Programmer's error! Unknown driver_init level [%d]\n",
4458 /****************************************************************************
4459 Delete driver init data stored for a specified driver
4460 ****************************************************************************/
4462 bool del_driver_init(const char *drivername)
4467 if (!drivername || !*drivername) {
4468 DEBUG(3,("del_driver_init: No drivername specified!\n"));
4472 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, drivername) < 0) {
4476 DEBUG(6,("del_driver_init: Removing driver init data for [%s]\n",
4479 ret = (tdb_delete_bystring(tdb_drivers, key) == 0);
4484 /****************************************************************************
4485 Pack up the DEVMODE and values for a printer into a 'driver init' entry
4486 in the tdb. Note: this is different from the driver entry and the printer
4487 entry. There should be a single driver init entry for each driver regardless
4488 of whether it was installed from NT or 2K. Technically, they should be
4489 different, but they work out to the same struct.
4490 ****************************************************************************/
4492 static uint32 update_driver_init_2(NT_PRINTER_INFO_LEVEL_2 *info)
4496 int buflen, len, ret;
4505 len += pack_devicemode(info->devmode, buf+len, buflen-len);
4507 retlen = pack_values( info->data, buf+len, buflen-len );
4515 buf = (uint8 *)SMB_REALLOC(buf, len);
4517 DEBUG(0, ("update_driver_init_2: failed to enlarge buffer!\n"));
4526 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, info->drivername) < 0) {
4534 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
4538 DEBUG(8, ("update_driver_init_2: error updating printer init to tdb on disk\n"));
4542 DEBUG(10,("update_driver_init_2: Saved printer [%s] init DEVMODE & values for driver [%s]\n",
4543 info->sharename, info->drivername));
4548 /****************************************************************************
4549 Update (i.e. save) the driver init info (DEVMODE and values) for a printer
4550 ****************************************************************************/
4552 static uint32 update_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4556 dump_a_printer(printer, level);
4560 result = update_driver_init_2(printer->info_2);
4570 /****************************************************************************
4571 Convert the printer data value, a REG_BINARY array, into an initialization
4572 DEVMODE. Note: the array must be parsed as if it was a DEVMODE in an rpc...
4573 got to keep the endians happy :).
4574 ****************************************************************************/
4576 static bool convert_driver_init(TALLOC_CTX *mem_ctx, NT_DEVICEMODE *nt_devmode,
4577 const uint8_t *data, uint32_t data_len)
4579 struct spoolss_DeviceMode devmode;
4580 enum ndr_err_code ndr_err;
4583 ZERO_STRUCT(devmode);
4585 blob = data_blob_const(data, data_len);
4587 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &devmode,
4588 (ndr_pull_flags_fn_t)ndr_pull_spoolss_DeviceMode);
4589 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
4590 DEBUG(10,("convert_driver_init: error parsing spoolss_DeviceMode\n"));
4594 return convert_devicemode("", &devmode, &nt_devmode);
4597 /****************************************************************************
4598 Set the DRIVER_INIT info in the tdb. Requires Win32 client code that:
4600 1. Use the driver's config DLL to this UNC printername and:
4601 a. Call DrvPrintEvent with PRINTER_EVENT_INITIALIZE
4602 b. Call DrvConvertDevMode with CDM_DRIVER_DEFAULT to get default DEVMODE
4603 2. Call SetPrinterData with the 'magic' key and the DEVMODE as data.
4605 The last step triggers saving the "driver initialization" information for
4606 this printer into the tdb. Later, new printers that use this driver will
4607 have this initialization information bound to them. This simulates the
4608 driver initialization, as if it had run on the Samba server (as it would
4611 The Win32 client side code requirement sucks! But until we can run arbitrary
4612 Win32 printer driver code on any Unix that Samba runs on, we are stuck with it.
4614 It would have been easier to use SetPrinter because all the UNMARSHALLING of
4615 the DEVMODE is done there, but 2K/XP clients do not set the DEVMODE... think
4616 about it and you will realize why. JRR 010720
4617 ****************************************************************************/
4619 static WERROR save_driver_init_2(NT_PRINTER_INFO_LEVEL *printer, uint8 *data, uint32 data_len )
4621 WERROR status = WERR_OK;
4622 TALLOC_CTX *ctx = NULL;
4623 NT_DEVICEMODE *nt_devmode = NULL;
4624 NT_DEVICEMODE *tmp_devmode = printer->info_2->devmode;
4627 * When the DEVMODE is already set on the printer, don't try to unpack it.
4629 DEBUG(8,("save_driver_init_2: Enter...\n"));
4631 if ( !printer->info_2->devmode && data_len ) {
4633 * Set devmode on printer info, so entire printer initialization can be
4637 if ((ctx = talloc_init("save_driver_init_2")) == NULL)
4640 if ((nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE)) == NULL) {
4641 status = WERR_NOMEM;
4645 ZERO_STRUCTP(nt_devmode);
4648 * The DEVMODE is held in the 'data' component of the param in raw binary.
4649 * Convert it to to a devmode structure
4651 if ( !convert_driver_init( ctx, nt_devmode, data, data_len )) {
4652 DEBUG(10,("save_driver_init_2: error converting DEVMODE\n"));
4653 status = WERR_INVALID_PARAM;
4657 printer->info_2->devmode = nt_devmode;
4661 * Pack up and add (or update) the DEVMODE and any current printer data to
4662 * a 'driver init' element in the tdb
4666 if ( update_driver_init(printer, 2) != 0 ) {
4667 DEBUG(10,("save_driver_init_2: error updating DEVMODE\n"));
4668 status = WERR_NOMEM;
4673 * If driver initialization info was successfully saved, set the current
4674 * printer to match it. This allows initialization of the current printer
4675 * as well as the driver.
4677 status = mod_a_printer(printer, 2);
4678 if (!W_ERROR_IS_OK(status)) {
4679 DEBUG(10,("save_driver_init_2: error setting DEVMODE on printer [%s]\n",
4680 printer->info_2->printername));
4684 talloc_destroy(ctx);
4685 free_nt_devicemode( &nt_devmode );
4687 printer->info_2->devmode = tmp_devmode;
4692 /****************************************************************************
4693 Update the driver init info (DEVMODE and specifics) for a printer
4694 ****************************************************************************/
4696 WERROR save_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level, uint8 *data, uint32 data_len)
4698 WERROR status = WERR_OK;
4702 status = save_driver_init_2( printer, data, data_len );
4705 status = WERR_UNKNOWN_LEVEL;
4712 /****************************************************************************
4713 Get a NT_PRINTER_INFO_LEVEL struct. It returns malloced memory.
4715 Previously the code had a memory allocation problem because it always
4716 used the TALLOC_CTX from the Printer_entry*. This context lasts
4717 as a long as the original handle is open. So if the client made a lot
4718 of getprinter[data]() calls, the memory usage would climb. Now we use
4719 a short lived TALLOC_CTX for printer_info_2 objects returned. We
4720 still use the Printer_entry->ctx for maintaining the cache copy though
4721 since that object must live as long as the handle by definition.
4724 ****************************************************************************/
4726 static WERROR get_a_printer_internal( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level,
4727 const char *sharename, bool get_loc_com)
4732 DEBUG(10,("get_a_printer: [%s] level %u\n", sharename, (unsigned int)level));
4734 if ( !(*pp_printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
4735 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4741 if ( !((*pp_printer)->info_2 = TALLOC_ZERO_P(*pp_printer, NT_PRINTER_INFO_LEVEL_2)) ) {
4742 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4743 TALLOC_FREE( *pp_printer );
4748 fstrcpy( servername, print_hnd->servername );
4750 fstrcpy( servername, "%L" );
4751 standard_sub_basic( "", "", servername,
4752 sizeof(servername)-1 );
4755 result = get_a_printer_2( (*pp_printer)->info_2,
4756 servername, sharename, get_loc_com);
4758 /* we have a new printer now. Save it with this handle */
4760 if ( !W_ERROR_IS_OK(result) ) {
4761 TALLOC_FREE( *pp_printer );
4762 DEBUG(10,("get_a_printer: [%s] level %u returning %s\n",
4763 sharename, (unsigned int)level, win_errstr(result)));
4767 dump_a_printer( *pp_printer, level);
4772 TALLOC_FREE( *pp_printer );
4773 return WERR_UNKNOWN_LEVEL;
4779 WERROR get_a_printer( Printer_entry *print_hnd,
4780 NT_PRINTER_INFO_LEVEL **pp_printer,
4782 const char *sharename)
4784 return get_a_printer_internal(print_hnd, pp_printer, level,
4788 WERROR get_a_printer_search( Printer_entry *print_hnd,
4789 NT_PRINTER_INFO_LEVEL **pp_printer,
4791 const char *sharename)
4793 return get_a_printer_internal(print_hnd, pp_printer, level,
4797 /****************************************************************************
4798 Deletes a NT_PRINTER_INFO_LEVEL struct.
4799 ****************************************************************************/
4801 uint32 free_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level)
4803 NT_PRINTER_INFO_LEVEL *printer = *pp_printer;
4810 if ( printer->info_2 )
4811 free_nt_printer_info_level_2(&printer->info_2);
4815 DEBUG(0,("free_a_printer: unknown level! [%d]\n", level ));
4819 TALLOC_FREE(*pp_printer);
4824 /****************************************************************************
4825 ****************************************************************************/
4827 bool driver_info_ctr_to_info8(struct spoolss_AddDriverInfoCtr *r,
4828 struct spoolss_DriverInfo8 *_info8)
4830 struct spoolss_DriverInfo8 info8;
4836 info8.version = r->info.info3->version;
4837 info8.driver_name = r->info.info3->driver_name;
4838 info8.architecture = r->info.info3->architecture;
4839 info8.driver_path = r->info.info3->driver_path;
4840 info8.data_file = r->info.info3->data_file;
4841 info8.config_file = r->info.info3->config_file;
4842 info8.help_file = r->info.info3->help_file;
4843 info8.monitor_name = r->info.info3->monitor_name;
4844 info8.default_datatype = r->info.info3->default_datatype;
4845 if (r->info.info3->dependent_files && r->info.info3->dependent_files->string) {
4846 info8.dependent_files = r->info.info3->dependent_files->string;
4850 info8.version = r->info.info6->version;
4851 info8.driver_name = r->info.info6->driver_name;
4852 info8.architecture = r->info.info6->architecture;
4853 info8.driver_path = r->info.info6->driver_path;
4854 info8.data_file = r->info.info6->data_file;
4855 info8.config_file = r->info.info6->config_file;
4856 info8.help_file = r->info.info6->help_file;
4857 info8.monitor_name = r->info.info6->monitor_name;
4858 info8.default_datatype = r->info.info6->default_datatype;
4859 if (r->info.info6->dependent_files && r->info.info6->dependent_files->string) {
4860 info8.dependent_files = r->info.info6->dependent_files->string;
4862 info8.driver_date = r->info.info6->driver_date;
4863 info8.driver_version = r->info.info6->driver_version;
4864 info8.manufacturer_name = r->info.info6->manufacturer_name;
4865 info8.manufacturer_url = r->info.info6->manufacturer_url;
4866 info8.hardware_id = r->info.info6->hardware_id;
4867 info8.provider = r->info.info6->provider;
4870 info8.version = r->info.info8->version;
4871 info8.driver_name = r->info.info8->driver_name;
4872 info8.architecture = r->info.info8->architecture;
4873 info8.driver_path = r->info.info8->driver_path;
4874 info8.data_file = r->info.info8->data_file;
4875 info8.config_file = r->info.info8->config_file;
4876 info8.help_file = r->info.info8->help_file;
4877 info8.monitor_name = r->info.info8->monitor_name;
4878 info8.default_datatype = r->info.info8->default_datatype;
4879 if (r->info.info8->dependent_files && r->info.info8->dependent_files->string) {
4880 info8.dependent_files = r->info.info8->dependent_files->string;
4882 if (r->info.info8->previous_names && r->info.info8->previous_names->string) {
4883 info8.previous_names = r->info.info8->previous_names->string;
4885 info8.driver_date = r->info.info8->driver_date;
4886 info8.driver_version = r->info.info8->driver_version;
4887 info8.manufacturer_name = r->info.info8->manufacturer_name;
4888 info8.manufacturer_url = r->info.info8->manufacturer_url;
4889 info8.hardware_id = r->info.info8->hardware_id;
4890 info8.provider = r->info.info8->provider;
4891 info8.print_processor = r->info.info8->print_processor;
4892 info8.vendor_setup = r->info.info8->vendor_setup;
4893 if (r->info.info8->color_profiles && r->info.info8->color_profiles->string) {
4894 info8.color_profiles = r->info.info8->color_profiles->string;
4896 info8.inf_path = r->info.info8->inf_path;
4897 info8.printer_driver_attributes = r->info.info8->printer_driver_attributes;
4898 if (r->info.info8->core_driver_dependencies && r->info.info8->core_driver_dependencies->string) {
4899 info8.core_driver_dependencies = r->info.info8->core_driver_dependencies->string;
4901 info8.min_inbox_driver_ver_date = r->info.info8->min_inbox_driver_ver_date;
4902 info8.min_inbox_driver_ver_version = r->info.info8->min_inbox_driver_ver_version;
4914 uint32_t add_a_printer_driver(TALLOC_CTX *mem_ctx,
4915 struct spoolss_AddDriverInfoCtr *r,
4919 struct spoolss_DriverInfo8 info8;
4923 DEBUG(10,("adding a printer at level [%d]\n", r->level));
4925 if (!driver_info_ctr_to_info8(r, &info8)) {
4929 *driver_name = talloc_strdup(mem_ctx, info8.driver_name);
4930 if (!*driver_name) {
4933 *version = info8.version;
4935 return add_a_printer_driver_8(&info8);
4938 /****************************************************************************
4939 ****************************************************************************/
4941 WERROR get_a_printer_driver(TALLOC_CTX *mem_ctx,
4942 struct spoolss_DriverInfo8 **driver,
4943 const char *drivername, const char *architecture,
4947 struct spoolss_DriverInfo3 info3;
4948 struct spoolss_DriverInfo8 *info8;
4952 /* Sometime we just want any version of the driver */
4954 if (version == DRIVER_ANY_VERSION) {
4955 /* look for Win2k first and then for NT4 */
4956 result = get_a_printer_driver_3(mem_ctx,
4960 if (!W_ERROR_IS_OK(result)) {
4961 result = get_a_printer_driver_3(mem_ctx,
4967 result = get_a_printer_driver_3(mem_ctx,
4974 if (!W_ERROR_IS_OK(result)) {
4978 info8 = talloc_zero(mem_ctx, struct spoolss_DriverInfo8);
4983 info8->version = info3.version;
4984 info8->driver_name = info3.driver_name;
4985 info8->architecture = info3.architecture;
4986 info8->driver_path = info3.driver_path;
4987 info8->data_file = info3.data_file;
4988 info8->config_file = info3.config_file;
4989 info8->help_file = info3.help_file;
4990 info8->dependent_files = info3.dependent_files;
4991 info8->monitor_name = info3.monitor_name;
4992 info8->default_datatype = info3.default_datatype;
4999 /****************************************************************************
5000 ****************************************************************************/
5002 uint32_t free_a_printer_driver(struct spoolss_DriverInfo8 *driver)
5004 talloc_free(driver);
5009 /****************************************************************************
5010 Determine whether or not a particular driver is currently assigned
5012 ****************************************************************************/
5014 bool printer_driver_in_use(const struct spoolss_DriverInfo8 *r)
5017 int n_services = lp_numservices();
5018 NT_PRINTER_INFO_LEVEL *printer = NULL;
5019 bool in_use = False;
5025 DEBUG(10,("printer_driver_in_use: Beginning search through ntprinters.tdb...\n"));
5027 /* loop through the printers.tdb and check for the drivername */
5029 for (snum=0; snum<n_services && !in_use; snum++) {
5030 if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
5033 if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_servicename(snum))) )
5036 if (strequal(r->driver_name, printer->info_2->drivername))
5039 free_a_printer( &printer, 2 );
5042 DEBUG(10,("printer_driver_in_use: Completed search through ntprinters.tdb...\n"));
5045 struct spoolss_DriverInfo8 *d;
5048 DEBUG(5,("printer_driver_in_use: driver \"%s\" is currently in use\n", r->driver_name));
5050 /* we can still remove the driver if there is one of
5051 "Windows NT x86" version 2 or 3 left */
5053 if (!strequal("Windows NT x86", r->architecture)) {
5054 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", DRIVER_ANY_VERSION);
5057 switch (r->version) {
5059 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", 3);
5062 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", 2);
5065 DEBUG(0,("printer_driver_in_use: ERROR! unknown driver version (%d)\n",
5067 werr = WERR_UNKNOWN_PRINTER_DRIVER;
5072 /* now check the error code */
5074 if ( W_ERROR_IS_OK(werr) ) {
5075 /* it's ok to remove the driver, we have other architctures left */
5077 free_a_printer_driver(d);
5081 /* report that the driver is not in use by default */
5087 /**********************************************************************
5088 Check to see if a ogiven file is in use by *info
5089 *********************************************************************/
5091 static bool drv_file_in_use(const char *file, const struct spoolss_DriverInfo8 *info)
5098 /* mz: skip files that are in the list but already deleted */
5099 if (!file || !file[0]) {
5103 if (strequal(file, info->driver_path))
5106 if (strequal(file, info->data_file))
5109 if (strequal(file, info->config_file))
5112 if (strequal(file, info->help_file))
5115 /* see of there are any dependent files to examine */
5117 if (!info->dependent_files)
5120 while (info->dependent_files[i] && *info->dependent_files[i]) {
5121 if (strequal(file, info->dependent_files[i]))
5130 /**********************************************************************
5131 Utility function to remove the dependent file pointed to by the
5132 input parameter from the list
5133 *********************************************************************/
5135 static void trim_dependent_file(TALLOC_CTX *mem_ctx, const char **files, int idx)
5138 /* bump everything down a slot */
5140 while (files && files[idx+1]) {
5141 files[idx] = talloc_strdup(mem_ctx, files[idx+1]);
5150 /**********************************************************************
5151 Check if any of the files used by src are also used by drv
5152 *********************************************************************/
5154 static bool trim_overlap_drv_files(TALLOC_CTX *mem_ctx,
5155 struct spoolss_DriverInfo8 *src,
5156 const struct spoolss_DriverInfo8 *drv)
5158 bool in_use = False;
5164 /* check each file. Remove it from the src structure if it overlaps */
5166 if (drv_file_in_use(src->driver_path, drv)) {
5168 DEBUG(10,("Removing driverfile [%s] from list\n", src->driver_path));
5169 src->driver_path = talloc_strdup(mem_ctx, "");
5170 if (!src->driver_path) { return false; }
5173 if (drv_file_in_use(src->data_file, drv)) {
5175 DEBUG(10,("Removing datafile [%s] from list\n", src->data_file));
5176 src->data_file = talloc_strdup(mem_ctx, "");
5177 if (!src->data_file) { return false; }
5180 if (drv_file_in_use(src->config_file, drv)) {
5182 DEBUG(10,("Removing configfile [%s] from list\n", src->config_file));
5183 src->config_file = talloc_strdup(mem_ctx, "");
5184 if (!src->config_file) { return false; }
5187 if (drv_file_in_use(src->help_file, drv)) {
5189 DEBUG(10,("Removing helpfile [%s] from list\n", src->help_file));
5190 src->help_file = talloc_strdup(mem_ctx, "");
5191 if (!src->help_file) { return false; }
5194 /* are there any dependentfiles to examine? */
5196 if (!src->dependent_files)
5199 while (src->dependent_files[i] && *src->dependent_files[i]) {
5200 if (drv_file_in_use(src->dependent_files[i], drv)) {
5202 DEBUG(10,("Removing [%s] from dependent file list\n", src->dependent_files[i]));
5203 trim_dependent_file(mem_ctx, src->dependent_files, i);
5211 /****************************************************************************
5212 Determine whether or not a particular driver files are currently being
5213 used by any other driver.
5215 Return value is True if any files were in use by other drivers
5216 and False otherwise.
5218 Upon return, *info has been modified to only contain the driver files
5219 which are not in use
5223 This needs to check all drivers to ensure that all files in use
5224 have been removed from *info, not just the ones in the first
5226 ****************************************************************************/
5228 bool printer_driver_files_in_use(TALLOC_CTX *mem_ctx,
5229 struct spoolss_DriverInfo8 *info)
5234 fstring *list = NULL;
5235 struct spoolss_DriverInfo8 *driver;
5236 bool in_use = false;
5241 version = info->version;
5243 /* loop over all driver versions */
5245 DEBUG(5,("printer_driver_files_in_use: Beginning search through ntdrivers.tdb...\n"));
5247 /* get the list of drivers */
5250 ndrivers = get_ntdrivers(&list, info->architecture, version);
5252 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
5253 ndrivers, info->architecture, version));
5255 /* check each driver for overlap in files */
5257 for (i=0; i<ndrivers; i++) {
5258 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
5262 if (!W_ERROR_IS_OK(get_a_printer_driver(talloc_tos(), &driver, list[i], info->architecture, version))) {
5267 /* check if d2 uses any files from d1 */
5268 /* only if this is a different driver than the one being deleted */
5270 if (!strequal(info->driver_name, driver->driver_name)) {
5271 if (trim_overlap_drv_files(mem_ctx, info, driver)) {
5272 /* mz: Do not instantly return -
5273 * we need to ensure this file isn't
5274 * also in use by other drivers. */
5279 free_a_printer_driver(driver);
5284 DEBUG(5,("printer_driver_files_in_use: Completed search through ntdrivers.tdb...\n"));
5289 static NTSTATUS driver_unlink_internals(connection_struct *conn,
5292 struct smb_filename *smb_fname = NULL;
5295 status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
5297 if (!NT_STATUS_IS_OK(status)) {
5301 status = unlink_internals(conn, NULL, 0, smb_fname, false);
5303 TALLOC_FREE(smb_fname);
5307 /****************************************************************************
5308 Actually delete the driver files. Make sure that
5309 printer_driver_files_in_use() return False before calling
5311 ****************************************************************************/
5313 static bool delete_driver_files(struct pipes_struct *rpc_pipe,
5314 const struct spoolss_DriverInfo8 *r)
5319 connection_struct *conn;
5322 fstring printdollar;
5323 int printdollar_snum;
5330 DEBUG(6,("delete_driver_files: deleting driver [%s] - version [%d]\n",
5331 r->driver_name, r->version));
5333 fstrcpy(printdollar, "print$");
5335 printdollar_snum = find_service(printdollar);
5336 if (printdollar_snum == -1) {
5340 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
5341 lp_pathname(printdollar_snum),
5342 rpc_pipe->server_info, &oldcwd);
5343 if (!NT_STATUS_IS_OK(nt_status)) {
5344 DEBUG(0,("delete_driver_files: create_conn_struct "
5345 "returned %s\n", nt_errstr(nt_status)));
5349 if ( !CAN_WRITE(conn) ) {
5350 DEBUG(3,("delete_driver_files: Cannot delete print driver when [print$] is read-only\n"));
5354 /* now delete the files; must strip the '\print$' string from
5357 if (r->driver_path && r->driver_path[0]) {
5358 if ((s = strchr(&r->driver_path[1], '\\')) != NULL) {
5360 DEBUG(10,("deleting driverfile [%s]\n", s));
5361 driver_unlink_internals(conn, file);
5365 if (r->config_file && r->config_file[0]) {
5366 if ((s = strchr(&r->config_file[1], '\\')) != NULL) {
5368 DEBUG(10,("deleting configfile [%s]\n", s));
5369 driver_unlink_internals(conn, file);
5373 if (r->data_file && r->data_file[0]) {
5374 if ((s = strchr(&r->data_file[1], '\\')) != NULL) {
5376 DEBUG(10,("deleting datafile [%s]\n", s));
5377 driver_unlink_internals(conn, file);
5381 if (r->help_file && r->help_file[0]) {
5382 if ((s = strchr(&r->help_file[1], '\\')) != NULL) {
5384 DEBUG(10,("deleting helpfile [%s]\n", s));
5385 driver_unlink_internals(conn, file);
5389 /* check if we are done removing files */
5391 if (r->dependent_files) {
5392 while (r->dependent_files[i] && r->dependent_files[i][0]) {
5395 /* bypass the "\print$" portion of the path */
5397 if ((p = strchr(r->dependent_files[i]+1, '\\')) != NULL) {
5399 DEBUG(10,("deleting dependent file [%s]\n", file));
5400 driver_unlink_internals(conn, file);
5412 vfs_ChDir(conn, oldcwd);
5418 /****************************************************************************
5419 Remove a printer driver from the TDB. This assumes that the the driver was
5420 previously looked up.
5421 ***************************************************************************/
5423 WERROR delete_printer_driver(struct pipes_struct *rpc_pipe,
5424 const struct spoolss_DriverInfo8 *r,
5425 uint32 version, bool delete_files )
5431 /* delete the tdb data first */
5433 arch = get_short_archi(r->architecture);
5435 return WERR_UNKNOWN_PRINTER_DRIVER;
5437 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
5438 arch, version, r->driver_name) < 0) {
5442 DEBUG(5,("delete_printer_driver: key = [%s] delete_files = %s\n",
5443 key, delete_files ? "TRUE" : "FALSE" ));
5445 /* check if the driver actually exists for this environment */
5447 dbuf = tdb_fetch_bystring( tdb_drivers, key );
5449 DEBUG(8,("delete_printer_driver: Driver unknown [%s]\n", key));
5451 return WERR_UNKNOWN_PRINTER_DRIVER;
5454 SAFE_FREE( dbuf.dptr );
5456 /* ok... the driver exists so the delete should return success */
5458 if (tdb_delete_bystring(tdb_drivers, key) == -1) {
5459 DEBUG (0,("delete_printer_driver: fail to delete %s!\n", key));
5461 return WERR_ACCESS_DENIED;
5465 * now delete any associated files if delete_files == True
5466 * even if this part failes, we return succes because the
5467 * driver doesn not exist any more
5471 delete_driver_files(rpc_pipe, r);
5473 DEBUG(5,("delete_printer_driver: driver delete successful [%s]\n", key));
5479 /****************************************************************************
5480 Store a security desc for a printer.
5481 ****************************************************************************/
5483 WERROR nt_printing_setsec(const char *sharename, struct sec_desc_buf *secdesc_ctr)
5485 struct sec_desc_buf *new_secdesc_ctr = NULL;
5486 struct sec_desc_buf *old_secdesc_ctr = NULL;
5487 TALLOC_CTX *mem_ctx = NULL;
5494 mem_ctx = talloc_init("nt_printing_setsec");
5495 if (mem_ctx == NULL)
5498 /* The old owner and group sids of the security descriptor are not
5499 present when new ACEs are added or removed by changing printer
5500 permissions through NT. If they are NULL in the new security
5501 descriptor then copy them over from the old one. */
5503 if (!secdesc_ctr->sd->owner_sid || !secdesc_ctr->sd->group_sid) {
5504 DOM_SID *owner_sid, *group_sid;
5505 struct security_acl *dacl, *sacl;
5506 struct security_descriptor *psd = NULL;
5509 if (!nt_printing_getsec(mem_ctx, sharename, &old_secdesc_ctr)) {
5510 status = WERR_NOMEM;
5514 /* Pick out correct owner and group sids */
5516 owner_sid = secdesc_ctr->sd->owner_sid ?
5517 secdesc_ctr->sd->owner_sid :
5518 old_secdesc_ctr->sd->owner_sid;
5520 group_sid = secdesc_ctr->sd->group_sid ?
5521 secdesc_ctr->sd->group_sid :
5522 old_secdesc_ctr->sd->group_sid;
5524 dacl = secdesc_ctr->sd->dacl ?
5525 secdesc_ctr->sd->dacl :
5526 old_secdesc_ctr->sd->dacl;
5528 sacl = secdesc_ctr->sd->sacl ?
5529 secdesc_ctr->sd->sacl :
5530 old_secdesc_ctr->sd->sacl;
5532 /* Make a deep copy of the security descriptor */
5534 psd = make_sec_desc(mem_ctx, secdesc_ctr->sd->revision, secdesc_ctr->sd->type,
5535 owner_sid, group_sid,
5541 status = WERR_NOMEM;
5545 new_secdesc_ctr = make_sec_desc_buf(mem_ctx, size, psd);
5548 if (!new_secdesc_ctr) {
5549 new_secdesc_ctr = secdesc_ctr;
5552 /* Store the security descriptor in a tdb */
5554 nt_status = marshall_sec_desc_buf(mem_ctx, new_secdesc_ctr,
5555 &blob.data, &blob.length);
5556 if (!NT_STATUS_IS_OK(nt_status)) {
5557 status = ntstatus_to_werror(nt_status);
5561 kbuf = make_printers_secdesc_tdbkey(mem_ctx, sharename );
5563 dbuf.dptr = (unsigned char *)blob.data;
5564 dbuf.dsize = blob.length;
5566 if (tdb_trans_store(tdb_printers, kbuf, dbuf, TDB_REPLACE)==0) {
5569 DEBUG(1,("Failed to store secdesc for %s\n", sharename));
5570 status = WERR_BADFUNC;
5573 /* Free malloc'ed memory */
5574 talloc_free(blob.data);
5579 talloc_destroy(mem_ctx);
5583 /****************************************************************************
5584 Construct a default security descriptor buffer for a printer.
5585 ****************************************************************************/
5587 static struct sec_desc_buf *construct_default_printer_sdb(TALLOC_CTX *ctx)
5589 struct security_ace ace[5]; /* max number of ace entries */
5592 struct security_acl *psa = NULL;
5593 struct sec_desc_buf *sdb = NULL;
5594 struct security_descriptor *psd = NULL;
5598 /* Create an ACE where Everyone is allowed to print */
5600 sa = PRINTER_ACE_PRINT;
5601 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
5602 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5604 /* Add the domain admins group if we are a DC */
5607 DOM_SID domadmins_sid;
5609 sid_compose(&domadmins_sid, get_global_sam_sid(),
5612 sa = PRINTER_ACE_FULL_CONTROL;
5613 init_sec_ace(&ace[i++], &domadmins_sid,
5614 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5615 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5616 init_sec_ace(&ace[i++], &domadmins_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5617 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5619 else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
5620 sid_append_rid(&adm_sid, DOMAIN_RID_ADMINISTRATOR);
5622 sa = PRINTER_ACE_FULL_CONTROL;
5623 init_sec_ace(&ace[i++], &adm_sid,
5624 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5625 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5626 init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5627 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5630 /* add BUILTIN\Administrators as FULL CONTROL */
5632 sa = PRINTER_ACE_FULL_CONTROL;
5633 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5634 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5635 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5636 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5637 SEC_ACE_TYPE_ACCESS_ALLOWED,
5638 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5640 /* Make the security descriptor owned by the BUILTIN\Administrators */
5642 /* The ACL revision number in rpc_secdesc.h differs from the one
5643 created by NT when setting ACE entries in printer
5644 descriptors. NT4 complains about the property being edited by a
5647 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) != NULL) {
5648 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
5649 &global_sid_Builtin_Administrators,
5650 &global_sid_Builtin_Administrators,
5651 NULL, psa, &sd_size);
5655 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
5659 sdb = make_sec_desc_buf(ctx, sd_size, psd);
5661 DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
5662 (unsigned int)sd_size));
5667 /****************************************************************************
5668 Get a security desc for a printer.
5669 ****************************************************************************/
5671 bool nt_printing_getsec(TALLOC_CTX *ctx, const char *sharename, struct sec_desc_buf **secdesc_ctr)
5679 if (strlen(sharename) > 2 && (temp = strchr(sharename + 2, '\\'))) {
5680 sharename = temp + 1;
5683 /* Fetch security descriptor from tdb */
5685 kbuf = make_printers_secdesc_tdbkey(ctx, sharename);
5687 dbuf = tdb_fetch(tdb_printers, kbuf);
5690 status = unmarshall_sec_desc_buf(ctx, dbuf.dptr, dbuf.dsize,
5692 SAFE_FREE(dbuf.dptr);
5694 if (NT_STATUS_IS_OK(status)) {
5699 *secdesc_ctr = construct_default_printer_sdb(ctx);
5700 if (!*secdesc_ctr) {
5704 status = marshall_sec_desc_buf(ctx, *secdesc_ctr,
5705 &blob.data, &blob.length);
5706 if (NT_STATUS_IS_OK(status)) {
5707 dbuf.dptr = (unsigned char *)blob.data;
5708 dbuf.dsize = blob.length;
5709 tdb_trans_store(tdb_printers, kbuf, dbuf, TDB_REPLACE);
5710 talloc_free(blob.data);
5713 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
5714 this security descriptor has been created when winbindd was
5715 down. Take ownership of security descriptor. */
5717 if (sid_equal((*secdesc_ctr)->sd->owner_sid, &global_sid_World)) {
5720 /* Change sd owner to workgroup administrator */
5722 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
5723 struct sec_desc_buf *new_secdesc_ctr = NULL;
5724 struct security_descriptor *psd = NULL;
5729 sid_append_rid(&owner_sid, DOMAIN_RID_ADMINISTRATOR);
5731 psd = make_sec_desc(ctx, (*secdesc_ctr)->sd->revision, (*secdesc_ctr)->sd->type,
5733 (*secdesc_ctr)->sd->group_sid,
5734 (*secdesc_ctr)->sd->sacl,
5735 (*secdesc_ctr)->sd->dacl,
5742 new_secdesc_ctr = make_sec_desc_buf(ctx, size, psd);
5743 if (!new_secdesc_ctr) {
5747 /* Swap with other one */
5749 *secdesc_ctr = new_secdesc_ctr;
5753 nt_printing_setsec(sharename, *secdesc_ctr);
5757 if (DEBUGLEVEL >= 10) {
5758 struct security_acl *the_acl = (*secdesc_ctr)->sd->dacl;
5761 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5762 sharename, the_acl->num_aces));
5764 for (i = 0; i < the_acl->num_aces; i++) {
5765 DEBUG(10, ("%s %d %d 0x%08x\n",
5766 sid_string_dbg(&the_acl->aces[i].trustee),
5767 the_acl->aces[i].type, the_acl->aces[i].flags,
5768 the_acl->aces[i].access_mask));
5777 1: level not implemented
5778 2: file doesn't exist
5779 3: can't allocate memory
5780 4: can't free memory
5781 5: non existant struct
5785 A printer and a printer driver are 2 different things.
5786 NT manages them separatelly, Samba does the same.
5787 Why ? Simply because it's easier and it makes sense !
5789 Now explanation: You have 3 printers behind your samba server,
5790 2 of them are the same make and model (laser A and B). But laser B
5791 has an 3000 sheet feeder and laser A doesn't such an option.
5792 Your third printer is an old dot-matrix model for the accounting :-).
5794 If the /usr/local/samba/lib directory (default dir), you will have
5795 5 files to describe all of this.
5797 3 files for the printers (1 by printer):
5800 NTprinter_accounting
5801 2 files for the drivers (1 for the laser and 1 for the dot matrix)
5802 NTdriver_printer model X
5803 NTdriver_printer model Y
5805 jfm: I should use this comment for the text file to explain
5806 same thing for the forms BTW.
5807 Je devrais mettre mes commentaires en francais, ca serait mieux :-)
5811 /* Convert generic access rights to printer object specific access rights.
5812 It turns out that NT4 security descriptors use generic access rights and
5813 NT5 the object specific ones. */
5815 void map_printer_permissions(struct security_descriptor *sd)
5819 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5820 se_map_generic(&sd->dacl->aces[i].access_mask,
5821 &printer_generic_mapping);
5825 void map_job_permissions(struct security_descriptor *sd)
5829 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5830 se_map_generic(&sd->dacl->aces[i].access_mask,
5831 &job_generic_mapping);
5836 /****************************************************************************
5837 Check a user has permissions to perform the given operation. We use the
5838 permission constants defined in include/rpc_spoolss.h to check the various
5839 actions we perform when checking printer access.
5841 PRINTER_ACCESS_ADMINISTER:
5842 print_queue_pause, print_queue_resume, update_printer_sec,
5843 update_printer, spoolss_addprinterex_level_2,
5844 _spoolss_setprinterdata
5849 JOB_ACCESS_ADMINISTER:
5850 print_job_delete, print_job_pause, print_job_resume,
5853 Try access control in the following order (for performance reasons):
5854 1) root and SE_PRINT_OPERATOR can do anything (easy check)
5855 2) check security descriptor (bit comparisons in memory)
5856 3) "printer admins" (may result in numerous calls to winbind)
5858 ****************************************************************************/
5859 bool print_access_check(struct auth_serversupplied_info *server_info, int snum,
5862 struct sec_desc_buf *secdesc = NULL;
5863 uint32 access_granted;
5866 TALLOC_CTX *mem_ctx = NULL;
5867 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5869 /* If user is NULL then use the current_user structure */
5871 /* Always allow root or SE_PRINT_OPERATROR to do anything */
5873 if (server_info->utok.uid == sec_initial_uid()
5874 || user_has_privileges(server_info->ptok, &se_printop ) ) {
5878 /* Get printer name */
5880 pname = PRINTERNAME(snum);
5882 if (!pname || !*pname) {
5887 /* Get printer security descriptor */
5889 if(!(mem_ctx = talloc_init("print_access_check"))) {
5894 if (!nt_printing_getsec(mem_ctx, pname, &secdesc)) {
5895 talloc_destroy(mem_ctx);
5900 if (access_type == JOB_ACCESS_ADMINISTER) {
5901 struct sec_desc_buf *parent_secdesc = secdesc;
5903 /* Create a child security descriptor to check permissions
5904 against. This is because print jobs are child objects
5905 objects of a printer. */
5907 status = se_create_child_secdesc_buf(mem_ctx, &secdesc, parent_secdesc->sd, False);
5909 if (!NT_STATUS_IS_OK(status)) {
5910 talloc_destroy(mem_ctx);
5911 errno = map_errno_from_nt_status(status);
5915 map_job_permissions(secdesc->sd);
5917 map_printer_permissions(secdesc->sd);
5921 status = se_access_check(secdesc->sd, server_info->ptok, access_type,
5924 DEBUG(4, ("access check was %s\n", NT_STATUS_IS_OK(status) ? "SUCCESS" : "FAILURE"));
5926 /* see if we need to try the printer admin list */
5928 if (!NT_STATUS_IS_OK(status) &&
5929 (token_contains_name_in_list(uidtoname(server_info->utok.uid),
5930 NULL, NULL, server_info->ptok,
5931 lp_printer_admin(snum)))) {
5932 talloc_destroy(mem_ctx);
5936 talloc_destroy(mem_ctx);
5938 if (!NT_STATUS_IS_OK(status)) {
5942 return NT_STATUS_IS_OK(status);
5945 /****************************************************************************
5946 Check the time parameters allow a print operation.
5947 *****************************************************************************/
5949 bool print_time_access_check(const char *servicename)
5951 NT_PRINTER_INFO_LEVEL *printer = NULL;
5953 time_t now = time(NULL);
5957 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
5960 if (printer->info_2->starttime == 0 && printer->info_2->untiltime == 0)
5964 mins = (uint32)t->tm_hour*60 + (uint32)t->tm_min;
5966 if (mins >= printer->info_2->starttime && mins <= printer->info_2->untiltime)
5969 free_a_printer(&printer, 2);
5977 /****************************************************************************
5978 Fill in the servername sent in the _spoolss_open_printer_ex() call
5979 ****************************************************************************/
5981 char* get_server_name( Printer_entry *printer )
5983 return printer->servername;