s3:services_db: extract core of svcctl_set_secdesc to _internal version
[amitay/samba.git] / source3 / services / services_db.c
index 19fb1b56235c1d7c25afdf3a1fc73dacf28868ee..dcfb018b41eaecd04f88679962ebce160de27aaa 100644 (file)
@@ -88,6 +88,8 @@ struct service_display_info common_unix_svcs[] = {
   { NULL, NULL, NULL, NULL }
 };
 
   { NULL, NULL, NULL, NULL }
 };
 
+static WERROR svcctl_set_secdesc_internal(struct registry_key *key,
+                                         struct security_descriptor *sec_desc);
 
 /********************************************************************
 ********************************************************************/
 
 /********************************************************************
 ********************************************************************/
@@ -565,30 +567,15 @@ done:
  Wrapper to make storing a Service sd easier
 ********************************************************************/
 
  Wrapper to make storing a Service sd easier
 ********************************************************************/
 
-bool svcctl_set_secdesc(const char *name, struct security_descriptor *sec_desc,
-                       struct security_token *token)
+static WERROR svcctl_set_secdesc_internal(struct registry_key *key,
+                                         struct security_descriptor *sec_desc)
 {
 {
-       struct registry_key *key = NULL;
        struct registry_key *key_security = NULL;
        WERROR wresult;
        struct registry_key *key_security = NULL;
        WERROR wresult;
-       char *path = NULL;
        struct registry_value value;
        NTSTATUS status;
        struct registry_value value;
        NTSTATUS status;
-       bool ret = false;
-       TALLOC_CTX *mem_ctx = talloc_stackframe();
        enum winreg_CreateAction action = REG_ACTION_NONE;
        enum winreg_CreateAction action = REG_ACTION_NONE;
-
-       path = talloc_asprintf(mem_ctx, "%s\\%s", KEY_SERVICES, name);
-       if (path == NULL) {
-               goto done;
-       }
-
-       wresult = reg_open_path(mem_ctx, path, REG_KEY_ALL, token, &key);
-       if ( !W_ERROR_IS_OK(wresult) ) {
-               DEBUG(0, ("svcctl_set_secdesc: key lookup failed! [%s] (%s)\n",
-                         path, win_errstr(wresult)));
-               goto done;
-       }
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
 
        wresult = reg_createkey(mem_ctx, key, "Security", REG_KEY_ALL, &key_security, &action);
        if (!W_ERROR_IS_OK(wresult)) {
 
        wresult = reg_createkey(mem_ctx, key, "Security", REG_KEY_ALL, &key_security, &action);
        if (!W_ERROR_IS_OK(wresult)) {
@@ -601,7 +588,9 @@ bool svcctl_set_secdesc(const char *name, struct security_descriptor *sec_desc,
        status = marshall_sec_desc(mem_ctx, sec_desc, &value.data.data,
                                   &value.data.length);
        if (!NT_STATUS_IS_OK(status)) {
        status = marshall_sec_desc(mem_ctx, sec_desc, &value.data.data,
                                   &value.data.length);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("svcctl_set_secdesc: ndr_push_struct_blob() failed!\n"));
+               DEBUG(0, ("svcctl_set_secdesc: marshall_sec_desc() failed: %s\n",
+                         nt_errstr(status)));
+               wresult = ntstatus_to_werror(status);
                goto done;
        }
 
                goto done;
        }
 
@@ -611,10 +600,37 @@ bool svcctl_set_secdesc(const char *name, struct security_descriptor *sec_desc,
        if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0, ("svcctl_set_secdesc: reg_setvalue failed: %s\n",
                          win_errstr(wresult)));
        if (!W_ERROR_IS_OK(wresult)) {
                DEBUG(0, ("svcctl_set_secdesc: reg_setvalue failed: %s\n",
                          win_errstr(wresult)));
+       }
+
+done:
+       talloc_free(mem_ctx);
+       return wresult;
+}
+
+bool svcctl_set_secdesc(const char *name, struct security_descriptor *sec_desc,
+                       struct security_token *token)
+{
+       struct registry_key *key = NULL;
+       WERROR wresult;
+       char *path = NULL;
+       bool ret = false;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
+
+       path = talloc_asprintf(mem_ctx, "%s\\%s", KEY_SERVICES, name);
+       if (path == NULL) {
                goto done;
        }
 
                goto done;
        }
 
-       ret = true;
+       wresult = reg_open_path(mem_ctx, path, REG_KEY_ALL, token, &key);
+       if ( !W_ERROR_IS_OK(wresult) ) {
+               DEBUG(0, ("svcctl_set_secdesc: key lookup failed! [%s] (%s)\n",
+                         path, win_errstr(wresult)));
+               goto done;
+       }
+
+       wresult = svcctl_set_secdesc_internal(key, sec_desc);
+
+       ret = W_ERROR_IS_OK(wresult);
 
 done:
        talloc_free(mem_ctx);
 
 done:
        talloc_free(mem_ctx);