r23468: Open registry.tdb with sequence number.
[sfrench/samba-autobuild/.git] / source / registry / reg_db.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Gerald Carter                     2002-2005
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /* Implementation of internal registry database functions. */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
27
28 static TDB_CONTEXT *tdb_reg;
29 static int tdb_refcount;
30
31 #define VALUE_PREFIX    "SAMBA_REGVAL"
32 #define SECDESC_PREFIX  "SAMBA_SECDESC"
33
34 #define REG_TDB_FLAGS TDB_SEQNUM
35
36 /* List the deepest path into the registry.  All part components will be created.*/
37
38 /* If you want to have a part of the path controlled by the tdb and part by
39    a virtual registry db (e.g. printing), then you have to list the deepest path.
40    For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print" 
41    allows the reg_db backend to handle everything up to 
42    "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook 
43    the reg_printing backend onto the last component of the path (see 
44    KEY_PRINTING_2K in include/rpc_reg.h)   --jerry */
45
46 static const char *builtin_registry_paths[] = {
47         KEY_PRINTING_2K,
48         KEY_PRINTING_PORTS,
49         KEY_PRINTING,
50         KEY_SHARES,
51         KEY_EVENTLOG,
52         KEY_SMBCONF,
53         "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
54         "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
55         "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
56         "HKLM\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
57         "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
58         "HKLM\\SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters",
59         "HKLM\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters",
60         "HKU",
61         "HKCR",
62         "HKPD",
63         "HKPT",
64          NULL };
65
66 struct builtin_regkey_value {
67         const char *path;
68         const char *valuename;
69         uint32 type;
70         union {
71                 const char *string;
72                 uint32 dw_value;
73         } data;
74 };
75
76 static struct builtin_regkey_value builtin_registry_values[] = {
77         { KEY_PRINTING_PORTS,
78                 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
79         { KEY_PRINTING_2K,
80                 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
81         { KEY_EVENTLOG,
82                 "DisplayName", REG_SZ, { "Event Log" } }, 
83         { KEY_EVENTLOG,
84                 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
85         { NULL, NULL, 0, { NULL } }
86 };
87
88 #define REGVER_V1       1       /* first db version with write support */
89         
90 /***********************************************************************
91  Open the registry data in the tdb
92  ***********************************************************************/
93  
94 static BOOL init_registry_data( void )
95 {
96         pstring path, base, remaining;
97         fstring keyname, subkeyname;
98         REGSUBKEY_CTR *subkeys;
99         REGVAL_CTR *values;
100         int i;
101         const char *p, *p2;
102         UNISTR2 data;
103
104         /*
105          * There are potentially quite a few store operations which are all
106          * indiviually wrapped in tdb transactions. Wrapping them in a single
107          * transaction gives just a single transaction_commit() to actually do
108          * its fsync()s. See tdb/common/transaction.c for info about nested
109          * transaction behaviour.
110          */
111
112         if ( tdb_transaction_start( tdb_reg ) == -1 ) {
113                 DEBUG(0, ("init_registry_data: tdb_transaction_start "
114                           "failed\n"));
115                 return False;
116         }
117         
118         /* loop over all of the predefined paths and add each component */
119         
120         for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
121
122                 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
123
124                 pstrcpy( path, builtin_registry_paths[i] );
125                 pstrcpy( base, "" );
126                 p = path;
127                 
128                 while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) {
129                 
130                         /* build up the registry path from the components */
131                         
132                         if ( *base )
133                                 pstrcat( base, "\\" );
134                         pstrcat( base, keyname );
135                         
136                         /* get the immediate subkeyname (if we have one ) */
137                         
138                         *subkeyname = '\0';
139                         if ( *p ) {
140                                 pstrcpy( remaining, p );
141                                 p2 = remaining;
142                                 
143                                 if ( !next_token(&p2, subkeyname, "\\", sizeof(subkeyname)) )
144                                         fstrcpy( subkeyname, p2 );
145                         }
146
147                         DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
148                                 base, *subkeyname ? subkeyname : "NULL"));
149                         
150                         /* we don't really care if the lookup succeeds or not since
151                            we are about to update the record.  We just want any 
152                            subkeys already present */
153                         
154                         if ( !(subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
155                                 DEBUG(0,("talloc() failure!\n"));
156                                 goto fail;
157                         }
158
159                         regdb_fetch_keys( base, subkeys );
160                         if ( *subkeyname ) 
161                                 regsubkey_ctr_addkey( subkeys, subkeyname );
162                         if ( !regdb_store_keys( base, subkeys ))
163                                 goto fail;
164                         
165                         TALLOC_FREE( subkeys );
166                 }
167         }
168
169         /* loop over all of the predefined values and add each component */
170         
171         for ( i=0; builtin_registry_values[i].path != NULL; i++ ) {
172                 if ( !(values = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
173                         DEBUG(0,("talloc() failure!\n"));
174                         goto fail;
175                 }
176
177                 regdb_fetch_values( builtin_registry_values[i].path, values );
178
179                 /* preserve existing values across restarts.  Only add new ones */
180
181                 if ( !regval_ctr_key_exists( values, builtin_registry_values[i].valuename ) ) 
182                 {
183                         switch( builtin_registry_values[i].type ) {
184                         case REG_DWORD:
185                                 regval_ctr_addvalue( values, 
186                                                      builtin_registry_values[i].valuename,
187                                                      REG_DWORD,
188                                                      (char*)&builtin_registry_values[i].data.dw_value,
189                                                      sizeof(uint32) );
190                                 break;
191                                 
192                         case REG_SZ:
193                                 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
194                                 regval_ctr_addvalue( values, 
195                                                      builtin_registry_values[i].valuename,
196                                                      REG_SZ,
197                                                      (char*)data.buffer,
198                                                      data.uni_str_len*sizeof(uint16) );
199                                 break;
200                         
201                         default:
202                                 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
203                                         builtin_registry_values[i].type));
204                         }
205                         regdb_store_values( builtin_registry_values[i].path, values );
206                 }
207                 
208                 TALLOC_FREE( values );
209         }
210         
211         if (tdb_transaction_commit( tdb_reg ) == -1) {
212                 DEBUG(0, ("init_registry_data: Could not commit "
213                           "transaction\n"));
214                 return False;
215         }
216
217         return True;
218
219  fail:
220
221         if (tdb_transaction_cancel( tdb_reg ) == -1) {
222                 smb_panic("init_registry_data: tdb_transaction_cancel "
223                           "failed\n");
224         }
225
226         return False;
227 }
228
229 /***********************************************************************
230  Open the registry database
231  ***********************************************************************/
232  
233 BOOL regdb_init( void )
234 {
235         const char *vstring = "INFO/version";
236         uint32 vers_id;
237
238         if ( tdb_reg )
239                 return True;
240
241         if ( !(tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
242         {
243                 tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
244                 if ( !tdb_reg ) {
245                         DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
246                                 lock_path("registry.tdb"), strerror(errno) ));
247                         return False;
248                 }
249                 
250                 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
251         }
252
253         tdb_refcount = 1;
254                 
255
256         vers_id = tdb_fetch_int32(tdb_reg, vstring);
257
258         if ( vers_id != REGVER_V1 ) {
259                 /* any upgrade code here if needed */
260         }
261
262         /* always setup the necessary keys and values */
263
264         if ( !init_registry_data() ) {
265                 DEBUG(0,("init_registry: Failed to initialize data in registry!\n"));
266                 return False;
267         }
268
269         return True;
270 }
271
272 /***********************************************************************
273  Open the registry.  Must already have been initialized by regdb_init()
274  ***********************************************************************/
275
276 WERROR regdb_open( void )
277 {
278         WERROR result = WERR_OK;
279
280         if ( tdb_reg ) {
281                 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount));
282                 tdb_refcount++;
283                 return WERR_OK;
284         }
285         
286         become_root();
287
288         tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
289         if ( !tdb_reg ) {
290                 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
291                 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n", 
292                         lock_path("registry.tdb"), strerror(errno) ));
293         }
294
295         unbecome_root();
296
297         tdb_refcount = 1;
298         DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount));
299
300         return result;
301 }
302
303 /***********************************************************************
304  ***********************************************************************/
305
306 int regdb_close( void )
307 {
308         int ret;
309
310         tdb_refcount--;
311
312         DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount));
313
314         if ( tdb_refcount > 0 )
315                 return 0;
316
317         SMB_ASSERT( tdb_refcount >= 0 );
318
319         ret = tdb_close( tdb_reg );
320         tdb_reg = NULL;
321
322         return ret;
323 }
324
325 /***********************************************************************
326  return the tdb sequence number of the registry tdb.
327  this is an indicator for the content of the registry
328  having changed. it will change upon regdb_init, too, though.
329  ***********************************************************************/
330 int regdb_get_seqnum(void)
331 {
332         return tdb_get_seqnum(tdb_reg);
333 }
334
335 /***********************************************************************
336  Add subkey strings to the registry tdb under a defined key
337  fmt is the same format as tdb_pack except this function only supports
338  fstrings
339  ***********************************************************************/
340  
341 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
342 {
343         TDB_DATA dbuf;
344         uint8 *buffer;
345         int i = 0;
346         uint32 len, buflen;
347         BOOL ret = True;
348         uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
349         pstring keyname;
350         
351         if ( !key )
352                 return False;
353
354         pstrcpy( keyname, key );
355         normalize_reg_path( keyname );
356
357         /* allocate some initial memory */
358                 
359         if (!(buffer = (uint8 *)SMB_MALLOC(sizeof(pstring)))) {
360                 return False;
361         }
362         buflen = sizeof(pstring);
363         len = 0;
364         
365         /* store the number of subkeys */
366         
367         len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
368         
369         /* pack all the strings */
370         
371         for (i=0; i<num_subkeys; i++) {
372                 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
373                 if ( len > buflen ) {
374                         /* allocate some extra space */
375                         if ((buffer = (uint8 *)SMB_REALLOC( buffer, len*2 )) == NULL) {
376                                 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
377                                 ret = False;
378                                 goto done;
379                         }
380                         buflen = len*2;
381                                         
382                         len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
383                 }               
384         }
385         
386         /* finally write out the data */
387         
388         dbuf.dptr = buffer;
389         dbuf.dsize = len;
390         if ( tdb_store_bystring( tdb_reg, keyname, dbuf, TDB_REPLACE ) == -1) {
391                 ret = False;
392                 goto done;
393         }
394
395 done:           
396         SAFE_FREE( buffer );
397         
398         return ret;
399 }
400
401 /***********************************************************************
402  Store the new subkey record and create any child key records that 
403  do not currently exist
404  ***********************************************************************/
405
406 BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr )
407 {
408         int num_subkeys, i;
409         pstring path;
410         REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
411         char *oldkeyname;
412         
413         if ( tdb_transaction_start( tdb_reg ) == -1 ) {
414                 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
415                 return False;
416         }
417
418         /* fetch a list of the old subkeys so we can determine if any were
419          * deleted */
420         
421         if ( !(old_subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
422                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
423                 goto fail;
424         }
425
426         regdb_fetch_keys( key, old_subkeys );
427         
428         /* store the subkey list for the parent */
429         
430         if ( !regdb_store_keys_internal( key, ctr ) ) {
431                 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
432                          "for parent [%s]\n", key ));
433                 goto fail;
434         }
435         
436         /* now delete removed keys */
437         
438         num_subkeys = regsubkey_ctr_numkeys( old_subkeys );
439         for ( i=0; i<num_subkeys; i++ ) {
440                 oldkeyname = regsubkey_ctr_specific_key( old_subkeys, i );
441
442                 if ( regsubkey_ctr_key_exists( ctr, oldkeyname ) ) {
443                         /*
444                          * It's still around, don't delete
445                          */
446
447                         continue;
448                 }
449
450                 pstr_sprintf( path, "%s/%s", key, oldkeyname );
451                 normalize_reg_path( path );
452                 if (tdb_delete_bystring( tdb_reg, path ) == -1) {
453                         DEBUG(1, ("Deleting %s failed\n", path));
454                         goto fail;
455                 }
456                 
457                 pstr_sprintf( path, "%s/%s/%s", VALUE_PREFIX, key,
458                               oldkeyname );
459                 normalize_reg_path( path );
460
461                 /*
462                  * Ignore errors here, we might have no values around
463                  */
464                 tdb_delete_bystring( tdb_reg, path );
465         }
466
467         TALLOC_FREE( old_subkeys );
468         
469         /* now create records for any subkeys that don't already exist */
470         
471         num_subkeys = regsubkey_ctr_numkeys( ctr );
472         for ( i=0; i<num_subkeys; i++ ) {
473                 pstr_sprintf( path, "%s/%s", key,
474                               regsubkey_ctr_specific_key( ctr, i ) );
475
476                 if ( !(subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
477                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
478                         goto fail;
479                 }
480
481                 if ( regdb_fetch_keys( path, subkeys ) == -1 ) {
482                         /* create a record with 0 subkeys */
483                         if ( !regdb_store_keys_internal( path, subkeys ) ) {
484                                 DEBUG(0,("regdb_store_keys: Failed to store "
485                                          "new record for key [%s]\n", path ));
486                                 goto fail;
487                         }
488                 }
489
490                 TALLOC_FREE( subkeys );
491         }
492
493         if (tdb_transaction_commit( tdb_reg ) == -1) {
494                 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
495                 return False;
496         }
497
498         return True;
499
500  fail:
501         TALLOC_FREE( old_subkeys );
502         TALLOC_FREE( subkeys );
503
504         if (tdb_transaction_cancel( tdb_reg ) == -1) {
505                 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
506         }
507
508         return False;
509 }
510
511
512 /***********************************************************************
513  Retrieve an array of strings containing subkeys.  Memory should be 
514  released by the caller.  
515  ***********************************************************************/
516
517 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
518 {
519         pstring path;
520         uint32 num_items;
521         TDB_DATA dbuf;
522         uint8 *buf;
523         uint32 buflen, len;
524         int i;
525         fstring subkeyname;
526
527         DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
528         
529         pstrcpy( path, key );
530         
531         /* convert to key format */
532         pstring_sub( path, "\\", "/" ); 
533         strupper_m( path );
534         
535         dbuf = tdb_fetch_bystring( tdb_reg, path );
536         
537         buf = dbuf.dptr;
538         buflen = dbuf.dsize;
539         
540         if ( !buf ) {
541                 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
542                 return -1;
543         }
544         
545         len = tdb_unpack( buf, buflen, "d", &num_items);
546         
547         for (i=0; i<num_items; i++) {
548                 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
549                 regsubkey_ctr_addkey( ctr, subkeyname );
550         }
551
552         SAFE_FREE( dbuf.dptr );
553         
554         DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
555         
556         return num_items;
557 }
558
559 /****************************************************************************
560  Unpack a list of registry values frem the TDB
561  ***************************************************************************/
562  
563 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
564 {
565         int             len = 0;
566         uint32          type;
567         pstring         valuename;
568         uint32          size;
569         uint8           *data_p;
570         uint32          num_values = 0;
571         int             i;
572         
573         
574         
575         /* loop and unpack the rest of the registry values */
576         
577         len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
578         
579         for ( i=0; i<num_values; i++ ) {
580                 /* unpack the next regval */
581                 
582                 type = REG_NONE;
583                 size = 0;
584                 data_p = NULL;
585                 len += tdb_unpack(buf+len, buflen-len, "fdB",
586                                   valuename,
587                                   &type,
588                                   &size,
589                                   &data_p);
590                                 
591                 /* add the new value. Paranoid protective code -- make sure data_p is valid */
592
593                 if ( size && data_p ) {
594                         regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
595                         SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
596                 }
597
598                 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
599         }
600
601         return len;
602 }
603
604 /****************************************************************************
605  Pack all values in all printer keys
606  ***************************************************************************/
607  
608 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
609 {
610         int             len = 0;
611         int             i;
612         REGISTRY_VALUE  *val;
613         int             num_values;
614
615         if ( !values )
616                 return 0;
617
618         num_values = regval_ctr_numvals( values );
619
620         /* pack the number of values first */
621         
622         len += tdb_pack( buf+len, buflen-len, "d", num_values );
623         
624         /* loop over all values */
625                 
626         for ( i=0; i<num_values; i++ ) {                        
627                 val = regval_ctr_specific_value( values, i );
628                 len += tdb_pack(buf+len, buflen-len, "fdB",
629                                 regval_name(val),
630                                 regval_type(val),
631                                 regval_size(val),
632                                 regval_data_p(val) );
633         }
634
635         return len;
636 }
637
638 /***********************************************************************
639  Retrieve an array of strings containing subkeys.  Memory should be 
640  released by the caller.
641  ***********************************************************************/
642
643 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
644 {
645         TDB_DATA data;
646         pstring keystr;
647
648         DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
649         
650         pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
651         normalize_reg_path( keystr );
652         
653         data = tdb_fetch_bystring( tdb_reg, keystr );
654         
655         if ( !data.dptr ) {
656                 /* all keys have zero values by default */
657                 return 0;
658         }
659         
660         regdb_unpack_values( values, data.dptr, data.dsize );
661         
662         SAFE_FREE( data.dptr );
663         
664         return regval_ctr_numvals(values);
665 }
666
667 /***********************************************************************
668  Stub function since we do not currently support storing registry 
669  values in the registry.tdb
670  ***********************************************************************/
671
672 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
673 {
674         TDB_DATA data;
675         pstring keystr;
676         int len, ret;
677         
678         DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
679         
680         ZERO_STRUCT( data );
681         
682         len = regdb_pack_values( values, data.dptr, data.dsize );
683         if ( len <= 0 ) {
684                 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
685                 return False;
686         }
687         
688         data.dptr = SMB_MALLOC_ARRAY( uint8, len );
689         data.dsize = len;
690         
691         len = regdb_pack_values( values, data.dptr, data.dsize );
692         
693         SMB_ASSERT( len == data.dsize );
694         
695         pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
696         normalize_reg_path( keystr );
697         
698         ret = tdb_trans_store_bystring(tdb_reg, keystr, data, TDB_REPLACE);
699         
700         SAFE_FREE( data.dptr );
701         
702         return ret != -1 ;
703 }
704
705 void normalize_dbkey(char *key)
706 {
707         size_t len = strlen(key);
708         string_sub(key, "\\", "/", len+1);
709         strupper_m(key);
710 }
711
712 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
713                                 struct security_descriptor **psecdesc)
714 {
715         char *tdbkey;
716         TDB_DATA data;
717         NTSTATUS status;
718
719         DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
720
721         if (asprintf(&tdbkey, "%s/%s", SECDESC_PREFIX, key) == -1) {
722                 return WERR_NOMEM;
723         }
724         normalize_dbkey(tdbkey);
725
726         data = tdb_fetch_bystring(tdb_reg, tdbkey);
727         SAFE_FREE(tdbkey);
728
729         if (data.dptr == NULL) {
730                 return WERR_BADFILE;
731         }
732
733         status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
734                                      psecdesc);
735
736         SAFE_FREE(data.dptr);
737
738         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
739                 return WERR_NOMEM;
740         }
741
742         if (!NT_STATUS_IS_OK(status)) {
743                 return WERR_REG_CORRUPT;
744         }
745
746         return WERR_OK;
747 }
748
749 static WERROR regdb_set_secdesc(const char *key,
750                                 struct security_descriptor *secdesc)
751 {
752         prs_struct ps;
753         TALLOC_CTX *mem_ctx;
754         char *tdbkey;
755         WERROR err = WERR_NOMEM;
756         TDB_DATA tdbdata;
757
758         if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
759                 return WERR_NOMEM;
760         }
761
762         ZERO_STRUCT(ps);
763
764         if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", SECDESC_PREFIX,
765                                        key))) {
766                 goto done;
767         }
768         normalize_dbkey(tdbkey);
769
770         err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
771                                                    &tdbdata.dptr,
772                                                    &tdbdata.dsize));
773         if (!W_ERROR_IS_OK(err)) {
774                 goto done;
775         }
776
777         if (tdb_trans_store_bystring(tdb_reg, tdbkey, tdbdata, 0) == -1) {
778                 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
779                 goto done;
780         }
781
782  done:
783         prs_mem_free(&ps);
784         TALLOC_FREE(mem_ctx);
785         return err;
786 }
787
788 /* 
789  * Table of function pointers for default access
790  */
791  
792 REGISTRY_OPS regdb_ops = {
793         regdb_fetch_keys,
794         regdb_fetch_values,
795         regdb_store_keys,
796         regdb_store_values,
797         NULL,
798         regdb_get_secdesc,
799         regdb_set_secdesc
800 };