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