r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / util_reg_api.c
blob43ad347d4b8833a4b81c9abb3740b40ee341b091
1 /*
2 * Unix SMB/CIFS implementation.
3 * Registry helper routines
4 * Copyright (C) Volker Lendecke 2006
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 WERROR registry_pull_value(TALLOC_CTX *mem_ctx,
24 struct registry_value **pvalue,
25 enum winreg_Type type, uint8 *data,
26 uint32 size, uint32 length)
28 struct registry_value *value;
29 WERROR err;
31 if (!(value = TALLOC_ZERO_P(mem_ctx, struct registry_value))) {
32 return WERR_NOMEM;
35 value->type = type;
37 switch (type) {
38 case REG_DWORD:
39 if ((size != 4) || (length != 4)) {
40 err = WERR_INVALID_PARAM;
41 goto error;
43 value->v.dword = IVAL(data, 0);
44 break;
45 case REG_SZ:
46 case REG_EXPAND_SZ:
49 * Make sure we get a NULL terminated string for
50 * convert_string_talloc().
53 smb_ucs2_t *tmp;
55 if (length == 1) {
56 /* win2k regedit gives us a string of 1 byte when
57 * creating a new value of type REG_SZ. this workaround
58 * replaces the input by using the same string as
59 * winxp delivers. */
60 length = 2;
61 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, 2))) {
62 err = WERR_NOMEM;
63 goto error;
65 tmp[0] = 2;
66 tmp[1] = 0;
68 else if ((length % 2) != 0) {
69 err = WERR_INVALID_PARAM;
70 goto error;
72 else {
73 uint32 num_ucs2 = length / 2;
74 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, num_ucs2+1))) {
75 err = WERR_NOMEM;
76 goto error;
79 memcpy((void *)tmp, (const void *)data, length);
80 tmp[num_ucs2] = 0;
83 if (length + 2 < length) {
84 /* Integer wrap. */
85 SAFE_FREE(tmp);
86 err = WERR_INVALID_PARAM;
87 goto error;
90 value->v.sz.len = convert_string_talloc(
91 value, CH_UTF16LE, CH_UNIX, tmp, length+2,
92 &value->v.sz.str, False);
94 SAFE_FREE(tmp);
96 if (value->v.sz.len == (size_t)-1) {
97 err = WERR_INVALID_PARAM;
98 goto error;
100 break;
102 case REG_MULTI_SZ:
103 err = reg_pull_multi_sz(value, (void *)data, length,
104 &value->v.multi_sz.num_strings,
105 &value->v.multi_sz.strings);
106 if (!(W_ERROR_IS_OK(err))) {
107 goto error;
109 break;
110 case REG_BINARY:
111 value->v.binary.data = talloc_move(value, &data);
112 value->v.binary.length = length;
113 break;
114 default:
115 err = WERR_INVALID_PARAM;
116 goto error;
119 *pvalue = value;
120 return WERR_OK;
122 error:
123 TALLOC_FREE(value);
124 return err;
127 WERROR registry_push_value(TALLOC_CTX *mem_ctx,
128 const struct registry_value *value,
129 DATA_BLOB *presult)
131 switch (value->type) {
132 case REG_DWORD: {
133 char buf[4];
134 SIVAL(buf, 0, value->v.dword);
135 *presult = data_blob_talloc(mem_ctx, (void *)buf, 4);
136 if (presult->data == NULL) {
137 return WERR_NOMEM;
139 break;
141 case REG_SZ:
142 case REG_EXPAND_SZ: {
143 presult->length = convert_string_talloc(
144 mem_ctx, CH_UNIX, CH_UTF16LE, value->v.sz.str,
145 MIN(value->v.sz.len, strlen(value->v.sz.str)+1),
146 (void *)&(presult->data), False);
147 if (presult->length == (size_t)-1) {
148 return WERR_NOMEM;
150 break;
152 default:
153 return WERR_INVALID_PARAM;
156 return WERR_OK;