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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
43 #include <sys/sysctl.h>
44 #include <sys/socket.h>
45 #include <sys/signalvar.h>
48 #include <sys/iconv.h>
50 #include <netsmb/smb.h>
51 #include <netsmb/smb_conn.h>
52 #include <netsmb/smb_rq.h>
53 #include <netsmb/smb_subr.h>
55 MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
56 MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
57 MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
59 smb_unichar smb_unieol = 0;
62 smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred)
66 scred->scr_cred = cred ? cred : td->td_ucred;
69 scred->scr_cred = cred ? cred : NULL;
74 smb_td_intr(struct thread *td)
84 tmpset = p->p_siglist;
85 SIGSETOR(tmpset, td->td_siglist);
86 SIGSETNAND(tmpset, td->td_sigmask);
87 mtx_lock(&p->p_sigacts->ps_mtx);
88 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
89 mtx_unlock(&p->p_sigacts->ps_mtx);
90 if (SIGNOTEMPTY(td->td_siglist) && SMB_SIGMASK(tmpset)) {
99 smb_strdup(const char *s)
104 len = s ? strlen(s) + 1 : 1;
105 p = malloc(len, M_SMBSTR, M_WAITOK);
114 * duplicate string from a user space.
117 smb_strdupin(char *s, int maxlen)
123 if (copyin(p, &bt, 1))
126 if (maxlen && len > maxlen)
131 p = malloc(len, M_SMBSTR, M_WAITOK);
132 error = copyin(s, p, len);
141 * duplicate memory block from a user space.
144 smb_memdupin(void *umem, int len)
150 p = malloc(len, M_SMBSTR, M_WAITOK);
151 if (copyin(umem, p, len) == 0)
158 * duplicate memory block in the kernel space.
161 smb_memdup(const void *umem, int len)
167 p = malloc(len, M_SMBSTR, M_WAITOK);
187 smb_zmalloc(unsigned long size, struct malloc_type *type, int flags)
190 return malloc(size, type, flags | M_ZERO);
194 smb_strtouni(u_int16_t *dst, const char *src)
197 *dst++ = htole16(*src++);
202 #ifdef SMB_SOCKETDATA_DEBUG
204 m_dumpm(struct mbuf *m) {
213 printf("%02x ",((int)*(p++)) & 0xff);
222 smb_maperror(int eclass, int eno)
224 if (eclass == 0 && eno == 0)
238 case 66: /* nt returns it when share not available */
239 case 67: /* observed from nt4sp6 when sharename wrong */
249 return ENOMEM; /* actually remote no mem... */
257 case ERRnotready: /* nt */
262 return 0; /* eeof ? */
268 case 123: /* dunno what is it, but samba maps as noent */
270 case 145: /* samba */
273 return 0; /* file become unlocked */
285 case ERRpasswordExpired:
292 SMBERROR("NetBIOS name is invalid\n");
294 case 3: /* reserved and returned */
296 case ERRaccountExpired:
298 case ERRbadLogonTime:
323 SMBERROR("Unmapped error %d:%d\n", eclass, eno);
328 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
329 size_t *srclen, size_t *dstlen)
332 size_t inlen = *srclen, outlen = *dstlen;
334 error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen,
336 if (inlen != *srclen || outlen != *dstlen) {
345 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
346 int size, int caseopt)
348 struct iconv_drv *dp = vcp->vc_toserver;
353 return mb_put_mem(mbp, src, size, MB_MSYSTEM);
355 mbp->mb_copy = smb_copy_iconv;
357 return mb_put_mem(mbp, src, size, MB_MCUSTOM);
361 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
366 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
369 return mb_put_uint8(mbp, 0);
373 smb_put_asunistring(struct smb_rq *rqp, const char *src)
375 struct mbchain *mbp = &rqp->sr_rq;
376 struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
381 iconv_convmem(dp, &c, src++, 1);
382 error = mb_put_uint16le(mbp, c);
386 return mb_put_uint16le(mbp, 0);