2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
2001-04-10 07:59:06 +00:00
|
|
|
* Copyright (c) 2000-2001 Boris Popov
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2003-06-11 05:37:42 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-04-10 07:59:06 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2002-04-26 22:48:23 +00:00
|
|
|
#include <sys/endian.h>
|
2001-04-10 07:59:06 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
|
|
|
|
#include <sys/iconv.h>
|
|
|
|
|
|
|
|
#include <netsmb/smb.h>
|
|
|
|
#include <netsmb/smb_conn.h>
|
|
|
|
#include <netsmb/smb_rq.h>
|
|
|
|
#include <netsmb/smb_subr.h>
|
|
|
|
|
|
|
|
MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
|
|
|
|
MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
|
|
|
|
MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
|
|
|
|
|
|
|
|
smb_unichar smb_unieol = 0;
|
|
|
|
|
|
|
|
void
|
2001-12-02 08:47:29 +00:00
|
|
|
smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred)
|
2001-04-10 07:59:06 +00:00
|
|
|
{
|
2001-12-02 08:47:29 +00:00
|
|
|
if (td) {
|
|
|
|
scred->scr_td = td;
|
2002-02-27 18:32:23 +00:00
|
|
|
scred->scr_cred = cred ? cred : td->td_ucred;
|
2001-04-10 07:59:06 +00:00
|
|
|
} else {
|
2001-12-02 08:47:29 +00:00
|
|
|
scred->scr_td = NULL;
|
2001-04-10 07:59:06 +00:00
|
|
|
scred->scr_cred = cred ? cred : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-03-31 22:49:17 +00:00
|
|
|
smb_td_intr(struct thread *td)
|
2001-04-10 07:59:06 +00:00
|
|
|
{
|
2003-03-31 22:49:17 +00:00
|
|
|
struct proc *p;
|
2001-04-10 07:59:06 +00:00
|
|
|
sigset_t tmpset;
|
|
|
|
|
2003-03-31 22:49:17 +00:00
|
|
|
if (td == NULL)
|
2001-04-10 07:59:06 +00:00
|
|
|
return 0;
|
2003-03-31 22:49:17 +00:00
|
|
|
|
|
|
|
p = td->td_proc;
|
2003-02-14 09:12:12 +00:00
|
|
|
PROC_LOCK(p);
|
2002-10-01 17:15:53 +00:00
|
|
|
tmpset = p->p_siglist;
|
2003-03-31 22:49:17 +00:00
|
|
|
SIGSETOR(tmpset, td->td_siglist);
|
|
|
|
SIGSETNAND(tmpset, td->td_sigmask);
|
- Merge struct procsig with struct sigacts.
- Move struct sigacts out of the u-area and malloc() it using the
M_SUBPROC malloc bucket.
- Add a small sigacts_*() API for managing sigacts structures: sigacts_alloc(),
sigacts_free(), sigacts_copy(), sigacts_share(), and sigacts_shared().
- Remove the p_sigignore, p_sigacts, and p_sigcatch macros.
- Add a mutex to struct sigacts that protects all the members of the struct.
- Add sigacts locking.
- Remove Giant from nosys(), kill(), killpg(), and kern_sigaction() now
that sigacts is locked.
- Several in-kernel functions such as psignal(), tdsignal(), trapsignal(),
and thread_stopped() are now MP safe.
Reviewed by: arch@
Approved by: re (rwatson)
2003-05-13 20:36:02 +00:00
|
|
|
mtx_lock(&p->p_sigacts->ps_mtx);
|
|
|
|
SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
|
|
|
|
mtx_unlock(&p->p_sigacts->ps_mtx);
|
2003-03-31 22:49:17 +00:00
|
|
|
if (SIGNOTEMPTY(td->td_siglist) && SMB_SIGMASK(tmpset)) {
|
2003-02-14 09:12:12 +00:00
|
|
|
PROC_UNLOCK(p);
|
2001-04-10 07:59:06 +00:00
|
|
|
return EINTR;
|
2003-02-14 09:12:12 +00:00
|
|
|
}
|
|
|
|
PROC_UNLOCK(p);
|
2001-04-10 07:59:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
smb_strdup(const char *s)
|
|
|
|
{
|
|
|
|
char *p;
|
2011-01-08 23:06:54 +00:00
|
|
|
size_t len;
|
2001-04-10 07:59:06 +00:00
|
|
|
|
|
|
|
len = s ? strlen(s) + 1 : 1;
|
2003-02-19 05:47:46 +00:00
|
|
|
p = malloc(len, M_SMBSTR, M_WAITOK);
|
2001-04-10 07:59:06 +00:00
|
|
|
if (s)
|
|
|
|
bcopy(s, p, len);
|
|
|
|
else
|
|
|
|
*p = 0;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* duplicate string from a user space.
|
|
|
|
*/
|
|
|
|
char *
|
2011-01-08 23:06:54 +00:00
|
|
|
smb_strdupin(char *s, size_t maxlen)
|
2001-04-10 07:59:06 +00:00
|
|
|
{
|
|
|
|
char *p, bt;
|
2011-01-08 23:06:54 +00:00
|
|
|
int error;
|
|
|
|
size_t len;
|
2001-04-10 07:59:06 +00:00
|
|
|
|
2011-01-08 23:06:54 +00:00
|
|
|
len = 0;
|
2001-04-10 07:59:06 +00:00
|
|
|
for (p = s; ;p++) {
|
|
|
|
if (copyin(p, &bt, 1))
|
|
|
|
return NULL;
|
|
|
|
len++;
|
|
|
|
if (maxlen && len > maxlen)
|
|
|
|
return NULL;
|
|
|
|
if (bt == 0)
|
|
|
|
break;
|
|
|
|
}
|
2003-02-19 05:47:46 +00:00
|
|
|
p = malloc(len, M_SMBSTR, M_WAITOK);
|
2006-01-16 17:03:21 +00:00
|
|
|
error = copyin(s, p, len);
|
|
|
|
if (error) {
|
|
|
|
free(p, M_SMBSTR);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2001-04-10 07:59:06 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* duplicate memory block from a user space.
|
|
|
|
*/
|
|
|
|
void *
|
2011-01-08 23:06:54 +00:00
|
|
|
smb_memdupin(void *umem, size_t len)
|
2001-04-10 07:59:06 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (len > 8 * 1024)
|
|
|
|
return NULL;
|
2003-02-19 05:47:46 +00:00
|
|
|
p = malloc(len, M_SMBSTR, M_WAITOK);
|
2001-04-10 07:59:06 +00:00
|
|
|
if (copyin(umem, p, len) == 0)
|
|
|
|
return p;
|
|
|
|
free(p, M_SMBSTR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* duplicate memory block in the kernel space.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
smb_memdup(const void *umem, int len)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (len > 8 * 1024)
|
|
|
|
return NULL;
|
2003-02-19 05:47:46 +00:00
|
|
|
p = malloc(len, M_SMBSTR, M_WAITOK);
|
2001-04-10 07:59:06 +00:00
|
|
|
if (p == NULL)
|
|
|
|
return NULL;
|
|
|
|
bcopy(umem, p, len);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
smb_strfree(char *s)
|
|
|
|
{
|
|
|
|
free(s, M_SMBSTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
smb_memfree(void *s)
|
|
|
|
{
|
|
|
|
free(s, M_SMBSTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2011-01-08 23:06:54 +00:00
|
|
|
smb_zmalloc(size_t size, struct malloc_type *type, int flags)
|
2001-04-10 07:59:06 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
return malloc(size, type, flags | M_ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
smb_strtouni(u_int16_t *dst, const char *src)
|
|
|
|
{
|
|
|
|
while (*src) {
|
2002-12-16 16:20:06 +00:00
|
|
|
*dst++ = htole16(*src++);
|
2001-04-10 07:59:06 +00:00
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SMB_SOCKETDATA_DEBUG
|
|
|
|
void
|
|
|
|
m_dumpm(struct mbuf *m) {
|
|
|
|
char *p;
|
2011-01-08 23:06:54 +00:00
|
|
|
size_t len;
|
2001-04-10 07:59:06 +00:00
|
|
|
printf("d=");
|
|
|
|
while(m) {
|
|
|
|
p=mtod(m,char *);
|
|
|
|
len=m->m_len;
|
2011-01-08 23:06:54 +00:00
|
|
|
printf("(%zu)",len);
|
2001-04-10 07:59:06 +00:00
|
|
|
while(len--){
|
|
|
|
printf("%02x ",((int)*(p++)) & 0xff);
|
|
|
|
}
|
|
|
|
m=m->m_next;
|
|
|
|
};
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_maperror(int eclass, int eno)
|
|
|
|
{
|
|
|
|
if (eclass == 0 && eno == 0)
|
|
|
|
return 0;
|
|
|
|
switch (eclass) {
|
|
|
|
case ERRDOS:
|
|
|
|
switch (eno) {
|
|
|
|
case ERRbadfunc:
|
|
|
|
case ERRbadmcb:
|
|
|
|
case ERRbadenv:
|
|
|
|
case ERRbadformat:
|
|
|
|
case ERRrmuns:
|
|
|
|
return EINVAL;
|
|
|
|
case ERRbadfile:
|
|
|
|
case ERRbadpath:
|
|
|
|
case ERRremcd:
|
|
|
|
case 66: /* nt returns it when share not available */
|
2001-08-21 09:16:57 +00:00
|
|
|
case 67: /* observed from nt4sp6 when sharename wrong */
|
2001-04-10 07:59:06 +00:00
|
|
|
return ENOENT;
|
|
|
|
case ERRnofids:
|
|
|
|
return EMFILE;
|
|
|
|
case ERRnoaccess:
|
|
|
|
case ERRbadshare:
|
|
|
|
return EACCES;
|
|
|
|
case ERRbadfid:
|
|
|
|
return EBADF;
|
|
|
|
case ERRnomem:
|
|
|
|
return ENOMEM; /* actually remote no mem... */
|
|
|
|
case ERRbadmem:
|
|
|
|
return EFAULT;
|
|
|
|
case ERRbadaccess:
|
|
|
|
return EACCES;
|
|
|
|
case ERRbaddata:
|
|
|
|
return E2BIG;
|
|
|
|
case ERRbaddrive:
|
|
|
|
case ERRnotready: /* nt */
|
|
|
|
return ENXIO;
|
|
|
|
case ERRdiffdevice:
|
|
|
|
return EXDEV;
|
|
|
|
case ERRnofiles:
|
|
|
|
return 0; /* eeof ? */
|
|
|
|
return ETXTBSY;
|
|
|
|
case ERRlock:
|
|
|
|
return EDEADLK;
|
|
|
|
case ERRfilexists:
|
|
|
|
return EEXIST;
|
|
|
|
case 123: /* dunno what is it, but samba maps as noent */
|
|
|
|
return ENOENT;
|
|
|
|
case 145: /* samba */
|
|
|
|
return ENOTEMPTY;
|
2006-11-05 06:31:08 +00:00
|
|
|
case ERRnotlocked:
|
|
|
|
return 0; /* file become unlocked */
|
2001-04-10 07:59:06 +00:00
|
|
|
case 183:
|
|
|
|
return EEXIST;
|
2002-04-17 03:14:28 +00:00
|
|
|
case ERRquota:
|
|
|
|
return EDQUOT;
|
2001-04-10 07:59:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ERRSRV:
|
|
|
|
switch (eno) {
|
|
|
|
case ERRerror:
|
|
|
|
return EINVAL;
|
|
|
|
case ERRbadpw:
|
2002-04-17 03:14:28 +00:00
|
|
|
case ERRpasswordExpired:
|
2001-04-10 07:59:06 +00:00
|
|
|
return EAUTH;
|
|
|
|
case ERRaccess:
|
|
|
|
return EACCES;
|
|
|
|
case ERRinvnid:
|
|
|
|
return ENETRESET;
|
|
|
|
case ERRinvnetname:
|
|
|
|
SMBERROR("NetBIOS name is invalid\n");
|
|
|
|
return EAUTH;
|
|
|
|
case 3: /* reserved and returned */
|
|
|
|
return EIO;
|
2002-04-17 03:14:28 +00:00
|
|
|
case ERRaccountExpired:
|
|
|
|
case ERRbadClient:
|
|
|
|
case ERRbadLogonTime:
|
2001-04-10 07:59:06 +00:00
|
|
|
return EPERM;
|
2002-04-17 03:14:28 +00:00
|
|
|
case ERRnosupport:
|
|
|
|
return EBADRPC;
|
2001-04-10 07:59:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ERRHRD:
|
|
|
|
switch (eno) {
|
|
|
|
case ERRnowrite:
|
|
|
|
return EROFS;
|
|
|
|
case ERRbadunit:
|
|
|
|
return ENODEV;
|
|
|
|
case ERRnotready:
|
|
|
|
case ERRbadcmd:
|
|
|
|
case ERRdata:
|
|
|
|
return EIO;
|
|
|
|
case ERRbadreq:
|
|
|
|
return EBADRPC;
|
|
|
|
case ERRbadshare:
|
|
|
|
return ETXTBSY;
|
|
|
|
case ERRlock:
|
|
|
|
return EDEADLK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SMBERROR("Unmapped error %d:%d\n", eclass, eno);
|
|
|
|
return EBADRPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-07-29 13:22:37 +00:00
|
|
|
smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
|
|
|
|
size_t *srclen, size_t *dstlen)
|
2001-04-10 07:59:06 +00:00
|
|
|
{
|
2005-07-29 13:22:37 +00:00
|
|
|
int error;
|
|
|
|
size_t inlen = *srclen, outlen = *dstlen;
|
2001-04-10 07:59:06 +00:00
|
|
|
|
2005-07-29 13:22:37 +00:00
|
|
|
error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen,
|
|
|
|
&dst, &outlen);
|
|
|
|
if (inlen != *srclen || outlen != *dstlen) {
|
|
|
|
*srclen -= inlen;
|
|
|
|
*dstlen -= outlen;
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return error;
|
2001-04-10 07:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
|
2011-01-08 23:06:54 +00:00
|
|
|
size_t size, int caseopt)
|
2001-04-10 07:59:06 +00:00
|
|
|
{
|
|
|
|
struct iconv_drv *dp = vcp->vc_toserver;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
if (dp == NULL) {
|
|
|
|
return mb_put_mem(mbp, src, size, MB_MSYSTEM);
|
|
|
|
}
|
|
|
|
mbp->mb_copy = smb_copy_iconv;
|
|
|
|
mbp->mb_udata = dp;
|
|
|
|
return mb_put_mem(mbp, src, size, MB_MCUSTOM);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
|
|
|
|
int caseopt)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
return mb_put_uint8(mbp, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_put_asunistring(struct smb_rq *rqp, const char *src)
|
|
|
|
{
|
|
|
|
struct mbchain *mbp = &rqp->sr_rq;
|
|
|
|
struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
|
|
|
|
u_char c;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
while (*src) {
|
|
|
|
iconv_convmem(dp, &c, src++, 1);
|
|
|
|
error = mb_put_uint16le(mbp, c);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
return mb_put_uint16le(mbp, 0);
|
|
|
|
}
|