summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb_opipe.c
blob: a77535e2f7df622783fcd89c5ee2268db48449ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * This module provides the interface to NDR RPC.
 */

#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/ksynch.h>
#include <sys/stropts.h>
#include <sys/socket.h>
#include <sys/filio.h>
#include <smbsrv/smb_kproto.h>
#include <smbsrv/smb_xdr.h>

/*
 * Allocate a new opipe and return it, or NULL, in which case
 * the caller will report "internal error".
 */
static smb_opipe_t *
smb_opipe_alloc(smb_request_t *sr)
{
	smb_server_t	*sv = sr->sr_server;
	smb_opipe_t	*opipe;
	ksocket_t	sock;

	if (ksocket_socket(&sock, AF_UNIX, SOCK_STREAM, 0,
	    KSOCKET_SLEEP, sr->user_cr) != 0)
		return (NULL);

	opipe = kmem_cache_alloc(smb_cache_opipe, KM_SLEEP);

	bzero(opipe, sizeof (smb_opipe_t));
	mutex_init(&opipe->p_mutex, NULL, MUTEX_DEFAULT, NULL);
	cv_init(&opipe->p_cv, NULL, CV_DEFAULT, NULL);
	opipe->p_magic = SMB_OPIPE_MAGIC;
	opipe->p_server = sv;
	opipe->p_refcnt = 1;
	opipe->p_socket = sock;

	return (opipe);
}

/*
 * Destroy an opipe.  This is normally called from smb_ofile_delete
 * when the ofile has no more references and is about to be free'd.
 * This is also called here in error handling code paths, before
 * the opipe is installed under an ofile.
 */
void
smb_opipe_dealloc(smb_opipe_t *opipe)
{
	smb_server_t *sv;

	SMB_OPIPE_VALID(opipe);
	sv = opipe->p_server;
	SMB_SERVER_VALID(sv);

	/*
	 * This is called in the error path when opening,
	 * in which case we close the socket here.
	 */
	if (opipe->p_socket != NULL)
		(void) ksocket_close(opipe->p_socket, zone_kcred());

	opipe->p_magic = (uint32_t)~SMB_OPIPE_MAGIC;
	cv_destroy(&opipe->p_cv);
	mutex_destroy(&opipe->p_mutex);

	kmem_cache_free(smb_cache_opipe, opipe);
}

/*
 * Helper for open: build pipe name and connect.
 */
static int
smb_opipe_connect(smb_request_t *sr, smb_opipe_t *opipe)
{
	struct sockaddr_un saddr;
	smb_arg_open_t	*op = &sr->sr_open;
	const char *name;
	int rc;

	name = op->fqi.fq_path.pn_path;
	name += strspn(name, "\\");
	if (smb_strcasecmp(name, "PIPE", 4) == 0) {
		name += 4;
		name += strspn(name, "\\");
	}
	(void) strlcpy(opipe->p_name, name, SMB_OPIPE_MAXNAME);
	(void) smb_strlwr(opipe->p_name);

	bzero(&saddr, sizeof (saddr));
	saddr.sun_family = AF_UNIX;
	(void) snprintf(saddr.sun_path, sizeof (saddr.sun_path),
	    "%s/%s", SMB_PIPE_DIR, opipe->p_name);
	rc = ksocket_connect(opipe->p_socket, (struct sockaddr *)&saddr,
	    sizeof (saddr), sr->user_cr);

	return (rc);
}

/*
 * Helper for open: encode and send the user info.
 *
 * We send information about this client + user to the
 * pipe service so it can use it for access checks.
 * The service MAY deny the open based on this info,
 * (i.e. anonymous session trying to open a pipe that
 * requires authentication) in which case we will read
 * an error status from the service and return that.
 */
