s3-build: avoid to globally include printing and spoolss headers.
[vlendec/samba-autobuild/.git] / source3 / printing / nt_printing_tdb.c
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.
8  *
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.
13  *
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.
18  *
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/>.
21  */
22
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"
27
28 #define FORMS_PREFIX "FORMS/"
29 #define DRIVERS_PREFIX "DRIVERS/"
30 #define PRINTERS_PREFIX "PRINTERS/"
31 #define SECDESC_PREFIX "SECDESC/"
32
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 */
38
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 */
42
43 /****************************************************************************
44  generate a new TDB_DATA key for storing a printer
45 ****************************************************************************/
46
47 static TDB_DATA make_printer_tdbkey(TALLOC_CTX *ctx, const char *sharename )
48 {
49         fstring share;
50         char *keystr = NULL;
51         TDB_DATA key;
52
53         fstrcpy(share, sharename);
54         strlower_m(share);
55
56         keystr = talloc_asprintf(ctx, "%s%s", PRINTERS_PREFIX, share);
57         key = string_term_tdb_data(keystr ? keystr : "");
58
59         return key;
60 }
61
62 /****************************************************************************
63  generate a new TDB_DATA key for storing a printer security descriptor
64 ****************************************************************************/
65
66 static TDB_DATA make_printers_secdesc_tdbkey(TALLOC_CTX *ctx,
67                                         const char* sharename  )
68 {
69         fstring share;
70         char *keystr = NULL;
71         TDB_DATA key;
72
73         fstrcpy(share, sharename );
74         strlower_m(share);
75
76         keystr = talloc_asprintf(ctx, "%s%s", SECDESC_PREFIX, share);
77         key = string_term_tdb_data(keystr ? keystr : "");
78
79         return key;
80 }
81
82 /****************************************************************************
83  Upgrade the tdb files to version 3
84 ****************************************************************************/
85
86 static bool upgrade_to_version_3(void)
87 {
88         TDB_DATA kbuf, newkey, dbuf;
89
90         DEBUG(0,("upgrade_to_version_3: upgrading print tdb's to version 3\n"));
91
92         for (kbuf = tdb_firstkey(tdb_drivers); kbuf.dptr;
93                         newkey = tdb_nextkey(tdb_drivers, kbuf), free(kbuf.dptr), kbuf=newkey) {
94
95                 dbuf = tdb_fetch(tdb_drivers, kbuf);
96
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;
103                         }
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;
108                         }
109                 }
110
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;
117                         }
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;
122                         }
123                 }
124
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;
131                         }
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;
136                         }
137                 }
138
139                 SAFE_FREE(dbuf.dptr);
140         }
141
142         return True;
143 }
144
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 *******************************************************************/
152
153 static int sec_desc_upg_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
154                             TDB_DATA data, void *state )
155 {
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;
164
165         if (!data.dptr || data.dsize == 0) {
166                 return 0;
167         }
168
169         if ( strncmp((const char *) key.dptr, SECDESC_PREFIX, strlen(SECDESC_PREFIX) ) != 0 ) {
170                 return 0;
171         }
172
173         /* upgrade the security descriptor */
174
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;
182         }
183
184         if (!sd_orig) {
185                 return 0;
186         }
187         sec = sd_orig->sd;
188
189         /* is this even valid? */
190
191         if ( !sec->dacl ) {
192                 return 0;
193         }
194
195         /* update access masks */
196
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;
202
203                         case GENERIC_ALL_ACCESS:
204                                 sec->dacl->aces[i].access_mask = PRINTER_ACE_FULL_CONTROL;
205                                 break;
206
207                         case READ_CONTROL_ACCESS:
208                                 sec->dacl->aces[i].access_mask = PRINTER_ACE_MANAGE_DOCUMENTS;
209
210                         default:        /* no change */
211                                 break;
212                 }
213         }
214
215         /* create a new struct security_descriptor with the appropriate owner and group SIDs */
216
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;
223         }
224         sd_new = make_sec_desc_buf( ctx, size_new_sec, new_sec );
225         if (!sd_new) {
226                 return 0;
227         }
228
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;
232         }
233
234         /* store it back */
235
236         sd_size = ndr_size_security_descriptor(sd_store->sd, 0)
237                 + sizeof(struct sec_desc_buf);
238
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;
243         }
244
245         result = tdb_store( tdb_printers, key, data, TDB_REPLACE );
246
247         /* 0 to continue and non-zero to stop traversal */
248
249         return (result == -1);
250 }
251
252 /*******************************************************************
253  Upgrade the tdb files to version 4
254 *******************************************************************/
255
256 static bool upgrade_to_version_4(void)
257 {
258         TALLOC_CTX *ctx;
259         int result;
260
261         DEBUG(0,("upgrade_to_version_4: upgrading printer security descriptors\n"));
262
263         if ( !(ctx = talloc_init( "upgrade_to_version_4" )) )
264                 return False;
265
266         result = tdb_traverse( tdb_printers, sec_desc_upg_fn, ctx );
267
268         talloc_destroy( ctx );
269
270         return ( result != -1 );
271 }
272
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 *******************************************************************/
280
281 static int normalize_printers_fn( TDB_CONTEXT *the_tdb, TDB_DATA key,
282                                   TDB_DATA data, void *state )
283 {
284         TALLOC_CTX *ctx = talloc_tos();
285         TDB_DATA new_key;
286
287         if (!data.dptr || data.dsize == 0)
288                 return 0;
289
290         /* upgrade printer records and security descriptors */
291
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) );
294         }
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) );
297         }
298         else {
299                 /* ignore this record */
300                 return 0;
301         }
302
303         /* delete the original record and store under the normalized key */
304
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;
309         }
310
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;
315         }
316
317         return 0;
318 }
319
320 /*******************************************************************
321  Upgrade the tdb files to version 5
322 *******************************************************************/
323
324 static bool upgrade_to_version_5(void)
325 {
326         TALLOC_CTX *ctx;
327         int result;
328
329         DEBUG(0,("upgrade_to_version_5: normalizing printer keys\n"));
330
331         if ( !(ctx = talloc_init( "upgrade_to_version_5" )) )
332                 return False;
333
334         result = tdb_traverse( tdb_printers, normalize_printers_fn, NULL );
335
336         talloc_destroy( ctx );
337
338         return ( result != -1 );
339 }
340
341 bool nt_printing_tdb_upgrade(void)
342 {
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;
351
352         if (!drivers_exists && !printers_exists && !forms_exists) {
353                 return true;
354         }
355
356         tdb_drivers = tdb_open_log(drivers_path,
357                                    0,
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;
366         }
367
368         tdb_printers = tdb_open_log(printers_path,
369                                     0,
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;
378         }
379
380         tdb_forms = tdb_open_log(forms_path,
381                                  0,
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;
390         }
391
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;
398         }
399
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;
405                         }
406
407                         tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
408                         vers_id = NTDRIVERS_DATABASE_VERSION_3;
409                 }
410
411                 if ((vers_id == NTDRIVERS_DATABASE_VERSION_2) ||
412                     (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_2)) {
413                         /*
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.
417                          */
418                         tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3);
419                         vers_id = NTDRIVERS_DATABASE_VERSION_3;
420                 }
421
422                 if (vers_id == NTDRIVERS_DATABASE_VERSION_3) {
423                         if (!upgrade_to_version_4()) {
424                                 return false;
425                         }
426                         tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_4);
427                         vers_id = NTDRIVERS_DATABASE_VERSION_4;
428                 }
429
430                 if (vers_id == NTDRIVERS_DATABASE_VERSION_4 ) {
431                         if (!upgrade_to_version_5()) {
432                                 return false;
433                         }
434                         tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5);
435                         vers_id = NTDRIVERS_DATABASE_VERSION_5;
436                 }
437
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;
441                 }
442         }
443
444         if (tdb_drivers) {
445                 tdb_close(tdb_drivers);
446                 tdb_drivers = NULL;
447         }
448
449         if (tdb_printers) {
450                 tdb_close(tdb_printers);
451                 tdb_printers = NULL;
452         }
453
454         if (tdb_forms) {
455                 tdb_close(tdb_forms);
456                 tdb_forms = NULL;
457         }
458
459         return true;
460 }