xref: /illumos-gate/usr/src/lib/smbsrv/libmlsvc/common/smbrdr_glue.c (revision b6805bf78d2bbbeeaea8909a05623587b42d58b3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25  */
26 
27 /*
28  * There used to be a "redirector" library, which has been replaced,
29  * leaving only the "glue" functions in this file that adapt this
30  * library to the interface provided by libsmbfs.
31  */
32 
33 #include <errno.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <priv.h>
38 
39 #include <netsmb/smbfs_api.h>
40 #include <smbsrv/libsmb.h>
41 #include <smbsrv/libmlsvc.h>
42 #include <libsmbrdr.h>
43 #include <mlsvc.h>
44 
45 #include <assert.h>
46 
47 /*
48  * mlsvc_disconnect
49  *
50  * Disconnects the session with given server.
51  * The new conection manager is smart enough
52  * so that we don't need this to do anything.
53  */
54 /* ARGSUSED */
55 void
56 smbrdr_disconnect(const char *server)
57 {
58 }
59 
60 
61 /*
62  * smbrdr_logon
63  *
64  * I'm not sure this really needs to do anything, but for now
65  * let's go ahead and authenticate here so this can return a
66  * status reflecting the outcome of authentication.
67  *
68  * If this successfully builds an smb_ctx, it just frees it.
69  * The driver retains sessions for a little while after the
70  * last reference goes away, so the session created here will
71  * usually still exist when the next call to smbrdr_ctx_new
72  * asks for this server+user (immediately after this returns),
73  * and only one session setup will go over the wire.
74  */
75 int
76 smbrdr_logon(char *srv, char *dom, char *user)
77 {
78 	struct smb_ctx *ctx;
79 	int err;
80 
81 	err = smbrdr_ctx_new(&ctx, srv, dom, user);
82 	if (err) {
83 		return (-1);
84 	}
85 	smb_ctx_free(ctx);
86 	return (AUTH_USER_GRANT);
87 }
88 
89 void
90 smbrdr_ctx_free(struct smb_ctx *ctx)
91 {
92 	smb_ctx_free(ctx);
93 }
94 
95 /*
96  * Setup a new SMB client context.
97  *
98  * Get the SMB server's configuration stuff and
99  * store it in the new client context object.
100  */
101 int
102 smbrdr_ctx_new(struct smb_ctx **ctx_p, char *server,
103 	char *domain, char *user)
104 {
105 	struct smb_ctx *ctx = NULL;
106 	uchar_t nthash[SMBAUTH_HASH_SZ];
107 	int64_t lmcl;
108 	int authflags, err;
109 
110 	assert(server != NULL);
111 	assert(domain != NULL);
112 	assert(user != NULL);
113 
114 	/* Some callers pass this when they want a NULL session. */
115 	if (strcmp(user, "IPC$") == 0)
116 		user = "";
117 
118 	if ((err = smb_ctx_alloc(&ctx)) != 0)
119 		return (err);
120 
121 	/*
122 	 * Set server, share, domain, user
123 	 * (in the ctx handle).
124 	 */
125 	(void) smb_ctx_setfullserver(ctx, server);
126 	(void) smb_ctx_setshare(ctx, "IPC$", USE_IPC);
127 	(void) smb_ctx_setdomain(ctx, domain, B_TRUE);
128 	(void) smb_ctx_setuser(ctx, user, B_TRUE);
129 
130 	/*
131 	 * Set auth. info (hash) and type.
132 	 */
133 	if (user[0] == '\0') {
134 		authflags = SMB_AT_ANON;
135 	} else {
136 		(void) smb_config_getnum(SMB_CI_LM_LEVEL, &lmcl);
137 		if (lmcl <= 2) {
138 			/* Send NTLM */
139 			authflags = SMB_AT_NTLM1;
140 		} else {
141 			/* Send NTLMv2 */
142 			authflags = SMB_AT_NTLM2;
143 		}
144 		smb_ipc_get_passwd(nthash, sizeof (nthash));
145 		(void) smb_ctx_setpwhash(ctx, nthash, NULL);
146 	}
147 	(void) smb_ctx_setauthflags(ctx, authflags);
148 
149 	/*
150 	 * Do lookup, connect, session setup, tree connect.
151 	 * Or find and reuse a session/tree, if one exists.
152 	 */
153 	if ((err = smb_ctx_resolve(ctx)) != 0)
154 		goto errout;
155 	if ((err = smb_ctx_get_ssn(ctx)) != 0)
156 		goto errout;
157 	if ((err = smb_ctx_get_tree(ctx)) != 0)
158 		goto errout;
159 
160 	/* Success! */
161 	*ctx_p = ctx;
162 	return (0);
163 
164 errout:
165 	smb_ctx_free(ctx);
166 	return (err);
167 }
168