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