r7603: * fix a bug in the SERVICE_ALL_ACCESS security mask
[Samba/gbeck.git] / source / rpc_server / srv_svcctl_nt.c
blobc557036800071c5f5bb263313a71b7f99406443b
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Gerald (Jerry) Carter 2005,
5 * Copyright (C) Marcin Krzysztof Porwit 2005.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* TODO - Do the OpenService service name matching case-independently, or at least make it an option. */
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_SRV
30 #define SERVICEDB_VERSION_V1 1 /* Will there be more? */
31 #define INTERNAL_SERVICES_LIST "NETLOGON Spooler"
33 /* */
34 /* scripts will execute from the following libdir, if they are in the enable svcctl=<list of scripts> */
35 /* these should likely be symbolic links. Note that information about them will be extracted from the files themselves */
36 /* using the LSB standard keynames for various information */
38 #define SVCCTL_SCRIPT_DIR "/svcctl/"
41 struct service_control_op_table {
42 const char *name;
43 SERVICE_CONTROL_OPS *ops;
46 extern SERVICE_CONTROL_OPS spoolss_svc_ops;
48 struct service_control_op_table svcctl_ops[] = {
49 { "Spooler", &spoolss_svc_ops },
50 { "NETLOGON", NULL },
51 { NULL, NULL }
55 /********************************************************************
56 ********************************************************************/
58 static NTSTATUS svcctl_access_check( SEC_DESC *sec_desc, NT_USER_TOKEN *token,
59 uint32 access_desired, uint32 *access_granted )
61 NTSTATUS result;
63 /* maybe add privilege checks in here later */
65 se_access_check( sec_desc, token, access_desired, access_granted, &result );
67 return result;
70 /********************************************************************
71 ********************************************************************/
73 static SEC_DESC* construct_scm_sd( TALLOC_CTX *ctx )
75 SEC_ACE ace[2];
76 SEC_ACCESS mask;
77 size_t i = 0;
78 SEC_DESC *sd;
79 SEC_ACL *acl;
80 uint32 sd_size;
82 /* basic access for Everyone */
84 init_sec_access(&mask, SC_MANAGER_READ_ACCESS );
85 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
87 /* Full Access 'BUILTIN\Administrators' */
89 init_sec_access(&mask,SC_MANAGER_ALL_ACCESS );
90 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
93 /* create the security descriptor */
95 if ( !(acl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) )
96 return NULL;
98 if ( !(sd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, acl, &sd_size)) )
99 return NULL;
101 return sd;
104 /********************************************************************
105 ********************************************************************/
107 static SEC_DESC* construct_service_sd( TALLOC_CTX *ctx )
109 SEC_ACE ace[4];
110 SEC_ACCESS mask;
111 size_t i = 0;
112 SEC_DESC *sd;
113 SEC_ACL *acl;
114 uint32 sd_size;
116 /* basic access for Everyone */
118 init_sec_access(&mask, SERVICE_READ_ACCESS );
119 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
121 init_sec_access(&mask,SERVICE_EXECUTE_ACCESS );
122 init_sec_ace(&ace[i++], &global_sid_Builtin_Power_Users, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
124 init_sec_access(&mask,SERVICE_ALL_ACCESS );
125 init_sec_ace(&ace[i++], &global_sid_Builtin_Server_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
126 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
128 /* create the security descriptor */
130 if ( !(acl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) )
131 return NULL;
133 if ( !(sd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, acl, &sd_size)) )
134 return NULL;
136 return sd;
139 /******************************************************************
140 free() function for REGISTRY_KEY
141 *****************************************************************/
143 static void free_service_handle_info(void *ptr)
145 SERVICE_INFO *info = (SERVICE_INFO*)ptr;
147 SAFE_FREE(info->name);
148 SAFE_FREE(info);
151 /******************************************************************
152 Find a registry key handle and return a SERVICE_INFO
153 *****************************************************************/
155 static SERVICE_INFO *find_service_info_by_hnd(pipes_struct *p, POLICY_HND *hnd)
157 SERVICE_INFO *service_info = NULL;
159 if( !find_policy_by_hnd( p, hnd, (void **)&service_info) ) {
160 DEBUG(2,("find_service_info_by_hnd: handle not found"));
161 return NULL;
164 return service_info;
167 /******************************************************************
168 *****************************************************************/
170 static WERROR create_open_service_handle( pipes_struct *p, POLICY_HND *handle,
171 const char *service, uint32 access_granted )
173 SERVICE_INFO *info = NULL;
175 if ( !(info = SMB_MALLOC_P( SERVICE_INFO )) )
176 return WERR_NOMEM;
178 ZERO_STRUCTP( info );
180 /* the Service Manager has a NULL name */
182 if ( !service ) {
183 info->type = SVC_HANDLE_IS_SCM;
184 } else {
185 int i;
187 info->type = SVC_HANDLE_IS_SERVICE;
189 if ( !(info->name = SMB_STRDUP( service )) ) {
190 free_service_handle_info( info );
191 WERR_NOMEM;
194 /* lookup the SERVICE_CONTROL_OPS */
196 for ( i=0; svcctl_ops[i].name; i++ ) {
197 if ( strequal( svcctl_ops[i].name, service ) )
198 info->ops = svcctl_ops[i].ops;
202 info->access_granted = access_granted;
204 /* store the SERVICE_INFO and create an open handle */
206 if ( !create_policy_hnd( p, handle, free_service_handle_info, info ) ) {
207 free_service_handle_info( info );
208 return WERR_ACCESS_DENIED;
211 return WERR_OK;
214 /********************************************************************
215 ********************************************************************/
217 WERROR _svcctl_open_scmanager(pipes_struct *p, SVCCTL_Q_OPEN_SCMANAGER *q_u, SVCCTL_R_OPEN_SCMANAGER *r_u)
219 SEC_DESC *sec_desc;
220 uint32 access_granted = 0;
221 NTSTATUS status;
223 /* perform access checks */
225 if ( !(sec_desc = construct_scm_sd( p->mem_ctx )) )
226 return WERR_NOMEM;
228 status = svcctl_access_check( sec_desc, p->pipe_user.nt_user_token, q_u->access, &access_granted );
229 if ( !NT_STATUS_IS_OK(status) )
230 return ntstatus_to_werror( status );
232 return create_open_service_handle( p, &r_u->handle, NULL, access_granted );
235 /********************************************************************
236 ********************************************************************/
238 WERROR _svcctl_open_service(pipes_struct *p, SVCCTL_Q_OPEN_SERVICE *q_u, SVCCTL_R_OPEN_SERVICE *r_u)
240 SEC_DESC *sec_desc;
241 uint32 access_granted = 0;
242 NTSTATUS status;
243 pstring service;
244 SERVICE_INFO *scm_info;
246 rpcstr_pull(service, q_u->servicename.buffer, sizeof(service), q_u->servicename.uni_str_len*2, 0);
248 DEBUG(5, ("_svcctl_open_service: Attempting to open Service [%s], \n", service));
251 /* based on my tests you can open a service if you have a valid scm handle */
253 if ( !(scm_info = find_service_info_by_hnd( p, &q_u->handle )) )
254 return WERR_BADFID;
256 /* perform access checks */
258 if ( !(sec_desc = construct_service_sd( p->mem_ctx )) )
259 return WERR_NOMEM;
261 status = svcctl_access_check( sec_desc, p->pipe_user.nt_user_token, q_u->access, &access_granted );
262 if ( !NT_STATUS_IS_OK(status) )
263 return ntstatus_to_werror( status );
265 #if 0 /* FIXME!!! */
266 if ( ! get_service_info(service_tdb, service, info) ) {
267 return WERR_NO_SUCH_SERVICE;
268 #endif
270 return create_open_service_handle( p, &r_u->handle, service, access_granted );
273 /********************************************************************
274 ********************************************************************/
276 WERROR _svcctl_close_service(pipes_struct *p, SVCCTL_Q_CLOSE_SERVICE *q_u, SVCCTL_R_CLOSE_SERVICE *r_u)
278 return close_policy_hnd( p, &q_u->handle ) ? WERR_OK : WERR_BADFID;
281 /********************************************************************
282 ********************************************************************/
284 WERROR _svcctl_get_display_name(pipes_struct *p, SVCCTL_Q_GET_DISPLAY_NAME *q_u, SVCCTL_R_GET_DISPLAY_NAME *r_u)
286 fstring service;
287 fstring displayname;
288 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
290 /* can only use an SCM handle here */
292 if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
293 return WERR_BADFID;
295 rpcstr_pull(service, q_u->servicename.buffer, sizeof(service), q_u->servicename.uni_str_len*2, 0);
297 /* need a tdb lookup here or something */
299 fstrcpy( displayname, "FIX ME!" );
301 init_svcctl_r_get_display_name( r_u, displayname );
303 return WERR_OK;
306 /********************************************************************
307 ********************************************************************/
309 WERROR _svcctl_query_status(pipes_struct *p, SVCCTL_Q_QUERY_STATUS *q_u, SVCCTL_R_QUERY_STATUS *r_u)
311 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
313 /* perform access checks */
315 if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
316 return WERR_BADFID;
318 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
319 return WERR_ACCESS_DENIED;
321 r_u->svc_status.type = 0x0020;
322 r_u->svc_status.state = 0x0004;
323 r_u->svc_status.controls_accepted = 0x0005;
325 return WERR_OK;
329 /*********************************************************************
330 TODO - for internal services, do similar to external services, except
331 we have to call the right status routine...
332 **********************************************************************/
334 static WERROR enum_internal_services(TALLOC_CTX *tcx,ENUM_SERVICES_STATUS **svc_ptr, int existing_services, int *added)
336 int num_services = 2;
337 int i = 0;
338 ENUM_SERVICES_STATUS *services=NULL;
340 if (!svc_ptr || !(*svc_ptr))
341 return WERR_NOMEM;
343 services = *svc_ptr;
345 #if 0
346 /* *svc_ptr has the pointer to the array if there is one already. NULL if not. */
347 if ((existing_services>0) && svc_ptr && *svc_ptr) { /* reallocate vs. allocate */
348 DEBUG(8,("enum_internal_services: REALLOCing %d services\n", num_services));
349 services = TALLOC_REALLOC_ARRAY(tcx,*svc_ptr,ENUM_SERVICES_STATUS,existing_services+num_services);
350 if (!rsvcs)
351 return WERR_NOMEM;
352 *svc_ptr = services;
353 } else {
354 if ( !(services = TALLOC_ARRAY( tcx, ENUM_SERVICES_STATUS, num_services )) )
355 return WERR_NOMEM;
357 #endif
359 if (existing_services > 0) {
360 i += existing_services;
362 DEBUG(8,("enum_internal_services: Creating %d services, starting index %d\n", num_services,existing_services));
364 init_unistr( &services[i].servicename, "Spooler" );
365 init_unistr( &services[i].displayname, "Print Spooler" );
367 services[i].status.type = 0x110;
368 services[i].status.controls_accepted = 0x0;
369 services[i].status.win32_exit_code = 0x0;
370 services[i].status.service_exit_code = 0x0;
371 services[i].status.check_point = 0x0;
372 services[i].status.wait_hint = 0x0;
373 if ( !lp_disable_spoolss() )
374 services[i].status.state = SVCCTL_RUNNING;
375 else
376 services[i].status.state = SVCCTL_STOPPED;
378 i++;
380 init_unistr( &services[i].servicename, "NETLOGON" );
381 init_unistr( &services[i].displayname, "Net Logon" );
383 services[i].status.type = 0x20;
384 services[i].status.controls_accepted = 0x0;
385 services[i].status.win32_exit_code = 0x0;
386 services[i].status.service_exit_code = 0x0;
387 services[i].status.check_point = 0x0;
388 services[i].status.wait_hint = 0x0;
389 if ( lp_servicenumber("NETLOGON") != -1 )
390 services[i].status.state = SVCCTL_RUNNING;
391 else
392 services[i].status.state = SVCCTL_STOPPED;
394 *added = num_services;
396 return WERR_OK;
399 /********************************************************************
400 ********************************************************************/
402 WERROR _svcctl_enum_services_status(pipes_struct *p, SVCCTL_Q_ENUM_SERVICES_STATUS *q_u, SVCCTL_R_ENUM_SERVICES_STATUS *r_u)
404 ENUM_SERVICES_STATUS *services = NULL;
405 uint32 num_int_services = 0;
406 uint32 num_ext_services = 0;
407 int i = 0;
408 size_t buffer_size;
409 WERROR result = WERR_OK;
410 WERROR ext_result = WERR_OK;
411 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
413 /* perform access checks */
415 if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
416 return WERR_BADFID;
418 if ( !(info->access_granted & SC_RIGHT_MGR_ENUMERATE_SERVICE) )
419 return WERR_ACCESS_DENIED;
421 /* num_services = str_list_count( lp_enable_svcctl() ); */
423 /* here's where we'll read the db of external services */
424 /* _svcctl_read_LSB_data(NULL,NULL); */
425 /* init_svcctl_db(); */
427 num_int_services = 0;
429 num_int_services = num_internal_services();
431 num_ext_services = num_external_services();
433 if ( !(services = TALLOC_ARRAY(p->mem_ctx, ENUM_SERVICES_STATUS, num_int_services+num_ext_services )) )
434 return WERR_NOMEM;
436 result = enum_internal_services(p->mem_ctx, &services, 0, &num_int_services);
438 if (W_ERROR_IS_OK(result)) {
439 DEBUG(8,("_svcctl_enum_services_status: Got %d internal services\n", num_int_services));
442 ext_result=enum_external_services(p->mem_ctx, &services, num_int_services, &num_ext_services);
444 if (W_ERROR_IS_OK(ext_result)) {
445 DEBUG(8,("_svcctl_enum_services_status: Got %d external services\n", num_ext_services));
448 DEBUG(8,("_svcctl_enum_services_status: total of %d services\n", num_int_services+num_ext_services));
450 buffer_size = 0;
451 for (i=0;i<num_int_services+num_ext_services;i++) {
452 buffer_size += svcctl_sizeof_enum_services_status(&services[i]);
455 /* */
456 buffer_size += buffer_size % 4;
457 DEBUG(8,("_svcctl_enum_services_status: buffer size passed %d, we need %d\n",
458 q_u->buffer_size, buffer_size));
460 if (buffer_size > q_u->buffer_size ) {
461 num_int_services = 0;
462 num_ext_services = 0;
463 result = WERR_MORE_DATA;
466 rpcbuf_init(&r_u->buffer, q_u->buffer_size, p->mem_ctx);
468 if ( W_ERROR_IS_OK(result) ) {
469 for ( i=0; i<num_int_services+num_ext_services; i++ )
470 svcctl_io_enum_services_status( "", &services[i], &r_u->buffer, 0 );
473 r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
474 r_u->returned = num_int_services+num_ext_services;
476 if ( !(r_u->resume = TALLOC_P( p->mem_ctx, uint32 )) )
477 return WERR_NOMEM;
479 *r_u->resume = 0x0;
481 return result;
484 /********************************************************************
485 ********************************************************************/
487 WERROR _svcctl_start_service(pipes_struct *p, SVCCTL_Q_START_SERVICE *q_u, SVCCTL_R_START_SERVICE *r_u)
489 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
491 /* perform access checks */
493 if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
494 return WERR_BADFID;
496 if ( !(info->access_granted & SC_RIGHT_SVC_START) )
497 return WERR_ACCESS_DENIED;
499 return info->ops->start_service();
501 return WERR_OK;
504 /********************************************************************
505 ********************************************************************/
507 WERROR _svcctl_control_service(pipes_struct *p, SVCCTL_Q_CONTROL_SERVICE *q_u, SVCCTL_R_CONTROL_SERVICE *r_u)
509 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
511 /* perform access checks */
512 /* we only support stop so don't get complicated */
514 if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
515 return WERR_BADFID;
517 if ( q_u->control != SVCCTL_CONTROL_STOP )
518 return WERR_ACCESS_DENIED;
520 if ( !(info->access_granted & SC_RIGHT_SVC_STOP) )
521 return WERR_ACCESS_DENIED;
523 return info->ops->stop_service( &r_u->svc_status );
525 #if 0
526 SERVICE_INFO *service_info;
527 POLICY_HND *handle;
528 pstring command;
529 SERVICE_STATUS *service_status;
530 int ret,fd;
532 /* need to find the service name by the handle that is open */
533 handle = &(q_u->handle);
535 service_info = find_service_info_by_hnd(p, handle);
537 if (!service_info) {
538 DEBUG(10, ("_svcctl_control_service : Can't find the service for the handle\n"));
539 return WERR_BADFID;
542 /* we return a SERVICE_STATUS structure if there's an error. */
543 if ( !(service_status = TALLOC_ARRAY(p->mem_ctx, SERVICE_STATUS, 1 )) )
544 return WERR_NOMEM;
546 DEBUG(10, ("_svcctl_control_service: Found service [%s], [%s]\n",
547 service_info->servicename, service_info->filename));
549 /* TODO - call the service config function here... */
550 memset(command, 0, sizeof(command));
551 if (q_u->control == SVCCTL_CONTROL_STOP) {
552 slprintf(command, sizeof(command)-1, "%s%s%s %s", dyn_LIBDIR, SVCCTL_SCRIPT_DIR,
553 service_info->filename, "stop");
556 if (q_u->control == SVCCTL_CONTROL_PAUSE) {
557 slprintf(command, sizeof(command)-1, "%s%s%s %s", dyn_LIBDIR, SVCCTL_SCRIPT_DIR,
558 service_info->filename, "stop");
561 if (q_u->control == SVCCTL_CONTROL_CONTINUE) {
562 slprintf(command, sizeof(command)-1, "%s%s%s %s", dyn_LIBDIR, SVCCTL_SCRIPT_DIR,
563 service_info->filename, "restart");
566 DEBUG(10, ("_svcctl_control_service: status command is [%s]\n", command));
568 /* TODO - wrap in privilege check */
570 ret = smbrun(command, &fd);
571 DEBUGADD(10, ("returned [%d]\n", ret));
572 close(fd);
574 if(ret != 0)
575 DEBUG(10, ("enum_external_services: Command returned [%d]\n", ret));
577 /* SET all service_stats bits here...*/
578 if (ret == 0) {
579 service_status->state = SVCCTL_RUNNING;
580 service_status->controls_accepted = SVCCTL_CONTROL_SHUTDOWN | SVCCTL_CONTROL_STOP;
581 } else {
582 service_status->state = SVCCTL_STOPPED;
583 service_status->controls_accepted = 0;
586 DEBUG(10, ("_svcctl_query_service_config: Should call the commFound service [%s], [%s]\n",service_info->servicename,service_info->filename));
588 #endif
591 /********************************************************************
592 ********************************************************************/
594 WERROR _svcctl_enum_dependent_services( pipes_struct *p, SVCCTL_Q_ENUM_DEPENDENT_SERVICES *q_u, SVCCTL_R_ENUM_DEPENDENT_SERVICES *r_u )
596 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
598 /* perform access checks */
600 if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
601 return WERR_BADFID;
603 if ( !(info->access_granted & SC_RIGHT_SVC_ENUMERATE_DEPENDENTS) )
604 return WERR_ACCESS_DENIED;
606 /* we have to set the outgoing buffer size to the same as the
607 incoming buffer size (even in the case of failure */
609 rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
611 r_u->needed = q_u->buffer_size;
613 /* no dependent services...basically a stub function */
614 r_u->returned = 0;
616 return WERR_OK;
619 /********************************************************************
620 ********************************************************************/
622 WERROR _svcctl_query_service_status_ex( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_STATUSEX *q_u, SVCCTL_R_QUERY_SERVICE_STATUSEX *r_u )
624 SERVICE_STATUS_PROCESS ssp;
625 POLICY_HND *handle;
626 SERVICE_INFO *service_info;
627 pstring command;
628 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
630 /* perform access checks */
632 if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
633 return WERR_BADFID;
635 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
636 return WERR_ACCESS_DENIED;
638 /* we have to set the outgoing buffer size to the same as the
639 incoming buffer size (even in the case of failure */
641 r_u->needed = q_u->buffer_size;
643 /* need to find the service name by the handle that is open */
644 handle = &(q_u->handle);
647 /* get rid of the easy errors */
649 if (q_u->info_level != SVC_STATUS_PROCESS_INFO) {
650 DEBUG(10, ("_svcctl_query_service_status_ex : Invalid information level specified\n"));
651 return WERR_UNKNOWN_LEVEL;
654 service_info = find_service_info_by_hnd(p, handle);
656 if (!service_info) {
657 DEBUG(10, ("_svcctl_query_service_status_ex : Can't find the service for the handle\n"));
658 return WERR_BADFID;
661 if (r_u->needed < (sizeof(SERVICE_STATUS_PROCESS)+sizeof(uint32)+sizeof(uint32))) {
662 DEBUG(10, ("_svcctl_query_service_status_ex : buffer size of [%d] is too small.\n",r_u->needed));
663 return WERR_INSUFFICIENT_BUFFER;
666 ZERO_STRUCT(ssp);
668 #if 0
669 if (!strwicmp(service_info->servicetype,"EXTERNAL"))
670 ssp.type = SVCCTL_WIN32_OWN_PROC;
671 else
672 ssp.type = SVCCTL_WIN32_SHARED_PROC;
673 #endif
675 /* Get the status of the service.. */
677 memset(command, 0, sizeof(command));
679 #if 0
680 slprintf(command, sizeof(command)-1, "%s%s%s %s", dyn_LIBDIR, SVCCTL_SCRIPT_DIR, service_info->filename, "status");
682 DEBUG(10, ("_svcctl_query_service_status_ex: status command is [%s]\n", command));
684 /* TODO - wrap in privilege check */
686 ret = smbrun(command, &fd);
687 DEBUGADD(10, ("returned [%d]\n", ret));
688 close(fd);
689 if(ret != 0)
690 DEBUG(10, ("_svcctl_query_service_status_ex: Command returned [%d]\n", ret));
692 /* SET all service_stats bits here... */
693 if (ret == 0) {
694 ssp.state = SVCCTL_RUNNING;
695 ssp.controls_accepted = SVCCTL_CONTROL_SHUTDOWN | SVCCTL_CONTROL_STOP;
696 } else {
697 ssp.state = SVCCTL_STOPPED;
698 ssp.controls_accepted = 0;
700 #endif
702 return WERR_OK;
705 /********************************************************************
706 ********************************************************************/
708 WERROR _svcctl_query_service_config( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CONFIG *q_u, SVCCTL_R_QUERY_SERVICE_CONFIG *r_u )
710 POLICY_HND *handle;
711 SERVICE_INFO *service_info;
712 uint32 needed_size;
713 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
715 /* perform access checks */
717 if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
718 return WERR_BADFID;
720 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
721 return WERR_ACCESS_DENIED;
723 /* we have to set the outgoing buffer size to the same as the
724 incoming buffer size (even in the case of failure */
726 r_u->needed = q_u->buffer_size;
728 /* need to find the service name by the handle that is open */
729 handle = &(q_u->handle);
731 service_info = find_service_info_by_hnd(p, handle);
733 #if 0
734 if (q_u->buffer_size < sizeof(Service_info)) {
735 /* have to report need more... */
736 /* TODO worst case -- should actualy calc what we need here. */
737 r_u->needed = sizeof(Service_info)+sizeof(pstring)*5;
738 DEBUG(10, ("_svcctl_query_service_config: NOT ENOUGH BUFFER ALLOCATED FOR RETURN DATA -- provided %d wanted %d\n",
739 q_u->buffer_size,r_u->needed));
741 return WERR_INSUFFICIENT_BUFFER;
743 #endif
744 if (!service_info) {
745 DEBUG(10, ("_svcctl_query_service_config : Can't find the service for the handle\n"));
746 return WERR_BADFID;
749 #if 0
750 if ( !(service_config = (SERVICE_CONFIG *)TALLOC_ZERO_P(p->mem_ctx, SERVICE_CONFIG)) )
751 return WERR_NOMEM;
752 #endif
754 r_u->config.service_type = SVCCTL_WIN32_OWN_PROC;
755 r_u->config.start_type = SVCCTL_DEMAND_START;
756 r_u->config.error_control = SVCCTL_SVC_ERROR_IGNORE;
757 r_u->config.tag_id = 0x00000000;
759 /* Init the strings */
761 r_u->config.executablepath = TALLOC_ZERO_P(p->mem_ctx, UNISTR2);
762 r_u->config.loadordergroup = TALLOC_ZERO_P(p->mem_ctx, UNISTR2);
763 r_u->config.dependencies = TALLOC_ZERO_P(p->mem_ctx, UNISTR2);
764 r_u->config.startname = TALLOC_ZERO_P(p->mem_ctx, UNISTR2);
765 r_u->config.displayname = TALLOC_ZERO_P(p->mem_ctx, UNISTR2);
767 #if 0
768 pstrcpy(fullpathinfo,dyn_LIBDIR);
769 pstrcat(fullpathinfo,SVCCTL_SCRIPT_DIR);
770 pstrcat(fullpathinfo,service_info->filename);
771 /* Get and calculate the size of the fields. Note that we're still building the fields in the "too-small buffer case"
772 even though we throw it away. */
774 DEBUG(10, ("_svcctl_query_service_config: fullpath info [%s]\n",fullpathinfo));
775 init_unistr2(r_u->config.executablepath,fullpathinfo,UNI_STR_TERMINATE);
776 init_unistr2(r_u->config.loadordergroup,"",UNI_STR_TERMINATE);
777 init_unistr2(r_u->config.dependencies,service_info->dependencies,UNI_STR_TERMINATE);
779 /* TODO - if someone really cares, perhaps "LocalSystem" should be changed to something else here... */
781 init_unistr2(r_u->config.startname,"LocalSystem",UNI_STR_TERMINATE);
782 init_unistr2(r_u->config.displayname,service_info->servicename,UNI_STR_TERMINATE);
783 #endif
785 needed_size = 0x04 + sizeof(SERVICE_CONFIG)+ 2*(
786 r_u->config.executablepath->uni_str_len +
787 r_u->config.loadordergroup->uni_str_len +
788 r_u->config.dependencies->uni_str_len +
789 r_u->config.startname->uni_str_len +
790 r_u->config.displayname->uni_str_len);
792 DEBUG(10, ("_svcctl_query_service_config: ****** need to have a buffer of [%d], [%d] for struct \n",needed_size,
793 sizeof(SERVICE_CONFIG)));
794 DEBUG(10, ("\tsize of executable path : %d\n",r_u->config.executablepath->uni_str_len));
795 DEBUG(10, ("\tsize of loadordergroup : %d\n", r_u->config.loadordergroup->uni_str_len));
796 DEBUG(10, ("\tsize of dependencies : %d\n", r_u->config.dependencies->uni_str_len));
797 DEBUG(10, ("\tsize of startname : %d\n", r_u->config.startname->uni_str_len));
798 DEBUG(10, ("\tsize of displayname : %d\n", r_u->config.displayname->uni_str_len));
800 if (q_u->buffer_size < needed_size) {
801 /* have to report need more...*/
802 r_u->needed = needed_size;
803 DEBUG(10, ("_svcctl_query_service_config: ****** zeroing strings for return\n"));
804 memset(&r_u->config,0,sizeof(SERVICE_CONFIG));
805 DEBUG(10, ("_svcctl_query_service_config: Not enouh buffer provided for return -- provided %d wanted %d\n",
806 q_u->buffer_size,needed_size));
807 return WERR_INSUFFICIENT_BUFFER;
810 return WERR_OK;
813 /********************************************************************
814 ********************************************************************/
816 WERROR _svcctl_query_service_config2( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CONFIG2 *q_u, SVCCTL_R_QUERY_SERVICE_CONFIG2 *r_u )
818 POLICY_HND *handle;
819 SERVICE_INFO *service_info;
820 uint32 level;
821 SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
823 /* perform access checks */
825 if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
826 return WERR_BADFID;
828 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
829 return WERR_ACCESS_DENIED;
831 /* we have to set the outgoing buffer size to the same as the
832 incoming buffer size (even in the case of failure */
834 r_u->needed = q_u->buffer_size;
835 r_u->description = NULL;
836 r_u->returned = q_u->buffer_size;
837 r_u->offset = 4;
839 handle = &(q_u->handle);
841 service_info = find_service_info_by_hnd(p, handle);
843 if (!service_info) {
844 DEBUG(10, ("_svcctl_query_service_config2 : Can't find the service for the handle\n"));
845 return WERR_BADFID;
849 TODO - perhaps move the RPC_DATA_BLOB into the R_QUERY_SERVICE_CONFIG structure, and to the processing in here, vs
850 in the *r_query_config2 marshalling routine...
853 level = q_u->info_level;
855 #if 0
856 if (SERVICE_CONFIG_DESCRIPTION == level) {
857 if (service_info && service_info->shortdescription) {
858 /* length of the string, plus the terminator... */
859 string_buffer_size = strlen(service_info->shortdescription)+1;
860 DEBUG(10, ("_svcctl_query_service_config: copying the description [%s] length [%d]\n",
861 service_info->shortdescription,string_buffer_size));
863 if (q_u->buffer_size >= ((string_buffer_size)*2+4)) {
864 r_u->description = TALLOC_ZERO_P(p->mem_ctx, UNISTR2);
865 if (!r_u->description) return WERR_NOMEM;
866 init_unistr2(r_u->description,service_info->shortdescription,UNI_STR_TERMINATE);
869 else {
870 string_buffer_size = 0;
872 DEBUG(10, ("_svcctl_query_service_config2: buffer needed is [%x], return buffer size is [%x]\n",
873 string_buffer_size,q_u->buffer_size));
874 if (((string_buffer_size)*2+4) > q_u->buffer_size) {
875 r_u->needed = (string_buffer_size+1)*2+4;
876 DEBUG(10, ("_svcctl_query_service_config2: INSUFFICIENT BUFFER\n"));
877 return WERR_INSUFFICIENT_BUFFER;
879 DEBUG(10, ("_svcctl_query_service_config2: returning ok, needed is [%x], buffer size is [%x]\n",
880 r_u->needed,q_u->buffer_size));
882 return WERR_OK;
884 #endif
886 return WERR_ACCESS_DENIED;