d4bf2e70e351c9603bb80a861ab0461747a40662
[metze/samba/wip.git] / source3 / rpc_server / svcctl / srv_svcctl_nt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *
5  *  Copyright (C) Marcin Krzysztof Porwit           2005.
6  *
7  *  Largely Rewritten (Again) by:
8  *  Copyright (C) Gerald (Jerry) Carter             2005.
9  *  Copyright (C) Guenther Deschner                 2008,2009.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 3 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include "includes.h"
26 #include "system/passwd.h" /* uid_wrapper */
27 #include "ntdomain.h"
28 #include "../librpc/gen_ndr/srv_svcctl.h"
29 #include "../libcli/security/security.h"
30 #include "../librpc/gen_ndr/ndr_security.h"
31 #include "services/services.h"
32 #include "services/svc_winreg_glue.h"
33 #include "auth.h"
34 #include "rpc_server/svcctl/srv_svcctl_nt.h"
35
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_RPC_SRV
38
39 struct service_control_op {
40         const char *name;
41         SERVICE_CONTROL_OPS *ops;
42 };
43
44 /* handle external services */
45 extern SERVICE_CONTROL_OPS rcinit_svc_ops;
46
47 /* builtin services (see service_db.c and services/svc_*.c */
48 extern SERVICE_CONTROL_OPS spoolss_svc_ops;
49 extern SERVICE_CONTROL_OPS netlogon_svc_ops;
50 extern SERVICE_CONTROL_OPS winreg_svc_ops;
51 extern SERVICE_CONTROL_OPS wins_svc_ops;
52
53 /* make sure this number patches the number of builtin
54    SERVICE_CONTROL_OPS structure listed above */
55
56 #define SVCCTL_NUM_INTERNAL_SERVICES    4
57
58 struct service_control_op *svcctl_ops;
59
60 static const struct generic_mapping scm_generic_map =
61         { SC_MANAGER_READ_ACCESS, SC_MANAGER_WRITE_ACCESS, SC_MANAGER_EXECUTE_ACCESS, SC_MANAGER_ALL_ACCESS };
62 static const struct generic_mapping svc_generic_map =
63         { SERVICE_READ_ACCESS, SERVICE_WRITE_ACCESS, SERVICE_EXECUTE_ACCESS, SERVICE_ALL_ACCESS };
64
65
66 /********************************************************************
67 ********************************************************************/
68
69 bool init_service_op_table( void )
70 {
71         const char **service_list = lp_svcctl_list();
72         int num_services = SVCCTL_NUM_INTERNAL_SERVICES + str_list_length( service_list );
73         int i;
74
75         if ( !(svcctl_ops = talloc_array( NULL, struct service_control_op, num_services+1)) ) {
76                 DEBUG(0,("init_service_op_table: talloc() failed!\n"));
77                 return False;
78         }
79
80         /* services listed in smb.conf get the rc.init interface */
81
82         for ( i=0; service_list && service_list[i]; i++ ) {
83                 svcctl_ops[i].name = talloc_strdup( svcctl_ops, service_list[i] );
84                 svcctl_ops[i].ops  = &rcinit_svc_ops;
85         }
86
87         /* add builtin services */
88
89         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "Spooler" );
90         svcctl_ops[i].ops  = &spoolss_svc_ops;
91         i++;
92
93         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "NETLOGON" );
94         svcctl_ops[i].ops  = &netlogon_svc_ops;
95         i++;
96
97         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "RemoteRegistry" );
98         svcctl_ops[i].ops  = &winreg_svc_ops;
99         i++;
100
101         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "WINS" );
102         svcctl_ops[i].ops  = &wins_svc_ops;
103         i++;
104
105         /* NULL terminate the array */
106
107         svcctl_ops[i].name = NULL;
108         svcctl_ops[i].ops  = NULL;
109
110         return True;
111 }
112
113 bool shutdown_service_op_table(void)
114 {
115         TALLOC_FREE(svcctl_ops);
116
117         return true;
118 }
119
120 /********************************************************************
121 ********************************************************************/
122
123 static struct service_control_op* find_service_by_name( const char *name )
124 {
125         int i;
126
127         for ( i=0; svcctl_ops[i].name; i++ ) {
128                 if ( strequal( name, svcctl_ops[i].name ) )
129                         return &svcctl_ops[i];
130         }
131
132         return NULL;
133 }
134 /********************************************************************
135 ********************************************************************/
136
137 static NTSTATUS svcctl_access_check( struct security_descriptor *sec_desc, struct security_token *token,
138                                      uint32_t access_desired, uint32_t *access_granted )
139 {
140         if ( geteuid() == sec_initial_uid() ) {
141                 DEBUG(5,("svcctl_access_check: using root's token\n"));
142                 token = get_root_nt_token();
143         }
144
145         return se_access_check( sec_desc, token, access_desired, access_granted);
146 }
147
148 /********************************************************************
149 ********************************************************************/
150
151 static struct security_descriptor* construct_scm_sd( TALLOC_CTX *ctx )
152 {
153         struct security_ace ace[2];
154         size_t i = 0;
155         struct security_descriptor *sd;
156         struct security_acl *theacl;
157         size_t sd_size;
158
159         /* basic access for Everyone */
160
161         init_sec_ace(&ace[i++], &global_sid_World,
162                 SEC_ACE_TYPE_ACCESS_ALLOWED, SC_MANAGER_READ_ACCESS, 0);
163
164         /* Full Access 'BUILTIN\Administrators' */
165
166         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
167                 SEC_ACE_TYPE_ACCESS_ALLOWED, SC_MANAGER_ALL_ACCESS, 0);
168
169
170         /* create the security descriptor */
171
172         if ( !(theacl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) )
173                 return NULL;
174
175         if ( !(sd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
176                                   SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
177                                   theacl, &sd_size)) )
178                 return NULL;
179
180         return sd;
181 }
182
183 /******************************************************************
184  Find a registry key handle and return a SERVICE_INFO
185  *****************************************************************/
186
187 static SERVICE_INFO *find_service_info_by_hnd(struct pipes_struct *p,
188                                               struct policy_handle *hnd)
189 {
190         SERVICE_INFO *service_info = NULL;
191
192         if( !find_policy_by_hnd( p, hnd, (void **)(void *)&service_info) ) {
193                 DEBUG(2,("find_service_info_by_hnd: handle not found\n"));
194                 return NULL;
195         }
196
197         return service_info;
198 }
199
200 /******************************************************************
201  *****************************************************************/
202
203 static WERROR create_open_service_handle(struct pipes_struct *p,
204                                          struct policy_handle *handle,
205                                          uint32_t type,
206                                          const char *service,
207                                          uint32_t access_granted)
208 {
209         SERVICE_INFO *info = NULL;
210         WERROR result = WERR_OK;
211         struct service_control_op *s_op;
212
213         if ( !(info = talloc_zero( NULL, SERVICE_INFO )) )
214                 return WERR_NOT_ENOUGH_MEMORY;
215
216         /* the Service Manager has a NULL name */
217
218         info->type = SVC_HANDLE_IS_SCM;
219
220         switch ( type ) {
221         case SVC_HANDLE_IS_SCM:
222                 info->type = SVC_HANDLE_IS_SCM;
223                 break;
224
225         case SVC_HANDLE_IS_DBLOCK:
226                 info->type = SVC_HANDLE_IS_DBLOCK;
227                 break;
228
229         case SVC_HANDLE_IS_SERVICE:
230                 info->type = SVC_HANDLE_IS_SERVICE;
231
232                 /* lookup the SERVICE_CONTROL_OPS */
233
234                 if ( !(s_op = find_service_by_name( service )) ) {
235                         result = WERR_SERVICE_DOES_NOT_EXIST;
236                         goto done;
237                 }
238
239                 info->ops = s_op->ops;
240
241                 if ( !(info->name  = talloc_strdup( info, s_op->name )) ) {
242                         result = WERR_NOT_ENOUGH_MEMORY;
243                         goto done;
244                 }
245                 break;
246
247         default:
248                 result = WERR_SERVICE_DOES_NOT_EXIST;
249                 goto done;
250         }
251
252         info->access_granted = access_granted;
253
254         /* store the SERVICE_INFO and create an open handle */
255
256         if ( !create_policy_hnd( p, handle, info ) ) {
257                 result = WERR_ACCESS_DENIED;
258                 goto done;
259         }
260
261 done:
262         if ( !W_ERROR_IS_OK(result) )
263                 TALLOC_FREE(info);
264
265         return result;
266 }
267
268 /********************************************************************
269  _svcctl_OpenSCManagerW
270 ********************************************************************/
271
272 WERROR _svcctl_OpenSCManagerW(struct pipes_struct *p,
273                               struct svcctl_OpenSCManagerW *r)
274 {
275         struct security_descriptor *sec_desc;
276         uint32_t access_granted = 0;
277         NTSTATUS status;
278
279         /* perform access checks */
280
281         if ( !(sec_desc = construct_scm_sd( p->mem_ctx )) )
282                 return WERR_NOT_ENOUGH_MEMORY;
283
284         se_map_generic( &r->in.access_mask, &scm_generic_map );
285         status = svcctl_access_check( sec_desc, p->session_info->security_token,
286                                       r->in.access_mask, &access_granted );
287         if ( !NT_STATUS_IS_OK(status) )
288                 return ntstatus_to_werror( status );
289
290         return create_open_service_handle( p, r->out.handle, SVC_HANDLE_IS_SCM, NULL, access_granted );
291 }
292
293 /********************************************************************
294  _svcctl_OpenServiceW
295 ********************************************************************/
296
297 WERROR _svcctl_OpenServiceW(struct pipes_struct *p,
298                             struct svcctl_OpenServiceW *r)
299 {
300         struct security_descriptor *sec_desc;
301         uint32_t access_granted = 0;
302         NTSTATUS status;
303         const char *service = NULL;
304
305         service = r->in.ServiceName;
306         if (!service) {
307                 return WERR_NOT_ENOUGH_MEMORY;
308         }
309         DEBUG(5, ("_svcctl_OpenServiceW: Attempting to open Service [%s], \n", service));
310
311         /* based on my tests you can open a service if you have a valid scm handle */
312
313         if ( !find_service_info_by_hnd( p, r->in.scmanager_handle) )
314                 return WERR_INVALID_HANDLE;
315
316         /*
317          * Perform access checks. Use the system session_info in order to ensure
318          * that we retrieve the security descriptor
319          */
320         sec_desc = svcctl_get_secdesc(p->mem_ctx,
321                                       p->msg_ctx,
322                                       get_session_info_system(),
323                                       service);
324         if (sec_desc == NULL) {
325                 DEBUG(0, ("_svcctl_OpenServiceW: Failed to get a valid security "
326                           "descriptor"));
327                 return WERR_NOT_ENOUGH_MEMORY;
328         }
329
330         se_map_generic( &r->in.access_mask, &svc_generic_map );
331         status = svcctl_access_check( sec_desc, p->session_info->security_token,
332                                       r->in.access_mask, &access_granted );
333         if ( !NT_STATUS_IS_OK(status) )
334                 return ntstatus_to_werror( status );
335
336         return create_open_service_handle( p, r->out.handle, SVC_HANDLE_IS_SERVICE, service, access_granted );
337 }
338
339 /********************************************************************
340  _svcctl_CloseServiceHandle
341 ********************************************************************/
342
343 WERROR _svcctl_CloseServiceHandle(struct pipes_struct *p,
344                                   struct svcctl_CloseServiceHandle *r)
345 {
346         if ( !close_policy_hnd( p, r->in.handle ) )
347                 return  WERR_INVALID_HANDLE;
348
349         ZERO_STRUCTP(r->out.handle);
350
351         return WERR_OK;
352 }
353
354 /********************************************************************
355  _svcctl_GetServiceDisplayNameW
356 ********************************************************************/
357
358 WERROR _svcctl_GetServiceDisplayNameW(struct pipes_struct *p,
359                                       struct svcctl_GetServiceDisplayNameW *r)
360 {
361         const char *service;
362         const char *display_name;
363         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
364
365         /* can only use an SCM handle here */
366
367         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
368                 return WERR_INVALID_HANDLE;
369
370         service = r->in.service_name;
371
372         display_name = svcctl_lookup_dispname(p->mem_ctx,
373                                               p->msg_ctx,
374                                               p->session_info,
375                                               service);
376         if (!display_name) {
377                 display_name = "";
378         }
379
380         *r->out.display_name = display_name;
381         *r->out.display_name_length = strlen(display_name);
382
383         return WERR_OK;
384 }
385
386 /********************************************************************
387  _svcctl_QueryServiceStatus
388 ********************************************************************/
389
390 WERROR _svcctl_QueryServiceStatus(struct pipes_struct *p,
391                                   struct svcctl_QueryServiceStatus *r)
392 {
393         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
394
395         /* perform access checks */
396
397         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
398                 return WERR_INVALID_HANDLE;
399
400         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
401                 return WERR_ACCESS_DENIED;
402
403         /* try the service specific status call */
404
405         return info->ops->service_status( info->name, r->out.service_status );
406 }
407
408 /********************************************************************
409 ********************************************************************/
410
411 static int enumerate_status(TALLOC_CTX *ctx,
412                             struct messaging_context *msg_ctx,
413                             struct auth_session_info *session_info,
414                             struct ENUM_SERVICE_STATUSW **status)
415 {
416         int num_services = 0;
417         int i;
418         struct ENUM_SERVICE_STATUSW *st;
419         const char *display_name;
420
421         /* just count */
422         while ( svcctl_ops[num_services].name )
423                 num_services++;
424
425         if ( !(st = talloc_array( ctx, struct ENUM_SERVICE_STATUSW, num_services )) ) {
426                 DEBUG(0,("enumerate_status: talloc() failed!\n"));
427                 return -1;
428         }
429
430         for ( i=0; i<num_services; i++ ) {
431                 st[i].service_name = talloc_strdup(st, svcctl_ops[i].name );
432
433                 display_name = svcctl_lookup_dispname(ctx,
434                                                       msg_ctx,
435                                                       session_info,
436                                                       svcctl_ops[i].name);
437                 st[i].display_name = talloc_strdup(st, display_name ? display_name : "");
438
439                 svcctl_ops[i].ops->service_status( svcctl_ops[i].name, &st[i].status );
440         }
441
442         *status = st;
443
444         return num_services;
445 }
446
447 /********************************************************************
448  _svcctl_EnumServicesStatusW
449 ********************************************************************/
450
451 WERROR _svcctl_EnumServicesStatusW(struct pipes_struct *p,
452                                    struct svcctl_EnumServicesStatusW *r)
453 {
454         struct ENUM_SERVICE_STATUSW *services = NULL;
455         int num_services;
456         int i = 0;
457         size_t buffer_size = 0;
458         WERROR result = WERR_OK;
459         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
460         DATA_BLOB blob = data_blob_null;
461
462         /* perform access checks */
463
464         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
465                 return WERR_INVALID_HANDLE;
466
467         if ( !(info->access_granted & SC_RIGHT_MGR_ENUMERATE_SERVICE) ) {
468                 return WERR_ACCESS_DENIED;
469         }
470
471         num_services = enumerate_status(p->mem_ctx,
472                                         p->msg_ctx,
473                                         p->session_info,
474                                         &services);
475         if (num_services == -1 ) {
476                 return WERR_NOT_ENOUGH_MEMORY;
477         }
478
479         for ( i=0; i<num_services; i++ ) {
480                 buffer_size += ndr_size_ENUM_SERVICE_STATUSW(&services[i], 0);
481         }
482
483         buffer_size += buffer_size % 4;
484
485         if (buffer_size > r->in.offered) {
486                 num_services = 0;
487                 result = WERR_MORE_DATA;
488         }
489
490         if ( W_ERROR_IS_OK(result) ) {
491
492                 enum ndr_err_code ndr_err;
493                 struct ndr_push *ndr;
494
495                 ndr = ndr_push_init_ctx(p->mem_ctx);
496                 if (ndr == NULL) {
497                         return WERR_INVALID_PARAMETER;
498                 }
499
500                 ndr_err = ndr_push_ENUM_SERVICE_STATUSW_array(
501                         ndr, num_services, services);
502                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
503                         return ntstatus_to_werror(ndr_map_error2ntstatus(ndr_err));
504                 }
505                 blob = ndr_push_blob(ndr);
506                 memcpy(r->out.service, blob.data, MIN(blob.length, r->in.offered));
507         }
508
509         *r->out.needed                  = (buffer_size > r->in.offered) ? buffer_size : r->in.offered;
510         *r->out.services_returned       = (uint32_t)num_services;
511         if (r->out.resume_handle) {
512                 *r->out.resume_handle   = 0;
513         }
514
515         return result;
516 }
517
518 /********************************************************************
519  _svcctl_StartServiceW
520 ********************************************************************/
521
522 WERROR _svcctl_StartServiceW(struct pipes_struct *p,
523                              struct svcctl_StartServiceW *r)
524 {
525         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
526
527         /* perform access checks */
528
529         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
530                 return WERR_INVALID_HANDLE;
531
532         if ( !(info->access_granted & SC_RIGHT_SVC_START) )
533                 return WERR_ACCESS_DENIED;
534
535         return info->ops->start_service( info->name );
536 }
537
538 /********************************************************************
539  _svcctl_ControlService
540 ********************************************************************/
541
542 WERROR _svcctl_ControlService(struct pipes_struct *p,
543                               struct svcctl_ControlService *r)
544 {
545         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
546
547         /* perform access checks */
548
549         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
550                 return WERR_INVALID_HANDLE;
551
552         switch ( r->in.control ) {
553         case SVCCTL_CONTROL_STOP:
554                 if ( !(info->access_granted & SC_RIGHT_SVC_STOP) )
555                         return WERR_ACCESS_DENIED;
556
557                 return info->ops->stop_service( info->name,
558                                                 r->out.service_status );
559
560         case SVCCTL_CONTROL_INTERROGATE:
561                 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
562                         return WERR_ACCESS_DENIED;
563
564                 return info->ops->service_status( info->name,
565                                                   r->out.service_status );
566         default:
567                 return WERR_INVALID_PARAMETER;
568         }
569 }
570
571 /********************************************************************
572  _svcctl_EnumDependentServicesW
573 ********************************************************************/
574
575 WERROR _svcctl_EnumDependentServicesW(struct pipes_struct *p,
576                                       struct svcctl_EnumDependentServicesW *r)
577 {
578         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.service );
579
580         /* perform access checks */
581
582         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
583                 return WERR_INVALID_HANDLE;
584
585         if ( !(info->access_granted & SC_RIGHT_SVC_ENUMERATE_DEPENDENTS) )
586                 return WERR_ACCESS_DENIED;
587
588         switch (r->in.state) {
589         case SERVICE_STATE_ACTIVE:
590         case SERVICE_STATE_INACTIVE:
591         case SERVICE_STATE_ALL:
592                 break;
593         default:
594                 return WERR_INVALID_PARAMETER;
595         }
596
597         /* we have to set the outgoing buffer size to the same as the
598            incoming buffer size (even in the case of failure */
599         /* this is done in the autogenerated server already - gd */
600
601         *r->out.needed = r->in.offered;
602
603         /* no dependent services...basically a stub function */
604         *r->out.services_returned = 0;
605
606         return WERR_OK;
607 }
608
609 /********************************************************************
610  _svcctl_QueryServiceStatusEx
611 ********************************************************************/
612
613 WERROR _svcctl_QueryServiceStatusEx(struct pipes_struct *p,
614                                     struct svcctl_QueryServiceStatusEx *r)
615 {
616         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
617         uint32_t buffer_size;
618
619         /* perform access checks */
620
621         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
622                 return WERR_INVALID_HANDLE;
623
624         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
625                 return WERR_ACCESS_DENIED;
626
627         /* we have to set the outgoing buffer size to the same as the
628            incoming buffer size (even in the case of failure) */
629         *r->out.needed = r->in.offered;
630
631         switch ( r->in.info_level ) {
632                 case SVC_STATUS_PROCESS_INFO:
633                 {
634                         struct SERVICE_STATUS_PROCESS svc_stat_proc;
635                         enum ndr_err_code ndr_err;
636                         DATA_BLOB blob;
637
638                         /* Get the status of the service.. */
639                         info->ops->service_status( info->name, &svc_stat_proc.status );
640                         svc_stat_proc.process_id     = getpid();
641                         svc_stat_proc.service_flags  = 0x0;
642
643                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, &svc_stat_proc,
644                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_STATUS_PROCESS);
645                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
646                                 return WERR_INVALID_PARAMETER;
647                         }
648
649                         r->out.buffer = blob.data;
650                         buffer_size = sizeof(struct SERVICE_STATUS_PROCESS);
651                         break;
652                 }
653
654                 default:
655                         return WERR_INVALID_LEVEL;
656         }
657
658
659         buffer_size += buffer_size % 4;
660         *r->out.needed = (buffer_size > r->in.offered) ? buffer_size : r->in.offered;
661
662         if (buffer_size > r->in.offered ) {
663                 return WERR_INSUFFICIENT_BUFFER;
664         }
665
666         return WERR_OK;
667 }
668
669 /********************************************************************
670 ********************************************************************/
671
672 static WERROR fill_svc_config(TALLOC_CTX *mem_ctx,
673                               struct messaging_context *msg_ctx,
674                               struct auth_session_info *session_info,
675                               const char *name,
676                               struct QUERY_SERVICE_CONFIG *config)
677 {
678         const char *result = NULL;
679
680         /* now fill in the individual values */
681
682         ZERO_STRUCTP(config);
683
684         config->displayname = svcctl_lookup_dispname(mem_ctx,
685                                                      msg_ctx,
686                                                      session_info,
687                                                      name);
688
689         result = svcctl_get_string_value(mem_ctx,
690                                          msg_ctx,
691                                          session_info,
692                                          name,
693                                          "ObjectName");
694         if (result != NULL) {
695                 config->startname = result;
696         }
697
698         result = svcctl_get_string_value(mem_ctx,
699                                          msg_ctx,
700                                          session_info,
701                                          name,
702                                          "ImagePath");
703         if (result != NULL) {
704                 config->executablepath = result;
705         }
706
707         /* a few hard coded values */
708         /* loadordergroup and dependencies are empty */
709
710         config->tag_id           = 0x00000000;                  /* unassigned loadorder group */
711         config->service_type     = SERVICE_TYPE_WIN32_OWN_PROCESS;
712         config->error_control    = SVCCTL_SVC_ERROR_NORMAL;
713
714         /* set the start type.  NetLogon and WINS are disabled to prevent
715            the client from showing the "Start" button (if of course the services
716            are not running */
717
718         if ( strequal( name, "NETLOGON" ) && ( lp_servicenumber(name) == -1 ) )
719                 config->start_type = SVCCTL_DISABLED;
720         else if ( strequal( name, "WINS" ) && ( !lp_we_are_a_wins_server() ))
721                 config->start_type = SVCCTL_DISABLED;
722         else
723                 config->start_type = SVCCTL_DEMAND_START;
724
725         return WERR_OK;
726 }
727
728 /********************************************************************
729  _svcctl_QueryServiceConfigW
730 ********************************************************************/
731
732 WERROR _svcctl_QueryServiceConfigW(struct pipes_struct *p,
733                                    struct svcctl_QueryServiceConfigW *r)
734 {
735         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
736         uint32_t buffer_size;
737         WERROR wresult;
738
739         /* perform access checks */
740
741         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
742                 return WERR_INVALID_HANDLE;
743
744         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
745                 return WERR_ACCESS_DENIED;
746
747         /* we have to set the outgoing buffer size to the same as the
748            incoming buffer size (even in the case of failure */
749
750         *r->out.needed = r->in.offered;
751
752         wresult = fill_svc_config(p->mem_ctx,
753                                   p->msg_ctx,
754                                   p->session_info,
755                                   info->name,
756                                   r->out.query);
757         if ( !W_ERROR_IS_OK(wresult) )
758                 return wresult;
759
760         buffer_size = ndr_size_QUERY_SERVICE_CONFIG(r->out.query, 0);
761         *r->out.needed = (buffer_size > r->in.offered) ? buffer_size : r->in.offered;
762
763         if (buffer_size > r->in.offered ) {
764                 ZERO_STRUCTP(r->out.query);
765                 return WERR_INSUFFICIENT_BUFFER;
766         }
767
768         return WERR_OK;
769 }
770
771 /********************************************************************
772  _svcctl_QueryServiceConfig2W
773 ********************************************************************/
774
775 WERROR _svcctl_QueryServiceConfig2W(struct pipes_struct *p,
776                                     struct svcctl_QueryServiceConfig2W *r)
777 {
778         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
779         uint32_t buffer_size;
780         DATA_BLOB blob = data_blob_null;
781
782         /* perform access checks */
783
784         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
785                 return WERR_INVALID_HANDLE;
786
787         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
788                 return WERR_ACCESS_DENIED;
789
790         /* we have to set the outgoing buffer size to the same as the
791            incoming buffer size (even in the case of failure */
792         *r->out.needed = r->in.offered;
793
794         switch ( r->in.info_level ) {
795         case SERVICE_CONFIG_DESCRIPTION:
796                 {
797                         struct SERVICE_DESCRIPTION desc_buf;
798                         const char *description;
799                         enum ndr_err_code ndr_err;
800
801                         description = svcctl_lookup_description(p->mem_ctx,
802                                                                 p->msg_ctx,
803                                                                 p->session_info,
804                                                                 info->name);
805
806                         desc_buf.description = description;
807
808                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, &desc_buf,
809                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_DESCRIPTION);
810                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
811                                 return WERR_INVALID_PARAMETER;
812                         }
813
814                         break;
815                 }
816                 break;
817         case SERVICE_CONFIG_FAILURE_ACTIONS:
818                 {
819                         struct SERVICE_FAILURE_ACTIONS actions;
820                         enum ndr_err_code ndr_err;
821
822                         /* nothing to say...just service the request */
823
824                         ZERO_STRUCT( actions );
825
826                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, &actions,
827                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_FAILURE_ACTIONS);
828                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
829                                 return WERR_INVALID_PARAMETER;
830                         }
831
832                         break;
833                 }
834                 break;
835
836         default:
837                 return WERR_INVALID_LEVEL;
838         }
839
840         buffer_size = blob.length;
841         buffer_size += buffer_size % 4;
842         *r->out.needed = (buffer_size > r->in.offered) ? buffer_size : r->in.offered;
843
844         if (buffer_size > r->in.offered)
845                 return WERR_INSUFFICIENT_BUFFER;
846
847         memcpy(r->out.buffer, blob.data, blob.length);
848
849         return WERR_OK;
850 }
851
852 /********************************************************************
853  _svcctl_LockServiceDatabase
854 ********************************************************************/
855
856 WERROR _svcctl_LockServiceDatabase(struct pipes_struct *p,
857                                    struct svcctl_LockServiceDatabase *r)
858 {
859         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
860
861         /* perform access checks */
862
863         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
864                 return WERR_INVALID_HANDLE;
865
866         if ( !(info->access_granted & SC_RIGHT_MGR_LOCK) )
867                 return WERR_ACCESS_DENIED;
868
869         /* Just open a handle.  Doesn't actually lock anything */
870
871         return create_open_service_handle( p, r->out.lock, SVC_HANDLE_IS_DBLOCK, NULL, 0 );
872 }
873
874 /********************************************************************
875  _svcctl_UnlockServiceDatabase
876 ********************************************************************/
877
878 WERROR _svcctl_UnlockServiceDatabase(struct pipes_struct *p,
879                                      struct svcctl_UnlockServiceDatabase *r)
880 {
881         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.lock );
882
883
884         if ( !info || (info->type != SVC_HANDLE_IS_DBLOCK) )
885                 return WERR_INVALID_HANDLE;
886
887         return close_policy_hnd( p, r->out.lock) ? WERR_OK : WERR_INVALID_HANDLE;
888 }
889
890 /********************************************************************
891  _svcctl_QueryServiceObjectSecurity
892 ********************************************************************/
893
894 WERROR _svcctl_QueryServiceObjectSecurity(struct pipes_struct *p,
895                                           struct svcctl_QueryServiceObjectSecurity *r)
896 {
897         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
898         struct security_descriptor *sec_desc;
899         NTSTATUS status;
900         uint8_t *buffer = NULL;
901         size_t len = 0;
902
903
904         /* only support the SCM and individual services */
905
906         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM)) )
907                 return WERR_INVALID_HANDLE;
908
909         /* check access reights (according to MSDN) */
910
911         if ( !(info->access_granted & SEC_STD_READ_CONTROL) )
912                 return WERR_ACCESS_DENIED;
913
914         /* TODO: handle something besides SECINFO_DACL */
915
916         if ( (r->in.security_flags & SECINFO_DACL) != SECINFO_DACL )
917                 return WERR_INVALID_PARAMETER;
918
919         /* Lookup the security descriptor and marshall it up for a reply */
920         sec_desc = svcctl_get_secdesc(p->mem_ctx,
921                                       p->msg_ctx,
922                                       get_session_info_system(),
923                                       info->name);
924         if (sec_desc == NULL) {
925                 return WERR_NOT_ENOUGH_MEMORY;
926         }
927
928         *r->out.needed = ndr_size_security_descriptor(sec_desc, 0);
929
930         if ( *r->out.needed > r->in.offered) {
931                 return WERR_INSUFFICIENT_BUFFER;
932         }
933
934         status = marshall_sec_desc(p->mem_ctx, sec_desc, &buffer, &len);
935         if (!NT_STATUS_IS_OK(status)) {
936                 return ntstatus_to_werror(status);
937         }
938
939         *r->out.needed = len;
940         memcpy(r->out.buffer, buffer, len);
941
942         return WERR_OK;
943 }
944
945 /********************************************************************
946  _svcctl_SetServiceObjectSecurity
947 ********************************************************************/
948
949 WERROR _svcctl_SetServiceObjectSecurity(struct pipes_struct *p,
950                                         struct svcctl_SetServiceObjectSecurity *r)
951 {
952         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
953         struct security_descriptor *sec_desc = NULL;
954         uint32_t required_access;
955         NTSTATUS status;
956
957         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM))  )
958                 return WERR_INVALID_HANDLE;
959
960         /* can't set the security de4scriptor on the ServiceControlManager */
961
962         if ( info->type == SVC_HANDLE_IS_SCM )
963                 return WERR_ACCESS_DENIED;
964
965         /* check the access on the open handle */
966
967         switch ( r->in.security_flags ) {
968                 case SECINFO_DACL:
969                         required_access = SEC_STD_WRITE_DAC;
970                         break;
971
972                 case SECINFO_OWNER:
973                 case SECINFO_GROUP:
974                         required_access = SEC_STD_WRITE_OWNER;
975                         break;
976
977                 case SECINFO_SACL:
978                         return WERR_INVALID_PARAMETER;
979                 default:
980                         return WERR_INVALID_PARAMETER;
981         }
982
983         if ( !(info->access_granted & required_access) )
984                 return WERR_ACCESS_DENIED;
985
986         /* read the security descfriptor */
987
988         status = unmarshall_sec_desc(p->mem_ctx,
989                                      r->in.buffer,
990                                      r->in.offered,
991                                      &sec_desc);
992         if (!NT_STATUS_IS_OK(status)) {
993                 return ntstatus_to_werror(status);
994         }
995
996         /* store the new SD */
997
998         if (!svcctl_set_secdesc(p->msg_ctx, p->session_info, info->name, sec_desc))
999                 return WERR_ACCESS_DENIED;
1000
1001         return WERR_OK;
1002 }
1003
1004
1005 WERROR _svcctl_DeleteService(struct pipes_struct *p,
1006                              struct svcctl_DeleteService *r)
1007 {
1008         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1009         return WERR_NOT_SUPPORTED;
1010 }
1011
1012 WERROR _svcctl_SetServiceStatus(struct pipes_struct *p,
1013                                 struct svcctl_SetServiceStatus *r)
1014 {
1015         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1016         return WERR_NOT_SUPPORTED;
1017 }
1018
1019 WERROR _svcctl_NotifyBootConfigStatus(struct pipes_struct *p,
1020                                       struct svcctl_NotifyBootConfigStatus *r)
1021 {
1022         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1023         return WERR_NOT_SUPPORTED;
1024 }
1025
1026 WERROR _svcctl_SCSetServiceBitsW(struct pipes_struct *p,
1027                                  struct svcctl_SCSetServiceBitsW *r)
1028 {
1029         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1030         return WERR_NOT_SUPPORTED;
1031 }
1032
1033 WERROR _svcctl_ChangeServiceConfigW(struct pipes_struct *p,
1034                                     struct svcctl_ChangeServiceConfigW *r)
1035 {
1036         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1037         return WERR_NOT_SUPPORTED;
1038 }
1039
1040 WERROR _svcctl_CreateServiceW(struct pipes_struct *p,
1041                               struct svcctl_CreateServiceW *r)
1042 {
1043         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1044         return WERR_NOT_SUPPORTED;
1045 }
1046
1047 WERROR _svcctl_QueryServiceLockStatusW(struct pipes_struct *p,
1048                                        struct svcctl_QueryServiceLockStatusW *r)
1049 {
1050         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1051         return WERR_NOT_SUPPORTED;
1052 }
1053
1054 WERROR _svcctl_GetServiceKeyNameW(struct pipes_struct *p,
1055                                   struct svcctl_GetServiceKeyNameW *r)
1056 {
1057         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1058         return WERR_NOT_SUPPORTED;
1059 }
1060
1061 WERROR _svcctl_SCSetServiceBitsA(struct pipes_struct *p,
1062                                  struct svcctl_SCSetServiceBitsA *r)
1063 {
1064         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1065         return WERR_NOT_SUPPORTED;
1066 }
1067
1068 WERROR _svcctl_ChangeServiceConfigA(struct pipes_struct *p,
1069                                     struct svcctl_ChangeServiceConfigA *r)
1070 {
1071         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1072         return WERR_NOT_SUPPORTED;
1073 }
1074
1075 WERROR _svcctl_CreateServiceA(struct pipes_struct *p,
1076                               struct svcctl_CreateServiceA *r)
1077 {
1078         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1079         return WERR_NOT_SUPPORTED;
1080 }
1081
1082 WERROR _svcctl_EnumDependentServicesA(struct pipes_struct *p,
1083                                       struct svcctl_EnumDependentServicesA *r)
1084 {
1085         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1086         return WERR_NOT_SUPPORTED;
1087 }
1088
1089 WERROR _svcctl_EnumServicesStatusA(struct pipes_struct *p,
1090                                    struct svcctl_EnumServicesStatusA *r)
1091 {
1092         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1093         return WERR_NOT_SUPPORTED;
1094 }
1095
1096 WERROR _svcctl_OpenSCManagerA(struct pipes_struct *p,
1097                               struct svcctl_OpenSCManagerA *r)
1098 {
1099         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1100         return WERR_NOT_SUPPORTED;
1101 }
1102
1103 WERROR _svcctl_OpenServiceA(struct pipes_struct *p,
1104                             struct svcctl_OpenServiceA *r)
1105 {
1106         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1107         return WERR_NOT_SUPPORTED;
1108 }
1109
1110 WERROR _svcctl_QueryServiceConfigA(struct pipes_struct *p,
1111                                    struct svcctl_QueryServiceConfigA *r)
1112 {
1113         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1114         return WERR_NOT_SUPPORTED;
1115 }
1116
1117 WERROR _svcctl_QueryServiceLockStatusA(struct pipes_struct *p,
1118                                        struct svcctl_QueryServiceLockStatusA *r)
1119 {
1120         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1121         return WERR_NOT_SUPPORTED;
1122 }
1123
1124 WERROR _svcctl_StartServiceA(struct pipes_struct *p,
1125                              struct svcctl_StartServiceA *r)
1126 {
1127         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1128         return WERR_NOT_SUPPORTED;
1129 }
1130
1131 WERROR _svcctl_GetServiceDisplayNameA(struct pipes_struct *p,
1132                                       struct svcctl_GetServiceDisplayNameA *r)
1133 {
1134         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1135         return WERR_NOT_SUPPORTED;
1136 }
1137
1138 WERROR _svcctl_GetServiceKeyNameA(struct pipes_struct *p,
1139                                   struct svcctl_GetServiceKeyNameA *r)
1140 {
1141         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1142         return WERR_NOT_SUPPORTED;
1143 }
1144
1145 WERROR _svcctl_GetCurrentGroupeStateW(struct pipes_struct *p,
1146                                       struct svcctl_GetCurrentGroupeStateW *r)
1147 {
1148         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1149         return WERR_NOT_SUPPORTED;
1150 }
1151
1152 WERROR _svcctl_EnumServiceGroupW(struct pipes_struct *p,
1153                                  struct svcctl_EnumServiceGroupW *r)
1154 {
1155         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1156         return WERR_NOT_SUPPORTED;
1157 }
1158
1159 WERROR _svcctl_ChangeServiceConfig2A(struct pipes_struct *p,
1160                                      struct svcctl_ChangeServiceConfig2A *r)
1161 {
1162         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1163         return WERR_NOT_SUPPORTED;
1164 }
1165
1166 WERROR _svcctl_ChangeServiceConfig2W(struct pipes_struct *p,
1167                                      struct svcctl_ChangeServiceConfig2W *r)
1168 {
1169         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1170         return WERR_NOT_SUPPORTED;
1171 }
1172
1173 WERROR _svcctl_QueryServiceConfig2A(struct pipes_struct *p,
1174                                     struct svcctl_QueryServiceConfig2A *r)
1175 {
1176         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1177         return WERR_NOT_SUPPORTED;
1178 }
1179
1180 WERROR _EnumServicesStatusExA(struct pipes_struct *p,
1181                               struct EnumServicesStatusExA *r)
1182 {
1183         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1184         return WERR_NOT_SUPPORTED;
1185 }
1186
1187 WERROR _EnumServicesStatusExW(struct pipes_struct *p,
1188                               struct EnumServicesStatusExW *r)
1189 {
1190         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1191         return WERR_NOT_SUPPORTED;
1192 }
1193
1194 WERROR _svcctl_SCSendTSMessage(struct pipes_struct *p,
1195                                struct svcctl_SCSendTSMessage *r)
1196 {
1197         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1198         return WERR_NOT_SUPPORTED;
1199 }