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