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/>.
24 static TDB_CONTEXT *tdb_forms; /* used for forms files */
25 static TDB_CONTEXT *tdb_drivers; /* used for driver files */
26 static TDB_CONTEXT *tdb_printers; /* used for printers files */
28 #define FORMS_PREFIX "FORMS/"
29 #define DRIVERS_PREFIX "DRIVERS/"
30 #define DRIVER_INIT_PREFIX "DRIVER_INIT/"
31 #define PRINTERS_PREFIX "PRINTERS/"
32 #define SECDESC_PREFIX "SECDESC/"
33 #define GLOBAL_C_SETPRINTER "GLOBALS/c_setprinter"
35 #define NTDRIVERS_DATABASE_VERSION_1 1
36 #define NTDRIVERS_DATABASE_VERSION_2 2
37 #define NTDRIVERS_DATABASE_VERSION_3 3 /* little endian version of v2 */
38 #define NTDRIVERS_DATABASE_VERSION_4 4 /* fix generic bits in security descriptors */
39 #define NTDRIVERS_DATABASE_VERSION_5 5 /* normalize keys in ntprinters.tdb */
41 /* Map generic permissions to printer object specific permissions */
43 const struct generic_mapping printer_generic_mapping = {
50 const struct standard_mapping printer_std_mapping = {
57 /* Map generic permissions to print server object specific permissions */
59 const struct generic_mapping printserver_generic_mapping = {
66 const struct generic_mapping printserver_std_mapping = {
73 /* Map generic permissions to job object specific permissions */
75 const struct generic_mapping job_generic_mapping = {
82 /* We need one default form to support our default printer. Msoft adds the
83 forms it wants and in the ORDER it wants them (note: DEVMODE papersize is an
84 array index). Letter is always first, so (for the current code) additions
85 always put things in the correct order. */
86 static const nt_forms_struct default_forms[] = {
87 {"Letter",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
88 {"Letter Small",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
89 {"Tabloid",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
90 {"Ledger",0x1,0x696b8,0x44368,0x0,0x0,0x696b8,0x44368},
91 {"Legal",0x1,0x34b5c,0x56d10,0x0,0x0,0x34b5c,0x56d10},
92 {"Statement",0x1,0x221b4,0x34b5c,0x0,0x0,0x221b4,0x34b5c},
93 {"Executive",0x1,0x2cf56,0x411cc,0x0,0x0,0x2cf56,0x411cc},
94 {"A3",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
95 {"A4",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
96 {"A4 Small",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
97 {"A5",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
98 {"B4 (JIS)",0x1,0x3ebe8,0x58de0,0x0,0x0,0x3ebe8,0x58de0},
99 {"B5 (JIS)",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
100 {"Folio",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
101 {"Quarto",0x1,0x347d8,0x43238,0x0,0x0,0x347d8,0x43238},
102 {"10x14",0x1,0x3e030,0x56d10,0x0,0x0,0x3e030,0x56d10},
103 {"11x17",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
104 {"Note",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
105 {"Envelope #9",0x1,0x18079,0x37091,0x0,0x0,0x18079,0x37091},
106 {"Envelope #10",0x1,0x19947,0x3ae94,0x0,0x0,0x19947,0x3ae94},
107 {"Envelope #11",0x1,0x1be7c,0x40565,0x0,0x0,0x1be7c,0x40565},
108 {"Envelope #12",0x1,0x1d74a,0x44368,0x0,0x0,0x1d74a,0x44368},
109 {"Envelope #14",0x1,0x1f018,0x47504,0x0,0x0,0x1f018,0x47504},
110 {"C size sheet",0x1,0x696b8,0x886d0,0x0,0x0,0x696b8,0x886d0},
111 {"D size sheet",0x1,0x886d0,0xd2d70,0x0,0x0,0x886d0,0xd2d70},
112 {"E size sheet",0x1,0xd2d70,0x110da0,0x0,0x0,0xd2d70,0x110da0},
113 {"Envelope DL",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
114 {"Envelope C5",0x1,0x278d0,0x37e88,0x0,0x0,0x278d0,0x37e88},
115 {"Envelope C3",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
116 {"Envelope C4",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
117 {"Envelope C6",0x1,0x1bd50,0x278d0,0x0,0x0,0x1bd50,0x278d0},
118 {"Envelope C65",0x1,0x1bd50,0x37e88,0x0,0x0,0x1bd50,0x37e88},
119 {"Envelope B4",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
120 {"Envelope B5",0x1,0x2af80,0x3d090,0x0,0x0,0x2af80,0x3d090},
121 {"Envelope B6",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
122 {"Envelope",0x1,0x1adb0,0x38270,0x0,0x0,0x1adb0,0x38270},
123 {"Envelope Monarch",0x1,0x18079,0x2e824,0x0,0x0,0x18079,0x2e824},
124 {"6 3/4 Envelope",0x1,0x167ab,0x284ec,0x0,0x0,0x167ab,0x284ec},
125 {"US Std Fanfold",0x1,0x5c3e1,0x44368,0x0,0x0,0x5c3e1,0x44368},
126 {"German Std Fanfold",0x1,0x34b5c,0x4a6a0,0x0,0x0,0x34b5c,0x4a6a0},
127 {"German Legal Fanfold",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
128 {"B4 (ISO)",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
129 {"Japanese Postcard",0x1,0x186a0,0x24220,0x0,0x0,0x186a0,0x24220},
130 {"9x11",0x1,0x37cf8,0x44368,0x0,0x0,0x37cf8,0x44368},
131 {"10x11",0x1,0x3e030,0x44368,0x0,0x0,0x3e030,0x44368},
132 {"15x11",0x1,0x5d048,0x44368,0x0,0x0,0x5d048,0x44368},
133 {"Envelope Invite",0x1,0x35b60,0x35b60,0x0,0x0,0x35b60,0x35b60},
134 {"Reserved48",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
135 {"Reserved49",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
136 {"Letter Extra",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
137 {"Legal Extra",0x1,0x3ae94,0x5d048,0x0,0x0,0x3ae94,0x5d048},
138 {"Tabloid Extra",0x1,0x4a6a0,0x6f9f0,0x0,0x0,0x4a6a0,0x6f9f0},
139 {"A4 Extra",0x1,0x397c2,0x4eb16,0x0,0x0,0x397c2,0x4eb16},
140 {"Letter Transverse",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
141 {"A4 Transverse",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
142 {"Letter Extra Transverse",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
143 {"Super A",0x1,0x376b8,0x56ea0,0x0,0x0,0x376b8,0x56ea0},
144 {"Super B",0x1,0x4a768,0x76e58,0x0,0x0,0x4a768,0x76e58},
145 {"Letter Plus",0x1,0x34b5c,0x4eb16,0x0,0x0,0x34b5c,0x4eb16},
146 {"A4 Plus",0x1,0x33450,0x50910,0x0,0x0,0x33450,0x50910},
147 {"A5 Transverse",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
148 {"B5 (JIS) Transverse",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
149 {"A3 Extra",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
150 {"A5 Extra",0x1,0x2a7b0,0x395f8,0x0,0x0,0x2a7b0,0x395f8},
151 {"B5 (ISO) Extra",0x1,0x31128,0x43620,0x0,0x0,0x31128,0x43620},
152 {"A2",0x1,0x668a0,0x91050,0x0,0x0,0x668a0,0x91050},
153 {"A3 Transverse",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
154 {"A3 Extra Transverse",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
155 {"Japanese Double Postcard",0x1,0x30d40,0x24220,0x0,0x0,0x30d40,0x24220},
156 {"A6",0x1,0x19a28,0x24220,0x0,0x0,0x19a28,0x24220},
157 {"Japanese Envelope Kaku #2",0x1,0x3a980,0x510e0,0x0,0x0,0x3a980,0x510e0},
158 {"Japanese Envelope Kaku #3",0x1,0x34bc0,0x43a08,0x0,0x0,0x34bc0,0x43a08},
159 {"Japanese Envelope Chou #3",0x1,0x1d4c0,0x395f8,0x0,0x0,0x1d4c0,0x395f8},
160 {"Japanese Envelope Chou #4",0x1,0x15f90,0x320c8,0x0,0x0,0x15f90,0x320c8},
161 {"Letter Rotated",0x1,0x44368,0x34b5c,0x0,0x0,0x44368,0x34b5c},
162 {"A3 Rotated",0x1,0x668a0,0x48828,0x0,0x0,0x668a0,0x48828},
163 {"A4 Rotated",0x1,0x48828,0x33450,0x0,0x0,0x48828,0x33450},
164 {"A5 Rotated",0x1,0x33450,0x24220,0x0,0x0,0x33450,0x24220},
165 {"B4 (JIS) Rotated",0x1,0x58de0,0x3ebe8,0x0,0x0,0x58de0,0x3ebe8},
166 {"B5 (JIS) Rotated",0x1,0x3ebe8,0x2c6f0,0x0,0x0,0x3ebe8,0x2c6f0},
167 {"Japanese Postcard Rotated",0x1,0x24220,0x186a0,0x0,0x0,0x24220,0x186a0},
168 {"Double Japan Postcard Rotated",0x1,0x24220,0x30d40,0x0,0x0,0x24220,0x30d40},
169 {"A6 Rotated",0x1,0x24220,0x19a28,0x0,0x0,0x24220,0x19a28},
170 {"Japan Envelope Kaku #2 Rotated",0x1,0x510e0,0x3a980,0x0,0x0,0x510e0,0x3a980},
171 {"Japan Envelope Kaku #3 Rotated",0x1,0x43a08,0x34bc0,0x0,0x0,0x43a08, 0x34bc0},
172 {"Japan Envelope Chou #3 Rotated",0x1,0x395f8,0x1d4c0,0x0,0x0,0x395f8,0x1d4c0},
173 {"Japan Envelope Chou #4 Rotated",0x1,0x320c8,0x15f90,0x0,0x0,0x320c8,0x15f90},
174 {"B6 (JIS)",0x1,0x1f400,0x2c6f0,0x0,0x0,0x1f400,0x2c6f0},
175 {"B6 (JIS) Rotated",0x1,0x2c6f0,0x1f400,0x0,0x0,0x2c6f0,0x1f400},
176 {"12x11",0x1,0x4a724,0x443e1,0x0,0x0,0x4a724,0x443e1},
177 {"Japan Envelope You #4",0x1,0x19a28,0x395f8,0x0,0x0,0x19a28,0x395f8},
178 {"Japan Envelope You #4 Rotated",0x1,0x395f8,0x19a28,0x0,0x0,0x395f8,0x19a28},
179 {"PRC 16K",0x1,0x2de60,0x3f7a0,0x0,0x0,0x2de60,0x3f7a0},
180 {"PRC 32K",0x1,0x1fbd0,0x2cec0,0x0,0x0,0x1fbd0,0x2cec0},
181 {"PRC 32K(Big)",0x1,0x222e0,0x318f8,0x0,0x0,0x222e0,0x318f8},
182 {"PRC Envelope #1",0x1,0x18e70,0x28488,0x0,0x0,0x18e70,0x28488},
183 {"PRC Envelope #2",0x1,0x18e70,0x2af80,0x0,0x0,0x18e70,0x2af80},
184 {"PRC Envelope #3",0x1,0x1e848,0x2af80,0x0,0x0,0x1e848,0x2af80},
185 {"PRC Envelope #4",0x1,0x1adb0,0x32c80,0x0,0x0,0x1adb0,0x32c80},
186 {"PRC Envelope #5",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
187 {"PRC Envelope #6",0x1,0x1d4c0,0x38270,0x0,0x0,0x1d4c0,0x38270},
188 {"PRC Envelope #7",0x1,0x27100,0x38270,0x0,0x0,0x27100,0x38270},
189 {"PRC Envelope #8",0x1,0x1d4c0,0x4b708,0x0,0x0,0x1d4c0,0x4b708},
190 {"PRC Envelope #9",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
191 {"PRC Envelope #10",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
192 {"PRC 16K Rotated",0x1,0x3f7a0,0x2de60,0x0,0x0,0x3f7a0,0x2de60},
193 {"PRC 32K Rotated",0x1,0x2cec0,0x1fbd0,0x0,0x0,0x2cec0,0x1fbd0},
194 {"PRC 32K(Big) Rotated",0x1,0x318f8,0x222e0,0x0,0x0,0x318f8,0x222e0},
195 {"PRC Envelope #1 Rotated",0x1,0x28488,0x18e70,0x0,0x0,0x28488,0x18e70},
196 {"PRC Envelope #2 Rotated",0x1,0x2af80,0x18e70,0x0,0x0,0x2af80,0x18e70},
197 {"PRC Envelope #3 Rotated",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
198 {"PRC Envelope #4 Rotated",0x1,0x32c80,0x1adb0,0x0,0x0,0x32c80,0x1adb0},
199 {"PRC Envelope #5 Rotated",0x1,0x35b60,0x1adb0,0x0,0x0,0x35b60,0x1adb0},
200 {"PRC Envelope #6 Rotated",0x1,0x38270,0x1d4c0,0x0,0x0,0x38270,0x1d4c0},
201 {"PRC Envelope #7 Rotated",0x1,0x38270,0x27100,0x0,0x0,0x38270,0x27100},
202 {"PRC Envelope #8 Rotated",0x1,0x4b708,0x1d4c0,0x0,0x0,0x4b708,0x1d4c0},
203 {"PRC Envelope #9 Rotated",0x1,0x4f1a0,0x37e88,0x0,0x0,0x4f1a0,0x37e88},
204 {"PRC Envelope #10 Rotated",0x1,0x6fd10,0x4f1a0,0x0,0x0,0x6fd10,0x4f1a0}
208 const char *long_archi;
209 const char *short_archi;
213 #define SPL_ARCH_WIN40 "WIN40"
214 #define SPL_ARCH_W32X86 "W32X86"
215 #define SPL_ARCH_W32MIPS "W32MIPS"
216 #define SPL_ARCH_W32ALPHA "W32ALPHA"
217 #define SPL_ARCH_W32PPC "W32PPC"
218 #define SPL_ARCH_IA64 "IA64"
219 #define SPL_ARCH_X64 "x64"
221 static const struct table_node archi_table[]= {
223 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
224 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
225 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
226 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
227 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
228 {"Windows IA64", SPL_ARCH_IA64, 3 },
229 {"Windows x64", SPL_ARCH_X64, 3 },
234 /****************************************************************************
235 generate a new TDB_DATA key for storing a printer
236 ****************************************************************************/
238 static TDB_DATA make_printer_tdbkey(TALLOC_CTX *ctx, const char *sharename )
244 fstrcpy(share, sharename);
247 keystr = talloc_asprintf(ctx, "%s%s", PRINTERS_PREFIX, share);
248 key = string_term_tdb_data(keystr ? keystr : "");
253 /****************************************************************************
254 generate a new TDB_DATA key for storing a printer security descriptor
255 ****************************************************************************/
257 static TDB_DATA make_printers_secdesc_tdbkey(TALLOC_CTX *ctx,
258 const char* sharename )
264 fstrcpy(share, sharename );
267 keystr = talloc_asprintf(ctx, "%s%s", SECDESC_PREFIX, share);
268 key = string_term_tdb_data(keystr ? keystr : "");
273 /****************************************************************************
274 ****************************************************************************/
276 static bool upgrade_to_version_3(void)
278 TDB_DATA kbuf, newkey, dbuf;
280 DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
282 for (kbuf = tdb_firstkey(tdb_drivers); kbuf.dptr;
283 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
285 dbuf = tdb_fetch(tdb_drivers, kbuf);
287 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
288 DEBUG(0,("upgrade_to_version_3:moving form\n"));
289 if (tdb_store(tdb_forms, kbuf, dbuf, TDB_REPLACE) != 0) {
290 SAFE_FREE(dbuf.dptr);
291 DEBUG(0,("upgrade_to_version_3: failed to move form. Error (%s).\n", tdb_errorstr(tdb_forms)));
294 if (tdb_delete(tdb_drivers, kbuf) != 0) {
295 SAFE_FREE(dbuf.dptr);
296 DEBUG(0,("upgrade_to_version_3: failed to delete form. Error (%s)\n", tdb_errorstr(tdb_drivers)));
301 if (strncmp((const char *)kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
302 DEBUG(0,("upgrade_to_version_3:moving printer\n"));
303 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
304 SAFE_FREE(dbuf.dptr);
305 DEBUG(0,("upgrade_to_version_3: failed to move printer. Error (%s)\n", tdb_errorstr(tdb_printers)));
308 if (tdb_delete(tdb_drivers, kbuf) != 0) {
309 SAFE_FREE(dbuf.dptr);
310 DEBUG(0,("upgrade_to_version_3: failed to delete printer. Error (%s)\n", tdb_errorstr(tdb_drivers)));
315 if (strncmp((const char *)kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
316 DEBUG(0,("upgrade_to_version_3:moving secdesc\n"));
317 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
318 SAFE_FREE(dbuf.dptr);
319 DEBUG(0,("upgrade_to_version_3: failed to move secdesc. Error (%s)\n", tdb_errorstr(tdb_printers)));
322 if (tdb_delete(tdb_drivers, kbuf) != 0) {
323 SAFE_FREE(dbuf.dptr);
324 DEBUG(0,("upgrade_to_version_3: failed to delete secdesc. Error (%s)\n", tdb_errorstr(tdb_drivers)));
329 SAFE_FREE(dbuf.dptr);
335 /*******************************************************************
336 Fix an issue with security descriptors. Printer sec_desc must
337 use more than the generic bits that were previously used
338 in <= 3.0.14a. They must also have a owner and group SID assigned.
339 Otherwise, any printers than have been migrated to a Windows
340 host using printmig.exe will not be accessible.
341 *******************************************************************/
343 static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
344 TDB_DATA data, void *state )
347 SEC_DESC_BUF *sd_orig = NULL;
348 SEC_DESC_BUF *sd_new, *sd_store;
349 SEC_DESC *sec, *new_sec;
350 TALLOC_CTX *ctx = state;
355 if (!data.dptr || data.dsize == 0) {
359 if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) != 0 ) {
363 /* upgrade the security descriptor */
367 prs_init_empty( &ps, ctx, UNMARSHALL );
368 prs_give_memory( &ps, (char *)data.dptr, data.dsize, False );
370 if ( !sec_io_desc_buf( "sec_desc_upg_fn", &sd_orig, &ps, 1 ) ) {
371 /* delete bad entries */
372 DEBUG(0,("sec_desc_upg_fn: Failed to parse original sec_desc for %si. Deleting....\n",
373 (const char *)key.dptr ));
374 tdb_delete( tdb_printers, key );
385 /* is this even valid? */
392 /* update access masks */
394 for ( i=0; i<sec->dacl->num_aces; i++ ) {
395 switch ( sec->dacl->aces[i].access_mask ) {
396 case (GENERIC_READ_ACCESS | GENERIC_WRITE_ACCESS | GENERIC_EXECUTE_ACCESS):
397 sec->dacl->aces[i].access_mask = PRINTER_ACE_PRINT;
400 case GENERIC_ALL_ACCESS:
401 sec->dacl->aces[i].access_mask = PRINTER_ACE_FULL_CONTROL;
404 case READ_CONTROL_ACCESS:
405 sec->dacl->aces[i].access_mask = PRINTER_ACE_MANAGE_DOCUMENTS;
407 default: /* no change */
412 /* create a new SEC_DESC with the appropriate owner and group SIDs */
414 new_sec = make_sec_desc( ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
415 &global_sid_Builtin_Administrators,
416 &global_sid_Builtin_Administrators,
417 NULL, NULL, &size_new_sec );
422 sd_new = make_sec_desc_buf( ctx, size_new_sec, new_sec );
428 if ( !(sd_store = sec_desc_merge( ctx, sd_new, sd_orig )) ) {
429 DEBUG(0,("sec_desc_upg_fn: Failed to update sec_desc for %s\n", key.dptr ));
438 sd_size = ndr_size_security_descriptor(sd_store->sd, NULL, 0)
439 + sizeof(SEC_DESC_BUF);
440 if ( !prs_init(&ps, sd_size, ctx, MARSHALL) ) {
441 DEBUG(0,("sec_desc_upg_fn: Failed to allocate prs memory for %s\n", key.dptr ));
445 if ( !sec_io_desc_buf( "sec_desc_upg_fn", &sd_store, &ps, 1 ) ) {
446 DEBUG(0,("sec_desc_upg_fn: Failed to parse new sec_desc for %s\n", key.dptr ));
451 data.dptr = (uint8 *)prs_data_p( &ps );
452 data.dsize = sd_size;
454 result = tdb_store( tdb_printers, key, data, TDB_REPLACE );
458 /* 0 to continue and non-zero to stop traversal */
460 return (result == -1);
463 /*******************************************************************
464 *******************************************************************/
466 static bool upgrade_to_version_4(void)
471 DEBUG(0,("upgrade_to_version_4: upgrading printer security descriptors\n"));
473 if ( !(ctx = talloc_init( "upgrade_to_version_4" )) )
476 result = tdb_traverse( tdb_printers, sec_desc_upg_fn, ctx );
478 talloc_destroy( ctx );
480 return ( result != -1 );
483 /*******************************************************************
484 Fix an issue with security descriptors. Printer sec_desc must
485 use more than the generic bits that were previously used
486 in <= 3.0.14a. They must also have a owner and group SID assigned.
487 Otherwise, any printers than have been migrated to a Windows
488 host using printmig.exe will not be accessible.
489 *******************************************************************/
491 static int normalize_printers_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
492 TDB_DATA data, void *state )
494 TALLOC_CTX *ctx = talloc_tos();
497 if (!data.dptr || data.dsize == 0)
500 /* upgrade printer records and security descriptors */
502 if ( strncmp((const char *) key.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX) ) == 0 ) {
503 new_key = make_printer_tdbkey(ctx, (const char *)key.dptr+strlen(PRINTERS_PREFIX) );
505 else if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) == 0 ) {
506 new_key = make_printers_secdesc_tdbkey(ctx, (const char *)key.dptr+strlen(SECDESC_PREFIX) );
509 /* ignore this record */
513 /* delete the original record and store under the normalized key */
515 if ( tdb_delete( the_tdb, key ) != 0 ) {
516 DEBUG(0,("normalize_printers_fn: tdb_delete for [%s] failed!\n",
521 if ( tdb_store( the_tdb, new_key, data, TDB_REPLACE) != 0 ) {
522 DEBUG(0,("normalize_printers_fn: failed to store new record for [%s]!\n",
530 /*******************************************************************
531 *******************************************************************/
533 static bool upgrade_to_version_5(void)
538 DEBUG(0,("upgrade_to_version_5: normalizing printer keys\n"));
540 if ( !(ctx = talloc_init( "upgrade_to_version_5" )) )
543 result = tdb_traverse( tdb_printers, normalize_printers_fn, NULL );
545 talloc_destroy( ctx );
547 return ( result != -1 );
550 /****************************************************************************
551 Open the NT printing tdbs. Done once before fork().
552 ****************************************************************************/
554 bool nt_printing_init(struct messaging_context *msg_ctx)
556 const char *vstring = "INFO/version";
560 if ( tdb_drivers && tdb_printers && tdb_forms )
564 tdb_close(tdb_drivers);
565 tdb_drivers = tdb_open_log(state_path("ntdrivers.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
567 DEBUG(0,("nt_printing_init: Failed to open nt drivers database %s (%s)\n",
568 state_path("ntdrivers.tdb"), strerror(errno) ));
573 tdb_close(tdb_printers);
574 tdb_printers = tdb_open_log(state_path("ntprinters.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
576 DEBUG(0,("nt_printing_init: Failed to open nt printers database %s (%s)\n",
577 state_path("ntprinters.tdb"), strerror(errno) ));
582 tdb_close(tdb_forms);
583 tdb_forms = tdb_open_log(state_path("ntforms.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
585 DEBUG(0,("nt_printing_init: Failed to open nt forms database %s (%s)\n",
586 state_path("ntforms.tdb"), strerror(errno) ));
590 /* handle a Samba upgrade */
592 vers_id = tdb_fetch_int32(tdb_drivers, vstring);
594 DEBUG(10, ("Fresh database\n"));
595 tdb_store_int32( tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5 );
596 vers_id = NTDRIVERS_DATABASE_VERSION_5;
599 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
601 if ((vers_id == NTDRIVERS_DATABASE_VERSION_1) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_1)) {
602 if (!upgrade_to_version_3())
604 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
605 vers_id = NTDRIVERS_DATABASE_VERSION_3;
608 if ((vers_id == NTDRIVERS_DATABASE_VERSION_2) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_2)) {
609 /* Written on a bigendian machine with old fetch_int code. Save as le. */
610 /* The only upgrade between V2 and V3 is to save the version in little-endian. */
611 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
612 vers_id = NTDRIVERS_DATABASE_VERSION_3;
615 if (vers_id == NTDRIVERS_DATABASE_VERSION_3 ) {
616 if ( !upgrade_to_version_4() )
618 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_4);
619 vers_id = NTDRIVERS_DATABASE_VERSION_4;
622 if (vers_id == NTDRIVERS_DATABASE_VERSION_4 ) {
623 if ( !upgrade_to_version_5() )
625 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5);
626 vers_id = NTDRIVERS_DATABASE_VERSION_5;
630 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
631 DEBUG(0,("nt_printing_init: Unknown printer database version [%d]\n", vers_id));
636 update_c_setprinter(True);
639 * register callback to handle updating printers as new
640 * drivers are installed
643 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
644 do_drv_upgrade_printer);
647 * register callback to handle updating printer data
648 * when a driver is initialized
651 messaging_register(msg_ctx, NULL, MSG_PRINTERDATA_INIT_RESET,
652 reset_all_printerdata);
654 /* of course, none of the message callbacks matter if you don't
655 tell messages.c that you interested in receiving PRINT_GENERAL
656 msgs. This is done in claim_connection() */
659 if ( lp_security() == SEC_ADS ) {
660 win_rc = check_published_printers();
661 if (!W_ERROR_IS_OK(win_rc))
662 DEBUG(0, ("nt_printing_init: error checking published printers: %s\n", win_errstr(win_rc)));
668 /*******************************************************************
669 Function to allow filename parsing "the old way".
670 ********************************************************************/
672 static char *driver_unix_convert(connection_struct *conn,
673 const char *old_name,
674 SMB_STRUCT_STAT *pst)
676 TALLOC_CTX *ctx = talloc_tos();
677 char *name = talloc_strdup(ctx, old_name);
678 char *new_name = NULL;
684 name = unix_clean_name(ctx, name);
688 trim_string(name,"/","/");
689 unix_convert(ctx,conn, name, false, &new_name, NULL, pst);
693 /*******************************************************************
694 tdb traversal function for counting printers.
695 ********************************************************************/
697 static int traverse_counting_printers(TDB_CONTEXT *t, TDB_DATA key,
698 TDB_DATA data, void *context)
700 int *printer_count = (int*)context;
702 if (memcmp(PRINTERS_PREFIX, key.dptr, sizeof(PRINTERS_PREFIX)-1) == 0) {
704 DEBUG(10,("traverse_counting_printers: printer = [%s] printer_count = %d\n", key.dptr, *printer_count));
710 /*******************************************************************
711 Update the spooler global c_setprinter. This variable is initialized
712 when the parent smbd starts with the number of existing printers. It
713 is monotonically increased by the current number of printers *after*
714 each add or delete printer RPC. Only Microsoft knows why... JRR020119
715 ********************************************************************/
717 uint32 update_c_setprinter(bool initialize)
720 int32 printer_count = 0;
722 tdb_lock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
724 /* Traverse the tdb, counting the printers */
725 tdb_traverse(tdb_printers, traverse_counting_printers, (void *)&printer_count);
727 /* If initializing, set c_setprinter to current printers count
728 * otherwise, bump it by the current printer count
731 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER) + printer_count;
733 c_setprinter = printer_count;
735 DEBUG(10,("update_c_setprinter: c_setprinter = %u\n", (unsigned int)c_setprinter));
736 tdb_store_int32(tdb_printers, GLOBAL_C_SETPRINTER, c_setprinter);
738 tdb_unlock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
740 return (uint32)c_setprinter;
743 /*******************************************************************
744 Get the spooler global c_setprinter, accounting for initialization.
745 ********************************************************************/
747 uint32 get_c_setprinter(void)
749 int32 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER);
751 if (c_setprinter == (int32)-1)
752 c_setprinter = update_c_setprinter(True);
754 DEBUG(10,("get_c_setprinter: c_setprinter = %d\n", c_setprinter));
756 return (uint32)c_setprinter;
759 /****************************************************************************
760 Get builtin form struct list.
761 ****************************************************************************/
763 int get_builtin_ntforms(nt_forms_struct **list)
765 *list = (nt_forms_struct *)memdup(&default_forms[0], sizeof(default_forms));
769 return sizeof(default_forms) / sizeof(default_forms[0]);
772 /****************************************************************************
773 get a builtin form struct
774 ****************************************************************************/
776 bool get_a_builtin_ntform_by_string(const char *form_name, nt_forms_struct *form)
779 DEBUGADD(6,("Looking for builtin form %s \n", form_name));
780 count = sizeof(default_forms) / sizeof(default_forms[0]);
781 for (i=0;i<count;i++) {
782 if (strequal(form_name,default_forms[i].name)) {
783 DEBUGADD(6,("Found builtin form %s \n", form_name));
784 memcpy(form,&default_forms[i],sizeof(*form));
792 bool get_a_builtin_ntform(UNISTR2 *uni_formname,nt_forms_struct *form)
795 unistr2_to_ascii(form_name, uni_formname, sizeof(form_name));
796 return get_a_builtin_ntform_by_string(form_name, form);
799 /****************************************************************************
800 get a form struct list.
801 ****************************************************************************/
803 int get_ntforms(nt_forms_struct **list)
805 TDB_DATA kbuf, newkey, dbuf;
806 nt_forms_struct form;
813 for (kbuf = tdb_firstkey(tdb_forms);
815 newkey = tdb_nextkey(tdb_forms, kbuf), free(kbuf.dptr), kbuf=newkey)
817 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) != 0)
820 dbuf = tdb_fetch(tdb_forms, kbuf);
824 fstrcpy(form.name, (const char *)kbuf.dptr+strlen(FORMS_PREFIX));
825 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "dddddddd",
826 &i, &form.flag, &form.width, &form.length, &form.left,
827 &form.top, &form.right, &form.bottom);
828 SAFE_FREE(dbuf.dptr);
829 if (ret != dbuf.dsize)
832 *list = SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1);
834 DEBUG(0,("get_ntforms: Realloc fail.\n"));
845 /****************************************************************************
846 write a form struct list
847 ****************************************************************************/
849 int write_ntforms(nt_forms_struct **list, int number)
851 TALLOC_CTX *ctx = talloc_tos();
858 for (i=0;i<number;i++) {
859 /* save index, so list is rebuilt in correct order */
860 len = tdb_pack(NULL, 0, "dddddddd",
861 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
862 (*list)[i].left, (*list)[i].top, (*list)[i].right,
867 buf = TALLOC_ARRAY(ctx, char, len);
871 len = tdb_pack((uint8 *)buf, len, "dddddddd",
872 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
873 (*list)[i].left, (*list)[i].top, (*list)[i].right,
875 key = talloc_asprintf(ctx, "%s%s", FORMS_PREFIX, (*list)[i].name);
880 dbuf.dptr = (uint8 *)buf;
881 if (tdb_store_bystring(tdb_forms, key, dbuf, TDB_REPLACE) != 0) {
893 /****************************************************************************
894 add a form struct at the end of the list
895 ****************************************************************************/
896 bool add_a_form(nt_forms_struct **list, const FORM *form, int *count)
903 * NT tries to add forms even when
904 * they are already in the base
905 * only update the values if already present
910 unistr2_to_ascii(form_name, &form->name, sizeof(form_name));
911 for (n=0; n<*count; n++) {
912 if ( strequal((*list)[n].name, form_name) ) {
919 if((*list=SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1)) == NULL) {
920 DEBUG(0,("add_a_form: failed to enlarge forms list!\n"));
923 unistr2_to_ascii((*list)[n].name, &form->name, sizeof((*list)[n].name));
927 (*list)[n].flag=form->flags;
928 (*list)[n].width=form->size_x;
929 (*list)[n].length=form->size_y;
930 (*list)[n].left=form->left;
931 (*list)[n].top=form->top;
932 (*list)[n].right=form->right;
933 (*list)[n].bottom=form->bottom;
935 DEBUG(6,("add_a_form: Successfully %s form [%s]\n",
936 update ? "updated" : "added", form_name));
941 /****************************************************************************
942 Delete a named form struct.
943 ****************************************************************************/
945 bool delete_a_form_by_string(nt_forms_struct **list, const char *del_name, int *count, WERROR *ret)
952 for (n=0; n<*count; n++) {
953 if (!strncmp((*list)[n].name, del_name, strlen(del_name))) {
954 DEBUG(103, ("delete_a_form, [%s] in list\n", del_name));
960 DEBUG(10,("delete_a_form, [%s] not found\n", del_name));
961 *ret = WERR_INVALID_PARAM;
965 if (asprintf(&key, "%s%s", FORMS_PREFIX, (*list)[n].name) < 0) {
969 if (tdb_delete_bystring(tdb_forms, key) != 0) {
978 bool delete_a_form(nt_forms_struct **list, UNISTR2 *del_name, int *count, WERROR *ret)
982 unistr2_to_ascii(form_name, del_name, sizeof(form_name));
984 return delete_a_form_by_string(list, form_name, count, ret);
987 /****************************************************************************
988 Update a form struct.
989 ****************************************************************************/
991 void update_a_form(nt_forms_struct **list, const FORM *form, int count)
995 unistr2_to_ascii(form_name, &(form->name), sizeof(form_name));
997 DEBUG(106, ("[%s]\n", form_name));
998 for (n=0; n<count; n++) {
999 DEBUGADD(106, ("n [%d]:[%s]\n", n, (*list)[n].name));
1000 if (!strncmp((*list)[n].name, form_name, strlen(form_name)))
1004 if (n==count) return;
1006 (*list)[n].flag=form->flags;
1007 (*list)[n].width=form->size_x;
1008 (*list)[n].length=form->size_y;
1009 (*list)[n].left=form->left;
1010 (*list)[n].top=form->top;
1011 (*list)[n].right=form->right;
1012 (*list)[n].bottom=form->bottom;
1015 /****************************************************************************
1016 Get the nt drivers list.
1017 Traverse the database and look-up the matching names.
1018 ****************************************************************************/
1019 int get_ntdrivers(fstring **list, const char *architecture, uint32 version)
1022 const char *short_archi;
1024 TDB_DATA kbuf, newkey;
1026 short_archi = get_short_archi(architecture);
1031 if (asprintf(&key, "%s%s/%d/", DRIVERS_PREFIX,
1032 short_archi, version) < 0) {
1036 for (kbuf = tdb_firstkey(tdb_drivers);
1038 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
1040 if (strncmp((const char *)kbuf.dptr, key, strlen(key)) != 0)
1043 if((*list = SMB_REALLOC_ARRAY(*list, fstring, total+1)) == NULL) {
1044 DEBUG(0,("get_ntdrivers: failed to enlarge list!\n"));
1049 fstrcpy((*list)[total], (const char *)kbuf.dptr+strlen(key));
1057 /****************************************************************************
1058 Function to do the mapping between the long architecture name and
1060 ****************************************************************************/
1062 const char *get_short_archi(const char *long_archi)
1066 DEBUG(107,("Getting architecture dependant directory\n"));
1069 } while ( (archi_table[i].long_archi!=NULL ) &&
1070 StrCaseCmp(long_archi, archi_table[i].long_archi) );
1072 if (archi_table[i].long_archi==NULL) {
1073 DEBUGADD(10,("Unknown architecture [%s] !\n", long_archi));
1077 /* this might be client code - but shouldn't this be an fstrcpy etc? */
1079 DEBUGADD(108,("index: [%d]\n", i));
1080 DEBUGADD(108,("long architecture: [%s]\n", archi_table[i].long_archi));
1081 DEBUGADD(108,("short architecture: [%s]\n", archi_table[i].short_archi));
1083 return archi_table[i].short_archi;
1086 /****************************************************************************
1087 Version information in Microsoft files is held in a VS_VERSION_INFO structure.
1088 There are two case to be covered here: PE (Portable Executable) and NE (New
1089 Executable) files. Both files support the same INFO structure, but PE files
1090 store the signature in unicode, and NE files store it as !unicode.
1091 returns -1 on error, 1 on version info found, and 0 on no version info found.
1092 ****************************************************************************/
1094 static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32 *minor)
1100 if ((buf=(char *)SMB_MALLOC(DOS_HEADER_SIZE)) == NULL) {
1101 DEBUG(0,("get_file_version: PE file [%s] DOS Header malloc failed bytes = %d\n",
1102 fname, DOS_HEADER_SIZE));
1106 if ((byte_count = vfs_read_data(fsp, buf, DOS_HEADER_SIZE)) < DOS_HEADER_SIZE) {
1107 DEBUG(3,("get_file_version: File [%s] DOS header too short, bytes read = %lu\n",
1108 fname, (unsigned long)byte_count));
1109 goto no_version_info;
1112 /* Is this really a DOS header? */
1113 if (SVAL(buf,DOS_HEADER_MAGIC_OFFSET) != DOS_HEADER_MAGIC) {
1114 DEBUG(6,("get_file_version: File [%s] bad DOS magic = 0x%x\n",
1115 fname, SVAL(buf,DOS_HEADER_MAGIC_OFFSET)));
1116 goto no_version_info;
1119 /* Skip OEM header (if any) and the DOS stub to start of Windows header */
1120 if (SMB_VFS_LSEEK(fsp, SVAL(buf,DOS_HEADER_LFANEW_OFFSET), SEEK_SET) == (SMB_OFF_T)-1) {
1121 DEBUG(3,("get_file_version: File [%s] too short, errno = %d\n",
1123 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1124 goto no_version_info;
1127 /* Note: DOS_HEADER_SIZE and NE_HEADER_SIZE are incidentally same */
1128 if ((byte_count = vfs_read_data(fsp, buf, NE_HEADER_SIZE)) < NE_HEADER_SIZE) {
1129 DEBUG(3,("get_file_version: File [%s] Windows header too short, bytes read = %lu\n",
1130 fname, (unsigned long)byte_count));
1131 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1132 goto no_version_info;
1135 /* The header may be a PE (Portable Executable) or an NE (New Executable) */
1136 if (IVAL(buf,PE_HEADER_SIGNATURE_OFFSET) == PE_HEADER_SIGNATURE) {
1137 unsigned int num_sections;
1138 unsigned int section_table_bytes;
1140 /* Just skip over optional header to get to section table */
1141 if (SMB_VFS_LSEEK(fsp,
1142 SVAL(buf,PE_HEADER_OPTIONAL_HEADER_SIZE)-(NE_HEADER_SIZE-PE_HEADER_SIZE),
1143 SEEK_CUR) == (SMB_OFF_T)-1) {
1144 DEBUG(3,("get_file_version: File [%s] Windows optional header too short, errno = %d\n",
1149 /* get the section table */
1150 num_sections = SVAL(buf,PE_HEADER_NUMBER_OF_SECTIONS);
1151 section_table_bytes = num_sections * PE_HEADER_SECT_HEADER_SIZE;
1152 if (section_table_bytes == 0)
1156 if ((buf=(char *)SMB_MALLOC(section_table_bytes)) == NULL) {
1157 DEBUG(0,("get_file_version: PE file [%s] section table malloc failed bytes = %d\n",
1158 fname, section_table_bytes));
1162 if ((byte_count = vfs_read_data(fsp, buf, section_table_bytes)) < section_table_bytes) {
1163 DEBUG(3,("get_file_version: PE file [%s] Section header too short, bytes read = %lu\n",
1164 fname, (unsigned long)byte_count));
1168 /* Iterate the section table looking for the resource section ".rsrc" */
1169 for (i = 0; i < num_sections; i++) {
1170 int sec_offset = i * PE_HEADER_SECT_HEADER_SIZE;
1172 if (strcmp(".rsrc", &buf[sec_offset+PE_HEADER_SECT_NAME_OFFSET]) == 0) {
1173 unsigned int section_pos = IVAL(buf,sec_offset+PE_HEADER_SECT_PTR_DATA_OFFSET);
1174 unsigned int section_bytes = IVAL(buf,sec_offset+PE_HEADER_SECT_SIZE_DATA_OFFSET);
1176 if (section_bytes == 0)
1180 if ((buf=(char *)SMB_MALLOC(section_bytes)) == NULL) {
1181 DEBUG(0,("get_file_version: PE file [%s] version malloc failed bytes = %d\n",
1182 fname, section_bytes));
1186 /* Seek to the start of the .rsrc section info */
1187 if (SMB_VFS_LSEEK(fsp, section_pos, SEEK_SET) == (SMB_OFF_T)-1) {
1188 DEBUG(3,("get_file_version: PE file [%s] too short for section info, errno = %d\n",
1193 if ((byte_count = vfs_read_data(fsp, buf, section_bytes)) < section_bytes) {
1194 DEBUG(3,("get_file_version: PE file [%s] .rsrc section too short, bytes read = %lu\n",
1195 fname, (unsigned long)byte_count));
1199 if (section_bytes < VS_VERSION_INFO_UNICODE_SIZE)
1202 for (i=0; i<section_bytes-VS_VERSION_INFO_UNICODE_SIZE; i++) {
1203 /* Scan for 1st 3 unicoded bytes followed by word aligned magic value */
1204 if (buf[i] == 'V' && buf[i+1] == '\0' && buf[i+2] == 'S') {
1205 /* Align to next long address */
1206 int pos = (i + sizeof(VS_SIGNATURE)*2 + 3) & 0xfffffffc;
1208 if (IVAL(buf,pos) == VS_MAGIC_VALUE) {
1209 *major = IVAL(buf,pos+VS_MAJOR_OFFSET);
1210 *minor = IVAL(buf,pos+VS_MINOR_OFFSET);
1212 DEBUG(6,("get_file_version: PE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1213 fname, *major, *minor,
1214 (*major>>16)&0xffff, *major&0xffff,
1215 (*minor>>16)&0xffff, *minor&0xffff));
1224 /* Version info not found, fall back to origin date/time */
1225 DEBUG(10,("get_file_version: PE file [%s] has no version info\n", fname));
1229 } else if (SVAL(buf,NE_HEADER_SIGNATURE_OFFSET) == NE_HEADER_SIGNATURE) {
1230 if (CVAL(buf,NE_HEADER_TARGET_OS_OFFSET) != NE_HEADER_TARGOS_WIN ) {
1231 DEBUG(3,("get_file_version: NE file [%s] wrong target OS = 0x%x\n",
1232 fname, CVAL(buf,NE_HEADER_TARGET_OS_OFFSET)));
1233 /* At this point, we assume the file is in error. It still could be somthing
1234 * else besides a NE file, but it unlikely at this point. */
1238 /* Allocate a bit more space to speed up things */
1240 if ((buf=(char *)SMB_MALLOC(VS_NE_BUF_SIZE)) == NULL) {
1241 DEBUG(0,("get_file_version: NE file [%s] malloc failed bytes = %d\n",
1242 fname, PE_HEADER_SIZE));
1246 /* This is a HACK! I got tired of trying to sort through the messy
1247 * 'NE' file format. If anyone wants to clean this up please have at
1248 * it, but this works. 'NE' files will eventually fade away. JRR */
1249 while((byte_count = vfs_read_data(fsp, buf, VS_NE_BUF_SIZE)) > 0) {
1250 /* Cover case that should not occur in a well formed 'NE' .dll file */
1251 if (byte_count-VS_VERSION_INFO_SIZE <= 0) break;
1253 for(i=0; i<byte_count; i++) {
1254 /* Fast skip past data that can't possibly match */
1255 if (buf[i] != 'V') continue;
1257 /* Potential match data crosses buf boundry, move it to beginning
1258 * of buf, and fill the buf with as much as it will hold. */
1259 if (i>byte_count-VS_VERSION_INFO_SIZE) {
1262 memcpy(buf, &buf[i], byte_count-i);
1263 if ((bc = vfs_read_data(fsp, &buf[byte_count-i], VS_NE_BUF_SIZE-
1264 (byte_count-i))) < 0) {
1266 DEBUG(0,("get_file_version: NE file [%s] Read error, errno=%d\n",
1271 byte_count = bc + (byte_count - i);
1272 if (byte_count<VS_VERSION_INFO_SIZE) break;
1277 /* Check that the full signature string and the magic number that
1278 * follows exist (not a perfect solution, but the chances that this
1279 * occurs in code is, well, remote. Yes I know I'm comparing the 'V'
1280 * twice, as it is simpler to read the code. */
1281 if (strcmp(&buf[i], VS_SIGNATURE) == 0) {
1282 /* Compute skip alignment to next long address */
1283 int skip = -(SMB_VFS_LSEEK(fsp, 0, SEEK_CUR) - (byte_count - i) +
1284 sizeof(VS_SIGNATURE)) & 3;
1285 if (IVAL(buf,i+sizeof(VS_SIGNATURE)+skip) != 0xfeef04bd) continue;
1287 *major = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MAJOR_OFFSET);
1288 *minor = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MINOR_OFFSET);
1289 DEBUG(6,("get_file_version: NE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1290 fname, *major, *minor,
1291 (*major>>16)&0xffff, *major&0xffff,
1292 (*minor>>16)&0xffff, *minor&0xffff));
1299 /* Version info not found, fall back to origin date/time */
1300 DEBUG(0,("get_file_version: NE file [%s] Version info not found\n", fname));
1305 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1306 DEBUG(3,("get_file_version: File [%s] unknown file format, signature = 0x%x\n",
1307 fname, IVAL(buf,PE_HEADER_SIGNATURE_OFFSET)));
1318 /****************************************************************************
1319 Drivers for Microsoft systems contain multiple files. Often, multiple drivers
1320 share one or more files. During the MS installation process files are checked
1321 to insure that only a newer version of a shared file is installed over an
1322 older version. There are several possibilities for this comparison. If there
1323 is no previous version, the new one is newer (obviously). If either file is
1324 missing the version info structure, compare the creation date (on Unix use
1325 the modification date). Otherwise chose the numerically larger version number.
1326 ****************************************************************************/
1328 static int file_version_is_newer(connection_struct *conn, fstring new_file, fstring old_file)
1330 bool use_version = true;
1331 char *filepath = NULL;
1335 time_t new_create_time;
1339 time_t old_create_time;
1341 files_struct *fsp = NULL;
1343 SMB_STRUCT_STAT stat_buf;
1347 SET_STAT_INVALID(st);
1348 SET_STAT_INVALID(stat_buf);
1349 new_create_time = (time_t)0;
1350 old_create_time = (time_t)0;
1352 /* Get file version info (if available) for previous file (if it exists) */
1353 filepath = driver_unix_convert(conn,old_file,&stat_buf);
1358 status = SMB_VFS_CREATE_FILE(
1361 0, /* root_dir_fid */
1362 filepath, /* fname */
1363 0, /* create_file_flags */
1364 FILE_GENERIC_READ, /* access_mask */
1365 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1366 FILE_OPEN, /* create_disposition*/
1367 0, /* create_options */
1368 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1369 INTERNAL_OPEN_ONLY, /* oplock_request */
1370 0, /* allocation_size */
1375 &stat_buf); /* psbuf */
1377 if (!NT_STATUS_IS_OK(status)) {
1378 /* Old file not found, so by definition new file is in fact newer */
1379 DEBUG(10,("file_version_is_newer: Can't open old file [%s], errno = %d\n",
1384 int ret = get_file_version(fsp, old_file, &old_major, &old_minor);
1390 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1392 use_version = false;
1393 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1396 old_create_time = st.st_mtime;
1397 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n", old_create_time));
1400 close_file(NULL, fsp, NORMAL_CLOSE);
1403 /* Get file version info (if available) for new file */
1404 filepath = driver_unix_convert(conn,new_file,&stat_buf);
1409 status = SMB_VFS_CREATE_FILE(
1412 0, /* root_dir_fid */
1413 filepath, /* fname */
1414 0, /* create_file_flags */
1415 FILE_GENERIC_READ, /* access_mask */
1416 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1417 FILE_OPEN, /* create_disposition*/
1418 0, /* create_options */
1419 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1420 INTERNAL_OPEN_ONLY, /* oplock_request */
1421 0, /* allocation_size */
1426 &stat_buf); /* psbuf */
1428 if (!NT_STATUS_IS_OK(status)) {
1429 /* New file not found, this shouldn't occur if the caller did its job */
1430 DEBUG(3,("file_version_is_newer: Can't open new file [%s], errno = %d\n",
1435 int ret = get_file_version(fsp, new_file, &new_major, &new_minor);
1441 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1443 use_version = false;
1444 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1447 new_create_time = st.st_mtime;
1448 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n", new_create_time));
1451 close_file(NULL, fsp, NORMAL_CLOSE);
1454 if (use_version && (new_major != old_major || new_minor != old_minor)) {
1455 /* Compare versions and choose the larger version number */
1456 if (new_major > old_major ||
1457 (new_major == old_major && new_minor > old_minor)) {
1459 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1463 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1468 /* Compare modification time/dates and choose the newest time/date */
1469 if (new_create_time > old_create_time) {
1470 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1474 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1481 close_file(NULL, fsp, NORMAL_CLOSE);
1485 /****************************************************************************
1486 Determine the correct cVersion associated with an architecture and driver
1487 ****************************************************************************/
1488 static uint32 get_correct_cversion(struct pipes_struct *p,
1489 const char *architecture,
1490 fstring driverpath_in,
1495 char *driverpath = NULL;
1496 files_struct *fsp = NULL;
1498 connection_struct *conn = NULL;
1501 fstring printdollar;
1502 int printdollar_snum;
1504 SET_STAT_INVALID(st);
1506 *perr = WERR_INVALID_PARAM;
1508 /* If architecture is Windows 95/98/ME, the version is always 0. */
1509 if (strcmp(architecture, SPL_ARCH_WIN40) == 0) {
1510 DEBUG(10,("get_correct_cversion: Driver is Win9x, cversion = 0\n"));
1515 /* If architecture is Windows x64, the version is always 3. */
1516 if (strcmp(architecture, SPL_ARCH_X64) == 0) {
1517 DEBUG(10,("get_correct_cversion: Driver is x64, cversion = 3\n"));
1522 fstrcpy(printdollar, "print$");
1524 printdollar_snum = find_service(printdollar);
1525 if (printdollar_snum == -1) {
1526 *perr = WERR_NO_SUCH_SHARE;
1530 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
1531 lp_pathname(printdollar_snum),
1532 p->server_info, &oldcwd);
1533 if (!NT_STATUS_IS_OK(nt_status)) {
1534 DEBUG(0,("get_correct_cversion: create_conn_struct "
1535 "returned %s\n", nt_errstr(nt_status)));
1536 *perr = ntstatus_to_werror(nt_status);
1540 /* Open the driver file (Portable Executable format) and determine the
1541 * deriver the cversion. */
1542 driverpath = talloc_asprintf(talloc_tos(),
1551 driverpath = driver_unix_convert(conn,driverpath,&st);
1557 if (!vfs_file_exist(conn, driverpath, &st)) {
1558 *perr = WERR_BADFILE;
1562 status = SMB_VFS_CREATE_FILE(
1565 0, /* root_dir_fid */
1566 driverpath, /* fname */
1567 0, /* create_file_flags */
1568 FILE_GENERIC_READ, /* access_mask */
1569 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
1570 FILE_OPEN, /* create_disposition*/
1571 0, /* create_options */
1572 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1573 INTERNAL_OPEN_ONLY, /* oplock_request */
1574 0, /* allocation_size */
1581 if (!NT_STATUS_IS_OK(status)) {
1582 DEBUG(3,("get_correct_cversion: Can't open file [%s], errno = %d\n",
1583 driverpath, errno));
1584 *perr = WERR_ACCESS_DENIED;
1589 int ret = get_file_version(fsp, driverpath, &major, &minor);
1590 if (ret == -1) goto error_exit;
1593 DEBUG(6,("get_correct_cversion: Version info not found [%s]\n", driverpath));
1598 * This is a Microsoft'ism. See references in MSDN to VER_FILEVERSION
1599 * for more details. Version in this case is not just the version of the
1600 * file, but the version in the sense of kernal mode (2) vs. user mode
1601 * (3) drivers. Other bits of the version fields are the version info.
1604 cversion = major & 0x0000ffff;
1606 case 2: /* WinNT drivers */
1607 case 3: /* Win2K drivers */
1611 DEBUG(6,("get_correct_cversion: cversion invalid [%s] cversion = %d\n",
1612 driverpath, cversion));
1616 DEBUG(10,("get_correct_cversion: Version info found [%s] major = 0x%x minor = 0x%x\n",
1617 driverpath, major, minor));
1620 DEBUG(10,("get_correct_cversion: Driver file [%s] cversion = %d\n",
1621 driverpath, cversion));
1629 close_file(NULL, fsp, NORMAL_CLOSE);
1632 vfs_ChDir(conn, oldcwd);
1633 conn_free_internal(conn);
1635 if (cversion != -1) {
1641 /****************************************************************************
1642 ****************************************************************************/
1643 static WERROR clean_up_driver_struct_level_3(struct pipes_struct *rpc_pipe,
1644 NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver)
1646 const char *architecture;
1652 /* clean up the driver name.
1653 * we can get .\driver.dll
1654 * or worse c:\windows\system\driver.dll !
1656 /* using an intermediate string to not have overlaping memcpy()'s */
1657 if ((p = strrchr(driver->driverpath,'\\')) != NULL) {
1658 fstrcpy(new_name, p+1);
1659 fstrcpy(driver->driverpath, new_name);
1662 if ((p = strrchr(driver->datafile,'\\')) != NULL) {
1663 fstrcpy(new_name, p+1);
1664 fstrcpy(driver->datafile, new_name);
1667 if ((p = strrchr(driver->configfile,'\\')) != NULL) {
1668 fstrcpy(new_name, p+1);
1669 fstrcpy(driver->configfile, new_name);
1672 if ((p = strrchr(driver->helpfile,'\\')) != NULL) {
1673 fstrcpy(new_name, p+1);
1674 fstrcpy(driver->helpfile, new_name);
1677 if (driver->dependentfiles) {
1678 for (i=0; *driver->dependentfiles[i]; i++) {
1679 if ((p = strrchr(driver->dependentfiles[i],'\\')) != NULL) {
1680 fstrcpy(new_name, p+1);
1681 fstrcpy(driver->dependentfiles[i], new_name);
1686 architecture = get_short_archi(driver->environment);
1687 if (!architecture) {
1688 return WERR_UNKNOWN_PRINTER_DRIVER;
1691 /* jfm:7/16/2000 the client always sends the cversion=0.
1692 * The server should check which version the driver is by reading
1693 * the PE header of driver->driverpath.
1695 * For Windows 95/98 the version is 0 (so the value sent is correct)
1696 * For Windows NT (the architecture doesn't matter)
1697 * NT 3.1: cversion=0
1698 * NT 3.5/3.51: cversion=1
1702 if ((driver->cversion = get_correct_cversion(rpc_pipe, architecture,
1710 /****************************************************************************
1711 ****************************************************************************/
1712 static WERROR clean_up_driver_struct_level_6(struct pipes_struct *rpc_pipe,
1713 NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver)
1715 const char *architecture;
1721 /* clean up the driver name.
1722 * we can get .\driver.dll
1723 * or worse c:\windows\system\driver.dll !
1725 /* using an intermediate string to not have overlaping memcpy()'s */
1726 if ((p = strrchr(driver->driverpath,'\\')) != NULL) {
1727 fstrcpy(new_name, p+1);
1728 fstrcpy(driver->driverpath, new_name);
1731 if ((p = strrchr(driver->datafile,'\\')) != NULL) {
1732 fstrcpy(new_name, p+1);
1733 fstrcpy(driver->datafile, new_name);
1736 if ((p = strrchr(driver->configfile,'\\')) != NULL) {
1737 fstrcpy(new_name, p+1);
1738 fstrcpy(driver->configfile, new_name);
1741 if ((p = strrchr(driver->helpfile,'\\')) != NULL) {
1742 fstrcpy(new_name, p+1);
1743 fstrcpy(driver->helpfile, new_name);
1746 if (driver->dependentfiles) {
1747 for (i=0; *driver->dependentfiles[i]; i++) {
1748 if ((p = strrchr(driver->dependentfiles[i],'\\')) != NULL) {
1749 fstrcpy(new_name, p+1);
1750 fstrcpy(driver->dependentfiles[i], new_name);
1755 architecture = get_short_archi(driver->environment);
1756 if (!architecture) {
1757 return WERR_UNKNOWN_PRINTER_DRIVER;
1760 /* jfm:7/16/2000 the client always sends the cversion=0.
1761 * The server should check which version the driver is by reading
1762 * the PE header of driver->driverpath.
1764 * For Windows 95/98 the version is 0 (so the value sent is correct)
1765 * For Windows NT (the architecture doesn't matter)
1766 * NT 3.1: cversion=0
1767 * NT 3.5/3.51: cversion=1
1772 if ((driver->version = get_correct_cversion(rpc_pipe, architecture,
1780 /****************************************************************************
1781 ****************************************************************************/
1782 WERROR clean_up_driver_struct(struct pipes_struct *rpc_pipe,
1783 NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract,
1789 NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver;
1790 driver=driver_abstract.info_3;
1791 return clean_up_driver_struct_level_3(rpc_pipe,
1796 NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver;
1797 driver=driver_abstract.info_6;
1798 return clean_up_driver_struct_level_6(rpc_pipe,
1802 return WERR_INVALID_PARAM;
1806 /****************************************************************************
1807 This function sucks and should be replaced. JRA.
1808 ****************************************************************************/
1810 static void convert_level_6_to_level3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *dst, NT_PRINTER_DRIVER_INFO_LEVEL_6 *src)
1812 dst->cversion = src->version;
1814 fstrcpy( dst->name, src->name);
1815 fstrcpy( dst->environment, src->environment);
1816 fstrcpy( dst->driverpath, src->driverpath);
1817 fstrcpy( dst->datafile, src->datafile);
1818 fstrcpy( dst->configfile, src->configfile);
1819 fstrcpy( dst->helpfile, src->helpfile);
1820 fstrcpy( dst->monitorname, src->monitorname);
1821 fstrcpy( dst->defaultdatatype, src->defaultdatatype);
1822 dst->dependentfiles = src->dependentfiles;
1825 #if 0 /* Debugging function */
1827 static char* ffmt(unsigned char *c){
1829 static char ffmt_str[17];
1831 for (i=0; i<16; i++) {
1832 if ((c[i] < ' ') || (c[i] > '~'))
1843 /****************************************************************************
1844 ****************************************************************************/
1845 WERROR move_driver_to_download_area(struct pipes_struct *p,
1846 NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract,
1847 uint32 level, WERROR *perr)
1849 NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver;
1850 NT_PRINTER_DRIVER_INFO_LEVEL_3 converted_driver;
1851 const char *architecture;
1852 char *new_dir = NULL;
1853 char *old_name = NULL;
1854 char *new_name = NULL;
1855 connection_struct *conn = NULL;
1859 TALLOC_CTX *ctx = talloc_tos();
1862 fstring printdollar;
1863 int printdollar_snum;
1868 driver=driver_abstract.info_3;
1869 else if (level==6) {
1870 convert_level_6_to_level3(&converted_driver, driver_abstract.info_6);
1871 driver = &converted_driver;
1873 DEBUG(0,("move_driver_to_download_area: Unknown info level (%u)\n", (unsigned int)level ));
1874 return WERR_UNKNOWN_LEVEL;
1877 architecture = get_short_archi(driver->environment);
1878 if (!architecture) {
1879 return WERR_UNKNOWN_PRINTER_DRIVER;
1882 fstrcpy(printdollar, "print$");
1884 printdollar_snum = find_service(printdollar);
1885 if (printdollar_snum == -1) {
1886 *perr = WERR_NO_SUCH_SHARE;
1887 return WERR_NO_SUCH_SHARE;
1890 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
1891 lp_pathname(printdollar_snum),
1892 p->server_info, &oldcwd);
1893 if (!NT_STATUS_IS_OK(nt_status)) {
1894 DEBUG(0,("move_driver_to_download_area: create_conn_struct "
1895 "returned %s\n", nt_errstr(nt_status)));
1896 *perr = ntstatus_to_werror(nt_status);
1900 DEBUG(5,("Creating first directory\n"));
1901 new_dir = talloc_asprintf(ctx,
1909 new_dir = driver_unix_convert(conn,new_dir,&st);
1915 create_directory(conn, NULL, new_dir);
1917 /* For each driver file, archi\filexxx.yyy, if there is a duplicate file
1918 * listed for this driver which has already been moved, skip it (note:
1919 * drivers may list the same file name several times. Then check if the
1920 * file already exists in archi\cversion\, if so, check that the version
1921 * info (or time stamps if version info is unavailable) is newer (or the
1922 * date is later). If it is, move it to archi\cversion\filexxx.yyy.
1923 * Otherwise, delete the file.
1925 * If a file is not moved to archi\cversion\ because of an error, all the
1926 * rest of the 'unmoved' driver files are removed from archi\. If one or
1927 * more of the driver's files was already moved to archi\cversion\, it
1928 * potentially leaves the driver in a partially updated state. Version
1929 * trauma will most likely occur if an client attempts to use any printer
1930 * bound to the driver. Perhaps a rewrite to make sure the moves can be
1931 * done is appropriate... later JRR
1934 DEBUG(5,("Moving files now !\n"));
1936 if (driver->driverpath && strlen(driver->driverpath)) {
1937 new_name = talloc_asprintf(ctx,
1940 driver->driverpath);
1945 old_name = talloc_asprintf(ctx,
1948 driver->driverpath);
1954 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
1955 new_name = driver_unix_convert(conn,new_name,&st);
1960 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name, OPENX_FILE_EXISTS_TRUNCATE|
1961 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
1962 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
1963 new_name, old_name));
1964 *perr = WERR_ACCESS_DENIED;
1970 if (driver->datafile && strlen(driver->datafile)) {
1971 if (!strequal(driver->datafile, driver->driverpath)) {
1972 new_name = talloc_asprintf(ctx,
1980 old_name = talloc_asprintf(ctx,
1988 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
1989 new_name = driver_unix_convert(conn,new_name,&st);
1994 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name, OPENX_FILE_EXISTS_TRUNCATE|
1995 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
1996 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
1997 new_name, old_name));
1998 *perr = WERR_ACCESS_DENIED;
2005 if (driver->configfile && strlen(driver->configfile)) {
2006 if (!strequal(driver->configfile, driver->driverpath) &&
2007 !strequal(driver->configfile, driver->datafile)) {
2008 new_name = talloc_asprintf(ctx,
2011 driver->configfile);
2016 old_name = talloc_asprintf(ctx,
2019 driver->configfile);
2024 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
2025 new_name = driver_unix_convert(conn,new_name,&st);
2030 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name, OPENX_FILE_EXISTS_TRUNCATE|
2031 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
2032 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
2033 new_name, old_name));
2034 *perr = WERR_ACCESS_DENIED;
2041 if (driver->helpfile && strlen(driver->helpfile)) {
2042 if (!strequal(driver->helpfile, driver->driverpath) &&
2043 !strequal(driver->helpfile, driver->datafile) &&
2044 !strequal(driver->helpfile, driver->configfile)) {
2045 new_name = talloc_asprintf(ctx,
2053 old_name = talloc_asprintf(ctx,
2061 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
2062 new_name = driver_unix_convert(conn,new_name,&st);
2067 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name, OPENX_FILE_EXISTS_TRUNCATE|
2068 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
2069 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
2070 new_name, old_name));
2071 *perr = WERR_ACCESS_DENIED;
2078 if (driver->dependentfiles) {
2079 for (i=0; *driver->dependentfiles[i]; i++) {
2080 if (!strequal(driver->dependentfiles[i], driver->driverpath) &&
2081 !strequal(driver->dependentfiles[i], driver->datafile) &&
2082 !strequal(driver->dependentfiles[i], driver->configfile) &&
2083 !strequal(driver->dependentfiles[i], driver->helpfile)) {
2085 for (j=0; j < i; j++) {
2086 if (strequal(driver->dependentfiles[i], driver->dependentfiles[j])) {
2091 new_name = talloc_asprintf(ctx,
2094 driver->dependentfiles[i]);
2099 old_name = talloc_asprintf(ctx,
2102 driver->dependentfiles[i]);
2107 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
2108 new_name = driver_unix_convert(conn,new_name,&st);
2113 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name,
2114 OPENX_FILE_EXISTS_TRUNCATE|
2115 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
2116 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
2117 new_name, old_name));
2118 *perr = WERR_ACCESS_DENIED;
2130 vfs_ChDir(conn, oldcwd);
2131 conn_free_internal(conn);
2134 if (W_ERROR_EQUAL(*perr, WERR_OK)) {
2138 return WERR_UNKNOWN_PRINTER_DRIVER;
2143 /****************************************************************************
2144 ****************************************************************************/
2146 static uint32 add_a_printer_driver_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver)
2148 TALLOC_CTX *ctx = talloc_tos();
2150 const char *architecture;
2151 char *directory = NULL;
2158 architecture = get_short_archi(driver->environment);
2159 if (!architecture) {
2163 /* The names are relative. We store them in the form: \print$\arch\version\driver.xxx
2164 * \\server is added in the rpc server layer.
2165 * It does make sense to NOT store the server's name in the printer TDB.
2168 directory = talloc_asprintf(ctx, "\\print$\\%s\\%d\\",
2169 architecture, driver->cversion);
2174 /* .inf files do not always list a file for each of the four standard files.
2175 * Don't prepend a path to a null filename, or client claims:
2176 * "The server on which the printer resides does not have a suitable
2177 * <printer driver name> printer driver installed. Click OK if you
2178 * wish to install the driver on your local machine."
2180 if (strlen(driver->driverpath)) {
2181 fstrcpy(temp_name, driver->driverpath);
2182 slprintf(driver->driverpath, sizeof(driver->driverpath)-1, "%s%s", directory, temp_name);
2185 if (strlen(driver->datafile)) {
2186 fstrcpy(temp_name, driver->datafile);
2187 slprintf(driver->datafile, sizeof(driver->datafile)-1, "%s%s", directory, temp_name);
2190 if (strlen(driver->configfile)) {
2191 fstrcpy(temp_name, driver->configfile);
2192 slprintf(driver->configfile, sizeof(driver->configfile)-1, "%s%s", directory, temp_name);
2195 if (strlen(driver->helpfile)) {
2196 fstrcpy(temp_name, driver->helpfile);
2197 slprintf(driver->helpfile, sizeof(driver->helpfile)-1, "%s%s", directory, temp_name);
2200 if (driver->dependentfiles) {
2201 for (i=0; *driver->dependentfiles[i]; i++) {
2202 fstrcpy(temp_name, driver->dependentfiles[i]);
2203 slprintf(driver->dependentfiles[i], sizeof(driver->dependentfiles[i])-1, "%s%s", directory, temp_name);
2207 key = talloc_asprintf(ctx, "%s%s/%d/%s", DRIVERS_PREFIX,
2208 architecture, driver->cversion, driver->name);
2213 DEBUG(5,("add_a_printer_driver_3: Adding driver with key %s\n", key ));
2220 len += tdb_pack(buf+len, buflen-len, "dffffffff",
2223 driver->environment,
2228 driver->monitorname,
2229 driver->defaultdatatype);
2231 if (driver->dependentfiles) {
2232 for (i=0; *driver->dependentfiles[i]; i++) {
2233 len += tdb_pack(buf+len, buflen-len, "f",
2234 driver->dependentfiles[i]);
2238 if (len != buflen) {
2239 buf = (uint8 *)SMB_REALLOC(buf, len);
2241 DEBUG(0,("add_a_printer_driver_3: failed to enlarge buffer\n!"));
2252 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
2256 DEBUG(0,("add_a_printer_driver_3: Adding driver with key %s failed.\n", key ));
2262 /****************************************************************************
2263 ****************************************************************************/
2264 static uint32 add_a_printer_driver_6(NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver)
2266 NT_PRINTER_DRIVER_INFO_LEVEL_3 info3;
2269 info3.cversion = driver->version;
2270 fstrcpy(info3.name,driver->name);
2271 fstrcpy(info3.environment,driver->environment);
2272 fstrcpy(info3.driverpath,driver->driverpath);
2273 fstrcpy(info3.datafile,driver->datafile);
2274 fstrcpy(info3.configfile,driver->configfile);
2275 fstrcpy(info3.helpfile,driver->helpfile);
2276 fstrcpy(info3.monitorname,driver->monitorname);
2277 fstrcpy(info3.defaultdatatype,driver->defaultdatatype);
2278 info3.dependentfiles = driver->dependentfiles;
2280 return add_a_printer_driver_3(&info3);
2284 /****************************************************************************
2285 ****************************************************************************/
2286 static WERROR get_a_printer_driver_3_default(NT_PRINTER_DRIVER_INFO_LEVEL_3 **info_ptr, const char *driver, const char *arch)
2288 NT_PRINTER_DRIVER_INFO_LEVEL_3 info;
2292 fstrcpy(info.name, driver);
2293 fstrcpy(info.defaultdatatype, "RAW");
2295 fstrcpy(info.driverpath, "");
2296 fstrcpy(info.datafile, "");
2297 fstrcpy(info.configfile, "");
2298 fstrcpy(info.helpfile, "");
2300 if ((info.dependentfiles= SMB_MALLOC_ARRAY(fstring, 2)) == NULL)
2303 memset(info.dependentfiles, '\0', 2*sizeof(fstring));
2304 fstrcpy(info.dependentfiles[0], "");
2306 *info_ptr = (NT_PRINTER_DRIVER_INFO_LEVEL_3 *)memdup(&info, sizeof(info));
2308 SAFE_FREE(info.dependentfiles);
2315 /****************************************************************************
2316 ****************************************************************************/
2317 static WERROR get_a_printer_driver_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 **info_ptr, fstring drivername, const char *arch, uint32 version)
2319 NT_PRINTER_DRIVER_INFO_LEVEL_3 driver;
2321 const char *architecture;
2326 ZERO_STRUCT(driver);
2328 architecture = get_short_archi(arch);
2329 if ( !architecture ) {
2330 return WERR_UNKNOWN_PRINTER_DRIVER;
2333 /* Windows 4.0 (i.e. win9x) should always use a version of 0 */
2335 if ( strcmp( architecture, SPL_ARCH_WIN40 ) == 0 )
2338 DEBUG(8,("get_a_printer_driver_3: [%s%s/%d/%s]\n", DRIVERS_PREFIX, architecture, version, drivername));
2340 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
2341 architecture, version, drivername) < 0) {
2345 dbuf = tdb_fetch_bystring(tdb_drivers, key);
2348 return WERR_UNKNOWN_PRINTER_DRIVER;
2351 len += tdb_unpack(dbuf.dptr, dbuf.dsize, "dffffffff",
2360 driver.defaultdatatype);
2363 while (len < dbuf.dsize) {
2364 driver.dependentfiles = SMB_REALLOC_ARRAY(driver.dependentfiles, fstring, i+2);
2365 if ( !driver.dependentfiles ) {
2366 DEBUG(0,("get_a_printer_driver_3: failed to enlarge buffer!\n"));
2370 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "f",
2371 &driver.dependentfiles[i]);
2375 if ( driver.dependentfiles )
2376 fstrcpy( driver.dependentfiles[i], "" );
2378 SAFE_FREE(dbuf.dptr);
2381 if (len != dbuf.dsize) {
2382 SAFE_FREE(driver.dependentfiles);
2384 return get_a_printer_driver_3_default(info_ptr, drivername, arch);
2387 *info_ptr = (NT_PRINTER_DRIVER_INFO_LEVEL_3 *)memdup(&driver, sizeof(driver));
2389 SAFE_FREE(driver.dependentfiles);
2396 /****************************************************************************
2397 Debugging function, dump at level 6 the struct in the logs.
2398 ****************************************************************************/
2400 static uint32 dump_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
2403 NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3;
2406 DEBUG(20,("Dumping printer driver at level [%d]\n", level));
2412 if (driver.info_3 == NULL)
2415 info3=driver.info_3;
2417 DEBUGADD(20,("version:[%d]\n", info3->cversion));
2418 DEBUGADD(20,("name:[%s]\n", info3->name));
2419 DEBUGADD(20,("environment:[%s]\n", info3->environment));
2420 DEBUGADD(20,("driverpath:[%s]\n", info3->driverpath));
2421 DEBUGADD(20,("datafile:[%s]\n", info3->datafile));
2422 DEBUGADD(20,("configfile:[%s]\n", info3->configfile));
2423 DEBUGADD(20,("helpfile:[%s]\n", info3->helpfile));
2424 DEBUGADD(20,("monitorname:[%s]\n", info3->monitorname));
2425 DEBUGADD(20,("defaultdatatype:[%s]\n", info3->defaultdatatype));
2427 for (i=0; info3->dependentfiles &&
2428 *info3->dependentfiles[i]; i++) {
2429 DEBUGADD(20,("dependentfile:[%s]\n",
2430 info3->dependentfiles[i]));
2437 DEBUGADD(20,("dump_a_printer_driver: Level %u not implemented\n", (unsigned int)level));
2445 /****************************************************************************
2446 ****************************************************************************/
2447 int pack_devicemode(NT_DEVICEMODE *nt_devmode, uint8 *buf, int buflen)
2451 len += tdb_pack(buf+len, buflen-len, "p", nt_devmode);
2456 len += tdb_pack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2457 nt_devmode->devicename,
2458 nt_devmode->formname,
2460 nt_devmode->specversion,
2461 nt_devmode->driverversion,
2463 nt_devmode->driverextra,
2464 nt_devmode->orientation,
2465 nt_devmode->papersize,
2466 nt_devmode->paperlength,
2467 nt_devmode->paperwidth,
2470 nt_devmode->defaultsource,
2471 nt_devmode->printquality,
2474 nt_devmode->yresolution,
2475 nt_devmode->ttoption,
2476 nt_devmode->collate,
2477 nt_devmode->logpixels,
2480 nt_devmode->bitsperpel,
2481 nt_devmode->pelswidth,
2482 nt_devmode->pelsheight,
2483 nt_devmode->displayflags,
2484 nt_devmode->displayfrequency,
2485 nt_devmode->icmmethod,
2486 nt_devmode->icmintent,
2487 nt_devmode->mediatype,
2488 nt_devmode->dithertype,
2489 nt_devmode->reserved1,
2490 nt_devmode->reserved2,
2491 nt_devmode->panningwidth,
2492 nt_devmode->panningheight,
2493 nt_devmode->nt_dev_private);
2495 if (nt_devmode->nt_dev_private) {
2496 len += tdb_pack(buf+len, buflen-len, "B",
2497 nt_devmode->driverextra,
2498 nt_devmode->nt_dev_private);
2501 DEBUG(8,("Packed devicemode [%s]\n", nt_devmode->formname));
2506 /****************************************************************************
2507 Pack all values in all printer keys
2508 ***************************************************************************/
2510 static int pack_values(NT_PRINTER_DATA *data, uint8 *buf, int buflen)
2514 REGISTRY_VALUE *val;
2515 REGVAL_CTR *val_ctr;
2522 /* loop over all keys */
2524 for ( i=0; i<data->num_keys; i++ ) {
2525 val_ctr = data->keys[i].values;
2526 num_values = regval_ctr_numvals( val_ctr );
2528 /* pack the keyname followed by a empty value */
2530 len += tdb_pack(buf+len, buflen-len, "pPdB",
2531 &data->keys[i].name,
2537 /* now loop over all values */
2539 for ( j=0; j<num_values; j++ ) {
2540 /* pathname should be stored as <key>\<value> */
2542 val = regval_ctr_specific_value( val_ctr, j );
2543 if (asprintf(&path, "%s\\%s",
2545 regval_name(val)) < 0) {
2549 len += tdb_pack(buf+len, buflen-len, "pPdB",
2554 regval_data_p(val) );
2556 DEBUG(8,("specific: [%s], len: %d\n", regval_name(val), regval_size(val)));
2564 len += tdb_pack(buf+len, buflen-len, "p", NULL);
2570 /****************************************************************************
2571 Delete a printer - this just deletes the printer info file, any open
2572 handles are not affected.
2573 ****************************************************************************/
2575 uint32 del_a_printer(const char *sharename)
2578 char *printdb_path = NULL;
2579 TALLOC_CTX *ctx = talloc_tos();
2581 kbuf = make_printer_tdbkey(ctx, sharename);
2582 tdb_delete(tdb_printers, kbuf);
2584 kbuf= make_printers_secdesc_tdbkey(ctx, sharename);
2585 tdb_delete(tdb_printers, kbuf);
2587 close_all_print_db();
2589 if (geteuid() == 0) {
2590 if (asprintf(&printdb_path, "%s%s.tdb",
2591 cache_path("printing/"),
2595 unlink(printdb_path);
2596 SAFE_FREE(printdb_path);
2602 /****************************************************************************
2603 ****************************************************************************/
2604 static WERROR update_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info)
2610 TDB_DATA kbuf, dbuf;
2613 * in addprinter: no servername and the printer is the name
2614 * in setprinter: servername is \\server
2615 * and printer is \\server\\printer
2617 * Samba manages only local printers.
2618 * we currently don't support things like i
2619 * path=\\other_server\printer
2621 * We only store the printername, not \\server\printername
2624 if ( info->servername[0] != '\0' ) {
2625 trim_string(info->printername, info->servername, NULL);
2626 trim_char(info->printername, '\\', '\0');
2627 info->servername[0]='\0';
2631 * JFM: one day I'll forget.
2632 * below that's info->portname because that's the SAMBA sharename
2633 * and I made NT 'thinks' it's the portname
2634 * the info->sharename is the thing you can name when you add a printer
2635 * that's the short-name when you create shared printer for 95/98
2636 * So I've made a limitation in SAMBA: you can only have 1 printer model
2637 * behind a SAMBA share.
2645 len += tdb_pack(buf+len, buflen-len, "dddddddddddfffffPfffff",
2648 info->default_priority,
2665 info->printprocessor,
2669 len += pack_devicemode(info->devmode, buf+len, buflen-len);
2670 retlen = pack_values( info->data, buf+len, buflen-len );
2677 if (buflen != len) {
2678 buf = (uint8 *)SMB_REALLOC(buf, len);
2680 DEBUG(0,("update_a_printer_2: failed to enlarge buffer!\n"));
2688 kbuf = make_printer_tdbkey(talloc_tos(), info->sharename );
2693 ret = (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) == 0? WERR_OK : WERR_NOMEM);
2696 if (!W_ERROR_IS_OK(ret))
2697 DEBUG(8, ("error updating printer to tdb on disk\n"));
2701 DEBUG(8,("packed printer [%s] with driver [%s] portname=[%s] len=%d\n",
2702 info->sharename, info->drivername, info->portname, len));
2708 /****************************************************************************
2709 Malloc and return an NT devicemode.
2710 ****************************************************************************/
2712 NT_DEVICEMODE *construct_nt_devicemode(const fstring default_devicename)
2715 char adevice[MAXDEVICENAME];
2716 NT_DEVICEMODE *nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE);
2718 if (nt_devmode == NULL) {
2719 DEBUG(0,("construct_nt_devicemode: malloc fail.\n"));
2723 ZERO_STRUCTP(nt_devmode);
2725 slprintf(adevice, sizeof(adevice), "%s", default_devicename);
2726 fstrcpy(nt_devmode->devicename, adevice);
2728 fstrcpy(nt_devmode->formname, "Letter");
2730 nt_devmode->specversion = 0x0401;
2731 nt_devmode->driverversion = 0x0400;
2732 nt_devmode->size = 0x00DC;
2733 nt_devmode->driverextra = 0x0000;
2734 nt_devmode->fields = FORMNAME | TTOPTION | PRINTQUALITY |
2735 DEFAULTSOURCE | COPIES | SCALE |
2736 PAPERSIZE | ORIENTATION;
2737 nt_devmode->orientation = 1;
2738 nt_devmode->papersize = PAPER_LETTER;
2739 nt_devmode->paperlength = 0;
2740 nt_devmode->paperwidth = 0;
2741 nt_devmode->scale = 0x64;
2742 nt_devmode->copies = 1;
2743 nt_devmode->defaultsource = BIN_FORMSOURCE;
2744 nt_devmode->printquality = RES_HIGH; /* 0x0258 */
2745 nt_devmode->color = COLOR_MONOCHROME;
2746 nt_devmode->duplex = DUP_SIMPLEX;
2747 nt_devmode->yresolution = 0;
2748 nt_devmode->ttoption = TT_SUBDEV;
2749 nt_devmode->collate = COLLATE_FALSE;
2750 nt_devmode->icmmethod = 0;
2751 nt_devmode->icmintent = 0;
2752 nt_devmode->mediatype = 0;
2753 nt_devmode->dithertype = 0;
2755 /* non utilisés par un driver d'imprimante */
2756 nt_devmode->logpixels = 0;
2757 nt_devmode->bitsperpel = 0;
2758 nt_devmode->pelswidth = 0;
2759 nt_devmode->pelsheight = 0;
2760 nt_devmode->displayflags = 0;
2761 nt_devmode->displayfrequency = 0;
2762 nt_devmode->reserved1 = 0;
2763 nt_devmode->reserved2 = 0;
2764 nt_devmode->panningwidth = 0;
2765 nt_devmode->panningheight = 0;
2767 nt_devmode->nt_dev_private = NULL;
2771 /****************************************************************************
2772 Deepcopy an NT devicemode.
2773 ****************************************************************************/
2775 NT_DEVICEMODE *dup_nt_devicemode(NT_DEVICEMODE *nt_devicemode)
2777 NT_DEVICEMODE *new_nt_devicemode = NULL;
2779 if ( !nt_devicemode )
2782 if ((new_nt_devicemode = (NT_DEVICEMODE *)memdup(nt_devicemode, sizeof(NT_DEVICEMODE))) == NULL) {
2783 DEBUG(0,("dup_nt_devicemode: malloc fail.\n"));
2787 new_nt_devicemode->nt_dev_private = NULL;
2788 if (nt_devicemode->nt_dev_private != NULL) {
2789 if ((new_nt_devicemode->nt_dev_private = (uint8 *)memdup(nt_devicemode->nt_dev_private, nt_devicemode->driverextra)) == NULL) {
2790 SAFE_FREE(new_nt_devicemode);
2791 DEBUG(0,("dup_nt_devicemode: malloc fail.\n"));
2796 return new_nt_devicemode;
2799 /****************************************************************************
2800 Clean up and deallocate a (maybe partially) allocated NT_DEVICEMODE.
2801 ****************************************************************************/
2803 void free_nt_devicemode(NT_DEVICEMODE **devmode_ptr)
2805 NT_DEVICEMODE *nt_devmode = *devmode_ptr;
2807 if(nt_devmode == NULL)
2810 DEBUG(106,("free_nt_devicemode: deleting DEVMODE\n"));
2812 SAFE_FREE(nt_devmode->nt_dev_private);
2813 SAFE_FREE(*devmode_ptr);
2816 /****************************************************************************
2817 Clean up and deallocate a (maybe partially) allocated NT_PRINTER_INFO_LEVEL_2.
2818 ****************************************************************************/
2820 static void free_nt_printer_info_level_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr)
2822 NT_PRINTER_INFO_LEVEL_2 *info = *info_ptr;
2827 free_nt_devicemode(&info->devmode);
2829 TALLOC_FREE( *info_ptr );
2833 /****************************************************************************
2834 ****************************************************************************/
2835 int unpack_devicemode(NT_DEVICEMODE **nt_devmode, const uint8 *buf, int buflen)
2839 NT_DEVICEMODE devmode;
2841 ZERO_STRUCT(devmode);
2843 len += tdb_unpack(buf+len, buflen-len, "p", nt_devmode);
2845 if (!*nt_devmode) return len;
2847 len += tdb_unpack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2851 &devmode.specversion,
2852 &devmode.driverversion,
2854 &devmode.driverextra,
2855 &devmode.orientation,
2857 &devmode.paperlength,
2858 &devmode.paperwidth,
2861 &devmode.defaultsource,
2862 &devmode.printquality,
2865 &devmode.yresolution,
2871 &devmode.bitsperpel,
2873 &devmode.pelsheight,
2874 &devmode.displayflags,
2875 &devmode.displayfrequency,
2879 &devmode.dithertype,
2882 &devmode.panningwidth,
2883 &devmode.panningheight,
2884 &devmode.nt_dev_private);
2886 if (devmode.nt_dev_private) {
2887 /* the len in tdb_unpack is an int value and
2888 * devmode.driverextra is only a short
2890 len += tdb_unpack(buf+len, buflen-len, "B", &extra_len, &devmode.nt_dev_private);
2891 devmode.driverextra=(uint16)extra_len;
2893 /* check to catch an invalid TDB entry so we don't segfault */
2894 if (devmode.driverextra == 0) {
2895 devmode.nt_dev_private = NULL;
2899 *nt_devmode = (NT_DEVICEMODE *)memdup(&devmode, sizeof(devmode));
2901 SAFE_FREE(devmode.nt_dev_private);
2905 DEBUG(8,("Unpacked devicemode [%s](%s)\n", devmode.devicename, devmode.formname));
2906 if (devmode.nt_dev_private)
2907 DEBUG(8,("with a private section of %d bytes\n", devmode.driverextra));
2912 /****************************************************************************
2913 Allocate and initialize a new slot.
2914 ***************************************************************************/
2916 int add_new_printer_key( NT_PRINTER_DATA *data, const char *name )
2921 if ( !name || !data )
2924 /* allocate another slot in the NT_PRINTER_KEY array */
2926 if ( !(d = TALLOC_REALLOC_ARRAY( data, data->keys, NT_PRINTER_KEY, data->num_keys+1)) ) {
2927 DEBUG(0,("add_new_printer_key: Realloc() failed!\n"));
2933 key_index = data->num_keys;
2935 /* initialze new key */
2937 data->keys[key_index].name = talloc_strdup( data, name );
2939 if ( !(data->keys[key_index].values = TALLOC_ZERO_P( data, REGVAL_CTR )) )
2944 DEBUG(10,("add_new_printer_key: Inserted new data key [%s]\n", name ));
2949 /****************************************************************************
2950 search for a registry key name in the existing printer data
2951 ***************************************************************************/
2953 int delete_printer_key( NT_PRINTER_DATA *data, const char *name )
2957 for ( i=0; i<data->num_keys; i++ ) {
2958 if ( strequal( data->keys[i].name, name ) ) {
2960 /* cleanup memory */
2962 TALLOC_FREE( data->keys[i].name );
2963 TALLOC_FREE( data->keys[i].values );
2965 /* if not the end of the array, move remaining elements down one slot */
2968 if ( data->num_keys && (i < data->num_keys) )
2969 memmove( &data->keys[i], &data->keys[i+1], sizeof(NT_PRINTER_KEY)*(data->num_keys-i) );
2976 return data->num_keys;
2979 /****************************************************************************
2980 search for a registry key name in the existing printer data
2981 ***************************************************************************/
2983 int lookup_printerkey( NT_PRINTER_DATA *data, const char *name )
2988 if ( !data || !name )
2991 DEBUG(12,("lookup_printerkey: Looking for [%s]\n", name));
2993 /* loop over all existing keys */
2995 for ( i=0; i<data->num_keys; i++ ) {
2996 if ( strequal(data->keys[i].name, name) ) {
2997 DEBUG(12,("lookup_printerkey: Found [%s]!\n", name));
3007 /****************************************************************************
3008 ***************************************************************************/
3010 int get_printer_subkeys( NT_PRINTER_DATA *data, const char* key, fstring **subkeys )
3014 int num_subkeys = 0;
3016 fstring *subkeys_ptr = NULL;
3027 /* special case of asking for the top level printer data registry key names */
3029 if ( strlen(key) == 0 ) {
3030 for ( i=0; i<data->num_keys; i++ ) {
3032 /* found a match, so allocate space and copy the name */
3034 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
3035 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
3040 fstrcpy( subkeys_ptr[num_subkeys], data->keys[i].name );
3047 /* asking for the subkeys of some key */
3048 /* subkey paths are stored in the key name using '\' as the delimiter */
3050 for ( i=0; i<data->num_keys; i++ ) {
3051 if ( StrnCaseCmp(data->keys[i].name, key, strlen(key)) == 0 ) {
3053 /* if we found the exact key, then break */
3054 key_len = strlen( key );
3055 if ( strlen(data->keys[i].name) == key_len )
3058 /* get subkey path */
3060 p = data->keys[i].name + key_len;
3063 fstrcpy( subkeyname, p );
3064 if ( (p = strchr( subkeyname, '\\' )) )
3067 /* don't add a key more than once */
3069 for ( j=0; j<num_subkeys; j++ ) {
3070 if ( strequal( subkeys_ptr[j], subkeyname ) )
3074 if ( j != num_subkeys )
3077 /* found a match, so allocate space and copy the name */
3079 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
3080 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
3085 fstrcpy( subkeys_ptr[num_subkeys], subkeyname );
3091 /* return error if the key was not found */
3093 if ( i == data->num_keys ) {
3094 SAFE_FREE(subkeys_ptr);
3099 /* tag off the end */
3102 fstrcpy(subkeys_ptr[num_subkeys], "" );
3104 *subkeys = subkeys_ptr;
3110 static void map_sz_into_ctr(REGVAL_CTR *ctr, const char *val_name,
3113 smb_ucs2_t conv_str[1024];
3116 regval_ctr_delvalue(ctr, val_name);
3117 str_size = push_ucs2(NULL, conv_str, sz, sizeof(conv_str),
3118 STR_TERMINATE | STR_NOALIGN);
3119 regval_ctr_addvalue(ctr, val_name, REG_SZ,
3120 (char *) conv_str, str_size);
3123 static void map_dword_into_ctr(REGVAL_CTR *ctr, const char *val_name,
3126 regval_ctr_delvalue(ctr, val_name);
3127 regval_ctr_addvalue(ctr, val_name, REG_DWORD,
3128 (char *) &dword, sizeof(dword));
3131 static void map_bool_into_ctr(REGVAL_CTR *ctr, const char *val_name,
3134 uint8 bin_bool = (b ? 1 : 0);
3135 regval_ctr_delvalue(ctr, val_name);
3136 regval_ctr_addvalue(ctr, val_name, REG_BINARY,
3137 (char *) &bin_bool, sizeof(bin_bool));
3140 static void map_single_multi_sz_into_ctr(REGVAL_CTR *ctr, const char *val_name,
3141 const char *multi_sz)
3143 smb_ucs2_t *conv_strs = NULL;
3146 /* a multi-sz has to have a null string terminator, i.e., the last
3147 string must be followed by two nulls */
3148 str_size = strlen(multi_sz) + 2;
3149 conv_strs = SMB_CALLOC_ARRAY(smb_ucs2_t, str_size);
3154 /* Change to byte units. */
3155 str_size *= sizeof(smb_ucs2_t);
3156 push_ucs2(NULL, conv_strs, multi_sz, str_size,
3157 STR_TERMINATE | STR_NOALIGN);
3159 regval_ctr_delvalue(ctr, val_name);
3160 regval_ctr_addvalue(ctr, val_name, REG_MULTI_SZ,
3161 (char *) conv_strs, str_size);
3162 SAFE_FREE(conv_strs);
3165 /****************************************************************************
3166 * Map the NT_PRINTER_INFO_LEVEL_2 data into DsSpooler keys for publishing.
3168 * @param info2 NT_PRINTER_INFO_LEVEL_2 describing printer - gets modified
3169 * @return bool indicating success or failure
3170 ***************************************************************************/
3172 static bool map_nt_printer_info2_to_dsspooler(NT_PRINTER_INFO_LEVEL_2 *info2)
3174 REGVAL_CTR *ctr = NULL;
3176 const char *dnssuffix;
3177 char *allocated_string = NULL;
3178 const char *ascii_str;
3181 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3182 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3183 ctr = info2->data->keys[i].values;
3185 map_sz_into_ctr(ctr, SPOOL_REG_PRINTERNAME, info2->sharename);
3186 map_sz_into_ctr(ctr, SPOOL_REG_SHORTSERVERNAME, global_myname());
3188 /* we make the assumption that the netbios name is the same
3189 as the DNS name sinc ethe former will be what we used to
3192 dnssuffix = get_mydnsdomname(talloc_tos());
3193 if (dnssuffix && *dnssuffix) {
3194 fstr_sprintf( longname, "%s.%s", global_myname(), dnssuffix );
3196 fstrcpy( longname, global_myname() );
3199 map_sz_into_ctr(ctr, SPOOL_REG_SERVERNAME, longname);
3201 if (asprintf(&allocated_string, "\\\\%s\\%s", longname, info2->sharename) == -1) {
3204 map_sz_into_ctr(ctr, SPOOL_REG_UNCNAME, allocated_string);
3205 SAFE_FREE(allocated_string);
3207 map_dword_into_ctr(ctr, SPOOL_REG_VERSIONNUMBER, 4);
3208 map_sz_into_ctr(ctr, SPOOL_REG_DRIVERNAME, info2->drivername);
3209 map_sz_into_ctr(ctr, SPOOL_REG_LOCATION, info2->location);
3210 map_sz_into_ctr(ctr, SPOOL_REG_DESCRIPTION, info2->comment);
3211 map_single_multi_sz_into_ctr(ctr, SPOOL_REG_PORTNAME, info2->portname);
3212 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSEPARATORFILE, info2->sepfile);
3213 map_dword_into_ctr(ctr, SPOOL_REG_PRINTSTARTTIME, info2->starttime);
3214 map_dword_into_ctr(ctr, SPOOL_REG_PRINTENDTIME, info2->untiltime);
3215 map_dword_into_ctr(ctr, SPOOL_REG_PRIORITY, info2->priority);
3217 map_bool_into_ctr(ctr, SPOOL_REG_PRINTKEEPPRINTEDJOBS,
3218 (info2->attributes &
3219 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
3221 switch (info2->attributes & 0x3) {
3223 ascii_str = SPOOL_REGVAL_PRINTWHILESPOOLING;
3226 ascii_str = SPOOL_REGVAL_PRINTAFTERSPOOLED;
3229 ascii_str = SPOOL_REGVAL_PRINTDIRECT;
3232 ascii_str = "unknown";
3234 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSPOOLING, ascii_str);
3239 /*****************************************************************
3240 ****************************************************************/
3242 static void store_printer_guid(NT_PRINTER_INFO_LEVEL_2 *info2,
3246 REGVAL_CTR *ctr=NULL;
3247 UNISTR2 unistr_guid;
3249 /* find the DsSpooler key */
3250 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3251 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3252 ctr = info2->data->keys[i].values;
3254 regval_ctr_delvalue(ctr, "objectGUID");
3256 /* We used to store this as a REG_BINARY but that causes
3259 ZERO_STRUCT( unistr_guid );
3261 init_unistr2( &unistr_guid, GUID_string(talloc_tos(), &guid),
3262 UNI_STR_TERMINATE );
3264 regval_ctr_addvalue(ctr, "objectGUID", REG_SZ,
3265 (char *)unistr_guid.buffer,
3266 unistr_guid.uni_max_len*2);
3270 static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
3271 NT_PRINTER_INFO_LEVEL *printer)
3275 char *prt_dn = NULL, *srv_dn, *srv_cn_0, *srv_cn_escaped, *sharename_escaped;
3276 char *srv_dn_utf8, **srv_cn_utf8;
3279 const char *attrs[] = {"objectGUID", NULL};
3281 WERROR win_rc = WERR_OK;
3282 size_t converted_size;
3285 DEBUG(5, ("publishing printer %s\n", printer->info_2->printername));
3287 /* figure out where to publish */
3288 ads_find_machine_acct(ads, &res, global_myname());
3290 /* We use ldap_get_dn here as we need the answer
3291 * in utf8 to call ldap_explode_dn(). JRA. */
3293 srv_dn_utf8 = ldap_get_dn((LDAP *)ads->ldap.ld, (LDAPMessage *)res);
3295 return WERR_SERVER_UNAVAILABLE;
3297 ads_msgfree(ads, res);
3298 srv_cn_utf8 = ldap_explode_dn(srv_dn_utf8, 1);
3300 ldap_memfree(srv_dn_utf8);
3301 return WERR_SERVER_UNAVAILABLE;
3303 /* Now convert to CH_UNIX. */
3304 if (!pull_utf8_allocate(&srv_dn, srv_dn_utf8, &converted_size)) {
3305 ldap_memfree(srv_dn_utf8);
3306 ldap_memfree(srv_cn_utf8);
3307 return WERR_SERVER_UNAVAILABLE;
3309 if (!pull_utf8_allocate(&srv_cn_0, srv_cn_utf8[0], &converted_size)) {
3310 ldap_memfree(srv_dn_utf8);
3311 ldap_memfree(srv_cn_utf8);
3313 return WERR_SERVER_UNAVAILABLE;
3316 ldap_memfree(srv_dn_utf8);
3317 ldap_memfree(srv_cn_utf8);
3319 srv_cn_escaped = escape_rdn_val_string_alloc(srv_cn_0);
3320 if (!srv_cn_escaped) {
3321 SAFE_FREE(srv_cn_0);
3323 return WERR_SERVER_UNAVAILABLE;
3325 sharename_escaped = escape_rdn_val_string_alloc(printer->info_2->sharename);
3326 if (!sharename_escaped) {
3327 SAFE_FREE(srv_cn_escaped);
3328 SAFE_FREE(srv_cn_0);
3330 return WERR_SERVER_UNAVAILABLE;
3333 ret = asprintf(&prt_dn, "cn=%s-%s,%s", srv_cn_escaped, sharename_escaped, srv_dn);
3336 SAFE_FREE(srv_cn_0);
3337 SAFE_FREE(srv_cn_escaped);
3338 SAFE_FREE(sharename_escaped);
3344 /* build the ads mods */
3345 ctx = talloc_init("nt_printer_publish_ads");
3351 mods = ads_init_mods(ctx);
3355 talloc_destroy(ctx);
3359 get_local_printer_publishing_data(ctx, &mods, printer->info_2->data);
3360 ads_mod_str(ctx, &mods, SPOOL_REG_PRINTERNAME,
3361 printer->info_2->sharename);
3364 ads_rc = ads_mod_printer_entry(ads, prt_dn, ctx, &mods);
3365 if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT) {
3367 for (i=0; mods[i] != 0; i++)
3369 mods[i] = (LDAPMod *)-1;
3370 ads_rc = ads_add_printer_entry(ads, prt_dn, ctx, &mods);
3373 if (!ADS_ERR_OK(ads_rc))
3374 DEBUG(3, ("error publishing %s: %s\n", printer->info_2->sharename, ads_errstr(ads_rc)));
3376 talloc_destroy(ctx);
3378 /* retreive the guid and store it locally */
3379 if (ADS_ERR_OK(ads_search_dn(ads, &res, prt_dn, attrs))) {
3381 ads_pull_guid(ads, res, &guid);
3382 ads_msgfree(ads, res);
3383 store_printer_guid(printer->info_2, guid);
3384 win_rc = mod_a_printer(printer, 2);
3391 static WERROR nt_printer_unpublish_ads(ADS_STRUCT *ads,
3392 NT_PRINTER_INFO_LEVEL *printer)
3395 LDAPMessage *res = NULL;
3396 char *prt_dn = NULL;
3398 DEBUG(5, ("unpublishing printer %s\n", printer->info_2->printername));
3400 /* remove the printer from the directory */
3401 ads_rc = ads_find_printer_on_server(ads, &res,
3402 printer->info_2->sharename, global_myname());
3404 if (ADS_ERR_OK(ads_rc) && res && ads_count_replies(ads, res)) {
3405 prt_dn = ads_get_dn(ads, res);
3407 ads_msgfree(ads, res);
3410 ads_rc = ads_del_dn(ads, prt_dn);
3411 ads_memfree(ads, prt_dn);
3415 ads_msgfree(ads, res);
3420 /****************************************************************************
3421 * Publish a printer in the directory
3423 * @param snum describing printer service
3424 * @return WERROR indicating status of publishing
3425 ***************************************************************************/
3427 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3430 ADS_STRUCT *ads = NULL;
3431 NT_PRINTER_INFO_LEVEL *printer = NULL;
3434 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3435 if (!W_ERROR_IS_OK(win_rc))
3439 case SPOOL_DS_PUBLISH:
3440 case SPOOL_DS_UPDATE:
3441 /* set the DsSpooler info and attributes */
3442 if (!(map_nt_printer_info2_to_dsspooler(printer->info_2))) {
3443 win_rc = WERR_NOMEM;
3447 printer->info_2->attributes |= PRINTER_ATTRIBUTE_PUBLISHED;
3449 case SPOOL_DS_UNPUBLISH:
3450 printer->info_2->attributes ^= PRINTER_ATTRIBUTE_PUBLISHED;
3453 win_rc = WERR_NOT_SUPPORTED;
3457 win_rc = mod_a_printer(printer, 2);
3458 if (!W_ERROR_IS_OK(win_rc)) {
3459 DEBUG(3, ("err %d saving data\n", W_ERROR_V(win_rc)));
3463 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3465 DEBUG(3, ("ads_init() failed\n"));
3466 win_rc = WERR_SERVER_UNAVAILABLE;
3469 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3470 SAFE_FREE(ads->auth.password);
3471 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3474 /* ads_connect() will find the DC for us */
3475 ads_rc = ads_connect(ads);
3476 if (!ADS_ERR_OK(ads_rc)) {
3477 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3478 win_rc = WERR_ACCESS_DENIED;
3483 case SPOOL_DS_PUBLISH:
3484 case SPOOL_DS_UPDATE:
3485 win_rc = nt_printer_publish_ads(ads, printer);
3487 case SPOOL_DS_UNPUBLISH:
3488 win_rc = nt_printer_unpublish_ads(ads, printer);
3493 free_a_printer(&printer, 2);
3498 WERROR check_published_printers(void)
3501 ADS_STRUCT *ads = NULL;
3503 int n_services = lp_numservices();
3504 NT_PRINTER_INFO_LEVEL *printer = NULL;
3506 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3508 DEBUG(3, ("ads_init() failed\n"));
3509 return WERR_SERVER_UNAVAILABLE;
3511 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3512 SAFE_FREE(ads->auth.password);
3513 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3516 /* ads_connect() will find the DC for us */
3517 ads_rc = ads_connect(ads);
3518 if (!ADS_ERR_OK(ads_rc)) {
3519 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3521 ads_kdestroy("MEMORY:prtpub_cache");
3522 return WERR_ACCESS_DENIED;
3525 for (snum = 0; snum < n_services; snum++) {
3526 if (!(lp_snum_ok(snum) && lp_print_ok(snum)))
3529 if (W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2,
3530 lp_servicename(snum))) &&
3531 (printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED))
3532 nt_printer_publish_ads(ads, printer);
3534 free_a_printer(&printer, 2);
3538 ads_kdestroy("MEMORY:prtpub_cache");
3542 bool is_printer_published(Printer_entry *print_hnd, int snum,
3545 NT_PRINTER_INFO_LEVEL *printer = NULL;
3547 REGISTRY_VALUE *guid_val;
3552 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3554 if (!W_ERROR_IS_OK(win_rc) ||
3555 !(printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED) ||
3556 ((i = lookup_printerkey(printer->info_2->data, SPOOL_DSSPOOLER_KEY)) < 0) ||
3557 !(ctr = printer->info_2->data->keys[i].values) ||
3558 !(guid_val = regval_ctr_getvalue(ctr, "objectGUID")))
3560 free_a_printer(&printer, 2);
3564 /* fetching printer guids really ought to be a separate function. */
3569 /* We used to store the guid as REG_BINARY, then swapped
3570 to REG_SZ for Vista compatibility so check for both */
3572 switch ( regval_type(guid_val) ){
3574 rpcstr_pull( guid_str, regval_data_p(guid_val),
3575 sizeof(guid_str)-1, -1, STR_TERMINATE );
3576 ret = NT_STATUS_IS_OK(GUID_from_string( guid_str, guid ));
3579 if ( regval_size(guid_val) != sizeof(struct GUID) ) {
3583 memcpy(guid, regval_data_p(guid_val), sizeof(struct GUID));
3586 DEBUG(0,("is_printer_published: GUID value stored as "
3587 "invaluid type (%d)\n", regval_type(guid_val) ));
3592 free_a_printer(&printer, 2);
3596 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3601 WERROR check_published_printers(void)
3606 bool is_printer_published(Printer_entry *print_hnd, int snum,
3611 #endif /* HAVE_ADS */
3613 /****************************************************************************
3614 ***************************************************************************/
3616 WERROR delete_all_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key )
3618 NT_PRINTER_DATA *data;
3620 int removed_keys = 0;
3624 empty_slot = data->num_keys;
3627 return WERR_INVALID_PARAM;
3629 /* remove all keys */
3631 if ( !strlen(key) ) {
3633 TALLOC_FREE( data );
3637 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from printer [%s]\n",
3643 /* remove a specific key (and all subkeys) */
3645 for ( i=0; i<data->num_keys; i++ ) {
3646 if ( StrnCaseCmp( data->keys[i].name, key, strlen(key)) == 0 ) {
3647 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from key [%s]\n",
3648 data->keys[i].name));
3650 TALLOC_FREE( data->keys[i].name );
3651 TALLOC_FREE( data->keys[i].values );
3653 /* mark the slot as empty */
3655 ZERO_STRUCTP( &data->keys[i] );
3659 /* find the first empty slot */
3661 for ( i=0; i<data->num_keys; i++ ) {
3662 if ( !data->keys[i].name ) {
3669 if ( i == data->num_keys )
3670 /* nothing was removed */
3671 return WERR_INVALID_PARAM;
3673 /* move everything down */
3675 for ( i=empty_slot+1; i<data->num_keys; i++ ) {
3676 if ( data->keys[i].name ) {
3677 memcpy( &data->keys[empty_slot], &data->keys[i], sizeof(NT_PRINTER_KEY) );
3678 ZERO_STRUCTP( &data->keys[i] );
3686 data->num_keys -= removed_keys;
3688 /* sanity check to see if anything is left */
3690 if ( !data->num_keys ) {
3691 DEBUG(8,("delete_all_printer_data: No keys left for printer [%s]\n", p2->printername ));
3693 SAFE_FREE( data->keys );
3694 ZERO_STRUCTP( data );
3700 /****************************************************************************
3701 ***************************************************************************/
3703 WERROR delete_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3705 WERROR result = WERR_OK;
3708 /* we must have names on non-zero length */
3710 if ( !key || !*key|| !value || !*value )
3711 return WERR_INVALID_NAME;
3713 /* find the printer key first */
3715 key_index = lookup_printerkey( p2->data, key );
3716 if ( key_index == -1 )
3719 /* make sure the value exists so we can return the correct error code */
3721 if ( !regval_ctr_getvalue( p2->data->keys[key_index].values, value ) )
3722 return WERR_BADFILE;
3724 regval_ctr_delvalue( p2->data->keys[key_index].values, value );
3726 DEBUG(8,("delete_printer_data: Removed key => [%s], value => [%s]\n",
3732 /****************************************************************************
3733 ***************************************************************************/
3735 WERROR add_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value,
3736 uint32 type, uint8 *data, int real_len )
3738 WERROR result = WERR_OK;
3741 /* we must have names on non-zero length */
3743 if ( !key || !*key|| !value || !*value )
3744 return WERR_INVALID_NAME;
3746 /* find the printer key first */
3748 key_index = lookup_printerkey( p2->data, key );
3749 if ( key_index == -1 )
3750 key_index = add_new_printer_key( p2->data, key );
3752 if ( key_index == -1 )
3755 regval_ctr_addvalue( p2->data->keys[key_index].values, value,
3756 type, (const char *)data, real_len );
3758 DEBUG(8,("add_printer_data: Added key => [%s], value => [%s], type=> [%d], size => [%d]\n",
3759 key, value, type, real_len ));
3764 /****************************************************************************
3765 ***************************************************************************/
3767 REGISTRY_VALUE* get_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3771 if ( (key_index = lookup_printerkey( p2->data, key )) == -1 )
3774 DEBUG(8,("get_printer_data: Attempting to lookup key => [%s], value => [%s]\n",
3777 return regval_ctr_getvalue( p2->data->keys[key_index].values, value );
3780 /****************************************************************************
3781 Unpack a list of registry values frem the TDB
3782 ***************************************************************************/
3784 static int unpack_values(NT_PRINTER_DATA *printer_data, const uint8 *buf, int buflen)
3789 const char *valuename = NULL;
3790 const char *keyname = NULL;
3794 REGISTRY_VALUE *regval_p;
3797 /* add the "PrinterDriverData" key first for performance reasons */
3799 add_new_printer_key( printer_data, SPOOL_PRINTERDATA_KEY );
3801 /* loop and unpack the rest of the registry values */
3805 /* check to see if there are any more registry values */
3808 len += tdb_unpack(buf+len, buflen-len, "p", ®val_p);
3812 /* unpack the next regval */
3814 len += tdb_unpack(buf+len, buflen-len, "fdB",
3820 /* lookup for subkey names which have a type of REG_NONE */
3821 /* there's no data with this entry */
3823 if ( type == REG_NONE ) {
3824 if ( (key_index=lookup_printerkey( printer_data, string)) == -1 )
3825 add_new_printer_key( printer_data, string );
3830 * break of the keyname from the value name.
3831 * Valuenames can have embedded '\'s so be careful.
3832 * only support one level of keys. See the
3833 * "Konica Fiery S300 50C-K v1.1. enu" 2k driver.
3837 str = strchr_m( string, '\\');
3839 /* Put in "PrinterDriverData" is no key specified */
3842 keyname = SPOOL_PRINTERDATA_KEY;
3851 /* see if we need a new key */
3853 if ( (key_index=lookup_printerkey( printer_data, keyname )) == -1 )
3854 key_index = add_new_printer_key( printer_data, keyname );
3856 if ( key_index == -1 ) {
3857 DEBUG(0,("unpack_values: Failed to allocate a new key [%s]!\n",
3862 DEBUG(8,("specific: [%s:%s], len: %d\n", keyname, valuename, size));
3864 /* Vista doesn't like unknown REG_BINARY values in DsSpooler.
3865 Thanks to Martin Zielinski for the hint. */
3867 if ( type == REG_BINARY &&
3868 strequal( keyname, SPOOL_DSSPOOLER_KEY ) &&
3869 strequal( valuename, "objectGUID" ) )
3872 UNISTR2 unistr_guid;
3874 ZERO_STRUCT( unistr_guid );
3876 /* convert the GUID to a UNICODE string */
3878 memcpy( &guid, data_p, sizeof(struct GUID) );
3880 init_unistr2( &unistr_guid,
3881 GUID_string(talloc_tos(), &guid),
3882 UNI_STR_TERMINATE );
3884 regval_ctr_addvalue( printer_data->keys[key_index].values,
3886 (const char *)unistr_guid.buffer,
3887 unistr_guid.uni_str_len*2 );
3892 regval_ctr_addvalue( printer_data->keys[key_index].values,
3893 valuename, type, (const char *)data_p,
3897 SAFE_FREE(data_p); /* 'B' option to tdbpack does a malloc() */
3904 /****************************************************************************
3905 ***************************************************************************/
3907 static char *last_from;
3908 static char *last_to;
3910 static const char *get_last_from(void)
3918 static const char *get_last_to(void)
3926 static bool set_last_from_to(const char *from, const char *to)
3928 char *orig_from = last_from;
3929 char *orig_to = last_to;
3931 last_from = SMB_STRDUP(from);
3932 last_to = SMB_STRDUP(to);
3934 SAFE_FREE(orig_from);
3937 if (!last_from || !last_to) {
3938 SAFE_FREE(last_from);
3945 static void map_to_os2_driver(fstring drivername)
3947 char *mapfile = lp_os2_driver_map();
3948 char **lines = NULL;
3952 if (!strlen(drivername))
3958 if (strequal(drivername,get_last_from())) {
3959 DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",
3960 drivername,get_last_to()));
3961 fstrcpy(drivername,get_last_to());
3965 lines = file_lines_load(mapfile, &numlines,0,NULL);
3966 if (numlines == 0 || lines == NULL) {
3967 DEBUG(0,("No entries in OS/2 driver map %s\n",mapfile));
3972 DEBUG(4,("Scanning OS/2 driver map %s\n",mapfile));
3974 for( i = 0; i < numlines; i++) {
3975 char *nt_name = lines[i];
3976 char *os2_name = strchr(nt_name,'=');
3983 while (isspace(*nt_name))
3986 if (!*nt_name || strchr("#;",*nt_name))
3990 int l = strlen(nt_name);
3991 while (l && isspace(nt_name[l-1])) {
3997 while (isspace(*os2_name))
4001 int l = strlen(os2_name);
4002 while (l && isspace(os2_name[l-1])) {
4008 if (strequal(nt_name,drivername)) {
4009 DEBUG(3,("Mapped windows driver %s to os2 driver%s\n",drivername,os2_name));
4010 set_last_from_to(drivername,os2_name);
4011 fstrcpy(drivername,os2_name);
4020 /****************************************************************************
4021 Get a default printer info 2 struct.
4022 ****************************************************************************/
4024 static WERROR get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 *info,
4025 const char *servername,
4026 const char* sharename,
4029 int snum = lp_servicenumber(sharename);
4031 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
4032 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
4033 servername, sharename);
4034 fstrcpy(info->sharename, sharename);
4035 fstrcpy(info->portname, SAMBA_PRINTER_PORT_NAME);
4037 /* by setting the driver name to an empty string, a local NT admin
4038 can now run the **local** APW to install a local printer driver
4039 for a Samba shared printer in 2.2. Without this, drivers **must** be
4040 installed on the Samba server for NT clients --jerry */
4041 #if 0 /* JERRY --do not uncomment-- */
4042 if (!*info->drivername)
4043 fstrcpy(info->drivername, "NO DRIVER AVAILABLE FOR THIS PRINTER");
4047 DEBUG(10,("get_a_printer_2_default: driver name set to [%s]\n", info->drivername));
4049 strlcpy(info->comment, "", sizeof(info->comment));
4050 fstrcpy(info->printprocessor, "winprint");
4051 fstrcpy(info->datatype, "RAW");
4054 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
4055 /* Pull the location and comment strings from cups if we don't
4057 if ( !strlen(info->location) || !strlen(info->comment) )
4058 cups_pull_comment_location( info );
4062 info->attributes = PRINTER_ATTRIBUTE_SAMBA;
4064 info->starttime = 0; /* Minutes since 12:00am GMT */
4065 info->untiltime = 0; /* Minutes since 12:00am GMT */
4067 info->default_priority = 1;
4068 info->setuptime = (uint32)time(NULL);
4071 * I changed this as I think it is better to have a generic
4072 * DEVMODE than to crash Win2k explorer.exe --jerry
4073 * See the HP Deskjet 990c Win2k drivers for an example.
4075 * However the default devmode appears to cause problems
4076 * with the HP CLJ 8500 PCL driver. Hence the addition of
4077 * the "default devmode" parameter --jerry 22/01/2002
4080 if (lp_default_devmode(snum)) {
4081 if ((info->devmode = construct_nt_devicemode(info->printername)) == NULL) {
4085 info->devmode = NULL;
4088 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
4096 free_nt_devicemode(&info->devmode);
4098 return WERR_ACCESS_DENIED;
4101 /****************************************************************************
4102 ****************************************************************************/
4104 static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info,
4105 const char *servername,
4106 const char *sharename,
4110 int snum = lp_servicenumber(sharename);
4111 TDB_DATA kbuf, dbuf;
4112 fstring printername;
4113 char adevice[MAXDEVICENAME];
4114 char *comment = NULL;
4116 kbuf = make_printer_tdbkey(talloc_tos(), sharename);
4118 dbuf = tdb_fetch(tdb_printers, kbuf);
4120 return get_a_printer_2_default(info, servername,
4121 sharename, get_loc_com);
4124 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "dddddddddddfffffPfffff",
4127 &info->default_priority,
4134 &info->c_setprinter,
4144 info->printprocessor,
4149 strlcpy(info->comment, comment, sizeof(info->comment));
4153 /* Samba has to have shared raw drivers. */
4154 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
4155 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
4157 /* Restore the stripped strings. */
4158 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
4160 if ( lp_force_printername(snum) ) {
4161 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, sharename );
4163 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, info->printername);
4166 fstrcpy(info->printername, printername);
4169 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
4170 /* Pull the location and comment strings from cups if we don't
4172 if ( !strlen(info->location) || !strlen(info->comment) )
4173 cups_pull_comment_location( info );
4177 len += unpack_devicemode(&info->devmode,dbuf.dptr+len, dbuf.dsize-len);
4180 * Some client drivers freak out if there is a NULL devmode
4181 * (probably the driver is not checking before accessing
4182 * the devmode pointer) --jerry
4184 * See comments in get_a_printer_2_default()
4187 if (lp_default_devmode(snum) && !info->devmode) {
4188 DEBUG(8,("get_a_printer_2: Constructing a default device mode for [%s]\n",
4190 info->devmode = construct_nt_devicemode(printername);
4193 slprintf( adevice, sizeof(adevice), "%s", info->printername );
4194 if (info->devmode) {
4195 fstrcpy(info->devmode->devicename, adevice);
4198 if ( !(info->data = TALLOC_ZERO_P( info, NT_PRINTER_DATA )) ) {
4199 DEBUG(0,("unpack_values: talloc() failed!\n"));
4200 SAFE_FREE(dbuf.dptr);
4203 len += unpack_values( info->data, dbuf.dptr+len, dbuf.dsize-len );
4205 /* This will get the current RPC talloc context, but we should be
4206 passing this as a parameter... fixme... JRA ! */
4208 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
4209 SAFE_FREE(dbuf.dptr);
4213 /* Fix for OS/2 drivers. */
4215 if (get_remote_arch() == RA_OS2) {
4216 map_to_os2_driver(info->drivername);
4219 SAFE_FREE(dbuf.dptr);
4221 DEBUG(9,("Unpacked printer [%s] name [%s] running driver [%s]\n",
4222 sharename, info->printername, info->drivername));
4227 /****************************************************************************
4228 Debugging function, dump at level 6 the struct in the logs.
4229 ****************************************************************************/
4230 static uint32 dump_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4233 NT_PRINTER_INFO_LEVEL_2 *info2;
4235 DEBUG(106,("Dumping printer at level [%d]\n", level));
4240 if (printer->info_2 == NULL)
4244 info2=printer->info_2;
4246 DEBUGADD(106,("attributes:[%d]\n", info2->attributes));
4247 DEBUGADD(106,("priority:[%d]\n", info2->priority));
4248 DEBUGADD(106,("default_priority:[%d]\n", info2->default_priority));
4249 DEBUGADD(106,("starttime:[%d]\n", info2->starttime));
4250 DEBUGADD(106,("untiltime:[%d]\n", info2->untiltime));
4251 DEBUGADD(106,("status:[%d]\n", info2->status));
4252 DEBUGADD(106,("cjobs:[%d]\n", info2->cjobs));
4253 DEBUGADD(106,("averageppm:[%d]\n", info2->averageppm));
4254 DEBUGADD(106,("changeid:[%d]\n", info2->changeid));
4255 DEBUGADD(106,("c_setprinter:[%d]\n", info2->c_setprinter));
4256 DEBUGADD(106,("setuptime:[%d]\n", info2->setuptime));
4258 DEBUGADD(106,("servername:[%s]\n", info2->servername));
4259 DEBUGADD(106,("printername:[%s]\n", info2->printername));
4260 DEBUGADD(106,("sharename:[%s]\n", info2->sharename));
4261 DEBUGADD(106,("portname:[%s]\n", info2->portname));
4262 DEBUGADD(106,("drivername:[%s]\n", info2->drivername));
4263 DEBUGADD(106,("comment:[%s]\n", info2->comment));
4264 DEBUGADD(106,("location:[%s]\n", info2->location));
4265 DEBUGADD(106,("sepfile:[%s]\n", info2->sepfile));
4266 DEBUGADD(106,("printprocessor:[%s]\n", info2->printprocessor));
4267 DEBUGADD(106,("datatype:[%s]\n", info2->datatype));
4268 DEBUGADD(106,("parameters:[%s]\n", info2->parameters));
4274 DEBUGADD(106,("dump_a_printer: Level %u not implemented\n", (unsigned int)level ));
4282 /****************************************************************************
4283 Update the changeid time.
4284 This is SO NASTY as some drivers need this to change, others need it
4285 static. This value will change every second, and I must hope that this
4286 is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
4288 ****************************************************************************/
4290 static uint32 rev_changeid(void)
4294 get_process_uptime(&tv);
4297 /* Return changeid as msec since spooler restart */
4298 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
4301 * This setting seems to work well but is too untested
4302 * to replace the above calculation. Left in for experiementation
4303 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
4305 return tv.tv_sec * 10 + tv.tv_usec / 100000;
4311 * The function below are the high level ones.
4312 * only those ones must be called from the spoolss code.
4316 /****************************************************************************
4317 Modify a printer. This is called from SETPRINTERDATA/DELETEPRINTERDATA.
4318 ****************************************************************************/
4320 WERROR mod_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4324 dump_a_printer(printer, level);
4330 * Update the changestamp. Emperical tests show that the
4331 * ChangeID is always updated,but c_setprinter is
4332 * global spooler variable (not per printer).
4335 /* ChangeID **must** be increasing over the lifetime
4336 of client's spoolss service in order for the
4337 client's cache to show updates */
4339 printer->info_2->changeid = rev_changeid();
4342 * Because one day someone will ask:
4343 * NT->NT An admin connection to a remote
4344 * printer show changes imeediately in
4345 * the properities dialog
4347 * A non-admin connection will only show the
4348 * changes after viewing the properites page
4349 * 2 times. Seems to be related to a
4350 * race condition in the client between the spooler
4351 * updating the local cache and the Explorer.exe GUI
4352 * actually displaying the properties.
4354 * This is fixed in Win2k. admin/non-admin
4355 * connections both display changes immediately.
4360 result=update_a_printer_2(printer->info_2);
4364 result=WERR_UNKNOWN_LEVEL;
4371 /****************************************************************************
4372 Initialize printer devmode & data with previously saved driver init values.
4373 ****************************************************************************/
4375 static bool set_driver_init_2( NT_PRINTER_INFO_LEVEL_2 *info_ptr )
4380 NT_PRINTER_INFO_LEVEL_2 info;
4386 * Delete any printer data 'values' already set. When called for driver
4387 * replace, there will generally be some, but during an add printer, there
4388 * should not be any (if there are delete them).
4391 if ( info_ptr->data )
4392 delete_all_printer_data( info_ptr, "" );
4394 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX,
4395 info_ptr->drivername) < 0) {
4399 dbuf = tdb_fetch_bystring(tdb_drivers, key);
4402 * When changing to a driver that has no init info in the tdb, remove
4403 * the previous drivers init info and leave the new on blank.
4405 free_nt_devicemode(&info_ptr->devmode);
4412 * Get the saved DEVMODE..
4415 len += unpack_devicemode(&info.devmode,dbuf.dptr+len, dbuf.dsize-len);
4418 * The saved DEVMODE contains the devicename from the printer used during
4419 * the initialization save. Change it to reflect the new printer.
4422 if ( info.devmode ) {
4423 ZERO_STRUCT(info.devmode->devicename);
4424 fstrcpy(info.devmode->devicename, info_ptr->printername);
4428 * NT/2k does not change out the entire DeviceMode of a printer
4429 * when changing the driver. Only the driverextra, private, &
4430 * driverversion fields. --jerry (Thu Mar 14 08:58:43 CST 2002)
4432 * Later examination revealed that Windows NT/2k does reset the
4433 * the printer's device mode, bit **only** when you change a
4434 * property of the device mode such as the page orientation.
4439 /* Bind the saved DEVMODE to the new the printer */
4441 free_nt_devicemode(&info_ptr->devmode);
4442 info_ptr->devmode = info.devmode;
4444 DEBUG(10,("set_driver_init_2: Set printer [%s] init %s DEVMODE for driver [%s]\n",
4445 info_ptr->printername, info_ptr->devmode?"VALID":"NULL", info_ptr->drivername));
4447 /* Add the printer data 'values' to the new printer */
4449 if ( !(info_ptr->data = TALLOC_ZERO_P( info_ptr, NT_PRINTER_DATA )) ) {
4450 DEBUG(0,("set_driver_init_2: talloc() failed!\n"));
4454 len += unpack_values( info_ptr->data, dbuf.dptr+len, dbuf.dsize-len );
4456 SAFE_FREE(dbuf.dptr);
4461 /****************************************************************************
4462 Initialize printer devmode & data with previously saved driver init values.
4463 When a printer is created using AddPrinter, the drivername bound to the
4464 printer is used to lookup previously saved driver initialization info, which
4465 is bound to the new printer.
4466 ****************************************************************************/
4468 bool set_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4470 bool result = False;
4474 result = set_driver_init_2(printer->info_2);
4478 DEBUG(0,("set_driver_init: Programmer's error! Unknown driver_init level [%d]\n",
4486 /****************************************************************************
4487 Delete driver init data stored for a specified driver
4488 ****************************************************************************/
4490 bool del_driver_init(char *drivername)
4495 if (!drivername || !*drivername) {
4496 DEBUG(3,("del_driver_init: No drivername specified!\n"));
4500 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, drivername) < 0) {
4504 DEBUG(6,("del_driver_init: Removing driver init data for [%s]\n",
4507 ret = (tdb_delete_bystring(tdb_drivers, key) == 0);
4512 /****************************************************************************
4513 Pack up the DEVMODE and values for a printer into a 'driver init' entry
4514 in the tdb. Note: this is different from the driver entry and the printer
4515 entry. There should be a single driver init entry for each driver regardless
4516 of whether it was installed from NT or 2K. Technically, they should be
4517 different, but they work out to the same struct.
4518 ****************************************************************************/
4520 static uint32 update_driver_init_2(NT_PRINTER_INFO_LEVEL_2 *info)
4524 int buflen, len, ret;
4533 len += pack_devicemode(info->devmode, buf+len, buflen-len);
4535 retlen = pack_values( info->data, buf+len, buflen-len );
4543 buf = (uint8 *)SMB_REALLOC(buf, len);
4545 DEBUG(0, ("update_driver_init_2: failed to enlarge buffer!\n"));
4554 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, info->drivername) < 0) {
4562 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
4566 DEBUG(8, ("update_driver_init_2: error updating printer init to tdb on disk\n"));
4570 DEBUG(10,("update_driver_init_2: Saved printer [%s] init DEVMODE & values for driver [%s]\n",
4571 info->sharename, info->drivername));
4576 /****************************************************************************
4577 Update (i.e. save) the driver init info (DEVMODE and values) for a printer
4578 ****************************************************************************/
4580 static uint32 update_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4584 dump_a_printer(printer, level);
4588 result = update_driver_init_2(printer->info_2);
4598 /****************************************************************************
4599 Convert the printer data value, a REG_BINARY array, into an initialization
4600 DEVMODE. Note: the array must be parsed as if it was a DEVMODE in an rpc...
4601 got to keep the endians happy :).
4602 ****************************************************************************/
4604 static bool convert_driver_init( TALLOC_CTX *ctx, NT_DEVICEMODE *nt_devmode, uint8 *data, uint32 data_len )
4606 bool result = False;
4610 ZERO_STRUCT(devmode);
4612 prs_init_empty(&ps, ctx, UNMARSHALL);
4613 ps.data_p = (char *)data;
4614 ps.buffer_size = data_len;
4616 if (spoolss_io_devmode("phantom DEVMODE", &ps, 0, &devmode))
4617 result = convert_devicemode("", &devmode, &nt_devmode);
4619 DEBUG(10,("convert_driver_init: error parsing DEVMODE\n"));
4624 /****************************************************************************
4625 Set the DRIVER_INIT info in the tdb. Requires Win32 client code that:
4627 1. Use the driver's config DLL to this UNC printername and:
4628 a. Call DrvPrintEvent with PRINTER_EVENT_INITIALIZE
4629 b. Call DrvConvertDevMode with CDM_DRIVER_DEFAULT to get default DEVMODE
4630 2. Call SetPrinterData with the 'magic' key and the DEVMODE as data.
4632 The last step triggers saving the "driver initialization" information for
4633 this printer into the tdb. Later, new printers that use this driver will
4634 have this initialization information bound to them. This simulates the
4635 driver initialization, as if it had run on the Samba server (as it would
4638 The Win32 client side code requirement sucks! But until we can run arbitrary
4639 Win32 printer driver code on any Unix that Samba runs on, we are stuck with it.
4641 It would have been easier to use SetPrinter because all the UNMARSHALLING of
4642 the DEVMODE is done there, but 2K/XP clients do not set the DEVMODE... think
4643 about it and you will realize why. JRR 010720
4644 ****************************************************************************/
4646 static WERROR save_driver_init_2(NT_PRINTER_INFO_LEVEL *printer, uint8 *data, uint32 data_len )
4648 WERROR status = WERR_OK;
4649 TALLOC_CTX *ctx = NULL;
4650 NT_DEVICEMODE *nt_devmode = NULL;
4651 NT_DEVICEMODE *tmp_devmode = printer->info_2->devmode;
4654 * When the DEVMODE is already set on the printer, don't try to unpack it.
4656 DEBUG(8,("save_driver_init_2: Enter...\n"));
4658 if ( !printer->info_2->devmode && data_len ) {
4660 * Set devmode on printer info, so entire printer initialization can be
4664 if ((ctx = talloc_init("save_driver_init_2")) == NULL)
4667 if ((nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE)) == NULL) {
4668 status = WERR_NOMEM;
4672 ZERO_STRUCTP(nt_devmode);
4675 * The DEVMODE is held in the 'data' component of the param in raw binary.
4676 * Convert it to to a devmode structure
4678 if ( !convert_driver_init( ctx, nt_devmode, data, data_len )) {
4679 DEBUG(10,("save_driver_init_2: error converting DEVMODE\n"));
4680 status = WERR_INVALID_PARAM;
4684 printer->info_2->devmode = nt_devmode;
4688 * Pack up and add (or update) the DEVMODE and any current printer data to
4689 * a 'driver init' element in the tdb
4693 if ( update_driver_init(printer, 2) != 0 ) {
4694 DEBUG(10,("save_driver_init_2: error updating DEVMODE\n"));
4695 status = WERR_NOMEM;
4700 * If driver initialization info was successfully saved, set the current
4701 * printer to match it. This allows initialization of the current printer
4702 * as well as the driver.
4704 status = mod_a_printer(printer, 2);
4705 if (!W_ERROR_IS_OK(status)) {
4706 DEBUG(10,("save_driver_init_2: error setting DEVMODE on printer [%s]\n",
4707 printer->info_2->printername));
4711 talloc_destroy(ctx);
4712 free_nt_devicemode( &nt_devmode );
4714 printer->info_2->devmode = tmp_devmode;
4719 /****************************************************************************
4720 Update the driver init info (DEVMODE and specifics) for a printer
4721 ****************************************************************************/
4723 WERROR save_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level, uint8 *data, uint32 data_len)
4725 WERROR status = WERR_OK;
4729 status = save_driver_init_2( printer, data, data_len );
4732 status = WERR_UNKNOWN_LEVEL;
4739 /****************************************************************************
4740 Get a NT_PRINTER_INFO_LEVEL struct. It returns malloced memory.
4742 Previously the code had a memory allocation problem because it always
4743 used the TALLOC_CTX from the Printer_entry*. This context lasts
4744 as a long as the original handle is open. So if the client made a lot
4745 of getprinter[data]() calls, the memory usage would climb. Now we use
4746 a short lived TALLOC_CTX for printer_info_2 objects returned. We
4747 still use the Printer_entry->ctx for maintaining the cache copy though
4748 since that object must live as long as the handle by definition.
4751 ****************************************************************************/
4753 static WERROR get_a_printer_internal( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level,
4754 const char *sharename, bool get_loc_com)
4759 DEBUG(10,("get_a_printer: [%s] level %u\n", sharename, (unsigned int)level));
4761 if ( !(*pp_printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
4762 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4768 if ( !((*pp_printer)->info_2 = TALLOC_ZERO_P(*pp_printer, NT_PRINTER_INFO_LEVEL_2)) ) {
4769 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4770 TALLOC_FREE( *pp_printer );
4775 fstrcpy( servername, print_hnd->servername );
4777 fstrcpy( servername, "%L" );
4778 standard_sub_basic( "", "", servername,
4779 sizeof(servername)-1 );
4782 result = get_a_printer_2( (*pp_printer)->info_2,
4783 servername, sharename, get_loc_com);
4785 /* we have a new printer now. Save it with this handle */
4787 if ( !W_ERROR_IS_OK(result) ) {
4788 TALLOC_FREE( *pp_printer );
4789 DEBUG(10,("get_a_printer: [%s] level %u returning %s\n",
4790 sharename, (unsigned int)level, win_errstr(result)));
4794 dump_a_printer( *pp_printer, level);
4799 TALLOC_FREE( *pp_printer );
4800 return WERR_UNKNOWN_LEVEL;
4806 WERROR get_a_printer( Printer_entry *print_hnd,
4807 NT_PRINTER_INFO_LEVEL **pp_printer,
4809 const char *sharename)
4811 return get_a_printer_internal(print_hnd, pp_printer, level,
4815 WERROR get_a_printer_search( Printer_entry *print_hnd,
4816 NT_PRINTER_INFO_LEVEL **pp_printer,
4818 const char *sharename)
4820 return get_a_printer_internal(print_hnd, pp_printer, level,
4824 /****************************************************************************
4825 Deletes a NT_PRINTER_INFO_LEVEL struct.
4826 ****************************************************************************/
4828 uint32 free_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level)
4830 NT_PRINTER_INFO_LEVEL *printer = *pp_printer;
4837 if ( printer->info_2 )
4838 free_nt_printer_info_level_2(&printer->info_2);
4842 DEBUG(0,("free_a_printer: unknown level! [%d]\n", level ));
4846 TALLOC_FREE(*pp_printer);
4851 /****************************************************************************
4852 ****************************************************************************/
4853 uint32 add_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
4856 DEBUG(104,("adding a printer at level [%d]\n", level));
4857 dump_a_printer_driver(driver, level);
4861 result=add_a_printer_driver_3(driver.info_3);
4865 result=add_a_printer_driver_6(driver.info_6);
4875 /****************************************************************************
4876 ****************************************************************************/
4878 WERROR get_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL *driver, uint32 level,
4879 fstring drivername, const char *architecture, uint32 version)
4885 /* Sometime we just want any version of the driver */
4887 if ( version == DRIVER_ANY_VERSION ) {
4888 /* look for Win2k first and then for NT4 */
4889 result = get_a_printer_driver_3(&driver->info_3, drivername,
4892 if ( !W_ERROR_IS_OK(result) ) {
4893 result = get_a_printer_driver_3( &driver->info_3,
4894 drivername, architecture, 2 );
4897 result = get_a_printer_driver_3(&driver->info_3, drivername,
4898 architecture, version);
4907 if (W_ERROR_IS_OK(result))
4908 dump_a_printer_driver(*driver, level);
4913 /****************************************************************************
4914 ****************************************************************************/
4915 uint32 free_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
4922 NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3;
4923 if (driver.info_3 != NULL)
4925 info3=driver.info_3;
4926 SAFE_FREE(info3->dependentfiles);
4927 ZERO_STRUCTP(info3);
4937 NT_PRINTER_DRIVER_INFO_LEVEL_6 *info6;
4938 if (driver.info_6 != NULL) {
4939 info6=driver.info_6;
4940 SAFE_FREE(info6->dependentfiles);
4941 SAFE_FREE(info6->previousnames);
4942 ZERO_STRUCTP(info6);
4958 /****************************************************************************
4959 Determine whether or not a particular driver is currently assigned
4961 ****************************************************************************/
4963 bool printer_driver_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3 )
4966 int n_services = lp_numservices();
4967 NT_PRINTER_INFO_LEVEL *printer = NULL;
4968 bool in_use = False;
4973 DEBUG(10,("printer_driver_in_use: Beginning search through ntprinters.tdb...\n"));
4975 /* loop through the printers.tdb and check for the drivername */
4977 for (snum=0; snum<n_services && !in_use; snum++) {
4978 if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
4981 if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_servicename(snum))) )
4984 if ( strequal(info_3->name, printer->info_2->drivername) )
4987 free_a_printer( &printer, 2 );
4990 DEBUG(10,("printer_driver_in_use: Completed search through ntprinters.tdb...\n"));
4993 NT_PRINTER_DRIVER_INFO_LEVEL d;
4996 DEBUG(5,("printer_driver_in_use: driver \"%s\" is currently in use\n", info_3->name));
4998 /* we can still remove the driver if there is one of
4999 "Windows NT x86" version 2 or 3 left */
5001 if ( !strequal( "Windows NT x86", info_3->environment ) ) {
5002 werr = get_a_printer_driver( &d, 3, info_3->name, "Windows NT x86", DRIVER_ANY_VERSION );
5005 switch ( info_3->cversion ) {
5007 werr = get_a_printer_driver( &d, 3, info_3->name, "Windows NT x86", 3 );
5010 werr = get_a_printer_driver( &d, 3, info_3->name, "Windows NT x86", 2 );
5013 DEBUG(0,("printer_driver_in_use: ERROR! unknown driver version (%d)\n",
5015 werr = WERR_UNKNOWN_PRINTER_DRIVER;
5020 /* now check the error code */
5022 if ( W_ERROR_IS_OK(werr) ) {
5023 /* it's ok to remove the driver, we have other architctures left */
5025 free_a_printer_driver( d, 3 );
5029 /* report that the driver is not in use by default */
5035 /**********************************************************************
5036 Check to see if a ogiven file is in use by *info
5037 *********************************************************************/
5039 static bool drv_file_in_use( char* file, NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
5046 /* mz: skip files that are in the list but already deleted */
5047 if (!file || !file[0]) {
5051 if ( strequal(file, info->driverpath) )
5054 if ( strequal(file, info->datafile) )
5057 if ( strequal(file, info->configfile) )
5060 if ( strequal(file, info->helpfile) )
5063 /* see of there are any dependent files to examine */
5065 if ( !info->dependentfiles )
5068 while ( *info->dependentfiles[i] ) {
5069 if ( strequal(file, info->dependentfiles[i]) )
5078 /**********************************************************************
5079 Utility function to remove the dependent file pointed to by the
5080 input parameter from the list
5081 *********************************************************************/
5083 static void trim_dependent_file( fstring files[], int idx )
5086 /* bump everything down a slot */
5088 while( *files[idx+1] ) {
5089 fstrcpy( files[idx], files[idx+1] );
5098 /**********************************************************************
5099 Check if any of the files used by src are also used by drv
5100 *********************************************************************/
5102 static bool trim_overlap_drv_files( NT_PRINTER_DRIVER_INFO_LEVEL_3 *src,
5103 NT_PRINTER_DRIVER_INFO_LEVEL_3 *drv )
5105 bool in_use = False;
5111 /* check each file. Remove it from the src structure if it overlaps */
5113 if ( drv_file_in_use(src->driverpath, drv) ) {
5115 DEBUG(10,("Removing driverfile [%s] from list\n", src->driverpath));
5116 fstrcpy( src->driverpath, "" );
5119 if ( drv_file_in_use(src->datafile, drv) ) {
5121 DEBUG(10,("Removing datafile [%s] from list\n", src->datafile));
5122 fstrcpy( src->datafile, "" );
5125 if ( drv_file_in_use(src->configfile, drv) ) {
5127 DEBUG(10,("Removing configfile [%s] from list\n", src->configfile));
5128 fstrcpy( src->configfile, "" );
5131 if ( drv_file_in_use(src->helpfile, drv) ) {
5133 DEBUG(10,("Removing helpfile [%s] from list\n", src->helpfile));
5134 fstrcpy( src->helpfile, "" );
5137 /* are there any dependentfiles to examine? */
5139 if ( !src->dependentfiles )
5142 while ( *src->dependentfiles[i] ) {
5143 if ( drv_file_in_use(src->dependentfiles[i], drv) ) {
5145 DEBUG(10,("Removing [%s] from dependent file list\n", src->dependentfiles[i]));
5146 trim_dependent_file( src->dependentfiles, i );
5154 /****************************************************************************
5155 Determine whether or not a particular driver files are currently being
5156 used by any other driver.
5158 Return value is True if any files were in use by other drivers
5159 and False otherwise.
5161 Upon return, *info has been modified to only contain the driver files
5162 which are not in use
5166 This needs to check all drivers to ensure that all files in use
5167 have been removed from *info, not just the ones in the first
5169 ****************************************************************************/
5171 bool printer_driver_files_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
5176 fstring *list = NULL;
5177 NT_PRINTER_DRIVER_INFO_LEVEL driver;
5178 bool in_use = false;
5183 version = info->cversion;
5185 /* loop over all driver versions */
5187 DEBUG(5,("printer_driver_files_in_use: Beginning search through ntdrivers.tdb...\n"));
5189 /* get the list of drivers */
5192 ndrivers = get_ntdrivers(&list, info->environment, version);
5194 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
5195 ndrivers, info->environment, version));
5197 /* check each driver for overlap in files */
5199 for (i=0; i<ndrivers; i++) {
5200 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
5202 ZERO_STRUCT(driver);
5204 if ( !W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, list[i], info->environment, version)) ) {
5209 /* check if d2 uses any files from d1 */
5210 /* only if this is a different driver than the one being deleted */
5212 if ( !strequal(info->name, driver.info_3->name) ) {
5213 if ( trim_overlap_drv_files(info, driver.info_3) ) {
5214 /* mz: Do not instantly return -
5215 * we need to ensure this file isn't
5216 * also in use by other drivers. */
5221 free_a_printer_driver(driver, 3);
5226 DEBUG(5,("printer_driver_files_in_use: Completed search through ntdrivers.tdb...\n"));
5228 driver.info_3 = info;
5230 if ( DEBUGLEVEL >= 20 )
5231 dump_a_printer_driver( driver, 3 );
5236 /****************************************************************************
5237 Actually delete the driver files. Make sure that
5238 printer_driver_files_in_use() return False before calling
5240 ****************************************************************************/
5242 static bool delete_driver_files(struct pipes_struct *rpc_pipe,
5243 NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3)
5248 connection_struct *conn;
5252 fstring printdollar;
5253 int printdollar_snum;
5259 DEBUG(6,("delete_driver_files: deleting driver [%s] - version [%d]\n", info_3->name, info_3->cversion));
5261 fstrcpy(printdollar, "print$");
5263 printdollar_snum = find_service(printdollar);
5264 if (printdollar_snum == -1) {
5268 nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
5269 lp_pathname(printdollar_snum),
5270 rpc_pipe->server_info, &oldcwd);
5271 if (!NT_STATUS_IS_OK(nt_status)) {
5272 DEBUG(0,("delete_driver_files: create_conn_struct "
5273 "returned %s\n", nt_errstr(nt_status)));
5277 if ( !CAN_WRITE(conn) ) {
5278 DEBUG(3,("delete_driver_files: Cannot delete print driver when [print$] is read-only\n"));
5282 /* now delete the files; must strip the '\print$' string from
5285 if ( *info_3->driverpath ) {
5286 if ( (s = strchr( &info_3->driverpath[1], '\\' )) != NULL ) {
5288 driver_unix_convert(conn,file,&st);
5289 DEBUG(10,("deleting driverfile [%s]\n", s));
5290 unlink_internals(conn, NULL, 0, file, False);
5294 if ( *info_3->configfile ) {
5295 if ( (s = strchr( &info_3->configfile[1], '\\' )) != NULL ) {
5297 driver_unix_convert(conn,file,&st);
5298 DEBUG(10,("deleting configfile [%s]\n", s));
5299 unlink_internals(conn, NULL, 0, file, False);
5303 if ( *info_3->datafile ) {
5304 if ( (s = strchr( &info_3->datafile[1], '\\' )) != NULL ) {
5306 driver_unix_convert(conn,file,&st);
5307 DEBUG(10,("deleting datafile [%s]\n", s));
5308 unlink_internals(conn, NULL, 0, file, False);
5312 if ( *info_3->helpfile ) {
5313 if ( (s = strchr( &info_3->helpfile[1], '\\' )) != NULL ) {
5315 driver_unix_convert(conn,file,&st);
5316 DEBUG(10,("deleting helpfile [%s]\n", s));
5317 unlink_internals(conn, NULL, 0, file, False);
5321 /* check if we are done removing files */
5323 if ( info_3->dependentfiles ) {
5324 while ( info_3->dependentfiles[i][0] ) {
5327 /* bypass the "\print$" portion of the path */
5329 if ( (p = strchr( info_3->dependentfiles[i]+1, '\\' )) != NULL ) {
5331 driver_unix_convert(conn,file,&st);
5332 DEBUG(10,("deleting dependent file [%s]\n", file));
5333 unlink_internals(conn, NULL, 0, file, False);
5345 vfs_ChDir(conn, oldcwd);
5346 conn_free_internal(conn);
5351 /****************************************************************************
5352 Remove a printer driver from the TDB. This assumes that the the driver was
5353 previously looked up.
5354 ***************************************************************************/
5356 WERROR delete_printer_driver(struct pipes_struct *rpc_pipe,
5357 NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3,
5358 uint32 version, bool delete_files )
5363 NT_PRINTER_DRIVER_INFO_LEVEL ctr;
5365 /* delete the tdb data first */
5367 arch = get_short_archi(info_3->environment);
5369 return WERR_UNKNOWN_PRINTER_DRIVER;
5371 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
5372 arch, version, info_3->name) < 0) {
5376 DEBUG(5,("delete_printer_driver: key = [%s] delete_files = %s\n",
5377 key, delete_files ? "TRUE" : "FALSE" ));
5379 ctr.info_3 = info_3;
5380 dump_a_printer_driver( ctr, 3 );
5382 /* check if the driver actually exists for this environment */
5384 dbuf = tdb_fetch_bystring( tdb_drivers, key );
5386 DEBUG(8,("delete_printer_driver: Driver unknown [%s]\n", key));
5388 return WERR_UNKNOWN_PRINTER_DRIVER;
5391 SAFE_FREE( dbuf.dptr );
5393 /* ok... the driver exists so the delete should return success */
5395 if (tdb_delete_bystring(tdb_drivers, key) == -1) {
5396 DEBUG (0,("delete_printer_driver: fail to delete %s!\n", key));
5398 return WERR_ACCESS_DENIED;
5402 * now delete any associated files if delete_files == True
5403 * even if this part failes, we return succes because the
5404 * driver doesn not exist any more
5408 delete_driver_files(rpc_pipe, info_3);
5410 DEBUG(5,("delete_printer_driver: driver delete successful [%s]\n", key));
5416 /****************************************************************************
5417 Store a security desc for a printer.
5418 ****************************************************************************/
5420 WERROR nt_printing_setsec(const char *sharename, SEC_DESC_BUF *secdesc_ctr)
5422 SEC_DESC_BUF *new_secdesc_ctr = NULL;
5423 SEC_DESC_BUF *old_secdesc_ctr = NULL;
5425 bool prs_init_done = false;
5426 TALLOC_CTX *mem_ctx = NULL;
5430 mem_ctx = talloc_init("nt_printing_setsec");
5431 if (mem_ctx == NULL)
5434 /* The old owner and group sids of the security descriptor are not
5435 present when new ACEs are added or removed by changing printer
5436 permissions through NT. If they are NULL in the new security
5437 descriptor then copy them over from the old one. */
5439 if (!secdesc_ctr->sd->owner_sid || !secdesc_ctr->sd->group_sid) {
5440 DOM_SID *owner_sid, *group_sid;
5441 SEC_ACL *dacl, *sacl;
5442 SEC_DESC *psd = NULL;
5445 if (!nt_printing_getsec(mem_ctx, sharename, &old_secdesc_ctr)) {
5446 status = WERR_NOMEM;
5450 /* Pick out correct owner and group sids */
5452 owner_sid = secdesc_ctr->sd->owner_sid ?
5453 secdesc_ctr->sd->owner_sid :
5454 old_secdesc_ctr->sd->owner_sid;
5456 group_sid = secdesc_ctr->sd->group_sid ?
5457 secdesc_ctr->sd->group_sid :
5458 old_secdesc_ctr->sd->group_sid;
5460 dacl = secdesc_ctr->sd->dacl ?
5461 secdesc_ctr->sd->dacl :
5462 old_secdesc_ctr->sd->dacl;
5464 sacl = secdesc_ctr->sd->sacl ?
5465 secdesc_ctr->sd->sacl :
5466 old_secdesc_ctr->sd->sacl;
5468 /* Make a deep copy of the security descriptor */
5470 psd = make_sec_desc(mem_ctx, secdesc_ctr->sd->revision, secdesc_ctr->sd->type,
5471 owner_sid, group_sid,
5477 status = WERR_NOMEM;
5481 new_secdesc_ctr = make_sec_desc_buf(mem_ctx, size, psd);
5484 if (!new_secdesc_ctr) {
5485 new_secdesc_ctr = secdesc_ctr;
5488 /* Store the security descriptor in a tdb */
5491 (uint32_t)ndr_size_security_descriptor(new_secdesc_ctr->sd,
5493 + sizeof(SEC_DESC_BUF), mem_ctx, MARSHALL) ) {
5494 status = WERR_NOMEM;
5499 prs_init_done = true;
5501 if (!sec_io_desc_buf("nt_printing_setsec", &new_secdesc_ctr,
5503 status = WERR_BADFUNC;
5507 kbuf = make_printers_secdesc_tdbkey(mem_ctx, sharename );
5509 if (tdb_prs_store(tdb_printers, kbuf, &ps)==0) {
5512 DEBUG(1,("Failed to store secdesc for %s\n", sharename));
5513 status = WERR_BADFUNC;
5516 /* Free malloc'ed memory */
5520 if (prs_init_done) {
5524 talloc_destroy(mem_ctx);
5528 /****************************************************************************
5529 Construct a default security descriptor buffer for a printer.
5530 ****************************************************************************/
5532 static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
5534 SEC_ACE ace[5]; /* max number of ace entries */
5537 SEC_ACL *psa = NULL;
5538 SEC_DESC_BUF *sdb = NULL;
5539 SEC_DESC *psd = NULL;
5543 /* Create an ACE where Everyone is allowed to print */
5545 sa = PRINTER_ACE_PRINT;
5546 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
5547 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5549 /* Add the domain admins group if we are a DC */
5552 DOM_SID domadmins_sid;
5554 sid_copy(&domadmins_sid, get_global_sam_sid());
5555 sid_append_rid(&domadmins_sid, DOMAIN_GROUP_RID_ADMINS);
5557 sa = PRINTER_ACE_FULL_CONTROL;
5558 init_sec_ace(&ace[i++], &domadmins_sid,
5559 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5560 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5561 init_sec_ace(&ace[i++], &domadmins_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5562 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5564 else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
5565 sid_append_rid(&adm_sid, DOMAIN_USER_RID_ADMIN);
5567 sa = PRINTER_ACE_FULL_CONTROL;
5568 init_sec_ace(&ace[i++], &adm_sid,
5569 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5570 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5571 init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5572 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5575 /* add BUILTIN\Administrators as FULL CONTROL */
5577 sa = PRINTER_ACE_FULL_CONTROL;
5578 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5579 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5580 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5581 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5582 SEC_ACE_TYPE_ACCESS_ALLOWED,
5583 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5585 /* Make the security descriptor owned by the BUILTIN\Administrators */
5587 /* The ACL revision number in rpc_secdesc.h differs from the one
5588 created by NT when setting ACE entries in printer
5589 descriptors. NT4 complains about the property being edited by a
5592 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) != NULL) {
5593 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
5594 &global_sid_Builtin_Administrators,
5595 &global_sid_Builtin_Administrators,
5596 NULL, psa, &sd_size);
5600 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
5604 sdb = make_sec_desc_buf(ctx, sd_size, psd);
5606 DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
5607 (unsigned int)sd_size));
5612 /****************************************************************************
5613 Get a security desc for a printer.
5614 ****************************************************************************/
5616 bool nt_printing_getsec(TALLOC_CTX *ctx, const char *sharename, SEC_DESC_BUF **secdesc_ctr)
5622 if (strlen(sharename) > 2 && (temp = strchr(sharename + 2, '\\'))) {
5623 sharename = temp + 1;
5628 /* Fetch security descriptor from tdb */
5630 kbuf = make_printers_secdesc_tdbkey(ctx, sharename );
5632 if (tdb_prs_fetch(tdb_printers, kbuf, &ps, ctx)!=0 ||
5633 !sec_io_desc_buf("nt_printing_getsec", secdesc_ctr, &ps, 1)) {
5637 DEBUG(4,("using default secdesc for %s\n", sharename));
5639 if (!(*secdesc_ctr = construct_default_printer_sdb(ctx))) {
5643 /* Save default security descriptor for later */
5645 if (!prs_init(&ps, (uint32_t)ndr_size_security_descriptor((*secdesc_ctr)->sd, NULL, 0) +
5646 sizeof(SEC_DESC_BUF), ctx, MARSHALL))
5649 if (sec_io_desc_buf("nt_printing_getsec", secdesc_ctr, &ps, 1)) {
5650 tdb_prs_store(tdb_printers, kbuf, &ps);
5660 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
5661 this security descriptor has been created when winbindd was
5662 down. Take ownership of security descriptor. */
5664 if (sid_equal((*secdesc_ctr)->sd->owner_sid, &global_sid_World)) {
5667 /* Change sd owner to workgroup administrator */
5669 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
5670 SEC_DESC_BUF *new_secdesc_ctr = NULL;
5671 SEC_DESC *psd = NULL;
5676 sid_append_rid(&owner_sid, DOMAIN_USER_RID_ADMIN);
5678 psd = make_sec_desc(ctx, (*secdesc_ctr)->sd->revision, (*secdesc_ctr)->sd->type,
5680 (*secdesc_ctr)->sd->group_sid,
5681 (*secdesc_ctr)->sd->sacl,
5682 (*secdesc_ctr)->sd->dacl,
5689 new_secdesc_ctr = make_sec_desc_buf(ctx, size, psd);
5690 if (!new_secdesc_ctr) {
5694 /* Swap with other one */
5696 *secdesc_ctr = new_secdesc_ctr;
5700 nt_printing_setsec(sharename, *secdesc_ctr);
5704 if (DEBUGLEVEL >= 10) {
5705 SEC_ACL *the_acl = (*secdesc_ctr)->sd->dacl;
5708 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5709 sharename, the_acl->num_aces));
5711 for (i = 0; i < the_acl->num_aces; i++) {
5712 DEBUG(10, ("%s %d %d 0x%08x\n",
5713 sid_string_dbg(&the_acl->aces[i].trustee),
5714 the_acl->aces[i].type, the_acl->aces[i].flags,
5715 the_acl->aces[i].access_mask));
5724 1: level not implemented
5725 2: file doesn't exist
5726 3: can't allocate memory
5727 4: can't free memory
5728 5: non existant struct
5732 A printer and a printer driver are 2 different things.
5733 NT manages them separatelly, Samba does the same.
5734 Why ? Simply because it's easier and it makes sense !
5736 Now explanation: You have 3 printers behind your samba server,
5737 2 of them are the same make and model (laser A and B). But laser B
5738 has an 3000 sheet feeder and laser A doesn't such an option.
5739 Your third printer is an old dot-matrix model for the accounting :-).
5741 If the /usr/local/samba/lib directory (default dir), you will have
5742 5 files to describe all of this.
5744 3 files for the printers (1 by printer):
5747 NTprinter_accounting
5748 2 files for the drivers (1 for the laser and 1 for the dot matrix)
5749 NTdriver_printer model X
5750 NTdriver_printer model Y
5752 jfm: I should use this comment for the text file to explain
5753 same thing for the forms BTW.
5754 Je devrais mettre mes commentaires en francais, ca serait mieux :-)
5758 /* Convert generic access rights to printer object specific access rights.
5759 It turns out that NT4 security descriptors use generic access rights and
5760 NT5 the object specific ones. */
5762 void map_printer_permissions(SEC_DESC *sd)
5766 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5767 se_map_generic(&sd->dacl->aces[i].access_mask,
5768 &printer_generic_mapping);
5772 void map_job_permissions(SEC_DESC *sd)
5776 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5777 se_map_generic(&sd->dacl->aces[i].access_mask,
5778 &job_generic_mapping);
5783 /****************************************************************************
5784 Check a user has permissions to perform the given operation. We use the
5785 permission constants defined in include/rpc_spoolss.h to check the various
5786 actions we perform when checking printer access.
5788 PRINTER_ACCESS_ADMINISTER:
5789 print_queue_pause, print_queue_resume, update_printer_sec,
5790 update_printer, spoolss_addprinterex_level_2,
5791 _spoolss_setprinterdata
5796 JOB_ACCESS_ADMINISTER:
5797 print_job_delete, print_job_pause, print_job_resume,
5800 Try access control in the following order (for performance reasons):
5801 1) root ans SE_PRINT_OPERATOR can do anything (easy check)
5802 2) check security descriptor (bit comparisons in memory)
5803 3) "printer admins" (may result in numerous calls to winbind)
5805 ****************************************************************************/
5806 bool print_access_check(struct auth_serversupplied_info *server_info, int snum,
5809 SEC_DESC_BUF *secdesc = NULL;
5810 uint32 access_granted;
5813 TALLOC_CTX *mem_ctx = NULL;
5814 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5816 /* If user is NULL then use the current_user structure */
5818 /* Always allow root or SE_PRINT_OPERATROR to do anything */
5820 if (server_info->utok.uid == 0
5821 || user_has_privileges(server_info->ptok, &se_printop ) ) {
5825 /* Get printer name */
5827 pname = PRINTERNAME(snum);
5829 if (!pname || !*pname) {
5834 /* Get printer security descriptor */
5836 if(!(mem_ctx = talloc_init("print_access_check"))) {
5841 if (!nt_printing_getsec(mem_ctx, pname, &secdesc)) {
5842 talloc_destroy(mem_ctx);
5847 if (access_type == JOB_ACCESS_ADMINISTER) {
5848 SEC_DESC_BUF *parent_secdesc = secdesc;
5850 /* Create a child security descriptor to check permissions
5851 against. This is because print jobs are child objects
5852 objects of a printer. */
5854 status = se_create_child_secdesc_buf(mem_ctx, &secdesc, parent_secdesc->sd, False);
5856 if (!NT_STATUS_IS_OK(status)) {
5857 talloc_destroy(mem_ctx);
5858 errno = map_errno_from_nt_status(status);
5862 map_job_permissions(secdesc->sd);
5864 map_printer_permissions(secdesc->sd);
5868 status = se_access_check(secdesc->sd, server_info->ptok, access_type,
5871 DEBUG(4, ("access check was %s\n", NT_STATUS_IS_OK(status) ? "SUCCESS" : "FAILURE"));
5873 /* see if we need to try the printer admin list */
5875 if ((access_granted == 0) &&
5876 (token_contains_name_in_list(uidtoname(server_info->utok.uid),
5877 NULL, NULL, server_info->ptok,
5878 lp_printer_admin(snum)))) {
5879 talloc_destroy(mem_ctx);
5883 talloc_destroy(mem_ctx);
5885 if (!NT_STATUS_IS_OK(status)) {
5889 return NT_STATUS_IS_OK(status);
5892 /****************************************************************************
5893 Check the time parameters allow a print operation.
5894 *****************************************************************************/
5896 bool print_time_access_check(const char *servicename)
5898 NT_PRINTER_INFO_LEVEL *printer = NULL;
5900 time_t now = time(NULL);
5904 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
5907 if (printer->info_2->starttime == 0 && printer->info_2->untiltime == 0)
5911 mins = (uint32)t->tm_hour*60 + (uint32)t->tm_min;
5913 if (mins >= printer->info_2->starttime && mins <= printer->info_2->untiltime)
5916 free_a_printer(&printer, 2);
5924 /****************************************************************************
5925 Fill in the servername sent in the _spoolss_open_printer_ex() call
5926 ****************************************************************************/
5928 char* get_server_name( Printer_entry *printer )
5930 return printer->servername;