Coverage Report

Created: 2026-02-14 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/registry/reg_objects.c
Line
Count
Source
1
/*
2
 *  Unix SMB/CIFS implementation.
3
 *  Virtual Windows Registry Layer
4
 *  Copyright (C) Gerald Carter                     2002-2005
5
 *  Copyright (C) Michael Adam                      2007-2010
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 3 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/* Implementation of registry frontend view functions. */
22
23
#include "includes.h"
24
#include "registry.h"
25
#include "reg_objects.h"
26
#include "util_tdb.h"
27
#include "dbwrap/dbwrap.h"
28
#include "dbwrap/dbwrap_rbt.h"
29
#include "../libcli/registry/util_reg.h"
30
#include "lib/util/string_wrappers.h"
31
32
#undef DBGC_CLASS
33
0
#define DBGC_CLASS DBGC_REGISTRY
34
35
/* low level structure to contain registry values */
36
37
struct regval_blob {
38
  fstring   valuename;
39
  uint32_t  type;
40
  /* this should be encapsulated in an RPC_DATA_BLOB */
41
  uint32_t  size; /* in bytes */
42
  uint8_t   *data_p;
43
};
44
45
/* container for registry values */
46
47
struct regval_ctr {
48
  uint32_t num_values;
49
  struct regval_blob **values;
50
  int seqnum;
51
};
52
53
struct regsubkey_ctr {
54
  uint32_t        num_subkeys;
55
  char            **subkeys;
56
  struct db_context *subkeys_hash;
57
  int seqnum;
58
};
59
60
/**********************************************************************
61
62
 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
63
 talloc()'d since the methods use the object pointer as the talloc
64
 context for internal private data.
65
66
 There is no longer a regval_ctr_init() and regval_ctr_destroy()
67
 pair of functions.  Simply talloc_zero() and TALLOC_FREE() the
68
 object.
69
70
 **********************************************************************/
71
72
WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
73
0
{
74
0
  if (ctr == NULL) {
75
0
    return WERR_INVALID_PARAMETER;
76
0
  }
77
78
0
  *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
79
0
  if (*ctr == NULL) {
80
0
    return WERR_NOT_ENOUGH_MEMORY;
81
0
  }
82
83
0
  (*ctr)->subkeys_hash = db_open_rbt(*ctr);
84
0
  if ((*ctr)->subkeys_hash == NULL) {
85
0
    talloc_free(*ctr);
86
0
    return WERR_NOT_ENOUGH_MEMORY;
87
0
  }
88
89
0
  return WERR_OK;
90
0
}
91
92
/**
93
 * re-initialize the list of subkeys (to the empty list)
94
 * in an already allocated regsubkey_ctr
95
 */
96
97
WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
98
0
{
99
0
  if (ctr == NULL) {
100
0
    return WERR_INVALID_PARAMETER;
101
0
  }
102
103
0
  talloc_free(ctr->subkeys_hash);
104
0
  ctr->subkeys_hash = db_open_rbt(ctr);
105
0
  W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
106
107
0
  TALLOC_FREE(ctr->subkeys);
108
109
0
  ctr->num_subkeys = 0;
110
0
  ctr->seqnum = 0;
111
112
0
  return WERR_OK;
113
0
}
114
115
WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
116
0
{
117
0
  if (ctr == NULL) {
118
0
    return WERR_INVALID_PARAMETER;
119
0
  }
120
121
0
  ctr->seqnum = seqnum;
122
123
0
  return WERR_OK;
124
0
}
125
126
int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
127
0
{
128
0
  if (ctr == NULL) {
129
0
    return -1;
130
0
  }
131
132
0
  return ctr->seqnum;
133
0
}
134
135
static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
136
           const char *keyname,
137
           uint32_t idx)
138
0
{
139
0
  WERROR werr;
140
141
0
  werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
142
0
            keyname,
143
0
            make_tdb_data((uint8_t *)&idx,
144
0
                    sizeof(idx)),
145
0
            TDB_REPLACE));
146
0
  if (!W_ERROR_IS_OK(werr)) {
147
0
    DEBUG(1, ("error hashing new key '%s' in container: %s\n",
148
0
        keyname, win_errstr(werr)));
149
0
  }
150
151
0
  return werr;
152
0
}
153
154
static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
155
             const char *keyname)
