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