s3-samr: in _samr_QueryUserInfo() make sure to not return any info in error case.
[Samba.git] / source3 / rpc_client / rpc_transport_sock.c
blobb1d9d8fbe133aa7ffbdccb6b2afaebff4b1432cf
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC client transport over a socket
4 * Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_RPC_CLI
25 struct rpc_transport_sock_state {
26 int fd;
29 static int rpc_transport_sock_state_destructor(struct rpc_transport_sock_state *s)
31 if (s->fd != -1) {
32 close(s->fd);
33 s->fd = -1;
35 return 0;
38 struct rpc_sock_read_state {
39 ssize_t received;
42 static void rpc_sock_read_done(struct tevent_req *subreq);
44 static struct async_req *rpc_sock_read_send(TALLOC_CTX *mem_ctx,
45 struct event_context *ev,
46 uint8_t *data, size_t size,
47 void *priv)
49 struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
50 priv, struct rpc_transport_sock_state);
51 struct async_req *result;
52 struct tevent_req *subreq;
53 struct rpc_sock_read_state *state;
55 if (!async_req_setup(mem_ctx, &result, &state,
56 struct rpc_sock_read_state)) {
57 return NULL;
60 subreq = async_recv_send(state, ev, sock_transp->fd, data, size, 0);
61 if (subreq == NULL) {
62 goto fail;
64 tevent_req_set_callback(subreq, rpc_sock_read_done, result);
65 return result;
66 fail:
67 TALLOC_FREE(result);
68 return NULL;
71 static void rpc_sock_read_done(struct tevent_req *subreq)
73 struct async_req *req =
74 tevent_req_callback_data(subreq, struct async_req);
75 struct rpc_sock_read_state *state = talloc_get_type_abort(
76 req->private_data, struct rpc_sock_read_state);
77 int err;
79 state->received = async_recv_recv(subreq, &err);
80 if (state->received == -1) {
81 async_req_nterror(req, map_nt_error_from_unix(err));
82 return;
84 async_req_done(req);
87 static NTSTATUS rpc_sock_read_recv(struct async_req *req, ssize_t *preceived)
89 struct rpc_sock_read_state *state = talloc_get_type_abort(
90 req->private_data, struct rpc_sock_read_state);
91 NTSTATUS status;
93 if (async_req_is_nterror(req, &status)) {
94 return status;
96 *preceived = state->received;
97 return NT_STATUS_OK;
100 struct rpc_sock_write_state {
101 ssize_t sent;
104 static void rpc_sock_write_done(struct tevent_req *subreq);
106 static struct async_req *rpc_sock_write_send(TALLOC_CTX *mem_ctx,
107 struct event_context *ev,
108 const uint8_t *data, size_t size,
109 void *priv)
111 struct rpc_transport_sock_state *sock_transp = talloc_get_type_abort(
112 priv, struct rpc_transport_sock_state);
113 struct async_req *result;
114 struct tevent_req *subreq;
115 struct rpc_sock_write_state *state;
117 if (!async_req_setup(mem_ctx, &result, &state,
118 struct rpc_sock_write_state)) {
119 return NULL;
121 subreq = async_send_send(state, ev, sock_transp->fd, data, size, 0);
122 if (subreq == NULL) {
123 goto fail;
125 tevent_req_set_callback(subreq, rpc_sock_write_done, result);
126 return result;
127 fail:
128 TALLOC_FREE(result);
129 return NULL;
132 static void rpc_sock_write_done(struct tevent_req *subreq)
134 struct async_req *req =
135 tevent_req_callback_data(subreq, struct async_req);
136 struct rpc_sock_write_state *state = talloc_get_type_abort(
137 req->private_data, struct rpc_sock_write_state);
138 int err;
140 state->sent = async_send_recv(subreq, &err);
141 if (state->sent == -1) {
142 async_req_nterror(req, map_nt_error_from_unix(err));
143 return;
145 async_req_done(req);
148 static NTSTATUS rpc_sock_write_recv(struct async_req *req, ssize_t *psent)
150 struct rpc_sock_write_state *state = talloc_get_type_abort(
151 req->private_data, struct rpc_sock_write_state);
152 NTSTATUS status;
154 if (async_req_is_nterror(req, &status)) {
155 return status;
157 *psent = state->sent;
158 return NT_STATUS_OK;
161 NTSTATUS rpc_transport_sock_init(TALLOC_CTX *mem_ctx, int fd,
162 struct rpc_cli_transport **presult)
164 struct rpc_cli_transport *result;
165 struct rpc_transport_sock_state *state;
167 result = talloc(mem_ctx, struct rpc_cli_transport);
168 if (result == NULL) {
169 return NT_STATUS_NO_MEMORY;
171 state = talloc(result, struct rpc_transport_sock_state);
172 if (state == NULL) {
173 TALLOC_FREE(result);
174 return NT_STATUS_NO_MEMORY;
176 result->priv = state;
178 state->fd = fd;
179 talloc_set_destructor(state, rpc_transport_sock_state_destructor);
181 result->trans_send = NULL;
182 result->trans_recv = NULL;
183 result->write_send = rpc_sock_write_send;
184 result->write_recv = rpc_sock_write_recv;
185 result->read_send = rpc_sock_read_send;
186 result->read_recv = rpc_sock_read_recv;
188 *presult = result;
189 return NT_STATUS_OK;