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"
25 static TDB_CONTEXT *tdb_forms; /* used for forms files */
26 static TDB_CONTEXT *tdb_drivers; /* used for driver files */
27 static TDB_CONTEXT *tdb_printers; /* used for printers files */
29 #define FORMS_PREFIX "FORMS/"
30 #define DRIVERS_PREFIX "DRIVERS/"
31 #define DRIVER_INIT_PREFIX "DRIVER_INIT/"
32 #define PRINTERS_PREFIX "PRINTERS/"
33 #define SECDESC_PREFIX "SECDESC/"
34 #define GLOBAL_C_SETPRINTER "GLOBALS/c_setprinter"
36 #define NTDRIVERS_DATABASE_VERSION_1 1
37 #define NTDRIVERS_DATABASE_VERSION_2 2
38 #define NTDRIVERS_DATABASE_VERSION_3 3 /* little endian version of v2 */
39 #define NTDRIVERS_DATABASE_VERSION_4 4 /* fix generic bits in security descriptors */
40 #define NTDRIVERS_DATABASE_VERSION_5 5 /* normalize keys in ntprinters.tdb */
42 /* Map generic permissions to printer object specific permissions */
44 const struct generic_mapping printer_generic_mapping = {
51 const struct standard_mapping printer_std_mapping = {
58 /* Map generic permissions to print server object specific permissions */
60 const struct generic_mapping printserver_generic_mapping = {
67 const struct generic_mapping printserver_std_mapping = {
74 /* Map generic permissions to job object specific permissions */
76 const struct generic_mapping job_generic_mapping = {
83 /* We need one default form to support our default printer. Msoft adds the
84 forms it wants and in the ORDER it wants them (note: DEVMODE papersize is an
85 array index). Letter is always first, so (for the current code) additions
86 always put things in the correct order. */
87 static const nt_forms_struct default_forms[] = {
88 {"Letter",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
89 {"Letter Small",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
90 {"Tabloid",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
91 {"Ledger",0x1,0x696b8,0x44368,0x0,0x0,0x696b8,0x44368},
92 {"Legal",0x1,0x34b5c,0x56d10,0x0,0x0,0x34b5c,0x56d10},
93 {"Statement",0x1,0x221b4,0x34b5c,0x0,0x0,0x221b4,0x34b5c},
94 {"Executive",0x1,0x2cf56,0x411cc,0x0,0x0,0x2cf56,0x411cc},
95 {"A3",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
96 {"A4",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
97 {"A4 Small",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
98 {"A5",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
99 {"B4 (JIS)",0x1,0x3ebe8,0x58de0,0x0,0x0,0x3ebe8,0x58de0},
100 {"B5 (JIS)",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
101 {"Folio",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
102 {"Quarto",0x1,0x347d8,0x43238,0x0,0x0,0x347d8,0x43238},
103 {"10x14",0x1,0x3e030,0x56d10,0x0,0x0,0x3e030,0x56d10},
104 {"11x17",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
105 {"Note",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
106 {"Envelope #9",0x1,0x18079,0x37091,0x0,0x0,0x18079,0x37091},
107 {"Envelope #10",0x1,0x19947,0x3ae94,0x0,0x0,0x19947,0x3ae94},
108 {"Envelope #11",0x1,0x1be7c,0x40565,0x0,0x0,0x1be7c,0x40565},
109 {"Envelope #12",0x1,0x1d74a,0x44368,0x0,0x0,0x1d74a,0x44368},
110 {"Envelope #14",0x1,0x1f018,0x47504,0x0,0x0,0x1f018,0x47504},
111 {"C size sheet",0x1,0x696b8,0x886d0,0x0,0x0,0x696b8,0x886d0},
112 {"D size sheet",0x1,0x886d0,0xd2d70,0x0,0x0,0x886d0,0xd2d70},
113 {"E size sheet",0x1,0xd2d70,0x110da0,0x0,0x0,0xd2d70,0x110da0},
114 {"Envelope DL",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
115 {"Envelope C5",0x1,0x278d0,0x37e88,0x0,0x0,0x278d0,0x37e88},
116 {"Envelope C3",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
117 {"Envelope C4",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
118 {"Envelope C6",0x1,0x1bd50,0x278d0,0x0,0x0,0x1bd50,0x278d0},
119 {"Envelope C65",0x1,0x1bd50,0x37e88,0x0,0x0,0x1bd50,0x37e88},
120 {"Envelope B4",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
121 {"Envelope B5",0x1,0x2af80,0x3d090,0x0,0x0,0x2af80,0x3d090},
122 {"Envelope B6",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
123 {"Envelope",0x1,0x1adb0,0x38270,0x0,0x0,0x1adb0,0x38270},
124 {"Envelope Monarch",0x1,0x18079,0x2e824,0x0,0x0,0x18079,0x2e824},
125 {"6 3/4 Envelope",0x1,0x167ab,0x284ec,0x0,0x0,0x167ab,0x284ec},
126 {"US Std Fanfold",0x1,0x5c3e1,0x44368,0x0,0x0,0x5c3e1,0x44368},
127 {"German Std Fanfold",0x1,0x34b5c,0x4a6a0,0x0,0x0,0x34b5c,0x4a6a0},
128 {"German Legal Fanfold",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
129 {"B4 (ISO)",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
130 {"Japanese Postcard",0x1,0x186a0,0x24220,0x0,0x0,0x186a0,0x24220},
131 {"9x11",0x1,0x37cf8,0x44368,0x0,0x0,0x37cf8,0x44368},
132 {"10x11",0x1,0x3e030,0x44368,0x0,0x0,0x3e030,0x44368},
133 {"15x11",0x1,0x5d048,0x44368,0x0,0x0,0x5d048,0x44368},
134 {"Envelope Invite",0x1,0x35b60,0x35b60,0x0,0x0,0x35b60,0x35b60},
135 {"Reserved48",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
136 {"Reserved49",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
137 {"Letter Extra",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
138 {"Legal Extra",0x1,0x3ae94,0x5d048,0x0,0x0,0x3ae94,0x5d048},
139 {"Tabloid Extra",0x1,0x4a6a0,0x6f9f0,0x0,0x0,0x4a6a0,0x6f9f0},
140 {"A4 Extra",0x1,0x397c2,0x4eb16,0x0,0x0,0x397c2,0x4eb16},
141 {"Letter Transverse",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
142 {"A4 Transverse",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
143 {"Letter Extra Transverse",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
144 {"Super A",0x1,0x376b8,0x56ea0,0x0,0x0,0x376b8,0x56ea0},
145 {"Super B",0x1,0x4a768,0x76e58,0x0,0x0,0x4a768,0x76e58},
146 {"Letter Plus",0x1,0x34b5c,0x4eb16,0x0,0x0,0x34b5c,0x4eb16},
147 {"A4 Plus",0x1,0x33450,0x50910,0x0,0x0,0x33450,0x50910},
148 {"A5 Transverse",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
149 {"B5 (JIS) Transverse",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
150 {"A3 Extra",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
151 {"A5 Extra",0x1,0x2a7b0,0x395f8,0x0,0x0,0x2a7b0,0x395f8},
152 {"B5 (ISO) Extra",0x1,0x31128,0x43620,0x0,0x0,0x31128,0x43620},
153 {"A2",0x1,0x668a0,0x91050,0x0,0x0,0x668a0,0x91050},
154 {"A3 Transverse",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
155 {"A3 Extra Transverse",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
156 {"Japanese Double Postcard",0x1,0x30d40,0x24220,0x0,0x0,0x30d40,0x24220},
157 {"A6",0x1,0x19a28,0x24220,0x0,0x0,0x19a28,0x24220},
158 {"Japanese Envelope Kaku #2",0x1,0x3a980,0x510e0,0x0,0x0,0x3a980,0x510e0},
159 {"Japanese Envelope Kaku #3",0x1,0x34bc0,0x43a08,0x0,0x0,0x34bc0,0x43a08},
160 {"Japanese Envelope Chou #3",0x1,0x1d4c0,0x395f8,0x0,0x0,0x1d4c0,0x395f8},
161 {"Japanese Envelope Chou #4",0x1,0x15f90,0x320c8,0x0,0x0,0x15f90,0x320c8},
162 {"Letter Rotated",0x1,0x44368,0x34b5c,0x0,0x0,0x44368,0x34b5c},
163 {"A3 Rotated",0x1,0x668a0,0x48828,0x0,0x0,0x668a0,0x48828},
164 {"A4 Rotated",0x1,0x48828,0x33450,0x0,0x0,0x48828,0x33450},
165 {"A5 Rotated",0x1,0x33450,0x24220,0x0,0x0,0x33450,0x24220},
166 {"B4 (JIS) Rotated",0x1,0x58de0,0x3ebe8,0x0,0x0,0x58de0,0x3ebe8},
167 {"B5 (JIS) Rotated",0x1,0x3ebe8,0x2c6f0,0x0,0x0,0x3ebe8,0x2c6f0},
168 {"Japanese Postcard Rotated",0x1,0x24220,0x186a0,0x0,0x0,0x24220,0x186a0},
169 {"Double Japan Postcard Rotated",0x1,0x24220,0x30d40,0x0,0x0,0x24220,0x30d40},
170 {"A6 Rotated",0x1,0x24220,0x19a28,0x0,0x0,0x24220,0x19a28},
171 {"Japan Envelope Kaku #2 Rotated",0x1,0x510e0,0x3a980,0x0,0x0,0x510e0,0x3a980},
172 {"Japan Envelope Kaku #3 Rotated",0x1,0x43a08,0x34bc0,0x0,0x0,0x43a08, 0x34bc0},
173 {"Japan Envelope Chou #3 Rotated",0x1,0x395f8,0x1d4c0,0x0,0x0,0x395f8,0x1d4c0},
174 {"Japan Envelope Chou #4 Rotated",0x1,0x320c8,0x15f90,0x0,0x0,0x320c8,0x15f90},
175 {"B6 (JIS)",0x1,0x1f400,0x2c6f0,0x0,0x0,0x1f400,0x2c6f0},
176 {"B6 (JIS) Rotated",0x1,0x2c6f0,0x1f400,0x0,0x0,0x2c6f0,0x1f400},
177 {"12x11",0x1,0x4a724,0x443e1,0x0,0x0,0x4a724,0x443e1},
178 {"Japan Envelope You #4",0x1,0x19a28,0x395f8,0x0,0x0,0x19a28,0x395f8},
179 {"Japan Envelope You #4 Rotated",0x1,0x395f8,0x19a28,0x0,0x0,0x395f8,0x19a28},
180 {"PRC 16K",0x1,0x2de60,0x3f7a0,0x0,0x0,0x2de60,0x3f7a0},
181 {"PRC 32K",0x1,0x1fbd0,0x2cec0,0x0,0x0,0x1fbd0,0x2cec0},
182 {"PRC 32K(Big)",0x1,0x222e0,0x318f8,0x0,0x0,0x222e0,0x318f8},
183 {"PRC Envelope #1",0x1,0x18e70,0x28488,0x0,0x0,0x18e70,0x28488},
184 {"PRC Envelope #2",0x1,0x18e70,0x2af80,0x0,0x0,0x18e70,0x2af80},
185 {"PRC Envelope #3",0x1,0x1e848,0x2af80,0x0,0x0,0x1e848,0x2af80},
186 {"PRC Envelope #4",0x1,0x1adb0,0x32c80,0x0,0x0,0x1adb0,0x32c80},
187 {"PRC Envelope #5",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
188 {"PRC Envelope #6",0x1,0x1d4c0,0x38270,0x0,0x0,0x1d4c0,0x38270},
189 {"PRC Envelope #7",0x1,0x27100,0x38270,0x0,0x0,0x27100,0x38270},
190 {"PRC Envelope #8",0x1,0x1d4c0,0x4b708,0x0,0x0,0x1d4c0,0x4b708},
191 {"PRC Envelope #9",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
192 {"PRC Envelope #10",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
193 {"PRC 16K Rotated",0x1,0x3f7a0,0x2de60,0x0,0x0,0x3f7a0,0x2de60},
194 {"PRC 32K Rotated",0x1,0x2cec0,0x1fbd0,0x0,0x0,0x2cec0,0x1fbd0},
195 {"PRC 32K(Big) Rotated",0x1,0x318f8,0x222e0,0x0,0x0,0x318f8,0x222e0},
196 {"PRC Envelope #1 Rotated",0x1,0x28488,0x18e70,0x0,0x0,0x28488,0x18e70},
197 {"PRC Envelope #2 Rotated",0x1,0x2af80,0x18e70,0x0,0x0,0x2af80,0x18e70},
198 {"PRC Envelope #3 Rotated",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
199 {"PRC Envelope #4 Rotated",0x1,0x32c80,0x1adb0,0x0,0x0,0x32c80,0x1adb0},
200 {"PRC Envelope #5 Rotated",0x1,0x35b60,0x1adb0,0x0,0x0,0x35b60,0x1adb0},
201 {"PRC Envelope #6 Rotated",0x1,0x38270,0x1d4c0,0x0,0x0,0x38270,0x1d4c0},
202 {"PRC Envelope #7 Rotated",0x1,0x38270,0x27100,0x0,0x0,0x38270,0x27100},
203 {"PRC Envelope #8 Rotated",0x1,0x4b708,0x1d4c0,0x0,0x0,0x4b708,0x1d4c0},
204 {"PRC Envelope #9 Rotated",0x1,0x4f1a0,0x37e88,0x0,0x0,0x4f1a0,0x37e88},
205 {"PRC Envelope #10 Rotated",0x1,0x6fd10,0x4f1a0,0x0,0x0,0x6fd10,0x4f1a0}
208 static const struct print_architecture_table_node archi_table[]= {
210 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
211 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
212 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
213 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
214 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
215 {"Windows IA64", SPL_ARCH_IA64, 3 },
216 {"Windows x64", SPL_ARCH_X64, 3 },
221 /****************************************************************************
222 generate a new TDB_DATA key for storing a printer
223 ****************************************************************************/
225 static TDB_DATA make_printer_tdbkey(TALLOC_CTX *ctx, const char *sharename )
231 fstrcpy(share, sharename);
234 keystr = talloc_asprintf(ctx, "%s%s", PRINTERS_PREFIX, share);
235 key = string_term_tdb_data(keystr ? keystr : "");
240 /****************************************************************************
241 generate a new TDB_DATA key for storing a printer security descriptor
242 ****************************************************************************/
244 static TDB_DATA make_printers_secdesc_tdbkey(TALLOC_CTX *ctx,
245 const char* sharename )
251 fstrcpy(share, sharename );
254 keystr = talloc_asprintf(ctx, "%s%s", SECDESC_PREFIX, share);
255 key = string_term_tdb_data(keystr ? keystr : "");
260 /****************************************************************************
261 ****************************************************************************/
263 static bool upgrade_to_version_3(void)
265 TDB_DATA kbuf, newkey, dbuf;
267 DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
269 for (kbuf = tdb_firstkey(tdb_drivers); kbuf.dptr;
270 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
272 dbuf = tdb_fetch(tdb_drivers, kbuf);
274 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
275 DEBUG(0,("upgrade_to_version_3:moving form\n"));
276 if (tdb_store(tdb_forms, kbuf, dbuf, TDB_REPLACE) != 0) {
277 SAFE_FREE(dbuf.dptr);
278 DEBUG(0,("upgrade_to_version_3: failed to move form. Error (%s).\n", tdb_errorstr(tdb_forms)));
281 if (tdb_delete(tdb_drivers, kbuf) != 0) {
282 SAFE_FREE(dbuf.dptr);
283 DEBUG(0,("upgrade_to_version_3: failed to delete form. Error (%s)\n", tdb_errorstr(tdb_drivers)));
288 if (strncmp((const char *)kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
289 DEBUG(0,("upgrade_to_version_3:moving printer\n"));
290 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
291 SAFE_FREE(dbuf.dptr);
292 DEBUG(0,("upgrade_to_version_3: failed to move printer. Error (%s)\n", tdb_errorstr(tdb_printers)));
295 if (tdb_delete(tdb_drivers, kbuf) != 0) {
296 SAFE_FREE(dbuf.dptr);
297 DEBUG(0,("upgrade_to_version_3: failed to delete printer. Error (%s)\n", tdb_errorstr(tdb_drivers)));
302 if (strncmp((const char *)kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
303 DEBUG(0,("upgrade_to_version_3:moving secdesc\n"));
304 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
305 SAFE_FREE(dbuf.dptr);
306 DEBUG(0,("upgrade_to_version_3: failed to move secdesc. Error (%s)\n", tdb_errorstr(tdb_printers)));
309 if (tdb_delete(tdb_drivers, kbuf) != 0) {
310 SAFE_FREE(dbuf.dptr);
311 DEBUG(0,("upgrade_to_version_3: failed to delete secdesc. Error (%s)\n", tdb_errorstr(tdb_drivers)));
316 SAFE_FREE(dbuf.dptr);
322 /*******************************************************************
323 Fix an issue with security descriptors. Printer sec_desc must
324 use more than the generic bits that were previously used
325 in <= 3.0.14a. They must also have a owner and group SID assigned.
326 Otherwise, any printers than have been migrated to a Windows
327 host using printmig.exe will not be accessible.
328 *******************************************************************/
330 static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
331 TDB_DATA data, void *state )
334 SEC_DESC_BUF *sd_orig = NULL;
335 SEC_DESC_BUF *sd_new, *sd_store;
336 SEC_DESC *sec, *new_sec;
337 TALLOC_CTX *ctx = state;
342 if (!data.dptr || data.dsize == 0) {
346 if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) != 0 ) {
350 /* upgrade the security descriptor */
352 status = unmarshall_sec_desc_buf(ctx, data.dptr, data.dsize, &sd_orig);
353 if (!NT_STATUS_IS_OK(status)) {
354 /* delete bad entries */
355 DEBUG(0,("sec_desc_upg_fn: Failed to parse original sec_desc for %si. Deleting....\n",
356 (const char *)key.dptr ));
357 tdb_delete( tdb_printers, key );
366 /* is this even valid? */
372 /* update access masks */
374 for ( i=0; i<sec->dacl->num_aces; i++ ) {
375 switch ( sec->dacl->aces[i].access_mask ) {
376 case (GENERIC_READ_ACCESS | GENERIC_WRITE_ACCESS | GENERIC_EXECUTE_ACCESS):
377 sec->dacl->aces[i].access_mask = PRINTER_ACE_PRINT;
380 case GENERIC_ALL_ACCESS:
381 sec->dacl->aces[i].access_mask = PRINTER_ACE_FULL_CONTROL;
384 case READ_CONTROL_ACCESS:
385 sec->dacl->aces[i].access_mask = PRINTER_ACE_MANAGE_DOCUMENTS;
387 default: /* no change */
392 /* create a new SEC_DESC with the appropriate owner and group SIDs */
394 new_sec = make_sec_desc( ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
395 &global_sid_Builtin_Administrators,
396 &global_sid_Builtin_Administrators,
397 NULL, NULL, &size_new_sec );
401 sd_new = make_sec_desc_buf( ctx, size_new_sec, new_sec );
406 if ( !(sd_store = sec_desc_merge_buf( ctx, sd_new, sd_orig )) ) {
407 DEBUG(0,("sec_desc_upg_fn: Failed to update sec_desc for %s\n", key.dptr ));
413 sd_size = ndr_size_security_descriptor(sd_store->sd, NULL, 0)
414 + sizeof(SEC_DESC_BUF);
416 status = marshall_sec_desc_buf(ctx, sd_store, &data.dptr, &data.dsize);
417 if (!NT_STATUS_IS_OK(status)) {
418 DEBUG(0,("sec_desc_upg_fn: Failed to parse new sec_desc for %s\n", key.dptr ));
422 result = tdb_store( tdb_printers, key, data, TDB_REPLACE );
424 /* 0 to continue and non-zero to stop traversal */
426 return (result == -1);
429 /*******************************************************************
430 *******************************************************************/
432 static bool upgrade_to_version_4(void)
437 DEBUG(0,("upgrade_to_version_4: upgrading printer security descriptors\n"));
439 if ( !(ctx = talloc_init( "upgrade_to_version_4" )) )
442 result = tdb_traverse( tdb_printers, sec_desc_upg_fn, ctx );
444 talloc_destroy( ctx );
446 return ( result != -1 );
449 /*******************************************************************
450 Fix an issue with security descriptors. Printer sec_desc must
451 use more than the generic bits that were previously used
452 in <= 3.0.14a. They must also have a owner and group SID assigned.
453 Otherwise, any printers than have been migrated to a Windows
454 host using printmig.exe will not be accessible.
455 *******************************************************************/
457 static int normalize_printers_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
458 TDB_DATA data, void *state )
460 TALLOC_CTX *ctx = talloc_tos();
463 if (!data.dptr || data.dsize == 0)
466 /* upgrade printer records and security descriptors */
468 if ( strncmp((const char *) key.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX) ) == 0 ) {
469 new_key = make_printer_tdbkey(ctx, (const char *)key.dptr+strlen(PRINTERS_PREFIX) );
471 else if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) == 0 ) {
472 new_key = make_printers_secdesc_tdbkey(ctx, (const char *)key.dptr+strlen(SECDESC_PREFIX) );
475 /* ignore this record */
479 /* delete the original record and store under the normalized key */
481 if ( tdb_delete( the_tdb, key ) != 0 ) {
482 DEBUG(0,("normalize_printers_fn: tdb_delete for [%s] failed!\n",
487 if ( tdb_store( the_tdb, new_key, data, TDB_REPLACE) != 0 ) {
488 DEBUG(0,("normalize_printers_fn: failed to store new record for [%s]!\n",
496 /*******************************************************************
497 *******************************************************************/
499 static bool upgrade_to_version_5(void)
504 DEBUG(0,("upgrade_to_version_5: normalizing printer keys\n"));
506 if ( !(ctx = talloc_init( "upgrade_to_version_5" )) )
509 result = tdb_traverse( tdb_printers, normalize_printers_fn, NULL );
511 talloc_destroy( ctx );
513 return ( result != -1 );
516 /****************************************************************************
517 Open the NT printing tdbs. Done once before fork().
518 ****************************************************************************/
520 bool nt_printing_init(struct messaging_context *msg_ctx)
522 const char *vstring = "INFO/version";
526 if ( tdb_drivers && tdb_printers && tdb_forms )
530 tdb_close(tdb_drivers);
531 tdb_drivers = tdb_open_log(state_path("ntdrivers.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
533 DEBUG(0,("nt_printing_init: Failed to open nt drivers database %s (%s)\n",
534 state_path("ntdrivers.tdb"), strerror(errno) ));
539 tdb_close(tdb_printers);
540 tdb_printers = tdb_open_log(state_path("ntprinters.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
542 DEBUG(0,("nt_printing_init: Failed to open nt printers database %s (%s)\n",
543 state_path("ntprinters.tdb"), strerror(errno) ));
548 tdb_close(tdb_forms);
549 tdb_forms = tdb_open_log(state_path("ntforms.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
551 DEBUG(0,("nt_printing_init: Failed to open nt forms database %s (%s)\n",
552 state_path("ntforms.tdb"), strerror(errno) ));
556 /* handle a Samba upgrade */
558 vers_id = tdb_fetch_int32(tdb_drivers, vstring);
560 DEBUG(10, ("Fresh database\n"));
561 tdb_store_int32( tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5 );
562 vers_id = NTDRIVERS_DATABASE_VERSION_5;
565 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
567 if ((vers_id == NTDRIVERS_DATABASE_VERSION_1) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_1)) {
568 if (!upgrade_to_version_3())
570 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
571 vers_id = NTDRIVERS_DATABASE_VERSION_3;
574 if ((vers_id == NTDRIVERS_DATABASE_VERSION_2) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_2)) {
575 /* Written on a bigendian machine with old fetch_int code. Save as le. */
576 /* The only upgrade between V2 and V3 is to save the version in little-endian. */
577 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
578 vers_id = NTDRIVERS_DATABASE_VERSION_3;
581 if (vers_id == NTDRIVERS_DATABASE_VERSION_3 ) {
582 if ( !upgrade_to_version_4() )
584 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_4);
585 vers_id = NTDRIVERS_DATABASE_VERSION_4;
588 if (vers_id == NTDRIVERS_DATABASE_VERSION_4 ) {
589 if ( !upgrade_to_version_5() )
591 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5);
592 vers_id = NTDRIVERS_DATABASE_VERSION_5;
596 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
597 DEBUG(0,("nt_printing_init: Unknown printer database version [%d]\n", vers_id));
602 update_c_setprinter(True);
605 * register callback to handle updating printers as new
606 * drivers are installed
609 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
610 do_drv_upgrade_printer);
613 * register callback to handle updating printer data
614 * when a driver is initialized
617 messaging_register(msg_ctx, NULL, MSG_PRINTERDATA_INIT_RESET,
618 reset_all_printerdata);
620 /* of course, none of the message callbacks matter if you don't
621 tell messages.c that you interested in receiving PRINT_GENERAL
622 msgs. This is done in serverid_register() */
625 if ( lp_security() == SEC_ADS ) {
626 win_rc = check_published_printers();
627 if (!W_ERROR_IS_OK(win_rc))
628 DEBUG(0, ("nt_printing_init: error checking published printers: %s\n", win_errstr(win_rc)));
634 /*******************************************************************
635 Function to allow filename parsing "the old way".
636 ********************************************************************/
638 static NTSTATUS driver_unix_convert(connection_struct *conn,
639 const char *old_name,
640 struct smb_filename **smb_fname)
643 TALLOC_CTX *ctx = talloc_tos();
644 char *name = talloc_strdup(ctx, old_name);
647 return NT_STATUS_NO_MEMORY;
650 name = unix_clean_name(ctx, name);
652 return NT_STATUS_NO_MEMORY;
654 trim_string(name,"/","/");
656 status = unix_convert(ctx, conn, name, smb_fname, 0);
657 if (!NT_STATUS_IS_OK(status)) {
658 return NT_STATUS_NO_MEMORY;
664 /*******************************************************************
665 tdb traversal function for counting printers.
666 ********************************************************************/
668 static int traverse_counting_printers(TDB_CONTEXT *t, TDB_DATA key,
669 TDB_DATA data, void *context)
671 int *printer_count = (int*)context;
673 if (memcmp(PRINTERS_PREFIX, key.dptr, sizeof(PRINTERS_PREFIX)-1) == 0) {
675 DEBUG(10,("traverse_counting_printers: printer = [%s] printer_count = %d\n", key.dptr, *printer_count));
681 /*******************************************************************
682 Update the spooler global c_setprinter. This variable is initialized
683 when the parent smbd starts with the number of existing printers. It
684 is monotonically increased by the current number of printers *after*
685 each add or delete printer RPC. Only Microsoft knows why... JRR020119
686 ********************************************************************/
688 uint32 update_c_setprinter(bool initialize)
691 int32 printer_count = 0;
693 tdb_lock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
695 /* Traverse the tdb, counting the printers */
696 tdb_traverse(tdb_printers, traverse_counting_printers, (void *)&printer_count);
698 /* If initializing, set c_setprinter to current printers count
699 * otherwise, bump it by the current printer count
702 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER) + printer_count;
704 c_setprinter = printer_count;
706 DEBUG(10,("update_c_setprinter: c_setprinter = %u\n", (unsigned int)c_setprinter));
707 tdb_store_int32(tdb_printers, GLOBAL_C_SETPRINTER, c_setprinter);
709 tdb_unlock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
711 return (uint32)c_setprinter;
714 /*******************************************************************
715 Get the spooler global c_setprinter, accounting for initialization.
716 ********************************************************************/
718 uint32 get_c_setprinter(void)
720 int32 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER);
722 if (c_setprinter == (int32)-1)
723 c_setprinter = update_c_setprinter(True);
725 DEBUG(10,("get_c_setprinter: c_setprinter = %d\n", c_setprinter));
727 return (uint32)c_setprinter;
730 /****************************************************************************
731 Get builtin form struct list.
732 ****************************************************************************/
734 int get_builtin_ntforms(nt_forms_struct **list)
736 *list = (nt_forms_struct *)memdup(&default_forms[0], sizeof(default_forms));
740 return ARRAY_SIZE(default_forms);
743 /****************************************************************************
744 get a builtin form struct
745 ****************************************************************************/
747 bool get_a_builtin_ntform_by_string(const char *form_name, nt_forms_struct *form)
750 DEBUGADD(6,("Looking for builtin form %s \n", form_name));
751 for (i=0; i<ARRAY_SIZE(default_forms); i++) {
752 if (strequal(form_name,default_forms[i].name)) {
753 DEBUGADD(6,("Found builtin form %s \n", form_name));
754 memcpy(form,&default_forms[i],sizeof(*form));
762 /****************************************************************************
763 get a form struct list.
764 ****************************************************************************/
766 int get_ntforms(nt_forms_struct **list)
768 TDB_DATA kbuf, newkey, dbuf;
769 nt_forms_struct form;
776 for (kbuf = tdb_firstkey(tdb_forms);
778 newkey = tdb_nextkey(tdb_forms, kbuf), free(kbuf.dptr), kbuf=newkey)
780 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) != 0)
783 dbuf = tdb_fetch(tdb_forms, kbuf);
787 fstrcpy(form.name, (const char *)kbuf.dptr+strlen(FORMS_PREFIX));
788 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "dddddddd",
789 &i, &form.flag, &form.width, &form.length, &form.left,
790 &form.top, &form.right, &form.bottom);
791 SAFE_FREE(dbuf.dptr);
792 if (ret != dbuf.dsize)
795 *list = SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1);
797 DEBUG(0,("get_ntforms: Realloc fail.\n"));
808 /****************************************************************************
809 write a form struct list
810 ****************************************************************************/
812 int write_ntforms(nt_forms_struct **list, int number)
814 TALLOC_CTX *ctx = talloc_tos();
821 for (i=0;i<number;i++) {
822 /* save index, so list is rebuilt in correct order */
823 len = tdb_pack(NULL, 0, "dddddddd",
824 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
825 (*list)[i].left, (*list)[i].top, (*list)[i].right,
830 buf = TALLOC_ARRAY(ctx, char, len);
834 len = tdb_pack((uint8 *)buf, len, "dddddddd",
835 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
836 (*list)[i].left, (*list)[i].top, (*list)[i].right,
838 key = talloc_asprintf(ctx, "%s%s", FORMS_PREFIX, (*list)[i].name);
843 dbuf.dptr = (uint8 *)buf;
844 if (tdb_store_bystring(tdb_forms, key, dbuf, TDB_REPLACE) != 0) {
856 /****************************************************************************
857 add a form struct at the end of the list
858 ****************************************************************************/
859 bool add_a_form(nt_forms_struct **list, struct spoolss_AddFormInfo1 *form, int *count)
865 * NT tries to add forms even when
866 * they are already in the base
867 * only update the values if already present
872 for (n=0; n<*count; n++) {
873 if ( strequal((*list)[n].name, form->form_name) ) {
880 if((*list=SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1)) == NULL) {
881 DEBUG(0,("add_a_form: failed to enlarge forms list!\n"));
884 fstrcpy((*list)[n].name, form->form_name);
888 (*list)[n].flag = form->flags;
889 (*list)[n].width = form->size.width;
890 (*list)[n].length = form->size.height;
891 (*list)[n].left = form->area.left;
892 (*list)[n].top = form->area.top;
893 (*list)[n].right = form->area.right;
894 (*list)[n].bottom = form->area.bottom;
896 DEBUG(6,("add_a_form: Successfully %s form [%s]\n",
897 update ? "updated" : "added", form->form_name));
902 /****************************************************************************
903 Delete a named form struct.
904 ****************************************************************************/
906 bool delete_a_form(nt_forms_struct **list, const char *del_name, int *count, WERROR *ret)
913 for (n=0; n<*count; n++) {
914 if (!strncmp((*list)[n].name, del_name, strlen(del_name))) {
915 DEBUG(103, ("delete_a_form, [%s] in list\n", del_name));
921 DEBUG(10,("delete_a_form, [%s] not found\n", del_name));
922 *ret = WERR_INVALID_FORM_NAME;
926 if (asprintf(&key, "%s%s", FORMS_PREFIX, (*list)[n].name) < 0) {
930 if (tdb_delete_bystring(tdb_forms, key) != 0) {
939 /****************************************************************************
940 Update a form struct.
941 ****************************************************************************/
943 void update_a_form(nt_forms_struct **list, struct spoolss_AddFormInfo1 *form, int count)
947 DEBUG(106, ("[%s]\n", form->form_name));
948 for (n=0; n<count; n++) {
949 DEBUGADD(106, ("n [%d]:[%s]\n", n, (*list)[n].name));
950 if (!strncmp((*list)[n].name, form->form_name, strlen(form->form_name)))
954 if (n==count) return;
956 (*list)[n].flag = form->flags;
957 (*list)[n].width = form->size.width;
958 (*list)[n].length = form->size.height;
959 (*list)[n].left = form->area.left;
960 (*list)[n].top = form->area.top;
961 (*list)[n].right = form->area.right;
962 (*list)[n].bottom = form->area.bottom;
965 /****************************************************************************
966 Get the nt drivers list.
967 Traverse the database and look-up the matching names.
968 ****************************************************************************/
969 int get_ntdrivers(fstring **list, const char *architecture, uint32 version)
972 const char *short_archi;
974 TDB_DATA kbuf, newkey;
976 short_archi = get_short_archi(architecture);
981 if (asprintf(&key, "%s%s/%d/", DRIVERS_PREFIX,
982 short_archi, version) < 0) {
986 for (kbuf = tdb_firstkey(tdb_drivers);
988 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
990 if (strncmp((const char *)kbuf.dptr, key, strlen(key)) != 0)
993 if((*list = SMB_REALLOC_ARRAY(*list, fstring, total+1)) == NULL) {
994 DEBUG(0,("get_ntdrivers: failed to enlarge list!\n"));
999 fstrcpy((*list)[total], (const char *)kbuf.dptr+strlen(key));
1007 /****************************************************************************
1008 Function to do the mapping between the long architecture name and
1010 ****************************************************************************/
1012 const char *get_short_archi(const char *long_archi)
1016 DEBUG(107,("Getting architecture dependant directory\n"));
1019 } while ( (archi_table[i].long_archi!=NULL ) &&
1020 StrCaseCmp(long_archi, archi_table[i].long_archi) );
1022 if (archi_table[i].long_archi==NULL) {
1023 DEBUGADD(10,("Unknown architecture [%s] !\n", long_archi));
1027 /* this might be client code - but shouldn't this be an fstrcpy etc? */
1029 DEBUGADD(108,("index: [%d]\n", i));
1030 DEBUGADD(108,("long architecture: [%s]\n", archi_table[i].long_archi));
1031 DEBUGADD(108,("short architecture: [%s]\n", archi_table[i].short_archi));
1033 return archi_table[i].short_archi;
1036 /****************************************************************************
1037 Version information in Microsoft files is held in a VS_VERSION_INFO structure.
1038 There are two case to be covered here: PE (Portable Executable) and NE (New
1039 Executable) files. Both files support the same INFO structure, but PE files
1040 store the signature in unicode, and NE files store it as !unicode.
1041 returns -1 on error, 1 on version info found, and 0 on no version info found.
1042 ****************************************************************************/
1044 static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32 *minor)
1050 if ((buf=(char *)SMB_MALLOC(DOS_HEADER_SIZE)) == NULL) {
1051 DEBUG(0,("get_file_version: PE file [%s] DOS Header malloc failed bytes = %d\n",
1052 fname, DOS_HEADER_SIZE));
1056 if ((byte_count = vfs_read_data(fsp, buf, DOS_HEADER_SIZE)) < DOS_HEADER_SIZE) {
1057 DEBUG(3,("get_file_version: File [%s] DOS header too short, bytes read = %lu\n",
1058 fname, (unsigned long)byte_count));
1059 goto no_version_info;
1062 /* Is this really a DOS header? */
1063 if (SVAL(buf,DOS_HEADER_MAGIC_OFFSET) != DOS_HEADER_MAGIC) {
1064 DEBUG(6,("get_file_version: File [%s] bad DOS magic = 0x%x\n",
1065 fname, SVAL(buf,DOS_HEADER_MAGIC_OFFSET)));
1066 goto no_version_info;
1069 /* Skip OEM header (if any) and the DOS stub to start of Windows header */
1070 if (SMB_VFS_LSEEK(fsp, SVAL(buf,DOS_HEADER_LFANEW_OFFSET), SEEK_SET) == (SMB_OFF_T)-1) {
1071 DEBUG(3,("get_file_version: File [%s] too short, errno = %d\n",
1073 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1074 goto no_version_info;
1077 /* Note: DOS_HEADER_SIZE and NE_HEADER_SIZE are incidentally same */
1078 if ((byte_count = vfs_read_data(fsp, buf, NE_HEADER_SIZE)) < NE_HEADER_SIZE) {
1079 DEBUG(3,("get_file_version: File [%s] Windows header too short, bytes read = %lu\n",
1080 fname, (unsigned long)byte_count));
1081 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1082 goto no_version_info;
1085 /* The header may be a PE (Portable Executable) or an NE (New Executable) */
1086 if (IVAL(buf,PE_HEADER_SIGNATURE_OFFSET) == PE_HEADER_SIGNATURE) {
1087 unsigned int num_sections;
1088 unsigned int section_table_bytes;
1090 /* Just skip over optional header to get to section table */
1091 if (SMB_VFS_LSEEK(fsp,
1092 SVAL(buf,PE_HEADER_OPTIONAL_HEADER_SIZE)-(NE_HEADER_SIZE-PE_HEADER_SIZE),
1093 SEEK_CUR) == (SMB_OFF_T)-1) {
1094 DEBUG(3,("get_file_version: File [%s] Windows optional header too short, errno = %d\n",
1099 /* get the section table */
1100 num_sections = SVAL(buf,PE_HEADER_NUMBER_OF_SECTIONS);
1101 section_table_bytes = num_sections * PE_HEADER_SECT_HEADER_SIZE;
1102 if (section_table_bytes == 0)
1106 if ((buf=(char *)SMB_MALLOC(section_table_bytes)) == NULL) {
1107 DEBUG(0,("get_file_version: PE file [%s] section table malloc failed bytes = %d\n",
1108 fname, section_table_bytes));
1112 if ((byte_count = vfs_read_data(fsp, buf, section_table_bytes)) < section_table_bytes) {
1113 DEBUG(3,("get_file_version: PE file [%s] Section header too short, bytes read = %lu\n",
1114 fname, (unsigned long)byte_count));
1118 /* Iterate the section table looking for the resource section ".rsrc" */
1119 for (i = 0; i < num_sections; i++) {
1120 int sec_offset = i * PE_HEADER_SECT_HEADER_SIZE;
1122 if (strcmp(".rsrc", &buf[sec_offset+PE_HEADER_SECT_NAME_OFFSET]) == 0) {
1123 unsigned int section_pos = IVAL(buf,sec_offset+PE_HEADER_SECT_PTR_DATA_OFFSET);
1124 unsigned int section_bytes = IVAL(buf,sec_offset+PE_HEADER_SECT_SIZE_DATA_OFFSET);
1126 if (section_bytes == 0)
1130 if ((buf=(char *)SMB_MALLOC(section_bytes)) == NULL) {
1131 DEBUG(0,("get_file_version: PE file [%s] version malloc failed bytes = %d\n",
1132 fname, section_bytes));
1136 /* Seek to the start of the .rsrc section info */
1137 if (SMB_VFS_LSEEK(fsp, section_pos, SEEK_SET) == (SMB_OFF_T)-1) {
1138 DEBUG(3,("get_file_version: PE file [%s] too short for section info, errno = %d\n",
1143 if ((byte_count = vfs_read_data(fsp, buf, section_bytes)) < section_bytes) {
1144 DEBUG(3,("get_file_version: PE file [%s] .rsrc section too short, bytes read = %lu\n",
1145 fname, (unsigned long)byte_count));
1149 if (section_bytes < VS_VERSION_INFO_UNICODE_SIZE)
1152 for (i=0; i<section_bytes-VS_VERSION_INFO_UNICODE_SIZE; i++) {
1153 /* Scan for 1st 3 unicoded bytes followed by word aligned magic value */
1154 if (buf[i] == 'V' && buf[i+1] == '\0' && buf[i+2] == 'S') {
1155 /* Align to next long address */
1156 int pos = (i + sizeof(VS_SIGNATURE)*2 + 3) & 0xfffffffc;
1158 if (IVAL(buf,pos) == VS_MAGIC_VALUE) {
1159 *major = IVAL(buf,pos+VS_MAJOR_OFFSET);
1160 *minor = IVAL(buf,pos+VS_MINOR_OFFSET);
1162 DEBUG(6,("get_file_version: PE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1163 fname, *major, *minor,
1164 (*major>>16)&0xffff, *major&0xffff,
1165 (*minor>>16)&0xffff, *minor&0xffff));
1174 /* Version info not found, fall back to origin date/time */
1175 DEBUG(10,("get_file_version: PE file [%s] has no version info\n", fname));
1179 } else if (SVAL(buf,NE_HEADER_SIGNATURE_OFFSET) == NE_HEADER_SIGNATURE) {
1180 if (CVAL(buf,NE_HEADER_TARGET_OS_OFFSET) != NE_HEADER_TARGOS_WIN ) {
1181 DEBUG(3,("get_file_version: NE file [%s] wrong target OS = 0x%x\n",
1182 fname, CVAL(buf,NE_HEADER_TARGET_OS_OFFSET)));
1183 /* At this point, we assume the file is in error. It still could be somthing
1184 * else besides a NE file, but it unlikely at this point. */
1188 /* Allocate a bit more space to speed up things */
1190 if ((buf=(char *)SMB_MALLOC(VS_NE_BUF_SIZE)) == NULL) {
1191 DEBUG(0,("get_file_version: NE file [%s] malloc failed bytes = %d\n",
1192 fname, PE_HEADER_SIZE));
1196 /* This is a HACK! I got tired of trying to sort through the messy
1197 * 'NE' file format. If anyone wants to clean this up please have at
1198 * it, but this works. 'NE' files will eventually fade away. JRR */
1199 while((byte_count = vfs_read_data(fsp, buf, VS_NE_BUF_SIZE)) > 0) {
1200 /* Cover case that should not occur in a well formed 'NE' .dll file */
1201 if (byte_count-VS_VERSION_INFO_SIZE <= 0) break;
1203 for(i=0; i<byte_count; i++) {
1204 /* Fast skip past data that can't possibly match */
1205 if (buf[i] != 'V') continue;
1207 /* Potential match data crosses buf boundry, move it to beginning
1208 * of buf, and fill the buf with as much as it will hold. */
1209 if (i>byte_count-VS_VERSION_INFO_SIZE) {
1212 memcpy(buf, &buf[i], byte_count-i);
1213 if ((bc = vfs_read_data(fsp, &buf[byte_count-i], VS_NE_BUF_SIZE-
1214 (byte_count-i))) < 0) {
1216 DEBUG(0,("get_file_version: NE file [%s] Read error, errno=%d\n",
1221 byte_count = bc + (byte_count - i);
1222 if (byte_count<VS_VERSION_INFO_SIZE) break;
1227 /* Check that the full signature string and the magic number that
1228 * follows exist (not a perfect solution, but the chances that this
1229 * occurs in code is, well, remote. Yes I know I'm comparing the 'V'
1230 * twice, as it is simpler to read the code. */
1231 if (strcmp(&buf[i], VS_SIGNATURE) == 0) {
1232 /* Compute skip alignment to next long address */
1233 int skip = -(SMB_VFS_LSEEK(fsp, 0, SEEK_CUR) - (byte_count - i) +
1234 sizeof(VS_SIGNATURE)) & 3;
1235 if (IVAL(buf,i+sizeof(VS_SIGNATURE)+skip) != 0xfeef04bd) continue;
1237 *major = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MAJOR_OFFSET);
1238 *minor = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MINOR_OFFSET);
1239 DEBUG(6,("get_file_version: NE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1240 fname, *major, *minor,
1241 (*major>>16)&0xffff, *major&0xffff,
1242 (*minor>>16)&0xffff, *minor&0xffff));
1249 /* Version info not found, fall back to origin date/time */
1250 DEBUG(0,("get_file_version: NE file [%s] Version info not found\n", fname));
1255 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1256 DEBUG(3,("get_file_version: File [%s] unknown file format, signature = 0x%x\n",
1257 fname, IVAL(buf,PE_HEADER_SIGNATURE_OFFSET)));
1268 /****************************************************************************
1269 Drivers for Microsoft systems contain multiple files. Often, multiple drivers
1270 share one or more files. During the MS installation process files are checked
1271 to insure that only a newer version of a shared file is installed over an
1272 older version. There are several possibilities for this comparison. If there
1273 is no previous version, the new one is newer (obviously). If either file is
1274 missing the version info structure, compare the creation date (on Unix use
1275 the modification date). Otherwise chose the numerically larger version number.
1276 ****************************************************************************/
1278 static int file_version_is_newer(connection_struct *conn, fstring new_file, fstring old_file)
1280 bool use_version = true;
1284 time_t new_create_time;
1288 time_t old_create_time;
1290 struct smb_filename *smb_fname = NULL;
1291 files_struct *fsp = NULL;
1297 SET_STAT_INVALID(st);
1298 new_create_time = (time_t)0;
1299 old_create_time = (time_t)0;
1301 /* Get file version info (if available) for previous file (if it exists) */
1302 status = driver_unix_convert(conn, old_file, &smb_fname);
1303 if (!NT_STATUS_IS_OK(status)) {
1307 status = SMB_VFS_CREATE_FILE(
1310 0, /* root_dir_fid */
1311 smb_fname, /* fname */
1312 FILE_GENERIC_READ, /* access_mask */
1313 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1314 FILE_OPEN, /* create_disposition*/
1315 0, /* create_options */
1316 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1317 INTERNAL_OPEN_ONLY, /* oplock_request */
1318 0, /* allocation_size */
1319 0, /* private_flags */
1325 if (!NT_STATUS_IS_OK(status)) {
1326 /* Old file not found, so by definition new file is in fact newer */
1327 DEBUG(10,("file_version_is_newer: Can't open old file [%s], "
1328 "errno = %d\n", smb_fname_str_dbg(smb_fname),
1334 ret = get_file_version(fsp, old_file, &old_major, &old_minor);
1340 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1342 use_version = false;
1343 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1346 old_create_time = convert_timespec_to_time_t(st.st_ex_mtime);
1347 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n",
1348 (long)old_create_time));
1351 close_file(NULL, fsp, NORMAL_CLOSE);
1354 /* Get file version info (if available) for new file */
1355 status = driver_unix_convert(conn, new_file, &smb_fname);
1356 if (!NT_STATUS_IS_OK(status)) {
1360 status = SMB_VFS_CREATE_FILE(
1363 0, /* root_dir_fid */
1364 smb_fname, /* fname */
1365 FILE_GENERIC_READ, /* access_mask */
1366 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1367 FILE_OPEN, /* create_disposition*/
1368 0, /* create_options */
1369 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1370 INTERNAL_OPEN_ONLY, /* oplock_request */
1371 0, /* allocation_size */
1372 0, /* private_flags */
1378 if (!NT_STATUS_IS_OK(status)) {
1379 /* New file not found, this shouldn't occur if the caller did its job */
1380 DEBUG(3,("file_version_is_newer: Can't open new file [%s], "
1381 "errno = %d\n", smb_fname_str_dbg(smb_fname), errno));
1385 ret = get_file_version(fsp, new_file, &new_major, &new_minor);
1391 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1393 use_version = false;
1394 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1397 new_create_time = convert_timespec_to_time_t(st.st_ex_mtime);
1398 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n",
1399 (long)new_create_time));
1402 close_file(NULL, fsp, NORMAL_CLOSE);
1405 if (use_version && (new_major != old_major || new_minor != old_minor)) {
1406 /* Compare versions and choose the larger version number */
1407 if (new_major > old_major ||
1408 (new_major == old_major && new_minor > old_minor)) {
1410 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1415 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1421 /* Compare modification time/dates and choose the newest time/date */
1422 if (new_create_time > old_create_time) {
1423 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1428 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1436 close_file(NULL, fsp, NORMAL_CLOSE);
1439 TALLOC_FREE(smb_fname);
1443 /****************************************************************************
1444 Determine the correct cVersion associated with an architecture and driver
1445 ****************************************************************************/
1446 static uint32 get_correct_cversion(struct pipes_struct *p,
1447 const char *architecture,
1448 const char *driverpath_in,
1453 struct smb_filename *smb_fname = NULL;
1454 char *driverpath = NULL;
1455 files_struct *fsp = NULL;
1456 connection_struct *conn = NULL;
1459 fstring printdollar;
1460 int printdollar_snum;
1462 *perr = WERR_INVALID_PARAM;
1464 /* If architecture is Windows 95/98/ME, the version is always 0. */
1465 if (strcmp(architecture, SPL_ARCH_WIN40) == 0) {
1466 DEBUG(10,("get_correct_cversion: Driver is Win9x, cversion = 0\n"));
1471 /* If architecture is Windows x64, the version is always 3. */
1472 if (strcmp(architecture, SPL_ARCH_X64) == 0) {
1473 DEBUG(10,("get_correct_cversion: Driver is x64, cversion = 3\n"));
1478 fstrcpy(printdollar, "print$");
1480 printdollar_snum = find_service(printdollar);
1481 if (printdollar_snum == -1) {
1482 *perr = WERR_NO_SUCH_SHARE;
1486 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
1487 lp_pathname(printdollar_snum),
1488 p->server_info, &oldcwd);
1489 if (!NT_STATUS_IS_OK(nt_status)) {
1490 DEBUG(0,("get_correct_cversion: create_conn_struct "
1491 "returned %s\n", nt_errstr(nt_status)));
1492 *perr = ntstatus_to_werror(nt_status);
1496 /* Open the driver file (Portable Executable format) and determine the
1497 * deriver the cversion. */
1498 driverpath = talloc_asprintf(talloc_tos(),
1507 nt_status = driver_unix_convert(conn, driverpath, &smb_fname);
1508 if (!NT_STATUS_IS_OK(nt_status)) {
1509 *perr = ntstatus_to_werror(nt_status);
1513 nt_status = vfs_file_exist(conn, smb_fname);
1514 if (!NT_STATUS_IS_OK(nt_status)) {
1515 *perr = WERR_BADFILE;
1519 status = SMB_VFS_CREATE_FILE(
1522 0, /* root_dir_fid */
1523 smb_fname, /* fname */
1524 FILE_GENERIC_READ, /* access_mask */
1525 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1526 FILE_OPEN, /* create_disposition*/
1527 0, /* create_options */
1528 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1529 INTERNAL_OPEN_ONLY, /* oplock_request */
1530 0, /* private_flags */
1531 0, /* allocation_size */
1537 if (!NT_STATUS_IS_OK(status)) {
1538 DEBUG(3,("get_correct_cversion: Can't open file [%s], errno = "
1539 "%d\n", smb_fname_str_dbg(smb_fname), errno));
1540 *perr = WERR_ACCESS_DENIED;
1547 ret = get_file_version(fsp, smb_fname->base_name, &major, &minor);
1548 if (ret == -1) goto error_exit;
1551 DEBUG(6,("get_correct_cversion: Version info not "
1553 smb_fname_str_dbg(smb_fname)));
1558 * This is a Microsoft'ism. See references in MSDN to VER_FILEVERSION
1559 * for more details. Version in this case is not just the version of the
1560 * file, but the version in the sense of kernal mode (2) vs. user mode
1561 * (3) drivers. Other bits of the version fields are the version info.
1564 cversion = major & 0x0000ffff;
1566 case 2: /* WinNT drivers */
1567 case 3: /* Win2K drivers */
1571 DEBUG(6,("get_correct_cversion: cversion "
1572 "invalid [%s] cversion = %d\n",
1573 smb_fname_str_dbg(smb_fname),
1578 DEBUG(10,("get_correct_cversion: Version info found [%s] major"
1579 " = 0x%x minor = 0x%x\n",
1580 smb_fname_str_dbg(smb_fname), major, minor));
1583 DEBUG(10,("get_correct_cversion: Driver file [%s] cversion = %d\n",
1584 smb_fname_str_dbg(smb_fname), cversion));
1591 TALLOC_FREE(smb_fname);
1593 close_file(NULL, fsp, NORMAL_CLOSE);
1596 vfs_ChDir(conn, oldcwd);
1599 if (cversion != -1) {
1605 /****************************************************************************
1606 ****************************************************************************/
1608 #define strip_driver_path(_mem_ctx, _element) do { \
1609 if (_element && ((_p = strrchr((_element), '\\')) != NULL)) { \
1610 (_element) = talloc_asprintf((_mem_ctx), "%s", _p+1); \
1611 W_ERROR_HAVE_NO_MEMORY((_element)); \
1615 static WERROR clean_up_driver_struct_level(TALLOC_CTX *mem_ctx,
1616 struct pipes_struct *rpc_pipe,
1617 const char *architecture,
1618 const char **driver_path,
1619 const char **data_file,
1620 const char **config_file,
1621 const char **help_file,
1622 struct spoolss_StringArray *dependent_files,
1625 const char *short_architecture;
1630 if (!*driver_path || !*data_file || !*config_file) {
1631 return WERR_INVALID_PARAM;
1634 /* clean up the driver name.
1635 * we can get .\driver.dll
1636 * or worse c:\windows\system\driver.dll !
1638 /* using an intermediate string to not have overlaping memcpy()'s */
1640 strip_driver_path(mem_ctx, *driver_path);
1641 strip_driver_path(mem_ctx, *data_file);
1642 strip_driver_path(mem_ctx, *config_file);
1644 strip_driver_path(mem_ctx, *help_file);
1647 if (dependent_files && dependent_files->string) {
1648 for (i=0; dependent_files->string[i]; i++) {
1649 strip_driver_path(mem_ctx, dependent_files->string[i]);
1653 short_architecture = get_short_archi(architecture);
1654 if (!short_architecture) {
1655 return WERR_UNKNOWN_PRINTER_DRIVER;
1658 /* jfm:7/16/2000 the client always sends the cversion=0.
1659 * The server should check which version the driver is by reading
1660 * the PE header of driver->driverpath.
1662 * For Windows 95/98 the version is 0 (so the value sent is correct)
1663 * For Windows NT (the architecture doesn't matter)
1664 * NT 3.1: cversion=0
1665 * NT 3.5/3.51: cversion=1
1670 *version = get_correct_cversion(rpc_pipe, short_architecture,
1671 *driver_path, &err);
1672 if (*version == -1) {
1679 /****************************************************************************
1680 ****************************************************************************/
1682 WERROR clean_up_driver_struct(struct pipes_struct *rpc_pipe,
1683 struct spoolss_AddDriverInfoCtr *r)
1687 return clean_up_driver_struct_level(r, rpc_pipe,
1688 r->info.info3->architecture,
1689 &r->info.info3->driver_path,
1690 &r->info.info3->data_file,
1691 &r->info.info3->config_file,
1692 &r->info.info3->help_file,
1693 r->info.info3->dependent_files,
1694 &r->info.info3->version);
1696 return clean_up_driver_struct_level(r, rpc_pipe,
1697 r->info.info6->architecture,
1698 &r->info.info6->driver_path,
1699 &r->info.info6->data_file,
1700 &r->info.info6->config_file,
1701 &r->info.info6->help_file,
1702 r->info.info6->dependent_files,
1703 &r->info.info6->version);
1705 return WERR_NOT_SUPPORTED;
1709 /****************************************************************************
1710 This function sucks and should be replaced. JRA.
1711 ****************************************************************************/
1713 static void convert_level_6_to_level3(struct spoolss_AddDriverInfo3 *dst,
1714 const struct spoolss_AddDriverInfo6 *src)
1716 dst->version = src->version;
1718 dst->driver_name = src->driver_name;
1719 dst->architecture = src->architecture;
1720 dst->driver_path = src->driver_path;
1721 dst->data_file = src->data_file;
1722 dst->config_file = src->config_file;
1723 dst->help_file = src->help_file;
1724 dst->monitor_name = src->monitor_name;
1725 dst->default_datatype = src->default_datatype;
1726 dst->_ndr_size_dependent_files = src->_ndr_size_dependent_files;
1727 dst->dependent_files = src->dependent_files;
1730 /****************************************************************************
1731 This function sucks and should be replaced. JRA.
1732 ****************************************************************************/
1734 static void convert_level_8_to_level3(TALLOC_CTX *mem_ctx,
1735 struct spoolss_AddDriverInfo3 *dst,
1736 const struct spoolss_DriverInfo8 *src)
1738 dst->version = src->version;
1739 dst->driver_name = src->driver_name;
1740 dst->architecture = src->architecture;
1741 dst->driver_path = src->driver_path;
1742 dst->data_file = src->data_file;
1743 dst->config_file = src->config_file;
1744 dst->help_file = src->help_file;
1745 dst->monitor_name = src->monitor_name;
1746 dst->default_datatype = src->default_datatype;
1747 if (src->dependent_files) {
1748 dst->dependent_files = talloc_zero(mem_ctx, struct spoolss_StringArray);
1749 if (!dst->dependent_files) return;
1750 dst->dependent_files->string = src->dependent_files;
1752 dst->dependent_files = NULL;
1756 /****************************************************************************
1757 ****************************************************************************/
1759 static WERROR move_driver_file_to_download_area(TALLOC_CTX *mem_ctx,
1760 connection_struct *conn,
1761 const char *driver_file,
1762 const char *short_architecture,
1763 uint32_t driver_version,
1766 struct smb_filename *smb_fname_old = NULL;
1767 struct smb_filename *smb_fname_new = NULL;
1768 char *old_name = NULL;
1769 char *new_name = NULL;
1773 old_name = talloc_asprintf(mem_ctx, "%s/%s",
1774 short_architecture, driver_file);
1775 W_ERROR_HAVE_NO_MEMORY(old_name);
1777 new_name = talloc_asprintf(mem_ctx, "%s/%d/%s",
1778 short_architecture, driver_version, driver_file);
1779 if (new_name == NULL) {
1780 TALLOC_FREE(old_name);
1784 if (version != -1 && (version = file_version_is_newer(conn, old_name, new_name)) > 0) {
1786 status = driver_unix_convert(conn, old_name, &smb_fname_old);
1787 if (!NT_STATUS_IS_OK(status)) {
1792 /* Setup a synthetic smb_filename struct */
1793 smb_fname_new = TALLOC_ZERO_P(mem_ctx, struct smb_filename);
1794 if (!smb_fname_new) {
1799 smb_fname_new->base_name = new_name;
1801 DEBUG(10,("move_driver_file_to_download_area: copying '%s' to "
1802 "'%s'\n", smb_fname_old->base_name,
1803 smb_fname_new->base_name));
1805 status = copy_file(mem_ctx, conn, smb_fname_old, smb_fname_new,
1806 OPENX_FILE_EXISTS_TRUNCATE |
1807 OPENX_FILE_CREATE_IF_NOT_EXIST,
1810 if (!NT_STATUS_IS_OK(status)) {
1811 DEBUG(0,("move_driver_file_to_download_area: Unable "
1812 "to rename [%s] to [%s]: %s\n",
1813 smb_fname_old->base_name, new_name,
1814 nt_errstr(status)));
1815 ret = WERR_ACCESS_DENIED;
1822 TALLOC_FREE(smb_fname_old);
1823 TALLOC_FREE(smb_fname_new);
1827 WERROR move_driver_to_download_area(struct pipes_struct *p,
1828 struct spoolss_AddDriverInfoCtr *r,
1831 struct spoolss_AddDriverInfo3 *driver;
1832 struct spoolss_AddDriverInfo3 converted_driver;
1833 const char *short_architecture;
1834 struct smb_filename *smb_dname = NULL;
1835 char *new_dir = NULL;
1836 connection_struct *conn = NULL;
1839 TALLOC_CTX *ctx = talloc_tos();
1842 fstring printdollar;
1843 int printdollar_snum;
1849 driver = r->info.info3;
1852 convert_level_6_to_level3(&converted_driver, r->info.info6);
1853 driver = &converted_driver;
1856 DEBUG(0,("move_driver_to_download_area: Unknown info level (%u)\n", (unsigned int)r->level));
1857 return WERR_UNKNOWN_LEVEL;
1860 short_architecture = get_short_archi(driver->architecture);
1861 if (!short_architecture) {
1862 return WERR_UNKNOWN_PRINTER_DRIVER;
1865 fstrcpy(printdollar, "print$");
1867 printdollar_snum = find_service(printdollar);
1868 if (printdollar_snum == -1) {
1869 *perr = WERR_NO_SUCH_SHARE;
1870 return WERR_NO_SUCH_SHARE;
1873 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
1874 lp_pathname(printdollar_snum),
1875 p->server_info, &oldcwd);
1876 if (!NT_STATUS_IS_OK(nt_status)) {
1877 DEBUG(0,("move_driver_to_download_area: create_conn_struct "
1878 "returned %s\n", nt_errstr(nt_status)));
1879 *perr = ntstatus_to_werror(nt_status);
1883 new_dir = talloc_asprintf(ctx,
1891 nt_status = driver_unix_convert(conn, new_dir, &smb_dname);
1892 if (!NT_STATUS_IS_OK(nt_status)) {
1897 DEBUG(5,("Creating first directory: %s\n", smb_dname->base_name));
1899 create_directory(conn, NULL, smb_dname);
1901 /* For each driver file, archi\filexxx.yyy, if there is a duplicate file
1902 * listed for this driver which has already been moved, skip it (note:
1903 * drivers may list the same file name several times. Then check if the
1904 * file already exists in archi\version\, if so, check that the version
1905 * info (or time stamps if version info is unavailable) is newer (or the
1906 * date is later). If it is, move it to archi\version\filexxx.yyy.
1907 * Otherwise, delete the file.
1909 * If a file is not moved to archi\version\ because of an error, all the
1910 * rest of the 'unmoved' driver files are removed from archi\. If one or
1911 * more of the driver's files was already moved to archi\version\, it
1912 * potentially leaves the driver in a partially updated state. Version
1913 * trauma will most likely occur if an client attempts to use any printer
1914 * bound to the driver. Perhaps a rewrite to make sure the moves can be
1915 * done is appropriate... later JRR
1918 DEBUG(5,("Moving files now !\n"));
1920 if (driver->driver_path && strlen(driver->driver_path)) {
1922 *perr = move_driver_file_to_download_area(ctx,
1924 driver->driver_path,
1928 if (!W_ERROR_IS_OK(*perr)) {
1929 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1936 if (driver->data_file && strlen(driver->data_file)) {
1937 if (!strequal(driver->data_file, driver->driver_path)) {
1939 *perr = move_driver_file_to_download_area(ctx,
1945 if (!W_ERROR_IS_OK(*perr)) {
1946 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1954 if (driver->config_file && strlen(driver->config_file)) {
1955 if (!strequal(driver->config_file, driver->driver_path) &&
1956 !strequal(driver->config_file, driver->data_file)) {
1958 *perr = move_driver_file_to_download_area(ctx,
1960 driver->config_file,
1964 if (!W_ERROR_IS_OK(*perr)) {
1965 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1973 if (driver->help_file && strlen(driver->help_file)) {
1974 if (!strequal(driver->help_file, driver->driver_path) &&
1975 !strequal(driver->help_file, driver->data_file) &&
1976 !strequal(driver->help_file, driver->config_file)) {
1978 *perr = move_driver_file_to_download_area(ctx,
1984 if (!W_ERROR_IS_OK(*perr)) {
1985 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
1993 if (driver->dependent_files && driver->dependent_files->string) {
1994 for (i=0; driver->dependent_files->string[i]; i++) {
1995 if (!strequal(driver->dependent_files->string[i], driver->driver_path) &&
1996 !strequal(driver->dependent_files->string[i], driver->data_file) &&
1997 !strequal(driver->dependent_files->string[i], driver->config_file) &&
1998 !strequal(driver->dependent_files->string[i], driver->help_file)) {
2000 for (j=0; j < i; j++) {
2001 if (strequal(driver->dependent_files->string[i], driver->dependent_files->string[j])) {
2006 *perr = move_driver_file_to_download_area(ctx,
2008 driver->dependent_files->string[i],
2012 if (!W_ERROR_IS_OK(*perr)) {
2013 if (W_ERROR_EQUAL(*perr, WERR_ACCESS_DENIED)) {
2024 TALLOC_FREE(smb_dname);
2027 vfs_ChDir(conn, oldcwd);
2031 if (W_ERROR_EQUAL(*perr, WERR_OK)) {
2035 return WERR_UNKNOWN_PRINTER_DRIVER;
2040 /****************************************************************************
2041 ****************************************************************************/
2043 static uint32 add_a_printer_driver_3(struct spoolss_AddDriverInfo3 *driver)
2045 TALLOC_CTX *ctx = talloc_tos();
2047 const char *architecture;
2048 char *directory = NULL;
2054 architecture = get_short_archi(driver->architecture);
2055 if (!architecture) {
2059 /* The names are relative. We store them in the form: \print$\arch\version\driver.xxx
2060 * \\server is added in the rpc server layer.
2061 * It does make sense to NOT store the server's name in the printer TDB.
2064 directory = talloc_asprintf(ctx, "\\print$\\%s\\%d\\",
2065 architecture, driver->version);
2070 #define gen_full_driver_unc_path(ctx, directory, file) \
2072 if (file && strlen(file)) { \
2073 file = talloc_asprintf(ctx, "%s%s", directory, file); \
2075 file = talloc_strdup(ctx, ""); \
2078 return (uint32_t)-1; \
2082 /* .inf files do not always list a file for each of the four standard files.
2083 * Don't prepend a path to a null filename, or client claims:
2084 * "The server on which the printer resides does not have a suitable
2085 * <printer driver name> printer driver installed. Click OK if you
2086 * wish to install the driver on your local machine."
2089 gen_full_driver_unc_path(ctx, directory, driver->driver_path);
2090 gen_full_driver_unc_path(ctx, directory, driver->data_file);
2091 gen_full_driver_unc_path(ctx, directory, driver->config_file);
2092 gen_full_driver_unc_path(ctx, directory, driver->help_file);
2094 if (driver->dependent_files && driver->dependent_files->string) {
2095 for (i=0; driver->dependent_files->string[i]; i++) {
2096 gen_full_driver_unc_path(ctx, directory,
2097 driver->dependent_files->string[i]);
2101 key = talloc_asprintf(ctx, "%s%s/%d/%s", DRIVERS_PREFIX,
2102 architecture, driver->version, driver->driver_name);
2107 DEBUG(5,("add_a_printer_driver_3: Adding driver with key %s\n", key ));
2114 len += tdb_pack(buf+len, buflen-len, "dffffffff",
2116 driver->driver_name,
2117 driver->architecture,
2118 driver->driver_path,
2120 driver->config_file,
2122 driver->monitor_name ? driver->monitor_name : "",
2123 driver->default_datatype ? driver->default_datatype : "");
2125 if (driver->dependent_files && driver->dependent_files->string) {
2126 for (i=0; driver->dependent_files->string[i]; i++) {
2127 len += tdb_pack(buf+len, buflen-len, "f",
2128 driver->dependent_files->string[i]);
2132 if (len != buflen) {
2133 buf = (uint8 *)SMB_REALLOC(buf, len);
2135 DEBUG(0,("add_a_printer_driver_3: failed to enlarge buffer\n!"));
2146 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
2150 DEBUG(0,("add_a_printer_driver_3: Adding driver with key %s failed.\n", key ));
2156 /****************************************************************************
2157 ****************************************************************************/
2159 static uint32_t add_a_printer_driver_8(struct spoolss_DriverInfo8 *driver)
2161 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
2162 struct spoolss_AddDriverInfo3 info3;
2165 convert_level_8_to_level3(mem_ctx, &info3, driver);
2167 ret = add_a_printer_driver_3(&info3);
2168 talloc_free(mem_ctx);
2173 /****************************************************************************
2174 ****************************************************************************/
2176 static WERROR get_a_printer_driver_3_default(TALLOC_CTX *mem_ctx,
2177 struct spoolss_DriverInfo3 *info,
2178 const char *driver, const char *arch)
2180 info->driver_name = talloc_strdup(mem_ctx, driver);
2181 if (!info->driver_name) {
2185 info->default_datatype = talloc_strdup(mem_ctx, "RAW");
2186 if (!info->default_datatype) {
2190 info->driver_path = talloc_strdup(mem_ctx, "");
2191 info->data_file = talloc_strdup(mem_ctx, "");
2192 info->config_file = talloc_strdup(mem_ctx, "");
2193 info->help_file = talloc_strdup(mem_ctx, "");
2194 if (!info->driver_path || !info->data_file || !info->config_file || !info->help_file) {
2201 /****************************************************************************
2202 ****************************************************************************/
2204 static WERROR get_a_printer_driver_3(TALLOC_CTX *mem_ctx,
2205 struct spoolss_DriverInfo3 *driver,
2206 const char *drivername, const char *arch,
2210 const char *architecture;
2214 fstring name, driverpath, environment, datafile, configfile, helpfile, monitorname, defaultdatatype;
2216 architecture = get_short_archi(arch);
2217 if ( !architecture ) {
2218 return WERR_UNKNOWN_PRINTER_DRIVER;
2221 /* Windows 4.0 (i.e. win9x) should always use a version of 0 */
2223 if ( strcmp( architecture, SPL_ARCH_WIN40 ) == 0 )
2226 DEBUG(8,("get_a_printer_driver_3: [%s%s/%d/%s]\n", DRIVERS_PREFIX, architecture, version, drivername));
2228 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
2229 architecture, version, drivername) < 0) {
2233 dbuf = tdb_fetch_bystring(tdb_drivers, key);
2236 return WERR_UNKNOWN_PRINTER_DRIVER;
2239 len += tdb_unpack(dbuf.dptr, dbuf.dsize, "dffffffff",
2250 driver->driver_name = talloc_strdup(mem_ctx, name);
2251 driver->architecture = talloc_strdup(mem_ctx, environment);
2252 driver->driver_path = talloc_strdup(mem_ctx, driverpath);
2253 driver->data_file = talloc_strdup(mem_ctx, datafile);
2254 driver->config_file = talloc_strdup(mem_ctx, configfile);
2255 driver->help_file = talloc_strdup(mem_ctx, helpfile);
2256 driver->monitor_name = talloc_strdup(mem_ctx, monitorname);
2257 driver->default_datatype = talloc_strdup(mem_ctx, defaultdatatype);
2261 while (len < dbuf.dsize) {
2265 driver->dependent_files = talloc_realloc(mem_ctx, driver->dependent_files, const char *, i+2);
2266 if (!driver->dependent_files ) {
2267 DEBUG(0,("get_a_printer_driver_3: failed to enlarge buffer!\n"));
2271 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "f",
2274 driver->dependent_files[i] = talloc_strdup(mem_ctx, file);
2279 if (driver->dependent_files)
2280 driver->dependent_files[i] = NULL;
2282 SAFE_FREE(dbuf.dptr);
2285 if (len != dbuf.dsize) {
2286 return get_a_printer_driver_3_default(mem_ctx, driver, drivername, arch);
2292 /****************************************************************************
2293 ****************************************************************************/
2294 int pack_devicemode(NT_DEVICEMODE *nt_devmode, uint8 *buf, int buflen)
2298 len += tdb_pack(buf+len, buflen-len, "p", nt_devmode);
2303 len += tdb_pack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2304 nt_devmode->devicename,
2305 nt_devmode->formname,
2307 nt_devmode->specversion,
2308 nt_devmode->driverversion,
2310 nt_devmode->driverextra,
2311 nt_devmode->orientation,
2312 nt_devmode->papersize,
2313 nt_devmode->paperlength,
2314 nt_devmode->paperwidth,
2317 nt_devmode->defaultsource,
2318 nt_devmode->printquality,
2321 nt_devmode->yresolution,
2322 nt_devmode->ttoption,
2323 nt_devmode->collate,
2324 nt_devmode->logpixels,
2327 nt_devmode->bitsperpel,
2328 nt_devmode->pelswidth,
2329 nt_devmode->pelsheight,
2330 nt_devmode->displayflags,
2331 nt_devmode->displayfrequency,
2332 nt_devmode->icmmethod,
2333 nt_devmode->icmintent,
2334 nt_devmode->mediatype,
2335 nt_devmode->dithertype,
2336 nt_devmode->reserved1,
2337 nt_devmode->reserved2,
2338 nt_devmode->panningwidth,
2339 nt_devmode->panningheight,
2340 nt_devmode->nt_dev_private);
2342 if (nt_devmode->nt_dev_private) {
2343 len += tdb_pack(buf+len, buflen-len, "B",
2344 nt_devmode->driverextra,
2345 nt_devmode->nt_dev_private);
2348 DEBUG(8,("Packed devicemode [%s]\n", nt_devmode->formname));
2353 /****************************************************************************
2354 Pack all values in all printer keys
2355 ***************************************************************************/
2357 static int pack_values(NT_PRINTER_DATA *data, uint8 *buf, int buflen)
2361 struct regval_blob *val;
2362 struct regval_ctr *val_ctr;
2369 /* loop over all keys */
2371 for ( i=0; i<data->num_keys; i++ ) {
2372 val_ctr = data->keys[i].values;
2373 num_values = regval_ctr_numvals( val_ctr );
2375 /* pack the keyname followed by a empty value */
2377 len += tdb_pack(buf+len, buflen-len, "pPdB",
2378 &data->keys[i].name,
2384 /* now loop over all values */
2386 for ( j=0; j<num_values; j++ ) {
2387 /* pathname should be stored as <key>\<value> */
2389 val = regval_ctr_specific_value( val_ctr, j );
2390 if (asprintf(&path, "%s\\%s",
2392 regval_name(val)) < 0) {
2396 len += tdb_pack(buf+len, buflen-len, "pPdB",
2401 regval_data_p(val) );
2403 DEBUG(8,("specific: [%s], len: %d\n", regval_name(val), regval_size(val)));
2411 len += tdb_pack(buf+len, buflen-len, "p", NULL);
2417 /****************************************************************************
2418 Delete a printer - this just deletes the printer info file, any open
2419 handles are not affected.
2420 ****************************************************************************/
2422 uint32 del_a_printer(const char *sharename)
2425 char *printdb_path = NULL;
2426 TALLOC_CTX *ctx = talloc_tos();
2428 kbuf = make_printer_tdbkey(ctx, sharename);
2429 tdb_delete(tdb_printers, kbuf);
2431 kbuf= make_printers_secdesc_tdbkey(ctx, sharename);
2432 tdb_delete(tdb_printers, kbuf);
2434 close_all_print_db();
2436 if (geteuid() == sec_initial_uid()) {
2437 if (asprintf(&printdb_path, "%s%s.tdb",
2438 cache_path("printing/"),
2442 unlink(printdb_path);
2443 SAFE_FREE(printdb_path);
2449 /****************************************************************************
2450 ****************************************************************************/
2451 static WERROR update_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info)
2457 TDB_DATA kbuf, dbuf;
2460 * in addprinter: no servername and the printer is the name
2461 * in setprinter: servername is \\server
2462 * and printer is \\server\\printer
2464 * Samba manages only local printers.
2465 * we currently don't support things like i
2466 * path=\\other_server\printer
2468 * We only store the printername, not \\server\printername
2471 if ( info->servername[0] != '\0' ) {
2472 trim_string(info->printername, info->servername, NULL);
2473 trim_char(info->printername, '\\', '\0');
2474 info->servername[0]='\0';
2478 * JFM: one day I'll forget.
2479 * below that's info->portname because that's the SAMBA sharename
2480 * and I made NT 'thinks' it's the portname
2481 * the info->sharename is the thing you can name when you add a printer
2482 * that's the short-name when you create shared printer for 95/98
2483 * So I've made a limitation in SAMBA: you can only have 1 printer model
2484 * behind a SAMBA share.
2492 len += tdb_pack(buf+len, buflen-len, "dddddddddddfffffPfffff",
2495 info->default_priority,
2512 info->printprocessor,
2516 len += pack_devicemode(info->devmode, buf+len, buflen-len);
2517 retlen = pack_values( info->data, buf+len, buflen-len );
2524 if (buflen != len) {
2525 buf = (uint8 *)SMB_REALLOC(buf, len);
2527 DEBUG(0,("update_a_printer_2: failed to enlarge buffer!\n"));
2535 kbuf = make_printer_tdbkey(talloc_tos(), info->sharename );
2540 ret = (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) == 0? WERR_OK : WERR_NOMEM);
2543 if (!W_ERROR_IS_OK(ret))
2544 DEBUG(8, ("error updating printer to tdb on disk\n"));
2548 DEBUG(8,("packed printer [%s] with driver [%s] portname=[%s] len=%d\n",
2549 info->sharename, info->drivername, info->portname, len));
2554 /****************************************************************************
2555 Create and allocate a default devicemode.
2556 ****************************************************************************/
2558 WERROR spoolss_create_default_devmode(TALLOC_CTX *mem_ctx,
2559 const char *devicename,
2560 struct spoolss_DeviceMode **devmode)
2562 struct spoolss_DeviceMode *dm;
2565 dm = talloc_zero(mem_ctx, struct spoolss_DeviceMode);
2570 dname = talloc_asprintf(dm, "%s", devicename);
2571 if (dname == NULL) {
2574 if (strlen(dname) > MAXDEVICENAME) {
2575 dname[MAXDEVICENAME] = '\0';
2577 dm->devicename = dname;
2579 dm->formname = talloc_strdup(dm, "Letter");
2580 if (dm->formname == NULL) {
2584 dm->specversion = DMSPEC_NT4_AND_ABOVE;
2585 dm->driverversion = 0x0400;
2587 dm->__driverextra_length = 0;
2588 dm->fields = DEVMODE_FORMNAME |
2590 DEVMODE_PRINTQUALITY |
2591 DEVMODE_DEFAULTSOURCE |
2595 DEVMODE_ORIENTATION;
2596 dm->orientation = DMORIENT_PORTRAIT;
2597 dm->papersize = DMPAPER_LETTER;
2598 dm->paperlength = 0;
2602 dm->defaultsource = DMBIN_FORMSOURCE;
2603 dm->printquality = DMRES_HIGH; /* 0x0258 */
2604 dm->color = DMRES_MONOCHROME;
2605 dm->duplex = DMDUP_SIMPLEX;
2606 dm->yresolution = 0;
2607 dm->ttoption = DMTT_SUBDEV;
2608 dm->collate = DMCOLLATE_FALSE;
2618 dm->displayflags = 0;
2619 dm->displayfrequency = 0;
2622 dm->panningwidth = 0;
2623 dm->panningheight = 0;
2625 dm->driverextra_data.data = NULL;
2626 dm->driverextra_data.length = 0;
2632 WERROR spoolss_create_default_secdesc(TALLOC_CTX *mem_ctx,
2633 struct spoolss_security_descriptor **secdesc)
2635 SEC_ACE ace[5]; /* max number of ace entries */
2638 SEC_ACL *psa = NULL;
2639 SEC_DESC *psd = NULL;
2643 /* Create an ACE where Everyone is allowed to print */
2645 sa = PRINTER_ACE_PRINT;
2646 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
2647 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
2649 /* Add the domain admins group if we are a DC */
2652 DOM_SID domadmins_sid;
2654 sid_compose(&domadmins_sid, get_global_sam_sid(),
2655 DOMAIN_GROUP_RID_ADMINS);
2657 sa = PRINTER_ACE_FULL_CONTROL;
2658 init_sec_ace(&ace[i++], &domadmins_sid,
2659 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
2660 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
2661 init_sec_ace(&ace[i++], &domadmins_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
2662 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
2664 else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
2665 sid_append_rid(&adm_sid, DOMAIN_USER_RID_ADMIN);
2667 sa = PRINTER_ACE_FULL_CONTROL;
2668 init_sec_ace(&ace[i++], &adm_sid,
2669 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
2670 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
2671 init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
2672 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
2675 /* add BUILTIN\Administrators as FULL CONTROL */
2677 sa = PRINTER_ACE_FULL_CONTROL;
2678 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
2679 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
2680 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
2681 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
2682 SEC_ACE_TYPE_ACCESS_ALLOWED,
2683 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
2685 /* Make the security descriptor owned by the BUILTIN\Administrators */
2687 /* The ACL revision number in rpc_secdesc.h differs from the one
2688 created by NT when setting ACE entries in printer
2689 descriptors. NT4 complains about the property being edited by a
2692 if ((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, i, ace)) != NULL) {
2693 psd = make_sec_desc(mem_ctx,
2695 SEC_DESC_SELF_RELATIVE,
2696 &global_sid_Builtin_Administrators,
2697 &global_sid_Builtin_Administrators,
2704 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
2708 DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
2709 (unsigned int)sd_size));
2716 /****************************************************************************
2717 Malloc and return an NT devicemode.
2718 ****************************************************************************/
2720 NT_DEVICEMODE *construct_nt_devicemode(const fstring default_devicename)
2723 char adevice[MAXDEVICENAME];
2724 NT_DEVICEMODE *nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE);
2726 if (nt_devmode == NULL) {
2727 DEBUG(0,("construct_nt_devicemode: malloc fail.\n"));
2731 ZERO_STRUCTP(nt_devmode);
2733 slprintf(adevice, sizeof(adevice), "%s", default_devicename);
2734 fstrcpy(nt_devmode->devicename, adevice);
2736 fstrcpy(nt_devmode->formname, "Letter");
2738 nt_devmode->specversion = DMSPEC_NT4_AND_ABOVE;
2739 nt_devmode->driverversion = 0x0400;
2740 nt_devmode->size = 0x00DC;
2741 nt_devmode->driverextra = 0x0000;
2742 nt_devmode->fields = DEVMODE_FORMNAME |
2744 DEVMODE_PRINTQUALITY |
2745 DEVMODE_DEFAULTSOURCE |
2749 DEVMODE_ORIENTATION;
2750 nt_devmode->orientation = DMORIENT_PORTRAIT;
2751 nt_devmode->papersize = DMPAPER_LETTER;
2752 nt_devmode->paperlength = 0;
2753 nt_devmode->paperwidth = 0;
2754 nt_devmode->scale = 0x64;
2755 nt_devmode->copies = 1;
2756 nt_devmode->defaultsource = DMBIN_FORMSOURCE;
2757 nt_devmode->printquality = DMRES_HIGH; /* 0x0258 */
2758 nt_devmode->color = DMRES_MONOCHROME;
2759 nt_devmode->duplex = DMDUP_SIMPLEX;
2760 nt_devmode->yresolution = 0;
2761 nt_devmode->ttoption = DMTT_SUBDEV;
2762 nt_devmode->collate = DMCOLLATE_FALSE;
2763 nt_devmode->icmmethod = 0;
2764 nt_devmode->icmintent = 0;
2765 nt_devmode->mediatype = 0;
2766 nt_devmode->dithertype = 0;
2768 /* non utilisés par un driver d'imprimante */
2769 nt_devmode->logpixels = 0;
2770 nt_devmode->bitsperpel = 0;
2771 nt_devmode->pelswidth = 0;
2772 nt_devmode->pelsheight = 0;
2773 nt_devmode->displayflags = 0;
2774 nt_devmode->displayfrequency = 0;
2775 nt_devmode->reserved1 = 0;
2776 nt_devmode->reserved2 = 0;
2777 nt_devmode->panningwidth = 0;
2778 nt_devmode->panningheight = 0;
2780 nt_devmode->nt_dev_private = NULL;
2784 /****************************************************************************
2785 Clean up and deallocate a (maybe partially) allocated NT_DEVICEMODE.
2786 ****************************************************************************/
2788 void free_nt_devicemode(NT_DEVICEMODE **devmode_ptr)
2790 NT_DEVICEMODE *nt_devmode = *devmode_ptr;
2792 if(nt_devmode == NULL)
2795 DEBUG(106,("free_nt_devicemode: deleting DEVMODE\n"));
2797 SAFE_FREE(nt_devmode->nt_dev_private);
2798 SAFE_FREE(*devmode_ptr);
2801 /****************************************************************************
2802 Clean up and deallocate a (maybe partially) allocated NT_PRINTER_INFO_LEVEL_2.
2803 ****************************************************************************/
2805 static void free_nt_printer_info_level_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr)
2807 NT_PRINTER_INFO_LEVEL_2 *info = *info_ptr;
2812 free_nt_devicemode(&info->devmode);
2814 TALLOC_FREE( *info_ptr );
2818 /****************************************************************************
2819 ****************************************************************************/
2820 int unpack_devicemode(NT_DEVICEMODE **nt_devmode, const uint8 *buf, int buflen)
2824 NT_DEVICEMODE devmode;
2826 ZERO_STRUCT(devmode);
2828 len += tdb_unpack(buf+len, buflen-len, "p", nt_devmode);
2830 if (!*nt_devmode) return len;
2832 len += tdb_unpack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2836 &devmode.specversion,
2837 &devmode.driverversion,
2839 &devmode.driverextra,
2840 &devmode.orientation,
2842 &devmode.paperlength,
2843 &devmode.paperwidth,
2846 &devmode.defaultsource,
2847 &devmode.printquality,
2850 &devmode.yresolution,
2856 &devmode.bitsperpel,
2858 &devmode.pelsheight,
2859 &devmode.displayflags,
2860 &devmode.displayfrequency,
2864 &devmode.dithertype,
2867 &devmode.panningwidth,
2868 &devmode.panningheight,
2869 &devmode.nt_dev_private);
2871 if (devmode.nt_dev_private) {
2872 /* the len in tdb_unpack is an int value and
2873 * devmode.driverextra is only a short
2875 len += tdb_unpack(buf+len, buflen-len, "B", &extra_len, &devmode.nt_dev_private);
2876 devmode.driverextra=(uint16)extra_len;
2878 /* check to catch an invalid TDB entry so we don't segfault */
2879 if (devmode.driverextra == 0) {
2880 devmode.nt_dev_private = NULL;
2884 *nt_devmode = (NT_DEVICEMODE *)memdup(&devmode, sizeof(devmode));
2886 SAFE_FREE(devmode.nt_dev_private);
2890 DEBUG(8,("Unpacked devicemode [%s](%s)\n", devmode.devicename, devmode.formname));
2891 if (devmode.nt_dev_private)
2892 DEBUG(8,("with a private section of %d bytes\n", devmode.driverextra));
2897 /****************************************************************************
2898 Allocate and initialize a new slot.
2899 ***************************************************************************/
2901 int add_new_printer_key( NT_PRINTER_DATA *data, const char *name )
2906 if ( !name || !data )
2909 /* allocate another slot in the NT_PRINTER_KEY array */
2911 if ( !(d = TALLOC_REALLOC_ARRAY( data, data->keys, NT_PRINTER_KEY, data->num_keys+1)) ) {
2912 DEBUG(0,("add_new_printer_key: Realloc() failed!\n"));
2918 key_index = data->num_keys;
2920 /* initialze new key */
2922 data->keys[key_index].name = talloc_strdup( data, name );
2924 if ( !(data->keys[key_index].values = TALLOC_ZERO_P( data, struct regval_ctr )) )
2929 DEBUG(10,("add_new_printer_key: Inserted new data key [%s]\n", name ));
2934 /****************************************************************************
2935 search for a registry key name in the existing printer data
2936 ***************************************************************************/
2938 int delete_printer_key( NT_PRINTER_DATA *data, const char *name )
2942 for ( i=0; i<data->num_keys; i++ ) {
2943 if ( strequal( data->keys[i].name, name ) ) {
2945 /* cleanup memory */
2947 TALLOC_FREE( data->keys[i].name );
2948 TALLOC_FREE( data->keys[i].values );
2950 /* if not the end of the array, move remaining elements down one slot */
2953 if ( data->num_keys && (i < data->num_keys) )
2954 memmove( &data->keys[i], &data->keys[i+1], sizeof(NT_PRINTER_KEY)*(data->num_keys-i) );
2961 return data->num_keys;
2964 /****************************************************************************
2965 search for a registry key name in the existing printer data
2966 ***************************************************************************/
2968 int lookup_printerkey( NT_PRINTER_DATA *data, const char *name )
2973 if ( !data || !name )
2976 DEBUG(12,("lookup_printerkey: Looking for [%s]\n", name));
2978 /* loop over all existing keys */
2980 for ( i=0; i<data->num_keys; i++ ) {
2981 if ( strequal(data->keys[i].name, name) ) {
2982 DEBUG(12,("lookup_printerkey: Found [%s]!\n", name));
2992 /****************************************************************************
2993 ***************************************************************************/
2995 int get_printer_subkeys( NT_PRINTER_DATA *data, const char* key, fstring **subkeys )
2999 int num_subkeys = 0;
3001 fstring *subkeys_ptr = NULL;
3012 /* special case of asking for the top level printer data registry key names */
3014 if ( strlen(key) == 0 ) {
3015 for ( i=0; i<data->num_keys; i++ ) {
3017 /* found a match, so allocate space and copy the name */
3019 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
3020 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
3025 fstrcpy( subkeys_ptr[num_subkeys], data->keys[i].name );
3032 /* asking for the subkeys of some key */
3033 /* subkey paths are stored in the key name using '\' as the delimiter */
3035 for ( i=0; i<data->num_keys; i++ ) {
3036 if ( StrnCaseCmp(data->keys[i].name, key, strlen(key)) == 0 ) {
3038 /* if we found the exact key, then break */
3039 key_len = strlen( key );
3040 if ( strlen(data->keys[i].name) == key_len )
3043 /* get subkey path */
3045 p = data->keys[i].name + key_len;
3048 fstrcpy( subkeyname, p );
3049 if ( (p = strchr( subkeyname, '\\' )) )
3052 /* don't add a key more than once */
3054 for ( j=0; j<num_subkeys; j++ ) {
3055 if ( strequal( subkeys_ptr[j], subkeyname ) )
3059 if ( j != num_subkeys )
3062 /* found a match, so allocate space and copy the name */
3064 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
3065 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
3070 fstrcpy( subkeys_ptr[num_subkeys], subkeyname );
3076 /* return error if the key was not found */
3078 if ( i == data->num_keys ) {
3079 SAFE_FREE(subkeys_ptr);
3084 /* tag off the end */
3087 fstrcpy(subkeys_ptr[num_subkeys], "" );
3089 *subkeys = subkeys_ptr;
3095 static void map_sz_into_ctr(struct regval_ctr *ctr, const char *val_name,
3098 regval_ctr_delvalue(ctr, val_name);
3099 regval_ctr_addvalue_sz(ctr, val_name, sz);
3102 static void map_dword_into_ctr(struct regval_ctr *ctr, const char *val_name,
3105 regval_ctr_delvalue(ctr, val_name);
3106 regval_ctr_addvalue(ctr, val_name, REG_DWORD,
3107 (char *) &dword, sizeof(dword));
3110 static void map_bool_into_ctr(struct regval_ctr *ctr, const char *val_name,
3113 uint8 bin_bool = (b ? 1 : 0);
3114 regval_ctr_delvalue(ctr, val_name);
3115 regval_ctr_addvalue(ctr, val_name, REG_BINARY,
3116 (char *) &bin_bool, sizeof(bin_bool));
3119 static void map_single_multi_sz_into_ctr(struct regval_ctr *ctr, const char *val_name,
3120 const char *multi_sz)
3127 regval_ctr_delvalue(ctr, val_name);
3128 regval_ctr_addvalue_multi_sz(ctr, val_name, a);
3131 /****************************************************************************
3132 * Map the NT_PRINTER_INFO_LEVEL_2 data into DsSpooler keys for publishing.
3134 * @param info2 NT_PRINTER_INFO_LEVEL_2 describing printer - gets modified
3135 * @return bool indicating success or failure
3136 ***************************************************************************/
3138 static bool map_nt_printer_info2_to_dsspooler(NT_PRINTER_INFO_LEVEL_2 *info2)
3140 struct regval_ctr *ctr = NULL;
3142 const char *dnssuffix;
3143 char *allocated_string = NULL;
3144 const char *ascii_str;
3147 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3148 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3149 ctr = info2->data->keys[i].values;
3151 map_sz_into_ctr(ctr, SPOOL_REG_PRINTERNAME, info2->sharename);
3152 map_sz_into_ctr(ctr, SPOOL_REG_SHORTSERVERNAME, global_myname());
3154 /* we make the assumption that the netbios name is the same
3155 as the DNS name sinc ethe former will be what we used to
3158 dnssuffix = get_mydnsdomname(talloc_tos());
3159 if (dnssuffix && *dnssuffix) {
3160 fstr_sprintf( longname, "%s.%s", global_myname(), dnssuffix );
3162 fstrcpy( longname, global_myname() );
3165 map_sz_into_ctr(ctr, SPOOL_REG_SERVERNAME, longname);
3167 if (asprintf(&allocated_string, "\\\\%s\\%s", longname, info2->sharename) == -1) {
3170 map_sz_into_ctr(ctr, SPOOL_REG_UNCNAME, allocated_string);
3171 SAFE_FREE(allocated_string);
3173 map_dword_into_ctr(ctr, SPOOL_REG_VERSIONNUMBER, 4);
3174 map_sz_into_ctr(ctr, SPOOL_REG_DRIVERNAME, info2->drivername);
3175 map_sz_into_ctr(ctr, SPOOL_REG_LOCATION, info2->location);
3176 map_sz_into_ctr(ctr, SPOOL_REG_DESCRIPTION, info2->comment);
3177 map_single_multi_sz_into_ctr(ctr, SPOOL_REG_PORTNAME, info2->portname);
3178 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSEPARATORFILE, info2->sepfile);
3179 map_dword_into_ctr(ctr, SPOOL_REG_PRINTSTARTTIME, info2->starttime);
3180 map_dword_into_ctr(ctr, SPOOL_REG_PRINTENDTIME, info2->untiltime);
3181 map_dword_into_ctr(ctr, SPOOL_REG_PRIORITY, info2->priority);
3183 map_bool_into_ctr(ctr, SPOOL_REG_PRINTKEEPPRINTEDJOBS,
3184 (info2->attributes &
3185 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
3187 switch (info2->attributes & 0x3) {
3189 ascii_str = SPOOL_REGVAL_PRINTWHILESPOOLING;
3192 ascii_str = SPOOL_REGVAL_PRINTAFTERSPOOLED;
3195 ascii_str = SPOOL_REGVAL_PRINTDIRECT;
3198 ascii_str = "unknown";
3200 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSPOOLING, ascii_str);
3205 /*****************************************************************
3206 ****************************************************************/
3208 static void store_printer_guid(NT_PRINTER_INFO_LEVEL_2 *info2,
3212 struct regval_ctr *ctr=NULL;
3214 /* find the DsSpooler key */
3215 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3216 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3217 ctr = info2->data->keys[i].values;
3219 regval_ctr_delvalue(ctr, "objectGUID");
3221 /* We used to store this as a REG_BINARY but that causes
3224 regval_ctr_addvalue_sz(ctr, "objectGUID",
3225 GUID_string(talloc_tos(), &guid));
3228 static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
3229 NT_PRINTER_INFO_LEVEL *printer)
3233 char *prt_dn = NULL, *srv_dn, *srv_cn_0, *srv_cn_escaped, *sharename_escaped;
3234 char *srv_dn_utf8, **srv_cn_utf8;
3237 const char *attrs[] = {"objectGUID", NULL};
3239 WERROR win_rc = WERR_OK;
3240 size_t converted_size;
3242 /* build the ads mods */
3243 ctx = talloc_init("nt_printer_publish_ads");
3248 DEBUG(5, ("publishing printer %s\n", printer->info_2->printername));
3250 /* figure out where to publish */
3251 ads_find_machine_acct(ads, &res, global_myname());
3253 /* We use ldap_get_dn here as we need the answer
3254 * in utf8 to call ldap_explode_dn(). JRA. */
3256 srv_dn_utf8 = ldap_get_dn((LDAP *)ads->ldap.ld, (LDAPMessage *)res);
3259 return WERR_SERVER_UNAVAILABLE;
3261 ads_msgfree(ads, res);
3262 srv_cn_utf8 = ldap_explode_dn(srv_dn_utf8, 1);
3265 ldap_memfree(srv_dn_utf8);
3266 return WERR_SERVER_UNAVAILABLE;
3268 /* Now convert to CH_UNIX. */
3269 if (!pull_utf8_talloc(ctx, &srv_dn, srv_dn_utf8, &converted_size)) {
3271 ldap_memfree(srv_dn_utf8);
3272 ldap_memfree(srv_cn_utf8);
3273 return WERR_SERVER_UNAVAILABLE;
3275 if (!pull_utf8_talloc(ctx, &srv_cn_0, srv_cn_utf8[0], &converted_size)) {
3277 ldap_memfree(srv_dn_utf8);
3278 ldap_memfree(srv_cn_utf8);
3279 TALLOC_FREE(srv_dn);
3280 return WERR_SERVER_UNAVAILABLE;
3283 ldap_memfree(srv_dn_utf8);
3284 ldap_memfree(srv_cn_utf8);
3286 srv_cn_escaped = escape_rdn_val_string_alloc(srv_cn_0);
3287 if (!srv_cn_escaped) {
3289 return WERR_SERVER_UNAVAILABLE;
3291 sharename_escaped = escape_rdn_val_string_alloc(printer->info_2->sharename);
3292 if (!sharename_escaped) {
3293 SAFE_FREE(srv_cn_escaped);
3295 return WERR_SERVER_UNAVAILABLE;
3298 prt_dn = talloc_asprintf(ctx, "cn=%s-%s,%s", srv_cn_escaped, sharename_escaped, srv_dn);
3300 SAFE_FREE(srv_cn_escaped);
3301 SAFE_FREE(sharename_escaped);
3303 mods = ads_init_mods(ctx);
3311 get_local_printer_publishing_data(ctx, &mods, printer->info_2->data);
3312 ads_mod_str(ctx, &mods, SPOOL_REG_PRINTERNAME,
3313 printer->info_2->sharename);
3316 ads_rc = ads_mod_printer_entry(ads, prt_dn, ctx, &mods);
3317 if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT) {
3319 for (i=0; mods[i] != 0; i++)
3321 mods[i] = (LDAPMod *)-1;
3322 ads_rc = ads_add_printer_entry(ads, prt_dn, ctx, &mods);
3325 if (!ADS_ERR_OK(ads_rc))
3326 DEBUG(3, ("error publishing %s: %s\n", printer->info_2->sharename, ads_errstr(ads_rc)));
3328 /* retreive the guid and store it locally */
3329 if (ADS_ERR_OK(ads_search_dn(ads, &res, prt_dn, attrs))) {
3331 ads_pull_guid(ads, res, &guid);
3332 ads_msgfree(ads, res);
3333 store_printer_guid(printer->info_2, guid);
3334 win_rc = mod_a_printer(printer, 2);
3341 static WERROR nt_printer_unpublish_ads(ADS_STRUCT *ads,
3342 NT_PRINTER_INFO_LEVEL *printer)
3345 LDAPMessage *res = NULL;
3346 char *prt_dn = NULL;
3348 DEBUG(5, ("unpublishing printer %s\n", printer->info_2->printername));
3350 /* remove the printer from the directory */
3351 ads_rc = ads_find_printer_on_server(ads, &res,
3352 printer->info_2->sharename, global_myname());
3354 if (ADS_ERR_OK(ads_rc) && res && ads_count_replies(ads, res)) {
3355 prt_dn = ads_get_dn(ads, talloc_tos(), res);
3357 ads_msgfree(ads, res);
3360 ads_rc = ads_del_dn(ads, prt_dn);
3361 TALLOC_FREE(prt_dn);
3365 ads_msgfree(ads, res);
3370 /****************************************************************************
3371 * Publish a printer in the directory
3373 * @param snum describing printer service
3374 * @return WERROR indicating status of publishing
3375 ***************************************************************************/
3377 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3380 ADS_STRUCT *ads = NULL;
3381 NT_PRINTER_INFO_LEVEL *printer = NULL;
3384 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3385 if (!W_ERROR_IS_OK(win_rc))
3389 case DSPRINT_PUBLISH:
3390 case DSPRINT_UPDATE:
3391 /* set the DsSpooler info and attributes */
3392 if (!(map_nt_printer_info2_to_dsspooler(printer->info_2))) {
3393 win_rc = WERR_NOMEM;
3397 printer->info_2->attributes |= PRINTER_ATTRIBUTE_PUBLISHED;
3399 case DSPRINT_UNPUBLISH:
3400 printer->info_2->attributes ^= PRINTER_ATTRIBUTE_PUBLISHED;
3403 win_rc = WERR_NOT_SUPPORTED;
3407 win_rc = mod_a_printer(printer, 2);
3408 if (!W_ERROR_IS_OK(win_rc)) {
3409 DEBUG(3, ("err %d saving data\n", W_ERROR_V(win_rc)));
3413 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3415 DEBUG(3, ("ads_init() failed\n"));
3416 win_rc = WERR_SERVER_UNAVAILABLE;
3419 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3420 SAFE_FREE(ads->auth.password);
3421 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3424 /* ads_connect() will find the DC for us */
3425 ads_rc = ads_connect(ads);
3426 if (!ADS_ERR_OK(ads_rc)) {
3427 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3428 win_rc = WERR_ACCESS_DENIED;
3433 case DSPRINT_PUBLISH:
3434 case DSPRINT_UPDATE:
3435 win_rc = nt_printer_publish_ads(ads, printer);
3437 case DSPRINT_UNPUBLISH:
3438 win_rc = nt_printer_unpublish_ads(ads, printer);
3443 free_a_printer(&printer, 2);
3448 WERROR check_published_printers(void)
3451 ADS_STRUCT *ads = NULL;
3453 int n_services = lp_numservices();
3454 NT_PRINTER_INFO_LEVEL *printer = NULL;
3456 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3458 DEBUG(3, ("ads_init() failed\n"));
3459 return WERR_SERVER_UNAVAILABLE;
3461 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3462 SAFE_FREE(ads->auth.password);
3463 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3466 /* ads_connect() will find the DC for us */
3467 ads_rc = ads_connect(ads);
3468 if (!ADS_ERR_OK(ads_rc)) {
3469 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3471 ads_kdestroy("MEMORY:prtpub_cache");
3472 return WERR_ACCESS_DENIED;
3475 for (snum = 0; snum < n_services; snum++) {
3476 if (!(lp_snum_ok(snum) && lp_print_ok(snum)))
3479 if (W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2,
3480 lp_servicename(snum))) &&
3481 (printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED))
3482 nt_printer_publish_ads(ads, printer);
3484 free_a_printer(&printer, 2);
3488 ads_kdestroy("MEMORY:prtpub_cache");
3492 bool is_printer_published(Printer_entry *print_hnd, int snum,
3495 NT_PRINTER_INFO_LEVEL *printer = NULL;
3496 struct regval_ctr *ctr;
3497 struct regval_blob *guid_val;
3503 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3505 if (!W_ERROR_IS_OK(win_rc) ||
3506 !(printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED) ||
3507 ((i = lookup_printerkey(printer->info_2->data, SPOOL_DSSPOOLER_KEY)) < 0) ||
3508 !(ctr = printer->info_2->data->keys[i].values) ||
3509 !(guid_val = regval_ctr_getvalue(ctr, "objectGUID")))
3511 free_a_printer(&printer, 2);
3515 /* fetching printer guids really ought to be a separate function. */
3520 /* We used to store the guid as REG_BINARY, then swapped
3521 to REG_SZ for Vista compatibility so check for both */
3523 switch ( regval_type(guid_val) ){
3525 blob = data_blob_const(regval_data_p(guid_val),
3526 regval_size(guid_val));
3527 pull_reg_sz(talloc_tos(), NULL, &blob, (const char **)&guid_str);
3528 ret = NT_STATUS_IS_OK(GUID_from_string( guid_str, guid ));
3529 talloc_free(guid_str);
3532 if ( regval_size(guid_val) != sizeof(struct GUID) ) {
3536 memcpy(guid, regval_data_p(guid_val), sizeof(struct GUID));
3539 DEBUG(0,("is_printer_published: GUID value stored as "
3540 "invaluid type (%d)\n", regval_type(guid_val) ));
3545 free_a_printer(&printer, 2);
3549 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3554 WERROR check_published_printers(void)
3559 bool is_printer_published(Printer_entry *print_hnd, int snum,
3564 #endif /* HAVE_ADS */
3566 /****************************************************************************
3567 ***************************************************************************/
3569 WERROR delete_all_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key )
3571 NT_PRINTER_DATA *data;
3573 int removed_keys = 0;
3577 empty_slot = data->num_keys;
3580 return WERR_INVALID_PARAM;
3582 /* remove all keys */
3584 if ( !strlen(key) ) {
3586 TALLOC_FREE( data );
3590 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from printer [%s]\n",
3596 /* remove a specific key (and all subkeys) */
3598 for ( i=0; i<data->num_keys; i++ ) {
3599 if ( StrnCaseCmp( data->keys[i].name, key, strlen(key)) == 0 ) {
3600 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from key [%s]\n",
3601 data->keys[i].name));
3603 TALLOC_FREE( data->keys[i].name );
3604 TALLOC_FREE( data->keys[i].values );
3606 /* mark the slot as empty */
3608 ZERO_STRUCTP( &data->keys[i] );
3612 /* find the first empty slot */
3614 for ( i=0; i<data->num_keys; i++ ) {
3615 if ( !data->keys[i].name ) {
3622 if ( i == data->num_keys )
3623 /* nothing was removed */
3624 return WERR_INVALID_PARAM;
3626 /* move everything down */
3628 for ( i=empty_slot+1; i<data->num_keys; i++ ) {
3629 if ( data->keys[i].name ) {
3630 memcpy( &data->keys[empty_slot], &data->keys[i], sizeof(NT_PRINTER_KEY) );
3631 ZERO_STRUCTP( &data->keys[i] );
3639 data->num_keys -= removed_keys;
3641 /* sanity check to see if anything is left */
3643 if ( !data->num_keys ) {
3644 DEBUG(8,("delete_all_printer_data: No keys left for printer [%s]\n", p2->printername ));
3646 SAFE_FREE( data->keys );
3647 ZERO_STRUCTP( data );
3653 /****************************************************************************
3654 ***************************************************************************/
3656 WERROR delete_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3658 WERROR result = WERR_OK;
3661 /* we must have names on non-zero length */
3663 if ( !key || !*key|| !value || !*value )
3664 return WERR_INVALID_NAME;
3666 /* find the printer key first */
3668 key_index = lookup_printerkey( p2->data, key );
3669 if ( key_index == -1 )
3672 /* make sure the value exists so we can return the correct error code */
3674 if ( !regval_ctr_getvalue( p2->data->keys[key_index].values, value ) )
3675 return WERR_BADFILE;
3677 regval_ctr_delvalue( p2->data->keys[key_index].values, value );
3679 DEBUG(8,("delete_printer_data: Removed key => [%s], value => [%s]\n",
3685 /****************************************************************************
3686 ***************************************************************************/
3688 WERROR add_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value,
3689 uint32 type, uint8 *data, int real_len )
3691 WERROR result = WERR_OK;
3694 /* we must have names on non-zero length */
3696 if ( !key || !*key|| !value || !*value )
3697 return WERR_INVALID_NAME;
3699 /* find the printer key first */
3701 key_index = lookup_printerkey( p2->data, key );
3702 if ( key_index == -1 )
3703 key_index = add_new_printer_key( p2->data, key );
3705 if ( key_index == -1 )
3708 regval_ctr_addvalue( p2->data->keys[key_index].values, value,
3709 type, (const char *)data, real_len );
3711 DEBUG(8,("add_printer_data: Added key => [%s], value => [%s], type=> [%d], size => [%d]\n",
3712 key, value, type, real_len ));
3717 /****************************************************************************
3718 ***************************************************************************/
3720 struct regval_blob* get_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3724 if ( (key_index = lookup_printerkey( p2->data, key )) == -1 )
3727 DEBUG(8,("get_printer_data: Attempting to lookup key => [%s], value => [%s]\n",
3730 return regval_ctr_getvalue( p2->data->keys[key_index].values, value );
3733 /****************************************************************************
3734 Unpack a list of registry values frem the TDB
3735 ***************************************************************************/
3737 static int unpack_values(NT_PRINTER_DATA *printer_data, const uint8 *buf, int buflen)
3742 const char *valuename = NULL;
3743 const char *keyname = NULL;
3747 struct regval_blob *regval_p;
3750 /* add the "PrinterDriverData" key first for performance reasons */
3752 add_new_printer_key( printer_data, SPOOL_PRINTERDATA_KEY );
3754 /* loop and unpack the rest of the registry values */
3758 /* check to see if there are any more registry values */
3761 len += tdb_unpack(buf+len, buflen-len, "p", ®val_p);
3765 /* unpack the next regval */
3767 len += tdb_unpack(buf+len, buflen-len, "fdB",
3773 /* lookup for subkey names which have a type of REG_NONE */
3774 /* there's no data with this entry */
3776 if ( type == REG_NONE ) {
3777 if ( (key_index=lookup_printerkey( printer_data, string)) == -1 )
3778 add_new_printer_key( printer_data, string );
3783 * break of the keyname from the value name.
3784 * Valuenames can have embedded '\'s so be careful.
3785 * only support one level of keys. See the
3786 * "Konica Fiery S300 50C-K v1.1. enu" 2k driver.
3790 str = strchr_m( string, '\\');
3792 /* Put in "PrinterDriverData" is no key specified */
3795 keyname = SPOOL_PRINTERDATA_KEY;
3804 /* see if we need a new key */
3806 if ( (key_index=lookup_printerkey( printer_data, keyname )) == -1 )
3807 key_index = add_new_printer_key( printer_data, keyname );
3809 if ( key_index == -1 ) {
3810 DEBUG(0,("unpack_values: Failed to allocate a new key [%s]!\n",
3815 DEBUG(8,("specific: [%s:%s], len: %d\n", keyname, valuename, size));
3817 /* Vista doesn't like unknown REG_BINARY values in DsSpooler.
3818 Thanks to Martin Zielinski for the hint. */
3820 if ( type == REG_BINARY &&
3821 strequal( keyname, SPOOL_DSSPOOLER_KEY ) &&
3822 strequal( valuename, "objectGUID" ) )
3826 /* convert the GUID to a UNICODE string */
3828 memcpy( &guid, data_p, sizeof(struct GUID) );
3830 regval_ctr_addvalue_sz(printer_data->keys[key_index].values,
3832 GUID_string(talloc_tos(), &guid));
3837 regval_ctr_addvalue( printer_data->keys[key_index].values,
3838 valuename, type, (const char *)data_p,
3842 SAFE_FREE(data_p); /* 'B' option to tdbpack does a malloc() */
3849 /****************************************************************************
3850 ***************************************************************************/
3852 static char *last_from;
3853 static char *last_to;
3855 static const char *get_last_from(void)
3863 static const char *get_last_to(void)
3871 static bool set_last_from_to(const char *from, const char *to)
3873 char *orig_from = last_from;
3874 char *orig_to = last_to;
3876 last_from = SMB_STRDUP(from);
3877 last_to = SMB_STRDUP(to);
3879 SAFE_FREE(orig_from);
3882 if (!last_from || !last_to) {
3883 SAFE_FREE(last_from);
3890 static void map_to_os2_driver(fstring drivername)
3892 char *mapfile = lp_os2_driver_map();
3893 char **lines = NULL;
3897 if (!strlen(drivername))
3903 if (strequal(drivername,get_last_from())) {
3904 DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",
3905 drivername,get_last_to()));
3906 fstrcpy(drivername,get_last_to());
3910 lines = file_lines_load(mapfile, &numlines,0,NULL);
3911 if (numlines == 0 || lines == NULL) {
3912 DEBUG(0,("No entries in OS/2 driver map %s\n",mapfile));
3917 DEBUG(4,("Scanning OS/2 driver map %s\n",mapfile));
3919 for( i = 0; i < numlines; i++) {
3920 char *nt_name = lines[i];
3921 char *os2_name = strchr(nt_name,'=');
3928 while (isspace(*nt_name))
3931 if (!*nt_name || strchr("#;",*nt_name))
3935 int l = strlen(nt_name);
3936 while (l && isspace(nt_name[l-1])) {
3942 while (isspace(*os2_name))
3946 int l = strlen(os2_name);
3947 while (l && isspace(os2_name[l-1])) {
3953 if (strequal(nt_name,drivername)) {
3954 DEBUG(3,("Mapped windows driver %s to os2 driver%s\n",drivername,os2_name));
3955 set_last_from_to(drivername,os2_name);
3956 fstrcpy(drivername,os2_name);
3965 /****************************************************************************
3966 Get a default printer info 2 struct.
3967 ****************************************************************************/
3969 static WERROR get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 *info,
3970 const char *servername,
3971 const char* sharename,
3974 int snum = lp_servicenumber(sharename);
3976 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
3977 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
3978 servername, sharename);
3979 fstrcpy(info->sharename, sharename);
3980 fstrcpy(info->portname, SAMBA_PRINTER_PORT_NAME);
3982 /* by setting the driver name to an empty string, a local NT admin
3983 can now run the **local** APW to install a local printer driver
3984 for a Samba shared printer in 2.2. Without this, drivers **must** be
3985 installed on the Samba server for NT clients --jerry */
3986 #if 0 /* JERRY --do not uncomment-- */
3987 if (!*info->drivername)
3988 fstrcpy(info->drivername, "NO DRIVER AVAILABLE FOR THIS PRINTER");
3992 DEBUG(10,("get_a_printer_2_default: driver name set to [%s]\n", info->drivername));
3994 strlcpy(info->comment, "", sizeof(info->comment));
3995 fstrcpy(info->printprocessor, "winprint");
3996 fstrcpy(info->datatype, "RAW");
3999 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
4000 /* Pull the location and comment strings from cups if we don't
4002 if ( !strlen(info->location) || !strlen(info->comment) ) {
4003 char *comment = NULL;
4004 char *location = NULL;
4005 if (cups_pull_comment_location(info, info->sharename,
4006 &comment, &location)) {
4007 strlcpy(info->comment, comment, sizeof(info->comment));
4008 fstrcpy(info->location, location);
4009 TALLOC_FREE(comment);
4010 TALLOC_FREE(location);
4016 info->attributes = PRINTER_ATTRIBUTE_SAMBA;
4018 info->starttime = 0; /* Minutes since 12:00am GMT */
4019 info->untiltime = 0; /* Minutes since 12:00am GMT */
4021 info->default_priority = 1;
4022 info->setuptime = (uint32)time(NULL);
4025 * I changed this as I think it is better to have a generic
4026 * DEVMODE than to crash Win2k explorer.exe --jerry
4027 * See the HP Deskjet 990c Win2k drivers for an example.
4029 * However the default devmode appears to cause problems
4030 * with the HP CLJ 8500 PCL driver. Hence the addition of
4031 * the "default devmode" parameter --jerry 22/01/2002
4034 if (lp_default_devmode(snum)) {
4035 if ((info->devmode = construct_nt_devicemode(info->printername)) == NULL) {
4039 info->devmode = NULL;
4042 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
4046 info->data = TALLOC_ZERO_P(info, NT_PRINTER_DATA);
4051 add_new_printer_key(info->data, SPOOL_PRINTERDATA_KEY);
4057 free_nt_devicemode(&info->devmode);
4059 return WERR_ACCESS_DENIED;
4062 /****************************************************************************
4063 ****************************************************************************/
4065 static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info,
4066 const char *servername,
4067 const char *sharename,
4071 int snum = lp_servicenumber(sharename);
4072 TDB_DATA kbuf, dbuf;
4073 fstring printername;
4074 char adevice[MAXDEVICENAME];
4075 char *comment = NULL;
4077 kbuf = make_printer_tdbkey(talloc_tos(), sharename);
4079 dbuf = tdb_fetch(tdb_printers, kbuf);
4081 return get_a_printer_2_default(info, servername,
4082 sharename, get_loc_com);
4085 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "dddddddddddfffffPfffff",
4088 &info->default_priority,
4095 &info->c_setprinter,
4105 info->printprocessor,
4110 strlcpy(info->comment, comment, sizeof(info->comment));
4114 /* Samba has to have shared raw drivers. */
4115 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
4116 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
4118 /* Restore the stripped strings. */
4119 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
4121 if ( lp_force_printername(snum) ) {
4122 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, sharename );
4124 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, info->printername);
4127 fstrcpy(info->printername, printername);
4130 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
4131 /* Pull the location and comment strings from cups if we don't
4133 if ( !strlen(info->location) || !strlen(info->comment) ) {
4134 char *location = NULL;
4136 if (cups_pull_comment_location(info, info->sharename,
4137 &comment, &location)) {
4138 strlcpy(info->comment, comment, sizeof(info->comment));
4139 fstrcpy(info->location, location);
4140 TALLOC_FREE(comment);
4141 TALLOC_FREE(location);
4147 len += unpack_devicemode(&info->devmode,dbuf.dptr+len, dbuf.dsize-len);
4150 * Some client drivers freak out if there is a NULL devmode
4151 * (probably the driver is not checking before accessing
4152 * the devmode pointer) --jerry
4154 * See comments in get_a_printer_2_default()
4157 if (lp_default_devmode(snum) && !info->devmode) {
4158 DEBUG(8,("get_a_printer_2: Constructing a default device mode for [%s]\n",
4160 info->devmode = construct_nt_devicemode(printername);
4163 slprintf( adevice, sizeof(adevice), "%s", info->printername );
4164 if (info->devmode) {
4165 fstrcpy(info->devmode->devicename, adevice);
4168 if ( !(info->data = TALLOC_ZERO_P( info, NT_PRINTER_DATA )) ) {
4169 DEBUG(0,("unpack_values: talloc() failed!\n"));
4170 SAFE_FREE(dbuf.dptr);
4173 len += unpack_values( info->data, dbuf.dptr+len, dbuf.dsize-len );
4175 /* This will get the current RPC talloc context, but we should be
4176 passing this as a parameter... fixme... JRA ! */
4178 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
4179 SAFE_FREE(dbuf.dptr);
4183 /* Fix for OS/2 drivers. */
4185 if (get_remote_arch() == RA_OS2) {
4186 map_to_os2_driver(info->drivername);
4189 SAFE_FREE(dbuf.dptr);
4191 DEBUG(9,("Unpacked printer [%s] name [%s] running driver [%s]\n",
4192 sharename, info->printername, info->drivername));
4197 /****************************************************************************
4198 Debugging function, dump at level 6 the struct in the logs.
4199 ****************************************************************************/
4200 static uint32 dump_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4203 NT_PRINTER_INFO_LEVEL_2 *info2;
4205 DEBUG(106,("Dumping printer at level [%d]\n", level));
4210 if (printer->info_2 == NULL)
4214 info2=printer->info_2;
4216 DEBUGADD(106,("attributes:[%d]\n", info2->attributes));
4217 DEBUGADD(106,("priority:[%d]\n", info2->priority));
4218 DEBUGADD(106,("default_priority:[%d]\n", info2->default_priority));
4219 DEBUGADD(106,("starttime:[%d]\n", info2->starttime));
4220 DEBUGADD(106,("untiltime:[%d]\n", info2->untiltime));
4221 DEBUGADD(106,("status:[%d]\n", info2->status));
4222 DEBUGADD(106,("cjobs:[%d]\n", info2->cjobs));
4223 DEBUGADD(106,("averageppm:[%d]\n", info2->averageppm));
4224 DEBUGADD(106,("changeid:[%d]\n", info2->changeid));
4225 DEBUGADD(106,("c_setprinter:[%d]\n", info2->c_setprinter));
4226 DEBUGADD(106,("setuptime:[%d]\n", info2->setuptime));
4228 DEBUGADD(106,("servername:[%s]\n", info2->servername));
4229 DEBUGADD(106,("printername:[%s]\n", info2->printername));
4230 DEBUGADD(106,("sharename:[%s]\n", info2->sharename));
4231 DEBUGADD(106,("portname:[%s]\n", info2->portname));
4232 DEBUGADD(106,("drivername:[%s]\n", info2->drivername));
4233 DEBUGADD(106,("comment:[%s]\n", info2->comment));
4234 DEBUGADD(106,("location:[%s]\n", info2->location));
4235 DEBUGADD(106,("sepfile:[%s]\n", info2->sepfile));
4236 DEBUGADD(106,("printprocessor:[%s]\n", info2->printprocessor));
4237 DEBUGADD(106,("datatype:[%s]\n", info2->datatype));
4238 DEBUGADD(106,("parameters:[%s]\n", info2->parameters));
4244 DEBUGADD(106,("dump_a_printer: Level %u not implemented\n", (unsigned int)level ));
4252 /****************************************************************************
4253 Update the changeid time.
4254 This is SO NASTY as some drivers need this to change, others need it
4255 static. This value will change every second, and I must hope that this
4256 is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
4258 ****************************************************************************/
4260 static uint32 rev_changeid(void)
4264 get_process_uptime(&tv);
4267 /* Return changeid as msec since spooler restart */
4268 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
4271 * This setting seems to work well but is too untested
4272 * to replace the above calculation. Left in for experiementation
4273 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
4275 return tv.tv_sec * 10 + tv.tv_usec / 100000;
4281 * The function below are the high level ones.
4282 * only those ones must be called from the spoolss code.
4286 /****************************************************************************
4287 Modify a printer. This is called from SETPRINTERDATA/DELETEPRINTERDATA.
4288 ****************************************************************************/
4290 WERROR mod_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4294 dump_a_printer(printer, level);
4300 * Update the changestamp. Emperical tests show that the
4301 * ChangeID is always updated,but c_setprinter is
4302 * global spooler variable (not per printer).
4305 /* ChangeID **must** be increasing over the lifetime
4306 of client's spoolss service in order for the
4307 client's cache to show updates */
4309 printer->info_2->changeid = rev_changeid();
4312 * Because one day someone will ask:
4313 * NT->NT An admin connection to a remote
4314 * printer show changes imeediately in
4315 * the properities dialog
4317 * A non-admin connection will only show the
4318 * changes after viewing the properites page
4319 * 2 times. Seems to be related to a
4320 * race condition in the client between the spooler
4321 * updating the local cache and the Explorer.exe GUI
4322 * actually displaying the properties.
4324 * This is fixed in Win2k. admin/non-admin
4325 * connections both display changes immediately.
4330 result=update_a_printer_2(printer->info_2);
4334 result=WERR_UNKNOWN_LEVEL;
4341 /****************************************************************************
4342 Initialize printer devmode & data with previously saved driver init values.
4343 ****************************************************************************/
4345 static bool set_driver_init_2( NT_PRINTER_INFO_LEVEL_2 *info_ptr )
4350 NT_PRINTER_INFO_LEVEL_2 info;
4356 * Delete any printer data 'values' already set. When called for driver
4357 * replace, there will generally be some, but during an add printer, there
4358 * should not be any (if there are delete them).
4361 if ( info_ptr->data )
4362 delete_all_printer_data( info_ptr, "" );
4364 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX,
4365 info_ptr->drivername) < 0) {
4369 dbuf = tdb_fetch_bystring(tdb_drivers, key);
4372 * When changing to a driver that has no init info in the tdb, remove
4373 * the previous drivers init info and leave the new on blank.
4375 free_nt_devicemode(&info_ptr->devmode);
4382 * Get the saved DEVMODE..
4385 len += unpack_devicemode(&info.devmode,dbuf.dptr+len, dbuf.dsize-len);
4388 * The saved DEVMODE contains the devicename from the printer used during
4389 * the initialization save. Change it to reflect the new printer.
4392 if ( info.devmode ) {
4393 ZERO_STRUCT(info.devmode->devicename);
4394 fstrcpy(info.devmode->devicename, info_ptr->printername);
4398 * NT/2k does not change out the entire DeviceMode of a printer
4399 * when changing the driver. Only the driverextra, private, &
4400 * driverversion fields. --jerry (Thu Mar 14 08:58:43 CST 2002)
4402 * Later examination revealed that Windows NT/2k does reset the
4403 * the printer's device mode, bit **only** when you change a
4404 * property of the device mode such as the page orientation.
4409 /* Bind the saved DEVMODE to the new the printer */
4411 free_nt_devicemode(&info_ptr->devmode);
4412 info_ptr->devmode = info.devmode;
4414 DEBUG(10,("set_driver_init_2: Set printer [%s] init %s DEVMODE for driver [%s]\n",
4415 info_ptr->printername, info_ptr->devmode?"VALID":"NULL", info_ptr->drivername));
4417 /* Add the printer data 'values' to the new printer */
4419 if ( !(info_ptr->data = TALLOC_ZERO_P( info_ptr, NT_PRINTER_DATA )) ) {
4420 DEBUG(0,("set_driver_init_2: talloc() failed!\n"));
4424 len += unpack_values( info_ptr->data, dbuf.dptr+len, dbuf.dsize-len );
4426 SAFE_FREE(dbuf.dptr);
4431 /****************************************************************************
4432 Initialize printer devmode & data with previously saved driver init values.
4433 When a printer is created using AddPrinter, the drivername bound to the
4434 printer is used to lookup previously saved driver initialization info, which
4435 is bound to the new printer.
4436 ****************************************************************************/
4438 bool set_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4440 bool result = False;
4444 result = set_driver_init_2(printer->info_2);
4448 DEBUG(0,("set_driver_init: Programmer's error! Unknown driver_init level [%d]\n",
4456 /****************************************************************************
4457 Delete driver init data stored for a specified driver
4458 ****************************************************************************/
4460 bool del_driver_init(const char *drivername)
4465 if (!drivername || !*drivername) {
4466 DEBUG(3,("del_driver_init: No drivername specified!\n"));
4470 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, drivername) < 0) {
4474 DEBUG(6,("del_driver_init: Removing driver init data for [%s]\n",
4477 ret = (tdb_delete_bystring(tdb_drivers, key) == 0);
4482 /****************************************************************************
4483 Pack up the DEVMODE and values for a printer into a 'driver init' entry
4484 in the tdb. Note: this is different from the driver entry and the printer
4485 entry. There should be a single driver init entry for each driver regardless
4486 of whether it was installed from NT or 2K. Technically, they should be
4487 different, but they work out to the same struct.
4488 ****************************************************************************/
4490 static uint32 update_driver_init_2(NT_PRINTER_INFO_LEVEL_2 *info)
4494 int buflen, len, ret;
4503 len += pack_devicemode(info->devmode, buf+len, buflen-len);
4505 retlen = pack_values( info->data, buf+len, buflen-len );
4513 buf = (uint8 *)SMB_REALLOC(buf, len);
4515 DEBUG(0, ("update_driver_init_2: failed to enlarge buffer!\n"));
4524 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, info->drivername) < 0) {
4532 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
4536 DEBUG(8, ("update_driver_init_2: error updating printer init to tdb on disk\n"));
4540 DEBUG(10,("update_driver_init_2: Saved printer [%s] init DEVMODE & values for driver [%s]\n",
4541 info->sharename, info->drivername));
4546 /****************************************************************************
4547 Update (i.e. save) the driver init info (DEVMODE and values) for a printer
4548 ****************************************************************************/
4550 static uint32 update_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4554 dump_a_printer(printer, level);
4558 result = update_driver_init_2(printer->info_2);
4568 /****************************************************************************
4569 Convert the printer data value, a REG_BINARY array, into an initialization
4570 DEVMODE. Note: the array must be parsed as if it was a DEVMODE in an rpc...
4571 got to keep the endians happy :).
4572 ****************************************************************************/
4574 static bool convert_driver_init(TALLOC_CTX *mem_ctx, NT_DEVICEMODE *nt_devmode,
4575 const uint8_t *data, uint32_t data_len)
4577 struct spoolss_DeviceMode devmode;
4578 enum ndr_err_code ndr_err;
4581 ZERO_STRUCT(devmode);
4583 blob = data_blob_const(data, data_len);
4585 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, NULL, &devmode,
4586 (ndr_pull_flags_fn_t)ndr_pull_spoolss_DeviceMode);
4587 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
4588 DEBUG(10,("convert_driver_init: error parsing spoolss_DeviceMode\n"));
4592 return convert_devicemode("", &devmode, &nt_devmode);
4595 /****************************************************************************
4596 Set the DRIVER_INIT info in the tdb. Requires Win32 client code that:
4598 1. Use the driver's config DLL to this UNC printername and:
4599 a. Call DrvPrintEvent with PRINTER_EVENT_INITIALIZE
4600 b. Call DrvConvertDevMode with CDM_DRIVER_DEFAULT to get default DEVMODE
4601 2. Call SetPrinterData with the 'magic' key and the DEVMODE as data.
4603 The last step triggers saving the "driver initialization" information for
4604 this printer into the tdb. Later, new printers that use this driver will
4605 have this initialization information bound to them. This simulates the
4606 driver initialization, as if it had run on the Samba server (as it would
4609 The Win32 client side code requirement sucks! But until we can run arbitrary
4610 Win32 printer driver code on any Unix that Samba runs on, we are stuck with it.
4612 It would have been easier to use SetPrinter because all the UNMARSHALLING of
4613 the DEVMODE is done there, but 2K/XP clients do not set the DEVMODE... think
4614 about it and you will realize why. JRR 010720
4615 ****************************************************************************/
4617 static WERROR save_driver_init_2(NT_PRINTER_INFO_LEVEL *printer, uint8 *data, uint32 data_len )
4619 WERROR status = WERR_OK;
4620 TALLOC_CTX *ctx = NULL;
4621 NT_DEVICEMODE *nt_devmode = NULL;
4622 NT_DEVICEMODE *tmp_devmode = printer->info_2->devmode;
4625 * When the DEVMODE is already set on the printer, don't try to unpack it.
4627 DEBUG(8,("save_driver_init_2: Enter...\n"));
4629 if ( !printer->info_2->devmode && data_len ) {
4631 * Set devmode on printer info, so entire printer initialization can be
4635 if ((ctx = talloc_init("save_driver_init_2")) == NULL)
4638 if ((nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE)) == NULL) {
4639 status = WERR_NOMEM;
4643 ZERO_STRUCTP(nt_devmode);
4646 * The DEVMODE is held in the 'data' component of the param in raw binary.
4647 * Convert it to to a devmode structure
4649 if ( !convert_driver_init( ctx, nt_devmode, data, data_len )) {
4650 DEBUG(10,("save_driver_init_2: error converting DEVMODE\n"));
4651 status = WERR_INVALID_PARAM;
4655 printer->info_2->devmode = nt_devmode;
4659 * Pack up and add (or update) the DEVMODE and any current printer data to
4660 * a 'driver init' element in the tdb
4664 if ( update_driver_init(printer, 2) != 0 ) {
4665 DEBUG(10,("save_driver_init_2: error updating DEVMODE\n"));
4666 status = WERR_NOMEM;
4671 * If driver initialization info was successfully saved, set the current
4672 * printer to match it. This allows initialization of the current printer
4673 * as well as the driver.
4675 status = mod_a_printer(printer, 2);
4676 if (!W_ERROR_IS_OK(status)) {
4677 DEBUG(10,("save_driver_init_2: error setting DEVMODE on printer [%s]\n",
4678 printer->info_2->printername));
4682 talloc_destroy(ctx);
4683 free_nt_devicemode( &nt_devmode );
4685 printer->info_2->devmode = tmp_devmode;
4690 /****************************************************************************
4691 Update the driver init info (DEVMODE and specifics) for a printer
4692 ****************************************************************************/
4694 WERROR save_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level, uint8 *data, uint32 data_len)
4696 WERROR status = WERR_OK;
4700 status = save_driver_init_2( printer, data, data_len );
4703 status = WERR_UNKNOWN_LEVEL;
4710 /****************************************************************************
4711 Get a NT_PRINTER_INFO_LEVEL struct. It returns malloced memory.
4713 Previously the code had a memory allocation problem because it always
4714 used the TALLOC_CTX from the Printer_entry*. This context lasts
4715 as a long as the original handle is open. So if the client made a lot
4716 of getprinter[data]() calls, the memory usage would climb. Now we use
4717 a short lived TALLOC_CTX for printer_info_2 objects returned. We
4718 still use the Printer_entry->ctx for maintaining the cache copy though
4719 since that object must live as long as the handle by definition.
4722 ****************************************************************************/
4724 static WERROR get_a_printer_internal( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level,
4725 const char *sharename, bool get_loc_com)
4730 DEBUG(10,("get_a_printer: [%s] level %u\n", sharename, (unsigned int)level));
4732 if ( !(*pp_printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
4733 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4739 if ( !((*pp_printer)->info_2 = TALLOC_ZERO_P(*pp_printer, NT_PRINTER_INFO_LEVEL_2)) ) {
4740 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4741 TALLOC_FREE( *pp_printer );
4746 fstrcpy( servername, print_hnd->servername );
4748 fstrcpy( servername, "%L" );
4749 standard_sub_basic( "", "", servername,
4750 sizeof(servername)-1 );
4753 result = get_a_printer_2( (*pp_printer)->info_2,
4754 servername, sharename, get_loc_com);
4756 /* we have a new printer now. Save it with this handle */
4758 if ( !W_ERROR_IS_OK(result) ) {
4759 TALLOC_FREE( *pp_printer );
4760 DEBUG(10,("get_a_printer: [%s] level %u returning %s\n",
4761 sharename, (unsigned int)level, win_errstr(result)));
4765 dump_a_printer( *pp_printer, level);
4770 TALLOC_FREE( *pp_printer );
4771 return WERR_UNKNOWN_LEVEL;
4777 WERROR get_a_printer( Printer_entry *print_hnd,
4778 NT_PRINTER_INFO_LEVEL **pp_printer,
4780 const char *sharename)
4782 return get_a_printer_internal(print_hnd, pp_printer, level,
4786 WERROR get_a_printer_search( Printer_entry *print_hnd,
4787 NT_PRINTER_INFO_LEVEL **pp_printer,
4789 const char *sharename)
4791 return get_a_printer_internal(print_hnd, pp_printer, level,
4795 /****************************************************************************
4796 Deletes a NT_PRINTER_INFO_LEVEL struct.
4797 ****************************************************************************/
4799 uint32 free_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level)
4801 NT_PRINTER_INFO_LEVEL *printer = *pp_printer;
4808 if ( printer->info_2 )
4809 free_nt_printer_info_level_2(&printer->info_2);
4813 DEBUG(0,("free_a_printer: unknown level! [%d]\n", level ));
4817 TALLOC_FREE(*pp_printer);
4822 /****************************************************************************
4823 ****************************************************************************/
4825 bool driver_info_ctr_to_info8(struct spoolss_AddDriverInfoCtr *r,
4826 struct spoolss_DriverInfo8 *_info8)
4828 struct spoolss_DriverInfo8 info8;
4834 info8.version = r->info.info3->version;
4835 info8.driver_name = r->info.info3->driver_name;
4836 info8.architecture = r->info.info3->architecture;
4837 info8.driver_path = r->info.info3->driver_path;
4838 info8.data_file = r->info.info3->data_file;
4839 info8.config_file = r->info.info3->config_file;
4840 info8.help_file = r->info.info3->help_file;
4841 info8.monitor_name = r->info.info3->monitor_name;
4842 info8.default_datatype = r->info.info3->default_datatype;
4843 if (r->info.info3->dependent_files && r->info.info3->dependent_files->string) {
4844 info8.dependent_files = r->info.info3->dependent_files->string;
4848 info8.version = r->info.info6->version;
4849 info8.driver_name = r->info.info6->driver_name;
4850 info8.architecture = r->info.info6->architecture;
4851 info8.driver_path = r->info.info6->driver_path;
4852 info8.data_file = r->info.info6->data_file;
4853 info8.config_file = r->info.info6->config_file;
4854 info8.help_file = r->info.info6->help_file;
4855 info8.monitor_name = r->info.info6->monitor_name;
4856 info8.default_datatype = r->info.info6->default_datatype;
4857 if (r->info.info6->dependent_files && r->info.info6->dependent_files->string) {
4858 info8.dependent_files = r->info.info6->dependent_files->string;
4860 info8.driver_date = r->info.info6->driver_date;
4861 info8.driver_version = r->info.info6->driver_version;
4862 info8.manufacturer_name = r->info.info6->manufacturer_name;
4863 info8.manufacturer_url = r->info.info6->manufacturer_url;
4864 info8.hardware_id = r->info.info6->hardware_id;
4865 info8.provider = r->info.info6->provider;
4868 info8.version = r->info.info8->version;
4869 info8.driver_name = r->info.info8->driver_name;
4870 info8.architecture = r->info.info8->architecture;
4871 info8.driver_path = r->info.info8->driver_path;
4872 info8.data_file = r->info.info8->data_file;
4873 info8.config_file = r->info.info8->config_file;
4874 info8.help_file = r->info.info8->help_file;
4875 info8.monitor_name = r->info.info8->monitor_name;
4876 info8.default_datatype = r->info.info8->default_datatype;
4877 if (r->info.info8->dependent_files && r->info.info8->dependent_files->string) {
4878 info8.dependent_files = r->info.info8->dependent_files->string;
4880 if (r->info.info8->previous_names && r->info.info8->previous_names->string) {
4881 info8.previous_names = r->info.info8->previous_names->string;
4883 info8.driver_date = r->info.info8->driver_date;
4884 info8.driver_version = r->info.info8->driver_version;
4885 info8.manufacturer_name = r->info.info8->manufacturer_name;
4886 info8.manufacturer_url = r->info.info8->manufacturer_url;
4887 info8.hardware_id = r->info.info8->hardware_id;
4888 info8.provider = r->info.info8->provider;
4889 info8.print_processor = r->info.info8->print_processor;
4890 info8.vendor_setup = r->info.info8->vendor_setup;
4891 if (r->info.info8->color_profiles && r->info.info8->color_profiles->string) {
4892 info8.color_profiles = r->info.info8->color_profiles->string;
4894 info8.inf_path = r->info.info8->inf_path;
4895 info8.printer_driver_attributes = r->info.info8->printer_driver_attributes;
4896 if (r->info.info8->core_driver_dependencies && r->info.info8->core_driver_dependencies->string) {
4897 info8.core_driver_dependencies = r->info.info8->core_driver_dependencies->string;
4899 info8.min_inbox_driver_ver_date = r->info.info8->min_inbox_driver_ver_date;
4900 info8.min_inbox_driver_ver_version = r->info.info8->min_inbox_driver_ver_version;
4912 uint32_t add_a_printer_driver(TALLOC_CTX *mem_ctx,
4913 struct spoolss_AddDriverInfoCtr *r,
4917 struct spoolss_DriverInfo8 info8;
4921 DEBUG(10,("adding a printer at level [%d]\n", r->level));
4923 if (!driver_info_ctr_to_info8(r, &info8)) {
4927 *driver_name = talloc_strdup(mem_ctx, info8.driver_name);
4928 if (!*driver_name) {
4931 *version = info8.version;
4933 return add_a_printer_driver_8(&info8);
4936 /****************************************************************************
4937 ****************************************************************************/
4939 WERROR get_a_printer_driver(TALLOC_CTX *mem_ctx,
4940 struct spoolss_DriverInfo8 **driver,
4941 const char *drivername, const char *architecture,
4945 struct spoolss_DriverInfo3 info3;
4946 struct spoolss_DriverInfo8 *info8;
4950 /* Sometime we just want any version of the driver */
4952 if (version == DRIVER_ANY_VERSION) {
4953 /* look for Win2k first and then for NT4 */
4954 result = get_a_printer_driver_3(mem_ctx,
4958 if (!W_ERROR_IS_OK(result)) {
4959 result = get_a_printer_driver_3(mem_ctx,
4965 result = get_a_printer_driver_3(mem_ctx,
4972 if (!W_ERROR_IS_OK(result)) {
4976 info8 = talloc_zero(mem_ctx, struct spoolss_DriverInfo8);
4981 info8->version = info3.version;
4982 info8->driver_name = info3.driver_name;
4983 info8->architecture = info3.architecture;
4984 info8->driver_path = info3.driver_path;
4985 info8->data_file = info3.data_file;
4986 info8->config_file = info3.config_file;
4987 info8->help_file = info3.help_file;
4988 info8->dependent_files = info3.dependent_files;
4989 info8->monitor_name = info3.monitor_name;
4990 info8->default_datatype = info3.default_datatype;
4997 /****************************************************************************
4998 ****************************************************************************/
5000 uint32_t free_a_printer_driver(struct spoolss_DriverInfo8 *driver)
5002 talloc_free(driver);
5007 /****************************************************************************
5008 Determine whether or not a particular driver is currently assigned
5010 ****************************************************************************/
5012 bool printer_driver_in_use(const struct spoolss_DriverInfo8 *r)
5015 int n_services = lp_numservices();
5016 NT_PRINTER_INFO_LEVEL *printer = NULL;
5017 bool in_use = False;
5023 DEBUG(10,("printer_driver_in_use: Beginning search through ntprinters.tdb...\n"));
5025 /* loop through the printers.tdb and check for the drivername */
5027 for (snum=0; snum<n_services && !in_use; snum++) {
5028 if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
5031 if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_servicename(snum))) )
5034 if (strequal(r->driver_name, printer->info_2->drivername))
5037 free_a_printer( &printer, 2 );
5040 DEBUG(10,("printer_driver_in_use: Completed search through ntprinters.tdb...\n"));
5043 struct spoolss_DriverInfo8 *d;
5046 DEBUG(5,("printer_driver_in_use: driver \"%s\" is currently in use\n", r->driver_name));
5048 /* we can still remove the driver if there is one of
5049 "Windows NT x86" version 2 or 3 left */
5051 if (!strequal("Windows NT x86", r->architecture)) {
5052 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", DRIVER_ANY_VERSION);
5055 switch (r->version) {
5057 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", 3);
5060 werr = get_a_printer_driver(talloc_tos(), &d, r->driver_name, "Windows NT x86", 2);
5063 DEBUG(0,("printer_driver_in_use: ERROR! unknown driver version (%d)\n",
5065 werr = WERR_UNKNOWN_PRINTER_DRIVER;
5070 /* now check the error code */
5072 if ( W_ERROR_IS_OK(werr) ) {
5073 /* it's ok to remove the driver, we have other architctures left */
5075 free_a_printer_driver(d);
5079 /* report that the driver is not in use by default */
5085 /**********************************************************************
5086 Check to see if a ogiven file is in use by *info
5087 *********************************************************************/
5089 static bool drv_file_in_use(const char *file, const struct spoolss_DriverInfo8 *info)
5096 /* mz: skip files that are in the list but already deleted */
5097 if (!file || !file[0]) {
5101 if (strequal(file, info->driver_path))
5104 if (strequal(file, info->data_file))
5107 if (strequal(file, info->config_file))
5110 if (strequal(file, info->help_file))
5113 /* see of there are any dependent files to examine */
5115 if (!info->dependent_files)
5118 while (info->dependent_files[i] && *info->dependent_files[i]) {
5119 if (strequal(file, info->dependent_files[i]))
5128 /**********************************************************************
5129 Utility function to remove the dependent file pointed to by the
5130 input parameter from the list
5131 *********************************************************************/
5133 static void trim_dependent_file(TALLOC_CTX *mem_ctx, const char **files, int idx)
5136 /* bump everything down a slot */
5138 while (files && files[idx+1]) {
5139 files[idx] = talloc_strdup(mem_ctx, files[idx+1]);
5148 /**********************************************************************
5149 Check if any of the files used by src are also used by drv
5150 *********************************************************************/
5152 static bool trim_overlap_drv_files(TALLOC_CTX *mem_ctx,
5153 struct spoolss_DriverInfo8 *src,
5154 const struct spoolss_DriverInfo8 *drv)
5156 bool in_use = False;
5162 /* check each file. Remove it from the src structure if it overlaps */
5164 if (drv_file_in_use(src->driver_path, drv)) {
5166 DEBUG(10,("Removing driverfile [%s] from list\n", src->driver_path));
5167 src->driver_path = talloc_strdup(mem_ctx, "");
5168 if (!src->driver_path) { return false; }
5171 if (drv_file_in_use(src->data_file, drv)) {
5173 DEBUG(10,("Removing datafile [%s] from list\n", src->data_file));
5174 src->data_file = talloc_strdup(mem_ctx, "");
5175 if (!src->data_file) { return false; }
5178 if (drv_file_in_use(src->config_file, drv)) {
5180 DEBUG(10,("Removing configfile [%s] from list\n", src->config_file));
5181 src->config_file = talloc_strdup(mem_ctx, "");
5182 if (!src->config_file) { return false; }
5185 if (drv_file_in_use(src->help_file, drv)) {
5187 DEBUG(10,("Removing helpfile [%s] from list\n", src->help_file));
5188 src->help_file = talloc_strdup(mem_ctx, "");
5189 if (!src->help_file) { return false; }
5192 /* are there any dependentfiles to examine? */
5194 if (!src->dependent_files)
5197 while (src->dependent_files[i] && *src->dependent_files[i]) {
5198 if (drv_file_in_use(src->dependent_files[i], drv)) {
5200 DEBUG(10,("Removing [%s] from dependent file list\n", src->dependent_files[i]));
5201 trim_dependent_file(mem_ctx, src->dependent_files, i);
5209 /****************************************************************************
5210 Determine whether or not a particular driver files are currently being
5211 used by any other driver.
5213 Return value is True if any files were in use by other drivers
5214 and False otherwise.
5216 Upon return, *info has been modified to only contain the driver files
5217 which are not in use
5221 This needs to check all drivers to ensure that all files in use
5222 have been removed from *info, not just the ones in the first
5224 ****************************************************************************/
5226 bool printer_driver_files_in_use(TALLOC_CTX *mem_ctx,
5227 struct spoolss_DriverInfo8 *info)
5232 fstring *list = NULL;
5233 struct spoolss_DriverInfo8 *driver;
5234 bool in_use = false;
5239 version = info->version;
5241 /* loop over all driver versions */
5243 DEBUG(5,("printer_driver_files_in_use: Beginning search through ntdrivers.tdb...\n"));
5245 /* get the list of drivers */
5248 ndrivers = get_ntdrivers(&list, info->architecture, version);
5250 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
5251 ndrivers, info->architecture, version));
5253 /* check each driver for overlap in files */
5255 for (i=0; i<ndrivers; i++) {
5256 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
5260 if (!W_ERROR_IS_OK(get_a_printer_driver(talloc_tos(), &driver, list[i], info->architecture, version))) {
5265 /* check if d2 uses any files from d1 */
5266 /* only if this is a different driver than the one being deleted */
5268 if (!strequal(info->driver_name, driver->driver_name)) {
5269 if (trim_overlap_drv_files(mem_ctx, info, driver)) {
5270 /* mz: Do not instantly return -
5271 * we need to ensure this file isn't
5272 * also in use by other drivers. */
5277 free_a_printer_driver(driver);
5282 DEBUG(5,("printer_driver_files_in_use: Completed search through ntdrivers.tdb...\n"));
5287 static NTSTATUS driver_unlink_internals(connection_struct *conn,
5290 struct smb_filename *smb_fname = NULL;
5293 status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
5295 if (!NT_STATUS_IS_OK(status)) {
5299 status = unlink_internals(conn, NULL, 0, smb_fname, false);
5301 TALLOC_FREE(smb_fname);
5305 /****************************************************************************
5306 Actually delete the driver files. Make sure that
5307 printer_driver_files_in_use() return False before calling
5309 ****************************************************************************/
5311 static bool delete_driver_files(struct pipes_struct *rpc_pipe,
5312 const struct spoolss_DriverInfo8 *r)
5317 connection_struct *conn;
5320 fstring printdollar;
5321 int printdollar_snum;
5328 DEBUG(6,("delete_driver_files: deleting driver [%s] - version [%d]\n",
5329 r->driver_name, r->version));
5331 fstrcpy(printdollar, "print$");
5333 printdollar_snum = find_service(printdollar);
5334 if (printdollar_snum == -1) {
5338 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
5339 lp_pathname(printdollar_snum),
5340 rpc_pipe->server_info, &oldcwd);
5341 if (!NT_STATUS_IS_OK(nt_status)) {
5342 DEBUG(0,("delete_driver_files: create_conn_struct "
5343 "returned %s\n", nt_errstr(nt_status)));
5347 if ( !CAN_WRITE(conn) ) {
5348 DEBUG(3,("delete_driver_files: Cannot delete print driver when [print$] is read-only\n"));
5352 /* now delete the files; must strip the '\print$' string from
5355 if (r->driver_path && r->driver_path[0]) {
5356 if ((s = strchr(&r->driver_path[1], '\\')) != NULL) {
5358 DEBUG(10,("deleting driverfile [%s]\n", s));
5359 driver_unlink_internals(conn, file);
5363 if (r->config_file && r->config_file[0]) {
5364 if ((s = strchr(&r->config_file[1], '\\')) != NULL) {
5366 DEBUG(10,("deleting configfile [%s]\n", s));
5367 driver_unlink_internals(conn, file);
5371 if (r->data_file && r->data_file[0]) {
5372 if ((s = strchr(&r->data_file[1], '\\')) != NULL) {
5374 DEBUG(10,("deleting datafile [%s]\n", s));
5375 driver_unlink_internals(conn, file);
5379 if (r->help_file && r->help_file[0]) {
5380 if ((s = strchr(&r->help_file[1], '\\')) != NULL) {
5382 DEBUG(10,("deleting helpfile [%s]\n", s));
5383 driver_unlink_internals(conn, file);
5387 /* check if we are done removing files */
5389 if (r->dependent_files) {
5390 while (r->dependent_files[i] && r->dependent_files[i][0]) {
5393 /* bypass the "\print$" portion of the path */
5395 if ((p = strchr(r->dependent_files[i]+1, '\\')) != NULL) {
5397 DEBUG(10,("deleting dependent file [%s]\n", file));
5398 driver_unlink_internals(conn, file);
5410 vfs_ChDir(conn, oldcwd);
5416 /****************************************************************************
5417 Remove a printer driver from the TDB. This assumes that the the driver was
5418 previously looked up.
5419 ***************************************************************************/
5421 WERROR delete_printer_driver(struct pipes_struct *rpc_pipe,
5422 const struct spoolss_DriverInfo8 *r,
5423 uint32 version, bool delete_files )
5429 /* delete the tdb data first */
5431 arch = get_short_archi(r->architecture);
5433 return WERR_UNKNOWN_PRINTER_DRIVER;
5435 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
5436 arch, version, r->driver_name) < 0) {
5440 DEBUG(5,("delete_printer_driver: key = [%s] delete_files = %s\n",
5441 key, delete_files ? "TRUE" : "FALSE" ));
5443 /* check if the driver actually exists for this environment */
5445 dbuf = tdb_fetch_bystring( tdb_drivers, key );
5447 DEBUG(8,("delete_printer_driver: Driver unknown [%s]\n", key));
5449 return WERR_UNKNOWN_PRINTER_DRIVER;
5452 SAFE_FREE( dbuf.dptr );
5454 /* ok... the driver exists so the delete should return success */
5456 if (tdb_delete_bystring(tdb_drivers, key) == -1) {
5457 DEBUG (0,("delete_printer_driver: fail to delete %s!\n", key));
5459 return WERR_ACCESS_DENIED;
5463 * now delete any associated files if delete_files == True
5464 * even if this part failes, we return succes because the
5465 * driver doesn not exist any more
5469 delete_driver_files(rpc_pipe, r);
5471 DEBUG(5,("delete_printer_driver: driver delete successful [%s]\n", key));
5477 /****************************************************************************
5478 Store a security desc for a printer.
5479 ****************************************************************************/
5481 WERROR nt_printing_setsec(const char *sharename, SEC_DESC_BUF *secdesc_ctr)
5483 SEC_DESC_BUF *new_secdesc_ctr = NULL;
5484 SEC_DESC_BUF *old_secdesc_ctr = NULL;
5485 TALLOC_CTX *mem_ctx = NULL;
5492 mem_ctx = talloc_init("nt_printing_setsec");
5493 if (mem_ctx == NULL)
5496 /* The old owner and group sids of the security descriptor are not
5497 present when new ACEs are added or removed by changing printer
5498 permissions through NT. If they are NULL in the new security
5499 descriptor then copy them over from the old one. */
5501 if (!secdesc_ctr->sd->owner_sid || !secdesc_ctr->sd->group_sid) {
5502 DOM_SID *owner_sid, *group_sid;
5503 SEC_ACL *dacl, *sacl;
5504 SEC_DESC *psd = NULL;
5507 if (!nt_printing_getsec(mem_ctx, sharename, &old_secdesc_ctr)) {
5508 status = WERR_NOMEM;
5512 /* Pick out correct owner and group sids */
5514 owner_sid = secdesc_ctr->sd->owner_sid ?
5515 secdesc_ctr->sd->owner_sid :
5516 old_secdesc_ctr->sd->owner_sid;
5518 group_sid = secdesc_ctr->sd->group_sid ?
5519 secdesc_ctr->sd->group_sid :
5520 old_secdesc_ctr->sd->group_sid;
5522 dacl = secdesc_ctr->sd->dacl ?
5523 secdesc_ctr->sd->dacl :
5524 old_secdesc_ctr->sd->dacl;
5526 sacl = secdesc_ctr->sd->sacl ?
5527 secdesc_ctr->sd->sacl :
5528 old_secdesc_ctr->sd->sacl;
5530 /* Make a deep copy of the security descriptor */
5532 psd = make_sec_desc(mem_ctx, secdesc_ctr->sd->revision, secdesc_ctr->sd->type,
5533 owner_sid, group_sid,
5539 status = WERR_NOMEM;
5543 new_secdesc_ctr = make_sec_desc_buf(mem_ctx, size, psd);
5546 if (!new_secdesc_ctr) {
5547 new_secdesc_ctr = secdesc_ctr;
5550 /* Store the security descriptor in a tdb */
5552 nt_status = marshall_sec_desc_buf(mem_ctx, new_secdesc_ctr,
5553 &blob.data, &blob.length);
5554 if (!NT_STATUS_IS_OK(nt_status)) {
5555 status = ntstatus_to_werror(nt_status);
5559 kbuf = make_printers_secdesc_tdbkey(mem_ctx, sharename );
5561 dbuf.dptr = (unsigned char *)blob.data;
5562 dbuf.dsize = blob.length;
5564 if (tdb_trans_store(tdb_printers, kbuf, dbuf, TDB_REPLACE)==0) {
5567 DEBUG(1,("Failed to store secdesc for %s\n", sharename));
5568 status = WERR_BADFUNC;
5571 /* Free malloc'ed memory */
5572 talloc_free(blob.data);
5577 talloc_destroy(mem_ctx);
5581 /****************************************************************************
5582 Construct a default security descriptor buffer for a printer.
5583 ****************************************************************************/
5585 static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
5587 SEC_ACE ace[5]; /* max number of ace entries */
5590 SEC_ACL *psa = NULL;
5591 SEC_DESC_BUF *sdb = NULL;
5592 SEC_DESC *psd = NULL;
5596 /* Create an ACE where Everyone is allowed to print */
5598 sa = PRINTER_ACE_PRINT;
5599 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
5600 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5602 /* Add the domain admins group if we are a DC */
5605 DOM_SID domadmins_sid;
5607 sid_compose(&domadmins_sid, get_global_sam_sid(),
5608 DOMAIN_GROUP_RID_ADMINS);
5610 sa = PRINTER_ACE_FULL_CONTROL;
5611 init_sec_ace(&ace[i++], &domadmins_sid,
5612 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5613 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5614 init_sec_ace(&ace[i++], &domadmins_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5615 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5617 else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
5618 sid_append_rid(&adm_sid, DOMAIN_USER_RID_ADMIN);
5620 sa = PRINTER_ACE_FULL_CONTROL;
5621 init_sec_ace(&ace[i++], &adm_sid,
5622 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5623 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5624 init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5625 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5628 /* add BUILTIN\Administrators as FULL CONTROL */
5630 sa = PRINTER_ACE_FULL_CONTROL;
5631 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5632 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5633 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5634 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5635 SEC_ACE_TYPE_ACCESS_ALLOWED,
5636 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5638 /* Make the security descriptor owned by the BUILTIN\Administrators */
5640 /* The ACL revision number in rpc_secdesc.h differs from the one
5641 created by NT when setting ACE entries in printer
5642 descriptors. NT4 complains about the property being edited by a
5645 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) != NULL) {
5646 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
5647 &global_sid_Builtin_Administrators,
5648 &global_sid_Builtin_Administrators,
5649 NULL, psa, &sd_size);
5653 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
5657 sdb = make_sec_desc_buf(ctx, sd_size, psd);
5659 DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
5660 (unsigned int)sd_size));
5665 /****************************************************************************
5666 Get a security desc for a printer.
5667 ****************************************************************************/
5669 bool nt_printing_getsec(TALLOC_CTX *ctx, const char *sharename, SEC_DESC_BUF **secdesc_ctr)
5677 if (strlen(sharename) > 2 && (temp = strchr(sharename + 2, '\\'))) {
5678 sharename = temp + 1;
5681 /* Fetch security descriptor from tdb */
5683 kbuf = make_printers_secdesc_tdbkey(ctx, sharename);
5685 dbuf = tdb_fetch(tdb_printers, kbuf);
5688 status = unmarshall_sec_desc_buf(ctx, dbuf.dptr, dbuf.dsize,
5690 SAFE_FREE(dbuf.dptr);
5692 if (NT_STATUS_IS_OK(status)) {
5697 *secdesc_ctr = construct_default_printer_sdb(ctx);
5698 if (!*secdesc_ctr) {
5702 status = marshall_sec_desc_buf(ctx, *secdesc_ctr,
5703 &blob.data, &blob.length);
5704 if (NT_STATUS_IS_OK(status)) {
5705 dbuf.dptr = (unsigned char *)blob.data;
5706 dbuf.dsize = blob.length;
5707 tdb_trans_store(tdb_printers, kbuf, dbuf, TDB_REPLACE);
5708 talloc_free(blob.data);
5711 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
5712 this security descriptor has been created when winbindd was
5713 down. Take ownership of security descriptor. */
5715 if (sid_equal((*secdesc_ctr)->sd->owner_sid, &global_sid_World)) {
5718 /* Change sd owner to workgroup administrator */
5720 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
5721 SEC_DESC_BUF *new_secdesc_ctr = NULL;
5722 SEC_DESC *psd = NULL;
5727 sid_append_rid(&owner_sid, DOMAIN_USER_RID_ADMIN);
5729 psd = make_sec_desc(ctx, (*secdesc_ctr)->sd->revision, (*secdesc_ctr)->sd->type,
5731 (*secdesc_ctr)->sd->group_sid,
5732 (*secdesc_ctr)->sd->sacl,
5733 (*secdesc_ctr)->sd->dacl,
5740 new_secdesc_ctr = make_sec_desc_buf(ctx, size, psd);
5741 if (!new_secdesc_ctr) {
5745 /* Swap with other one */
5747 *secdesc_ctr = new_secdesc_ctr;
5751 nt_printing_setsec(sharename, *secdesc_ctr);
5755 if (DEBUGLEVEL >= 10) {
5756 SEC_ACL *the_acl = (*secdesc_ctr)->sd->dacl;
5759 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5760 sharename, the_acl->num_aces));
5762 for (i = 0; i < the_acl->num_aces; i++) {
5763 DEBUG(10, ("%s %d %d 0x%08x\n",
5764 sid_string_dbg(&the_acl->aces[i].trustee),
5765 the_acl->aces[i].type, the_acl->aces[i].flags,
5766 the_acl->aces[i].access_mask));
5775 1: level not implemented
5776 2: file doesn't exist
5777 3: can't allocate memory
5778 4: can't free memory
5779 5: non existant struct
5783 A printer and a printer driver are 2 different things.
5784 NT manages them separatelly, Samba does the same.
5785 Why ? Simply because it's easier and it makes sense !
5787 Now explanation: You have 3 printers behind your samba server,
5788 2 of them are the same make and model (laser A and B). But laser B
5789 has an 3000 sheet feeder and laser A doesn't such an option.
5790 Your third printer is an old dot-matrix model for the accounting :-).
5792 If the /usr/local/samba/lib directory (default dir), you will have
5793 5 files to describe all of this.
5795 3 files for the printers (1 by printer):
5798 NTprinter_accounting
5799 2 files for the drivers (1 for the laser and 1 for the dot matrix)
5800 NTdriver_printer model X
5801 NTdriver_printer model Y
5803 jfm: I should use this comment for the text file to explain
5804 same thing for the forms BTW.
5805 Je devrais mettre mes commentaires en francais, ca serait mieux :-)
5809 /* Convert generic access rights to printer object specific access rights.
5810 It turns out that NT4 security descriptors use generic access rights and
5811 NT5 the object specific ones. */
5813 void map_printer_permissions(SEC_DESC *sd)
5817 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5818 se_map_generic(&sd->dacl->aces[i].access_mask,
5819 &printer_generic_mapping);
5823 void map_job_permissions(SEC_DESC *sd)
5827 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5828 se_map_generic(&sd->dacl->aces[i].access_mask,
5829 &job_generic_mapping);
5834 /****************************************************************************
5835 Check a user has permissions to perform the given operation. We use the
5836 permission constants defined in include/rpc_spoolss.h to check the various
5837 actions we perform when checking printer access.
5839 PRINTER_ACCESS_ADMINISTER:
5840 print_queue_pause, print_queue_resume, update_printer_sec,
5841 update_printer, spoolss_addprinterex_level_2,
5842 _spoolss_setprinterdata
5847 JOB_ACCESS_ADMINISTER:
5848 print_job_delete, print_job_pause, print_job_resume,
5851 Try access control in the following order (for performance reasons):
5852 1) root and SE_PRINT_OPERATOR can do anything (easy check)
5853 2) check security descriptor (bit comparisons in memory)
5854 3) "printer admins" (may result in numerous calls to winbind)
5856 ****************************************************************************/
5857 bool print_access_check(struct auth_serversupplied_info *server_info, int snum,
5860 SEC_DESC_BUF *secdesc = NULL;
5861 uint32 access_granted;
5864 TALLOC_CTX *mem_ctx = NULL;
5865 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5867 /* If user is NULL then use the current_user structure */
5869 /* Always allow root or SE_PRINT_OPERATROR to do anything */
5871 if (server_info->utok.uid == sec_initial_uid()
5872 || user_has_privileges(server_info->ptok, &se_printop ) ) {
5876 /* Get printer name */
5878 pname = PRINTERNAME(snum);
5880 if (!pname || !*pname) {
5885 /* Get printer security descriptor */
5887 if(!(mem_ctx = talloc_init("print_access_check"))) {
5892 if (!nt_printing_getsec(mem_ctx, pname, &secdesc)) {
5893 talloc_destroy(mem_ctx);
5898 if (access_type == JOB_ACCESS_ADMINISTER) {
5899 SEC_DESC_BUF *parent_secdesc = secdesc;
5901 /* Create a child security descriptor to check permissions
5902 against. This is because print jobs are child objects
5903 objects of a printer. */
5905 status = se_create_child_secdesc_buf(mem_ctx, &secdesc, parent_secdesc->sd, False);
5907 if (!NT_STATUS_IS_OK(status)) {
5908 talloc_destroy(mem_ctx);
5909 errno = map_errno_from_nt_status(status);
5913 map_job_permissions(secdesc->sd);
5915 map_printer_permissions(secdesc->sd);
5919 status = se_access_check(secdesc->sd, server_info->ptok, access_type,
5922 DEBUG(4, ("access check was %s\n", NT_STATUS_IS_OK(status) ? "SUCCESS" : "FAILURE"));
5924 /* see if we need to try the printer admin list */
5926 if (!NT_STATUS_IS_OK(status) &&
5927 (token_contains_name_in_list(uidtoname(server_info->utok.uid),
5928 NULL, NULL, server_info->ptok,
5929 lp_printer_admin(snum)))) {
5930 talloc_destroy(mem_ctx);
5934 talloc_destroy(mem_ctx);
5936 if (!NT_STATUS_IS_OK(status)) {
5940 return NT_STATUS_IS_OK(status);
5943 /****************************************************************************
5944 Check the time parameters allow a print operation.
5945 *****************************************************************************/
5947 bool print_time_access_check(const char *servicename)
5949 NT_PRINTER_INFO_LEVEL *printer = NULL;
5951 time_t now = time(NULL);
5955 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
5958 if (printer->info_2->starttime == 0 && printer->info_2->untiltime == 0)
5962 mins = (uint32)t->tm_hour*60 + (uint32)t->tm_min;
5964 if (mins >= printer->info_2->starttime && mins <= printer->info_2->untiltime)
5967 free_a_printer(&printer, 2);
5975 /****************************************************************************
5976 Fill in the servername sent in the _spoolss_open_printer_ex() call
5977 ****************************************************************************/
5979 char* get_server_name( Printer_entry *printer )
5981 return printer->servername;