| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | Packet handling | 
|---|
| 4 | Copyright (C) Volker Lendecke 2007 | 
|---|
| 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 |  | 
|---|
| 22 | struct packet_context { | 
|---|
| 23 | int fd; | 
|---|
| 24 | struct data_blob in, out; | 
|---|
| 25 | }; | 
|---|
| 26 |  | 
|---|
| 27 | /* | 
|---|
| 28 | * Close the underlying fd | 
|---|
| 29 | */ | 
|---|
| 30 | static int packet_context_destructor(struct packet_context *ctx) | 
|---|
| 31 | { | 
|---|
| 32 | return close(ctx->fd); | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | /* | 
|---|
| 36 | * Initialize a packet context. The fd is given to the packet context, meaning | 
|---|
| 37 | * that it is automatically closed when the packet context is freed. | 
|---|
| 38 | */ | 
|---|
| 39 | struct packet_context *packet_init(TALLOC_CTX *mem_ctx, int fd) | 
|---|
| 40 | { | 
|---|
| 41 | struct packet_context *result; | 
|---|
| 42 |  | 
|---|
| 43 | if (!(result = TALLOC_ZERO_P(mem_ctx, struct packet_context))) { | 
|---|
| 44 | return NULL; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | result->fd = fd; | 
|---|
| 48 | talloc_set_destructor(result, packet_context_destructor); | 
|---|
| 49 | return result; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | /* | 
|---|
| 53 | * Pull data from the fd | 
|---|
| 54 | */ | 
|---|
| 55 | NTSTATUS packet_fd_read(struct packet_context *ctx) | 
|---|
| 56 | { | 
|---|
| 57 | int res, available; | 
|---|
| 58 | size_t new_size; | 
|---|
| 59 | uint8 *in; | 
|---|
| 60 |  | 
|---|
| 61 | res = ioctl(ctx->fd, FIONREAD, &available); | 
|---|
| 62 |  | 
|---|
| 63 | if (res == -1) { | 
|---|
| 64 | DEBUG(10, ("ioctl(FIONREAD) failed: %s\n", strerror(errno))); | 
|---|
| 65 | return map_nt_error_from_unix(errno); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | SMB_ASSERT(available >= 0); | 
|---|
| 69 |  | 
|---|
| 70 | if (available == 0) { | 
|---|
| 71 | return NT_STATUS_END_OF_FILE; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | new_size = ctx->in.length + available; | 
|---|
| 75 |  | 
|---|
| 76 | if (new_size < ctx->in.length) { | 
|---|
| 77 | DEBUG(0, ("integer wrap\n")); | 
|---|
| 78 | return NT_STATUS_NO_MEMORY; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | if (!(in = TALLOC_REALLOC_ARRAY(ctx, ctx->in.data, uint8, new_size))) { | 
|---|
| 82 | DEBUG(10, ("talloc failed\n")); | 
|---|
| 83 | return NT_STATUS_NO_MEMORY; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | ctx->in.data = in; | 
|---|
| 87 |  | 
|---|
| 88 | res = recv(ctx->fd, in + ctx->in.length, available, 0); | 
|---|
| 89 |  | 
|---|
| 90 | if (res < 0) { | 
|---|
| 91 | DEBUG(10, ("recv failed: %s\n", strerror(errno))); | 
|---|
| 92 | return map_nt_error_from_unix(errno); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | if (res == 0) { | 
|---|
| 96 | return NT_STATUS_END_OF_FILE; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | ctx->in.length += res; | 
|---|
| 100 |  | 
|---|
| 101 | return NT_STATUS_OK; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | NTSTATUS packet_fd_read_sync(struct packet_context *ctx) | 
|---|
| 105 | { | 
|---|
| 106 | int res; | 
|---|
| 107 | fd_set r_fds; | 
|---|
| 108 |  | 
|---|
| 109 | if (ctx->fd < 0 || ctx->fd >= FD_SETSIZE) { | 
|---|
| 110 | errno = EBADF; | 
|---|
| 111 | return map_nt_error_from_unix(errno); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | FD_ZERO(&r_fds); | 
|---|
| 115 | FD_SET(ctx->fd, &r_fds); | 
|---|
| 116 |  | 
|---|
| 117 | res = sys_select(ctx->fd+1, &r_fds, NULL, NULL, NULL); | 
|---|
| 118 |  | 
|---|
| 119 | if (res == -1) { | 
|---|
| 120 | DEBUG(10, ("select returned %s\n", strerror(errno))); | 
|---|
| 121 | return map_nt_error_from_unix(errno); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | return packet_fd_read(ctx); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | bool packet_handler(struct packet_context *ctx, | 
|---|
| 128 | bool (*full_req)(const struct data_blob *data, | 
|---|
| 129 | size_t *length, | 
|---|
| 130 | void *private_data), | 
|---|
| 131 | NTSTATUS (*callback)(const struct data_blob *data, | 
|---|
| 132 | void *private_data), | 
|---|
| 133 | void *private_data, | 
|---|
| 134 | NTSTATUS *status) | 
|---|
| 135 | { | 
|---|
| 136 | size_t length; | 
|---|
| 137 | struct data_blob data; | 
|---|
| 138 |  | 
|---|
| 139 | if (!full_req(&ctx->in, &length, private_data)) { | 
|---|
| 140 | return False; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | SMB_ASSERT(length <= ctx->in.length); | 
|---|
| 144 |  | 
|---|
| 145 | data = data_blob(ctx->in.data, length); | 
|---|
| 146 |  | 
|---|
| 147 | memmove(ctx->in.data, ctx->in.data + length, | 
|---|
| 148 | ctx->in.length - length); | 
|---|
| 149 | ctx->in.length -= length; | 
|---|
| 150 |  | 
|---|
| 151 | *status = callback(&data, private_data); | 
|---|
| 152 |  | 
|---|
| 153 | data_blob_free(&data); | 
|---|
| 154 |  | 
|---|
| 155 | return True; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | /* | 
|---|
| 159 | * How many bytes of outgoing data do we have pending? | 
|---|
| 160 | */ | 
|---|
| 161 | size_t packet_outgoing_bytes(struct packet_context *ctx) | 
|---|
| 162 | { | 
|---|
| 163 | return ctx->out.length; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | /* | 
|---|
| 167 | * Push data to the fd | 
|---|
| 168 | */ | 
|---|
| 169 | NTSTATUS packet_fd_write(struct packet_context *ctx) | 
|---|
| 170 | { | 
|---|
| 171 | ssize_t sent; | 
|---|
| 172 |  | 
|---|
| 173 | sent = send(ctx->fd, ctx->out.data, ctx->out.length, 0); | 
|---|
| 174 |  | 
|---|
| 175 | if (sent == -1) { | 
|---|
| 176 | DEBUG(0, ("send failed: %s\n", strerror(errno))); | 
|---|
| 177 | return map_nt_error_from_unix(errno); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | memmove(ctx->out.data, ctx->out.data + sent, | 
|---|
| 181 | ctx->out.length - sent); | 
|---|
| 182 | ctx->out.length -= sent; | 
|---|
| 183 |  | 
|---|
| 184 | return NT_STATUS_OK; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | /* | 
|---|
| 188 | * Sync flush all outgoing bytes | 
|---|
| 189 | */ | 
|---|
| 190 | NTSTATUS packet_flush(struct packet_context *ctx) | 
|---|
| 191 | { | 
|---|
| 192 | while (ctx->out.length != 0) { | 
|---|
| 193 | NTSTATUS status = packet_fd_write(ctx); | 
|---|
| 194 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 195 | return status; | 
|---|
| 196 | } | 
|---|
| 197 | } | 
|---|
| 198 | return NT_STATUS_OK; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | /* | 
|---|
| 202 | * Send a list of DATA_BLOBs | 
|---|
| 203 | * | 
|---|
| 204 | * Example:  packet_send(ctx, 2, data_blob_const(&size, sizeof(size)), | 
|---|
| 205 | *                       data_blob_const(buf, size)); | 
|---|
| 206 | */ | 
|---|
| 207 | NTSTATUS packet_send(struct packet_context *ctx, int num_blobs, ...) | 
|---|
| 208 | { | 
|---|
| 209 | va_list ap; | 
|---|
| 210 | int i; | 
|---|
| 211 | size_t len; | 
|---|
| 212 | uint8 *out; | 
|---|
| 213 |  | 
|---|
| 214 | len = ctx->out.length; | 
|---|
| 215 |  | 
|---|
| 216 | va_start(ap, num_blobs); | 
|---|
| 217 | for (i=0; i<num_blobs; i++) { | 
|---|
| 218 | size_t tmp; | 
|---|
| 219 | struct data_blob blob = va_arg(ap, struct data_blob); | 
|---|
| 220 |  | 
|---|
| 221 | tmp = len + blob.length; | 
|---|
| 222 | if (tmp < len) { | 
|---|
| 223 | DEBUG(0, ("integer overflow\n")); | 
|---|
| 224 | va_end(ap); | 
|---|
| 225 | return NT_STATUS_NO_MEMORY; | 
|---|
| 226 | } | 
|---|
| 227 | len = tmp; | 
|---|
| 228 | } | 
|---|
| 229 | va_end(ap); | 
|---|
| 230 |  | 
|---|
| 231 | if (len == 0) { | 
|---|
| 232 | return NT_STATUS_OK; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | if (!(out = TALLOC_REALLOC_ARRAY(ctx, ctx->out.data, uint8, len))) { | 
|---|
| 236 | DEBUG(0, ("talloc failed\n")); | 
|---|
| 237 | return NT_STATUS_NO_MEMORY; | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | ctx->out.data = out; | 
|---|
| 241 |  | 
|---|
| 242 | va_start(ap, num_blobs); | 
|---|
| 243 | for (i=0; i<num_blobs; i++) { | 
|---|
| 244 | struct data_blob blob = va_arg(ap, struct data_blob); | 
|---|
| 245 |  | 
|---|
| 246 | memcpy(ctx->out.data+ctx->out.length, blob.data, blob.length); | 
|---|
| 247 | ctx->out.length += blob.length; | 
|---|
| 248 | } | 
|---|
| 249 | va_end(ap); | 
|---|
| 250 |  | 
|---|
| 251 | SMB_ASSERT(ctx->out.length == len); | 
|---|
| 252 | return NT_STATUS_OK; | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | /* | 
|---|
| 256 | * Get the packet context's file descriptor | 
|---|
| 257 | */ | 
|---|
| 258 | int packet_get_fd(struct packet_context *ctx) | 
|---|
| 259 | { | 
|---|
| 260 | return ctx->fd; | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|