156
0
{
157
0
  WERROR werr;
158
159
0
  werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
160
0
          keyname));
161
0
  if (!W_ERROR_IS_OK(werr)) {
162
0
    DEBUG(1, ("error unhashing key '%s' in container: %s\n",
163
0
        keyname, win_errstr(werr)));
164
0
  }
165
166
0
  return werr;
167
0
}
168
169
static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
170
                const char *keyname,
171
                uint32_t *idx)
172
0
{
173
0
  TDB_DATA data;
174
0
  NTSTATUS status;
175
176
0
  if ((ctr == NULL) || (keyname == NULL)) {
177
0
    return WERR_INVALID_PARAMETER;
178
0
  }
179
180
0
  status = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname,
181
0
               &data);
182
0
  if (!NT_STATUS_IS_OK(status)) {
183
0
    return WERR_NOT_FOUND;
184
0
  }
185
186
0
  if (data.dsize != sizeof(*idx)) {
187
0
    talloc_free(data.dptr);
188
0
    return WERR_INVALID_DATATYPE;
189
0
  }
190
191
0
  if (idx != NULL) {
192
0
    memcpy(idx, data.dptr, sizeof(*idx));
193
0
  }
194
195
0
  talloc_free(data.dptr);
196
0
  return WERR_OK;
197
0
}
198
199
/***********************************************************************
200
 Add a new key to the array
201
 **********************************************************************/
202
203
WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
204
0
{
205
0
  char **newkeys;
206
0
  WERROR werr;
207
208
0
  if ( !keyname ) {
209
0
    return WERR_OK;
210
0
  }
211
212
  /* make sure the keyname is not already there */
213
214
0
  if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
215
0
    return WERR_OK;
216
0
  }
217
218
0
  if (!(newkeys = talloc_realloc(ctr, ctr->subkeys, char *,
219
0
               ctr->num_subkeys+1))) {
220
0
    return WERR_NOT_ENOUGH_MEMORY;
221
0
  }
222
223
0
  ctr->subkeys = newkeys;
224
225
0
  if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
226
0
                   keyname ))) {
227
    /*
228
     * Don't shrink the new array again, this wastes a pointer
229
     */
230
0
    return WERR_NOT_ENOUGH_MEMORY;
231
0
  }
232
233
0
  werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
234
0
  W_ERROR_NOT_OK_RETURN(werr);
235
236
0
  ctr->num_subkeys++;
237
238
0
  return WERR_OK;
239
0
}
240
241
 /***********************************************************************
242
 Delete a key from the array
243
 **********************************************************************/
244
245
WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
246
0
{
247
0
  WERROR werr;
248
0
  uint32_t idx, j;
249
250
0
  if (keyname == NULL) {
251
0
    return WERR_INVALID_PARAMETER;
252
0
  }
253
254
  /* make sure the keyname is actually already there */
255
256
0
  werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
257
0
  W_ERROR_NOT_OK_RETURN(werr);
258
259
0
  werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
260
0
  W_ERROR_NOT_OK_RETURN(werr);
261
262
  /* update if we have any keys left */
263
0
  ctr->num_subkeys--;
264
0
  if (idx < ctr->num_subkeys) {
265
0
    memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
266
0
      sizeof(char *) * (ctr->num_subkeys - idx));
267
268
    /* we have to re-hash rest of the array...  :-( */
269
0
    for (j = idx; j < ctr->num_subkeys; j++) {
270
0
      werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
271
0
      W_ERROR_NOT_OK_RETURN(werr);
272
0
    }
273
0
  }
274
275
0
  return WERR_OK;
276
0
}
277
278
/***********************************************************************
279
 Check for the existence of a key
280
 **********************************************************************/
281
282
bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
283
0
{
284
0
  WERROR werr;
285
286
0
  if (!ctr->subkeys) {
287
0
    return False;
288
0
  }
289
290
0
  werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
291
0
  if (!W_ERROR_IS_OK(werr)) {
292
0
    return false;
293
0
  }
294
295
0
  return true;
296
0
}
297
298
/***********************************************************************
299
 How many keys does the container hold ?
300
 **********************************************************************/
301
302
uint32_t regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
303
0
{
304
0
  return ctr->num_subkeys;
305
0
}
306
307
/***********************************************************************
308
 Retrieve a specific key string
309
 **********************************************************************/
