s3:smbd: close the low level fd of the base_fsp, if the file was created
[Samba.git] / source / printing / nt_printing.c
blob0468769f9ba20e9568973d9a091c374b8f7e877e
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2000,
5 * Copyright (C) Jean François Micouleau 1998-2000.
6 * Copyright (C) Gerald Carter 2002-2005.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 extern struct current_user current_user;
26 static TDB_CONTEXT *tdb_forms; /* used for forms files */
27 static TDB_CONTEXT *tdb_drivers; /* used for driver files */
28 static TDB_CONTEXT *tdb_printers; /* used for printers files */
30 #define FORMS_PREFIX "FORMS/"
31 #define DRIVERS_PREFIX "DRIVERS/"
32 #define DRIVER_INIT_PREFIX "DRIVER_INIT/"
33 #define PRINTERS_PREFIX "PRINTERS/"
34 #define SECDESC_PREFIX "SECDESC/"
35 #define GLOBAL_C_SETPRINTER "GLOBALS/c_setprinter"
37 #define NTDRIVERS_DATABASE_VERSION_1 1
38 #define NTDRIVERS_DATABASE_VERSION_2 2
39 #define NTDRIVERS_DATABASE_VERSION_3 3 /* little endian version of v2 */
40 #define NTDRIVERS_DATABASE_VERSION_4 4 /* fix generic bits in security descriptors */
41 #define NTDRIVERS_DATABASE_VERSION_5 5 /* normalize keys in ntprinters.tdb */
43 /* Map generic permissions to printer object specific permissions */
45 const struct generic_mapping printer_generic_mapping = {
46 PRINTER_READ,
47 PRINTER_WRITE,
48 PRINTER_EXECUTE,
49 PRINTER_ALL_ACCESS
52 const struct standard_mapping printer_std_mapping = {
53 PRINTER_READ,
54 PRINTER_WRITE,
55 PRINTER_EXECUTE,
56 PRINTER_ALL_ACCESS
59 /* Map generic permissions to print server object specific permissions */
61 const struct generic_mapping printserver_generic_mapping = {
62 SERVER_READ,
63 SERVER_WRITE,
64 SERVER_EXECUTE,
65 SERVER_ALL_ACCESS
68 const struct generic_mapping printserver_std_mapping = {
69 SERVER_READ,
70 SERVER_WRITE,
71 SERVER_EXECUTE,
72 SERVER_ALL_ACCESS
75 /* Map generic permissions to job object specific permissions */
77 const struct generic_mapping job_generic_mapping = {
78 JOB_READ,
79 JOB_WRITE,
80 JOB_EXECUTE,
81 JOB_ALL_ACCESS
84 /* We need one default form to support our default printer. Msoft adds the
85 forms it wants and in the ORDER it wants them (note: DEVMODE papersize is an
86 array index). Letter is always first, so (for the current code) additions
87 always put things in the correct order. */
88 static const nt_forms_struct default_forms[] = {
89 {"Letter",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
90 {"Letter Small",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
91 {"Tabloid",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
92 {"Ledger",0x1,0x696b8,0x44368,0x0,0x0,0x696b8,0x44368},
93 {"Legal",0x1,0x34b5c,0x56d10,0x0,0x0,0x34b5c,0x56d10},
94 {"Statement",0x1,0x221b4,0x34b5c,0x0,0x0,0x221b4,0x34b5c},
95 {"Executive",0x1,0x2cf56,0x411cc,0x0,0x0,0x2cf56,0x411cc},
96 {"A3",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
97 {"A4",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
98 {"A4 Small",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
99 {"A5",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
100 {"B4 (JIS)",0x1,0x3ebe8,0x58de0,0x0,0x0,0x3ebe8,0x58de0},
101 {"B5 (JIS)",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
102 {"Folio",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
103 {"Quarto",0x1,0x347d8,0x43238,0x0,0x0,0x347d8,0x43238},
104 {"10x14",0x1,0x3e030,0x56d10,0x0,0x0,0x3e030,0x56d10},
105 {"11x17",0x1,0x44368,0x696b8,0x0,0x0,0x44368,0x696b8},
106 {"Note",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
107 {"Envelope #9",0x1,0x18079,0x37091,0x0,0x0,0x18079,0x37091},
108 {"Envelope #10",0x1,0x19947,0x3ae94,0x0,0x0,0x19947,0x3ae94},
109 {"Envelope #11",0x1,0x1be7c,0x40565,0x0,0x0,0x1be7c,0x40565},
110 {"Envelope #12",0x1,0x1d74a,0x44368,0x0,0x0,0x1d74a,0x44368},
111 {"Envelope #14",0x1,0x1f018,0x47504,0x0,0x0,0x1f018,0x47504},
112 {"C size sheet",0x1,0x696b8,0x886d0,0x0,0x0,0x696b8,0x886d0},
113 {"D size sheet",0x1,0x886d0,0xd2d70,0x0,0x0,0x886d0,0xd2d70},
114 {"E size sheet",0x1,0xd2d70,0x110da0,0x0,0x0,0xd2d70,0x110da0},
115 {"Envelope DL",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
116 {"Envelope C5",0x1,0x278d0,0x37e88,0x0,0x0,0x278d0,0x37e88},
117 {"Envelope C3",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
118 {"Envelope C4",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
119 {"Envelope C6",0x1,0x1bd50,0x278d0,0x0,0x0,0x1bd50,0x278d0},
120 {"Envelope C65",0x1,0x1bd50,0x37e88,0x0,0x0,0x1bd50,0x37e88},
121 {"Envelope B4",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
122 {"Envelope B5",0x1,0x2af80,0x3d090,0x0,0x0,0x2af80,0x3d090},
123 {"Envelope B6",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
124 {"Envelope",0x1,0x1adb0,0x38270,0x0,0x0,0x1adb0,0x38270},
125 {"Envelope Monarch",0x1,0x18079,0x2e824,0x0,0x0,0x18079,0x2e824},
126 {"6 3/4 Envelope",0x1,0x167ab,0x284ec,0x0,0x0,0x167ab,0x284ec},
127 {"US Std Fanfold",0x1,0x5c3e1,0x44368,0x0,0x0,0x5c3e1,0x44368},
128 {"German Std Fanfold",0x1,0x34b5c,0x4a6a0,0x0,0x0,0x34b5c,0x4a6a0},
129 {"German Legal Fanfold",0x1,0x34b5c,0x509d8,0x0,0x0,0x34b5c,0x509d8},
130 {"B4 (ISO)",0x1,0x3d090,0x562e8,0x0,0x0,0x3d090,0x562e8},
131 {"Japanese Postcard",0x1,0x186a0,0x24220,0x0,0x0,0x186a0,0x24220},
132 {"9x11",0x1,0x37cf8,0x44368,0x0,0x0,0x37cf8,0x44368},
133 {"10x11",0x1,0x3e030,0x44368,0x0,0x0,0x3e030,0x44368},
134 {"15x11",0x1,0x5d048,0x44368,0x0,0x0,0x5d048,0x44368},
135 {"Envelope Invite",0x1,0x35b60,0x35b60,0x0,0x0,0x35b60,0x35b60},
136 {"Reserved48",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
137 {"Reserved49",0x1,0x1,0x1,0x0,0x0,0x1,0x1},
138 {"Letter Extra",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
139 {"Legal Extra",0x1,0x3ae94,0x5d048,0x0,0x0,0x3ae94,0x5d048},
140 {"Tabloid Extra",0x1,0x4a6a0,0x6f9f0,0x0,0x0,0x4a6a0,0x6f9f0},
141 {"A4 Extra",0x1,0x397c2,0x4eb16,0x0,0x0,0x397c2,0x4eb16},
142 {"Letter Transverse",0x1,0x34b5c,0x44368,0x0,0x0,0x34b5c,0x44368},
143 {"A4 Transverse",0x1,0x33450,0x48828,0x0,0x0,0x33450,0x48828},
144 {"Letter Extra Transverse",0x1,0x3ae94,0x4a6a0,0x0,0x0,0x3ae94,0x4a6a0},
145 {"Super A",0x1,0x376b8,0x56ea0,0x0,0x0,0x376b8,0x56ea0},
146 {"Super B",0x1,0x4a768,0x76e58,0x0,0x0,0x4a768,0x76e58},
147 {"Letter Plus",0x1,0x34b5c,0x4eb16,0x0,0x0,0x34b5c,0x4eb16},
148 {"A4 Plus",0x1,0x33450,0x50910,0x0,0x0,0x33450,0x50910},
149 {"A5 Transverse",0x1,0x24220,0x33450,0x0,0x0,0x24220,0x33450},
150 {"B5 (JIS) Transverse",0x1,0x2c6f0,0x3ebe8,0x0,0x0,0x2c6f0,0x3ebe8},
151 {"A3 Extra",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
152 {"A5 Extra",0x1,0x2a7b0,0x395f8,0x0,0x0,0x2a7b0,0x395f8},
153 {"B5 (ISO) Extra",0x1,0x31128,0x43620,0x0,0x0,0x31128,0x43620},
154 {"A2",0x1,0x668a0,0x91050,0x0,0x0,0x668a0,0x91050},
155 {"A3 Transverse",0x1,0x48828,0x668a0,0x0,0x0,0x48828,0x668a0},
156 {"A3 Extra Transverse",0x1,0x4e9d0,0x6ca48,0x0,0x0,0x4e9d0,0x6ca48},
157 {"Japanese Double Postcard",0x1,0x30d40,0x24220,0x0,0x0,0x30d40,0x24220},
158 {"A6",0x1,0x19a28,0x24220,0x0,0x0,0x19a28,0x24220},
159 {"Japanese Envelope Kaku #2",0x1,0x3a980,0x510e0,0x0,0x0,0x3a980,0x510e0},
160 {"Japanese Envelope Kaku #3",0x1,0x34bc0,0x43a08,0x0,0x0,0x34bc0,0x43a08},
161 {"Japanese Envelope Chou #3",0x1,0x1d4c0,0x395f8,0x0,0x0,0x1d4c0,0x395f8},
162 {"Japanese Envelope Chou #4",0x1,0x15f90,0x320c8,0x0,0x0,0x15f90,0x320c8},
163 {"Letter Rotated",0x1,0x44368,0x34b5c,0x0,0x0,0x44368,0x34b5c},
164 {"A3 Rotated",0x1,0x668a0,0x48828,0x0,0x0,0x668a0,0x48828},
165 {"A4 Rotated",0x1,0x48828,0x33450,0x0,0x0,0x48828,0x33450},
166 {"A5 Rotated",0x1,0x33450,0x24220,0x0,0x0,0x33450,0x24220},
167 {"B4 (JIS) Rotated",0x1,0x58de0,0x3ebe8,0x0,0x0,0x58de0,0x3ebe8},
168 {"B5 (JIS) Rotated",0x1,0x3ebe8,0x2c6f0,0x0,0x0,0x3ebe8,0x2c6f0},
169 {"Japanese Postcard Rotated",0x1,0x24220,0x186a0,0x0,0x0,0x24220,0x186a0},
170 {"Double Japan Postcard Rotated",0x1,0x24220,0x30d40,0x0,0x0,0x24220,0x30d40},
171 {"A6 Rotated",0x1,0x24220,0x19a28,0x0,0x0,0x24220,0x19a28},
172 {"Japan Envelope Kaku #2 Rotated",0x1,0x510e0,0x3a980,0x0,0x0,0x510e0,0x3a980},
173 {"Japan Envelope Kaku #3 Rotated",0x1,0x43a08,0x34bc0,0x0,0x0,0x43a08, 0x34bc0},
174 {"Japan Envelope Chou #3 Rotated",0x1,0x395f8,0x1d4c0,0x0,0x0,0x395f8,0x1d4c0},
175 {"Japan Envelope Chou #4 Rotated",0x1,0x320c8,0x15f90,0x0,0x0,0x320c8,0x15f90},
176 {"B6 (JIS)",0x1,0x1f400,0x2c6f0,0x0,0x0,0x1f400,0x2c6f0},
177 {"B6 (JIS) Rotated",0x1,0x2c6f0,0x1f400,0x0,0x0,0x2c6f0,0x1f400},
178 {"12x11",0x1,0x4a724,0x443e1,0x0,0x0,0x4a724,0x443e1},
179 {"Japan Envelope You #4",0x1,0x19a28,0x395f8,0x0,0x0,0x19a28,0x395f8},
180 {"Japan Envelope You #4 Rotated",0x1,0x395f8,0x19a28,0x0,0x0,0x395f8,0x19a28},
181 {"PRC 16K",0x1,0x2de60,0x3f7a0,0x0,0x0,0x2de60,0x3f7a0},
182 {"PRC 32K",0x1,0x1fbd0,0x2cec0,0x0,0x0,0x1fbd0,0x2cec0},
183 {"PRC 32K(Big)",0x1,0x222e0,0x318f8,0x0,0x0,0x222e0,0x318f8},
184 {"PRC Envelope #1",0x1,0x18e70,0x28488,0x0,0x0,0x18e70,0x28488},
185 {"PRC Envelope #2",0x1,0x18e70,0x2af80,0x0,0x0,0x18e70,0x2af80},
186 {"PRC Envelope #3",0x1,0x1e848,0x2af80,0x0,0x0,0x1e848,0x2af80},
187 {"PRC Envelope #4",0x1,0x1adb0,0x32c80,0x0,0x0,0x1adb0,0x32c80},
188 {"PRC Envelope #5",0x1,0x1adb0,0x35b60,0x0,0x0,0x1adb0,0x35b60},
189 {"PRC Envelope #6",0x1,0x1d4c0,0x38270,0x0,0x0,0x1d4c0,0x38270},
190 {"PRC Envelope #7",0x1,0x27100,0x38270,0x0,0x0,0x27100,0x38270},
191 {"PRC Envelope #8",0x1,0x1d4c0,0x4b708,0x0,0x0,0x1d4c0,0x4b708},
192 {"PRC Envelope #9",0x1,0x37e88,0x4f1a0,0x0,0x0,0x37e88,0x4f1a0},
193 {"PRC Envelope #10",0x1,0x4f1a0,0x6fd10,0x0,0x0,0x4f1a0,0x6fd10},
194 {"PRC 16K Rotated",0x1,0x3f7a0,0x2de60,0x0,0x0,0x3f7a0,0x2de60},
195 {"PRC 32K Rotated",0x1,0x2cec0,0x1fbd0,0x0,0x0,0x2cec0,0x1fbd0},
196 {"PRC 32K(Big) Rotated",0x1,0x318f8,0x222e0,0x0,0x0,0x318f8,0x222e0},
197 {"PRC Envelope #1 Rotated",0x1,0x28488,0x18e70,0x0,0x0,0x28488,0x18e70},
198 {"PRC Envelope #2 Rotated",0x1,0x2af80,0x18e70,0x0,0x0,0x2af80,0x18e70},
199 {"PRC Envelope #3 Rotated",0x1,0x2af80,0x1e848,0x0,0x0,0x2af80,0x1e848},
200 {"PRC Envelope #4 Rotated",0x1,0x32c80,0x1adb0,0x0,0x0,0x32c80,0x1adb0},
201 {"PRC Envelope #5 Rotated",0x1,0x35b60,0x1adb0,0x0,0x0,0x35b60,0x1adb0},
202 {"PRC Envelope #6 Rotated",0x1,0x38270,0x1d4c0,0x0,0x0,0x38270,0x1d4c0},
203 {"PRC Envelope #7 Rotated",0x1,0x38270,0x27100,0x0,0x0,0x38270,0x27100},
204 {"PRC Envelope #8 Rotated",0x1,0x4b708,0x1d4c0,0x0,0x0,0x4b708,0x1d4c0},
205 {"PRC Envelope #9 Rotated",0x1,0x4f1a0,0x37e88,0x0,0x0,0x4f1a0,0x37e88},
206 {"PRC Envelope #10 Rotated",0x1,0x6fd10,0x4f1a0,0x0,0x0,0x6fd10,0x4f1a0}
209 struct table_node {
210 const char *long_archi;
211 const char *short_archi;
212 int version;
215 #define SPL_ARCH_WIN40 "WIN40"
216 #define SPL_ARCH_W32X86 "W32X86"
217 #define SPL_ARCH_W32MIPS "W32MIPS"
218 #define SPL_ARCH_W32ALPHA "W32ALPHA"
219 #define SPL_ARCH_W32PPC "W32PPC"
220 #define SPL_ARCH_IA64 "IA64"
221 #define SPL_ARCH_X64 "x64"
223 static const struct table_node archi_table[]= {
225 {"Windows 4.0", SPL_ARCH_WIN40, 0 },
226 {"Windows NT x86", SPL_ARCH_W32X86, 2 },
227 {"Windows NT R4000", SPL_ARCH_W32MIPS, 2 },
228 {"Windows NT Alpha_AXP", SPL_ARCH_W32ALPHA, 2 },
229 {"Windows NT PowerPC", SPL_ARCH_W32PPC, 2 },
230 {"Windows IA64", SPL_ARCH_IA64, 3 },
231 {"Windows x64", SPL_ARCH_X64, 3 },
232 {NULL, "", -1 }
236 /****************************************************************************
237 generate a new TDB_DATA key for storing a printer
238 ****************************************************************************/
240 static TDB_DATA make_printer_tdbkey(TALLOC_CTX *ctx, const char *sharename )
242 fstring share;
243 char *keystr = NULL;
244 TDB_DATA key;
246 fstrcpy(share, sharename);
247 strlower_m(share);
249 keystr = talloc_asprintf(ctx, "%s%s", PRINTERS_PREFIX, share);
250 key = string_term_tdb_data(keystr ? keystr : "");
252 return key;
255 /****************************************************************************
256 generate a new TDB_DATA key for storing a printer security descriptor
257 ****************************************************************************/
259 static TDB_DATA make_printers_secdesc_tdbkey(TALLOC_CTX *ctx,
260 const char* sharename )
262 fstring share;
263 char *keystr = NULL;
264 TDB_DATA key;
266 fstrcpy(share, sharename );
267 strlower_m(share);
269 keystr = talloc_asprintf(ctx, "%s%s", SECDESC_PREFIX, share);
270 key = string_term_tdb_data(keystr ? keystr : "");
272 return key;
275 /****************************************************************************
276 ****************************************************************************/
278 static bool upgrade_to_version_3(void)
280 TDB_DATA kbuf, newkey, dbuf;
282 DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
284 for (kbuf = tdb_firstkey(tdb_drivers); kbuf.dptr;
285 newkey = tdb_nextkey(tdb_drivers, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
287 dbuf = tdb_fetch(tdb_drivers, kbuf);
289 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
290 DEBUG(0,("upgrade_to_version_3:moving form\n"));
291 if (tdb_store(tdb_forms, kbuf, dbuf, TDB_REPLACE) != 0) {
292 SAFE_FREE(dbuf.dptr);
293 DEBUG(0,("upgrade_to_version_3: failed to move form. Error (%s).\n", tdb_errorstr(tdb_forms)));
294 return False;
296 if (tdb_delete(tdb_drivers, kbuf) != 0) {
297 SAFE_FREE(dbuf.dptr);
298 DEBUG(0,("upgrade_to_version_3: failed to delete form. Error (%s)\n", tdb_errorstr(tdb_drivers)));
299 return False;
303 if (strncmp((const char *)kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
304 DEBUG(0,("upgrade_to_version_3:moving printer\n"));
305 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
306 SAFE_FREE(dbuf.dptr);
307 DEBUG(0,("upgrade_to_version_3: failed to move printer. Error (%s)\n", tdb_errorstr(tdb_printers)));
308 return False;
310 if (tdb_delete(tdb_drivers, kbuf) != 0) {
311 SAFE_FREE(dbuf.dptr);
312 DEBUG(0,("upgrade_to_version_3: failed to delete printer. Error (%s)\n", tdb_errorstr(tdb_drivers)));
313 return False;
317 if (strncmp((const char *)kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
318 DEBUG(0,("upgrade_to_version_3:moving secdesc\n"));
319 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
320 SAFE_FREE(dbuf.dptr);
321 DEBUG(0,("upgrade_to_version_3: failed to move secdesc. Error (%s)\n", tdb_errorstr(tdb_printers)));
322 return False;
324 if (tdb_delete(tdb_drivers, kbuf) != 0) {
325 SAFE_FREE(dbuf.dptr);
326 DEBUG(0,("upgrade_to_version_3: failed to delete secdesc. Error (%s)\n", tdb_errorstr(tdb_drivers)));
327 return False;
331 SAFE_FREE(dbuf.dptr);
334 return True;
337 /*******************************************************************
338 Fix an issue with security descriptors. Printer sec_desc must
339 use more than the generic bits that were previously used
340 in <= 3.0.14a. They must also have a owner and group SID assigned.
341 Otherwise, any printers than have been migrated to a Windows
342 host using printmig.exe will not be accessible.
343 *******************************************************************/
345 static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
346 TDB_DATA data, void *state )
348 prs_struct ps;
349 SEC_DESC_BUF *sd_orig = NULL;
350 SEC_DESC_BUF *sd_new, *sd_store;
351 SEC_DESC *sec, *new_sec;
352 TALLOC_CTX *ctx = state;
353 int result, i;
354 uint32 sd_size;
355 size_t size_new_sec;
357 if (!data.dptr || data.dsize == 0) {
358 return 0;
361 if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) != 0 ) {
362 return 0;
365 /* upgrade the security descriptor */
367 ZERO_STRUCT( ps );
369 prs_init_empty( &ps, ctx, UNMARSHALL );
370 prs_give_memory( &ps, (char *)data.dptr, data.dsize, False );
372 if ( !sec_io_desc_buf( "sec_desc_upg_fn", &sd_orig, &ps, 1 ) ) {
373 /* delete bad entries */
374 DEBUG(0,("sec_desc_upg_fn: Failed to parse original sec_desc for %si. Deleting....\n",
375 (const char *)key.dptr ));
376 tdb_delete( tdb_printers, key );
377 prs_mem_free( &ps );
378 return 0;
381 if (!sd_orig) {
382 prs_mem_free( &ps );
383 return 0;
385 sec = sd_orig->sd;
387 /* is this even valid? */
389 if ( !sec->dacl ) {
390 prs_mem_free( &ps );
391 return 0;
394 /* update access masks */
396 for ( i=0; i<sec->dacl->num_aces; i++ ) {
397 switch ( sec->dacl->aces[i].access_mask ) {
398 case (GENERIC_READ_ACCESS | GENERIC_WRITE_ACCESS | GENERIC_EXECUTE_ACCESS):
399 sec->dacl->aces[i].access_mask = PRINTER_ACE_PRINT;
400 break;
402 case GENERIC_ALL_ACCESS:
403 sec->dacl->aces[i].access_mask = PRINTER_ACE_FULL_CONTROL;
404 break;
406 case READ_CONTROL_ACCESS:
407 sec->dacl->aces[i].access_mask = PRINTER_ACE_MANAGE_DOCUMENTS;
409 default: /* no change */
410 break;
414 /* create a new SEC_DESC with the appropriate owner and group SIDs */
416 new_sec = make_sec_desc( ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
417 &global_sid_Builtin_Administrators,
418 &global_sid_Builtin_Administrators,
419 NULL, NULL, &size_new_sec );
420 if (!new_sec) {
421 prs_mem_free( &ps );
422 return 0;
424 sd_new = make_sec_desc_buf( ctx, size_new_sec, new_sec );
425 if (!sd_new) {
426 prs_mem_free( &ps );
427 return 0;
430 if ( !(sd_store = sec_desc_merge( ctx, sd_new, sd_orig )) ) {
431 DEBUG(0,("sec_desc_upg_fn: Failed to update sec_desc for %s\n", key.dptr ));
432 prs_mem_free( &ps );
433 return 0;
436 prs_mem_free( &ps );
438 /* store it back */
440 sd_size = ndr_size_security_descriptor(sd_store->sd, 0)
441 + sizeof(SEC_DESC_BUF);
442 if ( !prs_init(&ps, sd_size, ctx, MARSHALL) ) {
443 DEBUG(0,("sec_desc_upg_fn: Failed to allocate prs memory for %s\n", key.dptr ));
444 return 0;
447 if ( !sec_io_desc_buf( "sec_desc_upg_fn", &sd_store, &ps, 1 ) ) {
448 DEBUG(0,("sec_desc_upg_fn: Failed to parse new sec_desc for %s\n", key.dptr ));
449 prs_mem_free( &ps );
450 return 0;
453 data.dptr = (uint8 *)prs_data_p( &ps );
454 data.dsize = sd_size;
456 result = tdb_store( tdb_printers, key, data, TDB_REPLACE );
458 prs_mem_free( &ps );
460 /* 0 to continue and non-zero to stop traversal */
462 return (result == -1);
465 /*******************************************************************
466 *******************************************************************/
468 static bool upgrade_to_version_4(void)
470 TALLOC_CTX *ctx;
471 int result;
473 DEBUG(0,("upgrade_to_version_4: upgrading printer security descriptors\n"));
475 if ( !(ctx = talloc_init( "upgrade_to_version_4" )) )
476 return False;
478 result = tdb_traverse( tdb_printers, sec_desc_upg_fn, ctx );
480 talloc_destroy( ctx );
482 return ( result != -1 );
485 /*******************************************************************
486 Fix an issue with security descriptors. Printer sec_desc must
487 use more than the generic bits that were previously used
488 in <= 3.0.14a. They must also have a owner and group SID assigned.
489 Otherwise, any printers than have been migrated to a Windows
490 host using printmig.exe will not be accessible.
491 *******************************************************************/
493 static int normalize_printers_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
494 TDB_DATA data, void *state )
496 TALLOC_CTX *ctx = talloc_tos();
497 TDB_DATA new_key;
499 if (!data.dptr || data.dsize == 0)
500 return 0;
502 /* upgrade printer records and security descriptors */
504 if ( strncmp((const char *) key.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX) ) == 0 ) {
505 new_key = make_printer_tdbkey(ctx, (const char *)key.dptr+strlen(PRINTERS_PREFIX) );
507 else if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) == 0 ) {
508 new_key = make_printers_secdesc_tdbkey(ctx, (const char *)key.dptr+strlen(SECDESC_PREFIX) );
510 else {
511 /* ignore this record */
512 return 0;
515 /* delete the original record and store under the normalized key */
517 if ( tdb_delete( the_tdb, key ) != 0 ) {
518 DEBUG(0,("normalize_printers_fn: tdb_delete for [%s] failed!\n",
519 key.dptr));
520 return 1;
523 if ( tdb_store( the_tdb, new_key, data, TDB_REPLACE) != 0 ) {
524 DEBUG(0,("normalize_printers_fn: failed to store new record for [%s]!\n",
525 key.dptr));
526 return 1;
529 return 0;
532 /*******************************************************************
533 *******************************************************************/
535 static bool upgrade_to_version_5(void)
537 TALLOC_CTX *ctx;
538 int result;
540 DEBUG(0,("upgrade_to_version_5: normalizing printer keys\n"));
542 if ( !(ctx = talloc_init( "upgrade_to_version_5" )) )
543 return False;
545 result = tdb_traverse( tdb_printers, normalize_printers_fn, NULL );
547 talloc_destroy( ctx );
549 return ( result != -1 );
552 /****************************************************************************
553 Open the NT printing tdbs. Done once before fork().
554 ****************************************************************************/
556 bool nt_printing_init(struct messaging_context *msg_ctx)
558 const char *vstring = "INFO/version";
559 WERROR win_rc;
560 int32 vers_id;
562 if ( tdb_drivers && tdb_printers && tdb_forms )
563 return True;
565 if (tdb_drivers)
566 tdb_close(tdb_drivers);
567 tdb_drivers = tdb_open_log(state_path("ntdrivers.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
568 if (!tdb_drivers) {
569 DEBUG(0,("nt_printing_init: Failed to open nt drivers database %s (%s)\n",
570 state_path("ntdrivers.tdb"), strerror(errno) ));
571 return False;
574 if (tdb_printers)
575 tdb_close(tdb_printers);
576 tdb_printers = tdb_open_log(state_path("ntprinters.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
577 if (!tdb_printers) {
578 DEBUG(0,("nt_printing_init: Failed to open nt printers database %s (%s)\n",
579 state_path("ntprinters.tdb"), strerror(errno) ));
580 return False;
583 if (tdb_forms)
584 tdb_close(tdb_forms);
585 tdb_forms = tdb_open_log(state_path("ntforms.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
586 if (!tdb_forms) {
587 DEBUG(0,("nt_printing_init: Failed to open nt forms database %s (%s)\n",
588 state_path("ntforms.tdb"), strerror(errno) ));
589 return False;
592 /* handle a Samba upgrade */
594 vers_id = tdb_fetch_int32(tdb_drivers, vstring);
595 if (vers_id == -1) {
596 DEBUG(10, ("Fresh database\n"));
597 tdb_store_int32( tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5 );
598 vers_id = NTDRIVERS_DATABASE_VERSION_5;
601 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
603 if ((vers_id == NTDRIVERS_DATABASE_VERSION_1) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_1)) {
604 if (!upgrade_to_version_3())
605 return False;
606 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
607 vers_id = NTDRIVERS_DATABASE_VERSION_3;
610 if ((vers_id == NTDRIVERS_DATABASE_VERSION_2) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_2)) {
611 /* Written on a bigendian machine with old fetch_int code. Save as le. */
612 /* The only upgrade between V2 and V3 is to save the version in little-endian. */
613 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
614 vers_id = NTDRIVERS_DATABASE_VERSION_3;
617 if (vers_id == NTDRIVERS_DATABASE_VERSION_3 ) {
618 if ( !upgrade_to_version_4() )
619 return False;
620 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_4);
621 vers_id = NTDRIVERS_DATABASE_VERSION_4;
624 if (vers_id == NTDRIVERS_DATABASE_VERSION_4 ) {
625 if ( !upgrade_to_version_5() )
626 return False;
627 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5);
628 vers_id = NTDRIVERS_DATABASE_VERSION_5;
632 if ( vers_id != NTDRIVERS_DATABASE_VERSION_5 ) {
633 DEBUG(0,("nt_printing_init: Unknown printer database version [%d]\n", vers_id));
634 return False;
638 update_c_setprinter(True);
641 * register callback to handle updating printers as new
642 * drivers are installed
645 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
646 do_drv_upgrade_printer);
649 * register callback to handle updating printer data
650 * when a driver is initialized
653 messaging_register(msg_ctx, NULL, MSG_PRINTERDATA_INIT_RESET,
654 reset_all_printerdata);
656 /* of course, none of the message callbacks matter if you don't
657 tell messages.c that you interested in receiving PRINT_GENERAL
658 msgs. This is done in claim_connection() */
661 if ( lp_security() == SEC_ADS ) {
662 win_rc = check_published_printers();
663 if (!W_ERROR_IS_OK(win_rc))
664 DEBUG(0, ("nt_printing_init: error checking published printers: %s\n", dos_errstr(win_rc)));
667 return True;
670 /*******************************************************************
671 Function to allow filename parsing "the old way".
672 ********************************************************************/
674 static char *driver_unix_convert(connection_struct *conn,
675 const char *old_name,
676 SMB_STRUCT_STAT *pst)
678 TALLOC_CTX *ctx = talloc_tos();
679 char *name = talloc_strdup(ctx, old_name);
680 char *new_name = NULL;
682 if (!name) {
683 return NULL;
685 unix_format(name);
686 name = unix_clean_name(ctx, name);
687 if (!name) {
688 return NULL;
690 trim_string(name,"/","/");
691 unix_convert(ctx,conn, name, false, &new_name, NULL, pst);
692 return new_name;
695 /*******************************************************************
696 tdb traversal function for counting printers.
697 ********************************************************************/
699 static int traverse_counting_printers(TDB_CONTEXT *t, TDB_DATA key,
700 TDB_DATA data, void *context)
702 int *printer_count = (int*)context;
704 if (memcmp(PRINTERS_PREFIX, key.dptr, sizeof(PRINTERS_PREFIX)-1) == 0) {
705 (*printer_count)++;
706 DEBUG(10,("traverse_counting_printers: printer = [%s] printer_count = %d\n", key.dptr, *printer_count));
709 return 0;
712 /*******************************************************************
713 Update the spooler global c_setprinter. This variable is initialized
714 when the parent smbd starts with the number of existing printers. It
715 is monotonically increased by the current number of printers *after*
716 each add or delete printer RPC. Only Microsoft knows why... JRR020119
717 ********************************************************************/
719 uint32 update_c_setprinter(bool initialize)
721 int32 c_setprinter;
722 int32 printer_count = 0;
724 tdb_lock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
726 /* Traverse the tdb, counting the printers */
727 tdb_traverse(tdb_printers, traverse_counting_printers, (void *)&printer_count);
729 /* If initializing, set c_setprinter to current printers count
730 * otherwise, bump it by the current printer count
732 if (!initialize)
733 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER) + printer_count;
734 else
735 c_setprinter = printer_count;
737 DEBUG(10,("update_c_setprinter: c_setprinter = %u\n", (unsigned int)c_setprinter));
738 tdb_store_int32(tdb_printers, GLOBAL_C_SETPRINTER, c_setprinter);
740 tdb_unlock_bystring(tdb_printers, GLOBAL_C_SETPRINTER);
742 return (uint32)c_setprinter;
745 /*******************************************************************
746 Get the spooler global c_setprinter, accounting for initialization.
747 ********************************************************************/
749 uint32 get_c_setprinter(void)
751 int32 c_setprinter = tdb_fetch_int32(tdb_printers, GLOBAL_C_SETPRINTER);
753 if (c_setprinter == (int32)-1)
754 c_setprinter = update_c_setprinter(True);
756 DEBUG(10,("get_c_setprinter: c_setprinter = %d\n", c_setprinter));
758 return (uint32)c_setprinter;
761 /****************************************************************************
762 Get builtin form struct list.
763 ****************************************************************************/
765 int get_builtin_ntforms(nt_forms_struct **list)
767 *list = (nt_forms_struct *)memdup(&default_forms[0], sizeof(default_forms));
768 if (!*list) {
769 return 0;
771 return sizeof(default_forms) / sizeof(default_forms[0]);
774 /****************************************************************************
775 get a builtin form struct
776 ****************************************************************************/
778 bool get_a_builtin_ntform(UNISTR2 *uni_formname,nt_forms_struct *form)
780 int i,count;
781 fstring form_name;
782 unistr2_to_ascii(form_name, uni_formname, sizeof(form_name));
783 DEBUGADD(6,("Looking for builtin form %s \n", form_name));
784 count = sizeof(default_forms) / sizeof(default_forms[0]);
785 for (i=0;i<count;i++) {
786 if (strequal(form_name,default_forms[i].name)) {
787 DEBUGADD(6,("Found builtin form %s \n", form_name));
788 memcpy(form,&default_forms[i],sizeof(*form));
789 break;
793 return (i !=count);
796 /****************************************************************************
797 get a form struct list.
798 ****************************************************************************/
800 int get_ntforms(nt_forms_struct **list)
802 TDB_DATA kbuf, newkey, dbuf;
803 nt_forms_struct form;
804 int ret;
805 int i;
806 int n = 0;
808 *list = NULL;
810 for (kbuf = tdb_firstkey(tdb_forms);
811 kbuf.dptr;
812 newkey = tdb_nextkey(tdb_forms, kbuf), safe_free(kbuf.dptr), kbuf=newkey)
814 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) != 0)
815 continue;
817 dbuf = tdb_fetch(tdb_forms, kbuf);
818 if (!dbuf.dptr)
819 continue;
821 fstrcpy(form.name, (const char *)kbuf.dptr+strlen(FORMS_PREFIX));
822 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "dddddddd",
823 &i, &form.flag, &form.width, &form.length, &form.left,
824 &form.top, &form.right, &form.bottom);
825 SAFE_FREE(dbuf.dptr);
826 if (ret != dbuf.dsize)
827 continue;
829 *list = SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1);
830 if (!*list) {
831 DEBUG(0,("get_ntforms: Realloc fail.\n"));
832 return 0;
834 (*list)[n] = form;
835 n++;
839 return n;
842 /****************************************************************************
843 write a form struct list
844 ****************************************************************************/
846 int write_ntforms(nt_forms_struct **list, int number)
848 TALLOC_CTX *ctx = talloc_tos();
849 char *buf = NULL;
850 char *key = NULL;
851 int len;
852 TDB_DATA dbuf;
853 int i;
855 for (i=0;i<number;i++) {
856 /* save index, so list is rebuilt in correct order */
857 len = tdb_pack(NULL, 0, "dddddddd",
858 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
859 (*list)[i].left, (*list)[i].top, (*list)[i].right,
860 (*list)[i].bottom);
861 if (!len) {
862 continue;
864 buf = TALLOC_ARRAY(ctx, char, len);
865 if (!buf) {
866 return 0;
868 len = tdb_pack((uint8 *)buf, len, "dddddddd",
869 i, (*list)[i].flag, (*list)[i].width, (*list)[i].length,
870 (*list)[i].left, (*list)[i].top, (*list)[i].right,
871 (*list)[i].bottom);
872 key = talloc_asprintf(ctx, "%s%s", FORMS_PREFIX, (*list)[i].name);
873 if (!key) {
874 return 0;
876 dbuf.dsize = len;
877 dbuf.dptr = (uint8 *)buf;
878 if (tdb_store_bystring(tdb_forms, key, dbuf, TDB_REPLACE) != 0) {
879 TALLOC_FREE(key);
880 TALLOC_FREE(buf);
881 break;
883 TALLOC_FREE(key);
884 TALLOC_FREE(buf);
887 return i;
890 /****************************************************************************
891 add a form struct at the end of the list
892 ****************************************************************************/
893 bool add_a_form(nt_forms_struct **list, const FORM *form, int *count)
895 int n=0;
896 bool update;
897 fstring form_name;
900 * NT tries to add forms even when
901 * they are already in the base
902 * only update the values if already present
905 update=False;
907 unistr2_to_ascii(form_name, &form->name, sizeof(form_name));
908 for (n=0; n<*count; n++) {
909 if ( strequal((*list)[n].name, form_name) ) {
910 update=True;
911 break;
915 if (update==False) {
916 if((*list=SMB_REALLOC_ARRAY(*list, nt_forms_struct, n+1)) == NULL) {
917 DEBUG(0,("add_a_form: failed to enlarge forms list!\n"));
918 return False;
920 unistr2_to_ascii((*list)[n].name, &form->name, sizeof((*list)[n].name));
921 (*count)++;
924 (*list)[n].flag=form->flags;
925 (*list)[n].width=form->size_x;
926 (*list)[n].length=form->size_y;
927 (*list)[n].left=form->left;
928 (*list)[n].top=form->top;
929 (*list)[n].right=form->right;
930 (*list)[n].bottom=form->bottom;
932 DEBUG(6,("add_a_form: Successfully %s form [%s]\n",
933 update ? "updated" : "added", form_name));
935 return True;
938 /****************************************************************************
939 Delete a named form struct.
940 ****************************************************************************/
942 bool delete_a_form(nt_forms_struct **list, UNISTR2 *del_name, int *count, WERROR *ret)
944 char *key = NULL;
945 int n=0;
946 fstring form_name;
948 *ret = WERR_OK;
950 unistr2_to_ascii(form_name, del_name, sizeof(form_name));
952 for (n=0; n<*count; n++) {
953 if (!strncmp((*list)[n].name, form_name, strlen(form_name))) {
954 DEBUG(103, ("delete_a_form, [%s] in list\n", form_name));
955 break;
959 if (n == *count) {
960 DEBUG(10,("delete_a_form, [%s] not found\n", form_name));
961 *ret = WERR_INVALID_PARAM;
962 return False;
965 if (asprintf(&key, "%s%s", FORMS_PREFIX, (*list)[n].name) < 0) {
966 *ret = WERR_NOMEM;
967 return false;
969 if (tdb_delete_bystring(tdb_forms, key) != 0) {
970 SAFE_FREE(key);
971 *ret = WERR_NOMEM;
972 return False;
974 SAFE_FREE(key);
975 return true;
978 /****************************************************************************
979 Update a form struct.
980 ****************************************************************************/
982 void update_a_form(nt_forms_struct **list, const FORM *form, int count)
984 int n=0;
985 fstring form_name;
986 unistr2_to_ascii(form_name, &(form->name), sizeof(form_name));
988 DEBUG(106, ("[%s]\n", form_name));
989 for (n=0; n<count; n++) {
990 DEBUGADD(106, ("n [%d]:[%s]\n", n, (*list)[n].name));
991 if (!strncmp((*list)[n].name, form_name, strlen(form_name)))
992 break;
995 if (n==count) return;
997 (*list)[n].flag=form->flags;
998 (*list)[n].width=form->size_x;
999 (*list)[n].length=form->size_y;
1000 (*list)[n].left=form->left;
1001 (*list)[n].top=form->top;
1002 (*list)[n].right=form->right;
1003 (*list)[n].bottom=form->bottom;
1006 /****************************************************************************
1007 Get the nt drivers list.
1008 Traverse the database and look-up the matching names.
1009 ****************************************************************************/
1010 int get_ntdrivers(fstring **list, const char *architecture, uint32 version)
1012 int total=0;
1013 const char *short_archi;
1014 char *key = NULL;
1015 TDB_DATA kbuf, newkey;
1017 short_archi = get_short_archi(architecture);
1018 if (!short_archi) {
1019 return 0;
1022 if (asprintf(&key, "%s%s/%d/", DRIVERS_PREFIX,
1023 short_archi, version) < 0) {
1024 return 0;
1027 for (kbuf = tdb_firstkey(tdb_drivers);
1028 kbuf.dptr;
1029 newkey = tdb_nextkey(tdb_drivers, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
1031 if (strncmp((const char *)kbuf.dptr, key, strlen(key)) != 0)
1032 continue;
1034 if((*list = SMB_REALLOC_ARRAY(*list, fstring, total+1)) == NULL) {
1035 DEBUG(0,("get_ntdrivers: failed to enlarge list!\n"));
1036 SAFE_FREE(key);
1037 return -1;
1040 fstrcpy((*list)[total], (const char *)kbuf.dptr+strlen(key));
1041 total++;
1044 SAFE_FREE(key);
1045 return(total);
1048 /****************************************************************************
1049 Function to do the mapping between the long architecture name and
1050 the short one.
1051 ****************************************************************************/
1053 const char *get_short_archi(const char *long_archi)
1055 int i=-1;
1057 DEBUG(107,("Getting architecture dependant directory\n"));
1058 do {
1059 i++;
1060 } while ( (archi_table[i].long_archi!=NULL ) &&
1061 StrCaseCmp(long_archi, archi_table[i].long_archi) );
1063 if (archi_table[i].long_archi==NULL) {
1064 DEBUGADD(10,("Unknown architecture [%s] !\n", long_archi));
1065 return NULL;
1068 /* this might be client code - but shouldn't this be an fstrcpy etc? */
1070 DEBUGADD(108,("index: [%d]\n", i));
1071 DEBUGADD(108,("long architecture: [%s]\n", archi_table[i].long_archi));
1072 DEBUGADD(108,("short architecture: [%s]\n", archi_table[i].short_archi));
1074 return archi_table[i].short_archi;
1077 /****************************************************************************
1078 Version information in Microsoft files is held in a VS_VERSION_INFO structure.
1079 There are two case to be covered here: PE (Portable Executable) and NE (New
1080 Executable) files. Both files support the same INFO structure, but PE files
1081 store the signature in unicode, and NE files store it as !unicode.
1082 returns -1 on error, 1 on version info found, and 0 on no version info found.
1083 ****************************************************************************/
1085 static int get_file_version(files_struct *fsp, char *fname,uint32 *major, uint32 *minor)
1087 int i;
1088 char *buf = NULL;
1089 ssize_t byte_count;
1091 if ((buf=(char *)SMB_MALLOC(DOS_HEADER_SIZE)) == NULL) {
1092 DEBUG(0,("get_file_version: PE file [%s] DOS Header malloc failed bytes = %d\n",
1093 fname, DOS_HEADER_SIZE));
1094 goto error_exit;
1097 if ((byte_count = vfs_read_data(fsp, buf, DOS_HEADER_SIZE)) < DOS_HEADER_SIZE) {
1098 DEBUG(3,("get_file_version: File [%s] DOS header too short, bytes read = %lu\n",
1099 fname, (unsigned long)byte_count));
1100 goto no_version_info;
1103 /* Is this really a DOS header? */
1104 if (SVAL(buf,DOS_HEADER_MAGIC_OFFSET) != DOS_HEADER_MAGIC) {
1105 DEBUG(6,("get_file_version: File [%s] bad DOS magic = 0x%x\n",
1106 fname, SVAL(buf,DOS_HEADER_MAGIC_OFFSET)));
1107 goto no_version_info;
1110 /* Skip OEM header (if any) and the DOS stub to start of Windows header */
1111 if (SMB_VFS_LSEEK(fsp, SVAL(buf,DOS_HEADER_LFANEW_OFFSET), SEEK_SET) == (SMB_OFF_T)-1) {
1112 DEBUG(3,("get_file_version: File [%s] too short, errno = %d\n",
1113 fname, errno));
1114 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1115 goto no_version_info;
1118 /* Note: DOS_HEADER_SIZE and NE_HEADER_SIZE are incidentally same */
1119 if ((byte_count = vfs_read_data(fsp, buf, NE_HEADER_SIZE)) < NE_HEADER_SIZE) {
1120 DEBUG(3,("get_file_version: File [%s] Windows header too short, bytes read = %lu\n",
1121 fname, (unsigned long)byte_count));
1122 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1123 goto no_version_info;
1126 /* The header may be a PE (Portable Executable) or an NE (New Executable) */
1127 if (IVAL(buf,PE_HEADER_SIGNATURE_OFFSET) == PE_HEADER_SIGNATURE) {
1128 unsigned int num_sections;
1129 unsigned int section_table_bytes;
1131 /* Just skip over optional header to get to section table */
1132 if (SMB_VFS_LSEEK(fsp,
1133 SVAL(buf,PE_HEADER_OPTIONAL_HEADER_SIZE)-(NE_HEADER_SIZE-PE_HEADER_SIZE),
1134 SEEK_CUR) == (SMB_OFF_T)-1) {
1135 DEBUG(3,("get_file_version: File [%s] Windows optional header too short, errno = %d\n",
1136 fname, errno));
1137 goto error_exit;
1140 /* get the section table */
1141 num_sections = SVAL(buf,PE_HEADER_NUMBER_OF_SECTIONS);
1142 section_table_bytes = num_sections * PE_HEADER_SECT_HEADER_SIZE;
1143 if (section_table_bytes == 0)
1144 goto error_exit;
1146 SAFE_FREE(buf);
1147 if ((buf=(char *)SMB_MALLOC(section_table_bytes)) == NULL) {
1148 DEBUG(0,("get_file_version: PE file [%s] section table malloc failed bytes = %d\n",
1149 fname, section_table_bytes));
1150 goto error_exit;
1153 if ((byte_count = vfs_read_data(fsp, buf, section_table_bytes)) < section_table_bytes) {
1154 DEBUG(3,("get_file_version: PE file [%s] Section header too short, bytes read = %lu\n",
1155 fname, (unsigned long)byte_count));
1156 goto error_exit;
1159 /* Iterate the section table looking for the resource section ".rsrc" */
1160 for (i = 0; i < num_sections; i++) {
1161 int sec_offset = i * PE_HEADER_SECT_HEADER_SIZE;
1163 if (strcmp(".rsrc", &buf[sec_offset+PE_HEADER_SECT_NAME_OFFSET]) == 0) {
1164 unsigned int section_pos = IVAL(buf,sec_offset+PE_HEADER_SECT_PTR_DATA_OFFSET);
1165 unsigned int section_bytes = IVAL(buf,sec_offset+PE_HEADER_SECT_SIZE_DATA_OFFSET);
1167 if (section_bytes == 0)
1168 goto error_exit;
1170 SAFE_FREE(buf);
1171 if ((buf=(char *)SMB_MALLOC(section_bytes)) == NULL) {
1172 DEBUG(0,("get_file_version: PE file [%s] version malloc failed bytes = %d\n",
1173 fname, section_bytes));
1174 goto error_exit;
1177 /* Seek to the start of the .rsrc section info */
1178 if (SMB_VFS_LSEEK(fsp, section_pos, SEEK_SET) == (SMB_OFF_T)-1) {
1179 DEBUG(3,("get_file_version: PE file [%s] too short for section info, errno = %d\n",
1180 fname, errno));
1181 goto error_exit;
1184 if ((byte_count = vfs_read_data(fsp, buf, section_bytes)) < section_bytes) {
1185 DEBUG(3,("get_file_version: PE file [%s] .rsrc section too short, bytes read = %lu\n",
1186 fname, (unsigned long)byte_count));
1187 goto error_exit;
1190 if (section_bytes < VS_VERSION_INFO_UNICODE_SIZE)
1191 goto error_exit;
1193 for (i=0; i<section_bytes-VS_VERSION_INFO_UNICODE_SIZE; i++) {
1194 /* Scan for 1st 3 unicoded bytes followed by word aligned magic value */
1195 if (buf[i] == 'V' && buf[i+1] == '\0' && buf[i+2] == 'S') {
1196 /* Align to next long address */
1197 int pos = (i + sizeof(VS_SIGNATURE)*2 + 3) & 0xfffffffc;
1199 if (IVAL(buf,pos) == VS_MAGIC_VALUE) {
1200 *major = IVAL(buf,pos+VS_MAJOR_OFFSET);
1201 *minor = IVAL(buf,pos+VS_MINOR_OFFSET);
1203 DEBUG(6,("get_file_version: PE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1204 fname, *major, *minor,
1205 (*major>>16)&0xffff, *major&0xffff,
1206 (*minor>>16)&0xffff, *minor&0xffff));
1207 SAFE_FREE(buf);
1208 return 1;
1215 /* Version info not found, fall back to origin date/time */
1216 DEBUG(10,("get_file_version: PE file [%s] has no version info\n", fname));
1217 SAFE_FREE(buf);
1218 return 0;
1220 } else if (SVAL(buf,NE_HEADER_SIGNATURE_OFFSET) == NE_HEADER_SIGNATURE) {
1221 if (CVAL(buf,NE_HEADER_TARGET_OS_OFFSET) != NE_HEADER_TARGOS_WIN ) {
1222 DEBUG(3,("get_file_version: NE file [%s] wrong target OS = 0x%x\n",
1223 fname, CVAL(buf,NE_HEADER_TARGET_OS_OFFSET)));
1224 /* At this point, we assume the file is in error. It still could be somthing
1225 * else besides a NE file, but it unlikely at this point. */
1226 goto error_exit;
1229 /* Allocate a bit more space to speed up things */
1230 SAFE_FREE(buf);
1231 if ((buf=(char *)SMB_MALLOC(VS_NE_BUF_SIZE)) == NULL) {
1232 DEBUG(0,("get_file_version: NE file [%s] malloc failed bytes = %d\n",
1233 fname, PE_HEADER_SIZE));
1234 goto error_exit;
1237 /* This is a HACK! I got tired of trying to sort through the messy
1238 * 'NE' file format. If anyone wants to clean this up please have at
1239 * it, but this works. 'NE' files will eventually fade away. JRR */
1240 while((byte_count = vfs_read_data(fsp, buf, VS_NE_BUF_SIZE)) > 0) {
1241 /* Cover case that should not occur in a well formed 'NE' .dll file */
1242 if (byte_count-VS_VERSION_INFO_SIZE <= 0) break;
1244 for(i=0; i<byte_count; i++) {
1245 /* Fast skip past data that can't possibly match */
1246 if (buf[i] != 'V') continue;
1248 /* Potential match data crosses buf boundry, move it to beginning
1249 * of buf, and fill the buf with as much as it will hold. */
1250 if (i>byte_count-VS_VERSION_INFO_SIZE) {
1251 int bc;
1253 memcpy(buf, &buf[i], byte_count-i);
1254 if ((bc = vfs_read_data(fsp, &buf[byte_count-i], VS_NE_BUF_SIZE-
1255 (byte_count-i))) < 0) {
1257 DEBUG(0,("get_file_version: NE file [%s] Read error, errno=%d\n",
1258 fname, errno));
1259 goto error_exit;
1262 byte_count = bc + (byte_count - i);
1263 if (byte_count<VS_VERSION_INFO_SIZE) break;
1265 i = 0;
1268 /* Check that the full signature string and the magic number that
1269 * follows exist (not a perfect solution, but the chances that this
1270 * occurs in code is, well, remote. Yes I know I'm comparing the 'V'
1271 * twice, as it is simpler to read the code. */
1272 if (strcmp(&buf[i], VS_SIGNATURE) == 0) {
1273 /* Compute skip alignment to next long address */
1274 int skip = -(SMB_VFS_LSEEK(fsp, 0, SEEK_CUR) - (byte_count - i) +
1275 sizeof(VS_SIGNATURE)) & 3;
1276 if (IVAL(buf,i+sizeof(VS_SIGNATURE)+skip) != 0xfeef04bd) continue;
1278 *major = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MAJOR_OFFSET);
1279 *minor = IVAL(buf,i+sizeof(VS_SIGNATURE)+skip+VS_MINOR_OFFSET);
1280 DEBUG(6,("get_file_version: NE file [%s] Version = %08x:%08x (%d.%d.%d.%d)\n",
1281 fname, *major, *minor,
1282 (*major>>16)&0xffff, *major&0xffff,
1283 (*minor>>16)&0xffff, *minor&0xffff));
1284 SAFE_FREE(buf);
1285 return 1;
1290 /* Version info not found, fall back to origin date/time */
1291 DEBUG(0,("get_file_version: NE file [%s] Version info not found\n", fname));
1292 SAFE_FREE(buf);
1293 return 0;
1295 } else
1296 /* Assume this isn't an error... the file just looks sort of like a PE/NE file */
1297 DEBUG(3,("get_file_version: File [%s] unknown file format, signature = 0x%x\n",
1298 fname, IVAL(buf,PE_HEADER_SIGNATURE_OFFSET)));
1300 no_version_info:
1301 SAFE_FREE(buf);
1302 return 0;
1304 error_exit:
1305 SAFE_FREE(buf);
1306 return -1;
1309 /****************************************************************************
1310 Drivers for Microsoft systems contain multiple files. Often, multiple drivers
1311 share one or more files. During the MS installation process files are checked
1312 to insure that only a newer version of a shared file is installed over an
1313 older version. There are several possibilities for this comparison. If there
1314 is no previous version, the new one is newer (obviously). If either file is
1315 missing the version info structure, compare the creation date (on Unix use
1316 the modification date). Otherwise chose the numerically larger version number.
1317 ****************************************************************************/
1319 static int file_version_is_newer(connection_struct *conn, fstring new_file, fstring old_file)
1321 bool use_version = true;
1322 char *filepath = NULL;
1324 uint32 new_major;
1325 uint32 new_minor;
1326 time_t new_create_time;
1328 uint32 old_major;
1329 uint32 old_minor;
1330 time_t old_create_time;
1332 files_struct *fsp = NULL;
1333 SMB_STRUCT_STAT st;
1334 SMB_STRUCT_STAT stat_buf;
1336 NTSTATUS status;
1338 SET_STAT_INVALID(st);
1339 SET_STAT_INVALID(stat_buf);
1340 new_create_time = (time_t)0;
1341 old_create_time = (time_t)0;
1343 /* Get file version info (if available) for previous file (if it exists) */
1344 filepath = driver_unix_convert(conn,old_file,&stat_buf);
1345 if (!filepath) {
1346 goto error_exit;
1349 status = open_file_ntcreate(conn, NULL, filepath, &stat_buf,
1350 FILE_GENERIC_READ,
1351 FILE_SHARE_READ|FILE_SHARE_WRITE,
1352 FILE_OPEN,
1354 FILE_ATTRIBUTE_NORMAL,
1355 INTERNAL_OPEN_ONLY,
1356 NULL, &fsp);
1358 if (!NT_STATUS_IS_OK(status)) {
1359 /* Old file not found, so by definition new file is in fact newer */
1360 DEBUG(10,("file_version_is_newer: Can't open old file [%s], errno = %d\n",
1361 filepath, errno));
1362 return 1;
1364 } else {
1365 int ret = get_file_version(fsp, old_file, &old_major, &old_minor);
1366 if (ret == -1) {
1367 goto error_exit;
1370 if (!ret) {
1371 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1372 old_file));
1373 use_version = false;
1374 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1375 goto error_exit;
1377 old_create_time = st.st_mtime;
1378 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n", old_create_time));
1381 close_file(fsp, NORMAL_CLOSE);
1382 fsp = NULL;
1384 /* Get file version info (if available) for new file */
1385 filepath = driver_unix_convert(conn,new_file,&stat_buf);
1386 if (!filepath) {
1387 goto error_exit;
1390 status = open_file_ntcreate(conn, NULL, filepath, &stat_buf,
1391 FILE_GENERIC_READ,
1392 FILE_SHARE_READ|FILE_SHARE_WRITE,
1393 FILE_OPEN,
1395 FILE_ATTRIBUTE_NORMAL,
1396 INTERNAL_OPEN_ONLY,
1397 NULL, &fsp);
1399 if (!NT_STATUS_IS_OK(status)) {
1400 /* New file not found, this shouldn't occur if the caller did its job */
1401 DEBUG(3,("file_version_is_newer: Can't open new file [%s], errno = %d\n",
1402 filepath, errno));
1403 goto error_exit;
1405 } else {
1406 int ret = get_file_version(fsp, new_file, &new_major, &new_minor);
1407 if (ret == -1) {
1408 goto error_exit;
1411 if (!ret) {
1412 DEBUG(6,("file_version_is_newer: Version info not found [%s], use mod time\n",
1413 new_file));
1414 use_version = false;
1415 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1416 goto error_exit;
1418 new_create_time = st.st_mtime;
1419 DEBUGADD(6,("file_version_is_newer: mod time = %ld sec\n", new_create_time));
1422 close_file(fsp, NORMAL_CLOSE);
1423 fsp = NULL;
1425 if (use_version && (new_major != old_major || new_minor != old_minor)) {
1426 /* Compare versions and choose the larger version number */
1427 if (new_major > old_major ||
1428 (new_major == old_major && new_minor > old_minor)) {
1430 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1431 return 1;
1433 else {
1434 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1435 return 0;
1438 } else {
1439 /* Compare modification time/dates and choose the newest time/date */
1440 if (new_create_time > old_create_time) {
1441 DEBUG(6,("file_version_is_newer: Replacing [%s] with [%s]\n", old_file, new_file));
1442 return 1;
1444 else {
1445 DEBUG(6,("file_version_is_newer: Leaving [%s] unchanged\n", old_file));
1446 return 0;
1450 error_exit:
1451 if(fsp)
1452 close_file(fsp, NORMAL_CLOSE);
1453 return -1;
1456 /****************************************************************************
1457 Determine the correct cVersion associated with an architecture and driver
1458 ****************************************************************************/
1459 static uint32 get_correct_cversion(const char *architecture, fstring driverpath_in,
1460 struct current_user *user, WERROR *perr)
1462 int cversion;
1463 NTSTATUS nt_status;
1464 char *driverpath = NULL;
1465 DATA_BLOB null_pw;
1466 fstring res_type;
1467 files_struct *fsp = NULL;
1468 SMB_STRUCT_STAT st;
1469 connection_struct *conn;
1470 NTSTATUS status;
1472 SET_STAT_INVALID(st);
1474 *perr = WERR_INVALID_PARAM;
1476 /* If architecture is Windows 95/98/ME, the version is always 0. */
1477 if (strcmp(architecture, SPL_ARCH_WIN40) == 0) {
1478 DEBUG(10,("get_correct_cversion: Driver is Win9x, cversion = 0\n"));
1479 *perr = WERR_OK;
1480 return 0;
1483 /* If architecture is Windows x64, the version is always 3. */
1484 if (strcmp(architecture, SPL_ARCH_X64) == 0) {
1485 DEBUG(10,("get_correct_cversion: Driver is x64, cversion = 3\n"));
1486 *perr = WERR_OK;
1487 return 3;
1491 * Connect to the print$ share under the same account as the user connected
1492 * to the rpc pipe. Note we must still be root to do this.
1495 /* Null password is ok - we are already an authenticated user... */
1496 null_pw = data_blob_null;
1497 fstrcpy(res_type, "A:");
1498 become_root();
1499 conn = make_connection_with_chdir("print$", null_pw, res_type, user->vuid, &nt_status);
1500 unbecome_root();
1502 if (conn == NULL) {
1503 DEBUG(0,("get_correct_cversion: Unable to connect\n"));
1504 *perr = ntstatus_to_werror(nt_status);
1505 return -1;
1508 /* We are temporarily becoming the connection user. */
1509 if (!become_user(conn, user->vuid)) {
1510 DEBUG(0,("get_correct_cversion: Can't become user!\n"));
1511 *perr = WERR_ACCESS_DENIED;
1512 return -1;
1515 /* Open the driver file (Portable Executable format) and determine the
1516 * deriver the cversion. */
1517 driverpath = talloc_asprintf(talloc_tos(),
1518 "%s/%s",
1519 architecture,
1520 driverpath_in);
1521 if (!driverpath) {
1522 *perr = WERR_NOMEM;
1523 goto error_exit;
1526 driverpath = driver_unix_convert(conn,driverpath,&st);
1527 if (!driverpath) {
1528 *perr = WERR_NOMEM;
1529 goto error_exit;
1532 if (!vfs_file_exist(conn, driverpath, &st)) {
1533 *perr = WERR_BADFILE;
1534 goto error_exit;
1537 status = open_file_ntcreate(conn, NULL, driverpath, &st,
1538 FILE_GENERIC_READ,
1539 FILE_SHARE_READ|FILE_SHARE_WRITE,
1540 FILE_OPEN,
1542 FILE_ATTRIBUTE_NORMAL,
1543 INTERNAL_OPEN_ONLY,
1544 NULL, &fsp);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 DEBUG(3,("get_correct_cversion: Can't open file [%s], errno = %d\n",
1548 driverpath, errno));
1549 *perr = WERR_ACCESS_DENIED;
1550 goto error_exit;
1551 } else {
1552 uint32 major;
1553 uint32 minor;
1554 int ret = get_file_version(fsp, driverpath, &major, &minor);
1555 if (ret == -1) goto error_exit;
1557 if (!ret) {
1558 DEBUG(6,("get_correct_cversion: Version info not found [%s]\n", driverpath));
1559 goto error_exit;
1563 * This is a Microsoft'ism. See references in MSDN to VER_FILEVERSION
1564 * for more details. Version in this case is not just the version of the
1565 * file, but the version in the sense of kernal mode (2) vs. user mode
1566 * (3) drivers. Other bits of the version fields are the version info.
1567 * JRR 010716
1569 cversion = major & 0x0000ffff;
1570 switch (cversion) {
1571 case 2: /* WinNT drivers */
1572 case 3: /* Win2K drivers */
1573 break;
1575 default:
1576 DEBUG(6,("get_correct_cversion: cversion invalid [%s] cversion = %d\n",
1577 driverpath, cversion));
1578 goto error_exit;
1581 DEBUG(10,("get_correct_cversion: Version info found [%s] major = 0x%x minor = 0x%x\n",
1582 driverpath, major, minor));
1585 DEBUG(10,("get_correct_cversion: Driver file [%s] cversion = %d\n",
1586 driverpath, cversion));
1588 close_file(fsp, NORMAL_CLOSE);
1589 close_cnum(conn, user->vuid);
1590 unbecome_user();
1591 *perr = WERR_OK;
1592 return cversion;
1595 error_exit:
1597 if(fsp)
1598 close_file(fsp, NORMAL_CLOSE);
1600 close_cnum(conn, user->vuid);
1601 unbecome_user();
1602 return -1;
1605 /****************************************************************************
1606 ****************************************************************************/
1607 static WERROR clean_up_driver_struct_level_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver,
1608 struct current_user *user)
1610 const char *architecture;
1611 fstring new_name;
1612 char *p;
1613 int i;
1614 WERROR err;
1616 /* clean up the driver name.
1617 * we can get .\driver.dll
1618 * or worse c:\windows\system\driver.dll !
1620 /* using an intermediate string to not have overlaping memcpy()'s */
1621 if ((p = strrchr(driver->driverpath,'\\')) != NULL) {
1622 fstrcpy(new_name, p+1);
1623 fstrcpy(driver->driverpath, new_name);
1626 if ((p = strrchr(driver->datafile,'\\')) != NULL) {
1627 fstrcpy(new_name, p+1);
1628 fstrcpy(driver->datafile, new_name);
1631 if ((p = strrchr(driver->configfile,'\\')) != NULL) {
1632 fstrcpy(new_name, p+1);
1633 fstrcpy(driver->configfile, new_name);
1636 if ((p = strrchr(driver->helpfile,'\\')) != NULL) {
1637 fstrcpy(new_name, p+1);
1638 fstrcpy(driver->helpfile, new_name);
1641 if (driver->dependentfiles) {
1642 for (i=0; *driver->dependentfiles[i]; i++) {
1643 if ((p = strrchr(driver->dependentfiles[i],'\\')) != NULL) {
1644 fstrcpy(new_name, p+1);
1645 fstrcpy(driver->dependentfiles[i], new_name);
1650 architecture = get_short_archi(driver->environment);
1651 if (!architecture) {
1652 return WERR_UNKNOWN_PRINTER_DRIVER;
1655 /* jfm:7/16/2000 the client always sends the cversion=0.
1656 * The server should check which version the driver is by reading
1657 * the PE header of driver->driverpath.
1659 * For Windows 95/98 the version is 0 (so the value sent is correct)
1660 * For Windows NT (the architecture doesn't matter)
1661 * NT 3.1: cversion=0
1662 * NT 3.5/3.51: cversion=1
1663 * NT 4: cversion=2
1664 * NT2K: cversion=3
1666 if ((driver->cversion = get_correct_cversion( architecture, driver->driverpath, user, &err)) == -1)
1667 return err;
1669 return WERR_OK;
1672 /****************************************************************************
1673 ****************************************************************************/
1674 static WERROR clean_up_driver_struct_level_6(NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver, struct current_user *user)
1676 const char *architecture;
1677 fstring new_name;
1678 char *p;
1679 int i;
1680 WERROR err;
1682 /* clean up the driver name.
1683 * we can get .\driver.dll
1684 * or worse c:\windows\system\driver.dll !
1686 /* using an intermediate string to not have overlaping memcpy()'s */
1687 if ((p = strrchr(driver->driverpath,'\\')) != NULL) {
1688 fstrcpy(new_name, p+1);
1689 fstrcpy(driver->driverpath, new_name);
1692 if ((p = strrchr(driver->datafile,'\\')) != NULL) {
1693 fstrcpy(new_name, p+1);
1694 fstrcpy(driver->datafile, new_name);
1697 if ((p = strrchr(driver->configfile,'\\')) != NULL) {
1698 fstrcpy(new_name, p+1);
1699 fstrcpy(driver->configfile, new_name);
1702 if ((p = strrchr(driver->helpfile,'\\')) != NULL) {
1703 fstrcpy(new_name, p+1);
1704 fstrcpy(driver->helpfile, new_name);
1707 if (driver->dependentfiles) {
1708 for (i=0; *driver->dependentfiles[i]; i++) {
1709 if ((p = strrchr(driver->dependentfiles[i],'\\')) != NULL) {
1710 fstrcpy(new_name, p+1);
1711 fstrcpy(driver->dependentfiles[i], new_name);
1716 architecture = get_short_archi(driver->environment);
1717 if (!architecture) {
1718 return WERR_UNKNOWN_PRINTER_DRIVER;
1721 /* jfm:7/16/2000 the client always sends the cversion=0.
1722 * The server should check which version the driver is by reading
1723 * the PE header of driver->driverpath.
1725 * For Windows 95/98 the version is 0 (so the value sent is correct)
1726 * For Windows NT (the architecture doesn't matter)
1727 * NT 3.1: cversion=0
1728 * NT 3.5/3.51: cversion=1
1729 * NT 4: cversion=2
1730 * NT2K: cversion=3
1733 if ((driver->version = get_correct_cversion(architecture, driver->driverpath, user, &err)) == -1)
1734 return err;
1736 return WERR_OK;
1739 /****************************************************************************
1740 ****************************************************************************/
1741 WERROR clean_up_driver_struct(NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract,
1742 uint32 level, struct current_user *user)
1744 switch (level) {
1745 case 3:
1747 NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver;
1748 driver=driver_abstract.info_3;
1749 return clean_up_driver_struct_level_3(driver, user);
1751 case 6:
1753 NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver;
1754 driver=driver_abstract.info_6;
1755 return clean_up_driver_struct_level_6(driver, user);
1757 default:
1758 return WERR_INVALID_PARAM;
1762 /****************************************************************************
1763 This function sucks and should be replaced. JRA.
1764 ****************************************************************************/
1766 static void convert_level_6_to_level3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *dst, NT_PRINTER_DRIVER_INFO_LEVEL_6 *src)
1768 dst->cversion = src->version;
1770 fstrcpy( dst->name, src->name);
1771 fstrcpy( dst->environment, src->environment);
1772 fstrcpy( dst->driverpath, src->driverpath);
1773 fstrcpy( dst->datafile, src->datafile);
1774 fstrcpy( dst->configfile, src->configfile);
1775 fstrcpy( dst->helpfile, src->helpfile);
1776 fstrcpy( dst->monitorname, src->monitorname);
1777 fstrcpy( dst->defaultdatatype, src->defaultdatatype);
1778 dst->dependentfiles = src->dependentfiles;
1781 #if 0 /* Debugging function */
1783 static char* ffmt(unsigned char *c){
1784 int i;
1785 static char ffmt_str[17];
1787 for (i=0; i<16; i++) {
1788 if ((c[i] < ' ') || (c[i] > '~'))
1789 ffmt_str[i]='.';
1790 else
1791 ffmt_str[i]=c[i];
1793 ffmt_str[16]='\0';
1794 return ffmt_str;
1797 #endif
1799 /****************************************************************************
1800 ****************************************************************************/
1801 WERROR move_driver_to_download_area(NT_PRINTER_DRIVER_INFO_LEVEL driver_abstract, uint32 level,
1802 struct current_user *user, WERROR *perr)
1804 NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver;
1805 NT_PRINTER_DRIVER_INFO_LEVEL_3 converted_driver;
1806 const char *architecture;
1807 char *new_dir = NULL;
1808 char *old_name = NULL;
1809 char *new_name = NULL;
1810 DATA_BLOB null_pw;
1811 connection_struct *conn;
1812 NTSTATUS nt_status;
1813 fstring res_type;
1814 SMB_STRUCT_STAT st;
1815 int i;
1816 TALLOC_CTX *ctx = talloc_tos();
1817 int ver = 0;
1819 *perr = WERR_OK;
1821 if (level==3)
1822 driver=driver_abstract.info_3;
1823 else if (level==6) {
1824 convert_level_6_to_level3(&converted_driver, driver_abstract.info_6);
1825 driver = &converted_driver;
1826 } else {
1827 DEBUG(0,("move_driver_to_download_area: Unknown info level (%u)\n", (unsigned int)level ));
1828 return WERR_UNKNOWN_LEVEL;
1831 architecture = get_short_archi(driver->environment);
1832 if (!architecture) {
1833 return WERR_UNKNOWN_PRINTER_DRIVER;
1837 * Connect to the print$ share under the same account as the user connected to the rpc pipe.
1838 * Note we must be root to do this.
1841 null_pw = data_blob_null;
1842 fstrcpy(res_type, "A:");
1843 become_root();
1844 conn = make_connection_with_chdir("print$", null_pw, res_type, user->vuid, &nt_status);
1845 unbecome_root();
1847 if (conn == NULL) {
1848 DEBUG(0,("move_driver_to_download_area: Unable to connect\n"));
1849 *perr = ntstatus_to_werror(nt_status);
1850 return WERR_NO_SUCH_SHARE;
1854 * Save who we are - we are temporarily becoming the connection user.
1857 if (!become_user(conn, conn->vuid)) {
1858 DEBUG(0,("move_driver_to_download_area: Can't become user!\n"));
1859 return WERR_ACCESS_DENIED;
1862 /* WE ARE NOW RUNNING AS USER conn->vuid !!!!! */
1865 * make the directories version and version\driver_name
1866 * under the architecture directory.
1868 DEBUG(5,("Creating first directory\n"));
1869 new_dir = talloc_asprintf(ctx,
1870 "%s/%d",
1871 architecture,
1872 driver->cversion);
1873 if (!new_dir) {
1874 *perr = WERR_NOMEM;
1875 goto err_exit;
1877 new_dir = driver_unix_convert(conn,new_dir,&st);
1878 if (!new_dir) {
1879 *perr = WERR_NOMEM;
1880 goto err_exit;
1883 create_directory(conn, NULL, new_dir);
1885 /* For each driver file, archi\filexxx.yyy, if there is a duplicate file
1886 * listed for this driver which has already been moved, skip it (note:
1887 * drivers may list the same file name several times. Then check if the
1888 * file already exists in archi\cversion\, if so, check that the version
1889 * info (or time stamps if version info is unavailable) is newer (or the
1890 * date is later). If it is, move it to archi\cversion\filexxx.yyy.
1891 * Otherwise, delete the file.
1893 * If a file is not moved to archi\cversion\ because of an error, all the
1894 * rest of the 'unmoved' driver files are removed from archi\. If one or
1895 * more of the driver's files was already moved to archi\cversion\, it
1896 * potentially leaves the driver in a partially updated state. Version
1897 * trauma will most likely occur if an client attempts to use any printer
1898 * bound to the driver. Perhaps a rewrite to make sure the moves can be
1899 * done is appropriate... later JRR
1902 DEBUG(5,("Moving files now !\n"));
1904 if (driver->driverpath && strlen(driver->driverpath)) {
1905 new_name = talloc_asprintf(ctx,
1906 "%s/%s",
1907 architecture,
1908 driver->driverpath);
1909 if (!new_name) {
1910 *perr = WERR_NOMEM;
1911 goto err_exit;
1913 old_name = talloc_asprintf(ctx,
1914 "%s/%s",
1915 new_dir,
1916 driver->driverpath);
1917 if (!old_name) {
1918 *perr = WERR_NOMEM;
1919 goto err_exit;
1922 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
1923 new_name = driver_unix_convert(conn,new_name,&st);
1924 if (!new_name) {
1925 *perr = WERR_NOMEM;
1926 goto err_exit;
1928 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name, OPENX_FILE_EXISTS_TRUNCATE|
1929 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
1930 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
1931 new_name, old_name));
1932 *perr = WERR_ACCESS_DENIED;
1933 ver = -1;
1938 if (driver->datafile && strlen(driver->datafile)) {
1939 if (!strequal(driver->datafile, driver->driverpath)) {
1940 new_name = talloc_asprintf(ctx,
1941 "%s/%s",
1942 architecture,
1943 driver->datafile);
1944 if (!new_name) {
1945 *perr = WERR_NOMEM;
1946 goto err_exit;
1948 old_name = talloc_asprintf(ctx,
1949 "%s/%s",
1950 new_dir,
1951 driver->datafile);
1952 if (!old_name) {
1953 *perr = WERR_NOMEM;
1954 goto err_exit;
1956 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
1957 new_name = driver_unix_convert(conn,new_name,&st);
1958 if (!new_name) {
1959 *perr = WERR_NOMEM;
1960 goto err_exit;
1962 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name, OPENX_FILE_EXISTS_TRUNCATE|
1963 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
1964 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
1965 new_name, old_name));
1966 *perr = WERR_ACCESS_DENIED;
1967 ver = -1;
1973 if (driver->configfile && strlen(driver->configfile)) {
1974 if (!strequal(driver->configfile, driver->driverpath) &&
1975 !strequal(driver->configfile, driver->datafile)) {
1976 new_name = talloc_asprintf(ctx,
1977 "%s/%s",
1978 architecture,
1979 driver->configfile);
1980 if (!new_name) {
1981 *perr = WERR_NOMEM;
1982 goto err_exit;
1984 old_name = talloc_asprintf(ctx,
1985 "%s/%s",
1986 new_dir,
1987 driver->configfile);
1988 if (!old_name) {
1989 *perr = WERR_NOMEM;
1990 goto err_exit;
1992 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
1993 new_name = driver_unix_convert(conn,new_name,&st);
1994 if (!new_name) {
1995 *perr = WERR_NOMEM;
1996 goto err_exit;
1998 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name, OPENX_FILE_EXISTS_TRUNCATE|
1999 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
2000 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
2001 new_name, old_name));
2002 *perr = WERR_ACCESS_DENIED;
2003 ver = -1;
2009 if (driver->helpfile && strlen(driver->helpfile)) {
2010 if (!strequal(driver->helpfile, driver->driverpath) &&
2011 !strequal(driver->helpfile, driver->datafile) &&
2012 !strequal(driver->helpfile, driver->configfile)) {
2013 new_name = talloc_asprintf(ctx,
2014 "%s/%s",
2015 architecture,
2016 driver->helpfile);
2017 if (!new_name) {
2018 *perr = WERR_NOMEM;
2019 goto err_exit;
2021 old_name = talloc_asprintf(ctx,
2022 "%s/%s",
2023 new_dir,
2024 driver->helpfile);
2025 if (!old_name) {
2026 *perr = WERR_NOMEM;
2027 goto err_exit;
2029 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
2030 new_name = driver_unix_convert(conn,new_name,&st);
2031 if (!new_name) {
2032 *perr = WERR_NOMEM;
2033 goto err_exit;
2035 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name, OPENX_FILE_EXISTS_TRUNCATE|
2036 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
2037 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
2038 new_name, old_name));
2039 *perr = WERR_ACCESS_DENIED;
2040 ver = -1;
2046 if (driver->dependentfiles) {
2047 for (i=0; *driver->dependentfiles[i]; i++) {
2048 if (!strequal(driver->dependentfiles[i], driver->driverpath) &&
2049 !strequal(driver->dependentfiles[i], driver->datafile) &&
2050 !strequal(driver->dependentfiles[i], driver->configfile) &&
2051 !strequal(driver->dependentfiles[i], driver->helpfile)) {
2052 int j;
2053 for (j=0; j < i; j++) {
2054 if (strequal(driver->dependentfiles[i], driver->dependentfiles[j])) {
2055 goto NextDriver;
2059 new_name = talloc_asprintf(ctx,
2060 "%s/%s",
2061 architecture,
2062 driver->dependentfiles[i]);
2063 if (!new_name) {
2064 *perr = WERR_NOMEM;
2065 goto err_exit;
2067 old_name = talloc_asprintf(ctx,
2068 "%s/%s",
2069 new_dir,
2070 driver->dependentfiles[i]);
2071 if (!old_name) {
2072 *perr = WERR_NOMEM;
2073 goto err_exit;
2075 if (ver != -1 && (ver=file_version_is_newer(conn, new_name, old_name)) > 0) {
2076 new_name = driver_unix_convert(conn,new_name,&st);
2077 if (!new_name) {
2078 *perr = WERR_NOMEM;
2079 goto err_exit;
2081 if ( !NT_STATUS_IS_OK(copy_file(ctx,conn, new_name, old_name,
2082 OPENX_FILE_EXISTS_TRUNCATE|
2083 OPENX_FILE_CREATE_IF_NOT_EXIST, 0, False))) {
2084 DEBUG(0,("move_driver_to_download_area: Unable to rename [%s] to [%s]\n",
2085 new_name, old_name));
2086 *perr = WERR_ACCESS_DENIED;
2087 ver = -1;
2091 NextDriver: ;
2095 err_exit:
2097 close_cnum(conn, user->vuid);
2098 unbecome_user();
2100 if (W_ERROR_EQUAL(*perr, WERR_OK)) {
2101 return WERR_OK;
2103 if (ver == -1) {
2104 return WERR_UNKNOWN_PRINTER_DRIVER;
2106 return (*perr);
2109 /****************************************************************************
2110 ****************************************************************************/
2112 static uint32 add_a_printer_driver_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 *driver)
2114 TALLOC_CTX *ctx = talloc_tos();
2115 int len, buflen;
2116 const char *architecture;
2117 char *directory = NULL;
2118 fstring temp_name;
2119 char *key = NULL;
2120 uint8 *buf;
2121 int i, ret;
2122 TDB_DATA dbuf;
2124 architecture = get_short_archi(driver->environment);
2125 if (!architecture) {
2126 return (uint32)-1;
2129 /* The names are relative. We store them in the form: \print$\arch\version\driver.xxx
2130 * \\server is added in the rpc server layer.
2131 * It does make sense to NOT store the server's name in the printer TDB.
2134 directory = talloc_asprintf(ctx, "\\print$\\%s\\%d\\",
2135 architecture, driver->cversion);
2136 if (!directory) {
2137 return (uint32)-1;
2140 /* .inf files do not always list a file for each of the four standard files.
2141 * Don't prepend a path to a null filename, or client claims:
2142 * "The server on which the printer resides does not have a suitable
2143 * <printer driver name> printer driver installed. Click OK if you
2144 * wish to install the driver on your local machine."
2146 if (strlen(driver->driverpath)) {
2147 fstrcpy(temp_name, driver->driverpath);
2148 slprintf(driver->driverpath, sizeof(driver->driverpath)-1, "%s%s", directory, temp_name);
2151 if (strlen(driver->datafile)) {
2152 fstrcpy(temp_name, driver->datafile);
2153 slprintf(driver->datafile, sizeof(driver->datafile)-1, "%s%s", directory, temp_name);
2156 if (strlen(driver->configfile)) {
2157 fstrcpy(temp_name, driver->configfile);
2158 slprintf(driver->configfile, sizeof(driver->configfile)-1, "%s%s", directory, temp_name);
2161 if (strlen(driver->helpfile)) {
2162 fstrcpy(temp_name, driver->helpfile);
2163 slprintf(driver->helpfile, sizeof(driver->helpfile)-1, "%s%s", directory, temp_name);
2166 if (driver->dependentfiles) {
2167 for (i=0; *driver->dependentfiles[i]; i++) {
2168 fstrcpy(temp_name, driver->dependentfiles[i]);
2169 slprintf(driver->dependentfiles[i], sizeof(driver->dependentfiles[i])-1, "%s%s", directory, temp_name);
2173 key = talloc_asprintf(ctx, "%s%s/%d/%s", DRIVERS_PREFIX,
2174 architecture, driver->cversion, driver->name);
2175 if (!key) {
2176 return (uint32)-1;
2179 DEBUG(5,("add_a_printer_driver_3: Adding driver with key %s\n", key ));
2181 buf = NULL;
2182 len = buflen = 0;
2184 again:
2185 len = 0;
2186 len += tdb_pack(buf+len, buflen-len, "dffffffff",
2187 driver->cversion,
2188 driver->name,
2189 driver->environment,
2190 driver->driverpath,
2191 driver->datafile,
2192 driver->configfile,
2193 driver->helpfile,
2194 driver->monitorname,
2195 driver->defaultdatatype);
2197 if (driver->dependentfiles) {
2198 for (i=0; *driver->dependentfiles[i]; i++) {
2199 len += tdb_pack(buf+len, buflen-len, "f",
2200 driver->dependentfiles[i]);
2204 if (len != buflen) {
2205 buf = (uint8 *)SMB_REALLOC(buf, len);
2206 if (!buf) {
2207 DEBUG(0,("add_a_printer_driver_3: failed to enlarge buffer\n!"));
2208 ret = -1;
2209 goto done;
2211 buflen = len;
2212 goto again;
2215 dbuf.dptr = buf;
2216 dbuf.dsize = len;
2218 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
2220 done:
2221 if (ret)
2222 DEBUG(0,("add_a_printer_driver_3: Adding driver with key %s failed.\n", key ));
2224 SAFE_FREE(buf);
2225 return ret;
2228 /****************************************************************************
2229 ****************************************************************************/
2230 static uint32 add_a_printer_driver_6(NT_PRINTER_DRIVER_INFO_LEVEL_6 *driver)
2232 NT_PRINTER_DRIVER_INFO_LEVEL_3 info3;
2234 ZERO_STRUCT(info3);
2235 info3.cversion = driver->version;
2236 fstrcpy(info3.name,driver->name);
2237 fstrcpy(info3.environment,driver->environment);
2238 fstrcpy(info3.driverpath,driver->driverpath);
2239 fstrcpy(info3.datafile,driver->datafile);
2240 fstrcpy(info3.configfile,driver->configfile);
2241 fstrcpy(info3.helpfile,driver->helpfile);
2242 fstrcpy(info3.monitorname,driver->monitorname);
2243 fstrcpy(info3.defaultdatatype,driver->defaultdatatype);
2244 info3.dependentfiles = driver->dependentfiles;
2246 return add_a_printer_driver_3(&info3);
2250 /****************************************************************************
2251 ****************************************************************************/
2252 static WERROR get_a_printer_driver_3_default(NT_PRINTER_DRIVER_INFO_LEVEL_3 **info_ptr, const char *driver, const char *arch)
2254 NT_PRINTER_DRIVER_INFO_LEVEL_3 info;
2256 ZERO_STRUCT(info);
2258 fstrcpy(info.name, driver);
2259 fstrcpy(info.defaultdatatype, "RAW");
2261 fstrcpy(info.driverpath, "");
2262 fstrcpy(info.datafile, "");
2263 fstrcpy(info.configfile, "");
2264 fstrcpy(info.helpfile, "");
2266 if ((info.dependentfiles= SMB_MALLOC_ARRAY(fstring, 2)) == NULL)
2267 return WERR_NOMEM;
2269 memset(info.dependentfiles, '\0', 2*sizeof(fstring));
2270 fstrcpy(info.dependentfiles[0], "");
2272 *info_ptr = (NT_PRINTER_DRIVER_INFO_LEVEL_3 *)memdup(&info, sizeof(info));
2273 if (!*info_ptr) {
2274 SAFE_FREE(info.dependentfiles);
2275 return WERR_NOMEM;
2278 return WERR_OK;
2281 /****************************************************************************
2282 ****************************************************************************/
2283 static WERROR get_a_printer_driver_3(NT_PRINTER_DRIVER_INFO_LEVEL_3 **info_ptr, fstring drivername, const char *arch, uint32 version)
2285 NT_PRINTER_DRIVER_INFO_LEVEL_3 driver;
2286 TDB_DATA dbuf;
2287 const char *architecture;
2288 int len = 0;
2289 int i;
2290 char *key = NULL;
2292 ZERO_STRUCT(driver);
2294 architecture = get_short_archi(arch);
2295 if ( !architecture ) {
2296 return WERR_UNKNOWN_PRINTER_DRIVER;
2299 /* Windows 4.0 (i.e. win9x) should always use a version of 0 */
2301 if ( strcmp( architecture, SPL_ARCH_WIN40 ) == 0 )
2302 version = 0;
2304 DEBUG(8,("get_a_printer_driver_3: [%s%s/%d/%s]\n", DRIVERS_PREFIX, architecture, version, drivername));
2306 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
2307 architecture, version, drivername) < 0) {
2308 return WERR_NOMEM;
2311 dbuf = tdb_fetch_bystring(tdb_drivers, key);
2312 if (!dbuf.dptr) {
2313 SAFE_FREE(key);
2314 return WERR_UNKNOWN_PRINTER_DRIVER;
2317 len += tdb_unpack(dbuf.dptr, dbuf.dsize, "dffffffff",
2318 &driver.cversion,
2319 driver.name,
2320 driver.environment,
2321 driver.driverpath,
2322 driver.datafile,
2323 driver.configfile,
2324 driver.helpfile,
2325 driver.monitorname,
2326 driver.defaultdatatype);
2328 i=0;
2329 while (len < dbuf.dsize) {
2330 driver.dependentfiles = SMB_REALLOC_ARRAY(driver.dependentfiles, fstring, i+2);
2331 if ( !driver.dependentfiles ) {
2332 DEBUG(0,("get_a_printer_driver_3: failed to enlarge buffer!\n"));
2333 break;
2336 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "f",
2337 &driver.dependentfiles[i]);
2338 i++;
2341 if ( driver.dependentfiles )
2342 fstrcpy( driver.dependentfiles[i], "" );
2344 SAFE_FREE(dbuf.dptr);
2345 SAFE_FREE(key);
2347 if (len != dbuf.dsize) {
2348 SAFE_FREE(driver.dependentfiles);
2350 return get_a_printer_driver_3_default(info_ptr, drivername, arch);
2353 *info_ptr = (NT_PRINTER_DRIVER_INFO_LEVEL_3 *)memdup(&driver, sizeof(driver));
2354 if (!*info_ptr) {
2355 SAFE_FREE(driver.dependentfiles);
2356 return WERR_NOMEM;
2359 return WERR_OK;
2362 /****************************************************************************
2363 Debugging function, dump at level 6 the struct in the logs.
2364 ****************************************************************************/
2366 static uint32 dump_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
2368 uint32 result;
2369 NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3;
2370 int i;
2372 DEBUG(20,("Dumping printer driver at level [%d]\n", level));
2374 switch (level)
2376 case 3:
2378 if (driver.info_3 == NULL)
2379 result=5;
2380 else {
2381 info3=driver.info_3;
2383 DEBUGADD(20,("version:[%d]\n", info3->cversion));
2384 DEBUGADD(20,("name:[%s]\n", info3->name));
2385 DEBUGADD(20,("environment:[%s]\n", info3->environment));
2386 DEBUGADD(20,("driverpath:[%s]\n", info3->driverpath));
2387 DEBUGADD(20,("datafile:[%s]\n", info3->datafile));
2388 DEBUGADD(20,("configfile:[%s]\n", info3->configfile));
2389 DEBUGADD(20,("helpfile:[%s]\n", info3->helpfile));
2390 DEBUGADD(20,("monitorname:[%s]\n", info3->monitorname));
2391 DEBUGADD(20,("defaultdatatype:[%s]\n", info3->defaultdatatype));
2393 for (i=0; info3->dependentfiles &&
2394 *info3->dependentfiles[i]; i++) {
2395 DEBUGADD(20,("dependentfile:[%s]\n",
2396 info3->dependentfiles[i]));
2398 result=0;
2400 break;
2402 default:
2403 DEBUGADD(20,("dump_a_printer_driver: Level %u not implemented\n", (unsigned int)level));
2404 result=1;
2405 break;
2408 return result;
2411 /****************************************************************************
2412 ****************************************************************************/
2413 int pack_devicemode(NT_DEVICEMODE *nt_devmode, uint8 *buf, int buflen)
2415 int len = 0;
2417 len += tdb_pack(buf+len, buflen-len, "p", nt_devmode);
2419 if (!nt_devmode)
2420 return len;
2422 len += tdb_pack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2423 nt_devmode->devicename,
2424 nt_devmode->formname,
2426 nt_devmode->specversion,
2427 nt_devmode->driverversion,
2428 nt_devmode->size,
2429 nt_devmode->driverextra,
2430 nt_devmode->orientation,
2431 nt_devmode->papersize,
2432 nt_devmode->paperlength,
2433 nt_devmode->paperwidth,
2434 nt_devmode->scale,
2435 nt_devmode->copies,
2436 nt_devmode->defaultsource,
2437 nt_devmode->printquality,
2438 nt_devmode->color,
2439 nt_devmode->duplex,
2440 nt_devmode->yresolution,
2441 nt_devmode->ttoption,
2442 nt_devmode->collate,
2443 nt_devmode->logpixels,
2445 nt_devmode->fields,
2446 nt_devmode->bitsperpel,
2447 nt_devmode->pelswidth,
2448 nt_devmode->pelsheight,
2449 nt_devmode->displayflags,
2450 nt_devmode->displayfrequency,
2451 nt_devmode->icmmethod,
2452 nt_devmode->icmintent,
2453 nt_devmode->mediatype,
2454 nt_devmode->dithertype,
2455 nt_devmode->reserved1,
2456 nt_devmode->reserved2,
2457 nt_devmode->panningwidth,
2458 nt_devmode->panningheight,
2459 nt_devmode->nt_dev_private);
2461 if (nt_devmode->nt_dev_private) {
2462 len += tdb_pack(buf+len, buflen-len, "B",
2463 nt_devmode->driverextra,
2464 nt_devmode->nt_dev_private);
2467 DEBUG(8,("Packed devicemode [%s]\n", nt_devmode->formname));
2469 return len;
2472 /****************************************************************************
2473 Pack all values in all printer keys
2474 ***************************************************************************/
2476 static int pack_values(NT_PRINTER_DATA *data, uint8 *buf, int buflen)
2478 int len = 0;
2479 int i, j;
2480 REGISTRY_VALUE *val;
2481 REGVAL_CTR *val_ctr;
2482 char *path = NULL;
2483 int num_values;
2485 if ( !data )
2486 return 0;
2488 /* loop over all keys */
2490 for ( i=0; i<data->num_keys; i++ ) {
2491 val_ctr = data->keys[i].values;
2492 num_values = regval_ctr_numvals( val_ctr );
2494 /* pack the keyname followed by a empty value */
2496 len += tdb_pack(buf+len, buflen-len, "pPdB",
2497 &data->keys[i].name,
2498 data->keys[i].name,
2499 REG_NONE,
2501 NULL);
2503 /* now loop over all values */
2505 for ( j=0; j<num_values; j++ ) {
2506 /* pathname should be stored as <key>\<value> */
2508 val = regval_ctr_specific_value( val_ctr, j );
2509 if (asprintf(&path, "%s\\%s",
2510 data->keys[i].name,
2511 regval_name(val)) < 0) {
2512 return -1;
2515 len += tdb_pack(buf+len, buflen-len, "pPdB",
2516 val,
2517 path,
2518 regval_type(val),
2519 regval_size(val),
2520 regval_data_p(val) );
2522 DEBUG(8,("specific: [%s], len: %d\n", regval_name(val), regval_size(val)));
2523 SAFE_FREE(path);
2528 /* terminator */
2530 len += tdb_pack(buf+len, buflen-len, "p", NULL);
2532 return len;
2536 /****************************************************************************
2537 Delete a printer - this just deletes the printer info file, any open
2538 handles are not affected.
2539 ****************************************************************************/
2541 uint32 del_a_printer(const char *sharename)
2543 TDB_DATA kbuf;
2544 char *printdb_path = NULL;
2545 TALLOC_CTX *ctx = talloc_tos();
2547 kbuf = make_printer_tdbkey(ctx, sharename);
2548 tdb_delete(tdb_printers, kbuf);
2550 kbuf= make_printers_secdesc_tdbkey(ctx, sharename);
2551 tdb_delete(tdb_printers, kbuf);
2553 close_all_print_db();
2555 if (geteuid() == 0) {
2556 if (asprintf(&printdb_path, "%s%s.tdb",
2557 lock_path("printing/"),
2558 sharename) < 0) {
2559 return (uint32)-1;
2561 unlink(printdb_path);
2562 SAFE_FREE(printdb_path);
2565 return 0;
2568 /****************************************************************************
2569 ****************************************************************************/
2570 static WERROR update_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info)
2572 uint8 *buf;
2573 int buflen, len;
2574 int retlen;
2575 WERROR ret;
2576 TDB_DATA kbuf, dbuf;
2579 * in addprinter: no servername and the printer is the name
2580 * in setprinter: servername is \\server
2581 * and printer is \\server\\printer
2583 * Samba manages only local printers.
2584 * we currently don't support things like i
2585 * path=\\other_server\printer
2587 * We only store the printername, not \\server\printername
2590 if ( info->servername[0] != '\0' ) {
2591 trim_string(info->printername, info->servername, NULL);
2592 trim_char(info->printername, '\\', '\0');
2593 info->servername[0]='\0';
2597 * JFM: one day I'll forget.
2598 * below that's info->portname because that's the SAMBA sharename
2599 * and I made NT 'thinks' it's the portname
2600 * the info->sharename is the thing you can name when you add a printer
2601 * that's the short-name when you create shared printer for 95/98
2602 * So I've made a limitation in SAMBA: you can only have 1 printer model
2603 * behind a SAMBA share.
2606 buf = NULL;
2607 buflen = 0;
2609 again:
2610 len = 0;
2611 len += tdb_pack(buf+len, buflen-len, "dddddddddddfffffPfffff",
2612 info->attributes,
2613 info->priority,
2614 info->default_priority,
2615 info->starttime,
2616 info->untiltime,
2617 info->status,
2618 info->cjobs,
2619 info->averageppm,
2620 info->changeid,
2621 info->c_setprinter,
2622 info->setuptime,
2623 info->servername,
2624 info->printername,
2625 info->sharename,
2626 info->portname,
2627 info->drivername,
2628 info->comment,
2629 info->location,
2630 info->sepfile,
2631 info->printprocessor,
2632 info->datatype,
2633 info->parameters);
2635 len += pack_devicemode(info->devmode, buf+len, buflen-len);
2636 retlen = pack_values( info->data, buf+len, buflen-len );
2637 if (retlen == -1) {
2638 ret = WERR_NOMEM;
2639 goto done;
2641 len += retlen;
2643 if (buflen != len) {
2644 buf = (uint8 *)SMB_REALLOC(buf, len);
2645 if (!buf) {
2646 DEBUG(0,("update_a_printer_2: failed to enlarge buffer!\n"));
2647 ret = WERR_NOMEM;
2648 goto done;
2650 buflen = len;
2651 goto again;
2654 kbuf = make_printer_tdbkey(talloc_tos(), info->sharename );
2656 dbuf.dptr = buf;
2657 dbuf.dsize = len;
2659 ret = (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) == 0? WERR_OK : WERR_NOMEM);
2661 done:
2662 if (!W_ERROR_IS_OK(ret))
2663 DEBUG(8, ("error updating printer to tdb on disk\n"));
2665 SAFE_FREE(buf);
2667 DEBUG(8,("packed printer [%s] with driver [%s] portname=[%s] len=%d\n",
2668 info->sharename, info->drivername, info->portname, len));
2670 return ret;
2674 /****************************************************************************
2675 Malloc and return an NT devicemode.
2676 ****************************************************************************/
2678 NT_DEVICEMODE *construct_nt_devicemode(const fstring default_devicename)
2681 char adevice[MAXDEVICENAME];
2682 NT_DEVICEMODE *nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE);
2684 if (nt_devmode == NULL) {
2685 DEBUG(0,("construct_nt_devicemode: malloc fail.\n"));
2686 return NULL;
2689 ZERO_STRUCTP(nt_devmode);
2691 slprintf(adevice, sizeof(adevice), "%s", default_devicename);
2692 fstrcpy(nt_devmode->devicename, adevice);
2694 fstrcpy(nt_devmode->formname, "Letter");
2696 nt_devmode->specversion = 0x0401;
2697 nt_devmode->driverversion = 0x0400;
2698 nt_devmode->size = 0x00DC;
2699 nt_devmode->driverextra = 0x0000;
2700 nt_devmode->fields = FORMNAME | TTOPTION | PRINTQUALITY |
2701 DEFAULTSOURCE | COPIES | SCALE |
2702 PAPERSIZE | ORIENTATION;
2703 nt_devmode->orientation = 1;
2704 nt_devmode->papersize = PAPER_LETTER;
2705 nt_devmode->paperlength = 0;
2706 nt_devmode->paperwidth = 0;
2707 nt_devmode->scale = 0x64;
2708 nt_devmode->copies = 1;
2709 nt_devmode->defaultsource = BIN_FORMSOURCE;
2710 nt_devmode->printquality = RES_HIGH; /* 0x0258 */
2711 nt_devmode->color = COLOR_MONOCHROME;
2712 nt_devmode->duplex = DUP_SIMPLEX;
2713 nt_devmode->yresolution = 0;
2714 nt_devmode->ttoption = TT_SUBDEV;
2715 nt_devmode->collate = COLLATE_FALSE;
2716 nt_devmode->icmmethod = 0;
2717 nt_devmode->icmintent = 0;
2718 nt_devmode->mediatype = 0;
2719 nt_devmode->dithertype = 0;
2721 /* non utilisés par un driver d'imprimante */
2722 nt_devmode->logpixels = 0;
2723 nt_devmode->bitsperpel = 0;
2724 nt_devmode->pelswidth = 0;
2725 nt_devmode->pelsheight = 0;
2726 nt_devmode->displayflags = 0;
2727 nt_devmode->displayfrequency = 0;
2728 nt_devmode->reserved1 = 0;
2729 nt_devmode->reserved2 = 0;
2730 nt_devmode->panningwidth = 0;
2731 nt_devmode->panningheight = 0;
2733 nt_devmode->nt_dev_private = NULL;
2734 return nt_devmode;
2737 /****************************************************************************
2738 Deepcopy an NT devicemode.
2739 ****************************************************************************/
2741 NT_DEVICEMODE *dup_nt_devicemode(NT_DEVICEMODE *nt_devicemode)
2743 NT_DEVICEMODE *new_nt_devicemode = NULL;
2745 if ( !nt_devicemode )
2746 return NULL;
2748 if ((new_nt_devicemode = (NT_DEVICEMODE *)memdup(nt_devicemode, sizeof(NT_DEVICEMODE))) == NULL) {
2749 DEBUG(0,("dup_nt_devicemode: malloc fail.\n"));
2750 return NULL;
2753 new_nt_devicemode->nt_dev_private = NULL;
2754 if (nt_devicemode->nt_dev_private != NULL) {
2755 if ((new_nt_devicemode->nt_dev_private = (uint8 *)memdup(nt_devicemode->nt_dev_private, nt_devicemode->driverextra)) == NULL) {
2756 SAFE_FREE(new_nt_devicemode);
2757 DEBUG(0,("dup_nt_devicemode: malloc fail.\n"));
2758 return NULL;
2762 return new_nt_devicemode;
2765 /****************************************************************************
2766 Clean up and deallocate a (maybe partially) allocated NT_DEVICEMODE.
2767 ****************************************************************************/
2769 void free_nt_devicemode(NT_DEVICEMODE **devmode_ptr)
2771 NT_DEVICEMODE *nt_devmode = *devmode_ptr;
2773 if(nt_devmode == NULL)
2774 return;
2776 DEBUG(106,("free_nt_devicemode: deleting DEVMODE\n"));
2778 SAFE_FREE(nt_devmode->nt_dev_private);
2779 SAFE_FREE(*devmode_ptr);
2782 /****************************************************************************
2783 Clean up and deallocate a (maybe partially) allocated NT_PRINTER_INFO_LEVEL_2.
2784 ****************************************************************************/
2786 static void free_nt_printer_info_level_2(NT_PRINTER_INFO_LEVEL_2 **info_ptr)
2788 NT_PRINTER_INFO_LEVEL_2 *info = *info_ptr;
2790 if ( !info )
2791 return;
2793 free_nt_devicemode(&info->devmode);
2795 TALLOC_FREE( *info_ptr );
2799 /****************************************************************************
2800 ****************************************************************************/
2801 int unpack_devicemode(NT_DEVICEMODE **nt_devmode, const uint8 *buf, int buflen)
2803 int len = 0;
2804 int extra_len = 0;
2805 NT_DEVICEMODE devmode;
2807 ZERO_STRUCT(devmode);
2809 len += tdb_unpack(buf+len, buflen-len, "p", nt_devmode);
2811 if (!*nt_devmode) return len;
2813 len += tdb_unpack(buf+len, buflen-len, "ffwwwwwwwwwwwwwwwwwwddddddddddddddp",
2814 devmode.devicename,
2815 devmode.formname,
2817 &devmode.specversion,
2818 &devmode.driverversion,
2819 &devmode.size,
2820 &devmode.driverextra,
2821 &devmode.orientation,
2822 &devmode.papersize,
2823 &devmode.paperlength,
2824 &devmode.paperwidth,
2825 &devmode.scale,
2826 &devmode.copies,
2827 &devmode.defaultsource,
2828 &devmode.printquality,
2829 &devmode.color,
2830 &devmode.duplex,
2831 &devmode.yresolution,
2832 &devmode.ttoption,
2833 &devmode.collate,
2834 &devmode.logpixels,
2836 &devmode.fields,
2837 &devmode.bitsperpel,
2838 &devmode.pelswidth,
2839 &devmode.pelsheight,
2840 &devmode.displayflags,
2841 &devmode.displayfrequency,
2842 &devmode.icmmethod,
2843 &devmode.icmintent,
2844 &devmode.mediatype,
2845 &devmode.dithertype,
2846 &devmode.reserved1,
2847 &devmode.reserved2,
2848 &devmode.panningwidth,
2849 &devmode.panningheight,
2850 &devmode.nt_dev_private);
2852 if (devmode.nt_dev_private) {
2853 /* the len in tdb_unpack is an int value and
2854 * devmode.driverextra is only a short
2856 len += tdb_unpack(buf+len, buflen-len, "B", &extra_len, &devmode.nt_dev_private);
2857 devmode.driverextra=(uint16)extra_len;
2859 /* check to catch an invalid TDB entry so we don't segfault */
2860 if (devmode.driverextra == 0) {
2861 devmode.nt_dev_private = NULL;
2865 *nt_devmode = (NT_DEVICEMODE *)memdup(&devmode, sizeof(devmode));
2866 if (!*nt_devmode) {
2867 SAFE_FREE(devmode.nt_dev_private);
2868 return -1;
2871 DEBUG(8,("Unpacked devicemode [%s](%s)\n", devmode.devicename, devmode.formname));
2872 if (devmode.nt_dev_private)
2873 DEBUG(8,("with a private section of %d bytes\n", devmode.driverextra));
2875 return len;
2878 /****************************************************************************
2879 Allocate and initialize a new slot.
2880 ***************************************************************************/
2882 int add_new_printer_key( NT_PRINTER_DATA *data, const char *name )
2884 NT_PRINTER_KEY *d;
2885 int key_index;
2887 if ( !name || !data )
2888 return -1;
2890 /* allocate another slot in the NT_PRINTER_KEY array */
2892 if ( !(d = TALLOC_REALLOC_ARRAY( data, data->keys, NT_PRINTER_KEY, data->num_keys+1)) ) {
2893 DEBUG(0,("add_new_printer_key: Realloc() failed!\n"));
2894 return -1;
2897 data->keys = d;
2899 key_index = data->num_keys;
2901 /* initialze new key */
2903 data->keys[key_index].name = talloc_strdup( data, name );
2905 if ( !(data->keys[key_index].values = TALLOC_ZERO_P( data, REGVAL_CTR )) )
2906 return -1;
2908 data->num_keys++;
2910 DEBUG(10,("add_new_printer_key: Inserted new data key [%s]\n", name ));
2912 return key_index;
2915 /****************************************************************************
2916 search for a registry key name in the existing printer data
2917 ***************************************************************************/
2919 int delete_printer_key( NT_PRINTER_DATA *data, const char *name )
2921 int i;
2923 for ( i=0; i<data->num_keys; i++ ) {
2924 if ( strequal( data->keys[i].name, name ) ) {
2926 /* cleanup memory */
2928 TALLOC_FREE( data->keys[i].name );
2929 TALLOC_FREE( data->keys[i].values );
2931 /* if not the end of the array, move remaining elements down one slot */
2933 data->num_keys--;
2934 if ( data->num_keys && (i < data->num_keys) )
2935 memmove( &data->keys[i], &data->keys[i+1], sizeof(NT_PRINTER_KEY)*(data->num_keys-i) );
2937 break;
2942 return data->num_keys;
2945 /****************************************************************************
2946 search for a registry key name in the existing printer data
2947 ***************************************************************************/
2949 int lookup_printerkey( NT_PRINTER_DATA *data, const char *name )
2951 int key_index = -1;
2952 int i;
2954 if ( !data || !name )
2955 return -1;
2957 DEBUG(12,("lookup_printerkey: Looking for [%s]\n", name));
2959 /* loop over all existing keys */
2961 for ( i=0; i<data->num_keys; i++ ) {
2962 if ( strequal(data->keys[i].name, name) ) {
2963 DEBUG(12,("lookup_printerkey: Found [%s]!\n", name));
2964 key_index = i;
2965 break;
2970 return key_index;
2973 /****************************************************************************
2974 ***************************************************************************/
2976 int get_printer_subkeys( NT_PRINTER_DATA *data, const char* key, fstring **subkeys )
2978 int i, j;
2979 int key_len;
2980 int num_subkeys = 0;
2981 char *p;
2982 fstring *subkeys_ptr = NULL;
2983 fstring subkeyname;
2985 *subkeys = NULL;
2987 if ( !data )
2988 return 0;
2990 if ( !key )
2991 return -1;
2993 /* special case of asking for the top level printer data registry key names */
2995 if ( strlen(key) == 0 ) {
2996 for ( i=0; i<data->num_keys; i++ ) {
2998 /* found a match, so allocate space and copy the name */
3000 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
3001 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
3002 num_subkeys+1));
3003 return -1;
3006 fstrcpy( subkeys_ptr[num_subkeys], data->keys[i].name );
3007 num_subkeys++;
3010 goto done;
3013 /* asking for the subkeys of some key */
3014 /* subkey paths are stored in the key name using '\' as the delimiter */
3016 for ( i=0; i<data->num_keys; i++ ) {
3017 if ( StrnCaseCmp(data->keys[i].name, key, strlen(key)) == 0 ) {
3019 /* if we found the exact key, then break */
3020 key_len = strlen( key );
3021 if ( strlen(data->keys[i].name) == key_len )
3022 break;
3024 /* get subkey path */
3026 p = data->keys[i].name + key_len;
3027 if ( *p == '\\' )
3028 p++;
3029 fstrcpy( subkeyname, p );
3030 if ( (p = strchr( subkeyname, '\\' )) )
3031 *p = '\0';
3033 /* don't add a key more than once */
3035 for ( j=0; j<num_subkeys; j++ ) {
3036 if ( strequal( subkeys_ptr[j], subkeyname ) )
3037 break;
3040 if ( j != num_subkeys )
3041 continue;
3043 /* found a match, so allocate space and copy the name */
3045 if ( !(subkeys_ptr = SMB_REALLOC_ARRAY( subkeys_ptr, fstring, num_subkeys+2)) ) {
3046 DEBUG(0,("get_printer_subkeys: Realloc failed for [%d] entries!\n",
3047 num_subkeys+1));
3048 return 0;
3051 fstrcpy( subkeys_ptr[num_subkeys], subkeyname );
3052 num_subkeys++;
3057 /* return error if the key was not found */
3059 if ( i == data->num_keys ) {
3060 SAFE_FREE(subkeys_ptr);
3061 return -1;
3064 done:
3065 /* tag off the end */
3067 if (num_subkeys)
3068 fstrcpy(subkeys_ptr[num_subkeys], "" );
3070 *subkeys = subkeys_ptr;
3072 return num_subkeys;
3075 #ifdef HAVE_ADS
3076 static void map_sz_into_ctr(REGVAL_CTR *ctr, const char *val_name,
3077 const char *sz)
3079 smb_ucs2_t conv_str[1024];
3080 size_t str_size;
3082 regval_ctr_delvalue(ctr, val_name);
3083 str_size = push_ucs2(NULL, conv_str, sz, sizeof(conv_str),
3084 STR_TERMINATE | STR_NOALIGN);
3085 regval_ctr_addvalue(ctr, val_name, REG_SZ,
3086 (char *) conv_str, str_size);
3089 static void map_dword_into_ctr(REGVAL_CTR *ctr, const char *val_name,
3090 uint32 dword)
3092 regval_ctr_delvalue(ctr, val_name);
3093 regval_ctr_addvalue(ctr, val_name, REG_DWORD,
3094 (char *) &dword, sizeof(dword));
3097 static void map_bool_into_ctr(REGVAL_CTR *ctr, const char *val_name,
3098 bool b)
3100 uint8 bin_bool = (b ? 1 : 0);
3101 regval_ctr_delvalue(ctr, val_name);
3102 regval_ctr_addvalue(ctr, val_name, REG_BINARY,
3103 (char *) &bin_bool, sizeof(bin_bool));
3106 static void map_single_multi_sz_into_ctr(REGVAL_CTR *ctr, const char *val_name,
3107 const char *multi_sz)
3109 smb_ucs2_t *conv_strs = NULL;
3110 size_t str_size;
3112 /* a multi-sz has to have a null string terminator, i.e., the last
3113 string must be followed by two nulls */
3114 str_size = strlen(multi_sz) + 2;
3115 conv_strs = SMB_CALLOC_ARRAY(smb_ucs2_t, str_size);
3116 if (!conv_strs) {
3117 return;
3120 /* Change to byte units. */
3121 str_size *= sizeof(smb_ucs2_t);
3122 push_ucs2(NULL, conv_strs, multi_sz, str_size,
3123 STR_TERMINATE | STR_NOALIGN);
3125 regval_ctr_delvalue(ctr, val_name);
3126 regval_ctr_addvalue(ctr, val_name, REG_MULTI_SZ,
3127 (char *) conv_strs, str_size);
3128 safe_free(conv_strs);
3132 /****************************************************************************
3133 * Map the NT_PRINTER_INFO_LEVEL_2 data into DsSpooler keys for publishing.
3135 * @param info2 NT_PRINTER_INFO_LEVEL_2 describing printer - gets modified
3136 * @return bool indicating success or failure
3137 ***************************************************************************/
3139 static bool map_nt_printer_info2_to_dsspooler(NT_PRINTER_INFO_LEVEL_2 *info2)
3141 REGVAL_CTR *ctr = NULL;
3142 fstring longname;
3143 const char *dnssuffix;
3144 char *allocated_string = NULL;
3145 const char *ascii_str;
3146 int i;
3148 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3149 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3150 ctr = info2->data->keys[i].values;
3152 map_sz_into_ctr(ctr, SPOOL_REG_PRINTERNAME, info2->sharename);
3153 map_sz_into_ctr(ctr, SPOOL_REG_SHORTSERVERNAME, global_myname());
3155 /* we make the assumption that the netbios name is the same
3156 as the DNS name sinc ethe former will be what we used to
3157 join the domain */
3159 dnssuffix = get_mydnsdomname(talloc_tos());
3160 if (dnssuffix && *dnssuffix) {
3161 fstr_sprintf( longname, "%s.%s", global_myname(), dnssuffix );
3162 } else {
3163 fstrcpy( longname, global_myname() );
3166 map_sz_into_ctr(ctr, SPOOL_REG_SERVERNAME, longname);
3168 asprintf(&allocated_string, "\\\\%s\\%s", longname, info2->sharename);
3169 map_sz_into_ctr(ctr, SPOOL_REG_UNCNAME, allocated_string);
3170 SAFE_FREE(allocated_string);
3172 map_dword_into_ctr(ctr, SPOOL_REG_VERSIONNUMBER, 4);
3173 map_sz_into_ctr(ctr, SPOOL_REG_DRIVERNAME, info2->drivername);
3174 map_sz_into_ctr(ctr, SPOOL_REG_LOCATION, info2->location);
3175 map_sz_into_ctr(ctr, SPOOL_REG_DESCRIPTION, info2->comment);
3176 map_single_multi_sz_into_ctr(ctr, SPOOL_REG_PORTNAME, info2->portname);
3177 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSEPARATORFILE, info2->sepfile);
3178 map_dword_into_ctr(ctr, SPOOL_REG_PRINTSTARTTIME, info2->starttime);
3179 map_dword_into_ctr(ctr, SPOOL_REG_PRINTENDTIME, info2->untiltime);
3180 map_dword_into_ctr(ctr, SPOOL_REG_PRIORITY, info2->priority);
3182 map_bool_into_ctr(ctr, SPOOL_REG_PRINTKEEPPRINTEDJOBS,
3183 (info2->attributes &
3184 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
3186 switch (info2->attributes & 0x3) {
3187 case 0:
3188 ascii_str = SPOOL_REGVAL_PRINTWHILESPOOLING;
3189 break;
3190 case 1:
3191 ascii_str = SPOOL_REGVAL_PRINTAFTERSPOOLED;
3192 break;
3193 case 2:
3194 ascii_str = SPOOL_REGVAL_PRINTDIRECT;
3195 break;
3196 default:
3197 ascii_str = "unknown";
3199 map_sz_into_ctr(ctr, SPOOL_REG_PRINTSPOOLING, ascii_str);
3201 return True;
3204 /*****************************************************************
3205 ****************************************************************/
3207 static void store_printer_guid(NT_PRINTER_INFO_LEVEL_2 *info2,
3208 struct GUID guid)
3210 int i;
3211 REGVAL_CTR *ctr=NULL;
3212 UNISTR2 unistr_guid;
3214 /* find the DsSpooler key */
3215 if ((i = lookup_printerkey(info2->data, SPOOL_DSSPOOLER_KEY)) < 0)
3216 i = add_new_printer_key(info2->data, SPOOL_DSSPOOLER_KEY);
3217 ctr = info2->data->keys[i].values;
3219 regval_ctr_delvalue(ctr, "objectGUID");
3221 /* We used to store this as a REG_BINARY but that causes
3222 Vista to whine */
3224 ZERO_STRUCT( unistr_guid );
3226 init_unistr2( &unistr_guid, smb_uuid_string(talloc_tos(), guid),
3227 UNI_STR_TERMINATE );
3229 regval_ctr_addvalue(ctr, "objectGUID", REG_SZ,
3230 (char *)unistr_guid.buffer,
3231 unistr_guid.uni_max_len*2);
3235 static WERROR nt_printer_publish_ads(ADS_STRUCT *ads,
3236 NT_PRINTER_INFO_LEVEL *printer)
3238 ADS_STATUS ads_rc;
3239 LDAPMessage *res;
3240 char *prt_dn = NULL, *srv_dn, *srv_cn_0, *srv_cn_escaped, *sharename_escaped;
3241 char *srv_dn_utf8, **srv_cn_utf8;
3242 TALLOC_CTX *ctx;
3243 ADS_MODLIST mods;
3244 const char *attrs[] = {"objectGUID", NULL};
3245 struct GUID guid;
3246 WERROR win_rc = WERR_OK;
3248 DEBUG(5, ("publishing printer %s\n", printer->info_2->printername));
3250 /* figure out where to publish */
3251 ads_find_machine_acct(ads, &res, global_myname());
3253 /* We use ldap_get_dn here as we need the answer
3254 * in utf8 to call ldap_explode_dn(). JRA. */
3256 srv_dn_utf8 = ldap_get_dn((LDAP *)ads->ldap.ld, (LDAPMessage *)res);
3257 if (!srv_dn_utf8) {
3258 ads_destroy(&ads);
3259 return WERR_SERVER_UNAVAILABLE;
3261 ads_msgfree(ads, res);
3262 srv_cn_utf8 = ldap_explode_dn(srv_dn_utf8, 1);
3263 if (!srv_cn_utf8) {
3264 ldap_memfree(srv_dn_utf8);
3265 ads_destroy(&ads);
3266 return WERR_SERVER_UNAVAILABLE;
3268 /* Now convert to CH_UNIX. */
3269 if (pull_utf8_allocate(&srv_dn, srv_dn_utf8) == (size_t)-1) {
3270 ldap_memfree(srv_dn_utf8);
3271 ldap_memfree(srv_cn_utf8);
3272 ads_destroy(&ads);
3273 return WERR_SERVER_UNAVAILABLE;
3275 if (pull_utf8_allocate(&srv_cn_0, srv_cn_utf8[0]) == (size_t)-1) {
3276 ldap_memfree(srv_dn_utf8);
3277 ldap_memfree(srv_cn_utf8);
3278 ads_destroy(&ads);
3279 SAFE_FREE(srv_dn);
3280 return WERR_SERVER_UNAVAILABLE;
3283 ldap_memfree(srv_dn_utf8);
3284 ldap_memfree(srv_cn_utf8);
3286 srv_cn_escaped = escape_rdn_val_string_alloc(srv_cn_0);
3287 if (!srv_cn_escaped) {
3288 SAFE_FREE(srv_cn_0);
3289 ldap_memfree(srv_dn_utf8);
3290 ads_destroy(&ads);
3291 return WERR_SERVER_UNAVAILABLE;
3293 sharename_escaped = escape_rdn_val_string_alloc(printer->info_2->sharename);
3294 if (!sharename_escaped) {
3295 SAFE_FREE(srv_cn_escaped);
3296 SAFE_FREE(srv_cn_0);
3297 ldap_memfree(srv_dn_utf8);
3298 ads_destroy(&ads);
3299 return WERR_SERVER_UNAVAILABLE;
3303 asprintf(&prt_dn, "cn=%s-%s,%s", srv_cn_escaped, sharename_escaped, srv_dn);
3305 SAFE_FREE(srv_dn);
3306 SAFE_FREE(srv_cn_0);
3307 SAFE_FREE(srv_cn_escaped);
3308 SAFE_FREE(sharename_escaped);
3310 /* build the ads mods */
3311 ctx = talloc_init("nt_printer_publish_ads");
3312 if (ctx == NULL) {
3313 SAFE_FREE(prt_dn);
3314 return WERR_NOMEM;
3317 mods = ads_init_mods(ctx);
3319 if (mods == NULL) {
3320 SAFE_FREE(prt_dn);
3321 talloc_destroy(ctx);
3322 return WERR_NOMEM;
3325 get_local_printer_publishing_data(ctx, &mods, printer->info_2->data);
3326 ads_mod_str(ctx, &mods, SPOOL_REG_PRINTERNAME,
3327 printer->info_2->sharename);
3329 /* publish it */
3330 ads_rc = ads_mod_printer_entry(ads, prt_dn, ctx, &mods);
3331 if (ads_rc.err.rc == LDAP_NO_SUCH_OBJECT) {
3332 int i;
3333 for (i=0; mods[i] != 0; i++)
3335 mods[i] = (LDAPMod *)-1;
3336 ads_rc = ads_add_printer_entry(ads, prt_dn, ctx, &mods);
3339 if (!ADS_ERR_OK(ads_rc))
3340 DEBUG(3, ("error publishing %s: %s\n", printer->info_2->sharename, ads_errstr(ads_rc)));
3342 talloc_destroy(ctx);
3344 /* retreive the guid and store it locally */
3345 if (ADS_ERR_OK(ads_search_dn(ads, &res, prt_dn, attrs))) {
3346 ZERO_STRUCT(guid);
3347 ads_pull_guid(ads, res, &guid);
3348 ads_msgfree(ads, res);
3349 store_printer_guid(printer->info_2, guid);
3350 win_rc = mod_a_printer(printer, 2);
3353 SAFE_FREE(prt_dn);
3354 return win_rc;
3357 static WERROR nt_printer_unpublish_ads(ADS_STRUCT *ads,
3358 NT_PRINTER_INFO_LEVEL *printer)
3360 ADS_STATUS ads_rc;
3361 LDAPMessage *res;
3362 char *prt_dn = NULL;
3364 DEBUG(5, ("unpublishing printer %s\n", printer->info_2->printername));
3366 /* remove the printer from the directory */
3367 ads_rc = ads_find_printer_on_server(ads, &res,
3368 printer->info_2->sharename, global_myname());
3370 if (ADS_ERR_OK(ads_rc) && ads_count_replies(ads, res)) {
3371 prt_dn = ads_get_dn(ads, res);
3372 if (!prt_dn) {
3373 ads_msgfree(ads, res);
3374 return WERR_NOMEM;
3376 ads_rc = ads_del_dn(ads, prt_dn);
3377 ads_memfree(ads, prt_dn);
3380 ads_msgfree(ads, res);
3381 return WERR_OK;
3384 /****************************************************************************
3385 * Publish a printer in the directory
3387 * @param snum describing printer service
3388 * @return WERROR indicating status of publishing
3389 ***************************************************************************/
3391 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3393 ADS_STATUS ads_rc;
3394 ADS_STRUCT *ads = NULL;
3395 NT_PRINTER_INFO_LEVEL *printer = NULL;
3396 WERROR win_rc;
3398 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3399 if (!W_ERROR_IS_OK(win_rc))
3400 goto done;
3402 switch (action) {
3403 case SPOOL_DS_PUBLISH:
3404 case SPOOL_DS_UPDATE:
3405 /* set the DsSpooler info and attributes */
3406 if (!(map_nt_printer_info2_to_dsspooler(printer->info_2))) {
3407 win_rc = WERR_NOMEM;
3408 goto done;
3411 printer->info_2->attributes |= PRINTER_ATTRIBUTE_PUBLISHED;
3412 break;
3413 case SPOOL_DS_UNPUBLISH:
3414 printer->info_2->attributes ^= PRINTER_ATTRIBUTE_PUBLISHED;
3415 break;
3416 default:
3417 win_rc = WERR_NOT_SUPPORTED;
3418 goto done;
3421 win_rc = mod_a_printer(printer, 2);
3422 if (!W_ERROR_IS_OK(win_rc)) {
3423 DEBUG(3, ("err %d saving data\n", W_ERROR_V(win_rc)));
3424 goto done;
3427 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3428 if (!ads) {
3429 DEBUG(3, ("ads_init() failed\n"));
3430 win_rc = WERR_SERVER_UNAVAILABLE;
3431 goto done;
3433 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3434 SAFE_FREE(ads->auth.password);
3435 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3436 NULL, NULL);
3438 /* ads_connect() will find the DC for us */
3439 ads_rc = ads_connect(ads);
3440 if (!ADS_ERR_OK(ads_rc)) {
3441 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3442 win_rc = WERR_ACCESS_DENIED;
3443 goto done;
3446 switch (action) {
3447 case SPOOL_DS_PUBLISH:
3448 case SPOOL_DS_UPDATE:
3449 win_rc = nt_printer_publish_ads(ads, printer);
3450 break;
3451 case SPOOL_DS_UNPUBLISH:
3452 win_rc = nt_printer_unpublish_ads(ads, printer);
3453 break;
3456 done:
3457 free_a_printer(&printer, 2);
3458 ads_destroy(&ads);
3459 return win_rc;
3462 WERROR check_published_printers(void)
3464 ADS_STATUS ads_rc;
3465 ADS_STRUCT *ads = NULL;
3466 int snum;
3467 int n_services = lp_numservices();
3468 NT_PRINTER_INFO_LEVEL *printer = NULL;
3470 ads = ads_init(lp_realm(), lp_workgroup(), NULL);
3471 if (!ads) {
3472 DEBUG(3, ("ads_init() failed\n"));
3473 return WERR_SERVER_UNAVAILABLE;
3475 setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
3476 SAFE_FREE(ads->auth.password);
3477 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
3478 NULL, NULL);
3480 /* ads_connect() will find the DC for us */
3481 ads_rc = ads_connect(ads);
3482 if (!ADS_ERR_OK(ads_rc)) {
3483 DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_rc)));
3484 ads_destroy(&ads);
3485 ads_kdestroy("MEMORY:prtpub_cache");
3486 return WERR_ACCESS_DENIED;
3489 for (snum = 0; snum < n_services; snum++) {
3490 if (!(lp_snum_ok(snum) && lp_print_ok(snum)))
3491 continue;
3493 if (W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2,
3494 lp_servicename(snum))) &&
3495 (printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED))
3496 nt_printer_publish_ads(ads, printer);
3498 free_a_printer(&printer, 2);
3501 ads_destroy(&ads);
3502 ads_kdestroy("MEMORY:prtpub_cache");
3503 return WERR_OK;
3506 bool is_printer_published(Printer_entry *print_hnd, int snum,
3507 struct GUID *guid)
3509 NT_PRINTER_INFO_LEVEL *printer = NULL;
3510 REGVAL_CTR *ctr;
3511 REGISTRY_VALUE *guid_val;
3512 WERROR win_rc;
3513 int i;
3514 bool ret = False;
3516 win_rc = get_a_printer(print_hnd, &printer, 2, lp_servicename(snum));
3518 if (!W_ERROR_IS_OK(win_rc) ||
3519 !(printer->info_2->attributes & PRINTER_ATTRIBUTE_PUBLISHED) ||
3520 ((i = lookup_printerkey(printer->info_2->data, SPOOL_DSSPOOLER_KEY)) < 0) ||
3521 !(ctr = printer->info_2->data->keys[i].values) ||
3522 !(guid_val = regval_ctr_getvalue(ctr, "objectGUID")))
3524 free_a_printer(&printer, 2);
3525 return False;
3528 /* fetching printer guids really ought to be a separate function. */
3530 if ( guid ) {
3531 fstring guid_str;
3533 /* We used to store the guid as REG_BINARY, then swapped
3534 to REG_SZ for Vista compatibility so check for both */
3536 switch ( regval_type(guid_val) ){
3537 case REG_SZ:
3538 rpcstr_pull( guid_str, regval_data_p(guid_val),
3539 sizeof(guid_str)-1, -1, STR_TERMINATE );
3540 ret = smb_string_to_uuid( guid_str, guid );
3541 break;
3542 case REG_BINARY:
3543 if ( regval_size(guid_val) != sizeof(struct GUID) ) {
3544 ret = False;
3545 break;
3547 memcpy(guid, regval_data_p(guid_val), sizeof(struct GUID));
3548 break;
3549 default:
3550 DEBUG(0,("is_printer_published: GUID value stored as "
3551 "invaluid type (%d)\n", regval_type(guid_val) ));
3552 break;
3556 free_a_printer(&printer, 2);
3557 return ret;
3559 #else
3560 WERROR nt_printer_publish(Printer_entry *print_hnd, int snum, int action)
3562 return WERR_OK;
3565 WERROR check_published_printers(void)
3567 return WERR_OK;
3570 bool is_printer_published(Printer_entry *print_hnd, int snum,
3571 struct GUID *guid)
3573 return False;
3575 #endif /* HAVE_ADS */
3577 /****************************************************************************
3578 ***************************************************************************/
3580 WERROR delete_all_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key )
3582 NT_PRINTER_DATA *data;
3583 int i;
3584 int removed_keys = 0;
3585 int empty_slot;
3587 data = p2->data;
3588 empty_slot = data->num_keys;
3590 if ( !key )
3591 return WERR_INVALID_PARAM;
3593 /* remove all keys */
3595 if ( !strlen(key) ) {
3597 TALLOC_FREE( data );
3599 p2->data = NULL;
3601 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from printer [%s]\n",
3602 p2->printername ));
3604 return WERR_OK;
3607 /* remove a specific key (and all subkeys) */
3609 for ( i=0; i<data->num_keys; i++ ) {
3610 if ( StrnCaseCmp( data->keys[i].name, key, strlen(key)) == 0 ) {
3611 DEBUG(8,("delete_all_printer_data: Removed all Printer Data from key [%s]\n",
3612 data->keys[i].name));
3614 TALLOC_FREE( data->keys[i].name );
3615 TALLOC_FREE( data->keys[i].values );
3617 /* mark the slot as empty */
3619 ZERO_STRUCTP( &data->keys[i] );
3623 /* find the first empty slot */
3625 for ( i=0; i<data->num_keys; i++ ) {
3626 if ( !data->keys[i].name ) {
3627 empty_slot = i;
3628 removed_keys++;
3629 break;
3633 if ( i == data->num_keys )
3634 /* nothing was removed */
3635 return WERR_INVALID_PARAM;
3637 /* move everything down */
3639 for ( i=empty_slot+1; i<data->num_keys; i++ ) {
3640 if ( data->keys[i].name ) {
3641 memcpy( &data->keys[empty_slot], &data->keys[i], sizeof(NT_PRINTER_KEY) );
3642 ZERO_STRUCTP( &data->keys[i] );
3643 empty_slot++;
3644 removed_keys++;
3648 /* update count */
3650 data->num_keys -= removed_keys;
3652 /* sanity check to see if anything is left */
3654 if ( !data->num_keys ) {
3655 DEBUG(8,("delete_all_printer_data: No keys left for printer [%s]\n", p2->printername ));
3657 SAFE_FREE( data->keys );
3658 ZERO_STRUCTP( data );
3661 return WERR_OK;
3664 /****************************************************************************
3665 ***************************************************************************/
3667 WERROR delete_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3669 WERROR result = WERR_OK;
3670 int key_index;
3672 /* we must have names on non-zero length */
3674 if ( !key || !*key|| !value || !*value )
3675 return WERR_INVALID_NAME;
3677 /* find the printer key first */
3679 key_index = lookup_printerkey( p2->data, key );
3680 if ( key_index == -1 )
3681 return WERR_OK;
3683 /* make sure the value exists so we can return the correct error code */
3685 if ( !regval_ctr_getvalue( p2->data->keys[key_index].values, value ) )
3686 return WERR_BADFILE;
3688 regval_ctr_delvalue( p2->data->keys[key_index].values, value );
3690 DEBUG(8,("delete_printer_data: Removed key => [%s], value => [%s]\n",
3691 key, value ));
3693 return result;
3696 /****************************************************************************
3697 ***************************************************************************/
3699 WERROR add_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value,
3700 uint32 type, uint8 *data, int real_len )
3702 WERROR result = WERR_OK;
3703 int key_index;
3705 /* we must have names on non-zero length */
3707 if ( !key || !*key|| !value || !*value )
3708 return WERR_INVALID_NAME;
3710 /* find the printer key first */
3712 key_index = lookup_printerkey( p2->data, key );
3713 if ( key_index == -1 )
3714 key_index = add_new_printer_key( p2->data, key );
3716 if ( key_index == -1 )
3717 return WERR_NOMEM;
3719 regval_ctr_addvalue( p2->data->keys[key_index].values, value,
3720 type, (const char *)data, real_len );
3722 DEBUG(8,("add_printer_data: Added key => [%s], value => [%s], type=> [%d], size => [%d]\n",
3723 key, value, type, real_len ));
3725 return result;
3728 /****************************************************************************
3729 ***************************************************************************/
3731 REGISTRY_VALUE* get_printer_data( NT_PRINTER_INFO_LEVEL_2 *p2, const char *key, const char *value )
3733 int key_index;
3735 if ( (key_index = lookup_printerkey( p2->data, key )) == -1 )
3736 return NULL;
3738 DEBUG(8,("get_printer_data: Attempting to lookup key => [%s], value => [%s]\n",
3739 key, value ));
3741 return regval_ctr_getvalue( p2->data->keys[key_index].values, value );
3744 /****************************************************************************
3745 Unpack a list of registry values frem the TDB
3746 ***************************************************************************/
3748 static int unpack_values(NT_PRINTER_DATA *printer_data, const uint8 *buf, int buflen)
3750 int len = 0;
3751 uint32 type;
3752 fstring string;
3753 const char *valuename = NULL;
3754 const char *keyname = NULL;
3755 char *str;
3756 int size;
3757 uint8 *data_p;
3758 REGISTRY_VALUE *regval_p;
3759 int key_index;
3761 /* add the "PrinterDriverData" key first for performance reasons */
3763 add_new_printer_key( printer_data, SPOOL_PRINTERDATA_KEY );
3765 /* loop and unpack the rest of the registry values */
3767 while ( True ) {
3769 /* check to see if there are any more registry values */
3771 regval_p = NULL;
3772 len += tdb_unpack(buf+len, buflen-len, "p", &regval_p);
3773 if ( !regval_p )
3774 break;
3776 /* unpack the next regval */
3778 len += tdb_unpack(buf+len, buflen-len, "fdB",
3779 string,
3780 &type,
3781 &size,
3782 &data_p);
3784 /* lookup for subkey names which have a type of REG_NONE */
3785 /* there's no data with this entry */
3787 if ( type == REG_NONE ) {
3788 if ( (key_index=lookup_printerkey( printer_data, string)) == -1 )
3789 add_new_printer_key( printer_data, string );
3790 continue;
3794 * break of the keyname from the value name.
3795 * Valuenames can have embedded '\'s so be careful.
3796 * only support one level of keys. See the
3797 * "Konica Fiery S300 50C-K v1.1. enu" 2k driver.
3798 * -- jerry
3801 str = strchr_m( string, '\\');
3803 /* Put in "PrinterDriverData" is no key specified */
3805 if ( !str ) {
3806 keyname = SPOOL_PRINTERDATA_KEY;
3807 valuename = string;
3809 else {
3810 *str = '\0';
3811 keyname = string;
3812 valuename = str+1;
3815 /* see if we need a new key */
3817 if ( (key_index=lookup_printerkey( printer_data, keyname )) == -1 )
3818 key_index = add_new_printer_key( printer_data, keyname );
3820 if ( key_index == -1 ) {
3821 DEBUG(0,("unpack_values: Failed to allocate a new key [%s]!\n",
3822 keyname));
3823 break;
3826 DEBUG(8,("specific: [%s:%s], len: %d\n", keyname, valuename, size));
3828 /* Vista doesn't like unknown REG_BINARY values in DsSpooler.
3829 Thanks to Martin Zielinski for the hint. */
3831 if ( type == REG_BINARY &&
3832 strequal( keyname, SPOOL_DSSPOOLER_KEY ) &&
3833 strequal( valuename, "objectGUID" ) )
3835 struct GUID guid;
3836 UNISTR2 unistr_guid;
3838 ZERO_STRUCT( unistr_guid );
3840 /* convert the GUID to a UNICODE string */
3842 memcpy( &guid, data_p, sizeof(struct GUID) );
3844 init_unistr2( &unistr_guid,
3845 smb_uuid_string(talloc_tos(), guid),
3846 UNI_STR_TERMINATE );
3848 regval_ctr_addvalue( printer_data->keys[key_index].values,
3849 valuename, REG_SZ,
3850 (const char *)unistr_guid.buffer,
3851 unistr_guid.uni_str_len*2 );
3853 } else {
3854 /* add the value */
3856 regval_ctr_addvalue( printer_data->keys[key_index].values,
3857 valuename, type, (const char *)data_p,
3858 size );
3861 SAFE_FREE(data_p); /* 'B' option to tdbpack does a malloc() */
3865 return len;
3868 /****************************************************************************
3869 ***************************************************************************/
3871 static char *last_from;
3872 static char *last_to;
3874 static const char *get_last_from(void)
3876 if (!last_from) {
3877 return "";
3879 return last_from;
3882 static const char *get_last_to(void)
3884 if (!last_to) {
3885 return "";
3887 return last_to;
3890 static bool set_last_from_to(const char *from, const char *to)
3892 char *orig_from = last_from;
3893 char *orig_to = last_to;
3895 last_from = SMB_STRDUP(from);
3896 last_to = SMB_STRDUP(to);
3898 SAFE_FREE(orig_from);
3899 SAFE_FREE(orig_to);
3901 if (!last_from || !last_to) {
3902 SAFE_FREE(last_from);
3903 SAFE_FREE(last_to);
3904 return false;
3906 return true;
3909 static void map_to_os2_driver(fstring drivername)
3911 char *mapfile = lp_os2_driver_map();
3912 char **lines = NULL;
3913 int numlines = 0;
3914 int i;
3916 if (!strlen(drivername))
3917 return;
3919 if (!*mapfile)
3920 return;
3922 if (strequal(drivername,get_last_from())) {
3923 DEBUG(3,("Mapped Windows driver %s to OS/2 driver %s\n",
3924 drivername,get_last_to()));
3925 fstrcpy(drivername,get_last_to());
3926 return;
3929 lines = file_lines_load(mapfile, &numlines,0);
3930 if (numlines == 0 || lines == NULL) {
3931 DEBUG(0,("No entries in OS/2 driver map %s\n",mapfile));
3932 SAFE_FREE(lines);
3933 return;
3936 DEBUG(4,("Scanning OS/2 driver map %s\n",mapfile));
3938 for( i = 0; i < numlines; i++) {
3939 char *nt_name = lines[i];
3940 char *os2_name = strchr(nt_name,'=');
3942 if (!os2_name)
3943 continue;
3945 *os2_name++ = 0;
3947 while (isspace(*nt_name))
3948 nt_name++;
3950 if (!*nt_name || strchr("#;",*nt_name))
3951 continue;
3954 int l = strlen(nt_name);
3955 while (l && isspace(nt_name[l-1])) {
3956 nt_name[l-1] = 0;
3957 l--;
3961 while (isspace(*os2_name))
3962 os2_name++;
3965 int l = strlen(os2_name);
3966 while (l && isspace(os2_name[l-1])) {
3967 os2_name[l-1] = 0;
3968 l--;
3972 if (strequal(nt_name,drivername)) {
3973 DEBUG(3,("Mapped windows driver %s to os2 driver%s\n",drivername,os2_name));
3974 set_last_from_to(drivername,os2_name);
3975 fstrcpy(drivername,os2_name);
3976 file_lines_free(lines);
3977 return;
3981 file_lines_free(lines);
3984 /****************************************************************************
3985 Get a default printer info 2 struct.
3986 ****************************************************************************/
3988 static WERROR get_a_printer_2_default(NT_PRINTER_INFO_LEVEL_2 *info,
3989 const char *servername,
3990 const char* sharename,
3991 bool get_loc_com)
3993 int snum = lp_servicenumber(sharename);
3995 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
3996 slprintf(info->printername, sizeof(info->printername)-1, "\\\\%s\\%s",
3997 servername, sharename);
3998 fstrcpy(info->sharename, sharename);
3999 fstrcpy(info->portname, SAMBA_PRINTER_PORT_NAME);
4001 /* by setting the driver name to an empty string, a local NT admin
4002 can now run the **local** APW to install a local printer driver
4003 for a Samba shared printer in 2.2. Without this, drivers **must** be
4004 installed on the Samba server for NT clients --jerry */
4005 #if 0 /* JERRY --do not uncomment-- */
4006 if (!*info->drivername)
4007 fstrcpy(info->drivername, "NO DRIVER AVAILABLE FOR THIS PRINTER");
4008 #endif
4011 DEBUG(10,("get_a_printer_2_default: driver name set to [%s]\n", info->drivername));
4013 strlcpy(info->comment, "", sizeof(info->comment));
4014 fstrcpy(info->printprocessor, "winprint");
4015 fstrcpy(info->datatype, "RAW");
4017 #ifdef HAVE_CUPS
4018 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
4019 /* Pull the location and comment strings from cups if we don't
4020 already have one */
4021 if ( !strlen(info->location) || !strlen(info->comment) )
4022 cups_pull_comment_location( info );
4024 #endif
4026 info->attributes = PRINTER_ATTRIBUTE_SAMBA;
4028 info->starttime = 0; /* Minutes since 12:00am GMT */
4029 info->untiltime = 0; /* Minutes since 12:00am GMT */
4030 info->priority = 1;
4031 info->default_priority = 1;
4032 info->setuptime = (uint32)time(NULL);
4035 * I changed this as I think it is better to have a generic
4036 * DEVMODE than to crash Win2k explorer.exe --jerry
4037 * See the HP Deskjet 990c Win2k drivers for an example.
4039 * However the default devmode appears to cause problems
4040 * with the HP CLJ 8500 PCL driver. Hence the addition of
4041 * the "default devmode" parameter --jerry 22/01/2002
4044 if (lp_default_devmode(snum)) {
4045 if ((info->devmode = construct_nt_devicemode(info->printername)) == NULL) {
4046 goto fail;
4048 } else {
4049 info->devmode = NULL;
4052 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
4053 goto fail;
4056 return WERR_OK;
4058 fail:
4059 if (info->devmode)
4060 free_nt_devicemode(&info->devmode);
4062 return WERR_ACCESS_DENIED;
4065 /****************************************************************************
4066 ****************************************************************************/
4068 static WERROR get_a_printer_2(NT_PRINTER_INFO_LEVEL_2 *info,
4069 const char *servername,
4070 const char *sharename,
4071 bool get_loc_com)
4073 int len = 0;
4074 int snum = lp_servicenumber(sharename);
4075 TDB_DATA kbuf, dbuf;
4076 fstring printername;
4077 char adevice[MAXDEVICENAME];
4078 char *comment = NULL;
4080 kbuf = make_printer_tdbkey(talloc_tos(), sharename);
4082 dbuf = tdb_fetch(tdb_printers, kbuf);
4083 if (!dbuf.dptr) {
4084 return get_a_printer_2_default(info, servername,
4085 sharename, get_loc_com);
4088 len += tdb_unpack(dbuf.dptr+len, dbuf.dsize-len, "dddddddddddfffffPfffff",
4089 &info->attributes,
4090 &info->priority,
4091 &info->default_priority,
4092 &info->starttime,
4093 &info->untiltime,
4094 &info->status,
4095 &info->cjobs,
4096 &info->averageppm,
4097 &info->changeid,
4098 &info->c_setprinter,
4099 &info->setuptime,
4100 info->servername,
4101 info->printername,
4102 info->sharename,
4103 info->portname,
4104 info->drivername,
4105 &comment,
4106 info->location,
4107 info->sepfile,
4108 info->printprocessor,
4109 info->datatype,
4110 info->parameters);
4112 if (comment) {
4113 strlcpy(info->comment, comment, sizeof(info->comment));
4114 SAFE_FREE(comment);
4117 /* Samba has to have shared raw drivers. */
4118 info->attributes |= PRINTER_ATTRIBUTE_SAMBA;
4119 info->attributes &= ~PRINTER_ATTRIBUTE_NOT_SAMBA;
4121 /* Restore the stripped strings. */
4122 slprintf(info->servername, sizeof(info->servername)-1, "\\\\%s", servername);
4124 if ( lp_force_printername(snum) ) {
4125 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, sharename );
4126 } else {
4127 slprintf(printername, sizeof(printername)-1, "\\\\%s\\%s", servername, info->printername);
4130 fstrcpy(info->printername, printername);
4132 #ifdef HAVE_CUPS
4133 if (get_loc_com && (enum printing_types)lp_printing(snum) == PRINT_CUPS ) {
4134 /* Pull the location and comment strings from cups if we don't
4135 already have one */
4136 if ( !strlen(info->location) || !strlen(info->comment) )
4137 cups_pull_comment_location( info );
4139 #endif
4141 len += unpack_devicemode(&info->devmode,dbuf.dptr+len, dbuf.dsize-len);
4144 * Some client drivers freak out if there is a NULL devmode
4145 * (probably the driver is not checking before accessing
4146 * the devmode pointer) --jerry
4148 * See comments in get_a_printer_2_default()
4151 if (lp_default_devmode(snum) && !info->devmode) {
4152 DEBUG(8,("get_a_printer_2: Constructing a default device mode for [%s]\n",
4153 printername));
4154 info->devmode = construct_nt_devicemode(printername);
4157 slprintf( adevice, sizeof(adevice), "%s", info->printername );
4158 if (info->devmode) {
4159 fstrcpy(info->devmode->devicename, adevice);
4162 if ( !(info->data = TALLOC_ZERO_P( info, NT_PRINTER_DATA )) ) {
4163 DEBUG(0,("unpack_values: talloc() failed!\n"));
4164 SAFE_FREE(dbuf.dptr);
4165 return WERR_NOMEM;
4167 len += unpack_values( info->data, dbuf.dptr+len, dbuf.dsize-len );
4169 /* This will get the current RPC talloc context, but we should be
4170 passing this as a parameter... fixme... JRA ! */
4172 if (!nt_printing_getsec(info, sharename, &info->secdesc_buf)) {
4173 SAFE_FREE(dbuf.dptr);
4174 return WERR_NOMEM;
4177 /* Fix for OS/2 drivers. */
4179 if (get_remote_arch() == RA_OS2) {
4180 map_to_os2_driver(info->drivername);
4183 SAFE_FREE(dbuf.dptr);
4185 DEBUG(9,("Unpacked printer [%s] name [%s] running driver [%s]\n",
4186 sharename, info->printername, info->drivername));
4188 return WERR_OK;
4191 /****************************************************************************
4192 Debugging function, dump at level 6 the struct in the logs.
4193 ****************************************************************************/
4194 static uint32 dump_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4196 uint32 result;
4197 NT_PRINTER_INFO_LEVEL_2 *info2;
4199 DEBUG(106,("Dumping printer at level [%d]\n", level));
4201 switch (level) {
4202 case 2:
4204 if (printer->info_2 == NULL)
4205 result=5;
4206 else
4208 info2=printer->info_2;
4210 DEBUGADD(106,("attributes:[%d]\n", info2->attributes));
4211 DEBUGADD(106,("priority:[%d]\n", info2->priority));
4212 DEBUGADD(106,("default_priority:[%d]\n", info2->default_priority));
4213 DEBUGADD(106,("starttime:[%d]\n", info2->starttime));
4214 DEBUGADD(106,("untiltime:[%d]\n", info2->untiltime));
4215 DEBUGADD(106,("status:[%d]\n", info2->status));
4216 DEBUGADD(106,("cjobs:[%d]\n", info2->cjobs));
4217 DEBUGADD(106,("averageppm:[%d]\n", info2->averageppm));
4218 DEBUGADD(106,("changeid:[%d]\n", info2->changeid));
4219 DEBUGADD(106,("c_setprinter:[%d]\n", info2->c_setprinter));
4220 DEBUGADD(106,("setuptime:[%d]\n", info2->setuptime));
4222 DEBUGADD(106,("servername:[%s]\n", info2->servername));
4223 DEBUGADD(106,("printername:[%s]\n", info2->printername));
4224 DEBUGADD(106,("sharename:[%s]\n", info2->sharename));
4225 DEBUGADD(106,("portname:[%s]\n", info2->portname));
4226 DEBUGADD(106,("drivername:[%s]\n", info2->drivername));
4227 DEBUGADD(106,("comment:[%s]\n", info2->comment));
4228 DEBUGADD(106,("location:[%s]\n", info2->location));
4229 DEBUGADD(106,("sepfile:[%s]\n", info2->sepfile));
4230 DEBUGADD(106,("printprocessor:[%s]\n", info2->printprocessor));
4231 DEBUGADD(106,("datatype:[%s]\n", info2->datatype));
4232 DEBUGADD(106,("parameters:[%s]\n", info2->parameters));
4233 result=0;
4235 break;
4237 default:
4238 DEBUGADD(106,("dump_a_printer: Level %u not implemented\n", (unsigned int)level ));
4239 result=1;
4240 break;
4243 return result;
4246 /****************************************************************************
4247 Update the changeid time.
4248 This is SO NASTY as some drivers need this to change, others need it
4249 static. This value will change every second, and I must hope that this
4250 is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
4251 UTAH ! JRA.
4252 ****************************************************************************/
4254 static uint32 rev_changeid(void)
4256 struct timeval tv;
4258 get_process_uptime(&tv);
4260 #if 1 /* JERRY */
4261 /* Return changeid as msec since spooler restart */
4262 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
4263 #else
4265 * This setting seems to work well but is too untested
4266 * to replace the above calculation. Left in for experiementation
4267 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
4269 return tv.tv_sec * 10 + tv.tv_usec / 100000;
4270 #endif
4275 * The function below are the high level ones.
4276 * only those ones must be called from the spoolss code.
4277 * JFM.
4280 /****************************************************************************
4281 Modify a printer. This is called from SETPRINTERDATA/DELETEPRINTERDATA.
4282 ****************************************************************************/
4284 WERROR mod_a_printer(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4286 WERROR result;
4288 dump_a_printer(printer, level);
4290 switch (level) {
4291 case 2:
4294 * Update the changestamp. Emperical tests show that the
4295 * ChangeID is always updated,but c_setprinter is
4296 * global spooler variable (not per printer).
4299 /* ChangeID **must** be increasing over the lifetime
4300 of client's spoolss service in order for the
4301 client's cache to show updates */
4303 printer->info_2->changeid = rev_changeid();
4306 * Because one day someone will ask:
4307 * NT->NT An admin connection to a remote
4308 * printer show changes imeediately in
4309 * the properities dialog
4311 * A non-admin connection will only show the
4312 * changes after viewing the properites page
4313 * 2 times. Seems to be related to a
4314 * race condition in the client between the spooler
4315 * updating the local cache and the Explorer.exe GUI
4316 * actually displaying the properties.
4318 * This is fixed in Win2k. admin/non-admin
4319 * connections both display changes immediately.
4321 * 14/12/01 --jerry
4324 result=update_a_printer_2(printer->info_2);
4325 break;
4327 default:
4328 result=WERR_UNKNOWN_LEVEL;
4329 break;
4332 return result;
4335 /****************************************************************************
4336 Initialize printer devmode & data with previously saved driver init values.
4337 ****************************************************************************/
4339 static bool set_driver_init_2( NT_PRINTER_INFO_LEVEL_2 *info_ptr )
4341 int len = 0;
4342 char *key = NULL;
4343 TDB_DATA dbuf;
4344 NT_PRINTER_INFO_LEVEL_2 info;
4347 ZERO_STRUCT(info);
4350 * Delete any printer data 'values' already set. When called for driver
4351 * replace, there will generally be some, but during an add printer, there
4352 * should not be any (if there are delete them).
4355 if ( info_ptr->data )
4356 delete_all_printer_data( info_ptr, "" );
4358 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX,
4359 info_ptr->drivername) < 0) {
4360 return false;
4363 dbuf = tdb_fetch_bystring(tdb_drivers, key);
4364 if (!dbuf.dptr) {
4366 * When changing to a driver that has no init info in the tdb, remove
4367 * the previous drivers init info and leave the new on blank.
4369 free_nt_devicemode(&info_ptr->devmode);
4370 SAFE_FREE(key);
4371 return false;
4374 SAFE_FREE(key);
4376 * Get the saved DEVMODE..
4379 len += unpack_devicemode(&info.devmode,dbuf.dptr+len, dbuf.dsize-len);
4382 * The saved DEVMODE contains the devicename from the printer used during
4383 * the initialization save. Change it to reflect the new printer.
4386 if ( info.devmode ) {
4387 ZERO_STRUCT(info.devmode->devicename);
4388 fstrcpy(info.devmode->devicename, info_ptr->printername);
4392 * NT/2k does not change out the entire DeviceMode of a printer
4393 * when changing the driver. Only the driverextra, private, &
4394 * driverversion fields. --jerry (Thu Mar 14 08:58:43 CST 2002)
4396 * Later examination revealed that Windows NT/2k does reset the
4397 * the printer's device mode, bit **only** when you change a
4398 * property of the device mode such as the page orientation.
4399 * --jerry
4403 /* Bind the saved DEVMODE to the new the printer */
4405 free_nt_devicemode(&info_ptr->devmode);
4406 info_ptr->devmode = info.devmode;
4408 DEBUG(10,("set_driver_init_2: Set printer [%s] init %s DEVMODE for driver [%s]\n",
4409 info_ptr->printername, info_ptr->devmode?"VALID":"NULL", info_ptr->drivername));
4411 /* Add the printer data 'values' to the new printer */
4413 if ( !(info_ptr->data = TALLOC_ZERO_P( info_ptr, NT_PRINTER_DATA )) ) {
4414 DEBUG(0,("set_driver_init_2: talloc() failed!\n"));
4415 return False;
4418 len += unpack_values( info_ptr->data, dbuf.dptr+len, dbuf.dsize-len );
4420 SAFE_FREE(dbuf.dptr);
4422 return true;
4425 /****************************************************************************
4426 Initialize printer devmode & data with previously saved driver init values.
4427 When a printer is created using AddPrinter, the drivername bound to the
4428 printer is used to lookup previously saved driver initialization info, which
4429 is bound to the new printer.
4430 ****************************************************************************/
4432 bool set_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4434 bool result = False;
4436 switch (level) {
4437 case 2:
4438 result = set_driver_init_2(printer->info_2);
4439 break;
4441 default:
4442 DEBUG(0,("set_driver_init: Programmer's error! Unknown driver_init level [%d]\n",
4443 level));
4444 break;
4447 return result;
4450 /****************************************************************************
4451 Delete driver init data stored for a specified driver
4452 ****************************************************************************/
4454 bool del_driver_init(char *drivername)
4456 char *key;
4457 bool ret;
4459 if (!drivername || !*drivername) {
4460 DEBUG(3,("del_driver_init: No drivername specified!\n"));
4461 return false;
4464 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, drivername) < 0) {
4465 return false;
4468 DEBUG(6,("del_driver_init: Removing driver init data for [%s]\n",
4469 drivername));
4471 ret = (tdb_delete_bystring(tdb_drivers, key) == 0);
4472 SAFE_FREE(key);
4473 return ret;
4476 /****************************************************************************
4477 Pack up the DEVMODE and values for a printer into a 'driver init' entry
4478 in the tdb. Note: this is different from the driver entry and the printer
4479 entry. There should be a single driver init entry for each driver regardless
4480 of whether it was installed from NT or 2K. Technically, they should be
4481 different, but they work out to the same struct.
4482 ****************************************************************************/
4484 static uint32 update_driver_init_2(NT_PRINTER_INFO_LEVEL_2 *info)
4486 char *key = NULL;
4487 uint8 *buf;
4488 int buflen, len, ret;
4489 int retlen;
4490 TDB_DATA dbuf;
4492 buf = NULL;
4493 buflen = 0;
4495 again:
4496 len = 0;
4497 len += pack_devicemode(info->devmode, buf+len, buflen-len);
4499 retlen = pack_values( info->data, buf+len, buflen-len );
4500 if (retlen == -1) {
4501 ret = -1;
4502 goto done;
4504 len += retlen;
4506 if (buflen < len) {
4507 buf = (uint8 *)SMB_REALLOC(buf, len);
4508 if (!buf) {
4509 DEBUG(0, ("update_driver_init_2: failed to enlarge buffer!\n"));
4510 ret = -1;
4511 goto done;
4513 buflen = len;
4514 goto again;
4517 SAFE_FREE(key);
4518 if (asprintf(&key, "%s%s", DRIVER_INIT_PREFIX, info->drivername) < 0) {
4519 ret = (uint32)-1;
4520 goto done;
4523 dbuf.dptr = buf;
4524 dbuf.dsize = len;
4526 ret = tdb_store_bystring(tdb_drivers, key, dbuf, TDB_REPLACE);
4528 done:
4529 if (ret == -1)
4530 DEBUG(8, ("update_driver_init_2: error updating printer init to tdb on disk\n"));
4532 SAFE_FREE(buf);
4534 DEBUG(10,("update_driver_init_2: Saved printer [%s] init DEVMODE & values for driver [%s]\n",
4535 info->sharename, info->drivername));
4537 return ret;
4540 /****************************************************************************
4541 Update (i.e. save) the driver init info (DEVMODE and values) for a printer
4542 ****************************************************************************/
4544 static uint32 update_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level)
4546 uint32 result;
4548 dump_a_printer(printer, level);
4550 switch (level) {
4551 case 2:
4552 result = update_driver_init_2(printer->info_2);
4553 break;
4554 default:
4555 result = 1;
4556 break;
4559 return result;
4562 /****************************************************************************
4563 Convert the printer data value, a REG_BINARY array, into an initialization
4564 DEVMODE. Note: the array must be parsed as if it was a DEVMODE in an rpc...
4565 got to keep the endians happy :).
4566 ****************************************************************************/
4568 static bool convert_driver_init( TALLOC_CTX *ctx, NT_DEVICEMODE *nt_devmode, uint8 *data, uint32 data_len )
4570 bool result = False;
4571 prs_struct ps;
4572 DEVICEMODE devmode;
4574 ZERO_STRUCT(devmode);
4576 prs_init_empty(&ps, ctx, UNMARSHALL);
4577 ps.data_p = (char *)data;
4578 ps.buffer_size = data_len;
4580 if (spoolss_io_devmode("phantom DEVMODE", &ps, 0, &devmode))
4581 result = convert_devicemode("", &devmode, &nt_devmode);
4582 else
4583 DEBUG(10,("convert_driver_init: error parsing DEVMODE\n"));
4585 return result;
4588 /****************************************************************************
4589 Set the DRIVER_INIT info in the tdb. Requires Win32 client code that:
4591 1. Use the driver's config DLL to this UNC printername and:
4592 a. Call DrvPrintEvent with PRINTER_EVENT_INITIALIZE
4593 b. Call DrvConvertDevMode with CDM_DRIVER_DEFAULT to get default DEVMODE
4594 2. Call SetPrinterData with the 'magic' key and the DEVMODE as data.
4596 The last step triggers saving the "driver initialization" information for
4597 this printer into the tdb. Later, new printers that use this driver will
4598 have this initialization information bound to them. This simulates the
4599 driver initialization, as if it had run on the Samba server (as it would
4600 have done on NT).
4602 The Win32 client side code requirement sucks! But until we can run arbitrary
4603 Win32 printer driver code on any Unix that Samba runs on, we are stuck with it.
4605 It would have been easier to use SetPrinter because all the UNMARSHALLING of
4606 the DEVMODE is done there, but 2K/XP clients do not set the DEVMODE... think
4607 about it and you will realize why. JRR 010720
4608 ****************************************************************************/
4610 static WERROR save_driver_init_2(NT_PRINTER_INFO_LEVEL *printer, uint8 *data, uint32 data_len )
4612 WERROR status = WERR_OK;
4613 TALLOC_CTX *ctx = NULL;
4614 NT_DEVICEMODE *nt_devmode = NULL;
4615 NT_DEVICEMODE *tmp_devmode = printer->info_2->devmode;
4618 * When the DEVMODE is already set on the printer, don't try to unpack it.
4620 DEBUG(8,("save_driver_init_2: Enter...\n"));
4622 if ( !printer->info_2->devmode && data_len ) {
4624 * Set devmode on printer info, so entire printer initialization can be
4625 * saved to tdb.
4628 if ((ctx = talloc_init("save_driver_init_2")) == NULL)
4629 return WERR_NOMEM;
4631 if ((nt_devmode = SMB_MALLOC_P(NT_DEVICEMODE)) == NULL) {
4632 status = WERR_NOMEM;
4633 goto done;
4636 ZERO_STRUCTP(nt_devmode);
4639 * The DEVMODE is held in the 'data' component of the param in raw binary.
4640 * Convert it to to a devmode structure
4642 if ( !convert_driver_init( ctx, nt_devmode, data, data_len )) {
4643 DEBUG(10,("save_driver_init_2: error converting DEVMODE\n"));
4644 status = WERR_INVALID_PARAM;
4645 goto done;
4648 printer->info_2->devmode = nt_devmode;
4652 * Pack up and add (or update) the DEVMODE and any current printer data to
4653 * a 'driver init' element in the tdb
4657 if ( update_driver_init(printer, 2) != 0 ) {
4658 DEBUG(10,("save_driver_init_2: error updating DEVMODE\n"));
4659 status = WERR_NOMEM;
4660 goto done;
4664 * If driver initialization info was successfully saved, set the current
4665 * printer to match it. This allows initialization of the current printer
4666 * as well as the driver.
4668 status = mod_a_printer(printer, 2);
4669 if (!W_ERROR_IS_OK(status)) {
4670 DEBUG(10,("save_driver_init_2: error setting DEVMODE on printer [%s]\n",
4671 printer->info_2->printername));
4674 done:
4675 talloc_destroy(ctx);
4676 free_nt_devicemode( &nt_devmode );
4678 printer->info_2->devmode = tmp_devmode;
4680 return status;
4683 /****************************************************************************
4684 Update the driver init info (DEVMODE and specifics) for a printer
4685 ****************************************************************************/
4687 WERROR save_driver_init(NT_PRINTER_INFO_LEVEL *printer, uint32 level, uint8 *data, uint32 data_len)
4689 WERROR status = WERR_OK;
4691 switch (level) {
4692 case 2:
4693 status = save_driver_init_2( printer, data, data_len );
4694 break;
4695 default:
4696 status = WERR_UNKNOWN_LEVEL;
4697 break;
4700 return status;
4703 /****************************************************************************
4704 Get a NT_PRINTER_INFO_LEVEL struct. It returns malloced memory.
4706 Previously the code had a memory allocation problem because it always
4707 used the TALLOC_CTX from the Printer_entry*. This context lasts
4708 as a long as the original handle is open. So if the client made a lot
4709 of getprinter[data]() calls, the memory usage would climb. Now we use
4710 a short lived TALLOC_CTX for printer_info_2 objects returned. We
4711 still use the Printer_entry->ctx for maintaining the cache copy though
4712 since that object must live as long as the handle by definition.
4713 --jerry
4715 ****************************************************************************/
4717 static WERROR get_a_printer_internal( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level,
4718 const char *sharename, bool get_loc_com)
4720 WERROR result;
4721 fstring servername;
4723 DEBUG(10,("get_a_printer: [%s] level %u\n", sharename, (unsigned int)level));
4725 if ( !(*pp_printer = TALLOC_ZERO_P(NULL, NT_PRINTER_INFO_LEVEL)) ) {
4726 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4727 return WERR_NOMEM;
4730 switch (level) {
4731 case 2:
4732 if ( !((*pp_printer)->info_2 = TALLOC_ZERO_P(*pp_printer, NT_PRINTER_INFO_LEVEL_2)) ) {
4733 DEBUG(0,("get_a_printer: talloc() fail.\n"));
4734 TALLOC_FREE( *pp_printer );
4735 return WERR_NOMEM;
4738 if ( print_hnd )
4739 fstrcpy( servername, print_hnd->servername );
4740 else {
4741 fstrcpy( servername, "%L" );
4742 standard_sub_basic( "", "", servername,
4743 sizeof(servername)-1 );
4746 result = get_a_printer_2( (*pp_printer)->info_2,
4747 servername, sharename, get_loc_com);
4749 /* we have a new printer now. Save it with this handle */
4751 if ( !W_ERROR_IS_OK(result) ) {
4752 TALLOC_FREE( *pp_printer );
4753 DEBUG(10,("get_a_printer: [%s] level %u returning %s\n",
4754 sharename, (unsigned int)level, dos_errstr(result)));
4755 return result;
4758 dump_a_printer( *pp_printer, level);
4760 break;
4762 default:
4763 TALLOC_FREE( *pp_printer );
4764 return WERR_UNKNOWN_LEVEL;
4767 return WERR_OK;
4770 WERROR get_a_printer( Printer_entry *print_hnd,
4771 NT_PRINTER_INFO_LEVEL **pp_printer,
4772 uint32 level,
4773 const char *sharename)
4775 return get_a_printer_internal(print_hnd, pp_printer, level,
4776 sharename, true);
4779 WERROR get_a_printer_search( Printer_entry *print_hnd,
4780 NT_PRINTER_INFO_LEVEL **pp_printer,
4781 uint32 level,
4782 const char *sharename)
4784 return get_a_printer_internal(print_hnd, pp_printer, level,
4785 sharename, false);
4788 /****************************************************************************
4789 Deletes a NT_PRINTER_INFO_LEVEL struct.
4790 ****************************************************************************/
4792 uint32 free_a_printer(NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level)
4794 NT_PRINTER_INFO_LEVEL *printer = *pp_printer;
4796 if ( !printer )
4797 return 0;
4799 switch (level) {
4800 case 2:
4801 if ( printer->info_2 )
4802 free_nt_printer_info_level_2(&printer->info_2);
4803 break;
4805 default:
4806 DEBUG(0,("free_a_printer: unknown level! [%d]\n", level ));
4807 return 1;
4810 TALLOC_FREE(*pp_printer);
4812 return 0;
4815 /****************************************************************************
4816 ****************************************************************************/
4817 uint32 add_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
4819 uint32 result;
4820 DEBUG(104,("adding a printer at level [%d]\n", level));
4821 dump_a_printer_driver(driver, level);
4823 switch (level) {
4824 case 3:
4825 result=add_a_printer_driver_3(driver.info_3);
4826 break;
4828 case 6:
4829 result=add_a_printer_driver_6(driver.info_6);
4830 break;
4832 default:
4833 result=1;
4834 break;
4837 return result;
4839 /****************************************************************************
4840 ****************************************************************************/
4842 WERROR get_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL *driver, uint32 level,
4843 fstring drivername, const char *architecture, uint32 version)
4845 WERROR result;
4847 switch (level) {
4848 case 3:
4849 /* Sometime we just want any version of the driver */
4851 if ( version == DRIVER_ANY_VERSION ) {
4852 /* look for Win2k first and then for NT4 */
4853 result = get_a_printer_driver_3(&driver->info_3, drivername,
4854 architecture, 3);
4856 if ( !W_ERROR_IS_OK(result) ) {
4857 result = get_a_printer_driver_3( &driver->info_3,
4858 drivername, architecture, 2 );
4860 } else {
4861 result = get_a_printer_driver_3(&driver->info_3, drivername,
4862 architecture, version);
4864 break;
4866 default:
4867 result=W_ERROR(1);
4868 break;
4871 if (W_ERROR_IS_OK(result))
4872 dump_a_printer_driver(*driver, level);
4874 return result;
4877 /****************************************************************************
4878 ****************************************************************************/
4879 uint32 free_a_printer_driver(NT_PRINTER_DRIVER_INFO_LEVEL driver, uint32 level)
4881 uint32 result;
4883 switch (level) {
4884 case 3:
4886 NT_PRINTER_DRIVER_INFO_LEVEL_3 *info3;
4887 if (driver.info_3 != NULL)
4889 info3=driver.info_3;
4890 SAFE_FREE(info3->dependentfiles);
4891 ZERO_STRUCTP(info3);
4892 SAFE_FREE(info3);
4893 result=0;
4894 } else {
4895 result=4;
4897 break;
4899 case 6:
4901 NT_PRINTER_DRIVER_INFO_LEVEL_6 *info6;
4902 if (driver.info_6 != NULL) {
4903 info6=driver.info_6;
4904 SAFE_FREE(info6->dependentfiles);
4905 SAFE_FREE(info6->previousnames);
4906 ZERO_STRUCTP(info6);
4907 SAFE_FREE(info6);
4908 result=0;
4909 } else {
4910 result=4;
4912 break;
4914 default:
4915 result=1;
4916 break;
4918 return result;
4922 /****************************************************************************
4923 Determine whether or not a particular driver is currently assigned
4924 to a printer
4925 ****************************************************************************/
4927 bool printer_driver_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3 )
4929 int snum;
4930 int n_services = lp_numservices();
4931 NT_PRINTER_INFO_LEVEL *printer = NULL;
4932 bool in_use = False;
4934 if ( !info_3 )
4935 return False;
4937 DEBUG(10,("printer_driver_in_use: Beginning search through ntprinters.tdb...\n"));
4939 /* loop through the printers.tdb and check for the drivername */
4941 for (snum=0; snum<n_services && !in_use; snum++) {
4942 if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) )
4943 continue;
4945 if ( !W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, lp_servicename(snum))) )
4946 continue;
4948 if ( strequal(info_3->name, printer->info_2->drivername) )
4949 in_use = True;
4951 free_a_printer( &printer, 2 );
4954 DEBUG(10,("printer_driver_in_use: Completed search through ntprinters.tdb...\n"));
4956 if ( in_use ) {
4957 NT_PRINTER_DRIVER_INFO_LEVEL d;
4958 WERROR werr;
4960 DEBUG(5,("printer_driver_in_use: driver \"%s\" is currently in use\n", info_3->name));
4962 /* we can still remove the driver if there is one of
4963 "Windows NT x86" version 2 or 3 left */
4965 if ( !strequal( "Windows NT x86", info_3->environment ) ) {
4966 werr = get_a_printer_driver( &d, 3, info_3->name, "Windows NT x86", DRIVER_ANY_VERSION );
4968 else {
4969 switch ( info_3->cversion ) {
4970 case 2:
4971 werr = get_a_printer_driver( &d, 3, info_3->name, "Windows NT x86", 3 );
4972 break;
4973 case 3:
4974 werr = get_a_printer_driver( &d, 3, info_3->name, "Windows NT x86", 2 );
4975 break;
4976 default:
4977 DEBUG(0,("printer_driver_in_use: ERROR! unknown driver version (%d)\n",
4978 info_3->cversion));
4979 werr = WERR_UNKNOWN_PRINTER_DRIVER;
4980 break;
4984 /* now check the error code */
4986 if ( W_ERROR_IS_OK(werr) ) {
4987 /* it's ok to remove the driver, we have other architctures left */
4988 in_use = False;
4989 free_a_printer_driver( d, 3 );
4993 /* report that the driver is not in use by default */
4995 return in_use;
4999 /**********************************************************************
5000 Check to see if a ogiven file is in use by *info
5001 *********************************************************************/
5003 static bool drv_file_in_use( char* file, NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
5005 int i = 0;
5007 if ( !info )
5008 return False;
5010 /* mz: skip files that are in the list but already deleted */
5011 if (!file || !file[0]) {
5012 return false;
5015 if ( strequal(file, info->driverpath) )
5016 return True;
5018 if ( strequal(file, info->datafile) )
5019 return True;
5021 if ( strequal(file, info->configfile) )
5022 return True;
5024 if ( strequal(file, info->helpfile) )
5025 return True;
5027 /* see of there are any dependent files to examine */
5029 if ( !info->dependentfiles )
5030 return False;
5032 while ( *info->dependentfiles[i] ) {
5033 if ( strequal(file, info->dependentfiles[i]) )
5034 return True;
5035 i++;
5038 return False;
5042 /**********************************************************************
5043 Utility function to remove the dependent file pointed to by the
5044 input parameter from the list
5045 *********************************************************************/
5047 static void trim_dependent_file( fstring files[], int idx )
5050 /* bump everything down a slot */
5052 while( *files[idx+1] ) {
5053 fstrcpy( files[idx], files[idx+1] );
5054 idx++;
5057 *files[idx] = '\0';
5059 return;
5062 /**********************************************************************
5063 Check if any of the files used by src are also used by drv
5064 *********************************************************************/
5066 static bool trim_overlap_drv_files( NT_PRINTER_DRIVER_INFO_LEVEL_3 *src,
5067 NT_PRINTER_DRIVER_INFO_LEVEL_3 *drv )
5069 bool in_use = False;
5070 int i = 0;
5072 if ( !src || !drv )
5073 return False;
5075 /* check each file. Remove it from the src structure if it overlaps */
5077 if ( drv_file_in_use(src->driverpath, drv) ) {
5078 in_use = True;
5079 DEBUG(10,("Removing driverfile [%s] from list\n", src->driverpath));
5080 fstrcpy( src->driverpath, "" );
5083 if ( drv_file_in_use(src->datafile, drv) ) {
5084 in_use = True;
5085 DEBUG(10,("Removing datafile [%s] from list\n", src->datafile));
5086 fstrcpy( src->datafile, "" );
5089 if ( drv_file_in_use(src->configfile, drv) ) {
5090 in_use = True;
5091 DEBUG(10,("Removing configfile [%s] from list\n", src->configfile));
5092 fstrcpy( src->configfile, "" );
5095 if ( drv_file_in_use(src->helpfile, drv) ) {
5096 in_use = True;
5097 DEBUG(10,("Removing helpfile [%s] from list\n", src->helpfile));
5098 fstrcpy( src->helpfile, "" );
5101 /* are there any dependentfiles to examine? */
5103 if ( !src->dependentfiles )
5104 return in_use;
5106 while ( *src->dependentfiles[i] ) {
5107 if ( drv_file_in_use(src->dependentfiles[i], drv) ) {
5108 in_use = True;
5109 DEBUG(10,("Removing [%s] from dependent file list\n", src->dependentfiles[i]));
5110 trim_dependent_file( src->dependentfiles, i );
5111 } else
5112 i++;
5115 return in_use;
5118 /****************************************************************************
5119 Determine whether or not a particular driver files are currently being
5120 used by any other driver.
5122 Return value is True if any files were in use by other drivers
5123 and False otherwise.
5125 Upon return, *info has been modified to only contain the driver files
5126 which are not in use
5128 Fix from mz:
5130 This needs to check all drivers to ensure that all files in use
5131 have been removed from *info, not just the ones in the first
5132 match.
5133 ****************************************************************************/
5135 bool printer_driver_files_in_use ( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info )
5137 int i;
5138 int ndrivers;
5139 uint32 version;
5140 fstring *list = NULL;
5141 NT_PRINTER_DRIVER_INFO_LEVEL driver;
5142 bool in_use = false;
5144 if ( !info )
5145 return False;
5147 version = info->cversion;
5149 /* loop over all driver versions */
5151 DEBUG(5,("printer_driver_files_in_use: Beginning search through ntdrivers.tdb...\n"));
5153 /* get the list of drivers */
5155 list = NULL;
5156 ndrivers = get_ntdrivers(&list, info->environment, version);
5158 DEBUGADD(4,("we have:[%d] drivers in environment [%s] and version [%d]\n",
5159 ndrivers, info->environment, version));
5161 /* check each driver for overlap in files */
5163 for (i=0; i<ndrivers; i++) {
5164 DEBUGADD(5,("\tdriver: [%s]\n", list[i]));
5166 ZERO_STRUCT(driver);
5168 if ( !W_ERROR_IS_OK(get_a_printer_driver(&driver, 3, list[i], info->environment, version)) ) {
5169 SAFE_FREE(list);
5170 return True;
5173 /* check if d2 uses any files from d1 */
5174 /* only if this is a different driver than the one being deleted */
5176 if ( !strequal(info->name, driver.info_3->name) ) {
5177 if ( trim_overlap_drv_files(info, driver.info_3) ) {
5178 /* mz: Do not instantly return -
5179 * we need to ensure this file isn't
5180 * also in use by other drivers. */
5181 in_use = true;
5185 free_a_printer_driver(driver, 3);
5188 SAFE_FREE(list);
5190 DEBUG(5,("printer_driver_files_in_use: Completed search through ntdrivers.tdb...\n"));
5192 driver.info_3 = info;
5194 if ( DEBUGLEVEL >= 20 )
5195 dump_a_printer_driver( driver, 3 );
5197 return in_use;
5200 /****************************************************************************
5201 Actually delete the driver files. Make sure that
5202 printer_driver_files_in_use() return False before calling
5203 this.
5204 ****************************************************************************/
5206 static bool delete_driver_files( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3, struct current_user *user )
5208 int i = 0;
5209 char *s;
5210 const char *file;
5211 connection_struct *conn;
5212 DATA_BLOB null_pw;
5213 NTSTATUS nt_status;
5214 fstring res_type;
5215 SMB_STRUCT_STAT st;
5217 if ( !info_3 )
5218 return False;
5220 DEBUG(6,("delete_driver_files: deleting driver [%s] - version [%d]\n", info_3->name, info_3->cversion));
5223 * Connect to the print$ share under the same account as the
5224 * user connected to the rpc pipe. Note we must be root to
5225 * do this.
5228 null_pw = data_blob_null;
5229 fstrcpy(res_type, "A:");
5230 become_root();
5231 conn = make_connection_with_chdir( "print$", null_pw, res_type, user->vuid, &nt_status );
5232 unbecome_root();
5234 if ( !conn ) {
5235 DEBUG(0,("delete_driver_files: Unable to connect\n"));
5236 return False;
5239 if ( !CAN_WRITE(conn) ) {
5240 DEBUG(3,("delete_driver_files: Cannot delete print driver when [print$] is read-only\n"));
5241 return False;
5244 /* Save who we are - we are temporarily becoming the connection user. */
5246 if ( !become_user(conn, conn->vuid) ) {
5247 DEBUG(0,("delete_driver_files: Can't become user!\n"));
5248 return False;
5251 /* now delete the files; must strip the '\print$' string from
5252 fron of path */
5254 if ( *info_3->driverpath ) {
5255 if ( (s = strchr( &info_3->driverpath[1], '\\' )) != NULL ) {
5256 file = s;
5257 driver_unix_convert(conn,file,&st);
5258 DEBUG(10,("deleting driverfile [%s]\n", s));
5259 unlink_internals(conn, NULL, 0, file, False);
5263 if ( *info_3->configfile ) {
5264 if ( (s = strchr( &info_3->configfile[1], '\\' )) != NULL ) {
5265 file = s;
5266 driver_unix_convert(conn,file,&st);
5267 DEBUG(10,("deleting configfile [%s]\n", s));
5268 unlink_internals(conn, NULL, 0, file, False);
5272 if ( *info_3->datafile ) {
5273 if ( (s = strchr( &info_3->datafile[1], '\\' )) != NULL ) {
5274 file = s;
5275 driver_unix_convert(conn,file,&st);
5276 DEBUG(10,("deleting datafile [%s]\n", s));
5277 unlink_internals(conn, NULL, 0, file, False);
5281 if ( *info_3->helpfile ) {
5282 if ( (s = strchr( &info_3->helpfile[1], '\\' )) != NULL ) {
5283 file = s;
5284 driver_unix_convert(conn,file,&st);
5285 DEBUG(10,("deleting helpfile [%s]\n", s));
5286 unlink_internals(conn, NULL, 0, file, False);
5290 /* check if we are done removing files */
5292 if ( info_3->dependentfiles ) {
5293 while ( info_3->dependentfiles[i][0] ) {
5294 char *p;
5296 /* bypass the "\print$" portion of the path */
5298 if ( (p = strchr( info_3->dependentfiles[i]+1, '\\' )) != NULL ) {
5299 file = p;
5300 driver_unix_convert(conn,file,&st);
5301 DEBUG(10,("deleting dependent file [%s]\n", file));
5302 unlink_internals(conn, NULL, 0, file, False);
5305 i++;
5309 unbecome_user();
5311 return true;
5314 /****************************************************************************
5315 Remove a printer driver from the TDB. This assumes that the the driver was
5316 previously looked up.
5317 ***************************************************************************/
5319 WERROR delete_printer_driver( NT_PRINTER_DRIVER_INFO_LEVEL_3 *info_3, struct current_user *user,
5320 uint32 version, bool delete_files )
5322 char *key = NULL;
5323 const char *arch;
5324 TDB_DATA dbuf;
5325 NT_PRINTER_DRIVER_INFO_LEVEL ctr;
5327 /* delete the tdb data first */
5329 arch = get_short_archi(info_3->environment);
5330 if (!arch) {
5331 return WERR_UNKNOWN_PRINTER_DRIVER;
5333 if (asprintf(&key, "%s%s/%d/%s", DRIVERS_PREFIX,
5334 arch, version, info_3->name) < 0) {
5335 return WERR_NOMEM;
5338 DEBUG(5,("delete_printer_driver: key = [%s] delete_files = %s\n",
5339 key, delete_files ? "TRUE" : "FALSE" ));
5341 ctr.info_3 = info_3;
5342 dump_a_printer_driver( ctr, 3 );
5344 /* check if the driver actually exists for this environment */
5346 dbuf = tdb_fetch_bystring( tdb_drivers, key );
5347 if ( !dbuf.dptr ) {
5348 DEBUG(8,("delete_printer_driver: Driver unknown [%s]\n", key));
5349 SAFE_FREE(key);
5350 return WERR_UNKNOWN_PRINTER_DRIVER;
5353 SAFE_FREE( dbuf.dptr );
5355 /* ok... the driver exists so the delete should return success */
5357 if (tdb_delete_bystring(tdb_drivers, key) == -1) {
5358 DEBUG (0,("delete_printer_driver: fail to delete %s!\n", key));
5359 SAFE_FREE(key);
5360 return WERR_ACCESS_DENIED;
5364 * now delete any associated files if delete_files == True
5365 * even if this part failes, we return succes because the
5366 * driver doesn not exist any more
5369 if ( delete_files )
5370 delete_driver_files( info_3, user );
5372 DEBUG(5,("delete_printer_driver: driver delete successful [%s]\n", key));
5373 SAFE_FREE(key);
5375 return WERR_OK;
5378 /****************************************************************************
5379 Store a security desc for a printer.
5380 ****************************************************************************/
5382 WERROR nt_printing_setsec(const char *sharename, SEC_DESC_BUF *secdesc_ctr)
5384 SEC_DESC_BUF *new_secdesc_ctr = NULL;
5385 SEC_DESC_BUF *old_secdesc_ctr = NULL;
5386 prs_struct ps;
5387 bool prs_init_done = false;
5388 TALLOC_CTX *mem_ctx = NULL;
5389 TDB_DATA kbuf;
5390 WERROR status;
5392 mem_ctx = talloc_init("nt_printing_setsec");
5393 if (mem_ctx == NULL)
5394 return WERR_NOMEM;
5396 /* The old owner and group sids of the security descriptor are not
5397 present when new ACEs are added or removed by changing printer
5398 permissions through NT. If they are NULL in the new security
5399 descriptor then copy them over from the old one. */
5401 if (!secdesc_ctr->sd->owner_sid || !secdesc_ctr->sd->group_sid) {
5402 DOM_SID *owner_sid, *group_sid;
5403 SEC_ACL *dacl, *sacl;
5404 SEC_DESC *psd = NULL;
5405 size_t size;
5407 if (!nt_printing_getsec(mem_ctx, sharename, &old_secdesc_ctr)) {
5408 status = WERR_NOMEM;
5409 goto out;
5412 /* Pick out correct owner and group sids */
5414 owner_sid = secdesc_ctr->sd->owner_sid ?
5415 secdesc_ctr->sd->owner_sid :
5416 old_secdesc_ctr->sd->owner_sid;
5418 group_sid = secdesc_ctr->sd->group_sid ?
5419 secdesc_ctr->sd->group_sid :
5420 old_secdesc_ctr->sd->group_sid;
5422 dacl = secdesc_ctr->sd->dacl ?
5423 secdesc_ctr->sd->dacl :
5424 old_secdesc_ctr->sd->dacl;
5426 sacl = secdesc_ctr->sd->sacl ?
5427 secdesc_ctr->sd->sacl :
5428 old_secdesc_ctr->sd->sacl;
5430 /* Make a deep copy of the security descriptor */
5432 psd = make_sec_desc(mem_ctx, secdesc_ctr->sd->revision, secdesc_ctr->sd->type,
5433 owner_sid, group_sid,
5434 sacl,
5435 dacl,
5436 &size);
5438 if (!psd) {
5439 status = WERR_NOMEM;
5440 goto out;
5443 new_secdesc_ctr = make_sec_desc_buf(mem_ctx, size, psd);
5446 if (!new_secdesc_ctr) {
5447 new_secdesc_ctr = secdesc_ctr;
5450 /* Store the security descriptor in a tdb */
5452 if (!prs_init(&ps,
5453 (uint32)ndr_size_security_descriptor(new_secdesc_ctr->sd, 0)
5454 + sizeof(SEC_DESC_BUF), mem_ctx, MARSHALL) ) {
5455 status = WERR_NOMEM;
5456 goto out;
5460 prs_init_done = true;
5462 if (!sec_io_desc_buf("nt_printing_setsec", &new_secdesc_ctr,
5463 &ps, 1)) {
5464 status = WERR_BADFUNC;
5465 goto out;
5468 kbuf = make_printers_secdesc_tdbkey(mem_ctx, sharename );
5470 if (tdb_prs_store(tdb_printers, kbuf, &ps)==0) {
5471 status = WERR_OK;
5472 } else {
5473 DEBUG(1,("Failed to store secdesc for %s\n", sharename));
5474 status = WERR_BADFUNC;
5477 /* Free malloc'ed memory */
5479 out:
5481 if (prs_init_done) {
5482 prs_mem_free(&ps);
5484 if (mem_ctx)
5485 talloc_destroy(mem_ctx);
5486 return status;
5489 /****************************************************************************
5490 Construct a default security descriptor buffer for a printer.
5491 ****************************************************************************/
5493 static SEC_DESC_BUF *construct_default_printer_sdb(TALLOC_CTX *ctx)
5495 SEC_ACE ace[5]; /* max number of ace entries */
5496 int i = 0;
5497 SEC_ACCESS sa;
5498 SEC_ACL *psa = NULL;
5499 SEC_DESC_BUF *sdb = NULL;
5500 SEC_DESC *psd = NULL;
5501 DOM_SID adm_sid;
5502 size_t sd_size;
5504 /* Create an ACE where Everyone is allowed to print */
5506 init_sec_access(&sa, PRINTER_ACE_PRINT);
5507 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
5508 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5510 /* Add the domain admins group if we are a DC */
5512 if ( IS_DC ) {
5513 DOM_SID domadmins_sid;
5515 sid_copy(&domadmins_sid, get_global_sam_sid());
5516 sid_append_rid(&domadmins_sid, DOMAIN_GROUP_RID_ADMINS);
5518 init_sec_access(&sa, PRINTER_ACE_FULL_CONTROL);
5519 init_sec_ace(&ace[i++], &domadmins_sid,
5520 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5521 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5522 init_sec_ace(&ace[i++], &domadmins_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5523 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5525 else if (secrets_fetch_domain_sid(lp_workgroup(), &adm_sid)) {
5526 sid_append_rid(&adm_sid, DOMAIN_USER_RID_ADMIN);
5528 init_sec_access(&sa, PRINTER_ACE_FULL_CONTROL);
5529 init_sec_ace(&ace[i++], &adm_sid,
5530 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5531 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5532 init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
5533 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5536 /* add BUILTIN\Administrators as FULL CONTROL */
5538 init_sec_access(&sa, PRINTER_ACE_FULL_CONTROL);
5539 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5540 SEC_ACE_TYPE_ACCESS_ALLOWED, sa,
5541 SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY);
5542 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
5543 SEC_ACE_TYPE_ACCESS_ALLOWED,
5544 sa, SEC_ACE_FLAG_CONTAINER_INHERIT);
5546 /* Make the security descriptor owned by the BUILTIN\Administrators */
5548 /* The ACL revision number in rpc_secdesc.h differs from the one
5549 created by NT when setting ACE entries in printer
5550 descriptors. NT4 complains about the property being edited by a
5551 NT5 machine. */
5553 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) != NULL) {
5554 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
5555 &global_sid_Builtin_Administrators,
5556 &global_sid_Builtin_Administrators,
5557 NULL, psa, &sd_size);
5560 if (!psd) {
5561 DEBUG(0,("construct_default_printer_sd: Failed to make SEC_DESC.\n"));
5562 return NULL;
5565 sdb = make_sec_desc_buf(ctx, sd_size, psd);
5567 DEBUG(4,("construct_default_printer_sdb: size = %u.\n",
5568 (unsigned int)sd_size));
5570 return sdb;
5573 /****************************************************************************
5574 Get a security desc for a printer.
5575 ****************************************************************************/
5577 bool nt_printing_getsec(TALLOC_CTX *ctx, const char *sharename, SEC_DESC_BUF **secdesc_ctr)
5579 prs_struct ps;
5580 TDB_DATA kbuf;
5581 char *temp;
5583 if (strlen(sharename) > 2 && (temp = strchr(sharename + 2, '\\'))) {
5584 sharename = temp + 1;
5587 ZERO_STRUCT(ps);
5589 /* Fetch security descriptor from tdb */
5591 kbuf = make_printers_secdesc_tdbkey(ctx, sharename );
5593 if (tdb_prs_fetch(tdb_printers, kbuf, &ps, ctx)!=0 ||
5594 !sec_io_desc_buf("nt_printing_getsec", secdesc_ctr, &ps, 1)) {
5596 prs_mem_free(&ps);
5598 DEBUG(4,("using default secdesc for %s\n", sharename));
5600 if (!(*secdesc_ctr = construct_default_printer_sdb(ctx))) {
5601 return False;
5604 /* Save default security descriptor for later */
5606 if (!prs_init(&ps, (uint32)ndr_size_security_descriptor((*secdesc_ctr)->sd, 0) +
5607 sizeof(SEC_DESC_BUF), ctx, MARSHALL))
5608 return False;
5610 if (sec_io_desc_buf("nt_printing_getsec", secdesc_ctr, &ps, 1)) {
5611 tdb_prs_store(tdb_printers, kbuf, &ps);
5614 prs_mem_free(&ps);
5616 return True;
5619 prs_mem_free(&ps);
5621 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
5622 this security descriptor has been created when winbindd was
5623 down. Take ownership of security descriptor. */
5625 if (sid_equal((*secdesc_ctr)->sd->owner_sid, &global_sid_World)) {
5626 DOM_SID owner_sid;
5628 /* Change sd owner to workgroup administrator */
5630 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
5631 SEC_DESC_BUF *new_secdesc_ctr = NULL;
5632 SEC_DESC *psd = NULL;
5633 size_t size;
5635 /* Create new sd */
5637 sid_append_rid(&owner_sid, DOMAIN_USER_RID_ADMIN);
5639 psd = make_sec_desc(ctx, (*secdesc_ctr)->sd->revision, (*secdesc_ctr)->sd->type,
5640 &owner_sid,
5641 (*secdesc_ctr)->sd->group_sid,
5642 (*secdesc_ctr)->sd->sacl,
5643 (*secdesc_ctr)->sd->dacl,
5644 &size);
5646 if (!psd) {
5647 return False;
5650 new_secdesc_ctr = make_sec_desc_buf(ctx, size, psd);
5651 if (!new_secdesc_ctr) {
5652 return False;
5655 /* Swap with other one */
5657 *secdesc_ctr = new_secdesc_ctr;
5659 /* Set it */
5661 nt_printing_setsec(sharename, *secdesc_ctr);
5665 if (DEBUGLEVEL >= 10) {
5666 SEC_ACL *the_acl = (*secdesc_ctr)->sd->dacl;
5667 int i;
5669 DEBUG(10, ("secdesc_ctr for %s has %d aces:\n",
5670 sharename, the_acl->num_aces));
5672 for (i = 0; i < the_acl->num_aces; i++) {
5673 DEBUG(10, ("%s %d %d 0x%08x\n",
5674 sid_string_dbg(&the_acl->aces[i].trustee),
5675 the_acl->aces[i].type, the_acl->aces[i].flags,
5676 the_acl->aces[i].access_mask));
5680 return True;
5683 /* error code:
5684 0: everything OK
5685 1: level not implemented
5686 2: file doesn't exist
5687 3: can't allocate memory
5688 4: can't free memory
5689 5: non existant struct
5693 A printer and a printer driver are 2 different things.
5694 NT manages them separatelly, Samba does the same.
5695 Why ? Simply because it's easier and it makes sense !
5697 Now explanation: You have 3 printers behind your samba server,
5698 2 of them are the same make and model (laser A and B). But laser B
5699 has an 3000 sheet feeder and laser A doesn't such an option.
5700 Your third printer is an old dot-matrix model for the accounting :-).
5702 If the /usr/local/samba/lib directory (default dir), you will have
5703 5 files to describe all of this.
5705 3 files for the printers (1 by printer):
5706 NTprinter_laser A
5707 NTprinter_laser B
5708 NTprinter_accounting
5709 2 files for the drivers (1 for the laser and 1 for the dot matrix)
5710 NTdriver_printer model X
5711 NTdriver_printer model Y
5713 jfm: I should use this comment for the text file to explain
5714 same thing for the forms BTW.
5715 Je devrais mettre mes commentaires en francais, ca serait mieux :-)
5719 /* Convert generic access rights to printer object specific access rights.
5720 It turns out that NT4 security descriptors use generic access rights and
5721 NT5 the object specific ones. */
5723 void map_printer_permissions(SEC_DESC *sd)
5725 int i;
5727 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5728 se_map_generic(&sd->dacl->aces[i].access_mask,
5729 &printer_generic_mapping);
5733 void map_job_permissions(SEC_DESC *sd)
5735 int i;
5737 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5738 se_map_generic(&sd->dacl->aces[i].access_mask,
5739 &job_generic_mapping);
5744 /****************************************************************************
5745 Check a user has permissions to perform the given operation. We use the
5746 permission constants defined in include/rpc_spoolss.h to check the various
5747 actions we perform when checking printer access.
5749 PRINTER_ACCESS_ADMINISTER:
5750 print_queue_pause, print_queue_resume, update_printer_sec,
5751 update_printer, spoolss_addprinterex_level_2,
5752 _spoolss_setprinterdata
5754 PRINTER_ACCESS_USE:
5755 print_job_start
5757 JOB_ACCESS_ADMINISTER:
5758 print_job_delete, print_job_pause, print_job_resume,
5759 print_queue_purge
5761 Try access control in the following order (for performance reasons):
5762 1) root ans SE_PRINT_OPERATOR can do anything (easy check)
5763 2) check security descriptor (bit comparisons in memory)
5764 3) "printer admins" (may result in numerous calls to winbind)
5766 ****************************************************************************/
5767 bool print_access_check(struct current_user *user, int snum, int access_type)
5769 SEC_DESC_BUF *secdesc = NULL;
5770 uint32 access_granted;
5771 NTSTATUS status;
5772 bool result;
5773 const char *pname;
5774 TALLOC_CTX *mem_ctx = NULL;
5775 SE_PRIV se_printop = SE_PRINT_OPERATOR;
5777 /* If user is NULL then use the current_user structure */
5779 if (!user)
5780 user = &current_user;
5782 /* Always allow root or SE_PRINT_OPERATROR to do anything */
5784 if ( user->ut.uid == 0 || user_has_privileges(user->nt_user_token, &se_printop ) ) {
5785 return True;
5788 /* Get printer name */
5790 pname = PRINTERNAME(snum);
5792 if (!pname || !*pname) {
5793 errno = EACCES;
5794 return False;
5797 /* Get printer security descriptor */
5799 if(!(mem_ctx = talloc_init("print_access_check"))) {
5800 errno = ENOMEM;
5801 return False;
5804 if (!nt_printing_getsec(mem_ctx, pname, &secdesc)) {
5805 talloc_destroy(mem_ctx);
5806 errno = ENOMEM;
5807 return False;
5810 if (access_type == JOB_ACCESS_ADMINISTER) {
5811 SEC_DESC_BUF *parent_secdesc = secdesc;
5813 /* Create a child security descriptor to check permissions
5814 against. This is because print jobs are child objects
5815 objects of a printer. */
5817 secdesc = se_create_child_secdesc(mem_ctx, parent_secdesc->sd, False);
5819 if (!secdesc) {
5820 talloc_destroy(mem_ctx);
5821 errno = ENOMEM;
5822 return False;
5825 map_job_permissions(secdesc->sd);
5826 } else {
5827 map_printer_permissions(secdesc->sd);
5830 /* Check access */
5831 result = se_access_check(secdesc->sd, user->nt_user_token, access_type,
5832 &access_granted, &status);
5834 DEBUG(4, ("access check was %s\n", result ? "SUCCESS" : "FAILURE"));
5836 /* see if we need to try the printer admin list */
5838 if ((access_granted == 0) &&
5839 (token_contains_name_in_list(uidtoname(user->ut.uid), NULL,
5840 user->nt_user_token,
5841 lp_printer_admin(snum)))) {
5842 talloc_destroy(mem_ctx);
5843 return True;
5846 talloc_destroy(mem_ctx);
5848 if (!result) {
5849 errno = EACCES;
5852 return result;
5855 /****************************************************************************
5856 Check the time parameters allow a print operation.
5857 *****************************************************************************/
5859 bool print_time_access_check(const char *servicename)
5861 NT_PRINTER_INFO_LEVEL *printer = NULL;
5862 bool ok = False;
5863 time_t now = time(NULL);
5864 struct tm *t;
5865 uint32 mins;
5867 if (!W_ERROR_IS_OK(get_a_printer(NULL, &printer, 2, servicename)))
5868 return False;
5870 if (printer->info_2->starttime == 0 && printer->info_2->untiltime == 0)
5871 ok = True;
5873 t = gmtime(&now);
5874 mins = (uint32)t->tm_hour*60 + (uint32)t->tm_min;
5876 if (mins >= printer->info_2->starttime && mins <= printer->info_2->untiltime)
5877 ok = True;
5879 free_a_printer(&printer, 2);
5881 if (!ok)
5882 errno = EACCES;
5884 return ok;
5887 /****************************************************************************
5888 Fill in the servername sent in the _spoolss_open_printer_ex() call
5889 ****************************************************************************/
5891 char* get_server_name( Printer_entry *printer )
5893 return printer->servername;