r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[nivanova/samba-autobuild/.git] / source3 / lib / util_reg_api.c
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 WERROR registry_pull_value(TALLOC_CTX *mem_ctx,
23                            struct registry_value **pvalue,
24                            enum winreg_Type type, uint8 *data,
25                            uint32 size, uint32 length)
26 {
27         struct registry_value *value;
28         WERROR err;
29
30         if (!(value = TALLOC_ZERO_P(mem_ctx, struct registry_value))) {
31                 return WERR_NOMEM;
32         }
33
34         value->type = type;
35
36         switch (type) {
37         case REG_DWORD:
38                 if ((size != 4) || (length != 4)) {
39                         err = WERR_INVALID_PARAM;
40                         goto error;
41                 }
42                 value->v.dword = IVAL(data, 0);
43                 break;
44         case REG_SZ:
45         case REG_EXPAND_SZ:
46         {
47                 /*
48                  * Make sure we get a NULL terminated string for
49                  * convert_string_talloc().
50                  */
51
52                 smb_ucs2_t *tmp;
53
54                 if (length == 1) {
55                         /* win2k regedit gives us a string of 1 byte when
56                          * creating a new value of type REG_SZ. this workaround
57                          * replaces the input by using the same string as
58                          * winxp delivers. */
59                         length = 2;
60                         if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, 2))) {
61                                 err = WERR_NOMEM;
62                                 goto error;
63                         }
64                         tmp[0] = 2;
65                         tmp[1] = 0;
66                 }
67                 else if ((length % 2) != 0) {
68                         err = WERR_INVALID_PARAM;
69                         goto error;
70                 }
71                 else {
72                         uint32 num_ucs2 = length / 2;
73                         if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, num_ucs2+1))) {
74                                 err = WERR_NOMEM;
75                                 goto error;
76                         }
77
78                         memcpy((void *)tmp, (const void *)data, length);
79                         tmp[num_ucs2] = 0;
80                 }
81
82                 if (length + 2 < length) {
83                         /* Integer wrap. */
84                         SAFE_FREE(tmp);
85                         err = WERR_INVALID_PARAM;
86                         goto error;
87                 }
88
89                 value->v.sz.len = convert_string_talloc(
90                         value, CH_UTF16LE, CH_UNIX, tmp, length+2,
91                         &value->v.sz.str, False);
92
93                 SAFE_FREE(tmp);
94
95                 if (value->v.sz.len == (size_t)-1) {
96                         err = WERR_INVALID_PARAM;
97                         goto error;
98                 }
99                 break;
100         }
101         case REG_MULTI_SZ:
102                 err = reg_pull_multi_sz(value, (void *)data, length,
103                                         &value->v.multi_sz.num_strings,
104                                         &value->v.multi_sz.strings);
105                 if (!(W_ERROR_IS_OK(err))) {
106                         goto error;
107                 }
108                 break;
109         case REG_BINARY:
110                 value->v.binary.data = talloc_move(value, &data);
111                 value->v.binary.length = length;
112                 break;
113         default:
114                 err = WERR_INVALID_PARAM;
115                 goto error;
116         }
117
118         *pvalue = value;
119         return WERR_OK;
120
121  error:
122         TALLOC_FREE(value);
123         return err;
124 }
125
126 WERROR registry_push_value(TALLOC_CTX *mem_ctx,
127                            const struct registry_value *value,
128                            DATA_BLOB *presult)
129 {
130         switch (value->type) {
131         case REG_DWORD: {
132                 char buf[4];
133                 SIVAL(buf, 0, value->v.dword);
134                 *presult = data_blob_talloc(mem_ctx, (void *)buf, 4);
135                 if (presult->data == NULL) {
136                         return WERR_NOMEM;
137                 }
138                 break;
139         }
140         case REG_SZ:
141         case REG_EXPAND_SZ: {
142                 presult->length = convert_string_talloc(
143                         mem_ctx, CH_UNIX, CH_UTF16LE, value->v.sz.str,
144                         MIN(value->v.sz.len, strlen(value->v.sz.str)+1),
145                         (void *)&(presult->data), False);
146                 if (presult->length == (size_t)-1) {
147                         return WERR_NOMEM;
148                 }
149                 break;
150         }
151         default:
152                 return WERR_INVALID_PARAM;
153         }
154
155         return WERR_OK;
156 }