docs: update idmap cache time to be consistent with testparm
[Samba.git] / source3 / rpc_server / srv_pipe_hnd.c
bloba05fafd4b070c34c2d206dfc7ec51921fb7d68b5
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1998,
5 * Largely re-written : 2005
6 * Copyright (C) Jeremy Allison 1998 - 2005
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "fake_file.h"
24 #include "rpc_dce.h"
25 #include "ntdomain.h"
26 #include "rpc_server/rpc_ncacn_np.h"
27 #include "rpc_server/srv_pipe_hnd.h"
28 #include "rpc_server/srv_pipe.h"
29 #include "rpc_server/rpc_server.h"
30 #include "rpc_server/rpc_config.h"
31 #include "../lib/tsocket/tsocket.h"
32 #include "../lib/util/tevent_ntstatus.h"
33 #include "librpc/ndr/ndr_table.h"
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_RPC_SRV
38 bool fsp_is_np(struct files_struct *fsp)
40 enum FAKE_FILE_TYPE type;
42 if ((fsp == NULL) || (fsp->fake_file_handle == NULL)) {
43 return false;
46 type = fsp->fake_file_handle->type;
48 return (type == FAKE_FILE_TYPE_NAMED_PIPE_PROXY);
51 NTSTATUS np_open(TALLOC_CTX *mem_ctx, const char *name,
52 const struct tsocket_address *local_address,
53 const struct tsocket_address *remote_address,
54 struct auth_session_info *session_info,
55 struct tevent_context *ev_ctx,
56 struct messaging_context *msg_ctx,
57 struct fake_file_handle **phandle)
59 enum rpc_service_mode_e pipe_mode;
60 const char **proxy_list;
61 struct fake_file_handle *handle;
62 struct ndr_syntax_id syntax;
63 struct npa_state *npa = NULL;
64 NTSTATUS status;
65 bool ok;
67 proxy_list = lp_parm_string_list(-1, "np", "proxy", NULL);
69 handle = talloc(mem_ctx, struct fake_file_handle);
70 if (handle == NULL) {
71 return NT_STATUS_NO_MEMORY;
74 /* Check what is the server type for this pipe.
75 Defaults to "embedded" */
76 pipe_mode = rpc_service_mode(name);
78 /* Still support the old method for defining external servers */
79 if ((proxy_list != NULL) && str_list_check_ci(proxy_list, name)) {
80 pipe_mode = RPC_SERVICE_MODE_EXTERNAL;
83 switch (pipe_mode) {
84 case RPC_SERVICE_MODE_EXTERNAL:
85 status = make_external_rpc_pipe(handle,
86 name,
87 local_address,
88 remote_address,
89 session_info,
90 &npa);
91 if (!NT_STATUS_IS_OK(status)) {
92 talloc_free(handle);
93 return status;
96 handle->private_data = (void *)npa;
97 handle->type = FAKE_FILE_TYPE_NAMED_PIPE_PROXY;
99 break;
100 case RPC_SERVICE_MODE_EMBEDDED:
101 /* Check if we this daemon handles this pipe */
102 ok = is_known_pipename(name, &syntax);
103 if (!ok) {
104 DEBUG(0, ("ERROR! '%s' is not a registred pipe!\n",
105 name));
106 talloc_free(handle);
107 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
110 status = make_internal_rpc_pipe_socketpair(handle,
111 ev_ctx,
112 msg_ctx,
113 name,
114 &syntax,
115 remote_address,
116 session_info,
117 &npa);
118 if (!NT_STATUS_IS_OK(status)) {
119 talloc_free(handle);
120 return status;
123 handle->private_data = (void *)npa;
124 handle->type = FAKE_FILE_TYPE_NAMED_PIPE_PROXY;
126 break;
127 case RPC_SERVICE_MODE_DISABLED:
128 talloc_free(handle);
129 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
132 *phandle = handle;
134 return NT_STATUS_OK;
137 bool np_read_in_progress(struct fake_file_handle *handle)
139 if (handle->type == FAKE_FILE_TYPE_NAMED_PIPE_PROXY) {
140 struct npa_state *p =
141 talloc_get_type_abort(handle->private_data,
142 struct npa_state);
143 size_t read_count;
145 read_count = tevent_queue_length(p->read_queue);
146 if (read_count > 0) {
147 return true;
150 return false;
153 return false;
156 struct np_write_state {
157 struct tevent_context *ev;
158 struct npa_state *p;
159 struct iovec iov;
160 ssize_t nwritten;
163 static void np_write_done(struct tevent_req *subreq);
165 struct tevent_req *np_write_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
166 struct fake_file_handle *handle,
167 const uint8_t *data, size_t len)
169 struct tevent_req *req;
170 struct np_write_state *state;
171 NTSTATUS status;
173 DEBUG(6, ("np_write_send: len: %d\n", (int)len));
174 dump_data(50, data, len);
176 req = tevent_req_create(mem_ctx, &state, struct np_write_state);
177 if (req == NULL) {
178 return NULL;
181 if (len == 0) {
182 state->nwritten = 0;
183 status = NT_STATUS_OK;
184 goto post_status;
187 if (handle->type == FAKE_FILE_TYPE_NAMED_PIPE_PROXY) {
188 struct npa_state *p = talloc_get_type_abort(
189 handle->private_data, struct npa_state);
190 struct tevent_req *subreq;
192 state->ev = ev;
193 state->p = p;
194 state->iov.iov_base = discard_const_p(void, data);
195 state->iov.iov_len = len;
197 subreq = tstream_writev_queue_send(state, ev,
198 p->stream,
199 p->write_queue,
200 &state->iov, 1);
201 if (subreq == NULL) {
202 goto fail;
204 tevent_req_set_callback(subreq, np_write_done, req);
205 return req;
208 status = NT_STATUS_INVALID_HANDLE;
209 post_status:
210 if (NT_STATUS_IS_OK(status)) {
211 tevent_req_done(req);
212 } else {
213 tevent_req_nterror(req, status);
215 return tevent_req_post(req, ev);
216 fail:
217 TALLOC_FREE(req);
218 return NULL;
221 static void np_write_done(struct tevent_req *subreq)
223 struct tevent_req *req = tevent_req_callback_data(
224 subreq, struct tevent_req);
225 struct np_write_state *state = tevent_req_data(
226 req, struct np_write_state);
227 ssize_t received;
228 int err;
230 received = tstream_writev_queue_recv(subreq, &err);
231 if (received < 0) {
232 tevent_req_nterror(req, map_nt_error_from_unix(err));
233 return;
235 state->nwritten = received;
236 tevent_req_done(req);
239 NTSTATUS np_write_recv(struct tevent_req *req, ssize_t *pnwritten)
241 struct np_write_state *state = tevent_req_data(
242 req, struct np_write_state);
243 NTSTATUS status;
245 if (tevent_req_is_nterror(req, &status)) {
246 return status;
248 *pnwritten = state->nwritten;
249 return NT_STATUS_OK;
252 struct np_ipc_readv_next_vector_state {
253 uint8_t *buf;
254 size_t len;
255 off_t ofs;
256 size_t remaining;
259 static void np_ipc_readv_next_vector_init(struct np_ipc_readv_next_vector_state *s,
260 uint8_t *buf, size_t len)
262 ZERO_STRUCTP(s);
264 s->buf = buf;
265 s->len = MIN(len, UINT16_MAX);
268 static int np_ipc_readv_next_vector(struct tstream_context *stream,
269 void *private_data,
270 TALLOC_CTX *mem_ctx,
271 struct iovec **_vector,
272 size_t *count)
274 struct np_ipc_readv_next_vector_state *state =
275 (struct np_ipc_readv_next_vector_state *)private_data;
276 struct iovec *vector;
277 ssize_t pending;
278 size_t wanted;
280 if (state->ofs == state->len) {
281 *_vector = NULL;
282 *count = 0;
283 return 0;
286 pending = tstream_pending_bytes(stream);
287 if (pending == -1) {
288 return -1;
291 if (pending == 0 && state->ofs != 0) {
292 /* return a short read */
293 *_vector = NULL;
294 *count = 0;
295 return 0;
298 if (pending == 0) {
299 /* we want at least one byte and recheck again */
300 wanted = 1;
301 } else {
302 size_t missing = state->len - state->ofs;
303 if (pending > missing) {
304 /* there's more available */
305 state->remaining = pending - missing;
306 wanted = missing;
307 } else {
308 /* read what we can get and recheck in the next cycle */
309 wanted = pending;
313 vector = talloc_array(mem_ctx, struct iovec, 1);
314 if (!vector) {
315 return -1;
318 vector[0].iov_base = state->buf + state->ofs;
319 vector[0].iov_len = wanted;
321 state->ofs += wanted;
323 *_vector = vector;
324 *count = 1;
325 return 0;
328 struct np_read_state {
329 struct npa_state *p;
330 struct np_ipc_readv_next_vector_state next_vector;
332 ssize_t nread;
333 bool is_data_outstanding;
336 static void np_read_done(struct tevent_req *subreq);
338 struct tevent_req *np_read_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
339 struct fake_file_handle *handle,
340 uint8_t *data, size_t len)
342 struct tevent_req *req;
343 struct np_read_state *state;
344 NTSTATUS status;
346 req = tevent_req_create(mem_ctx, &state, struct np_read_state);
347 if (req == NULL) {
348 return NULL;
351 if (handle->type == FAKE_FILE_TYPE_NAMED_PIPE_PROXY) {
352 struct npa_state *p = talloc_get_type_abort(
353 handle->private_data, struct npa_state);
354 struct tevent_req *subreq;
356 np_ipc_readv_next_vector_init(&state->next_vector,
357 data, len);
359 subreq = tstream_readv_pdu_queue_send(state,
361 p->stream,
362 p->read_queue,
363 np_ipc_readv_next_vector,
364 &state->next_vector);
365 if (subreq == NULL) {
366 status = NT_STATUS_NO_MEMORY;
367 goto post_status;
369 tevent_req_set_callback(subreq, np_read_done, req);
370 return req;
373 status = NT_STATUS_INVALID_HANDLE;
374 post_status:
375 if (NT_STATUS_IS_OK(status)) {
376 tevent_req_done(req);
377 } else {
378 tevent_req_nterror(req, status);
380 return tevent_req_post(req, ev);
383 static void np_read_done(struct tevent_req *subreq)
385 struct tevent_req *req = tevent_req_callback_data(
386 subreq, struct tevent_req);
387 struct np_read_state *state = tevent_req_data(
388 req, struct np_read_state);
389 ssize_t ret;
390 int err;
392 ret = tstream_readv_pdu_queue_recv(subreq, &err);
393 TALLOC_FREE(subreq);
394 if (ret == -1) {
395 tevent_req_nterror(req, map_nt_error_from_unix(err));
396 return;
399 state->nread = ret;
400 state->is_data_outstanding = (state->next_vector.remaining > 0);
402 tevent_req_done(req);
403 return;
406 NTSTATUS np_read_recv(struct tevent_req *req, ssize_t *nread,
407 bool *is_data_outstanding)
409 struct np_read_state *state = tevent_req_data(
410 req, struct np_read_state);
411 NTSTATUS status;
413 if (tevent_req_is_nterror(req, &status)) {
414 return status;
417 DEBUG(10, ("Received %d bytes. There is %smore data outstanding\n",
418 (int)state->nread, state->is_data_outstanding?"":"no "));
420 *nread = state->nread;
421 *is_data_outstanding = state->is_data_outstanding;
422 return NT_STATUS_OK;