2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (c) Andrew Tridgell 1992-2000,
5 * Copyright (c) Jean François Micouleau 1998-2000.
6 * Copyright (c) Gerald Carter 2002-2005.
7 * Copyright (c) Andreas Schneider 2010.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "system/filesys.h"
25 #include "printing/nt_printing_tdb.h"
26 #include "librpc/gen_ndr/spoolss.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "libcli/security/security.h"
30 #include "lib/util/string_wrappers.h"
32 #define FORMS_PREFIX "FORMS/"
33 #define DRIVERS_PREFIX "DRIVERS/"
34 #define PRINTERS_PREFIX "PRINTERS/"
35 #define SECDESC_PREFIX "SECDESC/"
37 #define NTDRIVERS_DATABASE_VERSION_1 1
38 #define NTDRIVERS_DATABASE_VERSION_2 2
39 #define NTDRIVERS_DATABASE_VERSION_3 3 /* little endian version of v2 */
40 #define NTDRIVERS_DATABASE_VERSION_4 4 /* fix generic bits in security descriptors */
41 #define NTDRIVERS_DATABASE_VERSION_5 5 /* normalize keys in ntprinters.tdb */
43 static TDB_CONTEXT
*tdb_forms
; /* used for forms files */
44 static TDB_CONTEXT
*tdb_drivers
; /* used for driver files */
45 static TDB_CONTEXT
*tdb_printers
; /* used for printers files */
47 /****************************************************************************
48 generate a new TDB_DATA key for storing a printer
49 ****************************************************************************/
51 static TDB_DATA
make_printer_tdbkey(TALLOC_CTX
*ctx
, const char *sharename
)
57 fstrcpy(share
, sharename
);
58 (void)strlower_m(share
);
60 keystr
= talloc_asprintf(ctx
, "%s%s", PRINTERS_PREFIX
, share
);
61 key
= string_term_tdb_data(keystr
? keystr
: "");
66 /****************************************************************************
67 generate a new TDB_DATA key for storing a printer security descriptor
68 ****************************************************************************/
70 static TDB_DATA
make_printers_secdesc_tdbkey(TALLOC_CTX
*ctx
,
71 const char* sharename
)
77 fstrcpy(share
, sharename
);
78 (void)strlower_m(share
);
80 keystr
= talloc_asprintf(ctx
, "%s%s", SECDESC_PREFIX
, share
);
81 key
= string_term_tdb_data(keystr
? keystr
: "");
86 /****************************************************************************
87 Upgrade the tdb files to version 3
88 ****************************************************************************/
90 static bool upgrade_to_version_3(void)
92 TDB_DATA kbuf
, newkey
, dbuf
;
94 DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
96 for (kbuf
= tdb_firstkey(tdb_drivers
); kbuf
.dptr
;
97 newkey
= tdb_nextkey(tdb_drivers
, kbuf
), free(kbuf
.dptr
), kbuf
=newkey
) {
99 dbuf
= tdb_fetch(tdb_drivers
, kbuf
);
101 if (strncmp((const char *)kbuf
.dptr
, FORMS_PREFIX
, strlen(FORMS_PREFIX
)) == 0) {
102 DEBUG(0,("upgrade_to_version_3:moving form\n"));
103 if (tdb_store(tdb_forms
, kbuf
, dbuf
, TDB_REPLACE
) != 0) {
104 SAFE_FREE(dbuf
.dptr
);
105 DEBUG(0,("upgrade_to_version_3: failed to move form. Error (%s).\n", tdb_errorstr(tdb_forms
)));
108 if (tdb_delete(tdb_drivers
, kbuf
) != 0) {
109 SAFE_FREE(dbuf
.dptr
);
110 DEBUG(0,("upgrade_to_version_3: failed to delete form. Error (%s)\n", tdb_errorstr(tdb_drivers
)));
115 if (strncmp((const char *)kbuf
.dptr
, PRINTERS_PREFIX
, strlen(PRINTERS_PREFIX
)) == 0) {
116 DEBUG(0,("upgrade_to_version_3:moving printer\n"));
117 if (tdb_store(tdb_printers
, kbuf
, dbuf
, TDB_REPLACE
) != 0) {
118 SAFE_FREE(dbuf
.dptr
);
119 DEBUG(0,("upgrade_to_version_3: failed to move printer. Error (%s)\n", tdb_errorstr(tdb_printers
)));
122 if (tdb_delete(tdb_drivers
, kbuf
) != 0) {
123 SAFE_FREE(dbuf
.dptr
);
124 DEBUG(0,("upgrade_to_version_3: failed to delete printer. Error (%s)\n", tdb_errorstr(tdb_drivers
)));
129 if (strncmp((const char *)kbuf
.dptr
, SECDESC_PREFIX
, strlen(SECDESC_PREFIX
)) == 0) {
130 DEBUG(0,("upgrade_to_version_3:moving secdesc\n"));
131 if (tdb_store(tdb_printers
, kbuf
, dbuf
, TDB_REPLACE
) != 0) {
132 SAFE_FREE(dbuf
.dptr
);
133 DEBUG(0,("upgrade_to_version_3: failed to move secdesc. Error (%s)\n", tdb_errorstr(tdb_printers
)));
136 if (tdb_delete(tdb_drivers
, kbuf
) != 0) {
137 SAFE_FREE(dbuf
.dptr
);
138 DEBUG(0,("upgrade_to_version_3: failed to delete secdesc. Error (%s)\n", tdb_errorstr(tdb_drivers
)));
143 SAFE_FREE(dbuf
.dptr
);
149 /*******************************************************************
150 Fix an issue with security descriptors. Printer sec_desc must
151 use more than the generic bits that were previously used
152 in <= 3.0.14a. They must also have a owner and group SID assigned.
153 Otherwise, any printers than have been migrated to a Windows
154 host using printmig.exe will not be accessible.
155 *******************************************************************/
157 static int sec_desc_upg_fn( TDB_CONTEXT
*the_tdb
, TDB_DATA key
,
158 TDB_DATA data
, void *state
)
161 struct sec_desc_buf
*sd_orig
= NULL
;
162 struct sec_desc_buf
*sd_new
, *sd_store
;
163 struct security_descriptor
*sec
, *new_sec
;
164 TALLOC_CTX
*ctx
= state
;
168 if (!data
.dptr
|| data
.dsize
== 0) {
172 if ( strncmp((const char *) key
.dptr
, SECDESC_PREFIX
, strlen(SECDESC_PREFIX
) ) != 0 ) {
176 /* upgrade the security descriptor */
178 status
= unmarshall_sec_desc_buf(ctx
, data
.dptr
, data
.dsize
, &sd_orig
);
179 if (!NT_STATUS_IS_OK(status
)) {
180 /* delete bad entries */
181 DEBUG(0,("sec_desc_upg_fn: Failed to parse original sec_desc for %si. Deleting....\n",
182 (const char *)key
.dptr
));
183 tdb_delete( tdb_printers
, key
);
192 /* is this even valid? */
198 /* update access masks */
200 for ( i
=0; i
<sec
->dacl
->num_aces
; i
++ ) {
201 switch ( sec
->dacl
->aces
[i
].access_mask
) {
202 case (GENERIC_READ_ACCESS
| GENERIC_WRITE_ACCESS
| GENERIC_EXECUTE_ACCESS
):
203 sec
->dacl
->aces
[i
].access_mask
= PRINTER_ACE_PRINT
;
206 case GENERIC_ALL_ACCESS
:
207 sec
->dacl
->aces
[i
].access_mask
= PRINTER_ACE_FULL_CONTROL
;
210 case READ_CONTROL_ACCESS
:
211 sec
->dacl
->aces
[i
].access_mask
= PRINTER_ACE_MANAGE_DOCUMENTS
;
214 default: /* no change */
219 /* create a new struct security_descriptor with the appropriate owner and group SIDs */
221 new_sec
= make_sec_desc( ctx
, SD_REVISION
, SEC_DESC_SELF_RELATIVE
,
222 &global_sid_Builtin_Administrators
,
223 &global_sid_Builtin_Administrators
,
224 NULL
, NULL
, &size_new_sec
);
228 sd_new
= make_sec_desc_buf( ctx
, size_new_sec
, new_sec
);
233 if ( !(sd_store
= sec_desc_merge_buf( ctx
, sd_new
, sd_orig
)) ) {
234 DEBUG(0,("sec_desc_upg_fn: Failed to update sec_desc for %s\n", key
.dptr
));
240 status
= marshall_sec_desc_buf(ctx
, sd_store
, &data
.dptr
, &data
.dsize
);
241 if (!NT_STATUS_IS_OK(status
)) {
242 DEBUG(0,("sec_desc_upg_fn: Failed to parse new sec_desc for %s\n", key
.dptr
));
246 result
= tdb_store( tdb_printers
, key
, data
, TDB_REPLACE
);
248 /* 0 to continue and non-zero to stop traversal */
250 return (result
!= 0);
253 /*******************************************************************
254 Upgrade the tdb files to version 4
255 *******************************************************************/
257 static bool upgrade_to_version_4(void)
262 DEBUG(0,("upgrade_to_version_4: upgrading printer security descriptors\n"));
264 if ( !(ctx
= talloc_init( "upgrade_to_version_4" )) )
267 result
= tdb_traverse( tdb_printers
, sec_desc_upg_fn
, ctx
);
269 talloc_destroy( ctx
);
271 return ( result
>= 0 );
274 /*******************************************************************
275 Fix an issue with security descriptors. Printer sec_desc must
276 use more than the generic bits that were previously used
277 in <= 3.0.14a. They must also have a owner and group SID assigned.
278 Otherwise, any printers than have been migrated to a Windows
279 host using printmig.exe will not be accessible.
280 *******************************************************************/
282 static int normalize_printers_fn( TDB_CONTEXT
*the_tdb
, TDB_DATA key
,
283 TDB_DATA data
, void *state
)
285 TALLOC_CTX
*ctx
= talloc_tos();
288 if (!data
.dptr
|| data
.dsize
== 0)
291 /* upgrade printer records and security descriptors */
293 if ( strncmp((const char *) key
.dptr
, PRINTERS_PREFIX
, strlen(PRINTERS_PREFIX
) ) == 0 ) {
294 new_key
= make_printer_tdbkey(ctx
, (const char *)key
.dptr
+strlen(PRINTERS_PREFIX
) );
296 else if ( strncmp((const char *) key
.dptr
, SECDESC_PREFIX
, strlen(SECDESC_PREFIX
) ) == 0 ) {
297 new_key
= make_printers_secdesc_tdbkey(ctx
, (const char *)key
.dptr
+strlen(SECDESC_PREFIX
) );
300 /* ignore this record */
304 /* delete the original record and store under the normalized key */
306 if ( tdb_delete( the_tdb
, key
) != 0 ) {
307 DEBUG(0,("normalize_printers_fn: tdb_delete for [%s] failed!\n",
312 if ( tdb_store( the_tdb
, new_key
, data
, TDB_REPLACE
) != 0 ) {
313 DEBUG(0,("normalize_printers_fn: failed to store new record for [%s]!\n",
321 /*******************************************************************
322 Upgrade the tdb files to version 5
323 *******************************************************************/
325 static bool upgrade_to_version_5(void)
330 DEBUG(0,("upgrade_to_version_5: normalizing printer keys\n"));
332 if ( !(ctx
= talloc_init( "upgrade_to_version_5" )) )
335 result
= tdb_traverse( tdb_printers
, normalize_printers_fn
, NULL
);
337 talloc_destroy( ctx
);
339 return ( result
>= 0 );
342 bool nt_printing_tdb_upgrade(void)
348 bool printers_exists
;
350 const char *vstring
= "INFO/version";
354 drivers_path
= state_path(talloc_tos(), "ntdrivers.tdb");
355 if (drivers_path
== NULL
) {
359 printers_path
= state_path(talloc_tos(), "ntprinters.tdb");
360 if (printers_path
== NULL
) {
364 forms_path
= state_path(talloc_tos(), "ntforms.tdb");
365 if (forms_path
== NULL
) {
370 drivers_exists
= file_exist(drivers_path
);
371 printers_exists
= file_exist(printers_path
);
372 forms_exists
= file_exist(forms_path
);
374 if (!drivers_exists
&& !printers_exists
&& !forms_exists
) {
376 goto err_formsdb_free
;
379 tdb_drivers
= tdb_open_log(drivers_path
,
384 if (tdb_drivers
== NULL
) {
385 DEBUG(0,("nt_printing_init: Failed to open nt drivers "
386 "database %s (%s)\n",
387 drivers_path
, strerror(errno
)));
389 goto err_formsdb_free
;
392 tdb_printers
= tdb_open_log(printers_path
,
397 if (tdb_printers
== NULL
) {
398 DEBUG(0,("nt_printing_init: Failed to open nt printers "
399 "database %s (%s)\n",
400 printers_path
, strerror(errno
)));
402 goto err_drvdb_close
;
405 tdb_forms
= tdb_open_log(forms_path
,
410 if (tdb_forms
== NULL
) {
411 DEBUG(0,("nt_printing_init: Failed to open nt forms "
412 "database %s (%s)\n",
413 forms_path
, strerror(errno
)));
419 vers_id
= tdb_fetch_int32(tdb_drivers
, vstring
);
421 DEBUG(10, ("Fresh database\n"));
422 tdb_store_int32(tdb_drivers
, vstring
, NTDRIVERS_DATABASE_VERSION_5
);
423 vers_id
= NTDRIVERS_DATABASE_VERSION_5
;
426 if (vers_id
!= NTDRIVERS_DATABASE_VERSION_5
) {
427 if ((vers_id
== NTDRIVERS_DATABASE_VERSION_1
) ||
428 (IREV(vers_id
) == NTDRIVERS_DATABASE_VERSION_1
)) {
429 if (!upgrade_to_version_3()) {
431 goto err_formsdb_close
;
434 tdb_store_int32(tdb_drivers
, vstring
, NTDRIVERS_DATABASE_VERSION_3
);
435 vers_id
= NTDRIVERS_DATABASE_VERSION_3
;
438 if ((vers_id
== NTDRIVERS_DATABASE_VERSION_2
) ||
439 (IREV(vers_id
) == NTDRIVERS_DATABASE_VERSION_2
)) {
441 * Written on a bigendian machine with old fetch_int
442 * code. Save as le. The only upgrade between V2 and V3
443 * is to save the version in little-endian.
445 tdb_store_int32(tdb_drivers
, vstring
, NTDRIVERS_DATABASE_VERSION_3
);
446 vers_id
= NTDRIVERS_DATABASE_VERSION_3
;
449 if (vers_id
== NTDRIVERS_DATABASE_VERSION_3
) {
450 if (!upgrade_to_version_4()) {
452 goto err_formsdb_close
;
454 tdb_store_int32(tdb_drivers
, vstring
, NTDRIVERS_DATABASE_VERSION_4
);
455 vers_id
= NTDRIVERS_DATABASE_VERSION_4
;
458 if (vers_id
== NTDRIVERS_DATABASE_VERSION_4
) {
459 if (!upgrade_to_version_5()) {
461 goto err_formsdb_close
;
463 tdb_store_int32(tdb_drivers
, vstring
, NTDRIVERS_DATABASE_VERSION_5
);
464 vers_id
= NTDRIVERS_DATABASE_VERSION_5
;
467 if (vers_id
!= NTDRIVERS_DATABASE_VERSION_5
) {
468 DEBUG(0,("nt_printing_init: Unknown printer database version [%d]\n", vers_id
));
470 goto err_formsdb_close
;
477 tdb_close(tdb_forms
);
482 tdb_close(tdb_printers
);
487 tdb_close(tdb_drivers
);
491 talloc_free(forms_path
);
493 talloc_free(printers_path
);
495 talloc_free(drivers_path
);