2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
37 #include <sys/sysctl.h>
38 #include <sys/socket.h>
39 #include <sys/signalvar.h>
42 #include <sys/iconv.h>
44 #include <netsmb/smb.h>
45 #include <netsmb/smb_conn.h>
46 #include <netsmb/smb_rq.h>
47 #include <netsmb/smb_subr.h>
49 static MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
50 static MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
51 MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
53 smb_unichar smb_unieol = 0;
56 smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred)
60 scred->scr_cred = cred ? cred : td->td_ucred;
63 scred->scr_cred = cred ? cred : NULL;
68 smb_td_intr(struct thread *td)
78 tmpset = p->p_siglist;
79 SIGSETOR(tmpset, td->td_siglist);
80 SIGSETNAND(tmpset, td->td_sigmask);
81 mtx_lock(&p->p_sigacts->ps_mtx);
82 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
83 mtx_unlock(&p->p_sigacts->ps_mtx);
84 if (SIGNOTEMPTY(td->td_siglist) && SMB_SIGMASK(tmpset)) {
93 smb_strdup(const char *s)
98 len = s ? strlen(s) + 1 : 1;
99 p = malloc(len, M_SMBSTR, M_WAITOK);
108 * duplicate string from a user space.
111 smb_strdupin(char *s, size_t maxlen)
116 p = malloc(maxlen + 1, M_SMBSTR, M_WAITOK);
117 error = copyinstr(s, p, maxlen + 1, NULL);
126 * duplicate memory block from a user space.
129 smb_memdupin(void *umem, size_t len)
135 p = malloc(len, M_SMBSTR, M_WAITOK);
136 if (copyin(umem, p, len) == 0)
143 * duplicate memory block in the kernel space.
146 smb_memdup(const void *umem, int len)
152 p = malloc(len, M_SMBSTR, M_WAITOK);
172 smb_zmalloc(size_t size, struct malloc_type *type, int flags)
175 return malloc(size, type, flags | M_ZERO);
179 smb_strtouni(u_int16_t *dst, const char *src)
182 *dst++ = htole16(*src++);
187 #ifdef SMB_SOCKETDATA_DEBUG
189 m_dumpm(struct mbuf *m) {
198 printf("%02x ",((int)*(p++)) & 0xff);
207 smb_maperror(int eclass, int eno)
209 if (eclass == 0 && eno == 0)
223 case 66: /* nt returns it when share not available */
224 case 67: /* observed from nt4sp6 when sharename wrong */
234 return ENOMEM; /* actually remote no mem... */
242 case ERRnotready: /* nt */
247 return 0; /* eeof ? */
253 case 123: /* dunno what is it, but samba maps as noent */
255 case 145: /* samba */
258 return 0; /* file become unlocked */
270 case ERRpasswordExpired:
277 SMBERROR("NetBIOS name is invalid\n");
279 case 3: /* reserved and returned */
281 case ERRaccountExpired:
283 case ERRbadLogonTime:
308 SMBERROR("Unmapped error %d:%d\n", eclass, eno);
313 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
314 size_t *srclen, size_t *dstlen)
317 size_t inlen = *srclen, outlen = *dstlen;
319 error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen,
321 if (inlen != *srclen || outlen != *dstlen) {
330 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
331 size_t size, int caseopt)
333 struct iconv_drv *dp = vcp->vc_toserver;
338 return mb_put_mem(mbp, src, size, MB_MSYSTEM);
340 mbp->mb_copy = smb_copy_iconv;
342 if (SMB_UNICODE_STRINGS(vcp))
344 return mb_put_mem(mbp, src, size, MB_MCUSTOM);
348 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
353 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
356 if (SMB_UNICODE_STRINGS(vcp))
357 return mb_put_uint16le(mbp, 0);
358 return mb_put_uint8(mbp, 0);
362 smb_put_asunistring(struct smb_rq *rqp, const char *src)
364 struct mbchain *mbp = &rqp->sr_rq;
365 struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
370 iconv_convmem(dp, &c, src++, 1);
371 error = mb_put_uint16le(mbp, c);
375 return mb_put_uint16le(mbp, 0);