1 /* $NetBSD: smb_subr.c,v 1.33 2009/03/18 17:06:53 cegger Exp $ */
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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Boris Popov.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * FreeBSD: src/sys/netsmb/smb_subr.c,v 1.6 2002/04/17 03:14:28 bp Exp
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: smb_subr.c,v 1.33 2009/03/18 17:06:53 cegger Exp $");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/socket.h>
48 #include <sys/signal.h>
49 #include <sys/signalvar.h>
51 #include <sys/socketvar.h> /* for M_SONAME */
52 #include <sys/kauth.h>
54 #include <netsmb/iconv.h>
56 #include <netsmb/smb.h>
57 #include <netsmb/smb_conn.h>
58 #include <netsmb/smb_rq.h>
59 #include <netsmb/smb_subr.h>
61 const smb_unichar smb_unieol
= 0;
63 static MALLOC_DEFINE(M_SMBSTR
, "smbstr", "SMB strings");
64 MALLOC_DEFINE(M_SMBTEMP
, "smbtemp", "Temp netsmb data");
67 smb_makescred(struct smb_cred
*scred
, struct lwp
*l
, kauth_cred_t cred
)
71 scred
->scr_cred
= cred
? cred
: l
->l_cred
;
74 scred
->scr_cred
= cred
? cred
: NULL
;
79 smb_proc_intr(struct lwp
*l
)
88 mutex_enter(p
->p_lock
);
89 error
= sigispending(l
, 0);
90 mutex_exit(p
->p_lock
);
92 return (error
!= 0 ? EINTR
: 0);
96 smb_strdup(const char *s
)
101 len
= s
? strlen(s
) + 1 : 1;
102 p
= malloc(len
, M_SMBSTR
, M_WAITOK
);
111 * duplicate string from a user space.
114 smb_strdupin(char *s
, int maxlen
)
120 if (copyin(p
, &bt
, 1))
123 if (maxlen
&& len
> maxlen
)
128 p
= malloc(len
, M_SMBSTR
, M_WAITOK
);
134 * duplicate memory block from a user space.
137 smb_memdupin(void *umem
, int len
)
143 p
= malloc(len
, M_SMBSTR
, M_WAITOK
);
144 if (copyin(umem
, p
, len
) == 0)
163 smb_zmalloc(unsigned long size
, struct malloc_type
*type
, int flags
)
166 return malloc(size
, type
, flags
| M_ZERO
);
170 smb_strtouni(u_int16_t
*dst
, const char *src
)
173 *dst
++ = htole16(*src
++);
178 #ifdef SMB_SOCKETDATA_DEBUG
180 m_dumpm(struct mbuf
*m
) {
189 printf("%02x ",((int)*(p
++)) & 0xff);
198 smb_maperror(int eclass
, int eno
)
200 if (eclass
== 0 && eno
== 0)
215 case ERRnoipc
: /* nt returns it when share not available */
216 case ERRnosuchshare
: /* observed from nt4sp6 when sharename wrong */
226 return ENOMEM
; /* actually remote no mem... */
234 case ERRnotready
: /* nt */
242 case ERRinvalidname
: /* dunno what is it, but samba maps as noent */
244 case ERRdirnempty
: /* samba */
251 /* it's okay to try to unlock already unlocked file */
253 case NT_STATUS_NOTIFY_ENUM_DIR
:
262 case ERRpasswordExpired
:
270 SMBERROR(("NetBIOS name is invalid\n"));
272 case ERRbadtype
: /* reserved and returned */
274 case ERRaccountExpired
:
276 case ERRbadLogonTime
:
299 /* returned e.g. for NT CANCEL SMB by Samba */
304 SMBERROR(("Unmapped error %d:%d\n", eclass
, eno
));
309 smb_copy_iconv(struct mbchain
*mbp
, const char *src
, char *dst
, size_t len
)
313 return iconv_conv((struct iconv_drv
*)mbp
->mb_udata
, &src
, &len
, &dst
, &outlen
);
317 smb_put_dmem(struct mbchain
*mbp
, struct smb_vc
*vcp
, const char *src
,
318 int size
, int caseopt
)
320 struct iconv_drv
*dp
= vcp
->vc_toserver
;
325 return mb_put_mem(mbp
, (const void *)src
, size
, MB_MSYSTEM
);
327 mbp
->mb_copy
= smb_copy_iconv
;
329 return mb_put_mem(mbp
, (const void *)src
, size
, MB_MCUSTOM
);
333 smb_put_dstring(struct mbchain
*mbp
, struct smb_vc
*vcp
, const char *src
,
338 error
= smb_put_dmem(mbp
, vcp
, src
, strlen(src
), caseopt
);
341 return mb_put_uint8(mbp
, 0);
346 smb_put_asunistring(struct smb_rq
*rqp
, const char *src
)
348 struct mbchain
*mbp
= &rqp
->sr_rq
;
349 struct iconv_drv
*dp
= rqp
->sr_vc
->vc_toserver
;
354 iconv_convmem(dp
, &c
, src
++, 1);
355 error
= mb_put_uint16le(mbp
, c
);
359 return mb_put_uint16le(mbp
, 0);
364 dup_sockaddr(struct sockaddr
*sa
, int canwait
)
366 struct sockaddr
*sa2
;
368 sa2
= malloc(sa
->sa_len
, M_SONAME
, canwait
? M_WAITOK
: M_NOWAIT
);
370 memcpy(sa2
, sa
, sa
->sa_len
);