s3:services_db: untangle assignments from check in construct_service_sd().
[amitay/samba.git] / source3 / services / services_db.c
index e41524851fb86f37a27bd52282dc6e59a1eb7a21..f3c550a91fc31ffbd9da12734a78dc861756b8cb 100644 (file)
  */
 
 #include "includes.h"
+#include "services/services.h"
+#include "registry.h"
+#include "registry/reg_util_legacy.h"
+#include "registry/reg_dispatcher.h"
+#include "registry/reg_objects.h"
 
 struct rcinit_file_information {
        char *description;
@@ -86,12 +91,12 @@ struct service_display_info common_unix_svcs[] = {
 /********************************************************************
 ********************************************************************/
 
-static SEC_DESC* construct_service_sd( TALLOC_CTX *ctx )
+static struct security_descriptor* construct_service_sd( TALLOC_CTX *ctx )
 {
-       SEC_ACE ace[4];
+       struct security_ace ace[4];
        size_t i = 0;
-       SEC_DESC *sd = NULL;
-       SEC_ACL *acl = NULL;
+       struct security_descriptor *sd = NULL;
+       struct security_acl *theacl = NULL;
        size_t sd_size;
 
        /* basic access for Everyone */
@@ -109,13 +114,17 @@ static SEC_DESC* construct_service_sd( TALLOC_CTX *ctx )
 
        /* create the security descriptor */
 
-       if ( !(acl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) )
+       theacl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace);
+       if (theacl == NULL) {
                return NULL;
+       }
 
-       if ( !(sd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
-                                 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
-                                 acl, &sd_size)) )
+       sd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
+                          SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
+                          theacl, &sd_size);
+       if (sd == NULL) {
                return NULL;
+       }
 
        return sd;
 }
