LCOV - code coverage report
Current view: top level - source4/lib/registry - interface.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 60 92 65.2 %
Date: 2024-04-21 15:09:00 Functions: 14 15 93.3 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Transparent registry backend handling
       4             :    Copyright (C) Jelmer Vernooij                        2003-2007.
       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             : #include "includes.h"
      21             : #include "../lib/util/dlinklist.h"
      22             : #include "lib/registry/registry.h"
      23             : #include "system/filesys.h"
      24             : 
      25             : #undef strcasecmp
      26             : 
      27             : /**
      28             :  * @file
      29             :  * @brief Main registry functions
      30             :  */
      31             : 
      32             : const struct reg_predefined_key reg_predefined_keys[] = {
      33             :         {HKEY_CLASSES_ROOT,"HKEY_CLASSES_ROOT" },
      34             :         {HKEY_CURRENT_USER,"HKEY_CURRENT_USER" },
      35             :         {HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" },
      36             :         {HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA" },
      37             :         {HKEY_USERS, "HKEY_USERS" },
      38             :         {HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG" },
      39             :         {HKEY_DYN_DATA, "HKEY_DYN_DATA" },
      40             :         {HKEY_PERFORMANCE_TEXT, "HKEY_PERFORMANCE_TEXT" },
      41             :         {HKEY_PERFORMANCE_NLSTEXT, "HKEY_PERFORMANCE_NLSTEXT" },
      42             :         { 0, NULL }
      43             : };
      44             : 
      45             : /** Obtain name of specific hkey. */
      46           1 : _PUBLIC_ const char *reg_get_predef_name(uint32_t hkey)
      47             : {
      48           1 :         unsigned int i;
      49           3 :         for (i = 0; reg_predefined_keys[i].name; i++) {
      50           3 :                 if (reg_predefined_keys[i].handle == hkey)
      51           1 :                         return reg_predefined_keys[i].name;
      52             :         }
      53             : 
      54           0 :         return NULL;
      55             : }
      56             : 
      57             : /** Get predefined key by name. */
      58       11313 : _PUBLIC_ WERROR reg_get_predefined_key_by_name(struct registry_context *ctx,
      59             :                                                const char *name,
      60             :                                                struct registry_key **key)
      61             : {
      62        1303 :         unsigned int i;
      63             : 
      64       33934 :         for (i = 0; reg_predefined_keys[i].name; i++) {
      65       33933 :                 if (!strcasecmp(reg_predefined_keys[i].name, name))
      66       11312 :                         return reg_get_predefined_key(ctx,
      67       11312 :                                                       reg_predefined_keys[i].handle,
      68             :                                                       key);
      69             :         }
      70             : 
      71           1 :         DEBUG(1, ("No predefined key with name '%s'\n", name));
      72             : 
      73           1 :         return WERR_FILE_NOT_FOUND;
      74             : }
      75             : 
      76             : /** Get predefined key by id. */
      77       11686 : _PUBLIC_ WERROR reg_get_predefined_key(struct registry_context *ctx,
      78             :                                        uint32_t hkey, struct registry_key **key)
      79             : {
      80       11686 :         return ctx->ops->get_predefined_key(ctx, hkey, key);
      81             : }
      82             : 
      83             : /**
      84             :  * Open a key
      85             :  * First tries to use the open_key function from the backend
      86             :  * then falls back to get_subkey_by_name and later get_subkey_by_index
      87             :  */
      88        8557 : _PUBLIC_ WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent,
      89             :                              const char *name, struct registry_key **result)
      90             : {
      91        8557 :         if (parent == NULL) {
      92           0 :                 DEBUG(0, ("Invalid parent key specified for open of '%s'\n",
      93             :                         name));
      94           0 :                 return WERR_INVALID_PARAMETER;
      95             :         }
      96             : 
      97        8557 :         if (parent->context->ops->open_key == NULL) {
      98           0 :                 DEBUG(0, ("Registry backend doesn't have open_key!\n"));
      99           0 :                 return WERR_NOT_SUPPORTED;
     100             :         }
     101             : 
     102        8557 :         return parent->context->ops->open_key(mem_ctx, parent, name, result);
     103             : }
     104             : 
     105             : /**
     106             :  * Get value by index
     107             :  */
     108        2885 : _PUBLIC_ WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx,
     109             :                                            const struct registry_key *key,
     110             :                                            uint32_t idx, const char **name,
     111             :                                            uint32_t *type, DATA_BLOB *data)
     112             : {
     113        2885 :         if (key == NULL)
     114           0 :                 return WERR_INVALID_PARAMETER;
     115             : 
     116        2885 :         if (key->context->ops->enum_value == NULL)
     117           0 :                 return WERR_NOT_SUPPORTED;
     118             : 
     119        2885 :         return key->context->ops->enum_value(mem_ctx, key, idx, name,
     120             :                                              type, data);
     121             : }
     122             : 
     123             : /**
     124             :  * Get the number of subkeys.
     125             :  */
     126         268 : _PUBLIC_ WERROR reg_key_get_info(TALLOC_CTX *mem_ctx,
     127             :                                  const struct registry_key *key,
     128             :                                  const char **classname,
     129             :                                  uint32_t *num_subkeys,
     130             :                                  uint32_t *num_values,
     131             :                                  NTTIME *last_change_time,
     132             :                                  uint32_t *max_subkeynamelen,
     133             :                                  uint32_t *max_valnamelen,
     134             :                                  uint32_t *max_valbufsize)
     135             : {
     136         268 :         if (key == NULL)
     137           0 :                 return WERR_INVALID_PARAMETER;
     138             : 
     139         268 :         if (key->context->ops->get_key_info == NULL)
     140           0 :                 return WERR_NOT_SUPPORTED;
     141             : 
     142         268 :         return key->context->ops->get_key_info(mem_ctx,
     143             :                                                key, classname, num_subkeys,
     144             :                                                num_values, last_change_time,
     145             :                                                max_subkeynamelen,
     146             :                                                max_valnamelen, max_valbufsize);
     147             : }
     148             : 
     149             : /**
     150             :  * Get subkey by index.
     151             :  */
     152         260 : _PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx,
     153             :                                             const struct registry_key *key,
     154             :                                             uint32_t idx, const char **name,
     155             :                                             const char **keyclass,
     156             :                                             NTTIME *last_changed_time)
     157             : {
     158         260 :         if (key == NULL)
     159           0 :                 return WERR_INVALID_PARAMETER;
     160             : 
     161         260 :         if (key->context->ops->enum_key == NULL)
     162           0 :                 return WERR_NOT_SUPPORTED;
     163             : 
     164         260 :         return key->context->ops->enum_key(mem_ctx, key, idx, name,
     165             :                                            keyclass, last_changed_time);
     166             : }
     167             : 
     168             : /**
     169             :  * Get value by name.
     170             :  */
     171        8165 : _PUBLIC_ WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx,
     172             :                                           const struct registry_key *key,
     173             :                                           const char *name,
     174             :                                           uint32_t *type,
     175             :                                           DATA_BLOB *data)
     176             : {
     177        8165 :         if (key == NULL)
     178           0 :                 return WERR_INVALID_PARAMETER;
     179             : 
     180        8165 :         if (key->context->ops->get_value == NULL)
     181           0 :                 return WERR_NOT_SUPPORTED;
     182             : 
     183        8165 :         return key->context->ops->get_value(mem_ctx, key, name, type, data);
     184             : }
     185             : 
     186             : /**
     187             :  * Delete a key.
     188             :  */
     189        2564 : _PUBLIC_ WERROR reg_key_del(TALLOC_CTX *mem_ctx, struct registry_key *parent,
     190             :                             const char *name)
     191             : {
     192        2564 :         if (parent == NULL)
     193           0 :                 return WERR_INVALID_PARAMETER;
     194             : 
     195        2564 :         if (parent->context->ops->delete_key == NULL)
     196           0 :                 return WERR_NOT_SUPPORTED;
     197             : 
     198        2564 :         return parent->context->ops->delete_key(mem_ctx, parent, name);
     199             : }
     200             : 
     201             : /**
     202             :  * Add a key.
     203             :  */
     204       12382 : _PUBLIC_ WERROR reg_key_add_name(TALLOC_CTX *mem_ctx,
     205             :                                  struct registry_key *parent,
     206             :                                  const char *path, const char *key_class,
     207             :                                  struct security_descriptor *desc,
     208             :                                  struct registry_key **newkey)
     209             : {
     210       12382 :         if (parent == NULL)
     211           0 :                 return WERR_INVALID_PARAMETER;
     212             : 
     213       12382 :         if (parent->context->ops->create_key == NULL) {
     214           0 :                 DEBUG(1, ("Backend '%s' doesn't support method add_key\n",
     215             :                                   parent->context->ops->name));
     216           0 :                 return WERR_NOT_SUPPORTED;
     217             :         }
     218             : 
     219       12382 :         return parent->context->ops->create_key(mem_ctx, parent, path,
     220             :                                                 key_class, desc, newkey);
     221             : }
     222             : 
     223             : /**
     224             :  * Set a value.
     225             :  */
     226        3026 : _PUBLIC_ WERROR reg_val_set(struct registry_key *key, const char *value,
     227             :                             uint32_t type, const DATA_BLOB data)
     228             : {
     229        3026 :         if (key == NULL)
     230           0 :                 return WERR_INVALID_PARAMETER;
     231             : 
     232             :         /* A 'real' set function has preference */
     233        3026 :         if (key->context->ops->set_value == NULL) {
     234           0 :                 DEBUG(1, ("Backend '%s' doesn't support method set_value\n",
     235             :                                   key->context->ops->name));
     236           0 :                 return WERR_NOT_SUPPORTED;
     237             :         }
     238             : 
     239        3026 :         return key->context->ops->set_value(key, value, type, data);
     240             : }
     241             : 
     242             : /**
     243             :  * Get the security descriptor on a key.
     244             :  */
     245           0 : _PUBLIC_ WERROR reg_get_sec_desc(TALLOC_CTX *ctx,
     246             :                                  const struct registry_key *key,
     247             :                                  struct security_descriptor **secdesc)
     248             : {
     249           0 :         if (key == NULL)
     250           0 :                 return WERR_INVALID_PARAMETER;
     251             : 
     252             :         /* A 'real' set function has preference */
     253           0 :         if (key->context->ops->get_sec_desc == NULL)
     254           0 :                 return WERR_NOT_SUPPORTED;
     255             : 
     256           0 :         return key->context->ops->get_sec_desc(ctx, key, secdesc);
     257             : }
     258             : 
     259             : /**
     260             :  * Delete a value.
     261             :  */
     262        2403 : _PUBLIC_ WERROR reg_del_value(TALLOC_CTX *mem_ctx, struct registry_key *key,
     263             :                               const char *valname)
     264             : {
     265        2403 :         if (key == NULL)
     266           0 :                 return WERR_INVALID_PARAMETER;
     267             : 
     268        2403 :         if (key->context->ops->delete_value == NULL)
     269           0 :                 return WERR_NOT_SUPPORTED;
     270             : 
     271        2403 :         return key->context->ops->delete_value(mem_ctx, key, valname);
     272             : }
     273             : 
     274             : /**
     275             :  * Flush a key to disk.
     276             :  */
     277         642 : _PUBLIC_ WERROR reg_key_flush(struct registry_key *key)
     278             : {
     279         642 :         if (key == NULL)
     280           1 :                 return WERR_INVALID_PARAMETER;
     281             : 
     282         641 :         if (key->context->ops->flush_key == NULL)
     283           0 :                 return WERR_NOT_SUPPORTED;
     284             : 
     285         641 :         return key->context->ops->flush_key(key);
     286             : }
     287             : 
     288           1 : _PUBLIC_ WERROR reg_set_sec_desc(struct registry_key *key,
     289             :                                  const struct security_descriptor *security)
     290             : {
     291           1 :         if (key == NULL)
     292           0 :                 return WERR_INVALID_PARAMETER;
     293             : 
     294           1 :         if (key->context->ops->set_sec_desc == NULL)
     295           0 :                 return WERR_NOT_SUPPORTED;
     296             : 
     297           1 :         return key->context->ops->set_sec_desc(key, security);
     298             : }

Generated by: LCOV version 1.14