LCOV - code coverage report
Current view: top level - libcli/named_pipe_auth - npa_tstream.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 397 657 60.4 %
Date: 2021-09-23 10:06:22 Functions: 20 25 80.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Copyright (C) Stefan Metzmacher 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 "system/network.h"
      22             : #include "../util/tevent_unix.h"
      23             : #include "../lib/tsocket/tsocket.h"
      24             : #include "../lib/tsocket/tsocket_internal.h"
      25             : #include "../librpc/gen_ndr/ndr_named_pipe_auth.h"
      26             : #include "../libcli/named_pipe_auth/npa_tstream.h"
      27             : #include "../libcli/named_pipe_auth/tstream_u32_read.h"
      28             : #include "../libcli/smb/smb_constants.h"
      29             : 
      30             : static const struct tstream_context_ops tstream_npa_ops;
      31             : 
      32             : struct tstream_npa {
      33             :         struct tstream_context *unix_stream;
      34             : 
      35             :         uint16_t file_type;
      36             : 
      37             :         struct iovec pending;
      38             : };
      39             : 
      40             : struct tstream_npa_connect_state {
      41             :         struct {
      42             :                 struct tevent_context *ev;
      43             :         } caller;
      44             : 
      45             :         const char *unix_path;
      46             :         struct tsocket_address *unix_laddr;
      47             :         struct tsocket_address *unix_raddr;
      48             :         struct tstream_context *unix_stream;
      49             : 
      50             :         struct named_pipe_auth_req auth_req;
      51             :         DATA_BLOB auth_req_blob;
      52             :         struct iovec auth_req_iov;
      53             : 
      54             :         struct named_pipe_auth_rep auth_rep;
      55             : };
      56             : 
      57             : static void tstream_npa_connect_unix_done(struct tevent_req *subreq);
      58             : 
      59        9401 : struct tevent_req *tstream_npa_connect_send(TALLOC_CTX *mem_ctx,
      60             :                                             struct tevent_context *ev,
      61             :                                             const char *directory,
      62             :                                             const char *npipe,
      63             :                                             const struct tsocket_address *remote_client_addr,
      64             :                                             const char *remote_client_name_in,
      65             :                                             const struct tsocket_address *local_server_addr,
      66             :                                             const char *local_server_name_in,
      67             :                                             const struct auth_session_info_transport *session_info)
      68             : {
      69             :         struct tevent_req *req;
      70             :         struct tstream_npa_connect_state *state;
      71             :         struct tevent_req *subreq;
      72             :         int ret;
      73             :         enum ndr_err_code ndr_err;
      74             :         char *lower_case_npipe;
      75             :         struct named_pipe_auth_req_info4 *info4;
      76             : 
      77        9401 :         req = tevent_req_create(mem_ctx, &state,
      78             :                                 struct tstream_npa_connect_state);
      79        9401 :         if (!req) {
      80           0 :                 return NULL;
      81             :         }
      82             : 
      83        9401 :         state->caller.ev = ev;
      84             : 
      85        9401 :         lower_case_npipe = strlower_talloc(state, npipe);
      86        9401 :         if (tevent_req_nomem(lower_case_npipe, req)) {
      87           0 :                 goto post;
      88             :         }
      89             : 
      90        9401 :         state->unix_path = talloc_asprintf(state, "%s/%s",
      91             :                                            directory,
      92             :                                            lower_case_npipe);
      93        9401 :         talloc_free(lower_case_npipe);
      94        9401 :         if (tevent_req_nomem(state->unix_path, req)) {
      95           0 :                 goto post;
      96             :         }
      97             : 
      98        9401 :         ret = tsocket_address_unix_from_path(state,
      99             :                                              "",
     100             :                                              &state->unix_laddr);
     101        9401 :         if (ret == -1) {
     102           0 :                 tevent_req_error(req, errno);
     103           0 :                 goto post;
     104             :         }
     105             : 
     106        9401 :         ret = tsocket_address_unix_from_path(state,
     107             :                                              state->unix_path,
     108             :                                              &state->unix_raddr);
     109        9401 :         if (ret == -1) {
     110           0 :                 tevent_req_error(req, errno);
     111           0 :                 goto post;
     112             :         }
     113             : 
     114        9401 :         ZERO_STRUCT(state->auth_req);
     115             : 
     116        9401 :         if (!local_server_addr) {
     117           0 :                 tevent_req_error(req, EINVAL);
     118           0 :                 goto post;
     119             :         }
     120             : 
     121        9401 :         state->auth_req.level = 4;
     122        9401 :         info4 = &state->auth_req.info.info4;
     123             : 
     124        9401 :         info4->remote_client_name = remote_client_name_in;
     125        9401 :         info4->remote_client_addr = tsocket_address_inet_addr_string(remote_client_addr,
     126             :                                                                      state);
     127        9401 :         if (!info4->remote_client_addr) {
     128             :                 /* errno might be EINVAL */
     129           0 :                 tevent_req_error(req, errno);
     130           0 :                 goto post;
     131             :         }
     132        9401 :         info4->remote_client_port = tsocket_address_inet_port(remote_client_addr);
     133        9401 :         if (!info4->remote_client_name) {
     134        9401 :                 info4->remote_client_name = info4->remote_client_addr;
     135             :         }
     136             : 
     137        9401 :         info4->local_server_name = local_server_name_in;
     138        9401 :         info4->local_server_addr = tsocket_address_inet_addr_string(local_server_addr,
     139             :                                                                     state);
     140        9401 :         if (!info4->local_server_addr) {
     141             :                 /* errno might be EINVAL */
     142           0 :                 tevent_req_error(req, errno);
     143           0 :                 goto post;
     144             :         }
     145        9401 :         info4->local_server_port = tsocket_address_inet_port(local_server_addr);
     146        9401 :         if (!info4->local_server_name) {
     147        9401 :                 info4->local_server_name = info4->local_server_addr;
     148             :         }
     149             : 
     150        9401 :         info4->session_info = discard_const_p(struct auth_session_info_transport, session_info);
     151             : 
     152        9401 :         if (DEBUGLVL(10)) {
     153           0 :                 NDR_PRINT_DEBUG(named_pipe_auth_req, &state->auth_req);
     154             :         }
     155             : 
     156        9401 :         ndr_err = ndr_push_struct_blob(&state->auth_req_blob,
     157        9401 :                         state, &state->auth_req,
     158             :                         (ndr_push_flags_fn_t)ndr_push_named_pipe_auth_req);
     159        9401 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     160           0 :                 tevent_req_error(req, EINVAL);
     161           0 :                 goto post;
     162             :         }
     163             : 
     164        9401 :         state->auth_req_iov.iov_base = (char *) state->auth_req_blob.data;
     165        9401 :         state->auth_req_iov.iov_len = state->auth_req_blob.length;
     166             : 
     167        9401 :         subreq = tstream_unix_connect_send(state,
     168        8775 :                                            state->caller.ev,
     169        9401 :                                            state->unix_laddr,
     170        9401 :                                            state->unix_raddr);
     171        9401 :         if (tevent_req_nomem(subreq, req)) {
     172           0 :                 goto post;
     173             :         }
     174        9401 :         tevent_req_set_callback(subreq, tstream_npa_connect_unix_done, req);
     175             : 
     176        9401 :         return req;
     177             : 
     178           0 : post:
     179           0 :         tevent_req_post(req, ev);
     180           0 :         return req;
     181             : }
     182             : 
     183             : static void tstream_npa_connect_writev_done(struct tevent_req *subreq);
     184             : 
     185        9401 : static void tstream_npa_connect_unix_done(struct tevent_req *subreq)
     186             : {
     187        7269 :         struct tevent_req *req =
     188        9401 :                 tevent_req_callback_data(subreq,
     189             :                 struct tevent_req);
     190        7269 :         struct tstream_npa_connect_state *state =
     191        9401 :                 tevent_req_data(req,
     192             :                 struct tstream_npa_connect_state);
     193             :         int ret;
     194             :         int sys_errno;
     195             : 
     196        9401 :         ret = tstream_unix_connect_recv(subreq, &sys_errno,
     197             :                                         state, &state->unix_stream);
     198        9401 :         TALLOC_FREE(subreq);
     199        9401 :         if (ret == -1) {
     200         179 :                 tevent_req_error(req, sys_errno);
     201         179 :                 return;
     202             :         }
     203             : 
     204        9222 :         subreq = tstream_writev_send(state,
     205             :                                      state->caller.ev,
     206             :                                      state->unix_stream,
     207        9222 :                                      &state->auth_req_iov, 1);
     208        9222 :         if (tevent_req_nomem(subreq, req)) {
     209           0 :                 return;
     210             :         }
     211        9222 :         tevent_req_set_callback(subreq, tstream_npa_connect_writev_done, req);
     212             : }
     213             : 
     214             : static void tstream_npa_connect_readv_done(struct tevent_req *subreq);
     215             : 
     216        9222 : static void tstream_npa_connect_writev_done(struct tevent_req *subreq)
     217             : {
     218        7103 :         struct tevent_req *req =
     219        9222 :                 tevent_req_callback_data(subreq,
     220             :                 struct tevent_req);
     221        7103 :         struct tstream_npa_connect_state *state =
     222        9222 :                 tevent_req_data(req,
     223             :                 struct tstream_npa_connect_state);
     224             :         int ret;
     225             :         int sys_errno;
     226             : 
     227        9222 :         ret = tstream_writev_recv(subreq, &sys_errno);
     228        9222 :         TALLOC_FREE(subreq);
     229        9222 :         if (ret == -1) {
     230           0 :                 tevent_req_error(req, sys_errno);
     231           0 :                 return;
     232             :         }
     233             : 
     234        9222 :         subreq = tstream_u32_read_send(
     235             :                 state, state->caller.ev, 0x00FFFFFF, state->unix_stream);
     236        9222 :         if (tevent_req_nomem(subreq, req)) {
     237           0 :                 return;
     238             :         }
     239        9222 :         tevent_req_set_callback(subreq, tstream_npa_connect_readv_done, req);
     240             : }
     241             : 
     242        9222 : static void tstream_npa_connect_readv_done(struct tevent_req *subreq)
     243             : {
     244        7103 :         struct tevent_req *req =
     245        9222 :                 tevent_req_callback_data(subreq,
     246             :                 struct tevent_req);
     247        7103 :         struct tstream_npa_connect_state *state =
     248        9222 :                 tevent_req_data(req,
     249             :                 struct tstream_npa_connect_state);
     250             :         DATA_BLOB in;
     251             :         int err;
     252             :         enum ndr_err_code ndr_err;
     253             : 
     254        9222 :         err = tstream_u32_read_recv(subreq, state, &in.data, &in.length);
     255        9222 :         TALLOC_FREE(subreq);
     256        9222 :         if (tevent_req_error(req, err)) {
     257           0 :                 return;
     258             :         }
     259             : 
     260        9222 :         DBG_DEBUG("name_pipe_auth_rep(client)[%zu]\n", in.length);
     261        9222 :         dump_data(11, in.data, in.length);
     262             : 
     263        9222 :         ndr_err = ndr_pull_struct_blob_all(
     264             :                 &in,
     265             :                 state,
     266        9222 :                 &state->auth_rep,
     267             :                 (ndr_pull_flags_fn_t)ndr_pull_named_pipe_auth_rep);
     268             : 
     269        9222 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     270           0 :                 DEBUG(0, ("ndr_pull_named_pipe_auth_rep failed: %s\n",
     271             :                           ndr_map_error2string(ndr_err)));
     272           0 :                 tevent_req_error(req, EIO);
     273           0 :                 return;
     274             :         }
     275             : 
     276        9222 :         if (DEBUGLVL(10)) {
     277           0 :                 NDR_PRINT_DEBUG(named_pipe_auth_rep, &state->auth_rep);
     278             :         }
     279             : 
     280        9222 :         if (state->auth_rep.length < 16) {
     281           0 :                 DEBUG(0, ("req invalid length: %u < 16\n",
     282             :                           state->auth_rep.length));
     283           0 :                 tevent_req_error(req, EIO);
     284           0 :                 return;
     285             :         }
     286             : 
     287        9222 :         if (strcmp(NAMED_PIPE_AUTH_MAGIC, state->auth_rep.magic) != 0) {
     288           0 :                 DEBUG(0, ("req invalid magic: %s != %s\n",
     289             :                           state->auth_rep.magic, NAMED_PIPE_AUTH_MAGIC));
     290           0 :                 tevent_req_error(req, EIO);
     291           0 :                 return;
     292             :         }
     293             : 
     294        9222 :         if (!NT_STATUS_IS_OK(state->auth_rep.status)) {
     295           0 :                 DEBUG(0, ("req failed: %s\n",
     296             :                           nt_errstr(state->auth_rep.status)));
     297           0 :                 tevent_req_error(req, EACCES);
     298           0 :                 return;
     299             :         }
     300             : 
     301        9222 :         if (state->auth_rep.level != state->auth_req.level) {
     302           0 :                 DEBUG(0, ("req invalid level: %u != %u\n",
     303             :                           state->auth_rep.level, state->auth_req.level));
     304           0 :                 tevent_req_error(req, EIO);
     305           0 :                 return;
     306             :         }
     307             : 
     308        9222 :         tevent_req_done(req);
     309             : }
     310             : 
     311        9401 : int _tstream_npa_connect_recv(struct tevent_req *req,
     312             :                               int *perrno,
     313             :                               TALLOC_CTX *mem_ctx,
     314             :                               struct tstream_context **_stream,
     315             :                               uint16_t *_file_type,
     316             :                               uint16_t *_device_state,
     317             :                               uint64_t *_allocation_size,
     318             :                               const char *location)
     319             : {
     320        7269 :         struct tstream_npa_connect_state *state =
     321        9401 :                 tevent_req_data(req,
     322             :                 struct tstream_npa_connect_state);
     323             :         struct tstream_context *stream;
     324             :         struct tstream_npa *npas;
     325        9401 :         uint16_t device_state = 0;
     326        9401 :         uint64_t allocation_size = 0;
     327             : 
     328        9401 :         if (tevent_req_is_unix_error(req, perrno)) {
     329         179 :                 tevent_req_received(req);
     330         179 :                 return -1;
     331             :         }
     332             : 
     333        9222 :         stream = tstream_context_create(mem_ctx,
     334             :                                         &tstream_npa_ops,
     335             :                                         &npas,
     336             :                                         struct tstream_npa,
     337             :                                         location);
     338        9222 :         if (!stream) {
     339           0 :                 *perrno = ENOMEM;
     340           0 :                 tevent_req_received(req);
     341           0 :                 return -1;
     342             :         }
     343        9222 :         ZERO_STRUCTP(npas);
     344             : 
     345        9222 :         npas->unix_stream = talloc_move(stream, &state->unix_stream);
     346        9222 :         switch (state->auth_rep.level) {
     347        9222 :         case 4:
     348        9222 :                 npas->file_type = state->auth_rep.info.info4.file_type;
     349        9222 :                 device_state = state->auth_rep.info.info4.device_state;
     350        9222 :                 allocation_size = state->auth_rep.info.info4.allocation_size;
     351        9222 :                 break;
     352             :         }
     353             : 
     354        9222 :         *_stream = stream;
     355        9222 :         *_file_type = npas->file_type;
     356        9222 :         *_device_state = device_state;
     357        9222 :         *_allocation_size = allocation_size;
     358        9222 :         tevent_req_received(req);
     359        9222 :         return 0;
     360             : }
     361             : 
     362     1043167 : static ssize_t tstream_npa_pending_bytes(struct tstream_context *stream)
     363             : {
     364     1043167 :         struct tstream_npa *npas = tstream_context_data(stream,
     365             :                                    struct tstream_npa);
     366             :         ssize_t ret;
     367             : 
     368     1043167 :         if (!npas->unix_stream) {
     369           0 :                 errno = ENOTCONN;
     370           0 :                 return -1;
     371             :         }
     372             : 
     373     1043167 :         switch (npas->file_type) {
     374           0 :         case FILE_TYPE_BYTE_MODE_PIPE:
     375           0 :                 ret = tstream_pending_bytes(npas->unix_stream);
     376           0 :                 break;
     377             : 
     378     1043167 :         case FILE_TYPE_MESSAGE_MODE_PIPE:
     379     1043167 :                 ret = npas->pending.iov_len;
     380     1043167 :                 break;
     381             : 
     382           0 :         default:
     383           0 :                 ret = -1;
     384             :         }
     385             : 
     386     1023192 :         return ret;
     387             : }
     388             : 
     389             : struct tstream_npa_readv_state {
     390             :         struct tstream_context *stream;
     391             : 
     392             :         struct iovec *vector;
     393             :         size_t count;
     394             : 
     395             :         /* the header for message mode */
     396             :         uint8_t hdr[2];
     397             :         bool wait_for_hdr;
     398             : 
     399             :         int ret;
     400             : };
     401             : 
     402             : static void tstream_npa_readv_byte_mode_handler(struct tevent_req *subreq);
     403             : static int tstream_npa_readv_next_vector(struct tstream_context *stream,
     404             :                                          void *private_data,
     405             :                                          TALLOC_CTX *mem_ctx,
     406             :                                          struct iovec **_vector,
     407             :                                          size_t *_count);
     408             : static void tstream_npa_readv_msg_mode_handler(struct tevent_req *subreq);
     409             : 
     410     1390207 : static struct tevent_req *tstream_npa_readv_send(TALLOC_CTX *mem_ctx,
     411             :                                         struct tevent_context *ev,
     412             :                                         struct tstream_context *stream,
     413             :                                         struct iovec *vector,
     414             :                                         size_t count)
     415             : {
     416             :         struct tevent_req *req;
     417             :         struct tstream_npa_readv_state *state;
     418     1390207 :         struct tstream_npa *npas = tstream_context_data(stream, struct tstream_npa);
     419             :         struct tevent_req *subreq;
     420             :         off_t ofs;
     421             :         size_t left;
     422             :         uint8_t *pbase;
     423             : 
     424     1390207 :         req = tevent_req_create(mem_ctx, &state,
     425             :                                 struct tstream_npa_readv_state);
     426     1390207 :         if (!req) {
     427           0 :                 return NULL;
     428             :         }
     429             : 
     430     1390207 :         state->stream        = stream;
     431     1390207 :         state->ret   = 0;
     432             : 
     433     1390207 :         if (!npas->unix_stream) {
     434           0 :                 tevent_req_error(req, ENOTCONN);
     435           0 :                 goto post;
     436             :         }
     437             : 
     438     1390207 :         switch (npas->file_type) {
     439           0 :         case FILE_TYPE_BYTE_MODE_PIPE:
     440           0 :                 state->vector = vector;
     441           0 :                 state->count = count;
     442             : 
     443           0 :                 subreq = tstream_readv_send(state,
     444             :                                             ev,
     445             :                                             npas->unix_stream,
     446           0 :                                             state->vector,
     447           0 :                                             state->count);
     448           0 :                 if (tevent_req_nomem(subreq,req)) {
     449           0 :                         goto post;
     450             :                 }
     451           0 :                 tevent_req_set_callback(subreq,
     452             :                                         tstream_npa_readv_byte_mode_handler,
     453             :                                         req);
     454             : 
     455           0 :                 return req;
     456             : 
     457     1390207 :         case FILE_TYPE_MESSAGE_MODE_PIPE:
     458             :                 /*
     459             :                  * we make a copy of the vector and prepend a header
     460             :                  * with the length
     461             :                  */
     462     1390207 :                 state->vector        = talloc_array(state, struct iovec, count);
     463     1390207 :                 if (tevent_req_nomem(state->vector, req)) {
     464           0 :                         goto post;
     465             :                 }
     466     1417592 :                 memcpy(state->vector, vector, sizeof(struct iovec)*count);
     467     1390207 :                 state->count = count;
     468             : 
     469             :                 /*
     470             :                  * copy the pending buffer first
     471             :                  */
     472     1390207 :                 ofs = 0;
     473     1390207 :                 left = npas->pending.iov_len;
     474     1390207 :                 pbase = (uint8_t *)npas->pending.iov_base;
     475             : 
     476     1390207 :                 while (left > 0 && state->count > 0) {
     477             :                         uint8_t *base;
     478      686919 :                         base = (uint8_t *)state->vector[0].iov_base;
     479      686919 :                         if (left < state->vector[0].iov_len) {
     480           0 :                                 memcpy(base, pbase + ofs, left);
     481             : 
     482           0 :                                 base += left;
     483           0 :                                 state->vector[0].iov_base = (char *) base;
     484           0 :                                 state->vector[0].iov_len -= left;
     485             : 
     486           0 :                                 ofs += left;
     487           0 :                                 left = 0;
     488           0 :                                 TALLOC_FREE(pbase);
     489           0 :                                 ZERO_STRUCT(npas->pending);
     490           0 :                                 break;
     491             :                         }
     492      700295 :                         memcpy(base, pbase + ofs, state->vector[0].iov_len);
     493             : 
     494      686919 :                         ofs += state->vector[0].iov_len;
     495      686919 :                         left -= state->vector[0].iov_len;
     496      686919 :                         state->vector += 1;
     497      686919 :                         state->count -= 1;
     498             : 
     499      686919 :                         if (left == 0) {
     500      686919 :                                 TALLOC_FREE(pbase);
     501      686919 :                                 ZERO_STRUCT(npas->pending);
     502      686919 :                                 break;
     503             :                         }
     504             :                 }
     505             : 
     506     1390207 :                 if (left > 0) {
     507           0 :                         memmove(pbase, pbase + ofs, left);
     508           0 :                         npas->pending.iov_base = (char *) pbase;
     509           0 :                         npas->pending.iov_len = left;
     510             :                         /*
     511             :                          * this cannot fail and even if it
     512             :                          * fails we can handle it
     513             :                          */
     514           0 :                         pbase = talloc_realloc(npas, pbase, uint8_t, left);
     515           0 :                         if (pbase) {
     516           0 :                                 npas->pending.iov_base = (char *) pbase;
     517             :                         }
     518           0 :                         pbase = NULL;
     519             :                 }
     520             : 
     521     1390207 :                 state->ret += ofs;
     522             : 
     523     1390207 :                 if (state->count == 0) {
     524      686919 :                         tevent_req_done(req);
     525      686919 :                         goto post;
     526             :                 }
     527             : 
     528      703288 :                 ZERO_STRUCT(state->hdr);
     529      703288 :                 state->wait_for_hdr = false;
     530             : 
     531      703288 :                 subreq = tstream_readv_pdu_send(state,
     532             :                                                 ev,
     533             :                                                 npas->unix_stream,
     534             :                                                 tstream_npa_readv_next_vector,
     535             :                                                 state);
     536      703288 :                 if (tevent_req_nomem(subreq, req)) {
     537           0 :                         goto post;
     538             :                 }
     539      703288 :                 tevent_req_set_callback(subreq,
     540             :                                         tstream_npa_readv_msg_mode_handler,
     541             :                                         req);
     542             : 
     543      703288 :                 return req;
     544             :         }
     545             : 
     546             :         /* this can't happen */
     547           0 :         tevent_req_error(req, EINVAL);
     548           0 :         goto post;
     549             : 
     550      686919 :  post:
     551      686919 :         tevent_req_post(req, ev);
     552      686919 :         return req;
     553             : }
     554             : 
     555           0 : static void tstream_npa_readv_byte_mode_handler(struct tevent_req *subreq)
     556             : {
     557           0 :         struct tevent_req *req = tevent_req_callback_data(subreq,
     558             :                                  struct tevent_req);
     559           0 :         struct tstream_npa_readv_state *state = tevent_req_data(req,
     560             :                                         struct tstream_npa_readv_state);
     561             :         int ret;
     562             :         int sys_errno;
     563             : 
     564           0 :         ret = tstream_readv_recv(subreq, &sys_errno);
     565           0 :         TALLOC_FREE(subreq);
     566           0 :         if (ret == -1) {
     567           0 :                 tevent_req_error(req, sys_errno);
     568           0 :                 return;
     569             :         }
     570             : 
     571           0 :         state->ret = ret;
     572             : 
     573           0 :         tevent_req_done(req);
     574             : }
     575             : 
     576     2077126 : static int tstream_npa_readv_next_vector(struct tstream_context *unix_stream,
     577             :                                          void *private_data,
     578             :                                          TALLOC_CTX *mem_ctx,
     579             :                                          struct iovec **_vector,
     580             :                                          size_t *_count)
     581             : {
     582     2077126 :         struct tstream_npa_readv_state *state = talloc_get_type_abort(private_data,
     583             :                                         struct tstream_npa_readv_state);
     584     2077126 :         struct tstream_npa *npas = tstream_context_data(state->stream,
     585             :                                    struct tstream_npa);
     586             :         struct iovec *vector;
     587             :         size_t count;
     588             :         uint16_t msg_len;
     589             :         size_t left;
     590             : 
     591     2077126 :         if (state->count == 0) {
     592      686919 :                 *_vector = NULL;
     593      686919 :                 *_count = 0;
     594      686919 :                 return 0;
     595             :         }
     596             : 
     597     1390207 :         if (!state->wait_for_hdr) {
     598             :                 /* we need to get a message header */
     599      703288 :                 vector = talloc_array(mem_ctx, struct iovec, 1);
     600      703288 :                 if (!vector) {
     601           0 :                         return -1;
     602             :                 }
     603      703288 :                 ZERO_STRUCT(state->hdr);
     604      703288 :                 vector[0].iov_base = (char *) state->hdr;
     605      703288 :                 vector[0].iov_len = sizeof(state->hdr);
     606             : 
     607      703288 :                 count = 1;
     608             : 
     609      703288 :                 state->wait_for_hdr = true;
     610             : 
     611      703288 :                 *_vector = vector;
     612      703288 :                 *_count = count;
     613      703288 :                 return 0;
     614             :         }
     615             : 
     616             :         /* and now fill the callers buffers and maybe the pending buffer */
     617      686919 :         state->wait_for_hdr = false;
     618             : 
     619      686919 :         msg_len = SVAL(state->hdr, 0);
     620             : 
     621      686919 :         if (msg_len == 0) {
     622           0 :                 errno = EIO;
     623           0 :                 return -1;
     624             :         }
     625             : 
     626      673543 :         state->wait_for_hdr = false;
     627             : 
     628             :         /* +1 because we may need to fill the pending buffer */
     629      686919 :         vector = talloc_array(mem_ctx, struct iovec, state->count + 1);
     630      686919 :         if (!vector) {
     631           0 :                 return -1;
     632             :         }
     633             : 
     634      686919 :         count = 0;
     635      686919 :         left = msg_len;
     636     1940447 :         while (left > 0 && state->count > 0) {
     637      686919 :                 if (left < state->vector[0].iov_len) {
     638             :                         uint8_t *base;
     639           0 :                         base = (uint8_t *)state->vector[0].iov_base;
     640           0 :                         vector[count].iov_base = (char *) base;
     641           0 :                         vector[count].iov_len = left;
     642           0 :                         count++;
     643           0 :                         base += left;
     644           0 :                         state->vector[0].iov_base = (char *) base;
     645           0 :                         state->vector[0].iov_len -= left;
     646           0 :                         break;
     647             :                 }
     648      686919 :                 vector[count] = state->vector[0];
     649      686919 :                 count++;
     650      686919 :                 left -= state->vector[0].iov_len;
     651      686919 :                 state->vector += 1;
     652      686919 :                 state->count -= 1;
     653             :         }
     654             : 
     655      686919 :         if (left > 0) {
     656             :                 /*
     657             :                  * if the message is longer than the buffers the caller
     658             :                  * requested, we need to consume the rest of the message
     659             :                  * into the pending buffer, where the next readv can
     660             :                  * be served from.
     661             :                  */
     662      686919 :                 npas->pending.iov_base = talloc_array(npas, char, left);
     663      686919 :                 if (!npas->pending.iov_base) {
     664           0 :                         return -1;
     665             :                 }
     666      686919 :                 npas->pending.iov_len = left;
     667             : 
     668      686919 :                 vector[count] = npas->pending;
     669      686919 :                 count++;
     670             :         }
     671             : 
     672      686919 :         state->ret += (msg_len - left);
     673             : 
     674      686919 :         *_vector = vector;
     675      686919 :         *_count = count;
     676      686919 :         return 0;
     677             : }
     678             : 
     679      694826 : static void tstream_npa_readv_msg_mode_handler(struct tevent_req *subreq)
     680             : {
     681      694826 :         struct tevent_req *req = tevent_req_callback_data(subreq,
     682             :                                  struct tevent_req);
     683             :         int ret;
     684             :         int sys_errno;
     685             : 
     686      694826 :         ret = tstream_readv_pdu_recv(subreq, &sys_errno);
     687      694826 :         TALLOC_FREE(subreq);
     688      694826 :         if (ret == -1) {
     689        7907 :                 tevent_req_error(req, sys_errno);
     690        7797 :                 return;
     691             :         }
     692             : 
     693             :         /*
     694             :          * we do not set state->ret here as ret includes the header size.
     695             :          * we set it in tstream_npa_readv_pdu_next_vector()
     696             :          */
     697             : 
     698      686919 :         tevent_req_done(req);
     699             : }
     700             : 
     701     1381745 : static int tstream_npa_readv_recv(struct tevent_req *req,
     702             :                                    int *perrno)
     703             : {
     704     1381745 :         struct tstream_npa_readv_state *state = tevent_req_data(req,
     705             :                                         struct tstream_npa_readv_state);
     706             :         int ret;
     707             : 
     708     1381745 :         ret = tsocket_simple_int_recv(req, perrno);
     709     1381745 :         if (ret == 0) {
     710     1373838 :                 ret = state->ret;
     711             :         }
     712             : 
     713     1381745 :         tevent_req_received(req);
     714     1381745 :         return ret;
     715             : }
     716             : 
     717             : struct tstream_npa_writev_state {
     718             :         const struct iovec *vector;
     719             :         size_t count;
     720             : 
     721             :         /* the header for message mode */
     722             :         bool hdr_used;
     723             :         uint8_t hdr[2];
     724             : 
     725             :         int ret;
     726             : };
     727             : 
     728             : static void tstream_npa_writev_handler(struct tevent_req *subreq);
     729             : 
     730      686954 : static struct tevent_req *tstream_npa_writev_send(TALLOC_CTX *mem_ctx,
     731             :                                         struct tevent_context *ev,
     732             :                                         struct tstream_context *stream,
     733             :                                         const struct iovec *vector,
     734             :                                         size_t count)
     735             : {
     736             :         struct tevent_req *req;
     737             :         struct tstream_npa_writev_state *state;
     738      686954 :         struct tstream_npa *npas = tstream_context_data(stream, struct tstream_npa);
     739             :         struct tevent_req *subreq;
     740             :         size_t msg_len;
     741             :         size_t i;
     742             :         struct iovec *new_vector;
     743             : 
     744      686954 :         req = tevent_req_create(mem_ctx, &state,
     745             :                                 struct tstream_npa_writev_state);
     746      686954 :         if (!req) {
     747           0 :                 return NULL;
     748             :         }
     749             : 
     750      686954 :         state->ret   = 0;
     751             : 
     752      686954 :         if (!npas->unix_stream) {
     753           0 :                 tevent_req_error(req, ENOTCONN);
     754           0 :                 goto post;
     755             :         }
     756             : 
     757      686954 :         switch (npas->file_type) {
     758           0 :         case FILE_TYPE_BYTE_MODE_PIPE:
     759           0 :                 state->hdr_used      = false;
     760           0 :                 state->vector        = vector;
     761           0 :                 state->count = count;
     762           0 :                 break;
     763             : 
     764      686954 :         case FILE_TYPE_MESSAGE_MODE_PIPE:
     765             :                 /*
     766             :                  * we make a copy of the vector and prepend a header
     767             :                  * with the length
     768             :                  */
     769      686954 :                 new_vector      = talloc_array(state, struct iovec, count + 1);
     770      686954 :                 if (tevent_req_nomem(new_vector, req)) {
     771           0 :                         goto post;
     772             :                 }
     773      686954 :                 new_vector[0].iov_base = (char *) state->hdr;
     774      686954 :                 new_vector[0].iov_len = sizeof(state->hdr);
     775      700330 :                 memcpy(new_vector + 1, vector, sizeof(struct iovec)*count);
     776             : 
     777      686954 :                 state->hdr_used      = true;
     778      686954 :                 state->vector        = new_vector;
     779      686954 :                 state->count = count + 1;
     780             : 
     781      686954 :                 msg_len = 0;
     782     1373908 :                 for (i=0; i < count; i++) {
     783             :                         /*
     784             :                          * overflow check already done in tstream_writev_send
     785             :                          */
     786      686954 :                         msg_len += vector[i].iov_len;
     787             :                 }
     788             : 
     789      686954 :                 if (msg_len > UINT16_MAX) {
     790           0 :                         tevent_req_error(req, EMSGSIZE);
     791           0 :                         goto post;
     792             :                 }
     793             : 
     794      686954 :                 SSVAL(state->hdr, 0, msg_len);
     795      686954 :                 break;
     796             :         }
     797             : 
     798     1240175 :         subreq = tstream_writev_send(state,
     799             :                                      ev,
     800             :                                      npas->unix_stream,
     801      673578 :                                      state->vector,
     802      686954 :                                      state->count);
     803      686954 :         if (tevent_req_nomem(subreq, req)) {
     804           0 :                 goto post;
     805             :         }
     806      686954 :         tevent_req_set_callback(subreq, tstream_npa_writev_handler, req);
     807             : 
     808      686954 :         return req;
     809             : 
     810           0 :  post:
     811           0 :         tevent_req_post(req, ev);
     812           0 :         return req;
     813             : }
     814             : 
     815      686954 : static void tstream_npa_writev_handler(struct tevent_req *subreq)
     816             : {
     817      686954 :         struct tevent_req *req = tevent_req_callback_data(subreq,
     818             :                                  struct tevent_req);
     819      686954 :         struct tstream_npa_writev_state *state = tevent_req_data(req,
     820             :                                         struct tstream_npa_writev_state);
     821             :         int ret;
     822             :         int sys_errno;
     823             : 
     824      686954 :         ret = tstream_writev_recv(subreq, &sys_errno);
     825      686954 :         TALLOC_FREE(subreq);
     826      686954 :         if (ret == -1) {
     827           1 :                 tevent_req_error(req, sys_errno);
     828           1 :                 return;
     829             :         }
     830             : 
     831             :         /*
     832             :          * in message mode we need to hide the length
     833             :          * of the hdr from the caller
     834             :          */
     835      686953 :         if (state->hdr_used) {
     836      686953 :                 ret -= sizeof(state->hdr);
     837             :         }
     838             : 
     839      686953 :         state->ret = ret;
     840             : 
     841      686953 :         tevent_req_done(req);
     842             : }
     843             : 
     844      686954 : static int tstream_npa_writev_recv(struct tevent_req *req,
     845             :                                    int *perrno)
     846             : {
     847      686954 :         struct tstream_npa_writev_state *state = tevent_req_data(req,
     848             :                                         struct tstream_npa_writev_state);
     849             :         int ret;
     850             : 
     851      686954 :         ret = tsocket_simple_int_recv(req, perrno);
     852      686954 :         if (ret == 0) {
     853      686953 :                 ret = state->ret;
     854             :         }
     855             : 
     856      686954 :         tevent_req_received(req);
     857      686954 :         return ret;
     858             : }
     859             : 
     860             : struct tstream_npa_disconnect_state {
     861             :         struct tstream_context *stream;
     862             : };
     863             : 
     864             : static void tstream_npa_disconnect_handler(struct tevent_req *subreq);
     865             : 
     866           0 : static struct tevent_req *tstream_npa_disconnect_send(TALLOC_CTX *mem_ctx,
     867             :                                                 struct tevent_context *ev,
     868             :                                                 struct tstream_context *stream)
     869             : {
     870           0 :         struct tstream_npa *npas = tstream_context_data(stream, struct tstream_npa);
     871             :         struct tevent_req *req;
     872             :         struct tstream_npa_disconnect_state *state;
     873             :         struct tevent_req *subreq;
     874             : 
     875           0 :         req = tevent_req_create(mem_ctx, &state,
     876             :                                 struct tstream_npa_disconnect_state);
     877           0 :         if (req == NULL) {
     878           0 :                 return NULL;
     879             :         }
     880             : 
     881           0 :         state->stream = stream;
     882             : 
     883           0 :         if (!npas->unix_stream) {
     884           0 :                 tevent_req_error(req, ENOTCONN);
     885           0 :                 goto post;
     886             :         }
     887             : 
     888           0 :         subreq = tstream_disconnect_send(state,
     889             :                                          ev,
     890             :                                          npas->unix_stream);
     891           0 :         if (tevent_req_nomem(subreq, req)) {
     892           0 :                 goto post;
     893             :         }
     894           0 :         tevent_req_set_callback(subreq, tstream_npa_disconnect_handler, req);
     895             : 
     896           0 :         return req;
     897             : 
     898           0 : post:
     899           0 :         tevent_req_post(req, ev);
     900           0 :         return req;
     901             : }
     902             : 
     903           0 : static void tstream_npa_disconnect_handler(struct tevent_req *subreq)
     904             : {
     905           0 :         struct tevent_req *req = tevent_req_callback_data(subreq,
     906             :                                  struct tevent_req);
     907           0 :         struct tstream_npa_disconnect_state *state = tevent_req_data(req,
     908             :                                         struct tstream_npa_disconnect_state);
     909           0 :         struct tstream_context *stream = state->stream;
     910           0 :         struct tstream_npa *npas = tstream_context_data(stream, struct tstream_npa);
     911             :         int ret;
     912             :         int sys_errno;
     913             : 
     914           0 :         ret = tstream_disconnect_recv(subreq, &sys_errno);
     915           0 :         TALLOC_FREE(subreq);
     916           0 :         if (ret == -1) {
     917           0 :                 tevent_req_error(req, sys_errno);
     918           0 :                 return;
     919             :         }
     920             : 
     921           0 :         TALLOC_FREE(npas->unix_stream);
     922             : 
     923           0 :         tevent_req_done(req);
     924             : }
     925             : 
     926           0 : static int tstream_npa_disconnect_recv(struct tevent_req *req,
     927             :                                        int *perrno)
     928             : {
     929             :         int ret;
     930             : 
     931           0 :         ret = tsocket_simple_int_recv(req, perrno);
     932             : 
     933           0 :         tevent_req_received(req);
     934           0 :         return ret;
     935             : }
     936             : 
     937             : static const struct tstream_context_ops tstream_npa_ops = {
     938             :         .name                   = "npa",
     939             : 
     940             :         .pending_bytes          = tstream_npa_pending_bytes,
     941             : 
     942             :         .readv_send             = tstream_npa_readv_send,
     943             :         .readv_recv             = tstream_npa_readv_recv,
     944             : 
     945             :         .writev_send            = tstream_npa_writev_send,
     946             :         .writev_recv            = tstream_npa_writev_recv,
     947             : 
     948             :         .disconnect_send        = tstream_npa_disconnect_send,
     949             :         .disconnect_recv        = tstream_npa_disconnect_recv,
     950             : };
     951             : 
     952       16640 : int _tstream_npa_existing_stream(TALLOC_CTX *mem_ctx,
     953             :                                  struct tstream_context **transport,
     954             :                                  uint16_t file_type,
     955             :                                  struct tstream_context **_stream,
     956             :                                  const char *location)
     957             : {
     958             :         struct tstream_context *stream;
     959             :         struct tstream_npa *npas;
     960             : 
     961       16640 :         switch (file_type) {
     962           0 :         case FILE_TYPE_BYTE_MODE_PIPE:
     963           0 :                 break;
     964       16640 :         case FILE_TYPE_MESSAGE_MODE_PIPE:
     965       16640 :                 break;
     966           0 :         default:
     967           0 :                 errno = EINVAL;
     968           0 :                 return -1;
     969             :         }
     970             : 
     971       16640 :         stream = tstream_context_create(mem_ctx,
     972             :                                         &tstream_npa_ops,
     973             :                                         &npas,
     974             :                                         struct tstream_npa,
     975             :                                         location);
     976       16640 :         if (!stream) {
     977           0 :                 return -1;
     978             :         }
     979             : 
     980       33280 :         *npas = (struct tstream_npa) {
     981             :                 .file_type = file_type,
     982       16640 :                 .unix_stream = talloc_move(npas, transport),
     983             :         };
     984             : 
     985       16640 :         *_stream = stream;
     986       16640 :         return 0;
     987             : }
     988             : 
     989       16640 : int _tstream_npa_existing_socket(TALLOC_CTX *mem_ctx,
     990             :                                  int fd,
     991             :                                  uint16_t file_type,
     992             :                                  struct tstream_context **_stream,
     993             :                                  const char *location)
     994             : {
     995       16640 :         struct tstream_context *transport = NULL;
     996             :         int ret;
     997             : 
     998       16640 :         ret = _tstream_bsd_existing_socket(
     999             :                 mem_ctx, fd, &transport, location);
    1000       16640 :         if (ret == -1) {
    1001           0 :                 return -1;
    1002             :         }
    1003       16640 :         return _tstream_npa_existing_stream(
    1004             :                 mem_ctx, &transport, file_type, _stream, location);
    1005             : }
    1006             : 
    1007             : struct tstream_npa_accept_state {
    1008             :         struct tevent_context *ev;
    1009             :         struct tstream_context *plain;
    1010             :         uint16_t file_type;
    1011             :         uint16_t device_state;
    1012             :         uint64_t alloc_size;
    1013             : 
    1014             :         struct named_pipe_auth_req *pipe_request;
    1015             : 
    1016             :         DATA_BLOB npa_blob;
    1017             :         struct iovec out_iov;
    1018             : 
    1019             :         /* results */
    1020             :         NTSTATUS accept_status;
    1021             :         struct tsocket_address *remote_client_addr;
    1022             :         struct tsocket_address *local_server_addr;
    1023             : };
    1024             : 
    1025             : static void tstream_npa_accept_existing_reply(struct tevent_req *subreq);
    1026             : static void tstream_npa_accept_existing_done(struct tevent_req *subreq);
    1027             : 
    1028        8094 : struct tevent_req *tstream_npa_accept_existing_send(TALLOC_CTX *mem_ctx,
    1029             :                                         struct tevent_context *ev,
    1030             :                                         struct tstream_context *plain,
    1031             :                                         uint16_t file_type,
    1032             :                                         uint16_t device_state,
    1033             :                                         uint64_t allocation_size)
    1034             : {
    1035             :         struct tstream_npa_accept_state *state;
    1036             :         struct tevent_req *req, *subreq;
    1037             : 
    1038        8094 :         req = tevent_req_create(mem_ctx, &state,
    1039             :                                 struct tstream_npa_accept_state);
    1040        8094 :         if (req == NULL) {
    1041           0 :                 return NULL;
    1042             :         }
    1043             : 
    1044        8094 :         switch (file_type) {
    1045           0 :         case FILE_TYPE_BYTE_MODE_PIPE:
    1046           0 :                 break;
    1047        7464 :         case FILE_TYPE_MESSAGE_MODE_PIPE:
    1048        7464 :                 break;
    1049           0 :         default:
    1050           0 :                 tevent_req_error(req, EINVAL);
    1051           0 :                 goto post;
    1052             :         }
    1053             : 
    1054        8094 :         state->ev = ev;
    1055        8094 :         state->plain = plain;
    1056        8094 :         state->file_type = file_type;
    1057        8094 :         state->device_state = device_state;
    1058        8094 :         state->alloc_size = allocation_size;
    1059             : 
    1060        8094 :         subreq = tstream_u32_read_send(state, ev, 0x00FFFFFF, plain);
    1061        8094 :         if (tevent_req_nomem(subreq, req)) {
    1062           0 :                 goto post;
    1063             :         }
    1064             : 
    1065        8094 :         tevent_req_set_callback(subreq,
    1066             :                                 tstream_npa_accept_existing_reply, req);
    1067             : 
    1068        8094 :         return req;
    1069             : 
    1070           0 : post:
    1071           0 :         tevent_req_post(req, ev);
    1072           0 :         return req;
    1073             : }
    1074             : 
    1075        8094 : static void tstream_npa_accept_existing_reply(struct tevent_req *subreq)
    1076             : {
    1077        5933 :         struct tevent_req *req =
    1078        8094 :                         tevent_req_callback_data(subreq, struct tevent_req);
    1079        5933 :         struct tstream_npa_accept_state *state =
    1080        8094 :                         tevent_req_data(req, struct tstream_npa_accept_state);
    1081             :         struct named_pipe_auth_req *pipe_request;
    1082             :         struct named_pipe_auth_rep pipe_reply;
    1083             :         struct named_pipe_auth_req_info4 i4;
    1084             :         enum ndr_err_code ndr_err;
    1085             :         DATA_BLOB in, out;
    1086             :         int err;
    1087             :         int ret;
    1088             : 
    1089        8094 :         err = tstream_u32_read_recv(subreq, state, &in.data, &in.length);
    1090        8094 :         if (err != 0) {
    1091           0 :                 tevent_req_error(req, err);
    1092           0 :                 return;
    1093             :         }
    1094        8094 :         if (in.length < 8) {
    1095           0 :                 tevent_req_error(req, EMSGSIZE);
    1096           0 :                 return;
    1097             :         }
    1098             : 
    1099        8094 :         if (memcmp(&in.data[4], NAMED_PIPE_AUTH_MAGIC, 4) != 0) {
    1100           0 :                 DBG_ERR("Wrong protocol\n");
    1101             : #if defined(EPROTONOSUPPORT)
    1102           0 :                 err = EPROTONOSUPPORT;
    1103             : #elif defined(EPROTO)
    1104             :                 err = EPROTO;
    1105             : #else
    1106             :                 err = EINVAL;
    1107             : #endif
    1108           0 :                 tevent_req_error(req, err);
    1109           0 :                 return;
    1110             :         }
    1111             : 
    1112        8094 :         DBG_DEBUG("Received packet of length %zu\n", in.length);
    1113        8094 :         dump_data(11, in.data, in.length);
    1114             : 
    1115        8094 :         ZERO_STRUCT(pipe_reply);
    1116        8094 :         pipe_reply.level = 0;
    1117        8094 :         pipe_reply.status = NT_STATUS_INTERNAL_ERROR;
    1118             :         /*
    1119             :          * TODO: check it's a root (uid == 0) pipe
    1120             :          */
    1121             : 
    1122        8094 :         pipe_request = talloc(state, struct named_pipe_auth_req);
    1123        8094 :         if (!pipe_request) {
    1124           0 :                 DEBUG(0, ("Out of memory!\n"));
    1125           0 :                 goto reply;
    1126             :         }
    1127        8094 :         state->pipe_request = pipe_request;
    1128             : 
    1129             :         /* parse the passed credentials */
    1130        8094 :         ndr_err = ndr_pull_struct_blob_all(
    1131             :                 &in,
    1132             :                 pipe_request,
    1133             :                 pipe_request,
    1134             :                 (ndr_pull_flags_fn_t)ndr_pull_named_pipe_auth_req);
    1135        8094 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1136           0 :                 pipe_reply.status = ndr_map_error2ntstatus(ndr_err);
    1137           0 :                 DEBUG(2, ("Could not unmarshall named_pipe_auth_req: %s\n",
    1138             :                           nt_errstr(pipe_reply.status)));
    1139           0 :                 goto reply;
    1140             :         }
    1141             : 
    1142        8094 :         if (DEBUGLVL(10)) {
    1143           0 :                 NDR_PRINT_DEBUG(named_pipe_auth_req, pipe_request);
    1144             :         }
    1145             : 
    1146        8094 :         ZERO_STRUCT(i4);
    1147             : 
    1148        8094 :         if (pipe_request->level != 4) {
    1149           0 :                 DEBUG(0, ("Unknown level %u\n", pipe_request->level));
    1150           0 :                 pipe_reply.level = 0;
    1151           0 :                 pipe_reply.status = NT_STATUS_INVALID_LEVEL;
    1152           0 :                 goto reply;
    1153             :         }
    1154             : 
    1155        8094 :         pipe_reply.level = 4;
    1156        8094 :         pipe_reply.status = NT_STATUS_OK;
    1157        8094 :         pipe_reply.info.info4.file_type = state->file_type;
    1158        8094 :         pipe_reply.info.info4.device_state = state->device_state;
    1159        8094 :         pipe_reply.info.info4.allocation_size = state->alloc_size;
    1160             : 
    1161        8094 :         i4 = pipe_request->info.info4;
    1162        8094 :         if (i4.local_server_addr == NULL) {
    1163           0 :                 pipe_reply.status = NT_STATUS_INVALID_ADDRESS;
    1164           0 :                 DEBUG(2, ("Missing local server address\n"));
    1165           0 :                 goto reply;
    1166             :         }
    1167        8094 :         if (i4.remote_client_addr == NULL) {
    1168           0 :                 pipe_reply.status = NT_STATUS_INVALID_ADDRESS;
    1169           0 :                 DEBUG(2, ("Missing remote client address\n"));
    1170           0 :                 goto reply;
    1171             :         }
    1172             : 
    1173        8094 :         ret = tsocket_address_inet_from_strings(state, "ip",
    1174             :                                                 i4.local_server_addr,
    1175             :                                                 i4.local_server_port,
    1176             :                                                 &state->local_server_addr);
    1177        8094 :         if (ret != 0) {
    1178           0 :                 DEBUG(2, ("Invalid local server address[%s:%u] - %s\n",
    1179             :                           i4.local_server_addr, i4.local_server_port,
    1180             :                           strerror(errno)));
    1181           0 :                 pipe_reply.status = NT_STATUS_INVALID_ADDRESS;
    1182           0 :                 goto reply;
    1183             :         }
    1184             : 
    1185        8094 :         ret = tsocket_address_inet_from_strings(state, "ip",
    1186             :                                                 i4.remote_client_addr,
    1187             :                                                 i4.remote_client_port,
    1188             :                                                 &state->remote_client_addr);
    1189        8094 :         if (ret != 0) {
    1190           0 :                 DEBUG(2, ("Invalid remote client address[%s:%u] - %s\n",
    1191             :                           i4.remote_client_addr, i4.remote_client_port,
    1192             :                           strerror(errno)));
    1193           0 :                 pipe_reply.status = NT_STATUS_INVALID_ADDRESS;
    1194           0 :                 goto reply;
    1195             :         }
    1196             : 
    1197       14027 : reply:
    1198             :         /* create the output */
    1199        8094 :         ndr_err = ndr_push_struct_blob(&out, state, &pipe_reply,
    1200             :                         (ndr_push_flags_fn_t)ndr_push_named_pipe_auth_rep);
    1201        8094 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1202           0 :                 DEBUG(2, ("Error encoding structure: %s",
    1203             :                           ndr_map_error2string(ndr_err)));
    1204           0 :                 tevent_req_error(req, EIO);
    1205           0 :                 return;
    1206             :         }
    1207             : 
    1208        8094 :         DEBUG(10, ("named_pipe_auth reply[%u]\n", (unsigned)out.length));
    1209        8094 :         dump_data(11, out.data, out.length);
    1210             : 
    1211        8094 :         if (DEBUGLVL(10)) {
    1212           0 :                 NDR_PRINT_DEBUG(named_pipe_auth_rep, &pipe_reply);
    1213             :         }
    1214             : 
    1215        8094 :         state->accept_status = pipe_reply.status;
    1216             : 
    1217        8094 :         state->out_iov.iov_base = (char *) out.data;
    1218        8094 :         state->out_iov.iov_len = out.length;
    1219             : 
    1220        8094 :         subreq = tstream_writev_send(state, state->ev,
    1221             :                                      state->plain,
    1222        8094 :                                      &state->out_iov, 1);
    1223        8094 :         if (tevent_req_nomem(subreq, req)) {
    1224           0 :                 DEBUG(0, ("no memory for tstream_writev_send"));
    1225           0 :                 return;
    1226             :         }
    1227             : 
    1228        8094 :         tevent_req_set_callback(subreq, tstream_npa_accept_existing_done, req);
    1229             : }
    1230             : 
    1231        8094 : static void tstream_npa_accept_existing_done(struct tevent_req *subreq)
    1232             : {
    1233        5933 :         struct tevent_req *req =
    1234        8094 :                         tevent_req_callback_data(subreq, struct tevent_req);
    1235             :         int sys_errno;
    1236             :         int ret;
    1237             : 
    1238        8094 :         ret = tstream_writev_recv(subreq, &sys_errno);
    1239        8094 :         TALLOC_FREE(subreq);
    1240        8094 :         if (ret == -1) {
    1241           0 :                 tevent_req_error(req, sys_errno);
    1242           0 :                 return;
    1243             :         }
    1244             : 
    1245        8094 :         tevent_req_done(req);
    1246             : }
    1247             : 
    1248           0 : static struct named_pipe_auth_req_info4 *copy_npa_info4(
    1249             :         TALLOC_CTX *mem_ctx, const struct named_pipe_auth_req_info4 *src)
    1250             : {
    1251           0 :         struct named_pipe_auth_req_info4 *dst = NULL;
    1252             :         DATA_BLOB blob;
    1253             :         enum ndr_err_code ndr_err;
    1254             : 
    1255           0 :         dst = talloc_zero(mem_ctx, struct named_pipe_auth_req_info4);
    1256           0 :         if (dst == NULL) {
    1257           0 :                 return NULL;
    1258             :         }
    1259             : 
    1260           0 :         ndr_err = ndr_push_struct_blob(
    1261             :                 &blob,
    1262             :                 dst,
    1263             :                 src,
    1264             :                 (ndr_push_flags_fn_t)ndr_push_named_pipe_auth_req_info4);
    1265           0 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1266           0 :                 DBG_WARNING("ndr_push_named_pipe_auth_req_info4 failed: %s\n",
    1267             :                             ndr_errstr(ndr_err));
    1268           0 :                 TALLOC_FREE(dst);
    1269           0 :                 return NULL;
    1270             :         }
    1271             : 
    1272           0 :         ndr_err = ndr_pull_struct_blob_all(
    1273             :                 &blob,
    1274             :                 dst,
    1275             :                 dst,
    1276             :                 (ndr_pull_flags_fn_t)ndr_pull_named_pipe_auth_req_info4);
    1277           0 :         TALLOC_FREE(blob.data);
    1278           0 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1279           0 :                 DBG_WARNING("ndr_push_named_pipe_auth_req_info4 failed: %s\n",
    1280             :                             ndr_errstr(ndr_err));
    1281           0 :                 TALLOC_FREE(dst);
    1282           0 :                 return NULL;
    1283             :         }
    1284             : 
    1285           0 :         return dst;
    1286             : }
    1287             : 
    1288        8094 : int _tstream_npa_accept_existing_recv(
    1289             :         struct tevent_req *req,
    1290             :         int *perrno,
    1291             :         TALLOC_CTX *mem_ctx,
    1292             :         struct tstream_context **stream,
    1293             :         struct named_pipe_auth_req_info4 **info4,
    1294             :         struct tsocket_address **remote_client_addr,
    1295             :         char **_remote_client_name,
    1296             :         struct tsocket_address **local_server_addr,
    1297             :         char **local_server_name,
    1298             :         struct auth_session_info_transport **session_info,
    1299             :         const char *location)
    1300             : {
    1301        5933 :         struct tstream_npa_accept_state *state =
    1302        8094 :                         tevent_req_data(req, struct tstream_npa_accept_state);
    1303        8094 :         struct named_pipe_auth_req_info4 *i4 = &state->pipe_request->info.info4;
    1304             :         struct tstream_npa *npas;
    1305             :         int ret;
    1306             : 
    1307        8094 :         ret = tsocket_simple_int_recv(req, perrno);
    1308        8094 :         if (ret != 0) {
    1309           0 :                 DEBUG(2, ("Failed to accept named pipe connection: %s\n",
    1310             :                           strerror(*perrno)));
    1311           0 :                 tevent_req_received(req);
    1312           0 :                 return -1;
    1313             :         }
    1314             : 
    1315        8094 :         if (!NT_STATUS_IS_OK(state->accept_status)) {
    1316             : #if defined(EPROTONOSUPPORT)
    1317           0 :                 *perrno = EPROTONOSUPPORT;
    1318             : #elif defined(EPROTO)
    1319             :                 *perrno = EPROTO;
    1320             : #else
    1321             :                 *perrno = EINVAL;
    1322             : #endif
    1323           0 :                 DEBUG(2, ("Failed to accept named pipe connection: %s => %s\n",
    1324             :                           nt_errstr(state->accept_status),
    1325             :                           strerror(*perrno)));
    1326           0 :                 tevent_req_received(req);
    1327           0 :                 return -1;
    1328             :         }
    1329             : 
    1330        8094 :         *stream = tstream_context_create(mem_ctx,
    1331             :                                          &tstream_npa_ops,
    1332             :                                          &npas,
    1333             :                                          struct tstream_npa,
    1334             :                                          location);
    1335        8094 :         if (!*stream) {
    1336           0 :                 *perrno = ENOMEM;
    1337           0 :                 tevent_req_received(req);
    1338           0 :                 return -1;
    1339             :         }
    1340        8094 :         ZERO_STRUCTP(npas);
    1341        8094 :         npas->unix_stream = state->plain;
    1342        8094 :         npas->file_type = state->file_type;
    1343             : 
    1344        8094 :         if (info4 != NULL) {
    1345             :                 /*
    1346             :                  * Make a full copy of "info4" because further down we
    1347             :                  * talloc_move() away substructures from
    1348             :                  * state->pipe_request.
    1349             :                  */
    1350           0 :                 struct named_pipe_auth_req_info4 *dst = copy_npa_info4(
    1351             :                         mem_ctx, i4);
    1352           0 :                 if (dst == NULL) {
    1353           0 :                         *perrno = ENOMEM;
    1354           0 :                         tevent_req_received(req);
    1355           0 :                         return -1;
    1356             :                 }
    1357           0 :                 *info4 = dst;
    1358             :         }
    1359             : 
    1360        8094 :         if (remote_client_addr != NULL) {
    1361        8094 :                 *remote_client_addr = talloc_move(
    1362             :                         mem_ctx, &state->remote_client_addr);
    1363             :         }
    1364        8094 :         if (_remote_client_name != NULL) {
    1365        8094 :                 *_remote_client_name = discard_const_p(
    1366             :                         char, talloc_move(mem_ctx, &i4->remote_client_name));
    1367             :         }
    1368        8094 :         if (local_server_addr != NULL) {
    1369        8094 :                 *local_server_addr = talloc_move(
    1370             :                         mem_ctx, &state->local_server_addr);
    1371             :         }
    1372        8094 :         if (local_server_name != NULL) {
    1373        8094 :                 *local_server_name = discard_const_p(
    1374             :                         char, talloc_move(mem_ctx, &i4->local_server_name));
    1375             :         }
    1376        8094 :         if (session_info != NULL) {
    1377        8094 :                 *session_info = talloc_move(mem_ctx, &i4->session_info);
    1378             :         }
    1379             : 
    1380        8094 :         tevent_req_received(req);
    1381        8094 :         return 0;
    1382             : }
    1383             : 
    1384             : 
    1385             : /* SOCKETPAIR for internal rpc communication */
    1386             : 
    1387             : /* file_type is FILE_TYPE_BYTE_MODE_PIPE or FILE_TYPE_MESSAGE_MODE_PIPE */
    1388        8320 : int _tstream_npa_socketpair(uint16_t file_type,
    1389             :                             TALLOC_CTX *mem_ctx1,
    1390             :                             struct tstream_context **pstream1,
    1391             :                             TALLOC_CTX *mem_ctx2,
    1392             :                             struct tstream_context **pstream2,
    1393             :                             const char *location)
    1394             : {
    1395        8320 :         struct tstream_context *stream1 = NULL;
    1396        8320 :         struct tstream_context *stream2 = NULL;
    1397             :         int fds[2];
    1398             :         int fd1;
    1399             :         int fd2;
    1400             :         int rc;
    1401             :         bool ok;
    1402             : 
    1403        8320 :         rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
    1404        8320 :         if (rc == -1) {
    1405           0 :                 return -1;
    1406             :         }
    1407        8320 :         fd1 = fds[0];
    1408        8320 :         fd2 = fds[1];
    1409             : 
    1410        8320 :         ok = smb_set_close_on_exec(fd1);
    1411        8320 :         if (!ok) {
    1412           0 :                 goto close_fail;
    1413             :         }
    1414             : 
    1415        8320 :         ok = smb_set_close_on_exec(fd2);
    1416        8320 :         if (!ok) {
    1417           0 :                 goto close_fail;
    1418             :         }
    1419             : 
    1420        8320 :         rc = set_blocking(fd1, false);
    1421        8320 :         if (rc == -1) {
    1422           0 :                 goto close_fail;
    1423             :         }
    1424             : 
    1425        8320 :         rc = set_blocking(fd2, false);
    1426        8320 :         if (rc == -1) {
    1427           0 :                 goto close_fail;
    1428             :         }
    1429             : 
    1430        8320 :         rc = _tstream_npa_existing_socket(mem_ctx1,
    1431             :                                           fd1,
    1432             :                                           file_type,
    1433             :                                           &stream1,
    1434             :                                           location);
    1435        8320 :         if (rc == -1) {
    1436           0 :                 goto close_fail;
    1437             :         }
    1438             : 
    1439        8320 :         rc = _tstream_npa_existing_socket(mem_ctx2,
    1440             :                                           fd2,
    1441             :                                           file_type,
    1442             :                                           &stream2,
    1443             :                                           location);
    1444        8320 :         if (rc == -1) {
    1445           0 :                 int sys_errno = errno;
    1446           0 :                 talloc_free(stream1);
    1447           0 :                 close(fd2);
    1448           0 :                 errno = sys_errno;
    1449           0 :                 return -1;
    1450             :         }
    1451             : 
    1452        8320 :         *pstream1 = stream1;
    1453        8320 :         *pstream2 = stream2;
    1454             : 
    1455        8320 :         return 0;
    1456             : 
    1457           0 : close_fail:
    1458             :         {
    1459           0 :                 int sys_errno = errno;
    1460           0 :                 close(fd1);
    1461           0 :                 close(fd2);
    1462           0 :                 errno = sys_errno;
    1463           0 :                 return -1;
    1464             :         }
    1465             : }

Generated by: LCOV version 1.13