summaryrefslogtreecommitdiff
path: root/usr/src/lib/smbsrv/libmlsvc/common/mlsvc_client.c
blob: 20dea684427a2ebc14688f5f5845c433df1e380d (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
/*
 * 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 2020 Tintri by DDN, Inc. All rights reserved.
 */

/*
 * Client NDR RPC interface.
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <time.h>
#include <strings.h>
#include <assert.h>
#include <errno.h>
#include <thread.h>
#include <syslog.h>
#include <synch.h>

#include <libmlrpc/libmlrpc.h>
#include <netsmb/smbfs_api.h>

#include <smbsrv/libsmb.h>
#include <smbsrv/libmlsvc.h>
#include <libsmbrdr.h>
#include <mlsvc.h>


/*
 * This call must be made to initialize an RPC client structure and bind
 * to the remote service before any RPCs can be exchanged with that service.
 *
 * The mlsvc_handle_t is a wrapper that is used to associate an RPC handle
 * with the client context for an instance of the interface.  The handle
 * is zeroed to ensure that it doesn't look like a valid handle -
 * handle content is provided by the remove service.
 *
 * The client points to this top-level handle so that we know when to
 * unbind and teardown the connection.  As each handle is initialized it
 * will inherit a reference to the client context.
 *
 * Returns 0 or an NT_STATUS:		(failed in...)
 *
 *	NT_STATUS_BAD_NETWORK_PATH	(get server addr)
 *	NT_STATUS_NETWORK_ACCESS_DENIED	(connect, auth)
 *	NT_STATUS_BAD_NETWORK_NAME	(tcon)
 *	RPC_NT_SERVER_TOO_BUSY		(open pipe)
 *	RPC_NT_SERVER_UNAVAILABLE	(open pipe)
 *	NT_STATUS_ACCESS_DENIED		(open pipe)
 *	NT_STATUS_INVALID_PARAMETER	(rpc bind)
 *	NT_STATUS_INTERNAL_ERROR	(bad args etc)
 *	NT_STATUS_NO_MEMORY
 */
static DWORD
ndr_rpc_bind_common(mlsvc_handle_t *handle, char *server, char *domain,
    char *username, const char *service, ndr_auth_ctx_t *auth_ctx)
{
	struct smb_ctx		*ctx = NULL;
	ndr_service_t		*svc;
	DWORD			status;
	int			rc;

	if (handle == NULL || server == NULL || server[0] == '\0' ||
	    domain == NULL || username == NULL)
		return (NT_STATUS_INTERNAL_ERROR);

	/* In case the service was not registered... */
	if ((svc = ndr_svc_lookup_name(service)) == NULL)
		return (NT_STATUS_INTERNAL_ERROR);

	/*
	 * Some callers pass this when they want a NULL session.
	 * Todo: have callers pass an empty string for that.
	 */
	if (strcmp(username, MLSVC_ANON_USER) == 0)
		username = "";

	/*
	 * Setup smbfs library handle, authenticate, connect to
	 * the IPC$ share.  This will reuse an existing connection
	 * if the driver already has one for this combination of
	 * server, user, domain.  It may return any of:
	 *	NT_STATUS_BAD_NETWORK_PATH	(get server addr)
	 *	NT_STATUS_NETWORK_ACCESS_DENIED	(connect, auth)
	 *	NT_STATUS_BAD_NETWORK_NAME	(tcon)
	 */
	status = smbrdr_ctx_new(&ctx, server, domain, username);
	if (status != NT_STATUS_SUCCESS) {
		syslog(LOG_ERR, "ndr_rpc_bind: smbrdr_ctx_new"
		    "(Srv=%s Dom=%s User=%s), %s (0x%x)",
		    server, domain, username,
		    xlate_nt_status(status), status);
		/*
		 * If the error is one where changing to a new DC
		 * might help, try looking for a different DC.
		 */
		switch (status) {
		case NT_STATUS_BAD_NETWORK_PATH:
		case NT_STATUS_BAD_NETWORK_NAME:
			/* Look for a new DC */
			smb_ddiscover_bad_dc(server);
		default:
			break;
		}
		return (status);
	}

	/*
	 * Setup the RPC client handle.
	 */
	rc = mlrpc_clh_create(handle, ctx);
	if (rc != 0) {
		syslog(LOG_ERR, "ndr_rpc_bind: mlrpc_clh_create: rc=%d", rc);
		smbrdr_ctx_free(ctx);
		switch (rc) {
		case ENOMEM:
			return (NT_STATUS_NO_MEMORY);
		case EINVAL:
			return (NT_STATUS_INVALID_PARAMETER);
		default:
			return (NT_STATUS_INTERNAL_ERROR);
		}
	}

	/*
	 * Setup authentication, if requested.
	 */
	status = mlrpc_clh_set_auth(handle, auth_ctx);
	if (status != 0) {
		syslog(LOG_DEBUG, "ndr_rpc_bind: "
		    "mlrpc_clh_set_auth, %s (0x%x)",
		    xlate_nt_status(status), status);

		goto errout;
	}

	/*
	 * This does the pipe open and OtW RPC bind.
	 * Handles pipe open retries.
	 */
	status = mlrpc_clh_bind(handle, svc);
	if (status != 0) {
		syslog(LOG_DEBUG, "ndr_rpc_bind: "
		    "mlrpc_clh_bind, %s (0x%x)",
		    xlate_nt_status(status), status);
		switch (status) {
		case RPC_NT_SERVER_TOO_BUSY:
			/* Look for a new DC */
			smb_ddiscover_bad_dc(server);
			break;
		default:
			break;
		}

		goto errout;
	}

	return (NT_STATUS_SUCCESS);

errout:
	ctx = mlrpc_clh_free(handle);
	if (ctx != NULL) {
		smbrdr_ctx_free(ctx);
	}
	return (status);
}

DWORD
ndr_rpc_bind(mlsvc_handle_t *handle, char *server, char *domain,
    char *username, const char *service)
{
	return (ndr_rpc_bind_common(handle, server, domain, username, service,
	    NULL));
}

DWORD
ndr_rpc_bind_secure(mlsvc_handle_t *handle, char *server, char *domain,
    char *username, const char *service, ndr_auth_ctx_t *auth_ctx)
{
	return (ndr_rpc_bind_common(handle, server, domain, username, service,
	    auth_ctx));
}

/*
 * Unbind and close the pipe to an RPC service
 * and cleanup the smb_ctx.
 *
 * The heap may or may not be destroyed (see mlrpc_clh_free)
 */
void
ndr_rpc_unbind(mlsvc_handle_t *handle)
{
	struct smb_ctx *ctx;

	ctx = mlrpc_clh_free(handle);
	if (ctx != NULL)
		smbrdr_ctx_free(ctx);

	bzero(handle, sizeof (mlsvc_handle_t));
}

void
ndr_rpc_status(mlsvc_handle_t *handle, int opnum, DWORD status)
{
	ndr_service_t *svc;
	char *name = "NDR RPC";
	char *s = "unknown";

	switch (NT_SC_SEVERITY(status)) {
	case NT_STATUS_SEVERITY_SUCCESS:
		s = "success";
		break;
	case NT_STATUS_SEVERITY_INFORMATIONAL:
		s = "info";
		break;
	case NT_STATUS_SEVERITY_WARNING:
		s = "warning";
		break;
	case NT_STATUS_SEVERITY_ERROR:
		s = "error";
		break;
	}

	if (handle) {
		svc = handle->clnt->binding->service;
		name = svc->name;
	}

	smb_tracef("%s[0x%02x]: %s: %s (0x%08x)",
	    name, opnum, s, xlate_nt_status(status), status);
}