2007-02-10 00:55:00 +03:00
|
|
|
/* $NetBSD: smb_subr.c,v 1.30 2007/02/09 21:55:36 ad Exp $ */
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
/*
|
2002-01-04 05:39:37 +03:00
|
|
|
* Copyright (c) 2000-2001 Boris Popov
|
2000-12-07 06:48:09 +03:00
|
|
|
* 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
|
|
|
*
|
2003-03-24 11:20:01 +03:00
|
|
|
* FreeBSD: src/sys/netsmb/smb_subr.c,v 1.6 2002/04/17 03:14:28 bp Exp
|
2000-12-07 06:48:09 +03:00
|
|
|
*/
|
2003-02-25 12:12:11 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: smb_subr.c,v 1.30 2007/02/09 21:55:36 ad Exp $");
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
#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>
|
2000-12-07 06:48:09 +03:00
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/mbuf.h>
|
2003-02-16 02:02:30 +03:00
|
|
|
#include <sys/socketvar.h> /* for M_SONAME */
|
2006-05-15 01:19:33 +04:00
|
|
|
#include <sys/kauth.h>
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
#include <netsmb/iconv.h>
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
#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;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2003-02-01 09:23:35 +03:00
|
|
|
static MALLOC_DEFINE(M_SMBSTR, "smbstr", "SMB strings");
|
2003-02-16 02:02:30 +03:00
|
|
|
MALLOC_DEFINE(M_SMBTEMP, "smbtemp", "Temp netsmb data");
|
2003-02-01 09:23:35 +03:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
void
|
2006-05-15 01:19:33 +04:00
|
|
|
smb_makescred(struct smb_cred *scred, struct lwp *l, kauth_cred_t cred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
if (l) {
|
|
|
|
scred->scr_l = l;
|
2006-07-24 02:06:03 +04:00
|
|
|
scred->scr_cred = cred ? cred : l->l_cred;
|
2000-12-07 06:48:09 +03:00
|
|
|
} else {
|
2005-12-11 15:16:03 +03:00
|
|
|
scred->scr_l = NULL;
|
2000-12-07 06:48:09 +03:00
|
|
|
scred->scr_cred = cred ? cred : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
smb_proc_intr(struct lwp *l)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p;
|
2007-02-10 00:55:00 +03:00
|
|
|
int error;
|
2005-12-11 15:16:03 +03:00
|
|
|
|
|
|
|
if (l == NULL)
|
2000-12-07 06:48:09 +03:00
|
|
|
return 0;
|
2005-12-11 15:16:03 +03:00
|
|
|
p = l->l_proc;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
mutex_enter(&p->p_smutex);
|
|
|
|
error = sigispending(l, 0);
|
|
|
|
mutex_exit(&p->p_smutex);
|
|
|
|
|
|
|
|
return (error != 0 ? EINTR : 0);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2000-12-07 06:48:09 +03:00
|
|
|
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);
|
2000-12-07 06:48:09 +03:00
|
|
|
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);
|
2000-12-07 06:48:09 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
void
|
|
|
|
smb_strtouni(u_int16_t *dst, const char *src)
|
|
|
|
{
|
|
|
|
while (*src) {
|
2003-02-18 13:18:52 +03:00
|
|
|
*dst++ = htole16(*src++);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
*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;
|
2003-03-23 19:55:54 +03:00
|
|
|
case ERRnofiles:
|
2000-12-07 06:48:09 +03:00
|
|
|
case ERRbadfile:
|
|
|
|
case ERRbadpath:
|
|
|
|
case ERRremcd:
|
2003-03-24 11:39:17 +03:00
|
|
|
case ERRnoipc: /* nt returns it when share not available */
|
|
|
|
case ERRnosuchshare: /* observed from nt4sp6 when sharename wrong */
|
2000-12-07 06:48:09 +03: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:
|
2002-01-04 05:39:37 +03:00
|
|
|
case ERRnotready: /* nt */
|
2000-12-07 06:48:09 +03:00
|
|
|
return ENXIO;
|
|
|
|
case ERRdiffdevice:
|
|
|
|
return EXDEV;
|
|
|
|
case ERRlock:
|
|
|
|
return EDEADLK;
|
|
|
|
case ERRfilexists:
|
|
|
|
return EEXIST;
|
2003-03-24 11:39:17 +03:00
|
|
|
case ERRinvalidname: /* dunno what is it, but samba maps as noent */
|
2000-12-07 06:48:09 +03:00
|
|
|
return ENOENT;
|
2003-02-18 13:09:28 +03:00
|
|
|
case ERRdirnempty: /* samba */
|
2000-12-07 06:48:09 +03:00
|
|
|
return ENOTEMPTY;
|
2003-03-24 11:39:17 +03:00
|
|
|
case ERRrename:
|
2000-12-07 06:48:09 +03:00
|
|
|
return EEXIST;
|
2003-03-24 11:20:01 +03:00
|
|
|
case ERRquota:
|
|
|
|
return EDQUOT;
|
2003-03-24 13:05:43 +03:00
|
|
|
case ERRnotlocked:
|
2005-06-28 07:24:32 +04:00
|
|
|
/* it's okay to try to unlock already unlocked file */
|
|
|
|
return 0;
|
2003-04-07 15:13:24 +04:00
|
|
|
case NT_STATUS_NOTIFY_ENUM_DIR:
|
|
|
|
return EMSGSIZE;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ERRSRV:
|
|
|
|
switch (eno) {
|
|
|
|
case ERRerror:
|
|
|
|
return EINVAL;
|
|
|
|
case ERRbadpw:
|
2003-03-24 11:20:01 +03:00
|
|
|
case ERRpasswordExpired:
|
2003-09-27 16:24:25 +04:00
|
|
|
case ERRbaduid:
|
2000-12-07 06:48:09 +03:00
|
|
|
return EAUTH;
|
|
|
|
case ERRaccess:
|
|
|
|
return EACCES;
|
|
|
|
case ERRinvnid:
|
|
|
|
return ENETRESET;
|
|
|
|
case ERRinvnetname:
|
|
|
|
SMBERROR("NetBIOS name is invalid\n");
|
|
|
|
return EAUTH;
|
2003-03-24 11:39:17 +03:00
|
|
|
case ERRbadtype: /* reserved and returned */
|
2000-12-07 06:48:09 +03:00
|
|
|
return EIO;
|
2003-03-24 11:20:01 +03:00
|
|
|
case ERRaccountExpired:
|
|
|
|
case ERRbadClient:
|
|
|
|
case ERRbadLogonTime:
|
2000-12-07 06:48:09 +03:00
|
|
|
return EPERM;
|
2003-03-24 11:20:01 +03:00
|
|
|
case ERRnosupport:
|
|
|
|
return EBADRPC;
|
2000-12-07 06:48:09 +03: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;
|
2003-04-07 15:13:24 +04:00
|
|
|
case ERRgeneral:
|
|
|
|
/* returned e.g. for NT CANCEL SMB by Samba */
|
|
|
|
return ECANCELED;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SMBERROR("Unmapped error %d:%d\n", eclass, eno);
|
|
|
|
return EBADRPC;
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
static int
|
2005-05-30 01:26:27 +04:00
|
|
|
smb_copy_iconv(struct mbchain *mbp, const char *src, char *dst, size_t len)
|
2002-01-04 05:39:37 +03:00
|
|
|
{
|
2003-02-19 15:01:37 +03:00
|
|
|
size_t outlen = len;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2005-05-30 01:26:27 +04:00
|
|
|
return iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &len, &dst, &outlen);
|
2002-01-04 05:39:37 +03:00
|
|
|
}
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
|
2006-11-16 04:32:37 +03:00
|
|
|
int size, int caseopt)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct iconv_drv *dp = vcp->vc_toserver;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
if (dp == NULL) {
|
2005-05-30 01:26:27 +04:00
|
|
|
return mb_put_mem(mbp, (const void *)src, size, MB_MSYSTEM);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
mbp->mb_copy = smb_copy_iconv;
|
|
|
|
mbp->mb_udata = dp;
|
2005-05-30 01:26:27 +04:00
|
|
|
return mb_put_mem(mbp, (const void *)src, size, MB_MCUSTOM);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
|
2000-12-07 06:48:09 +03:00
|
|
|
int caseopt)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
2002-01-04 05:39:37 +03:00
|
|
|
return mb_put_uint8(mbp, 0);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2003-03-04 00:16:02 +03:00
|
|
|
#if 0
|
2000-12-07 06:48:09 +03:00
|
|
|
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;
|
2000-12-07 06:48:09 +03:00
|
|
|
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);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
return mb_put_uint16le(mbp, 0);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
2003-03-04 00:16:02 +03:00
|
|
|
#endif
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
struct sockaddr *
|
2002-01-04 05:39:37 +03:00
|
|
|
dup_sockaddr(struct sockaddr *sa, int canwait)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct sockaddr *sa2;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
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;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|