s3-rpc_server: Introduce transport in pipe_struct.
[kai/samba.git] / source3 / rpc_server / rpc_server.c
1 /*
2    Unix SMB/Netbios implementation.
3    Generic infrstructure for RPC Daemons
4    Copyright (C) Simo Sorce 2010
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 "rpc_server/rpc_server.h"
22 #include "rpc_dce.h"
23 #include "librpc/gen_ndr/netlogon.h"
24 #include "librpc/gen_ndr/auth.h"
25 #include "registry/reg_parse_prs.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "libcli/named_pipe_auth/npa_tstream.h"
28 #include "../auth/auth_sam_reply.h"
29
30 #define SERVER_TCP_LOW_PORT  1024
31 #define SERVER_TCP_HIGH_PORT 1300
32
33 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
34                                             struct auth_session_info_transport **session_info)
35 {
36         struct auth_session_info_transport *i;
37         struct auth_serversupplied_info *s;
38         struct auth_user_info_dc *u;
39         union netr_Validation val;
40         NTSTATUS status;
41
42         i = talloc_zero(mem_ctx, struct auth_session_info_transport);
43         if (i == NULL) {
44                 return NT_STATUS_NO_MEMORY;
45         }
46
47         status = make_server_info_guest(i, &s);
48         if (!NT_STATUS_IS_OK(status)) {
49                 return status;
50         }
51
52         i->security_token = s->security_token;
53         i->session_key    = s->user_session_key;
54
55         val.sam3 = s->info3;
56
57         status = make_user_info_dc_netlogon_validation(mem_ctx,
58                                                        "",
59                                                        3,
60                                                        &val,
61                                                        &u);
62         if (!NT_STATUS_IS_OK(status)) {
63                 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
64                 return status;
65         }
66         i->info = talloc_move(i, &u->info);
67         talloc_free(u);
68
69         *session_info = i;
70
71         return NT_STATUS_OK;
72 }
73
74 /* Creates a pipes_struct and initializes it with the information
75  * sent from the client */
76 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
77                                     const char *pipe_name,
78                                     const struct ndr_syntax_id id,
79                                     enum dcerpc_transport_t transport,
80                                     const char *client_address,
81                                     const char *server_address,
82                                     struct auth_session_info_transport *session_info,
83                                     struct pipes_struct **_p,
84                                     int *perrno)
85 {
86         struct netr_SamInfo3 *info3;
87         struct auth_user_info_dc *auth_user_info_dc;
88         struct pipes_struct *p;
89         NTSTATUS status;
90         bool ok;
91
92         p = talloc_zero(mem_ctx, struct pipes_struct);
93         if (!p) {
94                 *perrno = ENOMEM;
95                 return -1;
96         }
97         p->syntax = id;
98         p->transport = transport;
99
100         p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
101         if (!p->mem_ctx) {
102                 TALLOC_FREE(p);
103                 *perrno = ENOMEM;
104                 return -1;
105         }
106
107         ok = init_pipe_handles(p, &id);
108         if (!ok) {
109                 DEBUG(1, ("Failed to init handles\n"));
110                 TALLOC_FREE(p);
111                 *perrno = EINVAL;
112                 return -1;
113         }
114
115
116         data_blob_free(&p->in_data.data);
117         data_blob_free(&p->in_data.pdu);
118
119         p->endian = RPC_LITTLE_ENDIAN;
120
121         /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
122         auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
123         if (!auth_user_info_dc) {
124                 TALLOC_FREE(p);
125                 *perrno = ENOMEM;
126                 return -1;
127         }
128
129         auth_user_info_dc->num_sids = session_info->security_token->num_sids;
130         auth_user_info_dc->sids = session_info->security_token->sids;
131         auth_user_info_dc->info = session_info->info;
132         auth_user_info_dc->user_session_key = session_info->session_key;
133
134         /* This creates the input structure that make_server_info_info3 is looking for */
135         status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
136                                                     &info3);
137
138         if (!NT_STATUS_IS_OK(status)) {
139                 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
140                 TALLOC_FREE(p);
141                 *perrno = EINVAL;
142                 return -1;
143         }
144
145         status = make_server_info_info3(p,
146                                         info3->base.account_name.string,
147                                         info3->base.domain.string,
148                                         &p->session_info, info3);
149         if (!NT_STATUS_IS_OK(status)) {
150                 DEBUG(1, ("Failed to init server info\n"));
151                 TALLOC_FREE(p);
152                 *perrno = EINVAL;
153                 return -1;
154         }
155
156         /*
157          * Some internal functions need a local token to determine access to
158          * resoutrces.
159          */
160         status = create_local_token(p->session_info);
161         if (!NT_STATUS_IS_OK(status)) {
162                 DEBUG(1, ("Failed to init local auth token\n"));
163                 TALLOC_FREE(p);
164                 *perrno = EINVAL;
165                 return -1;
166         }
167
168         /* Now override the session_info->security_token with the exact
169          * security_token we were given from the other side,
170          * regardless of what we just calculated */
171         p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
172
173         /* Also set the session key to the correct value */
174         p->session_info->user_session_key = session_info->session_key;
175         p->session_info->user_session_key.data = talloc_move(p->session_info, &session_info->session_key.data);
176
177         p->client_id = talloc_zero(p, struct client_address);
178         if (!p->client_id) {
179                 TALLOC_FREE(p);
180                 *perrno = ENOMEM;
181                 return -1;
182         }
183         strlcpy(p->client_id->addr,
184                 client_address, sizeof(p->client_id->addr));
185         p->client_id->name = talloc_strdup(p->client_id, client_address);
186         if (p->client_id->name == NULL) {
187                 TALLOC_FREE(p);
188                 *perrno = ENOMEM;
189                 return -1;
190         }
191
192         if (server_address != NULL) {
193                 p->server_id = talloc_zero(p, struct client_address);
194                 if (p->client_id == NULL) {
195                         TALLOC_FREE(p);
196                         *perrno = ENOMEM;
197                         return -1;
198                 }
199
200                 strlcpy(p->server_id->addr,
201                         server_address,
202                         sizeof(p->server_id->addr));
203
204                 p->server_id->name = talloc_strdup(p->server_id,
205                                                    server_address);
206                 if (p->server_id->name == NULL) {
207                         TALLOC_FREE(p);
208                         *perrno = ENOMEM;
209                         return -1;
210                 }
211         }
212
213         talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
214
215         *_p = p;
216         return 0;
217 }
218
219 /* Add some helper functions to wrap the common ncacn packet reading functions
220  * until we can share more dcerpc code */
221 struct dcerpc_ncacn_read_packet_state {
222         struct ncacn_packet *pkt;
223         DATA_BLOB buffer;
224 };
225
226 static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq);
227
228 static struct tevent_req *dcerpc_ncacn_read_packet_send(TALLOC_CTX *mem_ctx,
229                                         struct tevent_context *ev,
230                                         struct tstream_context *tstream)
231 {
232         struct dcerpc_ncacn_read_packet_state *state;
233         struct tevent_req *req, *subreq;
234
235         req = tevent_req_create(mem_ctx, &state,
236                                 struct dcerpc_ncacn_read_packet_state);
237         if (!req) {
238                 return NULL;
239         }
240         ZERO_STRUCTP(state);
241
242         subreq = dcerpc_read_ncacn_packet_send(state, ev, tstream);
243         if (!subreq) {
244                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
245                 tevent_req_post(req, ev);
246                 return req;
247         }
248         tevent_req_set_callback(subreq, dcerpc_ncacn_read_packet_done, req);
249
250         return req;
251 }
252
253 static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq)
254 {
255         struct tevent_req *req =
256                 tevent_req_callback_data(subreq, struct tevent_req);
257         struct dcerpc_ncacn_read_packet_state *state =
258                 tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
259         NTSTATUS status;
260
261         status = dcerpc_read_ncacn_packet_recv(subreq, state,
262                                                 &state->pkt,
263                                                 &state->buffer);
264         TALLOC_FREE(subreq);
265         if (!NT_STATUS_IS_OK(status)) {
266                 DEBUG(3, ("Failed to receive dceprc packet!\n"));
267                 tevent_req_nterror(req, status);
268                 return;
269         }
270
271         tevent_req_done(req);
272 }
273
274 static NTSTATUS dcerpc_ncacn_read_packet_recv(struct tevent_req *req,
275                                                 TALLOC_CTX *mem_ctx,
276                                                 DATA_BLOB *buffer)
277 {
278         struct dcerpc_ncacn_read_packet_state *state =
279                 tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
280         NTSTATUS status;
281
282         if (tevent_req_is_nterror(req, &status)) {
283                 tevent_req_received(req);
284                 return status;
285         }
286
287         buffer->data = talloc_move(mem_ctx, &state->buffer.data);
288         buffer->length = state->buffer.length;
289
290         tevent_req_received(req);
291         return NT_STATUS_OK;
292 }
293
294
295
296 /* Start listening on the appropriate unix socket and setup all is needed to
297  * dispatch requests to the pipes rpc implementation */
298
299 struct dcerpc_ncacn_listen_state {
300         struct ndr_syntax_id syntax_id;
301
302         int fd;
303         union {
304                 char *name;
305                 uint16_t port;
306         } ep;
307
308         struct tevent_context *ev_ctx;
309         struct messaging_context *msg_ctx;
310 };
311
312 static void named_pipe_listener(struct tevent_context *ev,
313                                 struct tevent_fd *fde,
314                                 uint16_t flags,
315                                 void *private_data);
316
317 bool setup_named_pipe_socket(const char *pipe_name,
318                              struct tevent_context *ev_ctx)
319 {
320         struct dcerpc_ncacn_listen_state *state;
321         struct tevent_fd *fde;
322         char *np_dir;
323
324         state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
325         if (!state) {
326                 DEBUG(0, ("Out of memory\n"));
327                 return false;
328         }
329         state->ep.name = talloc_strdup(state, pipe_name);
330         if (state->ep.name == NULL) {
331                 DEBUG(0, ("Out of memory\n"));
332                 goto out;
333         }
334         state->fd = -1;
335
336         np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
337         if (!np_dir) {
338                 DEBUG(0, ("Out of memory\n"));
339                 goto out;
340         }
341
342         if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
343                 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
344                           np_dir, strerror(errno)));
345                 goto out;
346         }
347
348         state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
349         if (state->fd == -1) {
350                 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
351                           np_dir, pipe_name));
352                 goto out;
353         }
354
355         DEBUG(10, ("Openened pipe socket fd %d for %s\n",
356                    state->fd, pipe_name));
357
358         fde = tevent_add_fd(ev_ctx,
359                             state, state->fd, TEVENT_FD_READ,
360                             named_pipe_listener, state);
361         if (!fde) {
362                 DEBUG(0, ("Failed to add event handler!\n"));
363                 goto out;
364         }
365
366         tevent_fd_set_auto_close(fde);
367         return true;
368
369 out:
370         if (state->fd != -1) {
371                 close(state->fd);
372         }
373         TALLOC_FREE(state);
374         return false;
375 }
376
377 static void named_pipe_accept_function(const char *pipe_name, int fd);
378
379 static void named_pipe_listener(struct tevent_context *ev,
380                                 struct tevent_fd *fde,
381                                 uint16_t flags,
382                                 void *private_data)
383 {
384         struct dcerpc_ncacn_listen_state *state =
385                         talloc_get_type_abort(private_data,
386                                               struct dcerpc_ncacn_listen_state);
387         struct sockaddr_un sunaddr;
388         socklen_t len;
389         int sd = -1;
390
391         /* TODO: should we have a limit to the number of clients ? */
392
393         len = sizeof(sunaddr);
394
395         while (sd == -1) {
396                 sd = accept(state->fd,
397                             (struct sockaddr *)(void *)&sunaddr, &len);
398                 if (errno != EINTR) break;
399         }
400
401         if (sd == -1) {
402                 DEBUG(6, ("Failed to get a valid socket [%s]\n",
403                           strerror(errno)));
404                 return;
405         }
406
407         DEBUG(6, ("Accepted socket %d\n", sd));
408
409         named_pipe_accept_function(state->ep.name, sd);
410 }
411
412
413 /* This is the core of the rpc server.
414  * Accepts connections from clients and process requests using the appropriate
415  * dispatcher table. */
416
417 struct named_pipe_client {
418         const char *pipe_name;
419         struct ndr_syntax_id pipe_id;
420
421         struct tevent_context *ev;
422         struct messaging_context *msg_ctx;
423
424         uint16_t file_type;
425         uint16_t device_state;
426         uint64_t allocation_size;
427
428         struct tstream_context *tstream;
429
430         struct tsocket_address *client;
431         char *client_name;
432         struct tsocket_address *server;
433         char *server_name;
434         struct auth_session_info_transport *session_info;
435
436         struct pipes_struct *p;
437
438         struct tevent_queue *write_queue;
439
440         struct iovec *iov;
441         size_t count;
442 };
443
444 static void named_pipe_accept_done(struct tevent_req *subreq);
445
446 static void named_pipe_accept_function(const char *pipe_name, int fd)
447 {
448         struct ndr_syntax_id syntax;
449         struct named_pipe_client *npc;
450         struct tstream_context *plain;
451         struct tevent_req *subreq;
452         bool ok;
453         int ret;
454
455         ok = is_known_pipename(pipe_name, &syntax);
456         if (!ok) {
457                 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
458                 close(fd);
459                 return;
460         }
461
462         npc = talloc_zero(NULL, struct named_pipe_client);
463         if (!npc) {
464                 DEBUG(0, ("Out of memory!\n"));
465                 close(fd);
466                 return;
467         }
468         npc->pipe_name = pipe_name;
469         npc->pipe_id = syntax;
470         npc->ev = server_event_context();
471         npc->msg_ctx = server_messaging_context();
472
473         /* make sure socket is in NON blocking state */
474         ret = set_blocking(fd, false);
475         if (ret != 0) {
476                 DEBUG(2, ("Failed to make socket non-blocking\n"));
477                 TALLOC_FREE(npc);
478                 close(fd);
479                 return;
480         }
481
482         ret = tstream_bsd_existing_socket(npc, fd, &plain);
483         if (ret != 0) {
484                 DEBUG(2, ("Failed to create tstream socket\n"));
485                 TALLOC_FREE(npc);
486                 close(fd);
487                 return;
488         }
489
490         npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
491         npc->device_state = 0xff | 0x0400 | 0x0100;
492         npc->allocation_size = 4096;
493
494         subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
495                                                   npc->file_type,
496                                                   npc->device_state,
497                                                   npc->allocation_size);
498         if (!subreq) {
499                 DEBUG(2, ("Failed to start async accept procedure\n"));
500                 TALLOC_FREE(npc);
501                 close(fd);
502                 return;
503         }
504         tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
505 }
506
507 static void named_pipe_packet_process(struct tevent_req *subreq);
508 static void named_pipe_packet_done(struct tevent_req *subreq);
509
510 static void named_pipe_accept_done(struct tevent_req *subreq)
511 {
512         struct named_pipe_client *npc =
513                 tevent_req_callback_data(subreq, struct named_pipe_client);
514         const char *cli_addr;
515         int error;
516         int ret;
517
518         ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
519                                                 &npc->tstream,
520                                                 &npc->client,
521                                                 &npc->client_name,
522                                                 &npc->server,
523                                                 &npc->server_name,
524                                                 &npc->session_info);
525         TALLOC_FREE(subreq);
526         if (ret != 0) {
527                 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
528                           strerror(error)));
529                 TALLOC_FREE(npc);
530                 return;
531         }
532
533         if (tsocket_address_is_inet(npc->client, "ip")) {
534                 cli_addr = tsocket_address_inet_addr_string(npc->client,
535                                                             subreq);
536                 if (cli_addr == NULL) {
537                         TALLOC_FREE(npc);
538                         return;
539                 }
540         } else {
541                 cli_addr = "";
542         }
543
544         ret = make_server_pipes_struct(npc,
545                                         npc->pipe_name, npc->pipe_id, NCACN_NP,
546                                         cli_addr, NULL, npc->session_info,
547                                         &npc->p, &error);
548         if (ret != 0) {
549                 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
550                           strerror(error)));
551                 goto fail;
552         }
553         npc->p->msg_ctx = npc->msg_ctx;
554
555         npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
556         if (!npc->write_queue) {
557                 DEBUG(2, ("Failed to set up write queue!\n"));
558                 goto fail;
559         }
560
561         /* And now start receaving and processing packets */
562         subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
563         if (!subreq) {
564                 DEBUG(2, ("Failed to start receving packets\n"));
565                 goto fail;
566         }
567         tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
568         return;
569
570 fail:
571         DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
572                   npc->client_name));
573         /* terminate client connection */
574         talloc_free(npc);
575         return;
576 }
577
578 static void named_pipe_packet_process(struct tevent_req *subreq)
579 {
580         struct named_pipe_client *npc =
581                 tevent_req_callback_data(subreq, struct named_pipe_client);
582         struct _output_data *out = &npc->p->out_data;
583         DATA_BLOB recv_buffer = data_blob_null;
584         NTSTATUS status;
585         ssize_t data_left;
586         ssize_t data_used;
587         char *data;
588         uint32_t to_send;
589         bool ok;
590
591         status = dcerpc_ncacn_read_packet_recv(subreq, npc, &recv_buffer);
592         TALLOC_FREE(subreq);
593         if (!NT_STATUS_IS_OK(status)) {
594                 goto fail;
595         }
596
597         data_left = recv_buffer.length;
598         data = (char *)recv_buffer.data;
599
600         while (data_left) {
601
602                 data_used = process_incoming_data(npc->p, data, data_left);
603                 if (data_used < 0) {
604                         DEBUG(3, ("Failed to process dceprc request!\n"));
605                         status = NT_STATUS_UNEXPECTED_IO_ERROR;
606                         goto fail;
607                 }
608
609                 data_left -= data_used;
610                 data += data_used;
611         }
612
613         /* Do not leak this buffer, npc is a long lived context */
614         talloc_free(recv_buffer.data);
615
616         /* this is needed because of the way DCERPC Binds work in
617          * the RPC marshalling code */
618         to_send = out->frag.length - out->current_pdu_sent;
619         if (to_send > 0) {
620
621                 DEBUG(10, ("Current_pdu_len = %u, "
622                            "current_pdu_sent = %u "
623                            "Returning %u bytes\n",
624                            (unsigned int)out->frag.length,
625                            (unsigned int)out->current_pdu_sent,
626                            (unsigned int)to_send));
627
628                 npc->iov = talloc_zero(npc, struct iovec);
629                 if (!npc->iov) {
630                         status = NT_STATUS_NO_MEMORY;
631                         goto fail;
632                 }
633                 npc->count = 1;
634
635                 npc->iov[0].iov_base = out->frag.data
636                                         + out->current_pdu_sent;
637                 npc->iov[0].iov_len = to_send;
638
639                 out->current_pdu_sent += to_send;
640         }
641
642         /* this condition is false for bind packets, or when we haven't
643          * yet got a full request, and need to wait for more data from
644          * the client */
645         while (out->data_sent_length < out->rdata.length) {
646
647                 ok = create_next_pdu(npc->p);
648                 if (!ok) {
649                         DEBUG(3, ("Failed to create next PDU!\n"));
650                         status = NT_STATUS_UNEXPECTED_IO_ERROR;
651                         goto fail;
652                 }
653
654                 npc->iov = talloc_realloc(npc, npc->iov,
655                                             struct iovec, npc->count + 1);
656                 if (!npc->iov) {
657                         status = NT_STATUS_NO_MEMORY;
658                         goto fail;
659                 }
660
661                 npc->iov[npc->count].iov_base = out->frag.data;
662                 npc->iov[npc->count].iov_len = out->frag.length;
663
664                 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
665                            (unsigned int)npc->count,
666                            (unsigned int)npc->iov[npc->count].iov_len));
667                 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
668                                 npc->iov[npc->count].iov_len);
669                 npc->count++;
670         }
671
672         /* we still don't have a complete request, go back and wait for more
673          * data */
674         if (npc->count == 0) {
675                 /* Wait for the next packet */
676                 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
677                 if (!subreq) {
678                         DEBUG(2, ("Failed to start receving packets\n"));
679                         status = NT_STATUS_NO_MEMORY;
680                         goto fail;
681                 }
682                 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
683                 return;
684         }
685
686         DEBUG(10, ("Sending a total of %u bytes\n",
687                    (unsigned int)npc->p->out_data.data_sent_length));
688
689         subreq = tstream_writev_queue_send(npc, npc->ev,
690                                            npc->tstream,
691                                            npc->write_queue,
692                                            npc->iov, npc->count);
693         if (!subreq) {
694                 DEBUG(2, ("Failed to send packet\n"));
695                 status = NT_STATUS_NO_MEMORY;
696                 goto fail;
697         }
698         tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
699         return;
700
701 fail:
702         DEBUG(2, ("Fatal error(%s). "
703                   "Terminating client(%s) connection!\n",
704                   nt_errstr(status), npc->client_name));
705         /* terminate client connection */
706         talloc_free(npc);
707         return;
708 }
709
710 static void named_pipe_packet_done(struct tevent_req *subreq)
711 {
712         struct named_pipe_client *npc =
713                 tevent_req_callback_data(subreq, struct named_pipe_client);
714         int sys_errno;
715         int ret;
716
717         ret = tstream_writev_queue_recv(subreq, &sys_errno);
718         TALLOC_FREE(subreq);
719         if (ret == -1) {
720                 DEBUG(2, ("Writev failed!\n"));
721                 goto fail;
722         }
723
724         /* clear out any data that may have been left around */
725         npc->count = 0;
726         TALLOC_FREE(npc->iov);
727         data_blob_free(&npc->p->in_data.data);
728         data_blob_free(&npc->p->out_data.frag);
729         data_blob_free(&npc->p->out_data.rdata);
730
731         /* Wait for the next packet */
732         subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
733         if (!subreq) {
734                 DEBUG(2, ("Failed to start receving packets\n"));
735                 sys_errno = ENOMEM;
736                 goto fail;
737         }
738         tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
739         return;
740
741 fail:
742         DEBUG(2, ("Fatal error(%s). "
743                   "Terminating client(%s) connection!\n",
744                   strerror(sys_errno), npc->client_name));
745         /* terminate client connection */
746         talloc_free(npc);
747         return;
748 }
749
750 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
751                                 struct messaging_context *msg_ctx,
752                                 struct ndr_syntax_id syntax_id,
753                                 enum dcerpc_transport_t transport,
754                                 const char *name,
755                                 uint16_t port,
756                                 struct tsocket_address *cli_addr,
757                                 struct tsocket_address *srv_addr,
758                                 int s);
759
760 /********************************************************************
761  * Start listening on the tcp/ip socket
762  ********************************************************************/
763
764 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
765                                         struct tevent_fd *fde,
766                                         uint16_t flags,
767                                         void *private_data);
768
769 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
770                                          struct messaging_context *msg_ctx,
771                                          struct ndr_syntax_id syntax_id,
772                                          const struct sockaddr_storage *ifss,
773                                          uint16_t port)
774 {
775         struct dcerpc_ncacn_listen_state *state;
776         struct tevent_fd *fde;
777         int rc;
778
779         state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
780         if (state == NULL) {
781                 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
782                 return 0;
783         }
784
785         state->syntax_id = syntax_id;
786         state->fd = -1;
787         state->ep.port = port;
788
789         if (state->ep.port == 0) {
790                 uint16_t i;
791
792                 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
793                         state->fd = open_socket_in(SOCK_STREAM,
794                                                    i,
795                                                    0,
796                                                    ifss,
797                                                    false);
798                         if (state->fd > 0) {
799                                 state->ep.port = i;
800                                 break;
801                         }
802                 }
803         } else {
804                 state->fd = open_socket_in(SOCK_STREAM,
805                                            state->ep.port,
806                                            0,
807                                            ifss,
808                                            true);
809         }
810         if (state->fd == -1) {
811                 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
812                           "socket on port %u!\n", state->ep.port));
813                 goto out;
814         }
815
816         state->ev_ctx = ev_ctx;
817         state->msg_ctx = msg_ctx;
818
819         /* ready to listen */
820         set_socket_options(state->fd, "SO_KEEPALIVE");
821         set_socket_options(state->fd, lp_socket_options());
822
823         /* Set server socket to non-blocking for the accept. */
824         set_blocking(state->fd, false);
825
826         rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
827         if (rc == -1) {
828                 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
829                 goto out;
830         }
831
832         DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
833                    state->fd, state->ep.port));
834
835         fde = tevent_add_fd(state->ev_ctx,
836                             state,
837                             state->fd,
838                             TEVENT_FD_READ,
839                             dcerpc_ncacn_tcpip_listener,
840                             state);
841         if (fde == NULL) {
842                 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
843                 goto out;
844         }
845
846         tevent_fd_set_auto_close(fde);
847
848         return state->ep.port;
849 out:
850         if (state->fd != -1) {
851                 close(state->fd);
852         }
853         TALLOC_FREE(state);
854
855         return 0;
856 }
857
858 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
859                                         struct tevent_fd *fde,
860                                         uint16_t flags,
861                                         void *private_data)
862 {
863         struct dcerpc_ncacn_listen_state *state =
864                         talloc_get_type_abort(private_data,
865                                               struct dcerpc_ncacn_listen_state);
866         struct tsocket_address *cli_addr = NULL;
867         struct tsocket_address *srv_addr = NULL;
868         struct sockaddr_storage addr;
869         socklen_t in_addrlen = sizeof(addr);
870         int s = -1;
871         int rc;
872
873         while (s == -1) {
874                 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
875                 if (s == -1 && errno != EINTR) {
876                         break;
877                 }
878         }
879
880         if (s == -1) {
881                 DEBUG(0,("tcpip_listener accept: %s\n",
882                          strerror(errno)));
883                 return;
884         }
885
886         rc = tsocket_address_bsd_from_sockaddr(state,
887                                                (struct sockaddr *)(void *) &addr,
888                                                in_addrlen,
889                                                &cli_addr);
890         if (rc < 0) {
891                 close(s);
892                 return;
893         }
894
895         rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
896         if (rc < 0) {
897                 close(s);
898                 return;
899         }
900
901         rc = tsocket_address_bsd_from_sockaddr(state,
902                                                (struct sockaddr *)(void *) &addr,
903                                                in_addrlen,
904                                                &srv_addr);
905         if (rc < 0) {
906                 close(s);
907                 return;
908         }
909
910         DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
911
912         dcerpc_ncacn_accept(state->ev_ctx,
913                             state->msg_ctx,
914                             state->syntax_id,
915                             NCACN_IP_TCP,
916                             NULL,
917                             state->ep.port,
918                             cli_addr,
919                             srv_addr,
920                             s);
921 }
922
923 /********************************************************************
924  * Start listening on the ncalrpc socket
925  ********************************************************************/
926
927 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
928                                     struct tevent_fd *fde,
929                                     uint16_t flags,
930                                     void *private_data);
931
932 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
933                                  struct messaging_context *msg_ctx,
934                                  struct ndr_syntax_id syntax_id,
935                                  const char *name)
936 {
937         struct dcerpc_ncacn_listen_state *state;
938         struct tevent_fd *fde;
939
940         state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
941         if (state == NULL) {
942                 DEBUG(0, ("Out of memory\n"));
943                 return false;
944         }
945
946         state->syntax_id = syntax_id;
947         state->fd = -1;
948
949         if (name == NULL) {
950                 name = "DEFAULT";
951         }
952         state->ep.name = talloc_strdup(state, name);
953
954         if (state->ep.name == NULL) {
955                 DEBUG(0, ("Out of memory\n"));
956                 talloc_free(state);
957                 return false;
958         }
959
960         if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0700)) {
961                 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
962                           lp_ncalrpc_dir(), strerror(errno)));
963                 goto out;
964         }
965
966         state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0700);
967         if (state->fd == -1) {
968                 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
969                           lp_ncalrpc_dir(), name));
970                 goto out;
971         }
972
973         DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
974
975         state->ev_ctx = ev_ctx;
976         state->msg_ctx = msg_ctx;
977
978         /* Set server socket to non-blocking for the accept. */
979         set_blocking(state->fd, false);
980
981         fde = tevent_add_fd(state->ev_ctx,
982                             state,
983                             state->fd,
984                             TEVENT_FD_READ,
985                             dcerpc_ncalrpc_listener,
986                             state);
987         if (fde == NULL) {
988                 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
989                 goto out;
990         }
991
992         tevent_fd_set_auto_close(fde);
993
994         return true;
995 out:
996         if (state->fd != -1) {
997                 close(state->fd);
998         }
999         TALLOC_FREE(state);
1000
1001         return 0;
1002 }
1003
1004 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
1005                                         struct tevent_fd *fde,
1006                                         uint16_t flags,
1007                                         void *private_data)
1008 {
1009         struct dcerpc_ncacn_listen_state *state =
1010                         talloc_get_type_abort(private_data,
1011                                               struct dcerpc_ncacn_listen_state);
1012         struct tsocket_address *cli_addr = NULL;
1013         struct sockaddr_un sunaddr;
1014         struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
1015         socklen_t len;
1016         int sd = -1;
1017         int rc;
1018
1019         while (sd == -1) {
1020                 sd = accept(state->fd, addr, &len);
1021                 if (sd == -1 && errno != EINTR) {
1022                         break;
1023                 }
1024         }
1025
1026         if (sd == -1) {
1027                 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
1028                 return;
1029         }
1030
1031         rc = tsocket_address_bsd_from_sockaddr(state,
1032                                                addr, len,
1033                                                &cli_addr);
1034         if (rc < 0) {
1035                 close(sd);
1036                 return;
1037         }
1038
1039         DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
1040
1041         dcerpc_ncacn_accept(state->ev_ctx,
1042                             state->msg_ctx,
1043                             state->syntax_id, NCALRPC,
1044                             state->ep.name, 0,
1045                             cli_addr, NULL, sd);
1046 }
1047
1048 struct dcerpc_ncacn_conn {
1049         struct ndr_syntax_id syntax_id;
1050
1051         enum dcerpc_transport_t transport;
1052
1053         union {
1054                 const char *name;
1055                 uint16_t port;
1056         } ep;
1057
1058         int sock;
1059
1060         struct pipes_struct *p;
1061
1062         struct tevent_context *ev_ctx;
1063         struct messaging_context *msg_ctx;
1064
1065         struct tstream_context *tstream;
1066         struct tevent_queue *send_queue;
1067
1068         struct tsocket_address *client;
1069         char *client_name;
1070         struct tsocket_address *server;
1071         char *server_name;
1072         struct auth_session_info_transport *session_info;
1073
1074         struct iovec *iov;
1075         size_t count;
1076 };
1077
1078 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
1079 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
1080
1081 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
1082                                 struct messaging_context *msg_ctx,
1083                                 struct ndr_syntax_id syntax_id,
1084                                 enum dcerpc_transport_t transport,
1085                                 const char *name,
1086                                 uint16_t port,
1087                                 struct tsocket_address *cli_addr,
1088                                 struct tsocket_address *srv_addr,
1089                                 int s) {
1090         struct dcerpc_ncacn_conn *ncacn_conn;
1091         struct tevent_req *subreq;
1092         const char *cli_str;
1093         const char *srv_str = NULL;
1094         char *pipe_name;
1095         NTSTATUS status;
1096         int sys_errno;
1097         int rc;
1098
1099         DEBUG(10, ("dcerpc_ncacn_accept\n"));
1100
1101         ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
1102         if (ncacn_conn == NULL) {
1103                 DEBUG(0, ("Out of memory!\n"));
1104                 close(s);
1105                 return;
1106         }
1107
1108         switch (transport) {
1109                 case NCACN_IP_TCP:
1110                         ncacn_conn->ep.port = port;
1111                         break;
1112                 case NCALRPC:
1113                 case NCACN_NP:
1114                         ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1115                         break;
1116                 default:
1117                         DEBUG(0, ("unknown dcerpc transport: %u!\n",
1118                                   transport));
1119                         talloc_free(ncacn_conn);
1120                         close(s);
1121                         return;
1122         }
1123         ncacn_conn->transport = transport;
1124         ncacn_conn->syntax_id = syntax_id;
1125         ncacn_conn->ev_ctx = ev_ctx;
1126         ncacn_conn->msg_ctx = msg_ctx;
1127         ncacn_conn->sock = s;
1128
1129         ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1130         if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1131                 ncacn_conn->client_name =
1132                         tsocket_address_inet_addr_string(ncacn_conn->client,
1133                                                          ncacn_conn);
1134         } else {
1135                 ncacn_conn->client_name =
1136                         tsocket_address_unix_path(ncacn_conn->client,
1137                                                   ncacn_conn);
1138         }
1139         if (ncacn_conn->client_name == NULL) {
1140                 DEBUG(0, ("Out of memory!\n"));
1141                 talloc_free(ncacn_conn);
1142                 close(s);
1143                 return;
1144         }
1145
1146         if (srv_addr != NULL) {
1147                 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1148
1149                 ncacn_conn->server_name =
1150                         tsocket_address_inet_addr_string(ncacn_conn->server,
1151                                                          ncacn_conn);
1152                 if (ncacn_conn->server_name == NULL) {
1153                         DEBUG(0, ("Out of memory!\n"));
1154                         talloc_free(ncacn_conn);
1155                         close(s);
1156                         return;
1157                 }
1158         }
1159
1160         rc = set_blocking(s, false);
1161         if (rc < 0) {
1162                 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1163                 talloc_free(ncacn_conn);
1164                 close(s);
1165                 return;
1166         }
1167
1168         /*
1169          * As soon as we have tstream_bsd_existing_socket set up it will
1170          * take care of closing the socket.
1171          */
1172         rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1173         if (rc < 0) {
1174                 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1175                 talloc_free(ncacn_conn);
1176                 close(s);
1177                 return;
1178         }
1179
1180         switch (ncacn_conn->transport) {
1181                 case NCACN_IP_TCP:
1182                         pipe_name = tsocket_address_string(ncacn_conn->client,
1183                                                            ncacn_conn);
1184                         break;
1185                 case NCALRPC:
1186                         pipe_name = talloc_strdup(ncacn_conn,
1187                                                   ncacn_conn->ep.name);
1188                         break;
1189                 default:
1190                         talloc_free(ncacn_conn);
1191                         return;
1192         }
1193
1194         if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1195                 cli_str = ncacn_conn->client_name;
1196         } else {
1197                 cli_str = "";
1198         }
1199
1200         if (ncacn_conn->server != NULL) {
1201                 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1202                         srv_str = ncacn_conn->server_name;
1203                 } else {
1204                         srv_str = NULL;
1205                 }
1206         }
1207
1208         if (ncacn_conn->session_info == NULL) {
1209                 status = auth_anonymous_session_info(ncacn_conn,
1210                                                      &ncacn_conn->session_info);
1211                 if (!NT_STATUS_IS_OK(status)) {
1212                         DEBUG(2, ("Failed to create "
1213                                   "auth_anonymous_session_info - %s\n",
1214                                   nt_errstr(status)));
1215                         talloc_free(ncacn_conn);
1216                         return;
1217                 }
1218         }
1219
1220         rc = make_server_pipes_struct(ncacn_conn,
1221                                       pipe_name,
1222                                       ncacn_conn->syntax_id,
1223                                       ncacn_conn->transport,
1224                                       cli_str,
1225                                       srv_str,
1226                                       ncacn_conn->session_info,
1227                                       &ncacn_conn->p,
1228                                       &sys_errno);
1229         if (rc < 0) {
1230                 DEBUG(2, ("Failed to create pipe struct - %s",
1231                           strerror(sys_errno)));
1232                 talloc_free(ncacn_conn);
1233                 return;
1234         }
1235
1236         ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1237                                                         "dcerpc send queue");
1238         if (ncacn_conn->send_queue == NULL) {
1239                 DEBUG(0, ("Out of memory!\n"));
1240                 talloc_free(ncacn_conn);
1241                 return;
1242         }
1243
1244         subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1245                                                ncacn_conn->ev_ctx,
1246                                                ncacn_conn->tstream);
1247         if (subreq == NULL) {
1248                 DEBUG(2, ("Failed to send ncacn packet\n"));
1249                 talloc_free(ncacn_conn);
1250                 return;
1251         }
1252
1253         tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1254
1255         DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1256
1257         return;
1258 }
1259
1260 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1261 {
1262         struct dcerpc_ncacn_conn *ncacn_conn =
1263                 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1264
1265         struct _output_data *out = &ncacn_conn->p->out_data;
1266         DATA_BLOB recv_buffer = data_blob_null;
1267         ssize_t data_left;
1268         ssize_t data_used;
1269         uint32_t to_send;
1270         char *data;
1271         NTSTATUS status;
1272         bool ok;
1273
1274         status = dcerpc_ncacn_read_packet_recv(subreq, ncacn_conn, &recv_buffer);
1275         TALLOC_FREE(subreq);
1276         if (!NT_STATUS_IS_OK(status)) {
1277                 goto fail;
1278         }
1279
1280         data_left = recv_buffer.length;
1281         data = (char *) recv_buffer.data;
1282
1283         while (data_left) {
1284                 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1285                 if (data_used < 0) {
1286                         DEBUG(3, ("Failed to process dcerpc request!\n"));
1287                         status = NT_STATUS_UNEXPECTED_IO_ERROR;
1288                         goto fail;
1289                 }
1290
1291                 data_left -= data_used;
1292                 data += data_used;
1293         }
1294
1295         /* Do not leak this buffer */
1296         talloc_free(recv_buffer.data);
1297
1298         /*
1299          * This is needed because of the way DCERPC binds work in the RPC
1300          * marshalling code
1301          */
1302         to_send = out->frag.length - out->current_pdu_sent;
1303         if (to_send > 0) {
1304
1305                 DEBUG(10, ("Current_pdu_len = %u, "
1306                            "current_pdu_sent = %u "
1307                            "Returning %u bytes\n",
1308                            (unsigned int)out->frag.length,
1309                            (unsigned int)out->current_pdu_sent,
1310                            (unsigned int)to_send));
1311
1312                 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1313                 if (ncacn_conn->iov == NULL) {
1314                         status = NT_STATUS_NO_MEMORY;
1315                         goto fail;
1316                 }
1317                 ncacn_conn->count = 1;
1318
1319                 ncacn_conn->iov[0].iov_base = out->frag.data
1320                                             + out->current_pdu_sent;
1321                 ncacn_conn->iov[0].iov_len = to_send;
1322
1323                 out->current_pdu_sent += to_send;
1324         }
1325
1326         /*
1327          * This condition is false for bind packets, or when we haven't yet got
1328          * a full request, and need to wait for more data from the client
1329          */
1330         while (out->data_sent_length < out->rdata.length) {
1331                 ok = create_next_pdu(ncacn_conn->p);
1332                 if (!ok) {
1333                         DEBUG(3, ("Failed to create next PDU!\n"));
1334                         status = NT_STATUS_UNEXPECTED_IO_ERROR;
1335                         goto fail;
1336                 }
1337
1338                 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1339                                                  ncacn_conn->iov,
1340                                                  struct iovec,
1341                                                  ncacn_conn->count + 1);
1342                 if (ncacn_conn->iov == NULL) {
1343                         status = NT_STATUS_NO_MEMORY;
1344                         goto fail;
1345                 }
1346
1347                 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1348                 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1349
1350                 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1351                            (unsigned int) ncacn_conn->count,
1352                            (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1353                 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1354                               ncacn_conn->iov[ncacn_conn->count].iov_len);
1355                 ncacn_conn->count++;
1356         }
1357
1358         /*
1359          * We still don't have a complete request, go back and wait for more
1360          * data.
1361          */
1362         if (ncacn_conn->count == 0) {
1363                 /* Wait for the next packet */
1364                 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1365                                                        ncacn_conn->ev_ctx,
1366                                                        ncacn_conn->tstream);
1367                 if (subreq == NULL) {
1368                         DEBUG(2, ("Failed to start receving packets\n"));
1369                         status = NT_STATUS_NO_MEMORY;
1370                         goto fail;
1371                 }
1372                 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1373                 return;
1374         }
1375
1376         DEBUG(10, ("Sending a total of %u bytes\n",
1377                    (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1378
1379         subreq = tstream_writev_queue_send(ncacn_conn,
1380                                            ncacn_conn->ev_ctx,
1381                                            ncacn_conn->tstream,
1382                                            ncacn_conn->send_queue,
1383                                            ncacn_conn->iov,
1384                                            ncacn_conn->count);
1385         if (subreq == NULL) {
1386                 DEBUG(2, ("Failed to send packet\n"));
1387                 status = NT_STATUS_NO_MEMORY;
1388                 goto fail;
1389         }
1390
1391         tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1392         return;
1393
1394 fail:
1395         DEBUG(2, ("Fatal error(%s). "
1396                   "Terminating client(%s) connection!\n",
1397                   nt_errstr(status), ncacn_conn->client_name));
1398
1399         /* Terminate client connection */
1400         talloc_free(ncacn_conn);
1401         return;
1402 }
1403
1404 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1405 {
1406         struct dcerpc_ncacn_conn *ncacn_conn =
1407                 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1408         int sys_errno;
1409         int rc;
1410
1411         rc = tstream_writev_queue_recv(subreq, &sys_errno);
1412         TALLOC_FREE(subreq);
1413         if (rc < 0) {
1414                 DEBUG(2, ("Writev failed!\n"));
1415                 goto fail;
1416         }
1417
1418         /* clear out any data that may have been left around */
1419         ncacn_conn->count = 0;
1420         TALLOC_FREE(ncacn_conn->iov);
1421         data_blob_free(&ncacn_conn->p->in_data.data);
1422         data_blob_free(&ncacn_conn->p->out_data.frag);
1423         data_blob_free(&ncacn_conn->p->out_data.rdata);
1424
1425         /* Wait for the next packet */
1426         subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1427                                                ncacn_conn->ev_ctx,
1428                                                ncacn_conn->tstream);
1429         if (subreq == NULL) {
1430                 DEBUG(2, ("Failed to start receving packets\n"));
1431                 sys_errno = ENOMEM;
1432                 goto fail;
1433         }
1434
1435         tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1436         return;
1437
1438 fail:
1439         DEBUG(2, ("Fatal error(%s). Terminating client(%s) connection!\n",
1440                   strerror(sys_errno), ncacn_conn->client_name));
1441
1442         /* Terminate client connection */
1443         talloc_free(ncacn_conn);
1444         return;
1445 }
1446
1447 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */