xref: /onnv-gate/usr/src/lib/smbsrv/libsmbrdr/common/smbrdr.h (revision 10504:ee04788f8605)
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 
38 #include <smbsrv/cifs.h>
39 #include <smbsrv/smbinfo.h>
40 #include <smbsrv/smb.h>
41 #include <smbsrv/wintypes.h>
42 
43 #define	SMBRDR_REQ_BUFSZ	4096
44 
45 #define	MAX_ACCOUNT_NAME	32
46 #define	MAX_SHARE_NAME		32
47 #define	MAX_SCOPE_NAME		64
48 #define	MAX_FILE_PATH		128
49 
50 /*
51  * The number of shares and pipes is limited to 48 based on the note
52  * below. This really shouldn't cause a problem because we always
53  * our shares and named pipes are always opened and closed round every
54  * RPC transaction. This also tends to limit the number of active
55  * logons because we (currently) need two named pipes per logon.
56  *
57  * Q141709 Limit of 49 named pipe connections from a single workstation.
58  * If a named pipe server creates more than 49 distincly named pipes, a
59  * single client cannot connect more than 49 pipes on the named pipe
60  * server. Chapter 4, p113. Network Programming for Microsoft Windows
61  * Anthony Jones and Jim Ohlund, Microsoft Press, ISBN: 0-7356-0560-2
62  */
63 #define	N_NETUSE_TABLE		256
64 #define	N_OFILE_TABLE		256
65 
66 /*
67  * Logon's states
68  */
69 #define	SDB_LSTATE_START	0
70 #define	SDB_LSTATE_INIT		1
71 #define	SDB_LSTATE_LOGGING_OFF	2
72 #define	SDB_LSTATE_SETUP	3
73 
74 #define	SDB_LOGON_NONE		0
75 #define	SDB_LOGON_GUEST		1
76 #define	SDB_LOGON_ANONYMOUS	2
77 #define	SDB_LOGON_USER		3
78 
79 typedef struct sdb_logon {
80 	struct sdb_session *session;
81 	char username[MAX_ACCOUNT_NAME];
82 	unsigned short uid;
83 	unsigned int type;
84 	unsigned short state;
85 	smb_auth_info_t auth;
86 	unsigned char ssn_key[SMBAUTH_SESSION_KEY_SZ];
87 } sdb_logon_t;
88 
89 /*
90  * Session's states
91  *
92  *   SDB_SSTATE_START             ready to be used
93  *   SDB_SSTATE_INIT              initialized
94  *   SDB_SSTATE_STALE             lost transport connection
95  *   SDB_SSTATE_DISCONNECTING     disconnecting: logoff the user
96  *                                disconnect trees, close files
97  *   SDB_SSTATE_CLEANING          was in STALE state now just
98  *                                cleaning up
99  *   SDB_SSTATE_CONNECTED         got transport connection
100  *   SDB_SSTATE_NEGOTIATED        did SMB negotiate
101  */
102 #define	SDB_SSTATE_START		0
103 #define	SDB_SSTATE_INIT			1
104 #define	SDB_SSTATE_STALE		2
105 #define	SDB_SSTATE_DISCONNECTING	3
106 #define	SDB_SSTATE_CLEANING		4
107 #define	SDB_SSTATE_CONNECTED		5
108 #define	SDB_SSTATE_NEGOTIATED		6
109 
110 #define	SDB_SLCK_READ   1
111 #define	SDB_SLCK_WRITE  2
112 
113 struct sdb_session {
114 	char srv_name[MAXHOSTNAMELEN];
115 	smb_inaddr_t srv_ipaddr;
116 	char domain[MAXHOSTNAMELEN];
117 	char scope[SMB_PI_MAX_SCOPE];
118 	char native_os[SMB_PI_MAX_NATIVE_OS];
119 	char native_lanman[SMB_PI_MAX_LANMAN];
120 	int sock;
121 	short port;
122 	uint16_t secmode;
123 	uint32_t sesskey;
124 	uint32_t challenge_len;
125 	uint8_t challenge_key[32];
126 	uint8_t smb_flags;
127 	uint16_t smb_flags2;
128 	uint16_t vc;
129 	uint32_t remote_caps;
130 	uint8_t state;
131 	uint32_t sid;	/* session id */
132 	int remote_os;
133 	int remote_lm;
134 	int pdc_type;
135 	smb_sign_ctx_t sign_ctx;
136 	sdb_logon_t logon;
137 	rwlock_t rwl;
138 };
139 
140 /*
141  * Netuse's states
142  */
143 #define	SDB_NSTATE_START		0
144 #define	SDB_NSTATE_INIT			1
145 #define	SDB_NSTATE_DISCONNECTING	2
146 #define	SDB_NSTATE_CONNECTED		3
147 
148 struct sdb_netuse {
149 	struct sdb_session *session;
150 	unsigned short state;
151 	int letter;		/* local identity */
152 	unsigned int sid;
153 	unsigned short uid;
154 	unsigned short tid;		/* remote identity */
155 	char share[MAX_SHARE_NAME];
156 	mutex_t mtx;
157 };
158 
159 /*
160  * Ofile's states
161  */
162 #define	SDB_FSTATE_START	0
163 #define	SDB_FSTATE_INIT		1
164 #define	SDB_FSTATE_CLOSING	2
165 #define	SDB_FSTATE_OPEN		3
166 
167 struct sdb_ofile {
168 	struct sdb_session *session;
169 	struct sdb_netuse *netuse;
170 	unsigned short state;
171 	unsigned int sid;
172 	unsigned short uid;
173 	unsigned short tid;
174 	unsigned short fid;		/* remote identity */
175 	char path[MAX_FILE_PATH];
176 	mutex_t mtx;
177 };
178 
179 typedef struct smbrdr_handle {
180 	unsigned char *srh_buf;
181 	smb_msgbuf_t srh_mbuf;
182 	unsigned int srh_mbflags;
183 	unsigned char srh_cmd;
184 	struct sdb_session *srh_session;
185 	struct sdb_logon *srh_user;
186 	struct sdb_netuse *srh_tree;
187 } smbrdr_handle_t;
188 
189 /*
190  * smbrdr_netbios.c
191  */
192 void nb_lock(void);
193 void nb_unlock(void);
194 void nb_close(int);
195 int nb_keep_alive(int, short);
196 
197 int nb_send(int, unsigned char *, unsigned);
198 int nb_rcv(int, unsigned char *, unsigned, long);
199 int nb_exchange(int, unsigned char *, unsigned,
200     unsigned char *, unsigned, long);
201 int nb_session_request(int, char *, char *, char *, char *);
202 
203 /*
204  * smbrdr_session.c
205  */
206 int smbrdr_negotiate(char *, char *);
207 struct sdb_session *smbrdr_session_lock(char *, char *, int);
208 void smbrdr_session_unlock(struct sdb_session *);
209 
210 /*
211  * smbrdr_logon.c
212  */
213 int smbrdr_logoffx(struct sdb_logon *);
214 
215 /* smbrdr_netuse.c */
216 void smbrdr_netuse_logoff(unsigned short);
217 struct sdb_netuse *smbrdr_netuse_get(int);
218 DWORD smbrdr_tree_connect(char *, char *, char *, unsigned short *);
219 int smbrdr_tree_disconnect(unsigned short);
220 void smbrdr_netuse_put(struct sdb_netuse *);
221 int smbrdr_tdcon(struct sdb_netuse *);
222 
223 /*
224  * smbrdr_rpcpipe.c
225  */
226 void smbrdr_ofile_end_of_share(unsigned short);
227 struct sdb_ofile *smbrdr_ofile_get(int);
228 void smbrdr_ofile_put(struct sdb_ofile *);
229 
230 /* smbrdr_lib.c */
231 DWORD smbrdr_request_init(smbrdr_handle_t *, unsigned char,
232     struct sdb_session *, struct sdb_logon *, struct sdb_netuse *);
233 DWORD smbrdr_send(smbrdr_handle_t *);
234 DWORD smbrdr_rcv(smbrdr_handle_t *, int);
235 DWORD smbrdr_exchange(smbrdr_handle_t *, smb_hdr_t *, long);
236 void smbrdr_handle_free(smbrdr_handle_t *);
237 int smbrdr_sign_init(struct sdb_session *, struct sdb_logon *);
238 void smbrdr_sign_fini(struct sdb_session *);
239 void smbrdr_sign_unset_key(struct sdb_session *);
240 
241 void smbrdr_lock_transport(void);
242 void smbrdr_unlock_transport(void);
243 
244 #endif /* _SMBRDR_H_ */
245