xref: /freebsd/sys/netsmb/smb_subr.h (revision 681a5bbef259fc57ece764cdc43d57327ab65c47)
1681a5bbeSBoris Popov /*
2681a5bbeSBoris Popov  * Copyright (c) 2000-2001, Boris Popov
3681a5bbeSBoris Popov  * All rights reserved.
4681a5bbeSBoris Popov  *
5681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
6681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
7681a5bbeSBoris Popov  * are met:
8681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
9681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
10681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
12681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
13681a5bbeSBoris Popov  * 3. All advertising materials mentioning features or use of this software
14681a5bbeSBoris Popov  *    must display the following acknowledgement:
15681a5bbeSBoris Popov  *    This product includes software developed by Boris Popov.
16681a5bbeSBoris Popov  * 4. Neither the name of the author nor the names of any co-contributors
17681a5bbeSBoris Popov  *    may be used to endorse or promote products derived from this software
18681a5bbeSBoris Popov  *    without specific prior written permission.
19681a5bbeSBoris Popov  *
20681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30681a5bbeSBoris Popov  * SUCH DAMAGE.
31681a5bbeSBoris Popov  *
32681a5bbeSBoris Popov  * $FreeBSD$
33681a5bbeSBoris Popov  */
34681a5bbeSBoris Popov #ifndef _NETSMB_SMB_SUBR_H_
35681a5bbeSBoris Popov #define _NETSMB_SMB_SUBR_H_
36681a5bbeSBoris Popov 
37681a5bbeSBoris Popov #ifndef _KERNEL
38681a5bbeSBoris Popov #error "This file shouldn't be included from userland programs"
39681a5bbeSBoris Popov #endif
40681a5bbeSBoris Popov 
41681a5bbeSBoris Popov #ifdef MALLOC_DECLARE
42681a5bbeSBoris Popov MALLOC_DECLARE(M_SMBTEMP);
43681a5bbeSBoris Popov #endif
44681a5bbeSBoris Popov 
45681a5bbeSBoris Popov #if __FreeBSD_version > 500000
46681a5bbeSBoris Popov #define	FB_CURRENT
47681a5bbeSBoris Popov #else
48681a5bbeSBoris Popov #  if __FreeBSD_version > 400000
49681a5bbeSBoris Popov #  define	FB_RELENG4
50681a5bbeSBoris Popov #  else
51681a5bbeSBoris Popov #  error "Unsupported version of FreeBSD"
52681a5bbeSBoris Popov #  endif
53681a5bbeSBoris Popov #endif
54681a5bbeSBoris Popov 
55681a5bbeSBoris Popov #define SMBERROR(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
56681a5bbeSBoris Popov #define SMBPANIC(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
57681a5bbeSBoris Popov 
58681a5bbeSBoris Popov #ifdef SMB_SOCKET_DEBUG
59681a5bbeSBoris Popov #define SMBSDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
60681a5bbeSBoris Popov #else
61681a5bbeSBoris Popov #define SMBSDEBUG(format, args...)
62681a5bbeSBoris Popov #endif
63681a5bbeSBoris Popov 
64681a5bbeSBoris Popov #ifdef SMB_IOD_DEBUG
65681a5bbeSBoris Popov #define SMBIODEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
66681a5bbeSBoris Popov #else
67681a5bbeSBoris Popov #define SMBIODEBUG(format, args...)
68681a5bbeSBoris Popov #endif
69681a5bbeSBoris Popov 
70681a5bbeSBoris Popov #ifdef SMB_SOCKETDATA_DEBUG
71681a5bbeSBoris Popov void m_dumpm(struct mbuf *m);
72681a5bbeSBoris Popov #else
73681a5bbeSBoris Popov #define m_dumpm(m)
74681a5bbeSBoris Popov #endif
75681a5bbeSBoris Popov 
76681a5bbeSBoris Popov #if __FreeBSD_version > 400009
77681a5bbeSBoris Popov #define	SMB_SIGMASK(set) 						\
78681a5bbeSBoris Popov 	(SIGISMEMBER(set, SIGINT) || SIGISMEMBER(set, SIGTERM) ||	\
79681a5bbeSBoris Popov 	 SIGISMEMBER(set, SIGHUP) || SIGISMEMBER(set, SIGKILL) ||	\
80681a5bbeSBoris Popov 	 SIGISMEMBER(set, SIGQUIT))
81681a5bbeSBoris Popov 
82681a5bbeSBoris Popov #define	smb_suser(cred)	suser_xxx(cred, NULL, 0)
83681a5bbeSBoris Popov #else
84681a5bbeSBoris Popov #define	SMB_SIGMASK	(sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGKILL)| \
85681a5bbeSBoris Popov 			 sigmask(SIGHUP)|sigmask(SIGQUIT))
86681a5bbeSBoris Popov 
87681a5bbeSBoris Popov #define	smb_suser(cred)	suser((cred), NULL)
88681a5bbeSBoris Popov #endif
89681a5bbeSBoris Popov 
90681a5bbeSBoris Popov /*
91681a5bbeSBoris Popov  * Compatibility wrappers for simple locks
92681a5bbeSBoris Popov  */
93681a5bbeSBoris Popov #if __FreeBSD_version < 500000
94681a5bbeSBoris Popov 
95681a5bbeSBoris Popov #include <sys/lock.h>
96681a5bbeSBoris Popov 
97681a5bbeSBoris Popov #define	lockdestroy(lock)
98681a5bbeSBoris Popov #define	smb_slock			simplelock
99681a5bbeSBoris Popov #define	smb_sl_init(mtx, desc)		simple_lock_init(mtx)
100681a5bbeSBoris Popov #define	smb_sl_destroy(mtx)
101681a5bbeSBoris Popov #define	smb_sl_lock(mtx)		simple_lock(mtx)
102681a5bbeSBoris Popov #define	smb_sl_unlock(mtx)		simple_unlock(mtx)
103681a5bbeSBoris Popov /*
104681a5bbeSBoris Popov #define	mtx				lock
105681a5bbeSBoris Popov #define	mtx_init(mtx, desc, flags)	lockinit(mtx, PWAIT, desc, 0, 0)
106681a5bbeSBoris Popov #define	mtx_lock(mtx)			lockmgr(mtx, LK_EXCLUSIVE, NULL, curproc)
107681a5bbeSBoris Popov #define	mtx_unlock(mtx)			lockmgr(mtx, LK_RELEASE, NULL, curproc)
108681a5bbeSBoris Popov #define	mtx_destroy(mtx)
109681a5bbeSBoris Popov */
110681a5bbeSBoris Popov #else
111681a5bbeSBoris Popov 
112681a5bbeSBoris Popov #include <sys/mutex.h>
113681a5bbeSBoris Popov 
114681a5bbeSBoris Popov #define	smb_slock			mtx
115681a5bbeSBoris Popov #define	smb_sl_init(mtx, desc)		mtx_init(mtx, desc, MTX_DEF)
116681a5bbeSBoris Popov #define	smb_sl_destroy(mtx)		mtx_destroy(mtx)
117681a5bbeSBoris Popov #define	smb_sl_lock(mtx)		mtx_lock(mtx)
118681a5bbeSBoris Popov #define	smb_sl_unlock(mtx)		mtx_unlock(mtx)
119681a5bbeSBoris Popov 
120681a5bbeSBoris Popov #endif
121681a5bbeSBoris Popov 
122681a5bbeSBoris Popov #define SMB_STRFREE(p)	do { if (p) smb_strfree(p); } while(0)
123681a5bbeSBoris Popov 
124681a5bbeSBoris Popov /*
125681a5bbeSBoris Popov  * The simple try/catch/finally interface.
126681a5bbeSBoris Popov  * With GCC it is possible to allow more than one try/finally block per
127681a5bbeSBoris Popov  * function, but we'll avoid it to maintain portability.
128681a5bbeSBoris Popov  */
129681a5bbeSBoris Popov #define itry		{						\
130681a5bbeSBoris Popov 				__label__ _finlab, _catchlab;		\
131681a5bbeSBoris Popov 				int _tval;				\
132681a5bbeSBoris Popov 
133681a5bbeSBoris Popov #define icatch(var)							\
134681a5bbeSBoris Popov 				goto _finlab;				\
135681a5bbeSBoris Popov 				(void)&&_catchlab;			\
136681a5bbeSBoris Popov 				_catchlab:				\
137681a5bbeSBoris Popov 				var = _tval;
138681a5bbeSBoris Popov 
139681a5bbeSBoris Popov #define ifinally		(void)&&_finlab;			\
140681a5bbeSBoris Popov 				_finlab:
141681a5bbeSBoris Popov #define iendtry		}
142681a5bbeSBoris Popov 
143681a5bbeSBoris Popov #define inocatch							\
144681a5bbeSBoris Popov 				goto _finlab;				\
145681a5bbeSBoris Popov 				(void)&&_catchlab;			\
146681a5bbeSBoris Popov 				_catchlab:				\
147681a5bbeSBoris Popov 
148681a5bbeSBoris Popov #define ithrow(t)	do {						\
149681a5bbeSBoris Popov 				if ((_tval = (int)(t)) != 0)		\
150681a5bbeSBoris Popov 					goto _catchlab;			\
151681a5bbeSBoris Popov 			} while (0)
152681a5bbeSBoris Popov 
153681a5bbeSBoris Popov #define ierror(t,e)	do {						\
154681a5bbeSBoris Popov 				if (t) {				\
155681a5bbeSBoris Popov 					_tval = e;			\
156681a5bbeSBoris Popov 					goto _catchlab;			\
157681a5bbeSBoris Popov 				}					\
158681a5bbeSBoris Popov 			} while (0)
159681a5bbeSBoris Popov 
160681a5bbeSBoris Popov typedef u_int16_t	smb_unichar;
161681a5bbeSBoris Popov typedef	smb_unichar	*smb_uniptr;
162681a5bbeSBoris Popov 
163681a5bbeSBoris Popov /*
164681a5bbeSBoris Popov  * Crediantials of user/process being processing in the connection procedures
165681a5bbeSBoris Popov  */
166681a5bbeSBoris Popov struct smb_cred {
167681a5bbeSBoris Popov 	struct proc *	scr_p;
168681a5bbeSBoris Popov 	struct ucred *	scr_cred;
169681a5bbeSBoris Popov };
170681a5bbeSBoris Popov 
171681a5bbeSBoris Popov extern smb_unichar smb_unieol;
172681a5bbeSBoris Popov 
173681a5bbeSBoris Popov struct mbchain;
174681a5bbeSBoris Popov struct smb_vc;
175681a5bbeSBoris Popov struct smb_rq;
176681a5bbeSBoris Popov 
177681a5bbeSBoris Popov void smb_makescred(struct smb_cred *scred, struct proc *p, struct ucred *cred);
178681a5bbeSBoris Popov int  smb_proc_intr(struct proc *);
179681a5bbeSBoris Popov char *smb_strdup(const char *s);
180681a5bbeSBoris Popov void *smb_memdup(const void *umem, int len);
181681a5bbeSBoris Popov char *smb_strdupin(char *s, int maxlen);
182681a5bbeSBoris Popov void *smb_memdupin(void *umem, int len);
183681a5bbeSBoris Popov void smb_strtouni(u_int16_t *dst, const char *src);
184681a5bbeSBoris Popov void smb_strfree(char *s);
185681a5bbeSBoris Popov void smb_memfree(void *s);
186681a5bbeSBoris Popov void *smb_zmalloc(unsigned long size, struct malloc_type *type, int flags);
187681a5bbeSBoris Popov 
188681a5bbeSBoris Popov int  smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN);
189681a5bbeSBoris Popov int  smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN);
190681a5bbeSBoris Popov int  smb_maperror(int eclass, int eno);
191681a5bbeSBoris Popov int  smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp,
192681a5bbeSBoris Popov 	const char *src, int len, int caseopt);
193681a5bbeSBoris Popov int  smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp,
194681a5bbeSBoris Popov 	const char *src, int caseopt);
195681a5bbeSBoris Popov int  smb_put_string(struct smb_rq *rqp, const char *src);
196681a5bbeSBoris Popov int  smb_put_asunistring(struct smb_rq *rqp, const char *src);
197681a5bbeSBoris Popov 
198681a5bbeSBoris Popov #endif /* !_NETSMB_SMB_SUBR_H_ */
199