2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2001 Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
40 #include <sys/socket.h>
41 #include <sys/signalvar.h>
44 #include <sys/iconv.h>
46 #include <netsmb/smb.h>
47 #include <netsmb/smb_conn.h>
48 #include <netsmb/smb_rq.h>
49 #include <netsmb/smb_subr.h>
51 static MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
52 static MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
53 MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
55 smb_unichar smb_unieol = 0;
58 smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred)
62 scred->scr_cred = cred ? cred : td->td_ucred;
65 scred->scr_cred = cred ? cred : NULL;
70 smb_td_intr(struct thread *td)
80 tmpset = p->p_siglist;
81 SIGSETOR(tmpset, td->td_siglist);
82 SIGSETNAND(tmpset, td->td_sigmask);
83 mtx_lock(&p->p_sigacts->ps_mtx);
84 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
85 mtx_unlock(&p->p_sigacts->ps_mtx);
86 if (SIGNOTEMPTY(td->td_siglist) && SMB_SIGMASK(tmpset)) {
95 smb_strdup(const char *s)
100 len = s ? strlen(s) + 1 : 1;
101 p = malloc(len, M_SMBSTR, M_WAITOK);
110 * duplicate string from a user space.
113 smb_strdupin(char *s, size_t maxlen)
118 p = malloc(maxlen + 1, M_SMBSTR, M_WAITOK);
119 error = copyinstr(s, p, maxlen + 1, NULL);
128 * duplicate memory block from a user space.
131 smb_memdupin(void *umem, size_t len)
137 p = malloc(len, M_SMBSTR, M_WAITOK);
138 if (copyin(umem, p, len) == 0)
145 * duplicate memory block in the kernel space.
148 smb_memdup(const void *umem, int len)
154 p = malloc(len, M_SMBSTR, M_WAITOK);
174 smb_zmalloc(size_t size, struct malloc_type *type, int flags)
177 return malloc(size, type, flags | M_ZERO);
181 smb_strtouni(u_int16_t *dst, const char *src)
184 *dst++ = htole16(*src++);
189 #ifdef SMB_SOCKETDATA_DEBUG
191 m_dumpm(struct mbuf *m) {
200 printf("%02x ",((int)*(p++)) & 0xff);
209 smb_maperror(int eclass, int eno)
211 if (eclass == 0 && eno == 0)
225 case 66: /* nt returns it when share not available */
226 case 67: /* observed from nt4sp6 when sharename wrong */
236 return ENOMEM; /* actually remote no mem... */
244 case ERRnotready: /* nt */
249 return 0; /* eeof ? */
255 case 123: /* dunno what is it, but samba maps as noent */
257 case 145: /* samba */
260 return 0; /* file become unlocked */
272 case ERRpasswordExpired:
279 SMBERROR("NetBIOS name is invalid\n");
281 case 3: /* reserved and returned */
283 case ERRaccountExpired:
285 case ERRbadLogonTime:
310 SMBERROR("Unmapped error %d:%d\n", eclass, eno);
315 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
316 size_t *srclen, size_t *dstlen)
319 size_t inlen = *srclen, outlen = *dstlen;
321 error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen,
323 if (inlen != *srclen || outlen != *dstlen) {
332 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
333 size_t size, int caseopt)
335 struct iconv_drv *dp = vcp->vc_toserver;
340 return mb_put_mem(mbp, src, size, MB_MSYSTEM);
342 mbp->mb_copy = smb_copy_iconv;
344 if (SMB_UNICODE_STRINGS(vcp))
346 return mb_put_mem(mbp, src, size, MB_MCUSTOM);
350 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
355 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
358 if (SMB_UNICODE_STRINGS(vcp))
359 return mb_put_uint16le(mbp, 0);
360 return mb_put_uint8(mbp, 0);
364 smb_put_asunistring(struct smb_rq *rqp, const char *src)
366 struct mbchain *mbp = &rqp->sr_rq;
367 struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
372 iconv_convmem(dp, &c, src++, 1);
373 error = mb_put_uint16le(mbp, c);
377 return mb_put_uint16le(mbp, 0);