Fix bug #8521 - winbindd cache timeout expiry test was reversed
[Samba.git] / source3 / lib / util_reg_api.c
blob7150444cf67e7150b69c6850711f75273d14034d
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.
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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_REGISTRY
25 WERROR registry_pull_value(TALLOC_CTX *mem_ctx,
26 struct registry_value **pvalue,
27 enum winreg_Type type, uint8 *data,
28 uint32 size, uint32 length)
30 struct registry_value *value;
31 WERROR err;
33 if (!(value = TALLOC_ZERO_P(mem_ctx, struct registry_value))) {
34 return WERR_NOMEM;
37 value->type = type;
39 switch (type) {
40 case REG_DWORD:
41 if ((size != 4) || (length != 4)) {
42 err = WERR_INVALID_PARAM;
43 goto error;
45 value->v.dword = IVAL(data, 0);
46 break;
47 case REG_SZ:
48 case REG_EXPAND_SZ:
51 * Make sure we get a NULL terminated string for
52 * convert_string_talloc().
55 smb_ucs2_t *tmp;
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;
67 tmp[0] = 0;
68 tmp[1] = 0;
69 DEBUG(10, ("got REG_SZ value of length 1 - workaround "
70 "activated.\n"));
72 else if ((length % 2) != 0) {
73 err = WERR_INVALID_PARAM;
74 goto error;
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;
83 memcpy((void *)tmp, (const void *)data, length);
84 tmp[num_ucs2] = 0;
87 if (length + 2 < length) {
88 /* Integer wrap. */
89 SAFE_FREE(tmp);
90 err = WERR_INVALID_PARAM;
91 goto error;
94 if (!convert_string_talloc(value, CH_UTF16LE, CH_UNIX, tmp,
95 length+2, (void *)&value->v.sz.str,
96 &value->v.sz.len, False)) {
97 SAFE_FREE(tmp);
98 err = WERR_INVALID_PARAM;
99 goto error;
102 SAFE_FREE(tmp);
103 break;
105 case REG_MULTI_SZ: {
106 int i;
107 const char **vals;
108 DATA_BLOB blob;
110 blob = data_blob_const(data, length);
112 if (!pull_reg_multi_sz(mem_ctx, &blob, &vals)) {
113 err = WERR_NOMEM;
114 goto error;
117 for (i=0; vals[i]; i++) {
121 value->v.multi_sz.num_strings = i;
122 value->v.multi_sz.strings = (char **)vals;
124 break;
126 case REG_BINARY:
127 value->v.binary = data_blob_talloc(mem_ctx, data, length);
128 break;
129 default:
130 err = WERR_INVALID_PARAM;
131 goto error;
134 *pvalue = value;
135 return WERR_OK;
137 error:
138 TALLOC_FREE(value);
139 return err;
142 WERROR registry_push_value(TALLOC_CTX *mem_ctx,
143 const struct registry_value *value,
144 DATA_BLOB *presult)
146 switch (value->type) {
147 case REG_DWORD: {
148 char buf[4];
149 SIVAL(buf, 0, value->v.dword);
150 *presult = data_blob_talloc(mem_ctx, (void *)buf, 4);
151 if (presult->data == NULL) {
152 return WERR_NOMEM;
154 break;
156 case REG_SZ:
157 case REG_EXPAND_SZ: {
158 if (!push_reg_sz(mem_ctx, presult, value->v.sz.str))
160 return WERR_NOMEM;
162 break;
164 case REG_MULTI_SZ: {
165 /* handle the case where we don't get a NULL terminated array */
166 const char **array;
167 int i;
169 array = talloc_array(mem_ctx, const char *,
170 value->v.multi_sz.num_strings + 1);
171 if (!array) {
172 return WERR_NOMEM;
175 for (i=0; i < value->v.multi_sz.num_strings; i++) {
176 array[i] = value->v.multi_sz.strings[i];
178 array[i] = NULL;
180 if (!push_reg_multi_sz(mem_ctx, presult, array)) {
181 talloc_free(array);
182 return WERR_NOMEM;
184 talloc_free(array);
185 break;
187 case REG_BINARY:
188 *presult = data_blob_talloc(mem_ctx,
189 value->v.binary.data,
190 value->v.binary.length);
191 break;
192 default:
193 return WERR_INVALID_PARAM;
196 return WERR_OK;