17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $Id: smb_subr.c,v 1.27.108.1 2005/06/02 00:55:39 lindak Exp $
33 */
34
35 /*
36 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kmem.h>
42 #include <sys/proc.h>
43 #include <sys/lock.h>
44 #include <sys/socket.h>
45 #include <sys/isa_defs.h>
46 #include <sys/stream.h>
47 #include <sys/strsun.h>
48 #include <sys/sunddi.h>
49 #include <sys/cmn_err.h>
50 #include <sys/sdt.h>
51 #include <sys/priv.h>
52 #include <sys/u8_textprep.h>
53
54 #include <netsmb/smb_osdep.h>
55 #include <netsmb/smb.h>
56 #include <netsmb/smb_conn.h>
57 #include <netsmb/smb_rq.h>
58 #include <netsmb/smb_subr.h>
59
60 /*
61 * XXX:This conversion might not be fully MS-Compatible
62 * for calculating hashes. The output length may differ
63 * for some locales and needs to be handled from where
64 * the call is made.
65 */
66 int
67 smb_toupper(const char *inbuf, char *outbuf, size_t outlen)
68 {
69 int err = 0;
70 size_t inlen, inrem, outrem;
71
72 inrem = inlen = strlen(inbuf);
73 outrem = outlen;
74 (void) u8_textprep_str((char *)inbuf, &inrem, outbuf, &outrem,
75 U8_TEXTPREP_TOUPPER, U8_UNICODE_LATEST, &err);
76 /* inrem, outrem are bytes unused, remaining */
77 if (inrem) {
78 SMBSDEBUG("input %d remains: %s\n", (int)inrem, inbuf);
79 inlen -= inrem;
80 }
81 if (outrem) {
82 outlen -= outrem;
83 outbuf[outlen] = '\0';
84 }
85 if (outlen > inlen) {
86 SMBSDEBUG("outlen > inlen! (%d > %d)\n",
87 (int)outlen, (int)inlen);
88 /* Truncate to inlen here? */
89 }
90
91 return (err);
92 }
93
94 void
95 smb_credinit(struct smb_cred *scred, cred_t *cr)
96 {
97 /* cr arg is optional */
98 if (cr == NULL)
99 cr = ddi_get_cred();
100 if (is_system_labeled()) {
101 cr = crdup(cr);
102 (void) setpflags(NET_MAC_AWARE, 1, cr);
103 } else {
104 crhold(cr);
105 }
106 scred->scr_cred = cr;
107 }
108
109 void
110 smb_credrele(struct smb_cred *scred)
111 {
112 if (scred->scr_cred != NULL) {
113 crfree(scred->scr_cred);
114 scred->scr_cred = NULL;
115 }
116 }
117
118 /*
119 * Helper for the SMBERROR macro, etc.
120 * This is also a good place for a breakpoint
121 * or a dtrace probe, i.e. fbt:nsmb:smb_errmsg
122 */
123 void
124 smb_errmsg(int cel, const char *func_name, const char *fmt, ...)
125 {
126 va_list adx;
127 char buf[100];
128
129 va_start(adx, fmt);
130 if (cel == CE_CONT) {
131 /*
132 * This is one of our xxxDEBUG macros.
133 * Don't bother to log these, but just
134 * fire a dtrace probe with the message.
135 */
136 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
137 DTRACE_PROBE2(debugmsg2,
138 (char *), func_name,
139 (char *), buf);
140 } else {
141 /*
142 * This is one of our xxxERROR macros.
143 * Add a prefix to the fmt string,
144 * then let vcmn_err do the args.
145 */
146 (void) snprintf(buf, sizeof (buf), "?%s: %s", func_name, fmt);
147 DTRACE_PROBE3(debugmsg3,
148 (char *), func_name,
149 (char *), buf,
150 va_list, adx);
151 vcmn_err(cel, buf, adx);
152 }
153 va_end(adx);
154 }
155
156 #if 1 /* def SMB_SOCKETDATA_DEBUG */
157 void
158 m_dumpm(mblk_t *m)
159 {
173 #define EPROTO ECONNABORTED
174 #endif
175 #ifndef ELIBACC
176 #define ELIBACC ENOENT
177 #endif
178 #ifndef ENODATA
179 #define ENODATA EINVAL
180 #endif
181 #ifndef ENOTUNIQ
182 #define ENOTUNIQ EADDRINUSE
183 #endif
184 #ifndef ECOMM
185 #define ECOMM EIO
186 #endif
187 #ifndef ENOMEDIUM
188 #define ENOMEDIUM ENXIO
189 #endif
190 #ifndef ETIME
191 #define ETIME ETIMEDOUT
192 #endif
193
194 /*
195 * Log any un-handled NT or DOS errors we encounter.
196 * Make these log NOTICE in a debug build to ensure
197 * they get noticed during tests. In the field these
198 * are unimportant, so just fire a Dtrace probe.
199 */
200 static int unknown_err_logpri =
201 #ifdef DEBUG
202 CE_NOTE;
203 #else
204 CE_CONT;
205 #endif
206
207 typedef struct nt2errno {
208 unsigned int nterr;
209 int errno;
210 } nt2errno_t;
211
212 static const nt2errno_t nt2errno[] = {
213 /* Alphabetical order. */
214 {NT_STATUS_ACCESS_DENIED, EACCES},
215 {NT_STATUS_ACCESS_VIOLATION, EACCES},
216 {NT_STATUS_ACCOUNT_DISABLED, EACCES},
217 {NT_STATUS_ACCOUNT_EXPIRED, EACCES},
218 {NT_STATUS_ACCOUNT_LOCKED_OUT, EACCES},
219 {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
220 {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
221 {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
222 {NT_STATUS_BUFFER_TOO_SMALL, EMOREDATA},
223 {NT_STATUS_CANNOT_DELETE, EACCES},
224 {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
225 {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
226 {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
227 {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
228 {NT_STATUS_CONNECTION_RESET, ENETRESET},
229 {NT_STATUS_DELETE_PENDING, EACCES},
230 {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
231 {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
232 {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
233 {NT_STATUS_DISK_FULL, ENOSPC},
234 {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
235 {NT_STATUS_DUPLICATE_NAME, EINVAL},
236 {NT_STATUS_END_OF_FILE, ENODATA},
237 {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
238 {NT_STATUS_FILE_LOCK_CONFLICT, EAGAIN},
239 {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
240 {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
241 {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
242 {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
243 {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
244 {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
245 {NT_STATUS_INVALID_ACCOUNT_NAME, EACCES},
246 {NT_STATUS_INVALID_HANDLE, EBADF},
247 {NT_STATUS_INVALID_LEVEL, ENOTSUP},
248 {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
249 {NT_STATUS_INVALID_OWNER, EINVAL},
250 {NT_STATUS_INVALID_PARAMETER, EINVAL},
251 {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
252 {NT_STATUS_INVALID_PRIMARY_GROUP, EINVAL},
253 {NT_STATUS_INVALID_WORKSTATION, EACCES},
254 {NT_STATUS_IN_PAGE_ERROR, EFAULT},
255 {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
256 {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
257 {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
258 {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
259 {NT_STATUS_LOCK_NOT_GRANTED, EAGAIN},
260 {NT_STATUS_LOGIN_TIME_RESTRICTION, EACCES},
261 {NT_STATUS_LOGON_FAILURE, EACCES},
262 {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
263 {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
264 {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
265 {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
266 {NT_STATUS_NETWORK_BUSY, EBUSY},
267 {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
268 {NT_STATUS_NET_WRITE_FAULT, ECOMM},
269 {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE},
270 {NT_STATUS_NONE_MAPPED, EINVAL},
271 {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
272 {NT_STATUS_NOT_IMPLEMENTED, ENOTSUP},
273 {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
274 {NT_STATUS_NOT_SUPPORTED, ENOTSUP},
275 {NT_STATUS_NO_MEDIA, ENOMEDIUM},
276 {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM},
277 {NT_STATUS_NO_MEMORY, ENOMEM},
278 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
279 {NT_STATUS_NO_SUCH_FILE, ENOENT},
280 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
281 {NT_STATUS_OBJECT_NAME_INVALID, EINVAL},
282 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
283 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
284 {NT_STATUS_OBJECT_PATH_NOT_FOUND, ENOENT},
285 {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
286 {NT_STATUS_PASSWORD_EXPIRED, EACCES},
287 {NT_STATUS_PASSWORD_MUST_CHANGE, EACCES},
288 {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
289 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
290 {NT_STATUS_PIPE_BROKEN, EPIPE},
291 {NT_STATUS_PIPE_BUSY, EPIPE},
292 {NT_STATUS_PIPE_CONNECTED, EISCONN},
293 {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
294 {NT_STATUS_PIPE_NOT_AVAILABLE, EBUSY},
295 {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
296 {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
297 {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
298 {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
299 {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
300 {NT_STATUS_RANGE_NOT_LOCKED, EIO},
301 {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
302 {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
303 {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
304 {NT_STATUS_REQUEST_NOT_ACCEPTED, EACCES},
305 {NT_STATUS_RETRY, EAGAIN},
306 {NT_STATUS_SHARING_VIOLATION, EBUSY},
307 {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
308 {NT_STATUS_TOO_MANY_LINKS, EMLINK},
309 {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
310 {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
311 {NT_STATUS_UNSUCCESSFUL, EINVAL},
312 {NT_STATUS_WRONG_PASSWORD, EACCES},
313 {0, 0}
314 };
315
316 /*
317 * Table for converting NT STATUS values to DOS class/code.
318 * Rows ordered by integer value of last column (NT STATUS)
319 */
320 typedef struct nt2doserr {
321 unsigned short dclass;
322 unsigned short derr;
323 unsigned int nterr;
324 } nt2doserr_t;
325
326 static const nt2doserr_t nt2doserr[] = {
327 {ERRDOS, ERRgeneral, NT_STATUS_UNSUCCESSFUL},
328 {ERRDOS, ERRbadfunc, NT_STATUS_NOT_IMPLEMENTED},
329 {ERRDOS, ERROR_INVALID_PARAMETER, NT_STATUS_INVALID_INFO_CLASS},
330 {ERRDOS, ERROR_BAD_LENGTH, NT_STATUS_INFO_LENGTH_MISMATCH},
331 {ERRHRD, ERRgeneral, NT_STATUS_ACCESS_VIOLATION},
332 {ERRHRD, ERRgeneral, NT_STATUS_IN_PAGE_ERROR},
576 {ERRDOS, ERRbadpath, NT_STATUS_REDIRECTOR_NOT_STARTED},
577 {ERRHRD, ERRgeneral, NT_STATUS_REDIRECTOR_STARTED},
578 {ERRHRD, ERRgeneral, NT_STATUS_STACK_OVERFLOW},
579 {ERRHRD, ERRgeneral, NT_STATUS_NO_SUCH_PACKAGE},
580 {ERRHRD, ERRgeneral, NT_STATUS_BAD_FUNCTION_TABLE},
581 {ERRDOS, ERROR_ENVVAR_NOT_FOUND, NT_STATUS_VARIABLE_NOT_FOUND},
582 {ERRDOS, ERROR_DIR_NOT_EMPTY, NT_STATUS_DIRECTORY_NOT_EMPTY},
583 {ERRHRD, ERRgeneral, NT_STATUS_FILE_CORRUPT_ERROR},
584 {ERRDOS, ERROR_DIRECTORY, NT_STATUS_NOT_A_DIRECTORY},
585 {ERRHRD, ERRgeneral, NT_STATUS_BAD_LOGON_SESSION_STATE},
586 {ERRHRD, ERRgeneral, NT_STATUS_LOGON_SESSION_COLLISION},
587 {ERRDOS, ERROR_FILENAME_EXCED_RANGE, NT_STATUS_NAME_TOO_LONG},
588 {ERRDOS, NERR_OpenFiles, NT_STATUS_FILES_OPEN},
589 {ERRDOS, NERR_DevInUse, NT_STATUS_CONNECTION_IN_USE},
590 {ERRHRD, ERRgeneral, NT_STATUS_MESSAGE_NOT_FOUND},
591 {ERRDOS, ERRnoaccess, NT_STATUS_PROCESS_IS_TERMINATING},
592 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_LOGON_TYPE},
593 {ERRHRD, ERRgeneral, NT_STATUS_NO_GUID_TRANSLATION},
594 {ERRHRD, ERRgeneral, NT_STATUS_CANNOT_IMPERSONATE},
595 {ERRHRD, ERRgeneral, NT_STATUS_IMAGE_ALREADY_LOADED},
596 {ERRHRD, ERRgeneral, NT_STATUS_ABIOS_NOT_PRESENT},
597 {ERRHRD, ERRgeneral, NT_STATUS_ABIOS_LID_NOT_EXIST},
598 {ERRHRD, ERRgeneral, NT_STATUS_ABIOS_LID_ALREADY_OWNED},
599 {ERRHRD, ERRgeneral, NT_STATUS_ABIOS_NOT_LID_OWNER},
600 {ERRHRD, ERRgeneral, NT_STATUS_ABIOS_INVALID_COMMAND},
601 {ERRHRD, ERRgeneral, NT_STATUS_ABIOS_INVALID_LID},
602 {ERRHRD, ERRgeneral, NT_STATUS_ABIOS_SELECTOR_NOT_AVAILABLE},
603 {ERRHRD, ERRgeneral, NT_STATUS_ABIOS_INVALID_SELECTOR},
604 {ERRHRD, ERRgeneral, NT_STATUS_NO_LDT},
605 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_LDT_SIZE},
606 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_LDT_OFFSET},
607 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_LDT_DESCRIPTOR},
608 {ERRDOS, ERROR_BAD_EXE_FORMAT, NT_STATUS_INVALID_IMAGE_NE_FORMAT},
609 {ERRHRD, ERRgeneral, NT_STATUS_RXACT_INVALID_STATE},
610 {ERRHRD, ERRgeneral, NT_STATUS_RXACT_COMMIT_FAILURE},
611 {ERRHRD, ERRgeneral, NT_STATUS_MAPPED_FILE_SIZE_ZERO},
612 {ERRDOS, ERRnofids, NT_STATUS_TOO_MANY_OPENED_FILES},
613 {ERRHRD, ERRgeneral, NT_STATUS_CANCELLED},
614 {ERRDOS, ERRnoaccess, NT_STATUS_CANNOT_DELETE},
615 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_COMPUTER_NAME},
616 {ERRDOS, ERRnoaccess, NT_STATUS_FILE_DELETED},
617 {ERRHRD, ERRgeneral, NT_STATUS_SPECIAL_ACCOUNT},
618 {ERRHRD, ERRgeneral, NT_STATUS_SPECIAL_GROUP},
619 {ERRHRD, ERRgeneral, NT_STATUS_SPECIAL_USER},
620 {ERRHRD, ERRgeneral, NT_STATUS_MEMBERS_PRIMARY_GROUP},
621 {ERRDOS, ERRbadfid, NT_STATUS_FILE_CLOSED},
622 {ERRHRD, ERRgeneral, NT_STATUS_TOO_MANY_THREADS},
623 {ERRHRD, ERRgeneral, NT_STATUS_THREAD_NOT_IN_PROCESS},
851 switch (NT_SC_SEVERITY(nterr)) {
852 case NT_STATUS_SEVERITY_SUCCESS:
853 case NT_STATUS_SEVERITY_INFORMATIONAL:
854 return (0);
855 }
856
857 /* first try direct map to unix */
858 for (nt2e = nt2errno; nt2e->errno; nt2e++)
859 if (nt2e->nterr == nterr)
860 return (nt2e->errno);
861 smb_errmsg(unknown_err_logpri, "smb_maperr32",
862 "No direct map for 32 bit server error (0x%x)\n", nterr);
863
864 /* ok, then try mapping to dos to unix */
865 for (nt2d = nt2doserr; nt2d->nterr; nt2d++)
866 if (nt2d->nterr == nterr)
867 return (smb_maperror(nt2d->dclass, nt2d->derr));
868 return (EIO);
869 }
870
871
872 int
873 smb_maperror(int eclass, int eno)
874 {
875 if (eclass == 0 && eno == 0)
876 return (0);
877 switch (eclass) {
878 case ERRDOS:
879 switch (eno) {
880 case ERROR_INVALID_LEVEL:
881 return (ENOTSUP);
882 case ERRbadfunc:
883 case ERRbadenv:
884 case ERRbadformat:
885 case ERRremcd:
886 case ERRrmuns:
887 return (EINVAL);
888 case ERRbadfile:
889 case ERRbadpath:
890 case ERROR_BAD_DEV_TYPE:
891 case ERROR_BAD_NET_NAME:
979 return (EBADRPC);
980 case ERRbadshare:
981 return (ETXTBSY);
982 case ERRlock:
983 return (EAGAIN);
984 case ERRdiskfull:
985 return (EFBIG);
986 case ERRnotready:
987 case ERRbadcmd:
988 case ERRdata:
989 case ERRgeneral:
990 return (EIO);
991 }
992 }
993
994 smb_errmsg(unknown_err_logpri, "smb_maperror",
995 "Unknown DOS error %d/%d\n", eclass, eno);
996 return (EIO);
997 }
998
999 #if defined(NOICONVSUPPORT) || defined(lint)
1000 extern int iconv_conv(void *handle, const char **inbuf,
1001 size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
1002 #endif
1003
1004 #define SMALL_CONV 256
1005
1006 /*ARGSUSED*/
1007 int
1008 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
1009 int size, int caseopt, int *lenp)
1010 {
1011 uint16_t convbuf[SMALL_CONV];
1012 uint16_t *cbuf;
1013 size_t cbufalloc, inlen, outlen;
1014 int error;
1015
1016 if (size <= 0)
1017 return (0);
1018
1019 /*
1020 * Handle the easy case (non-unicode).
1021 * XXX: Technically, we should convert
1022 * the string to OEM codeset first...
1023 * Modern servers all use Unicode, so
1024 * this is good enough.
1025 */
1062
1063 return (error);
1064 }
1065
1066 int
1067 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
1068 int caseopt)
1069 {
1070 int error, len;
1071
1072 /*
1073 * Let smb_put_dmem put both the string
1074 * and the terminating null.
1075 */
1076 len = strlen(src) + 1;
1077 error = smb_put_dmem(mbp, vcp, src, len, caseopt, NULL);
1078 if (error)
1079 return (error);
1080
1081 return (error);
1082 }
|
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $Id: smb_subr.c,v 1.27.108.1 2005/06/02 00:55:39 lindak Exp $
33 */
34
35 /*
36 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
37 * Portions Copyright (C) 2001 - 2013 Apple Inc. All rights reserved.
38 * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kmem.h>
44 #include <sys/proc.h>
45 #include <sys/lock.h>
46 #include <sys/socket.h>
47 #include <sys/isa_defs.h>
48 #include <sys/stream.h>
49 #include <sys/strsun.h>
50 #include <sys/sunddi.h>
51 #include <sys/cmn_err.h>
52 #include <sys/sdt.h>
53 #include <sys/priv.h>
54 #include <sys/u8_textprep.h>
55
56 #include <netsmb/smb_osdep.h>
57 #include <netsmb/smb.h>
58 #include <netsmb/smb_conn.h>
59 #include <netsmb/smb_rq.h>
60 #include <netsmb/smb_subr.h>
61
62 void
63 smb_credinit(struct smb_cred *scred, cred_t *cr)
64 {
65 /* cr arg is optional */
66 if (cr == NULL)
67 cr = ddi_get_cred();
68 #ifdef _KERNEL
69 if (is_system_labeled()) {
70 cr = crdup(cr);
71 (void) setpflags(NET_MAC_AWARE, 1, cr);
72 } else
73 #endif
74 {
75 crhold(cr);
76 }
77 scred->scr_cred = cr;
78 }
79
80 void
81 smb_credrele(struct smb_cred *scred)
82 {
83 if (scred->scr_cred != NULL) {
84 crfree(scred->scr_cred);
85 scred->scr_cred = NULL;
86 }
87 }
88
89 #ifndef _KERNEL
90 /* ARGSUSED */
91 void
92 smb_debugmsg(const char *func, char *msg)
93 {
94 }
95 #endif /* _KERNEL */
96
97 /*
98 * Helper for the SMBERROR macro, etc.
99 * This is also a good place for a breakpoint
100 * or a dtrace probe, i.e. fbt:nsmb:smb_errmsg
101 */
102 void
103 smb_errmsg(int cel, const char *func_name, const char *fmt, ...)
104 {
105 va_list adx;
106 char buf[100];
107
108 va_start(adx, fmt);
109 if (cel == CE_CONT) {
110 /*
111 * This is one of our xxxDEBUG macros.
112 * Don't bother to log these, but just
113 * fire a dtrace probe with the message.
114 */
115 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
116 DTRACE_PROBE2(debugmsg2,
117 (char *), func_name,
118 (char *), buf);
119 #ifndef _KERNEL
120 smb_debugmsg(func_name, buf);
121 #endif
122 } else {
123 /*
124 * This is one of our xxxERROR macros.
125 * Add a prefix to the fmt string,
126 * then let vcmn_err do the args.
127 */
128 (void) snprintf(buf, sizeof (buf), "?%s: %s", func_name, fmt);
129 DTRACE_PROBE3(debugmsg3,
130 (char *), func_name,
131 (char *), buf,
132 va_list, adx);
133 vcmn_err(cel, buf, adx);
134 }
135 va_end(adx);
136 }
137
138 #if 1 /* def SMB_SOCKETDATA_DEBUG */
139 void
140 m_dumpm(mblk_t *m)
141 {
155 #define EPROTO ECONNABORTED
156 #endif
157 #ifndef ELIBACC
158 #define ELIBACC ENOENT
159 #endif
160 #ifndef ENODATA
161 #define ENODATA EINVAL
162 #endif
163 #ifndef ENOTUNIQ
164 #define ENOTUNIQ EADDRINUSE
165 #endif
166 #ifndef ECOMM
167 #define ECOMM EIO
168 #endif
169 #ifndef ENOMEDIUM
170 #define ENOMEDIUM ENXIO
171 #endif
172 #ifndef ETIME
173 #define ETIME ETIMEDOUT
174 #endif
175 #ifndef EMOREDATA
176 #define EMOREDATA (0x7fff)
177 #endif
178
179 /*
180 * Log any un-handled NT or DOS errors we encounter.
181 * Make these log NOTICE in a debug build to ensure
182 * they get noticed during tests. In the field these
183 * are unimportant, so just fire a Dtrace probe.
184 */
185 static int unknown_err_logpri =
186 #ifdef DEBUG
187 CE_NOTE;
188 #else
189 CE_CONT;
190 #endif
191
192 typedef struct nt2errno {
193 unsigned int nterr;
194 int errno;
195 } nt2errno_t;
196
197 static const nt2errno_t nt2errno[] = {
198 /* Alphabetical order. */
199 {NT_STATUS_ACCESS_DENIED, EACCES},
200 {NT_STATUS_ACCESS_VIOLATION, EACCES},
201 {NT_STATUS_ACCOUNT_DISABLED, EACCES},
202 {NT_STATUS_ACCOUNT_EXPIRED, EACCES},
203 {NT_STATUS_ACCOUNT_LOCKED_OUT, EACCES},
204 {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
205 {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
206 {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
207 {NT_STATUS_BAD_NETWORK_PATH, ENOENT},
208 {NT_STATUS_BUFFER_TOO_SMALL, E2BIG},
209 {NT_STATUS_CANCELLED, ECANCELED},
210 {NT_STATUS_CANNOT_DELETE, EACCES},
211 {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
212 {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
213 {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
214 {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
215 {NT_STATUS_CONNECTION_RESET, ENETRESET},
216 {NT_STATUS_DELETE_PENDING, EACCES},
217 {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
218 {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
219 {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
220 {NT_STATUS_DISK_FULL, ENOSPC},
221 {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
222 {NT_STATUS_DUPLICATE_NAME, EINVAL},
223 {NT_STATUS_EAS_NOT_SUPPORTED, ENOTSUP},
224 {NT_STATUS_EA_TOO_LARGE, E2BIG},
225 {NT_STATUS_END_OF_FILE, ENODATA},
226 {NT_STATUS_FILE_CLOSED, EBADF},
227 {NT_STATUS_FILE_DELETED, ENOENT},
228 {NT_STATUS_FILE_INVALID, EIO},
229 {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
230 {NT_STATUS_FILE_LOCK_CONFLICT, EAGAIN},
231 {NT_STATUS_FILE_RENAMED, ENOENT},
232 {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
233 {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
234 {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
235 {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
236 {NT_STATUS_ILL_FORMED_PASSWORD, EAUTH},
237 {NT_STATUS_INFO_LENGTH_MISMATCH, EINVAL},
238 {NT_STATUS_INSUFFICIENT_RESOURCES, EAGAIN},
239 {NT_STATUS_INSUFF_SERVER_RESOURCES, EAGAIN},
240 {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
241 {NT_STATUS_INVALID_ACCOUNT_NAME, EAUTH},
242 {NT_STATUS_INVALID_BUFFER_SIZE, EIO},
243 {NT_STATUS_INVALID_DEVICE_REQUEST, EINVAL},
244 {NT_STATUS_INVALID_HANDLE, EBADF},
245 {NT_STATUS_INVALID_INFO_CLASS, EINVAL},
246 {NT_STATUS_INVALID_LEVEL, ENOTSUP},
247 {NT_STATUS_INVALID_LOCK_SEQUENCE, EINVAL},
248 {NT_STATUS_INVALID_LOGON_HOURS, EAUTH},
249 {NT_STATUS_INVALID_OWNER, EINVAL},
250 {NT_STATUS_INVALID_PARAMETER, EINVAL},
251 {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
252 {NT_STATUS_INVALID_PRIMARY_GROUP, EINVAL},
253 {NT_STATUS_INVALID_WORKSTATION, EACCES},
254 {NT_STATUS_IN_PAGE_ERROR, EFAULT},
255 {NT_STATUS_IO_DEVICE_ERROR, EIO},
256 {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
257 {NT_STATUS_IP_ADDRESS_CONFLICT1, EADDRINUSE},
258 {NT_STATUS_IP_ADDRESS_CONFLICT2, EADDRINUSE},
259 {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
260 {NT_STATUS_LOCK_NOT_GRANTED, EAGAIN},
261 {NT_STATUS_LOGIN_TIME_RESTRICTION, EAUTH},
262 {NT_STATUS_LOGON_FAILURE, EAUTH},
263 {NT_STATUS_LOGON_TYPE_NOT_GRANTED, EAUTH},
264 {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
265 {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
266 {NT_STATUS_MORE_PROCESSING_REQUIRED, EINPROGRESS},
267 {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
268 {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
269 {NT_STATUS_NETWORK_BUSY, EBUSY},
270 {NT_STATUS_NETWORK_NAME_DELETED, ENOENT},
271 {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
272 {NT_STATUS_NET_WRITE_FAULT, ECOMM},
273 {NT_STATUS_NONEXISTENT_EA_ENTRY, ENOENT},
274 {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE},
275 {NT_STATUS_NONE_MAPPED, EINVAL},
276 {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
277 {NT_STATUS_NOT_FOUND, ENOENT},
278 {NT_STATUS_NOT_IMPLEMENTED, ENOTSUP},
279 {NT_STATUS_NOT_LOCKED, ENOLCK},
280 {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
281 {NT_STATUS_NOT_SUPPORTED, ENOTSUP},
282 {NT_STATUS_NO_EAS_ON_FILE, ENOENT},
283 {NT_STATUS_NO_LOGON_SERVERS, EAUTH},
284 {NT_STATUS_NO_MEDIA, ENOMEDIUM},
285 {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM},
286 {NT_STATUS_NO_MEMORY, ENOMEM},
287 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
288 {NT_STATUS_NO_SUCH_FILE, ENOENT},
289 {NT_STATUS_NO_SUCH_LOGON_SESSION, EAUTH},
290 {NT_STATUS_NO_SUCH_USER, EAUTH},
291 {NT_STATUS_NO_TRUST_LSA_SECRET, EAUTH},
292 {NT_STATUS_NO_TRUST_SAM_ACCOUNT, EAUTH},
293 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
294 {NT_STATUS_OBJECT_NAME_INVALID, EINVAL},
295 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
296 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
297 {NT_STATUS_OBJECT_PATH_NOT_FOUND, ENOENT},
298 {NT_STATUS_OBJECT_PATH_SYNTAX_BAD, EINVAL},
299 {NT_STATUS_OBJECT_TYPE_MISMATCH, EBADF},
300 {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
301 {NT_STATUS_PASSWORD_EXPIRED, EAUTH},
302 {NT_STATUS_PASSWORD_MUST_CHANGE, EAUTH},
303 {NT_STATUS_PASSWORD_RESTRICTION, EAUTH},
304 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
305 {NT_STATUS_PIPE_BROKEN, EPIPE},
306 {NT_STATUS_PIPE_BUSY, EPIPE},
307 {NT_STATUS_PIPE_CONNECTED, EISCONN},
308 {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
309 {NT_STATUS_PIPE_NOT_AVAILABLE, EBUSY},
310 {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
311 {NT_STATUS_PORT_DISCONNECTED, EBADF},
312 {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
313 {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
314 {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
315 {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
316 {NT_STATUS_RANGE_NOT_LOCKED, EAGAIN}, /* like F_SETLK */
317 {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
318 {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
319 {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
320 {NT_STATUS_REQUEST_NOT_ACCEPTED, EACCES},
321 {NT_STATUS_RETRY, EAGAIN},
322 {NT_STATUS_SHARING_VIOLATION, EBUSY},
323 {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
324 {NT_STATUS_TOO_MANY_LINKS, EMLINK},
325 {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
326 {NT_STATUS_TRUSTED_DOMAIN_FAILURE, EAUTH},
327 {NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE, EAUTH},
328 {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
329 {NT_STATUS_UNSUCCESSFUL, EINVAL},
330 {NT_STATUS_WRONG_PASSWORD, EAUTH},
331 {0, 0}
332 };
333
334 /*
335 * Table for converting NT STATUS values to DOS class/code.
336 * Rows ordered by integer value of last column (NT STATUS)
337 */
338 typedef struct nt2doserr {
339 unsigned short dclass;
340 unsigned short derr;
341 unsigned int nterr;
342 } nt2doserr_t;
343
344 static const nt2doserr_t nt2doserr[] = {
345 {ERRDOS, ERRgeneral, NT_STATUS_UNSUCCESSFUL},
346 {ERRDOS, ERRbadfunc, NT_STATUS_NOT_IMPLEMENTED},
347 {ERRDOS, ERROR_INVALID_PARAMETER, NT_STATUS_INVALID_INFO_CLASS},
348 {ERRDOS, ERROR_BAD_LENGTH, NT_STATUS_INFO_LENGTH_MISMATCH},
349 {ERRHRD, ERRgeneral, NT_STATUS_ACCESS_VIOLATION},
350 {ERRHRD, ERRgeneral, NT_STATUS_IN_PAGE_ERROR},
594 {ERRDOS, ERRbadpath, NT_STATUS_REDIRECTOR_NOT_STARTED},
595 {ERRHRD, ERRgeneral, NT_STATUS_REDIRECTOR_STARTED},
596 {ERRHRD, ERRgeneral, NT_STATUS_STACK_OVERFLOW},
597 {ERRHRD, ERRgeneral, NT_STATUS_NO_SUCH_PACKAGE},
598 {ERRHRD, ERRgeneral, NT_STATUS_BAD_FUNCTION_TABLE},
599 {ERRDOS, ERROR_ENVVAR_NOT_FOUND, NT_STATUS_VARIABLE_NOT_FOUND},
600 {ERRDOS, ERROR_DIR_NOT_EMPTY, NT_STATUS_DIRECTORY_NOT_EMPTY},
601 {ERRHRD, ERRgeneral, NT_STATUS_FILE_CORRUPT_ERROR},
602 {ERRDOS, ERROR_DIRECTORY, NT_STATUS_NOT_A_DIRECTORY},
603 {ERRHRD, ERRgeneral, NT_STATUS_BAD_LOGON_SESSION_STATE},
604 {ERRHRD, ERRgeneral, NT_STATUS_LOGON_SESSION_COLLISION},
605 {ERRDOS, ERROR_FILENAME_EXCED_RANGE, NT_STATUS_NAME_TOO_LONG},
606 {ERRDOS, NERR_OpenFiles, NT_STATUS_FILES_OPEN},
607 {ERRDOS, NERR_DevInUse, NT_STATUS_CONNECTION_IN_USE},
608 {ERRHRD, ERRgeneral, NT_STATUS_MESSAGE_NOT_FOUND},
609 {ERRDOS, ERRnoaccess, NT_STATUS_PROCESS_IS_TERMINATING},
610 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_LOGON_TYPE},
611 {ERRHRD, ERRgeneral, NT_STATUS_NO_GUID_TRANSLATION},
612 {ERRHRD, ERRgeneral, NT_STATUS_CANNOT_IMPERSONATE},
613 {ERRHRD, ERRgeneral, NT_STATUS_IMAGE_ALREADY_LOADED},
614 {ERRHRD, ERRgeneral, NT_STATUS_NO_LDT},
615 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_LDT_SIZE},
616 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_LDT_OFFSET},
617 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_LDT_DESCRIPTOR},
618 {ERRDOS, ERROR_BAD_EXE_FORMAT, NT_STATUS_INVALID_IMAGE_NE_FORMAT},
619 {ERRHRD, ERRgeneral, NT_STATUS_RXACT_INVALID_STATE},
620 {ERRHRD, ERRgeneral, NT_STATUS_RXACT_COMMIT_FAILURE},
621 {ERRHRD, ERRgeneral, NT_STATUS_MAPPED_FILE_SIZE_ZERO},
622 {ERRDOS, ERRnofids, NT_STATUS_TOO_MANY_OPENED_FILES},
623 {ERRHRD, ERRgeneral, NT_STATUS_CANCELLED},
624 {ERRDOS, ERRnoaccess, NT_STATUS_CANNOT_DELETE},
625 {ERRHRD, ERRgeneral, NT_STATUS_INVALID_COMPUTER_NAME},
626 {ERRDOS, ERRnoaccess, NT_STATUS_FILE_DELETED},
627 {ERRHRD, ERRgeneral, NT_STATUS_SPECIAL_ACCOUNT},
628 {ERRHRD, ERRgeneral, NT_STATUS_SPECIAL_GROUP},
629 {ERRHRD, ERRgeneral, NT_STATUS_SPECIAL_USER},
630 {ERRHRD, ERRgeneral, NT_STATUS_MEMBERS_PRIMARY_GROUP},
631 {ERRDOS, ERRbadfid, NT_STATUS_FILE_CLOSED},
632 {ERRHRD, ERRgeneral, NT_STATUS_TOO_MANY_THREADS},
633 {ERRHRD, ERRgeneral, NT_STATUS_THREAD_NOT_IN_PROCESS},
861 switch (NT_SC_SEVERITY(nterr)) {
862 case NT_STATUS_SEVERITY_SUCCESS:
863 case NT_STATUS_SEVERITY_INFORMATIONAL:
864 return (0);
865 }
866
867 /* first try direct map to unix */
868 for (nt2e = nt2errno; nt2e->errno; nt2e++)
869 if (nt2e->nterr == nterr)
870 return (nt2e->errno);
871 smb_errmsg(unknown_err_logpri, "smb_maperr32",
872 "No direct map for 32 bit server error (0x%x)\n", nterr);
873
874 /* ok, then try mapping to dos to unix */
875 for (nt2d = nt2doserr; nt2d->nterr; nt2d++)
876 if (nt2d->nterr == nterr)
877 return (smb_maperror(nt2d->dclass, nt2d->derr));
878 return (EIO);
879 }
880
881 uint_t
882 smb_doserr2status(int dclass, int derr)
883 {
884 const nt2doserr_t *nt2d;
885
886 if (dclass == 0 && derr == 0)
887 return (0);
888
889 for (nt2d = nt2doserr; nt2d->nterr; nt2d++)
890 if (nt2d->dclass == dclass && nt2d->derr == derr)
891 return (nt2d->nterr);
892 return (NT_STATUS_UNSUCCESSFUL);
893 }
894
895 int
896 smb_maperror(int eclass, int eno)
897 {
898 if (eclass == 0 && eno == 0)
899 return (0);
900 switch (eclass) {
901 case ERRDOS:
902 switch (eno) {
903 case ERROR_INVALID_LEVEL:
904 return (ENOTSUP);
905 case ERRbadfunc:
906 case ERRbadenv:
907 case ERRbadformat:
908 case ERRremcd:
909 case ERRrmuns:
910 return (EINVAL);
911 case ERRbadfile:
912 case ERRbadpath:
913 case ERROR_BAD_DEV_TYPE:
914 case ERROR_BAD_NET_NAME:
1002 return (EBADRPC);
1003 case ERRbadshare:
1004 return (ETXTBSY);
1005 case ERRlock:
1006 return (EAGAIN);
1007 case ERRdiskfull:
1008 return (EFBIG);
1009 case ERRnotready:
1010 case ERRbadcmd:
1011 case ERRdata:
1012 case ERRgeneral:
1013 return (EIO);
1014 }
1015 }
1016
1017 smb_errmsg(unknown_err_logpri, "smb_maperror",
1018 "Unknown DOS error %d/%d\n", eclass, eno);
1019 return (EIO);
1020 }
1021
1022 #define SMALL_CONV 256
1023
1024 /*
1025 * Decode an SMB OTW string (Unicode or OEM chars)
1026 * converting to UTF-8 in the output buffer.
1027 * outlen is in/out (max size on input)
1028 * insize is the wire size (2 * chars if unicode)
1029 * The output string is null terminated.
1030 * Output length does not include the null.
1031 */
1032 int
1033 smb_get_dstring(struct mdchain *mdc, struct smb_vc *vcp,
1034 char *outbuf, size_t *outlen, int insize)
1035 {
1036 uint16_t convbuf[SMALL_CONV];
1037 uint16_t *cbuf;
1038 size_t cbufalloc, inlen, outsize;
1039 int error;
1040
1041 if (insize <= 0)
1042 return (0);
1043 /* Note: inlen is UTF-16 symbols. */
1044 inlen = insize / 2;
1045
1046 if (*outlen < 2)
1047 return (EINVAL);
1048 outsize = *outlen - 1; /* room for null */
1049
1050 /*
1051 * Get a buffer for the conversion and fill it.
1052 * Use stack buffer if the string is
1053 * small enough, else allocate.
1054 */
1055 if (insize < sizeof (convbuf)) {
1056 cbufalloc = 0;
1057 cbuf = convbuf;
1058 } else {
1059 cbufalloc = insize + 2;
1060 cbuf = kmem_alloc(cbufalloc, KM_SLEEP);
1061 }
1062 error = md_get_mem(mdc, cbuf, insize, MB_MSYSTEM);
1063 if (error != 0)
1064 goto out;
1065 cbuf[inlen] = 0;
1066
1067 /*
1068 * Handle the easy case (non-unicode).
1069 * XXX: Technically, we should convert
1070 * the string to OEM codeset first...
1071 * Modern servers all use Unicode, so
1072 * this is good enough.
1073 */
1074 if (SMB_UNICODE_STRINGS(vcp) == 0) {
1075 *outlen = strlcpy(outbuf, (char *)cbuf, outsize);
1076 if (*outlen > outsize) {
1077 *outlen = outsize;
1078 error = E2BIG;
1079 }
1080 } else {
1081 /*
1082 * Convert from UTF-16 to UTF-8
1083 */
1084 error = uconv_u16tou8(cbuf, &inlen,
1085 (uchar_t *)outbuf, outlen,
1086 UCONV_IN_LITTLE_ENDIAN);
1087 if (error == 0) {
1088 outbuf[*outlen] = '\0';
1089 }
1090 }
1091
1092 ASSERT(*outlen == strlen(outbuf));
1093
1094 out:
1095 if (cbufalloc != 0)
1096 kmem_free(cbuf, cbufalloc);
1097
1098 return (error);
1099 }
1100
1101 /*
1102 * It's surprising that this function does utf8-ucs2 conversion.
1103 * One would expect only smb_put_dstring to do that.
1104 * Fixing that will require changing a bunch of callers. XXX
1105 */
1106 /*ARGSUSED*/
1107 int
1108 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
1109 int size, int caseopt, int *lenp)
1110 {
1111 uint16_t convbuf[SMALL_CONV];
1112 uint16_t *cbuf;
1113 size_t cbufalloc, inlen, outlen;
1114 int error;
1115
1116 if (size <= 0)
1117 return (0);
1118
1119 /*
1120 * Handle the easy case (non-unicode).
1121 * XXX: Technically, we should convert
1122 * the string to OEM codeset first...
1123 * Modern servers all use Unicode, so
1124 * this is good enough.
1125 */
1162
1163 return (error);
1164 }
1165
1166 int
1167 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
1168 int caseopt)
1169 {
1170 int error, len;
1171
1172 /*
1173 * Let smb_put_dmem put both the string
1174 * and the terminating null.
1175 */
1176 len = strlen(src) + 1;
1177 error = smb_put_dmem(mbp, vcp, src, len, caseopt, NULL);
1178 if (error)
1179 return (error);
1180
1181 return (error);
1182 }
1183 int
1184 smb_smb_ntcreate(struct smb_share *ssp, struct mbchain *name_mb,
1185 uint32_t crflag, uint32_t req_acc, uint32_t efa, uint32_t sh_acc,
1186 uint32_t disp, uint32_t createopt, uint32_t impersonate,
1187 struct smb_cred *scrp, smb_fh_t *fhp,
1188 uint32_t *cr_act_p, struct smbfattr *fap)
1189 {
1190 int err;
1191
1192 if (SSTOVC(ssp)->vc_flags & SMBV_SMB2) {
1193 err = smb2_smb_ntcreate(ssp, name_mb, NULL, NULL,
1194 crflag, req_acc, efa, sh_acc, disp, createopt,
1195 impersonate, scrp, &fhp->fh_fid2, cr_act_p, fap);
1196 } else {
1197 err = smb1_smb_ntcreate(ssp, name_mb, crflag, req_acc,
1198 efa, sh_acc, disp, createopt, impersonate, scrp,
1199 &fhp->fh_fid1, cr_act_p, fap);
1200 }
1201 return (err);
1202 }
1203
1204 int
1205 smb_smb_close(struct smb_share *ssp, smb_fh_t *fhp,
1206 struct smb_cred *scrp)
1207 {
1208 int err;
1209
1210 if (SSTOVC(ssp)->vc_flags & SMBV_SMB2) {
1211 err = smb2_smb_close(ssp, &fhp->fh_fid2, scrp);
1212 } else {
1213 err = smb1_smb_close(ssp, fhp->fh_fid1, NULL, scrp);
1214 }
1215
1216 return (err);
1217 }
1218
1219 /*
1220 * Largest size to use with LARGE_READ/LARGE_WRITE.
1221 * Specs say up to 64k data bytes, but Windows traffic
1222 * uses 60k... no doubt for some good reason.
1223 * (Probably to keep 4k block alignment.)
1224 */
1225 uint32_t smb1_large_io_max = (60*1024);
1226
1227 /*
1228 * Common function for read/write with UIO.
1229 * Called by netsmb smb_usr_rw,
1230 * smbfs_readvnode, smbfs_writevnode
1231 */
1232 int
1233 smb_rwuio(smb_fh_t *fhp, uio_rw_t rw,
1234 uio_t *uiop, smb_cred_t *scred, int timo)
1235 {
1236 struct smb_share *ssp = FHTOSS(fhp);
1237 struct smb_vc *vcp = SSTOVC(ssp);
1238 ssize_t save_resid;
1239 uint32_t len, rlen, maxlen;
1240 int error = 0;
1241 int (*iofun)(smb_fh_t *, uint32_t *,
1242 uio_t *, smb_cred_t *, int);
1243
1244 /* After reconnect, the fid is invalid. */
1245 if (fhp->fh_vcgenid != ssp->ss_vcgenid)
1246 return (ESTALE);
1247
1248 if (SSTOVC(ssp)->vc_flags & SMBV_SMB2) {
1249 if (rw == UIO_READ) {
1250 iofun = smb2_smb_read;
1251 maxlen = vcp->vc_sopt.sv2_maxread;
1252 } else { /* UIO_WRITE */
1253 iofun = smb2_smb_write;
1254 maxlen = vcp->vc_sopt.sv2_maxwrite;
1255 }
1256 } else {
1257 /*
1258 * Using NT LM 0.12, so readx, writex.
1259 * Make sure we can represent the offset.
1260 */
1261 if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_FILES) == 0 &&
1262 (uiop->uio_loffset + uiop->uio_resid) > UINT32_MAX)
1263 return (EFBIG);
1264
1265 if (rw == UIO_READ) {
1266 iofun = smb_smb_readx;
1267 if (vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_READX)
1268 maxlen = smb1_large_io_max;
1269 else
1270 maxlen = vcp->vc_rxmax;
1271 } else { /* UIO_WRITE */
1272 iofun = smb_smb_writex;
1273 if (vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX)
1274 maxlen = smb1_large_io_max;
1275 else
1276 maxlen = vcp->vc_wxmax;
1277 }
1278 }
1279
1280 save_resid = uiop->uio_resid;
1281 while (uiop->uio_resid > 0) {
1282 /* Lint: uio_resid may be 64-bits */
1283 rlen = len = (uint32_t)min(maxlen, uiop->uio_resid);
1284 error = (*iofun)(fhp, &rlen, uiop, scred, timo);
1285
1286 /*
1287 * Note: the iofun called uio_update, so
1288 * not doing that here as one might expect.
1289 *
1290 * Quit the loop either on error, or if we
1291 * transferred less then requested.
1292 */
1293 if (error || (rlen < len))
1294 break;
1295
1296 timo = 0; /* only first I/O should wait */
1297 }
1298 if (error && (save_resid != uiop->uio_resid)) {
1299 /*
1300 * Stopped on an error after having
1301 * successfully transferred data.
1302 * Suppress this error.
1303 */
1304 SMBSDEBUG("error %d suppressed\n", error);
1305 error = 0;
1306 }
1307
1308 return (error);
1309 }
|