310
311
char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
312
0
{
313
0
  if ( ! (key_index < ctr->num_subkeys) )
314
0
    return NULL;
315
316
0
  return ctr->subkeys[key_index];
317
0
}
318
319
/*
320
 * Utility functions for struct regval_ctr
321
 */
322
323
/**
324
 * allocate a regval_ctr structure.
325
 */
326
WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
327
0
{
328
0
  if (ctr == NULL) {
329
0
    return WERR_INVALID_PARAMETER;
330
0
  }
331
332
0
  *ctr = talloc_zero(mem_ctx, struct regval_ctr);
333
0
  if (*ctr == NULL) {
334
0
    return WERR_NOT_ENOUGH_MEMORY;
335
0
  }
336
337
0
  return WERR_OK;
338
0
}
339
340
/***********************************************************************
341
 How many keys does the container hold ?
342
 **********************************************************************/
343
344
uint32_t regval_ctr_numvals(struct regval_ctr *ctr)
345
0
{
346
0
  return ctr->num_values;
347
0
}
348
349
/**********************************************************************
350
 *********************************************************************/
351
352
uint8_t* regval_data_p(struct regval_blob *val)
353
0
{
354
0
  return val->data_p;
355
0
}
356
357
/**********************************************************************
358
 *********************************************************************/
359
360
uint32_t regval_size(struct regval_blob *val)
361
0
{
362
0
  return val->size;
363
0
}
364
365
/**********************************************************************
366
 *********************************************************************/
367
368
char* regval_name(struct regval_blob *val)
369
0
{
370
0
  return val->valuename;
371
0
}
372
373
/**********************************************************************
374
 *********************************************************************/
375
376
uint32_t regval_type(struct regval_blob *val)
377
0
{
378
0
  return val->type;
379
0
}
380
381
/***********************************************************************
382
 Retrieve a pointer to a specific value. Caller should dup the structure
383
 since this memory will go away when the ctr is free()'d
384
 **********************************************************************/
385
386
struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
387
                uint32_t idx)
388
0
{
389
0
  if ( !(idx < ctr->num_values) )
390
0
    return NULL;
391
392
0
  return ctr->values[idx];
393
0
}
394
395
/***********************************************************************
396
 Check for the existence of a value
397
 **********************************************************************/
398
399
bool regval_ctr_value_exists(struct regval_ctr *ctr, const char *value)
400
0
{
401
0
  uint32_t i;
402
403
0
  for ( i=0; i<ctr->num_values; i++ ) {
404
0
    if ( strequal( ctr->values[i]->valuename, value) )
405
0
      return True;
406
0
  }
407
408
0
  return False;
409
0
}
410
411
/**
412
 * Get a value by its name
413
 */
414
struct regval_blob *regval_ctr_value_byname(struct regval_ctr *ctr,
415
              const char *value)
416
0
{
417
0
  uint32_t i;
418
419
0
  for (i = 0; i < ctr->num_values; i++) {
420
0
    if (strequal(ctr->values[i]->valuename, value)) {
421
0
      return ctr->values[i];
422
0
    }
423
0
  }
424
425
0
  return NULL;
426
0
}
427
428
429
/***********************************************************************
430
 * compose a struct regval_blob from input data
431
 **********************************************************************/
432
433
struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
434
           uint32_t type,
435
           const uint8_t *data_p, size_t size)
436
0
{
437
0
  struct regval_blob *regval = talloc(ctx, struct regval_blob);
438
439
0
  if (regval == NULL) {
440
0
    return NULL;
441
0
  }
442
443
0
  fstrcpy(regval->valuename, name);
444
0
  regval->type = type;
445
0
  if (size) {
446
0
    regval->data_p = (uint8_t *)talloc_memdup(regval, data_p, size);
447
0
    if (!regval->data_p) {
448
0
      TALLOC_FREE(regval);
449
0
      return NULL;
450
0
    }
451
0
  } else {
452
0
    regval->data_p = NULL;
453
0
  }
454
0
  regval->size = size;
455
456
0
  return regval;
457
0
}
458
459
/***********************************************************************
460
 Add a new registry value to the array
461
 **********************************************************************/
462
463
int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint32_t type,
464
                        const uint8_t *data_p, size_t size)