static void
smb_opipe_send_userinfo(smb_request_t *sr, smb_opipe_t *opipe,
    smb_error_t *errp)
{
	XDR xdrs;
	smb_netuserinfo_t nui;
	smb_pipehdr_t phdr;
	char *buf;
	uint32_t buflen;
	uint32_t status;
	size_t iocnt = 0;
	int rc;

	/*
	 * Any errors building the XDR message etc.
	 */
	errp->status = NT_STATUS_INTERNAL_ERROR;

	smb_user_netinfo_init(sr->uid_user, &nui);
	phdr.ph_magic = SMB_PIPE_HDR_MAGIC;
	phdr.ph_uilen = xdr_sizeof(smb_netuserinfo_xdr, &nui);

	buflen = sizeof (phdr) + phdr.ph_uilen;
	buf = kmem_alloc(buflen, KM_SLEEP);

	bcopy(&phdr, buf, sizeof (phdr));
	xdrmem_create(&xdrs, buf + sizeof (phdr),
	    buflen - (sizeof (phdr)), XDR_ENCODE);
	if (!smb_netuserinfo_xdr(&xdrs, &nui))
		goto out;

	/*
	 * If we fail sending the netuserinfo or recv'ing the
	 * status reponse, we have probably run into the limit
	 * on the number of open pipes.  That's this status:
	 */
	errp->status = NT_STATUS_PIPE_NOT_AVAILABLE;

	rc = ksocket_send(opipe->p_socket, buf, buflen, 0,
	    &iocnt, sr->user_cr);
	if (rc == 0 && iocnt != buflen)
		rc = EIO;
	if (rc != 0)
		goto out;

	rc = ksocket_recv(opipe->p_socket, &status, sizeof (status), 0,
	    &iocnt, sr->user_cr);
	if (rc != 0 || iocnt != sizeof (status))
		goto out;

	/*
	 * Return the status we read from the pipe service,
	 * normally NT_STATUS_SUCCESS, but could be something
	 * else like NT_STATUS_ACCESS_DENIED.
	 */
	errp->status = status;

out:
	xdr_destroy(&xdrs);
	kmem_free(buf, buflen);
	smb_user_netinfo_fini(&nui);
}

/*
 * smb_opipe_open
 *
 * Open an RPC named pipe. This routine should be called if
 * a file open is requested on a share of type STYPE_IPC.
 * If we recognize the pipe, we setup a new ofile.
 *
 * Returns 0 on success, Otherwise an NT status code.
 */
int
smb_opipe_open(smb_request_t *sr, uint32_t uniqid)
{
	smb_arg_open_t	*op = &sr->sr_open;
	smb_ofile_t *ofile;
	smb_opipe_t *opipe;
	smb_error_t err;

	opipe = smb_opipe_alloc(sr);
	if (opipe == NULL)
		return (NT_STATUS_INTERNAL_ERROR);

	if (smb_opipe_connect(sr, opipe) != 0) {
		smb_opipe_dealloc(opipe);
		return (NT_STATUS_OBJECT_NAME_NOT_FOUND);
	}

	smb_opipe_send_userinfo(sr, opipe, &err);
	if (err.status != 0) {
		smb_opipe_dealloc(opipe);
		return (err.status);
	}

	/*
	 * Note: If smb_ofile_open succeeds, the new ofile is
	 * in the FID lists can can be used by I/O requests.
	 */
	op->create_options = 0;
	op->pipe = opipe;
	ofile = smb_ofile_open(sr, NULL, op,
	    SMB_FTYPE_MESG_PIPE, uniqid, &err);
	op->pipe = NULL;
	if (ofile == NULL) {
		smb_opipe_dealloc(opipe);
		return (err.status);
	}

	/* An "up" pointer, for debug. */
	opipe->p_ofile = ofile;

	op->dsize = 0x01000;
	op->dattr = FILE_ATTRIBUTE_NORMAL;
	op->ftype = SMB_FTYPE_MESG_PIPE;
	op->action_taken = SMB_OACT_LOCK | SMB_OACT_OPENED; /* 0x8001 */
	op->devstate = SMB_PIPE_READMODE_MESSAGE
	    | SMB_PIPE_TYPE_MESSAGE
	    | SMB_PIPE_UNLIMITED_INSTANCES; /* 0x05ff */
	op->fileid = ofile->f_fid;

	sr->smb_fid = ofile->f_fid;
	sr->fid_ofile = ofile;

	return (NT_STATUS_SUCCESS);
}

/*
 * smb_opipe_close
 *
 * Called by smb_ofile_close for pipes.
 *
 * Note: ksocket_close may block while waiting for
 * any I/O threads with a hold to get out.
 */
