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