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