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