465
0
{
466
0
  if ( !name )
467
0
    return ctr->num_values;
468
469
  /* Delete the current value (if it exists) and add the new one */
470
471
0
  regval_ctr_delvalue( ctr, name );
472
473
  /* allocate a slot in the array of pointers */
474
475
0
  if (  ctr->num_values == 0 ) {
476
0
    ctr->values = talloc( ctr, struct regval_blob *);
477
0
  } else {
478
0
    ctr->values = talloc_realloc(ctr, ctr->values,
479
0
               struct regval_blob *,
480
0
               ctr->num_values+1);
481
0
  }
482
483
0
  if (!ctr->values) {
484
0
    ctr->num_values = 0;
485
0
    return 0;
486
0
  }
487
488
  /* allocate a new value and store the pointer in the array */
489
490
0
  ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
491
0
                  size);
492
0
  if (ctr->values[ctr->num_values] == NULL) {
493
0
    ctr->num_values = 0;
494
0
    return 0;
495
0
  }
496
0
  ctr->num_values++;
497
498
0
  return ctr->num_values;
499
0
}
500
501
/***********************************************************************
502
 Add a new registry SZ value to the array
503
 **********************************************************************/
504
505
int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
506
0
{
507
0
  DATA_BLOB blob;
508
509
0
  if (!push_reg_sz(ctr, &blob, data)) {
510
0
    return -1;
511
0
  }
512
513
0
  return regval_ctr_addvalue(ctr, name, REG_SZ,
514
0
           (const uint8_t *)blob.data,
515
0
           blob.length);
516
0
}
517
518
/***********************************************************************
519
 Add a new registry MULTI_SZ value to the array
520
 **********************************************************************/
521
522
int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
523
0
{
524
0
  DATA_BLOB blob;
525
526
0
  if (!push_reg_multi_sz(ctr, &blob, data)) {
527
0
    return -1;
528
0
  }
529
530
0
  return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
531
0
           (const uint8_t *)blob.data,
532
0
           blob.length);
533
0
}
534
535
/***********************************************************************
536
 Add a new registry value to the array
537
 **********************************************************************/
538
539
int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
540
0
{
541
0
  if ( val ) {
542
0
    regval_ctr_addvalue(ctr, val->valuename, val->type,
543
0
            (uint8_t *)val->data_p, val->size);
544
0
  }
545
546
0
  return ctr->num_values;
547
0
}
548
549
/***********************************************************************
550
 Delete a single value from the registry container.
551
 No need to free memory since it is talloc'd.
552
 **********************************************************************/
553
554
int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
555
0
{
556
0
  uint32_t i;
557
558
0
  for ( i=0; i<ctr->num_values; i++ ) {
559
0
    if ( strequal( ctr->values[i]->valuename, name ) )
560
0
      break;
561
0
  }
562
563
  /* just return if we don't find it */
564
565
0
  if ( i == ctr->num_values )
566
0
    return ctr->num_values;
567
568
  /* If 'i' was not the last element, just shift everything down one */
569
0
  ctr->num_values--;
570
0
  if ( i < ctr->num_values )
571
0
    memmove(&ctr->values[i], &ctr->values[i+1],
572
0
      sizeof(struct regval_blob*)*(ctr->num_values-i));
573
574
0
  return ctr->num_values;
575
0
}
576
577
/***********************************************************************
578
 Retrieve single value from the registry container.
579
 No need to free memory since it is talloc'd.
580
 **********************************************************************/
581
582
struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
583
          const char *name)
584
0
{
585
0
  uint32_t i;
586
587
  /* search for the value */
588
589
0
  for ( i=0; i<ctr->num_values; i++ ) {
590
0
    if ( strequal( ctr->values[i]->valuename, name ) )
591
0
      return ctr->values[i];
592
0
  }
593
594
0
  return NULL;
595
0
}
596
597
int regval_ctr_get_seqnum(struct regval_ctr *ctr)
598
0
{
599
0
  if (ctr == NULL) {
600
0
    return -1;
601
0
  }
602
603
0
  return ctr->seqnum;
604
0
}
605
606
WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
607
0
{
608
0
  if (ctr == NULL) {
609
0
    return WERR_INVALID_PARAMETER;
610
0
  }
611
612
0
  ctr->seqnum = seqnum;
613
614
0
  return WERR_OK;
615
0
}