When Windows initially creates a new value, the value name is "New Value #1".
[tprouty/samba.git] / source4 / rpc_server / winreg / rpc_winreg.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the winreg pipe
5
6    Copyright (C) Jelmer Vernooij 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "lib/registry/registry.h"
25 #include "librpc/gen_ndr/ndr_winreg.h"
26 #include "rpc_server/common/common.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "param/param.h"
29
30 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
31
32 static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call,
33                                    const struct dcesrv_interface *iface)
34 {
35         struct registry_context *ctx;
36         WERROR err;
37
38         err = reg_open_samba(dce_call->context,
39                              &ctx, dce_call->conn->dce_ctx->lp_ctx, dce_call->conn->auth_state.session_info,
40                              NULL);
41
42         if (!W_ERROR_IS_OK(err)) {
43                 DEBUG(0, ("Error opening registry: %s\n", win_errstr(err)));
44                 return NT_STATUS_UNSUCCESSFUL;
45         }
46
47         dce_call->context->private = ctx;
48
49         return NT_STATUS_OK;
50 }
51
52 #define DCESRV_INTERFACE_WINREG_BIND dcerpc_winreg_bind
53
54 static WERROR dcesrv_winreg_openhive(struct dcesrv_call_state *dce_call,
55                                      TALLOC_CTX *mem_ctx, uint32_t hkey,
56                                      struct policy_handle **outh)
57 {
58         struct registry_context *ctx = dce_call->context->private;
59         struct dcesrv_handle *h;
60         WERROR error;
61
62         h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
63
64         error = reg_get_predefined_key(ctx, hkey,
65                                        (struct registry_key **)&h->data);
66         if (!W_ERROR_IS_OK(error)) {
67                 return error;
68         }
69
70         *outh = &h->wire_handle;
71
72         return error;
73 }
74
75 #define func_winreg_OpenHive(k,n) static WERROR dcesrv_winreg_Open ## k (struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct winreg_Open ## k *r) \
76 { \
77         return dcesrv_winreg_openhive (dce_call, mem_ctx, n, &r->out.handle);\
78 }
79
80 func_winreg_OpenHive(HKCR,HKEY_CLASSES_ROOT)
81 func_winreg_OpenHive(HKCU,HKEY_CURRENT_USER)
82 func_winreg_OpenHive(HKLM,HKEY_LOCAL_MACHINE)
83 func_winreg_OpenHive(HKPD,HKEY_PERFORMANCE_DATA)
84 func_winreg_OpenHive(HKU,HKEY_USERS)
85 func_winreg_OpenHive(HKCC,HKEY_CURRENT_CONFIG)
86 func_winreg_OpenHive(HKDD,HKEY_DYN_DATA)
87 func_winreg_OpenHive(HKPT,HKEY_PERFORMANCE_TEXT)
88 func_winreg_OpenHive(HKPN,HKEY_PERFORMANCE_NLSTEXT)
89
90 /*
91   winreg_CloseKey
92 */
93 static WERROR dcesrv_winreg_CloseKey(struct dcesrv_call_state *dce_call,
94                                      TALLOC_CTX *mem_ctx,
95                                      struct winreg_CloseKey *r)
96 {
97         struct dcesrv_handle *h;
98
99         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
100
101         talloc_free(h);
102
103         ZERO_STRUCTP(r->out.handle);
104
105         return WERR_OK;
106 }
107
108 /*
109   winreg_CreateKey
110 */
111 static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call,
112                                       TALLOC_CTX *mem_ctx,
113                                       struct winreg_CreateKey *r)
114 {
115         struct dcesrv_handle *h, *newh;
116         WERROR error;
117         struct security_descriptor sd;
118
119         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
120
121         newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
122
123         /* the security descriptor is optional */
124         if (r->in.secdesc != NULL) {
125                 DATA_BLOB sdblob;
126                 enum ndr_err_code ndr_err;
127                 sdblob.data = r->in.secdesc->sd.data;
128                 sdblob.length = r->in.secdesc->sd.len;
129                 if (sdblob.data == NULL) {
130                         return WERR_INVALID_PARAM;
131                 }
132                 ndr_err = ndr_pull_struct_blob_all(&sdblob, mem_ctx, NULL, &sd,
133                                                    (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
134                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
135                         return WERR_INVALID_PARAM;
136                 }
137         }
138
139         error = reg_key_add_name(newh, (struct registry_key *)h->data,
140                                  r->in.name.name, NULL, r->in.secdesc?&sd:NULL,
141                                  (struct registry_key **)&newh->data);
142         if (W_ERROR_IS_OK(error)) {
143                 r->out.new_handle = &newh->wire_handle;
144         } else {
145                 talloc_free(newh);
146         }
147
148         return error;
149 }
150
151
152 /*
153   winreg_DeleteKey
154 */
155 static WERROR dcesrv_winreg_DeleteKey(struct dcesrv_call_state *dce_call,
156                                       TALLOC_CTX *mem_ctx,
157                                       struct winreg_DeleteKey *r)
158 {
159         struct dcesrv_handle *h;
160
161         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
162
163         return reg_key_del((struct registry_key *)h->data, r->in.key.name);
164 }
165
166
167 /*
168   winreg_DeleteValue
169 */
170 static WERROR dcesrv_winreg_DeleteValue(struct dcesrv_call_state *dce_call,
171                                         TALLOC_CTX *mem_ctx,
172                                         struct winreg_DeleteValue *r)
173 {
174         struct dcesrv_handle *h;
175         struct registry_key *key;
176
177         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
178
179         key = h->data;
180
181         return reg_del_value(key, r->in.value.name);
182 }
183
184
185 /*
186   winreg_EnumKey
187 */
188 static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call,
189                                     TALLOC_CTX *mem_ctx,
190                                     struct winreg_EnumKey *r)
191 {
192         struct dcesrv_handle *h;
193         const char *name;
194         NTTIME last_mod;
195
196         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
197
198         r->out.result = reg_key_get_subkey_by_index(mem_ctx,
199                                                     (struct registry_key *)h->data,
200                                                     r->in.enum_index,
201                                                     &name, NULL, &last_mod);
202
203         if (W_ERROR_IS_OK(r->out.result)) {
204                 if (2*strlen_m_term(name) > r->in.name->size) {
205                         return WERR_MORE_DATA;
206                 }
207                 r->out.name->length = 2*strlen_m_term(name);
208                 r->out.name->name = name;
209                 r->out.keyclass = talloc_zero(mem_ctx, struct winreg_StringBuf);
210                 if (r->in.last_changed_time) {
211                         r->out.last_changed_time = &last_mod;
212                 }
213         }
214
215         return r->out.result;
216 }
217
218
219 /*
220   winreg_EnumValue
221 */
222 static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call,
223                                       TALLOC_CTX *mem_ctx,
224                                       struct winreg_EnumValue *r)
225 {
226         struct dcesrv_handle *h;
227         struct registry_key *key;
228         WERROR result;
229         const char *data_name;
230         uint32_t data_type;
231         DATA_BLOB data;
232
233         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
234
235         key = h->data;
236
237         result = reg_key_get_value_by_index(mem_ctx, key, r->in.enum_index,
238                                             &data_name,
239                                             &data_type, &data);
240         if (!W_ERROR_IS_OK(result)) {
241                 return result;
242         }
243
244         /* the client can optionally pass a NULL for type, meaning they don't
245            want that back */
246         if (r->in.type != NULL) {
247                 r->out.type = talloc(mem_ctx, enum winreg_Type);
248                 *r->out.type = data_type;
249         }
250
251         /* check the client has enough room for the value */
252         if (r->in.value != NULL &&
253             r->in.size != NULL &&
254             data.length > *r->in.size) {
255                 return WERR_MORE_DATA;
256         }
257
258         /* and enough room for the name */
259         if (r->in.name->size < 2*strlen_m_term(data_name)) {
260                 return WERR_MORE_DATA;
261         }
262
263         r->out.name->name = data_name;
264         r->out.name->length = 2*strlen_m_term(data_name);
265         r->out.name->size = r->in.name->size;
266
267         if (r->in.value) {
268                 r->out.value = data.data;
269         }
270
271         if (r->in.size) {
272                 r->out.size = talloc(mem_ctx, uint32_t);
273                 *r->out.size = data.length;
274                 r->out.length = r->out.size;
275         }
276
277         return WERR_OK;
278 }
279
280
281 /*
282   winreg_FlushKey
283 */
284 static WERROR dcesrv_winreg_FlushKey(struct dcesrv_call_state *dce_call,
285                                      TALLOC_CTX *mem_ctx,
286                                      struct winreg_FlushKey *r)
287 {
288         struct dcesrv_handle *h;
289
290         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
291
292         return reg_key_flush(h->data);
293 }
294
295
296 /*
297   winreg_GetKeySecurity
298 */
299 static WERROR dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state *dce_call,
300                                            TALLOC_CTX *mem_ctx,
301                                            struct winreg_GetKeySecurity *r)
302 {
303         struct dcesrv_handle *h;
304
305         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
306
307         return WERR_NOT_SUPPORTED;
308 }
309
310
311 /*
312   winreg_LoadKey
313 */
314 static WERROR dcesrv_winreg_LoadKey(struct dcesrv_call_state *dce_call,
315                                     TALLOC_CTX *mem_ctx,
316                                     struct winreg_LoadKey *r)
317 {
318         return WERR_NOT_SUPPORTED;
319 }
320
321
322 /*
323   winreg_NotifyChangeKeyValue
324 */
325 static WERROR dcesrv_winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call,
326                                                  TALLOC_CTX *mem_ctx,
327                                                  struct winreg_NotifyChangeKeyValue *r)
328 {
329         return WERR_NOT_SUPPORTED;
330 }
331
332
333 /*
334   winreg_OpenKey
335 */
336 static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call,
337                                     TALLOC_CTX *mem_ctx,
338                                     struct winreg_OpenKey *r)
339 {
340         struct dcesrv_handle *h, *newh;
341         WERROR result;
342
343         DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY);
344
345         if (r->in.keyname.name && strcmp(r->in.keyname.name, "") == 0) {
346                 newh = talloc_reference(dce_call->context, h);
347                 result = WERR_OK;
348         } else {
349                 newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
350                 result = reg_open_key(newh, (struct registry_key *)h->data,
351                                       r->in.keyname.name,
352                                       (struct registry_key **)&newh->data);
353         }
354
355         if (W_ERROR_IS_OK(result)) {
356                 r->out.handle = &newh->wire_handle;
357         } else {
358                 talloc_free(newh);
359         }
360
361         return result;
362 }
363
364
365 /*
366   winreg_QueryInfoKey
367 */
368 static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call,
369                                          TALLOC_CTX *mem_ctx,
370                                          struct winreg_QueryInfoKey *r)
371 {
372         struct dcesrv_handle *h;
373         struct registry_key *k;
374         WERROR ret;
375         const char *classname = NULL;
376
377         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
378
379         k = h->data;
380
381         ret = reg_key_get_info(mem_ctx, k, &classname, r->out.num_subkeys,
382                                r->out.num_values, r->out.last_changed_time,
383                                r->out.max_subkeylen, r->out.max_valnamelen, 
384                                r->out.max_valbufsize);
385
386         if (r->out.classname != NULL)
387                 r->out.classname->name = classname;
388
389         return ret;
390 }
391
392
393 /*
394   winreg_QueryValue
395 */
396 static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call,
397                                        TALLOC_CTX *mem_ctx,
398                                        struct winreg_QueryValue *r)
399 {
400         struct dcesrv_handle *h;
401         struct registry_key *key;
402         uint32_t value_type;
403         DATA_BLOB value_data;
404         WERROR result;
405
406         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
407
408         key = h->data;
409
410         result = reg_key_get_value_by_name(mem_ctx, key, r->in.value_name.name,
411                                            &value_type, &value_data);
412
413         if (!W_ERROR_IS_OK(result)) {
414                 /*
415                  * Windows expects WERR_BADFILE when a particular value
416                  * is not found.  If we receive WERR_NOT_FOUND from the lower
417                  * layer calls, translate it here to return what is expected.
418                  */
419                 if (W_ERROR_EQUAL(result, WERR_NOT_FOUND))
420                         return WERR_BADFILE;
421                 else
422                         return result;
423         }
424
425         /* Just asking for the size of the buffer */
426         r->out.type = talloc(mem_ctx, uint32_t);
427         if (!r->out.type) {
428                 return WERR_NOMEM;
429         }
430         *r->out.type = value_type;
431         r->out.length = talloc(mem_ctx, uint32_t);
432         if (!r->out.length) {
433                 return WERR_NOMEM;
434         }
435         *r->out.length = value_data.length;
436         if (r->in.data == NULL) {
437                 r->out.size = talloc(mem_ctx, uint32_t);
438                 *r->out.size = value_data.length;
439         } else {
440                 r->out.size = r->in.size;
441                 r->out.data = value_data.data;
442         }
443
444         return WERR_OK;
445 }
446
447
448 /*
449   winreg_ReplaceKey
450 */
451 static WERROR dcesrv_winreg_ReplaceKey(struct dcesrv_call_state *dce_call,
452                                        TALLOC_CTX *mem_ctx,
453                                        struct winreg_ReplaceKey *r)
454 {
455         return WERR_NOT_SUPPORTED;
456 }
457
458
459 /*
460   winreg_RestoreKey
461 */
462 static WERROR dcesrv_winreg_RestoreKey(struct dcesrv_call_state *dce_call,
463                                        TALLOC_CTX *mem_ctx,
464                                        struct winreg_RestoreKey *r)
465 {
466         return WERR_NOT_SUPPORTED;
467 }
468
469
470 /*
471   winreg_SaveKey
472 */
473 static WERROR dcesrv_winreg_SaveKey(struct dcesrv_call_state *dce_call,
474                                     TALLOC_CTX *mem_ctx,
475                                     struct winreg_SaveKey *r)
476 {
477         return WERR_NOT_SUPPORTED;
478 }
479
480
481 /*
482   winreg_SetKeySecurity
483 */
484 static WERROR dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state *dce_call,
485                                            TALLOC_CTX *mem_ctx,
486                                            struct winreg_SetKeySecurity *r)
487 {
488         return WERR_NOT_SUPPORTED;
489 }
490
491
492 /*
493   winreg_SetValue
494 */
495 static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call,
496                                      TALLOC_CTX *mem_ctx,
497                                      struct winreg_SetValue *r)
498 {
499         struct dcesrv_handle *h;
500         struct registry_key *key;
501         WERROR result;
502         DATA_BLOB data;
503
504         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
505
506         key = h->data;
507
508         data.data = r->in.data;
509         data.length = r->in.size;
510         result = reg_val_set(key, r->in.name.name, r->in.type, data);
511
512         return result;
513 }
514
515
516 /*
517   winreg_UnLoadKey
518 */
519 static WERROR dcesrv_winreg_UnLoadKey(struct dcesrv_call_state *dce_call,
520                                       TALLOC_CTX *mem_ctx,
521                                       struct winreg_UnLoadKey *r)
522 {
523         return WERR_NOT_SUPPORTED;
524 }
525
526
527 /*
528   winreg_InitiateSystemShutdown
529 */
530 static WERROR dcesrv_winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call,
531                                                    TALLOC_CTX *mem_ctx,
532                                                    struct winreg_InitiateSystemShutdown *r)
533 {
534         return WERR_NOT_SUPPORTED;
535 }
536
537
538 /*
539   winreg_AbortSystemShutdown
540 */
541 static WERROR dcesrv_winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call,
542                                                 TALLOC_CTX *mem_ctx,
543                                                 struct winreg_AbortSystemShutdown *r)
544 {
545         return WERR_NOT_SUPPORTED;
546 }
547
548
549 /*
550   winreg_GetVersion
551 */
552 static WERROR dcesrv_winreg_GetVersion(struct dcesrv_call_state *dce_call,
553                                        TALLOC_CTX *mem_ctx,
554                                        struct winreg_GetVersion *r)
555 {
556         struct dcesrv_handle *h;
557
558         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
559
560         r->out.version = talloc(mem_ctx, uint32_t);
561         W_ERROR_HAVE_NO_MEMORY(r->out.version);
562
563         *r->out.version = 5;
564
565         return WERR_OK;
566 }
567
568
569 /*
570   winreg_QueryMultipleValues
571 */
572 static WERROR dcesrv_winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call,
573                                                 TALLOC_CTX *mem_ctx,
574                                                 struct winreg_QueryMultipleValues *r)
575 {
576         return WERR_NOT_SUPPORTED;
577 }
578
579
580 /*
581   winreg_InitiateSystemShutdownEx
582 */
583 static WERROR dcesrv_winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call,
584                                                      TALLOC_CTX *mem_ctx,
585                                                      struct winreg_InitiateSystemShutdownEx *r)
586 {
587         return WERR_NOT_SUPPORTED;
588 }
589
590
591 /*
592   winreg_SaveKeyEx
593 */
594 static WERROR dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state *dce_call,
595                                       TALLOC_CTX *mem_ctx,
596                                       struct winreg_SaveKeyEx *r)
597 {
598         return WERR_NOT_SUPPORTED;
599 }
600
601
602 /*
603   winreg_QueryMultipleValues2
604 */
605 static WERROR dcesrv_winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call,
606                                                  TALLOC_CTX *mem_ctx,
607                                                  struct winreg_QueryMultipleValues2 *r)
608 {
609         return WERR_NOT_SUPPORTED;
610 }
611
612
613 /* include the generated boilerplate */
614 #include "librpc/gen_ndr/ndr_winreg_s.c"