s3-winbind: talloc winbindd request and response structs
authorDavid Disseldorp <ddiss@samba.org>
Tue, 16 Apr 2013 12:19:53 +0000 (14:19 +0200)
committerDavid Disseldorp <ddiss@samba.org>
Tue, 16 Apr 2013 13:51:57 +0000 (15:51 +0200)
source3/winbindd/winbindd_dual.c

index f4b55643a090ca94da3f6c735a105309292ec29e..dc5e50f67a18106d3c0bd0e17716c464889bb1c2 100644 (file)
@@ -48,28 +48,36 @@ static struct winbindd_child *winbindd_children = NULL;
 
 /* Read some data from a client connection */
 
-static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
+static NTSTATUS child_read_request(TALLOC_CTX *mem_ctx, int sock,
+                                  struct winbindd_request **wreq_out)
 {
        NTSTATUS status;
+       struct winbindd_request *wreq;
+
+       wreq = talloc(mem_ctx, struct winbindd_request);
+       if (wreq == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto err_out;
+       }
 
        status = read_data(sock, (char *)wreq, sizeof(*wreq));
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("child_read_request: read_data failed: %s\n",
                          nt_errstr(status)));
-               return status;
+               goto err_wreq_free;
        }
 
        if (wreq->extra_len == 0) {
                wreq->extra_data.data = NULL;
-               return NT_STATUS_OK;
+               goto done;
        }
 
        DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq->extra_len));
 
-       wreq->extra_data.data = SMB_MALLOC_ARRAY(char, wreq->extra_len + 1);
+       wreq->extra_data.data = talloc_array(wreq, char, wreq->extra_len + 1);
        if (wreq->extra_data.data == NULL) {
-               DEBUG(0, ("malloc failed\n"));
-               return NT_STATUS_NO_MEMORY;
+               status = NT_STATUS_NO_MEMORY;
+               goto err_wreq_free;
        }
 
        /* Ensure null termination */
@@ -79,7 +87,16 @@ static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("Could not read extra data: %s\n",
                          nt_errstr(status)));
+               goto err_wreq_free;
        }
+done:
+       wreq->null_term = '\0';
+       *wreq_out = wreq;
+       return NT_STATUS_OK;
+
+err_wreq_free:
+       talloc_free(wreq);
+err_out:
        return status;
 }
 
@@ -1309,8 +1326,6 @@ static bool fork_domain_child(struct winbindd_child *child)
 {
        int fdpair[2];
        struct winbindd_cli_state state;
-       struct winbindd_request request;
-       struct winbindd_response response;
        struct winbindd_domain *primary_domain = NULL;
        NTSTATUS status;
        ssize_t nwritten;
@@ -1331,8 +1346,6 @@ static bool fork_domain_child(struct winbindd_child *child)
 
        ZERO_STRUCT(state);
        state.pid = getpid();
-       state.request = &request;
-       state.response = &response;
 
        child->pid = fork();
 
@@ -1557,9 +1570,9 @@ static bool fork_domain_child(struct winbindd_child *child)
                        _exit(1);
                }
 
-               /* fetch a request from the main daemon */
-               status = child_read_request(state.sock, state.request);
-
+               /* alloc and fetch a request from the main daemon */
+               status = child_read_request(mem_ctx, state.sock,
+                                           &state.request);
                if (!NT_STATUS_IS_OK(status)) {
                        /* we lost contact with our parent */
                        _exit(0);
@@ -1567,16 +1580,17 @@ static bool fork_domain_child(struct winbindd_child *child)
 
                DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
 
-               ZERO_STRUCTP(state.response);
-               state.request->null_term = '\0';
+               state.response = talloc_zero(mem_ctx, struct winbindd_response);
+               if (state.response == NULL) {
+                       _exit(1);
+               }
+
                state.mem_ctx = mem_ctx;
                child_process_request(child, &state);
 
                DEBUG(4, ("Finished processing child request %d\n",
                          (int)state.request->cmd));
 
-               SAFE_FREE(state.request->extra_data.data);
-
                status = child_write_response(state.sock, state.response);
                if (!NT_STATUS_IS_OK(status)) {
                        _exit(1);