xref: /onnv-gate/usr/src/lib/smbsrv/libsmbrdr/common/smbrdr.h (revision 10966:37e5dcdf36d3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SMBRDR_H_
27 #define	_SMBRDR_H_
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <syslog.h>
32 #include <synch.h>
33 #include <sys/types.h>
34 
35 #include <smbsrv/libsmb.h>
36 #include <smbsrv/libsmbrdr.h>
37 #include <smbsrv/smb.h>
38 #include <smbsrv/smbinfo.h>
39 #include <smbsrv/smb.h>
40 #include <smbsrv/wintypes.h>
41 
42 #define	SMBRDR_REQ_BUFSZ	4096
43 
44 #define	MAX_ACCOUNT_NAME	32
45 #define	MAX_SHARE_NAME		32
46 #define	MAX_SCOPE_NAME		64
47 #define	MAX_FILE_PATH		128
48 
49 /*
50  * The number of shares and pipes is limited to 48 based on the note
51  * below. This really shouldn't cause a problem because we always
52  * our shares and named pipes are always opened and closed round every
53  * RPC transaction. This also tends to limit the number of active
54  * logons because we (currently) need two named pipes per logon.
55  *
56  * Q141709 Limit of 49 named pipe connections from a single workstation.
57  * If a named pipe server creates more than 49 distincly named pipes, a
58  * single client cannot connect more than 49 pipes on the named pipe
59  * server. Chapter 4, p113. Network Programming for Microsoft Windows
60  * Anthony Jones and Jim Ohlund, Microsoft Press, ISBN: 0-7356-0560-2
61  */
62 #define	N_NETUSE_TABLE		256
63 #define	N_OFILE_TABLE		256
64 
65 /*
66  * Logon's states
67  */
68 #define	SDB_LSTATE_START	0
69 #define	SDB_LSTATE_INIT		1
70 #define	SDB_LSTATE_LOGGING_OFF	2
71 #define	SDB_LSTATE_SETUP	3
72 
73 #define	SDB_LOGON_NONE		0
74 #define	SDB_LOGON_GUEST		1
75 #define	SDB_LOGON_ANONYMOUS	2
76 #define	SDB_LOGON_USER		3
77 
78 typedef struct sdb_logon {
79 	struct sdb_session *session;
80 	char username[MAX_ACCOUNT_NAME];
81 	unsigned short uid;
82 	unsigned int type;
83 	unsigned short state;
84 	smb_auth_info_t auth;
85 	unsigned char ssn_key[SMBAUTH_SESSION_KEY_SZ];
86 } sdb_logon_t;
87 
88 /*
89  * Session's states
90  *
91  *   SDB_SSTATE_START             ready to be used
92  *   SDB_SSTATE_INIT              initialized
93  *   SDB_SSTATE_STALE             lost transport connection
94  *   SDB_SSTATE_DISCONNECTING     disconnecting: logoff the user
95  *                                disconnect trees, close files
96  *   SDB_SSTATE_CLEANING          was in STALE state now just
97  *                                cleaning up
98  *   SDB_SSTATE_CONNECTED         got transport connection
99  *   SDB_SSTATE_NEGOTIATED        did SMB negotiate
100  */
101 #define	SDB_SSTATE_START		0
102 #define	SDB_SSTATE_INIT			1
103 #define	SDB_SSTATE_STALE		2
104 #define	SDB_SSTATE_DISCONNECTING	3
105 #define	SDB_SSTATE_CLEANING		4
106 #define	SDB_SSTATE_CONNECTED		5
107 #define	SDB_SSTATE_NEGOTIATED		6
108 
109 #define	SDB_SLCK_READ   1
110 #define	SDB_SLCK_WRITE  2
111 
112 struct sdb_session {
113 	char srv_name[MAXHOSTNAMELEN];
114 	smb_inaddr_t srv_ipaddr;
115 	char domain[MAXHOSTNAMELEN];
116 	char scope[SMB_PI_MAX_SCOPE];
117 	char native_os[SMB_PI_MAX_NATIVE_OS];
118 	char native_lanman[SMB_PI_MAX_LANMAN];
119 	int sock;
120 	short port;
121 	uint16_t secmode;
122 	uint32_t sesskey;
123 	uint32_t challenge_len;
124 	uint8_t challenge_key[32];
125 	uint8_t smb_flags;
126 	uint16_t smb_flags2;
127 	uint16_t vc;
128 	uint32_t remote_caps;
129 	uint8_t state;
130 	uint32_t sid;	/* session id */
131 	int remote_os;
132 	int remote_lm;
133 	int pdc_type;
134 	smb_sign_ctx_t sign_ctx;
135 	sdb_logon_t logon;
136 	rwlock_t rwl;
137 };
138 
139 /*
140  * Netuse's states
141  */
142 #define	SDB_NSTATE_START		0
143 #define	SDB_NSTATE_INIT			1
144 #define	SDB_NSTATE_DISCONNECTING	2
145 #define	SDB_NSTATE_CONNECTED		3
146 
147 struct sdb_netuse {
148 	struct sdb_session *session;
149 	unsigned short state;
150 	int letter;		/* local identity */
151 	unsigned int sid;
152 	unsigned short uid;
153 	unsigned short tid;		/* remote identity */
154 	char share[MAX_SHARE_NAME];
155 	mutex_t mtx;
156 };
157 
158 /*
159  * Ofile's states
160  */
161 #define	SDB_FSTATE_START	0
162 #define	SDB_FSTATE_INIT		1
163 #define	SDB_FSTATE_CLOSING	2
164 #define	SDB_FSTATE_OPEN		3
165 
166 struct sdb_ofile {
167 	struct sdb_session *session;
168 	struct sdb_netuse *netuse;
169 	unsigned short state;
170 	unsigned int sid;
171 	unsigned short uid;
172 	unsigned short tid;
173 	unsigned short fid;		/* remote identity */
174 	char path[MAX_FILE_PATH];
175 	mutex_t mtx;
176 };
177 
178 typedef struct smbrdr_handle {
179 	unsigned char *srh_buf;
180 	smb_msgbuf_t srh_mbuf;
181 	unsigned int srh_mbflags;
182 	unsigned char srh_cmd;
183 	struct sdb_session *srh_session;
184 	struct sdb_logon *srh_user;
185 	struct sdb_netuse *srh_tree;
186 } smbrdr_handle_t;
187 
188 typedef struct smb_nt_negotiate_rsp {
189 	uint8_t word_count;
190 	uint16_t dialect_index;
191 	uint8_t security_mode;
192 	uint16_t max_mpx;
193 	uint16_t max_vc;
194 	uint32_t max_buffer_size;
195 	uint32_t max_raw_size;
196 	uint32_t session_key;
197 	uint32_t capabilities;
198 	uint32_t time_low;
199 	uint32_t time_high;
200 	uint16_t server_tz;
201 	uint8_t security_len;
202 	uint16_t byte_count;
203 	uint8_t *guid;
204 	uint8_t *challenge;
205 	uint8_t *oem_domain;
206 } smb_nt_negotiate_rsp_t;
207 
208 /*
209  * SMB_COM_TRANSACTION
210  */
211 typedef struct smb_transact_rsp {
212 	uint8_t WordCount;		/* Count of data bytes */
213 					/* value = 10 + SetupCount */
214 	uint16_t TotalParamCount;	/* Total parameter bytes being sent */
215 	uint16_t TotalDataCount;	/* Total data bytes being sent */
216 	uint16_t Reserved;
217 	uint16_t ParamCount;		/* Parameter bytes sent this buffer */
218 	uint16_t ParamOffset;		/* Offset (from hdr start) to params */
219 	uint16_t ParamDisplacement;	/* Displacement of these param bytes */
220 	uint16_t DataCount;		/* Data bytes sent this buffer */
221 	uint16_t DataOffset;		/* Offset (from hdr start) to data */
222 	uint16_t DataDisplacement;	/* Displacement of these data bytes */
223 	uint8_t SetupCount;		/* Count of setup words */
224 	uint16_t BCC;
225 #if 0
226 	uint8_t Reserved2;		/* Reserved (pad above to word) */
227 	uint8_t Buffer[1];		/* Buffer containing: */
228 	uint16_t Setup[];		/*  Setup words (# = SetupWordCount) */
229 	uint16_t ByteCount;		/*  Count of data bytes */
230 	uint8_t Pad[];			/*  Pad to SHORT or LONG */
231 	uint8_t Params[];		/*  Param. bytes (# = ParamCount) */
232 	uint8_t Pad1[];			/*  Pad to SHORT or LONG */
233 	uint8_t Data[];			/*  Data bytes (# = DataCount) */
234 #endif
235 } smb_transact_rsp_t;
236 
237 /*
238  * SMBreadX
239  */
240 typedef struct smb_read_andx_rsp {
241 	uint8_t WordCount;
242 	uint8_t AndXCmd;
243 	uint8_t AndXReserved;
244 	uint16_t AndXOffset;
245 	uint16_t Remaining;
246 	uint16_t DataCompactionMode;
247 	uint16_t Reserved;
248 	uint16_t DataLength;
249 	uint16_t DataOffset;
250 	uint32_t DataLengthHigh;
251 	uint16_t Reserved2[3];
252 	uint16_t ByteCount;
253 #if 0
254 	uint8_t Pad[];
255 	uint8_t Data[];
256 #endif
257 } smb_read_andx_rsp_t;
258 
259 /*
260  * smbrdr_netbios.c
261  */
262 void nb_lock(void);
263 void nb_unlock(void);
264 void nb_close(int);
265 int nb_keep_alive(int, short);
266 
267 int nb_send(int, unsigned char *, unsigned);
268 int nb_rcv(int, unsigned char *, unsigned, long);
269 int nb_exchange(int, unsigned char *, unsigned,
270     unsigned char *, unsigned, long);
271 int nb_session_request(int, char *, char *, char *, char *);
272 
273 /*
274  * smbrdr_session.c
275  */
276 int smbrdr_negotiate(char *, char *);
277 struct sdb_session *smbrdr_session_lock(const char *, const char *, int);
278 void smbrdr_session_unlock(struct sdb_session *);
279 
280 /*
281  * smbrdr_logon.c
282  */
283 int smbrdr_logoffx(struct sdb_logon *);
284 
285 /* smbrdr_netuse.c */
286 void smbrdr_netuse_logoff(unsigned short);
287 struct sdb_netuse *smbrdr_netuse_get(int);
288 DWORD smbrdr_tree_connect(char *, char *, char *, unsigned short *);
289 int smbrdr_tree_disconnect(unsigned short);
290 void smbrdr_netuse_put(struct sdb_netuse *);
291 int smbrdr_tdcon(struct sdb_netuse *);
292 
293 /*
294  * smbrdr_rpcpipe.c
295  */
296 void smbrdr_ofile_end_of_share(unsigned short);
297 struct sdb_ofile *smbrdr_ofile_get(int);
298 void smbrdr_ofile_put(struct sdb_ofile *);
299 
300 /* smbrdr_lib.c */
301 DWORD smbrdr_request_init(smbrdr_handle_t *, unsigned char,
302     struct sdb_session *, struct sdb_logon *, struct sdb_netuse *);
303 DWORD smbrdr_send(smbrdr_handle_t *);
304 DWORD smbrdr_rcv(smbrdr_handle_t *, int);
305 DWORD smbrdr_exchange(smbrdr_handle_t *, smb_hdr_t *, long);
306 void smbrdr_handle_free(smbrdr_handle_t *);
307 int smbrdr_sign_init(struct sdb_session *, struct sdb_logon *);
308 void smbrdr_sign_fini(struct sdb_session *);
309 void smbrdr_sign_unset_key(struct sdb_session *);
310 
311 void smbrdr_lock_transport(void);
312 void smbrdr_unlock_transport(void);
313 
314 #endif /* _SMBRDR_H_ */
315