Remove tiny code duplication
[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  *  
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_open_scmanager(pipes_struct *p, SVCCTL_Q_OPEN_SCMANAGER *q_u, SVCCTL_R_OPEN_SCMANAGER *r_u)
267 {
268         SEC_DESC *sec_desc;
269         uint32 access_granted = 0;
270         NTSTATUS status;
271         
272         /* perform access checks */
273         
274         if ( !(sec_desc = construct_scm_sd( p->mem_ctx )) )
275                 return WERR_NOMEM;
276                 
277         se_map_generic( &q_u->access, &scm_generic_map );
278         status = svcctl_access_check( sec_desc, p->pipe_user.nt_user_token, q_u->access, &access_granted );
279         if ( !NT_STATUS_IS_OK(status) )
280                 return ntstatus_to_werror( status );
281                 
282         return create_open_service_handle( p, &r_u->handle, SVC_HANDLE_IS_SCM, NULL, access_granted );
283 }
284
285 /********************************************************************
286 ********************************************************************/
287
288 WERROR _svcctl_open_service(pipes_struct *p, SVCCTL_Q_OPEN_SERVICE *q_u, SVCCTL_R_OPEN_SERVICE *r_u)
289 {
290         SEC_DESC *sec_desc;
291         uint32 access_granted = 0;
292         NTSTATUS status;
293         char *service = NULL;
294         size_t ret = rpcstr_pull_talloc(p->mem_ctx,
295                                         &service,
296                                         q_u->servicename.buffer,
297                                         q_u->servicename.uni_str_len*2,
298                                         0);
299
300         if (ret == (size_t)-1 || !service) {
301                 return WERR_NOMEM;
302         }
303         DEBUG(5, ("_svcctl_open_service: Attempting to open Service [%s], \n", service));
304
305         /* based on my tests you can open a service if you have a valid scm handle */
306
307         if ( !find_service_info_by_hnd( p, &q_u->handle ) )
308                 return WERR_BADFID;
309
310         /* perform access checks.  Use the root token in order to ensure that we 
311            retrieve the security descriptor */
312
313         if ( !(sec_desc = svcctl_get_secdesc( p->mem_ctx, service, get_root_nt_token() )) )
314                 return WERR_NOMEM;
315
316         se_map_generic( &q_u->access, &svc_generic_map );
317         status = svcctl_access_check( sec_desc, p->pipe_user.nt_user_token, q_u->access, &access_granted );
318         if ( !NT_STATUS_IS_OK(status) )
319                 return ntstatus_to_werror( status );
320
321         return create_open_service_handle( p, &r_u->handle, SVC_HANDLE_IS_SERVICE, service, access_granted );
322 }
323
324 /********************************************************************
325 ********************************************************************/
326
327 WERROR _svcctl_CloseServiceHandle(pipes_struct *p, struct svcctl_CloseServiceHandle *r)
328 {
329         if ( !close_policy_hnd( p, r->in.handle ) )
330                 return  WERR_BADFID;
331
332         return WERR_OK; 
333 }
334
335 /********************************************************************
336 ********************************************************************/
337
338 WERROR _svcctl_get_display_name(pipes_struct *p, SVCCTL_Q_GET_DISPLAY_NAME *q_u, SVCCTL_R_GET_DISPLAY_NAME *r_u)
339 {
340         fstring service;
341         const char *display_name;
342         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
343         
344         /* can only use an SCM handle here */
345         
346         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
347                 return WERR_BADFID;
348                 
349         rpcstr_pull(service, q_u->servicename.buffer, sizeof(service), q_u->servicename.uni_str_len*2, 0);
350         
351         display_name = svcctl_lookup_dispname(p->mem_ctx, service, p->pipe_user.nt_user_token );
352         init_svcctl_r_get_display_name( r_u, display_name ? display_name : "");
353
354         return WERR_OK;
355 }
356
357 /********************************************************************
358 ********************************************************************/
359
360 WERROR _svcctl_query_status(pipes_struct *p, SVCCTL_Q_QUERY_STATUS *q_u, SVCCTL_R_QUERY_STATUS *r_u)
361 {
362         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
363         
364         /* perform access checks */
365
366         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
367                 return WERR_BADFID;
368                 
369         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
370                 return WERR_ACCESS_DENIED;
371                 
372         /* try the service specific status call */
373
374         return info->ops->service_status( info->name, &r_u->svc_status );
375 }
376
377 /********************************************************************
378 ********************************************************************/
379
380 static int enumerate_status( TALLOC_CTX *ctx, ENUM_SERVICES_STATUS **status, NT_USER_TOKEN *token )
381 {
382         int num_services = 0;
383         int i;
384         ENUM_SERVICES_STATUS *st;
385         const char *display_name;
386         
387         /* just count */
388         while ( svcctl_ops[num_services].name )
389                 num_services++;
390
391         if ( !(st = TALLOC_ARRAY( ctx, ENUM_SERVICES_STATUS, num_services )) ) {
392                 DEBUG(0,("enumerate_status: talloc() failed!\n"));
393                 return -1;
394         }
395         
396         for ( i=0; i<num_services; i++ ) {
397                 init_unistr( &st[i].servicename, svcctl_ops[i].name );
398                 
399                 display_name = svcctl_lookup_dispname(ctx, svcctl_ops[i].name, token );
400                 init_unistr( &st[i].displayname, display_name ? display_name : "");
401                 
402                 svcctl_ops[i].ops->service_status( svcctl_ops[i].name, &st[i].status );
403         }
404         
405         *status = st;
406
407         return num_services;
408 }
409
410 /********************************************************************
411 ********************************************************************/
412
413 WERROR _svcctl_enum_services_status(pipes_struct *p, SVCCTL_Q_ENUM_SERVICES_STATUS *q_u, SVCCTL_R_ENUM_SERVICES_STATUS *r_u)
414 {
415         ENUM_SERVICES_STATUS *services = NULL;
416         int num_services;
417         int i = 0;
418         size_t buffer_size = 0;
419         WERROR result = WERR_OK;
420         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
421         NT_USER_TOKEN *token = p->pipe_user.nt_user_token;
422         
423         /* perform access checks */
424
425         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
426                 return WERR_BADFID;
427                 
428         if ( !(info->access_granted & SC_RIGHT_MGR_ENUMERATE_SERVICE) ) {
429                 return WERR_ACCESS_DENIED;
430         }
431
432         num_services = enumerate_status( p->mem_ctx, &services, token );
433         if (num_services == -1 ) {
434                 return WERR_NOMEM;
435         }
436
437         for ( i=0; i<num_services; i++ ) {
438                 buffer_size += svcctl_sizeof_enum_services_status(&services[i]);
439         }
440
441         buffer_size += buffer_size % 4;
442
443         if (buffer_size > q_u->buffer_size ) {
444                 num_services = 0;
445                 result = WERR_MORE_DATA;
446         }
447
448         rpcbuf_init(&r_u->buffer, q_u->buffer_size, p->mem_ctx);
449
450         if ( W_ERROR_IS_OK(result) ) {
451                 for ( i=0; i<num_services; i++ )
452                         svcctl_io_enum_services_status( "", &services[i], &r_u->buffer, 0 );
453         }
454
455         r_u->needed      = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
456         r_u->returned    = (uint32)num_services;
457
458         if ( !(r_u->resume = TALLOC_P( p->mem_ctx, uint32 )) )
459                 return WERR_NOMEM;
460
461         *r_u->resume = 0x0;
462
463         return result;
464 }
465
466 /********************************************************************
467 ********************************************************************/
468
469 WERROR _svcctl_start_service(pipes_struct *p, SVCCTL_Q_START_SERVICE *q_u, SVCCTL_R_START_SERVICE *r_u)
470 {
471         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
472         
473         /* perform access checks */
474
475         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
476                 return WERR_BADFID;
477         
478         if ( !(info->access_granted & SC_RIGHT_SVC_START) )
479                 return WERR_ACCESS_DENIED;
480                 
481         return info->ops->start_service( info->name );
482 }
483
484 /********************************************************************
485 ********************************************************************/
486
487 WERROR _svcctl_control_service(pipes_struct *p, SVCCTL_Q_CONTROL_SERVICE *q_u, SVCCTL_R_CONTROL_SERVICE *r_u)
488 {
489         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
490         
491         /* perform access checks */
492         
493         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
494                 return WERR_BADFID;     
495         
496         switch ( q_u->control ) {
497         case SVCCTL_CONTROL_STOP:
498                 if ( !(info->access_granted & SC_RIGHT_SVC_STOP) )
499                         return WERR_ACCESS_DENIED;
500                         
501                 return info->ops->stop_service( info->name, &r_u->svc_status );
502                 
503         case SVCCTL_CONTROL_INTERROGATE:
504                 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
505                         return WERR_ACCESS_DENIED;
506                         
507                 return info->ops->service_status( info->name, &r_u->svc_status );
508         }
509         
510         /* default control action */
511         
512         return WERR_ACCESS_DENIED;
513 }
514
515 /********************************************************************
516 ********************************************************************/
517
518 WERROR _svcctl_enum_dependent_services( pipes_struct *p, SVCCTL_Q_ENUM_DEPENDENT_SERVICES *q_u, SVCCTL_R_ENUM_DEPENDENT_SERVICES *r_u )
519 {
520         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
521         
522         /* perform access checks */
523
524         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
525                 return WERR_BADFID;     
526         
527         if ( !(info->access_granted & SC_RIGHT_SVC_ENUMERATE_DEPENDENTS) )
528                 return WERR_ACCESS_DENIED;
529                         
530         /* we have to set the outgoing buffer size to the same as the 
531            incoming buffer size (even in the case of failure */
532
533         rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
534                                 
535         r_u->needed      = q_u->buffer_size;
536         
537         /* no dependent services...basically a stub function */
538         r_u->returned    = 0;
539
540         return WERR_OK;
541 }
542
543 /********************************************************************
544 ********************************************************************/
545
546 WERROR _svcctl_query_service_status_ex( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_STATUSEX *q_u, SVCCTL_R_QUERY_SERVICE_STATUSEX *r_u )
547 {
548         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
549         uint32 buffer_size;
550         
551         /* perform access checks */
552
553         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
554                 return WERR_BADFID;     
555         
556         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
557                 return WERR_ACCESS_DENIED;
558
559         /* we have to set the outgoing buffer size to the same as the 
560            incoming buffer size (even in the case of failure) */
561
562         rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
563         r_u->needed = q_u->buffer_size;
564         
565         switch ( q_u->level ) {
566                 case SVC_STATUS_PROCESS_INFO:
567                 {
568                         SERVICE_STATUS_PROCESS svc_stat_proc;
569
570                         /* Get the status of the service.. */
571                         info->ops->service_status( info->name, &svc_stat_proc.status );
572                         svc_stat_proc.process_id     = sys_getpid();
573                         svc_stat_proc.service_flags  = 0x0;
574
575                         svcctl_io_service_status_process( "", &svc_stat_proc, &r_u->buffer, 0 );
576                         buffer_size = sizeof(SERVICE_STATUS_PROCESS);
577                         break;
578                 }
579                         
580                 default:
581                         return WERR_UNKNOWN_LEVEL; 
582         }
583
584         
585         buffer_size += buffer_size % 4;
586         r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
587
588         if (buffer_size > q_u->buffer_size ) 
589                 return WERR_MORE_DATA;
590         
591         return WERR_OK;
592 }
593
594 /********************************************************************
595 ********************************************************************/
596
597 static WERROR fill_svc_config( TALLOC_CTX *ctx, const char *name, SERVICE_CONFIG *config, NT_USER_TOKEN *token )
598 {
599         REGVAL_CTR *values;
600         REGISTRY_VALUE *val;
601
602         /* retrieve the registry values for this service */
603         
604         if ( !(values = svcctl_fetch_regvalues( name, token )) )
605                 return WERR_REG_CORRUPT;
606         
607         /* now fill in the individual values */
608                 
609         config->displayname = TALLOC_ZERO_P( ctx, UNISTR2 );
610         if ( (val = regval_ctr_getvalue( values, "DisplayName" )) != NULL )
611                 init_unistr2( config->displayname, regval_sz( val ), UNI_STR_TERMINATE );
612         else
613                 init_unistr2( config->displayname, name, UNI_STR_TERMINATE );
614
615         if ( (val = regval_ctr_getvalue( values, "ObjectName" )) != NULL ) {
616                 config->startname = TALLOC_ZERO_P( ctx, UNISTR2 );              
617                 init_unistr2( config->startname, regval_sz( val ), UNI_STR_TERMINATE );
618         }
619                 
620         if ( (val = regval_ctr_getvalue( values, "ImagePath" )) != NULL ) {
621                 config->executablepath = TALLOC_ZERO_P( ctx, UNISTR2 );         
622                 init_unistr2( config->executablepath, regval_sz( val ), UNI_STR_TERMINATE );
623         }
624
625         /* a few hard coded values */
626         /* loadordergroup and dependencies are empty */
627         
628         config->tag_id           = 0x00000000;                  /* unassigned loadorder group */
629         config->service_type     = SVCCTL_WIN32_OWN_PROC;
630         config->error_control    = SVCCTL_SVC_ERROR_NORMAL;
631
632         /* set the start type.  NetLogon and WINS are disabled to prevent 
633            the client from showing the "Start" button (if of course the services
634            are not running */
635
636         if ( strequal( name, "NETLOGON" ) && ( lp_servicenumber(name) == -1 ) )
637                 config->start_type = SVCCTL_DISABLED;
638         else if ( strequal( name, "WINS" ) && ( !lp_wins_support() ))
639                 config->start_type = SVCCTL_DISABLED;
640         else
641                 config->start_type = SVCCTL_DEMAND_START;
642         
643
644         TALLOC_FREE( values );
645
646         return WERR_OK;
647 }
648
649 /********************************************************************
650 ********************************************************************/
651
652 WERROR _svcctl_query_service_config( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CONFIG *q_u, SVCCTL_R_QUERY_SERVICE_CONFIG *r_u )
653 {
654         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
655         uint32 buffer_size;
656         WERROR wresult;
657         
658         /* perform access checks */
659
660         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
661                 return WERR_BADFID;     
662         
663         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
664                 return WERR_ACCESS_DENIED;
665
666         /* we have to set the outgoing buffer size to the same as the 
667            incoming buffer size (even in the case of failure */
668
669         r_u->needed      = q_u->buffer_size;
670         
671         wresult = fill_svc_config( p->mem_ctx, info->name, &r_u->config, p->pipe_user.nt_user_token );
672         if ( !W_ERROR_IS_OK(wresult) )
673                 return wresult;
674         
675         buffer_size = svcctl_sizeof_service_config( &r_u->config );
676         r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
677
678         if (buffer_size > q_u->buffer_size ) {
679                 ZERO_STRUCTP( &r_u->config );
680                 return WERR_INSUFFICIENT_BUFFER;
681         }
682                 
683         return WERR_OK;
684 }
685
686 /********************************************************************
687 ********************************************************************/
688
689 WERROR _svcctl_query_service_config2( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CONFIG2 *q_u, SVCCTL_R_QUERY_SERVICE_CONFIG2 *r_u )
690 {
691         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
692         uint32 buffer_size;
693
694         /* perform access checks */
695
696         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
697                 return WERR_BADFID;
698
699         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
700                 return WERR_ACCESS_DENIED;
701
702         /* we have to set the outgoing buffer size to the same as the
703            incoming buffer size (even in the case of failure */
704
705         rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
706         r_u->needed = q_u->buffer_size;
707
708         switch ( q_u->level ) {
709         case SERVICE_CONFIG_DESCRIPTION:
710                 {
711                         SERVICE_DESCRIPTION desc_buf;
712                         const char *description;
713
714                         description = svcctl_lookup_description(p->mem_ctx, info->name, p->pipe_user.nt_user_token );
715
716                         ZERO_STRUCTP( &desc_buf );
717
718                         init_service_description_buffer( &desc_buf, description ? description : "");
719                         svcctl_io_service_description( "", &desc_buf, &r_u->buffer, 0 );
720                         buffer_size = svcctl_sizeof_service_description( &desc_buf );
721
722                         break;
723                 }
724                 break;
725         case SERVICE_CONFIG_FAILURE_ACTIONS:
726                 {
727                         SERVICE_FAILURE_ACTIONS actions;
728
729                         /* nothing to say...just service the request */
730
731                         ZERO_STRUCTP( &actions );
732                         svcctl_io_service_fa( "", &actions, &r_u->buffer, 0 );
733                         buffer_size = svcctl_sizeof_service_fa( &actions );
734
735                         break;
736                 }
737                 break;
738
739         default:
740                 return WERR_UNKNOWN_LEVEL;
741         }
742
743         buffer_size += buffer_size % 4;
744         r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
745
746         if (buffer_size > q_u->buffer_size )
747                 return WERR_INSUFFICIENT_BUFFER;
748
749         return WERR_OK;
750 }
751
752 /********************************************************************
753 ********************************************************************/
754
755 WERROR _svcctl_lock_service_db( pipes_struct *p, SVCCTL_Q_LOCK_SERVICE_DB *q_u, SVCCTL_R_LOCK_SERVICE_DB *r_u )
756 {
757         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
758         
759         /* perform access checks */
760
761         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
762                 return WERR_BADFID;     
763         
764         if ( !(info->access_granted & SC_RIGHT_MGR_LOCK) )
765                 return WERR_ACCESS_DENIED;
766
767         /* Just open a handle.  Doesn't actually lock anything */
768         
769         return create_open_service_handle( p, &r_u->h_lock, SVC_HANDLE_IS_DBLOCK, NULL, 0 );
770 ;
771 }
772
773 /********************************************************************
774 ********************************************************************/
775
776 WERROR _svcctl_unlock_service_db( pipes_struct *p, SVCCTL_Q_UNLOCK_SERVICE_DB *q_u, SVCCTL_R_UNLOCK_SERVICE_DB *r_u )
777 {
778         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->h_lock );
779
780
781         if ( !info || (info->type != SVC_HANDLE_IS_DBLOCK) )
782                 return WERR_BADFID;     
783                 
784         return close_policy_hnd( p, &q_u->h_lock) ? WERR_OK : WERR_BADFID;
785 }
786
787 /********************************************************************
788 ********************************************************************/
789
790 WERROR _svcctl_query_service_sec( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_SEC *q_u, SVCCTL_R_QUERY_SERVICE_SEC *r_u )
791 {
792         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
793         SEC_DESC *sec_desc;
794
795
796         /* only support the SCM and individual services */
797
798         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM)) )
799                 return WERR_BADFID;     
800
801         /* check access reights (according to MSDN) */
802
803         if ( !(info->access_granted & STD_RIGHT_READ_CONTROL_ACCESS) )
804                 return WERR_ACCESS_DENIED;
805
806         /* TODO: handle something besides DACL_SECURITY_INFORMATION */
807
808         if ( (q_u->security_flags & DACL_SECURITY_INFORMATION) != DACL_SECURITY_INFORMATION )
809                 return WERR_INVALID_PARAM;
810
811         /* lookup the security descriptor and marshall it up for a reply */
812
813         if ( !(sec_desc = svcctl_get_secdesc( p->mem_ctx, info->name, get_root_nt_token() )) )
814                 return WERR_NOMEM;
815
816         r_u->needed = ndr_size_security_descriptor( sec_desc, 0 );
817
818         if ( r_u->needed > q_u->buffer_size ) {
819                 ZERO_STRUCTP( &r_u->buffer );
820                 return WERR_INSUFFICIENT_BUFFER;
821         }
822
823         rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
824
825         if ( !sec_io_desc("", &sec_desc, &r_u->buffer.prs, 0 ) )
826                 return WERR_NOMEM;
827                 
828         return WERR_OK;
829 }
830
831 /********************************************************************
832 ********************************************************************/
833
834 WERROR _svcctl_set_service_sec( pipes_struct *p, SVCCTL_Q_SET_SERVICE_SEC *q_u, SVCCTL_R_SET_SERVICE_SEC *r_u )
835 {
836         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
837         SEC_DESC *sec_desc = NULL;
838         uint32 required_access;
839
840         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM))  )
841                 return WERR_BADFID;
842
843         /* can't set the security de4scriptor on the ServiceControlManager */
844
845         if ( info->type == SVC_HANDLE_IS_SCM )
846                 return WERR_ACCESS_DENIED;      
847
848         /* check the access on the open handle */
849         
850         switch ( q_u->security_flags ) {
851                 case DACL_SECURITY_INFORMATION:
852                         required_access = STD_RIGHT_WRITE_DAC_ACCESS;
853                         break;
854                         
855                 case OWNER_SECURITY_INFORMATION:
856                 case GROUP_SECURITY_INFORMATION:
857                         required_access = STD_RIGHT_WRITE_OWNER_ACCESS;
858                         break;
859                         
860                 case SACL_SECURITY_INFORMATION:
861                         return WERR_INVALID_PARAM;
862                 default:
863                         return WERR_INVALID_PARAM;
864         }
865         
866         if ( !(info->access_granted & required_access) )
867                 return WERR_ACCESS_DENIED;
868         
869         /* read the security descfriptor */
870                 
871         if ( !sec_io_desc("", &sec_desc, &q_u->buffer.prs, 0 ) )
872                 return WERR_NOMEM;
873                 
874         /* store the new SD */
875
876         if ( !svcctl_set_secdesc( p->mem_ctx, info->name, sec_desc, p->pipe_user.nt_user_token ) ) 
877                 return WERR_ACCESS_DENIED;
878
879         return WERR_OK;
880 }
881
882
883 WERROR _svcctl_ControlService(pipes_struct *p, struct svcctl_ControlService *r)
884 {
885         p->rng_fault_state = True;
886         return WERR_NOT_SUPPORTED;      
887 }
888
889 WERROR _svcctl_DeleteService(pipes_struct *p, struct svcctl_DeleteService *r)
890 {
891         p->rng_fault_state = True;
892         return WERR_NOT_SUPPORTED;      
893 }
894
895 WERROR _svcctl_LockServiceDatabase(pipes_struct *p, struct svcctl_LockServiceDatabase *r)
896 {
897         p->rng_fault_state = True;
898         return WERR_NOT_SUPPORTED;      
899 }
900
901 WERROR _svcctl_QueryServiceObjectSecurity(pipes_struct *p, struct svcctl_QueryServiceObjectSecurity *r)
902 {
903         p->rng_fault_state = True;
904         return WERR_NOT_SUPPORTED;      
905 }
906
907 WERROR _svcctl_SetServiceObjectSecurity(pipes_struct *p, struct svcctl_SetServiceObjectSecurity *r)
908 {
909         p->rng_fault_state = True;
910         return WERR_NOT_SUPPORTED;      
911 }
912
913 WERROR _svcctl_QueryServiceStatus(pipes_struct *p, struct svcctl_QueryServiceStatus *r)
914 {
915         p->rng_fault_state = True;
916         return WERR_NOT_SUPPORTED;      
917 }
918
919 WERROR _svcctl_SetServiceStatus(pipes_struct *p, struct svcctl_SetServiceStatus *r)
920 {
921         p->rng_fault_state = True;
922         return WERR_NOT_SUPPORTED;      
923 }
924
925 WERROR _svcctl_UnlockServiceDatabase(pipes_struct *p, struct svcctl_UnlockServiceDatabase *r)
926 {
927         p->rng_fault_state = True;
928         return WERR_NOT_SUPPORTED;      
929 }
930
931 WERROR _svcctl_NotifyBootConfigStatus(pipes_struct *p, struct svcctl_NotifyBootConfigStatus *r)
932 {
933         p->rng_fault_state = True;
934         return WERR_NOT_SUPPORTED;      
935 }
936
937 WERROR _svcctl_SCSetServiceBitsW(pipes_struct *p, struct svcctl_SCSetServiceBitsW *r)
938 {
939         p->rng_fault_state = True;
940         return WERR_NOT_SUPPORTED;      
941 }
942
943 WERROR _svcctl_ChangeServiceConfigW(pipes_struct *p, struct svcctl_ChangeServiceConfigW *r)
944 {
945         p->rng_fault_state = True;
946         return WERR_NOT_SUPPORTED;      
947 }
948
949 WERROR _svcctl_CreateServiceW(pipes_struct *p, struct svcctl_CreateServiceW *r)
950 {
951         p->rng_fault_state = True;
952         return WERR_NOT_SUPPORTED;      
953 }
954
955 WERROR _svcctl_EnumDependentServicesW(pipes_struct *p, struct svcctl_EnumDependentServicesW *r)
956 {
957         p->rng_fault_state = True;
958         return WERR_NOT_SUPPORTED;      
959 }
960
961 WERROR _svcctl_EnumServicesStatusW(pipes_struct *p, struct svcctl_EnumServicesStatusW *r)
962 {
963         p->rng_fault_state = True;
964         return WERR_NOT_SUPPORTED;      
965 }
966
967 WERROR _svcctl_OpenSCManagerW(pipes_struct *p, struct svcctl_OpenSCManagerW *r)
968 {
969         p->rng_fault_state = True;
970         return WERR_NOT_SUPPORTED;      
971 }
972
973 WERROR _svcctl_OpenServiceW(pipes_struct *p, struct svcctl_OpenServiceW *r)
974 {
975         p->rng_fault_state = True;
976         return WERR_NOT_SUPPORTED;      
977 }
978
979 WERROR _svcctl_QueryServiceConfigW(pipes_struct *p, struct svcctl_QueryServiceConfigW *r)
980 {
981         p->rng_fault_state = True;
982         return WERR_NOT_SUPPORTED;      
983 }
984
985 WERROR _svcctl_QueryServiceLockStatusW(pipes_struct *p, struct svcctl_QueryServiceLockStatusW *r)
986 {
987         p->rng_fault_state = True;
988         return WERR_NOT_SUPPORTED;      
989 }
990
991 WERROR _svcctl_StartServiceW(pipes_struct *p, struct svcctl_StartServiceW *r)
992 {
993         p->rng_fault_state = True;
994         return WERR_NOT_SUPPORTED;      
995 }
996
997 WERROR _svcctl_GetServiceDisplayNameW(pipes_struct *p, struct svcctl_GetServiceDisplayNameW *r)
998 {
999         p->rng_fault_state = True;
1000         return WERR_NOT_SUPPORTED;      
1001 }
1002
1003 WERROR _svcctl_GetServiceKeyNameW(pipes_struct *p, struct svcctl_GetServiceKeyNameW *r)
1004 {
1005         p->rng_fault_state = True;
1006         return WERR_NOT_SUPPORTED;      
1007 }
1008
1009 WERROR _svcctl_SCSetServiceBitsA(pipes_struct *p, struct svcctl_SCSetServiceBitsA *r)
1010 {
1011         p->rng_fault_state = True;
1012         return WERR_NOT_SUPPORTED;      
1013 }
1014
1015 WERROR _svcctl_ChangeServiceConfigA(pipes_struct *p, struct svcctl_ChangeServiceConfigA *r)
1016 {
1017         p->rng_fault_state = True;
1018         return WERR_NOT_SUPPORTED;      
1019 }
1020
1021 WERROR _svcctl_CreateServiceA(pipes_struct *p, struct svcctl_CreateServiceA *r)
1022 {
1023         p->rng_fault_state = True;
1024         return WERR_NOT_SUPPORTED;      
1025 }
1026
1027 WERROR _svcctl_EnumDependentServicesA(pipes_struct *p, struct svcctl_EnumDependentServicesA *r)
1028 {
1029         p->rng_fault_state = True;
1030         return WERR_NOT_SUPPORTED;      
1031 }
1032
1033 WERROR _svcctl_EnumServicesStatusA(pipes_struct *p, struct svcctl_EnumServicesStatusA *r)
1034 {
1035         p->rng_fault_state = True;
1036         return WERR_NOT_SUPPORTED;      
1037 }
1038
1039 WERROR _svcctl_OpenSCManagerA(pipes_struct *p, struct svcctl_OpenSCManagerA *r)
1040 {
1041         p->rng_fault_state = True;
1042         return WERR_NOT_SUPPORTED;      
1043 }
1044
1045 WERROR _svcctl_OpenServiceA(pipes_struct *p, struct svcctl_OpenServiceA *r)
1046 {
1047         p->rng_fault_state = True;
1048         return WERR_NOT_SUPPORTED;      
1049 }
1050
1051 WERROR _svcctl_QueryServiceConfigA(pipes_struct *p, struct svcctl_QueryServiceConfigA *r)
1052 {
1053         p->rng_fault_state = True;
1054         return WERR_NOT_SUPPORTED;      
1055 }
1056
1057 WERROR _svcctl_QueryServiceLockStatusA(pipes_struct *p, struct svcctl_QueryServiceLockStatusA *r)
1058 {
1059         p->rng_fault_state = True;
1060         return WERR_NOT_SUPPORTED;      
1061 }
1062
1063 WERROR _svcctl_StartServiceA(pipes_struct *p, struct svcctl_StartServiceA *r)
1064 {
1065         p->rng_fault_state = True;
1066         return WERR_NOT_SUPPORTED;      
1067 }
1068
1069 WERROR _svcctl_GetServiceDisplayNameA(pipes_struct *p, struct svcctl_GetServiceDisplayNameA *r)
1070 {
1071         p->rng_fault_state = True;
1072         return WERR_NOT_SUPPORTED;      
1073 }
1074
1075 WERROR _svcctl_GetServiceKeyNameA(pipes_struct *p, struct svcctl_GetServiceKeyNameA *r)
1076 {
1077         p->rng_fault_state = True;
1078         return WERR_NOT_SUPPORTED;      
1079 }
1080
1081 WERROR _svcctl_GetCurrentGroupeStateW(pipes_struct *p, struct svcctl_GetCurrentGroupeStateW *r)
1082 {
1083         p->rng_fault_state = True;
1084         return WERR_NOT_SUPPORTED;      
1085 }
1086
1087 WERROR _svcctl_EnumServiceGroupW(pipes_struct *p, struct svcctl_EnumServiceGroupW *r)
1088 {
1089         p->rng_fault_state = True;
1090         return WERR_NOT_SUPPORTED;      
1091 }
1092
1093 WERROR _svcctl_ChangeServiceConfig2A(pipes_struct *p, struct svcctl_ChangeServiceConfig2A *r)
1094 {
1095         p->rng_fault_state = True;
1096         return WERR_NOT_SUPPORTED;      
1097 }
1098
1099 WERROR _svcctl_ChangeServiceConfig2W(pipes_struct *p, struct svcctl_ChangeServiceConfig2W *r)
1100 {
1101         p->rng_fault_state = True;
1102         return WERR_NOT_SUPPORTED;      
1103 }
1104
1105 WERROR _svcctl_QueryServiceConfig2A(pipes_struct *p, struct svcctl_QueryServiceConfig2A *r)
1106 {
1107         p->rng_fault_state = True;
1108         return WERR_NOT_SUPPORTED;      
1109 }
1110
1111 WERROR _svcctl_QueryServiceConfig2W(pipes_struct *p, struct svcctl_QueryServiceConfig2W *r)
1112 {
1113         p->rng_fault_state = True;
1114         return WERR_NOT_SUPPORTED;      
1115 }
1116
1117 WERROR _svcctl_QueryServiceStatusEx(pipes_struct *p, struct svcctl_QueryServiceStatusEx *r)
1118 {
1119         p->rng_fault_state = True;
1120         return WERR_NOT_SUPPORTED;      
1121 }
1122
1123 WERROR _EnumServicesStatusExA(pipes_struct *p, struct EnumServicesStatusExA *r)
1124 {
1125         p->rng_fault_state = True;
1126         return WERR_NOT_SUPPORTED;      
1127 }
1128
1129 WERROR _EnumServicesStatusExW(pipes_struct *p, struct EnumServicesStatusExW *r)
1130 {
1131         p->rng_fault_state = True;
1132         return WERR_NOT_SUPPORTED;      
1133 }
1134
1135 WERROR _svcctl_SCSendTSMessage(pipes_struct *p, struct svcctl_SCSendTSMessage *r)
1136 {
1137         p->rng_fault_state = True;
1138         return WERR_NOT_SUPPORTED;      
1139 }
1140