78caa5bae70fb2e8198086d440216f292651bf94
[metze/samba/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 "../lib/util/tevent_ntstatus.h"
22 #include "rpc_client/rpc_transport.h"
23 #include "libsmb/cli_np_tstream.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_CLI
27
28 struct rpc_transport_np_init_state {
29         struct rpc_cli_transport *transport;
30 };
31
32 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
33
34 struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
35                                               struct tevent_context *ev,
36                                               struct cli_state *cli,
37                                               const struct ndr_syntax_id *abstract_syntax)
38 {
39         struct tevent_req *req;
40         struct rpc_transport_np_init_state *state;
41         const char *pipe_name;
42         struct tevent_req *subreq;
43
44         req = tevent_req_create(mem_ctx, &state,
45                                 struct rpc_transport_np_init_state);
46         if (req == NULL) {
47                 return NULL;
48         }
49
50         pipe_name = get_pipe_name_from_syntax(state, abstract_syntax);
51         if (tevent_req_nomem(pipe_name, req)) {
52                 return tevent_req_post(req, ev);
53         }
54
55         while (pipe_name[0] == '\\') {
56                 pipe_name++;
57         }
58
59         subreq = tstream_cli_np_open_send(state, ev, cli, pipe_name);
60         if (tevent_req_nomem(subreq, req)) {
61                 return tevent_req_post(req, ev);
62         }
63         tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
64
65         return req;
66 }
67
68 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
69 {
70         struct tevent_req *req = tevent_req_callback_data(
71                 subreq, struct tevent_req);
72         struct rpc_transport_np_init_state *state = tevent_req_data(
73                 req, struct rpc_transport_np_init_state);
74         NTSTATUS status;
75         struct tstream_context *stream;
76
77         status = tstream_cli_np_open_recv(subreq, state, &stream);
78         TALLOC_FREE(subreq);
79         if (!NT_STATUS_IS_OK(status)) {
80                 tevent_req_nterror(req, status);
81                 return;
82         }
83
84         status = rpc_transport_tstream_init(state,
85                                             &stream,
86                                             &state->transport);
87         if (!NT_STATUS_IS_OK(status)) {
88                 tevent_req_nterror(req, status);
89                 return;
90         }
91
92         tevent_req_done(req);
93 }
94
95 NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
96                                     TALLOC_CTX *mem_ctx,
97                                     struct rpc_cli_transport **presult)
98 {
99         struct rpc_transport_np_init_state *state = tevent_req_data(
100                 req, struct rpc_transport_np_init_state);
101         NTSTATUS status;
102
103         if (tevent_req_is_nterror(req, &status)) {
104                 return status;
105         }
106
107         *presult = talloc_move(mem_ctx, &state->transport);
108         return NT_STATUS_OK;
109 }
110
111 NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
112                                const struct ndr_syntax_id *abstract_syntax,
113                                struct rpc_cli_transport **presult)
114 {
115         TALLOC_CTX *frame = talloc_stackframe();
116         struct tevent_context *ev;
117         struct tevent_req *req;
118         NTSTATUS status = NT_STATUS_OK;
119
120         ev = samba_tevent_context_init(frame);
121         if (ev == NULL) {
122                 status = NT_STATUS_NO_MEMORY;
123                 goto fail;
124         }
125
126         req = rpc_transport_np_init_send(frame, ev, cli, abstract_syntax);
127         if (req == NULL) {
128                 status = NT_STATUS_NO_MEMORY;
129                 goto fail;
130         }
131
132         if (!tevent_req_poll(req, ev)) {
133                 status = map_nt_error_from_unix(errno);
134                 goto fail;
135         }
136
137         status = rpc_transport_np_init_recv(req, mem_ctx, presult);
138  fail:
139         TALLOC_FREE(frame);
140         return status;
141 }