s3: Make the difference between r/o and r/w in connections_db_ctx more obvious
[ira/wip.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 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_REGISTRY
24
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)
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                 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;
100                 }
101
102                 SAFE_FREE(tmp);
103                 break;
104         }
105         case REG_MULTI_SZ: {
106                 int i;
107                 const char **vals;
108                 DATA_BLOB blob;
109
110                 blob = data_blob_const(data, length);
111
112                 if (!pull_reg_multi_sz(mem_ctx, &blob, &vals)) {
113                         err = WERR_NOMEM;
114                         goto error;
115                 }
116
117                 for (i=0; vals[i]; i++) {
118                         ;;
119                 }
120
121                 value->v.multi_sz.num_strings = i;
122                 value->v.multi_sz.strings = (char **)vals;
123
124                 break;
125         }
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;
132         }
133
134         *pvalue = value;
135         return WERR_OK;
136
137  error:
138         TALLOC_FREE(value);
139         return err;
140 }
141
142 WERROR registry_push_value(TALLOC_CTX *mem_ctx,
143                            const struct registry_value *value,
144                            DATA_BLOB *presult)
145 {
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;
153                 }
154                 break;
155         }
156         case REG_SZ:
157         case REG_EXPAND_SZ: {
158                 if (!push_reg_sz(mem_ctx, presult, value->v.sz.str))
159                 {
160                         return WERR_NOMEM;
161                 }
162                 break;
163         }
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;
168
169                 array = talloc_array(mem_ctx, const char *,
170                                      value->v.multi_sz.num_strings + 1);
171                 if (!array) {
172                         return WERR_NOMEM;
173                 }
174
175                 for (i=0; i < value->v.multi_sz.num_strings; i++) {
176                         array[i] = value->v.multi_sz.strings[i];
177                 }
178                 array[i] = NULL;
179
180                 if (!push_reg_multi_sz(mem_ctx, presult, array)) {
181                         talloc_free(array);
182                         return WERR_NOMEM;
183                 }
184                 talloc_free(array);
185                 break;
186         }
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;
194         }
195
196         return WERR_OK;
197 }