Fix bug #7996 - sgid bit lost on folder rename.
[ira/wip.git] / source3 / rpc_client / rpc_transport_np.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC client transport over named pipes
4  *  Copyright (C) Volker Lendecke 2009
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "libsmb/cli_np_tstream.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_RPC_CLI
25
26 struct rpc_transport_np_init_state {
27         struct rpc_cli_transport *transport;
28 };
29
30 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
31
32 struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
33                                               struct event_context *ev,
34                                               struct cli_state *cli,
35                                               const struct ndr_syntax_id *abstract_syntax)
36 {
37         struct tevent_req *req;
38         struct rpc_transport_np_init_state *state;
39         const char *pipe_name;
40         struct tevent_req *subreq;
41
42         req = tevent_req_create(mem_ctx, &state,
43                                 struct rpc_transport_np_init_state);
44         if (req == NULL) {
45                 return NULL;
46         }
47
48         pipe_name = get_pipe_name_from_syntax(state, abstract_syntax);
49         if (tevent_req_nomem(pipe_name, req)) {
50                 return tevent_req_post(req, ev);
51         }
52
53         subreq = tstream_cli_np_open_send(state, ev, cli, pipe_name);
54         if (tevent_req_nomem(subreq, req)) {
55                 return tevent_req_post(req, ev);
56         }
57         tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
58
59         return req;
60 }
61
62 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
63 {
64         struct tevent_req *req = tevent_req_callback_data(
65                 subreq, struct tevent_req);
66         struct rpc_transport_np_init_state *state = tevent_req_data(
67                 req, struct rpc_transport_np_init_state);
68         NTSTATUS status;
69         struct tstream_context *stream;
70
71         status = tstream_cli_np_open_recv(subreq, state, &stream);
72         TALLOC_FREE(subreq);
73         if (!NT_STATUS_IS_OK(status)) {
74                 tevent_req_nterror(req, status);
75                 return;
76         }
77
78         status = rpc_transport_tstream_init(state,
79                                             &stream,
80                                             &state->transport);
81         if (!NT_STATUS_IS_OK(status)) {
82                 tevent_req_nterror(req, status);
83                 return;
84         }
85
86         tevent_req_done(req);
87 }
88
89 NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
90                                     TALLOC_CTX *mem_ctx,
91                                     struct rpc_cli_transport **presult)
92 {
93         struct rpc_transport_np_init_state *state = tevent_req_data(
94                 req, struct rpc_transport_np_init_state);
95         NTSTATUS status;
96
97         if (tevent_req_is_nterror(req, &status)) {
98                 return status;
99         }
100
101         *presult = talloc_move(mem_ctx, &state->transport);
102         return NT_STATUS_OK;
103 }
104
105 NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
106                                const struct ndr_syntax_id *abstract_syntax,
107                                struct rpc_cli_transport **presult)
108 {
109         TALLOC_CTX *frame = talloc_stackframe();
110         struct event_context *ev;
111         struct tevent_req *req;
112         NTSTATUS status = NT_STATUS_OK;
113
114         ev = event_context_init(frame);
115         if (ev == NULL) {
116                 status = NT_STATUS_NO_MEMORY;
117                 goto fail;
118         }
119
120         req = rpc_transport_np_init_send(frame, ev, cli, abstract_syntax);
121         if (req == NULL) {
122                 status = NT_STATUS_NO_MEMORY;
123                 goto fail;
124         }
125
126         if (!tevent_req_poll(req, ev)) {
127                 status = map_nt_error_from_unix(errno);
128                 goto fail;
129         }
130
131         status = rpc_transport_np_init_recv(req, mem_ctx, presult);
132  fail:
133         TALLOC_FREE(frame);
134         return status;
135 }