s3-svcctl: remove some more redundant headers.
[metze/samba/wip.git] / source3 / rpc_server / srv_svcctl_nt.c
index 73d09b1bbb37a9b3875c3a91ba17f123e336ec01..1b9c9b84e4cae25bdd94e823d67dce4f1c0d60db 100644 (file)
@@ -6,6 +6,7 @@
  *
  *  Largely Rewritten (Again) by:
  *  Copyright (C) Gerald (Jerry) Carter             2005.
+ *  Copyright (C) Guenther Deschner                 2008.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -61,7 +62,7 @@ static const struct generic_mapping svc_generic_map =
 bool init_service_op_table( void )
 {
        const char **service_list = lp_svcctl_list();
-       int num_services = SVCCTL_NUM_INTERNAL_SERVICES + str_list_count( service_list );
+       int num_services = SVCCTL_NUM_INTERNAL_SERVICES + str_list_length( service_list );
        int i;
 
        if ( !(svcctl_ops = TALLOC_ARRAY( NULL, struct service_control_op, num_services+1)) ) {
@@ -122,16 +123,12 @@ static struct service_control_op* find_service_by_name( const char *name )
 static NTSTATUS svcctl_access_check( SEC_DESC *sec_desc, NT_USER_TOKEN *token,
                                      uint32 access_desired, uint32 *access_granted )
 {
-       NTSTATUS result;
-
        if ( geteuid() == sec_initial_uid() ) {
                DEBUG(5,("svcctl_access_check: using root's token\n"));
                token = get_root_nt_token();
        }
 
-       se_access_check( sec_desc, token, access_desired, access_granted, &result );
-
-       return result;
+       return se_access_check( sec_desc, token, access_desired, access_granted);
 }
 
 /********************************************************************
@@ -140,7 +137,6 @@ static NTSTATUS svcctl_access_check( SEC_DESC *sec_desc, NT_USER_TOKEN *token,
 static SEC_DESC* construct_scm_sd( TALLOC_CTX *ctx )
 {
        SEC_ACE ace[2];
-       SEC_ACCESS mask;
        size_t i = 0;
        SEC_DESC *sd;
        SEC_ACL *acl;
@@ -148,13 +144,13 @@ static SEC_DESC* construct_scm_sd( TALLOC_CTX *ctx )
 
        /* basic access for Everyone */
 
-       init_sec_access(&mask, SC_MANAGER_READ_ACCESS );
-       init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
+       init_sec_ace(&ace[i++], &global_sid_World,
+               SEC_ACE_TYPE_ACCESS_ALLOWED, SC_MANAGER_READ_ACCESS, 0);
 
        /* Full Access 'BUILTIN\Administrators' */
 
-       init_sec_access(&mask,SC_MANAGER_ALL_ACCESS );
-       init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
+       init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
+               SEC_ACE_TYPE_ACCESS_ALLOWED, SC_MANAGER_ALL_ACCESS, 0);
 
 
        /* create the security descriptor */
@@ -188,7 +184,7 @@ static SERVICE_INFO *find_service_info_by_hnd(pipes_struct *p, POLICY_HND *hnd)
        SERVICE_INFO *service_info = NULL;
 
        if( !find_policy_by_hnd( p, hnd, (void **)(void *)&service_info) ) {
-               DEBUG(2,("find_service_info_by_hnd: handle not found"));
+               DEBUG(2,("find_service_info_by_hnd: handle not found\n"));
                return NULL;
        }
 
@@ -276,7 +272,8 @@ WERROR _svcctl_OpenSCManagerW(pipes_struct *p,
                return WERR_NOMEM;
 
        se_map_generic( &r->in.access_mask, &scm_generic_map );
-       status = svcctl_access_check( sec_desc, p->pipe_user.nt_user_token, r->in.access_mask, &access_granted );
+       status = svcctl_access_check( sec_desc, p->server_info->ptok,
+                                     r->in.access_mask, &access_granted );
        if ( !NT_STATUS_IS_OK(status) )
                return ntstatus_to_werror( status );
 
@@ -313,7 +310,8 @@ WERROR _svcctl_OpenServiceW(pipes_struct *p,
                return WERR_NOMEM;
 
        se_map_generic( &r->in.access_mask, &svc_generic_map );
-       status = svcctl_access_check( sec_desc, p->pipe_user.nt_user_token, r->in.access_mask, &access_granted );
+       status = svcctl_access_check( sec_desc, p->server_info->ptok,
+                                     r->in.access_mask, &access_granted );
        if ( !NT_STATUS_IS_OK(status) )
                return ntstatus_to_werror( status );
 
@@ -328,6 +326,8 @@ WERROR _svcctl_CloseServiceHandle(pipes_struct *p, struct svcctl_CloseServiceHan
        if ( !close_policy_hnd( p, r->in.handle ) )
                return  WERR_BADFID;
 
+       ZERO_STRUCTP(r->out.handle);
+
        return WERR_OK;
 }
 
@@ -349,7 +349,8 @@ WERROR _svcctl_GetServiceDisplayNameW(pipes_struct *p,
 
        service = r->in.service_name;
 
-       display_name = svcctl_lookup_dispname(p->mem_ctx, service, p->pipe_user.nt_user_token );
+       display_name = svcctl_lookup_dispname(p->mem_ctx, service,
+                                             p->server_info->ptok);
        if (!display_name) {
                display_name = "";
        }
@@ -426,7 +427,7 @@ WERROR _svcctl_enum_services_status(pipes_struct *p, SVCCTL_Q_ENUM_SERVICES_STAT
        size_t buffer_size = 0;
        WERROR result = WERR_OK;
        SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
-       NT_USER_TOKEN *token = p->pipe_user.nt_user_token;
+       NT_USER_TOKEN *token = p->server_info->ptok;
 
        /* perform access checks */
 
@@ -519,19 +520,19 @@ WERROR _svcctl_ControlService(pipes_struct *p,
 
                return info->ops->service_status( info->name,
                                                  r->out.service_status );
+       default:
+               return WERR_ACCESS_DENIED;
        }
-
-       /* default control action */
-
-       return WERR_ACCESS_DENIED;
 }
 
 /********************************************************************
+ _svcctl_EnumDependentServicesW
 ********************************************************************/
 
-WERROR _svcctl_enum_dependent_services( pipes_struct *p, SVCCTL_Q_ENUM_DEPENDENT_SERVICES *q_u, SVCCTL_R_ENUM_DEPENDENT_SERVICES *r_u )
+WERROR _svcctl_EnumDependentServicesW(pipes_struct *p,
+                                     struct svcctl_EnumDependentServicesW *r)
 {
-       SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
+       SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.service );
 
        /* perform access checks */
 
@@ -543,23 +544,24 @@ WERROR _svcctl_enum_dependent_services( pipes_struct *p, SVCCTL_Q_ENUM_DEPENDENT
 
        /* we have to set the outgoing buffer size to the same as the
           incoming buffer size (even in the case of failure */
+       /* this is done in the autogenerated server already - gd */
 
-       rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
-
-       r_u->needed      = q_u->buffer_size;
+       *r->out.bytes_needed = r->in.buf_size;
 
        /* no dependent services...basically a stub function */
-       r_u->returned    = 0;
+       *r->out.services_returned = 0;
 
        return WERR_OK;
 }
 
 /********************************************************************
+ _svcctl_QueryServiceStatusEx
 ********************************************************************/
 
-WERROR _svcctl_query_service_status_ex( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_STATUSEX *q_u, SVCCTL_R_QUERY_SERVICE_STATUSEX *r_u )
+WERROR _svcctl_QueryServiceStatusEx(pipes_struct *p,
+                                   struct svcctl_QueryServiceStatusEx *r)
 {
-       SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
+       SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
        uint32 buffer_size;
 
        /* perform access checks */
@@ -573,21 +575,29 @@ WERROR _svcctl_query_service_status_ex( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_
        /* we have to set the outgoing buffer size to the same as the
           incoming buffer size (even in the case of failure) */
 
-       rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
-       r_u->needed = q_u->buffer_size;
+       *r->out.bytes_needed = r->in.buf_size;
 
-       switch ( q_u->level ) {
+       switch ( r->in.info_level ) {
                case SVC_STATUS_PROCESS_INFO:
                {
-                       SERVICE_STATUS_PROCESS svc_stat_proc;
+                       struct SERVICE_STATUS_PROCESS svc_stat_proc;
+                       enum ndr_err_code ndr_err;
+                       DATA_BLOB blob;
 
                        /* Get the status of the service.. */
                        info->ops->service_status( info->name, &svc_stat_proc.status );
                        svc_stat_proc.process_id     = sys_getpid();
                        svc_stat_proc.service_flags  = 0x0;
 
-                       svcctl_io_service_status_process( "", &svc_stat_proc, &r_u->buffer, 0 );
-                       buffer_size = sizeof(SERVICE_STATUS_PROCESS);
+                       ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
+                                                      &svc_stat_proc,
+                                                      (ndr_push_flags_fn_t)ndr_push_SERVICE_STATUS_PROCESS);
+                       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                               return WERR_INVALID_PARAM;
+                       }
+
+                       r->out.buffer = blob.data;
+                       buffer_size = sizeof(struct SERVICE_STATUS_PROCESS);
                        break;
                }
 
@@ -597,10 +607,11 @@ WERROR _svcctl_query_service_status_ex( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_
 
 
         buffer_size += buffer_size % 4;
-       r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
+       *r->out.bytes_needed = (buffer_size > r->in.buf_size) ? buffer_size : r->in.buf_size;
 
-        if (buffer_size > q_u->buffer_size )
-                return WERR_MORE_DATA;
+        if (buffer_size > r->in.buf_size ) {
+                return WERR_INSUFFICIENT_BUFFER;
+       }
 
        return WERR_OK;
 }
@@ -608,7 +619,9 @@ WERROR _svcctl_query_service_status_ex( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_
 /********************************************************************
 ********************************************************************/
 
-static WERROR fill_svc_config( TALLOC_CTX *ctx, const char *name, SERVICE_CONFIG *config, NT_USER_TOKEN *token )
+static WERROR fill_svc_config( TALLOC_CTX *ctx, const char *name,
+                              struct QUERY_SERVICE_CONFIG *config,
+                              NT_USER_TOKEN *token )
 {
        REGVAL_CTR *values;
        REGISTRY_VALUE *val;
@@ -620,27 +633,24 @@ static WERROR fill_svc_config( TALLOC_CTX *ctx, const char *name, SERVICE_CONFIG
 
        /* now fill in the individual values */
 
-       config->displayname = TALLOC_ZERO_P( ctx, UNISTR2 );
        if ( (val = regval_ctr_getvalue( values, "DisplayName" )) != NULL )
-               init_unistr2( config->displayname, regval_sz( val ), UNI_STR_TERMINATE );
+               config->displayname = regval_sz(val);
        else
-               init_unistr2( config->displayname, name, UNI_STR_TERMINATE );
+               config->displayname = name;
 
        if ( (val = regval_ctr_getvalue( values, "ObjectName" )) != NULL ) {
-               config->startname = TALLOC_ZERO_P( ctx, UNISTR2 );
-               init_unistr2( config->startname, regval_sz( val ), UNI_STR_TERMINATE );
+               config->startname = regval_sz(val);
        }
 
        if ( (val = regval_ctr_getvalue( values, "ImagePath" )) != NULL ) {
-               config->executablepath = TALLOC_ZERO_P( ctx, UNISTR2 );
-               init_unistr2( config->executablepath, regval_sz( val ), UNI_STR_TERMINATE );
+               config->executablepath = regval_sz(val);
        }
 
        /* a few hard coded values */
        /* loadordergroup and dependencies are empty */
 
        config->tag_id           = 0x00000000;                  /* unassigned loadorder group */
-       config->service_type     = SVCCTL_WIN32_OWN_PROC;
+       config->service_type     = SERVICE_TYPE_WIN32_OWN_PROCESS;
        config->error_control    = SVCCTL_SVC_ERROR_NORMAL;
 
        /* set the start type.  NetLogon and WINS are disabled to prevent
@@ -661,11 +671,13 @@ static WERROR fill_svc_config( TALLOC_CTX *ctx, const char *name, SERVICE_CONFIG
 }
 
 /********************************************************************
+ _svcctl_QueryServiceConfigW
 ********************************************************************/
 
-WERROR _svcctl_query_service_config( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CONFIG *q_u, SVCCTL_R_QUERY_SERVICE_CONFIG *r_u )
+WERROR _svcctl_QueryServiceConfigW(pipes_struct *p,
+                                  struct svcctl_QueryServiceConfigW *r)
 {
-       SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
+       SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
        uint32 buffer_size;
        WERROR wresult;
 
@@ -680,17 +692,18 @@ WERROR _svcctl_query_service_config( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CON
        /* we have to set the outgoing buffer size to the same as the
           incoming buffer size (even in the case of failure */
 
-       r_u->needed      = q_u->buffer_size;
+       *r->out.bytes_needed = r->in.buf_size;
 
-       wresult = fill_svc_config( p->mem_ctx, info->name, &r_u->config, p->pipe_user.nt_user_token );
+       wresult = fill_svc_config( p->mem_ctx, info->name, r->out.query,
+                                  p->server_info->ptok);
        if ( !W_ERROR_IS_OK(wresult) )
                return wresult;
 
-       buffer_size = svcctl_sizeof_service_config( &r_u->config );
-       r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
+       buffer_size = ndr_size_QUERY_SERVICE_CONFIG(r->out.query, NULL, 0);
+       *r->out.bytes_needed = (buffer_size > r->in.buf_size) ? buffer_size : r->in.buf_size;
 
-        if (buffer_size > q_u->buffer_size ) {
-               ZERO_STRUCTP( &r_u->config );
+        if (buffer_size > r->in.buf_size ) {
+               ZERO_STRUCTP(r->out.query);
                 return WERR_INSUFFICIENT_BUFFER;
        }
 
@@ -698,11 +711,13 @@ WERROR _svcctl_query_service_config( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CON
 }
 
 /********************************************************************
+ _svcctl_QueryServiceConfig2W
 ********************************************************************/
 
-WERROR _svcctl_query_service_config2( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CONFIG2 *q_u, SVCCTL_R_QUERY_SERVICE_CONFIG2 *r_u )
+WERROR _svcctl_QueryServiceConfig2W(pipes_struct *p,
+                                   struct svcctl_QueryServiceConfig2W *r)
 {
-       SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
+       SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
        uint32 buffer_size;
 
        /* perform access checks */
@@ -716,35 +731,53 @@ WERROR _svcctl_query_service_config2( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CO
        /* we have to set the outgoing buffer size to the same as the
           incoming buffer size (even in the case of failure */
 
-       rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
-       r_u->needed = q_u->buffer_size;
+       *r->out.bytes_needed = r->in.buf_size;
 
-       switch ( q_u->level ) {
+       switch ( r->in.info_level ) {
        case SERVICE_CONFIG_DESCRIPTION:
                {
-                       SERVICE_DESCRIPTION desc_buf;
+                       struct SERVICE_DESCRIPTION desc_buf;
                        const char *description;
+                       enum ndr_err_code ndr_err;
+                       DATA_BLOB blob;
 
-                       description = svcctl_lookup_description(p->mem_ctx, info->name, p->pipe_user.nt_user_token );
+                       description = svcctl_lookup_description(
+                               p->mem_ctx, info->name, p->server_info->ptok);
 
-                       ZERO_STRUCTP( &desc_buf );
+                       desc_buf.description = description;
 
-                       init_service_description_buffer( &desc_buf, description ? description : "");
-                       svcctl_io_service_description( "", &desc_buf, &r_u->buffer, 0 );
-                       buffer_size = svcctl_sizeof_service_description( &desc_buf );
+                       ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
+                                                      &desc_buf,
+                                                      (ndr_push_flags_fn_t)ndr_push_SERVICE_DESCRIPTION);
+                       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                               return WERR_INVALID_PARAM;
+                       }
+
+                       buffer_size = ndr_size_SERVICE_DESCRIPTION(&desc_buf, NULL, 0);
+                       r->out.buffer = blob.data;
 
                        break;
                }
                break;
        case SERVICE_CONFIG_FAILURE_ACTIONS:
                {
-                       SERVICE_FAILURE_ACTIONS actions;
+                       struct SERVICE_FAILURE_ACTIONS actions;
+                       enum ndr_err_code ndr_err;
+                       DATA_BLOB blob;
 
                        /* nothing to say...just service the request */
 
-                       ZERO_STRUCTP( &actions );
-                       svcctl_io_service_fa( "", &actions, &r_u->buffer, 0 );
-                       buffer_size = svcctl_sizeof_service_fa( &actions );
+                       ZERO_STRUCT( actions );
+
+                       ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
+                                                      &actions,
+                                                      (ndr_push_flags_fn_t)ndr_push_SERVICE_FAILURE_ACTIONS);
+                       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                               return WERR_INVALID_PARAM;
+                       }
+
+                       buffer_size = ndr_size_SERVICE_FAILURE_ACTIONS(&actions, NULL, 0);
+                       r->out.buffer = blob.data;
 
                        break;
                }
@@ -755,9 +788,9 @@ WERROR _svcctl_query_service_config2( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CO
        }
 
        buffer_size += buffer_size % 4;
-       r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
+       *r->out.bytes_needed = (buffer_size > r->in.buf_size) ? buffer_size : r->in.buf_size;
 
-        if (buffer_size > q_u->buffer_size )
+        if (buffer_size > r->in.buf_size )
                 return WERR_INSUFFICIENT_BUFFER;
 
        return WERR_OK;
@@ -835,7 +868,7 @@ WERROR _svcctl_QueryServiceObjectSecurity(pipes_struct *p,
        if ( !(sec_desc = svcctl_get_secdesc( p->mem_ctx, info->name, get_root_nt_token() )) )
                 return WERR_NOMEM;
 
-       *r->out.needed = ndr_size_security_descriptor( sec_desc, 0 );
+       *r->out.needed = ndr_size_security_descriptor( sec_desc, NULL, 0 );
 
        if ( *r->out.needed > r->in.buffer_size ) {
                ZERO_STRUCTP( &r->out.buffer );
@@ -905,7 +938,8 @@ WERROR _svcctl_SetServiceObjectSecurity(pipes_struct *p,
 
        /* store the new SD */
 
-       if ( !svcctl_set_secdesc( p->mem_ctx, info->name, sec_desc, p->pipe_user.nt_user_token ) )
+       if ( !svcctl_set_secdesc( p->mem_ctx, info->name, sec_desc,
+                                 p->server_info->ptok) )
                return WERR_ACCESS_DENIED;
 
        return WERR_OK;
@@ -948,24 +982,12 @@ WERROR _svcctl_CreateServiceW(pipes_struct *p, struct svcctl_CreateServiceW *r)
        return WERR_NOT_SUPPORTED;
 }
 
-WERROR _svcctl_EnumDependentServicesW(pipes_struct *p, struct svcctl_EnumDependentServicesW *r)
-{
-       p->rng_fault_state = True;
-       return WERR_NOT_SUPPORTED;
-}
-
 WERROR _svcctl_EnumServicesStatusW(pipes_struct *p, struct svcctl_EnumServicesStatusW *r)
 {
        p->rng_fault_state = True;
        return WERR_NOT_SUPPORTED;
 }
 
-WERROR _svcctl_QueryServiceConfigW(pipes_struct *p, struct svcctl_QueryServiceConfigW *r)
-{
-       p->rng_fault_state = True;
-       return WERR_NOT_SUPPORTED;
-}
-
 WERROR _svcctl_QueryServiceLockStatusW(pipes_struct *p, struct svcctl_QueryServiceLockStatusW *r)
 {
        p->rng_fault_state = True;
@@ -1080,18 +1102,6 @@ WERROR _svcctl_QueryServiceConfig2A(pipes_struct *p, struct svcctl_QueryServiceC
        return WERR_NOT_SUPPORTED;
 }
 
-WERROR _svcctl_QueryServiceConfig2W(pipes_struct *p, struct svcctl_QueryServiceConfig2W *r)
-{
-       p->rng_fault_state = True;
-       return WERR_NOT_SUPPORTED;
-}
-
-WERROR _svcctl_QueryServiceStatusEx(pipes_struct *p, struct svcctl_QueryServiceStatusEx *r)
-{
-       p->rng_fault_state = True;
-       return WERR_NOT_SUPPORTED;
-}
-
 WERROR _EnumServicesStatusExA(pipes_struct *p, struct EnumServicesStatusExA *r)
 {
        p->rng_fault_state = True;