Ignore:
Timestamp:
Nov 27, 2012, 4:43:17 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: updated trunk to 3.6.0

Location:
trunk/server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/server

  • trunk/server/source3/utils/net_registry.c

    r414 r745  
    2121
    2222#include "includes.h"
     23#include "registry.h"
     24#include "registry/reg_api.h"
     25#include "registry/reg_util_token.h"
     26#include "registry/reg_init_basic.h"
    2327#include "utils/net.h"
    2428#include "utils/net_registry_util.h"
    25 
     29#include "include/g_lock.h"
     30#include "registry/reg_backend_db.h"
     31#include "registry/reg_import.h"
     32#include "registry/reg_format.h"
     33#include <assert.h>
     34#include "../libcli/security/display_sec.h"
     35#include "../libcli/security/sddl.h"
     36#include "../libcli/registry/util_reg.h"
     37#include "passdb/machine_sid.h"
    2638
    2739/*
     
    4052{
    4153        WERROR werr;
    42         NT_USER_TOKEN *token = NULL;
     54        struct security_token *token = NULL;
    4355        char *hivename = NULL;
    4456        char *tmp_subkeyname = NULL;
     
    111123}
    112124
    113 /*
    114  *
    115  * the main "net registry" function implementations
    116  *
    117  */
    118 
    119 static int net_registry_enumerate(struct net_context *c, int argc,
    120                                   const char **argv)
    121 {
    122         WERROR werr;
    123         struct registry_key *key = NULL;
    124         TALLOC_CTX *ctx = talloc_stackframe();
    125         char *subkey_name;
     125static WERROR registry_enumkey(struct registry_key* parent, const char* keyname, bool recursive)
     126{
     127        WERROR werr;
     128        TALLOC_CTX *ctx = talloc_stackframe();
     129        char*  subkey_name;
    126130        NTTIME modtime;
    127131        uint32_t count;
    128         char *valname = NULL;
     132        char* valname = NULL;
    129133        struct registry_value *valvalue = NULL;
    130         int ret = -1;
    131 
    132         if (argc != 1 || c->display_usage) {
    133                 d_printf("%s\n%s",
    134                          _("Usage:"),
    135                          _("net registry enumerate <path>\n"));
    136                 d_printf("%s\n%s",
    137                          _("Example:"),
    138                          _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
    139                 goto done;
    140         }
    141 
    142         werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
    143         if (!W_ERROR_IS_OK(werr)) {
    144                 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
    145                 goto done;
    146         }
    147 
    148         for (count = 0;
    149              werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
    150              W_ERROR_IS_OK(werr);
    151              count++)
    152         {
    153                 print_registry_key(subkey_name, &modtime);
    154         }
    155         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
    156                 goto done;
     134        struct registry_key* key = NULL;
     135
     136        werr = reg_openkey(ctx, parent, keyname, REG_KEY_READ, &key);
     137        if (!W_ERROR_IS_OK(werr)) {
     138                goto done;
     139        }
     140
     141        if (recursive) {
     142                printf("[%s]\n\n", key->key->name);
     143        } else {
     144                for (count = 0;
     145                     werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
     146                     W_ERROR_IS_OK(werr);
     147                     count++)
     148                {
     149                        print_registry_key(subkey_name, &modtime);
     150                }
     151                if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
     152                        goto done;
     153                }
    157154        }
    158155
     
    168165        }
    169166
    170         ret = 0;
    171 done:
    172         TALLOC_FREE(ctx);
    173         return ret;
    174 }
     167        if (!recursive) {
     168                werr = WERR_OK;
     169                goto done;
     170        }
     171
     172        for (count = 0;
     173             werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
     174             W_ERROR_IS_OK(werr);
     175             count++)
     176        {
     177                werr = registry_enumkey(key, subkey_name, recursive);
     178                if (!W_ERROR_IS_OK(werr)) {
     179                        goto done;
     180                }
     181        }
     182        if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
     183                goto done;
     184        }
     185
     186        werr = WERR_OK;
     187
     188done:
     189        TALLOC_FREE(ctx);
     190        return werr;
     191}
     192
     193
     194
     195/*
     196 *
     197 * the main "net registry" function implementations
     198 *
     199 */
     200static int net_registry_enumerate(struct net_context *c, int argc,
     201                                  const char **argv)
     202{
     203        WERROR werr;
     204        struct registry_key *key = NULL;
     205        char* name = NULL;
     206        TALLOC_CTX *ctx = talloc_stackframe();
     207        int ret = -1;
     208
     209        if (argc != 1 || c->display_usage) {
     210                d_printf("%s\n%s",
     211                         _("Usage:"),
     212                         _("net registry enumerate <path>\n"));
     213                d_printf("%s\n%s",
     214                         _("Example:"),
     215                         _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
     216                goto done;
     217        }
     218
     219        werr = open_hive(ctx, argv[0], REG_KEY_READ, &key, &name);
     220        if (!W_ERROR_IS_OK(werr)) {
     221                d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
     222                goto done;
     223        }
     224
     225        werr = registry_enumkey(key, name, c->opt_reboot);
     226        if (W_ERROR_IS_OK(werr)) {
     227                ret = 0;
     228        }
     229done:
     230        TALLOC_FREE(ctx);
     231        return ret;
     232}
     233
     234static int net_registry_enumerate_recursive(struct net_context *c, int argc,
     235                                            const char **argv)
     236{
     237        WERROR werr;
     238        struct registry_key *key = NULL;
     239        char* name = NULL;
     240        TALLOC_CTX *ctx = talloc_stackframe();
     241        int ret = -1;
     242
     243        if (argc != 1 || c->display_usage) {
     244                d_printf("%s\n%s",
     245                         _("Usage:"),
     246                         _("net registry enumerate <path>\n"));
     247                d_printf("%s\n%s",
     248                         _("Example:"),
     249                         _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
     250                goto done;
     251        }
     252
     253        werr = open_hive(ctx, argv[0], REG_KEY_READ, &key, &name);
     254        if (!W_ERROR_IS_OK(werr)) {
     255                d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
     256                goto done;
     257        }
     258
     259        werr = registry_enumkey(key, name, true);
     260        if (W_ERROR_IS_OK(werr)) {
     261                ret = 0;
     262        }
     263done:
     264        TALLOC_FREE(ctx);
     265        return ret;
     266}
     267
    175268
    176269static int net_registry_createkey(struct net_context *c, int argc,
     
    233326}
    234327
    235 static int net_registry_deletekey(struct net_context *c, int argc,
    236                                   const char **argv)
     328static int net_registry_deletekey_internal(struct net_context *c, int argc,
     329                                           const char **argv,
     330                                           bool recursive)
    237331{
    238332        WERROR werr;
     
    264358        }
    265359
    266         werr = reg_deletekey(hivekey, subkeyname);
    267         if (!W_ERROR_IS_OK(werr)) {
     360        if (recursive) {
     361                werr = reg_deletekey_recursive(hivekey, subkeyname);
     362        } else {
     363                werr = reg_deletekey(hivekey, subkeyname);
     364        }
     365        if (!W_ERROR_IS_OK(werr) &&
     366            !(c->opt_force && W_ERROR_EQUAL(werr, WERR_BADFILE)))
     367        {
    268368                d_fprintf(stderr, "reg_deletekey %s: %s\n", _("failed"),
    269369                          win_errstr(werr));
     
    276376        TALLOC_FREE(ctx);
    277377        return ret;
     378}
     379
     380static int net_registry_deletekey(struct net_context *c, int argc,
     381                                  const char **argv)
     382{
     383        return net_registry_deletekey_internal(c, argc, argv, false);
     384}
     385
     386static int net_registry_deletekey_recursive(struct net_context *c, int argc,
     387                                            const char **argv)
     388{
     389        return net_registry_deletekey_internal(c, argc, argv, true);
    278390}
    279391
     
    290402                d_fprintf(stderr, "%s\n%s",
    291403                          _("Usage:"),
    292                           _("net rpc registry getvalue <key> <valuename>\n"));
     404                          _("net registry getvalue <key> <valuename>\n"));
    293405                goto done;
    294406        }
     
    326438{
    327439        return net_registry_getvalue_internal(c, argc, argv, true);
     440}
     441
     442static int net_registry_getvaluesraw(struct net_context *c, int argc,
     443                                     const char **argv)
     444{
     445        WERROR werr;
     446        int ret = -1;
     447        struct registry_key *key = NULL;
     448        TALLOC_CTX *ctx = talloc_stackframe();
     449        uint32_t idx;
     450
     451        if (argc != 1 || c->display_usage) {
     452                d_fprintf(stderr, "usage: net rpc registry getvaluesraw "
     453                          "<key>\n");
     454                goto done;
     455        }
     456
     457        werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
     458        if (!W_ERROR_IS_OK(werr)) {
     459                d_fprintf(stderr, "open_key failed: %s\n", win_errstr(werr));
     460                goto done;
     461        }
     462
     463        idx = 0;
     464        while (true) {
     465                struct registry_value *val;
     466
     467                werr = reg_enumvalue(talloc_tos(), key, idx, NULL, &val);
     468
     469                if (W_ERROR_EQUAL(werr, WERR_NO_MORE_ITEMS)) {
     470                        ret = 0;
     471                        break;
     472                }
     473                if (!W_ERROR_IS_OK(werr)) {
     474                        break;
     475                }
     476                print_registry_value(val, true);
     477                TALLOC_FREE(val);
     478                idx += 1;
     479        }
     480done:
     481        TALLOC_FREE(ctx);
     482        return ret;
    328483}
    329484
     
    340495                d_fprintf(stderr, "%s\n%s",
    341496                          _("Usage:"),
    342                           _("net rpc registry setvalue <key> <valuename> "
     497                          _("net registry setvalue <key> <valuename> "
    343498                            "<type> [<val>]+\n"));
    344499                goto done;
     
    351506
    352507        if (strequal(argv[2], "dword")) {
     508                uint32_t v = strtoul(argv[3], NULL, 10);
    353509                value.type = REG_DWORD;
    354                 value.v.dword = strtoul(argv[3], NULL, 10);
     510                value.data = data_blob_talloc(ctx, NULL, 4);
     511                SIVAL(value.data.data, 0, v);
    355512        } else if (strequal(argv[2], "sz")) {
    356513                value.type = REG_SZ;
    357                 value.v.sz.len = strlen(argv[3])+1;
    358                 value.v.sz.str = CONST_DISCARD(char *, argv[3]);
     514                if (!push_reg_sz(ctx, &value.data, argv[3])) {
     515                        goto done;
     516                }
    359517        } else if (strequal(argv[2], "multi_sz")) {
     518                const char **array;
     519                int count = argc - 3;
     520                int i;
    360521                value.type = REG_MULTI_SZ;
    361                 value.v.multi_sz.num_strings = argc - 3;
    362                 value.v.multi_sz.strings = (char **)(argv + 3);
     522                array = talloc_zero_array(ctx, const char *, count + 1);
     523                if (array == NULL) {
     524                        goto done;
     525                }
     526                for (i=0; i < count; i++) {
     527                        array[i] = talloc_strdup(array, argv[count+i]);
     528                        if (array[i] == NULL) {
     529                                goto done;
     530                        }
     531                }
     532                if (!push_reg_multi_sz(ctx, &value.data, array)) {
     533                        goto done;
     534                }
    363535        } else {
    364536                d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
     
    383555done:
    384556        TALLOC_FREE(ctx);
     557        return ret;
     558}
     559
     560struct net_registry_increment_state {
     561        const char *keyname;
     562        const char *valuename;
     563        uint32_t increment;
     564        uint32_t newvalue;
     565        WERROR werr;
     566};
     567
     568static void net_registry_increment_fn(void *private_data)
     569{
     570        struct net_registry_increment_state *state =
     571                (struct net_registry_increment_state *)private_data;
     572        struct registry_value *value;
     573        struct registry_key *key = NULL;
     574        uint32_t v;
     575
     576        state->werr = open_key(talloc_tos(), state->keyname,
     577                               REG_KEY_READ|REG_KEY_WRITE, &key);
     578        if (!W_ERROR_IS_OK(state->werr)) {
     579                d_fprintf(stderr, _("open_key failed: %s\n"),
     580                          win_errstr(state->werr));
     581                goto done;
     582        }
     583
     584        state->werr = reg_queryvalue(key, key, state->valuename, &value);
     585        if (!W_ERROR_IS_OK(state->werr)) {
     586                d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
     587                          win_errstr(state->werr));
     588                goto done;
     589        }
     590
     591        if (value->type != REG_DWORD) {
     592                d_fprintf(stderr, _("value not a DWORD: %s\n"),
     593                          str_regtype(value->type));
     594                goto done;
     595        }
     596
     597        if (value->data.length < 4) {
     598                d_fprintf(stderr, _("value too short for regular DWORD\n"));
     599                goto done;
     600        }
     601
     602        v = IVAL(value->data.data, 0);
     603        v += state->increment;
     604        state->newvalue = v;
     605
     606        SIVAL(value->data.data, 0, v);
     607
     608        state->werr = reg_setvalue(key, state->valuename, value);
     609        if (!W_ERROR_IS_OK(state->werr)) {
     610                d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
     611                          win_errstr(state->werr));
     612                goto done;
     613        }
     614
     615done:
     616        TALLOC_FREE(key);
     617        return;
     618}
     619
     620static int net_registry_increment(struct net_context *c, int argc,
     621                                  const char **argv)
     622{
     623        struct net_registry_increment_state state;
     624        NTSTATUS status;
     625        int ret = -1;
     626
     627        if (argc < 2 || c->display_usage) {
     628                d_fprintf(stderr, "%s\n%s",
     629                          _("Usage:"),
     630                          _("net registry increment <key> <valuename> "
     631                            "[<increment>]\n"));
     632                goto done;
     633        }
     634
     635        state.keyname = argv[0];
     636        state.valuename = argv[1];
     637
     638        state.increment = 1;
     639        if (argc == 3) {
     640                state.increment = strtoul(argv[2], NULL, 10);
     641        }
     642
     643        status = g_lock_do("registry_increment_lock", G_LOCK_WRITE,
     644                           timeval_set(600, 0), procid_self(),
     645                           net_registry_increment_fn, &state);
     646        if (!NT_STATUS_IS_OK(status)) {
     647                d_fprintf(stderr, _("g_lock_do failed: %s\n"),
     648                          nt_errstr(status));
     649                goto done;
     650        }
     651        if (!W_ERROR_IS_OK(state.werr)) {
     652                d_fprintf(stderr, _("increment failed: %s\n"),
     653                          win_errstr(state.werr));
     654                goto done;
     655        }
     656
     657        d_printf(_("%u\n"), (unsigned)state.newvalue);
     658
     659        ret = 0;
     660
     661done:
    385662        return ret;
    386663}
     
    397674                d_fprintf(stderr, "%s\n%s",
    398675                          _("Usage:"),
    399                           _("net rpc registry deletevalue <key> <valuename>\n"));
     676                          _("net registry deletevalue <key> <valuename>\n"));
    400677                goto done;
    401678        }
     
    409686        werr = reg_deletevalue(key, argv[1]);
    410687        if (!W_ERROR_IS_OK(werr)) {
    411                 d_fprintf(stderr, _("reg_deletekey failed: %s\n"),
     688                d_fprintf(stderr, _("reg_deletevalue failed: %s\n"),
    412689                          win_errstr(werr));
    413690                goto done;
     
    421698}
    422699
    423 static int net_registry_getsd(struct net_context *c, int argc,
    424                               const char **argv)
    425 {
    426         WERROR werr;
    427         int ret = -1;
     700static WERROR net_registry_getsd_internal(struct net_context *c,
     701                                          TALLOC_CTX *mem_ctx,
     702                                          const char *keyname,
     703                                          struct security_descriptor **sd)
     704{
     705        WERROR werr;
    428706        struct registry_key *key = NULL;
    429         struct security_descriptor *secdesc = NULL;
    430707        TALLOC_CTX *ctx = talloc_stackframe();
    431708        uint32_t access_mask = REG_KEY_READ |
     
    439716        access_mask = REG_KEY_READ;
    440717
     718        if (sd == NULL) {
     719                d_fprintf(stderr, _("internal error: invalid argument\n"));
     720                werr = WERR_INVALID_PARAM;
     721                goto done;
     722        }
     723
     724        if (strlen(keyname) == 0) {
     725                d_fprintf(stderr, _("error: zero length key name given\n"));
     726                werr = WERR_INVALID_PARAM;
     727                goto done;
     728        }
     729
     730        werr = open_key(ctx, keyname, access_mask, &key);
     731        if (!W_ERROR_IS_OK(werr)) {
     732                d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
     733                          win_errstr(werr));
     734                goto done;
     735        }
     736
     737        werr = reg_getkeysecurity(mem_ctx, key, sd);
     738        if (!W_ERROR_IS_OK(werr)) {
     739                d_fprintf(stderr, "%s%s\n", _("reg_getkeysecurity failed: "),
     740                          win_errstr(werr));
     741                goto done;
     742        }
     743
     744        werr = WERR_OK;
     745
     746done:
     747        TALLOC_FREE(ctx);
     748        return werr;
     749}
     750
     751static int net_registry_getsd(struct net_context *c, int argc,
     752                              const char **argv)
     753{
     754        WERROR werr;
     755        int ret = -1;
     756        struct security_descriptor *secdesc = NULL;
     757        TALLOC_CTX *ctx = talloc_stackframe();
     758
    441759        if (argc != 1 || c->display_usage) {
    442760                d_printf("%s\n%s",
     
    448766                goto done;
    449767        }
    450         if (strlen(argv[0]) == 0) {
    451                 d_fprintf(stderr, "error: zero length key name given\n");
    452                 goto done;
    453         }
    454 
    455         werr = open_key(ctx, argv[0], access_mask, &key);
     768
     769        werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
     770        if (!W_ERROR_IS_OK(werr)) {
     771                goto done;
     772        }
     773
     774        display_sec_desc(secdesc);
     775
     776        ret = 0;
     777
     778done:
     779        TALLOC_FREE(ctx);
     780        return ret;
     781}
     782
     783static int net_registry_getsd_sddl(struct net_context *c,
     784                                   int argc, const char **argv)
     785{
     786        WERROR werr;
     787        int ret = -1;
     788        struct security_descriptor *secdesc = NULL;
     789        TALLOC_CTX *ctx = talloc_stackframe();
     790
     791        if (argc != 1 || c->display_usage) {
     792                d_printf("%s\n%s",
     793                         _("Usage:"),
     794                         _("net registry getsd_sddl <path>\n"));
     795                d_printf("%s\n%s",
     796                         _("Example:"),
     797                         _("net registry getsd_sddl 'HKLM\\Software\\Samba'\n"));
     798                goto done;
     799        }
     800
     801        werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
     802        if (!W_ERROR_IS_OK(werr)) {
     803                goto done;
     804        }
     805
     806        d_printf("%s\n", sddl_encode(ctx, secdesc, get_global_sam_sid()));
     807
     808        ret = 0;
     809
     810done:
     811        TALLOC_FREE(ctx);
     812        return ret;
     813}
     814
     815static WERROR net_registry_setsd_internal(struct net_context *c,
     816                                          TALLOC_CTX *mem_ctx,
     817                                          const char *keyname,
     818                                          struct security_descriptor *sd)
     819{
     820        WERROR werr;
     821        struct registry_key *key = NULL;
     822        TALLOC_CTX *ctx = talloc_stackframe();
     823        uint32_t access_mask = REG_KEY_WRITE |
     824                               SEC_FLAG_MAXIMUM_ALLOWED |
     825                               SEC_FLAG_SYSTEM_SECURITY;
     826
     827        /*
     828         * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
     829         * is denied with these perms right now...
     830         */
     831        access_mask = REG_KEY_WRITE;
     832
     833        if (strlen(keyname) == 0) {
     834                d_fprintf(stderr, _("error: zero length key name given\n"));
     835                werr = WERR_INVALID_PARAM;
     836                goto done;
     837        }
     838
     839        werr = open_key(ctx, keyname, access_mask, &key);
     840        if (!W_ERROR_IS_OK(werr)) {
     841                d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
     842                          win_errstr(werr));
     843                goto done;
     844        }
     845
     846        werr = reg_setkeysecurity(key, sd);
     847        if (!W_ERROR_IS_OK(werr)) {
     848                d_fprintf(stderr, "%s%s\n", _("reg_setkeysecurity failed: "),
     849                          win_errstr(werr));
     850                goto done;
     851        }
     852
     853        werr = WERR_OK;
     854
     855done:
     856        TALLOC_FREE(ctx);
     857        return werr;
     858}
     859
     860static int net_registry_setsd_sddl(struct net_context *c,
     861                                   int argc, const char **argv)
     862{
     863        WERROR werr;
     864        int ret = -1;
     865        struct security_descriptor *secdesc = NULL;
     866        TALLOC_CTX *ctx = talloc_stackframe();
     867
     868        if (argc != 2 || c->display_usage) {
     869                d_printf("%s\n%s",
     870                         _("Usage:"),
     871                         _("net registry setsd_sddl <path> <security_descriptor>\n"));
     872                d_printf("%s\n%s",
     873                         _("Example:"),
     874                         _("net registry setsd_sddl 'HKLM\\Software\\Samba'\n"));
     875                goto done;
     876        }
     877
     878        secdesc = sddl_decode(ctx, argv[1], get_global_sam_sid());
     879        if (secdesc == NULL) {
     880                goto done;
     881        }
     882
     883        werr = net_registry_setsd_internal(c, ctx, argv[0], secdesc);
     884        if (!W_ERROR_IS_OK(werr)) {
     885                goto done;
     886        }
     887
     888        ret = 0;
     889
     890done:
     891        TALLOC_FREE(ctx);
     892        return ret;
     893}
     894
     895/******************************************************************************/
     896/**
     897 * @defgroup net_registry net registry
     898 */
     899
     900/**
     901 * @defgroup net_registry_import Import
     902 * @ingroup net_registry
     903 * @{
     904 */
     905
     906struct import_ctx {
     907        TALLOC_CTX *mem_ctx;
     908};
     909
     910
     911static WERROR import_create_key(struct import_ctx* ctx,
     912                                struct registry_key* parent,
     913                                const char* name, void** pkey, bool* existing)
     914{
     915        WERROR werr;
     916        void* mem_ctx = talloc_new(ctx->mem_ctx);
     917
     918        struct registry_key* key = NULL;
     919        enum winreg_CreateAction action;
     920
     921        if (parent == NULL) {
     922                char* subkeyname = NULL;
     923                werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
     924                         &parent, &subkeyname);
     925                if (!W_ERROR_IS_OK(werr)) {
     926                        d_fprintf(stderr, _("open_hive failed: %s\n"),
     927                                  win_errstr(werr));
     928                        goto done;
     929                }
     930                name = subkeyname;
     931        }
     932
     933        action = REG_ACTION_NONE;
     934        werr = reg_createkey(mem_ctx, parent, name, REG_KEY_WRITE,
     935                             &key, &action);
     936        if (!W_ERROR_IS_OK(werr)) {
     937                d_fprintf(stderr, _("reg_createkey failed: %s\n"),
     938                          win_errstr(werr));
     939                goto done;
     940        }
     941
     942        if (action == REG_ACTION_NONE) {
     943                d_fprintf(stderr, _("createkey did nothing -- huh?\n"));
     944                werr = WERR_CREATE_FAILED;
     945                goto done;
     946        }
     947
     948        if (existing != NULL) {
     949                *existing = (action == REG_OPENED_EXISTING_KEY);
     950        }
     951
     952        if (pkey!=NULL) {
     953                *pkey = talloc_steal(ctx->mem_ctx, key);
     954        }
     955
     956done:
     957        talloc_free(mem_ctx);
     958        return werr;
     959}
     960
     961static WERROR import_close_key(struct import_ctx* ctx,
     962                               struct registry_key* key)
     963{
     964        return WERR_OK;
     965}
     966
     967static WERROR import_delete_key(struct import_ctx* ctx,
     968                                struct registry_key* parent, const char* name)
     969{
     970        WERROR werr;
     971        void* mem_ctx = talloc_new(talloc_tos());
     972
     973        if (parent == NULL) {
     974                char* subkeyname = NULL;
     975                werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
     976                         &parent, &subkeyname);
     977                if (!W_ERROR_IS_OK(werr)) {
     978                        d_fprintf(stderr, _("open_hive failed: %s\n"),
     979                                  win_errstr(werr));
     980                        goto done;
     981                }
     982                name = subkeyname;
     983        }
     984
     985        werr = reg_deletekey_recursive(parent, name);
     986        if (!W_ERROR_IS_OK(werr)) {
     987                d_fprintf(stderr, "reg_deletekey_recursive %s: %s\n", _("failed"),
     988                          win_errstr(werr));
     989                goto done;
     990        }
     991
     992done:
     993        talloc_free(mem_ctx);
     994        return werr;
     995}
     996
     997static WERROR import_create_val (struct import_ctx* ctx,
     998                                 struct registry_key* parent, const char* name,
     999                                 const struct registry_value* value)
     1000{
     1001        WERROR werr;
     1002
     1003        if (parent == NULL) {
     1004                return WERR_INVALID_PARAM;
     1005        }
     1006
     1007        werr = reg_setvalue(parent, name, value);
     1008        if (!W_ERROR_IS_OK(werr)) {
     1009                d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
     1010                          win_errstr(werr));
     1011        }
     1012        return werr;
     1013}
     1014
     1015static WERROR import_delete_val (struct import_ctx* ctx, struct registry_key* parent, const char* name) {
     1016        WERROR werr;
     1017
     1018        if (parent == NULL) {
     1019                return WERR_INVALID_PARAM;
     1020        }
     1021
     1022        werr = reg_deletevalue(parent, name);
     1023        if (!W_ERROR_IS_OK(werr)) {
     1024                d_fprintf(stderr, _("reg_deletevalue failed: %s\n"),
     1025                          win_errstr(werr));
     1026        }
     1027
     1028        return werr;
     1029}
     1030
     1031
     1032static int net_registry_import(struct net_context *c, int argc,
     1033                               const char **argv)
     1034{
     1035        struct import_ctx import_ctx;
     1036        struct reg_import_callback import_callback = {
     1037                .openkey     = NULL,
     1038                .closekey    = (reg_import_callback_closekey_t)&import_close_key,
     1039                .createkey   = (reg_import_callback_createkey_t)&import_create_key,
     1040                .deletekey   = (reg_import_callback_deletekey_t)&import_delete_key,
     1041                .deleteval   = (reg_import_callback_deleteval_t)&import_delete_val,
     1042                .setval      = {
     1043                        .registry_value = (reg_import_callback_setval_registry_value_t)
     1044                                          &import_create_val,
     1045                },
     1046                .setval_type = REGISTRY_VALUE,
     1047                .data        = &import_ctx
     1048        };
     1049
     1050        int ret;
     1051
     1052        if (argc < 1 || argc > 2 || c->display_usage) {
     1053                d_printf("%s\n%s",
     1054                         _("Usage:"),
     1055                         _("net registry import <reg> [options]\n"));
     1056                d_printf("%s\n%s",
     1057                         _("Example:"),
     1058                         _("net registry import file.reg enc=CP1252\n"));
     1059                return -1;
     1060        }
     1061
     1062        ZERO_STRUCT(import_ctx);
     1063        import_ctx.mem_ctx = talloc_stackframe();
     1064
     1065        regdb_open();
     1066        regdb_transaction_start();
     1067
     1068        ret = reg_parse_file(argv[0],
     1069                             reg_import_adapter(import_ctx.mem_ctx,
     1070                                                import_callback),
     1071                             (argc > 1) ? argv[1] : NULL
     1072                );
     1073        if (ret < 0) {
     1074                d_printf("reg_parse_file failed: transaction canceled\n");
     1075                regdb_transaction_cancel();
     1076        } else{
     1077                regdb_transaction_commit();
     1078        }
     1079
     1080        regdb_close();
     1081        talloc_free(import_ctx.mem_ctx);
     1082
     1083        return ret;
     1084}
     1085/**@}*/
     1086
     1087/******************************************************************************/
     1088
     1089/**
     1090 * @defgroup net_registry_export Export
     1091 * @ingroup net_registry
     1092 * @{
     1093 */
     1094
     1095static int registry_export(TALLOC_CTX *ctx, /*const*/ struct registry_key* key,
     1096                           struct reg_format* f)
     1097{
     1098        int ret=-1;
     1099        WERROR werr;
     1100        uint32_t count;
     1101
     1102        struct registry_value *valvalue = NULL;
     1103        char *valname = NULL;
     1104
     1105        struct registry_key* subkey = NULL;
     1106        char *subkey_name = NULL;
     1107        NTTIME modtime = 0;
     1108
     1109        reg_format_registry_key(f, key, false);
     1110
     1111        /* print values */
     1112        for (count = 0;
     1113             werr = reg_enumvalue(ctx, key, count, &valname, &valvalue),
     1114                     W_ERROR_IS_OK(werr);
     1115             count++)
     1116        {
     1117                reg_format_registry_value(f, valname, valvalue);
     1118        }
     1119        if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
     1120                d_fprintf(stderr, _("reg_enumvalue failed: %s\n"),
     1121                          win_errstr(werr));
     1122                goto done;
     1123        }
     1124
     1125        /* recurse on subkeys */
     1126        for (count = 0;
     1127             werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
     1128                     W_ERROR_IS_OK(werr);
     1129             count++)
     1130        {
     1131                werr = reg_openkey(ctx, key, subkey_name, REG_KEY_READ,
     1132                                   &subkey);
     1133                if (!W_ERROR_IS_OK(werr)) {
     1134                        d_fprintf(stderr, _("reg_openkey failed: %s\n"),
     1135                                  win_errstr(werr));
     1136                        goto done;
     1137                }
     1138
     1139                registry_export(ctx, subkey, f);
     1140        }
     1141        if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
     1142                d_fprintf(stderr, _("reg_enumkey failed: %s\n"),
     1143                          win_errstr(werr));
     1144                goto done;
     1145        }
     1146        ret = 0;
     1147done:
     1148        return ret;
     1149}
     1150
     1151static int net_registry_export(struct net_context *c, int argc,
     1152                               const char **argv)
     1153{
     1154        int ret=-1;
     1155        WERROR werr;
     1156        struct registry_key *key = NULL;
     1157        TALLOC_CTX *ctx = talloc_stackframe();
     1158        struct reg_format* f=NULL;
     1159
     1160        if (argc < 2 || argc > 3 || c->display_usage) {
     1161                d_printf("%s\n%s",
     1162                         _("Usage:"),
     1163                         _("net registry export <path> <file> [opt]\n"));
     1164                d_printf("%s\n%s",
     1165                         _("Example:"),
     1166                         _("net registry export 'HKLM\\Software\\Samba' "
     1167                           "samba.reg regedit5\n"));
     1168                goto done;
     1169        }
     1170
     1171        werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
    4561172        if (!W_ERROR_IS_OK(werr)) {
    4571173                d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
     
    4591175        }
    4601176
    461         werr = reg_getkeysecurity(ctx, key, &secdesc);
    462         if (!W_ERROR_IS_OK(werr)) {
    463                 d_fprintf(stderr, _("reg_getkeysecurity failed: %s\n"),
    464                           win_errstr(werr));
    465                 goto done;
    466         }
    467 
    468         display_sec_desc(secdesc);
    469 
    470         ret = 0;
    471 
    472 done:
    473         TALLOC_FREE(ctx);
    474         return ret;
    475 }
     1177        f = reg_format_file(ctx, argv[1], (argc > 2) ? argv[2] : NULL);
     1178        if (f == NULL) {
     1179                d_fprintf(stderr, _("open file failed: %s\n"), strerror(errno));
     1180                goto done;
     1181        }
     1182
     1183        ret = registry_export(ctx, key, f);
     1184
     1185done:
     1186        TALLOC_FREE(ctx);
     1187        return ret;
     1188}
     1189/**@}*/
     1190
     1191/******************************************************************************/
     1192/**
     1193 * @defgroup net_registry_convert Convert
     1194 * @ingroup net_registry
     1195 * @{
     1196 */
     1197
     1198static int net_registry_convert(struct net_context *c, int argc,
     1199                               const char **argv)
     1200{
     1201        int ret;
     1202        void* mem_ctx;
     1203        const char* in_opt  = NULL;
     1204        const char* out_opt = NULL;
     1205
     1206        if (argc < 2 || argc > 4|| c->display_usage) {
     1207                d_printf("%s\n%s",
     1208                         _("Usage:"),
     1209                         _("net registry convert <in> <out> [in_opt] [out_opt]\n"
     1210                           "net registry convert <in> <out> [out_opt]\n"));
     1211                d_printf("%s\n%s",
     1212                         _("Example:"),
     1213                         _("net registry convert in.reg out.reg regedit4,enc=CP1252\n"));
     1214                return -1;
     1215        }
     1216
     1217        mem_ctx = talloc_stackframe();
     1218
     1219        switch (argc ) {
     1220        case 2:
     1221                break;
     1222        case 3:
     1223                out_opt = argv[2];
     1224                break;
     1225        case 4:
     1226                out_opt = argv[3];
     1227                in_opt  = argv[2];
     1228                break;
     1229        default:
     1230                assert(false);
     1231        }
     1232
     1233
     1234        ret = reg_parse_file(argv[0], (struct reg_parse_callback*)
     1235                             reg_format_file(mem_ctx, argv[1], out_opt),
     1236                             in_opt);
     1237
     1238        talloc_free(mem_ctx);
     1239
     1240        return ret;
     1241}
     1242/**@}*/
     1243
     1244/******************************************************************************/
    4761245
    4771246int net_registry(struct net_context *c, int argc, const char **argv)
     
    4891258                },
    4901259                {
     1260                        "enumerate_recursive",
     1261                        net_registry_enumerate_recursive,
     1262                        NET_TRANSPORT_LOCAL,
     1263                        N_("Enumerate registry keys and values"),
     1264                        N_("net registry enumerate_recursive\n"
     1265                           "    Enumerate registry keys and values")
     1266                },
     1267                {
    4911268                        "createkey",
    4921269                        net_registry_createkey,
     
    5051282                },
    5061283                {
     1284                        "deletekey_recursive",
     1285                        net_registry_deletekey_recursive,
     1286                        NET_TRANSPORT_LOCAL,
     1287                        N_("Delete a registry key with subkeys"),
     1288                        N_("net registry deletekey_recursive\n"
     1289                           "    Delete a registry key with subkeys")
     1290                },
     1291                {
    5071292                        "getvalue",
    5081293                        net_registry_getvalue,
     
    5211306                },
    5221307                {
     1308                        "getvaluesraw",
     1309                        net_registry_getvaluesraw,
     1310                        NET_TRANSPORT_LOCAL,
     1311                        "Print all values of a key in raw format",
     1312                        "net registry getvaluesraw <key>\n"
     1313                        "    Print a registry value (raw format)"
     1314                },
     1315                {
    5231316                        "setvalue",
    5241317                        net_registry_setvalue,
     
    5291322                },
    5301323                {
     1324                        "increment",
     1325                        net_registry_increment,
     1326                        NET_TRANSPORT_LOCAL,
     1327                        N_("Increment a DWORD registry value under a lock"),
     1328                        N_("net registry increment\n"
     1329                           "    Increment a DWORD registry value under a lock")
     1330                },
     1331                {
    5311332                        "deletevalue",
    5321333                        net_registry_deletevalue,
     
    5441345                           "    Get security descriptor")
    5451346                },
     1347                {
     1348                        "getsd_sddl",
     1349                        net_registry_getsd_sddl,
     1350                        NET_TRANSPORT_LOCAL,
     1351                        N_("Get security descriptor in sddl format"),
     1352                        N_("net registry getsd_sddl\n"
     1353                           "    Get security descriptor in sddl format")
     1354                },
     1355                {
     1356                        "setsd_sddl",
     1357                        net_registry_setsd_sddl,
     1358                        NET_TRANSPORT_LOCAL,
     1359                        N_("Set security descriptor from sddl format string"),
     1360                        N_("net registry setsd_sddl\n"
     1361                           "    Set security descriptor from sddl format string")
     1362                },
     1363                {
     1364                        "import",
     1365                        net_registry_import,
     1366                        NET_TRANSPORT_LOCAL,
     1367                        N_("Import .reg file"),
     1368                        N_("net registry import\n"
     1369                           "    Import .reg file")
     1370                },
     1371                {
     1372                        "export",
     1373                        net_registry_export,
     1374                        NET_TRANSPORT_LOCAL,
     1375                        N_("Export .reg file"),
     1376                        N_("net registry export\n"
     1377                           "    Export .reg file")
     1378                },
     1379                {
     1380                        "convert",
     1381                        net_registry_convert,
     1382                        NET_TRANSPORT_LOCAL,
     1383                        N_("Convert .reg file"),
     1384                        N_("net registry convert\n"
     1385                           "    Convert .reg file")
     1386                },
    5461387        { NULL, NULL, 0, NULL, NULL }
    5471388        };
Note: See TracChangeset for help on using the changeset viewer.