@@ -248,9 +257,9 @@ static bool read_init_file( const char *servicename, struct rcinit_file_informat
  Display name, Description, etc...
 ********************************************************************/
 
-static void fill_service_values( const char *name, REGVAL_CTR *values )
+static void fill_service_values(const char *name, struct regval_ctr *values)
 {
-       UNISTR2 data, dname, ipath, description;
+       char *dname, *ipath, *description;
        uint32 dword;
        int i;
 
@@ -258,34 +267,27 @@ static void fill_service_values( const char *name, REGVAL_CTR *values )
           I'm just storing them here for cosmetic purposes */
 
        dword = SVCCTL_AUTO_START;
-       regval_ctr_addvalue( values, "Start", REG_DWORD, (char*)&dword, sizeof(uint32));
+       regval_ctr_addvalue( values, "Start", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
 
        dword = SERVICE_TYPE_WIN32_OWN_PROCESS;
-       regval_ctr_addvalue( values, "Type", REG_DWORD, (char*)&dword, sizeof(uint32));
+       regval_ctr_addvalue( values, "Type", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
 
        dword = SVCCTL_SVC_ERROR_NORMAL;
-       regval_ctr_addvalue( values, "ErrorControl", REG_DWORD, (char*)&dword, sizeof(uint32));
+       regval_ctr_addvalue( values, "ErrorControl", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
 
        /* everything runs as LocalSystem */
 
-       init_unistr2( &data, "LocalSystem", UNI_STR_TERMINATE );
-       regval_ctr_addvalue( values, "ObjectName", REG_SZ, (char*)data.buffer, data.uni_str_len*2);
+       regval_ctr_addvalue_sz(values, "ObjectName", "LocalSystem");
 
        /* special considerations for internal services and the DisplayName value */
 
        for ( i=0; builtin_svcs[i].servicename; i++ ) {
                if ( strequal( name, builtin_svcs[i].servicename ) ) {
-                       char *pstr = NULL;
-                       if (asprintf(&pstr, "%s/%s/%s",
+                       ipath = talloc_asprintf(talloc_tos(), "%s/%s/%s",
                                        get_dyn_MODULESDIR(), SVCCTL_SCRIPT_DIR,
-                                       builtin_svcs[i].daemon) > 0) {
-                               init_unistr2( &ipath, pstr, UNI_STR_TERMINATE );
-                               SAFE_FREE(pstr);
-                       } else {
-                               init_unistr2( &ipath, "", UNI_STR_TERMINATE );
-                       }
-                       init_unistr2( &description, builtin_svcs[i].description, UNI_STR_TERMINATE );
-                       init_unistr2( &dname, builtin_svcs[i].dispname, UNI_STR_TERMINATE );
+                                       builtin_svcs[i].daemon);
+                       description = talloc_strdup(talloc_tos(), builtin_svcs[i].description);
+                       dname = talloc_strdup(talloc_tos(), builtin_svcs[i].dispname);
                        break;
                }
        }
@@ -293,38 +295,37 @@ static void fill_service_values( const char *name, REGVAL_CTR *values )
        /* default to an external service if we haven't found a match */
 
        if ( builtin_svcs[i].servicename == NULL ) {
-               char *pstr = NULL;
                char *dispname = NULL;
                struct rcinit_file_information *init_info = NULL;
 
-               if (asprintf(&pstr, "%s/%s/%s",get_dyn_MODULESDIR(),
-                                       SVCCTL_SCRIPT_DIR, name) > 0) {
-                       init_unistr2( &ipath, pstr, UNI_STR_TERMINATE );
-                       SAFE_FREE(pstr);
-               } else {
-                       init_unistr2( &ipath, "", UNI_STR_TERMINATE );
-               }
+               ipath = talloc_asprintf(talloc_tos(), "%s/%s/%s",
+                                       get_dyn_MODULESDIR(), SVCCTL_SCRIPT_DIR,
+                                       name);
 
                /* lookup common unix display names */
                dispname = get_common_service_dispname(name);
-               init_unistr2( &dname, dispname ? dispname : "", UNI_STR_TERMINATE );
+               dname = talloc_strdup(talloc_tos(), dispname ? dispname : "");
                SAFE_FREE(dispname);
 
                /* get info from init file itself */
                if ( read_init_file( name, &init_info ) ) {
-                       init_unistr2( &description, init_info->description, UNI_STR_TERMINATE );
+                       description = talloc_strdup(talloc_tos(), init_info->description);
                        TALLOC_FREE( init_info );
                }
                else {
-                       init_unistr2( &description, "External Unix Service", UNI_STR_TERMINATE );
+                       description = talloc_strdup(talloc_tos(), "External Unix Service");
                }
        }
 
        /* add the new values */
 
-       regval_ctr_addvalue( values, "DisplayName", REG_SZ, (char*)dname.buffer, dname.uni_str_len*2);
-       regval_ctr_addvalue( values, "ImagePath", REG_SZ, (char*)ipath.buffer, ipath.uni_str_len*2);
-       regval_ctr_addvalue( values, "Description", REG_SZ, (char*)description.buffer, description.uni_str_len*2);
+       regval_ctr_addvalue_sz(values, "DisplayName", dname);
+       regval_ctr_addvalue_sz(values, "ImagePath", ipath);
+       regval_ctr_addvalue_sz(values, "Description", description);
+
+       TALLOC_FREE(dname);
+       TALLOC_FREE(ipath);
+       TALLOC_FREE(description);
 
        return;
 }
@@ -332,15 +333,16 @@ static void fill_service_values( const char *name, REGVAL_CTR *values )
 /********************************************************************
 ********************************************************************/
 
-static void add_new_svc_name( REGISTRY_KEY *key_parent, REGSUBKEY_CTR *subkeys,
-                              const char *name )
+static void add_new_svc_name(struct registry_key_handle *key_parent,
+                            struct regsubkey_ctr *subkeys,
+                            const char *name )
 {
-       REGISTRY_KEY *key_service = NULL, *key_secdesc = NULL;
+       struct registry_key_handle *key_service = NULL, *key_secdesc = NULL;
        WERROR wresult;
        char *path = NULL;
-       REGVAL_CTR *values = NULL;
-       REGSUBKEY_CTR *svc_subkeys = NULL;
-       SEC_DESC *sd = NULL;
+       struct regval_ctr *values = NULL;
+       struct regsubkey_ctr *svc_subkeys = NULL;
+       struct security_descriptor *sd = NULL;
        DATA_BLOB sd_blob;
        NTSTATUS status;
 
@@ -366,7 +368,8 @@ static void add_new_svc_name( REGISTRY_KEY *key_parent, REGSUBKEY_CTR *subkeys,
 
        /* add the 'Security' key */
 
-       if ( !(svc_subkeys = TALLOC_ZERO_P( key_service, REGSUBKEY_CTR )) ) {
+       wresult = regsubkey_ctr_init(key_service, &svc_subkeys);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
                TALLOC_FREE( key_service );
                return;
@@ -378,7 +381,8 @@ static void add_new_svc_name( REGISTRY_KEY *key_parent, REGSUBKEY_CTR *subkeys,
 
        /* now for the service values */
 
-       if ( !(values = TALLOC_ZERO_P( key_service, REGVAL_CTR )) ) {
+       wresult = regval_ctr_init(key_service, &values);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
                TALLOC_FREE( key_service );
                return;
@@ -407,7 +411,8 @@ static void add_new_svc_name( REGISTRY_KEY *key_parent, REGSUBKEY_CTR *subkeys,
        }
        SAFE_FREE(path);
 
-       if ( !(values = TALLOC_ZERO_P( key_secdesc, REGVAL_CTR )) ) {
+       wresult = regval_ctr_init(key_secdesc, &values);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
                TALLOC_FREE( key_secdesc );
                return;
@@ -429,7 +434,7 @@ static void add_new_svc_name( REGISTRY_KEY *key_parent, REGSUBKEY_CTR *subkeys,
        }
 
        regval_ctr_addvalue(values, "Security", REG_BINARY,
-                           (const char *)sd_blob.data, sd_blob.length);
+                           sd_blob.data, sd_blob.length);
        store_reg_values( key_secdesc, values );
 
        TALLOC_FREE( key_secdesc );
@@ -444,8 +449,8 @@ void svcctl_init_keys( void )
 {
        const char **service_list = lp_svcctl_list();
        int i;
-       REGSUBKEY_CTR *subkeys = NULL;
-       REGISTRY_KEY *key = NULL;
+       struct regsubkey_ctr *subkeys = NULL;
+       struct registry_key_handle *key = NULL;
        WERROR wresult;
 
        /* bad mojo here if the lookup failed.  Should not happen */
@@ -461,7 +466,8 @@ void svcctl_init_keys( void )
 
        /* lookup the available subkeys */
 
-       if ( !(subkeys = TALLOC_ZERO_P( key, REGSUBKEY_CTR )) ) {
+       wresult = regsubkey_ctr_init(key, &subkeys);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("svcctl_init_keys: talloc() failed!\n"));
                TALLOC_FREE( key );
                return;
@@ -500,12 +506,12 @@ void svcctl_init_keys( void )
  in case of any failure.
 ********************************************************************/
 
-SEC_DESC *svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *token )
+struct security_descriptor *svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, struct security_token *token )
 {
-       REGISTRY_KEY *key = NULL;
-       REGVAL_CTR *values = NULL;
-       REGISTRY_VALUE *val = NULL;
-       SEC_DESC *ret_sd = NULL;
+       struct registry_key_handle *key = NULL;
+       struct regval_ctr *values = NULL;
+       struct regval_blob *val = NULL;
+       struct security_descriptor *ret_sd = NULL;
        char *path= NULL;
        WERROR wresult;
        NTSTATUS status;
@@ -523,7 +529,8 @@ SEC_DESC *svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *
                goto done;
        }
 
-       if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
+       wresult = regval_ctr_init(key, &values);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("svcctl_get_secdesc: talloc() failed!\n"));
                goto done;
        }
@@ -561,13 +568,14 @@ done:
  Wrapper to make storing a Service sd easier
 ********************************************************************/
 
-bool svcctl_set_secdesc( TALLOC_CTX *ctx, const char *name, SEC_DESC *sec_desc, NT_USER_TOKEN *token )
+bool svcctl_set_secdesc( TALLOC_CTX *ctx, const char *name, struct security_descriptor *sec_desc, struct security_token *token )
 {
-       REGISTRY_KEY *key = NULL;
+       struct registry_key_handle *key = NULL;
        WERROR wresult;
        char *path = NULL;
-       REGVAL_CTR *values = NULL;
-       prs_struct ps;
+       struct regval_ctr *values = NULL;
+       DATA_BLOB blob;
+       NTSTATUS status;
        bool ret = False;
 
        /* now add the security descriptor */
@@ -585,7 +593,8 @@ bool svcctl_set_secdesc( TALLOC_CTX *ctx, const char *name, SEC_DESC *sec_desc,
        }
        SAFE_FREE(path);
 
-       if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
+       wresult = regval_ctr_init(key, &values);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("svcctl_set_secdesc: talloc() failed!\n"));
                TALLOC_FREE( key );
                return False;
@@ -593,21 +602,18 @@ bool svcctl_set_secdesc( TALLOC_CTX *ctx, const char *name, SEC_DESC *sec_desc,
 
        /* stream the printer security descriptor */
 
-       if (!prs_init( &ps, RPC_MAX_PDU_FRAG_LEN, key, MARSHALL)) {
-               DEBUG(0,("svcctl_set_secdesc: prs_init() failed!\n"));
+       status = marshall_sec_desc(ctx, sec_desc, &blob.data, &blob.length);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("svcctl_set_secdesc: ndr_push_struct_blob() failed!\n"));
                TALLOC_FREE( key );
                return False;
        }
 
-       if ( sec_io_desc("sec_desc", &sec_desc, &ps, 0 ) ) {
-               uint32 offset = prs_offset( &ps );
-               regval_ctr_addvalue( values, "Security", REG_BINARY, prs_data_p(&ps), offset );
-               ret = store_reg_values( key, values );
-       }
+       regval_ctr_addvalue( values, "Security", REG_BINARY, blob.data, blob.length);
+       ret = store_reg_values( key, values );
 
        /* cleanup */
 
-       prs_mem_free( &ps );
        TALLOC_FREE( key);
 
        return ret;
@@ -616,14 +622,15 @@ bool svcctl_set_secdesc( TALLOC_CTX *ctx, const char *name, SEC_DESC *sec_desc,
 /********************************************************************
 ********************************************************************/
 
-const char *svcctl_lookup_dispname(TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *token )
+const char *svcctl_lookup_dispname(TALLOC_CTX *ctx, const char *name, struct security_token *token )
 {
-       char *display_name = NULL;
-       REGISTRY_KEY *key = NULL;
-       REGVAL_CTR *values = NULL;
-       REGISTRY_VALUE *val = NULL;
+       const char *display_name = NULL;
+       struct registry_key_handle *key = NULL;
+       struct regval_ctr *values = NULL;
+       struct regval_blob *val = NULL;
        char *path = NULL;
        WERROR wresult;
+       DATA_BLOB blob;
 
        /* now add the security descriptor */
 
@@ -640,7 +647,8 @@ const char *svcctl_lookup_dispname(TALLOC_CTX *ctx, const char *name, NT_USER_TO
        }
        SAFE_FREE(path);
 
-       if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
+       wresult = regval_ctr_init(key, &values);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("svcctl_lookup_dispname: talloc() failed!\n"));
                TALLOC_FREE( key );
                goto fail;
@@ -651,7 +659,8 @@ const char *svcctl_lookup_dispname(TALLOC_CTX *ctx, const char *name, NT_USER_TO
        if ( !(val = regval_ctr_getvalue( values, "DisplayName" )) )
                goto fail;
 
-       rpcstr_pull_talloc(ctx, &display_name, regval_data_p(val), regval_size(val), 0 );
+       blob = data_blob_const(regval_data_p(val), regval_size(val));
+       pull_reg_sz(ctx, &blob, &display_name);
 
        TALLOC_FREE( key );
 
@@ -666,14 +675,15 @@ fail:
 /********************************************************************
 ********************************************************************/
 
-const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *token )
+const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, struct security_token *token )
 {
-       char *description = NULL;
-       REGISTRY_KEY *key = NULL;
-       REGVAL_CTR *values = NULL;
-       REGISTRY_VALUE *val = NULL;
+       const char *description = NULL;
+       struct registry_key_handle *key = NULL;
+       struct regval_ctr *values = NULL;
+       struct regval_blob *val = NULL;
        char *path = NULL;
        WERROR wresult;
+       DATA_BLOB blob;
 
        /* now add the security descriptor */
 
@@ -690,7 +700,8 @@ const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, NT_USER
        }
        SAFE_FREE(path);
 
-       if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
+       wresult = regval_ctr_init(key, &values);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("svcctl_lookup_description: talloc() failed!\n"));
                TALLOC_FREE( key );
                return NULL;
@@ -702,7 +713,10 @@ const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, NT_USER
                TALLOC_FREE( key );
                return "Unix Service";
        }
-       rpcstr_pull_talloc(ctx, &description, regval_data_p(val), regval_size(val), 0 );
+
+       blob = data_blob_const(regval_data_p(val), regval_size(val));
+       pull_reg_sz(ctx, &blob, &description);
+
        TALLOC_FREE(key);
 
        return description;
@@ -712,10 +726,10 @@ const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, NT_USER
 /********************************************************************
 ********************************************************************/
 
-REGVAL_CTR *svcctl_fetch_regvalues( const char *name, NT_USER_TOKEN *token )
+struct regval_ctr *svcctl_fetch_regvalues(const char *name, struct security_token *token)
 {
-       REGISTRY_KEY *key = NULL;
-       REGVAL_CTR *values = NULL;
+       struct registry_key_handle *key = NULL;
+       struct regval_ctr *values = NULL;
        char *path = NULL;
        WERROR wresult;
 
@@ -734,7 +748,8 @@ REGVAL_CTR *svcctl_fetch_regvalues( const char *name, NT_USER_TOKEN *token )
        }
        SAFE_FREE(path);
 
-       if ( !(values = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
+       wresult = regval_ctr_init(NULL, &values);
+       if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0,("svcctl_fetch_regvalues: talloc() failed!\n"));
                TALLOC_FREE( key );
                return NULL;