Ignore:
Timestamp:
Nov 14, 2012, 12:59:34 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to 3.6.0

Location:
vendor/current/source4/lib/registry
Files:
1 added
2 deleted
28 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/source4/lib/registry/dir.c

    r414 r740  
    4141
    4242        path = talloc_asprintf(mem_ctx, "%s/%s", dk->path, name);
     43        W_ERROR_HAVE_NO_MEMORY(path);
    4344        ret = mkdir(path, 0700);
    4445        if (ret == 0) {
    4546                struct dir_key *key = talloc(mem_ctx, struct dir_key);
     47                W_ERROR_HAVE_NO_MEMORY(key);
    4648                key->key.ops = &reg_backend_dir;
    4749                key->path = talloc_steal(key, path);
     
    7779
    7880                path = talloc_asprintf(name, "%s/%s", name, e->d_name);
    79                 if (!path)
    80                         return WERR_NOMEM;
     81                W_ERROR_HAVE_NO_MEMORY(path);
    8182
    8283                stat(path, &stbuf);
     
    109110}
    110111
    111 static WERROR reg_dir_del_key(const struct hive_key *k, const char *name)
     112static WERROR reg_dir_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *k,
     113                              const char *name)
    112114{
    113115        struct dir_key *dk = talloc_get_type(k, struct dir_key);
    114         char *child = talloc_asprintf(NULL, "%s/%s", dk->path, name);
     116        char *child;
    115117        WERROR ret;
     118
     119        child = talloc_asprintf(mem_ctx, "%s/%s", dk->path, name);
     120        W_ERROR_HAVE_NO_MEMORY(child);
    116121
    117122        ret = reg_dir_delete_recursive(child);
     
    137142
    138143        fullpath = talloc_asprintf(mem_ctx, "%s/%s", p->path, name);
     144        W_ERROR_HAVE_NO_MEMORY(fullpath);
    139145
    140146        d = opendir(fullpath);
     
    142148                DEBUG(3,("Unable to open '%s': %s\n", fullpath,
    143149                        strerror(errno)));
     150                talloc_free(fullpath);
    144151                return WERR_BADFILE;
    145152        }
     
    160167        struct dirent *e;
    161168        const struct dir_key *dk = talloc_get_type(k, struct dir_key);
    162         int i = 0;
     169        unsigned int i = 0;
    163170        DIR *d;
    164171
     
    174181
    175182                        /* Check if file is a directory */
    176                         asprintf(&thispath, "%s/%s", dk->path, e->d_name);
     183                        thispath = talloc_asprintf(mem_ctx, "%s/%s", dk->path,
     184                                                   e->d_name);
     185                        W_ERROR_HAVE_NO_MEMORY(thispath);
    177186                        stat(thispath, &stbuf);
    178187
    179188                        if (!S_ISDIR(stbuf.st_mode)) {
    180                                 SAFE_FREE(thispath);
     189                                talloc_free(thispath);
    181190                                continue;
    182191                        }
     
    185194                                struct stat st;
    186195                                *name = talloc_strdup(mem_ctx, e->d_name);
     196                                W_ERROR_HAVE_NO_MEMORY(*name);
    187197                                *classname = NULL;
    188198                                stat(thispath, &st);
    189199                                unix_to_nt_time(last_mod_time, st.st_mtime);
    190                                 SAFE_FREE(thispath);
     200                                talloc_free(thispath);
    191201                                closedir(d);
    192202                                return WERR_OK;
     
    194204                        i++;
    195205
    196                         SAFE_FREE(thispath);
     206                        talloc_free(thispath);
    197207                }
    198208        }
     
    212222
    213223        dk = talloc(parent_ctx, struct dir_key);
     224        W_ERROR_HAVE_NO_MEMORY(dk);
    214225        dk->key.ops = &reg_backend_dir;
    215226        dk->path = talloc_strdup(dk, location);
     
    271282                        char *path = talloc_asprintf(ctx, "%s/%s",
    272283                                                     dk->path, e->d_name);
     284                        W_ERROR_HAVE_NO_MEMORY(path);
    273285
    274286                        if (stat(path, &st) < 0) {
    275287                                DEBUG(0, ("Error statting %s: %s\n", path,
    276288                                        strerror(errno)));
     289                                talloc_free(path);
    277290                                continue;
    278291                        }
     
    309322{
    310323        const struct dir_key *dk = talloc_get_type(key, struct dir_key);
    311         char *path = talloc_asprintf(dk, "%s/%s", dk->path, name);
    312 
    313         if (!file_save(path, data.data, data.length))
     324        char *path;
     325        bool ret;
     326
     327        path = talloc_asprintf(dk, "%s/%s", dk->path, name);
     328        W_ERROR_HAVE_NO_MEMORY(path);
     329
     330        ret = file_save(path, data.data, data.length);
     331
     332        talloc_free(path);
     333
     334        if (!ret) {
    314335                return WERR_GENERAL_FAILURE;
     336        }
    315337
    316338        /* FIXME: Type */
     
    324346{
    325347        const struct dir_key *dk = talloc_get_type(key, struct dir_key);
    326         char *path = talloc_asprintf(mem_ctx, "%s/%s", dk->path, name);
     348        char *path;
    327349        size_t size;
    328350        char *contents;
    329351
     352        path = talloc_asprintf(mem_ctx, "%s/%s", dk->path, name);
     353        W_ERROR_HAVE_NO_MEMORY(path);
     354
    330355        contents = file_load(path, &size, 0, mem_ctx);
     356
    331357        talloc_free(path);
     358
    332359        if (contents == NULL)
    333360                return WERR_BADFILE;
     
    343370
    344371static WERROR reg_dir_enum_value(TALLOC_CTX *mem_ctx,
    345                                  struct hive_key *key, int idx,
     372                                 struct hive_key *key, uint32_t idx,
    346373                                 const char **name,
    347374                                 uint32_t *type, DATA_BLOB *data)
     
    350377        DIR *d;
    351378        struct dirent *e;
    352         int i;
     379        unsigned int i;
    353380
    354381        d = opendir(dk->path);
     
    365392
    366393                if (i == idx) {
    367                         if (name != NULL)
     394                        if (name != NULL) {
    368395                                *name = talloc_strdup(mem_ctx, e->d_name);
     396                                W_ERROR_HAVE_NO_MEMORY(*name);
     397                        }
    369398                        W_ERROR_NOT_OK_RETURN(reg_dir_get_value(mem_ctx, key,
    370399                                                                *name, type,
     
    381410
    382411
    383 static WERROR reg_dir_del_value (struct hive_key *key, const char *name)
     412static WERROR reg_dir_del_value(TALLOC_CTX *mem_ctx,
     413                                struct hive_key *key, const char *name)
    384414{
    385415        const struct dir_key *dk = talloc_get_type(key, struct dir_key);
    386         char *path = talloc_asprintf(key, "%s/%s", dk->path, name);
    387         if (unlink(path) < 0) {
    388                 talloc_free(path);
     416        char *path;
     417        int ret;
     418
     419        path = talloc_asprintf(mem_ctx, "%s/%s", dk->path, name);
     420        W_ERROR_HAVE_NO_MEMORY(path);
     421
     422        ret = unlink(path);
     423
     424        talloc_free(path);
     425
     426        if (ret < 0) {
    389427                if (errno == ENOENT)
    390428                        return WERR_BADFILE;
    391429                return WERR_GENERAL_FAILURE;
    392430        }
    393         talloc_free(path);
    394431
    395432        return WERR_OK;
  • vendor/current/source4/lib/registry/hive.c

    r414 r740  
    5555        if (!strncmp(peek, "regf", 4)) {
    5656                close(fd);
    57                 return reg_open_regf_file(parent_ctx, location, lp_iconv_convenience(lp_ctx), root);
     57                return reg_open_regf_file(parent_ctx, location, root);
    5858        } else if (!strncmp(peek, "TDB file", 8)) {
    5959                close(fd);
     
    9292}
    9393
    94 _PUBLIC_ WERROR hive_key_del(const struct hive_key *key, const char *name)
     94_PUBLIC_ WERROR hive_key_del(TALLOC_CTX *mem_ctx, const struct hive_key *key,
     95                             const char *name)
    9596{
    96         return key->ops->del_key(key, name);
     97        return key->ops->del_key(mem_ctx, key, name);
    9798}
    9899
     
    164165}
    165166
    166 WERROR hive_key_del_value(struct hive_key *key, const char *name)
     167WERROR hive_key_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
     168                          const char *name)
    167169{
    168170        if (key->ops->delete_value == NULL)
    169171                return WERR_NOT_SUPPORTED;
    170172
    171         return key->ops->delete_value(key, name);
     173        return key->ops->delete_value(mem_ctx, key, name);
    172174}
    173175
  • vendor/current/source4/lib/registry/interface.c

    r414 r740  
    4545_PUBLIC_ const char *reg_get_predef_name(uint32_t hkey)
    4646{
    47         int i;
     47        unsigned int i;
    4848        for (i = 0; reg_predefined_keys[i].name; i++) {
    4949                if (reg_predefined_keys[i].handle == hkey)
     
    5959                                               struct registry_key **key)
    6060{
    61         int i;
     61        unsigned int i;
    6262
    6363        for (i = 0; reg_predefined_keys[i].name; i++) {
     
    151151_PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx,
    152152                                            const struct registry_key *key,
    153                                             int idx, const char **name,
     153                                            uint32_t idx, const char **name,
    154154                                            const char **keyclass,
    155155                                            NTTIME *last_changed_time)
     
    186186 * Delete a key.
    187187 */
    188 _PUBLIC_ WERROR reg_key_del(struct registry_key *parent, const char *name)
     188_PUBLIC_ WERROR reg_key_del(TALLOC_CTX *mem_ctx, struct registry_key *parent,
     189                            const char *name)
    189190{
    190191        if (parent == NULL)
     
    194195                return WERR_NOT_SUPPORTED;
    195196
    196         return parent->context->ops->delete_key(parent, name);
     197        return parent->context->ops->delete_key(mem_ctx, parent, name);
    197198}
    198199
     
    202203_PUBLIC_ WERROR reg_key_add_name(TALLOC_CTX *mem_ctx,
    203204                                 struct registry_key *parent,
    204                                  const char *name, const char *key_class,
     205                                 const char *path, const char *key_class,
    205206                                 struct security_descriptor *desc,
    206207                                 struct registry_key **newkey)
     
    215216        }
    216217
    217         return parent->context->ops->create_key(mem_ctx, parent, name,
     218        return parent->context->ops->create_key(mem_ctx, parent, path,
    218219                                                key_class, desc, newkey);
    219220}
     
    258259 * Delete a value.
    259260 */
    260 _PUBLIC_ WERROR reg_del_value(struct registry_key *key, const char *valname)
     261_PUBLIC_ WERROR reg_del_value(TALLOC_CTX *mem_ctx, struct registry_key *key,
     262                              const char *valname)
    261263{
    262264        if (key == NULL)
     
    266268                return WERR_NOT_SUPPORTED;
    267269
    268         return key->context->ops->delete_value(key, valname);
     270        return key->context->ops->delete_value(mem_ctx, key, valname);
    269271}
    270272
  • vendor/current/source4/lib/registry/ldb.c

    r414 r740  
    33   Registry interface
    44   Copyright (C) 2004-2007, Jelmer Vernooij, jelmer@samba.org
    5    Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
     5   Copyright (C) 2008-2010, Matthias Dieter Wallnöfer, mdw@samba.org
    66
    77   This program is free software; you can redistribute it and/or modify
     
    2121#include "includes.h"
    2222#include "registry.h"
    23 #include "lib/ldb/include/ldb.h"
    24 #include "lib/ldb/include/ldb_errors.h"
     23#include <ldb.h>
     24#include <ldb_errors.h>
    2525#include "ldb_wrap.h"
    2626#include "librpc/gen_ndr/winreg.h"
     
    3535        struct ldb_dn *dn;
    3636        struct ldb_message **subkeys, **values;
    37         int subkey_count, value_count;
     37        unsigned int subkey_count, value_count;
     38        const char *classname;
    3839};
    3940
    40 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, 
     41static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
    4142                                 struct ldb_message *msg,
    4243                                 const char **name, uint32_t *type,
     
    4647        uint32_t value_type;
    4748
    48         if (name != NULL)
     49        if (name != NULL) {
    4950                *name = talloc_strdup(mem_ctx,
    5051                                      ldb_msg_find_attr_as_string(msg, "value",
    51                                       NULL));
     52                                      ""));
     53        }
    5254
    5355        value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
    54         *type = value_type; 
     56        *type = value_type;
    5557
    5658        val = ldb_msg_find_ldb_val(msg, "data");
     
    6062        case REG_SZ:
    6163        case REG_EXPAND_SZ:
    62                 if (val != NULL)
     64                if (val != NULL) {
     65                        /* The data should be provided as UTF16 string */
    6366                        convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
    64                                                      val->data, val->length,
    65                                                      (void **)&data->data, &data->length, false);
    66                 else {
     67                                              val->data, val->length,
     68                                              (void **)&data->data, &data->length, false);
     69                } else {
    6770                        data->data = NULL;
    6871                        data->length = 0;
     
    7073                break;
    7174
    72         case REG_BINARY:
    73                 if (val != NULL)
    74                         *data = data_blob_talloc(mem_ctx, val->data, val->length);
    75                 else {
     75        case REG_DWORD:
     76        case REG_DWORD_BIG_ENDIAN:
     77                if (val != NULL) {
     78                        /* The data is a plain DWORD */
     79                        uint32_t tmp = strtoul((char *)val->data, NULL, 0);
     80                        data->data = talloc_size(mem_ctx, sizeof(uint32_t));
     81                        if (data->data != NULL) {
     82                                SIVAL(data->data, 0, tmp);
     83                        }
     84                        data->length = sizeof(uint32_t);
     85                } else {
    7686                        data->data = NULL;
    7787                        data->length = 0;
     
    7989                break;
    8090
    81         case REG_DWORD: {
    82                 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
    83                 *data = data_blob_talloc(mem_ctx, &tmp, 4);
     91        case REG_QWORD:
     92                if (val != NULL) {
     93                        /* The data is a plain QWORD */
     94                        uint64_t tmp = strtoull((char *)val->data, NULL, 0);
     95                        data->data = talloc_size(mem_ctx, sizeof(uint64_t));
     96                        if (data->data != NULL) {
     97                                SBVAL(data->data, 0, tmp);
     98                        }
     99                        data->length = sizeof(uint64_t);
     100                } else {
     101                        data->data = NULL;
     102                        data->length = 0;
    84103                }
    85104                break;
    86105
     106        case REG_BINARY:
    87107        default:
    88                 *data = data_blob_talloc(mem_ctx, val->data, val->length);
     108                if (val != NULL) {
     109                        data->data = talloc_memdup(mem_ctx, val->data,
     110                                                   val->length);
     111                        data->length = val->length;
     112                } else {
     113                        data->data = NULL;
     114                        data->length = 0;
     115                }
    89116                break;
    90117        }
     
    96123                                              uint32_t type, DATA_BLOB data)
    97124{
    98         struct ldb_val val;
    99         struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
    100         char *type_s;
    101 
    102         ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
     125        struct ldb_message *msg;
     126        char *name_dup, *type_str;
     127        int ret;
     128
     129        msg = talloc_zero(mem_ctx, struct ldb_message);
     130        if (msg == NULL) {
     131                return NULL;
     132        }
     133
     134        name_dup = talloc_strdup(msg, name);
     135        if (name_dup == NULL) {
     136                talloc_free(msg);
     137                return NULL;
     138        }
     139
     140        ret = ldb_msg_add_string(msg, "value", name_dup);
     141        if (ret != LDB_SUCCESS) {
     142                talloc_free(msg);
     143                return NULL;
     144        }
    103145
    104146        switch (type) {
    105147        case REG_SZ:
    106148        case REG_EXPAND_SZ:
    107                 if (data.data[0] != '\0') {
    108                         convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
    109                                                    (void *)data.data,
    110                                                    data.length,
    111                                                    (void **)&val.data, &val.length, false);
    112                         ldb_msg_add_value(msg, "data", &val, NULL);
     149                if ((data.length > 0) && (data.data != NULL)) {
     150                        struct ldb_val *val;
     151                        bool ret2 = false;
     152
     153                        val = talloc_zero(msg, struct ldb_val);
     154                        if (val == NULL) {
     155                                talloc_free(msg);
     156                                return NULL;
     157                        }
     158
     159                        /* The data is provided as UTF16 string */
     160                        ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
     161                                                     (void *)data.data, data.length,
     162                                                     (void **)&val->data, &val->length,
     163                                                     false);
     164                        if (ret2) {
     165                                ret = ldb_msg_add_value(msg, "data", val, NULL);
     166                        } else {
     167                                /* workaround for non-standard data */
     168                                ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
     169                        }
    113170                } else {
    114                         ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
     171                        ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
    115172                }
    116173                break;
    117174
     175        case REG_DWORD:
     176        case REG_DWORD_BIG_ENDIAN:
     177                if ((data.length > 0) && (data.data != NULL)) {
     178                        if (data.length == sizeof(uint32_t)) {
     179                                char *conv_str;
     180
     181                                conv_str = talloc_asprintf(msg, "0x%8.8x",
     182                                                           IVAL(data.data, 0));
     183                                if (conv_str == NULL) {
     184                                        talloc_free(msg);
     185                                        return NULL;
     186                                }
     187                                ret = ldb_msg_add_string(msg, "data", conv_str);
     188                        } else {
     189                                /* workaround for non-standard data */
     190                                talloc_free(msg);
     191                                return NULL;
     192                        }
     193                } else {
     194                        ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
     195                }
     196                break;
     197
     198        case REG_QWORD:
     199                if ((data.length > 0) && (data.data != NULL)) {
     200                        if (data.length == sizeof(uint64_t)) {
     201                                char *conv_str;
     202
     203                                conv_str = talloc_asprintf(msg, "0x%16.16llx",
     204                                                           (unsigned long long)BVAL(data.data, 0));
     205                                if (conv_str == NULL) {
     206                                        talloc_free(msg);
     207                                        return NULL;
     208                                }
     209                                ret = ldb_msg_add_string(msg, "data", conv_str);
     210                        } else {
     211                                /* workaround for non-standard data */
     212                                talloc_free(msg);
     213                                return NULL;
     214
     215                        }
     216                } else {
     217                        ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
     218                }
     219                break;
     220
    118221        case REG_BINARY:
    119                 if (data.length > 0)
    120                         ldb_msg_add_value(msg, "data", &data, NULL);
    121                 else
    122                         ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
     222        default:
     223                if ((data.length > 0) && (data.data != NULL)) {
     224                        ret = ldb_msg_add_value(msg, "data", &data, NULL);
     225                } else {
     226                        ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
     227                }
    123228                break;
    124 
    125         case REG_DWORD:
    126                 ldb_msg_add_string(msg, "data",
    127                                    talloc_asprintf(mem_ctx, "0x%x",
    128                                                    IVAL(data.data, 0)));
    129                 break;
    130         default:
    131                 ldb_msg_add_value(msg, "data", &data, NULL);
    132         }
    133 
    134 
    135         type_s = talloc_asprintf(mem_ctx, "%u", type);
    136         ldb_msg_add_string(msg, "type", type_s);
     229        }
     230
     231        if (ret != LDB_SUCCESS) {
     232                talloc_free(msg);
     233                return NULL;
     234        }
     235
     236        type_str = talloc_asprintf(mem_ctx, "%u", type);
     237        if (type_str == NULL) {
     238                talloc_free(msg);
     239                return NULL;
     240        }
     241
     242        ret = ldb_msg_add_string(msg, "type", type_str);
     243        if (ret != LDB_SUCCESS) {
     244                talloc_free(msg);
     245                return NULL;
     246        }
    137247
    138248        return msg;
     
    167277                                      const char *path, const char *add)
    168278{
    169         TALLOC_CTX *local_ctx;
    170279        struct ldb_dn *ret;
    171         char *mypath = talloc_strdup(mem_ctx, path);
     280        char *mypath;
    172281        char *begin;
    173282        struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
    174283        struct ldb_context *ldb = kd->ldb;
    175284
    176         local_ctx = talloc_new(mem_ctx);
    177 
    178         if (add) {
    179                 ret = ldb_dn_new(mem_ctx, ldb, add);
    180         } else {
    181                 ret = ldb_dn_new(mem_ctx, ldb, NULL);
    182         }
     285        mypath = talloc_strdup(mem_ctx, path);
     286        if (mypath == NULL) {
     287                return NULL;
     288        }
     289
     290        ret = ldb_dn_new(mem_ctx, ldb, add);
    183291        if (!ldb_dn_validate(ret)) {
    184292                talloc_free(ret);
    185                 talloc_free(local_ctx);
    186293                return NULL;
    187294        }
    188295
    189         while (mypath) {
    190                 char *keyname;
    191 
    192                 begin = strrchr(mypath, '\\');
    193 
    194                 if (begin) keyname = begin + 1;
    195                 else keyname = mypath;
    196 
    197                 if(strlen(keyname)) {
    198                         if (!ldb_dn_add_base_fmt(ret, "key=%s",
    199                                                  reg_ldb_escape(local_ctx,
    200                                                                 keyname)))
    201                         {
    202                                 talloc_free(local_ctx);
    203                                 return NULL;
    204                         }
    205                 }
    206 
    207                 if(begin) {
     296        if (!ldb_dn_add_base(ret, kd->dn)) {
     297                talloc_free(ret);
     298                return NULL;
     299        }
     300
     301        while (mypath[0] != '\0') {
     302                begin = strchr(mypath, '\\');
     303                if (begin != NULL) {
    208304                        *begin = '\0';
     305                }
     306
     307                if (!ldb_dn_add_child_fmt(ret, "key=%s",
     308                                          reg_ldb_escape(mem_ctx, mypath))) {
     309                        talloc_free(ret);
     310                        return NULL;
     311                }
     312
     313                if (begin != NULL) {
     314                        mypath = begin + 1;
    209315                } else {
    210316                        break;
    211317                }
    212318        }
    213 
    214         ldb_dn_add_base(ret, kd->dn);
    215 
    216         talloc_free(local_ctx);
    217319
    218320        return ret;
     
    225327        int ret;
    226328
    227         ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
    228 
     329        ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
     330                         NULL, "(key=*)");
    229331        if (ret != LDB_SUCCESS) {
    230332                DEBUG(0, ("Error getting subkeys for '%s': %s\n",
     
    248350        ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
    249351                         NULL, "(value=*)");
    250 
    251352        if (ret != LDB_SUCCESS) {
    252353                DEBUG(0, ("Error getting values for '%s': %s\n",
     
    269370                                   NTTIME *last_mod_time)
    270371{
    271         struct ldb_message_element *el;
    272372        struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
    273        
     373
    274374        /* Initialization */
    275375        if (name != NULL)
    276376                *name = NULL;
    277377        if (classname != NULL)
    278                 *classname = NULL; /* TODO: Store properly */
     378                *classname = NULL;
    279379        if (last_mod_time != NULL)
    280380                *last_mod_time = 0; /* TODO: we need to add this to the
     
    289389                return WERR_NO_MORE_ITEMS;
    290390
    291         el = ldb_msg_find_element(kd->subkeys[idx], "key");
    292         SMB_ASSERT(el != NULL);
    293         SMB_ASSERT(el->num_values != 0);
    294 
    295391        if (name != NULL)
    296                 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
    297 
    298         return WERR_OK;
    299 }
    300 
    301 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
    302                                   const char **name, uint32_t *data_type,
    303                                    DATA_BLOB *data)
     392                *name = talloc_strdup(mem_ctx,
     393                                      ldb_msg_find_attr_as_string(kd->subkeys[idx], "key", NULL));
     394        if (classname != NULL)
     395                *classname = talloc_strdup(mem_ctx,
     396                                           ldb_msg_find_attr_as_string(kd->subkeys[idx], "classname", NULL));
     397
     398        return WERR_OK;
     399}
     400
     401static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx,
     402                                    const struct hive_key *k,
     403                                    const char **name, uint32_t *data_type,
     404                                    DATA_BLOB *data)
    304405{
    305406        struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
     
    309410        int ret;
    310411
    311         ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "%s", "");
     412        ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(dn=*)");
    312413
    313414        if (ret != LDB_SUCCESS) {
     
    317418        }
    318419
    319         if (res->count == 0 || res->msgs[0]->num_elements == 0)
     420        if (res->count == 0 || res->msgs[0]->num_elements == 0) {
     421                talloc_free(res);
    320422                return WERR_BADFILE;
    321 
    322         reg_ldb_unpack_value(mem_ctx,
    323                  res->msgs[0], name, data_type, data);
     423        }
     424
     425        if ((data_type != NULL) && (data != NULL)) {
     426                reg_ldb_unpack_value(mem_ctx, res->msgs[0], name, data_type,
     427                                     data);
     428        }
    324429
    325430        talloc_free(res);
     
    329434
    330435static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
    331                                   int idx, const char **name,
     436                                  uint32_t idx, const char **name,
    332437                                  uint32_t *data_type, DATA_BLOB *data)
    333438{
    334439        struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
    335440
    336         /* if default value exists, give it back */
     441        /* if the default value exists, give it back */
    337442        if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
    338443                data))) {
     
    361466{
    362467        struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
    363         struct ldb_context *c = kd->ldb;
    364         struct ldb_result *res;
    365         int ret;
    366         char *query;
    367 
    368         if (strlen(name) == 0) {
    369                 /* default value */
     468        const char *res_name;
     469        uint32_t idx;
     470
     471        /* the default value was requested, give it back */
     472        if (name[0] == '\0') {
    370473                return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
    371         } else {
    372                 /* normal value */
    373                 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
    374                 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
    375                 talloc_free(query);
    376 
    377                 if (ret != LDB_SUCCESS) {
    378                         DEBUG(0, ("Error getting values for '%s': %s\n",
    379                                 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
    380                         return WERR_FOOBAR;
    381                 }
    382 
    383                 if (res->count == 0)
    384                         return WERR_BADFILE;
    385 
    386                 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
    387 
    388                 talloc_free(res);
    389         }
    390 
    391         return WERR_OK;
     474        }
     475
     476        /* Do the search if necessary */
     477        if (kd->values == NULL) {
     478                W_ERROR_NOT_OK_RETURN(cache_values(kd));
     479        }
     480
     481        for (idx = 0; idx < kd->value_count; idx++) {
     482                res_name = ldb_msg_find_attr_as_string(kd->values[idx], "value",
     483                                                       "");
     484                if (ldb_attr_cmp(name, res_name) == 0) {
     485                        reg_ldb_unpack_value(mem_ctx, kd->values[idx], NULL,
     486                                             data_type, data);
     487                        return WERR_OK;
     488                }
     489        }
     490
     491        return WERR_BADFILE;
    392492}
    393493
     
    396496{
    397497        struct ldb_result *res;
    398         struct ldb_dn *ldap_path;
     498        struct ldb_dn *ldb_path;
    399499        int ret;
    400500        struct ldb_key_data *newkd;
     
    402502        struct ldb_context *c = kd->ldb;
    403503
    404         ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
    405 
    406         ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
     504        ldb_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
     505        W_ERROR_HAVE_NO_MEMORY(ldb_path);
     506
     507        ret = ldb_search(c, mem_ctx, &res, ldb_path, LDB_SCOPE_BASE, NULL, "(key=*)");
    407508
    408509        if (ret != LDB_SUCCESS) {
    409510                DEBUG(3, ("Error opening key '%s': %s\n",
    410                         ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
     511                        ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
    411512                return WERR_FOOBAR;
    412513        } else if (res->count == 0) {
    413514                DEBUG(3, ("Key '%s' not found\n",
    414                         ldb_dn_get_linearized(ldap_path)));
     515                        ldb_dn_get_linearized(ldb_path)));
    415516                talloc_free(res);
    416517                return WERR_BADFILE;
     
    418519
    419520        newkd = talloc_zero(mem_ctx, struct ldb_key_data);
     521        W_ERROR_HAVE_NO_MEMORY(newkd);
    420522        newkd->key.ops = &reg_backend_ldb;
    421523        newkd->ldb = talloc_reference(newkd, kd->ldb);
    422         newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
     524        newkd->dn = ldb_dn_copy(newkd, res->msgs[0]->dn);
     525        newkd->classname = talloc_steal(newkd,
     526                                        ldb_msg_find_attr_as_string(res->msgs[0], "classname", NULL));
     527
     528        talloc_free(res);
    423529
    424530        *key = (struct hive_key *)newkd;
     
    442548
    443549        wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
    444                                 location, session_info, credentials, 0, NULL);
     550                                location, session_info, credentials, 0);
    445551
    446552        if (wrap == NULL) {
     
    477583{
    478584        struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
     585        struct ldb_dn *ldb_path;
    479586        struct ldb_message *msg;
    480587        struct ldb_key_data *newkd;
    481588        int ret;
    482589
     590        ldb_path = reg_path_to_ldb(mem_ctx, parent, name, NULL);
     591        W_ERROR_HAVE_NO_MEMORY(ldb_path);
     592
    483593        msg = ldb_msg_new(mem_ctx);
    484 
    485         msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
    486 
    487         ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
    488         if (classname != NULL)
    489                 ldb_msg_add_string(msg, "classname",
    490                                    talloc_strdup(mem_ctx, classname));
     594        W_ERROR_HAVE_NO_MEMORY(msg);
     595
     596        msg->dn = ldb_path;
     597
     598        ldb_msg_add_string(msg, "key", name);
     599        if (classname != NULL) {
     600                ldb_msg_add_string(msg, "classname", classname);
     601        }
    491602
    492603        ret = ldb_add(parentkd->ldb, msg);
     604
     605        talloc_free(msg);
     606
    493607        if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
    494608                return WERR_ALREADY_EXISTS;
     
    500614        }
    501615
    502         DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
     616        DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(ldb_path)));
    503617
    504618        newkd = talloc_zero(mem_ctx, struct ldb_key_data);
     619        W_ERROR_HAVE_NO_MEMORY(newkd);
    505620        newkd->ldb = talloc_reference(newkd, parentkd->ldb);
    506621        newkd->key.ops = &reg_backend_ldb;
    507         newkd->dn = talloc_steal(newkd, msg->dn);
     622        newkd->dn = talloc_steal(newkd, ldb_path);
     623        newkd->classname = talloc_steal(newkd, classname);
    508624
    509625        *newkey = (struct hive_key *)newkd;
     
    516632}
    517633
    518 static WERROR ldb_del_value (struct hive_key *key, const char *child)
     634static WERROR ldb_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
     635                            const char *child)
    519636{
    520637        int ret;
    521638        struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
    522         TALLOC_CTX *mem_ctx;
    523639        struct ldb_message *msg;
    524640        struct ldb_dn *childdn;
    525641
    526         if (strlen(child) == 0) {
     642        if (child[0] == '\0') {
    527643                /* default value */
    528                 mem_ctx = talloc_init("ldb_del_value");
    529 
    530644                msg = talloc_zero(mem_ctx, struct ldb_message);
     645                W_ERROR_HAVE_NO_MEMORY(msg);
    531646                msg->dn = ldb_dn_copy(msg, kd->dn);
     647                W_ERROR_HAVE_NO_MEMORY(msg->dn);
    532648                ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
    533649                ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
    534650
    535651                ret = ldb_modify(kd->ldb, msg);
     652
     653                talloc_free(msg);
     654
    536655                if (ret != LDB_SUCCESS) {
    537656                        DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
    538                         talloc_free(mem_ctx);
    539657                        return WERR_FOOBAR;
    540658                }
    541 
    542                 talloc_free(mem_ctx);
    543659        } else {
    544660                /* normal value */
     
    570686}
    571687
    572 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
    573 {
    574         int i, ret;
     688static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
     689                          const char *name)
     690{
     691        unsigned int i;
     692        int ret;
    575693        struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
    576         struct ldb_dn *ldap_path;
    577         TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
     694        struct ldb_dn *ldb_path;
    578695        struct ldb_context *c = parentkd->ldb;
    579696        struct ldb_result *res_keys;
     
    585702        werr = ldb_open_key(mem_ctx, key, name, &hk);
    586703        if (!W_ERROR_IS_OK(werr)) {
    587                 talloc_free(mem_ctx);
    588704                return werr;
    589705        }
    590706
    591         ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
    592         if (!ldap_path) {
    593                 talloc_free(mem_ctx);
    594                 return WERR_FOOBAR;
    595         }
     707        ldb_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
     708        W_ERROR_HAVE_NO_MEMORY(ldb_path);
    596709
    597710        /* Search for subkeys */
    598         ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
     711        ret = ldb_search(c, mem_ctx, &res_keys, ldb_path, LDB_SCOPE_ONELEVEL,
    599712                         NULL, "(key=*)");
    600713
    601714        if (ret != LDB_SUCCESS) {
    602715                DEBUG(0, ("Error getting subkeys for '%s': %s\n",
    603                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
    604                 talloc_free(mem_ctx);
     716                      ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
    605717                return WERR_FOOBAR;
    606718        }
    607719
    608720        /* Search for values */
    609         ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
     721        ret = ldb_search(c, mem_ctx, &res_vals, ldb_path, LDB_SCOPE_ONELEVEL,
    610722                         NULL, "(value=*)");
    611723
    612724        if (ret != LDB_SUCCESS) {
    613725                DEBUG(0, ("Error getting values for '%s': %s\n",
    614                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
    615                 talloc_free(mem_ctx);
     726                      ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
    616727                return WERR_FOOBAR;
    617728        }
     
    622733        if (ret != LDB_SUCCESS) {
    623734                DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
    624                 talloc_free(mem_ctx);
    625735                return WERR_FOOBAR;
    626736        }
     
    631741                for (i = 0; i < res_keys->count; i++)
    632742                {
    633                         werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
     743                        werr = ldb_del_key(mem_ctx, hk,
     744                                           ldb_msg_find_attr_as_string(
    634745                                                        res_keys->msgs[i],
    635746                                                        "key", NULL));
    636747                        if (!W_ERROR_IS_OK(werr)) {
    637748                                ret = ldb_transaction_cancel(c);
    638                                 talloc_free(mem_ctx);
    639749                                return werr;
    640750                        }
     
    644754                for (i = 0; i < res_vals->count; i++)
    645755                {
    646                         werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
     756                        werr = ldb_del_value(mem_ctx, hk,
     757                                             ldb_msg_find_attr_as_string(
    647758                                                        res_vals->msgs[i],
    648759                                                        "value", NULL));
    649760                        if (!W_ERROR_IS_OK(werr)) {
    650761                                ret = ldb_transaction_cancel(c);
    651                                 talloc_free(mem_ctx);
    652762                                return werr;
    653763                        }
    654764                }
    655765        }
     766        talloc_free(res_keys);
     767        talloc_free(res_vals);
    656768
    657769        /* Delete the key itself */
    658         ret = ldb_delete(c, ldap_path);
     770        ret = ldb_delete(c, ldb_path);
    659771
    660772        if (ret != LDB_SUCCESS)
     
    662774                DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
    663775                ret = ldb_transaction_cancel(c);
    664                 talloc_free(mem_ctx);
    665776                return WERR_FOOBAR;
    666777        }
     
    673784                DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
    674785                ret = ldb_transaction_cancel(c);
    675                 talloc_free(mem_ctx);
    676                 return WERR_FOOBAR;
    677         }
    678 
    679         talloc_free(mem_ctx);
     786                return WERR_FOOBAR;
     787        }
    680788
    681789        /* reset cache */
     
    692800        struct ldb_message *msg;
    693801        struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
     802        unsigned int i;
    694803        int ret;
    695804        TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
    696805
    697806        msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
     807        W_ERROR_HAVE_NO_MEMORY(msg);
     808
    698809        msg->dn = ldb_dn_copy(msg, kd->dn);
    699        
    700         if (strlen(name) > 0) {
     810        W_ERROR_HAVE_NO_MEMORY(msg->dn);
     811
     812        if (name[0] != '\0') {
    701813                /* For a default value, we add/overwrite the attributes to/of the hive.
    702                    For a normal value, we create new childs. */
     814                   For a normal value, we create a new child. */
    703815                if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
    704816                                  reg_ldb_escape(mem_ctx, name)))
     
    709821        }
    710822
    711         ret = ldb_add(kd->ldb, msg);
    712         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
    713                 int i;
    714                 for (i = 0; i < msg->num_elements; i++) {
    715                         if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE)
    716                                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
    717                 }
    718                 ret = ldb_modify(kd->ldb, msg);
    719         }
     823        /* Try first a "modify" and if this doesn't work do try an "add" */
     824        for (i = 0; i < msg->num_elements; i++) {
     825                if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
     826                        msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
     827                }
     828        }
     829        ret = ldb_modify(kd->ldb, msg);
     830        if (ret == LDB_ERR_NO_SUCH_OBJECT) {
     831                i = 0;
     832                while (i < msg->num_elements) {
     833                        if (LDB_FLAG_MOD_TYPE(msg->elements[i].flags) == LDB_FLAG_MOD_DELETE) {
     834                                ldb_msg_remove_element(msg, &msg->elements[i]);
     835                        } else {
     836                                ++i;
     837                        }
     838                }
     839                ret = ldb_add(kd->ldb, msg);
     840        }
     841        if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
     842                /* ignore this -> the value didn't exist and also now doesn't */
     843                ret = LDB_SUCCESS;
     844        }
     845
     846        talloc_free(msg);
    720847
    721848        if (ret != LDB_SUCCESS) {
     
    744871{
    745872        struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
     873        uint32_t default_value_type = REG_NONE;
     874        DATA_BLOB default_value = { NULL, 0 };
     875        WERROR werr;
    746876
    747877        /* Initialization */
     
    761891                *max_valbufsize = 0;
    762892
     893        /* We need this to get the default value (if it exists) for counting
     894         * the values under the key and for finding out the longest value buffer
     895         * size. If no default value exists the DATA_BLOB "default_value" will
     896         * remain { NULL, 0 }. */
     897        werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
     898                                     &default_value);
     899        if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_BADFILE))) {
     900                return werr;
     901        }
     902
    763903        if (kd->subkeys == NULL) {
    764904                W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
    765905        }
    766 
    767906        if (kd->values == NULL) {
    768907                W_ERROR_NOT_OK_RETURN(cache_values(kd));
    769908        }
    770909
     910        if (classname != NULL) {
     911                *classname = kd->classname;
     912        }
     913
    771914        if (num_subkeys != NULL) {
    772915                *num_subkeys = kd->subkey_count;
     
    774917        if (num_values != NULL) {
    775918                *num_values = kd->value_count;
     919                /* also consider the default value if it exists */
     920                if (default_value.data != NULL) {
     921                        ++(*num_values);
     922                }
    776923        }
    777924
    778925
    779926        if (max_subkeynamelen != NULL) {
    780                 int i;
     927                unsigned int i;
    781928                struct ldb_message_element *el;
    782 
    783                 *max_subkeynamelen = 0;
    784929
    785930                for (i = 0; i < kd->subkey_count; i++) {
     
    790935
    791936        if (max_valnamelen != NULL || max_valbufsize != NULL) {
    792                 int i;
     937                unsigned int i;
    793938                struct ldb_message_element *el;
    794939                W_ERROR_NOT_OK_RETURN(cache_values(kd));
    795940
    796                 if (max_valbufsize != NULL)
    797                         *max_valbufsize = 0;
    798 
    799                 if (max_valnamelen != NULL)
    800                         *max_valnamelen = 0;
     941                /* also consider the default value if it exists */
     942                if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
     943                                *max_valbufsize = MAX(*max_valbufsize,
     944                                                      default_value.length);
     945                }
    801946
    802947                for (i = 0; i < kd->value_count; i++) {
     
    809954                                uint32_t data_type;
    810955                                DATA_BLOB data;
    811                                 reg_ldb_unpack_value(mem_ctx, 
    812                                                      kd->values[i], NULL, 
     956                                reg_ldb_unpack_value(mem_ctx,
     957                                                     kd->values[i], NULL,
    813958                                                     &data_type, &data);
    814959                                *max_valbufsize = MAX(*max_valbufsize, data.length);
     
    817962                }
    818963        }
     964
     965        talloc_free(default_value.data);
    819966
    820967        return WERR_OK;
  • vendor/current/source4/lib/registry/local.c

    r414 r740  
    5858
    5959        local_key = talloc(ctx, struct local_key);
    60         local_key->hive_key = talloc_steal(local_key, hive);
    61         local_key->global.context = talloc_reference(local_key, ctx);
    62         local_key->path = parent_path;
     60        if (local_key != NULL) {
     61                local_key->hive_key = talloc_reference(local_key, hive);
     62                local_key->global.context = talloc_reference(local_key, ctx);
     63                local_key->path = parent_path;
     64        }
    6365
    6466        return (struct registry_key *)local_key;
     
    7173                             struct registry_key **result)
    7274{
    73         char *orig = talloc_strdup(mem_ctx, path),
    74                  *curbegin = orig,
    75                  *curend = strchr(orig, '\\');
     75        char *orig, *curbegin, *curend;
    7676        struct local_key *local_parent = talloc_get_type(parent,
    7777                                                         struct local_key);
     
    8181        int el;
    8282
     83        if (path == NULL) {
     84                return WERR_INVALID_PARAM;
     85        }
     86
     87        orig = talloc_strdup(mem_ctx, path);
     88        W_ERROR_HAVE_NO_MEMORY(orig);
     89        curbegin = orig;
     90        curend = strchr(orig, '\\');
     91
    8392        if (local_parent->path.elements != NULL) {
    8493                elements = talloc_array(mem_ctx, const char *,
    8594                                        str_list_length(local_parent->path.elements) + 1);
     95                W_ERROR_HAVE_NO_MEMORY(elements);
    8696                for (el = 0; local_parent->path.elements[el] != NULL; el++) {
    8797                        elements[el] = talloc_reference(elements,
     
    98108                        *curend = '\0';
    99109                elements = talloc_realloc(mem_ctx, elements, const char *, el+2);
     110                W_ERROR_HAVE_NO_MEMORY(elements);
    100111                elements[el] = talloc_strdup(elements, curbegin);
     112                W_ERROR_HAVE_NO_MEMORY(elements[el]);
    101113                el++;
    102114                elements[el] = NULL;
     
    159171
    160172static WERROR local_create_key(TALLOC_CTX *mem_ctx,
    161                                struct registry_key *parent_key,
    162                                const char *name,
     173                               struct registry_key *parent,
     174                               const char *path,
    163175                               const char *key_class,
    164176                               struct security_descriptor *security,
    165                                struct registry_key **key)
    166 {
    167         struct local_key *local_parent;
    168         struct hive_key *hivekey;
    169         const char **elements;
    170         int i;
    171         const char *last_part;
    172 
    173         last_part = strrchr(name, '\\');
    174         if (last_part == NULL) {
    175                 last_part = name;
    176                 local_parent = (struct local_key *)parent_key;
     177                               struct registry_key **result)
     178{
     179        char *orig, *curbegin, *curend;
     180        struct local_key *local_parent = talloc_get_type(parent,
     181                                                         struct local_key);
     182        struct hive_key *curkey = local_parent->hive_key;
     183        WERROR error;
     184        const char **elements = NULL;
     185        int el;
     186
     187        if (path == NULL) {
     188                return WERR_INVALID_PARAM;
     189        }
     190
     191        orig = talloc_strdup(mem_ctx, path);
     192        W_ERROR_HAVE_NO_MEMORY(orig);
     193        curbegin = orig;
     194        curend = strchr(orig, '\\');
     195
     196        if (local_parent->path.elements != NULL) {
     197                elements = talloc_array(mem_ctx, const char *,
     198                                        str_list_length(local_parent->path.elements) + 1);
     199                W_ERROR_HAVE_NO_MEMORY(elements);
     200                for (el = 0; local_parent->path.elements[el] != NULL; el++) {
     201                        elements[el] = talloc_reference(elements,
     202                                                        local_parent->path.elements[el]);
     203                }
     204                elements[el] = NULL;
    177205        } else {
    178                 W_ERROR_NOT_OK_RETURN(reg_open_key(mem_ctx, parent_key,
    179                                                    talloc_strndup(mem_ctx, name, last_part-name),
    180                                                    (struct registry_key **)&local_parent));
    181                 last_part++;
    182         }
    183 
    184         W_ERROR_NOT_OK_RETURN(hive_key_add_name(mem_ctx, local_parent->hive_key,
    185                                                 last_part, key_class, security,
    186                                                 &hivekey));
    187 
    188         if (local_parent->path.elements != NULL) {
    189                 elements = talloc_array(hivekey, const char *,
    190                                         str_list_length(local_parent->path.elements)+2);
    191                 for (i = 0; local_parent->path.elements[i] != NULL; i++) {
    192                         elements[i] = talloc_reference(elements,
    193                                                        local_parent->path.elements[i]);
    194                 }
    195         } else {
    196                 elements = talloc_array(hivekey, const char *, 2);
    197                 i = 0;
    198         }
    199 
    200         elements[i] = talloc_strdup(elements, name);
    201         elements[i+1] = NULL;
    202 
    203         *key = reg_import_hive_key(local_parent->global.context, hivekey,
    204                                    local_parent->path.predefined_key,
    205                                    elements);
     206                elements = NULL;
     207                el = 0;
     208        }
     209
     210        while (curbegin != NULL && *curbegin) {
     211                if (curend != NULL)
     212                        *curend = '\0';
     213                elements = talloc_realloc(mem_ctx, elements, const char *, el+2);
     214                W_ERROR_HAVE_NO_MEMORY(elements);
     215                elements[el] = talloc_strdup(elements, curbegin);
     216                W_ERROR_HAVE_NO_MEMORY(elements[el]);
     217                el++;
     218                elements[el] = NULL;
     219                error = hive_get_key_by_name(mem_ctx, curkey,
     220                                             curbegin, &curkey);
     221                if (W_ERROR_EQUAL(error, WERR_BADFILE)) {
     222                        error = hive_key_add_name(mem_ctx, curkey, curbegin,
     223                                                  key_class, security,
     224                                                  &curkey);
     225                }
     226                if (!W_ERROR_IS_OK(error)) {
     227                        DEBUG(2, ("Open/Creation of key %s failed: %s\n",
     228                                curbegin, win_errstr(error)));
     229                        talloc_free(orig);
     230                        return error;
     231                }
     232                if (curend == NULL)
     233                        break;
     234                curbegin = curend + 1;
     235                curend = strchr(curbegin, '\\');
     236        }
     237        talloc_free(orig);
     238
     239        *result = reg_import_hive_key(local_parent->global.context, curkey,
     240                                      local_parent->path.predefined_key,
     241                                      talloc_steal(curkey, elements));
    206242
    207243        return WERR_OK;
     
    212248{
    213249        struct local_key *local = (struct local_key *)key;
     250
     251        if (name == NULL) {
     252                return WERR_INVALID_PARAM;
     253        }
    214254
    215255        return hive_key_set_value(local->hive_key, name, type, data);
     
    221261{
    222262        const struct local_key *local = (const struct local_key *)key;
     263
     264        if (name == NULL) {
     265                return WERR_INVALID_PARAM;
     266        }
    223267
    224268        return hive_get_value(mem_ctx, local->hive_key, name, type, data);
     
    237281}
    238282
    239 static WERROR local_delete_key(struct registry_key *key, const char *name)
    240 {
    241         const struct local_key *local = (const struct local_key *)key;
    242 
    243         return hive_key_del(local->hive_key, name);
    244 }
    245 
    246 static WERROR local_delete_value(struct registry_key *key, const char *name)
    247 {
    248         const struct local_key *local = (const struct local_key *)key;
    249 
    250         return hive_key_del_value(local->hive_key, name);
     283static WERROR local_delete_key(TALLOC_CTX *mem_ctx, struct registry_key *key,
     284                               const char *name)
     285{
     286        const struct local_key *local = (const struct local_key *)key;
     287
     288        if (name == NULL) {
     289                return WERR_INVALID_PARAM;
     290        }
     291
     292        return hive_key_del(mem_ctx, local->hive_key, name);
     293}
     294
     295static WERROR local_delete_value(TALLOC_CTX *mem_ctx, struct registry_key *key,
     296                                 const char *name)
     297{
     298        const struct local_key *local = (const struct local_key *)key;
     299
     300        if (name == NULL) {
     301                return WERR_INVALID_PARAM;
     302        }
     303
     304        return hive_key_del_value(mem_ctx, local->hive_key, name);
    251305}
    252306
     
    328382        struct registry_local *reg_local = talloc_get_type(rctx,
    329383                                                           struct registry_local);
    330         struct mountpoint *mp = talloc(rctx, struct mountpoint);
    331         int i = 0;
    332 
     384        struct mountpoint *mp;
     385        unsigned int i = 0;
     386
     387        mp = talloc(rctx, struct mountpoint);
     388        W_ERROR_HAVE_NO_MEMORY(mp);
    333389        mp->path.predefined_key = key_id;
    334390        mp->prev = mp->next = NULL;
     
    337393                mp->path.elements = talloc_array(mp, const char *,
    338394                                                 str_list_length(elements));
     395                W_ERROR_HAVE_NO_MEMORY(mp->path.elements);
    339396                for (i = 0; elements[i] != NULL; i++) {
    340397                        mp->path.elements[i] = talloc_reference(mp->path.elements,
  • vendor/current/source4/lib/registry/man/regdiff.1.xml

    r414 r740  
    11<?xml version="1.0" encoding="iso-8859-1"?>
    2 <!DOCTYPE refentry PUBLIC "-//Samba-Team//DTD DocBook V4.2-Based Variant V1.0//EN" "http://www.samba.org/samba/DTD/samba-doc">
     2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
    33<refentry id="regdiff.1">
    44
  • vendor/current/source4/lib/registry/man/regpatch.1.xml

    r414 r740  
    11<?xml version="1.0" encoding="iso-8859-1"?>
    2 <!DOCTYPE refentry PUBLIC "-//Samba-Team//DTD DocBook V4.2-Based Variant V1.0//EN" "http://www.samba.org/samba/DTD/samba-doc">
     2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
    33<refentry id="regpatch.1">
    44
  • vendor/current/source4/lib/registry/man/regshell.1.xml

    r414 r740  
    11<?xml version="1.0" encoding="iso-8859-1"?>
    2 <!DOCTYPE refentry PUBLIC "-//Samba-Team//DTD DocBook V4.2-Based Variant V1.0//EN" "http://www.samba.org/samba/DTD/samba-doc">
     2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
    33<refentry id="regshell.1">
    44
  • vendor/current/source4/lib/registry/man/regtree.1.xml

    r414 r740  
    11<?xml version="1.0" encoding="iso-8859-1"?>
    2 <!DOCTYPE refentry PUBLIC "-//Samba-Team//DTD DocBook V4.2-Based Variant V1.0//EN" "http://www.samba.org/samba/DTD/samba-doc">
     2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
    33<refentry id="regtree.1">
    44
  • vendor/current/source4/lib/registry/patchfile.c

    r414 r740  
    55   Copyright (C) Jelmer Vernooij 2004-2007
    66   Copyright (C) Wilco Baan Hofman 2006
    7    Copyright (C) Matthias Dieter Wallnöfer 2008
     7   Copyright (C) Matthias Dieter Wallnöfer 2008-2010
    88
    99   This program is free software; you can redistribute it and/or modify
     
    2727
    2828_PUBLIC_ WERROR reg_preg_diff_load(int fd,
    29                                    struct smb_iconv_convenience *iconv_convenience,
    3029                                   const struct reg_diff_callbacks *callbacks,
    3130                                   void *callback_data);
    3231
    3332_PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
    34                                      struct smb_iconv_convenience *iconv_convenience,
    3533                                     const struct reg_diff_callbacks *callbacks,
    3634                                     void *callback_data);
     
    4543                             void *callback_data)
    4644{
    47         int i;
     45        unsigned int i;
    4846        struct registry_key *t1 = NULL, *t2 = NULL;
    4947        char *tmppath;
     
    8785
    8886                if (!W_ERROR_IS_OK(error2) && !W_ERROR_EQUAL(error2, WERR_BADFILE)) {
    89                         DEBUG(0, ("Error occured while getting subkey by name: %s\n",
     87                        DEBUG(0, ("Error occurred while getting subkey by name: %s\n",
    9088                                win_errstr(error2)));
    9189                        talloc_free(mem_ctx);
     
    9694                /* didn't have such a subkey and therefore add a del diff */
    9795                tmppath = talloc_asprintf(mem_ctx, "%s\\%s", path, keyname1);
     96                if (tmppath == NULL) {
     97                        DEBUG(0, ("Out of memory\n"));
     98                        talloc_free(mem_ctx);
     99                        return WERR_NOMEM;
     100                }
    98101                if (!W_ERROR_IS_OK(error2))
    99102                        callbacks->del_key(callback_data, tmppath);
     
    102105                error1 = reg_open_key(mem_ctx, oldkey, keyname1, &t1);
    103106                if (!W_ERROR_IS_OK(error1)) {
    104                         DEBUG(0, ("Error occured while getting subkey by name: %s\n",
     107                        DEBUG(0, ("Error occurred while getting subkey by name: %s\n",
    105108                        win_errstr(error1)));
    106109                        talloc_free(mem_ctx);
     
    155158                }
    156159
    157                 /* oldkey didn't have such a subkey, add add diff */
     160                /* oldkey didn't have such a subkey, add a add diff */
    158161                tmppath = talloc_asprintf(mem_ctx, "%s\\%s", path, keyname1);
     162                if (tmppath == NULL) {
     163                        DEBUG(0, ("Out of memory\n"));
     164                        talloc_free(mem_ctx);
     165                        return WERR_NOMEM;
     166                }
    159167                callbacks->add_key(callback_data, tmppath);
    160168
     
    162170                error1 = reg_open_key(mem_ctx, newkey, keyname1, &t2);
    163171                if (!W_ERROR_IS_OK(error1)) {
    164                         DEBUG(0, ("Error occured while getting subkey by name: %s\n",
     172                        DEBUG(0, ("Error occurred while getting subkey by name: %s\n",
    165173                        win_errstr(error1)));
    166174                        talloc_free(mem_ctx);
     
    176184                const char *name;
    177185                uint32_t type1, type2;
    178                 DATA_BLOB contents1, contents2;
     186                DATA_BLOB contents1 = { NULL, 0 }, contents2 = { NULL, 0 };
    179187
    180188                error1 = reg_key_get_value_by_index(mem_ctx, newkey, i,
     
    203211
    204212                if (W_ERROR_IS_OK(error2)
    205                         && (data_blob_cmp(&contents1, &contents2) == 0)
    206                         && (type1 == type2))
     213                    && (data_blob_cmp(&contents1, &contents2) == 0)
     214                    && (type1 == type2)) {
     215                        talloc_free(discard_const_p(char, name));
     216                        talloc_free(contents1.data);
     217                        talloc_free(contents2.data);
    207218                        continue;
     219                }
    208220
    209221                callbacks->set_value(callback_data, path, name,
    210222                                     type1, contents1);
     223
     224                talloc_free(discard_const_p(char, name));
     225                talloc_free(contents1.data);
     226                talloc_free(contents2.data);
    211227        }
    212228
     
    215231                const char *name;
    216232                uint32_t type;
    217                 DATA_BLOB contents;
     233                DATA_BLOB contents = { NULL, 0 };
    218234
    219235                error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &name,
     
    232248                        error2 = WERR_BADFILE;
    233249
    234                 if (W_ERROR_IS_OK(error2))
     250                if (W_ERROR_IS_OK(error2)) {
     251                        talloc_free(discard_const_p(char, name));
     252                        talloc_free(contents.data);
    235253                        continue;
     254                }
    236255
    237256                if (!W_ERROR_EQUAL(error2, WERR_BADFILE)) {
     
    243262
    244263                callbacks->del_value(callback_data, path, name);
     264
     265                talloc_free(discard_const_p(char, name));
     266                talloc_free(contents.data);
    245267        }
    246268
     
    257279                                  void *callback_data)
    258280{
    259         int i;
     281        unsigned int i;
    260282        WERROR error;
    261283
     
    281303                }
    282304
     305                /* if "r1" is NULL (old hive) and "r2" isn't (new hive) then
     306                 * the hive doesn't exist yet and we have to generate an add
     307                 * diff */
     308                if ((r1 == NULL) && (r2 != NULL)) {
     309                        callbacks->add_key(callback_data,
     310                                           reg_predefined_keys[i].name);
     311                }
     312                /* if "r1" isn't NULL (old hive) and "r2" is (new hive) then
     313                 * the hive shouldn't exist anymore and we have to generate a
     314                 * del diff */
     315                if ((r1 != NULL) && (r2 == NULL)) {
     316                        callbacks->del_key(callback_data,
     317                                           reg_predefined_keys[i].name);
     318                }
     319
    283320                error = reg_generate_diff_key(r1, r2,
    284321                        reg_predefined_keys[i].name, callbacks,
     
    300337 */
    301338_PUBLIC_ WERROR reg_diff_load(const char *filename,
    302                               struct smb_iconv_convenience *iconv_convenience,
    303339                              const struct reg_diff_callbacks *callbacks,
    304340                              void *callback_data)
     
    334370        if (strncmp(hdr, "PReg", 4) == 0) {
    335371                /* Must be a GPO Registry.pol file */
    336                 return reg_preg_diff_load(fd, iconv_convenience, callbacks, callback_data);
     372                return reg_preg_diff_load(fd, callbacks, callback_data);
    337373        } else {
    338374                /* Must be a normal .REG file */
    339                 return reg_dotreg_diff_load(fd, iconv_convenience, callbacks, callback_data);
     375                return reg_dotreg_diff_load(fd, callbacks, callback_data);
    340376        }
    341377}
     
    353389        /* Recursively create the path */
    354390        buf = talloc_strdup(ctx, key_name);
     391        W_ERROR_HAVE_NO_MEMORY(buf);
    355392        buf_ptr = buf;
    356393
     
    367404                        }
    368405                        *buf_ptr++ = '\\';
    369                 }
    370         }
     406                        talloc_free(tmp);
     407                }
     408        }
     409
     410        talloc_free(buf);
    371411
    372412        /* Add the key */
     
    379419                return error;
    380420        }
     421        talloc_free(tmp);
     422
    381423        return WERR_OK;
    382424}
     
    388430        /* We can't proof here for success, because a common superkey could */
    389431        /* have been deleted before the subkey's (diff order). This removed */
    390         /* therefore all childs recursively and the "WERR_BADFILE" result is */
     432        /* therefore all children recursively and the "WERR_BADFILE" result is */
    391433        /* expected. */
    392434
     
    420462        }
    421463
     464        talloc_free(tmp);
     465
    422466        return WERR_OK;
    423467}
     
    438482        }
    439483
    440         error = reg_del_value(tmp, value_name);
     484        error = reg_del_value(ctx, tmp, value_name);
    441485        if (!W_ERROR_IS_OK(error)) {
    442486                DEBUG(0, ("Error deleting value '%s'\n", value_name));
     
    444488        }
    445489
     490        talloc_free(tmp);
    446491
    447492        return WERR_OK;
     
    453498        struct registry_key *key;
    454499        WERROR error;
    455         const char* value_name;
     500        const char *value_name;
    456501
    457502        error = reg_open_key_abs(ctx, ctx, key_name, &key);
     
    467512        while (W_ERROR_IS_OK(reg_key_get_value_by_index(
    468513                        ctx, key, 0, &value_name, NULL, NULL))) {
    469                 error = reg_del_value(key, value_name);
     514                error = reg_del_value(ctx, key, value_name);
    470515                if (!W_ERROR_IS_OK(error)) {
    471516                        DEBUG(0, ("Error deleting value '%s'\n", value_name));
    472517                        return error;
    473518                }
    474         }
     519                talloc_free(discard_const_p(char, value_name));
     520        }
     521
     522        talloc_free(key);
    475523
    476524        return WERR_OK;
     
    481529 */
    482530_PUBLIC_ WERROR reg_diff_apply(struct registry_context *ctx,
    483                                                            struct smb_iconv_convenience *iconv_convenience,
    484531                                                           const char *filename)
    485532{
     
    493540        callbacks.done = NULL;
    494541
    495         return reg_diff_load(filename, iconv_convenience,
    496                              &callbacks, ctx);
    497 }
     542        return reg_diff_load(filename, &callbacks, ctx);
     543}
  • vendor/current/source4/lib/registry/patchfile_dotreg.c

    r414 r740  
    44
    55   Copyright (C) Jelmer Vernooij 2004-2007
    6    Copyright (C) Wilco Baan Hofman 2006-2008
     6   Copyright (C) Wilco Baan Hofman 2006-2010
    77
    88   This program is free software; you can redistribute it and/or modify
     
    2121*/
    2222
    23 /* FIXME Newer .REG files, created by Windows XP and above use unicode UCS-2 */
     23/* FIXME:
     24 * - Newer .REG files, created by Windows XP and above use unicode UCS-2
     25 * - @="" constructions should write value with empty name.
     26*/
    2427
    2528#include "includes.h"
     
    3639struct dotreg_data {
    3740        int fd;
    38         struct smb_iconv_convenience *iconv_convenience;
    3941};
     42
     43/*
     44 * This is basically a copy of data_blob_hex_string_upper, but with comma's
     45 * between the bytes in hex.
     46 */
     47static char *dotreg_data_blob_hex_string(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
     48{
     49        size_t i;
     50        char *hex_string;
     51
     52        hex_string = talloc_array(mem_ctx, char, (blob->length*3)+1);
     53        if (!hex_string) {
     54                return NULL;
     55        }
     56
     57        for (i = 0; i < blob->length; i++)
     58                slprintf(&hex_string[i*3], 4, "%02X,", blob->data[i]);
     59
     60        /* Remove last comma and NULL-terminate the string */
     61        hex_string[(blob->length*3)-1] = '\0';
     62        return hex_string;
     63}
     64
     65/*
     66 * This is basically a copy of reg_val_data_string, except that this function
     67 * has no 0x for dwords, everything else is regarded as binary, and binary
     68 * strings are represented with bytes comma-separated.
     69 */
     70static char *reg_val_dotreg_string(TALLOC_CTX *mem_ctx, uint32_t type,
     71                                   const DATA_BLOB data)
     72{
     73        char *ret = NULL;
     74
     75        if (data.length == 0)
     76                return talloc_strdup(mem_ctx, "");
     77
     78        switch (type) {
     79                case REG_EXPAND_SZ:
     80                case REG_SZ:
     81                        convert_string_talloc(mem_ctx,
     82                                                          CH_UTF16, CH_UNIX, data.data, data.length,
     83                                                          (void **)&ret, NULL, false);
     84                        break;
     85                case REG_DWORD:
     86                case REG_DWORD_BIG_ENDIAN:
     87                        SMB_ASSERT(data.length == sizeof(uint32_t));
     88                        ret = talloc_asprintf(mem_ctx, "%08x",
     89                                              IVAL(data.data, 0));
     90                        break;
     91                default: /* default means treat as binary */
     92                case REG_BINARY:
     93                        ret = dotreg_data_blob_hex_string(mem_ctx, &data);
     94                        break;
     95        }
     96
     97        return ret;
     98}
    4099
    41100static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
     
    62121{
    63122        struct dotreg_data *data = (struct dotreg_data *)_data;
    64 
    65         fdprintf(data->fd, "\"%s\"=%s:%s\n",
    66                         value_name, str_regtype(value_type),
    67                         reg_val_data_string(NULL, data->iconv_convenience, value_type, value));
     123        char *data_string = reg_val_dotreg_string(NULL,
     124                                                value_type, value);
     125        char *data_incl_type;
     126
     127        W_ERROR_HAVE_NO_MEMORY(data_string);
     128
     129        switch (value_type) {
     130                case REG_SZ:
     131                        data_incl_type = talloc_asprintf(data_string, "\"%s\"",
     132                                        data_string);
     133                        break;
     134                case REG_DWORD:
     135                        data_incl_type = talloc_asprintf(data_string,
     136                                        "dword:%s", data_string);
     137                        break;
     138                case REG_BINARY:
     139                        data_incl_type = talloc_asprintf(data_string, "hex:%s",
     140                                        data_string);
     141                        break;
     142                default:
     143                        data_incl_type = talloc_asprintf(data_string, "hex(%x):%s",
     144                                        value_type, data_string);
     145                        break;
     146        }
     147
     148        if (value_name[0] == '\0') {
     149                fdprintf(data->fd, "@=%s\n", data_incl_type);
     150        } else {
     151                fdprintf(data->fd, "\"%s\"=%s\n",
     152                         value_name, data_incl_type);
     153        }
     154
     155        talloc_free(data_string);
    68156
    69157        return WERR_OK;
     
    100188 */
    101189_PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
    102                                      struct smb_iconv_convenience *iconv_convenience,
    103190                                     struct reg_diff_callbacks **callbacks,
    104191                                     void **callback_data)
     
    108195        data = talloc_zero(ctx, struct dotreg_data);
    109196        *callback_data = data;
    110 
    111         data->iconv_convenience = iconv_convenience;
    112197
    113198        if (filename) {
     
    139224 */
    140225_PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
    141                                      struct smb_iconv_convenience *iconv_convenience,
    142226                                     const struct reg_diff_callbacks *callbacks,
    143227                                     void *callback_data)
     
    148232        WERROR error;
    149233        uint32_t value_type;
    150         DATA_BLOB value;
     234        DATA_BLOB data;
     235        bool result;
     236        char *type_str = NULL;
     237        char *data_str;
     238        char *value;
     239        bool continue_next_line = 0;
    151240
    152241        line = afdgets(fd, mem_ctx, 0);
     
    159248
    160249        while ((line = afdgets(fd, mem_ctx, 0))) {
     250                /* Remove '\r' if it's a Windows text file */
     251                if (line[strlen(line)-1] == '\r') {
     252                        line[strlen(line)-1] = '\0';
     253                }
     254
    161255                /* Ignore comments and empty lines */
    162256                if (strlen(line) == 0 || line[0] == ';') {
     
    172266                /* Start of key */
    173267                if (line[0] == '[') {
    174                         p = strchr_m(line, ']');
    175                         if (p[strlen(p)-1] != ']') {
    176                                 DEBUG(0, ("Missing ']'\n"));
    177                                 return WERR_GENERAL_FAILURE;
    178                         }
     268                        if (line[strlen(line)-1] != ']') {
     269                                DEBUG(0, ("Missing ']' on line: %s\n", line));
     270                                talloc_free(line);
     271                                continue;
     272                        }
     273
    179274                        /* Deleting key */
    180275                        if (line[1] == '-') {
    181276                                curkey = talloc_strndup(line, line+2, strlen(line)-3);
     277                                W_ERROR_HAVE_NO_MEMORY(curkey);
    182278
    183279                                error = callbacks->del_key(callback_data,
    184280                                                           curkey);
     281
    185282                                if (!W_ERROR_IS_OK(error)) {
    186283                                        DEBUG(0,("Error deleting key %s\n",
     
    195292                        }
    196293                        curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
     294                        W_ERROR_HAVE_NO_MEMORY(curkey);
    197295
    198296                        error = callbacks->add_key(callback_data, curkey);
     
    208306
    209307                /* Deleting/Changing value */
    210                 p = strchr_m(line, '=');
    211                 if (p == NULL) {
    212                         DEBUG(0, ("Malformed line\n"));
    213                         talloc_free(line);
    214                         continue;
    215                 }
    216 
    217                 *p = '\0'; p++;
    218 
    219                 if (curkey == NULL) {
    220                         DEBUG(0, ("Value change without key\n"));
    221                         talloc_free(line);
    222                         continue;
    223                 }
    224 
    225                 /* Delete value */
    226                 if (strcmp(p, "-") == 0) {
    227                         error = callbacks->del_value(callback_data,
    228                                                      curkey, line);
    229                         if (!W_ERROR_IS_OK(error)) {
    230                                 DEBUG(0, ("Error deleting value %s in key %s\n",
    231                                         line, curkey));
    232                                 talloc_free(mem_ctx);
    233                                 return error;
    234                         }
    235 
    236                         talloc_free(line);
    237                         continue;
    238                 }
    239 
    240                 q = strchr_m(p, ':');
    241                 if (q) {
    242                         *q = '\0';
    243                         q++;
    244                 }
    245 
    246                 reg_string_to_val(line, iconv_convenience,
    247                                   q?p:"REG_SZ", q?q:p,
    248                                   &value_type, &value);
    249 
    250                 error = callbacks->set_value(callback_data, curkey, line,
    251                                              value_type, value);
     308                if (continue_next_line) {
     309                        continue_next_line = 0;
     310
     311                        /* Continued data start with two whitespaces */
     312                        if (line[0] != ' ' || line[1] != ' ') {
     313                                DEBUG(0, ("Malformed line: %s\n", line));
     314                                talloc_free(line);
     315                                continue;
     316                        }
     317                        p = line + 2;
     318
     319                        /* Continue again if line ends with a backslash */
     320                        if (line[strlen(line)-1] == '\\') {
     321                                line[strlen(line)-1] = '\0';
     322                                continue_next_line = 1;
     323                                data_str = talloc_strdup_append(data_str, p);
     324                                talloc_free(line);
     325                                continue;
     326                        }
     327                        data_str = talloc_strdup_append(data_str, p);
     328                } else {
     329                        p = strchr_m(line, '=');
     330                        if (p == NULL) {
     331                                DEBUG(0, ("Malformed line: %s\n", line));
     332                                talloc_free(line);
     333                                continue;
     334                        }
     335
     336                        *p = '\0'; p++;
     337
     338
     339                        if (curkey == NULL) {
     340                                DEBUG(0, ("Value change without key\n"));
     341                                talloc_free(line);
     342                                continue;
     343                        }
     344
     345                        /* Values should be double-quoted */
     346                        if (line[0] != '"') {
     347                                DEBUG(0, ("Malformed line\n"));
     348                                talloc_free(line);
     349                                continue;
     350                        }
     351
     352                        /* Chop of the quotes and store as value */
     353                        value = talloc_strndup(mem_ctx, line+1,strlen(line)-2);
     354
     355                        /* Delete value */
     356                        if (p[0] == '-') {
     357                                error = callbacks->del_value(callback_data,
     358                                                     curkey, value);
     359
     360                                /* Ignore if key does not exist (WERR_BADFILE)
     361                                 * Consistent with Windows behaviour */
     362                                if (!W_ERROR_IS_OK(error) &&
     363                                    !W_ERROR_EQUAL(error, WERR_BADFILE)) {
     364                                        DEBUG(0, ("Error deleting value %s in key %s\n",
     365                                                value, curkey));
     366                                        talloc_free(mem_ctx);
     367                                        return error;
     368                                }
     369
     370                                talloc_free(line);
     371                                talloc_free(value);
     372                                continue;
     373                        }
     374
     375                        /* Do not look for colons in strings */
     376                        if (p[0] == '"') {
     377                                q = NULL;
     378                                data_str = talloc_strndup(mem_ctx, p+1,strlen(p)-2);
     379                        } else {
     380                                /* Split the value type from the data */
     381                                q = strchr_m(p, ':');
     382                                if (q) {
     383                                        *q = '\0';
     384                                        q++;
     385                                        type_str = talloc_strdup(mem_ctx, p);
     386                                        data_str = talloc_strdup(mem_ctx, q);
     387                                } else {
     388                                        data_str = talloc_strdup(mem_ctx, p);
     389                                }
     390                        }
     391
     392                        /* Backslash before the CRLF means continue on next line */
     393                        if (data_str[strlen(data_str)-1] == '\\') {
     394                                data_str[strlen(data_str)-1] = '\0';
     395                                talloc_free(line);
     396                                continue_next_line = 1;
     397                                continue;
     398                        }
     399                }
     400                DEBUG(9, ("About to write %s with type %s, length %ld: %s\n", value, type_str, (long) strlen(data_str), data_str));
     401                result = reg_string_to_val(value,
     402                                  type_str?type_str:"REG_SZ", data_str,
     403                                  &value_type, &data);
     404                if (!result) {
     405                        DEBUG(0, ("Error converting string to value for line:\n%s\n",
     406                                        line));
     407                        return WERR_GENERAL_FAILURE;
     408                }
     409
     410                error = callbacks->set_value(callback_data, curkey, value,
     411                                             value_type, data);
    252412                if (!W_ERROR_IS_OK(error)) {
    253413                        DEBUG(0, ("Error setting value for %s in %s\n",
    254                                 line, curkey));
     414                                value, curkey));
    255415                        talloc_free(mem_ctx);
    256416                        return error;
    257417                }
    258418
     419                /* Clean up buffers */
     420                if (type_str != NULL) {
     421                        talloc_free(type_str);
     422                        type_str = NULL;
     423                }
     424                talloc_free(data_str);
     425                talloc_free(value);
    259426                talloc_free(line);
    260427        }
     
    262429        close(fd);
    263430
    264         return WERR_OK;
    265 }
     431        talloc_free(mem_ctx);
     432
     433        return WERR_OK;
     434}
  • vendor/current/source4/lib/registry/patchfile_preg.c

    r414 r740  
    3434        uint16_t v;
    3535
    36         if (read(fd, &v, 2) < 2) {
     36        if (read(fd, &v, sizeof(uint16_t)) < sizeof(uint16_t)) {
    3737                return WERR_GENERAL_FAILURE;
    3838        }
     
    4242static WERROR preg_write_utf16(int fd, const char *string)
    4343{
    44         codepoint_t v;
    45         uint16_t i;
    46         size_t size;
     44        uint16_t v;
     45        size_t i, size;
    4746
    4847        for (i = 0; i < strlen(string); i+=size) {
    4948                v = next_codepoint(&string[i], &size);
    50                 if (write(fd, &v, 2) < 2) {
     49                if (write(fd, &v, sizeof(uint16_t)) < sizeof(uint16_t)) {
    5150                        return WERR_GENERAL_FAILURE;
    5251                }
     
    8988        char *parent_name;
    9089        DATA_BLOB blob;
    91 
    92         parent_name = talloc_strndup(data->ctx, key_name, strrchr(key_name, '\\')-key_name);
    93         blob.data = (uint8_t *)talloc_strndup(data->ctx, key_name+(strrchr(key_name, '\\')-key_name)+1,
    94                         strlen(key_name)-(strrchr(key_name, '\\')-key_name));
     90        WERROR werr;
     91
     92        parent_name = talloc_strndup(data->ctx, key_name,
     93                                     strrchr(key_name, '\\')-key_name);
     94        W_ERROR_HAVE_NO_MEMORY(parent_name);
     95        blob.data = (uint8_t*)talloc_strndup(data->ctx,
     96                                             key_name+(strrchr(key_name, '\\')-key_name)+1,
     97                                             strlen(key_name)-(strrchr(key_name, '\\')-key_name));
     98        W_ERROR_HAVE_NO_MEMORY(blob.data);
    9599        blob.length = strlen((char *)blob.data)+1;
    96100       
    97101
    98102        /* FIXME: These values should be accumulated to be written at done(). */
    99         return reg_preg_diff_set_value(data, parent_name, "**DeleteKeys", REG_SZ, blob);
     103        werr = reg_preg_diff_set_value(data, parent_name, "**DeleteKeys",
     104                                       REG_SZ, blob);
     105
     106        talloc_free(parent_name);
     107        talloc_free(blob.data);
     108
     109        return werr;
    100110}
    101111
     
    106116        char *val;
    107117        DATA_BLOB blob;
     118        WERROR werr;
    108119
    109120        val = talloc_asprintf(data->ctx, "**Del.%s", value_name);
    110 
     121        W_ERROR_HAVE_NO_MEMORY(val);
    111122        blob.data = (uint8_t *)talloc(data->ctx, uint32_t);
    112         *(uint32_t *)blob.data = 0;
    113         blob.length = 4;
    114         return reg_preg_diff_set_value(data, key_name, val, REG_DWORD, blob);
     123        W_ERROR_HAVE_NO_MEMORY(blob.data);
     124        SIVAL(blob.data, 0, 0);
     125        blob.length = sizeof(uint32_t);
     126
     127        werr = reg_preg_diff_set_value(data, key_name, val, REG_DWORD, blob);
     128
     129        talloc_free(val);
     130        talloc_free(blob.data);
     131
     132        return werr;
    115133}
    116134
     
    119137        struct preg_data *data = (struct preg_data *)_data;
    120138        DATA_BLOB blob;
     139        WERROR werr;
    121140
    122141        blob.data = (uint8_t *)talloc(data->ctx, uint32_t);
    123         *(uint32_t *)blob.data = 0;     
    124         blob.length = 4;
    125 
    126         return reg_preg_diff_set_value(data, key_name, "**DelVals.", REG_DWORD, blob);
     142        W_ERROR_HAVE_NO_MEMORY(blob.data);
     143        SIVAL(blob.data, 0, 0);
     144        blob.length = sizeof(uint32_t);
     145
     146        werr = reg_preg_diff_set_value(data, key_name, "**DelVals.", REG_DWORD,
     147                                       blob);
     148
     149        talloc_free(blob.data);
     150
     151        return werr;
    127152}
    128153
     
    140165 */
    141166_PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
    142                                    struct smb_iconv_convenience *ic,
    143167                                   struct reg_diff_callbacks **callbacks,
    144168                                   void **callback_data)
     
    165189
    166190        strncpy(preg_header.hdr, "PReg", 4);
    167         SIVAL(&preg_header, 4, 1);
    168         write(data->fd, (uint8_t *)&preg_header,8);
     191        SIVAL(&preg_header.version, 0, 1);
     192        write(data->fd, (uint8_t *)&preg_header, sizeof(preg_header));
    169193
    170194        data->ctx = ctx;
     
    185209 */
    186210_PUBLIC_ WERROR reg_preg_diff_load(int fd,
    187                                    struct smb_iconv_convenience *iconv_convenience,
    188211                                   const struct reg_diff_callbacks *callbacks,
    189212                                   void *callback_data)
     
    206229
    207230        /* Read first 8 bytes (the header) */
    208         if (read(fd, &preg_header, 8) != 8) {
     231        if (read(fd, &preg_header, sizeof(preg_header)) != sizeof(preg_header)) {
    209232                DEBUG(0, ("Could not read PReg file: %s\n",
    210233                                strerror(errno)));
     
    255278
    256279                /* Get the type */
    257                 if (read(fd, &value_type, 4) < 4) {
     280                if (read(fd, &value_type, sizeof(uint32_t)) < sizeof(uint32_t)) {
    258281                        DEBUG(0, ("Error while reading PReg\n"));
    259282                        ret = WERR_GENERAL_FAILURE;
     
    270293                        goto cleanup;
    271294                }
     295
    272296                /* Get data length */
    273                 if (read(fd, &length, 4) < 4) {
     297                if (read(fd, &length, sizeof(uint32_t)) < sizeof(uint32_t)) {
    274298                        DEBUG(0, ("Error while reading PReg\n"));
    275299                        ret = WERR_GENERAL_FAILURE;
    276300                        goto cleanup;
    277301                }
     302                length = IVAL(&length, 0);
     303
    278304                /* Read past delimiter */
    279305                buf_ptr = buf;
     
    284310                        goto cleanup;
    285311                }
     312
    286313                /* Get the data */
    287314                buf_ptr = buf;
  • vendor/current/source4/lib/registry/pyregistry.c

    r414 r740  
    33   Samba utility functions
    44   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
     5   Copyright (C) Wilco Baan Hofman <wilco@baanhofman.nl> 2010
    56   
    67   This program is free software; you can redistribute it and/or modify
     
    1819*/
    1920
     21#include <Python.h>
    2022#include "includes.h"
    21 #include <tevent.h>
    22 #include <Python.h>
    2323#include "libcli/util/pyerrors.h"
    2424#include "lib/registry/registry.h"
    25 #include "scripting/python/modules.h" /* for py_iconv_convenience() */
    26 #include <pytalloc.h>
     25#include "lib/talloc/pytalloc.h"
     26#include "lib/events/events.h"
    2727#include "auth/credentials/pycredentials.h"
    2828#include "param/pyparam.h"
    2929
    30 #ifndef Py_RETURN_NONE
    31 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
    32 #endif
    33 
    34 PyAPI_DATA(PyTypeObject) PyRegistryKey;
    35 PyAPI_DATA(PyTypeObject) PyRegistry;
    36 PyAPI_DATA(PyTypeObject) PyHiveKey;
     30extern PyTypeObject PyRegistryKey;
     31extern PyTypeObject PyRegistry;
     32extern PyTypeObject PyHiveKey;
    3733
    3834/*#define PyRegistryKey_AsRegistryKey(obj) py_talloc_get_type(obj, struct registry_key)*/
     
    9692                return NULL;
    9793
    98         result = reg_diff_apply(ctx, py_iconv_convenience(NULL), filename);
     94        result = reg_diff_apply(ctx, filename);
    9995        PyErr_WERROR_IS_ERR_RAISE(result);
    10096
     
    164160        .tp_new = registry_new,
    165161        .tp_basicsize = sizeof(py_talloc_Object),
    166         .tp_dealloc = py_talloc_dealloc,
    167162        .tp_flags = Py_TPFLAGS_DEFAULT,
    168163};
     
    177172                return NULL;
    178173
    179         result = hive_key_del(key, name);
     174        result = hive_key_del(NULL, key, name);
    180175
    181176        PyErr_WERROR_IS_ERR_RAISE(result);
     
    204199                return NULL;
    205200
    206         result = hive_key_del_value(key, name);
     201        result = hive_key_del_value(NULL, key, name);
    207202
    208203        PyErr_WERROR_IS_ERR_RAISE(result);
     
    225220                result = hive_key_set_value(key, name, type, value);
    226221        else
    227                 result = hive_key_del_value(key, name);
     222                result = hive_key_del_value(NULL, key, name);
    228223
    229224        PyErr_WERROR_IS_ERR_RAISE(result);
     
    244239};
    245240
    246 static PyObject *hive_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
    247 {
    248         /* reg_open_hive */
     241static PyObject *hive_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
    249242        Py_RETURN_NONE;
     243}
     244
     245static PyObject *py_open_hive(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     246{
     247        const char *kwnames[] = { "location", "lp_ctx", "session_info", "credentials", NULL };
     248        WERROR result;
     249        struct loadparm_context *lp_ctx;
     250        PyObject *py_lp_ctx, *py_session_info, *py_credentials;
     251        struct auth_session_info *session_info;
     252        struct cli_credentials *credentials;
     253        char *location;
     254        struct hive_key *hive_key;
     255        TALLOC_CTX *mem_ctx;
     256
     257        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
     258                                         discard_const_p(char *, kwnames),
     259                                         &location,
     260                                         &py_lp_ctx, &py_session_info,
     261                                         &py_credentials))
     262                return NULL;
     263
     264        mem_ctx = talloc_new(NULL);
     265        if (mem_ctx == NULL) {
     266                PyErr_NoMemory();
     267                return NULL;
     268        }
     269
     270        lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
     271        if (lp_ctx == NULL) {
     272                PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
     273                talloc_free(mem_ctx);
     274                return NULL;
     275        }
     276
     277        credentials = cli_credentials_from_py_object(py_credentials);
     278        if (credentials == NULL) {
     279                PyErr_SetString(PyExc_TypeError, "Expected credentials");
     280                talloc_free(mem_ctx);
     281                return NULL;
     282        }
     283        session_info = NULL;
     284
     285        result = reg_open_hive(NULL, location, session_info, credentials,
     286                               tevent_context_init(NULL),
     287                               lp_ctx, &hive_key);
     288        talloc_free(mem_ctx);
     289        PyErr_WERROR_IS_ERR_RAISE(result);
     290
     291        return py_talloc_steal(&PyHiveKey, hive_key);
    250292}
    251293
     
    253295        .tp_name = "HiveKey",
    254296        .tp_methods = hive_key_methods,
    255         .tp_new = hive_open,
     297        .tp_new = hive_new,
    256298        .tp_basicsize = sizeof(py_talloc_Object),
    257         .tp_dealloc = py_talloc_dealloc,
    258299        .tp_flags = Py_TPFLAGS_DEFAULT,
    259300};
     
    262303        .tp_name = "RegistryKey",
    263304        .tp_basicsize = sizeof(py_talloc_Object),
    264         .tp_dealloc = py_talloc_dealloc,
    265305        .tp_flags = Py_TPFLAGS_DEFAULT,
    266306};
     
    271311        struct registry_context *reg_ctx;
    272312        WERROR result;
    273     struct loadparm_context *lp_ctx;
     313        struct loadparm_context *lp_ctx;
    274314        PyObject *py_lp_ctx, *py_session_info, *py_credentials;
    275315        struct auth_session_info *session_info;
    276     struct cli_credentials *credentials;
    277         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO", discard_const_p(char *, kwnames),
    278                                          &py_lp_ctx, &py_session_info, &py_credentials))
    279                 return NULL;
    280 
    281     lp_ctx = lp_from_py_object(py_lp_ctx);
    282     if (lp_ctx == NULL) {
     316        struct cli_credentials *credentials;
     317        TALLOC_CTX *mem_ctx;
     318
     319        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO",
     320                                         discard_const_p(char *, kwnames),
     321                                         &py_lp_ctx, &py_session_info,
     322                                         &py_credentials))
     323                return NULL;
     324
     325        mem_ctx = talloc_new(NULL);
     326        if (mem_ctx == NULL) {
     327                PyErr_NoMemory();
     328                return NULL;
     329        }
     330
     331        lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
     332        if (lp_ctx == NULL) {
    283333                PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
    284                 return NULL;
    285     }
     334                talloc_free(mem_ctx);
     335                return NULL;
     336        }
    286337
    287338        credentials = cli_credentials_from_py_object(py_credentials);
    288339        if (credentials == NULL) {
    289340                PyErr_SetString(PyExc_TypeError, "Expected credentials");
     341                talloc_free(mem_ctx);
    290342                return NULL;
    291343        }
     
    295347        result = reg_open_samba(NULL, &reg_ctx, NULL,
    296348                                lp_ctx, session_info, credentials);
     349        talloc_free(mem_ctx);
    297350        if (!W_ERROR_IS_OK(result)) {
    298351                PyErr_SetWERROR(result);
    299352                return NULL;
    300353        }
    301        
     354
    302355        return py_talloc_steal(&PyRegistry, reg_ctx);
    303356}
     
    339392        WERROR result;
    340393        char *location;
    341     struct loadparm_context *lp_ctx;
    342     struct cli_credentials *credentials;
     394        struct loadparm_context *lp_ctx;
     395        struct cli_credentials *credentials;
    343396        struct hive_key *key;
    344397        struct auth_session_info *session_info;
     398        TALLOC_CTX *mem_ctx;
    345399
    346400        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
    347                                                                          discard_const_p(char *, kwnames),
    348                                                                          &location,
    349                                                                          &py_session_info, &py_credentials,
    350                                                                          &py_lp_ctx))
    351                 return NULL;
    352 
    353     lp_ctx = lp_from_py_object(py_lp_ctx);
    354     if (lp_ctx == NULL) {
     401                                         discard_const_p(char *, kwnames),
     402                                         &location, &py_session_info,
     403                                         &py_credentials, &py_lp_ctx))
     404                return NULL;
     405
     406        mem_ctx = talloc_new(NULL);
     407        if (mem_ctx == NULL) {
     408                PyErr_NoMemory();
     409                return NULL;
     410        }
     411
     412        lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
     413        if (lp_ctx == NULL) {
    355414                PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
    356                 return NULL;
    357     }
     415                talloc_free(mem_ctx);
     416                return NULL;
     417        }
    358418
    359419        credentials = cli_credentials_from_py_object(py_credentials);
    360420        if (credentials == NULL) {
    361421                PyErr_SetString(PyExc_TypeError, "Expected credentials");
     422                talloc_free(mem_ctx);
    362423                return NULL;
    363424        }
     
    366427
    367428        result = reg_open_ldb_file(NULL, location, session_info, credentials,
    368                                                            tevent_context_init(NULL), lp_ctx, &key);
     429                                   s4_event_context_init(NULL), lp_ctx, &key);
     430        talloc_free(mem_ctx);
    369431        PyErr_WERROR_IS_ERR_RAISE(result);
    370432
     
    401463        { "create_directory", py_create_directory, METH_VARARGS, "create_dir(location) -> key" },
    402464        { "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
     465        { "open_hive", (PyCFunction)py_open_hive, METH_VARARGS|METH_KEYWORDS, "open_hive(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
    403466        { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
    404467        { "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
     
    409472{
    410473        PyObject *m;
     474        PyTypeObject *talloc_type = PyTalloc_GetObjectType();
     475
     476        if (talloc_type == NULL)
     477                return;
     478
     479        PyHiveKey.tp_base = talloc_type;
     480        PyRegistry.tp_base = talloc_type;
     481        PyRegistryKey.tp_base = talloc_type;
    411482
    412483        if (PyType_Ready(&PyHiveKey) < 0)
  • vendor/current/source4/lib/registry/regf.c

    r414 r740  
    33   Registry backend for REGF files
    44   Copyright (C) 2005-2007 Jelmer Vernooij, jelmer@samba.org
    5    Copyright (C) 2006 Wilco Baan Hofman, wilco@baanhofman.nl
     5   Copyright (C) 2006-2010 Wilco Baan Hofman, wilco@baanhofman.nl
    66
    77   This program is free software; you can redistribute it and/or modify
     
    5050        struct hbin_block **hbins;
    5151        struct regf_hdr *header;
    52         struct smb_iconv_convenience *iconv_convenience;
     52        time_t last_write;
    5353};
    5454
    55 static WERROR regf_save_hbin(struct regf_data *data);
     55static WERROR regf_save_hbin(struct regf_data *data, bool flush);
    5656
    5757struct regf_key_data {
     
    6565                                         uint32_t offset, uint32_t *rel_offset)
    6666{
    67         int i;
     67        unsigned int i;
    6868
    6969        for (i = 0; data->hbins[i]; i++) {
     
    8787{
    8888        uint32_t checksum = 0, x;
    89         int i;
     89        unsigned int i;
    9090
    9191        for (i = 0; i < 0x01FB; i+= 4) {
     
    112112
    113113        if (hbin == NULL) {
    114                 DEBUG(1, ("Can't find HBIN containing 0x%04x\n", offset));
     114                DEBUG(1, ("Can't find HBIN at 0x%04x\n", offset));
    115115                return ret;
    116116        }
     
    135135                         TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
    136136{
    137         struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
     137        struct tdr_pull *pull = tdr_pull_init(regf);
    138138
    139139        pull->data = hbin_get(regf, offset);
     
    160160{
    161161        DATA_BLOB ret;
    162         uint32_t rel_offset = -1; /* Relative offset ! */
     162        uint32_t rel_offset = (uint32_t) -1; /* Relative offset ! */
    163163        struct hbin_block *hbin = NULL;
    164         int i;
     164        unsigned int i;
    165165
    166166        *offset = 0;
     
    218218                DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
    219219                        size));
     220
     221                /* Add extra hbin block */
    220222                data->hbins = talloc_realloc(data, data->hbins,
    221223                                             struct hbin_block *, i+2);
     
    226228                data->hbins[i+1] = NULL;
    227229
     230                /* Set hbin data */
    228231                hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
    229232                hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
    230233                hbin->offset_to_next = 0x1000;
    231234                hbin->unknown[0] = 0;
    232                 hbin->unknown[0] = 0;
     235                hbin->unknown[1] = 0;
    233236                unix_to_nt_time(&hbin->last_change, time(NULL));
    234237                hbin->block_size = hbin->offset_to_next;
    235238                hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
    236 
     239                /* Update the regf header */
     240                data->header->last_block += hbin->offset_to_next;
     241
     242                /* Set the next block to it's proper size and set the
     243                 * rel_offset for this block */
     244                SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
    237245                rel_offset = 0x0;
    238                 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
    239246        }
    240247
     
    262269        memcpy(dest.data, blob.data, blob.length);
    263270
     271        /* Make sure that we have no tailing garbage in the block */
     272        if (dest.length > blob.length) {
     273                memset(dest.data + blob.length, 0, dest.length - blob.length);
     274        }
     275
    264276        return ret;
    265277}
     
    268280                               tdr_push_fn_t push_fn, void *p)
    269281{
    270         struct tdr_push *push = tdr_push_init(data, data->iconv_convenience);
     282        struct tdr_push *push = tdr_push_init(data);
    271283        uint32_t ret;
    272284
     
    311323
    312324        /* If the next block is free, merge into big free block */
    313         if (rel_offset + size < hbin->offset_to_next) {
     325        if (rel_offset + size < hbin->offset_to_next - 0x20) {
    314326                next_size = IVALS(hbin->data, rel_offset+size);
    315327                if (next_size > 0) {
     
    336348        int32_t needed_size;
    337349        int32_t possible_size;
    338         int i;
     350        unsigned int i;
    339351
    340352        SMB_ASSERT(orig_offset > 0);
     
    395407                                      uint32_t orig_offset, void *p)
    396408{
    397         struct tdr_push *push = tdr_push_init(regf, regf->iconv_convenience);
     409        struct tdr_push *push = tdr_push_init(regf);
    398410        uint32_t ret;
    399411
     
    451463                                                    (char*)data.data,
    452464                                                    private_data->nk->clsname_length);
     465                        W_ERROR_HAVE_NO_MEMORY(*classname);
    453466                } else
    454467                        *classname = NULL;
     
    485498        if (!hbin_get_tdr(regf, offset, nk,
    486499                          (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
    487                 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
     500                DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n", offset));
    488501                return NULL;
    489502        }
     
    500513
    501514static WERROR regf_get_value(TALLOC_CTX *ctx, struct hive_key *key,
    502                              int idx, const char **name,
     515                             uint32_t idx, const char **name,
    503516                             uint32_t *data_type, DATA_BLOB *data)
    504517{
     
    515528        tmp = hbin_get(regf, private_data->nk->values_offset);
    516529        if (!tmp.data) {
    517                 DEBUG(0, ("Unable to find value list\n"));
     530                DEBUG(0, ("Unable to find value list at 0x%x\n",
     531                                private_data->nk->values_offset));
    518532                return WERR_GENERAL_FAILURE;
    519533        }
     
    530544        if (!hbin_get_tdr(regf, vk_offset, vk,
    531545                          (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
    532                 DEBUG(0, ("Unable to get VK block at %d\n", vk_offset));
     546                DEBUG(0, ("Unable to get VK block at 0x%x\n", vk_offset));
    533547                talloc_free(vk);
    534548                return WERR_GENERAL_FAILURE;
     
    536550
    537551        /* FIXME: name character set ?*/
    538         if (name != NULL)
     552        if (name != NULL) {
    539553                *name = talloc_strndup(ctx, vk->data_name, vk->name_length);
     554                W_ERROR_HAVE_NO_MEMORY(*name);
     555        }
    540556
    541557        if (data_type != NULL)
     
    543559
    544560        if (vk->data_length & 0x80000000) {
    545                 vk->data_length &=~0x80000000;
    546                 data->data = (uint8_t *)talloc_memdup(ctx, (uint8_t *)&vk->data_offset, vk->data_length);
    547                 data->length = vk->data_length;
     561                /* this is data of type "REG_DWORD" or "REG_DWORD_BIG_ENDIAN" */
     562                data->data = talloc_size(ctx, sizeof(uint32_t));
     563                W_ERROR_HAVE_NO_MEMORY(data->data);
     564                SIVAL(data->data, 0, vk->data_offset);
     565                data->length = sizeof(uint32_t);
    548566        } else {
    549567                *data = hbin_get(regf, vk->data_offset);
     
    563581                                     uint32_t *type, DATA_BLOB *data)
    564582{
    565         int i;
     583        unsigned int i;
    566584        const char *vname;
    567585        WERROR error;
     
    598616                return WERR_NO_MORE_ITEMS;
    599617
     618        /* Make sure that we don't crash if the key is empty */
     619        if (nk->subkeys_offset == -1) {
     620                return WERR_NO_MORE_ITEMS;
     621        }
     622
    600623        data = hbin_get(private_data->hive, nk->subkeys_offset);
    601624        if (!data.data) {
    602                 DEBUG(0, ("Unable to find subkey list\n"));
     625                DEBUG(0, ("Unable to find subkey list at 0x%x\n",
     626                        nk->subkeys_offset));
    603627                return WERR_GENERAL_FAILURE;
    604628        }
     
    606630        if (!strncmp((char *)data.data, "li", 2)) {
    607631                struct li_block li;
    608                 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
     632                struct tdr_pull *pull = tdr_pull_init(private_data->hive);
    609633
    610634                DEBUG(10, ("Subkeys in LI list\n"));
     
    627651        } else if (!strncmp((char *)data.data, "lf", 2)) {
    628652                struct lf_block lf;
    629                 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
     653                struct tdr_pull *pull = tdr_pull_init(private_data->hive);
    630654
    631655                DEBUG(10, ("Subkeys in LF list\n"));
     
    648672        } else if (!strncmp((char *)data.data, "lh", 2)) {
    649673                struct lh_block lh;
    650                 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
     674                struct tdr_pull *pull = tdr_pull_init(private_data->hive);
    651675
    652676                DEBUG(10, ("Subkeys in LH list\n"));
     
    668692        } else if (!strncmp((char *)data.data, "ri", 2)) {
    669693                struct ri_block ri;
    670                 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
     694                struct tdr_pull *pull = tdr_pull_init(ctx);
    671695                uint16_t i;
    672696                uint16_t sublist_count = 0;
     
    769793                                                    (char*)db.data,
    770794                                                    ret->nk->clsname_length);
     795                        W_ERROR_HAVE_NO_MEMORY(*classname);
    771796                } else
    772797                        *classname = NULL;
     
    801826        }
    802827
    803         pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
     828        pull = tdr_pull_init(ctx);
    804829
    805830        pull->data = subkey_data;
     
    836861        uint32_t key_off = 0;
    837862
     863        /* Make sure that we don't crash if the key is empty */
     864        if (nk->subkeys_offset == -1) {
     865                return WERR_BADFILE;
     866        }
     867
    838868        data = hbin_get(private_data->hive, nk->subkeys_offset);
    839869        if (!data.data) {
     
    844874        if (!strncmp((char *)data.data, "li", 2)) {
    845875                struct li_block li;
    846                 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
     876                struct tdr_pull *pull = tdr_pull_init(ctx);
    847877                uint16_t i;
    848878
     
    875905        } else if (!strncmp((char *)data.data, "lf", 2)) {
    876906                struct lf_block lf;
    877                 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
     907                struct tdr_pull *pull = tdr_pull_init(ctx);
    878908                uint16_t i;
    879909
     
    910940        } else if (!strncmp((char *)data.data, "lh", 2)) {
    911941                struct lh_block lh;
    912                 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
     942                struct tdr_pull *pull = tdr_pull_init(ctx);
    913943                uint16_t i;
    914944                uint32_t hash;
     
    947977        } else if (!strncmp((char *)data.data, "ri", 2)) {
    948978                struct ri_block ri;
    949                 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
     979                struct tdr_pull *pull = tdr_pull_init(ctx);
    950980                uint16_t i, j;
    951981
     
    10521082
    10531083        /* Push the security descriptor to a blob */
    1054         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf, NULL,
     1084        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
    10551085                                                          sec_desc, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
    10561086                DEBUG(0, ("Unable to push security descriptor\n"));
     
    12051235        data.data = sk.sec_desc;
    12061236        data.length = sk.rec_size;
    1207         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, NULL, *sd,
     1237        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, *sd,
    12081238                                                  (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
    12091239                DEBUG(0, ("Error parsing security descriptor\n"));
     
    12891319
    12901320        if (!strncmp((char *)data.data, "li", 2)) {
    1291                 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
     1321                struct tdr_pull *pull = tdr_pull_init(regf);
    12921322                struct li_block li;
     1323                struct nk_block sub_nk;
     1324                int32_t i, j;
    12931325
    12941326                pull->data = data;
     
    13071339                }
    13081340
     1341                /*
     1342                 * Find the position to store the pointer
     1343                 * Extensive testing reveils that at least on windows 7 subkeys
     1344                 * *MUST* be stored in alphabetical order
     1345                 */
     1346                for (i = 0; i < li.key_count; i++) {
     1347                        /* Get the nk */
     1348                        hbin_get_tdr(regf, li.nk_offset[i], regf,
     1349                                        (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
     1350                        if (strcasecmp(name, sub_nk.key_name) < 0) {
     1351                                break;
     1352                        }
     1353                }
     1354
    13091355                li.nk_offset = talloc_realloc(regf, li.nk_offset,
    13101356                                              uint32_t, li.key_count+1);
    13111357                W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
    1312                 li.nk_offset[li.key_count] = key_offset;
     1358
     1359                /* Move everything behind this offset */
     1360                for (j = li.key_count - 1; j >= i; j--) {
     1361                        li.nk_offset[j+1] = li.nk_offset[j];
     1362                }
     1363                       
     1364                li.nk_offset[i] = key_offset;
    13131365                li.key_count++;
    13141366                *ret = hbin_store_tdr_resize(regf,
     
    13181370                talloc_free(li.nk_offset);
    13191371        } else if (!strncmp((char *)data.data, "lf", 2)) {
    1320                 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
     1372                struct tdr_pull *pull = tdr_pull_init(regf);
    13211373                struct lf_block lf;
     1374                struct nk_block sub_nk;
     1375                int32_t i, j;
    13221376
    13231377                pull->data = data;
     
    13311385                SMB_ASSERT(!strncmp(lf.header, "lf", 2));
    13321386
     1387                /*
     1388                 * Find the position to store the hash record
     1389                 * Extensive testing reveils that at least on windows 7 subkeys
     1390                 * *MUST* be stored in alphabetical order
     1391                 */
     1392                for (i = 0; i < lf.key_count; i++) {
     1393                        /* Get the nk */
     1394                        hbin_get_tdr(regf, lf.hr[i].nk_offset, regf,
     1395                                        (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
     1396                        if (strcasecmp(name, sub_nk.key_name) < 0) {
     1397                                break;
     1398                        }
     1399                }
     1400
    13331401                lf.hr = talloc_realloc(regf, lf.hr, struct hash_record,
    13341402                                       lf.key_count+1);
    13351403                W_ERROR_HAVE_NO_MEMORY(lf.hr);
    1336                 lf.hr[lf.key_count].nk_offset = key_offset;
    1337                 lf.hr[lf.key_count].hash = talloc_strndup(lf.hr, name, 4);
     1404
     1405                /* Move everything behind this hash record */
     1406                for (j = lf.key_count - 1; j >= i; j--) {
     1407                        lf.hr[j+1] = lf.hr[j];
     1408                }
     1409
     1410                lf.hr[i].nk_offset = key_offset;
     1411                lf.hr[i].hash = talloc_strndup(lf.hr, name, 4);
    13381412                W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
    13391413                lf.key_count++;
     
    13441418                talloc_free(lf.hr);
    13451419        } else if (!strncmp((char *)data.data, "lh", 2)) {
    1346                 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
     1420                struct tdr_pull *pull = tdr_pull_init(regf);
    13471421                struct lh_block lh;
     1422                struct nk_block sub_nk;
     1423                int32_t i, j;
    13481424
    13491425                pull->data = data;
     
    13571433                SMB_ASSERT(!strncmp(lh.header, "lh", 2));
    13581434
     1435                /*
     1436                 * Find the position to store the hash record
     1437                 * Extensive testing reveils that at least on windows 7 subkeys
     1438                 * *MUST* be stored in alphabetical order
     1439                 */
     1440                for (i = 0; i < lh.key_count; i++) {
     1441                        /* Get the nk */
     1442                        hbin_get_tdr(regf, lh.hr[i].nk_offset, regf,
     1443                                        (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
     1444                        if (strcasecmp(name, sub_nk.key_name) < 0) {
     1445                                break;
     1446                        }
     1447                }
     1448
    13591449                lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash,
    13601450                                       lh.key_count+1);
    13611451                W_ERROR_HAVE_NO_MEMORY(lh.hr);
    1362                 lh.hr[lh.key_count].nk_offset = key_offset;
    1363                 lh.hr[lh.key_count].base37 = regf_create_lh_hash(name);
     1452
     1453                /* Move everything behind this hash record */
     1454                for (j = lh.key_count - 1; j >= i; j--) {
     1455                        lh.hr[j+1] = lh.hr[j];
     1456                }
     1457
     1458                lh.hr[i].nk_offset = key_offset;
     1459                lh.hr[i].base37 = regf_create_lh_hash(name);
    13641460                lh.key_count++;
    13651461                *ret = hbin_store_tdr_resize(regf,
     
    13931489        if (strncmp((char *)data.data, "li", 2) == 0) {
    13941490                struct li_block li;
    1395                 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
     1491                struct tdr_pull *pull = tdr_pull_init(regf);
    13961492                uint16_t i;
    13971493                bool found_offset = false;
     
    14371533        } else if (strncmp((char *)data.data, "lf", 2) == 0) {
    14381534                struct lf_block lf;
    1439                 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
     1535                struct tdr_pull *pull = tdr_pull_init(regf);
    14401536                uint16_t i;
    14411537                bool found_offset = false;
     
    14831579        } else if (strncmp((char *)data.data, "lh", 2) == 0) {
    14841580                struct lh_block lh;
    1485                 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
     1581                struct tdr_pull *pull = tdr_pull_init(regf);
    14861582                uint16_t i;
    14871583                bool found_offset = false;
     
    15381634}
    15391635
    1540 static WERROR regf_del_value (struct hive_key *key, const char *name)
     1636static WERROR regf_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
     1637                             const char *name)
    15411638{
    15421639        struct regf_key_data *private_data = (struct regf_key_data *)key;
     
    15471644        bool found_offset = false;
    15481645        DATA_BLOB values;
    1549         uint32_t i;
     1646        unsigned int i;
    15501647
    15511648        if (nk->values_offset == -1) {
     
    15921689                              private_data->offset, nk);
    15931690
    1594         return regf_save_hbin(private_data->hive);
    1595 }
    1596 
    1597 
    1598 static WERROR regf_del_key(const struct hive_key *parent, const char *name)
     1691        return regf_save_hbin(private_data->hive, 0);
     1692}
     1693
     1694
     1695static WERROR regf_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
     1696                           const char *name)
    15991697{
    16001698        const struct regf_key_data *private_data =
     
    16231721                char *sk_name;
    16241722                struct hive_key *sk = (struct hive_key *)key;
    1625                 int i = key->nk->num_subkeys;
     1723                unsigned int i = key->nk->num_subkeys;
    16261724                while (i--) {
    16271725                        /* Get subkey information. */
     
    16351733
    16361734                        /* Delete subkey. */
    1637                         error = regf_del_key(sk, sk_name);
     1735                        error = regf_del_key(NULL, sk, sk_name);
    16381736                        if (!W_ERROR_IS_OK(error)) {
    16391737                                DEBUG(0, ("Can't delete key '%s'.\n", sk_name));
     
    16491747                struct hive_key *sk = (struct hive_key *)key;
    16501748                DATA_BLOB data;
    1651                 int i = key->nk->num_values;
     1749                unsigned int i = key->nk->num_values;
    16521750                while (i--) {
    16531751                        /* Get value information. */
     
    16611759
    16621760                        /* Delete value. */
    1663                         error = regf_del_value(sk, val_name);
     1761                        error = regf_del_value(NULL, sk, val_name);
    16641762                        if (!W_ERROR_IS_OK(error)) {
    16651763                                DEBUG(0, ("Can't delete value '%s'.\n", val_name));
     
    16901788        hbin_free(private_data->hive, key->offset);
    16911789
    1692         return regf_save_hbin(private_data->hive);
     1790        return regf_save_hbin(private_data->hive, 0);
    16931791}
    16941792
     
    17171815        nk.num_values = 0;
    17181816        nk.values_offset = -1;
    1719         memset(nk.unk3, 0, 5);
     1817        memset(nk.unk3, 0, sizeof(nk.unk3));
    17201818        nk.clsname_offset = -1; /* FIXME: fill in */
    17211819        nk.clsname_length = 0;
     
    17281826        if (!hbin_get_tdr(regf, regf->header->data_offset, root,
    17291827                          (tdr_pull_fn_t)tdr_pull_nk_block, root)) {
    1730                 DEBUG(0, ("Unable to find HBIN data for offset %d\n",
     1828                DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n",
    17311829                        regf->header->data_offset));
    17321830                return WERR_GENERAL_FAILURE;
     
    17531851        *ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
    17541852
    1755         return regf_save_hbin(private_data->hive);
     1853        DEBUG(9, ("Storing key %s\n", name));
     1854        return regf_save_hbin(private_data->hive, 0);
    17561855}
    17571856
     
    17641863        struct vk_block vk;
    17651864        uint32_t i;
    1766         uint32_t tmp_vk_offset, vk_offset, old_vk_offset = -1;
     1865        uint32_t tmp_vk_offset, vk_offset, old_vk_offset = (uint32_t) -1;
    17671866        DATA_BLOB values;
    17681867
     
    17781877                                          (tdr_pull_fn_t)tdr_pull_vk_block,
    17791878                                          &vk)) {
    1780                                 DEBUG(0, ("Unable to get VK block at %d\n",
     1879                                DEBUG(0, ("Unable to get VK block at 0x%x\n",
    17811880                                        tmp_vk_offset));
    17821881                                return WERR_GENERAL_FAILURE;
     
    17871886                        }
    17881887                }
    1789                 /* Free data, if any */
    1790                 if (!(vk.data_length & 0x80000000)) {
    1791                         hbin_free(regf, vk.data_offset);
    1792                 }
    1793         }
     1888        }
     1889
     1890        /* If it's new, create the vk struct, if it's old, free the old data. */
    17941891        if (old_vk_offset == -1) {
    17951892                vk.header = "vk";
     
    18021899                        vk.flag = 0;
    18031900                }
    1804         }
     1901        } else {
     1902                /* Free data, if any */
     1903                if (!(vk.data_length & 0x80000000)) {
     1904                        hbin_free(regf, vk.data_offset);
     1905                }
     1906        }
     1907
    18051908        /* Set the type and data */
    18061909        vk.data_length = data.length;
    18071910        vk.data_type = type;
    1808         if (type == REG_DWORD) {
     1911        if ((type == REG_DWORD) || (type == REG_DWORD_BIG_ENDIAN)) {
     1912                if (vk.data_length != sizeof(uint32_t)) {
     1913                        DEBUG(0, ("DWORD or DWORD_BIG_ENDIAN value with size other than 4 byte!\n"));
     1914                        return WERR_NOT_SUPPORTED;
     1915                }
    18091916                vk.data_length |= 0x80000000;
    1810                 vk.data_offset = *(uint32_t *)data.data;
     1917                vk.data_offset = IVAL(data.data, 0);
    18111918        } else {
    18121919                /* Store data somewhere */
     
    18641971                              (tdr_push_fn_t) tdr_push_nk_block,
    18651972                              private_data->offset, nk);
    1866         return regf_save_hbin(private_data->hive);
    1867 }
    1868 
    1869 static WERROR regf_save_hbin(struct regf_data *regf)
    1870 {
    1871         struct tdr_push *push = tdr_push_init(regf, regf->iconv_convenience);
    1872         int i;
     1973        return regf_save_hbin(private_data->hive, 0);
     1974}
     1975
     1976static WERROR regf_save_hbin(struct regf_data *regf, bool flush)
     1977{
     1978        struct tdr_push *push = tdr_push_init(regf);
     1979        unsigned int i;
    18731980
    18741981        W_ERROR_HAVE_NO_MEMORY(push);
     1982
     1983        /* Only write once every 5 seconds, or when flush is set */
     1984        if (!flush && regf->last_write + 5 >= time(NULL)) {
     1985                return WERR_OK;
     1986        }
     1987
     1988        regf->last_write = time(NULL);
    18751989
    18761990        if (lseek(regf->fd, 0, SEEK_SET) == -1) {
     
    18872001        talloc_free(push);
    18882002
    1889         if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd, regf->iconv_convenience,
     2003        if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
    18902004                                            (tdr_push_fn_t)tdr_push_regf_hdr,
    18912005                                            regf->header))) {
     
    19002014
    19012015        for (i = 0; regf->hbins[i]; i++) {
    1902                 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd, regf->iconv_convenience,
     2016                if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
    19032017                                                    (tdr_push_fn_t)tdr_push_hbin_block,
    19042018                                                    regf->hbins[i]))) {
     
    19122026
    19132027WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx,
    1914                             struct smb_iconv_convenience *iconv_convenience,
    19152028                            const char *location,
    19162029                            int minor_version, struct hive_key **key)
     
    19262039
    19272040        regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
    1928 
    1929         regf->iconv_convenience = iconv_convenience;
    19302041
    19312042        W_ERROR_HAVE_NO_MEMORY(regf);
     
    19632074
    19642075        nk.header = "nk";
    1965         nk.type = REG_SUB_KEY;
     2076        nk.type = REG_ROOT_KEY;
    19662077        unix_to_nt_time(&nk.last_change, time(NULL));
    19672078        nk.uk1 = 0;
     
    19972108       
    19982109        /* Push the security descriptor to a blob */
    1999         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf, NULL,
     2110        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
    20002111                                     sd, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
    20012112                DEBUG(0, ("Unable to push security descriptor\n"));
     
    20282139                                               regf->header->data_offset);
    20292140
    2030         error = regf_save_hbin(regf);
     2141        error = regf_save_hbin(regf, 1);
    20312142        if (!W_ERROR_IS_OK(error)) {
    20322143                return error;
     
    20342145       
    20352146        /* We can drop our own reference now that *key will have created one */
    2036         talloc_free(regf);
     2147        talloc_unlink(NULL, regf);
    20372148
    20382149        return WERR_OK;
    20392150}
    20402151
     2152static WERROR regf_flush_key(struct hive_key *key)
     2153{
     2154        struct regf_key_data *private_data = (struct regf_key_data *)key;
     2155        struct regf_data *regf = private_data->hive;
     2156        WERROR error;
     2157
     2158        error = regf_save_hbin(regf, 1);
     2159        if (!W_ERROR_IS_OK(error)) {
     2160                DEBUG(0, ("Failed to flush regf to disk\n"));
     2161                return error;
     2162        }
     2163
     2164        return WERR_OK;
     2165}
     2166
     2167static int regf_destruct(struct regf_data *regf)
     2168{
     2169        WERROR error;
     2170
     2171        /* Write to disk */
     2172        error = regf_save_hbin(regf, 1);
     2173        if (!W_ERROR_IS_OK(error)) {
     2174                DEBUG(0, ("Failed to flush registry to disk\n"));
     2175                return -1;
     2176        }
     2177
     2178        /* Close file descriptor */
     2179        close(regf->fd);
     2180
     2181        return 0;
     2182}
     2183
    20412184WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx, const char *location,
    2042                           struct smb_iconv_convenience *iconv_convenience, struct hive_key **key)
     2185                          struct hive_key **key)
    20432186{
    20442187        struct regf_data *regf;
    20452188        struct regf_hdr *regf_hdr;
    20462189        struct tdr_pull *pull;
    2047         int i;
     2190        unsigned int i;
    20482191
    20492192        regf = (struct regf_data *)talloc_zero(parent_ctx, struct regf_data);
    2050 
    2051         regf->iconv_convenience = iconv_convenience;
    2052 
    20532193        W_ERROR_HAVE_NO_MEMORY(regf);
     2194
     2195        talloc_set_destructor(regf, regf_destruct);
    20542196
    20552197        DEBUG(5, ("Attempting to load registry file\n"));
     
    20652207        }
    20662208
    2067         pull = tdr_pull_init(regf, regf->iconv_convenience);
     2209        pull = tdr_pull_init(regf);
    20682210
    20692211        pull->data.data = (uint8_t*)fd_load(regf->fd, &pull->data.length, 0, regf);
     
    21632305        .del_key = regf_del_key,
    21642306        .delete_value = regf_del_value,
     2307        .flush_key = regf_flush_key
    21652308};
  • vendor/current/source4/lib/registry/regf.idl

    r414 r740  
    2525        [noprint] struct regf_version {
    2626                [value(1)] uint32 major;
    27                 [value(3)] uint32 minor;
     27                uint32 minor;
    2828                [value(0)] uint32 release;
    2929                [value(1)] uint32 build;
     
    7575
    7676        [noprint] enum reg_key_type {
    77                 REG_ROOT_KEY = 0x20,
    78                 REG_SUB_KEY  = 0x2C,
     77                REG_ROOT_KEY = 0x2C,
     78                REG_SUB_KEY  = 0x20,
    7979                REG_SYM_LINK = 0x10
    8080        };
  • vendor/current/source4/lib/registry/registry.h

    r414 r740  
    2424struct registry_context;
    2525struct loadparm_context;
    26 struct smb_iconv_convenience;
    2726
    2827#include <talloc.h>
     
    7170         */
    7271        WERROR (*add_key) (TALLOC_CTX *ctx,
    73                            const struct hive_key *parent_key, const char *name,
     72                           const struct hive_key *parent_key, const char *path,
    7473                           const char *classname,
    7574                           struct security_descriptor *desc,
     
    7877         * Remove an existing key.
    7978         */
    80         WERROR (*del_key) (const struct hive_key *key, const char *name);
     79        WERROR (*del_key) (TALLOC_CTX *mem_ctx,
     80                           const struct hive_key *key, const char *name);
    8181
    8282        /**
     
    8989         */
    9090        WERROR (*enum_value) (TALLOC_CTX *mem_ctx,
    91                               struct hive_key *key, int idx,
     91                              struct hive_key *key, uint32_t idx,
    9292                              const char **name, uint32_t *type,
    9393                              DATA_BLOB *data);
     
    109109         * Remove a value.
    110110         */
    111         WERROR (*delete_value) (struct hive_key *key, const char *name);
     111        WERROR (*delete_value) (TALLOC_CTX *mem_ctx,
     112                                struct hive_key *key, const char *name);
    112113
    113114        /* Security Descriptors */
     
    167168                         struct security_descriptor *desc,
    168169                         struct hive_key **key);
    169 WERROR hive_key_del(const struct hive_key *key, const char *name);
     170WERROR hive_key_del(TALLOC_CTX *mem_ctx,
     171                    const struct hive_key *key, const char *name);
    170172WERROR hive_get_key_by_name(TALLOC_CTX *mem_ctx,
    171173                            const struct hive_key *key, const char *name,
     
    194196                         const struct security_descriptor *security);
    195197
    196 WERROR hive_key_del_value(struct hive_key *key, const char *name);
     198WERROR hive_key_del_value(TALLOC_CTX *mem_ctx,
     199                          struct hive_key *key, const char *name);
    197200
    198201WERROR hive_key_flush(struct hive_key *key);
     
    203206                          const char *location, struct hive_key **key);
    204207WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx,
    205                           const char *location, struct smb_iconv_convenience *iconv_convenience,
    206                           struct hive_key **key);
     208                          const char *location, struct hive_key **key);
    207209WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
    208210                         struct auth_session_info *session_info,
     
    216218                            const char *location, struct hive_key **key);
    217219WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx,
    218                             struct smb_iconv_convenience *iconv_convenience,
    219220                            const char *location,
    220221                            int major_version,
     
    305306                              struct registry_key **key);
    306307
    307         WERROR (*delete_key) (struct registry_key *key, const char *name);
    308 
    309         WERROR (*delete_value) (struct registry_key *key, const char *name);
     308        WERROR (*delete_key) (TALLOC_CTX *mem_ctx,
     309                              struct registry_key *key, const char *name);
     310
     311        WERROR (*delete_value) (TALLOC_CTX *mem_ctx,
     312                                struct registry_key *key, const char *name);
    310313
    311314        WERROR (*enum_key) (TALLOC_CTX *mem_ctx,
     
    412415WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx,
    413416                                   const struct registry_key *key,
    414                                    int idx,
     417                                   uint32_t idx,
    415418                                   const char **name,
    416419                                   const char **classname,
     
    425428                                 uint32_t *type,
    426429                                 DATA_BLOB *data);
    427 WERROR reg_key_del(struct registry_key *parent, const char *name);
     430WERROR reg_key_del(TALLOC_CTX *mem_ctx,
     431                   struct registry_key *parent, const char *name);
    428432WERROR reg_key_add_name(TALLOC_CTX *mem_ctx,
    429433                        struct registry_key *parent, const char *name,
     
    435439WERROR reg_get_sec_desc(TALLOC_CTX *ctx, const struct registry_key *key,
    436440                        struct security_descriptor **secdesc);
    437 WERROR reg_del_value(struct registry_key *key, const char *valname);
     441WERROR reg_del_value(TALLOC_CTX *mem_ctx,
     442                     struct registry_key *key, const char *valname);
    438443WERROR reg_key_flush(struct registry_key *key);
    439444WERROR reg_create_key(TALLOC_CTX *mem_ctx,
     
    446451/* Utility functions */
    447452const char *str_regtype(int type);
    448 char *reg_val_data_string(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, uint32_t type, const DATA_BLOB data);
    449 char *reg_val_description(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, const char *name,
     453bool push_reg_sz(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *s);
     454bool push_reg_multi_sz(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char **a);
     455bool pull_reg_sz(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, const char **s);
     456bool pull_reg_multi_sz(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, const char ***a);
     457int regtype_by_string(const char *str);
     458char *reg_val_data_string(TALLOC_CTX *mem_ctx, uint32_t type, const DATA_BLOB data);
     459char *reg_val_description(TALLOC_CTX *mem_ctx, const char *name,
    450460                          uint32_t type, const DATA_BLOB data);
    451 bool reg_string_to_val(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, const char *type_str,
     461bool reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str,
    452462                       const char *data_str, uint32_t *type, DATA_BLOB *data);
    453463WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
     
    486496
    487497WERROR reg_diff_apply(struct registry_context *ctx,
    488                                           struct smb_iconv_convenience *ic, const char *filename);
     498                                          const char *filename);
    489499
    490500WERROR reg_generate_diff(struct registry_context *ctx1,
     
    493503                         void *callback_data);
    494504WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
    495                             struct smb_iconv_convenience *iconv_convenience,
    496505                            struct reg_diff_callbacks **callbacks,
    497506                            void **callback_data);
    498507WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
    499                           struct smb_iconv_convenience *ic,
    500508                          struct reg_diff_callbacks **callbacks,
    501509                          void **callback_data);
     
    506514                             void *callback_data);
    507515WERROR reg_diff_load(const char *filename,
    508                      struct smb_iconv_convenience *iconv_convenience,
    509516                     const struct reg_diff_callbacks *callbacks,
    510517                     void *callback_data);
    511518
    512519WERROR reg_dotreg_diff_load(int fd,
    513                                      struct smb_iconv_convenience *iconv_convenience,
    514520                                     const struct reg_diff_callbacks *callbacks,
    515521                                     void *callback_data);
    516522
    517523WERROR reg_preg_diff_load(int fd,
    518                    struct smb_iconv_convenience *iconv_convenience,
    519524                                   const struct reg_diff_callbacks *callbacks,
    520525                                   void *callback_data);
  • vendor/current/source4/lib/registry/registry.pc.in

    r414 r740  
    88Requires: talloc
    99Requires.private: ldb
    10 Version: 0.0.1
    11 Libs: -L${libdir} -lregistry
     10Version: @PACKAGE_VERSION@
     11Libs: @LIB_RPATH@ -L${libdir} -lregistry
    1212Cflags: -I${includedir}  -DHAVE_IMMEDIATE_STRUCTURES=1
  • vendor/current/source4/lib/registry/rpc.c

    r414 r740  
    2828        struct registry_key key;
    2929        struct policy_handle pol;
    30         struct dcerpc_pipe *pipe;
    31 
     30        struct dcerpc_binding_handle *binding_handle;
    3231        const char* classname; 
    3332        uint32_t num_subkeys;
     
    4443        struct registry_context context;
    4544        struct dcerpc_pipe *pipe;
     45        struct dcerpc_binding_handle *binding_handle;
    4646};
    4747
     
    5252 */
    5353
    54 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
     54#define openhive(u) static WERROR open_ ## u(struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
    5555{ \
    5656        struct winreg_Open ## u r; \
     
    6262        r.out.handle = hnd;\
    6363\
    64         status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
     64        status = dcerpc_winreg_Open ## u ## _r(b, mem_ctx, &r); \
    6565\
    6666        if (!NT_STATUS_IS_OK(status)) { \
     
    8282static struct {
    8383        uint32_t hkey;
    84         WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *,
     84        WERROR (*open) (struct dcerpc_binding_handle *b, TALLOC_CTX *,
    8585                        struct policy_handle *h);
    8686} known_hives[] = {
     
    118118
    119119        mykeydata = talloc_zero(ctx, struct rpc_key);
     120        W_ERROR_HAVE_NO_MEMORY(mykeydata);
    120121        mykeydata->key.context = ctx;
    121         mykeydata->pipe = talloc_reference(mykeydata, rctx->pipe);
     122        mykeydata->binding_handle = rctx->binding_handle;
    122123        mykeydata->num_values = -1;
    123124        mykeydata->num_subkeys = -1;
    124125        *k = (struct registry_key *)mykeydata;
    125         return known_hives[n].open(mykeydata->pipe, mykeydata, &(mykeydata->pol));
     126        return known_hives[n].open(mykeydata->binding_handle, mykeydata, &mykeydata->pol);
    126127}
    127128
     
    161162
    162163        mykeydata = talloc_zero(mem_ctx, struct rpc_key);
     164        W_ERROR_HAVE_NO_MEMORY(mykeydata);
    163165        mykeydata->key.context = parentkeydata->key.context;
    164         mykeydata->pipe = talloc_reference(mykeydata, parentkeydata->pipe);
     166        mykeydata->binding_handle = parentkeydata->binding_handle;
    165167        mykeydata->num_values = -1;
    166168        mykeydata->num_subkeys = -1;
     
    171173        r.in.parent_handle = &parentkeydata->pol;
    172174        r.in.keyname.name = name;
    173         r.in.unknown = 0x00000000;
     175        r.in.options = 0x00000000;
    174176        r.in.access_mask = 0x02000000;
    175177        r.out.handle = &mykeydata->pol;
    176178
    177         status = dcerpc_winreg_OpenKey(mykeydata->pipe, mem_ctx, &r);
     179        status = dcerpc_winreg_OpenKey_r(mykeydata->binding_handle, mem_ctx, &r);
    178180
    179181        if (!NT_STATUS_IS_OK(status)) {
     
    213215        r.in.enum_index = n;
    214216        r.in.name = &name;
    215         r.in.type = type;
     217        r.in.type = (enum winreg_Type *) type;
    216218        r.in.value = &value;
    217219        r.in.size = &val_size;
    218220        r.in.length = &zero;
    219221        r.out.name = &name;
    220         r.out.type = type;
     222        r.out.type = (enum winreg_Type *) type;
    221223        r.out.value = &value;
    222224        r.out.size = &val_size;
    223225        r.out.length = &zero;
    224226
    225         status = dcerpc_winreg_EnumValue(mykeydata->pipe, mem_ctx, &r);
     227        status = dcerpc_winreg_EnumValue_r(mykeydata->binding_handle, mem_ctx, &r);
    226228
    227229        if (!NT_STATUS_IS_OK(status)) {
     
    230232        }
    231233
    232         *value_name = talloc_reference(mem_ctx, r.out.name->name);
     234        *value_name = talloc_steal(mem_ctx, r.out.name->name);
    233235        *type = *(r.out.type);
    234236        *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length);
     
    262264        r.in.handle = &mykeydata->pol;
    263265        r.in.value_name = &name;
    264         r.in.type = type;
     266        r.in.type = (enum winreg_Type *) type;
    265267        r.in.data = &value;
    266268        r.in.data_size = &val_size;
    267269        r.in.data_length = &zero;
    268         r.out.type = type;
     270        r.out.type = (enum winreg_Type *) type;
    269271        r.out.data = &value;
    270272        r.out.data_size = &val_size;
    271273        r.out.data_length = &zero;
    272274
    273         status = dcerpc_winreg_QueryValue(mykeydata->pipe, mem_ctx, &r);
     275        status = dcerpc_winreg_QueryValue_r(mykeydata->binding_handle, mem_ctx, &r);
    274276
    275277        if (!NT_STATUS_IS_OK(status)) {
     
    312314        r.out.last_changed_time = &change_time;
    313315
    314         status = dcerpc_winreg_EnumKey(mykeydata->pipe, mem_ctx, &r);
     316        status = dcerpc_winreg_EnumKey_r(mykeydata->binding_handle, mem_ctx, &r);
    315317
    316318        if (!NT_STATUS_IS_OK(status)) {
     
    320322
    321323        if (name != NULL)
    322                 *name = talloc_reference(mem_ctx, r.out.name->name);
     324                *name = talloc_steal(mem_ctx, r.out.name->name);
    323325        if (keyclass != NULL)
    324                 *keyclass = talloc_reference(mem_ctx, r.out.keyclass->name);
     326                *keyclass = talloc_steal(mem_ctx, r.out.keyclass->name);
    325327        if (last_changed_time != NULL)
    326328                *last_changed_time = *(r.out.last_changed_time);
     
    330332
    331333static WERROR rpc_add_key(TALLOC_CTX *mem_ctx,
    332                           struct registry_key *parent, const char *name,
     334                          struct registry_key *parent, const char *path,
    333335                          const char *key_class,
    334336                          struct security_descriptor *sec,
     
    343345        ZERO_STRUCT(r);
    344346        r.in.handle = &parentkd->pol;
    345         r.in.name.name = name;
     347        r.in.name.name = path;
    346348        r.in.keyclass.name = NULL;
    347349        r.in.options = 0;
     
    352354        r.out.action_taken = NULL;
    353355
    354         status = dcerpc_winreg_CreateKey(parentkd->pipe, mem_ctx, &r);
     356        status = dcerpc_winreg_CreateKey_r(parentkd->binding_handle, mem_ctx, &r);
    355357
    356358        if (!NT_STATUS_IS_OK(status)) {
     
    360362        }
    361363
    362         rpck->pipe = talloc_reference(rpck, parentkd->pipe);
     364        rpck->binding_handle = parentkd->binding_handle;
    363365        *key = (struct registry_key *)rpck;
    364366
     
    388390        r.out.last_changed_time = &mykeydata->last_changed_time;
    389391
    390         status = dcerpc_winreg_QueryInfoKey(mykeydata->pipe, mem_ctx, &r);
     392        status = dcerpc_winreg_QueryInfoKey_r(mykeydata->binding_handle, mem_ctx, &r);
    391393
    392394        if (!NT_STATUS_IS_OK(status)) {
     
    395397        }
    396398
    397         mykeydata->classname = talloc_reference(mem_ctx, r.out.classname->name);
    398 
    399         return r.out.result;
    400 }
    401 
    402 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
     399        mykeydata->classname = talloc_steal(mem_ctx, r.out.classname->name);
     400
     401        return r.out.result;
     402}
     403
     404static WERROR rpc_del_key(TALLOC_CTX *mem_ctx, struct registry_key *parent,
     405                          const char *name)
    403406{
    404407        NTSTATUS status;
    405408        struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
    406409        struct winreg_DeleteKey r;
    407         TALLOC_CTX *mem_ctx = talloc_init("del_key");
    408410
    409411        ZERO_STRUCT(r);
     
    411413        r.in.key.name = name;
    412414
    413         status = dcerpc_winreg_DeleteKey(mykeydata->pipe, mem_ctx, &r);
    414 
    415         talloc_free(mem_ctx);
     415        status = dcerpc_winreg_DeleteKey_r(mykeydata->binding_handle, mem_ctx, &r);
    416416
    417417        if (!NT_STATUS_IS_OK(status)) {
     
    474474        .delete_key = rpc_del_key,
    475475        .get_key_info = rpc_get_info,
    476         .get_predefined_key = rpc_get_predefined_key,
    477476};
    478477
     
    490489
    491490        rctx = talloc(NULL, struct rpc_registry_context);
     491        W_ERROR_HAVE_NO_MEMORY(rctx);
    492492
    493493        /* Default to local smbd if no connection is specified */
     
    498498        status = dcerpc_pipe_connect(rctx /* TALLOC_CTX */,
    499499                                     &p, location,
    500                                         &ndr_table_winreg,
     500                                    &ndr_table_winreg,
    501501                                     credentials, ev, lp_ctx);
    502         rctx->pipe = p;
    503 
    504502        if(NT_STATUS_IS_ERR(status)) {
    505503                DEBUG(1, ("Unable to open '%s': %s\n", location,
     
    510508        }
    511509
     510        rctx->pipe = p;
     511        rctx->binding_handle = p->binding_handle;
     512
    512513        *ctx = (struct registry_context *)rctx;
    513514        (*ctx)->ops = &reg_backend_rpc;
  • vendor/current/source4/lib/registry/samba.c

    r414 r740  
    3939
    4040        location = talloc_asprintf(ctx, "%s/%s.ldb",
    41                                    lp_private_dir(lp_ctx),
     41                                   lpcfg_private_dir(lp_ctx),
    4242                                   name);
     43        W_ERROR_HAVE_NO_MEMORY(location);
    4344
    4445        error = reg_open_hive(ctx, location, auth_info, creds, event_ctx, lp_ctx, &hive);
     
    4748                error = reg_open_ldb_file(ctx, location, auth_info,
    4849                                          creds, event_ctx, lp_ctx, &hive);
     50
     51        talloc_free(discard_const_p(char, location));
    4952
    5053        if (!W_ERROR_IS_OK(error))
  • vendor/current/source4/lib/registry/tests/diff.c

    r414 r740  
    5353{
    5454        struct diff_tcase_data *td = tcase_data;
    55         struct smb_iconv_convenience *ic;
    5655        struct reg_diff_callbacks *callbacks;
    5756        void *data;
    5857        WERROR error;
    5958
    60         ic = lp_iconv_convenience(tctx->lp_ctx);
    61 
    62         error = reg_diff_load(td->filename, iconv_convenience, callbacks, data);
     59        error = reg_diff_load(td->filename, callbacks, data);
    6360        torture_assert_werr_ok(tctx, error, "reg_diff_load");
    6461
     
    7269        WERROR error;
    7370
    74         error = reg_diff_apply(td->r1_ctx, lp_iconv_convenience(tctx->lp_ctx), td->filename);
     71        error = reg_diff_apply(td->r1_ctx, td->filename);
    7572        torture_assert_werr_ok(tctx, error, "reg_diff_apply");
    7673
     
    228225        torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\Policies\\Explorer failed");
    229226
    230 
    231         blob.data = (void *)talloc(r2_ctx, uint32_t);
    232         SIVAL(blob.data, 0, 0x03ffffff);
    233         blob.length = sizeof(uint32_t);
     227        blob.data = talloc_array(r2_ctx, uint8_t, 4);
     228        /* set "0x03FFFFFF" in little endian format */
     229        blob.data[0] = 0xFF; blob.data[1] = 0xFF;
     230        blob.data[2] = 0xFF; blob.data[3] = 0x03;
     231        blob.length = 4;
    234232
    235233        r1_ctx->ops->set_value(newkey, "NoDrives", REG_DWORD, blob);
     
    247245{
    248246        struct diff_tcase_data *td;
    249         struct smb_iconv_convenience *ic;
    250247        WERROR error;
    251248
     
    253250        td = *data;
    254251
    255         ic = lp_iconv_convenience(tctx->lp_ctx);
    256 
    257252        td->filename = talloc_asprintf(tctx, "%s/test.pol", td->tempdir);
    258         error = reg_preg_diff_save(tctx, td->filename, ic, &td->callbacks, &td->callback_data);
     253        error = reg_preg_diff_save(tctx, td->filename,  &td->callbacks,
     254                                                           &td->callback_data);
    259255        torture_assert_werr_ok(tctx, error, "reg_preg_diff_save");
    260256
     
    265261{
    266262        struct diff_tcase_data *td;
    267         struct smb_iconv_convenience *ic;
    268263        WERROR error;
    269264
     
    271266        td = *data;
    272267
    273         ic = lp_iconv_convenience(tctx->lp_ctx);
    274        
    275268        td->filename = talloc_asprintf(tctx, "%s/test.reg", td->tempdir);
    276         error = reg_dotreg_diff_save(tctx, td->filename, ic, &td->callbacks, &td->callback_data);
     269        error = reg_dotreg_diff_save(tctx, td->filename, &td->callbacks,
     270                                                                 &td->callback_data);
    277271        torture_assert_werr_ok(tctx, error, "reg_dotreg_diff_save");
    278272
     
    283277{
    284278        struct torture_tcase *tcase;
    285         struct torture_suite *suite = torture_suite_create(mem_ctx, "DIFF");
     279        struct torture_suite *suite = torture_suite_create(mem_ctx, "diff");
    286280
    287281        tcase = torture_suite_add_tcase(suite, "PReg");
  • vendor/current/source4/lib/registry/tests/generic.c

    r414 r740  
    3232static bool test_str_regtype(struct torture_context *ctx)
    3333{
     34        torture_assert_str_equal(ctx, str_regtype(0),
     35                                 "REG_NONE", "REG_NONE failed");
    3436        torture_assert_str_equal(ctx, str_regtype(1),
    3537                                 "REG_SZ", "REG_SZ failed");
     38        torture_assert_str_equal(ctx, str_regtype(2),
     39                                 "REG_EXPAND_SZ", "REG_EXPAND_SZ failed");
     40        torture_assert_str_equal(ctx, str_regtype(3),
     41                                 "REG_BINARY", "REG_BINARY failed");
    3642        torture_assert_str_equal(ctx, str_regtype(4),
    3743                                 "REG_DWORD", "REG_DWORD failed");
     44        torture_assert_str_equal(ctx, str_regtype(5),
     45                                 "REG_DWORD_BIG_ENDIAN", "REG_DWORD_BIG_ENDIAN failed");
     46        torture_assert_str_equal(ctx, str_regtype(6),
     47                                 "REG_LINK", "REG_LINK failed");
     48        torture_assert_str_equal(ctx, str_regtype(7),
     49                                 "REG_MULTI_SZ", "REG_MULTI_SZ failed");
     50        torture_assert_str_equal(ctx, str_regtype(8),
     51                                 "REG_RESOURCE_LIST", "REG_RESOURCE_LIST failed");
     52        torture_assert_str_equal(ctx, str_regtype(9),
     53                                 "REG_FULL_RESOURCE_DESCRIPTOR", "REG_FULL_RESOURCE_DESCRIPTOR failed");
     54        torture_assert_str_equal(ctx, str_regtype(10),
     55                                 "REG_RESOURCE_REQUIREMENTS_LIST", "REG_RESOURCE_REQUIREMENTS_LIST failed");
     56        torture_assert_str_equal(ctx, str_regtype(11),
     57                                 "REG_QWORD", "REG_QWORD failed");
    3858
    3959        return true;
     
    4363static bool test_reg_val_data_string_dword(struct torture_context *ctx)
    4464{
    45         uint32_t d = 0x20;
    46         DATA_BLOB db = { (uint8_t *)&d, sizeof(d) };
    47         torture_assert_str_equal(ctx, "0x20",
    48                                  reg_val_data_string(ctx, lp_iconv_convenience(ctx->lp_ctx), REG_DWORD, db),
     65        uint8_t d[] = { 0x20, 0x00, 0x00, 0x00 };
     66        DATA_BLOB db = { d, 4 };
     67        torture_assert_str_equal(ctx, "0x00000020",
     68                                 reg_val_data_string(ctx, REG_DWORD, db),
    4969                                 "dword failed");
     70        return true;
     71}
     72
     73static bool test_reg_val_data_string_dword_big_endian(struct torture_context *ctx)
     74{
     75        uint8_t d[] = { 0x20, 0x00, 0x00, 0x00 };
     76        DATA_BLOB db = { d, 4 };
     77        torture_assert_str_equal(ctx, "0x00000020",
     78                                 reg_val_data_string(ctx, REG_DWORD_BIG_ENDIAN, db),
     79                                 "dword failed");
     80        return true;
     81}
     82
     83static bool test_reg_val_data_string_qword(struct torture_context *ctx)
     84{
     85        uint8_t d[] = { 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
     86        DATA_BLOB db = { d, 8 };
     87        torture_assert_str_equal(ctx, "0x0000000000000020",
     88                                 reg_val_data_string(ctx, REG_QWORD, db),
     89                                 "qword failed");
    5090        return true;
    5191}
     
    5494{
    5595        DATA_BLOB db;
    56         convert_string_talloc_convenience(ctx, lp_iconv_convenience(ctx->lp_ctx), CH_UTF8, CH_UTF16,
     96        convert_string_talloc(ctx, CH_UTF8, CH_UTF16,
    5797                                          "bla", 3, (void **)&db.data, &db.length, false);
    5898        torture_assert_str_equal(ctx, "bla",
    59                                  reg_val_data_string(ctx, lp_iconv_convenience(ctx->lp_ctx), REG_SZ, db),
     99                                 reg_val_data_string(ctx, REG_SZ, db),
    60100                                 "sz failed");
    61101        db.length = 4;
    62102        torture_assert_str_equal(ctx, "bl",
    63                                  reg_val_data_string(ctx, lp_iconv_convenience(ctx->lp_ctx), REG_SZ, db),
     103                                 reg_val_data_string(ctx, REG_SZ, db),
    64104                                 "sz failed");
    65105        return true;
     
    71111        DATA_BLOB db = { x, 4 };
    72112        torture_assert_str_equal(ctx, "01020304",
    73                                  reg_val_data_string(ctx, lp_iconv_convenience(ctx->lp_ctx), REG_BINARY, db),
     113                                 reg_val_data_string(ctx, REG_BINARY, db),
    74114                                 "binary failed");
    75115        return true;
     
    81121        DATA_BLOB db = { NULL, 0 };
    82122        torture_assert_str_equal(ctx, "",
    83                                  reg_val_data_string(ctx, lp_iconv_convenience(ctx->lp_ctx), REG_BINARY, db),
     123                                 reg_val_data_string(ctx, REG_BINARY, db),
    84124                                 "empty failed");
    85125        return true;
     
    89129{
    90130        DATA_BLOB data;
    91         convert_string_talloc_convenience(ctx, lp_iconv_convenience(ctx->lp_ctx), CH_UTF8, CH_UTF16,
     131        convert_string_talloc(ctx, CH_UTF8, CH_UTF16,
    92132                                            "stationary traveller",
    93133                                            strlen("stationary traveller"),
    94134                                            (void **)&data.data, &data.length, false);
    95135        torture_assert_str_equal(ctx, "camel = REG_SZ : stationary traveller",
    96                                  reg_val_description(ctx, lp_iconv_convenience(ctx->lp_ctx), "camel", REG_SZ, data),
     136                                 reg_val_description(ctx, "camel", REG_SZ, data),
    97137                                 "reg_val_description failed");
    98138        return true;
     
    103143{
    104144        DATA_BLOB data;
    105         convert_string_talloc_convenience(ctx, lp_iconv_convenience(ctx->lp_ctx), CH_UTF8, CH_UTF16,
     145        convert_string_talloc(ctx, CH_UTF8, CH_UTF16,
    106146                                            "west berlin",
    107147                                            strlen("west berlin"),
    108148                                            (void **)&data.data, &data.length, false);
    109149        torture_assert_str_equal(ctx, "<No Name> = REG_SZ : west berlin",
    110                                  reg_val_description(ctx, lp_iconv_convenience(ctx->lp_ctx), NULL, REG_SZ, data),
     150                                 reg_val_description(ctx, NULL, REG_SZ, data),
    111151                                 "description with null name failed");
    112152        return true;
     
    115155struct torture_suite *torture_registry(TALLOC_CTX *mem_ctx)
    116156{
    117         struct torture_suite *suite = torture_suite_create(mem_ctx, "REGISTRY");
     157        struct torture_suite *suite = torture_suite_create(mem_ctx, "registry");
    118158        torture_suite_add_simple_test(suite, "str_regtype",
    119159                                      test_str_regtype);
    120160        torture_suite_add_simple_test(suite, "reg_val_data_string dword",
    121161                                      test_reg_val_data_string_dword);
     162        torture_suite_add_simple_test(suite, "reg_val_data_string dword_big_endian",
     163                                      test_reg_val_data_string_dword_big_endian);
     164        torture_suite_add_simple_test(suite, "reg_val_data_string qword",
     165                                      test_reg_val_data_string_qword);
    122166        torture_suite_add_simple_test(suite, "reg_val_data_string sz",
    123167                                      test_reg_val_data_string_sz);
  • vendor/current/source4/lib/registry/tests/hive.c

    r414 r740  
    3333{
    3434        const struct hive_key *root = (const struct hive_key *)test_data;
    35         WERROR error = hive_key_del(root, "bla");
     35        WERROR error = hive_key_del(tctx, root, "bla");
    3636        torture_assert_werr_equal(tctx, error, WERR_BADFILE,
    3737                                  "invalid return code");
     
    7070        WERROR error;
    7171        struct hive_key *subkey;
    72         char data[4];
    73         SIVAL(data, 0, 42);
     72        uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     73        DATA_BLOB db = { d, 4 };
    7474
    7575        error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
     
    7777        torture_assert_werr_ok(tctx, error, "hive_key_add_name");
    7878
    79         error = hive_key_set_value(root, "Answer", REG_DWORD,
    80                                data_blob_talloc(tctx, data, sizeof(data)));
     79        error = hive_key_set_value(root, "Answer", REG_DWORD, db);
    8180        torture_assert_werr_ok(tctx, error, "hive_key_set_value");
    8281
     
    108107        torture_assert_werr_ok(tctx, error, "hive_key_add_name");
    109108
    110         error = hive_key_del(root, "Nested Key");
     109        error = hive_key_del(mem_ctx, root, "Nested Key");
    111110        torture_assert_werr_ok(tctx, error, "reg_key_del");
    112111
     
    122121        const struct hive_key *root = (const struct hive_key *)test_data;
    123122        TALLOC_CTX *mem_ctx = tctx;
    124         char data[4];
    125         SIVAL(data, 0, 42);
     123        uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     124        DATA_BLOB db = { d, 4 };
    126125
    127126        /* Create a new key under the root */
     
    136135
    137136        /* Create a new value under "Child Key" */
    138         error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD,
    139                                data_blob_talloc(mem_ctx, data, sizeof(data)));
     137        error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD, db);
    140138        torture_assert_werr_ok(tctx, error, "hive_key_set_value");
    141139
    142140        /* Deleting "Parent Key" will also delete "Child Key" and the value. */
    143         error = hive_key_del(root, "Parent Key");
     141        error = hive_key_del(mem_ctx, root, "Parent Key");
    144142        torture_assert_werr_ok(tctx, error, "hive_key_del");
    145143
     
    167165        torture_assert_werr_ok(tctx, error, "hive_key_add_name");
    168166
    169         error = hive_key_del(root, "Nested Key");
     167        error = hive_key_del(mem_ctx, root, "Nested Key");
    170168        torture_assert_werr_ok(tctx, error, "reg_key_del");
    171169
    172         error = hive_key_del(root, "Nested Key");
     170        error = hive_key_del(mem_ctx, root, "Nested Key");
    173171        torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");
    174172
     
    183181        const struct hive_key *root = (const struct hive_key *)test_data;
    184182        TALLOC_CTX *mem_ctx = tctx;
    185         char data[4];
    186         SIVAL(data, 0, 42);
     183        uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     184        DATA_BLOB db = { d, 4 };
    187185
    188186        error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
     
    190188        torture_assert_werr_ok(tctx, error, "hive_key_add_name");
    191189
    192         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
    193                                data_blob_talloc(mem_ctx, data, sizeof(data)));
     190        error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
    194191        torture_assert_werr_ok(tctx, error, "hive_key_set_value");
    195192
     
    203200        const struct hive_key *root = (const struct hive_key *)test_data;
    204201        TALLOC_CTX *mem_ctx = tctx;
    205         char data[4];
    206202        uint32_t type;
    207         DATA_BLOB value;
    208 
    209         SIVAL(data, 0, 42);
     203        uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     204        DATA_BLOB db = { d, 4 }, data;
    210205
    211206        error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
     
    213208        torture_assert_werr_ok(tctx, error, "hive_key_add_name");
    214209
    215         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
     210        error = hive_get_value(mem_ctx, subkey, "Answer", &type, &data);
    216211        torture_assert_werr_equal(tctx, error, WERR_BADFILE,
    217212                                  "getting missing value");
    218213
    219         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
    220                                data_blob_talloc(mem_ctx, data, sizeof(data)));
    221         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
    222 
    223         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
     214        error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     215        torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     216
     217        error = hive_get_value(mem_ctx, subkey, "Answer", &type, &data);
    224218        torture_assert_werr_ok(tctx, error, "getting value");
    225219
    226         torture_assert_int_equal(tctx, value.length, 4, "value length");
     220        torture_assert_int_equal(tctx, data.length, 4, "value length");
    227221        torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
    228222
    229         torture_assert_mem_equal(tctx, &data, value.data, sizeof(uint32_t),
    230                                  "value data");
     223        torture_assert_mem_equal(tctx, data.data, db.data, 4, "value data");
    231224
    232225        return true;
     
    239232        const struct hive_key *root = (const struct hive_key *)test_data;
    240233        TALLOC_CTX *mem_ctx = tctx;
    241         char data[4];
    242234        uint32_t type;
    243         DATA_BLOB value;
    244 
    245         SIVAL(data, 0, 42);
     235        uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     236        DATA_BLOB db = { d, 4 };
    246237
    247238        error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
     
    249240        torture_assert_werr_ok(tctx, error, "hive_key_add_name");
    250241
    251         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
    252                                data_blob_talloc(mem_ctx, data, sizeof(data)));
    253         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
    254 
    255         error = hive_key_del_value(subkey, "Answer");
     242        error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     243        torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     244
     245        error = hive_key_del_value(mem_ctx, subkey, "Answer");
    256246        torture_assert_werr_ok(tctx, error, "deleting value");
    257247
    258         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
     248        error = hive_get_value(mem_ctx, subkey, "Answer", &type, &db);
    259249        torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");
    260250
    261         error = hive_key_del_value(subkey, "Answer");
     251        error = hive_key_del_value(mem_ctx, subkey, "Answer");
    262252        torture_assert_werr_equal(tctx, error, WERR_BADFILE,
    263253                                  "deleting value");
     
    273263        const struct hive_key *root = (const struct hive_key *)test_data;
    274264        TALLOC_CTX *mem_ctx = tctx;
    275         char data[4];
    276265        uint32_t type;
    277         DATA_BLOB value;
     266        uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     267        DATA_BLOB db = { d, 4 }, data;
    278268        const char *name;
    279         int data_val = 42;
    280         SIVAL(data, 0, data_val);
    281269
    282270        error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
     
    284272        torture_assert_werr_ok(tctx, error, "hive_key_add_name");
    285273
    286         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
    287                                data_blob_talloc(mem_ctx, data, sizeof(data)));
     274        error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
    288275        torture_assert_werr_ok(tctx, error, "hive_key_set_value");
    289276
    290277        error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
    291                                         &type, &value);
     278                                        &type, &data);
    292279        torture_assert_werr_ok(tctx, error, "getting value");
    293280
    294281        torture_assert_str_equal(tctx, name, "Answer", "value name");
    295282
    296         torture_assert_int_equal(tctx, value.length, 4, "value length");
     283        torture_assert_int_equal(tctx, data.length, 4, "value length");
    297284        torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
    298285       
     286        torture_assert_mem_equal(tctx, data.data, db.data, 4, "value data");
    299287       
    300         torture_assert_int_equal(tctx, data_val, IVAL(value.data, 0), "value data");
    301 
    302288        error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
    303                                         &type, &value);
     289                                        &type, &data);
    304290        torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
    305291                                  "getting missing value");
     
    450436        rmdir(dirname);
    451437
    452         error = reg_create_regf_file(tctx, lp_iconv_convenience(tctx->lp_ctx),
    453                                      dirname, 5, &key);
     438        error = reg_create_regf_file(tctx, dirname, 5, &key);
    454439        if (!W_ERROR_IS_OK(error)) {
    455440                fprintf(stderr, "Unable to create new regf file\n");
     
    473458{
    474459        struct torture_tcase *tcase;
    475         struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
     460        struct torture_suite *suite = torture_suite_create(mem_ctx, "hive");
    476461
    477462        torture_suite_add_simple_test(suite, "dir-refuses-null-location",
  • vendor/current/source4/lib/registry/tests/registry.c

    r414 r740  
    118118{
    119119        struct registry_context *rctx = (struct registry_context *)_data;
    120         struct registry_key *root, *newkey1, *newkey2;
     120        struct registry_key *root, *newkey;
    121121        WERROR error;
    122122
     
    125125                               "getting predefined key failed");
    126126
    127         error = reg_key_add_name(rctx, root, "Hamburg", NULL, NULL,
    128                                  &newkey1);
     127        error = reg_key_add_name(rctx, root, "Hamburg\\Hamburg", NULL, NULL,
     128                                 &newkey);
    129129        torture_assert_werr_ok(tctx, error, "Creating key return code");
    130         torture_assert(tctx, newkey1 != NULL, "Creating new key");
    131 
    132         error = reg_key_add_name(rctx, root, "Hamburg\\Hamburg", NULL, NULL,
    133                                  &newkey2);
    134         torture_assert_werr_ok(tctx, error, "Creating key return code");
    135         torture_assert(tctx, newkey2 != NULL, "Creating new key");
     130        torture_assert(tctx, newkey != NULL, "Creating new key");
    136131
    137132        return true;
     
    201196        torture_assert(tctx, newkey != NULL, "Creating new key");
    202197
    203         error = reg_key_del(root, "Polen");
     198        error = reg_key_del(tctx, root, "Polen");
    204199        torture_assert_werr_ok(tctx, error, "Delete key");
    205200
    206         error = reg_key_del(root, "Polen");
     201        error = reg_key_del(tctx, root, "Polen");
    207202        torture_assert_werr_equal(tctx, error, WERR_BADFILE,
    208203                                  "Delete missing key");
     
    465460        torture_assert_werr_ok (tctx, error, "setting value");
    466461
    467         error = reg_del_value(subkey, __FUNCTION__);
     462        error = reg_del_value(tctx, subkey, __FUNCTION__);
    468463        torture_assert_werr_ok (tctx, error, "unsetting value");
    469464
     
    585580{
    586581        struct torture_tcase *tcase;
    587         struct torture_suite *suite = torture_suite_create(mem_ctx, "REGISTRY");
     582        struct torture_suite *suite = torture_suite_create(mem_ctx, "registry");
    588583
    589584        tcase = torture_suite_add_tcase(suite, "local");
  • vendor/current/source4/lib/registry/tools/regdiff.c

    r414 r740  
    131131        poptFreeContext(pc);
    132132
    133         error = reg_dotreg_diff_save(ctx, outputfile, lp_iconv_convenience(cmdline_lp_ctx), &callbacks,
    134                                      &callback_data);
     133        error = reg_dotreg_diff_save(ctx, outputfile, &callbacks, &callback_data);
    135134        if (!W_ERROR_IS_OK(error)) {
    136135                fprintf(stderr, "Problem saving registry diff to '%s': %s\n",
  • vendor/current/source4/lib/registry/tools/regpatch.c

    r414 r740  
    6969        poptFreeContext(pc);
    7070
    71         reg_diff_apply(h, lp_iconv_convenience(cmdline_lp_ctx), patch);
     71        reg_diff_apply(h, patch);
    7272
    7373        return 0;
  • vendor/current/source4/lib/registry/tools/regshell.c

    r414 r740  
    2525#include "lib/events/events.h"
    2626#include "system/time.h"
    27 #include "lib/smbreadline/smbreadline.h"
     27#include "../libcli/smbreadline/smbreadline.h"
    2828#include "librpc/gen_ndr/ndr_security.h"
    2929#include "lib/registry/tools/common.h"
     
    126126                printf("Key Class: %s\n", classname);
    127127        last_mod = nt_time_to_unix(last_change);
    128         printf("Time Last Modified: %s\n", ctime(&last_mod));
     128        printf("Time Last Modified: %s", ctime(&last_mod));
    129129        printf("Number of subkeys: %d\n", num_subkeys);
    130130        printf("Number of values: %d\n", num_values);
     
    141141        error = reg_get_sec_desc(ctx, ctx->current, &sec_desc);
    142142        if (!W_ERROR_IS_OK(error)) {
    143                 printf("Error getting security descriptor\n");
    144                 return error;
     143                printf("Error getting security descriptor: %s\n", win_errstr(error));
     144                return WERR_OK;
    145145        }
    146146        ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor,
     
    196196        }
    197197
    198         if (!reg_string_to_val(ctx, lp_iconv_convenience(cmdline_lp_ctx),
    199                                argv[2], argv[3], &val.data_type,
    200                                &val.data)) {
     198        if (!reg_string_to_val(ctx, argv[2], argv[3], &val.data_type, &val.data)) {
    201199                fprintf(stderr, "Unable to interpret data\n");
    202200                return WERR_INVALID_PARAM;
     
    260258
    261259        printf("%s\n%s\n", str_regtype(value_type),
    262                    reg_val_data_string(ctx, lp_iconv_convenience(cmdline_lp_ctx), value_type, value_data));
     260                   reg_val_data_string(ctx, value_type, value_data));
    263261
    264262        return WERR_OK;
     
    267265static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
    268266{
    269         int i;
     267        unsigned int i;
    270268        WERROR error;
    271269        uint32_t valuetype;
     
    283281
    284282        if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
    285                 fprintf(stderr, "Error occured while browsing thru keys: %s\n",
     283                fprintf(stderr, "Error occurred while browsing through keys: %s\n",
    286284                        win_errstr(error));
    287285                return error;
     
    291289                ctx->current, i, &name, &valuetype, &valuedata)); i++)
    292290                printf("V \"%s\" %s %s\n", name, str_regtype(valuetype),
    293                            reg_val_data_string(ctx, lp_iconv_convenience(cmdline_lp_ctx), valuetype, valuedata));
     291                           reg_val_data_string(ctx, valuetype, valuedata));
    294292
    295293        return WERR_OK;
     
    326324        }
    327325
    328         error = reg_key_del(ctx->current, argv[1]);
     326        error = reg_key_del(ctx, ctx->current, argv[1]);
    329327        if(!W_ERROR_IS_OK(error)) {
    330328                fprintf(stderr, "Error deleting '%s'\n", argv[1]);
     
    346344        }
    347345
    348         error = reg_del_value(ctx->current, argv[1]);
     346        error = reg_del_value(ctx, ctx->current, argv[1]);
    349347        if(!W_ERROR_IS_OK(error)) {
    350348                fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
     
    389387                       int argc, char **argv)
    390388{
    391         int i;
     389        unsigned int i;
    392390        printf("Available commands:\n");
    393391        for(i = 0; regshell_cmds[i].name; i++) {
     
    430428        /* Complete command */
    431429        char **matches;
    432         int i, len, samelen=0, count=1;
     430        size_t len, samelen=0;
     431        unsigned int i, count=1;
    433432
    434433        matches = malloc_array_p(char *, MAX_COMPLETIONS);
     
    478477        struct registry_key *base;
    479478        const char *subkeyname;
    480         int i, j = 1;
    481         int samelen = 0;
    482         int len;
     479        unsigned int i, j = 1;
     480        size_t len, samelen = 0;
    483481        char **matches;
    484482        const char *base_n = "";
     
    594592
    595593        if (ctx->current == NULL) {
    596                 int i;
     594                unsigned int i;
    597595
    598596                for (i = 0; (reg_predefined_keys[i].handle != 0) &&
     
    617615        if (ctx->current == NULL) {
    618616                fprintf(stderr, "Unable to access any of the predefined keys\n");
    619                 return -1;
     617                return 1;
    620618        }
    621619
     
    625623                char *line, *prompt;
    626624
    627                 asprintf(&prompt, "%s\\%s> ", ctx->predef?ctx->predef:"", ctx->path);
     625                if (asprintf(&prompt, "%s\\%s> ", ctx->predef?ctx->predef:"",
     626                             ctx->path) < 0) {
     627                        ret = false;
     628                        break;
     629                }
    628630
    629631                current_key = ctx->current;             /* No way to pass a void * pointer
  • vendor/current/source4/lib/registry/tools/regtree.c

    r414 r740  
    3434 * @param novals Whether values should not be printed
    3535 */
    36 static void print_tree(int level, struct registry_key *p,
     36static void print_tree(unsigned int level, struct registry_key *p,
    3737                       const char *name,
    3838                       bool fullpath, bool novals)
     
    4444        struct security_descriptor *sec_desc;
    4545        WERROR error;
    46         int i;
     46        unsigned int i;
    4747        TALLOC_CTX *mem_ctx;
    4848
     
    7070
    7171        if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
    72                 DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n",
     72                DEBUG(0, ("Error occurred while fetching subkeys for '%s': %s\n",
    7373                                  name, win_errstr(error)));
    7474        }
     
    7979                        mem_ctx, p, i, &valuename, &valuetype, &valuedata));
    8080                        i++) {
    81                         int j;
     81                        unsigned int j;
    8282                        for(j = 0; j < level+1; j++) putchar(' ');
    8383                        printf("%s\n",  reg_val_description(mem_ctx,
    84                                 lp_iconv_convenience(cmdline_lp_ctx), valuename,
    85                                 valuetype, valuedata));
     84                                valuename, valuetype, valuedata));
    8685                }
    8786                talloc_free(mem_ctx);
    8887
    8988                if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
    90                         DEBUG(0, ("Error occured while fetching values for '%s': %s\n",
     89                        DEBUG(0, ("Error occurred while fetching values for '%s': %s\n",
    9190                                name, win_errstr(error)));
    9291                }
     
    102101int main(int argc, char **argv)
    103102{
    104         int opt, i;
     103        int opt;
     104        unsigned int i;
    105105        const char *file = NULL;
    106106        const char *remote = NULL;
  • vendor/current/source4/lib/registry/util.c

    r414 r740  
    33   Transparent registry backend handling
    44   Copyright (C) Jelmer Vernooij                        2003-2007.
     5   Copyright (C) Wilco Baan Hofman                      2010.
    56
    67   This program is free software; you can redistribute it and/or modify
     
    2122#include "lib/registry/registry.h"
    2223#include "librpc/gen_ndr/winreg.h"
    23 
    24 /**
    25  * @file
    26  * @brief Registry utility functions
    27  */
    28 
    29 static const struct {
    30         uint32_t id;
    31         const char *name;
    32 } reg_value_types[] = {
    33         { REG_SZ, "REG_SZ" },
    34         { REG_DWORD, "REG_DWORD" },
    35         { REG_BINARY, "REG_BINARY" },
    36         { REG_EXPAND_SZ, "REG_EXPAND_SZ" },
    37         { REG_NONE, "REG_NONE" },
    38         { 0, NULL }
    39 };
    40 
    41 /** Return string description of registry value type */
    42 _PUBLIC_ const char *str_regtype(int type)
    43 {
    44         int i;
    45         for (i = 0; reg_value_types[i].name; i++) {
    46                 if (reg_value_types[i].id == type)
    47                         return reg_value_types[i].name;
    48         }
    49 
    50         return "Unknown";
    51 }
    52 
    53 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx,
    54                                    struct smb_iconv_convenience *iconv_convenience,
    55                                    uint32_t type,
     24#include "lib/util/data_blob.h"
     25
     26_PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx, uint32_t type,
    5627                                   const DATA_BLOB data)
    5728{
     
    6435                case REG_EXPAND_SZ:
    6536                case REG_SZ:
    66                         convert_string_talloc_convenience(mem_ctx, iconv_convenience, CH_UTF16, CH_UNIX,
     37                        convert_string_talloc(mem_ctx,
     38                                              CH_UTF16, CH_UNIX,
    6739                                              data.data, data.length,
    6840                                              (void **)&ret, NULL, false);
    69                         return ret;
     41                        break;
     42                case REG_DWORD:
     43                case REG_DWORD_BIG_ENDIAN:
     44                        SMB_ASSERT(data.length == sizeof(uint32_t));
     45                        ret = talloc_asprintf(mem_ctx, "0x%8.8x",
     46                                              IVAL(data.data, 0));
     47                        break;
     48                case REG_QWORD:
     49                        SMB_ASSERT(data.length == sizeof(uint64_t));
     50                        ret = talloc_asprintf(mem_ctx, "0x%16.16llx",
     51                                              (long long)BVAL(data.data, 0));
     52                        break;
    7053                case REG_BINARY:
    71                         ret = data_blob_hex_string(mem_ctx, &data);
    72                         return ret;
    73                 case REG_DWORD:
    74                         if (*(int *)data.data == 0)
    75                                 return talloc_strdup(mem_ctx, "0");
    76                         return talloc_asprintf(mem_ctx, "0x%x",
    77                                                *(int *)data.data);
     54                        ret = data_blob_hex_string_upper(mem_ctx, &data);
     55                        break;
     56                case REG_NONE:
     57                        /* "NULL" is the right return value */
     58                        break;
    7859                case REG_MULTI_SZ:
     60                        /* FIXME: We don't support this yet */
     61                        break;
     62                default:
    7963                        /* FIXME */
    80                         break;
    81                 default:
     64                        /* Other datatypes aren't supported -> return "NULL" */
    8265                        break;
    8366        }
     
    8770
    8871/** Generate a string that describes a registry value */
    89 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx,
    90                                    struct smb_iconv_convenience *iconv_convenience,
     72_PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx,
    9173                                   const char *name,
    9274                                   uint32_t data_type,
     
    9577        return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
    9678                               str_regtype(data_type),
    97                                reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
    98 }
    99 
    100 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx,
    101                                 struct smb_iconv_convenience *iconv_convenience,
    102                                 const char *type_str,
    103                                 const char *data_str, uint32_t *type,
    104                                 DATA_BLOB *data)
    105 {
    106         int i;
    107         *type = -1;
    108 
    109         /* Find the correct type */
    110         for (i = 0; reg_value_types[i].name; i++) {
    111                 if (!strcmp(reg_value_types[i].name, type_str)) {
    112                         *type = reg_value_types[i].id;
    113                         break;
     79                               reg_val_data_string(mem_ctx, data_type, data));
     80}
     81
     82/*
     83 * This implements reading hex bytes that include comma's.
     84 * It was previously handled by strhex_to_data_blob, but that did not cover
     85 * the format used by windows.
     86 */
     87static DATA_BLOB reg_strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *str)
     88{
     89        DATA_BLOB ret;
     90        const char *HEXCHARS = "0123456789ABCDEF";
     91        size_t i, j;
     92        char *hi, *lo;
     93
     94        ret = data_blob_talloc_zero(mem_ctx, (strlen(str)+(strlen(str) % 3))/3);
     95        j = 0;
     96        for (i = 0; i < strlen(str); i++) {
     97                hi = strchr(HEXCHARS, toupper(str[i]));
     98                if (hi == NULL)
     99                        continue;
     100
     101                i++;
     102                lo = strchr(HEXCHARS, toupper(str[i]));
     103                if (lo == NULL)
     104                        break;
     105
     106                ret.data[j] = PTR_DIFF(hi, HEXCHARS) << 4;
     107                ret.data[j] += PTR_DIFF(lo, HEXCHARS);
     108                j++;
     109
     110                if (j > ret.length) {
     111                        DEBUG(0, ("Trouble converting hex string to bin\n"));
     112                        break;
     113                }
     114        }
     115        return ret;
     116}
     117
     118
     119_PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str,
     120                                const char *data_str, uint32_t *type, DATA_BLOB *data)
     121{
     122        char *tmp_type_str, *p, *q;
     123        int result;
     124
     125        *type = regtype_by_string(type_str);
     126
     127        if (*type == -1) {
     128                /* Normal windows format is hex, hex(type int as string),
     129                   dword or just a string. */
     130                if (strncmp(type_str, "hex(", 4) == 0) {
     131                        /* there is a hex string with the value type between
     132                           the braces */
     133                        tmp_type_str = talloc_strdup(mem_ctx, type_str);
     134                        q = p = tmp_type_str + strlen("hex(");
     135
     136                        /* Go to the closing brace or end of the string */
     137                        while (*q != ')' && *q != '\0') q++;
     138                        *q = '\0';
     139
     140                        /* Convert hex string to int, store it in type */
     141                        result = sscanf(p, "%x", type);
     142                        if (!result) {
     143                                DEBUG(0, ("Could not convert hex to int\n"));
     144                                return false;
     145                        }
     146                        talloc_free(tmp_type_str);
     147                } else if (strcmp(type_str, "hex") == 0) {
     148                        *type = REG_BINARY;
     149                } else if (strcmp(type_str, "dword") == 0) {
     150                        *type = REG_DWORD;
    114151                }
    115152        }
     
    120157        /* Convert data appropriately */
    121158
    122         switch (*type)
    123         {
     159        switch (*type) {
    124160                case REG_SZ:
     161                        return convert_string_talloc(mem_ctx,
     162                                                     CH_UNIX, CH_UTF16,
     163                                                     data_str, strlen(data_str)+1,
     164                                                     (void **)&data->data,
     165                                                     &data->length, false);
     166                        break;
     167                case REG_MULTI_SZ:
    125168                case REG_EXPAND_SZ:
    126                 convert_string_talloc_convenience(mem_ctx, iconv_convenience, CH_UNIX, CH_UTF16,
    127                                                      data_str, strlen(data_str),
    128                                                      (void **)&data->data, &data->length, false);
    129                         break;
    130 
    131                 case REG_DWORD: {
    132                         uint32_t tmp = strtol(data_str, NULL, 0);
    133                         *data = data_blob_talloc(mem_ctx, &tmp, 4);
     169                case REG_BINARY:
     170                        *data = reg_strhex_to_data_blob(mem_ctx, data_str);
     171                        break;
     172                case REG_DWORD:
     173                case REG_DWORD_BIG_ENDIAN: {
     174                        uint32_t tmp = strtol(data_str, NULL, 16);
     175                        *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint32_t));
     176                        if (data->data == NULL) return false;
     177                        SIVAL(data->data, 0, tmp);
    134178                        }
    135179                        break;
    136 
     180                case REG_QWORD: {
     181                        uint64_t tmp = strtoll(data_str, NULL, 16);
     182                        *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint64_t));
     183                        if (data->data == NULL) return false;
     184                        SBVAL(data->data, 0, tmp);
     185                        }
     186                        break;
    137187                case REG_NONE:
    138188                        ZERO_STRUCTP(data);
    139189                        break;
    140 
    141                 case REG_BINARY:
    142                         *data = strhex_to_data_blob(mem_ctx, data_str);
    143                         break;
    144 
    145190                default:
    146191                        /* FIXME */
     192                        /* Other datatypes aren't supported -> return no success */
    147193                        return false;
    148194        }
     
    156202        struct registry_key *predef;
    157203        WERROR error;
    158         int predeflength;
     204        size_t predeflength;
    159205        char *predefname;
    160206
     
    165211
    166212        predefname = talloc_strndup(mem_ctx, name, predeflength);
     213        W_ERROR_HAVE_NO_MEMORY(predefname);
    167214        error = reg_get_predefined_key_by_name(handle, predefname, &predef);
    168215        talloc_free(predefname);
     
    193240
    194241        parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
    195 
     242        W_ERROR_HAVE_NO_MEMORY(parent_name);
    196243        error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
     244        talloc_free(parent_name);
    197245        if (!W_ERROR_IS_OK(error)) {
    198246                return error;
     
    200248
    201249        *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
     250        W_ERROR_HAVE_NO_MEMORY(*name);
    202251
    203252        return WERR_OK;
     
    217266        error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
    218267        if (W_ERROR_IS_OK(error)) {
    219                 error = reg_key_del(parent, n);
     268                error = reg_key_del(mem_ctx, parent, n);
    220269        }
    221270
     
    233282        const char *n;
    234283        WERROR error;
     284
     285        *result = NULL;
    235286
    236287        if (!strchr(path, '\\')) {
Note: See TracChangeset for help on using the changeset viewer.