cea089a9db1e0c274c21ef8c233345f457e1db36
[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 struct tdb_wrap *tdb_reg = NULL;
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->tdb ) == -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->tdb ) == -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->tdb ) == -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_wrap_open(NULL, lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
242         {
243                 tdb_reg = tdb_wrap_open(NULL, 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         vers_id = tdb_fetch_int32(tdb_reg->tdb, vstring);
256
257         if ( vers_id != REGVER_V1 ) {
258                 /* any upgrade code here if needed */
259         }
260
261         /* always setup the necessary keys and values */
262
263         if ( !init_registry_data() ) {
264                 DEBUG(0,("init_registry: Failed to initialize data in registry!\n"));
265                 return False;
266         }
267
268         return True;
269 }
270
271 /***********************************************************************
272  Open the registry.  Must already have been initialized by regdb_init()
273  ***********************************************************************/
274
275 WERROR regdb_open( void )
276 {
277         WERROR result = WERR_OK;
278
279         if ( tdb_reg ) {
280                 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount));
281                 tdb_refcount++;
282                 return WERR_OK;
283         }
284         
285         become_root();
286
287         tdb_reg = tdb_wrap_open(NULL, lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
288         if ( !tdb_reg ) {
289                 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
290                 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n", 
291                         lock_path("registry.tdb"), strerror(errno) ));
292         }
293
294         unbecome_root();
295
296         tdb_refcount = 1;
297         DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount));
298
299         return result;
300 }
301
302 /***********************************************************************
303  ***********************************************************************/
304
305 int regdb_close( void )
306 {
307         tdb_refcount--;
308
309         DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount));
310
311         if ( tdb_refcount > 0 )
312                 return 0;
313
314         SMB_ASSERT( tdb_refcount >= 0 );
315
316         TALLOC_FREE(tdb_reg);
317         return 0;
318 }
319
320 /***********************************************************************
321  return the tdb sequence number of the registry tdb.
322  this is an indicator for the content of the registry
323  having changed. it will change upon regdb_init, too, though.
324  ***********************************************************************/
325 int regdb_get_seqnum(void)
326 {
327         return tdb_get_seqnum(tdb_reg->tdb);
328 }
329
330 /***********************************************************************
331  Add subkey strings to the registry tdb under a defined key
332  fmt is the same format as tdb_pack except this function only supports
333  fstrings
334  ***********************************************************************/
335  
336 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
337 {
338         TDB_DATA dbuf;
339         uint8 *buffer;
340         int i = 0;
341         uint32 len, buflen;
342         BOOL ret = True;
343         uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
344         pstring keyname;
345         
346         if ( !key )
347                 return False;
348
349         pstrcpy( keyname, key );
350         normalize_reg_path( keyname );
351
352         /* allocate some initial memory */
353                 
354         if (!(buffer = (uint8 *)SMB_MALLOC(sizeof(pstring)))) {
355                 return False;
356         }
357         buflen = sizeof(pstring);
358         len = 0;
359         
360         /* store the number of subkeys */
361         
362         len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
363         
364         /* pack all the strings */
365         
366         for (i=0; i<num_subkeys; i++) {
367                 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
368                 if ( len > buflen ) {
369                         /* allocate some extra space */
370                         if ((buffer = (uint8 *)SMB_REALLOC( buffer, len*2 )) == NULL) {
371                                 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
372                                 ret = False;
373                                 goto done;
374                         }
375                         buflen = len*2;
376                                         
377                         len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
378                 }               
379         }
380         
381         /* finally write out the data */
382         
383         dbuf.dptr = buffer;
384         dbuf.dsize = len;
385         if ( tdb_store_bystring( tdb_reg->tdb, keyname, dbuf, TDB_REPLACE ) == -1) {
386                 ret = False;
387                 goto done;
388         }
389
390 done:           
391         SAFE_FREE( buffer );
392         
393         return ret;
394 }
395
396 /***********************************************************************
397  Store the new subkey record and create any child key records that 
398  do not currently exist
399  ***********************************************************************/
400
401 BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr )
402 {
403         int num_subkeys, i;
404         pstring path;
405         REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
406         char *oldkeyname;
407         
408         if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
409                 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
410                 return False;
411         }
412
413         /* fetch a list of the old subkeys so we can determine if any were
414          * deleted */
415         
416         if ( !(old_subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
417                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
418                 goto fail;
419         }
420
421         regdb_fetch_keys( key, old_subkeys );
422         
423         /* store the subkey list for the parent */
424         
425         if ( !regdb_store_keys_internal( key, ctr ) ) {
426                 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
427                          "for parent [%s]\n", key ));
428                 goto fail;
429         }
430         
431         /* now delete removed keys */
432         
433         num_subkeys = regsubkey_ctr_numkeys( old_subkeys );
434         for ( i=0; i<num_subkeys; i++ ) {
435                 oldkeyname = regsubkey_ctr_specific_key( old_subkeys, i );
436
437                 if ( regsubkey_ctr_key_exists( ctr, oldkeyname ) ) {
438                         /*
439                          * It's still around, don't delete
440                          */
441
442                         continue;
443                 }
444
445                 pstr_sprintf( path, "%s/%s", key, oldkeyname );
446                 normalize_reg_path( path );
447                 if (tdb_delete_bystring( tdb_reg->tdb, path ) == -1) {
448                         DEBUG(1, ("Deleting %s failed\n", path));
449                         goto fail;
450                 }
451                 
452                 pstr_sprintf( path, "%s/%s/%s", VALUE_PREFIX, key,
453                               oldkeyname );
454                 normalize_reg_path( path );
455
456                 /*
457                  * Ignore errors here, we might have no values around
458                  */
459                 tdb_delete_bystring( tdb_reg->tdb, path );
460         }
461
462         TALLOC_FREE( old_subkeys );
463         
464         /* now create records for any subkeys that don't already exist */
465         
466         num_subkeys = regsubkey_ctr_numkeys( ctr );
467         for ( i=0; i<num_subkeys; i++ ) {
468                 pstr_sprintf( path, "%s/%s", key,
469                               regsubkey_ctr_specific_key( ctr, i ) );
470
471                 if ( !(subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
472                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
473                         goto fail;
474                 }
475
476                 if ( regdb_fetch_keys( path, subkeys ) == -1 ) {
477                         /* create a record with 0 subkeys */
478                         if ( !regdb_store_keys_internal( path, subkeys ) ) {
479                                 DEBUG(0,("regdb_store_keys: Failed to store "
480                                          "new record for key [%s]\n", path ));
481                                 goto fail;
482                         }
483                 }
484
485                 TALLOC_FREE( subkeys );
486         }
487
488         if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
489                 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
490                 return False;
491         }
492
493         return True;
494
495  fail:
496         TALLOC_FREE( old_subkeys );
497         TALLOC_FREE( subkeys );
498
499         if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
500                 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
501         }
502
503         return False;
504 }
505
506
507 /***********************************************************************
508  Retrieve an array of strings containing subkeys.  Memory should be 
509  released by the caller.  
510  ***********************************************************************/
511
512 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
513 {
514         pstring path;
515         uint32 num_items;
516         TDB_DATA dbuf;
517         uint8 *buf;
518         uint32 buflen, len;
519         int i;
520         fstring subkeyname;
521
522         DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
523         
524         pstrcpy( path, key );
525         
526         /* convert to key format */
527         pstring_sub( path, "\\", "/" ); 
528         strupper_m( path );
529         
530         dbuf = tdb_fetch_bystring( tdb_reg->tdb, path );
531         
532         buf = dbuf.dptr;
533         buflen = dbuf.dsize;
534         
535         if ( !buf ) {
536                 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
537                 return -1;
538         }
539         
540         len = tdb_unpack( buf, buflen, "d", &num_items);
541         
542         for (i=0; i<num_items; i++) {
543                 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
544                 regsubkey_ctr_addkey( ctr, subkeyname );
545         }
546
547         SAFE_FREE( dbuf.dptr );
548         
549         DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
550         
551         return num_items;
552 }
553
554 /****************************************************************************
555  Unpack a list of registry values frem the TDB
556  ***************************************************************************/
557  
558 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
559 {
560         int             len = 0;
561         uint32          type;
562         pstring         valuename;
563         uint32          size;
564         uint8           *data_p;
565         uint32          num_values = 0;
566         int             i;
567         
568         
569         
570         /* loop and unpack the rest of the registry values */
571         
572         len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
573         
574         for ( i=0; i<num_values; i++ ) {
575                 /* unpack the next regval */
576                 
577                 type = REG_NONE;
578                 size = 0;
579                 data_p = NULL;
580                 len += tdb_unpack(buf+len, buflen-len, "fdB",
581                                   valuename,
582                                   &type,
583                                   &size,
584                                   &data_p);
585                                 
586                 /* add the new value. Paranoid protective code -- make sure data_p is valid */
587
588                 if ( size && data_p ) {
589                         regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
590                         SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
591                 }
592
593                 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
594         }
595
596         return len;
597 }
598
599 /****************************************************************************
600  Pack all values in all printer keys
601  ***************************************************************************/
602  
603 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
604 {
605         int             len = 0;
606         int             i;
607         REGISTRY_VALUE  *val;
608         int             num_values;
609
610         if ( !values )
611                 return 0;
612
613         num_values = regval_ctr_numvals( values );
614
615         /* pack the number of values first */
616         
617         len += tdb_pack( buf+len, buflen-len, "d", num_values );
618         
619         /* loop over all values */
620                 
621         for ( i=0; i<num_values; i++ ) {                        
622                 val = regval_ctr_specific_value( values, i );
623                 len += tdb_pack(buf+len, buflen-len, "fdB",
624                                 regval_name(val),
625                                 regval_type(val),
626                                 regval_size(val),
627                                 regval_data_p(val) );
628         }
629
630         return len;
631 }
632
633 /***********************************************************************
634  Retrieve an array of strings containing subkeys.  Memory should be 
635  released by the caller.
636  ***********************************************************************/
637
638 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
639 {
640         TDB_DATA data;
641         pstring keystr;
642
643         DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
644         
645         pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
646         normalize_reg_path( keystr );
647         
648         data = tdb_fetch_bystring( tdb_reg->tdb, keystr );
649         
650         if ( !data.dptr ) {
651                 /* all keys have zero values by default */
652                 return 0;
653         }
654         
655         regdb_unpack_values( values, data.dptr, data.dsize );
656         
657         SAFE_FREE( data.dptr );
658         
659         return regval_ctr_numvals(values);
660 }
661
662 /***********************************************************************
663  Stub function since we do not currently support storing registry 
664  values in the registry.tdb
665  ***********************************************************************/
666
667 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
668 {
669         TDB_DATA data;
670         pstring keystr;
671         int len, ret;
672         
673         DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
674         
675         ZERO_STRUCT( data );
676         
677         len = regdb_pack_values( values, data.dptr, data.dsize );
678         if ( len <= 0 ) {
679                 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
680                 return False;
681         }
682         
683         data.dptr = SMB_MALLOC_ARRAY( uint8, len );
684         data.dsize = len;
685         
686         len = regdb_pack_values( values, data.dptr, data.dsize );
687         
688         SMB_ASSERT( len == data.dsize );
689         
690         pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
691         normalize_reg_path( keystr );
692         
693         ret = tdb_trans_store_bystring(tdb_reg->tdb, keystr, data, TDB_REPLACE);
694         
695         SAFE_FREE( data.dptr );
696         
697         return ret != -1 ;
698 }
699
700 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
701                                 struct security_descriptor **psecdesc)
702 {
703         char *tdbkey;
704         TDB_DATA data;
705         NTSTATUS status;
706
707         DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
708
709         if (asprintf(&tdbkey, "%s/%s", SECDESC_PREFIX, key) == -1) {
710                 return WERR_NOMEM;
711         }
712         normalize_dbkey(tdbkey);
713
714         data = tdb_fetch_bystring(tdb_reg->tdb, tdbkey);
715         SAFE_FREE(tdbkey);
716
717         if (data.dptr == NULL) {
718                 return WERR_BADFILE;
719         }
720
721         status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
722                                      psecdesc);
723
724         SAFE_FREE(data.dptr);
725
726         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
727                 return WERR_NOMEM;
728         }
729
730         if (!NT_STATUS_IS_OK(status)) {
731                 return WERR_REG_CORRUPT;
732         }
733
734         return WERR_OK;
735 }
736
737 static WERROR regdb_set_secdesc(const char *key,
738                                 struct security_descriptor *secdesc)
739 {
740         prs_struct ps;
741         TALLOC_CTX *mem_ctx;
742         char *tdbkey;
743         WERROR err = WERR_NOMEM;
744         TDB_DATA tdbdata;
745
746         if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
747                 return WERR_NOMEM;
748         }
749
750         ZERO_STRUCT(ps);
751
752         if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", SECDESC_PREFIX,
753                                        key))) {
754                 goto done;
755         }
756         normalize_dbkey(tdbkey);
757
758         err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
759                                                    &tdbdata.dptr,
760                                                    &tdbdata.dsize));
761         if (!W_ERROR_IS_OK(err)) {
762                 goto done;
763         }
764
765         if (tdb_trans_store_bystring(tdb_reg->tdb, tdbkey, tdbdata, 0) == -1) {
766                 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
767                 goto done;
768         }
769
770  done:
771         prs_mem_free(&ps);
772         TALLOC_FREE(mem_ctx);
773         return err;
774 }
775
776 /* 
777  * Table of function pointers for default access
778  */
779  
780 REGISTRY_OPS regdb_ops = {
781         regdb_fetch_keys,
782         regdb_fetch_values,
783         regdb_store_keys,
784         regdb_store_values,
785         NULL,
786         regdb_get_secdesc,
787         regdb_set_secdesc
788 };