void
smb_opipe_close(smb_ofile_t *of)
{
	smb_opipe_t *opipe;
	ksocket_t sock;

	ASSERT(of->f_state == SMB_OFILE_STATE_CLOSING);
	ASSERT(of->f_ftype == SMB_FTYPE_MESG_PIPE);
	opipe = of->f_pipe;
	SMB_OPIPE_VALID(opipe);

	mutex_enter(&opipe->p_mutex);
	sock = opipe->p_socket;
	opipe->p_socket = NULL;
	mutex_exit(&opipe->p_mutex);

	(void) ksocket_shutdown(sock, SHUT_RDWR, of->f_cr);
	(void) ksocket_close(sock, of->f_cr);
}

/*
 * smb_opipe_write
 *
 * Write RPC request data to the pipe.  The client should call smb_opipe_read
 * to complete the exchange and obtain the RPC response.
 *
 * Returns 0 on success or an errno on failure.
 */
int
smb_opipe_write(smb_request_t *sr, struct uio *uio)
{
	struct nmsghdr msghdr;
	smb_ofile_t *ofile;
	smb_opipe_t *opipe;
	ksocket_t sock;
	size_t sent = 0;
	int rc = 0;

	ofile = sr->fid_ofile;
	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
	opipe = ofile->f_pipe;
	SMB_OPIPE_VALID(opipe);

	mutex_enter(&opipe->p_mutex);
	sock = opipe->p_socket;
	if (sock != NULL)
		ksocket_hold(sock);
	mutex_exit(&opipe->p_mutex);
	if (sock == NULL)
		return (EBADF);

	bzero(&msghdr, sizeof (msghdr));
	msghdr.msg_iov = uio->uio_iov;
	msghdr.msg_iovlen = uio->uio_iovcnt;

	/*
	 * This should block until we've sent it all,
	 * or given up due to errors (pipe closed).
	 */
	while (uio->uio_resid > 0) {
		rc = ksocket_sendmsg(sock, &msghdr, 0, &sent, ofile->f_cr);
		if (rc != 0)
			break;
		uio->uio_resid -= sent;
	}

	ksocket_rele(sock);

	return (rc);
}

/*
 * smb_opipe_read
 *
 * This interface may be called from smb_opipe_transact (write, read)
 * or from smb_read / smb2_read to get the rest of an RPC response.
 * The response data (and length) are returned via the uio.
 */
int
smb_opipe_read(smb_request_t *sr, struct uio *uio)
{
	struct nmsghdr msghdr;
	smb_ofile_t *ofile;
	smb_opipe_t *opipe;
	ksocket_t sock;
	size_t recvcnt = 0;
	int rc;

	ofile = sr->fid_ofile;
	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
	opipe = ofile->f_pipe;
	SMB_OPIPE_VALID(opipe);

	mutex_enter(&opipe->p_mutex);
	sock = opipe->p_socket;
	if (sock != NULL)
		ksocket_hold(sock);
	mutex_exit(&opipe->p_mutex);
	if (sock == NULL)
		return (EBADF);

	bzero(&msghdr, sizeof (msghdr));
	msghdr.msg_iov = uio->uio_iov;
	msghdr.msg_iovlen = uio->uio_iovcnt;

	/*
	 * This should block only if there's no data.
	 * A single call to recvmsg does just that.
	 * (Intentionaly no recv loop here.)
	 */
	rc = ksocket_recvmsg(sock, &msghdr, 0,
	    &recvcnt, ofile->f_cr);
	if (rc != 0)
		goto out;

	if (recvcnt == 0) {
		/* Other side closed. */
		rc = EPIPE;
		goto out;
	}
	uio->uio_resid -= recvcnt;

out:
	ksocket_rele(sock);

	return (rc);
}

int
smb_opipe_get_nread(smb_request_t *sr, int *nread)
{
	smb_ofile_t *ofile;
	smb_opipe_t *opipe;
	ksocket_t sock;
	int rc, trval;

	ofile = sr->fid_ofile;
	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
	opipe = ofile->f_pipe;
	SMB_OPIPE_VALID(opipe);

	mutex_enter(&opipe->p_mutex);
	sock = opipe->p_socket;
	if (sock != NULL)
		ksocket_hold(sock);
	mutex_exit(&opipe->p_mutex);
	if (sock == NULL)
		return (EBADF);

	rc = ksocket_ioctl(sock, FIONREAD, (intptr_t)nread, &trval,
	    ofile->f_cr);

	ksocket_rele(sock);

	return (rc);
}