smb_subr.h revision 75374
175374Sbp/*
275374Sbp * Copyright (c) 2000-2001, Boris Popov
375374Sbp * All rights reserved.
475374Sbp *
575374Sbp * Redistribution and use in source and binary forms, with or without
675374Sbp * modification, are permitted provided that the following conditions
775374Sbp * are met:
875374Sbp * 1. Redistributions of source code must retain the above copyright
975374Sbp *    notice, this list of conditions and the following disclaimer.
1075374Sbp * 2. Redistributions in binary form must reproduce the above copyright
1175374Sbp *    notice, this list of conditions and the following disclaimer in the
1275374Sbp *    documentation and/or other materials provided with the distribution.
1375374Sbp * 3. All advertising materials mentioning features or use of this software
1475374Sbp *    must display the following acknowledgement:
1575374Sbp *    This product includes software developed by Boris Popov.
1675374Sbp * 4. Neither the name of the author nor the names of any co-contributors
1775374Sbp *    may be used to endorse or promote products derived from this software
1875374Sbp *    without specific prior written permission.
1975374Sbp *
2075374Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2175374Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2275374Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2375374Sbp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2475374Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2575374Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2675374Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2775374Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2875374Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2975374Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3075374Sbp * SUCH DAMAGE.
3175374Sbp *
3275374Sbp * $FreeBSD: head/sys/netsmb/smb_subr.h 75374 2001-04-10 07:59:06Z bp $
3375374Sbp */
3475374Sbp#ifndef _NETSMB_SMB_SUBR_H_
3575374Sbp#define _NETSMB_SMB_SUBR_H_
3675374Sbp
3775374Sbp#ifndef _KERNEL
3875374Sbp#error "This file shouldn't be included from userland programs"
3975374Sbp#endif
4075374Sbp
4175374Sbp#ifdef MALLOC_DECLARE
4275374SbpMALLOC_DECLARE(M_SMBTEMP);
4375374Sbp#endif
4475374Sbp
4575374Sbp#if __FreeBSD_version > 500000
4675374Sbp#define	FB_CURRENT
4775374Sbp#else
4875374Sbp#  if __FreeBSD_version > 400000
4975374Sbp#  define	FB_RELENG4
5075374Sbp#  else
5175374Sbp#  error "Unsupported version of FreeBSD"
5275374Sbp#  endif
5375374Sbp#endif
5475374Sbp
5575374Sbp#define SMBERROR(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
5675374Sbp#define SMBPANIC(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
5775374Sbp
5875374Sbp#ifdef SMB_SOCKET_DEBUG
5975374Sbp#define SMBSDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
6075374Sbp#else
6175374Sbp#define SMBSDEBUG(format, args...)
6275374Sbp#endif
6375374Sbp
6475374Sbp#ifdef SMB_IOD_DEBUG
6575374Sbp#define SMBIODEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
6675374Sbp#else
6775374Sbp#define SMBIODEBUG(format, args...)
6875374Sbp#endif
6975374Sbp
7075374Sbp#ifdef SMB_SOCKETDATA_DEBUG
7175374Sbpvoid m_dumpm(struct mbuf *m);
7275374Sbp#else
7375374Sbp#define m_dumpm(m)
7475374Sbp#endif
7575374Sbp
7675374Sbp#if __FreeBSD_version > 400009
7775374Sbp#define	SMB_SIGMASK(set) 						\
7875374Sbp	(SIGISMEMBER(set, SIGINT) || SIGISMEMBER(set, SIGTERM) ||	\
7975374Sbp	 SIGISMEMBER(set, SIGHUP) || SIGISMEMBER(set, SIGKILL) ||	\
8075374Sbp	 SIGISMEMBER(set, SIGQUIT))
8175374Sbp
8275374Sbp#define	smb_suser(cred)	suser_xxx(cred, NULL, 0)
8375374Sbp#else
8475374Sbp#define	SMB_SIGMASK	(sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGKILL)| \
8575374Sbp			 sigmask(SIGHUP)|sigmask(SIGQUIT))
8675374Sbp
8775374Sbp#define	smb_suser(cred)	suser((cred), NULL)
8875374Sbp#endif
8975374Sbp
9075374Sbp/*
9175374Sbp * Compatibility wrappers for simple locks
9275374Sbp */
9375374Sbp#if __FreeBSD_version < 500000
9475374Sbp
9575374Sbp#include <sys/lock.h>
9675374Sbp
9775374Sbp#define	lockdestroy(lock)
9875374Sbp#define	smb_slock			simplelock
9975374Sbp#define	smb_sl_init(mtx, desc)		simple_lock_init(mtx)
10075374Sbp#define	smb_sl_destroy(mtx)
10175374Sbp#define	smb_sl_lock(mtx)		simple_lock(mtx)
10275374Sbp#define	smb_sl_unlock(mtx)		simple_unlock(mtx)
10375374Sbp/*
10475374Sbp#define	mtx				lock
10575374Sbp#define	mtx_init(mtx, desc, flags)	lockinit(mtx, PWAIT, desc, 0, 0)
10675374Sbp#define	mtx_lock(mtx)			lockmgr(mtx, LK_EXCLUSIVE, NULL, curproc)
10775374Sbp#define	mtx_unlock(mtx)			lockmgr(mtx, LK_RELEASE, NULL, curproc)
10875374Sbp#define	mtx_destroy(mtx)
10975374Sbp*/
11075374Sbp#else
11175374Sbp
11275374Sbp#include <sys/mutex.h>
11375374Sbp
11475374Sbp#define	smb_slock			mtx
11575374Sbp#define	smb_sl_init(mtx, desc)		mtx_init(mtx, desc, MTX_DEF)
11675374Sbp#define	smb_sl_destroy(mtx)		mtx_destroy(mtx)
11775374Sbp#define	smb_sl_lock(mtx)		mtx_lock(mtx)
11875374Sbp#define	smb_sl_unlock(mtx)		mtx_unlock(mtx)
11975374Sbp
12075374Sbp#endif
12175374Sbp
12275374Sbp#define SMB_STRFREE(p)	do { if (p) smb_strfree(p); } while(0)
12375374Sbp
12475374Sbp/*
12575374Sbp * The simple try/catch/finally interface.
12675374Sbp * With GCC it is possible to allow more than one try/finally block per
12775374Sbp * function, but we'll avoid it to maintain portability.
12875374Sbp */
12975374Sbp#define itry		{						\
13075374Sbp				__label__ _finlab, _catchlab;		\
13175374Sbp				int _tval;				\
13275374Sbp
13375374Sbp#define icatch(var)							\
13475374Sbp				goto _finlab;				\
13575374Sbp				(void)&&_catchlab;			\
13675374Sbp				_catchlab:				\
13775374Sbp				var = _tval;
13875374Sbp
13975374Sbp#define ifinally		(void)&&_finlab;			\
14075374Sbp				_finlab:
14175374Sbp#define iendtry		}
14275374Sbp
14375374Sbp#define inocatch							\
14475374Sbp				goto _finlab;				\
14575374Sbp				(void)&&_catchlab;			\
14675374Sbp				_catchlab:				\
14775374Sbp
14875374Sbp#define ithrow(t)	do {						\
14975374Sbp				if ((_tval = (int)(t)) != 0)		\
15075374Sbp					goto _catchlab;			\
15175374Sbp			} while (0)
15275374Sbp
15375374Sbp#define ierror(t,e)	do {						\
15475374Sbp				if (t) {				\
15575374Sbp					_tval = e;			\
15675374Sbp					goto _catchlab;			\
15775374Sbp				}					\
15875374Sbp			} while (0)
15975374Sbp
16075374Sbptypedef u_int16_t	smb_unichar;
16175374Sbptypedef	smb_unichar	*smb_uniptr;
16275374Sbp
16375374Sbp/*
16475374Sbp * Crediantials of user/process being processing in the connection procedures
16575374Sbp */
16675374Sbpstruct smb_cred {
16775374Sbp	struct proc *	scr_p;
16875374Sbp	struct ucred *	scr_cred;
16975374Sbp};
17075374Sbp
17175374Sbpextern smb_unichar smb_unieol;
17275374Sbp
17375374Sbpstruct mbchain;
17475374Sbpstruct smb_vc;
17575374Sbpstruct smb_rq;
17675374Sbp
17775374Sbpvoid smb_makescred(struct smb_cred *scred, struct proc *p, struct ucred *cred);
17875374Sbpint  smb_proc_intr(struct proc *);
17975374Sbpchar *smb_strdup(const char *s);
18075374Sbpvoid *smb_memdup(const void *umem, int len);
18175374Sbpchar *smb_strdupin(char *s, int maxlen);
18275374Sbpvoid *smb_memdupin(void *umem, int len);
18375374Sbpvoid smb_strtouni(u_int16_t *dst, const char *src);
18475374Sbpvoid smb_strfree(char *s);
18575374Sbpvoid smb_memfree(void *s);
18675374Sbpvoid *smb_zmalloc(unsigned long size, struct malloc_type *type, int flags);
18775374Sbp
18875374Sbpint  smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN);
18975374Sbpint  smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN);
19075374Sbpint  smb_maperror(int eclass, int eno);
19175374Sbpint  smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp,
19275374Sbp	const char *src, int len, int caseopt);
19375374Sbpint  smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp,
19475374Sbp	const char *src, int caseopt);
19575374Sbpint  smb_put_string(struct smb_rq *rqp, const char *src);
19675374Sbpint  smb_put_asunistring(struct smb_rq *rqp, const char *src);
19775374Sbp
19875374Sbp#endif /* !_NETSMB_SMB_SUBR_H_ */
199