source: branches/samba-3.2.x/source/lib/util_reg_api.c

Last change on this file was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

File size: 5.1 KB
Line 
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 3 of the License, or (at your option)
9 * any later version.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "includes.h"
21
22#undef DBGC_CLASS
23#define DBGC_CLASS DBGC_REGISTRY
24
25WERROR registry_pull_value(TALLOC_CTX *mem_ctx,
26 struct registry_value **pvalue,
27 enum winreg_Type type, uint8 *data,
28 uint32 size, uint32 length)
29{
30 struct registry_value *value;
31 WERROR err;
32
33 if (!(value = TALLOC_ZERO_P(mem_ctx, struct registry_value))) {
34 return WERR_NOMEM;
35 }
36
37 value->type = type;
38
39 switch (type) {
40 case REG_DWORD:
41 if ((size != 4) || (length != 4)) {
42 err = WERR_INVALID_PARAM;
43 goto error;
44 }
45 value->v.dword = IVAL(data, 0);
46 break;
47 case REG_SZ:
48 case REG_EXPAND_SZ:
49 {
50 /*
51 * Make sure we get a NULL terminated string for
52 * convert_string_talloc().
53 */
54
55 smb_ucs2_t *tmp;
56
57 if (length == 1) {
58 /* win2k regedit gives us a string of 1 byte when
59 * creating a new value of type REG_SZ. this workaround
60 * replaces the input by using the same string as
61 * winxp delivers. */
62 length = 2;
63 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, 2))) {
64 err = WERR_NOMEM;
65 goto error;
66 }
67 tmp[0] = 0;
68 tmp[1] = 0;
69 DEBUG(10, ("got REG_SZ value of length 1 - workaround "
70 "activated.\n"));
71 }
72 else if ((length % 2) != 0) {
73 err = WERR_INVALID_PARAM;
74 goto error;
75 }
76 else {
77 uint32 num_ucs2 = length / 2;
78 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, num_ucs2+1))) {
79 err = WERR_NOMEM;
80 goto error;
81 }
82
83 memcpy((void *)tmp, (const void *)data, length);
84 tmp[num_ucs2] = 0;
85 }
86
87 if (length + 2 < length) {
88 /* Integer wrap. */
89 SAFE_FREE(tmp);
90 err = WERR_INVALID_PARAM;
91 goto error;
92 }
93
94 value->v.sz.len = convert_string_talloc(
95 value, CH_UTF16LE, CH_UNIX, tmp, length+2,
96 &value->v.sz.str, False);
97
98 SAFE_FREE(tmp);
99
100 if (value->v.sz.len == (size_t)-1) {
101 err = WERR_INVALID_PARAM;
102 goto error;
103 }
104 break;
105 }
106 case REG_MULTI_SZ:
107 err = reg_pull_multi_sz(value, (void *)data, length,
108 &value->v.multi_sz.num_strings,
109 &value->v.multi_sz.strings);
110 if (!(W_ERROR_IS_OK(err))) {
111 goto error;
112 }
113 break;
114 case REG_BINARY:
115 value->v.binary = data_blob_talloc(mem_ctx, data, length);
116 break;
117 default:
118 err = WERR_INVALID_PARAM;
119 goto error;
120 }
121
122 *pvalue = value;
123 return WERR_OK;
124
125 error:
126 TALLOC_FREE(value);
127 return err;
128}
129
130WERROR registry_push_value(TALLOC_CTX *mem_ctx,
131 const struct registry_value *value,
132 DATA_BLOB *presult)
133{
134 switch (value->type) {
135 case REG_DWORD: {
136 char buf[4];
137 SIVAL(buf, 0, value->v.dword);
138 *presult = data_blob_talloc(mem_ctx, (void *)buf, 4);
139 if (presult->data == NULL) {
140 return WERR_NOMEM;
141 }
142 break;
143 }
144 case REG_SZ:
145 case REG_EXPAND_SZ: {
146 presult->length = convert_string_talloc(
147 mem_ctx, CH_UNIX, CH_UTF16LE, value->v.sz.str,
148 MIN(value->v.sz.len, strlen(value->v.sz.str)+1),
149 (void *)&(presult->data), False);
150 if (presult->length == (size_t)-1) {
151 return WERR_NOMEM;
152 }
153 break;
154 }
155 case REG_MULTI_SZ: {
156 uint32_t count;
157 size_t len = 0;
158 char **strings;
159 size_t *string_lengths;
160 uint32_t ofs;
161 TALLOC_CTX *tmp_ctx = talloc_stackframe();
162
163 strings = TALLOC_ARRAY(tmp_ctx, char *,
164 value->v.multi_sz.num_strings);
165 if (strings == NULL) {
166 return WERR_NOMEM;
167 }
168
169 string_lengths = TALLOC_ARRAY(tmp_ctx, size_t,
170 value->v.multi_sz.num_strings);
171 if (string_lengths == NULL) {
172 TALLOC_FREE(tmp_ctx);
173 return WERR_NOMEM;
174 }
175
176 /* convert the single strings */
177 for (count = 0; count < value->v.multi_sz.num_strings; count++)
178 {
179 string_lengths[count] = convert_string_talloc(
180 strings, CH_UNIX, CH_UTF16LE,
181 value->v.multi_sz.strings[count],
182 strlen(value->v.multi_sz.strings[count])+1,
183 (void *)&strings[count], false);
184 if (string_lengths[count] == (size_t)-1) {
185 TALLOC_FREE(tmp_ctx);
186 return WERR_NOMEM;
187 }
188 len += string_lengths[count];
189 }
190
191 /* now concatenate all into the data blob */
192 presult->data = TALLOC_ARRAY(mem_ctx, uint8_t, len);
193 if (presult->data == NULL) {
194 TALLOC_FREE(tmp_ctx);
195 return WERR_NOMEM;
196 }
197 for (count = 0, ofs = 0;
198 count < value->v.multi_sz.num_strings;
199 count++)
200 {
201 memcpy(presult->data + ofs, strings[count],
202 string_lengths[count]);
203 ofs += string_lengths[count];
204 }
205 presult->length = len;
206
207 TALLOC_FREE(tmp_ctx);
208
209 break;
210 }
211 case REG_BINARY:
212 *presult = data_blob_talloc(mem_ctx,
213 value->v.binary.data,
214 value->v.binary.length);
215 break;
216 default:
217 return WERR_INVALID_PARAM;
218 }
219
220 return WERR_OK;
221}
Note: See TracBrowser for help on using the repository browser.