1/*
2 * Copyright (c) 2000-2001, Boris Popov
3 * All rights reserved.
4 *
5 * Portions Copyright (C) 2001 - 2013 Apple Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *    This product includes software developed by Boris Popov.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35#ifndef _NETSMB_SMB_SUBR_H_
36#define _NETSMB_SMB_SUBR_H_
37
38#ifndef _KERNEL
39#error "This file shouldn't be included from userland programs"
40#endif
41
42#ifdef MALLOC_DECLARE
43MALLOC_DECLARE(M_SMBTEMP);
44#endif
45
46
47#define SMB_NO_LOG_LEVEL		0x00
48#define SMB_LOW_LOG_LEVEL		0x01
49#define SMB_ACL_LOG_LEVEL		0x02
50#define SMB_IO_LOG_LEVEL		0x04
51#define SMB_AUTH_LOG_LEVEL		0x08
52#define SMB_KTRACE_LOG_LEVEL    0x10
53
54extern int smbfs_loglevel;
55
56#ifdef SMB_DEBUG
57#define SMBDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
58
59#define SMBDEBUG_LOCK(np, format, args...) do { \
60    lck_rw_lock_shared(&(np)->n_name_rwlock); \
61    printf("%s: "format, __FUNCTION__ ,## args); \
62    lck_rw_unlock_shared(&(np)->n_name_rwlock); \
63    } while(0)
64
65#ifdef DEBUG_SYMBOLIC_LINKS
66#define SMBSYMDEBUG SMBDEBUG
67#define SMBSYMDEBUG_LOCK SMBDEBUG_LOCK
68#else // DEBUG_SYMBOLIC_LINKS
69#define SMBSYMDEBUG(format, args...)
70#define SMBSYMDEBUG_LOCK(np, format, args...)
71#endif // DEBUG_SYMBOLIC_LINKS
72#else // SMB_DEBUG
73#define SMBDEBUG(format, args...)
74#define SMBDEBUG_LOCK(np, format, args...)
75#define SMBSYMDEBUG(format, args...)
76#define SMBSYMDEBUG_LOCK(np, format, args...)
77#endif // SMB_DEBUG
78
79#define SMBERROR(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
80
81#define SMBERROR_LOCK(np, format, args...) do { \
82    lck_rw_lock_shared(&(np)->n_name_rwlock); \
83    printf("%s: "format, __FUNCTION__ ,## args); \
84    lck_rw_unlock_shared(&(np)->n_name_rwlock); \
85    } while(0)
86
87#define SMBWARNING_LOCK(np, format, args...) do { \
88    if (smbfs_loglevel) { \
89        lck_rw_lock_shared(&(np)->n_name_rwlock); \
90        printf("%s: "format, __FUNCTION__ ,## args); \
91        lck_rw_unlock_shared(&(np)->n_name_rwlock); \
92    } \
93    } while(0)
94
95#define SMBWARNING(format, args...) do { \
96    if (smbfs_loglevel) \
97        printf("%s: "format, __FUNCTION__ ,## args); \
98    } while(0)
99
100#define SMB_LOG_AUTH_LOCK(np, format, args...) do { \
101    if (smbfs_loglevel & SMB_AUTH_LOG_LEVEL) { \
102        lck_rw_lock_shared(&(np)->n_name_rwlock); \
103        printf("%s: "format, __FUNCTION__ ,## args); \
104        lck_rw_unlock_shared(&(np)->n_name_rwlock); \
105    } \
106    } while(0)
107
108#define SMB_LOG_AUTH(format, args...) do { \
109    if (smbfs_loglevel & SMB_AUTH_LOG_LEVEL) \
110        printf("%s: "format, __FUNCTION__ ,## args); \
111    } while(0)
112
113#define SMB_LOG_ACCESS_LOCK(np, format, args...) do { \
114    if (smbfs_loglevel & SMB_ACL_LOG_LEVEL) { \
115        lck_rw_lock_shared(&(np)->n_name_rwlock); \
116        printf("%s: "format, __FUNCTION__ ,## args); \
117        lck_rw_unlock_shared(&(np)->n_name_rwlock); \
118    } \
119    } while(0)
120
121#define SMB_LOG_ACCESS(format, args...) do { \
122    if (smbfs_loglevel & SMB_ACL_LOG_LEVEL) \
123        printf("%s: "format, __FUNCTION__ ,## args); \
124    } while(0)
125
126#define SMB_LOG_IO_LOCK(np, format, args...) do { \
127    if (smbfs_loglevel & SMB_IO_LOG_LEVEL) { \
128        lck_rw_lock_shared(&(np)->n_name_rwlock); \
129        printf("%s: "format, __FUNCTION__ ,## args); \
130        lck_rw_unlock_shared(&(np)->n_name_rwlock); \
131    } \
132   } while(0)
133
134#define SMB_LOG_IO(format, args...) do { \
135    if (smbfs_loglevel & SMB_IO_LOG_LEVEL) \
136        printf("%s: "format, __FUNCTION__ ,## args); \
137    } while(0)
138
139#define SMB_LOG_KTRACE(args...) do { \
140    if (smbfs_loglevel & SMB_KTRACE_LOG_LEVEL) \
141        KERNEL_DEBUG_CONSTANT(args); \
142    } while(0)
143
144#define SMB_ASSERT(a) { \
145	if (!(a)) { \
146		panic("File "__FILE__", line %d: assertion '%s' failed.\n", \
147		__LINE__, #a); \
148	} \
149}
150
151#ifdef SMB_DEBUG
152#define DBG_ASSERT SMB_ASSERT
153#define DBG_LOCKLIST_ASSERT(lock_cnt, lock_order) { \
154	int jj, kk; \
155	for (jj=0; jj < lock_cnt; jj++) \
156		for (kk=0; kk < lock_cnt; kk++)	\
157			if ((jj != kk) && (lock_order[jj] == lock_order[kk]))	\
158				panic("lock_order[%d] == lock_order[%d]", jj, kk); \
159}
160
161#else // SMB_DEBUG
162#define DBG_ASSERT(a)
163#define DBG_LOCKLIST_ASSERT(lock_cnt, lock_order)
164#endif // SMB_DEBUG
165
166#ifdef SMB_SOCKET_DEBUG
167#define SMBSDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
168#else
169#define SMBSDEBUG(format, args...)
170#endif
171
172#ifdef SMB_IOD_DEBUG
173#define SMBIODEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
174#else
175#define SMBIODEBUG(format, args...)
176#endif
177
178#ifdef SMB_SOCKETDATA_DEBUG
179void m_dumpm(mbuf_t m);
180#else
181#define m_dumpm(m)
182#endif
183
184#define	SMB_SIGMASK	(sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGKILL)| \
185			 sigmask(SIGHUP)|sigmask(SIGQUIT))
186
187/*
188 * Compatibility wrappers for simple locks
189 */
190
191#include <sys/lock.h>
192
193typedef uint16_t	smb_unichar;
194typedef	smb_unichar	*smb_uniptr;
195
196struct mbchain;
197struct mdchain;
198struct smb_vc;
199struct smb_rq;
200
201#ifdef SMB_DEBUG
202void smb_hexdump(const char *func, const char *s, unsigned char *buf, size_t inlen);
203#else // SMB_DEBUG
204#define smb_hexdump(a,b,c,d)
205#endif // SMB_DEBUG
206char *smb_strndup(const char *s, size_t maxlen);
207void *smb_memdup(const void *umem, int len);
208void *smb_memdupin(user_addr_t umem, int len);
209
210void smb_reset_sig(struct smb_vc *vcp);
211
212int  smb_lmresponse(const u_char *apwd, u_char *C8, u_char *RN);
213void  smb_ntlmresponse(const u_char *apwd, u_char *C8, u_char *RN);
214
215int smb_ntlmv2hash(const u_char *apwd, const u_char *user,
216                   const u_char *destination, u_char *v2hash);
217int smb_ntlmv2response(u_char *v2hash, u_char *C8, const u_char *blob,
218                       size_t bloblen, u_char **RN, size_t *RNlen);
219
220
221
222void *make_target_info(struct smb_vc *vcp, uint16_t *target_len);
223uint32_t smb_errClassCodes_to_ntstatus(uint8_t errClass, uint16_t errCode);
224uint32_t smb_ntstatus_to_errno(uint32_t ntstatus);
225int smb_put_dmem(struct mbchain *mbp, const char *src, size_t srcSize,
226				 int flags, int usingUnicode, size_t *lenp);
227int smb_put_dstring(struct mbchain *mbp, int usingUnicode, const char *src,
228					size_t maxlen, int flags);
229int  smb_put_string(struct smb_rq *rqp, const char *src);
230int  smb_put_asunistring(struct smb_rq *rqp, const char *src);
231struct sockaddr *smb_dup_sockaddr(struct sockaddr *sa, int canwait);
232int  smb_rq_sign(struct smb_rq *rqp);
233int  smb_rq_verify(struct smb_rq *rqp);
234int  smb2_rq_sign(struct smb_rq *rqp);
235int  smb2_rq_verify(struct smb_rq *rqp, struct mdchain *mdp, uint8_t *signature);
236int  smb3_derive_keys(struct smb_vc *vcp);
237int  smb3_rq_encrypt(struct smb_rq *rqp, mbuf_t *m);
238int  smb3_msg_decrypt(struct smb_vc *vcp, mbuf_t *m);
239#endif /* !_NETSMB_SMB_SUBR_H_ */
240