2000-11-15 00:56:32 +03:00
/*
2002-03-22 09:24:38 +03:00
* Unix SMB / CIFS implementation .
2000-03-10 20:12:24 +03:00
* RPC Pipe client / server routines
* Copyright ( C ) Andrew Tridgell 1992 - 2000 ,
2004-04-13 18:39:48 +04:00
* Copyright ( C ) Jean François Micouleau 1998 - 2000.
2005-07-15 18:26:11 +04:00
* Copyright ( C ) Gerald Carter 2002 - 2005.
2000-11-15 00:56:32 +03:00
*
2000-03-10 20:12:24 +03:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
* the Free Software Foundation ; either version 3 of the License , or
2000-03-10 20:12:24 +03:00
* ( at your option ) any later version .
2000-11-15 00:56:32 +03:00
*
2000-03-10 20:12:24 +03:00
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
2000-11-15 00:56:32 +03:00
*
2000-03-10 20:12:24 +03:00
* You should have received a copy of the GNU General Public License
2007-07-10 09:23:25 +04:00
* along with this program ; if not , see < http : //www.gnu.org/licenses/>.
2000-03-10 20:12:24 +03:00
*/
2000-02-07 19:17:59 +03:00
# include "includes.h"
2001-07-09 22:19:04 +04:00
static TDB_CONTEXT * tdb_forms ; /* used for forms files */
static TDB_CONTEXT * tdb_drivers ; /* used for driver files */
static TDB_CONTEXT * tdb_printers ; /* used for printers files */
2000-05-24 10:10:21 +04:00
# define FORMS_PREFIX "FORMS / "
# define DRIVERS_PREFIX "DRIVERS / "
2001-08-10 23:38:53 +04:00
# define DRIVER_INIT_PREFIX "DRIVER_INIT / "
2000-05-24 10:10:21 +04:00
# define PRINTERS_PREFIX "PRINTERS / "
2001-07-09 22:19:04 +04:00
# define SECDESC_PREFIX "SECDESC / "
2002-02-09 07:10:24 +03:00
# define GLOBAL_C_SETPRINTER "GLOBALS / c_setprinter"
2009-02-09 14:40:12 +03:00
2001-07-09 22:19:04 +04:00
# define NTDRIVERS_DATABASE_VERSION_1 1
# define NTDRIVERS_DATABASE_VERSION_2 2
2002-01-09 08:24:07 +03:00
# define NTDRIVERS_DATABASE_VERSION_3 3 /* little endian version of v2 */
2005-05-27 23:02:30 +04:00
# define NTDRIVERS_DATABASE_VERSION_4 4 /* fix generic bits in security descriptors */
2005-07-15 18:26:11 +04:00
# define NTDRIVERS_DATABASE_VERSION_5 5 /* normalize keys in ntprinters.tdb */
2000-05-24 10:10:21 +04:00
2001-01-04 22:27:08 +03:00
/* Map generic permissions to printer object specific permissions */
2007-10-06 01:41:17 +04:00
const struct generic_mapping printer_generic_mapping = {
2002-03-15 11:14:10 +03:00
PRINTER_READ ,
2002-04-11 07:15:02 +04:00
PRINTER_WRITE ,
2002-03-15 11:14:10 +03:00
PRINTER_EXECUTE ,
PRINTER_ALL_ACCESS
} ;
2007-10-06 01:41:17 +04:00
const struct standard_mapping printer_std_mapping = {
2001-01-04 22:27:08 +03:00
PRINTER_READ ,
PRINTER_WRITE ,
PRINTER_EXECUTE ,
PRINTER_ALL_ACCESS
} ;
2002-04-15 07:49:53 +04:00
/* Map generic permissions to print server object specific permissions */
2007-10-06 01:41:17 +04:00
const struct generic_mapping printserver_generic_mapping = {
2002-04-15 07:49:53 +04:00
SERVER_READ ,
SERVER_WRITE ,
SERVER_EXECUTE ,
SERVER_ALL_ACCESS
} ;
2007-10-06 01:41:17 +04:00
const struct generic_mapping printserver_std_mapping = {
2002-04-15 07:49:53 +04:00
SERVER_READ ,
SERVER_WRITE ,
SERVER_EXECUTE ,
SERVER_ALL_ACCESS
} ;
2008-05-06 03:24:52 +04:00
/* Map generic permissions to job object specific permissions */
const struct generic_mapping job_generic_mapping = {
JOB_READ ,
JOB_WRITE ,
JOB_EXECUTE ,
JOB_ALL_ACCESS
} ;
2000-09-29 01:33:08 +04:00
/* We need one default form to support our default printer. Msoft adds the
forms it wants and in the ORDER it wants them ( note : DEVMODE papersize is an
array index ) . Letter is always first , so ( for the current code ) additions
always put things in the correct order . */
2003-01-03 11:28:12 +03:00
static const nt_forms_struct default_forms [ ] = {
2001-04-04 02:41:31 +04:00
{ " Letter " , 0x1 , 0x34b5c , 0x44368 , 0x0 , 0x0 , 0x34b5c , 0x44368 } ,
{ " Letter Small " , 0x1 , 0x34b5c , 0x44368 , 0x0 , 0x0 , 0x34b5c , 0x44368 } ,
{ " Tabloid " , 0x1 , 0x44368 , 0x696b8 , 0x0 , 0x0 , 0x44368 , 0x696b8 } ,
{ " Ledger " , 0x1 , 0x696b8 , 0x44368 , 0x0 , 0x0 , 0x696b8 , 0x44368 } ,
{ " Legal " , 0x1 , 0x34b5c , 0x56d10 , 0x0 , 0x0 , 0x34b5c , 0x56d10 } ,
{ " Statement " , 0x1 , 0x221b4 , 0x34b5c , 0x0 , 0x0 , 0x221b4 , 0x34b5c } ,
{ " Executive " , 0x1 , 0x2cf56 , 0x411cc , 0x0 , 0x0 , 0x2cf56 , 0x411cc } ,
{ " A3 " , 0x1 , 0x48828 , 0x668a0 , 0x0 , 0x0 , 0x48828 , 0x668a0 } ,
{ " A4 " , 0x1 , 0x33450 , 0x48828 , 0x0 , 0x0 , 0x33450 , 0x48828 } ,
{ " A4 Small " , 0x1 , 0x33450 , 0x48828 , 0x0 , 0x0 , 0x33450 , 0x48828 } ,
{ " A5 " , 0x1 , 0x24220 , 0x33450 , 0x0 , 0x0 , 0x24220 , 0x33450 } ,
{ " B4 (JIS) " , 0x1 , 0x3ebe8 , 0x58de0 , 0x0 , 0x0 , 0x3ebe8 , 0x58de0 } ,
{ " B5 (JIS) " , 0x1 , 0x2c6f0 , 0x3ebe8 , 0x0 , 0x0 , 0x2c6f0 , 0x3ebe8 } ,
{ " Folio " , 0x1 , 0x34b5c , 0x509d8 , 0x0 , 0x0 , 0x34b5c , 0x509d8 } ,
{ " Quarto " , 0x1 , 0x347d8 , 0x43238 , 0x0 , 0x0 , 0x347d8 , 0x43238 } ,
{ " 10x14 " , 0x1 , 0x3e030 , 0x56d10 , 0x0 , 0x0 , 0x3e030 , 0x56d10 } ,
{ " 11x17 " , 0x1 , 0x44368 , 0x696b8 , 0x0 , 0x0 , 0x44368 , 0x696b8 } ,
{ " Note " , 0x1 , 0x34b5c , 0x44368 , 0x0 , 0x0 , 0x34b5c , 0x44368 } ,
{ " Envelope #9 " , 0x1 , 0x18079 , 0x37091 , 0x0 , 0x0 , 0x18079 , 0x37091 } ,
{ " Envelope #10 " , 0x1 , 0x19947 , 0x3ae94 , 0x0 , 0x0 , 0x19947 , 0x3ae94 } ,
{ " Envelope #11 " , 0x1 , 0x1be7c , 0x40565 , 0x0 , 0x0 , 0x1be7c , 0x40565 } ,
{ " Envelope #12 " , 0x1 , 0x1d74a , 0x44368 , 0x0 , 0x0 , 0x1d74a , 0x44368 } ,
{ " Envelope #14 " , 0x1 , 0x1f018 , 0x47504 , 0x0 , 0x0 , 0x1f018 , 0x47504 } ,
{ " C size sheet " , 0x1 , 0x696b8 , 0x886d0 , 0x0 , 0x0 , 0x696b8 , 0x886d0 } ,
{ " D size sheet " , 0x1 , 0x886d0 , 0xd2d70 , 0x0 , 0x0 , 0x886d0 , 0xd2d70 } ,
{ " E size sheet " , 0x1 , 0xd2d70 , 0x110da0 , 0x0 , 0x0 , 0xd2d70 , 0x110da0 } ,
{ " Envelope DL " , 0x1 , 0x1adb0 , 0x35b60 , 0x0 , 0x0 , 0x1adb0 , 0x35b60 } ,
{ " Envelope C5 " , 0x1 , 0x278d0 , 0x37e88 , 0x0 , 0x0 , 0x278d0 , 0x37e88 } ,
{ " Envelope C3 " , 0x1 , 0x4f1a0 , 0x6fd10 , 0x0 , 0x0 , 0x4f1a0 , 0x6fd10 } ,
{ " Envelope C4 " , 0x1 , 0x37e88 , 0x4f1a0 , 0x0 , 0x0 , 0x37e88 , 0x4f1a0 } ,
{ " Envelope C6 " , 0x1 , 0x1bd50 , 0x278d0 , 0x0 , 0x0 , 0x1bd50 , 0x278d0 } ,
{ " Envelope C65 " , 0x1 , 0x1bd50 , 0x37e88 , 0x0 , 0x0 , 0x1bd50 , 0x37e88 } ,
{ " Envelope B4 " , 0x1 , 0x3d090 , 0x562e8 , 0x0 , 0x0 , 0x3d090 , 0x562e8 } ,
{ " Envelope B5 " , 0x1 , 0x2af80 , 0x3d090 , 0x0 , 0x0 , 0x2af80 , 0x3d090 } ,
{ " Envelope B6 " , 0x1 , 0x2af80 , 0x1e848 , 0x0 , 0x0 , 0x2af80 , 0x1e848 } ,
{ " Envelope " , 0x1 , 0x1adb0 , 0x38270 , 0x0 , 0x0 , 0x1adb0 , 0x38270 } ,
{ " Envelope Monarch " , 0x1 , 0x18079 , 0x2e824 , 0x0 , 0x0 , 0x18079 , 0x2e824 } ,
{ " 6 3/4 Envelope " , 0x1 , 0x167ab , 0x284ec , 0x0 , 0x0 , 0x167ab , 0x284ec } ,
{ " US Std Fanfold " , 0x1 , 0x5c3e1 , 0x44368 , 0x0 , 0x0 , 0x5c3e1 , 0x44368 } ,
{ " German Std Fanfold " , 0x1 , 0x34b5c , 0x4a6a0 , 0x0 , 0x0 , 0x34b5c , 0x4a6a0 } ,
{ " German Legal Fanfold " , 0x1 , 0x34b5c , 0x509d8 , 0x0 , 0x0 , 0x34b5c , 0x509d8 } ,
{ " B4 (ISO) " , 0x1 , 0x3d090 , 0x562e8 , 0x0 , 0x0 , 0x3d090 , 0x562e8 } ,
{ " Japanese Postcard " , 0x1 , 0x186a0 , 0x24220 , 0x0 , 0x0 , 0x186a0 , 0x24220 } ,
{ " 9x11 " , 0x1 , 0x37cf8 , 0x44368 , 0x0 , 0x0 , 0x37cf8 , 0x44368 } ,
{ " 10x11 " , 0x1 , 0x3e030 , 0x44368 , 0x0 , 0x0 , 0x3e030 , 0x44368 } ,
{ " 15x11 " , 0x1 , 0x5d048 , 0x44368 , 0x0 , 0x0 , 0x5d048 , 0x44368 } ,
{ " Envelope Invite " , 0x1 , 0x35b60 , 0x35b60 , 0x0 , 0x0 , 0x35b60 , 0x35b60 } ,
{ " Reserved48 " , 0x1 , 0x1 , 0x1 , 0x0 , 0x0 , 0x1 , 0x1 } ,
{ " Reserved49 " , 0x1 , 0x1 , 0x1 , 0x0 , 0x0 , 0x1 , 0x1 } ,
{ " Letter Extra " , 0x1 , 0x3ae94 , 0x4a6a0 , 0x0 , 0x0 , 0x3ae94 , 0x4a6a0 } ,
{ " Legal Extra " , 0x1 , 0x3ae94 , 0x5d048 , 0x0 , 0x0 , 0x3ae94 , 0x5d048 } ,
{ " Tabloid Extra " , 0x1 , 0x4a6a0 , 0x6f9f0 , 0x0 , 0x0 , 0x4a6a0 , 0x6f9f0 } ,
{ " A4 Extra " , 0x1 , 0x397c2 , 0x4eb16 , 0x0 , 0x0 , 0x397c2 , 0x4eb16 } ,
{ " Letter Transverse " , 0x1 , 0x34b5c , 0x44368 , 0x0 , 0x0 , 0x34b5c , 0x44368 } ,
{ " A4 Transverse " , 0x1 , 0x33450 , 0x48828 , 0x0 , 0x0 , 0x33450 , 0x48828 } ,
{ " Letter Extra Transverse " , 0x1 , 0x3ae94 , 0x4a6a0 , 0x0 , 0x0 , 0x3ae94 , 0x4a6a0 } ,
{ " Super A " , 0x1 , 0x376b8 , 0x56ea0 , 0x0 , 0x0 , 0x376b8 , 0x56ea0 } ,
{ " Super B " , 0x1 , 0x4a768 , 0x76e58 , 0x0 , 0x0 , 0x4a768 , 0x76e58 } ,
{ " Letter Plus " , 0x1 , 0x34b5c , 0x4eb16 , 0x0 , 0x0 , 0x34b5c , 0x4eb16 } ,
{ " A4 Plus " , 0x1 , 0x33450 , 0x50910 , 0x0 , 0x0 , 0x33450 , 0x50910 } ,
{ " A5 Transverse " , 0x1 , 0x24220 , 0x33450 , 0x0 , 0x0 , 0x24220 , 0x33450 } ,
{ " B5 (JIS) Transverse " , 0x1 , 0x2c6f0 , 0x3ebe8 , 0x0 , 0x0 , 0x2c6f0 , 0x3ebe8 } ,
{ " A3 Extra " , 0x1 , 0x4e9d0 , 0x6ca48 , 0x0 , 0x0 , 0x4e9d0 , 0x6ca48 } ,
{ " A5 Extra " , 0x1 , 0x2a7b0 , 0x395f8 , 0x0 , 0x0 , 0x2a7b0 , 0x395f8 } ,
{ " B5 (ISO) Extra " , 0x1 , 0x31128 , 0x43620 , 0x0 , 0x0 , 0x31128 , 0x43620 } ,
{ " A2 " , 0x1 , 0x668a0 , 0x91050 , 0x0 , 0x0 , 0x668a0 , 0x91050 } ,
{ " A3 Transverse " , 0x1 , 0x48828 , 0x668a0 , 0x0 , 0x0 , 0x48828 , 0x668a0 } ,
{ " A3 Extra Transverse " , 0x1 , 0x4e9d0 , 0x6ca48 , 0x0 , 0x0 , 0x4e9d0 , 0x6ca48 } ,
{ " Japanese Double Postcard " , 0x1 , 0x30d40 , 0x24220 , 0x0 , 0x0 , 0x30d40 , 0x24220 } ,
{ " A6 " , 0x1 , 0x19a28 , 0x24220 , 0x0 , 0x0 , 0x19a28 , 0x24220 } ,
{ " Japanese Envelope Kaku #2 " , 0x1 , 0x3a980 , 0x510e0 , 0x0 , 0x0 , 0x3a980 , 0x510e0 } ,
{ " Japanese Envelope Kaku #3 " , 0x1 , 0x34bc0 , 0x43a08 , 0x0 , 0x0 , 0x34bc0 , 0x43a08 } ,
{ " Japanese Envelope Chou #3 " , 0x1 , 0x1d4c0 , 0x395f8 , 0x0 , 0x0 , 0x1d4c0 , 0x395f8 } ,
{ " Japanese Envelope Chou #4 " , 0x1 , 0x15f90 , 0x320c8 , 0x0 , 0x0 , 0x15f90 , 0x320c8 } ,
{ " Letter Rotated " , 0x1 , 0x44368 , 0x34b5c , 0x0 , 0x0 , 0x44368 , 0x34b5c } ,
{ " A3 Rotated " , 0x1 , 0x668a0 , 0x48828 , 0x0 , 0x0 , 0x668a0 , 0x48828 } ,
{ " A4 Rotated " , 0x1 , 0x48828 , 0x33450 , 0x0 , 0x0 , 0x48828 , 0x33450 } ,
{ " A5 Rotated " , 0x1 , 0x33450 , 0x24220 , 0x0 , 0x0 , 0x33450 , 0x24220 } ,
{ " B4 (JIS) Rotated " , 0x1 , 0x58de0 , 0x3ebe8 , 0x0 , 0x0 , 0x58de0 , 0x3ebe8 } ,
{ " B5 (JIS) Rotated " , 0x1 , 0x3ebe8 , 0x2c6f0 , 0x0 , 0x0 , 0x3ebe8 , 0x2c6f0 } ,
{ " Japanese Postcard Rotated " , 0x1 , 0x24220 , 0x186a0 , 0x0 , 0x0 , 0x24220 , 0x186a0 } ,
{ " Double Japan Postcard Rotated " , 0x1 , 0x24220 , 0x30d40 , 0x0 , 0x0 , 0x24220 , 0x30d40 } ,
{ " A6 Rotated " , 0x1 , 0x24220 , 0x19a28 , 0x0 , 0x0 , 0x24220 , 0x19a28 } ,
{ " Japan Envelope Kaku #2 Rotated " , 0x1 , 0x510e0 , 0x3a980 , 0x0 , 0x0 , 0x510e0 , 0x3a980 } ,
{ " Japan Envelope Kaku #3 Rotated " , 0x1 , 0x43a08 , 0x34bc0 , 0x0 , 0x0 , 0x43a08 , 0x34bc0 } ,
{ " Japan Envelope Chou #3 Rotated " , 0x1 , 0x395f8 , 0x1d4c0 , 0x0 , 0x0 , 0x395f8 , 0x1d4c0 } ,
{ " Japan Envelope Chou #4 Rotated " , 0x1 , 0x320c8 , 0x15f90 , 0x0 , 0x0 , 0x320c8 , 0x15f90 } ,
{ " B6 (JIS) " , 0x1 , 0x1f400 , 0x2c6f0 , 0x0 , 0x0 , 0x1f400 , 0x2c6f0 } ,
{ " B6 (JIS) Rotated " , 0x1 , 0x2c6f0 , 0x1f400 , 0x0 , 0x0 , 0x2c6f0 , 0x1f400 } ,
{ " 12x11 " , 0x1 , 0x4a724 , 0x443e1 , 0x0 , 0x0 , 0x4a724 , 0x443e1 } ,
{ " Japan Envelope You #4 " , 0x1 , 0x19a28 , 0x395f8 , 0x0 , 0x0 , 0x19a28 , 0x395f8 } ,
{ " Japan Envelope You #4 Rotated " , 0x1 , 0x395f8 , 0x19a28 , 0x0 , 0x0 , 0x395f8 , 0x19a28 } ,
{ " PRC 16K " , 0x1 , 0x2de60 , 0x3f7a0 , 0x0 , 0x0 , 0x2de60 , 0x3f7a0 } ,
{ " PRC 32K " , 0x1 , 0x1fbd0 , 0x2cec0 , 0x0 , 0x0 , 0x1fbd0 , 0x2cec0 } ,
{ " PRC 32K(Big) " , 0x1 , 0x222e0 , 0x318f8 , 0x0 , 0x0 , 0x222e0 , 0x318f8 } ,
{ " PRC Envelope #1 " , 0x1 , 0x18e70 , 0x28488 , 0x0 , 0x0 , 0x18e70 , 0x28488 } ,
{ " PRC Envelope #2 " , 0x1 , 0x18e70 , 0x2af80 , 0x0 , 0x0 , 0x18e70 , 0x2af80 } ,
{ " PRC Envelope #3 " , 0x1 , 0x1e848 , 0x2af80 , 0x0 , 0x0 , 0x1e848 , 0x2af80 } ,
{ " PRC Envelope #4 " , 0x1 , 0x1adb0 , 0x32c80 , 0x0 , 0x0 , 0x1adb0 , 0x32c80 } ,
{ " PRC Envelope #5 " , 0x1 , 0x1adb0 , 0x35b60 , 0x0 , 0x0 , 0x1adb0 , 0x35b60 } ,
{ " PRC Envelope #6 " , 0x1 , 0x1d4c0 , 0x38270 , 0x0 , 0x0 , 0x1d4c0 , 0x38270 } ,
{ " PRC Envelope #7 " , 0x1 , 0x27100 , 0x38270 , 0x0 , 0x0 , 0x27100 , 0x38270 } ,
{ " PRC Envelope #8 " , 0x1 , 0x1d4c0 , 0x4b708 , 0x0 , 0x0 , 0x1d4c0 , 0x4b708 } ,
{ " PRC Envelope #9 " , 0x1 , 0x37e88 , 0x4f1a0 , 0x0 , 0x0 , 0x37e88 , 0x4f1a0 } ,
{ " PRC Envelope #10 " , 0x1 , 0x4f1a0 , 0x6fd10 , 0x0 , 0x0 , 0x4f1a0 , 0x6fd10 } ,
{ " PRC 16K Rotated " , 0x1 , 0x3f7a0 , 0x2de60 , 0x0 , 0x0 , 0x3f7a0 , 0x2de60 } ,
{ " PRC 32K Rotated " , 0x1 , 0x2cec0 , 0x1fbd0 , 0x0 , 0x0 , 0x2cec0 , 0x1fbd0 } ,
{ " PRC 32K(Big) Rotated " , 0x1 , 0x318f8 , 0x222e0 , 0x0 , 0x0 , 0x318f8 , 0x222e0 } ,
{ " PRC Envelope #1 Rotated " , 0x1 , 0x28488 , 0x18e70 , 0x0 , 0x0 , 0x28488 , 0x18e70 } ,
{ " PRC Envelope #2 Rotated " , 0x1 , 0x2af80 , 0x18e70 , 0x0 , 0x0 , 0x2af80 , 0x18e70 } ,
{ " PRC Envelope #3 Rotated " , 0x1 , 0x2af80 , 0x1e848 , 0x0 , 0x0 , 0x2af80 , 0x1e848 } ,
{ " PRC Envelope #4 Rotated " , 0x1 , 0x32c80 , 0x1adb0 , 0x0 , 0x0 , 0x32c80 , 0x1adb0 } ,
{ " PRC Envelope #5 Rotated " , 0x1 , 0x35b60 , 0x1adb0 , 0x0 , 0x0 , 0x35b60 , 0x1adb0 } ,
{ " PRC Envelope #6 Rotated " , 0x1 , 0x38270 , 0x1d4c0 , 0x0 , 0x0 , 0x38270 , 0x1d4c0 } ,
{ " PRC Envelope #7 Rotated " , 0x1 , 0x38270 , 0x27100 , 0x0 , 0x0 , 0x38270 , 0x27100 } ,
{ " PRC Envelope #8 Rotated " , 0x1 , 0x4b708 , 0x1d4c0 , 0x0 , 0x0 , 0x4b708 , 0x1d4c0 } ,
{ " PRC Envelope #9 Rotated " , 0x1 , 0x4f1a0 , 0x37e88 , 0x0 , 0x0 , 0x4f1a0 , 0x37e88 } ,
{ " PRC Envelope #10 Rotated " , 0x1 , 0x6fd10 , 0x4f1a0 , 0x0 , 0x0 , 0x6fd10 , 0x4f1a0 }
2000-06-06 05:47:11 +04:00
} ;
2009-05-12 16:15:01 +04:00
static const struct print_architecture_table_node archi_table [ ] = {
2003-04-23 17:27:35 +04:00
2003-08-15 00:51:41 +04:00
{ " Windows 4.0 " , SPL_ARCH_WIN40 , 0 } ,
{ " Windows NT x86 " , SPL_ARCH_W32X86 , 2 } ,
{ " Windows NT R4000 " , SPL_ARCH_W32MIPS , 2 } ,
{ " Windows NT Alpha_AXP " , SPL_ARCH_W32ALPHA , 2 } ,
{ " Windows NT PowerPC " , SPL_ARCH_W32PPC , 2 } ,
2004-08-10 18:27:17 +04:00
{ " Windows IA64 " , SPL_ARCH_IA64 , 3 } ,
2004-10-05 02:13:57 +04:00
{ " Windows x64 " , SPL_ARCH_X64 , 3 } ,
2003-04-23 17:27:35 +04:00
{ NULL , " " , - 1 }
} ;
2005-07-15 18:26:11 +04:00
/****************************************************************************
generate a new TDB_DATA key for storing a printer
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-22 00:56:36 +03:00
static TDB_DATA make_printer_tdbkey ( TALLOC_CTX * ctx , const char * sharename )
2005-07-15 18:26:11 +04:00
{
fstring share ;
2007-11-22 00:56:36 +03:00
char * keystr = NULL ;
2005-07-15 18:26:11 +04:00
TDB_DATA key ;
2007-11-22 00:56:36 +03:00
fstrcpy ( share , sharename ) ;
strlower_m ( share ) ;
keystr = talloc_asprintf ( ctx , " %s%s " , PRINTERS_PREFIX , share ) ;
key = string_term_tdb_data ( keystr ? keystr : " " ) ;
2005-07-15 18:26:11 +04:00
return key ;
}
/****************************************************************************
generate a new TDB_DATA key for storing a printer security descriptor
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-22 00:56:36 +03:00
static TDB_DATA make_printers_secdesc_tdbkey ( TALLOC_CTX * ctx ,
const char * sharename )
2005-07-15 18:26:11 +04:00
{
fstring share ;
2007-11-22 00:56:36 +03:00
char * keystr = NULL ;
2007-03-27 15:22:07 +04:00
TDB_DATA key ;
2007-11-22 00:56:36 +03:00
fstrcpy ( share , sharename ) ;
strlower_m ( share ) ;
2005-07-15 18:26:11 +04:00
2007-11-22 00:56:36 +03:00
keystr = talloc_asprintf ( ctx , " %s%s " , SECDESC_PREFIX , share ) ;
key = string_term_tdb_data ( keystr ? keystr : " " ) ;
2007-03-27 15:22:07 +04:00
return key ;
2005-07-15 18:26:11 +04:00
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool upgrade_to_version_3 ( void )
2001-07-09 22:19:04 +04:00
{
TDB_DATA kbuf , newkey , dbuf ;
2009-02-09 14:40:12 +03:00
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3: upgrading print tdb's to version 3 \n " ) ) ;
2009-02-09 14:40:12 +03:00
2001-07-31 03:12:48 +04:00
for ( kbuf = tdb_firstkey ( tdb_drivers ) ; kbuf . dptr ;
2008-10-19 15:06:14 +04:00
newkey = tdb_nextkey ( tdb_drivers , kbuf ) , free ( kbuf . dptr ) , kbuf = newkey ) {
2001-07-09 22:19:04 +04:00
dbuf = tdb_fetch ( tdb_drivers , kbuf ) ;
2007-03-29 13:35:51 +04:00
if ( strncmp ( ( const char * ) kbuf . dptr , FORMS_PREFIX , strlen ( FORMS_PREFIX ) ) = = 0 ) {
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3:moving form \n " ) ) ;
2001-07-31 03:12:48 +04:00
if ( tdb_store ( tdb_forms , kbuf , dbuf , TDB_REPLACE ) ! = 0 ) {
2002-11-23 05:52:36 +03:00
SAFE_FREE ( dbuf . dptr ) ;
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3: failed to move form. Error (%s). \n " , tdb_errorstr ( tdb_forms ) ) ) ;
2001-07-31 03:12:48 +04:00
return False ;
}
if ( tdb_delete ( tdb_drivers , kbuf ) ! = 0 ) {
2002-11-23 05:52:36 +03:00
SAFE_FREE ( dbuf . dptr ) ;
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3: failed to delete form. Error (%s) \n " , tdb_errorstr ( tdb_drivers ) ) ) ;
2001-07-31 03:12:48 +04:00
return False ;
}
2001-07-09 22:19:04 +04:00
}
2009-02-09 14:40:12 +03:00
2007-03-29 13:35:51 +04:00
if ( strncmp ( ( const char * ) kbuf . dptr , PRINTERS_PREFIX , strlen ( PRINTERS_PREFIX ) ) = = 0 ) {
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3:moving printer \n " ) ) ;
2001-07-31 03:12:48 +04:00
if ( tdb_store ( tdb_printers , kbuf , dbuf , TDB_REPLACE ) ! = 0 ) {
2002-11-23 05:52:36 +03:00
SAFE_FREE ( dbuf . dptr ) ;
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3: failed to move printer. Error (%s) \n " , tdb_errorstr ( tdb_printers ) ) ) ;
2001-07-31 03:12:48 +04:00
return False ;
}
if ( tdb_delete ( tdb_drivers , kbuf ) ! = 0 ) {
2002-11-23 05:52:36 +03:00
SAFE_FREE ( dbuf . dptr ) ;
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3: failed to delete printer. Error (%s) \n " , tdb_errorstr ( tdb_drivers ) ) ) ;
2001-07-31 03:12:48 +04:00
return False ;
}
2001-07-09 22:19:04 +04:00
}
2009-02-09 14:40:12 +03:00
2007-03-29 13:35:51 +04:00
if ( strncmp ( ( const char * ) kbuf . dptr , SECDESC_PREFIX , strlen ( SECDESC_PREFIX ) ) = = 0 ) {
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3:moving secdesc \n " ) ) ;
2001-07-31 03:12:48 +04:00
if ( tdb_store ( tdb_printers , kbuf , dbuf , TDB_REPLACE ) ! = 0 ) {
2002-11-23 05:52:36 +03:00
SAFE_FREE ( dbuf . dptr ) ;
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3: failed to move secdesc. Error (%s) \n " , tdb_errorstr ( tdb_printers ) ) ) ;
2001-07-31 03:12:48 +04:00
return False ;
}
if ( tdb_delete ( tdb_drivers , kbuf ) ! = 0 ) {
2002-11-23 05:52:36 +03:00
SAFE_FREE ( dbuf . dptr ) ;
2002-01-09 08:24:07 +03:00
DEBUG ( 0 , ( " upgrade_to_version_3: failed to delete secdesc. Error (%s) \n " , tdb_errorstr ( tdb_drivers ) ) ) ;
2001-07-31 03:12:48 +04:00
return False ;
}
2001-07-09 22:19:04 +04:00
}
2009-02-09 14:40:12 +03:00
2001-09-17 13:25:57 +04:00
SAFE_FREE ( dbuf . dptr ) ;
2001-07-09 22:19:04 +04:00
}
2001-07-31 03:12:48 +04:00
return True ;
2001-07-09 22:19:04 +04:00
}
2005-05-27 23:02:30 +04:00
/*******************************************************************
2009-02-09 14:40:12 +03:00
Fix an issue with security descriptors . Printer sec_desc must
use more than the generic bits that were previously used
2005-05-27 23:02:30 +04:00
in < = 3.0 .14 a . They must also have a owner and group SID assigned .
2009-02-09 14:40:12 +03:00
Otherwise , any printers than have been migrated to a Windows
2005-05-27 23:02:30 +04:00
host using printmig . exe will not be accessible .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int sec_desc_upg_fn ( TDB_CONTEXT * the_tdb , TDB_DATA key ,
TDB_DATA data , void * state )
{
2009-03-18 14:46:40 +03:00
NTSTATUS status ;
2005-05-27 23:02:30 +04:00
SEC_DESC_BUF * sd_orig = NULL ;
SEC_DESC_BUF * sd_new , * sd_store ;
SEC_DESC * sec , * new_sec ;
TALLOC_CTX * ctx = state ;
int result , i ;
2005-09-12 00:53:21 +04:00
uint32 sd_size ;
size_t size_new_sec ;
2005-05-27 23:02:30 +04:00
2006-06-20 01:36:19 +04:00
if ( ! data . dptr | | data . dsize = = 0 ) {
2005-05-27 23:02:30 +04:00
return 0 ;
2006-06-20 01:36:19 +04:00
}
2005-05-27 23:02:30 +04:00
2007-03-29 13:35:51 +04:00
if ( strncmp ( ( const char * ) key . dptr , SECDESC_PREFIX , strlen ( SECDESC_PREFIX ) ) ! = 0 ) {
2005-05-27 23:02:30 +04:00
return 0 ;
2006-06-20 01:36:19 +04:00
}
2005-05-27 23:02:30 +04:00
/* upgrade the security descriptor */
2009-03-18 14:46:40 +03:00
status = unmarshall_sec_desc_buf ( ctx , data . dptr , data . dsize , & sd_orig ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2005-05-27 23:02:30 +04:00
/* delete bad entries */
2007-03-29 13:35:51 +04:00
DEBUG ( 0 , ( " sec_desc_upg_fn: Failed to parse original sec_desc for %si. Deleting.... \n " ,
( const char * ) key . dptr ) ) ;
2005-05-27 23:02:30 +04:00
tdb_delete ( tdb_printers , key ) ;
return 0 ;
}
2006-06-20 01:36:19 +04:00
if ( ! sd_orig ) {
return 0 ;
}
2006-09-21 02:23:12 +04:00
sec = sd_orig - > sd ;
2009-02-09 14:40:12 +03:00
2005-05-27 23:02:30 +04:00
/* is this even valid? */
2009-02-09 14:40:12 +03:00
2006-06-20 01:36:19 +04:00
if ( ! sec - > dacl ) {
2005-05-27 23:02:30 +04:00
return 0 ;
2006-06-20 01:36:19 +04:00
}
2009-02-09 14:40:12 +03:00
2005-05-27 23:02:30 +04:00
/* update access masks */
2009-02-09 14:40:12 +03:00
2005-05-27 23:02:30 +04:00
for ( i = 0 ; i < sec - > dacl - > num_aces ; i + + ) {
2006-09-21 02:23:12 +04:00
switch ( sec - > dacl - > aces [ i ] . access_mask ) {
2005-05-27 23:02:30 +04:00
case ( GENERIC_READ_ACCESS | GENERIC_WRITE_ACCESS | GENERIC_EXECUTE_ACCESS ) :
2006-09-21 02:23:12 +04:00
sec - > dacl - > aces [ i ] . access_mask = PRINTER_ACE_PRINT ;
2005-05-27 23:02:30 +04:00
break ;
2009-02-09 14:40:12 +03:00
2005-05-27 23:02:30 +04:00
case GENERIC_ALL_ACCESS :
2006-09-21 02:23:12 +04:00
sec - > dacl - > aces [ i ] . access_mask = PRINTER_ACE_FULL_CONTROL ;
2005-05-27 23:02:30 +04:00
break ;
2009-02-09 14:40:12 +03:00
2005-05-27 23:02:30 +04:00
case READ_CONTROL_ACCESS :
2006-09-21 02:23:12 +04:00
sec - > dacl - > aces [ i ] . access_mask = PRINTER_ACE_MANAGE_DOCUMENTS ;
2009-02-09 14:40:12 +03:00
2005-05-27 23:02:30 +04:00
default : /* no change */
break ;
}
}
/* create a new SEC_DESC with the appropriate owner and group SIDs */
2005-05-28 00:34:19 +04:00
new_sec = make_sec_desc ( ctx , SEC_DESC_REVISION , SEC_DESC_SELF_RELATIVE ,
2008-03-17 23:00:16 +03:00
& global_sid_Builtin_Administrators ,
& global_sid_Builtin_Administrators ,
NULL , NULL , & size_new_sec ) ;
2006-06-20 01:36:19 +04:00
if ( ! new_sec ) {
return 0 ;
}
2005-05-27 23:02:30 +04:00
sd_new = make_sec_desc_buf ( ctx , size_new_sec , new_sec ) ;
2006-06-20 01:36:19 +04:00
if ( ! sd_new ) {
return 0 ;
}
2005-05-27 23:02:30 +04:00
2010-04-26 19:38:56 +04:00
if ( ! ( sd_store = sec_desc_merge_buf ( ctx , sd_new , sd_orig ) ) ) {
2005-05-27 23:02:30 +04:00
DEBUG ( 0 , ( " sec_desc_upg_fn: Failed to update sec_desc for %s \n " , key . dptr ) ) ;
return 0 ;
}
2009-02-09 14:40:12 +03:00
2005-05-27 23:02:30 +04:00
/* store it back */
2009-02-09 14:40:12 +03:00
2009-01-01 06:45:33 +03:00
sd_size = ndr_size_security_descriptor ( sd_store - > sd , NULL , 0 )
2007-12-30 01:00:49 +03:00
+ sizeof ( SEC_DESC_BUF ) ;
2005-05-27 23:02:30 +04:00
2009-03-18 14:46:40 +03:00
status = marshall_sec_desc_buf ( ctx , sd_store , & data . dptr , & data . dsize ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2005-05-27 23:02:30 +04:00
DEBUG ( 0 , ( " sec_desc_upg_fn: Failed to parse new sec_desc for %s \n " , key . dptr ) ) ;
return 0 ;
}
result = tdb_store ( tdb_printers , key , data , TDB_REPLACE ) ;
/* 0 to continue and non-zero to stop traversal */
return ( result = = - 1 ) ;
}
/*******************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool upgrade_to_version_4 ( void )
2005-05-27 23:02:30 +04:00
{
TALLOC_CTX * ctx ;
int result ;
DEBUG ( 0 , ( " upgrade_to_version_4: upgrading printer security descriptors \n " ) ) ;
2009-02-09 14:40:12 +03:00
if ( ! ( ctx = talloc_init ( " upgrade_to_version_4 " ) ) )
2005-05-27 23:02:30 +04:00
return False ;
result = tdb_traverse ( tdb_printers , sec_desc_upg_fn , ctx ) ;
talloc_destroy ( ctx ) ;
return ( result ! = - 1 ) ;
}
2005-07-15 18:26:11 +04:00
/*******************************************************************
2009-02-09 14:40:12 +03:00
Fix an issue with security descriptors . Printer sec_desc must
use more than the generic bits that were previously used
2005-07-15 18:26:11 +04:00
in < = 3.0 .14 a . They must also have a owner and group SID assigned .
2009-02-09 14:40:12 +03:00
Otherwise , any printers than have been migrated to a Windows
2005-07-15 18:26:11 +04:00
host using printmig . exe will not be accessible .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int normalize_printers_fn ( TDB_CONTEXT * the_tdb , TDB_DATA key ,
TDB_DATA data , void * state )
{
2007-11-22 00:56:36 +03:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
2005-07-15 18:26:11 +04:00
TDB_DATA new_key ;
2007-11-22 00:56:36 +03:00
2005-07-15 18:26:11 +04:00
if ( ! data . dptr | | data . dsize = = 0 )
return 0 ;
/* upgrade printer records and security descriptors */
2007-11-22 00:56:36 +03:00
2007-03-29 13:35:51 +04:00
if ( strncmp ( ( const char * ) key . dptr , PRINTERS_PREFIX , strlen ( PRINTERS_PREFIX ) ) = = 0 ) {
2007-11-22 00:56:36 +03:00
new_key = make_printer_tdbkey ( ctx , ( const char * ) key . dptr + strlen ( PRINTERS_PREFIX ) ) ;
2005-07-15 18:26:11 +04:00
}
2007-03-29 13:35:51 +04:00
else if ( strncmp ( ( const char * ) key . dptr , SECDESC_PREFIX , strlen ( SECDESC_PREFIX ) ) = = 0 ) {
2007-11-22 00:56:36 +03:00
new_key = make_printers_secdesc_tdbkey ( ctx , ( const char * ) key . dptr + strlen ( SECDESC_PREFIX ) ) ;
2005-07-15 18:26:11 +04:00
}
else {
/* ignore this record */
return 0 ;
}
2007-11-22 00:56:36 +03:00
2005-07-15 18:26:11 +04:00
/* delete the original record and store under the normalized key */
2007-11-22 00:56:36 +03:00
2005-07-15 18:26:11 +04:00
if ( tdb_delete ( the_tdb , key ) ! = 0 ) {
2009-02-09 14:40:12 +03:00
DEBUG ( 0 , ( " normalize_printers_fn: tdb_delete for [%s] failed! \n " ,
2005-07-15 18:26:11 +04:00
key . dptr ) ) ;
return 1 ;
}
2007-11-22 00:56:36 +03:00
2005-07-15 18:26:11 +04:00
if ( tdb_store ( the_tdb , new_key , data , TDB_REPLACE ) ! = 0 ) {
DEBUG ( 0 , ( " normalize_printers_fn: failed to store new record for [%s]! \n " ,
key . dptr ) ) ;
return 1 ;
}
2007-11-22 00:56:36 +03:00
2005-07-15 18:26:11 +04:00
return 0 ;
}
/*******************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool upgrade_to_version_5 ( void )
2005-07-15 18:26:11 +04:00
{
TALLOC_CTX * ctx ;
int result ;
DEBUG ( 0 , ( " upgrade_to_version_5: normalizing printer keys \n " ) ) ;
2009-02-09 14:40:12 +03:00
if ( ! ( ctx = talloc_init ( " upgrade_to_version_5 " ) ) )
2005-07-15 18:26:11 +04:00
return False ;
result = tdb_traverse ( tdb_printers , normalize_printers_fn , NULL ) ;
talloc_destroy ( ctx ) ;
return ( result ! = - 1 ) ;
}
2000-02-07 19:17:59 +03:00
/****************************************************************************
2002-11-01 02:41:00 +03:00
Open the NT printing tdbs . Done once before fork ( ) .
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-09 08:24:07 +03:00
2007-10-19 04:40:25 +04:00
bool nt_printing_init ( struct messaging_context * msg_ctx )
2000-02-07 19:17:59 +03:00
{
2003-01-03 11:28:12 +03:00
const char * vstring = " INFO/version " ;
2004-09-23 23:24:02 +04:00
WERROR win_rc ;
2005-07-18 18:18:39 +04:00
int32 vers_id ;
2000-05-24 10:10:21 +04:00
2005-06-17 19:53:01 +04:00
if ( tdb_drivers & & tdb_printers & & tdb_forms )
2001-08-10 23:38:53 +04:00
return True ;
2009-02-09 14:40:12 +03:00
2002-11-01 02:41:00 +03:00
if ( tdb_drivers )
tdb_close ( tdb_drivers ) ;
2007-11-01 22:53:44 +03:00
tdb_drivers = tdb_open_log ( state_path ( " ntdrivers.tdb " ) , 0 , TDB_DEFAULT , O_RDWR | O_CREAT , 0600 ) ;
2001-07-09 22:19:04 +04:00
if ( ! tdb_drivers ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 0 , ( " nt_printing_init: Failed to open nt drivers database %s (%s) \n " ,
2007-11-01 22:53:44 +03:00
state_path ( " ntdrivers.tdb " ) , strerror ( errno ) ) ) ;
2000-05-27 11:27:21 +04:00
return False ;
}
2009-02-09 14:40:12 +03:00
2002-11-01 02:41:00 +03:00
if ( tdb_printers )
tdb_close ( tdb_printers ) ;
2007-11-01 22:53:44 +03:00
tdb_printers = tdb_open_log ( state_path ( " ntprinters.tdb " ) , 0 , TDB_DEFAULT , O_RDWR | O_CREAT , 0600 ) ;
2001-07-09 22:19:04 +04:00
if ( ! tdb_printers ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 0 , ( " nt_printing_init: Failed to open nt printers database %s (%s) \n " ,
2007-11-01 22:53:44 +03:00
state_path ( " ntprinters.tdb " ) , strerror ( errno ) ) ) ;
2001-07-09 22:19:04 +04:00
return False ;
}
2009-02-09 14:40:12 +03:00
2002-11-01 02:41:00 +03:00
if ( tdb_forms )
tdb_close ( tdb_forms ) ;
2007-11-01 22:53:44 +03:00
tdb_forms = tdb_open_log ( state_path ( " ntforms.tdb " ) , 0 , TDB_DEFAULT , O_RDWR | O_CREAT , 0600 ) ;
2001-07-09 22:19:04 +04:00
if ( ! tdb_forms ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 0 , ( " nt_printing_init: Failed to open nt forms database %s (%s) \n " ,
2007-11-01 22:53:44 +03:00
state_path ( " ntforms.tdb " ) , strerror ( errno ) ) ) ;
2001-07-09 22:19:04 +04:00
return False ;
}
2009-02-09 14:40:12 +03:00
2000-05-24 10:10:21 +04:00
/* handle a Samba upgrade */
2009-02-09 14:40:12 +03:00
2005-05-27 23:02:30 +04:00
vers_id = tdb_fetch_int32 ( tdb_drivers , vstring ) ;
2005-07-18 18:18:39 +04:00
if ( vers_id = = - 1 ) {
DEBUG ( 10 , ( " Fresh database \n " ) ) ;
tdb_store_int32 ( tdb_drivers , vstring , NTDRIVERS_DATABASE_VERSION_5 ) ;
vers_id = NTDRIVERS_DATABASE_VERSION_5 ;
}
2005-05-27 23:02:30 +04:00
2005-07-15 18:26:11 +04:00
if ( vers_id ! = NTDRIVERS_DATABASE_VERSION_5 ) {
2005-05-27 23:02:30 +04:00
2009-02-09 14:40:12 +03:00
if ( ( vers_id = = NTDRIVERS_DATABASE_VERSION_1 ) | | ( IREV ( vers_id ) = = NTDRIVERS_DATABASE_VERSION_1 ) ) {
2005-07-15 18:26:11 +04:00
if ( ! upgrade_to_version_3 ( ) )
return False ;
tdb_store_int32 ( tdb_drivers , vstring , NTDRIVERS_DATABASE_VERSION_3 ) ;
vers_id = NTDRIVERS_DATABASE_VERSION_3 ;
2009-02-09 14:40:12 +03:00
}
2002-01-09 08:24:07 +03:00
if ( ( vers_id = = NTDRIVERS_DATABASE_VERSION_2 ) | | ( IREV ( vers_id ) = = NTDRIVERS_DATABASE_VERSION_2 ) ) {
2002-01-09 07:13:30 +03:00
/* Written on a bigendian machine with old fetch_int code. Save as le. */
2002-01-09 08:24:07 +03:00
/* The only upgrade between V2 and V3 is to save the version in little-endian. */
2005-05-27 23:02:30 +04:00
tdb_store_int32 ( tdb_drivers , vstring , NTDRIVERS_DATABASE_VERSION_3 ) ;
vers_id = NTDRIVERS_DATABASE_VERSION_3 ;
2002-01-09 07:13:30 +03:00
}
2005-07-15 18:26:11 +04:00
if ( vers_id = = NTDRIVERS_DATABASE_VERSION_3 ) {
if ( ! upgrade_to_version_4 ( ) )
return False ;
tdb_store_int32 ( tdb_drivers , vstring , NTDRIVERS_DATABASE_VERSION_4 ) ;
vers_id = NTDRIVERS_DATABASE_VERSION_4 ;
2002-01-09 07:13:30 +03:00
}
2005-05-27 23:02:30 +04:00
2005-07-15 18:26:11 +04:00
if ( vers_id = = NTDRIVERS_DATABASE_VERSION_4 ) {
if ( ! upgrade_to_version_5 ( ) )
return False ;
tdb_store_int32 ( tdb_drivers , vstring , NTDRIVERS_DATABASE_VERSION_5 ) ;
vers_id = NTDRIVERS_DATABASE_VERSION_5 ;
}
2005-05-27 23:02:30 +04:00
2005-07-15 18:26:11 +04:00
if ( vers_id ! = NTDRIVERS_DATABASE_VERSION_5 ) {
DEBUG ( 0 , ( " nt_printing_init: Unknown printer database version [%d] \n " , vers_id ) ) ;
2005-05-27 23:02:30 +04:00
return False ;
2005-07-15 18:26:11 +04:00
}
2000-05-24 10:10:21 +04:00
}
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
update_c_setprinter ( True ) ;
2002-07-15 14:35:28 +04:00
/*
* register callback to handle updating printers as new
* drivers are installed
*/
2002-08-17 20:31:24 +04:00
2007-05-17 00:56:39 +04:00
messaging_register ( msg_ctx , NULL , MSG_PRINTER_DRVUPGRADE ,
do_drv_upgrade_printer ) ;
2002-08-17 20:31:24 +04:00
/*
* register callback to handle updating printer data
* when a driver is initialized
*/
2007-05-17 00:56:39 +04:00
messaging_register ( msg_ctx , NULL , MSG_PRINTERDATA_INIT_RESET ,
reset_all_printerdata ) ;
2002-08-17 20:31:24 +04:00
2004-08-18 17:55:58 +04:00
/* of course, none of the message callbacks matter if you don't
2009-02-09 14:40:12 +03:00
tell messages . c that you interested in receiving PRINT_GENERAL
s3: Fix a long-standing problem with recycled PIDs
When a samba server process dies hard, it has no chance to clean up its entries
in locking.tdb, brlock.tdb, connections.tdb and sessionid.tdb.
For locking.tdb and brlock.tdb Samba is robust by checking every time we read
an entry from the database if the corresponding process still exists. If it
does not exist anymore, the entry is deleted. This is not 100% failsafe though:
On systems with a limited PID space there is a non-zero chance that between the
smbd's death and the fresh access, the PID is recycled by another long-running
process. This renders all files that had been locked by the killed smbd
potentially unusable until the new process also dies.
This patch is supposed to fix the problem the following way: Every process ID
in every database is augmented by a random 64-bit number that is stored in a
serverid.tdb. Whenever we need to check if a process still exists we know its
PID and the 64-bit number. We look up the PID in serverid.tdb and compare the
64-bit number. If it's the same, the process still is a valid smbd holding the
lock. If it is different, a new smbd has taken over.
I believe this is safe against an smbd that has died hard and the PID has been
taken over by a non-samba process. This process would not have registered
itself with a fresh 64-bit number in serverid.tdb, so the old one still exists
in serverid.tdb. We protect against this case by the parent smbd taking care of
deregistering PIDs from serverid.tdb and the fact that serverid.tdb is
CLEAR_IF_FIRST.
CLEAR_IF_FIRST does not work in a cluster, so the automatic cleanup does not
work when all smbds are restarted. For this, "net serverid wipe" has to be run
before smbd starts up. As a convenience, "net serverid wipedbs" also cleans up
sessionid.tdb and connections.tdb.
While there, this also cleans up overloading connections.tdb with all the
process entries just for messaging_send_all().
Volker
2010-03-02 19:02:01 +03:00
msgs . This is done in serverid_register ( ) */
2004-08-18 17:55:58 +04:00
2002-08-17 20:31:24 +04:00
2004-10-13 23:40:22 +04:00
if ( lp_security ( ) = = SEC_ADS ) {
win_rc = check_published_printers ( ) ;
if ( ! W_ERROR_IS_OK ( win_rc ) )
2008-11-01 19:19:26 +03:00
DEBUG ( 0 , ( " nt_printing_init: error checking published printers: %s \n " , win_errstr ( win_rc ) ) ) ;
2004-10-13 23:40:22 +04:00
}
2004-09-23 23:24:02 +04:00
2000-05-24 10:10:21 +04:00
return True ;
}
2000-02-07 19:17:59 +03:00
2004-05-03 20:38:37 +04:00
/*******************************************************************
Function to allow filename parsing " the old way " .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-06-10 21:37:57 +04:00
static NTSTATUS driver_unix_convert ( connection_struct * conn ,
const char * old_name ,
struct smb_filename * * smb_fname )
2004-05-03 20:38:37 +04:00
{
s3: Change unix_convert (and its callers) to use struct smb_filename
This is the first of a series of patches that change path based
operations to operate on a struct smb_filename instead of a char *.
This same concept already exists in source4.
My goals for this series of patches are to eventually:
1) Solve the stream vs. posix filename that contains a colon ambiguity
that currently exists.
2) Make unix_convert the only function that parses the stream name.
3) Clean up the unix_convert API.
4) Change all path based vfs operation to take a struct smb_filename.
5) Make is_ntfs_stream_name() a constant operation that can simply
check the state of struct smb_filename rather than re-parse the
filename.
6) Eliminate the need for split_ntfs_stream_name() to exist.
My strategy is to start from the inside at unix_convert() and work my
way out through the vfs layer, call by call. This first patch does
just that, by changing unix_convert and all of its callers to operate
on struct smb_filename. Since this is such a large change, I plan on
pushing the patches in phases, where each phase keeps full
compatibility and passes make test.
The API of unix_convert has been simplified from:
NTSTATUS unix_convert(TALLOC_CTX *ctx,
connection_struct *conn,
const char *orig_path,
bool allow_wcard_last_component,
char **pp_conv_path,
char **pp_saved_last_component,
SMB_STRUCT_STAT *pst)
to:
NTSTATUS unix_convert(TALLOC_CTX *ctx,
connection_struct *conn,
const char *orig_path,
struct smb_filename *smb_fname,
uint32_t ucf_flags)
Currently the smb_filename struct looks like:
struct smb_filename {
char *base_name;
char *stream_name;
char *original_lcomp;
SMB_STRUCT_STAT st;
};
One key point here is the decision to break up the base_name and
stream_name. I have introduced a helper function called
get_full_smb_filename() that takes an smb_filename struct and
allocates the full_name. I changed the callers of unix_convert() to
subsequently call get_full_smb_filename() for the time being, but I
plan to eventually eliminate get_full_smb_filename().
2009-04-08 00:39:57 +04:00
NTSTATUS status ;
2007-11-16 01:19:52 +03:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
char * name = talloc_strdup ( ctx , old_name ) ;
if ( ! name ) {
2009-06-10 21:37:57 +04:00
return NT_STATUS_NO_MEMORY ;
2007-11-16 01:19:52 +03:00
}
2004-05-03 20:38:37 +04:00
unix_format ( name ) ;
2007-11-16 01:19:52 +03:00
name = unix_clean_name ( ctx , name ) ;
if ( ! name ) {
2009-06-10 21:37:57 +04:00
return NT_STATUS_NO_MEMORY ;
2007-09-08 00:57:01 +04:00
}
2007-11-16 01:19:52 +03:00
trim_string ( name , " / " , " / " ) ;
s3: Change unix_convert (and its callers) to use struct smb_filename
This is the first of a series of patches that change path based
operations to operate on a struct smb_filename instead of a char *.
This same concept already exists in source4.
My goals for this series of patches are to eventually:
1) Solve the stream vs. posix filename that contains a colon ambiguity
that currently exists.
2) Make unix_convert the only function that parses the stream name.
3) Clean up the unix_convert API.
4) Change all path based vfs operation to take a struct smb_filename.
5) Make is_ntfs_stream_name() a constant operation that can simply
check the state of struct smb_filename rather than re-parse the
filename.
6) Eliminate the need for split_ntfs_stream_name() to exist.
My strategy is to start from the inside at unix_convert() and work my
way out through the vfs layer, call by call. This first patch does
just that, by changing unix_convert and all of its callers to operate
on struct smb_filename. Since this is such a large change, I plan on
pushing the patches in phases, where each phase keeps full
compatibility and passes make test.
The API of unix_convert has been simplified from:
NTSTATUS unix_convert(TALLOC_CTX *ctx,
connection_struct *conn,
const char *orig_path,
bool allow_wcard_last_component,
char **pp_conv_path,
char **pp_saved_last_component,
SMB_STRUCT_STAT *pst)
to:
NTSTATUS unix_convert(TALLOC_CTX *ctx,
connection_struct *conn,
const char *orig_path,
struct smb_filename *smb_fname,
uint32_t ucf_flags)
Currently the smb_filename struct looks like:
struct smb_filename {
char *base_name;
char *stream_name;
char *original_lcomp;
SMB_STRUCT_STAT st;
};
One key point here is the decision to break up the base_name and
stream_name. I have introduced a helper function called
get_full_smb_filename() that takes an smb_filename struct and
allocates the full_name. I changed the callers of unix_convert() to
subsequently call get_full_smb_filename() for the time being, but I
plan to eventually eliminate get_full_smb_filename().
2009-04-08 00:39:57 +04:00
2009-06-10 21:37:57 +04:00
status = unix_convert ( ctx , conn , name , smb_fname , 0 ) ;
s3: Change unix_convert (and its callers) to use struct smb_filename
This is the first of a series of patches that change path based
operations to operate on a struct smb_filename instead of a char *.
This same concept already exists in source4.
My goals for this series of patches are to eventually:
1) Solve the stream vs. posix filename that contains a colon ambiguity
that currently exists.
2) Make unix_convert the only function that parses the stream name.
3) Clean up the unix_convert API.
4) Change all path based vfs operation to take a struct smb_filename.
5) Make is_ntfs_stream_name() a constant operation that can simply
check the state of struct smb_filename rather than re-parse the
filename.
6) Eliminate the need for split_ntfs_stream_name() to exist.
My strategy is to start from the inside at unix_convert() and work my
way out through the vfs layer, call by call. This first patch does
just that, by changing unix_convert and all of its callers to operate
on struct smb_filename. Since this is such a large change, I plan on
pushing the patches in phases, where each phase keeps full
compatibility and passes make test.
The API of unix_convert has been simplified from:
NTSTATUS unix_convert(TALLOC_CTX *ctx,
connection_struct *conn,
const char *orig_path,
bool allow_wcard_last_component,
char **pp_conv_path,
char **pp_saved_last_component,
SMB_STRUCT_STAT *pst)
to:
NTSTATUS unix_convert(TALLOC_CTX *ctx,
connection_struct *conn,
const char *orig_path,
struct smb_filename *smb_fname,
uint32_t ucf_flags)
Currently the smb_filename struct looks like:
struct smb_filename {
char *base_name;
char *stream_name;
char *original_lcomp;
SMB_STRUCT_STAT st;
};
One key point here is the decision to break up the base_name and
stream_name. I have introduced a helper function called
get_full_smb_filename() that takes an smb_filename struct and
allocates the full_name. I changed the callers of unix_convert() to
subsequently call get_full_smb_filename() for the time being, but I
plan to eventually eliminate get_full_smb_filename().
2009-04-08 00:39:57 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-06-10 21:37:57 +04:00
return NT_STATUS_NO_MEMORY ;
s3: Change unix_convert (and its callers) to use struct smb_filename
This is the first of a series of patches that change path based
operations to operate on a struct smb_filename instead of a char *.
This same concept already exists in source4.
My goals for this series of patches are to eventually:
1) Solve the stream vs. posix filename that contains a colon ambiguity
that currently exists.
2) Make unix_convert the only function that parses the stream name.
3) Clean up the unix_convert API.
4) Change all path based vfs operation to take a struct smb_filename.
5) Make is_ntfs_stream_name() a constant operation that can simply
check the state of struct smb_filename rather than re-parse the
filename.
6) Eliminate the need for split_ntfs_stream_name() to exist.
My strategy is to start from the inside at unix_convert() and work my
way out through the vfs layer, call by call. This first patch does
just that, by changing unix_convert and all of its callers to operate
on struct smb_filename. Since this is such a large change, I plan on
pushing the patches in phases, where each phase keeps full
compatibility and passes make test.
The API of unix_convert has been simplified from:
NTSTATUS unix_convert(TALLOC_CTX *ctx,
connection_struct *conn,
const char *orig_path,
bool allow_wcard_last_component,
char **pp_conv_path,
char **pp_saved_last_component,
SMB_STRUCT_STAT *pst)
to:
NTSTATUS unix_convert(TALLOC_CTX *ctx,
connection_struct *conn,
const char *orig_path,
struct smb_filename *smb_fname,
uint32_t ucf_flags)
Currently the smb_filename struct looks like:
struct smb_filename {
char *base_name;
char *stream_name;
char *original_lcomp;
SMB_STRUCT_STAT st;
};
One key point here is the decision to break up the base_name and
stream_name. I have introduced a helper function called
get_full_smb_filename() that takes an smb_filename struct and
allocates the full_name. I changed the callers of unix_convert() to
subsequently call get_full_smb_filename() for the time being, but I
plan to eventually eliminate get_full_smb_filename().
2009-04-08 00:39:57 +04:00
}
2009-06-10 21:37:57 +04:00
return NT_STATUS_OK ;
2004-05-03 20:38:37 +04:00
}
2002-02-09 07:10:24 +03:00
/*******************************************************************
tdb traversal function for counting printers .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int traverse_counting_printers ( TDB_CONTEXT * t , TDB_DATA key ,
TDB_DATA data , void * context )
{
int * printer_count = ( int * ) context ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
if ( memcmp ( PRINTERS_PREFIX , key . dptr , sizeof ( PRINTERS_PREFIX ) - 1 ) = = 0 ) {
( * printer_count ) + + ;
DEBUG ( 10 , ( " traverse_counting_printers: printer = [%s] printer_count = %d \n " , key . dptr , * printer_count ) ) ;
}
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
return 0 ;
}
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
/*******************************************************************
Update the spooler global c_setprinter . This variable is initialized
when the parent smbd starts with the number of existing printers . It
is monotonically increased by the current number of printers * after *
each add or delete printer RPC . Only Microsoft knows why . . . JRR020119
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
uint32 update_c_setprinter ( bool initialize )
2002-02-09 07:10:24 +03:00
{
int32 c_setprinter ;
int32 printer_count = 0 ;
2009-02-09 14:40:12 +03:00
2006-04-17 15:49:06 +04:00
tdb_lock_bystring ( tdb_printers , GLOBAL_C_SETPRINTER ) ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
/* Traverse the tdb, counting the printers */
tdb_traverse ( tdb_printers , traverse_counting_printers , ( void * ) & printer_count ) ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
/* If initializing, set c_setprinter to current printers count
* otherwise , bump it by the current printer count
*/
if ( ! initialize )
c_setprinter = tdb_fetch_int32 ( tdb_printers , GLOBAL_C_SETPRINTER ) + printer_count ;
else
c_setprinter = printer_count ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
DEBUG ( 10 , ( " update_c_setprinter: c_setprinter = %u \n " , ( unsigned int ) c_setprinter ) ) ;
tdb_store_int32 ( tdb_printers , GLOBAL_C_SETPRINTER , c_setprinter ) ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
tdb_unlock_bystring ( tdb_printers , GLOBAL_C_SETPRINTER ) ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
return ( uint32 ) c_setprinter ;
}
/*******************************************************************
Get the spooler global c_setprinter , accounting for initialization .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
uint32 get_c_setprinter ( void )
{
int32 c_setprinter = tdb_fetch_int32 ( tdb_printers , GLOBAL_C_SETPRINTER ) ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
if ( c_setprinter = = ( int32 ) - 1 )
c_setprinter = update_c_setprinter ( True ) ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
DEBUG ( 10 , ( " get_c_setprinter: c_setprinter = %d \n " , c_setprinter ) ) ;
2009-02-09 14:40:12 +03:00
2002-02-09 07:10:24 +03:00
return ( uint32 ) c_setprinter ;
}
2001-04-04 02:41:31 +04:00
/****************************************************************************
2002-02-09 07:10:24 +03:00
Get builtin form struct list .
2001-04-04 02:41:31 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-02-09 07:10:24 +03:00
2001-04-04 02:41:31 +04:00
int get_builtin_ntforms ( nt_forms_struct * * list )
{
* list = ( nt_forms_struct * ) memdup ( & default_forms [ 0 ] , sizeof ( default_forms ) ) ;
2006-06-28 04:50:14 +04:00
if ( ! * list ) {
return 0 ;
}
2009-04-18 02:58:12 +04:00
return ARRAY_SIZE ( default_forms ) ;
2001-04-04 02:41:31 +04:00
}
/****************************************************************************
get a builtin form struct
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-11-16 00:17:23 +03:00
bool get_a_builtin_ntform_by_string ( const char * form_name , nt_forms_struct * form )
2001-04-04 02:41:31 +04:00
{
2009-04-18 02:58:12 +04:00
int i ;
2001-04-04 02:41:31 +04:00
DEBUGADD ( 6 , ( " Looking for builtin form %s \n " , form_name ) ) ;
2009-04-18 02:58:12 +04:00
for ( i = 0 ; i < ARRAY_SIZE ( default_forms ) ; i + + ) {
2001-04-04 02:41:31 +04:00
if ( strequal ( form_name , default_forms [ i ] . name ) ) {
DEBUGADD ( 6 , ( " Found builtin form %s \n " , form_name ) ) ;
memcpy ( form , & default_forms [ i ] , sizeof ( * form ) ) ;
2009-04-18 02:58:12 +04:00
return true ;
2001-04-04 02:41:31 +04:00
}
}
2009-04-18 02:58:12 +04:00
return false ;
2001-04-04 02:41:31 +04:00
}
2001-03-03 08:27:26 +03:00
2000-02-07 19:17:59 +03:00
/****************************************************************************
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
get a form struct list .
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
2001-03-03 08:27:26 +03:00
int get_ntforms ( nt_forms_struct * * list )
2000-02-07 19:17:59 +03:00
{
2000-05-27 07:37:05 +04:00
TDB_DATA kbuf , newkey , dbuf ;
2000-05-24 10:10:21 +04:00
nt_forms_struct form ;
int ret ;
2000-09-29 01:33:08 +04:00
int i ;
2000-05-24 10:10:21 +04:00
int n = 0 ;
2006-03-17 00:05:15 +03:00
* list = NULL ;
2001-07-09 22:19:04 +04:00
for ( kbuf = tdb_firstkey ( tdb_forms ) ;
2000-11-15 00:56:32 +03:00
kbuf . dptr ;
2009-02-09 14:40:12 +03:00
newkey = tdb_nextkey ( tdb_forms , kbuf ) , free ( kbuf . dptr ) , kbuf = newkey )
2002-08-17 20:31:24 +04:00
{
2009-02-09 14:40:12 +03:00
if ( strncmp ( ( const char * ) kbuf . dptr , FORMS_PREFIX , strlen ( FORMS_PREFIX ) ) ! = 0 )
2002-08-17 20:31:24 +04:00
continue ;
2009-02-09 14:40:12 +03:00
2001-07-09 22:19:04 +04:00
dbuf = tdb_fetch ( tdb_forms , kbuf ) ;
2009-02-09 14:40:12 +03:00
if ( ! dbuf . dptr )
2002-08-17 20:31:24 +04:00
continue ;
2000-05-24 10:10:21 +04:00
2007-03-29 13:35:51 +04:00
fstrcpy ( form . name , ( const char * ) kbuf . dptr + strlen ( FORMS_PREFIX ) ) ;
2000-09-29 01:33:08 +04:00
ret = tdb_unpack ( dbuf . dptr , dbuf . dsize , " dddddddd " ,
& i , & form . flag , & form . width , & form . length , & form . left ,
2000-05-24 10:10:21 +04:00
& form . top , & form . right , & form . bottom ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( dbuf . dptr ) ;
2009-02-09 14:40:12 +03:00
if ( ret ! = dbuf . dsize )
2002-08-17 20:31:24 +04:00
continue ;
2000-05-24 10:10:21 +04:00
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
* list = SMB_REALLOC_ARRAY ( * list , nt_forms_struct , n + 1 ) ;
if ( ! * list ) {
2001-04-04 02:41:31 +04:00
DEBUG ( 0 , ( " get_ntforms: Realloc fail. \n " ) ) ;
return 0 ;
2000-09-29 01:33:08 +04:00
}
2001-04-04 02:41:31 +04:00
( * list ) [ n ] = form ;
n + + ;
2000-06-06 05:47:11 +04:00
}
2009-02-09 14:40:12 +03:00
2000-06-06 05:47:11 +04:00
2000-05-24 10:10:21 +04:00
return n ;
2000-02-07 19:17:59 +03:00
}
/****************************************************************************
write a form struct list
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-22 00:56:36 +03:00
2000-02-07 19:17:59 +03:00
int write_ntforms ( nt_forms_struct * * list , int number )
{
2007-11-22 00:56:36 +03:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
char * buf = NULL ;
char * key = NULL ;
2000-05-24 10:10:21 +04:00
int len ;
2007-03-27 15:22:07 +04:00
TDB_DATA dbuf ;
2000-05-24 10:10:21 +04:00
int i ;
2000-02-07 19:17:59 +03:00
2000-05-24 10:10:21 +04:00
for ( i = 0 ; i < number ; i + + ) {
2000-09-29 01:33:08 +04:00
/* save index, so list is rebuilt in correct order */
2007-11-22 00:56:36 +03:00
len = tdb_pack ( NULL , 0 , " dddddddd " ,
2000-09-29 01:33:08 +04:00
i , ( * list ) [ i ] . flag , ( * list ) [ i ] . width , ( * list ) [ i ] . length ,
2000-11-15 00:56:32 +03:00
( * list ) [ i ] . left , ( * list ) [ i ] . top , ( * list ) [ i ] . right ,
2000-05-24 10:10:21 +04:00
( * list ) [ i ] . bottom ) ;
2007-11-22 00:56:36 +03:00
if ( ! len ) {
continue ;
}
buf = TALLOC_ARRAY ( ctx , char , len ) ;
if ( ! buf ) {
return 0 ;
}
len = tdb_pack ( ( uint8 * ) buf , len , " dddddddd " ,
i , ( * list ) [ i ] . flag , ( * list ) [ i ] . width , ( * list ) [ i ] . length ,
( * list ) [ i ] . left , ( * list ) [ i ] . top , ( * list ) [ i ] . right ,
( * list ) [ i ] . bottom ) ;
key = talloc_asprintf ( ctx , " %s%s " , FORMS_PREFIX , ( * list ) [ i ] . name ) ;
if ( ! key ) {
return 0 ;
}
2000-05-24 10:10:21 +04:00
dbuf . dsize = len ;
2007-03-29 13:35:51 +04:00
dbuf . dptr = ( uint8 * ) buf ;
2007-11-22 00:56:36 +03:00
if ( tdb_store_bystring ( tdb_forms , key , dbuf , TDB_REPLACE ) ! = 0 ) {
TALLOC_FREE ( key ) ;
TALLOC_FREE ( buf ) ;
break ;
}
TALLOC_FREE ( key ) ;
TALLOC_FREE ( buf ) ;
2001-03-03 08:27:26 +03:00
}
2000-02-07 19:17:59 +03:00
2001-03-03 08:27:26 +03:00
return i ;
2000-02-07 19:17:59 +03:00
}
/****************************************************************************
add a form struct at the end of the list
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-08 02:04:27 +03:00
bool add_a_form ( nt_forms_struct * * list , struct spoolss_AddFormInfo1 * form , int * count )
2000-02-07 19:17:59 +03:00
{
int n = 0 ;
2007-10-19 04:40:25 +04:00
bool update ;
2000-02-07 19:17:59 +03:00
2000-11-15 00:56:32 +03:00
/*
* NT tries to add forms even when
2000-02-07 19:17:59 +03:00
* they are already in the base
* only update the values if already present
*/
update = False ;
2009-02-08 02:04:27 +03:00
2000-11-17 03:31:29 +03:00
for ( n = 0 ; n < * count ; n + + ) {
2009-02-08 02:04:27 +03:00
if ( strequal ( ( * list ) [ n ] . name , form - > form_name ) ) {
2000-02-07 19:17:59 +03:00
update = True ;
2000-11-17 03:31:29 +03:00
break ;
2000-02-07 19:17:59 +03:00
}
}
2000-11-17 03:31:29 +03:00
if ( update = = False ) {
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ( * list = SMB_REALLOC_ARRAY ( * list , nt_forms_struct , n + 1 ) ) = = NULL ) {
2001-08-12 21:30:01 +04:00
DEBUG ( 0 , ( " add_a_form: failed to enlarge forms list! \n " ) ) ;
2000-04-07 02:48:53 +04:00
return False ;
2001-08-12 21:30:01 +04:00
}
2009-02-08 02:04:27 +03:00
fstrcpy ( ( * list ) [ n ] . name , form - > form_name ) ;
2000-02-07 19:17:59 +03:00
( * count ) + + ;
}
2009-02-08 02:04:27 +03:00
( * list ) [ n ] . flag = form - > flags ;
( * list ) [ n ] . width = form - > size . width ;
( * list ) [ n ] . length = form - > size . height ;
( * list ) [ n ] . left = form - > area . left ;
( * list ) [ n ] . top = form - > area . top ;
( * list ) [ n ] . right = form - > area . right ;
( * list ) [ n ] . bottom = form - > area . bottom ;
2000-04-07 02:48:53 +04:00
2009-02-09 14:40:12 +03:00
DEBUG ( 6 , ( " add_a_form: Successfully %s form [%s] \n " ,
2009-02-08 02:04:27 +03:00
update ? " updated " : " added " , form - > form_name ) ) ;
2004-12-07 22:00:45 +03:00
2000-04-07 02:48:53 +04:00
return True ;
2000-02-07 19:17:59 +03:00
}
2000-08-31 23:04:51 +04:00
/****************************************************************************
2003-02-25 23:53:53 +03:00
Delete a named form struct .
2000-08-31 23:04:51 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-02-25 23:53:53 +03:00
2009-02-08 21:24:39 +03:00
bool delete_a_form ( nt_forms_struct * * list , const char * del_name , int * count , WERROR * ret )
2000-08-31 23:04:51 +04:00
{
2007-11-22 00:56:36 +03:00
char * key = NULL ;
2000-08-31 23:04:51 +04:00
int n = 0 ;
2001-09-04 11:13:01 +04:00
* ret = WERR_OK ;
2000-08-31 23:04:51 +04:00
for ( n = 0 ; n < * count ; n + + ) {
2008-11-16 00:17:23 +03:00
if ( ! strncmp ( ( * list ) [ n ] . name , del_name , strlen ( del_name ) ) ) {
DEBUG ( 103 , ( " delete_a_form, [%s] in list \n " , del_name ) ) ;
2000-08-31 23:04:51 +04:00
break ;
}
}
if ( n = = * count ) {
2008-11-16 00:17:23 +03:00
DEBUG ( 10 , ( " delete_a_form, [%s] not found \n " , del_name ) ) ;
2010-04-08 01:36:48 +04:00
* ret = WERR_INVALID_FORM_NAME ;
2000-08-31 23:04:51 +04:00
return False ;
}
2007-11-22 00:56:36 +03:00
if ( asprintf ( & key , " %s%s " , FORMS_PREFIX , ( * list ) [ n ] . name ) < 0 ) {
* ret = WERR_NOMEM ;
return false ;
}
2007-03-27 15:22:07 +04:00
if ( tdb_delete_bystring ( tdb_forms , key ) ! = 0 ) {
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
2001-09-04 11:13:01 +04:00
* ret = WERR_NOMEM ;
2000-08-31 23:04:51 +04:00
return False ;
}
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
return true ;
2000-08-31 23:04:51 +04:00
}
2000-02-07 19:17:59 +03:00
/****************************************************************************
2003-02-25 23:53:53 +03:00
Update a form struct .
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-02-25 23:53:53 +03:00
2009-02-08 21:19:57 +03:00
void update_a_form ( nt_forms_struct * * list , struct spoolss_AddFormInfo1 * form , int count )
2000-02-07 19:17:59 +03:00
{
int n = 0 ;
2009-02-08 21:19:57 +03:00
DEBUG ( 106 , ( " [%s] \n " , form - > form_name ) ) ;
2003-02-25 23:53:53 +03:00
for ( n = 0 ; n < count ; n + + ) {
2000-02-15 21:07:45 +03:00
DEBUGADD ( 106 , ( " n [%d]:[%s] \n " , n , ( * list ) [ n ] . name ) ) ;
2009-02-08 21:19:57 +03:00
if ( ! strncmp ( ( * list ) [ n ] . name , form - > form_name , strlen ( form - > form_name ) ) )
2000-02-07 19:17:59 +03:00
break ;
}
if ( n = = count ) return ;
2009-02-08 21:19:57 +03:00
( * list ) [ n ] . flag = form - > flags ;
( * list ) [ n ] . width = form - > size . width ;
( * list ) [ n ] . length = form - > size . height ;
( * list ) [ n ] . left = form - > area . left ;
( * list ) [ n ] . top = form - > area . top ;
( * list ) [ n ] . right = form - > area . right ;
( * list ) [ n ] . bottom = form - > area . bottom ;
2000-02-07 19:17:59 +03:00
}
2000-11-15 00:56:32 +03:00
2000-02-07 19:17:59 +03:00
/****************************************************************************
2003-02-25 23:53:53 +03:00
Get the nt drivers list .
Traverse the database and look - up the matching names .
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
int get_ntdrivers ( fstring * * list , const char * architecture , uint32 version )
2000-02-07 19:17:59 +03:00
{
int total = 0 ;
2003-04-23 17:27:35 +04:00
const char * short_archi ;
2007-11-22 00:56:36 +03:00
char * key = NULL ;
2000-05-27 07:37:05 +04:00
TDB_DATA kbuf , newkey ;
2000-02-07 19:17:59 +03:00
2003-04-23 17:27:35 +04:00
short_archi = get_short_archi ( architecture ) ;
2006-06-20 01:36:19 +04:00
if ( ! short_archi ) {
return 0 ;
}
2007-11-22 00:56:36 +03:00
if ( asprintf ( & key , " %s%s/%d/ " , DRIVERS_PREFIX ,
short_archi , version ) < 0 ) {
return 0 ;
}
2000-04-07 02:48:53 +04:00
2001-07-09 22:19:04 +04:00
for ( kbuf = tdb_firstkey ( tdb_drivers ) ;
2000-11-15 00:56:32 +03:00
kbuf . dptr ;
2008-10-19 15:06:14 +04:00
newkey = tdb_nextkey ( tdb_drivers , kbuf ) , free ( kbuf . dptr ) , kbuf = newkey ) {
2003-02-25 23:53:53 +03:00
2007-03-29 13:35:51 +04:00
if ( strncmp ( ( const char * ) kbuf . dptr , key , strlen ( key ) ) ! = 0 )
2003-02-25 23:53:53 +03:00
continue ;
2007-11-22 00:56:36 +03:00
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ( * list = SMB_REALLOC_ARRAY ( * list , fstring , total + 1 ) ) = = NULL ) {
2001-08-12 21:30:01 +04:00
DEBUG ( 0 , ( " get_ntdrivers: failed to enlarge list! \n " ) ) ;
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
2000-05-24 10:10:21 +04:00
return - 1 ;
2001-08-12 21:30:01 +04:00
}
2000-04-07 02:48:53 +04:00
2007-03-29 13:35:51 +04:00
fstrcpy ( ( * list ) [ total ] , ( const char * ) kbuf . dptr + strlen ( key ) ) ;
2000-05-24 10:10:21 +04:00
total + + ;
2000-02-07 19:17:59 +03:00
}
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
2000-02-07 19:17:59 +03:00
return ( total ) ;
}
/****************************************************************************
2006-06-20 01:36:19 +04:00
Function to do the mapping between the long architecture name and
the short one .
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-06-20 01:36:19 +04:00
2003-04-23 17:27:35 +04:00
const char * get_short_archi ( const char * long_archi )
2000-02-07 19:17:59 +03:00
{
2003-04-23 17:27:35 +04:00
int i = - 1 ;
2000-02-07 19:17:59 +03:00
2003-04-23 17:27:35 +04:00
DEBUG ( 107 , ( " Getting architecture dependant directory \n " ) ) ;
do {
i + + ;
} while ( ( archi_table [ i ] . long_archi ! = NULL ) & &
StrCaseCmp ( long_archi , archi_table [ i ] . long_archi ) ) ;
2002-10-31 21:42:38 +03:00
2003-04-23 17:27:35 +04:00
if ( archi_table [ i ] . long_archi = = NULL ) {
DEBUGADD ( 10 , ( " Unknown architecture [%s] ! \n " , long_archi ) ) ;
return NULL ;
}
2002-10-31 21:42:38 +03:00
2003-04-23 17:27:35 +04:00
/* this might be client code - but shouldn't this be an fstrcpy etc? */
2000-02-07 19:17:59 +03:00
2003-04-23 17:27:35 +04:00
DEBUGADD ( 108 , ( " index: [%d] \n " , i ) ) ;
DEBUGADD ( 108 , ( " long architecture: [%s] \n " , archi_table [ i ] . long_archi ) ) ;
DEBUGADD ( 108 , ( " short architecture: [%s] \n " , archi_table [ i ] . short_archi ) ) ;
2000-02-07 19:17:59 +03:00
2003-04-23 17:27:35 +04:00
return archi_table [ i ] . short_archi ;
2000-07-25 17:15:16 +04:00
}
/****************************************************************************
2002-01-08 04:22:23 +03:00
Version information in Microsoft files is held in a VS_VERSION_INFO structure .
There are two case to be covered here : PE ( Portable Executable ) and NE ( New
Executable ) files . Both files support the same INFO structure , but PE files
store the signature in unicode , and NE files store it as ! unicode .
returns - 1 on error , 1 on version info found , and 0 on no version info found .
2000-07-25 17:15:16 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-08 04:22:23 +03:00
static int get_file_version ( files_struct * fsp , char * fname , uint32 * major , uint32 * minor )
2000-07-25 17:15:16 +04:00
{
2001-08-10 23:38:53 +04:00
int i ;
2003-10-30 00:28:00 +03:00
char * buf = NULL ;
2001-08-10 23:38:53 +04:00
ssize_t byte_count ;
2000-11-28 02:59:42 +03:00
2007-03-19 20:45:13 +03:00
if ( ( buf = ( char * ) SMB_MALLOC ( DOS_HEADER_SIZE ) ) = = NULL ) {
DEBUG ( 0 , ( " get_file_version: PE file [%s] DOS Header malloc failed bytes = %d \n " ,
fname , DOS_HEADER_SIZE ) ) ;
2000-10-25 21:56:38 +04:00
goto error_exit ;
}
2000-11-15 00:56:32 +03:00
if ( ( byte_count = vfs_read_data ( fsp , buf , DOS_HEADER_SIZE ) ) < DOS_HEADER_SIZE ) {
2003-11-03 17:34:25 +03:00
DEBUG ( 3 , ( " get_file_version: File [%s] DOS header too short, bytes read = %lu \n " ,
fname , ( unsigned long ) byte_count ) ) ;
2001-08-10 23:38:53 +04:00
goto no_version_info ;
2000-10-25 21:56:38 +04:00
}
/* Is this really a DOS header? */
if ( SVAL ( buf , DOS_HEADER_MAGIC_OFFSET ) ! = DOS_HEADER_MAGIC ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 6 , ( " get_file_version: File [%s] bad DOS magic = 0x%x \n " ,
fname , SVAL ( buf , DOS_HEADER_MAGIC_OFFSET ) ) ) ;
goto no_version_info ;
2000-10-25 21:56:38 +04:00
}
/* Skip OEM header (if any) and the DOS stub to start of Windows header */
2008-01-07 12:15:08 +03:00
if ( SMB_VFS_LSEEK ( fsp , SVAL ( buf , DOS_HEADER_LFANEW_OFFSET ) , SEEK_SET ) = = ( SMB_OFF_T ) - 1 ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 3 , ( " get_file_version: File [%s] too short, errno = %d \n " ,
fname , errno ) ) ;
/* Assume this isn't an error... the file just looks sort of like a PE/NE file */
goto no_version_info ;
2000-10-25 21:56:38 +04:00
}
2007-03-19 20:45:13 +03:00
/* Note: DOS_HEADER_SIZE and NE_HEADER_SIZE are incidentally same */
if ( ( byte_count = vfs_read_data ( fsp , buf , NE_HEADER_SIZE ) ) < NE_HEADER_SIZE ) {
2003-11-03 17:34:25 +03:00
DEBUG ( 3 , ( " get_file_version: File [%s] Windows header too short, bytes read = %lu \n " ,
fname , ( unsigned long ) byte_count ) ) ;
2001-08-10 23:38:53 +04:00
/* Assume this isn't an error... the file just looks sort of like a PE/NE file */
goto no_version_info ;
2000-10-25 21:56:38 +04:00
}
/* The header may be a PE (Portable Executable) or an NE (New Executable) */
if ( IVAL ( buf , PE_HEADER_SIGNATURE_OFFSET ) = = PE_HEADER_SIGNATURE ) {
2003-10-30 00:28:00 +03:00
unsigned int num_sections ;
unsigned int section_table_bytes ;
2007-03-19 20:45:13 +03:00
/* Just skip over optional header to get to section table */
2008-01-07 12:15:08 +03:00
if ( SMB_VFS_LSEEK ( fsp ,
2007-03-19 20:45:13 +03:00
SVAL ( buf , PE_HEADER_OPTIONAL_HEADER_SIZE ) - ( NE_HEADER_SIZE - PE_HEADER_SIZE ) ,
SEEK_CUR ) = = ( SMB_OFF_T ) - 1 ) {
DEBUG ( 3 , ( " get_file_version: File [%s] Windows optional header too short, errno = %d \n " ,
fname , errno ) ) ;
2000-10-25 21:56:38 +04:00
goto error_exit ;
}
2001-08-10 23:38:53 +04:00
/* get the section table */
num_sections = SVAL ( buf , PE_HEADER_NUMBER_OF_SECTIONS ) ;
section_table_bytes = num_sections * PE_HEADER_SECT_HEADER_SIZE ;
2003-10-30 00:28:00 +03:00
if ( section_table_bytes = = 0 )
goto error_exit ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2006-08-17 18:28:03 +04:00
if ( ( buf = ( char * ) SMB_MALLOC ( section_table_bytes ) ) = = NULL ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 0 , ( " get_file_version: PE file [%s] section table malloc failed bytes = %d \n " ,
fname , section_table_bytes ) ) ;
2000-10-25 21:56:38 +04:00
goto error_exit ;
}
2001-08-10 23:38:53 +04:00
if ( ( byte_count = vfs_read_data ( fsp , buf , section_table_bytes ) ) < section_table_bytes ) {
2003-11-03 17:34:25 +03:00
DEBUG ( 3 , ( " get_file_version: PE file [%s] Section header too short, bytes read = %lu \n " ,
fname , ( unsigned long ) byte_count ) ) ;
2001-08-10 23:38:53 +04:00
goto error_exit ;
}
2000-11-15 00:56:32 +03:00
2001-08-10 23:38:53 +04:00
/* Iterate the section table looking for the resource section ".rsrc" */
for ( i = 0 ; i < num_sections ; i + + ) {
int sec_offset = i * PE_HEADER_SECT_HEADER_SIZE ;
2000-10-25 21:56:38 +04:00
2001-08-10 23:38:53 +04:00
if ( strcmp ( " .rsrc " , & buf [ sec_offset + PE_HEADER_SECT_NAME_OFFSET ] ) = = 0 ) {
2003-10-30 00:28:00 +03:00
unsigned int section_pos = IVAL ( buf , sec_offset + PE_HEADER_SECT_PTR_DATA_OFFSET ) ;
unsigned int section_bytes = IVAL ( buf , sec_offset + PE_HEADER_SECT_SIZE_DATA_OFFSET ) ;
if ( section_bytes = = 0 )
goto error_exit ;
2000-07-25 17:15:16 +04:00
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2006-08-17 18:28:03 +04:00
if ( ( buf = ( char * ) SMB_MALLOC ( section_bytes ) ) = = NULL ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 0 , ( " get_file_version: PE file [%s] version malloc failed bytes = %d \n " ,
fname , section_bytes ) ) ;
goto error_exit ;
}
2000-07-25 17:15:16 +04:00
2001-08-10 23:38:53 +04:00
/* Seek to the start of the .rsrc section info */
2008-01-07 12:15:08 +03:00
if ( SMB_VFS_LSEEK ( fsp , section_pos , SEEK_SET ) = = ( SMB_OFF_T ) - 1 ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 3 , ( " get_file_version: PE file [%s] too short for section info, errno = %d \n " ,
fname , errno ) ) ;
goto error_exit ;
}
2000-07-25 17:15:16 +04:00
2001-08-10 23:38:53 +04:00
if ( ( byte_count = vfs_read_data ( fsp , buf , section_bytes ) ) < section_bytes ) {
2003-11-03 17:34:25 +03:00
DEBUG ( 3 , ( " get_file_version: PE file [%s] .rsrc section too short, bytes read = %lu \n " ,
fname , ( unsigned long ) byte_count ) ) ;
2001-08-10 23:38:53 +04:00
goto error_exit ;
}
2000-07-25 17:15:16 +04:00
2003-10-30 00:28:00 +03:00
if ( section_bytes < VS_VERSION_INFO_UNICODE_SIZE )
goto error_exit ;
2001-08-10 23:38:53 +04:00
for ( i = 0 ; i < section_bytes - VS_VERSION_INFO_UNICODE_SIZE ; i + + ) {
/* Scan for 1st 3 unicoded bytes followed by word aligned magic value */
if ( buf [ i ] = = ' V ' & & buf [ i + 1 ] = = ' \0 ' & & buf [ i + 2 ] = = ' S ' ) {
/* Align to next long address */
int pos = ( i + sizeof ( VS_SIGNATURE ) * 2 + 3 ) & 0xfffffffc ;
2000-07-25 17:15:16 +04:00
2001-08-10 23:38:53 +04:00
if ( IVAL ( buf , pos ) = = VS_MAGIC_VALUE ) {
* major = IVAL ( buf , pos + VS_MAJOR_OFFSET ) ;
* minor = IVAL ( buf , pos + VS_MINOR_OFFSET ) ;
2007-11-16 01:19:52 +03:00
2001-08-10 23:38:53 +04:00
DEBUG ( 6 , ( " get_file_version: PE file [%s] Version = %08x:%08x (%d.%d.%d.%d) \n " ,
fname , * major , * minor ,
( * major > > 16 ) & 0xffff , * major & 0xffff ,
( * minor > > 16 ) & 0xffff , * minor & 0xffff ) ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2002-01-08 04:22:23 +03:00
return 1 ;
2001-08-10 23:38:53 +04:00
}
}
}
2000-11-15 00:56:32 +03:00
}
}
/* Version info not found, fall back to origin date/time */
DEBUG ( 10 , ( " get_file_version: PE file [%s] has no version info \n " , fname ) ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2002-01-08 04:22:23 +03:00
return 0 ;
2000-11-15 00:56:32 +03:00
} else if ( SVAL ( buf , NE_HEADER_SIGNATURE_OFFSET ) = = NE_HEADER_SIGNATURE ) {
if ( CVAL ( buf , NE_HEADER_TARGET_OS_OFFSET ) ! = NE_HEADER_TARGOS_WIN ) {
DEBUG ( 3 , ( " get_file_version: NE file [%s] wrong target OS = 0x%x \n " ,
fname , CVAL ( buf , NE_HEADER_TARGET_OS_OFFSET ) ) ) ;
/* At this point, we assume the file is in error. It still could be somthing
* else besides a NE file , but it unlikely at this point . */
goto error_exit ;
}
/* Allocate a bit more space to speed up things */
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2006-08-17 18:28:03 +04:00
if ( ( buf = ( char * ) SMB_MALLOC ( VS_NE_BUF_SIZE ) ) = = NULL ) {
2000-11-15 00:56:32 +03:00
DEBUG ( 0 , ( " get_file_version: NE file [%s] malloc failed bytes = %d \n " ,
fname , PE_HEADER_SIZE ) ) ;
goto error_exit ;
}
/* This is a HACK! I got tired of trying to sort through the messy
* ' NE ' file format . If anyone wants to clean this up please have at
* it , but this works . ' NE ' files will eventually fade away . JRR */
while ( ( byte_count = vfs_read_data ( fsp , buf , VS_NE_BUF_SIZE ) ) > 0 ) {
/* Cover case that should not occur in a well formed 'NE' .dll file */
if ( byte_count - VS_VERSION_INFO_SIZE < = 0 ) break ;
for ( i = 0 ; i < byte_count ; i + + ) {
/* Fast skip past data that can't possibly match */
if ( buf [ i ] ! = ' V ' ) continue ;
/* Potential match data crosses buf boundry, move it to beginning
* of buf , and fill the buf with as much as it will hold . */
if ( i > byte_count - VS_VERSION_INFO_SIZE ) {
int bc ;
memcpy ( buf , & buf [ i ] , byte_count - i ) ;
if ( ( bc = vfs_read_data ( fsp , & buf [ byte_count - i ] , VS_NE_BUF_SIZE -
( byte_count - i ) ) ) < 0 ) {
DEBUG ( 0 , ( " get_file_version: NE file [%s] Read error, errno=%d \n " ,
fname , errno ) ) ;
goto error_exit ;
}
byte_count = bc + ( byte_count - i ) ;
if ( byte_count < VS_VERSION_INFO_SIZE ) break ;
i = 0 ;
}
/* Check that the full signature string and the magic number that
* follows exist ( not a perfect solution , but the chances that this
* occurs in code is , well , remote . Yes I know I ' m comparing the ' V '
* twice , as it is simpler to read the code . */
if ( strcmp ( & buf [ i ] , VS_SIGNATURE ) = = 0 ) {
/* Compute skip alignment to next long address */
2008-01-07 12:15:08 +03:00
int skip = - ( SMB_VFS_LSEEK ( fsp , 0 , SEEK_CUR ) - ( byte_count - i ) +
2000-11-15 00:56:32 +03:00
sizeof ( VS_SIGNATURE ) ) & 3 ;
if ( IVAL ( buf , i + sizeof ( VS_SIGNATURE ) + skip ) ! = 0xfeef04bd ) continue ;
* major = IVAL ( buf , i + sizeof ( VS_SIGNATURE ) + skip + VS_MAJOR_OFFSET ) ;
* minor = IVAL ( buf , i + sizeof ( VS_SIGNATURE ) + skip + VS_MINOR_OFFSET ) ;
DEBUG ( 6 , ( " get_file_version: NE file [%s] Version = %08x:%08x (%d.%d.%d.%d) \n " ,
fname , * major , * minor ,
( * major > > 16 ) & 0xffff , * major & 0xffff ,
( * minor > > 16 ) & 0xffff , * minor & 0xffff ) ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2002-01-08 04:22:23 +03:00
return 1 ;
2000-11-15 00:56:32 +03:00
}
}
}
/* Version info not found, fall back to origin date/time */
DEBUG ( 0 , ( " get_file_version: NE file [%s] Version info not found \n " , fname ) ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2002-01-08 04:22:23 +03:00
return 0 ;
2000-11-15 00:56:32 +03:00
} else
/* Assume this isn't an error... the file just looks sort of like a PE/NE file */
DEBUG ( 3 , ( " get_file_version: File [%s] unknown file format, signature = 0x%x \n " ,
fname , IVAL ( buf , PE_HEADER_SIGNATURE_OFFSET ) ) ) ;
no_version_info :
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2002-01-08 04:22:23 +03:00
return 0 ;
2000-11-15 00:56:32 +03:00
error_exit :
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2000-11-15 00:56:32 +03:00
return - 1 ;
}
/****************************************************************************
Drivers for Microsoft systems contain multiple files . Often , multiple drivers
share one or more files . During the MS installation process files are checked
to insure that only a newer version of a shared file is installed over an
older version . There are several possibilities for this comparison . If there
is no previous version , the new one is newer ( obviously ) . If either file is
missing the version info structure , compare the creation date ( on Unix use
the modification date ) . Otherwise chose the numerically larger version number .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-02-25 23:53:53 +03:00
static int file_version_is_newer ( connection_struct * conn , fstring new_file , fstring old_file )
2000-11-15 00:56:32 +03:00
{
2007-11-16 01:19:52 +03:00
bool use_version = true ;
2000-11-15 00:56:32 +03:00
uint32 new_major ;
uint32 new_minor ;
time_t new_create_time ;
uint32 old_major ;
uint32 old_minor ;
time_t old_create_time ;
2009-06-10 21:37:57 +04:00
struct smb_filename * smb_fname = NULL ;
2000-11-15 00:56:32 +03:00
files_struct * fsp = NULL ;
SMB_STRUCT_STAT st ;
2000-11-28 02:59:42 +03:00
2006-07-11 22:01:26 +04:00
NTSTATUS status ;
2009-06-10 21:37:57 +04:00
int ret ;
2006-07-11 22:01:26 +04:00
2005-06-03 03:18:52 +04:00
SET_STAT_INVALID ( st ) ;
2000-11-15 05:39:11 +03:00
new_create_time = ( time_t ) 0 ;
old_create_time = ( time_t ) 0 ;
2000-11-15 00:56:32 +03:00
/* Get file version info (if available) for previous file (if it exists) */
2009-06-10 21:37:57 +04:00
status = driver_unix_convert ( conn , old_file , & smb_fname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto error_exit ;
}
2008-11-21 05:55:24 +03:00
status = SMB_VFS_CREATE_FILE (
conn , /* conn */
NULL , /* req */
0 , /* root_dir_fid */
2009-06-12 23:54:11 +04:00
smb_fname , /* fname */
2008-11-21 05:55:24 +03:00
FILE_GENERIC_READ , /* access_mask */
FILE_SHARE_READ | FILE_SHARE_WRITE , /* share_access */
FILE_OPEN , /* create_disposition*/
0 , /* create_options */
FILE_ATTRIBUTE_NORMAL , /* file_attributes */
INTERNAL_OPEN_ONLY , /* oplock_request */
0 , /* allocation_size */
2010-03-06 02:10:30 +03:00
0 , /* private_flags */
2008-11-21 05:55:24 +03:00
NULL , /* sd */
NULL , /* ea_list */
& fsp , /* result */
2009-06-12 23:54:11 +04:00
NULL ) ; /* pinfo */
2005-07-08 08:51:27 +04:00
2006-07-11 22:01:26 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2000-11-15 00:56:32 +03:00
/* Old file not found, so by definition new file is in fact newer */
2009-06-12 23:54:11 +04:00
DEBUG ( 10 , ( " file_version_is_newer: Can't open old file [%s], "
" errno = %d \n " , smb_fname_str_dbg ( smb_fname ) ,
errno ) ) ;
2009-06-10 21:37:57 +04:00
ret = 1 ;
goto done ;
2000-11-15 00:56:32 +03:00
} else {
2009-06-10 21:37:57 +04:00
ret = get_file_version ( fsp , old_file , & old_major , & old_minor ) ;
2005-07-08 08:51:27 +04:00
if ( ret = = - 1 ) {
goto error_exit ;
}
2000-11-15 00:56:32 +03:00
if ( ! ret ) {
DEBUG ( 6 , ( " file_version_is_newer: Version info not found [%s], use mod time \n " ,
old_file ) ) ;
2007-11-16 01:19:52 +03:00
use_version = false ;
2008-01-07 15:21:26 +03:00
if ( SMB_VFS_FSTAT ( fsp , & st ) = = - 1 ) {
2007-11-16 01:19:52 +03:00
goto error_exit ;
}
2009-05-14 17:34:42 +04:00
old_create_time = convert_timespec_to_time_t ( st . st_ex_mtime ) ;
2009-02-25 20:59:53 +03:00
DEBUGADD ( 6 , ( " file_version_is_newer: mod time = %ld sec \n " ,
( long ) old_create_time ) ) ;
2000-11-15 00:56:32 +03:00
}
}
2008-10-09 18:27:49 +04:00
close_file ( NULL , fsp , NORMAL_CLOSE ) ;
2008-09-29 21:09:53 +04:00
fsp = NULL ;
2000-11-15 00:56:32 +03:00
/* Get file version info (if available) for new file */
2009-06-10 21:37:57 +04:00
status = driver_unix_convert ( conn , new_file , & smb_fname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto error_exit ;
}
2008-11-21 05:55:24 +03:00
status = SMB_VFS_CREATE_FILE (
conn , /* conn */
NULL , /* req */
0 , /* root_dir_fid */
2009-06-12 23:54:11 +04:00
smb_fname , /* fname */
2008-11-21 05:55:24 +03:00
FILE_GENERIC_READ , /* access_mask */
FILE_SHARE_READ | FILE_SHARE_WRITE , /* share_access */
FILE_OPEN , /* create_disposition*/
0 , /* create_options */
FILE_ATTRIBUTE_NORMAL , /* file_attributes */
INTERNAL_OPEN_ONLY , /* oplock_request */
0 , /* allocation_size */
2010-03-06 02:10:30 +03:00
0 , /* private_flags */
2008-11-21 05:55:24 +03:00
NULL , /* sd */
NULL , /* ea_list */
& fsp , /* result */
2009-06-12 23:54:11 +04:00
NULL ) ; /* pinfo */
2005-07-08 08:51:27 +04:00
2006-07-11 22:01:26 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2000-11-15 00:56:32 +03:00
/* New file not found, this shouldn't occur if the caller did its job */
2009-06-12 23:54:11 +04:00
DEBUG ( 3 , ( " file_version_is_newer: Can't open new file [%s], "
" errno = %d \n " , smb_fname_str_dbg ( smb_fname ) , errno ) ) ;
2000-11-15 00:56:32 +03:00
goto error_exit ;
} else {
2009-06-10 21:37:57 +04:00
ret = get_file_version ( fsp , new_file , & new_major , & new_minor ) ;
2005-07-08 08:51:27 +04:00
if ( ret = = - 1 ) {
goto error_exit ;
}
2000-11-15 00:56:32 +03:00
if ( ! ret ) {
DEBUG ( 6 , ( " file_version_is_newer: Version info not found [%s], use mod time \n " ,
new_file ) ) ;
2007-11-16 01:19:52 +03:00
use_version = false ;
2008-01-07 15:21:26 +03:00
if ( SMB_VFS_FSTAT ( fsp , & st ) = = - 1 ) {
2007-11-16 01:19:52 +03:00
goto error_exit ;
}
2009-05-14 17:34:42 +04:00
new_create_time = convert_timespec_to_time_t ( st . st_ex_mtime ) ;
2009-02-25 20:59:53 +03:00
DEBUGADD ( 6 , ( " file_version_is_newer: mod time = %ld sec \n " ,
( long ) new_create_time ) ) ;
2000-11-15 00:56:32 +03:00
}
}
2008-10-09 18:27:49 +04:00
close_file ( NULL , fsp , NORMAL_CLOSE ) ;
2008-09-29 21:57:22 +04:00
fsp = NULL ;
2000-11-15 00:56:32 +03:00
2002-08-17 20:31:24 +04:00
if ( use_version & & ( new_major ! = old_major | | new_minor ! = old_minor ) ) {
2000-11-15 00:56:32 +03:00
/* Compare versions and choose the larger version number */
if ( new_major > old_major | |
( new_major = = old_major & & new_minor > old_minor ) ) {
2007-11-16 01:19:52 +03:00
2000-11-15 00:56:32 +03:00
DEBUG ( 6 , ( " file_version_is_newer: Replacing [%s] with [%s] \n " , old_file , new_file ) ) ;
2009-06-10 21:37:57 +04:00
ret = 1 ;
goto done ;
2000-11-15 00:56:32 +03:00
}
else {
DEBUG ( 6 , ( " file_version_is_newer: Leaving [%s] unchanged \n " , old_file ) ) ;
2009-06-10 21:37:57 +04:00
ret = 0 ;
goto done ;
2000-11-15 00:56:32 +03:00
}
2001-08-10 23:38:53 +04:00
} else {
/* Compare modification time/dates and choose the newest time/date */
if ( new_create_time > old_create_time ) {
DEBUG ( 6 , ( " file_version_is_newer: Replacing [%s] with [%s] \n " , old_file , new_file ) ) ;
2009-06-10 21:37:57 +04:00
ret = 1 ;
goto done ;
2001-08-10 23:38:53 +04:00
}
else {
DEBUG ( 6 , ( " file_version_is_newer: Leaving [%s] unchanged \n " , old_file ) ) ;
2009-06-10 21:37:57 +04:00
ret = 0 ;
goto done ;
2001-08-10 23:38:53 +04:00
}
}
2009-06-10 21:37:57 +04:00
error_exit :
if ( fsp )
close_file ( NULL , fsp , NORMAL_CLOSE ) ;
ret = - 1 ;
done :
TALLOC_FREE ( smb_fname ) ;
return ret ;
2001-08-10 23:38:53 +04:00
}
/****************************************************************************
Determine the correct cVersion associated with an architecture and driver
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-11-24 01:01:24 +03:00
static uint32 get_correct_cversion ( struct pipes_struct * p ,
const char * architecture ,
2009-11-16 17:52:31 +03:00
const char * driverpath_in ,
2008-11-24 01:01:24 +03:00
WERROR * perr )
2001-08-10 23:38:53 +04:00
{
int cversion ;
2001-09-16 03:32:19 +04:00
NTSTATUS nt_status ;
2009-06-10 21:37:57 +04:00
struct smb_filename * smb_fname = NULL ;
char * driverpath = NULL ;
2001-08-10 23:38:53 +04:00
files_struct * fsp = NULL ;
2008-11-24 01:01:24 +03:00
connection_struct * conn = NULL ;
2006-07-11 22:01:26 +04:00
NTSTATUS status ;
2008-11-24 01:01:24 +03:00
char * oldcwd ;
fstring printdollar ;
int printdollar_snum ;
2001-08-10 23:38:53 +04:00
2001-12-29 23:29:43 +03:00
* perr = WERR_INVALID_PARAM ;
2001-08-10 23:38:53 +04:00
/* If architecture is Windows 95/98/ME, the version is always 0. */
2005-09-27 23:18:20 +04:00
if ( strcmp ( architecture , SPL_ARCH_WIN40 ) = = 0 ) {
2001-08-10 23:38:53 +04:00
DEBUG ( 10 , ( " get_correct_cversion: Driver is Win9x, cversion = 0 \n " ) ) ;
2001-12-29 23:29:43 +03:00
* perr = WERR_OK ;
2001-08-10 23:38:53 +04:00
return 0 ;
}
2005-09-27 23:18:20 +04:00
/* If architecture is Windows x64, the version is always 3. */
if ( strcmp ( architecture , SPL_ARCH_X64 ) = = 0 ) {
DEBUG ( 10 , ( " get_correct_cversion: Driver is x64, cversion = 3 \n " ) ) ;
* perr = WERR_OK ;
return 3 ;
}
2008-11-24 01:01:24 +03:00
fstrcpy ( printdollar , " print$ " ) ;
2002-02-09 07:10:24 +03:00
2008-11-24 01:01:24 +03:00
printdollar_snum = find_service ( printdollar ) ;
if ( printdollar_snum = = - 1 ) {
* perr = WERR_NO_SUCH_SHARE ;
2001-08-10 23:38:53 +04:00
return - 1 ;
}
2008-11-24 01:01:24 +03:00
nt_status = create_conn_struct ( talloc_tos ( ) , & conn , printdollar_snum ,
lp_pathname ( printdollar_snum ) ,
p - > server_info , & oldcwd ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
DEBUG ( 0 , ( " get_correct_cversion: create_conn_struct "
" returned %s \n " , nt_errstr ( nt_status ) ) ) ;
* perr = ntstatus_to_werror ( nt_status ) ;
2001-08-10 23:38:53 +04:00
return - 1 ;
}
/* Open the driver file (Portable Executable format) and determine the
* deriver the cversion . */
2007-11-16 01:19:52 +03:00
driverpath = talloc_asprintf ( talloc_tos ( ) ,
" %s/%s " ,
architecture ,
driverpath_in ) ;
if ( ! driverpath ) {
* perr = WERR_NOMEM ;
goto error_exit ;
}
2001-08-10 23:38:53 +04:00
2009-06-10 21:37:57 +04:00
nt_status = driver_unix_convert ( conn , driverpath , & smb_fname ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
* perr = ntstatus_to_werror ( nt_status ) ;
2007-11-16 01:19:52 +03:00
goto error_exit ;
}
2001-08-10 23:38:53 +04:00
2009-06-10 21:37:57 +04:00
nt_status = vfs_file_exist ( conn , smb_fname ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2005-06-15 23:28:14 +04:00
* perr = WERR_BADFILE ;
goto error_exit ;
}
2008-11-21 05:55:24 +03:00
status = SMB_VFS_CREATE_FILE (
conn , /* conn */
NULL , /* req */
0 , /* root_dir_fid */
2009-06-12 23:54:11 +04:00
smb_fname , /* fname */
2008-11-21 05:55:24 +03:00
FILE_GENERIC_READ , /* access_mask */
FILE_SHARE_READ | FILE_SHARE_WRITE , /* share_access */
FILE_OPEN , /* create_disposition*/
0 , /* create_options */
FILE_ATTRIBUTE_NORMAL , /* file_attributes */
INTERNAL_OPEN_ONLY , /* oplock_request */
2010-03-06 02:10:30 +03:00
0 , /* private_flags */
2008-11-21 05:55:24 +03:00
0 , /* allocation_size */
NULL , /* sd */
NULL , /* ea_list */
& fsp , /* result */
2009-06-12 23:54:11 +04:00
NULL ) ; /* pinfo */
2005-06-15 23:28:14 +04:00
2006-07-11 22:01:26 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-06-12 23:54:11 +04:00
DEBUG ( 3 , ( " get_correct_cversion: Can't open file [%s], errno = "
" %d \n " , smb_fname_str_dbg ( smb_fname ) , errno ) ) ;
2001-09-04 11:13:01 +04:00
* perr = WERR_ACCESS_DENIED ;
2001-08-10 23:38:53 +04:00
goto error_exit ;
2005-07-08 08:51:27 +04:00
} else {
2001-08-10 23:38:53 +04:00
uint32 major ;
uint32 minor ;
2009-06-12 23:54:11 +04:00
int ret ;
ret = get_file_version ( fsp , smb_fname - > base_name , & major , & minor ) ;
2001-08-10 23:38:53 +04:00
if ( ret = = - 1 ) goto error_exit ;
if ( ! ret ) {
2009-06-12 23:54:11 +04:00
DEBUG ( 6 , ( " get_correct_cversion: Version info not "
" found [%s] \n " ,
smb_fname_str_dbg ( smb_fname ) ) ) ;
2001-08-10 23:38:53 +04:00
goto error_exit ;
}
/*
* This is a Microsoft ' ism . See references in MSDN to VER_FILEVERSION
2009-02-09 14:40:12 +03:00
* for more details . Version in this case is not just the version of the
2001-08-10 23:38:53 +04:00
* file , but the version in the sense of kernal mode ( 2 ) vs . user mode
2009-02-09 14:40:12 +03:00
* ( 3 ) drivers . Other bits of the version fields are the version info .
2001-08-10 23:38:53 +04:00
* JRR 010716
*/
cversion = major & 0x0000ffff ;
switch ( cversion ) {
case 2 : /* WinNT drivers */
case 3 : /* Win2K drivers */
break ;
2009-02-09 14:40:12 +03:00
2001-08-10 23:38:53 +04:00
default :
2009-06-12 23:54:11 +04:00
DEBUG ( 6 , ( " get_correct_cversion: cversion "
" invalid [%s] cversion = %d \n " ,
smb_fname_str_dbg ( smb_fname ) ,
cversion ) ) ;
2001-08-10 23:38:53 +04:00
goto error_exit ;
}
2009-06-12 23:54:11 +04:00
DEBUG ( 10 , ( " get_correct_cversion: Version info found [%s] major "
" = 0x%x minor = 0x%x \n " ,
smb_fname_str_dbg ( smb_fname ) , major , minor ) ) ;
2001-08-10 23:38:53 +04:00
}
2005-06-15 23:28:14 +04:00
DEBUG ( 10 , ( " get_correct_cversion: Driver file [%s] cversion = %d \n " ,
2009-06-12 23:54:11 +04:00
smb_fname_str_dbg ( smb_fname ) , cversion ) ) ;
2001-08-10 23:38:53 +04:00
2008-11-24 01:01:24 +03:00
goto done ;
2001-08-10 23:38:53 +04:00
2008-11-24 01:01:24 +03:00
error_exit :
cversion = - 1 ;
done :
2009-06-10 21:37:57 +04:00
TALLOC_FREE ( smb_fname ) ;
2008-11-24 01:01:24 +03:00
if ( fsp ! = NULL ) {
2008-10-09 18:27:49 +04:00
close_file ( NULL , fsp , NORMAL_CLOSE ) ;
2008-11-24 01:01:24 +03:00
}
if ( conn ! = NULL ) {
vfs_ChDir ( conn , oldcwd ) ;
2009-08-07 13:48:03 +04:00
conn_free ( conn ) ;
2008-11-24 01:01:24 +03:00
}
if ( cversion ! = - 1 ) {
* perr = WERR_OK ;
}
return cversion ;
2001-08-10 23:38:53 +04:00
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-16 17:52:31 +03:00
# define strip_driver_path(_mem_ctx, _element) do { \
2010-04-23 04:34:43 +04:00
if ( _element & & ( ( _p = strrchr ( ( _element ) , ' \\ ' ) ) ! = NULL ) ) { \
2009-11-26 01:47:36 +03:00
( _element ) = talloc_asprintf ( ( _mem_ctx ) , " %s " , _p + 1 ) ; \
W_ERROR_HAVE_NO_MEMORY ( ( _element ) ) ; \
2009-11-16 17:52:31 +03:00
} \
} while ( 0 ) ;
2009-11-23 14:57:46 +03:00
static WERROR clean_up_driver_struct_level ( TALLOC_CTX * mem_ctx ,
struct pipes_struct * rpc_pipe ,
const char * architecture ,
const char * * driver_path ,
const char * * data_file ,
const char * * config_file ,
const char * * help_file ,
struct spoolss_StringArray * dependent_files ,
uint32_t * version )
2001-08-10 23:38:53 +04:00
{
2009-11-23 14:57:46 +03:00
const char * short_architecture ;
2001-08-10 23:38:53 +04:00
int i ;
2001-09-04 11:13:01 +04:00
WERROR err ;
2009-11-16 17:52:31 +03:00
char * _p ;
2001-08-10 23:38:53 +04:00
2010-04-23 04:34:43 +04:00
if ( ! * driver_path | | ! * data_file | | ! * config_file ) {
return WERR_INVALID_PARAM ;
}
2001-08-10 23:38:53 +04:00
/* clean up the driver name.
* we can get . \ driver . dll
* or worse c : \ windows \ system \ driver . dll !
*/
/* using an intermediate string to not have overlaping memcpy()'s */
2009-11-23 14:57:46 +03:00
strip_driver_path ( mem_ctx , * driver_path ) ;
strip_driver_path ( mem_ctx , * data_file ) ;
strip_driver_path ( mem_ctx , * config_file ) ;
2010-04-23 04:34:43 +04:00
if ( help_file ) {
strip_driver_path ( mem_ctx , * help_file ) ;
}
2001-08-10 23:38:53 +04:00
2009-11-23 14:57:46 +03:00
if ( dependent_files & & dependent_files - > string ) {
for ( i = 0 ; dependent_files - > string [ i ] ; i + + ) {
strip_driver_path ( mem_ctx , dependent_files - > string [ i ] ) ;
2001-08-10 23:38:53 +04:00
}
}
2009-11-23 14:57:46 +03:00
short_architecture = get_short_archi ( architecture ) ;
if ( ! short_architecture ) {
2006-06-20 01:36:19 +04:00
return WERR_UNKNOWN_PRINTER_DRIVER ;
}
2009-02-09 14:40:12 +03:00
2001-08-10 23:38:53 +04:00
/* jfm:7/16/2000 the client always sends the cversion=0.
* The server should check which version the driver is by reading
* the PE header of driver - > driverpath .
*
* For Windows 95 / 98 the version is 0 ( so the value sent is correct )
* For Windows NT ( the architecture doesn ' t matter )
* NT 3.1 : cversion = 0
* NT 3.5 / 3.51 : cversion = 1
* NT 4 : cversion = 2
* NT2K : cversion = 3
*/
2009-11-16 17:52:31 +03:00
2009-11-23 14:57:46 +03:00
* version = get_correct_cversion ( rpc_pipe , short_architecture ,
* driver_path , & err ) ;
if ( * version = = - 1 ) {
2006-06-20 01:36:19 +04:00
return err ;
2001-08-10 23:38:53 +04:00
}
2001-09-04 11:13:01 +04:00
return WERR_OK ;
2001-08-10 23:38:53 +04:00
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-23 14:57:46 +03:00
2008-11-24 01:01:24 +03:00
WERROR clean_up_driver_struct ( struct pipes_struct * rpc_pipe ,
2009-11-16 17:52:31 +03:00
struct spoolss_AddDriverInfoCtr * r )
2001-08-10 23:38:53 +04:00
{
2009-11-16 17:52:31 +03:00
switch ( r - > level ) {
case 3 :
2009-11-23 14:57:46 +03:00
return clean_up_driver_struct_level ( r , rpc_pipe ,
r - > info . info3 - > architecture ,
& r - > info . info3 - > driver_path ,
& r - > info . info3 - > data_file ,
& r - > info . info3 - > config_file ,
& r - > info . info3 - > help_file ,
r - > info . info3 - > dependent_files ,
& r - > info . info3 - > version ) ;
2009-11-16 17:52:31 +03:00
case 6 :
2009-11-23 14:57:46 +03:00
return clean_up_driver_struct_level ( r , rpc_pipe ,
r - > info . info6 - > architecture ,
& r - > info . info6 - > driver_path ,
& r - > info . info6 - > data_file ,
& r - > info . info6 - > config_file ,
& r - > info . info6 - > help_file ,
r - > info . info6 - > dependent_files ,
& r - > info . info6 - > version ) ;
2009-11-16 17:52:31 +03:00
default :
return WERR_NOT_SUPPORTED ;
2000-11-15 00:56:32 +03:00
}
2001-08-10 23:38:53 +04:00
}
2000-11-15 00:56:32 +03:00
2001-08-10 23:38:53 +04:00
/****************************************************************************
This function sucks and should be replaced . JRA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-16 17:52:31 +03:00
static void convert_level_6_to_level3 ( struct spoolss_AddDriverInfo3 * dst ,
const struct spoolss_AddDriverInfo6 * src )
2001-08-10 23:38:53 +04:00
{
2009-11-16 17:52:31 +03:00
dst - > version = src - > version ;
dst - > driver_name = src - > driver_name ;
dst - > architecture = src - > architecture ;
dst - > driver_path = src - > driver_path ;
dst - > data_file = src - > data_file ;
dst - > config_file = src - > config_file ;
dst - > help_file = src - > help_file ;
dst - > monitor_name = src - > monitor_name ;
dst - > default_datatype = src - > default_datatype ;
dst - > _ndr_size_dependent_files = src - > _ndr_size_dependent_files ;
dst - > dependent_files = src - > dependent_files ;
2001-08-10 23:38:53 +04:00
}
2009-11-27 03:49:08 +03:00
/****************************************************************************
This function sucks and should be replaced . JRA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void convert_level_8_to_level3 ( TALLOC_CTX * mem_ctx ,
struct spoolss_AddDriverInfo3 * dst ,
const struct spoolss_DriverInfo8 * src )
{
dst - > version = src - > version ;
dst - > driver_name = src - > driver_name ;
dst - > architecture = src - > architecture ;
dst - > driver_path = src - > driver_path ;
dst - > data_file = src - > data_file ;
dst - > config_file = src - > config_file ;
dst - > help_file = src - > help_file ;
dst - > monitor_name = src - > monitor_name ;
dst - > default_datatype = src - > default_datatype ;
if ( src - > dependent_files ) {
dst - > dependent_files = talloc_zero ( mem_ctx , struct spoolss_StringArray ) ;
if ( ! dst - > dependent_files ) return ;
dst - > dependent_files - > string = src - > dependent_files ;
} else {
dst - > dependent_files = NULL ;
}
}
2000-11-15 00:56:32 +03:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-04-30 03:07:31 +04:00
static WERROR move_driver_file_to_download_area ( TALLOC_CTX * mem_ctx ,
connection_struct * conn ,
const char * driver_file ,
2009-04-30 18:49:50 +04:00
const char * short_architecture ,
uint32_t driver_version ,
2009-04-30 03:07:31 +04:00
uint32_t version )
{
2009-06-10 21:37:57 +04:00
struct smb_filename * smb_fname_old = NULL ;
struct smb_filename * smb_fname_new = NULL ;
2009-04-30 03:07:31 +04:00
char * old_name = NULL ;
char * new_name = NULL ;
NTSTATUS status ;
2009-06-10 21:37:57 +04:00
WERROR ret ;
2009-04-30 03:07:31 +04:00
old_name = talloc_asprintf ( mem_ctx , " %s/%s " ,
2009-04-30 18:49:50 +04:00
short_architecture , driver_file ) ;
2009-04-30 03:07:31 +04:00
W_ERROR_HAVE_NO_MEMORY ( old_name ) ;
2009-04-30 18:49:50 +04:00
new_name = talloc_asprintf ( mem_ctx , " %s/%d/%s " ,
short_architecture , driver_version , driver_file ) ;
2009-06-15 09:45:47 +04:00
if ( new_name = = NULL ) {
TALLOC_FREE ( old_name ) ;
return WERR_NOMEM ;
}
2009-04-30 18:49:50 +04:00
if ( version ! = - 1 & & ( version = file_version_is_newer ( conn , old_name , new_name ) ) > 0 ) {
2009-06-10 21:37:57 +04:00
status = driver_unix_convert ( conn , old_name , & smb_fname_old ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
ret = WERR_NOMEM ;
goto out ;
}
/* Setup a synthetic smb_filename struct */
smb_fname_new = TALLOC_ZERO_P ( mem_ctx , struct smb_filename ) ;
if ( ! smb_fname_new ) {
ret = WERR_NOMEM ;
goto out ;
}
smb_fname_new - > base_name = new_name ;
2009-04-30 03:07:31 +04:00
2009-06-10 21:37:57 +04:00
DEBUG ( 10 , ( " move_driver_file_to_download_area: copying '%s' to "
" '%s' \n " , smb_fname_old - > base_name ,
smb_fname_new - > base_name ) ) ;
2009-04-30 03:07:31 +04:00
2009-06-10 21:37:57 +04:00
status = copy_file ( mem_ctx , conn , smb_fname_old , smb_fname_new ,
2009-04-30 03:07:31 +04:00
OPENX_FILE_EXISTS_TRUNCATE |
OPENX_FILE_CREATE_IF_NOT_EXIST ,
0 , false ) ;
2009-04-30 18:49:50 +04:00
2009-04-30 03:07:31 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-06-10 21:37:57 +04:00
DEBUG ( 0 , ( " move_driver_file_to_download_area: Unable "
" to rename [%s] to [%s]: %s \n " ,
smb_fname_old - > base_name , new_name ,
nt_errstr ( status ) ) ) ;
ret = WERR_ACCESS_DENIED ;
goto out ;
2009-04-30 03:07:31 +04:00
}
}
2009-06-10 21:37:57 +04:00
ret = WERR_OK ;
out :
TALLOC_FREE ( smb_fname_old ) ;
TALLOC_FREE ( smb_fname_new ) ;
return ret ;
2009-04-30 03:07:31 +04:00
}
2008-11-24 00:46:08 +03:00
WERROR move_driver_to_download_area ( struct pipes_struct * p ,
2009-11-16 17:52:31 +03:00
struct spoolss_AddDriverInfoCtr * r ,
WERROR * perr )
2000-07-25 17:15:16 +04:00
{
2009-11-16 17:52:31 +03:00
struct spoolss_AddDriverInfo3 * driver ;
struct spoolss_AddDriverInfo3 converted_driver ;
2009-04-30 18:49:50 +04:00
const char * short_architecture ;
2009-06-10 21:37:57 +04:00
struct smb_filename * smb_dname = NULL ;
2007-11-16 01:19:52 +03:00
char * new_dir = NULL ;
2008-11-24 00:46:08 +03:00
connection_struct * conn = NULL ;
2001-09-16 03:32:19 +04:00
NTSTATUS nt_status ;
2000-07-25 17:15:16 +04:00
int i ;
2007-09-13 01:48:20 +04:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
2007-11-16 01:19:52 +03:00
int ver = 0 ;
2008-11-24 00:46:08 +03:00
char * oldcwd ;
fstring printdollar ;
int printdollar_snum ;
2000-07-25 17:15:16 +04:00
2001-09-04 11:13:01 +04:00
* perr = WERR_OK ;
2000-08-31 01:09:21 +04:00
2009-11-16 17:52:31 +03:00
switch ( r - > level ) {
2009-04-30 18:49:50 +04:00
case 3 :
2009-11-16 17:52:31 +03:00
driver = r - > info . info3 ;
2009-04-30 18:49:50 +04:00
break ;
case 6 :
2009-11-16 17:52:31 +03:00
convert_level_6_to_level3 ( & converted_driver , r - > info . info6 ) ;
2000-09-07 03:20:35 +04:00
driver = & converted_driver ;
2009-04-30 18:49:50 +04:00
break ;
default :
2009-11-16 17:52:31 +03:00
DEBUG ( 0 , ( " move_driver_to_download_area: Unknown info level (%u) \n " , ( unsigned int ) r - > level ) ) ;
2005-06-15 21:03:34 +04:00
return WERR_UNKNOWN_LEVEL ;
2000-09-07 03:20:35 +04:00
}
2009-11-16 17:52:31 +03:00
short_architecture = get_short_archi ( driver - > architecture ) ;
2009-04-30 18:49:50 +04:00
if ( ! short_architecture ) {
2006-06-20 01:36:19 +04:00
return WERR_UNKNOWN_PRINTER_DRIVER ;
}
2000-07-25 17:15:16 +04:00
2008-11-24 00:46:08 +03:00
fstrcpy ( printdollar , " print$ " ) ;
2000-07-25 17:15:16 +04:00
2008-11-24 00:46:08 +03:00
printdollar_snum = find_service ( printdollar ) ;
if ( printdollar_snum = = - 1 ) {
* perr = WERR_NO_SUCH_SHARE ;
2005-06-15 21:03:34 +04:00
return WERR_NO_SUCH_SHARE ;
2000-07-26 02:35:57 +04:00
}
2008-11-24 00:46:08 +03:00
nt_status = create_conn_struct ( talloc_tos ( ) , & conn , printdollar_snum ,
lp_pathname ( printdollar_snum ) ,
p - > server_info , & oldcwd ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
DEBUG ( 0 , ( " move_driver_to_download_area: create_conn_struct "
" returned %s \n " , nt_errstr ( nt_status ) ) ) ;
* perr = ntstatus_to_werror ( nt_status ) ;
return * perr ;
2000-07-25 21:09:29 +04:00
}
2007-11-16 01:19:52 +03:00
new_dir = talloc_asprintf ( ctx ,
" %s/%d " ,
2009-04-30 18:49:50 +04:00
short_architecture ,
2009-11-16 17:52:31 +03:00
driver - > version ) ;
2007-11-16 01:19:52 +03:00
if ( ! new_dir ) {
* perr = WERR_NOMEM ;
goto err_exit ;
}
2009-06-10 21:37:57 +04:00
nt_status = driver_unix_convert ( conn , new_dir , & smb_dname ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2007-11-16 01:19:52 +03:00
* perr = WERR_NOMEM ;
goto err_exit ;
}
2009-06-10 21:37:57 +04:00
DEBUG ( 5 , ( " Creating first directory: %s \n " , smb_dname - > base_name ) ) ;
2009-04-30 18:49:50 +04:00
2009-06-10 21:37:57 +04:00
create_directory ( conn , NULL , smb_dname ) ;
2000-07-25 17:15:16 +04:00
2000-11-15 00:56:32 +03:00
/* For each driver file, archi\filexxx.yyy, if there is a duplicate file
* listed for this driver which has already been moved , skip it ( note :
* drivers may list the same file name several times . Then check if the
2009-11-16 17:52:31 +03:00
* file already exists in archi \ version \ , if so , check that the version
2000-11-15 00:56:32 +03:00
* info ( or time stamps if version info is unavailable ) is newer ( or the
2009-11-16 17:52:31 +03:00
* date is later ) . If it is , move it to archi \ version \ filexxx . yyy .
2000-11-15 00:56:32 +03:00
* Otherwise , delete the file .
2000-08-01 00:41:51 +04:00
*
2009-11-16 17:52:31 +03:00
* If a file is not moved to archi \ version \ because of an error , all the
2000-11-15 00:56:32 +03:00
* rest of the ' unmoved ' driver files are removed from archi \ . If one or
2009-11-16 17:52:31 +03:00
* more of the driver ' s files was already moved to archi \ version \ , it
2000-11-15 00:56:32 +03:00
* potentially leaves the driver in a partially updated state . Version
* trauma will most likely occur if an client attempts to use any printer
* bound to the driver . Perhaps a rewrite to make sure the moves can be
* done is appropriate . . . later JRR
2000-07-25 17:15:16 +04:00
*/
2000-11-15 00:56:32 +03:00
DEBUG ( 5 , ( " Moving files now ! \n " ) ) ;
2000-10-25 21:56:38 +04:00
2009-11-16 17:52:31 +03:00
if ( driver - > driver_path & & strlen ( driver - > driver_path ) ) {
2007-11-16 01:19:52 +03:00
2009-04-30 03:25:23 +04:00
* perr = move_driver_file_to_download_area ( ctx ,
conn ,
2009-11-16 17:52:31 +03:00
driver - > driver_path ,
2009-04-30 18:49:50 +04:00
short_architecture ,
2009-11-16 17:52:31 +03:00
driver - > version ,
2009-04-30 03:25:23 +04:00
ver ) ;
if ( ! W_ERROR_IS_OK ( * perr ) ) {
if ( W_ERROR_EQUAL ( * perr , WERR_ACCESS_DENIED ) ) {
2000-11-15 00:56:32 +03:00
ver = - 1 ;
}
2009-04-30 03:25:23 +04:00
goto err_exit ;
2007-11-16 01:19:52 +03:00
}
2000-10-25 21:56:38 +04:00
}
2000-07-25 17:15:16 +04:00
2009-11-16 17:52:31 +03:00
if ( driver - > data_file & & strlen ( driver - > data_file ) ) {
if ( ! strequal ( driver - > data_file , driver - > driver_path ) ) {
2009-04-30 03:25:23 +04:00
* perr = move_driver_file_to_download_area ( ctx ,
conn ,
2009-11-16 17:52:31 +03:00
driver - > data_file ,
2009-04-30 18:49:50 +04:00
short_architecture ,
2009-11-16 17:52:31 +03:00
driver - > version ,
2009-04-30 03:25:23 +04:00
ver ) ;
if ( ! W_ERROR_IS_OK ( * perr ) ) {
if ( W_ERROR_EQUAL ( * perr , WERR_ACCESS_DENIED ) ) {
2000-11-15 00:56:32 +03:00
ver = - 1 ;
}
2009-04-30 03:25:23 +04:00
goto err_exit ;
2004-05-03 20:38:37 +04:00
}
2000-08-09 22:40:48 +04:00
}
2000-08-01 00:41:51 +04:00
}
2000-07-25 17:15:16 +04:00
2009-11-16 17:52:31 +03:00
if ( driver - > config_file & & strlen ( driver - > config_file ) ) {
if ( ! strequal ( driver - > config_file , driver - > driver_path ) & &
! strequal ( driver - > config_file , driver - > data_file ) ) {
2009-04-30 03:25:23 +04:00
* perr = move_driver_file_to_download_area ( ctx ,
conn ,
2009-11-16 17:52:31 +03:00
driver - > config_file ,
2009-04-30 18:49:50 +04:00
short_architecture ,
2009-11-16 17:52:31 +03:00
driver - > version ,
2009-04-30 03:25:23 +04:00
ver ) ;
if ( ! W_ERROR_IS_OK ( * perr ) ) {
if ( W_ERROR_EQUAL ( * perr , WERR_ACCESS_DENIED ) ) {
2000-11-15 00:56:32 +03:00
ver = - 1 ;
}
2009-04-30 03:25:23 +04:00
goto err_exit ;
2004-05-03 20:38:37 +04:00
}
2000-08-09 22:40:48 +04:00
}
2000-08-01 00:41:51 +04:00
}
2000-07-25 17:15:16 +04:00
2009-11-16 17:52:31 +03:00
if ( driver - > help_file & & strlen ( driver - > help_file ) ) {
if ( ! strequal ( driver - > help_file , driver - > driver_path ) & &
! strequal ( driver - > help_file , driver - > data_file ) & &
! strequal ( driver - > help_file , driver - > config_file ) ) {
2009-04-30 03:25:23 +04:00
* perr = move_driver_file_to_download_area ( ctx ,
conn ,
2009-11-16 17:52:31 +03:00
driver - > help_file ,
2009-04-30 18:49:50 +04:00
short_architecture ,
2009-11-16 17:52:31 +03:00
driver - > version ,
2009-04-30 03:25:23 +04:00
ver ) ;
if ( ! W_ERROR_IS_OK ( * perr ) ) {
if ( W_ERROR_EQUAL ( * perr , WERR_ACCESS_DENIED ) ) {
2000-11-15 00:56:32 +03:00
ver = - 1 ;
}
2009-04-30 03:25:23 +04:00
goto err_exit ;
2004-05-03 20:38:37 +04:00
}
2000-08-09 22:40:48 +04:00
}
2000-08-01 00:41:51 +04:00
}
2000-07-25 17:15:16 +04:00
2009-11-16 17:52:31 +03:00
if ( driver - > dependent_files & & driver - > dependent_files - > string ) {
for ( i = 0 ; driver - > dependent_files - > string [ i ] ; i + + ) {
if ( ! strequal ( driver - > dependent_files - > string [ i ] , driver - > driver_path ) & &
! strequal ( driver - > dependent_files - > string [ i ] , driver - > data_file ) & &
! strequal ( driver - > dependent_files - > string [ i ] , driver - > config_file ) & &
! strequal ( driver - > dependent_files - > string [ i ] , driver - > help_file ) ) {
2000-08-22 10:28:33 +04:00
int j ;
for ( j = 0 ; j < i ; j + + ) {
2009-11-16 17:52:31 +03:00
if ( strequal ( driver - > dependent_files - > string [ i ] , driver - > dependent_files - > string [ j ] ) ) {
2000-08-22 10:28:33 +04:00
goto NextDriver ;
}
}
2009-04-30 03:25:23 +04:00
* perr = move_driver_file_to_download_area ( ctx ,
conn ,
2009-11-16 17:52:31 +03:00
driver - > dependent_files - > string [ i ] ,
2009-04-30 18:49:50 +04:00
short_architecture ,
2009-11-16 17:52:31 +03:00
driver - > version ,
2009-04-30 03:25:23 +04:00
ver ) ;
if ( ! W_ERROR_IS_OK ( * perr ) ) {
if ( W_ERROR_EQUAL ( * perr , WERR_ACCESS_DENIED ) ) {
2000-11-15 00:56:32 +03:00
ver = - 1 ;
}
2009-04-30 03:25:23 +04:00
goto err_exit ;
2004-05-03 20:38:37 +04:00
}
2000-08-01 00:41:51 +04:00
}
2000-09-27 03:28:04 +04:00
NextDriver : ;
2000-07-25 17:15:16 +04:00
}
}
2007-11-16 01:19:52 +03:00
err_exit :
2009-06-10 21:37:57 +04:00
TALLOC_FREE ( smb_dname ) ;
2007-11-16 01:19:52 +03:00
2008-11-24 00:46:08 +03:00
if ( conn ! = NULL ) {
vfs_ChDir ( conn , oldcwd ) ;
2009-08-07 13:48:03 +04:00
conn_free ( conn ) ;
2008-11-24 00:46:08 +03:00
}
2000-07-25 21:09:29 +04:00
2007-11-16 01:19:52 +03:00
if ( W_ERROR_EQUAL ( * perr , WERR_OK ) ) {
return WERR_OK ;
}
if ( ver = = - 1 ) {
return WERR_UNKNOWN_PRINTER_DRIVER ;
}
return ( * perr ) ;
2000-02-07 19:17:59 +03:00
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-22 00:56:36 +03:00
2009-11-16 17:52:31 +03:00
static uint32 add_a_printer_driver_3 ( struct spoolss_AddDriverInfo3 * driver )
2000-02-07 19:17:59 +03:00
{
2007-11-22 00:56:36 +03:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
2000-05-24 10:10:21 +04:00
int len , buflen ;
2003-04-23 17:27:35 +04:00
const char * architecture ;
2007-11-22 00:56:36 +03:00
char * directory = NULL ;
char * key = NULL ;
2007-03-29 13:35:51 +04:00
uint8 * buf ;
2000-05-24 10:10:21 +04:00
int i , ret ;
2007-03-27 15:22:07 +04:00
TDB_DATA dbuf ;
2000-02-07 19:17:59 +03:00
2009-11-16 17:52:31 +03:00
architecture = get_short_archi ( driver - > architecture ) ;
2006-06-20 01:36:19 +04:00
if ( ! architecture ) {
return ( uint32 ) - 1 ;
}
2000-02-07 19:17:59 +03:00
2000-08-01 00:41:51 +04:00
/* The names are relative. We store them in the form: \print$\arch\version\driver.xxx
2000-07-25 17:15:16 +04:00
* \ \ server is added in the rpc server layer .
* It does make sense to NOT store the server ' s name in the printer TDB .
2000-02-07 19:17:59 +03:00
*/
2000-07-25 17:15:16 +04:00
2007-11-22 00:56:36 +03:00
directory = talloc_asprintf ( ctx , " \\ print$ \\ %s \\ %d \\ " ,
2009-11-16 17:52:31 +03:00
architecture , driver - > version ) ;
2007-11-22 00:56:36 +03:00
if ( ! directory ) {
return ( uint32 ) - 1 ;
}
2000-07-25 17:15:16 +04:00
2009-11-16 17:52:31 +03:00
# define gen_full_driver_unc_path(ctx, directory, file) \
do { \
if ( file & & strlen ( file ) ) { \
file = talloc_asprintf ( ctx , " %s%s " , directory , file ) ; \
} else { \
file = talloc_strdup ( ctx , " " ) ; \
} \
if ( ! file ) { \
return ( uint32_t ) - 1 ; \
} \
} while ( 0 ) ;
2009-02-09 14:40:12 +03:00
/* .inf files do not always list a file for each of the four standard files.
2002-02-09 07:10:24 +03:00
* Don ' t prepend a path to a null filename , or client claims :
2009-02-09 14:40:12 +03:00
* " The server on which the printer resides does not have a suitable
* < printer driver name > printer driver installed . Click OK if you
2002-02-09 07:10:24 +03:00
* wish to install the driver on your local machine . "
*/
2000-07-25 17:15:16 +04:00
2009-11-16 17:52:31 +03:00
gen_full_driver_unc_path ( ctx , directory , driver - > driver_path ) ;
gen_full_driver_unc_path ( ctx , directory , driver - > data_file ) ;
gen_full_driver_unc_path ( ctx , directory , driver - > config_file ) ;
gen_full_driver_unc_path ( ctx , directory , driver - > help_file ) ;
2000-07-25 17:15:16 +04:00
2009-11-16 17:52:31 +03:00
if ( driver - > dependent_files & & driver - > dependent_files - > string ) {
for ( i = 0 ; driver - > dependent_files - > string [ i ] ; i + + ) {
gen_full_driver_unc_path ( ctx , directory ,
driver - > dependent_files - > string [ i ] ) ;
2000-07-25 17:15:16 +04:00
}
}
2007-11-22 00:56:36 +03:00
key = talloc_asprintf ( ctx , " %s%s/%d/%s " , DRIVERS_PREFIX ,
2009-11-16 17:52:31 +03:00
architecture , driver - > version , driver - > driver_name ) ;
2007-11-22 00:56:36 +03:00
if ( ! key ) {
return ( uint32 ) - 1 ;
}
2000-07-25 17:15:16 +04:00
2000-09-15 04:15:10 +04:00
DEBUG ( 5 , ( " add_a_printer_driver_3: Adding driver with key %s \n " , key ) ) ;
2000-05-24 10:10:21 +04:00
buf = NULL ;
len = buflen = 0 ;
again :
len = 0 ;
2000-11-15 00:56:32 +03:00
len + = tdb_pack ( buf + len , buflen - len , " dffffffff " ,
2009-11-16 17:52:31 +03:00
driver - > version ,
driver - > driver_name ,
driver - > architecture ,
driver - > driver_path ,
driver - > data_file ,
driver - > config_file ,
driver - > help_file ,
driver - > monitor_name ? driver - > monitor_name : " " ,
driver - > default_datatype ? driver - > default_datatype : " " ) ;
if ( driver - > dependent_files & & driver - > dependent_files - > string ) {
for ( i = 0 ; driver - > dependent_files - > string [ i ] ; i + + ) {
2000-11-15 00:56:32 +03:00
len + = tdb_pack ( buf + len , buflen - len , " f " ,
2009-11-16 17:52:31 +03:00
driver - > dependent_files - > string [ i ] ) ;
2000-05-24 10:10:21 +04:00
}
2000-02-07 19:17:59 +03:00
}
2000-05-24 10:10:21 +04:00
if ( len ! = buflen ) {
2007-03-29 13:35:51 +04:00
buf = ( uint8 * ) SMB_REALLOC ( buf , len ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! buf ) {
2001-08-12 21:30:01 +04:00
DEBUG ( 0 , ( " add_a_printer_driver_3: failed to enlarge buffer \n ! " ) ) ;
ret = - 1 ;
goto done ;
}
2000-05-24 10:10:21 +04:00
buflen = len ;
goto again ;
}
dbuf . dptr = buf ;
dbuf . dsize = len ;
2007-11-22 00:56:36 +03:00
2007-03-27 15:22:07 +04:00
ret = tdb_store_bystring ( tdb_drivers , key , dbuf , TDB_REPLACE ) ;
2000-05-24 10:10:21 +04:00
2001-08-12 21:30:01 +04:00
done :
2000-09-15 04:15:10 +04:00
if ( ret )
DEBUG ( 0 , ( " add_a_printer_driver_3: Adding driver with key %s failed. \n " , key ) ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2000-05-24 10:10:21 +04:00
return ret ;
2000-02-07 19:17:59 +03:00
}
2000-05-12 18:28:46 +04:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-16 17:52:31 +03:00
2009-11-27 03:49:08 +03:00
static uint32_t add_a_printer_driver_8 ( struct spoolss_DriverInfo8 * driver )
2000-05-12 18:28:46 +04:00
{
2009-11-27 03:49:08 +03:00
TALLOC_CTX * mem_ctx = talloc_new ( talloc_tos ( ) ) ;
2009-11-16 17:52:31 +03:00
struct spoolss_AddDriverInfo3 info3 ;
2009-11-27 03:49:08 +03:00
uint32_t ret ;
2009-11-16 17:52:31 +03:00
2009-11-27 03:49:08 +03:00
convert_level_8_to_level3 ( mem_ctx , & info3 , driver ) ;
2000-05-24 10:10:21 +04:00
2009-11-27 03:49:08 +03:00
ret = add_a_printer_driver_3 ( & info3 ) ;
talloc_free ( mem_ctx ) ;
2000-05-12 18:28:46 +04:00
2009-11-27 03:49:08 +03:00
return ret ;
}
2000-05-12 18:28:46 +04:00
2000-05-24 10:10:21 +04:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
static WERROR get_a_printer_driver_3_default ( TALLOC_CTX * mem_ctx ,
struct spoolss_DriverInfo3 * info ,
const char * driver , const char * arch )
{
info - > driver_name = talloc_strdup ( mem_ctx , driver ) ;
if ( ! info - > driver_name ) {
2001-09-04 11:13:01 +04:00
return WERR_NOMEM ;
2009-11-17 14:54:02 +03:00
}
2000-06-02 01:00:37 +04:00
2009-11-17 14:54:02 +03:00
info - > default_datatype = talloc_strdup ( mem_ctx , " RAW " ) ;
if ( ! info - > default_datatype ) {
return WERR_NOMEM ;
}
2000-05-24 10:10:21 +04:00
2009-11-17 14:54:02 +03:00
info - > driver_path = talloc_strdup ( mem_ctx , " " ) ;
info - > data_file = talloc_strdup ( mem_ctx , " " ) ;
info - > config_file = talloc_strdup ( mem_ctx , " " ) ;
info - > help_file = talloc_strdup ( mem_ctx , " " ) ;
if ( ! info - > driver_path | | ! info - > data_file | | ! info - > config_file | | ! info - > help_file ) {
2006-06-28 04:50:14 +04:00
return WERR_NOMEM ;
}
2009-02-09 14:40:12 +03:00
2001-09-04 11:13:01 +04:00
return WERR_OK ;
2000-05-12 18:28:46 +04:00
}
2000-02-07 19:17:59 +03:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-17 14:54:02 +03:00
static WERROR get_a_printer_driver_3 ( TALLOC_CTX * mem_ctx ,
struct spoolss_DriverInfo3 * driver ,
2009-03-20 17:14:09 +03:00
const char * drivername , const char * arch ,
uint32_t version )
2000-02-07 19:17:59 +03:00
{
2007-03-27 15:22:07 +04:00
TDB_DATA dbuf ;
2003-04-23 17:27:35 +04:00
const char * architecture ;
2000-05-24 10:10:21 +04:00
int len = 0 ;
int i ;
2007-11-22 00:56:36 +03:00
char * key = NULL ;
2009-11-17 14:54:02 +03:00
fstring name , driverpath , environment , datafile , configfile , helpfile , monitorname , defaultdatatype ;
2000-04-05 23:48:06 +04:00
2003-04-23 17:27:35 +04:00
architecture = get_short_archi ( arch ) ;
2006-06-20 01:36:19 +04:00
if ( ! architecture ) {
2005-03-08 01:06:25 +03:00
return WERR_UNKNOWN_PRINTER_DRIVER ;
2006-06-20 01:36:19 +04:00
}
2007-11-22 00:56:36 +03:00
2003-08-15 00:51:41 +04:00
/* Windows 4.0 (i.e. win9x) should always use a version of 0 */
2007-11-22 00:56:36 +03:00
2003-08-16 05:40:38 +04:00
if ( strcmp ( architecture , SPL_ARCH_WIN40 ) = = 0 )
2003-08-15 00:51:41 +04:00
version = 0 ;
2000-07-25 17:15:16 +04:00
2002-07-15 14:35:28 +04:00
DEBUG ( 8 , ( " get_a_printer_driver_3: [%s%s/%d/%s] \n " , DRIVERS_PREFIX , architecture , version , drivername ) ) ;
2000-07-25 17:15:16 +04:00
2007-11-22 00:56:36 +03:00
if ( asprintf ( & key , " %s%s/%d/%s " , DRIVERS_PREFIX ,
architecture , version , drivername ) < 0 ) {
return WERR_NOMEM ;
}
2007-03-27 15:22:07 +04:00
dbuf = tdb_fetch_bystring ( tdb_drivers , key ) ;
2007-11-22 00:56:36 +03:00
if ( ! dbuf . dptr ) {
SAFE_FREE ( key ) ;
2002-09-25 19:19:00 +04:00
return WERR_UNKNOWN_PRINTER_DRIVER ;
2007-11-22 00:56:36 +03:00
}
2002-07-15 14:35:28 +04:00
2000-11-15 00:56:32 +03:00
len + = tdb_unpack ( dbuf . dptr , dbuf . dsize , " dffffffff " ,
2009-11-17 14:54:02 +03:00
& driver - > version ,
name ,
environment ,
driverpath ,
datafile ,
configfile ,
helpfile ,
monitorname ,
defaultdatatype ) ;
driver - > driver_name = talloc_strdup ( mem_ctx , name ) ;
driver - > architecture = talloc_strdup ( mem_ctx , environment ) ;
driver - > driver_path = talloc_strdup ( mem_ctx , driverpath ) ;
driver - > data_file = talloc_strdup ( mem_ctx , datafile ) ;
driver - > config_file = talloc_strdup ( mem_ctx , configfile ) ;
driver - > help_file = talloc_strdup ( mem_ctx , helpfile ) ;
driver - > monitor_name = talloc_strdup ( mem_ctx , monitorname ) ;
driver - > default_datatype = talloc_strdup ( mem_ctx , defaultdatatype ) ;
2000-05-24 10:10:21 +04:00
i = 0 ;
2009-11-17 14:54:02 +03:00
2002-11-23 05:52:36 +03:00
while ( len < dbuf . dsize ) {
2009-11-17 14:54:02 +03:00
fstring file ;
driver - > dependent_files = talloc_realloc ( mem_ctx , driver - > dependent_files , const char * , i + 2 ) ;
if ( ! driver - > dependent_files ) {
2001-08-12 21:30:01 +04:00
DEBUG ( 0 , ( " get_a_printer_driver_3: failed to enlarge buffer! \n " ) ) ;
2000-06-02 01:00:37 +04:00
break ;
2001-08-12 21:30:01 +04:00
}
2000-06-02 01:00:37 +04:00
2000-11-15 00:56:32 +03:00
len + = tdb_unpack ( dbuf . dptr + len , dbuf . dsize - len , " f " ,
2009-11-17 14:54:02 +03:00
& file ) ;
driver - > dependent_files [ i ] = talloc_strdup ( mem_ctx , file ) ;
2000-05-24 10:10:21 +04:00
i + + ;
2000-04-05 23:48:06 +04:00
}
2007-11-22 00:56:36 +03:00
2009-11-17 14:54:02 +03:00
if ( driver - > dependent_files )
driver - > dependent_files [ i ] = NULL ;
2000-04-05 23:48:06 +04:00
2001-09-17 13:25:57 +04:00
SAFE_FREE ( dbuf . dptr ) ;
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
2000-05-29 02:50:30 +04:00
2002-11-23 05:52:36 +03:00
if ( len ! = dbuf . dsize ) {
2009-11-17 14:54:02 +03:00
return get_a_printer_driver_3_default ( mem_ctx , driver , drivername , arch ) ;
2006-06-28 04:50:14 +04:00
}
2000-05-29 02:50:30 +04:00
2001-09-03 12:50:59 +04:00
return WERR_OK ;
2000-02-07 19:17:59 +03:00
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-03-29 13:35:51 +04:00
int pack_devicemode ( NT_DEVICEMODE * nt_devmode , uint8 * buf , int buflen )
2000-02-07 19:17:59 +03:00
{
2000-05-24 10:10:21 +04:00
int len = 0 ;
len + = tdb_pack ( buf + len , buflen - len , " p " , nt_devmode ) ;
2003-02-25 23:53:53 +03:00
if ( ! nt_devmode )
return len ;
2000-05-24 10:10:21 +04:00
2000-06-27 02:08:20 +04:00
len + = tdb_pack ( buf + len , buflen - len , " ffwwwwwwwwwwwwwwwwwwddddddddddddddp " ,
nt_devmode - > devicename ,
2000-06-04 06:29:00 +04:00
nt_devmode - > formname ,
nt_devmode - > specversion ,
nt_devmode - > driverversion ,
nt_devmode - > size ,
nt_devmode - > driverextra ,
nt_devmode - > orientation ,
nt_devmode - > papersize ,
nt_devmode - > paperlength ,
nt_devmode - > paperwidth ,
nt_devmode - > scale ,
nt_devmode - > copies ,
nt_devmode - > defaultsource ,
nt_devmode - > printquality ,
nt_devmode - > color ,
nt_devmode - > duplex ,
nt_devmode - > yresolution ,
nt_devmode - > ttoption ,
nt_devmode - > collate ,
nt_devmode - > logpixels ,
2009-02-09 14:40:12 +03:00
2000-06-04 06:29:00 +04:00
nt_devmode - > fields ,
nt_devmode - > bitsperpel ,
nt_devmode - > pelswidth ,
nt_devmode - > pelsheight ,
nt_devmode - > displayflags ,
nt_devmode - > displayfrequency ,
nt_devmode - > icmmethod ,
nt_devmode - > icmintent ,
nt_devmode - > mediatype ,
nt_devmode - > dithertype ,
nt_devmode - > reserved1 ,
nt_devmode - > reserved2 ,
nt_devmode - > panningwidth ,
nt_devmode - > panningheight ,
2005-06-25 00:25:18 +04:00
nt_devmode - > nt_dev_private ) ;
2000-06-04 06:29:00 +04:00
2005-06-25 00:25:18 +04:00
if ( nt_devmode - > nt_dev_private ) {
2000-05-24 10:10:21 +04:00
len + = tdb_pack ( buf + len , buflen - len , " B " ,
nt_devmode - > driverextra ,
2005-06-25 00:25:18 +04:00
nt_devmode - > nt_dev_private ) ;
2000-02-07 19:17:59 +03:00
}
2000-05-24 10:10:21 +04:00
2000-06-04 06:29:00 +04:00
DEBUG ( 8 , ( " Packed devicemode [%s] \n " , nt_devmode - > formname ) ) ;
2000-05-24 10:10:21 +04:00
return len ;
2000-02-07 19:17:59 +03:00
}
/****************************************************************************
2002-08-17 20:31:24 +04:00
Pack all values in all printer keys
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-22 00:56:36 +03:00
2007-03-29 13:35:51 +04:00
static int pack_values ( NT_PRINTER_DATA * data , uint8 * buf , int buflen )
2000-02-07 19:17:59 +03:00
{
2002-08-17 20:31:24 +04:00
int len = 0 ;
int i , j ;
2009-03-24 00:27:59 +03:00
struct regval_blob * val ;
2009-03-23 20:14:17 +03:00
struct regval_ctr * val_ctr ;
2007-11-22 00:56:36 +03:00
char * path = NULL ;
2002-08-17 20:31:24 +04:00
int num_values ;
2000-05-24 10:10:21 +04:00
2002-08-17 20:31:24 +04:00
if ( ! data )
return 0 ;
/* loop over all keys */
2007-11-22 00:56:36 +03:00
for ( i = 0 ; i < data - > num_keys ; i + + ) {
2005-08-29 18:55:40 +04:00
val_ctr = data - > keys [ i ] . values ;
2002-08-17 20:31:24 +04:00
num_values = regval_ctr_numvals ( val_ctr ) ;
2005-07-02 05:23:21 +04:00
/* pack the keyname followed by a empty value */
2007-11-22 00:56:36 +03:00
len + = tdb_pack ( buf + len , buflen - len , " pPdB " ,
2005-07-02 05:23:21 +04:00
& data - > keys [ i ] . name ,
2007-11-22 00:56:36 +03:00
data - > keys [ i ] . name ,
2005-07-02 05:23:21 +04:00
REG_NONE ,
0 ,
NULL ) ;
2007-11-22 00:56:36 +03:00
2005-07-02 05:23:21 +04:00
/* now loop over all values */
2007-11-22 00:56:36 +03:00
2003-02-25 23:53:53 +03:00
for ( j = 0 ; j < num_values ; j + + ) {
2002-08-17 20:31:24 +04:00
/* pathname should be stored as <key>\<value> */
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
val = regval_ctr_specific_value ( val_ctr , j ) ;
2007-11-22 00:56:36 +03:00
if ( asprintf ( & path , " %s \\ %s " ,
data - > keys [ i ] . name ,
regval_name ( val ) ) < 0 ) {
return - 1 ;
}
2002-08-17 20:31:24 +04:00
len + = tdb_pack ( buf + len , buflen - len , " pPdB " ,
val ,
path ,
regval_type ( val ) ,
regval_size ( val ) ,
regval_data_p ( val ) ) ;
2005-08-29 18:55:40 +04:00
DEBUG ( 8 , ( " specific: [%s], len: %d \n " , regval_name ( val ) , regval_size ( val ) ) ) ;
2007-11-22 00:56:36 +03:00
SAFE_FREE ( path ) ;
2002-08-17 20:31:24 +04:00
}
2007-11-22 00:56:36 +03:00
2000-02-07 19:17:59 +03:00
}
2000-05-24 10:10:21 +04:00
2002-08-17 20:31:24 +04:00
/* terminator */
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
len + = tdb_pack ( buf + len , buflen - len , " p " , NULL ) ;
2000-05-24 10:10:21 +04:00
return len ;
2000-02-07 19:17:59 +03:00
}
2000-05-02 19:31:55 +04:00
/****************************************************************************
2002-02-09 07:10:24 +03:00
Delete a printer - this just deletes the printer info file , any open
handles are not affected .
2000-05-02 19:31:55 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-02-09 07:10:24 +03:00
2005-01-05 19:20:35 +03:00
uint32 del_a_printer ( const char * sharename )
2000-05-02 19:31:55 +04:00
{
2000-05-24 10:10:21 +04:00
TDB_DATA kbuf ;
2007-11-22 00:56:36 +03:00
char * printdb_path = NULL ;
TALLOC_CTX * ctx = talloc_tos ( ) ;
2000-05-24 10:10:21 +04:00
2007-11-22 00:56:36 +03:00
kbuf = make_printer_tdbkey ( ctx , sharename ) ;
2005-01-05 19:20:35 +03:00
tdb_delete ( tdb_printers , kbuf ) ;
2000-05-24 10:10:21 +04:00
2007-11-22 00:56:36 +03:00
kbuf = make_printers_secdesc_tdbkey ( ctx , sharename ) ;
2001-07-09 22:19:04 +04:00
tdb_delete ( tdb_printers , kbuf ) ;
2005-01-05 19:20:35 +03:00
close_all_print_db ( ) ;
2009-06-23 22:27:05 +04:00
if ( geteuid ( ) = = sec_initial_uid ( ) ) {
2007-11-22 00:56:36 +03:00
if ( asprintf ( & printdb_path , " %s%s.tdb " ,
2009-01-16 02:48:29 +03:00
cache_path ( " printing/ " ) ,
2007-11-22 00:56:36 +03:00
sharename ) < 0 ) {
return ( uint32 ) - 1 ;
}
2005-01-05 19:20:35 +03:00
unlink ( printdb_path ) ;
2007-11-22 00:56:36 +03:00
SAFE_FREE ( printdb_path ) ;
2005-01-05 19:20:35 +03:00
}
2000-05-02 19:31:55 +04:00
return 0 ;
}
2000-02-07 19:17:59 +03:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-09-04 11:13:01 +04:00
static WERROR update_a_printer_2 ( NT_PRINTER_INFO_LEVEL_2 * info )
2000-02-07 19:17:59 +03:00
{
2007-03-29 13:35:51 +04:00
uint8 * buf ;
2001-09-04 11:13:01 +04:00
int buflen , len ;
2007-11-22 00:56:36 +03:00
int retlen ;
2001-09-04 11:13:01 +04:00
WERROR ret ;
2000-05-24 10:10:21 +04:00
TDB_DATA kbuf , dbuf ;
2009-02-09 14:40:12 +03:00
2000-11-15 00:56:32 +03:00
/*
2000-02-07 19:17:59 +03:00
* in addprinter : no servername and the printer is the name
* in setprinter : servername is \ \ server
* and printer is \ \ server \ \ printer
*
* Samba manages only local printers .
2004-05-19 21:17:33 +04:00
* we currently don ' t support things like i
* path = \ \ other_server \ printer
*
* We only store the printername , not \ \ server \ printername
2000-02-07 19:17:59 +03:00
*/
2000-07-22 04:48:29 +04:00
2004-05-19 21:17:33 +04:00
if ( info - > servername [ 0 ] ! = ' \0 ' ) {
2000-02-07 19:17:59 +03:00
trim_string ( info - > printername , info - > servername , NULL ) ;
2003-09-05 23:59:55 +04:00
trim_char ( info - > printername , ' \\ ' , ' \0 ' ) ;
2000-02-07 19:17:59 +03:00
info - > servername [ 0 ] = ' \0 ' ;
}
2000-05-24 10:10:21 +04:00
/*
* JFM : one day I ' ll forget .
* below that ' s info - > portname because that ' s the SAMBA sharename
* and I made NT ' thinks ' it ' s the portname
* the info - > sharename is the thing you can name when you add a printer
* that ' s the short - name when you create shared printer for 95 / 98
* So I ' ve made a limitation in SAMBA : you can only have 1 printer model
* behind a SAMBA share .
*/
buf = NULL ;
buflen = 0 ;
2007-11-22 00:56:36 +03:00
again :
2000-05-24 10:10:21 +04:00
len = 0 ;
2000-08-01 00:41:51 +04:00
len + = tdb_pack ( buf + len , buflen - len , " dddddddddddfffffPfffff " ,
2000-05-24 10:10:21 +04:00
info - > attributes ,
info - > priority ,
info - > default_priority ,
info - > starttime ,
info - > untiltime ,
info - > status ,
info - > cjobs ,
info - > averageppm ,
info - > changeid ,
info - > c_setprinter ,
info - > setuptime ,
info - > servername ,
info - > printername ,
info - > sharename ,
info - > portname ,
info - > drivername ,
2000-07-22 04:48:29 +04:00
info - > comment ,
2000-05-24 10:10:21 +04:00
info - > location ,
info - > sepfile ,
info - > printprocessor ,
info - > datatype ,
info - > parameters ) ;
2001-01-30 00:34:08 +03:00
len + = pack_devicemode ( info - > devmode , buf + len , buflen - len ) ;
2007-11-22 00:56:36 +03:00
retlen = pack_values ( info - > data , buf + len , buflen - len ) ;
if ( retlen = = - 1 ) {
ret = WERR_NOMEM ;
goto done ;
}
len + = retlen ;
2000-05-24 10:10:21 +04:00
if ( buflen ! = len ) {
2007-03-29 13:35:51 +04:00
buf = ( uint8 * ) SMB_REALLOC ( buf , len ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! buf ) {
2001-08-12 21:30:01 +04:00
DEBUG ( 0 , ( " update_a_printer_2: failed to enlarge buffer! \n " ) ) ;
2001-09-04 11:13:01 +04:00
ret = WERR_NOMEM ;
2001-08-12 21:30:01 +04:00
goto done ;
}
2000-05-24 10:10:21 +04:00
buflen = len ;
goto again ;
2000-02-07 19:17:59 +03:00
}
2007-11-22 00:56:36 +03:00
kbuf = make_printer_tdbkey ( talloc_tos ( ) , info - > sharename ) ;
2005-06-30 23:33:47 +04:00
2000-05-24 10:10:21 +04:00
dbuf . dptr = buf ;
dbuf . dsize = len ;
2000-02-07 19:17:59 +03:00
2001-09-04 11:13:01 +04:00
ret = ( tdb_store ( tdb_printers , kbuf , dbuf , TDB_REPLACE ) = = 0 ? WERR_OK : WERR_NOMEM ) ;
2000-02-07 19:17:59 +03:00
2001-08-12 21:30:01 +04:00
done :
2001-09-04 11:13:01 +04:00
if ( ! W_ERROR_IS_OK ( ret ) )
2000-07-12 18:10:40 +04:00
DEBUG ( 8 , ( " error updating printer to tdb on disk \n " ) ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2000-02-07 19:17:59 +03:00
2000-11-15 00:56:32 +03:00
DEBUG ( 8 , ( " packed printer [%s] with driver [%s] portname=[%s] len=%d \n " ,
2000-07-25 17:15:16 +04:00
info - > sharename , info - > drivername , info - > portname , len ) ) ;
2000-06-04 06:29:00 +04:00
2000-05-24 10:10:21 +04:00
return ret ;
2000-02-07 19:17:59 +03:00
}
2010-05-05 19:44:22 +04:00
/****************************************************************************
Create and allocate a default devicemode .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
WERROR spoolss_create_default_devmode ( TALLOC_CTX * mem_ctx ,
const char * devicename ,
struct spoolss_DeviceMode * * devmode )
{
struct spoolss_DeviceMode * dm ;
char * dname ;
dm = talloc_zero ( mem_ctx , struct spoolss_DeviceMode ) ;
if ( dm = = NULL ) {
return WERR_NOMEM ;
}
dname = talloc_asprintf ( dm , " %s " , devicename ) ;
if ( dname = = NULL ) {
return WERR_NOMEM ;
}
if ( strlen ( dname ) > MAXDEVICENAME ) {
dname [ MAXDEVICENAME ] = ' \0 ' ;
}
dm - > devicename = dname ;
dm - > formname = talloc_strdup ( dm , " Letter " ) ;
if ( dm - > formname = = NULL ) {
return WERR_NOMEM ;
}
dm - > specversion = DMSPEC_NT4_AND_ABOVE ;
dm - > driverversion = 0x0400 ;
dm - > size = 0x00DC ;
dm - > __driverextra_length = 0 ;
dm - > fields = DEVMODE_FORMNAME |
DEVMODE_TTOPTION |
DEVMODE_PRINTQUALITY |
DEVMODE_DEFAULTSOURCE |
DEVMODE_COPIES |
DEVMODE_SCALE |
DEVMODE_PAPERSIZE |
DEVMODE_ORIENTATION ;
dm - > orientation = DMORIENT_PORTRAIT ;
dm - > papersize = DMPAPER_LETTER ;
dm - > paperlength = 0 ;
dm - > paperwidth = 0 ;
dm - > scale = 0x64 ;
dm - > copies = 1 ;
dm - > defaultsource = DMBIN_FORMSOURCE ;
dm - > printquality = DMRES_HIGH ; /* 0x0258 */
dm - > color = DMRES_MONOCHROME ;
dm - > duplex = DMDUP_SIMPLEX ;
dm - > yresolution = 0 ;
dm - > ttoption = DMTT_SUBDEV ;
dm - > collate = DMCOLLATE_FALSE ;
dm - > icmmethod = 0 ;
dm - > icmintent = 0 ;
dm - > mediatype = 0 ;
dm - > dithertype = 0 ;
dm - > logpixels = 0 ;
dm - > bitsperpel = 0 ;
dm - > pelswidth = 0 ;
dm - > pelsheight = 0 ;
dm - > displayflags = 0 ;
dm - > displayfrequency = 0 ;
dm - > reserved1 = 0 ;
dm - > reserved2 = 0 ;
dm - > panningwidth = 0 ;
dm - > panningheight = 0 ;
dm - > driverextra_data . data = NULL ;
dm - > driverextra_data . length = 0 ;
* devmode = dm ;
return WERR_OK ;
}
2000-02-07 19:17:59 +03:00
2010-05-04 14:07:10 +04:00
WERROR spoolss_create_default_secdesc ( TALLOC_CTX * mem_ctx ,
struct spoolss_security_descriptor * * secdesc )
{
SEC_ACE ace [ 5 ] ; /* max number of ace entries */
int i = 0 ;
uint32_t sa ;
SEC_ACL * psa = NULL ;
SEC_DESC * psd = NULL ;
DOM_SID adm_sid ;
size_t sd_size ;
/* Create an ACE where Everyone is allowed to print */
sa = PRINTER_ACE_PRINT ;
init_sec_ace ( & ace [ i + + ] , & global_sid_World , SEC_ACE_TYPE_ACCESS_ALLOWED ,
sa , SEC_ACE_FLAG_CONTAINER_INHERIT ) ;
/* Add the domain admins group if we are a DC */
if ( IS_DC ) {
DOM_SID domadmins_sid ;
sid_compose ( & domadmins_sid , get_global_sam_sid ( ) ,
DOMAIN_GROUP_RID_ADMINS ) ;
sa = PRINTER_ACE_FULL_CONTROL ;
init_sec_ace ( & ace [ i + + ] , & domadmins_sid ,
SEC_ACE_TYPE_ACCESS_ALLOWED , sa ,
SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY ) ;
init_sec_ace ( & ace [ i + + ] , & domadmins_sid , SEC_ACE_TYPE_ACCESS_ALLOWED ,
sa , SEC_ACE_FLAG_CONTAINER_INHERIT ) ;
}
else if ( secrets_fetch_domain_sid ( lp_workgroup ( ) , & adm_sid ) ) {
sid_append_rid ( & adm_sid , DOMAIN_USER_RID_ADMIN ) ;
sa = PRINTER_ACE_FULL_CONTROL ;
init_sec_ace ( & ace [ i + + ] , & adm_sid ,
SEC_ACE_TYPE_ACCESS_ALLOWED , sa ,
SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY ) ;
init_sec_ace ( & ace [ i + + ] , & adm_sid , SEC_ACE_TYPE_ACCESS_ALLOWED ,
sa , SEC_ACE_FLAG_CONTAINER_INHERIT ) ;
}
/* add BUILTIN\Administrators as FULL CONTROL */
sa = PRINTER_ACE_FULL_CONTROL ;
init_sec_ace ( & ace [ i + + ] , & global_sid_Builtin_Administrators ,
SEC_ACE_TYPE_ACCESS_ALLOWED , sa ,
SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY ) ;
init_sec_ace ( & ace [ i + + ] , & global_sid_Builtin_Administrators ,
SEC_ACE_TYPE_ACCESS_ALLOWED ,
sa , SEC_ACE_FLAG_CONTAINER_INHERIT ) ;
/* Make the security descriptor owned by the BUILTIN\Administrators */
/* The ACL revision number in rpc_secdesc.h differs from the one
created by NT when setting ACE entries in printer
descriptors . NT4 complains about the property being edited by a
NT5 machine . */
if ( ( psa = make_sec_acl ( mem_ctx , NT4_ACL_REVISION , i , ace ) ) ! = NULL ) {
psd = make_sec_desc ( mem_ctx ,
SEC_DESC_REVISION ,
SEC_DESC_SELF_RELATIVE ,
& global_sid_Builtin_Administrators ,
& global_sid_Builtin_Administrators ,
NULL ,
psa ,
& sd_size ) ;
}
if ( psd = = NULL ) {
DEBUG ( 0 , ( " construct_default_printer_sd: Failed to make SEC_DESC. \n " ) ) ;
return WERR_NOMEM ;
}
DEBUG ( 4 , ( " construct_default_printer_sdb: size = %u. \n " ,
( unsigned int ) sd_size ) ) ;
* secdesc = psd ;
return WERR_OK ;
}
2000-06-01 21:01:34 +04:00
/****************************************************************************
Malloc and return an NT devicemode .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-06-27 02:08:20 +04:00
NT_DEVICEMODE * construct_nt_devicemode ( const fstring default_devicename )
2000-06-01 21:01:34 +04:00
{
2003-04-14 07:50:38 +04:00
char adevice [ MAXDEVICENAME ] ;
2004-12-07 21:25:53 +03:00
NT_DEVICEMODE * nt_devmode = SMB_MALLOC_P ( NT_DEVICEMODE ) ;
2000-06-01 21:01:34 +04:00
if ( nt_devmode = = NULL ) {
DEBUG ( 0 , ( " construct_nt_devicemode: malloc fail. \n " ) ) ;
return NULL ;
}
ZERO_STRUCTP ( nt_devmode ) ;
2004-11-13 01:15:55 +03:00
slprintf ( adevice , sizeof ( adevice ) , " %s " , default_devicename ) ;
2009-02-09 14:40:12 +03:00
fstrcpy ( nt_devmode - > devicename , adevice ) ;
2000-06-06 05:47:11 +04:00
fstrcpy ( nt_devmode - > formname , " Letter " ) ;
2000-06-01 21:01:34 +04:00
2009-08-10 13:25:52 +04:00
nt_devmode - > specversion = DMSPEC_NT4_AND_ABOVE ;
2000-06-01 21:01:34 +04:00
nt_devmode - > driverversion = 0x0400 ;
nt_devmode - > size = 0x00DC ;
nt_devmode - > driverextra = 0x0000 ;
2009-08-10 13:25:52 +04:00
nt_devmode - > fields = DEVMODE_FORMNAME |
DEVMODE_TTOPTION |
DEVMODE_PRINTQUALITY |
DEVMODE_DEFAULTSOURCE |
DEVMODE_COPIES |
DEVMODE_SCALE |
DEVMODE_PAPERSIZE |
DEVMODE_ORIENTATION ;
nt_devmode - > orientation = DMORIENT_PORTRAIT ;
nt_devmode - > papersize = DMPAPER_LETTER ;
2000-06-01 21:01:34 +04:00
nt_devmode - > paperlength = 0 ;
nt_devmode - > paperwidth = 0 ;
nt_devmode - > scale = 0x64 ;
2001-12-03 05:30:27 +03:00
nt_devmode - > copies = 1 ;
2009-08-10 13:25:52 +04:00
nt_devmode - > defaultsource = DMBIN_FORMSOURCE ;
nt_devmode - > printquality = DMRES_HIGH ; /* 0x0258 */
nt_devmode - > color = DMRES_MONOCHROME ;
nt_devmode - > duplex = DMDUP_SIMPLEX ;
2000-06-01 21:01:34 +04:00
nt_devmode - > yresolution = 0 ;
2009-08-10 13:25:52 +04:00
nt_devmode - > ttoption = DMTT_SUBDEV ;
nt_devmode - > collate = DMCOLLATE_FALSE ;
2000-06-01 21:01:34 +04:00
nt_devmode - > icmmethod = 0 ;
nt_devmode - > icmintent = 0 ;
nt_devmode - > mediatype = 0 ;
nt_devmode - > dithertype = 0 ;
2004-04-13 18:39:48 +04:00
/* non utilisés par un driver d'imprimante */
2000-06-01 21:01:34 +04:00
nt_devmode - > logpixels = 0 ;
nt_devmode - > bitsperpel = 0 ;
nt_devmode - > pelswidth = 0 ;
nt_devmode - > pelsheight = 0 ;
nt_devmode - > displayflags = 0 ;
nt_devmode - > displayfrequency = 0 ;
nt_devmode - > reserved1 = 0 ;
nt_devmode - > reserved2 = 0 ;
nt_devmode - > panningwidth = 0 ;
nt_devmode - > panningheight = 0 ;
2009-02-09 14:40:12 +03:00
2005-06-25 00:25:18 +04:00
nt_devmode - > nt_dev_private = NULL ;
2000-06-01 21:01:34 +04:00
return nt_devmode ;
}
2000-04-05 23:48:06 +04:00
/****************************************************************************
Clean up and deallocate a ( maybe partially ) allocated NT_DEVICEMODE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-06-01 21:01:34 +04:00
void free_nt_devicemode ( NT_DEVICEMODE * * devmode_ptr )
2000-04-05 23:48:06 +04:00
{
NT_DEVICEMODE * nt_devmode = * devmode_ptr ;
if ( nt_devmode = = NULL )
return ;
2000-04-07 02:48:53 +04:00
DEBUG ( 106 , ( " free_nt_devicemode: deleting DEVMODE \n " ) ) ;
2005-06-25 00:25:18 +04:00
SAFE_FREE ( nt_devmode - > nt_dev_private ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( * devmode_ptr ) ;
2000-04-05 23:48:06 +04:00
}
/****************************************************************************
Clean up and deallocate a ( maybe partially ) allocated NT_PRINTER_INFO_LEVEL_2 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-08-29 18:55:40 +04:00
2000-04-05 23:48:06 +04:00
static void free_nt_printer_info_level_2 ( NT_PRINTER_INFO_LEVEL_2 * * info_ptr )
{
NT_PRINTER_INFO_LEVEL_2 * info = * info_ptr ;
2002-08-17 20:31:24 +04:00
if ( ! info )
2000-04-05 23:48:06 +04:00
return ;
free_nt_devicemode ( & info - > devmode ) ;
2005-08-29 18:55:40 +04:00
TALLOC_FREE ( * info_ptr ) ;
2000-04-05 23:48:06 +04:00
}
2000-05-24 10:10:21 +04:00
2000-02-07 19:17:59 +03:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-03-29 13:35:51 +04:00
int unpack_devicemode ( NT_DEVICEMODE * * nt_devmode , const uint8 * buf , int buflen )
2000-02-07 19:17:59 +03:00
{
2000-05-24 10:10:21 +04:00
int len = 0 ;
2000-07-27 11:59:39 +04:00
int extra_len = 0 ;
2000-05-24 10:10:21 +04:00
NT_DEVICEMODE devmode ;
2009-02-09 14:40:12 +03:00
2000-05-24 10:10:21 +04:00
ZERO_STRUCT ( devmode ) ;
2000-05-27 13:53:11 +04:00
len + = tdb_unpack ( buf + len , buflen - len , " p " , nt_devmode ) ;
2000-05-24 10:10:21 +04:00
if ( ! * nt_devmode ) return len ;
2000-06-27 02:08:20 +04:00
len + = tdb_unpack ( buf + len , buflen - len , " ffwwwwwwwwwwwwwwwwwwddddddddddddddp " ,
devmode . devicename ,
2000-06-04 06:29:00 +04:00
devmode . formname ,
& devmode . specversion ,
& devmode . driverversion ,
& devmode . size ,
& devmode . driverextra ,
& devmode . orientation ,
& devmode . papersize ,
& devmode . paperlength ,
& devmode . paperwidth ,
& devmode . scale ,
& devmode . copies ,
& devmode . defaultsource ,
& devmode . printquality ,
& devmode . color ,
& devmode . duplex ,
& devmode . yresolution ,
& devmode . ttoption ,
& devmode . collate ,
& devmode . logpixels ,
2009-02-09 14:40:12 +03:00
2000-06-04 06:29:00 +04:00
& devmode . fields ,
& devmode . bitsperpel ,
& devmode . pelswidth ,
& devmode . pelsheight ,
& devmode . displayflags ,
& devmode . displayfrequency ,
& devmode . icmmethod ,
& devmode . icmintent ,
& devmode . mediatype ,
& devmode . dithertype ,
& devmode . reserved1 ,
& devmode . reserved2 ,
& devmode . panningwidth ,
& devmode . panningheight ,
2005-06-25 00:25:18 +04:00
& devmode . nt_dev_private ) ;
2009-02-09 14:40:12 +03:00
2005-06-25 00:25:18 +04:00
if ( devmode . nt_dev_private ) {
2000-07-27 11:59:39 +04:00
/* the len in tdb_unpack is an int value and
2001-01-30 00:34:08 +03:00
* devmode . driverextra is only a short
2000-07-27 11:59:39 +04:00
*/
2005-06-25 00:25:18 +04:00
len + = tdb_unpack ( buf + len , buflen - len , " B " , & extra_len , & devmode . nt_dev_private ) ;
2000-07-27 11:59:39 +04:00
devmode . driverextra = ( uint16 ) extra_len ;
2009-02-09 14:40:12 +03:00
2000-12-22 02:38:47 +03:00
/* check to catch an invalid TDB entry so we don't segfault */
if ( devmode . driverextra = = 0 ) {
2005-06-25 00:25:18 +04:00
devmode . nt_dev_private = NULL ;
2000-12-22 02:38:47 +03:00
}
2000-07-27 11:59:39 +04:00
}
2000-02-07 19:17:59 +03:00
2000-05-24 10:10:21 +04:00
* nt_devmode = ( NT_DEVICEMODE * ) memdup ( & devmode , sizeof ( devmode ) ) ;
2006-06-28 04:50:14 +04:00
if ( ! * nt_devmode ) {
SAFE_FREE ( devmode . nt_dev_private ) ;
return - 1 ;
}
2000-02-07 19:17:59 +03:00
2000-07-12 18:10:40 +04:00
DEBUG ( 8 , ( " Unpacked devicemode [%s](%s) \n " , devmode . devicename , devmode . formname ) ) ;
2005-06-25 00:25:18 +04:00
if ( devmode . nt_dev_private )
2000-07-12 18:10:40 +04:00
DEBUG ( 8 , ( " with a private section of %d bytes \n " , devmode . driverextra ) ) ;
2000-06-04 06:29:00 +04:00
2000-05-24 10:10:21 +04:00
return len ;
}
2000-02-07 19:17:59 +03:00
2000-05-24 10:10:21 +04:00
/****************************************************************************
2003-02-25 23:53:53 +03:00
Allocate and initialize a new slot .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
2005-07-02 05:23:21 +04:00
int add_new_printer_key ( NT_PRINTER_DATA * data , const char * name )
2000-05-24 10:10:21 +04:00
{
2002-08-17 20:31:24 +04:00
NT_PRINTER_KEY * d ;
int key_index ;
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
if ( ! name | | ! data )
2002-08-17 20:31:24 +04:00
return - 1 ;
2005-08-29 18:55:40 +04:00
2002-08-17 20:31:24 +04:00
/* allocate another slot in the NT_PRINTER_KEY array */
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
if ( ! ( d = TALLOC_REALLOC_ARRAY ( data , data - > keys , NT_PRINTER_KEY , data - > num_keys + 1 ) ) ) {
2005-07-02 05:23:21 +04:00
DEBUG ( 0 , ( " add_new_printer_key: Realloc() failed! \n " ) ) ;
return - 1 ;
}
data - > keys = d ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
key_index = data - > num_keys ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
/* initialze new key */
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
data - > keys [ key_index ] . name = talloc_strdup ( data , name ) ;
2009-02-09 14:40:12 +03:00
2009-03-23 20:14:17 +03:00
if ( ! ( data - > keys [ key_index ] . values = TALLOC_ZERO_P ( data , struct regval_ctr ) ) )
2005-08-29 18:55:40 +04:00
return - 1 ;
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
data - > num_keys + + ;
2002-08-17 20:31:24 +04:00
DEBUG ( 10 , ( " add_new_printer_key: Inserted new data key [%s] \n " , name ) ) ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
return key_index ;
}
2000-02-07 19:17:59 +03:00
2005-07-03 06:05:01 +04:00
/****************************************************************************
search for a registry key name in the existing printer data
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int delete_printer_key ( NT_PRINTER_DATA * data , const char * name )
{
int i ;
2009-02-09 14:40:12 +03:00
2005-07-03 06:05:01 +04:00
for ( i = 0 ; i < data - > num_keys ; i + + ) {
if ( strequal ( data - > keys [ i ] . name , name ) ) {
2009-02-09 14:40:12 +03:00
2005-07-03 06:05:01 +04:00
/* cleanup memory */
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
TALLOC_FREE ( data - > keys [ i ] . name ) ;
TALLOC_FREE ( data - > keys [ i ] . values ) ;
2009-02-09 14:40:12 +03:00
2005-07-03 06:05:01 +04:00
/* if not the end of the array, move remaining elements down one slot */
2009-02-09 14:40:12 +03:00
2005-07-03 06:05:01 +04:00
data - > num_keys - - ;
if ( data - > num_keys & & ( i < data - > num_keys ) )
memmove ( & data - > keys [ i ] , & data - > keys [ i + 1 ] , sizeof ( NT_PRINTER_KEY ) * ( data - > num_keys - i ) ) ;
2009-02-09 14:40:12 +03:00
2005-07-03 06:05:01 +04:00
break ;
}
}
2009-02-09 14:40:12 +03:00
2005-07-03 06:05:01 +04:00
return data - > num_keys ;
}
2002-08-17 20:31:24 +04:00
/****************************************************************************
search for a registry key name in the existing printer data
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
2003-01-03 11:28:12 +03:00
int lookup_printerkey ( NT_PRINTER_DATA * data , const char * name )
2002-08-17 20:31:24 +04:00
{
int key_index = - 1 ;
int i ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
if ( ! data | | ! name )
return - 1 ;
2000-02-07 19:17:59 +03:00
2002-08-17 20:31:24 +04:00
DEBUG ( 12 , ( " lookup_printerkey: Looking for [%s] \n " , name ) ) ;
2000-05-24 10:10:21 +04:00
2002-08-17 20:31:24 +04:00
/* loop over all existing keys */
2009-02-09 14:40:12 +03:00
2003-02-25 23:53:53 +03:00
for ( i = 0 ; i < data - > num_keys ; i + + ) {
2002-09-25 19:19:00 +04:00
if ( strequal ( data - > keys [ i ] . name , name ) ) {
2002-08-17 20:31:24 +04:00
DEBUG ( 12 , ( " lookup_printerkey: Found [%s]! \n " , name ) ) ;
key_index = i ;
break ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
}
}
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
return key_index ;
}
2002-09-25 19:19:00 +04:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-07-02 05:23:21 +04:00
int get_printer_subkeys ( NT_PRINTER_DATA * data , const char * key , fstring * * subkeys )
2002-09-25 19:19:00 +04:00
{
int i , j ;
int key_len ;
int num_subkeys = 0 ;
char * p ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
fstring * subkeys_ptr = NULL ;
2002-09-25 19:19:00 +04:00
fstring subkeyname ;
2009-02-09 14:40:12 +03:00
2006-06-20 23:06:57 +04:00
* subkeys = NULL ;
2002-09-25 19:19:00 +04:00
if ( ! data )
return 0 ;
2005-07-02 05:23:21 +04:00
if ( ! key )
return - 1 ;
/* special case of asking for the top level printer data registry key names */
if ( strlen ( key ) = = 0 ) {
for ( i = 0 ; i < data - > num_keys ; i + + ) {
2009-02-09 14:40:12 +03:00
2005-07-02 05:23:21 +04:00
/* found a match, so allocate space and copy the name */
2009-02-09 14:40:12 +03:00
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! ( subkeys_ptr = SMB_REALLOC_ARRAY ( subkeys_ptr , fstring , num_subkeys + 2 ) ) ) {
2009-02-09 14:40:12 +03:00
DEBUG ( 0 , ( " get_printer_subkeys: Realloc failed for [%d] entries! \n " ,
2005-07-02 05:23:21 +04:00
num_subkeys + 1 ) ) ;
return - 1 ;
}
2009-02-09 14:40:12 +03:00
2005-07-02 05:23:21 +04:00
fstrcpy ( subkeys_ptr [ num_subkeys ] , data - > keys [ i ] . name ) ;
num_subkeys + + ;
}
goto done ;
}
2009-02-09 14:40:12 +03:00
2005-07-02 05:23:21 +04:00
/* asking for the subkeys of some key */
/* subkey paths are stored in the key name using '\' as the delimiter */
2003-02-25 23:53:53 +03:00
for ( i = 0 ; i < data - > num_keys ; i + + ) {
if ( StrnCaseCmp ( data - > keys [ i ] . name , key , strlen ( key ) ) = = 0 ) {
2009-02-09 14:40:12 +03:00
2005-07-02 05:23:21 +04:00
/* if we found the exact key, then break */
2002-09-25 19:19:00 +04:00
key_len = strlen ( key ) ;
if ( strlen ( data - > keys [ i ] . name ) = = key_len )
2005-07-02 05:23:21 +04:00
break ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/* get subkey path */
p = data - > keys [ i ] . name + key_len ;
if ( * p = = ' \\ ' )
p + + ;
fstrcpy ( subkeyname , p ) ;
if ( ( p = strchr ( subkeyname , ' \\ ' ) ) )
* p = ' \0 ' ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/* don't add a key more than once */
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
for ( j = 0 ; j < num_subkeys ; j + + ) {
if ( strequal ( subkeys_ptr [ j ] , subkeyname ) )
break ;
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
if ( j ! = num_subkeys )
continue ;
/* found a match, so allocate space and copy the name */
2009-02-09 14:40:12 +03:00
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! ( subkeys_ptr = SMB_REALLOC_ARRAY ( subkeys_ptr , fstring , num_subkeys + 2 ) ) ) {
2009-02-09 14:40:12 +03:00
DEBUG ( 0 , ( " get_printer_subkeys: Realloc failed for [%d] entries! \n " ,
2002-09-25 19:19:00 +04:00
num_subkeys + 1 ) ) ;
return 0 ;
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
fstrcpy ( subkeys_ptr [ num_subkeys ] , subkeyname ) ;
num_subkeys + + ;
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
}
2009-02-09 14:40:12 +03:00
2005-07-02 05:23:21 +04:00
/* return error if the key was not found */
2009-02-09 14:40:12 +03:00
2006-03-13 03:37:57 +03:00
if ( i = = data - > num_keys ) {
SAFE_FREE ( subkeys_ptr ) ;
2005-07-02 05:23:21 +04:00
return - 1 ;
2006-03-13 03:37:57 +03:00
}
2009-02-09 14:40:12 +03:00
2005-07-02 05:23:21 +04:00
done :
/* tag off the end */
2009-02-09 14:40:12 +03:00
2002-11-07 10:13:29 +03:00
if ( num_subkeys )
2003-02-25 23:53:53 +03:00
fstrcpy ( subkeys_ptr [ num_subkeys ] , " " ) ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
* subkeys = subkeys_ptr ;
return num_subkeys ;
}
2002-12-04 20:40:50 +03:00
2003-06-25 23:49:27 +04:00
# ifdef HAVE_ADS
2009-03-23 20:14:17 +03:00
static void map_sz_into_ctr ( struct regval_ctr * ctr , const char * val_name ,
2002-12-05 22:13:40 +03:00
const char * sz )
{
regval_ctr_delvalue ( ctr , val_name ) ;
2009-09-24 17:01:32 +04:00
regval_ctr_addvalue_sz ( ctr , val_name , sz ) ;
2002-12-05 22:13:40 +03:00
}
2009-03-23 20:14:17 +03:00
static void map_dword_into_ctr ( struct regval_ctr * ctr , const char * val_name ,
2002-12-05 22:13:40 +03:00
uint32 dword )
{
regval_ctr_delvalue ( ctr , val_name ) ;
regval_ctr_addvalue ( ctr , val_name , REG_DWORD ,
( char * ) & dword , sizeof ( dword ) ) ;
}
2009-03-23 20:14:17 +03:00
static void map_bool_into_ctr ( struct regval_ctr * ctr , const char * val_name ,
2007-10-19 04:40:25 +04:00
bool b )
2002-12-05 22:13:40 +03:00
{
2003-05-27 11:29:13 +04:00
uint8 bin_bool = ( b ? 1 : 0 ) ;
2002-12-05 22:13:40 +03:00
regval_ctr_delvalue ( ctr , val_name ) ;
2009-02-09 14:40:12 +03:00
regval_ctr_addvalue ( ctr , val_name , REG_BINARY ,
2002-12-05 22:13:40 +03:00
( char * ) & bin_bool , sizeof ( bin_bool ) ) ;
}
2009-03-23 20:14:17 +03:00
static void map_single_multi_sz_into_ctr ( struct regval_ctr * ctr , const char * val_name ,
2002-12-05 22:13:40 +03:00
const char * multi_sz )
{
2009-09-30 17:22:02 +04:00
const char * a [ 2 ] ;
2002-12-05 22:13:40 +03:00
2009-09-30 17:22:02 +04:00
a [ 0 ] = multi_sz ;
a [ 1 ] = NULL ;
2002-12-05 22:13:40 +03:00
regval_ctr_delvalue ( ctr , val_name ) ;
2009-09-30 17:22:02 +04:00
regval_ctr_addvalue_multi_sz ( ctr , val_name , a ) ;
2002-12-05 22:13:40 +03:00
}
2002-12-04 20:40:50 +03:00
/****************************************************************************
* Map the NT_PRINTER_INFO_LEVEL_2 data into DsSpooler keys for publishing .
*
* @ param info2 NT_PRINTER_INFO_LEVEL_2 describing printer - gets modified
2007-10-19 04:40:25 +04:00
* @ return bool indicating success or failure
2002-12-04 20:40:50 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool map_nt_printer_info2_to_dsspooler ( NT_PRINTER_INFO_LEVEL_2 * info2 )
2002-12-04 20:40:50 +03:00
{
2009-03-23 20:14:17 +03:00
struct regval_ctr * ctr = NULL ;
2002-12-04 20:40:50 +03:00
fstring longname ;
2007-11-09 04:25:45 +03:00
const char * dnssuffix ;
2003-01-03 11:28:12 +03:00
char * allocated_string = NULL ;
const char * ascii_str ;
2002-12-04 20:40:50 +03:00
int i ;
2005-08-29 18:55:40 +04:00
if ( ( i = lookup_printerkey ( info2 - > data , SPOOL_DSSPOOLER_KEY ) ) < 0 )
i = add_new_printer_key ( info2 - > data , SPOOL_DSSPOOLER_KEY ) ;
ctr = info2 - > data - > keys [ i ] . values ;
2002-12-04 20:40:50 +03:00
2002-12-05 22:13:40 +03:00
map_sz_into_ctr ( ctr , SPOOL_REG_PRINTERNAME , info2 - > sharename ) ;
map_sz_into_ctr ( ctr , SPOOL_REG_SHORTSERVERNAME , global_myname ( ) ) ;
2002-12-04 20:40:50 +03:00
2007-11-09 04:25:45 +03:00
/* we make the assumption that the netbios name is the same
as the DNS name sinc ethe former will be what we used to
2005-01-27 05:56:18 +03:00
join the domain */
2007-11-09 04:25:45 +03:00
dnssuffix = get_mydnsdomname ( talloc_tos ( ) ) ;
2007-11-09 04:58:55 +03:00
if ( dnssuffix & & * dnssuffix ) {
2005-01-27 05:56:18 +03:00
fstr_sprintf ( longname , " %s.%s " , global_myname ( ) , dnssuffix ) ;
2007-11-09 04:25:45 +03:00
} else {
2005-01-27 05:56:18 +03:00
fstrcpy ( longname , global_myname ( ) ) ;
2007-11-09 04:25:45 +03:00
}
2002-12-05 22:13:40 +03:00
map_sz_into_ctr ( ctr , SPOOL_REG_SERVERNAME , longname ) ;
2008-12-23 23:11:12 +03:00
if ( asprintf ( & allocated_string , " \\ \\ %s \\ %s " , longname , info2 - > sharename ) = = - 1 ) {
return false ;
}
2003-01-03 11:28:12 +03:00
map_sz_into_ctr ( ctr , SPOOL_REG_UNCNAME , allocated_string ) ;
SAFE_FREE ( allocated_string ) ;
2002-12-05 22:13:40 +03:00
map_dword_into_ctr ( ctr , SPOOL_REG_VERSIONNUMBER , 4 ) ;
map_sz_into_ctr ( ctr , SPOOL_REG_DRIVERNAME , info2 - > drivername ) ;
map_sz_into_ctr ( ctr , SPOOL_REG_LOCATION , info2 - > location ) ;
map_sz_into_ctr ( ctr , SPOOL_REG_DESCRIPTION , info2 - > comment ) ;
map_single_multi_sz_into_ctr ( ctr , SPOOL_REG_PORTNAME , info2 - > portname ) ;
map_sz_into_ctr ( ctr , SPOOL_REG_PRINTSEPARATORFILE , info2 - > sepfile ) ;
map_dword_into_ctr ( ctr , SPOOL_REG_PRINTSTARTTIME , info2 - > starttime ) ;
map_dword_into_ctr ( ctr , SPOOL_REG_PRINTENDTIME , info2 - > untiltime ) ;
map_dword_into_ctr ( ctr , SPOOL_REG_PRIORITY , info2 - > priority ) ;
map_bool_into_ctr ( ctr , SPOOL_REG_PRINTKEEPPRINTEDJOBS ,
2009-02-09 14:40:12 +03:00
( info2 - > attributes &
2002-12-13 22:01:27 +03:00
PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS ) ) ;
2002-12-05 22:13:40 +03:00
switch ( info2 - > attributes & 0x3 ) {
case 0 :
ascii_str = SPOOL_REGVAL_PRINTWHILESPOOLING ;
break ;
case 1 :
ascii_str = SPOOL_REGVAL_PRINTAFTERSPOOLED ;
break ;
case 2 :
ascii_str = SPOOL_REGVAL_PRINTDIRECT ;
break ;
default :
ascii_str = " unknown " ;
}
map_sz_into_ctr ( ctr , SPOOL_REG_PRINTSPOOLING , ascii_str ) ;
2002-12-04 20:40:50 +03:00
return True ;
}
2007-02-09 05:12:12 +03:00
/*****************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
static void store_printer_guid ( NT_PRINTER_INFO_LEVEL_2 * info2 ,
2006-09-19 04:12:11 +04:00
struct GUID guid )
2002-12-14 00:56:34 +03:00
{
int i ;
2009-03-23 20:14:17 +03:00
struct regval_ctr * ctr = NULL ;
2002-12-14 00:56:34 +03:00
/* find the DsSpooler key */
2005-08-29 18:55:40 +04:00
if ( ( i = lookup_printerkey ( info2 - > data , SPOOL_DSSPOOLER_KEY ) ) < 0 )
i = add_new_printer_key ( info2 - > data , SPOOL_DSSPOOLER_KEY ) ;
ctr = info2 - > data - > keys [ i ] . values ;
2002-12-14 00:56:34 +03:00
regval_ctr_delvalue ( ctr , " objectGUID " ) ;
2007-02-09 05:12:12 +03:00
/* We used to store this as a REG_BINARY but that causes
Vista to whine */
2009-09-24 17:01:32 +04:00
regval_ctr_addvalue_sz ( ctr , " objectGUID " ,
GUID_string ( talloc_tos ( ) , & guid ) ) ;
2002-12-14 00:56:34 +03:00
}
2004-09-23 23:24:02 +04:00
static WERROR nt_printer_publish_ads ( ADS_STRUCT * ads ,
NT_PRINTER_INFO_LEVEL * printer )
2002-12-14 00:56:34 +03:00
{
ADS_STATUS ads_rc ;
2006-09-04 01:07:16 +04:00
LDAPMessage * res ;
2007-03-01 03:49:28 +03:00
char * prt_dn = NULL , * srv_dn , * srv_cn_0 , * srv_cn_escaped , * sharename_escaped ;
2003-09-11 02:33:06 +04:00
char * srv_dn_utf8 , * * srv_cn_utf8 ;
2004-09-23 23:24:02 +04:00
TALLOC_CTX * ctx ;
ADS_MODLIST mods ;
2002-12-14 00:56:34 +03:00
const char * attrs [ ] = { " objectGUID " , NULL } ;
2006-09-19 04:12:11 +04:00
struct GUID guid ;
2002-12-14 00:56:34 +03:00
WERROR win_rc = WERR_OK ;
2008-04-30 01:36:24 +04:00
size_t converted_size ;
2009-03-18 08:31:51 +03:00
/* build the ads mods */
ctx = talloc_init ( " nt_printer_publish_ads " ) ;
if ( ctx = = NULL ) {
return WERR_NOMEM ;
}
2002-12-14 00:56:34 +03:00
2004-09-23 23:24:02 +04:00
DEBUG ( 5 , ( " publishing printer %s \n " , printer - > info_2 - > printername ) ) ;
2002-12-14 00:56:34 +03:00
/* figure out where to publish */
ads_find_machine_acct ( ads , & res , global_myname ( ) ) ;
2003-09-11 02:33:06 +04:00
/* We use ldap_get_dn here as we need the answer
* in utf8 to call ldap_explode_dn ( ) . JRA . */
2007-07-16 15:08:00 +04:00
srv_dn_utf8 = ldap_get_dn ( ( LDAP * ) ads - > ldap . ld , ( LDAPMessage * ) res ) ;
2003-09-11 02:33:06 +04:00
if ( ! srv_dn_utf8 ) {
2009-03-18 08:31:51 +03:00
TALLOC_FREE ( ctx ) ;
2003-09-11 02:33:06 +04:00
return WERR_SERVER_UNAVAILABLE ;
}
2002-12-14 00:56:34 +03:00
ads_msgfree ( ads , res ) ;
2003-09-11 02:33:06 +04:00
srv_cn_utf8 = ldap_explode_dn ( srv_dn_utf8 , 1 ) ;
if ( ! srv_cn_utf8 ) {
2009-03-18 08:31:51 +03:00
TALLOC_FREE ( ctx ) ;
2003-09-11 02:33:06 +04:00
ldap_memfree ( srv_dn_utf8 ) ;
return WERR_SERVER_UNAVAILABLE ;
}
/* Now convert to CH_UNIX. */
2009-03-18 08:31:51 +03:00
if ( ! pull_utf8_talloc ( ctx , & srv_dn , srv_dn_utf8 , & converted_size ) ) {
TALLOC_FREE ( ctx ) ;
2003-09-11 02:33:06 +04:00
ldap_memfree ( srv_dn_utf8 ) ;
ldap_memfree ( srv_cn_utf8 ) ;
return WERR_SERVER_UNAVAILABLE ;
}
2009-03-18 08:31:51 +03:00
if ( ! pull_utf8_talloc ( ctx , & srv_cn_0 , srv_cn_utf8 [ 0 ] , & converted_size ) ) {
TALLOC_FREE ( ctx ) ;
2003-09-11 02:33:06 +04:00
ldap_memfree ( srv_dn_utf8 ) ;
ldap_memfree ( srv_cn_utf8 ) ;
2009-03-18 08:31:51 +03:00
TALLOC_FREE ( srv_dn ) ;
2003-09-11 02:33:06 +04:00
return WERR_SERVER_UNAVAILABLE ;
}
ldap_memfree ( srv_dn_utf8 ) ;
ldap_memfree ( srv_cn_utf8 ) ;
2007-03-01 03:49:28 +03:00
srv_cn_escaped = escape_rdn_val_string_alloc ( srv_cn_0 ) ;
if ( ! srv_cn_escaped ) {
2009-03-18 08:31:51 +03:00
TALLOC_FREE ( ctx ) ;
2007-03-01 03:49:28 +03:00
return WERR_SERVER_UNAVAILABLE ;
}
sharename_escaped = escape_rdn_val_string_alloc ( printer - > info_2 - > sharename ) ;
if ( ! sharename_escaped ) {
SAFE_FREE ( srv_cn_escaped ) ;
2009-03-18 08:31:51 +03:00
TALLOC_FREE ( ctx ) ;
2007-03-01 03:49:28 +03:00
return WERR_SERVER_UNAVAILABLE ;
}
2009-03-18 08:31:51 +03:00
prt_dn = talloc_asprintf ( ctx , " cn=%s-%s,%s " , srv_cn_escaped , sharename_escaped , srv_dn ) ;
2003-09-11 02:33:06 +04:00
2007-03-01 03:49:28 +03:00
SAFE_FREE ( srv_cn_escaped ) ;
SAFE_FREE ( sharename_escaped ) ;
2002-12-14 00:56:34 +03:00
2004-09-23 23:24:02 +04:00
mods = ads_init_mods ( ctx ) ;
2006-05-13 21:17:07 +04:00
if ( mods = = NULL ) {
SAFE_FREE ( prt_dn ) ;
2009-03-18 08:31:51 +03:00
TALLOC_FREE ( ctx ) ;
2006-05-13 21:17:07 +04:00
return WERR_NOMEM ;
}
2005-08-29 18:55:40 +04:00
get_local_printer_publishing_data ( ctx , & mods , printer - > info_2 - > data ) ;
2009-02-09 14:40:12 +03:00
ads_mod_str ( ctx , & mods , SPOOL_REG_PRINTERNAME ,
2004-09-23 23:24:02 +04:00
printer - > info_2 - > sharename ) ;
2002-12-14 00:56:34 +03:00
/* publish it */
2004-11-14 06:12:46 +03:00
ads_rc = ads_mod_printer_entry ( ads , prt_dn , ctx , & mods ) ;
2008-01-16 13:39:20 +03:00
if ( ads_rc . err . rc = = LDAP_NO_SUCH_OBJECT ) {
int i ;
for ( i = 0 ; mods [ i ] ! = 0 ; i + + )
;
mods [ i ] = ( LDAPMod * ) - 1 ;
2004-11-14 06:12:46 +03:00
ads_rc = ads_add_printer_entry ( ads , prt_dn , ctx , & mods ) ;
2008-01-16 13:39:20 +03:00
}
2004-11-14 06:12:46 +03:00
if ( ! ADS_ERR_OK ( ads_rc ) )
DEBUG ( 3 , ( " error publishing %s: %s \n " , printer - > info_2 - > sharename , ads_errstr ( ads_rc ) ) ) ;
2009-02-09 14:40:12 +03:00
2002-12-14 00:56:34 +03:00
/* retreive the guid and store it locally */
if ( ADS_ERR_OK ( ads_search_dn ( ads , & res , prt_dn , attrs ) ) ) {
2004-09-23 23:24:02 +04:00
ZERO_STRUCT ( guid ) ;
2002-12-14 00:56:34 +03:00
ads_pull_guid ( ads , res , & guid ) ;
ads_msgfree ( ads , res ) ;
store_printer_guid ( printer - > info_2 , guid ) ;
2004-09-23 23:24:02 +04:00
win_rc = mod_a_printer ( printer , 2 ) ;
2009-02-09 14:40:12 +03:00
}
2009-03-18 08:31:51 +03:00
TALLOC_FREE ( ctx ) ;
2002-12-14 00:56:34 +03:00
2004-09-23 23:24:02 +04:00
return win_rc ;
2002-12-14 00:56:34 +03:00
}
2004-09-23 23:24:02 +04:00
static WERROR nt_printer_unpublish_ads ( ADS_STRUCT * ads ,
NT_PRINTER_INFO_LEVEL * printer )
2002-12-14 00:56:34 +03:00
{
ADS_STATUS ads_rc ;
2008-12-23 22:27:19 +03:00
LDAPMessage * res = NULL ;
2002-12-14 00:56:34 +03:00
char * prt_dn = NULL ;
2004-09-23 23:24:02 +04:00
DEBUG ( 5 , ( " unpublishing printer %s \n " , printer - > info_2 - > printername ) ) ;
2003-06-25 21:41:05 +04:00
2002-12-14 00:56:34 +03:00
/* remove the printer from the directory */
2009-02-09 14:40:12 +03:00
ads_rc = ads_find_printer_on_server ( ads , & res ,
2002-12-14 00:56:34 +03:00
printer - > info_2 - > sharename , global_myname ( ) ) ;
2004-09-23 23:24:02 +04:00
2008-12-23 22:27:19 +03:00
if ( ADS_ERR_OK ( ads_rc ) & & res & & ads_count_replies ( ads , res ) ) {
2009-04-07 02:40:46 +04:00
prt_dn = ads_get_dn ( ads , talloc_tos ( ) , res ) ;
2006-06-20 01:42:07 +04:00
if ( ! prt_dn ) {
ads_msgfree ( ads , res ) ;
return WERR_NOMEM ;
}
2002-12-14 00:56:34 +03:00
ads_rc = ads_del_dn ( ads , prt_dn ) ;
2009-03-18 09:35:03 +03:00
TALLOC_FREE ( prt_dn ) ;
2002-12-14 00:56:34 +03:00
}
2008-12-23 22:27:19 +03:00
if ( res ) {
ads_msgfree ( ads , res ) ;
}
2002-12-14 00:56:34 +03:00
return WERR_OK ;
}
2002-12-04 20:40:50 +03:00
/****************************************************************************
* Publish a printer in the directory
*
* @ param snum describing printer service
* @ return WERROR indicating status of publishing
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-02-25 23:53:53 +03:00
WERROR nt_printer_publish ( Printer_entry * print_hnd , int snum , int action )
2002-12-04 20:40:50 +03:00
{
2004-09-23 23:24:02 +04:00
ADS_STATUS ads_rc ;
ADS_STRUCT * ads = NULL ;
2002-12-04 20:40:50 +03:00
NT_PRINTER_INFO_LEVEL * printer = NULL ;
WERROR win_rc ;
2003-02-25 23:53:53 +03:00
win_rc = get_a_printer ( print_hnd , & printer , 2 , lp_servicename ( snum ) ) ;
2002-12-04 20:40:50 +03:00
if ( ! W_ERROR_IS_OK ( win_rc ) )
2004-09-23 23:24:02 +04:00
goto done ;
2002-12-04 20:40:50 +03:00
2004-09-23 23:24:02 +04:00
switch ( action ) {
2009-02-27 01:40:58 +03:00
case DSPRINT_PUBLISH :
case DSPRINT_UPDATE :
2004-09-23 23:24:02 +04:00
/* set the DsSpooler info and attributes */
if ( ! ( map_nt_printer_info2_to_dsspooler ( printer - > info_2 ) ) ) {
win_rc = WERR_NOMEM ;
goto done ;
}
printer - > info_2 - > attributes | = PRINTER_ATTRIBUTE_PUBLISHED ;
2002-12-14 00:56:34 +03:00
break ;
2009-02-27 01:40:58 +03:00
case DSPRINT_UNPUBLISH :
2004-09-23 23:24:02 +04:00
printer - > info_2 - > attributes ^ = PRINTER_ATTRIBUTE_PUBLISHED ;
2002-12-14 00:56:34 +03:00
break ;
default :
win_rc = WERR_NOT_SUPPORTED ;
2004-09-23 23:24:02 +04:00
goto done ;
}
win_rc = mod_a_printer ( printer , 2 ) ;
if ( ! W_ERROR_IS_OK ( win_rc ) ) {
DEBUG ( 3 , ( " err %d saving data \n " , W_ERROR_V ( win_rc ) ) ) ;
goto done ;
}
2006-06-30 02:37:32 +04:00
ads = ads_init ( lp_realm ( ) , lp_workgroup ( ) , NULL ) ;
2004-09-23 23:24:02 +04:00
if ( ! ads ) {
DEBUG ( 3 , ( " ads_init() failed \n " ) ) ;
win_rc = WERR_SERVER_UNAVAILABLE ;
goto done ;
}
setenv ( KRB5_ENV_CCNAME , " MEMORY:prtpub_cache " , 1 ) ;
SAFE_FREE ( ads - > auth . password ) ;
ads - > auth . password = secrets_fetch_machine_password ( lp_workgroup ( ) ,
NULL , NULL ) ;
2009-02-09 14:40:12 +03:00
/* ads_connect() will find the DC for us */
2004-09-23 23:24:02 +04:00
ads_rc = ads_connect ( ads ) ;
if ( ! ADS_ERR_OK ( ads_rc ) ) {
DEBUG ( 3 , ( " ads_connect failed: %s \n " , ads_errstr ( ads_rc ) ) ) ;
win_rc = WERR_ACCESS_DENIED ;
goto done ;
}
switch ( action ) {
2009-02-27 01:40:58 +03:00
case DSPRINT_PUBLISH :
case DSPRINT_UPDATE :
2004-09-23 23:24:02 +04:00
win_rc = nt_printer_publish_ads ( ads , printer ) ;
break ;
2009-02-27 01:40:58 +03:00
case DSPRINT_UNPUBLISH :
2004-09-23 23:24:02 +04:00
win_rc = nt_printer_unpublish_ads ( ads , printer ) ;
break ;
2002-12-14 00:56:34 +03:00
}
2002-12-04 20:40:50 +03:00
2004-09-23 23:24:02 +04:00
done :
2002-12-14 00:56:34 +03:00
free_a_printer ( & printer , 2 ) ;
2004-09-23 23:24:02 +04:00
ads_destroy ( & ads ) ;
2002-12-14 00:56:34 +03:00
return win_rc ;
}
2002-12-13 22:01:27 +03:00
2004-09-23 23:24:02 +04:00
WERROR check_published_printers ( void )
{
ADS_STATUS ads_rc ;
ADS_STRUCT * ads = NULL ;
int snum ;
int n_services = lp_numservices ( ) ;
NT_PRINTER_INFO_LEVEL * printer = NULL ;
2006-05-12 19:17:35 +04:00
ads = ads_init ( lp_realm ( ) , lp_workgroup ( ) , NULL ) ;
2004-09-23 23:24:02 +04:00
if ( ! ads ) {
DEBUG ( 3 , ( " ads_init() failed \n " ) ) ;
return WERR_SERVER_UNAVAILABLE ;
}
setenv ( KRB5_ENV_CCNAME , " MEMORY:prtpub_cache " , 1 ) ;
SAFE_FREE ( ads - > auth . password ) ;
ads - > auth . password = secrets_fetch_machine_password ( lp_workgroup ( ) ,
NULL , NULL ) ;
2009-02-09 14:40:12 +03:00
/* ads_connect() will find the DC for us */
2004-09-23 23:24:02 +04:00
ads_rc = ads_connect ( ads ) ;
if ( ! ADS_ERR_OK ( ads_rc ) ) {
DEBUG ( 3 , ( " ads_connect failed: %s \n " , ads_errstr ( ads_rc ) ) ) ;
ads_destroy ( & ads ) ;
2007-04-26 16:09:25 +04:00
ads_kdestroy ( " MEMORY:prtpub_cache " ) ;
2004-09-23 23:24:02 +04:00
return WERR_ACCESS_DENIED ;
}
for ( snum = 0 ; snum < n_services ; snum + + ) {
if ( ! ( lp_snum_ok ( snum ) & & lp_print_ok ( snum ) ) )
continue ;
2004-11-14 06:12:46 +03:00
if ( W_ERROR_IS_OK ( get_a_printer ( NULL , & printer , 2 ,
lp_servicename ( snum ) ) ) & &
( printer - > info_2 - > attributes & PRINTER_ATTRIBUTE_PUBLISHED ) )
nt_printer_publish_ads ( ads , printer ) ;
2004-09-23 23:24:02 +04:00
free_a_printer ( & printer , 2 ) ;
}
ads_destroy ( & ads ) ;
2007-04-26 16:09:25 +04:00
ads_kdestroy ( " MEMORY:prtpub_cache " ) ;
2004-09-23 23:24:02 +04:00
return WERR_OK ;
}
2009-02-09 14:40:12 +03:00
bool is_printer_published ( Printer_entry * print_hnd , int snum ,
2006-09-19 04:12:11 +04:00
struct GUID * guid )
2002-12-14 00:56:34 +03:00
{
NT_PRINTER_INFO_LEVEL * printer = NULL ;
2009-03-23 20:14:17 +03:00
struct regval_ctr * ctr ;
2009-03-24 00:27:59 +03:00
struct regval_blob * guid_val ;
2002-12-14 00:56:34 +03:00
WERROR win_rc ;
int i ;
2007-10-19 04:40:25 +04:00
bool ret = False ;
2009-09-30 22:00:52 +04:00
DATA_BLOB blob ;
2002-12-04 20:40:50 +03:00
2003-02-25 23:53:53 +03:00
win_rc = get_a_printer ( print_hnd , & printer , 2 , lp_servicename ( snum ) ) ;
2003-01-15 21:57:41 +03:00
2004-09-23 23:24:02 +04:00
if ( ! W_ERROR_IS_OK ( win_rc ) | |
! ( printer - > info_2 - > attributes & PRINTER_ATTRIBUTE_PUBLISHED ) | |
2005-08-29 18:55:40 +04:00
( ( i = lookup_printerkey ( printer - > info_2 - > data , SPOOL_DSSPOOLER_KEY ) ) < 0 ) | |
! ( ctr = printer - > info_2 - > data - > keys [ i ] . values ) | |
2009-02-09 14:40:12 +03:00
! ( guid_val = regval_ctr_getvalue ( ctr , " objectGUID " ) ) )
2005-08-29 18:55:40 +04:00
{
2004-09-23 23:24:02 +04:00
free_a_printer ( & printer , 2 ) ;
2003-01-15 21:57:41 +03:00
return False ;
}
2002-12-04 20:40:50 +03:00
2007-02-09 05:12:12 +03:00
/* fetching printer guids really ought to be a separate function. */
2009-02-09 14:40:12 +03:00
if ( guid ) {
2009-09-30 22:00:52 +04:00
char * guid_str ;
2009-02-09 14:40:12 +03:00
/* We used to store the guid as REG_BINARY, then swapped
2007-02-09 05:12:12 +03:00
to REG_SZ for Vista compatibility so check for both */
switch ( regval_type ( guid_val ) ) {
2009-02-09 14:40:12 +03:00
case REG_SZ :
2009-09-30 22:00:52 +04:00
blob = data_blob_const ( regval_data_p ( guid_val ) ,
regval_size ( guid_val ) ) ;
2010-04-09 18:47:45 +04:00
pull_reg_sz ( talloc_tos ( ) , NULL , & blob , ( const char * * ) & guid_str ) ;
2008-10-14 04:33:41 +04:00
ret = NT_STATUS_IS_OK ( GUID_from_string ( guid_str , guid ) ) ;
2009-09-30 22:00:52 +04:00
talloc_free ( guid_str ) ;
2009-02-09 14:40:12 +03:00
break ;
2007-02-09 05:12:12 +03:00
case REG_BINARY :
if ( regval_size ( guid_val ) ! = sizeof ( struct GUID ) ) {
ret = False ;
break ;
}
memcpy ( guid , regval_data_p ( guid_val ) , sizeof ( struct GUID ) ) ;
break ;
default :
DEBUG ( 0 , ( " is_printer_published: GUID value stored as "
2009-02-09 14:40:12 +03:00
" invaluid type (%d) \n " , regval_type ( guid_val ) ) ) ;
2007-02-09 05:12:12 +03:00
break ;
}
}
2002-12-14 00:56:34 +03:00
2004-09-23 23:24:02 +04:00
free_a_printer ( & printer , 2 ) ;
2007-02-09 05:12:12 +03:00
return ret ;
2002-12-14 00:56:34 +03:00
}
2002-12-04 20:40:50 +03:00
# else
2003-02-25 23:53:53 +03:00
WERROR nt_printer_publish ( Printer_entry * print_hnd , int snum , int action )
2002-12-14 00:56:34 +03:00
{
2002-12-04 20:40:50 +03:00
return WERR_OK ;
}
2004-09-23 23:24:02 +04:00
WERROR check_published_printers ( void )
{
return WERR_OK ;
}
2009-02-09 14:40:12 +03:00
bool is_printer_published ( Printer_entry * print_hnd , int snum ,
2006-09-19 04:12:11 +04:00
struct GUID * guid )
2002-12-14 00:56:34 +03:00
{
return False ;
}
2004-09-23 23:24:02 +04:00
# endif /* HAVE_ADS */
2002-08-17 20:31:24 +04:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
2003-01-03 11:28:12 +03:00
WERROR delete_all_printer_data ( NT_PRINTER_INFO_LEVEL_2 * p2 , const char * key )
2002-08-17 20:31:24 +04:00
{
NT_PRINTER_DATA * data ;
int i ;
2002-09-25 19:19:00 +04:00
int removed_keys = 0 ;
int empty_slot ;
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
data = p2 - > data ;
2002-09-25 19:19:00 +04:00
empty_slot = data - > num_keys ;
if ( ! key )
return WERR_INVALID_PARAM ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/* remove all keys */
2003-02-25 23:53:53 +03:00
if ( ! strlen ( key ) ) {
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
TALLOC_FREE ( data ) ;
2006-01-13 06:10:50 +03:00
p2 - > data = NULL ;
2002-09-25 19:19:00 +04:00
DEBUG ( 8 , ( " delete_all_printer_data: Removed all Printer Data from printer [%s] \n " ,
p2 - > printername ) ) ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
return WERR_OK ;
}
2002-08-17 20:31:24 +04:00
2002-09-25 19:19:00 +04:00
/* remove a specific key (and all subkeys) */
2009-02-09 14:40:12 +03:00
2003-02-25 23:53:53 +03:00
for ( i = 0 ; i < data - > num_keys ; i + + ) {
if ( StrnCaseCmp ( data - > keys [ i ] . name , key , strlen ( key ) ) = = 0 ) {
2002-09-25 19:19:00 +04:00
DEBUG ( 8 , ( " delete_all_printer_data: Removed all Printer Data from key [%s] \n " ,
data - > keys [ i ] . name ) ) ;
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
TALLOC_FREE ( data - > keys [ i ] . name ) ;
TALLOC_FREE ( data - > keys [ i ] . values ) ;
2002-09-25 19:19:00 +04:00
/* mark the slot as empty */
ZERO_STRUCTP ( & data - > keys [ i ] ) ;
}
}
/* find the first empty slot */
for ( i = 0 ; i < data - > num_keys ; i + + ) {
if ( ! data - > keys [ i ] . name ) {
empty_slot = i ;
removed_keys + + ;
break ;
}
}
if ( i = = data - > num_keys )
/* nothing was removed */
return WERR_INVALID_PARAM ;
/* move everything down */
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
for ( i = empty_slot + 1 ; i < data - > num_keys ; i + + ) {
if ( data - > keys [ i ] . name ) {
2009-02-09 14:40:12 +03:00
memcpy ( & data - > keys [ empty_slot ] , & data - > keys [ i ] , sizeof ( NT_PRINTER_KEY ) ) ;
2002-09-25 19:19:00 +04:00
ZERO_STRUCTP ( & data - > keys [ i ] ) ;
empty_slot + + ;
removed_keys + + ;
}
}
/* update count */
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
data - > num_keys - = removed_keys ;
/* sanity check to see if anything is left */
2003-02-25 23:53:53 +03:00
if ( ! data - > num_keys ) {
2002-09-25 19:19:00 +04:00
DEBUG ( 8 , ( " delete_all_printer_data: No keys left for printer [%s] \n " , p2 - > printername ) ) ;
SAFE_FREE ( data - > keys ) ;
ZERO_STRUCTP ( data ) ;
}
return WERR_OK ;
2002-08-17 20:31:24 +04:00
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
2003-01-03 11:28:12 +03:00
WERROR delete_printer_data ( NT_PRINTER_INFO_LEVEL_2 * p2 , const char * key , const char * value )
2002-08-17 20:31:24 +04:00
{
WERROR result = WERR_OK ;
int key_index ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
/* we must have names on non-zero length */
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
if ( ! key | | ! * key | | ! value | | ! * value )
return WERR_INVALID_NAME ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
/* find the printer key first */
2005-08-29 18:55:40 +04:00
key_index = lookup_printerkey ( p2 - > data , key ) ;
2002-08-17 20:31:24 +04:00
if ( key_index = = - 1 )
2002-09-25 19:19:00 +04:00
return WERR_OK ;
2009-02-09 14:40:12 +03:00
2003-08-25 17:05:48 +04:00
/* make sure the value exists so we can return the correct error code */
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
if ( ! regval_ctr_getvalue ( p2 - > data - > keys [ key_index ] . values , value ) )
2003-08-25 17:05:48 +04:00
return WERR_BADFILE ;
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
regval_ctr_delvalue ( p2 - > data - > keys [ key_index ] . values , value ) ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
DEBUG ( 8 , ( " delete_printer_data: Removed key => [%s], value => [%s] \n " ,
key , value ) ) ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
return result ;
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
WERROR add_printer_data ( NT_PRINTER_INFO_LEVEL_2 * p2 , const char * key , const char * value ,
2002-08-17 20:31:24 +04:00
uint32 type , uint8 * data , int real_len )
{
WERROR result = WERR_OK ;
int key_index ;
/* we must have names on non-zero length */
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
if ( ! key | | ! * key | | ! value | | ! * value )
return WERR_INVALID_NAME ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
/* find the printer key first */
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
key_index = lookup_printerkey ( p2 - > data , key ) ;
2002-08-17 20:31:24 +04:00
if ( key_index = = - 1 )
2005-08-29 18:55:40 +04:00
key_index = add_new_printer_key ( p2 - > data , key ) ;
2009-02-09 14:40:12 +03:00
2002-08-17 20:31:24 +04:00
if ( key_index = = - 1 )
return WERR_NOMEM ;
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
regval_ctr_addvalue ( p2 - > data - > keys [ key_index ] . values , value ,
2003-08-15 08:42:05 +04:00
type , ( const char * ) data , real_len ) ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
DEBUG ( 8 , ( " add_printer_data: Added key => [%s], value => [%s], type=> [%d], size => [%d] \n " ,
key , value , type , real_len ) ) ;
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
return result ;
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-22 00:56:36 +03:00
2009-03-24 00:27:59 +03:00
struct regval_blob * get_printer_data ( NT_PRINTER_INFO_LEVEL_2 * p2 , const char * key , const char * value )
2002-08-17 20:31:24 +04:00
{
int key_index ;
2005-08-29 18:55:40 +04:00
if ( ( key_index = lookup_printerkey ( p2 - > data , key ) ) = = - 1 )
2002-08-17 20:31:24 +04:00
return NULL ;
DEBUG ( 8 , ( " get_printer_data: Attempting to lookup key => [%s], value => [%s] \n " ,
key , value ) ) ;
2005-08-29 18:55:40 +04:00
return regval_ctr_getvalue ( p2 - > data - > keys [ key_index ] . values , value ) ;
2002-08-17 20:31:24 +04:00
}
/****************************************************************************
Unpack a list of registry values frem the TDB
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-22 00:56:36 +03:00
2007-03-29 13:35:51 +04:00
static int unpack_values ( NT_PRINTER_DATA * printer_data , const uint8 * buf , int buflen )
2002-08-17 20:31:24 +04:00
{
int len = 0 ;
uint32 type ;
2007-11-22 00:56:36 +03:00
fstring string ;
const char * valuename = NULL ;
const char * keyname = NULL ;
2002-08-17 20:31:24 +04:00
char * str ;
int size ;
uint8 * data_p ;
2009-03-24 00:27:59 +03:00
struct regval_blob * regval_p ;
2002-08-17 20:31:24 +04:00
int key_index ;
2005-08-29 18:55:40 +04:00
2002-08-17 20:31:24 +04:00
/* add the "PrinterDriverData" key first for performance reasons */
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
add_new_printer_key ( printer_data , SPOOL_PRINTERDATA_KEY ) ;
/* loop and unpack the rest of the registry values */
2007-11-22 00:56:36 +03:00
2003-02-25 23:53:53 +03:00
while ( True ) {
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
/* check to see if there are any more registry values */
2007-11-22 00:56:36 +03:00
2004-05-07 07:15:54 +04:00
regval_p = NULL ;
2007-11-22 00:56:36 +03:00
len + = tdb_unpack ( buf + len , buflen - len , " p " , & regval_p ) ;
if ( ! regval_p )
2002-08-17 20:31:24 +04:00
break ;
/* unpack the next regval */
2007-11-22 00:56:36 +03:00
2000-05-24 10:10:21 +04:00
len + = tdb_unpack ( buf + len , buflen - len , " fdB " ,
2002-08-17 20:31:24 +04:00
string ,
& type ,
& size ,
& data_p ) ;
2005-07-02 05:23:21 +04:00
/* lookup for subkey names which have a type of REG_NONE */
/* there's no data with this entry */
if ( type = = REG_NONE ) {
if ( ( key_index = lookup_printerkey ( printer_data , string ) ) = = - 1 )
add_new_printer_key ( printer_data , string ) ;
continue ;
}
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
/*
2007-11-22 00:56:36 +03:00
* break of the keyname from the value name .
2004-05-19 21:17:33 +04:00
* Valuenames can have embedded ' \ ' s so be careful .
2007-11-22 00:56:36 +03:00
* only support one level of keys . See the
2004-05-19 21:17:33 +04:00
* " Konica Fiery S300 50C-K v1.1. enu " 2 k driver .
* - - jerry
2007-11-22 00:56:36 +03:00
*/
2004-05-19 21:17:33 +04:00
str = strchr_m ( string , ' \\ ' ) ;
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
/* Put in "PrinterDriverData" is no key specified */
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
if ( ! str ) {
2007-11-22 00:56:36 +03:00
keyname = SPOOL_PRINTERDATA_KEY ;
valuename = string ;
2002-08-17 20:31:24 +04:00
}
else {
* str = ' \0 ' ;
2007-11-22 00:56:36 +03:00
keyname = string ;
valuename = str + 1 ;
2002-08-17 20:31:24 +04:00
}
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
/* see if we need a new key */
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
if ( ( key_index = lookup_printerkey ( printer_data , keyname ) ) = = - 1 )
key_index = add_new_printer_key ( printer_data , keyname ) ;
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
if ( key_index = = - 1 ) {
DEBUG ( 0 , ( " unpack_values: Failed to allocate a new key [%s]! \n " ,
keyname ) ) ;
break ;
}
2007-11-22 00:56:36 +03:00
2007-02-09 05:12:12 +03:00
DEBUG ( 8 , ( " specific: [%s:%s], len: %d \n " , keyname , valuename , size ) ) ;
2007-11-22 00:56:36 +03:00
/* Vista doesn't like unknown REG_BINARY values in DsSpooler.
2007-02-09 05:12:12 +03:00
Thanks to Martin Zielinski for the hint . */
2007-11-22 00:56:36 +03:00
if ( type = = REG_BINARY & &
strequal ( keyname , SPOOL_DSSPOOLER_KEY ) & &
strequal ( valuename , " objectGUID " ) )
2007-02-09 05:12:12 +03:00
{
struct GUID guid ;
2007-11-22 00:56:36 +03:00
2007-02-09 05:12:12 +03:00
/* convert the GUID to a UNICODE string */
2007-11-22 00:56:36 +03:00
2007-02-09 05:12:12 +03:00
memcpy ( & guid , data_p , sizeof ( struct GUID ) ) ;
2007-11-22 00:56:36 +03:00
2009-09-24 17:01:32 +04:00
regval_ctr_addvalue_sz ( printer_data - > keys [ key_index ] . values ,
valuename ,
GUID_string ( talloc_tos ( ) , & guid ) ) ;
2007-02-09 05:12:12 +03:00
} else {
/* add the value */
2009-02-09 14:40:12 +03:00
regval_ctr_addvalue ( printer_data - > keys [ key_index ] . values ,
valuename , type , ( const char * ) data_p ,
2007-02-09 05:12:12 +03:00
size ) ;
}
2002-08-17 20:31:24 +04:00
2003-02-25 23:53:53 +03:00
SAFE_FREE ( data_p ) ; /* 'B' option to tdbpack does a malloc() */
2000-05-24 10:10:21 +04:00
}
2000-02-07 19:17:59 +03:00
2000-05-24 10:10:21 +04:00
return len ;
}
2000-02-07 19:17:59 +03:00
2002-08-17 20:31:24 +04:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-12-19 05:16:40 +03:00
static char * last_from ;
static char * last_to ;
static const char * get_last_from ( void )
{
if ( ! last_from ) {
return " " ;
}
return last_from ;
}
static const char * get_last_to ( void )
{
if ( ! last_to ) {
return " " ;
}
return last_to ;
}
static bool set_last_from_to ( const char * from , const char * to )
{
char * orig_from = last_from ;
char * orig_to = last_to ;
last_from = SMB_STRDUP ( from ) ;
last_to = SMB_STRDUP ( to ) ;
SAFE_FREE ( orig_from ) ;
SAFE_FREE ( orig_to ) ;
if ( ! last_from | | ! last_to ) {
SAFE_FREE ( last_from ) ;
SAFE_FREE ( last_to ) ;
return false ;
}
return true ;
}
2001-03-23 06:12:58 +03:00
static void map_to_os2_driver ( fstring drivername )
{
char * mapfile = lp_os2_driver_map ( ) ;
char * * lines = NULL ;
int numlines = 0 ;
int i ;
if ( ! strlen ( drivername ) )
return ;
if ( ! * mapfile )
return ;
2007-12-19 05:16:40 +03:00
if ( strequal ( drivername , get_last_from ( ) ) ) {
DEBUG ( 3 , ( " Mapped Windows driver %s to OS/2 driver %s \n " ,
drivername , get_last_to ( ) ) ) ;
fstrcpy ( drivername , get_last_to ( ) ) ;
2001-03-23 06:12:58 +03:00
return ;
}
2008-10-12 19:34:43 +04:00
lines = file_lines_load ( mapfile , & numlines , 0 , NULL ) ;
2006-03-11 05:12:25 +03:00
if ( numlines = = 0 | | lines = = NULL ) {
2001-03-23 06:12:58 +03:00
DEBUG ( 0 , ( " No entries in OS/2 driver map %s \n " , mapfile ) ) ;
2008-10-12 19:34:43 +04:00
TALLOC_FREE ( lines ) ;
2001-03-23 06:12:58 +03:00
return ;
}
DEBUG ( 4 , ( " Scanning OS/2 driver map %s \n " , mapfile ) ) ;
for ( i = 0 ; i < numlines ; i + + ) {
char * nt_name = lines [ i ] ;
2001-08-10 23:38:53 +04:00
char * os2_name = strchr ( nt_name , ' = ' ) ;
2001-03-23 06:12:58 +03:00
if ( ! os2_name )
continue ;
* os2_name + + = 0 ;
while ( isspace ( * nt_name ) )
nt_name + + ;
2001-08-10 23:38:53 +04:00
if ( ! * nt_name | | strchr ( " #; " , * nt_name ) )
2001-03-23 06:12:58 +03:00
continue ;
{
int l = strlen ( nt_name ) ;
while ( l & & isspace ( nt_name [ l - 1 ] ) ) {
nt_name [ l - 1 ] = 0 ;
l - - ;
}
}
while ( isspace ( * os2_name ) )
os2_name + + ;
{
int l = strlen ( os2_name ) ;
while ( l & & isspace ( os2_name [ l - 1 ] ) ) {
os2_name [ l - 1 ] = 0 ;
l - - ;
}
}
if ( strequal ( nt_name , drivername ) ) {
DEBUG ( 3 , ( " Mapped windows driver %s to os2 driver%s \n " , drivername , os2_name ) ) ;
2007-12-19 05:16:40 +03:00
set_last_from_to ( drivername , os2_name ) ;
2001-03-23 06:12:58 +03:00
fstrcpy ( drivername , os2_name ) ;
2008-10-12 19:34:43 +04:00
TALLOC_FREE ( lines ) ;
2001-03-23 06:12:58 +03:00
return ;
}
}
2008-10-12 19:34:43 +04:00
TALLOC_FREE ( lines ) ;
2001-03-23 06:12:58 +03:00
}
2000-02-07 19:17:59 +03:00
2000-05-24 10:10:21 +04:00
/****************************************************************************
2003-02-25 23:53:53 +03:00
Get a default printer info 2 struct .
2000-05-24 10:10:21 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-04-04 22:14:09 +04:00
static WERROR get_a_printer_2_default ( NT_PRINTER_INFO_LEVEL_2 * info ,
const char * servername ,
const char * sharename ,
bool get_loc_com )
2000-05-24 10:10:21 +04:00
{
2007-03-17 00:52:21 +03:00
int snum = lp_servicenumber ( sharename ) ;
2000-02-07 19:17:59 +03:00
2005-08-29 18:55:40 +04:00
slprintf ( info - > servername , sizeof ( info - > servername ) - 1 , " \\ \\ %s " , servername ) ;
2009-02-09 14:40:12 +03:00
slprintf ( info - > printername , sizeof ( info - > printername ) - 1 , " \\ \\ %s \\ %s " ,
2004-10-02 00:34:12 +04:00
servername , sharename ) ;
2005-08-29 18:55:40 +04:00
fstrcpy ( info - > sharename , sharename ) ;
fstrcpy ( info - > portname , SAMBA_PRINTER_PORT_NAME ) ;
2000-11-14 05:14:58 +03:00
2001-05-03 06:51:08 +04:00
/* by setting the driver name to an empty string, a local NT admin
can now run the * * local * * APW to install a local printer driver
2009-02-09 14:40:12 +03:00
for a Samba shared printer in 2.2 . Without this , drivers * * must * * be
2001-05-03 06:51:08 +04:00
installed on the Samba server for NT clients - - jerry */
2001-11-22 08:56:09 +03:00
#if 0 /* JERRY --do not uncomment-- */
2005-08-29 18:55:40 +04:00
if ( ! * info - > drivername )
fstrcpy ( info - > drivername , " NO DRIVER AVAILABLE FOR THIS PRINTER " ) ;
2001-05-03 06:51:08 +04:00
# endif
2000-11-15 00:56:32 +03:00
2005-08-29 18:55:40 +04:00
DEBUG ( 10 , ( " get_a_printer_2_default: driver name set to [%s] \n " , info - > drivername ) ) ;
2000-11-15 00:56:32 +03:00
2007-12-04 02:19:22 +03:00
strlcpy ( info - > comment , " " , sizeof ( info - > comment ) ) ;
2005-08-29 18:55:40 +04:00
fstrcpy ( info - > printprocessor , " winprint " ) ;
fstrcpy ( info - > datatype , " RAW " ) ;
2000-02-07 19:17:59 +03:00
2007-03-17 00:52:21 +03:00
# ifdef HAVE_CUPS
2009-02-09 14:40:12 +03:00
if ( get_loc_com & & ( enum printing_types ) lp_printing ( snum ) = = PRINT_CUPS ) {
2007-03-17 00:52:21 +03:00
/* Pull the location and comment strings from cups if we don't
already have one */
2010-03-26 15:17:49 +03:00
if ( ! strlen ( info - > location ) | | ! strlen ( info - > comment ) ) {
char * comment = NULL ;
char * location = NULL ;
if ( cups_pull_comment_location ( info , info - > sharename ,
& comment , & location ) ) {
strlcpy ( info - > comment , comment , sizeof ( info - > comment ) ) ;
fstrcpy ( info - > location , location ) ;
TALLOC_FREE ( comment ) ;
TALLOC_FREE ( location ) ;
}
}
2007-03-17 00:52:21 +03:00
}
# endif
2005-08-29 18:55:40 +04:00
info - > attributes = PRINTER_ATTRIBUTE_SAMBA ;
2000-06-06 05:34:20 +04:00
2005-08-29 18:55:40 +04:00
info - > starttime = 0 ; /* Minutes since 12:00am GMT */
info - > untiltime = 0 ; /* Minutes since 12:00am GMT */
info - > priority = 1 ;
info - > default_priority = 1 ;
info - > setuptime = ( uint32 ) time ( NULL ) ;
2001-11-22 08:56:09 +03:00
/*
* I changed this as I think it is better to have a generic
* DEVMODE than to crash Win2k explorer . exe - - jerry
* See the HP Deskjet 990 c Win2k drivers for an example .
2002-01-22 21:14:31 +03:00
*
* However the default devmode appears to cause problems
* with the HP CLJ 8500 PCL driver . Hence the addition of
* the " default devmode " parameter - - jerry 22 / 01 / 2002
2001-11-22 08:56:09 +03:00
*/
2000-06-06 05:34:20 +04:00
2002-01-22 21:14:31 +03:00
if ( lp_default_devmode ( snum ) ) {
2006-06-20 01:36:19 +04:00
if ( ( info - > devmode = construct_nt_devicemode ( info - > printername ) ) = = NULL ) {
2002-01-22 21:14:31 +03:00
goto fail ;
2006-06-20 01:36:19 +04:00
}
} else {
2005-08-29 18:55:40 +04:00
info - > devmode = NULL ;
2002-01-23 08:10:27 +03:00
}
2000-04-05 23:48:06 +04:00
2006-06-20 01:36:19 +04:00
if ( ! nt_printing_getsec ( info , sharename , & info - > secdesc_buf ) ) {
2000-06-01 21:01:34 +04:00
goto fail ;
2006-06-20 01:36:19 +04:00
}
2000-05-27 06:15:08 +04:00
2009-12-04 19:22:25 +03:00
info - > data = TALLOC_ZERO_P ( info , NT_PRINTER_DATA ) ;
if ( ! info - > data ) {
goto fail ;
}
add_new_printer_key ( info - > data , SPOOL_PRINTERDATA_KEY ) ;
2001-09-04 11:13:01 +04:00
return WERR_OK ;
2000-06-01 21:01:34 +04:00
2005-08-29 18:55:40 +04:00
fail :
if ( info - > devmode )
free_nt_devicemode ( & info - > devmode ) ;
2001-09-04 11:13:01 +04:00
return WERR_ACCESS_DENIED ;
2000-05-24 10:10:21 +04:00
}
2000-04-05 23:48:06 +04:00
2000-05-24 10:10:21 +04:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-04-04 22:14:09 +04:00
static WERROR get_a_printer_2 ( NT_PRINTER_INFO_LEVEL_2 * info ,
const char * servername ,
const char * sharename ,
bool get_loc_com )
2000-05-24 10:10:21 +04:00
{
2004-10-20 02:13:08 +04:00
int len = 0 ;
int snum = lp_servicenumber ( sharename ) ;
2000-05-24 10:10:21 +04:00
TDB_DATA kbuf , dbuf ;
2000-11-10 22:36:34 +03:00
fstring printername ;
2003-08-23 07:34:24 +04:00
char adevice [ MAXDEVICENAME ] ;
2007-12-04 01:54:06 +03:00
char * comment = NULL ;
2007-11-22 00:56:36 +03:00
kbuf = make_printer_tdbkey ( talloc_tos ( ) , sharename ) ;
2000-05-24 10:10:21 +04:00
2001-07-09 22:19:04 +04:00
dbuf = tdb_fetch ( tdb_printers , kbuf ) ;
2006-06-20 01:36:19 +04:00
if ( ! dbuf . dptr ) {
2008-04-04 22:14:09 +04:00
return get_a_printer_2_default ( info , servername ,
sharename , get_loc_com ) ;
2006-06-20 01:36:19 +04:00
}
2000-05-24 10:10:21 +04:00
2000-08-01 00:41:51 +04:00
len + = tdb_unpack ( dbuf . dptr + len , dbuf . dsize - len , " dddddddddddfffffPfffff " ,
2005-08-29 18:55:40 +04:00
& info - > attributes ,
& info - > priority ,
& info - > default_priority ,
& info - > starttime ,
& info - > untiltime ,
& info - > status ,
& info - > cjobs ,
& info - > averageppm ,
& info - > changeid ,
& info - > c_setprinter ,
& info - > setuptime ,
info - > servername ,
info - > printername ,
info - > sharename ,
info - > portname ,
info - > drivername ,
2007-12-04 01:54:06 +03:00
& comment ,
2005-08-29 18:55:40 +04:00
info - > location ,
info - > sepfile ,
info - > printprocessor ,
info - > datatype ,
info - > parameters ) ;
2000-05-24 10:10:21 +04:00
2007-12-04 01:54:06 +03:00
if ( comment ) {
strlcpy ( info - > comment , comment , sizeof ( info - > comment ) ) ;
SAFE_FREE ( comment ) ;
}
2000-07-22 04:48:29 +04:00
/* Samba has to have shared raw drivers. */
2005-08-29 18:55:40 +04:00
info - > attributes | = PRINTER_ATTRIBUTE_SAMBA ;
info - > attributes & = ~ PRINTER_ATTRIBUTE_NOT_SAMBA ;
2000-06-06 05:34:20 +04:00
2000-11-10 22:36:34 +03:00
/* Restore the stripped strings. */
2005-08-29 18:55:40 +04:00
slprintf ( info - > servername , sizeof ( info - > servername ) - 1 , " \\ \\ %s " , servername ) ;
2004-10-20 02:13:08 +04:00
2006-06-20 01:36:19 +04:00
if ( lp_force_printername ( snum ) ) {
2005-07-15 18:26:11 +04:00
slprintf ( printername , sizeof ( printername ) - 1 , " \\ \\ %s \\ %s " , servername , sharename ) ;
2006-06-20 01:36:19 +04:00
} else {
2005-08-29 18:55:40 +04:00
slprintf ( printername , sizeof ( printername ) - 1 , " \\ \\ %s \\ %s " , servername , info - > printername ) ;
2006-06-20 01:36:19 +04:00
}
2004-10-20 02:13:08 +04:00
2005-08-29 18:55:40 +04:00
fstrcpy ( info - > printername , printername ) ;
2000-11-10 22:36:34 +03:00
2007-03-17 00:46:58 +03:00
# ifdef HAVE_CUPS
2008-04-04 22:14:09 +04:00
if ( get_loc_com & & ( enum printing_types ) lp_printing ( snum ) = = PRINT_CUPS ) {
2007-03-17 00:46:58 +03:00
/* Pull the location and comment strings from cups if we don't
already have one */
2010-03-26 15:17:49 +03:00
if ( ! strlen ( info - > location ) | | ! strlen ( info - > comment ) ) {
char * location = NULL ;
comment = NULL ;
if ( cups_pull_comment_location ( info , info - > sharename ,
& comment , & location ) ) {
strlcpy ( info - > comment , comment , sizeof ( info - > comment ) ) ;
fstrcpy ( info - > location , location ) ;
TALLOC_FREE ( comment ) ;
TALLOC_FREE ( location ) ;
}
}
2007-03-17 00:46:58 +03:00
}
# endif
2005-08-29 18:55:40 +04:00
len + = unpack_devicemode ( & info - > devmode , dbuf . dptr + len , dbuf . dsize - len ) ;
2002-01-22 21:14:31 +03:00
2001-11-22 08:56:09 +03:00
/*
* Some client drivers freak out if there is a NULL devmode
2009-02-09 14:40:12 +03:00
* ( probably the driver is not checking before accessing
2001-11-22 08:56:09 +03:00
* the devmode pointer ) - - jerry
2002-01-22 21:14:31 +03:00
*
* See comments in get_a_printer_2_default ( )
2001-11-22 08:56:09 +03:00
*/
2002-01-22 21:14:31 +03:00
2005-08-29 18:55:40 +04:00
if ( lp_default_devmode ( snum ) & & ! info - > devmode ) {
2001-11-22 08:56:09 +03:00
DEBUG ( 8 , ( " get_a_printer_2: Constructing a default device mode for [%s] \n " ,
printername ) ) ;
2005-08-29 18:55:40 +04:00
info - > devmode = construct_nt_devicemode ( printername ) ;
2001-11-22 08:56:09 +03:00
}
2003-08-23 07:34:24 +04:00
2005-08-29 18:55:40 +04:00
slprintf ( adevice , sizeof ( adevice ) , " %s " , info - > printername ) ;
if ( info - > devmode ) {
2009-02-09 14:40:12 +03:00
fstrcpy ( info - > devmode - > devicename , adevice ) ;
2003-10-20 18:02:02 +04:00
}
2002-01-22 21:14:31 +03:00
2005-08-29 18:55:40 +04:00
if ( ! ( info - > data = TALLOC_ZERO_P ( info , NT_PRINTER_DATA ) ) ) {
DEBUG ( 0 , ( " unpack_values: talloc() failed! \n " ) ) ;
2006-06-20 01:36:19 +04:00
SAFE_FREE ( dbuf . dptr ) ;
2005-08-29 18:55:40 +04:00
return WERR_NOMEM ;
}
len + = unpack_values ( info - > data , dbuf . dptr + len , dbuf . dsize - len ) ;
2000-05-24 10:10:21 +04:00
2001-02-28 03:51:02 +03:00
/* This will get the current RPC talloc context, but we should be
2002-08-17 20:31:24 +04:00
passing this as a parameter . . . fixme . . . JRA ! */
2001-02-28 03:51:02 +03:00
2006-06-20 01:36:19 +04:00
if ( ! nt_printing_getsec ( info , sharename , & info - > secdesc_buf ) ) {
SAFE_FREE ( dbuf . dptr ) ;
return WERR_NOMEM ;
}
2000-06-06 05:47:11 +04:00
2001-03-23 06:12:58 +03:00
/* Fix for OS/2 drivers. */
2006-06-20 01:36:19 +04:00
if ( get_remote_arch ( ) = = RA_OS2 ) {
2005-08-29 18:55:40 +04:00
map_to_os2_driver ( info - > drivername ) ;
2006-06-20 01:36:19 +04:00
}
2001-03-23 06:12:58 +03:00
2001-09-17 13:25:57 +04:00
SAFE_FREE ( dbuf . dptr ) ;
2000-06-04 08:17:59 +04:00
2000-11-10 22:36:34 +03:00
DEBUG ( 9 , ( " Unpacked printer [%s] name [%s] running driver [%s] \n " ,
2005-08-29 18:55:40 +04:00
sharename , info - > printername , info - > drivername ) ) ;
2000-06-04 08:17:59 +04:00
2009-02-09 14:40:12 +03:00
return WERR_OK ;
2000-02-07 19:17:59 +03:00
}
/****************************************************************************
2003-02-25 23:53:53 +03:00
Debugging function , dump at level 6 the struct in the logs .
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-23 23:24:02 +04:00
static uint32 dump_a_printer ( NT_PRINTER_INFO_LEVEL * printer , uint32 level )
2000-02-07 19:17:59 +03:00
{
2000-12-06 03:37:25 +03:00
uint32 result ;
2000-02-07 19:17:59 +03:00
NT_PRINTER_INFO_LEVEL_2 * info2 ;
2009-02-09 14:40:12 +03:00
2000-02-15 21:07:45 +03:00
DEBUG ( 106 , ( " Dumping printer at level [%d] \n " , level ) ) ;
2009-02-09 14:40:12 +03:00
2003-02-25 23:53:53 +03:00
switch ( level ) {
2000-11-15 00:56:32 +03:00
case 2 :
2000-02-07 19:17:59 +03:00
{
2004-09-23 23:24:02 +04:00
if ( printer - > info_2 = = NULL )
2000-12-06 03:37:25 +03:00
result = 5 ;
2000-02-07 19:17:59 +03:00
else
{
2004-09-23 23:24:02 +04:00
info2 = printer - > info_2 ;
2009-02-09 14:40:12 +03:00
2000-03-10 20:12:24 +03:00
DEBUGADD ( 106 , ( " attributes:[%d] \n " , info2 - > attributes ) ) ;
DEBUGADD ( 106 , ( " priority:[%d] \n " , info2 - > priority ) ) ;
2000-02-15 21:07:45 +03:00
DEBUGADD ( 106 , ( " default_priority:[%d] \n " , info2 - > default_priority ) ) ;
2000-03-10 20:12:24 +03:00
DEBUGADD ( 106 , ( " starttime:[%d] \n " , info2 - > starttime ) ) ;
DEBUGADD ( 106 , ( " untiltime:[%d] \n " , info2 - > untiltime ) ) ;
2002-01-07 09:13:40 +03:00
DEBUGADD ( 106 , ( " status:[%d] \n " , info2 - > status ) ) ;
2000-03-10 20:12:24 +03:00
DEBUGADD ( 106 , ( " cjobs:[%d] \n " , info2 - > cjobs ) ) ;
DEBUGADD ( 106 , ( " averageppm:[%d] \n " , info2 - > averageppm ) ) ;
DEBUGADD ( 106 , ( " changeid:[%d] \n " , info2 - > changeid ) ) ;
DEBUGADD ( 106 , ( " c_setprinter:[%d] \n " , info2 - > c_setprinter ) ) ;
2000-05-24 10:10:21 +04:00
DEBUGADD ( 106 , ( " setuptime:[%d] \n " , info2 - > setuptime ) ) ;
2000-03-10 20:12:24 +03:00
DEBUGADD ( 106 , ( " servername:[%s] \n " , info2 - > servername ) ) ;
DEBUGADD ( 106 , ( " printername:[%s] \n " , info2 - > printername ) ) ;
DEBUGADD ( 106 , ( " sharename:[%s] \n " , info2 - > sharename ) ) ;
DEBUGADD ( 106 , ( " portname:[%s] \n " , info2 - > portname ) ) ;
DEBUGADD ( 106 , ( " drivername:[%s] \n " , info2 - > drivername ) ) ;
2000-07-22 04:48:29 +04:00
DEBUGADD ( 106 , ( " comment:[%s] \n " , info2 - > comment ) ) ;
2000-03-10 20:12:24 +03:00
DEBUGADD ( 106 , ( " location:[%s] \n " , info2 - > location ) ) ;
DEBUGADD ( 106 , ( " sepfile:[%s] \n " , info2 - > sepfile ) ) ;
DEBUGADD ( 106 , ( " printprocessor:[%s] \n " , info2 - > printprocessor ) ) ;
DEBUGADD ( 106 , ( " datatype:[%s] \n " , info2 - > datatype ) ) ;
DEBUGADD ( 106 , ( " parameters:[%s] \n " , info2 - > parameters ) ) ;
2000-12-06 03:37:25 +03:00
result = 0 ;
2000-02-07 19:17:59 +03:00
}
break ;
}
default :
2002-02-09 07:10:24 +03:00
DEBUGADD ( 106 , ( " dump_a_printer: Level %u not implemented \n " , ( unsigned int ) level ) ) ;
2000-12-06 03:37:25 +03:00
result = 1 ;
2000-02-07 19:17:59 +03:00
break ;
}
2009-02-09 14:40:12 +03:00
2000-12-06 03:37:25 +03:00
return result ;
2000-02-07 19:17:59 +03:00
}
2002-01-08 08:39:27 +03:00
/****************************************************************************
Update the changeid time .
2002-01-09 02:50:14 +03:00
This is SO NASTY as some drivers need this to change , others need it
static . This value will change every second , and I must hope that this
is enough . . . . . DON ' T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
UTAH ! JRA .
2002-01-08 08:39:27 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-09 02:50:14 +03:00
static uint32 rev_changeid ( void )
2002-01-08 08:39:27 +03:00
{
2002-01-12 00:52:46 +03:00
struct timeval tv ;
get_process_uptime ( & tv ) ;
2002-02-09 07:10:24 +03:00
2002-03-15 11:14:10 +03:00
# if 1 /* JERRY */
2002-02-09 07:10:24 +03:00
/* Return changeid as msec since spooler restart */
return tv . tv_sec * 1000 + tv . tv_usec / 1000 ;
2002-03-15 11:14:10 +03:00
# else
/*
* This setting seems to work well but is too untested
* to replace the above calculation . Left in for experiementation
* of the reader - - jerry ( Tue Mar 12 09 : 15 : 05 CST 2002 )
*/
return tv . tv_sec * 10 + tv . tv_usec / 100000 ;
# endif
2002-01-08 08:39:27 +03:00
}
2004-08-18 17:55:58 +04:00
2000-02-07 19:17:59 +03:00
/*
* The function below are the high level ones .
* only those ones must be called from the spoolss code .
* JFM .
*/
/****************************************************************************
2000-10-27 01:43:13 +04:00
Modify a printer . This is called from SETPRINTERDATA / DELETEPRINTERDATA .
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-10-27 01:43:13 +04:00
2004-09-23 23:24:02 +04:00
WERROR mod_a_printer ( NT_PRINTER_INFO_LEVEL * printer , uint32 level )
2000-10-27 01:43:13 +04:00
{
2001-09-04 11:13:01 +04:00
WERROR result ;
2009-02-09 14:40:12 +03:00
dump_a_printer ( printer , level ) ;
2003-02-25 23:53:53 +03:00
switch ( level ) {
2000-10-27 01:43:13 +04:00
case 2 :
{
2001-12-14 23:38:04 +03:00
/*
* Update the changestamp . Emperical tests show that the
2009-02-09 14:40:12 +03:00
* ChangeID is always updated , but c_setprinter is
2002-02-09 07:10:24 +03:00
* global spooler variable ( not per printer ) .
2001-12-14 23:38:04 +03:00
*/
/* ChangeID **must** be increasing over the lifetime
2002-01-08 08:39:27 +03:00
of client ' s spoolss service in order for the
2001-12-14 23:38:04 +03:00
client ' s cache to show updates */
2004-09-23 23:24:02 +04:00
printer - > info_2 - > changeid = rev_changeid ( ) ;
2001-12-14 23:38:04 +03:00
/*
* Because one day someone will ask :
* NT - > NT An admin connection to a remote
* printer show changes imeediately in
* the properities dialog
2009-02-09 14:40:12 +03:00
*
2001-12-14 23:38:04 +03:00
* A non - admin connection will only show the
* changes after viewing the properites page
* 2 times . Seems to be related to a
* race condition in the client between the spooler
* updating the local cache and the Explorer . exe GUI
* actually displaying the properties .
*
* This is fixed in Win2k . admin / non - admin
* connections both display changes immediately .
*
* 14 / 12 / 01 - - jerry
*/
2004-09-23 23:24:02 +04:00
result = update_a_printer_2 ( printer - > info_2 ) ;
2000-10-27 01:43:13 +04:00
break ;
}
default :
2001-09-04 11:13:01 +04:00
result = WERR_UNKNOWN_LEVEL ;
2000-10-27 01:43:13 +04:00
break ;
}
2007-11-22 00:56:36 +03:00
2000-12-06 03:37:25 +03:00
return result ;
2000-10-27 01:43:13 +04:00
}
2001-08-10 23:38:53 +04:00
/****************************************************************************
Initialize printer devmode & data with previously saved driver init values .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-02-09 07:10:24 +03:00
2007-10-19 04:40:25 +04:00
static bool set_driver_init_2 ( NT_PRINTER_INFO_LEVEL_2 * info_ptr )
2001-08-10 23:38:53 +04:00
{
int len = 0 ;
2007-11-22 00:56:36 +03:00
char * key = NULL ;
2007-03-27 15:22:07 +04:00
TDB_DATA dbuf ;
2001-08-10 23:38:53 +04:00
NT_PRINTER_INFO_LEVEL_2 info ;
2002-08-17 20:31:24 +04:00
ZERO_STRUCT ( info ) ;
2002-02-09 07:10:24 +03:00
/*
2002-08-17 20:31:24 +04:00
* Delete any printer data ' values ' already set . When called for driver
2002-02-09 07:10:24 +03:00
* replace , there will generally be some , but during an add printer , there
* should not be any ( if there are delete them ) .
*/
2007-11-22 00:56:36 +03:00
2005-08-29 18:55:40 +04:00
if ( info_ptr - > data )
delete_all_printer_data ( info_ptr , " " ) ;
2007-11-22 00:56:36 +03:00
if ( asprintf ( & key , " %s%s " , DRIVER_INIT_PREFIX ,
info_ptr - > drivername ) < 0 ) {
return false ;
}
2001-08-10 23:38:53 +04:00
2007-03-27 15:22:07 +04:00
dbuf = tdb_fetch_bystring ( tdb_drivers , key ) ;
2002-08-17 20:31:24 +04:00
if ( ! dbuf . dptr ) {
2002-02-09 07:10:24 +03:00
/*
* When changing to a driver that has no init info in the tdb , remove
* the previous drivers init info and leave the new on blank .
*/
free_nt_devicemode ( & info_ptr - > devmode ) ;
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
return false ;
2002-02-09 07:10:24 +03:00
}
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
2001-08-10 23:38:53 +04:00
/*
* Get the saved DEVMODE . .
*/
2007-11-22 00:56:36 +03:00
2001-08-10 23:38:53 +04:00
len + = unpack_devicemode ( & info . devmode , dbuf . dptr + len , dbuf . dsize - len ) ;
/*
* The saved DEVMODE contains the devicename from the printer used during
* the initialization save . Change it to reflect the new printer .
*/
2007-11-22 00:56:36 +03:00
2002-09-25 19:19:00 +04:00
if ( info . devmode ) {
2002-11-23 05:52:36 +03:00
ZERO_STRUCT ( info . devmode - > devicename ) ;
fstrcpy ( info . devmode - > devicename , info_ptr - > printername ) ;
2002-09-25 19:19:00 +04:00
}
2002-03-15 11:14:10 +03:00
/*
* NT / 2 k does not change out the entire DeviceMode of a printer
2007-11-22 00:56:36 +03:00
* when changing the driver . Only the driverextra , private , &
2002-03-15 11:14:10 +03:00
* driverversion fields . - - jerry ( Thu Mar 14 08 : 58 : 43 CST 2002 )
2002-07-15 14:35:28 +04:00
*
2002-09-25 19:19:00 +04:00
* Later examination revealed that Windows NT / 2 k does reset the
2007-11-22 00:56:36 +03:00
* the printer ' s device mode , bit * * only * * when you change a
2002-07-15 14:35:28 +04:00
* property of the device mode such as the page orientation .
* - - jerry
2002-03-15 11:14:10 +03:00
*/
2002-08-17 20:31:24 +04:00
/* Bind the saved DEVMODE to the new the printer */
2007-11-22 00:56:36 +03:00
2001-08-10 23:38:53 +04:00
free_nt_devicemode ( & info_ptr - > devmode ) ;
info_ptr - > devmode = info . devmode ;
2002-03-15 11:14:10 +03:00
2002-09-25 19:19:00 +04:00
DEBUG ( 10 , ( " set_driver_init_2: Set printer [%s] init %s DEVMODE for driver [%s] \n " ,
info_ptr - > printername , info_ptr - > devmode ? " VALID " : " NULL " , info_ptr - > drivername ) ) ;
2001-08-10 23:38:53 +04:00
2002-08-17 20:31:24 +04:00
/* Add the printer data 'values' to the new printer */
2005-08-29 18:55:40 +04:00
if ( ! ( info_ptr - > data = TALLOC_ZERO_P ( info_ptr , NT_PRINTER_DATA ) ) ) {
DEBUG ( 0 , ( " set_driver_init_2: talloc() failed! \n " ) ) ;
return False ;
}
2007-11-22 00:56:36 +03:00
2005-08-29 18:55:40 +04:00
len + = unpack_values ( info_ptr - > data , dbuf . dptr + len , dbuf . dsize - len ) ;
2001-08-10 23:38:53 +04:00
2001-09-17 13:25:57 +04:00
SAFE_FREE ( dbuf . dptr ) ;
2001-08-10 23:38:53 +04:00
2007-11-22 00:56:36 +03:00
return true ;
2001-08-10 23:38:53 +04:00
}
/****************************************************************************
Initialize printer devmode & data with previously saved driver init values .
When a printer is created using AddPrinter , the drivername bound to the
printer is used to lookup previously saved driver initialization info , which
is bound to the new printer .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool set_driver_init ( NT_PRINTER_INFO_LEVEL * printer , uint32 level )
2001-08-10 23:38:53 +04:00
{
2007-10-19 04:40:25 +04:00
bool result = False ;
2007-11-22 00:56:36 +03:00
2003-02-25 23:53:53 +03:00
switch ( level ) {
2001-08-10 23:38:53 +04:00
case 2 :
2002-08-17 20:31:24 +04:00
result = set_driver_init_2 ( printer - > info_2 ) ;
2001-08-10 23:38:53 +04:00
break ;
2007-11-22 00:56:36 +03:00
2001-08-10 23:38:53 +04:00
default :
2002-07-15 14:35:28 +04:00
DEBUG ( 0 , ( " set_driver_init: Programmer's error! Unknown driver_init level [%d] \n " ,
level ) ) ;
2001-08-10 23:38:53 +04:00
break ;
}
2007-11-22 00:56:36 +03:00
2001-08-10 23:38:53 +04:00
return result ;
}
2002-07-15 14:35:28 +04:00
/****************************************************************************
Delete driver init data stored for a specified driver
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-03-20 17:14:09 +03:00
bool del_driver_init ( const char * drivername )
2002-07-15 14:35:28 +04:00
{
2007-11-22 00:56:36 +03:00
char * key ;
bool ret ;
2002-07-15 14:35:28 +04:00
if ( ! drivername | | ! * drivername ) {
DEBUG ( 3 , ( " del_driver_init: No drivername specified! \n " ) ) ;
2007-11-22 00:56:36 +03:00
return false ;
2002-07-15 14:35:28 +04:00
}
2007-11-22 00:56:36 +03:00
if ( asprintf ( & key , " %s%s " , DRIVER_INIT_PREFIX , drivername ) < 0 ) {
return false ;
}
2002-07-15 14:35:28 +04:00
2007-11-22 00:56:36 +03:00
DEBUG ( 6 , ( " del_driver_init: Removing driver init data for [%s] \n " ,
drivername ) ) ;
2002-07-15 14:35:28 +04:00
2007-11-22 00:56:36 +03:00
ret = ( tdb_delete_bystring ( tdb_drivers , key ) = = 0 ) ;
SAFE_FREE ( key ) ;
return ret ;
2002-07-15 14:35:28 +04:00
}
2001-08-10 23:38:53 +04:00
/****************************************************************************
2009-02-09 14:40:12 +03:00
Pack up the DEVMODE and values for a printer into a ' driver init ' entry
2001-08-10 23:38:53 +04:00
in the tdb . Note : this is different from the driver entry and the printer
entry . There should be a single driver init entry for each driver regardless
of whether it was installed from NT or 2 K . Technically , they should be
different , but they work out to the same struct .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-02-09 07:10:24 +03:00
2001-08-10 23:38:53 +04:00
static uint32 update_driver_init_2 ( NT_PRINTER_INFO_LEVEL_2 * info )
{
2007-11-22 00:56:36 +03:00
char * key = NULL ;
2007-03-29 13:35:51 +04:00
uint8 * buf ;
2001-08-10 23:38:53 +04:00
int buflen , len , ret ;
2007-11-22 00:56:36 +03:00
int retlen ;
2007-03-27 15:22:07 +04:00
TDB_DATA dbuf ;
2001-08-10 23:38:53 +04:00
buf = NULL ;
buflen = 0 ;
2007-11-22 00:56:36 +03:00
again :
2001-08-10 23:38:53 +04:00
len = 0 ;
len + = pack_devicemode ( info - > devmode , buf + len , buflen - len ) ;
2007-11-22 00:56:36 +03:00
retlen = pack_values ( info - > data , buf + len , buflen - len ) ;
if ( retlen = = - 1 ) {
ret = - 1 ;
goto done ;
}
len + = retlen ;
2001-08-10 23:38:53 +04:00
2004-10-15 19:04:01 +04:00
if ( buflen < len ) {
2007-03-29 13:35:51 +04:00
buf = ( uint8 * ) SMB_REALLOC ( buf , len ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! buf ) {
2001-08-12 21:30:01 +04:00
DEBUG ( 0 , ( " update_driver_init_2: failed to enlarge buffer! \n " ) ) ;
ret = - 1 ;
goto done ;
}
2001-08-10 23:38:53 +04:00
buflen = len ;
goto again ;
}
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
if ( asprintf ( & key , " %s%s " , DRIVER_INIT_PREFIX , info - > drivername ) < 0 ) {
ret = ( uint32 ) - 1 ;
goto done ;
}
2001-08-10 23:38:53 +04:00
dbuf . dptr = buf ;
dbuf . dsize = len ;
2007-03-27 15:22:07 +04:00
ret = tdb_store_bystring ( tdb_drivers , key , dbuf , TDB_REPLACE ) ;
2001-08-10 23:38:53 +04:00
2001-08-12 21:30:01 +04:00
done :
2001-08-10 23:38:53 +04:00
if ( ret = = - 1 )
DEBUG ( 8 , ( " update_driver_init_2: error updating printer init to tdb on disk \n " ) ) ;
2001-09-17 13:25:57 +04:00
SAFE_FREE ( buf ) ;
2001-08-10 23:38:53 +04:00
2002-08-17 20:31:24 +04:00
DEBUG ( 10 , ( " update_driver_init_2: Saved printer [%s] init DEVMODE & values for driver [%s] \n " ,
2001-08-10 23:38:53 +04:00
info - > sharename , info - > drivername ) ) ;
return ret ;
}
/****************************************************************************
2002-08-17 20:31:24 +04:00
Update ( i . e . save ) the driver init info ( DEVMODE and values ) for a printer
2001-08-10 23:38:53 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-23 23:24:02 +04:00
static uint32 update_driver_init ( NT_PRINTER_INFO_LEVEL * printer , uint32 level )
2001-08-10 23:38:53 +04:00
{
uint32 result ;
2009-02-09 14:40:12 +03:00
dump_a_printer ( printer , level ) ;
2003-02-25 23:53:53 +03:00
switch ( level ) {
2001-08-10 23:38:53 +04:00
case 2 :
2004-09-23 23:24:02 +04:00
result = update_driver_init_2 ( printer - > info_2 ) ;
2001-08-10 23:38:53 +04:00
break ;
default :
2002-09-25 19:19:00 +04:00
result = 1 ;
2001-08-10 23:38:53 +04:00
break ;
}
2009-02-09 14:40:12 +03:00
2001-08-10 23:38:53 +04:00
return result ;
}
2002-09-25 19:19:00 +04:00
/****************************************************************************
2009-02-09 14:40:12 +03:00
Convert the printer data value , a REG_BINARY array , into an initialization
2002-09-25 19:19:00 +04:00
DEVMODE . Note : the array must be parsed as if it was a DEVMODE in an rpc . . .
got to keep the endians happy : ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-03-17 16:43:50 +03:00
static bool convert_driver_init ( TALLOC_CTX * mem_ctx , NT_DEVICEMODE * nt_devmode ,
const uint8_t * data , uint32_t data_len )
2002-09-25 19:19:00 +04:00
{
2009-03-17 16:43:50 +03:00
struct spoolss_DeviceMode devmode ;
enum ndr_err_code ndr_err ;
DATA_BLOB blob ;
2002-09-25 19:19:00 +04:00
ZERO_STRUCT ( devmode ) ;
2009-03-17 16:43:50 +03:00
blob = data_blob_const ( data , data_len ) ;
2002-09-25 19:19:00 +04:00
2009-03-17 16:43:50 +03:00
ndr_err = ndr_pull_struct_blob ( & blob , mem_ctx , NULL , & devmode ,
( ndr_pull_flags_fn_t ) ndr_pull_spoolss_DeviceMode ) ;
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_err ) ) {
DEBUG ( 10 , ( " convert_driver_init: error parsing spoolss_DeviceMode \n " ) ) ;
return false ;
}
2002-09-25 19:19:00 +04:00
2009-03-17 16:43:50 +03:00
return convert_devicemode ( " " , & devmode , & nt_devmode ) ;
2002-09-25 19:19:00 +04:00
}
/****************************************************************************
Set the DRIVER_INIT info in the tdb . Requires Win32 client code that :
1. Use the driver ' s config DLL to this UNC printername and :
a . Call DrvPrintEvent with PRINTER_EVENT_INITIALIZE
b . Call DrvConvertDevMode with CDM_DRIVER_DEFAULT to get default DEVMODE
2. Call SetPrinterData with the ' magic ' key and the DEVMODE as data .
The last step triggers saving the " driver initialization " information for
this printer into the tdb . Later , new printers that use this driver will
have this initialization information bound to them . This simulates the
driver initialization , as if it had run on the Samba server ( as it would
have done on NT ) .
The Win32 client side code requirement sucks ! But until we can run arbitrary
Win32 printer driver code on any Unix that Samba runs on , we are stuck with it .
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
It would have been easier to use SetPrinter because all the UNMARSHALLING of
the DEVMODE is done there , but 2 K / XP clients do not set the DEVMODE . . . think
about it and you will realize why . JRR 010720
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static WERROR save_driver_init_2 ( NT_PRINTER_INFO_LEVEL * printer , uint8 * data , uint32 data_len )
{
WERROR status = WERR_OK ;
TALLOC_CTX * ctx = NULL ;
NT_DEVICEMODE * nt_devmode = NULL ;
NT_DEVICEMODE * tmp_devmode = printer - > info_2 - > devmode ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/*
* When the DEVMODE is already set on the printer , don ' t try to unpack it .
*/
DEBUG ( 8 , ( " save_driver_init_2: Enter... \n " ) ) ;
2009-02-09 14:40:12 +03:00
2003-02-25 23:53:53 +03:00
if ( ! printer - > info_2 - > devmode & & data_len ) {
2002-09-25 19:19:00 +04:00
/*
* Set devmode on printer info , so entire printer initialization can be
* saved to tdb .
*/
2002-12-20 23:21:31 +03:00
if ( ( ctx = talloc_init ( " save_driver_init_2 " ) ) = = NULL )
2002-09-25 19:19:00 +04:00
return WERR_NOMEM ;
2004-12-07 21:25:53 +03:00
if ( ( nt_devmode = SMB_MALLOC_P ( NT_DEVICEMODE ) ) = = NULL ) {
2002-09-25 19:19:00 +04:00
status = WERR_NOMEM ;
goto done ;
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
ZERO_STRUCTP ( nt_devmode ) ;
/*
* The DEVMODE is held in the ' data ' component of the param in raw binary .
* Convert it to to a devmode structure
*/
if ( ! convert_driver_init ( ctx , nt_devmode , data , data_len ) ) {
DEBUG ( 10 , ( " save_driver_init_2: error converting DEVMODE \n " ) ) ;
status = WERR_INVALID_PARAM ;
goto done ;
}
printer - > info_2 - > devmode = nt_devmode ;
}
/*
* Pack up and add ( or update ) the DEVMODE and any current printer data to
* a ' driver init ' element in the tdb
2009-02-09 14:40:12 +03:00
*
2002-09-25 19:19:00 +04:00
*/
2004-09-23 23:24:02 +04:00
if ( update_driver_init ( printer , 2 ) ! = 0 ) {
2002-09-25 19:19:00 +04:00
DEBUG ( 10 , ( " save_driver_init_2: error updating DEVMODE \n " ) ) ;
status = WERR_NOMEM ;
goto done ;
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/*
2009-02-09 14:40:12 +03:00
* If driver initialization info was successfully saved , set the current
* printer to match it . This allows initialization of the current printer
2002-09-25 19:19:00 +04:00
* as well as the driver .
*/
2004-09-23 23:24:02 +04:00
status = mod_a_printer ( printer , 2 ) ;
2002-09-25 19:19:00 +04:00
if ( ! W_ERROR_IS_OK ( status ) ) {
DEBUG ( 10 , ( " save_driver_init_2: error setting DEVMODE on printer [%s] \n " ,
printer - > info_2 - > printername ) ) ;
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
done :
talloc_destroy ( ctx ) ;
free_nt_devicemode ( & nt_devmode ) ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
printer - > info_2 - > devmode = tmp_devmode ;
return status ;
}
/****************************************************************************
Update the driver init info ( DEVMODE and specifics ) for a printer
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
WERROR save_driver_init ( NT_PRINTER_INFO_LEVEL * printer , uint32 level , uint8 * data , uint32 data_len )
{
WERROR status = WERR_OK ;
2009-02-09 14:40:12 +03:00
2003-02-25 23:53:53 +03:00
switch ( level ) {
2002-09-25 19:19:00 +04:00
case 2 :
status = save_driver_init_2 ( printer , data , data_len ) ;
break ;
default :
status = WERR_UNKNOWN_LEVEL ;
break ;
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
return status ;
}
2000-02-07 19:17:59 +03:00
/****************************************************************************
2000-06-01 21:01:34 +04:00
Get a NT_PRINTER_INFO_LEVEL struct . It returns malloced memory .
2003-12-31 01:17:14 +03:00
Previously the code had a memory allocation problem because it always
2009-02-09 14:40:12 +03:00
used the TALLOC_CTX from the Printer_entry * . This context lasts
as a long as the original handle is open . So if the client made a lot
2003-12-31 01:17:14 +03:00
of getprinter [ data ] ( ) calls , the memory usage would climb . Now we use
2009-02-09 14:40:12 +03:00
a short lived TALLOC_CTX for printer_info_2 objects returned . We
2003-12-31 01:17:14 +03:00
still use the Printer_entry - > ctx for maintaining the cache copy though
2009-02-09 14:40:12 +03:00
since that object must live as long as the handle by definition .
2003-12-31 01:17:14 +03:00
- - jerry
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-06-01 21:01:34 +04:00
2009-02-09 14:40:12 +03:00
static WERROR get_a_printer_internal ( Printer_entry * print_hnd , NT_PRINTER_INFO_LEVEL * * pp_printer , uint32 level ,
2008-04-04 22:14:09 +04:00
const char * sharename , bool get_loc_com )
2000-02-07 19:17:59 +03:00
{
2001-09-03 12:50:59 +04:00
WERROR result ;
2004-10-02 00:34:12 +04:00
fstring servername ;
2009-02-09 14:40:12 +03:00
2000-04-07 02:48:53 +04:00
DEBUG ( 10 , ( " get_a_printer: [%s] level %u \n " , sharename , ( unsigned int ) level ) ) ;
2005-08-29 18:55:40 +04:00
if ( ! ( * pp_printer = TALLOC_ZERO_P ( NULL , NT_PRINTER_INFO_LEVEL ) ) ) {
DEBUG ( 0 , ( " get_a_printer: talloc() fail. \n " ) ) ;
return WERR_NOMEM ;
}
2003-02-25 23:53:53 +03:00
switch ( level ) {
2000-11-15 00:56:32 +03:00
case 2 :
2005-08-29 18:55:40 +04:00
if ( ! ( ( * pp_printer ) - > info_2 = TALLOC_ZERO_P ( * pp_printer , NT_PRINTER_INFO_LEVEL_2 ) ) ) {
DEBUG ( 0 , ( " get_a_printer: talloc() fail. \n " ) ) ;
TALLOC_FREE ( * pp_printer ) ;
2001-09-04 11:13:01 +04:00
return WERR_NOMEM ;
2000-06-01 21:01:34 +04:00
}
2004-10-02 00:34:12 +04:00
2009-02-09 14:40:12 +03:00
if ( print_hnd )
2004-10-02 00:34:12 +04:00
fstrcpy ( servername , print_hnd - > servername ) ;
else {
fstrcpy ( servername , " %L " ) ;
2006-07-11 22:01:26 +04:00
standard_sub_basic ( " " , " " , servername ,
sizeof ( servername ) - 1 ) ;
2004-10-02 00:34:12 +04:00
}
2005-07-15 18:26:11 +04:00
2008-04-04 22:14:09 +04:00
result = get_a_printer_2 ( ( * pp_printer ) - > info_2 ,
servername , sharename , get_loc_com ) ;
2003-07-14 23:51:34 +04:00
/* we have a new printer now. Save it with this handle */
2008-04-04 22:14:09 +04:00
2005-08-29 18:55:40 +04:00
if ( ! W_ERROR_IS_OK ( result ) ) {
TALLOC_FREE ( * pp_printer ) ;
2009-02-09 14:40:12 +03:00
DEBUG ( 10 , ( " get_a_printer: [%s] level %u returning %s \n " ,
2008-11-01 19:19:26 +03:00
sharename , ( unsigned int ) level , win_errstr ( result ) ) ) ;
2005-08-29 18:55:40 +04:00
return result ;
2000-06-01 21:01:34 +04:00
}
2005-08-29 18:55:40 +04:00
dump_a_printer ( * pp_printer , level ) ;
2009-02-09 14:40:12 +03:00
2000-02-07 19:17:59 +03:00
break ;
2009-02-09 14:40:12 +03:00
2000-02-07 19:17:59 +03:00
default :
2005-08-29 18:55:40 +04:00
TALLOC_FREE ( * pp_printer ) ;
return WERR_UNKNOWN_LEVEL ;
2000-02-07 19:17:59 +03:00
}
2009-02-09 14:40:12 +03:00
2005-08-29 18:55:40 +04:00
return WERR_OK ;
2000-02-07 19:17:59 +03:00
}
2008-04-04 22:14:09 +04:00
WERROR get_a_printer ( Printer_entry * print_hnd ,
NT_PRINTER_INFO_LEVEL * * pp_printer ,
uint32 level ,
const char * sharename )
{
return get_a_printer_internal ( print_hnd , pp_printer , level ,
sharename , true ) ;
}
WERROR get_a_printer_search ( Printer_entry * print_hnd ,
NT_PRINTER_INFO_LEVEL * * pp_printer ,
uint32 level ,
const char * sharename )
{
return get_a_printer_internal ( print_hnd , pp_printer , level ,
sharename , false ) ;
}
2000-02-07 19:17:59 +03:00
/****************************************************************************
2000-06-01 21:01:34 +04:00
Deletes a NT_PRINTER_INFO_LEVEL struct .
2000-02-07 19:17:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-06-01 21:01:34 +04:00
uint32 free_a_printer ( NT_PRINTER_INFO_LEVEL * * pp_printer , uint32 level )
2000-02-07 19:17:59 +03:00
{
2000-06-01 21:01:34 +04:00
NT_PRINTER_INFO_LEVEL * printer = * pp_printer ;
2005-08-29 18:55:40 +04:00
if ( ! printer )
2000-06-01 21:01:34 +04:00
return 0 ;
2009-02-09 14:40:12 +03:00
2003-02-25 23:53:53 +03:00
switch ( level ) {
2000-11-15 00:56:32 +03:00
case 2 :
2009-02-09 14:40:12 +03:00
if ( printer - > info_2 )
2000-06-01 21:01:34 +04:00
free_nt_printer_info_level_2 ( & printer - > info_2 ) ;
2000-02-07 19:17:59 +03:00
break ;
2003-02-25 23:53:53 +03:00
2000-02-07 19:17:59 +03:00
default :
2005-08-29 18:55:40 +04:00
DEBUG ( 0 , ( " free_a_printer: unknown level! [%d] \n " , level ) ) ;
return 1 ;
2000-02-07 19:17:59 +03:00
}
2000-06-01 21:01:34 +04:00
2005-08-29 18:55:40 +04:00
TALLOC_FREE ( * pp_printer ) ;
return 0 ;
2000-02-07 19:17:59 +03:00
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-27 03:49:08 +03:00
2010-04-15 04:46:55 +04:00
bool driver_info_ctr_to_info8 ( struct spoolss_AddDriverInfoCtr * r ,
struct spoolss_DriverInfo8 * _info8 )
2000-02-07 19:17:59 +03:00
{
2009-11-27 03:49:08 +03:00
struct spoolss_DriverInfo8 info8 ;
ZERO_STRUCT ( info8 ) ;
2009-11-16 17:52:31 +03:00
switch ( r - > level ) {
case 3 :
2009-11-27 03:49:08 +03:00
info8 . version = r - > info . info3 - > version ;
info8 . driver_name = r - > info . info3 - > driver_name ;
info8 . architecture = r - > info . info3 - > architecture ;
info8 . driver_path = r - > info . info3 - > driver_path ;
info8 . data_file = r - > info . info3 - > data_file ;
info8 . config_file = r - > info . info3 - > config_file ;
info8 . help_file = r - > info . info3 - > help_file ;
info8 . monitor_name = r - > info . info3 - > monitor_name ;
info8 . default_datatype = r - > info . info3 - > default_datatype ;
if ( r - > info . info3 - > dependent_files & & r - > info . info3 - > dependent_files - > string ) {
info8 . dependent_files = r - > info . info3 - > dependent_files - > string ;
2009-11-16 17:52:31 +03:00
}
break ;
case 6 :
2009-11-27 03:49:08 +03:00
info8 . version = r - > info . info6 - > version ;
info8 . driver_name = r - > info . info6 - > driver_name ;
info8 . architecture = r - > info . info6 - > architecture ;
info8 . driver_path = r - > info . info6 - > driver_path ;
info8 . data_file = r - > info . info6 - > data_file ;
info8 . config_file = r - > info . info6 - > config_file ;
info8 . help_file = r - > info . info6 - > help_file ;
info8 . monitor_name = r - > info . info6 - > monitor_name ;
info8 . default_datatype = r - > info . info6 - > default_datatype ;
if ( r - > info . info6 - > dependent_files & & r - > info . info6 - > dependent_files - > string ) {
info8 . dependent_files = r - > info . info6 - > dependent_files - > string ;
2009-11-16 17:52:31 +03:00
}
2009-11-27 03:49:08 +03:00
info8 . driver_date = r - > info . info6 - > driver_date ;
info8 . driver_version = r - > info . info6 - > driver_version ;
info8 . manufacturer_name = r - > info . info6 - > manufacturer_name ;
info8 . manufacturer_url = r - > info . info6 - > manufacturer_url ;
info8 . hardware_id = r - > info . info6 - > hardware_id ;
info8 . provider = r - > info . info6 - > provider ;
2009-11-16 17:52:31 +03:00
break ;
2009-11-27 03:49:08 +03:00
case 8 :
info8 . version = r - > info . info8 - > version ;
info8 . driver_name = r - > info . info8 - > driver_name ;
info8 . architecture = r - > info . info8 - > architecture ;
info8 . driver_path = r - > info . info8 - > driver_path ;
info8 . data_file = r - > info . info8 - > data_file ;
info8 . config_file = r - > info . info8 - > config_file ;
info8 . help_file = r - > info . info8 - > help_file ;
info8 . monitor_name = r - > info . info8 - > monitor_name ;
info8 . default_datatype = r - > info . info8 - > default_datatype ;
if ( r - > info . info8 - > dependent_files & & r - > info . info8 - > dependent_files - > string ) {
info8 . dependent_files = r - > info . info8 - > dependent_files - > string ;
}
if ( r - > info . info8 - > previous_names & & r - > info . info8 - > previous_names - > string ) {
info8 . previous_names = r - > info . info8 - > previous_names - > string ;
}
info8 . driver_date = r - > info . info8 - > driver_date ;
info8 . driver_version = r - > info . info8 - > driver_version ;
info8 . manufacturer_name = r - > info . info8 - > manufacturer_name ;
info8 . manufacturer_url = r - > info . info8 - > manufacturer_url ;
info8 . hardware_id = r - > info . info8 - > hardware_id ;
info8 . provider = r - > info . info8 - > provider ;
info8 . print_processor = r - > info . info8 - > print_processor ;
info8 . vendor_setup = r - > info . info8 - > vendor_setup ;
if ( r - > info . info8 - > color_profiles & & r - > info . info8 - > color_profiles - > string ) {
info8 . color_profiles = r - > info . info8 - > color_profiles - > string ;
}
info8 . inf_path = r - > info . info8 - > inf_path ;
info8 . printer_driver_attributes = r - > info . info8 - > printer_driver_attributes ;
if ( r - > info . info8 - > core_driver_dependencies & & r - > info . info8 - > core_driver_dependencies - > string ) {
info8 . core_driver_dependencies = r - > info . info8 - > core_driver_dependencies - > string ;
}
info8 . min_inbox_driver_ver_date = r - > info . info8 - > min_inbox_driver_ver_date ;
info8 . min_inbox_driver_ver_version = r - > info . info8 - > min_inbox_driver_ver_version ;
2009-11-16 17:52:31 +03:00
break ;
2009-11-27 03:49:08 +03:00
default :
2010-04-15 04:46:55 +04:00
return false ;
}
* _info8 = info8 ;
return true ;
}
uint32_t add_a_printer_driver ( TALLOC_CTX * mem_ctx ,
struct spoolss_AddDriverInfoCtr * r ,
char * * driver_name ,
uint32_t * version )
{
struct spoolss_DriverInfo8 info8 ;
ZERO_STRUCT ( info8 ) ;
DEBUG ( 10 , ( " adding a printer at level [%d] \n " , r - > level ) ) ;
if ( ! driver_info_ctr_to_info8 ( r , & info8 ) ) {
2009-11-27 03:49:08 +03:00
return - 1 ;
2000-02-07 19:17:59 +03:00
}
2009-02-09 14:40:12 +03:00
2009-11-27 03:49:08 +03:00
* driver_name = talloc_strdup ( mem_ctx , info8 . driver_name ) ;
if ( ! * driver_name ) {
return - 1 ;
}
* version = info8 . version ;
return add_a_printer_driver_8 ( & info8 ) ;
2000-02-07 19:17:59 +03:00
}
2009-11-27 03:49:08 +03:00
2000-02-07 19:17:59 +03:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-02-25 23:53:53 +03:00
2009-11-17 14:54:02 +03:00
WERROR get_a_printer_driver ( TALLOC_CTX * mem_ctx ,
2009-11-27 03:49:08 +03:00
struct spoolss_DriverInfo8 * * driver ,
2009-03-20 17:14:09 +03:00
const char * drivername , const char * architecture ,
uint32_t version )
2000-02-07 19:17:59 +03:00
{
2001-09-03 12:50:59 +04:00
WERROR result ;
2009-11-27 03:49:08 +03:00
struct spoolss_DriverInfo3 info3 ;
struct spoolss_DriverInfo8 * info8 ;
ZERO_STRUCT ( info3 ) ;
/* Sometime we just want any version of the driver */
if ( version = = DRIVER_ANY_VERSION ) {
/* look for Win2k first and then for NT4 */
result = get_a_printer_driver_3 ( mem_ctx ,
& info3 ,
drivername ,
architecture , 3 ) ;
if ( ! W_ERROR_IS_OK ( result ) ) {
result = get_a_printer_driver_3 ( mem_ctx ,
& info3 ,
drivername ,
architecture , 2 ) ;
}
} else {
result = get_a_printer_driver_3 ( mem_ctx ,
& info3 ,
drivername ,
architecture ,
version ) ;
2000-02-07 19:17:59 +03:00
}
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
if ( ! W_ERROR_IS_OK ( result ) ) {
return result ;
}
2009-11-27 03:49:08 +03:00
info8 = talloc_zero ( mem_ctx , struct spoolss_DriverInfo8 ) ;
if ( ! info8 ) {
return WERR_NOMEM ;
}
info8 - > version = info3 . version ;
info8 - > driver_name = info3 . driver_name ;
info8 - > architecture = info3 . architecture ;
info8 - > driver_path = info3 . driver_path ;
info8 - > data_file = info3 . data_file ;
info8 - > config_file = info3 . config_file ;
info8 - > help_file = info3 . help_file ;
info8 - > dependent_files = info3 . dependent_files ;
info8 - > monitor_name = info3 . monitor_name ;
info8 - > default_datatype = info3 . default_datatype ;
* driver = info8 ;
2009-11-17 14:54:02 +03:00
return WERR_OK ;
2000-02-07 19:17:59 +03:00
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
2009-11-27 03:49:08 +03:00
uint32_t free_a_printer_driver ( struct spoolss_DriverInfo8 * driver )
2009-11-17 14:54:02 +03:00
{
talloc_free ( driver ) ;
return 0 ;
2000-02-07 19:17:59 +03:00
}
2001-05-18 08:11:17 +04:00
/****************************************************************************
Determine whether or not a particular driver is currently assigned
to a printer
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
2009-11-27 03:49:08 +03:00
bool printer_driver_in_use ( const struct spoolss_DriverInfo8 * r )
2001-05-18 08:11:17 +04:00
{
2002-07-15 14:35:28 +04:00
int snum ;
int n_services = lp_numservices ( ) ;
NT_PRINTER_INFO_LEVEL * printer = NULL ;
2007-10-19 04:40:25 +04:00
bool in_use = False ;
2001-05-18 08:11:17 +04:00
2009-11-27 03:49:08 +03:00
if ( ! r ) {
return false ;
}
2001-05-18 08:11:17 +04:00
2005-03-08 01:06:25 +03:00
DEBUG ( 10 , ( " printer_driver_in_use: Beginning search through ntprinters.tdb... \n " ) ) ;
2009-02-09 14:40:12 +03:00
2001-05-18 08:11:17 +04:00
/* loop through the printers.tdb and check for the drivername */
2009-02-09 14:40:12 +03:00
2005-03-08 01:06:25 +03:00
for ( snum = 0 ; snum < n_services & & ! in_use ; snum + + ) {
2002-07-15 14:35:28 +04:00
if ( ! ( lp_snum_ok ( snum ) & & lp_print_ok ( snum ) ) )
2001-05-18 08:11:17 +04:00
continue ;
2009-02-09 14:40:12 +03:00
2003-02-25 23:53:53 +03:00
if ( ! W_ERROR_IS_OK ( get_a_printer ( NULL , & printer , 2 , lp_servicename ( snum ) ) ) )
2001-05-18 08:11:17 +04:00
continue ;
2009-02-09 14:40:12 +03:00
2009-11-27 03:49:08 +03:00
if ( strequal ( r - > driver_name , printer - > info_2 - > drivername ) )
2005-03-08 01:06:25 +03:00
in_use = True ;
2009-02-09 14:40:12 +03:00
2002-07-15 14:35:28 +04:00
free_a_printer ( & printer , 2 ) ;
}
2009-02-09 14:40:12 +03:00
2005-03-08 01:06:25 +03:00
DEBUG ( 10 , ( " printer_driver_in_use: Completed search through ntprinters.tdb... \n " ) ) ;
2009-02-09 14:40:12 +03:00
2005-03-08 01:06:25 +03:00
if ( in_use ) {
2009-11-27 03:49:08 +03:00
struct spoolss_DriverInfo8 * d ;
2005-03-08 01:06:25 +03:00
WERROR werr ;
2009-02-09 14:40:12 +03:00
2009-11-27 03:49:08 +03:00
DEBUG ( 5 , ( " printer_driver_in_use: driver \" %s \" is currently in use \n " , r - > driver_name ) ) ;
2009-02-09 14:40:12 +03:00
/* we can still remove the driver if there is one of
2005-03-08 01:06:25 +03:00
" Windows NT x86 " version 2 or 3 left */
2009-02-09 14:40:12 +03:00
2009-11-27 03:49:08 +03:00
if ( ! strequal ( " Windows NT x86 " , r - > architecture ) ) {
werr = get_a_printer_driver ( talloc_tos ( ) , & d , r - > driver_name , " Windows NT x86 " , DRIVER_ANY_VERSION ) ;
2005-03-08 01:06:25 +03:00
}
else {
2009-11-27 03:49:08 +03:00
switch ( r - > version ) {
2005-03-08 01:06:25 +03:00
case 2 :
2009-11-27 03:49:08 +03:00
werr = get_a_printer_driver ( talloc_tos ( ) , & d , r - > driver_name , " Windows NT x86 " , 3 ) ;
2005-03-08 01:06:25 +03:00
break ;
2009-02-09 14:40:12 +03:00
case 3 :
2009-11-27 03:49:08 +03:00
werr = get_a_printer_driver ( talloc_tos ( ) , & d , r - > driver_name , " Windows NT x86 " , 2 ) ;
2005-03-08 01:06:25 +03:00
break ;
default :
2009-02-09 14:40:12 +03:00
DEBUG ( 0 , ( " printer_driver_in_use: ERROR! unknown driver version (%d) \n " ,
2009-11-27 03:49:08 +03:00
r - > version ) ) ;
2005-03-08 01:06:25 +03:00
werr = WERR_UNKNOWN_PRINTER_DRIVER ;
break ;
}
}
/* now check the error code */
2009-02-09 14:40:12 +03:00
2005-03-08 01:06:25 +03:00
if ( W_ERROR_IS_OK ( werr ) ) {
/* it's ok to remove the driver, we have other architctures left */
in_use = False ;
2009-11-17 14:54:02 +03:00
free_a_printer_driver ( d ) ;
2005-03-08 01:06:25 +03:00
}
}
2009-02-09 14:40:12 +03:00
2002-07-15 14:35:28 +04:00
/* report that the driver is not in use by default */
2009-02-09 14:40:12 +03:00
2005-03-08 01:06:25 +03:00
return in_use ;
2002-07-15 14:35:28 +04:00
}
2001-05-18 08:11:17 +04:00
2002-07-15 14:35:28 +04:00
/**********************************************************************
Check to see if a ogiven file is in use by * info
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-02-09 14:40:12 +03:00
2009-11-27 03:49:08 +03:00
static bool drv_file_in_use ( const char * file , const struct spoolss_DriverInfo8 * info )
2002-07-15 14:35:28 +04:00
{
2002-09-25 19:19:00 +04:00
int i = 0 ;
2008-04-25 02:02:23 +04:00
2002-07-15 14:35:28 +04:00
if ( ! info )
return False ;
2008-04-25 02:02:23 +04:00
/* mz: skip files that are in the list but already deleted */
if ( ! file | | ! file [ 0 ] ) {
return false ;
}
2009-11-17 14:54:02 +03:00
if ( strequal ( file , info - > driver_path ) )
2002-07-15 14:35:28 +04:00
return True ;
2001-06-27 00:23:45 +04:00
2009-11-17 14:54:02 +03:00
if ( strequal ( file , info - > data_file ) )
2002-07-15 14:35:28 +04:00
return True ;
2009-11-17 14:54:02 +03:00
if ( strequal ( file , info - > config_file ) )
2002-07-15 14:35:28 +04:00
return True ;
2009-11-17 14:54:02 +03:00
if ( strequal ( file , info - > help_file ) )
2002-07-15 14:35:28 +04:00
return True ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/* see of there are any dependent files to examine */
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
if ( ! info - > dependent_files )
2002-09-25 19:19:00 +04:00
return False ;
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
while ( info - > dependent_files [ i ] & & * info - > dependent_files [ i ] ) {
if ( strequal ( file , info - > dependent_files [ i ] ) )
2002-09-25 19:19:00 +04:00
return True ;
i + + ;
2002-07-15 14:35:28 +04:00
}
2009-02-09 14:40:12 +03:00
2002-07-15 14:35:28 +04:00
return False ;
}
/**********************************************************************
2009-02-09 14:40:12 +03:00
Utility function to remove the dependent file pointed to by the
input parameter from the list
2002-07-15 14:35:28 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-17 14:54:02 +03:00
static void trim_dependent_file ( TALLOC_CTX * mem_ctx , const char * * files , int idx )
2002-07-15 14:35:28 +04:00
{
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/* bump everything down a slot */
2009-11-17 14:54:02 +03:00
while ( files & & files [ idx + 1 ] ) {
files [ idx ] = talloc_strdup ( mem_ctx , files [ idx + 1 ] ) ;
2002-09-25 19:19:00 +04:00
idx + + ;
2002-07-15 14:35:28 +04:00
}
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
files [ idx ] = NULL ;
2002-09-25 19:19:00 +04:00
2009-02-09 14:40:12 +03:00
return ;
2002-07-15 14:35:28 +04:00
}
/**********************************************************************
2009-02-09 14:40:12 +03:00
Check if any of the files used by src are also used by drv
2002-07-15 14:35:28 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-17 14:54:02 +03:00
static bool trim_overlap_drv_files ( TALLOC_CTX * mem_ctx ,
2009-11-27 03:49:08 +03:00
struct spoolss_DriverInfo8 * src ,
const struct spoolss_DriverInfo8 * drv )
2002-07-15 14:35:28 +04:00
{
2007-10-19 04:40:25 +04:00
bool in_use = False ;
2002-09-25 19:19:00 +04:00
int i = 0 ;
2009-02-09 14:40:12 +03:00
2002-07-15 14:35:28 +04:00
if ( ! src | | ! drv )
return False ;
2009-02-09 14:40:12 +03:00
2002-07-15 14:35:28 +04:00
/* check each file. Remove it from the src structure if it overlaps */
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
if ( drv_file_in_use ( src - > driver_path , drv ) ) {
2002-07-15 14:35:28 +04:00
in_use = True ;
2009-11-17 14:54:02 +03:00
DEBUG ( 10 , ( " Removing driverfile [%s] from list \n " , src - > driver_path ) ) ;
src - > driver_path = talloc_strdup ( mem_ctx , " " ) ;
if ( ! src - > driver_path ) { return false ; }
2002-07-15 14:35:28 +04:00
}
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
if ( drv_file_in_use ( src - > data_file , drv ) ) {
2002-07-15 14:35:28 +04:00
in_use = True ;
2009-11-17 14:54:02 +03:00
DEBUG ( 10 , ( " Removing datafile [%s] from list \n " , src - > data_file ) ) ;
src - > data_file = talloc_strdup ( mem_ctx , " " ) ;
if ( ! src - > data_file ) { return false ; }
2002-07-15 14:35:28 +04:00
}
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
if ( drv_file_in_use ( src - > config_file , drv ) ) {
2002-07-15 14:35:28 +04:00
in_use = True ;
2009-11-17 14:54:02 +03:00
DEBUG ( 10 , ( " Removing configfile [%s] from list \n " , src - > config_file ) ) ;
src - > config_file = talloc_strdup ( mem_ctx , " " ) ;
if ( ! src - > config_file ) { return false ; }
2002-07-15 14:35:28 +04:00
}
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
if ( drv_file_in_use ( src - > help_file , drv ) ) {
2002-09-25 19:19:00 +04:00
in_use = True ;
2009-11-17 14:54:02 +03:00
DEBUG ( 10 , ( " Removing helpfile [%s] from list \n " , src - > help_file ) ) ;
src - > help_file = talloc_strdup ( mem_ctx , " " ) ;
if ( ! src - > help_file ) { return false ; }
2002-07-15 14:35:28 +04:00
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/* are there any dependentfiles to examine? */
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
if ( ! src - > dependent_files )
2002-09-25 19:19:00 +04:00
return in_use ;
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
while ( src - > dependent_files [ i ] & & * src - > dependent_files [ i ] ) {
if ( drv_file_in_use ( src - > dependent_files [ i ] , drv ) ) {
2002-09-25 19:19:00 +04:00
in_use = True ;
2009-11-17 14:54:02 +03:00
DEBUG ( 10 , ( " Removing [%s] from dependent file list \n " , src - > dependent_files [ i ] ) ) ;
trim_dependent_file ( mem_ctx , src - > dependent_files , i ) ;
2003-02-25 23:53:53 +03:00
} else
2002-09-25 19:19:00 +04:00
i + + ;
2009-02-09 14:40:12 +03:00
}
2002-07-15 14:35:28 +04:00
return in_use ;
}
/****************************************************************************
2009-02-09 14:40:12 +03:00
Determine whether or not a particular driver files are currently being
used by any other driver .
2002-07-15 14:35:28 +04:00
Return value is True if any files were in use by other drivers
and False otherwise .
2009-02-09 14:40:12 +03:00
2002-07-15 14:35:28 +04:00
Upon return , * info has been modified to only contain the driver files
which are not in use
2008-04-25 02:02:23 +04:00
Fix from mz :
This needs to check all drivers to ensure that all files in use
have been removed from * info , not just the ones in the first
match .
2002-07-15 14:35:28 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-11-17 14:54:02 +03:00
bool printer_driver_files_in_use ( TALLOC_CTX * mem_ctx ,
2009-11-27 03:49:08 +03:00
struct spoolss_DriverInfo8 * info )
2002-07-15 14:35:28 +04:00
{
int i ;
int ndrivers ;
uint32 version ;
fstring * list = NULL ;
2009-11-27 03:49:08 +03:00
struct spoolss_DriverInfo8 * driver ;
2008-04-25 02:02:23 +04:00
bool in_use = false ;
2002-09-25 19:19:00 +04:00
if ( ! info )
return False ;
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
version = info - > version ;
2009-02-09 14:40:12 +03:00
2002-07-15 14:35:28 +04:00
/* loop over all driver versions */
2009-02-09 14:40:12 +03:00
2002-07-15 14:35:28 +04:00
DEBUG ( 5 , ( " printer_driver_files_in_use: Beginning search through ntdrivers.tdb... \n " ) ) ;
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/* get the list of drivers */
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
list = NULL ;
2009-11-17 14:54:02 +03:00
ndrivers = get_ntdrivers ( & list , info - > architecture , version ) ;
2009-02-09 14:40:12 +03:00
DEBUGADD ( 4 , ( " we have:[%d] drivers in environment [%s] and version [%d] \n " ,
2009-11-17 14:54:02 +03:00
ndrivers , info - > architecture , version ) ) ;
2002-07-15 14:35:28 +04:00
2002-09-25 19:19:00 +04:00
/* check each driver for overlap in files */
2009-02-09 14:40:12 +03:00
2003-02-25 23:53:53 +03:00
for ( i = 0 ; i < ndrivers ; i + + ) {
2002-09-25 19:19:00 +04:00
DEBUGADD ( 5 , ( " \t driver: [%s] \n " , list [ i ] ) ) ;
2009-02-09 14:40:12 +03:00
2009-11-17 14:54:02 +03:00
driver = NULL ;
2009-02-09 14:40:12 +03:00
2009-11-27 03:49:08 +03:00
if ( ! W_ERROR_IS_OK ( get_a_printer_driver ( talloc_tos ( ) , & driver , list [ i ] , info - > architecture , version ) ) ) {
2002-09-25 19:19:00 +04:00
SAFE_FREE ( list ) ;
return True ;
}
2009-02-09 14:40:12 +03:00
2002-09-25 19:19:00 +04:00
/* check if d2 uses any files from d1 */
/* only if this is a different driver than the one being deleted */
2009-02-09 14:40:12 +03:00
2009-11-27 03:49:08 +03:00
if ( ! strequal ( info - > driver_name , driver - > driver_name ) ) {
if ( trim_overlap_drv_files ( mem_ctx , info , driver ) ) {
2008-04-25 02:02:23 +04:00
/* mz: Do not instantly return -
* we need to ensure this file isn ' t
* also in use by other drivers . */
in_use = true ;
2002-07-15 14:35:28 +04:00
}
2002-09-25 19:19:00 +04:00
}
2007-11-22 00:56:36 +03:00
2009-11-17 14:54:02 +03:00
free_a_printer_driver ( driver ) ;
2007-11-22 00:56:36 +03:00
}
2002-09-25 19:19:00 +04:00
SAFE_FREE ( list ) ;
2007-11-22 00:56:36 +03:00
2002-07-15 14:35:28 +04:00
DEBUG ( 5 , ( " printer_driver_files_in_use: Completed search through ntdrivers.tdb... \n " ) ) ;
2007-11-22 00:56:36 +03:00
2008-04-25 02:02:23 +04:00
return in_use ;
2001-05-18 08:11:17 +04:00
}
2009-07-25 05:38:40 +04:00
static NTSTATUS driver_unlink_internals ( connection_struct * conn ,
const char * name )
{
struct smb_filename * smb_fname = NULL ;
NTSTATUS status ;
status = create_synthetic_smb_fname ( talloc_tos ( ) , name , NULL , NULL ,
& smb_fname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
status = unlink_internals ( conn , NULL , 0 , smb_fname , false ) ;
TALLOC_FREE ( smb_fname ) ;
return status ;
}
2002-07-15 14:35:28 +04:00
/****************************************************************************
2007-11-22 00:56:36 +03:00
Actually delete the driver files . Make sure that
printer_driver_files_in_use ( ) return False before calling
2002-07-15 14:35:28 +04:00
this .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-11-24 01:13:11 +03:00
static bool delete_driver_files ( struct pipes_struct * rpc_pipe ,
2009-11-27 03:49:08 +03:00
const struct spoolss_DriverInfo8 * r )
2002-07-15 14:35:28 +04:00
{
2002-09-25 19:19:00 +04:00
int i = 0 ;
2002-07-15 14:35:28 +04:00
char * s ;
2007-11-22 00:56:36 +03:00
const char * file ;
2002-07-15 14:35:28 +04:00
connection_struct * conn ;
NTSTATUS nt_status ;
2008-11-24 01:13:11 +03:00
char * oldcwd ;
fstring printdollar ;
int printdollar_snum ;
bool ret = false ;
2003-03-05 04:30:15 +03:00
2009-11-27 03:49:08 +03:00
if ( ! r ) {
return false ;
}
2007-11-22 00:56:36 +03:00
2009-11-17 14:54:02 +03:00
DEBUG ( 6 , ( " delete_driver_files: deleting driver [%s] - version [%d] \n " ,
2009-11-27 03:49:08 +03:00
r - > driver_name , r - > version ) ) ;
2007-11-22 00:56:36 +03:00
2008-11-24 01:13:11 +03:00
fstrcpy ( printdollar , " print$ " ) ;
2007-11-22 00:56:36 +03:00
2008-11-24 01:13:11 +03:00
printdollar_snum = find_service ( printdollar ) ;
if ( printdollar_snum = = - 1 ) {
return false ;
}
2007-11-22 00:56:36 +03:00
2008-11-24 01:13:11 +03:00
nt_status = create_conn_struct ( talloc_tos ( ) , & conn , printdollar_snum ,
lp_pathname ( printdollar_snum ) ,
rpc_pipe - > server_info , & oldcwd ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
DEBUG ( 0 , ( " delete_driver_files: create_conn_struct "
" returned %s \n " , nt_errstr ( nt_status ) ) ) ;
return false ;
2002-07-15 14:35:28 +04:00
}
2006-02-18 00:07:26 +03:00
if ( ! CAN_WRITE ( conn ) ) {
DEBUG ( 3 , ( " delete_driver_files: Cannot delete print driver when [print$] is read-only \n " ) ) ;
2008-11-24 01:13:11 +03:00
goto fail ;
2002-07-15 14:35:28 +04:00
}
2007-11-22 00:56:36 +03:00
/* now delete the files; must strip the '\print$' string from
2002-07-15 14:35:28 +04:00
fron of path */
2007-11-22 00:56:36 +03:00
2009-11-27 03:49:08 +03:00
if ( r - > driver_path & & r - > driver_path [ 0 ] ) {
if ( ( s = strchr ( & r - > driver_path [ 1 ] , ' \\ ' ) ) ! = NULL ) {
2007-11-22 00:56:36 +03:00
file = s ;
2002-07-15 14:35:28 +04:00
DEBUG ( 10 , ( " deleting driverfile [%s] \n " , s ) ) ;
2009-07-25 05:38:40 +04:00
driver_unlink_internals ( conn , file ) ;
2002-07-15 14:35:28 +04:00
}
}
2007-11-22 00:56:36 +03:00
2009-11-27 03:49:08 +03:00
if ( r - > config_file & & r - > config_file [ 0 ] ) {
if ( ( s = strchr ( & r - > config_file [ 1 ] , ' \\ ' ) ) ! = NULL ) {
2007-11-22 00:56:36 +03:00
file = s ;
2002-07-15 14:35:28 +04:00
DEBUG ( 10 , ( " deleting configfile [%s] \n " , s ) ) ;
2009-07-25 05:38:40 +04:00
driver_unlink_internals ( conn , file ) ;
2002-07-15 14:35:28 +04:00
}
}
2007-11-22 00:56:36 +03:00
2009-11-27 03:49:08 +03:00
if ( r - > data_file & & r - > data_file [ 0 ] ) {
if ( ( s = strchr ( & r - > data_file [ 1 ] , ' \\ ' ) ) ! = NULL ) {
2007-11-22 00:56:36 +03:00
file = s ;
2002-07-15 14:35:28 +04:00
DEBUG ( 10 , ( " deleting datafile [%s] \n " , s ) ) ;
2009-07-25 05:38:40 +04:00
driver_unlink_internals ( conn , file ) ;
2002-07-15 14:35:28 +04:00
}
}
2007-11-22 00:56:36 +03:00
2009-11-27 03:49:08 +03:00
if ( r - > help_file & & r - > help_file [ 0 ] ) {
if ( ( s = strchr ( & r - > help_file [ 1 ] , ' \\ ' ) ) ! = NULL ) {
2007-11-22 00:56:36 +03:00
file = s ;
2002-07-15 14:35:28 +04:00
DEBUG ( 10 , ( " deleting helpfile [%s] \n " , s ) ) ;
2009-07-25 05:38:40 +04:00
driver_unlink_internals ( conn , file ) ;
2002-07-15 14:35:28 +04:00
}
}
2007-11-22 00:56:36 +03:00
2002-09-25 19:19:00 +04:00
/* check if we are done removing files */
2007-11-22 00:56:36 +03:00
2009-11-27 03:49:08 +03:00
if ( r - > dependent_files ) {
while ( r - > dependent_files [ i ] & & r - > dependent_files [ i ] [ 0 ] ) {
2005-08-05 04:58:31 +04:00
char * p ;
2002-07-15 14:35:28 +04:00
2002-09-25 19:19:00 +04:00
/* bypass the "\print$" portion of the path */
2007-11-22 00:56:36 +03:00
2009-11-27 03:49:08 +03:00
if ( ( p = strchr ( r - > dependent_files [ i ] + 1 , ' \\ ' ) ) ! = NULL ) {
2007-11-22 00:56:36 +03:00
file = p ;
2002-09-25 19:19:00 +04:00
DEBUG ( 10 , ( " deleting dependent file [%s] \n " , file ) ) ;
2009-07-25 05:38:40 +04:00
driver_unlink_internals ( conn , file ) ;
2002-09-25 19:19:00 +04:00
}
2007-11-22 00:56:36 +03:00
2002-09-25 19:19:00 +04:00
i + + ;
2002-07-15 14:35:28 +04:00
}
}
2008-11-24 01:13:11 +03:00
goto done ;
fail :
ret = false ;
done :
if ( conn ! = NULL ) {
vfs_ChDir ( conn , oldcwd ) ;
2009-08-07 13:48:03 +04:00
conn_free ( conn ) ;
2008-11-24 01:13:11 +03:00
}
return ret ;
2002-07-15 14:35:28 +04:00
}
2001-05-18 08:11:17 +04:00
/****************************************************************************
Remove a printer driver from the TDB . This assumes that the the driver was
previously looked up .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
2008-11-24 01:13:11 +03:00
WERROR delete_printer_driver ( struct pipes_struct * rpc_pipe ,
2009-11-27 03:49:08 +03:00
const struct spoolss_DriverInfo8 * r ,
2008-11-24 01:13:11 +03:00
uint32 version , bool delete_files )
2001-05-18 08:11:17 +04:00
{
2007-11-22 00:56:36 +03:00
char * key = NULL ;
2003-04-23 17:27:35 +04:00
const char * arch ;
2007-03-27 15:22:07 +04:00
TDB_DATA dbuf ;
2001-05-18 08:11:17 +04:00
2002-07-15 14:35:28 +04:00
/* delete the tdb data first */
2001-05-18 08:11:17 +04:00
2009-11-27 03:49:08 +03:00
arch = get_short_archi ( r - > architecture ) ;
2006-06-20 01:36:19 +04:00
if ( ! arch ) {
return WERR_UNKNOWN_PRINTER_DRIVER ;
}
2007-11-22 00:56:36 +03:00
if ( asprintf ( & key , " %s%s/%d/%s " , DRIVERS_PREFIX ,
2009-11-27 03:49:08 +03:00
arch , version , r - > driver_name ) < 0 ) {
2007-11-22 00:56:36 +03:00
return WERR_NOMEM ;
}
2002-07-15 14:35:28 +04:00
DEBUG ( 5 , ( " delete_printer_driver: key = [%s] delete_files = %s \n " ,
key , delete_files ? " TRUE " : " FALSE " ) ) ;
2002-08-17 20:31:24 +04:00
/* check if the driver actually exists for this environment */
2007-11-22 00:56:36 +03:00
2007-03-27 15:22:07 +04:00
dbuf = tdb_fetch_bystring ( tdb_drivers , key ) ;
2002-08-17 20:31:24 +04:00
if ( ! dbuf . dptr ) {
2002-09-25 19:19:00 +04:00
DEBUG ( 8 , ( " delete_printer_driver: Driver unknown [%s] \n " , key ) ) ;
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
2002-08-17 20:31:24 +04:00
return WERR_UNKNOWN_PRINTER_DRIVER ;
}
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
SAFE_FREE ( dbuf . dptr ) ;
2007-11-22 00:56:36 +03:00
2002-08-17 20:31:24 +04:00
/* ok... the driver exists so the delete should return success */
2007-11-22 00:56:36 +03:00
2007-03-27 15:22:07 +04:00
if ( tdb_delete_bystring ( tdb_drivers , key ) = = - 1 ) {
2002-09-25 19:19:00 +04:00
DEBUG ( 0 , ( " delete_printer_driver: fail to delete %s! \n " , key ) ) ;
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
2001-09-04 11:13:01 +04:00
return WERR_ACCESS_DENIED ;
2001-05-18 08:11:17 +04:00
}
2002-07-15 14:35:28 +04:00
/*
* now delete any associated files if delete_files = = True
* even if this part failes , we return succes because the
* driver doesn not exist any more
*/
if ( delete_files )
2009-11-27 03:49:08 +03:00
delete_driver_files ( rpc_pipe , r ) ;
2007-11-22 00:56:36 +03:00
2002-09-25 19:19:00 +04:00
DEBUG ( 5 , ( " delete_printer_driver: driver delete successful [%s] \n " , key ) ) ;
2007-11-22 00:56:36 +03:00
SAFE_FREE ( key ) ;
2002-07-15 14:35:28 +04:00
2001-09-04 11:13:01 +04:00
return WERR_OK ;
2007-11-22 00:56:36 +03:00
}
2000-05-27 13:53:11 +04:00
/****************************************************************************
2000-08-01 04:41:19 +04:00
Store a security desc for a printer .
2000-05-27 13:53:11 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-08-01 04:41:19 +04:00
2005-06-30 23:33:47 +04:00
WERROR nt_printing_setsec ( const char * sharename , SEC_DESC_BUF * secdesc_ctr )
2000-05-27 13:53:11 +04:00
{
2000-11-15 00:56:32 +03:00
SEC_DESC_BUF * new_secdesc_ctr = NULL ;
SEC_DESC_BUF * old_secdesc_ctr = NULL ;
2000-07-27 04:47:19 +04:00
TALLOC_CTX * mem_ctx = NULL ;
2007-03-27 15:22:07 +04:00
TDB_DATA kbuf ;
2009-02-24 14:19:01 +03:00
TDB_DATA dbuf ;
DATA_BLOB blob ;
2001-09-03 14:38:13 +04:00
WERROR status ;
2009-02-24 14:19:01 +03:00
NTSTATUS nt_status ;
2000-07-10 09:08:21 +04:00
2002-12-20 23:21:31 +03:00
mem_ctx = talloc_init ( " nt_printing_setsec " ) ;
2000-11-15 00:56:32 +03:00
if ( mem_ctx = = NULL )
2001-09-03 14:38:13 +04:00
return WERR_NOMEM ;
2000-11-15 00:56:32 +03:00
/* The old owner and group sids of the security descriptor are not
present when new ACEs are added or removed by changing printer
permissions through NT . If they are NULL in the new security
descriptor then copy them over from the old one . */
2006-09-21 02:23:12 +04:00
if ( ! secdesc_ctr - > sd - > owner_sid | | ! secdesc_ctr - > sd - > group_sid ) {
2000-11-15 00:56:32 +03:00
DOM_SID * owner_sid , * group_sid ;
SEC_ACL * dacl , * sacl ;
SEC_DESC * psd = NULL ;
size_t size ;
2006-06-20 01:36:19 +04:00
if ( ! nt_printing_getsec ( mem_ctx , sharename , & old_secdesc_ctr ) ) {
status = WERR_NOMEM ;
goto out ;
}
2000-11-15 00:56:32 +03:00
/* Pick out correct owner and group sids */
2006-09-21 02:23:12 +04:00
owner_sid = secdesc_ctr - > sd - > owner_sid ?
secdesc_ctr - > sd - > owner_sid :
old_secdesc_ctr - > sd - > owner_sid ;
2000-11-15 00:56:32 +03:00
2006-09-21 02:23:12 +04:00
group_sid = secdesc_ctr - > sd - > group_sid ?
secdesc_ctr - > sd - > group_sid :
old_secdesc_ctr - > sd - > group_sid ;
2000-11-15 00:56:32 +03:00
2006-09-21 02:23:12 +04:00
dacl = secdesc_ctr - > sd - > dacl ?
secdesc_ctr - > sd - > dacl :
old_secdesc_ctr - > sd - > dacl ;
2000-11-15 00:56:32 +03:00
2006-09-21 02:23:12 +04:00
sacl = secdesc_ctr - > sd - > sacl ?
secdesc_ctr - > sd - > sacl :
old_secdesc_ctr - > sd - > sacl ;
2000-11-15 00:56:32 +03:00
/* Make a deep copy of the security descriptor */
2006-09-21 02:23:12 +04:00
psd = make_sec_desc ( mem_ctx , secdesc_ctr - > sd - > revision , secdesc_ctr - > sd - > type ,
2000-11-15 00:56:32 +03:00
owner_sid , group_sid ,
sacl ,
dacl ,
& size ) ;
2006-06-20 01:36:19 +04:00
if ( ! psd ) {
status = WERR_NOMEM ;
goto out ;
}
2001-02-28 03:51:02 +03:00
new_secdesc_ctr = make_sec_desc_buf ( mem_ctx , size , psd ) ;
2000-11-15 00:56:32 +03:00
}
if ( ! new_secdesc_ctr ) {
new_secdesc_ctr = secdesc_ctr ;
}
2000-05-27 13:53:11 +04:00
2000-07-06 11:06:05 +04:00
/* Store the security descriptor in a tdb */
2009-02-24 14:19:01 +03:00
nt_status = marshall_sec_desc_buf ( mem_ctx , new_secdesc_ctr ,
& blob . data , & blob . length ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
status = ntstatus_to_werror ( nt_status ) ;
2000-11-15 00:56:32 +03:00
goto out ;
2000-05-27 13:53:11 +04:00
}
2007-11-22 00:56:36 +03:00
kbuf = make_printers_secdesc_tdbkey ( mem_ctx , sharename ) ;
2000-05-27 13:53:11 +04:00
2009-02-24 14:19:01 +03:00
dbuf . dptr = ( unsigned char * ) blob . data ;
dbuf . dsize = blob . length ;
if ( tdb_trans_store ( tdb_printers , kbuf , dbuf , TDB_REPLACE ) = = 0 ) {
2001-09-04 11:13:01 +04:00
status = WERR_OK ;
2000-05-27 13:53:11 +04:00
} else {
2005-07-15 18:26:11 +04:00
DEBUG ( 1 , ( " Failed to store secdesc for %s \n " , sharename ) ) ;
2001-09-03 14:38:13 +04:00
status = WERR_BADFUNC ;
2000-05-27 13:53:11 +04:00
}
2001-04-09 00:01:51 +04:00
/* Free malloc'ed memory */
2009-02-24 14:19:01 +03:00
talloc_free ( blob . data ) ;
2000-07-10 09:08:21 +04:00
2000-11-15 00:56:32 +03:00
out :
2000-11-04 09:36:18 +03:00
2000-11-15 00:56:32 +03:00
if ( mem_ctx )
talloc_destroy ( mem_ctx ) ;
2000-05-27 13:53:11 +04:00
return status ;
}
/****************************************************************************
2000-06-01 21:01:34 +04:00
Construct a default security descriptor buffer for a printer .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-02-28 03:51:02 +03:00
static SEC_DESC_BUF * construct_default_printer_sdb ( TALLOC_CTX * ctx )
2000-06-01 21:01:34 +04:00
{
2005-01-20 20:42:15 +03:00
SEC_ACE ace [ 5 ] ; /* max number of ace entries */
int i = 0 ;
2008-10-09 20:49:03 +04:00
uint32_t sa ;
2000-06-07 05:49:23 +04:00
SEC_ACL * psa = NULL ;
2000-06-01 21:01:34 +04:00
SEC_DESC_BUF * sdb = NULL ;
2000-06-07 05:49:23 +04:00
SEC_DESC * psd = NULL ;
2005-06-15 00:50:37 +04:00
DOM_SID adm_sid ;
2000-06-01 21:01:34 +04:00
size_t sd_size ;
2000-06-07 05:49:23 +04:00
2000-07-10 09:08:21 +04:00
/* Create an ACE where Everyone is allowed to print */
2008-10-09 20:49:03 +04:00
sa = PRINTER_ACE_PRINT ;
2005-01-20 20:42:15 +03:00
init_sec_ace ( & ace [ i + + ] , & global_sid_World , SEC_ACE_TYPE_ACCESS_ALLOWED ,
2000-07-10 09:08:21 +04:00
sa , SEC_ACE_FLAG_CONTAINER_INHERIT ) ;
2005-01-20 20:42:15 +03:00
/* Add the domain admins group if we are a DC */
2009-02-09 14:40:12 +03:00
2005-01-20 20:42:15 +03:00
if ( IS_DC ) {
DOM_SID domadmins_sid ;
2009-02-09 14:40:12 +03:00
2010-01-10 19:39:27 +03:00
sid_compose ( & domadmins_sid , get_global_sam_sid ( ) ,
DOMAIN_GROUP_RID_ADMINS ) ;
2009-02-09 14:40:12 +03:00
2008-10-09 20:49:03 +04:00
sa = PRINTER_ACE_FULL_CONTROL ;
2009-02-09 14:40:12 +03:00
init_sec_ace ( & ace [ i + + ] , & domadmins_sid ,
SEC_ACE_TYPE_ACCESS_ALLOWED , sa ,
2005-06-15 00:50:37 +04:00
SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY ) ;
2005-01-20 20:42:15 +03:00
init_sec_ace ( & ace [ i + + ] , & domadmins_sid , SEC_ACE_TYPE_ACCESS_ALLOWED ,
2005-06-15 00:50:37 +04:00
sa , SEC_ACE_FLAG_CONTAINER_INHERIT ) ;
}
else if ( secrets_fetch_domain_sid ( lp_workgroup ( ) , & adm_sid ) ) {
sid_append_rid ( & adm_sid , DOMAIN_USER_RID_ADMIN ) ;
2005-01-20 20:42:15 +03:00
2008-10-09 20:49:03 +04:00
sa = PRINTER_ACE_FULL_CONTROL ;
2009-02-09 14:40:12 +03:00
init_sec_ace ( & ace [ i + + ] , & adm_sid ,
SEC_ACE_TYPE_ACCESS_ALLOWED , sa ,
2005-06-15 00:50:37 +04:00
SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY ) ;
init_sec_ace ( & ace [ i + + ] , & adm_sid , SEC_ACE_TYPE_ACCESS_ALLOWED ,
sa , SEC_ACE_FLAG_CONTAINER_INHERIT ) ;
2005-01-20 20:42:15 +03:00
}
2005-06-15 00:50:37 +04:00
/* add BUILTIN\Administrators as FULL CONTROL */
2008-10-09 20:49:03 +04:00
sa = PRINTER_ACE_FULL_CONTROL ;
2009-02-09 14:40:12 +03:00
init_sec_ace ( & ace [ i + + ] , & global_sid_Builtin_Administrators ,
SEC_ACE_TYPE_ACCESS_ALLOWED , sa ,
2005-06-15 00:50:37 +04:00
SEC_ACE_FLAG_OBJECT_INHERIT | SEC_ACE_FLAG_INHERIT_ONLY ) ;
2009-02-09 14:40:12 +03:00
init_sec_ace ( & ace [ i + + ] , & global_sid_Builtin_Administrators ,
2005-06-15 00:50:37 +04:00
SEC_ACE_TYPE_ACCESS_ALLOWED ,
sa , SEC_ACE_FLAG_CONTAINER_INHERIT ) ;
/* Make the security descriptor owned by the BUILTIN\Administrators */
2000-07-10 09:08:21 +04:00
/* The ACL revision number in rpc_secdesc.h differs from the one
created by NT when setting ACE entries in printer
descriptors . NT4 complains about the property being edited by a
NT5 machine . */
2005-01-20 20:42:15 +03:00
if ( ( psa = make_sec_acl ( ctx , NT4_ACL_REVISION , i , ace ) ) ! = NULL ) {
2003-09-20 01:57:43 +04:00
psd = make_sec_desc ( ctx , SEC_DESC_REVISION , SEC_DESC_SELF_RELATIVE ,
2009-02-09 14:40:12 +03:00
& global_sid_Builtin_Administrators ,
2005-06-15 00:50:37 +04:00
& global_sid_Builtin_Administrators ,
NULL , psa , & sd_size ) ;
2000-06-07 05:49:23 +04:00
}
2000-06-01 21:01:34 +04:00
if ( ! psd ) {
2002-02-09 07:10:24 +03:00
DEBUG ( 0 , ( " construct_default_printer_sd: Failed to make SEC_DESC. \n " ) ) ;
2000-06-01 21:01:34 +04:00
return NULL ;
}
2001-02-28 03:51:02 +03:00
sdb = make_sec_desc_buf ( ctx , sd_size , psd ) ;
2000-06-01 21:01:34 +04:00
2000-11-15 00:56:32 +03:00
DEBUG ( 4 , ( " construct_default_printer_sdb: size = %u. \n " ,
2000-07-10 09:08:21 +04:00
( unsigned int ) sd_size ) ) ;
2000-06-07 05:49:23 +04:00
2000-06-01 21:01:34 +04:00
return sdb ;
}
/****************************************************************************
Get a security desc for a printer .
2000-05-27 13:53:11 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-06-01 21:01:34 +04:00
2007-10-19 04:40:25 +04:00
bool nt_printing_getsec ( TALLOC_CTX * ctx , const char * sharename , SEC_DESC_BUF * * secdesc_ctr )
2000-05-27 13:53:11 +04:00
{
2007-03-27 15:22:07 +04:00
TDB_DATA kbuf ;
2009-02-24 14:19:01 +03:00
TDB_DATA dbuf ;
DATA_BLOB blob ;
2001-01-04 22:27:08 +03:00
char * temp ;
2009-02-24 14:19:01 +03:00
NTSTATUS status ;
2000-05-27 13:53:11 +04:00
2005-06-30 23:33:47 +04:00
if ( strlen ( sharename ) > 2 & & ( temp = strchr ( sharename + 2 , ' \\ ' ) ) ) {
sharename = temp + 1 ;
2003-02-25 23:53:53 +03:00
}
2001-01-04 22:27:08 +03:00
2000-07-17 06:41:38 +04:00
/* Fetch security descriptor from tdb */
2009-02-24 14:19:01 +03:00
kbuf = make_printers_secdesc_tdbkey ( ctx , sharename ) ;
2000-06-01 21:01:34 +04:00
2009-02-24 14:19:01 +03:00
dbuf = tdb_fetch ( tdb_printers , kbuf ) ;
if ( dbuf . dptr ) {
2001-11-26 06:39:16 +03:00
2009-02-24 14:19:01 +03:00
status = unmarshall_sec_desc_buf ( ctx , dbuf . dptr , dbuf . dsize ,
secdesc_ctr ) ;
SAFE_FREE ( dbuf . dptr ) ;
2001-11-26 06:39:16 +03:00
2009-02-24 14:19:01 +03:00
if ( NT_STATUS_IS_OK ( status ) ) {
return true ;
2006-06-20 01:36:19 +04:00
}
2009-02-24 14:19:01 +03:00
}
2001-11-26 06:39:16 +03:00
2009-02-24 14:19:01 +03:00
* secdesc_ctr = construct_default_printer_sdb ( ctx ) ;
if ( ! * secdesc_ctr ) {
return false ;
2000-05-27 13:53:11 +04:00
}
2009-02-24 14:19:01 +03:00
status = marshall_sec_desc_buf ( ctx , * secdesc_ctr ,
& blob . data , & blob . length ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
dbuf . dptr = ( unsigned char * ) blob . data ;
dbuf . dsize = blob . length ;
tdb_trans_store ( tdb_printers , kbuf , dbuf , TDB_REPLACE ) ;
talloc_free ( blob . data ) ;
}
2006-06-20 01:36:19 +04:00
2002-02-09 07:10:24 +03:00
/* If security descriptor is owned by S-1-1-0 and winbindd is up,
this security descriptor has been created when winbindd was
2000-07-17 06:41:38 +04:00
down . Take ownership of security descriptor . */
2006-09-21 02:23:12 +04:00
if ( sid_equal ( ( * secdesc_ctr ) - > sd - > owner_sid , & global_sid_World ) ) {
2000-07-17 06:41:38 +04:00
DOM_SID owner_sid ;
/* Change sd owner to workgroup administrator */
2002-01-26 09:02:28 +03:00
if ( secrets_fetch_domain_sid ( lp_workgroup ( ) , & owner_sid ) ) {
2000-07-17 06:41:38 +04:00
SEC_DESC_BUF * new_secdesc_ctr = NULL ;
SEC_DESC * psd = NULL ;
size_t size ;
/* Create new sd */
sid_append_rid ( & owner_sid , DOMAIN_USER_RID_ADMIN ) ;
2006-09-21 02:23:12 +04:00
psd = make_sec_desc ( ctx , ( * secdesc_ctr ) - > sd - > revision , ( * secdesc_ctr ) - > sd - > type ,
2000-07-17 06:41:38 +04:00
& owner_sid ,
2006-09-21 02:23:12 +04:00
( * secdesc_ctr ) - > sd - > group_sid ,
( * secdesc_ctr ) - > sd - > sacl ,
( * secdesc_ctr ) - > sd - > dacl ,
2000-07-17 06:41:38 +04:00
& size ) ;
2006-06-20 01:36:19 +04:00
if ( ! psd ) {
return False ;
}
2001-02-28 03:51:02 +03:00
new_secdesc_ctr = make_sec_desc_buf ( ctx , size , psd ) ;
2006-06-20 01:36:19 +04:00
if ( ! new_secdesc_ctr ) {
return False ;
}
2000-07-17 06:41:38 +04:00
/* Swap with other one */
* secdesc_ctr = new_secdesc_ctr ;
/* Set it */
2005-07-15 18:26:11 +04:00
nt_printing_setsec ( sharename , * secdesc_ctr ) ;
2000-07-17 06:41:38 +04:00
}
}
2000-12-11 22:24:59 +03:00
if ( DEBUGLEVEL > = 10 ) {
2006-09-21 02:23:12 +04:00
SEC_ACL * the_acl = ( * secdesc_ctr ) - > sd - > dacl ;
2000-12-11 22:24:59 +03:00
int i ;
2009-02-09 14:40:12 +03:00
DEBUG ( 10 , ( " secdesc_ctr for %s has %d aces: \n " ,
2005-07-15 18:26:11 +04:00
sharename , the_acl - > num_aces ) ) ;
2000-12-11 22:24:59 +03:00
2001-04-28 02:17:10 +04:00
for ( i = 0 ; i < the_acl - > num_aces ; i + + ) {
2007-12-15 23:11:36 +03:00
DEBUG ( 10 , ( " %s %d %d 0x%08x \n " ,
sid_string_dbg ( & the_acl - > aces [ i ] . trustee ) ,
2009-02-09 14:40:12 +03:00
the_acl - > aces [ i ] . type , the_acl - > aces [ i ] . flags ,
the_acl - > aces [ i ] . access_mask ) ) ;
2000-12-11 22:24:59 +03:00
}
}
2000-06-01 21:01:34 +04:00
return True ;
2000-05-27 13:53:11 +04:00
}
2000-02-07 19:17:59 +03:00
/* error code:
0 : everything OK
1 : level not implemented
2 : file doesn ' t exist
3 : can ' t allocate memory
4 : can ' t free memory
5 : non existant struct
*/
/*
A printer and a printer driver are 2 different things .
NT manages them separatelly , Samba does the same .
Why ? Simply because it ' s easier and it makes sense !
2009-02-09 14:40:12 +03:00
2000-02-07 19:17:59 +03:00
Now explanation : You have 3 printers behind your samba server ,
2000-11-15 00:56:32 +03:00
2 of them are the same make and model ( laser A and B ) . But laser B
2000-02-07 19:17:59 +03:00
has an 3000 sheet feeder and laser A doesn ' t such an option .
Your third printer is an old dot - matrix model for the accounting : - ) .
2009-02-09 14:40:12 +03:00
2000-02-07 19:17:59 +03:00
If the / usr / local / samba / lib directory ( default dir ) , you will have
5 files to describe all of this .
2009-02-09 14:40:12 +03:00
2000-02-07 19:17:59 +03:00
3 files for the printers ( 1 by printer ) :
NTprinter_laser A
NTprinter_laser B
NTprinter_accounting
2 files for the drivers ( 1 for the laser and 1 for the dot matrix )
NTdriver_printer model X
NTdriver_printer model Y
2000-11-15 00:56:32 +03:00
jfm : I should use this comment for the text file to explain
2000-02-07 19:17:59 +03:00
same thing for the forms BTW .
Je devrais mettre mes commentaires en francais , ca serait mieux : - )
*/
2001-01-04 22:27:08 +03:00
/* Convert generic access rights to printer object specific access rights.
It turns out that NT4 security descriptors use generic access rights and
NT5 the object specific ones . */
void map_printer_permissions ( SEC_DESC * sd )
{
int i ;
for ( i = 0 ; sd - > dacl & & i < sd - > dacl - > num_aces ; i + + ) {
2006-09-21 02:23:12 +04:00
se_map_generic ( & sd - > dacl - > aces [ i ] . access_mask ,
2001-01-04 22:27:08 +03:00
& printer_generic_mapping ) ;
}
}
2008-05-06 03:24:52 +04:00
void map_job_permissions ( SEC_DESC * sd )
{
int i ;
for ( i = 0 ; sd - > dacl & & i < sd - > dacl - > num_aces ; i + + ) {
se_map_generic ( & sd - > dacl - > aces [ i ] . access_mask ,
& job_generic_mapping ) ;
}
}
2000-08-09 08:19:18 +04:00
/****************************************************************************
2001-01-19 19:57:39 +03:00
Check a user has permissions to perform the given operation . We use the
permission constants defined in include / rpc_spoolss . h to check the various
actions we perform when checking printer access .
2000-09-01 22:49:26 +04:00
PRINTER_ACCESS_ADMINISTER :
print_queue_pause , print_queue_resume , update_printer_sec ,
update_printer , spoolss_addprinterex_level_2 ,
_spoolss_setprinterdata
2000-11-15 00:56:32 +03:00
2000-09-01 22:49:26 +04:00
PRINTER_ACCESS_USE :
print_job_start
2001-01-19 19:57:39 +03:00
JOB_ACCESS_ADMINISTER :
2000-09-01 22:49:26 +04:00
print_job_delete , print_job_pause , print_job_resume ,
print_queue_purge
2000-06-16 12:20:44 +04:00
2005-01-15 00:24:15 +03:00
Try access control in the following order ( for performance reasons ) :
2009-04-27 19:54:23 +04:00
1 ) root and SE_PRINT_OPERATOR can do anything ( easy check )
2005-01-15 00:24:15 +03:00
2 ) check security descriptor ( bit comparisons in memory )
3 ) " printer admins " ( may result in numerous calls to winbind )
2000-08-09 08:19:18 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-06-24 18:03:28 +04:00
bool print_access_check ( struct auth_serversupplied_info * server_info , int snum ,
int access_type )
2000-06-16 12:20:44 +04:00
{
SEC_DESC_BUF * secdesc = NULL ;
2001-09-04 11:13:01 +04:00
uint32 access_granted ;
NTSTATUS status ;
2002-07-15 14:35:28 +04:00
const char * pname ;
2001-02-28 03:51:02 +03:00
TALLOC_CTX * mem_ctx = NULL ;
2005-01-17 18:23:11 +03:00
SE_PRIV se_printop = SE_PRINT_OPERATOR ;
2009-02-09 14:40:12 +03:00
2000-09-01 22:49:26 +04:00
/* If user is NULL then use the current_user structure */
2005-01-15 00:24:15 +03:00
/* Always allow root or SE_PRINT_OPERATROR to do anything */
2000-09-01 22:49:26 +04:00
2009-04-20 02:57:53 +04:00
if ( server_info - > utok . uid = = sec_initial_uid ( )
2008-06-24 18:03:28 +04:00
| | user_has_privileges ( server_info - > ptok , & se_printop ) ) {
2000-08-09 08:19:18 +04:00
return True ;
}
2000-06-16 12:20:44 +04:00
2000-08-09 08:19:18 +04:00
/* Get printer name */
2000-09-01 22:49:26 +04:00
2000-07-06 11:06:05 +04:00
pname = PRINTERNAME ( snum ) ;
2000-09-01 22:49:26 +04:00
2000-08-24 03:05:49 +04:00
if ( ! pname | | ! * pname ) {
errno = EACCES ;
return False ;
}
2000-06-16 12:20:44 +04:00
2000-08-09 08:19:18 +04:00
/* Get printer security descriptor */
2000-09-01 22:49:26 +04:00
2002-12-20 23:21:31 +03:00
if ( ! ( mem_ctx = talloc_init ( " print_access_check " ) ) ) {
2001-02-28 03:51:02 +03:00
errno = ENOMEM ;
return False ;
}
2006-06-20 01:36:19 +04:00
if ( ! nt_printing_getsec ( mem_ctx , pname , & secdesc ) ) {
talloc_destroy ( mem_ctx ) ;
errno = ENOMEM ;
return False ;
}
2001-01-19 19:57:39 +03:00
if ( access_type = = JOB_ACCESS_ADMINISTER ) {
SEC_DESC_BUF * parent_secdesc = secdesc ;
/* Create a child security descriptor to check permissions
against . This is because print jobs are child objects
objects of a printer . */
2008-10-29 23:27:14 +03:00
status = se_create_child_secdesc_buf ( mem_ctx , & secdesc , parent_secdesc - > sd , False ) ;
2001-01-19 19:57:39 +03:00
2008-10-29 23:27:14 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2006-06-20 01:36:19 +04:00
talloc_destroy ( mem_ctx ) ;
2008-10-29 23:27:14 +03:00
errno = map_errno_from_nt_status ( status ) ;
2006-06-20 01:36:19 +04:00
return False ;
}
2008-05-06 03:24:52 +04:00
map_job_permissions ( secdesc - > sd ) ;
} else {
map_printer_permissions ( secdesc - > sd ) ;
2001-01-19 19:57:39 +03:00
}
2001-01-04 22:27:08 +03:00
2008-05-06 03:24:52 +04:00
/* Check access */
2008-10-31 20:51:45 +03:00
status = se_access_check ( secdesc - > sd , server_info - > ptok , access_type ,
& access_granted ) ;
2000-06-16 12:20:44 +04:00
2008-10-31 20:51:45 +03:00
DEBUG ( 4 , ( " access check was %s \n " , NT_STATUS_IS_OK ( status ) ? " SUCCESS " : " FAILURE " ) ) ;
2000-06-16 12:20:44 +04:00
2005-01-15 00:24:15 +03:00
/* see if we need to try the printer admin list */
2010-03-18 13:21:15 +03:00
if ( ! NT_STATUS_IS_OK ( status ) & &
2008-06-24 18:03:28 +04:00
( token_contains_name_in_list ( uidtoname ( server_info - > utok . uid ) ,
NULL , NULL , server_info - > ptok ,
2006-02-04 01:19:41 +03:00
lp_printer_admin ( snum ) ) ) ) {
2006-06-20 01:36:19 +04:00
talloc_destroy ( mem_ctx ) ;
2006-02-04 01:19:41 +03:00
return True ;
2005-01-15 00:24:15 +03:00
}
2001-02-28 03:51:02 +03:00
talloc_destroy ( mem_ctx ) ;
2009-02-09 14:40:12 +03:00
2008-10-31 20:51:45 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2000-08-24 03:05:49 +04:00
errno = EACCES ;
2006-06-20 01:36:19 +04:00
}
2000-08-24 03:05:49 +04:00
2008-10-31 20:51:45 +03:00
return NT_STATUS_IS_OK ( status ) ;
2000-06-16 12:20:44 +04:00
}
2000-08-01 04:41:19 +04:00
/****************************************************************************
Check the time parameters allow a print operation .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool print_time_access_check ( const char * servicename )
2000-08-01 04:41:19 +04:00
{
NT_PRINTER_INFO_LEVEL * printer = NULL ;
2007-10-19 04:40:25 +04:00
bool ok = False ;
2000-08-01 04:41:19 +04:00
time_t now = time ( NULL ) ;
struct tm * t ;
uint32 mins ;
2006-07-11 22:01:26 +04:00
if ( ! W_ERROR_IS_OK ( get_a_printer ( NULL , & printer , 2 , servicename ) ) )
2000-08-01 04:41:19 +04:00
return False ;
if ( printer - > info_2 - > starttime = = 0 & & printer - > info_2 - > untiltime = = 0 )
ok = True ;
t = gmtime ( & now ) ;
mins = ( uint32 ) t - > tm_hour * 60 + ( uint32 ) t - > tm_min ;
if ( mins > = printer - > info_2 - > starttime & & mins < = printer - > info_2 - > untiltime )
ok = True ;
free_a_printer ( & printer , 2 ) ;
2000-09-13 06:24:35 +04:00
if ( ! ok )
errno = EACCES ;
2000-08-01 04:41:19 +04:00
return ok ;
}
2004-10-02 00:34:12 +04:00
/****************************************************************************
Fill in the servername sent in the _spoolss_open_printer_ex ( ) call
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-07-15 18:26:11 +04:00
2004-10-02 00:34:12 +04:00
char * get_server_name ( Printer_entry * printer )
{
return printer - > servername ;
}
2005-07-15 18:26:11 +04:00