NetBSD/sys/netsmb/smb_subr.c

365 lines
8.0 KiB
C
Raw Normal View History

2005-02-27 01:31:44 +03:00
/* $NetBSD: smb_subr.c,v 1.21 2005/02/26 22:39:50 perry Exp $ */
/*
2002-01-04 05:39:37 +03: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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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.
2002-01-04 05:39:37 +03:00
*
* FreeBSD: src/sys/netsmb/smb_subr.c,v 1.6 2002/04/17 03:14:28 bp Exp
*/
2003-02-25 12:12:11 +03:00
#include <sys/cdefs.h>
2005-02-27 01:31:44 +03:00
__KERNEL_RCSID(0, "$NetBSD: smb_subr.c,v 1.21 2005/02/26 22:39:50 perry Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
2002-01-04 05:39:37 +03:00
#include <sys/signal.h>
#include <sys/signalvar.h>
#include <sys/mbuf.h>
2003-02-16 02:02:30 +03:00
#include <sys/socketvar.h> /* for M_SONAME */
2002-01-04 05:39:37 +03:00
#include <netsmb/iconv.h>
#include <netsmb/smb.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_rq.h>
#include <netsmb/smb_subr.h>
2003-02-24 22:32:16 +03:00
const smb_unichar smb_unieol = 0;
static MALLOC_DEFINE(M_SMBSTR, "smbstr", "SMB strings");
2003-02-16 02:02:30 +03:00
MALLOC_DEFINE(M_SMBTEMP, "smbtemp", "Temp netsmb data");
void
smb_makescred(struct smb_cred *scred, struct proc *p, struct ucred *cred)
{
if (p) {
scred->scr_p = p;
scred->scr_cred = cred ? cred : p->p_ucred;
} else {
scred->scr_p = NULL;
scred->scr_cred = cred ? cred : NULL;
}
}
int
smb_proc_intr(struct proc *p)
{
if (p == NULL)
return 0;
if (!sigemptyset(&p->p_sigctx.ps_siglist)
&& SMB_SIGMASK(p->p_sigctx.ps_siglist))
return EINTR;
return 0;
}
char *
smb_strdup(const char *s)
{
char *p;
int len;
len = s ? strlen(s) + 1 : 1;
2002-01-04 05:39:37 +03:00
p = malloc(len, M_SMBSTR, M_WAITOK);
if (s)
bcopy(s, p, len);
else
*p = 0;
return p;
}
/*
* duplicate string from a user space.
*/
char *
smb_strdupin(char *s, int maxlen)
{
char *p, bt;
int len = 0;
for (p = s; ;p++) {
if (copyin(p, &bt, 1))
return NULL;
len++;
if (maxlen && len > maxlen)
return NULL;
if (bt == 0)
break;
}
2002-01-04 05:39:37 +03:00
p = malloc(len, M_SMBSTR, M_WAITOK);
copyin(s, p, len);
return p;
}
/*
* duplicate memory block from a user space.
*/
void *
smb_memdupin(void *umem, int len)
{
char *p;
if (len > 8 * 1024)
return NULL;
2002-01-04 05:39:37 +03:00
p = malloc(len, M_SMBSTR, M_WAITOK);
if (copyin(umem, p, len) == 0)
return p;
free(p, M_SMBSTR);
return NULL;
}
void
smb_strfree(char *s)
{
free(s, M_SMBSTR);
}
void
smb_memfree(void *s)
{
free(s, M_SMBSTR);
}
2002-01-04 05:39:37 +03:00
void *
2003-02-16 02:02:30 +03:00
smb_zmalloc(unsigned long size, struct malloc_type *type, int flags)
2002-01-04 05:39:37 +03:00
{
return malloc(size, type, flags | M_ZERO);
}
void
smb_strtouni(u_int16_t *dst, const char *src)
{
while (*src) {
*dst++ = htole16(*src++);
}
*dst = 0;
}
#ifdef SMB_SOCKETDATA_DEBUG
void
m_dumpm(struct mbuf *m) {
char *p;
int len;
printf("d=");
while(m) {
p=mtod(m,char *);
len=m->m_len;
printf("(%d)",len);
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 ERRnofiles:
case ERRbadfile:
case ERRbadpath:
case ERRremcd:
case ERRnoipc: /* nt returns it when share not available */
case ERRnosuchshare: /* observed from nt4sp6 when sharename wrong */
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:
2002-01-04 05:39:37 +03:00
case ERRnotready: /* nt */
return ENXIO;
case ERRdiffdevice:
return EXDEV;
case ERRlock:
return EDEADLK;
case ERRfilexists:
return EEXIST;
case ERRinvalidname: /* dunno what is it, but samba maps as noent */
return ENOENT;
case ERRdirnempty: /* samba */
return ENOTEMPTY;
case ERRrename:
return EEXIST;
case ERRquota:
return EDQUOT;
2003-03-24 13:05:43 +03:00
case ERRnotlocked:
return EBUSY;
case NT_STATUS_NOTIFY_ENUM_DIR:
return EMSGSIZE;
}
break;
case ERRSRV:
switch (eno) {
case ERRerror:
return EINVAL;
case ERRbadpw:
case ERRpasswordExpired:
2003-09-27 16:24:25 +04:00
case ERRbaduid:
return EAUTH;
case ERRaccess:
return EACCES;
case ERRinvnid:
return ENETRESET;
case ERRinvnetname:
SMBERROR("NetBIOS name is invalid\n");
return EAUTH;
case ERRbadtype: /* reserved and returned */
return EIO;
case ERRaccountExpired:
case ERRbadClient:
case ERRbadLogonTime:
return EPERM;
case ERRnosupport:
return EBADRPC;
}
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;
case ERRgeneral:
/* returned e.g. for NT CANCEL SMB by Samba */
return ECANCELED;
}
break;
}
SMBERROR("Unmapped error %d:%d\n", eclass, eno);
return EBADRPC;
}
2002-01-04 05:39:37 +03:00
static int
smb_copy_iconv(struct mbchain *mbp, const caddr_t src, caddr_t dst, size_t len)
2002-01-04 05:39:37 +03:00
{
size_t outlen = len;
2002-01-04 05:39:37 +03:00
return iconv_conv((struct iconv_drv*)mbp->mb_udata, (const char **)(&src), &len, &dst, &outlen);
}
int
2002-01-04 05:39:37 +03:00
smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
int size, int caseopt)
{
2002-01-04 05:39:37 +03:00
struct iconv_drv *dp = vcp->vc_toserver;
if (size == 0)
return 0;
if (dp == NULL) {
2002-01-04 05:39:37 +03:00
return mb_put_mem(mbp, (caddr_t)src, size, MB_MSYSTEM);
}
2002-01-04 05:39:37 +03:00
mbp->mb_copy = smb_copy_iconv;
mbp->mb_udata = dp;
return mb_put_mem(mbp, (caddr_t)src, size, MB_MCUSTOM);
}
int
2002-01-04 05:39:37 +03:00
smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
int caseopt)
{
int error;
2002-01-04 05:39:37 +03:00
error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
if (error)
return error;
2002-01-04 05:39:37 +03:00
return mb_put_uint8(mbp, 0);
}
#if 0
int
smb_put_asunistring(struct smb_rq *rqp, const char *src)
{
2002-01-04 05:39:37 +03:00
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);
2002-01-04 05:39:37 +03:00
error = mb_put_uint16le(mbp, c);
if (error)
return error;
}
2002-01-04 05:39:37 +03:00
return mb_put_uint16le(mbp, 0);
}
#endif
struct sockaddr *
2002-01-04 05:39:37 +03:00
dup_sockaddr(struct sockaddr *sa, int canwait)
{
2002-01-04 05:39:37 +03:00
struct sockaddr *sa2;
2002-01-04 05:39:37 +03:00
sa2 = malloc(sa->sa_len, M_SONAME, canwait ? M_WAITOK : M_NOWAIT);
if (sa2)
memcpy(sa2, sa, sa->sa_len);
return sa2;
}