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