s3: Lift the smbd_messaging_context from check_published_printers
[Samba/gbeck.git] / source3 / printing / nt_printing_tdb.c
blob90f11eee743ebd320cca2c71e28b67993a8bb523
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (c) Andrew Tridgell 1992-2000,
5 * Copyright (c) Jean François Micouleau 1998-2000.
6 * Copyright (c) Gerald Carter 2002-2005.
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/>.
23 #include "includes.h"
24 #include "printing/nt_printing_tdb.h"
25 #include "librpc/gen_ndr/spoolss.h"
26 #include "librpc/gen_ndr/ndr_security.h"
28 #define FORMS_PREFIX "FORMS/"
29 #define DRIVERS_PREFIX "DRIVERS/"
30 #define PRINTERS_PREFIX "PRINTERS/"
31 #define SECDESC_PREFIX "SECDESC/"
33 #define NTDRIVERS_DATABASE_VERSION_1 1
34 #define NTDRIVERS_DATABASE_VERSION_2 2
35 #define NTDRIVERS_DATABASE_VERSION_3 3 /* little endian version of v2 */
36 #define NTDRIVERS_DATABASE_VERSION_4 4 /* fix generic bits in security descriptors */
37 #define NTDRIVERS_DATABASE_VERSION_5 5 /* normalize keys in ntprinters.tdb */
39 static TDB_CONTEXT *tdb_forms; /* used for forms files */
40 static TDB_CONTEXT *tdb_drivers; /* used for driver files */
41 static TDB_CONTEXT *tdb_printers; /* used for printers files */
43 /****************************************************************************
44 generate a new TDB_DATA key for storing a printer
45 ****************************************************************************/
47 static TDB_DATA make_printer_tdbkey(TALLOC_CTX *ctx, const char *sharename )
49 fstring share;
50 char *keystr = NULL;
51 TDB_DATA key;
53 fstrcpy(share, sharename);
54 strlower_m(share);
56 keystr = talloc_asprintf(ctx, "%s%s", PRINTERS_PREFIX, share);
57 key = string_term_tdb_data(keystr ? keystr : "");
59 return key;
62 /****************************************************************************
63 generate a new TDB_DATA key for storing a printer security descriptor
64 ****************************************************************************/
66 static TDB_DATA make_printers_secdesc_tdbkey(TALLOC_CTX *ctx,
67 const char* sharename )
69 fstring share;
70 char *keystr = NULL;
71 TDB_DATA key;
73 fstrcpy(share, sharename );
74 strlower_m(share);
76 keystr = talloc_asprintf(ctx, "%s%s", SECDESC_PREFIX, share);
77 key = string_term_tdb_data(keystr ? keystr : "");
79 return key;
82 /****************************************************************************
83 Upgrade the tdb files to version 3
84 ****************************************************************************/
86 static bool upgrade_to_version_3(void)
88 TDB_DATA kbuf, newkey, dbuf;
90 DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
92 for (kbuf = tdb_firstkey(tdb_drivers); kbuf.dptr;
93 newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
95 dbuf = tdb_fetch(tdb_drivers, kbuf);
97 if (strncmp((const char *)kbuf.dptr, FORMS_PREFIX, strlen(FORMS_PREFIX)) == 0) {
98 DEBUG(0,("upgrade_to_version_3:moving form\n"));
99 if (tdb_store(tdb_forms, kbuf, dbuf, TDB_REPLACE) != 0) {
100 SAFE_FREE(dbuf.dptr);
101 DEBUG(0,("upgrade_to_version_3: failed to move form. Error (%s).\n", tdb_errorstr(tdb_forms)));
102 return False;
104 if (tdb_delete(tdb_drivers, kbuf) != 0) {
105 SAFE_FREE(dbuf.dptr);
106 DEBUG(0,("upgrade_to_version_3: failed to delete form. Error (%s)\n", tdb_errorstr(tdb_drivers)));
107 return False;
111 if (strncmp((const char *)kbuf.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX)) == 0) {
112 DEBUG(0,("upgrade_to_version_3:moving printer\n"));
113 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
114 SAFE_FREE(dbuf.dptr);
115 DEBUG(0,("upgrade_to_version_3: failed to move printer. Error (%s)\n", tdb_errorstr(tdb_printers)));
116 return False;
118 if (tdb_delete(tdb_drivers, kbuf) != 0) {
119 SAFE_FREE(dbuf.dptr);
120 DEBUG(0,("upgrade_to_version_3: failed to delete printer. Error (%s)\n", tdb_errorstr(tdb_drivers)));
121 return False;
125 if (strncmp((const char *)kbuf.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX)) == 0) {
126 DEBUG(0,("upgrade_to_version_3:moving secdesc\n"));
127 if (tdb_store(tdb_printers, kbuf, dbuf, TDB_REPLACE) != 0) {
128 SAFE_FREE(dbuf.dptr);
129 DEBUG(0,("upgrade_to_version_3: failed to move secdesc. Error (%s)\n", tdb_errorstr(tdb_printers)));
130 return False;
132 if (tdb_delete(tdb_drivers, kbuf) != 0) {
133 SAFE_FREE(dbuf.dptr);
134 DEBUG(0,("upgrade_to_version_3: failed to delete secdesc. Error (%s)\n", tdb_errorstr(tdb_drivers)));
135 return False;
139 SAFE_FREE(dbuf.dptr);
142 return True;
145 /*******************************************************************
146 Fix an issue with security descriptors. Printer sec_desc must
147 use more than the generic bits that were previously used
148 in <= 3.0.14a. They must also have a owner and group SID assigned.
149 Otherwise, any printers than have been migrated to a Windows
150 host using printmig.exe will not be accessible.
151 *******************************************************************/
153 static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
154 TDB_DATA data, void *state )
156 NTSTATUS status;
157 struct sec_desc_buf *sd_orig = NULL;
158 struct sec_desc_buf *sd_new, *sd_store;
159 struct security_descriptor *sec, *new_sec;
160 TALLOC_CTX *ctx = state;
161 int result, i;
162 uint32 sd_size;
163 size_t size_new_sec;
165 if (!data.dptr || data.dsize == 0) {
166 return 0;
169 if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) != 0 ) {
170 return 0;
173 /* upgrade the security descriptor */
175 status = unmarshall_sec_desc_buf(ctx, data.dptr, data.dsize, &sd_orig);
176 if (!NT_STATUS_IS_OK(status)) {
177 /* delete bad entries */
178 DEBUG(0,("sec_desc_upg_fn: Failed to parse original sec_desc for %si. Deleting....\n",
179 (const char *)key.dptr ));
180 tdb_delete( tdb_printers, key );
181 return 0;
184 if (!sd_orig) {
185 return 0;
187 sec = sd_orig->sd;
189 /* is this even valid? */
191 if ( !sec->dacl ) {
192 return 0;
195 /* update access masks */
197 for ( i=0; i<sec->dacl->num_aces; i++ ) {
198 switch ( sec->dacl->aces[i].access_mask ) {
199 case (GENERIC_READ_ACCESS | GENERIC_WRITE_ACCESS | GENERIC_EXECUTE_ACCESS):
200 sec->dacl->aces[i].access_mask = PRINTER_ACE_PRINT;
201 break;
203 case GENERIC_ALL_ACCESS:
204 sec->dacl->aces[i].access_mask = PRINTER_ACE_FULL_CONTROL;
205 break;
207 case READ_CONTROL_ACCESS:
208 sec->dacl->aces[i].access_mask = PRINTER_ACE_MANAGE_DOCUMENTS;
210 default: /* no change */
211 break;
215 /* create a new struct security_descriptor with the appropriate owner and group SIDs */
217 new_sec = make_sec_desc( ctx, SD_REVISION, SEC_DESC_SELF_RELATIVE,
218 &global_sid_Builtin_Administrators,
219 &global_sid_Builtin_Administrators,
220 NULL, NULL, &size_new_sec );
221 if (!new_sec) {
222 return 0;
224 sd_new = make_sec_desc_buf( ctx, size_new_sec, new_sec );
225 if (!sd_new) {
226 return 0;
229 if ( !(sd_store = sec_desc_merge_buf( ctx, sd_new, sd_orig )) ) {
230 DEBUG(0,("sec_desc_upg_fn: Failed to update sec_desc for %s\n", key.dptr ));
231 return 0;
234 /* store it back */
236 sd_size = ndr_size_security_descriptor(sd_store->sd, 0)
237 + sizeof(struct sec_desc_buf);
239 status = marshall_sec_desc_buf(ctx, sd_store, &data.dptr, &data.dsize);
240 if (!NT_STATUS_IS_OK(status)) {
241 DEBUG(0,("sec_desc_upg_fn: Failed to parse new sec_desc for %s\n", key.dptr ));
242 return 0;
245 result = tdb_store( tdb_printers, key, data, TDB_REPLACE );
247 /* 0 to continue and non-zero to stop traversal */
249 return (result == -1);
252 /*******************************************************************
253 Upgrade the tdb files to version 4
254 *******************************************************************/
256 static bool upgrade_to_version_4(void)
258 TALLOC_CTX *ctx;
259 int result;
261 DEBUG(0,("upgrade_to_version_4: upgrading printer security descriptors\n"));
263 if ( !(ctx = talloc_init( "upgrade_to_version_4" )) )
264 return False;
266 result = tdb_traverse( tdb_printers, sec_desc_upg_fn, ctx );
268 talloc_destroy( ctx );
270 return ( result != -1 );
273 /*******************************************************************
274 Fix an issue with security descriptors. Printer sec_desc must
275 use more than the generic bits that were previously used
276 in <= 3.0.14a. They must also have a owner and group SID assigned.
277 Otherwise, any printers than have been migrated to a Windows
278 host using printmig.exe will not be accessible.
279 *******************************************************************/
281 static int normalize_printers_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
282 TDB_DATA data, void *state )
284 TALLOC_CTX *ctx = talloc_tos();
285 TDB_DATA new_key;
287 if (!data.dptr || data.dsize == 0)
288 return 0;
290 /* upgrade printer records and security descriptors */
292 if ( strncmp((const char *) key.dptr, PRINTERS_PREFIX, strlen(PRINTERS_PREFIX) ) == 0 ) {
293 new_key = make_printer_tdbkey(ctx, (const char *)key.dptr+strlen(PRINTERS_PREFIX) );
295 else if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) == 0 ) {
296 new_key = make_printers_secdesc_tdbkey(ctx, (const char *)key.dptr+strlen(SECDESC_PREFIX) );
298 else {
299 /* ignore this record */
300 return 0;
303 /* delete the original record and store under the normalized key */
305 if ( tdb_delete( the_tdb, key ) != 0 ) {
306 DEBUG(0,("normalize_printers_fn: tdb_delete for [%s] failed!\n",
307 key.dptr));
308 return 1;
311 if ( tdb_store( the_tdb, new_key, data, TDB_REPLACE) != 0 ) {
312 DEBUG(0,("normalize_printers_fn: failed to store new record for [%s]!\n",
313 key.dptr));
314 return 1;
317 return 0;
320 /*******************************************************************
321 Upgrade the tdb files to version 5
322 *******************************************************************/
324 static bool upgrade_to_version_5(void)
326 TALLOC_CTX *ctx;
327 int result;
329 DEBUG(0,("upgrade_to_version_5: normalizing printer keys\n"));
331 if ( !(ctx = talloc_init( "upgrade_to_version_5" )) )
332 return False;
334 result = tdb_traverse( tdb_printers, normalize_printers_fn, NULL );
336 talloc_destroy( ctx );
338 return ( result != -1 );
341 bool nt_printing_tdb_upgrade(void)
343 const char *drivers_path = state_path("ntdrivers.tdb");
344 const char *printers_path = state_path("ntprinters.tdb");
345 const char *forms_path = state_path("ntforms.tdb");
346 bool drivers_exists = file_exist(drivers_path);
347 bool printers_exists = file_exist(printers_path);
348 bool forms_exists = file_exist(forms_path);
349 const char *vstring = "INFO/version";
350 int32_t vers_id;
352 if (!drivers_exists && !printers_exists && !forms_exists) {
353 return true;
356 tdb_drivers = tdb_open_log(drivers_path,
358 TDB_DEFAULT,
359 O_RDWR|O_CREAT,
360 0600);
361 if (tdb_drivers == NULL) {
362 DEBUG(0,("nt_printing_init: Failed to open nt drivers "
363 "database %s (%s)\n",
364 drivers_path, strerror(errno)));
365 return false;
368 tdb_printers = tdb_open_log(printers_path,
370 TDB_DEFAULT,
371 O_RDWR|O_CREAT,
372 0600);
373 if (tdb_printers == NULL) {
374 DEBUG(0,("nt_printing_init: Failed to open nt printers "
375 "database %s (%s)\n",
376 printers_path, strerror(errno)));
377 return false;
380 tdb_forms = tdb_open_log(forms_path,
382 TDB_DEFAULT,
383 O_RDWR|O_CREAT,
384 0600);
385 if (tdb_forms == NULL) {
386 DEBUG(0,("nt_printing_init: Failed to open nt forms "
387 "database %s (%s)\n",
388 forms_path, strerror(errno)));
389 return false;
392 /* Samba upgrade */
393 vers_id = tdb_fetch_int32(tdb_drivers, vstring);
394 if (vers_id == -1) {
395 DEBUG(10, ("Fresh database\n"));
396 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5);
397 vers_id = NTDRIVERS_DATABASE_VERSION_5;
400 if (vers_id != NTDRIVERS_DATABASE_VERSION_5) {
401 if ((vers_id == NTDRIVERS_DATABASE_VERSION_1) ||
402 (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_1)) {
403 if (!upgrade_to_version_3()) {
404 return false;
407 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
408 vers_id = NTDRIVERS_DATABASE_VERSION_3;
411 if ((vers_id == NTDRIVERS_DATABASE_VERSION_2) ||
412 (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_2)) {
414 * Written on a bigendian machine with old fetch_int
415 * code. Save as le. The only upgrade between V2 and V3
416 * is to save the version in little-endian.
418 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
419 vers_id = NTDRIVERS_DATABASE_VERSION_3;
422 if (vers_id == NTDRIVERS_DATABASE_VERSION_3) {
423 if (!upgrade_to_version_4()) {
424 return false;
426 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_4);
427 vers_id = NTDRIVERS_DATABASE_VERSION_4;
430 if (vers_id == NTDRIVERS_DATABASE_VERSION_4 ) {
431 if (!upgrade_to_version_5()) {
432 return false;
434 tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5);
435 vers_id = NTDRIVERS_DATABASE_VERSION_5;
438 if (vers_id != NTDRIVERS_DATABASE_VERSION_5) {
439 DEBUG(0,("nt_printing_init: Unknown printer database version [%d]\n", vers_id));
440 return false;
444 if (tdb_drivers) {
445 tdb_close(tdb_drivers);
446 tdb_drivers = NULL;
449 if (tdb_printers) {
450 tdb_close(tdb_printers);
451 tdb_printers = NULL;
454 if (tdb_forms) {
455 tdb_close(tdb_forms);
456 tdb_forms = NULL;
459 return true;