2 Copyright 2009, jimmikaelkael
3 Licenced under Academic Free License version 3.0
4 Review OpenUsbLd README & LICENSE files for further details.
10 // SMB Headers are always 32 bytes long
11 #define SMB_HDR_SIZE 32
13 // FLAGS field bitmasks
14 #define SMB_FLAGS_SERVER_TO_REDIR 0x80
15 #define SMB_FLAGS_REQUEST_BATCH_OPLOCK 0x40
16 #define SMB_FLAGS_REQUEST_OPLOCK 0x20
17 #define SMB_FLAGS_CANONICAL_PATHNAMES 0x10
18 #define SMB_FLAGS_CASELESS_PATHNAMES 0x08
19 #define SMB_FLAGS_RESERVED 0x04
20 #define SMB_FLAGS_CLIENT_BUF_AVAIL 0x02
21 #define SMB_FLAGS_SUPPORT_LOCKREAD 0x01
22 #define SMB_FLAGS_MASK 0x00
24 // FLAGS2 field bitmasks
25 #define SMB_FLAGS2_UNICODE_STRING 0x8000
26 #define SMB_FLAGS2_32BIT_STATUS 0x4000
27 #define SMB_FLAGS2_READ_IF_EXECUTE 0x2000
28 #define SMB_FLAGS2_DFS_PATHNAME 0x1000
29 #define SMB_FLAGS2_EXTENDED_SECURITY 0x0800
30 #define SMB_FLAGS2_RESERVED_01 0x0400
31 #define SMB_FLAGS2_RESERVED_02 0x0200
32 #define SMB_FLAGS2_RESERVED_03 0x0100
33 #define SMB_FLAGS2_RESERVED_04 0x0080
34 #define SMB_FLAGS2_IS_LONG_NAME 0x0040
35 #define SMB_FLAGS2_RESERVED_05 0x0020
36 #define SMB_FLAGS2_RESERVED_06 0x0010
37 #define SMB_FLAGS2_RESERVED_07 0x0008
38 #define SMB_FLAGS2_SECURITY_SIGNATURE 0x0004
39 #define SMB_FLAGS2_EAS 0x0002
40 #define SMB_FLAGS2_KNOWS_LONG_NAMES 0x0001
41 #define SMB_FLAGS2_MASK 0xf847
44 #define SMB_OFFSET_CMD 4
45 #define SMB_OFFSET_NTSTATUS 5
46 #define SMB_OFFSET_ECLASS 5
47 #define SMB_OFFSET_ECODE 7
48 #define SMB_OFFSET_FLAGS 9
49 #define SMB_OFFSET_FLAGS2 10
50 #define SMB_OFFSET_EXTRA 12
51 #define SMB_OFFSET_TID 24
52 #define SMB_OFFSET_PID 26
53 #define SMB_OFFSET_UID 28
54 #define SMB_OFFSET_MID 30
55 #define SMB_OFFSET_WORDCOUNT 32
56 #define SMB_OFFSET_ANDX_CMD 33
57 #define SMB_OFFSET_ANDX_RESERVED 34
58 #define SMB_OFFSET_ANDX_OFFSET 35
60 // Transaction2 Request Field offsets
61 #define SMB_TRANS2_REQ_OFFSET_TOTALPARAMCOUNT 33
62 #define SMB_TRANS2_REQ_OFFSET_TOTALDATACOUNT 35
63 #define SMB_TRANS2_REQ_OFFSET_MAXPARAMCOUNT 37
64 #define SMB_TRANS2_REQ_OFFSET_MAXDATACOUNT 39
65 #define SMB_TRANS2_REQ_OFFSET_MAXSETUPCOUNT 41
66 #define SMB_TRANS2_REQ_OFFSET_RESERVED 42
67 #define SMB_TRANS2_REQ_OFFSET_FLAGS 43
68 #define SMB_TRANS2_REQ_OFFSET_TIMEOUT 45
69 #define SMB_TRANS2_REQ_OFFSET_RESERVED2 49
70 #define SMB_TRANS2_REQ_OFFSET_PARAMCOUNT 51
71 #define SMB_TRANS2_REQ_OFFSET_PARAMOFFSET 53
72 #define SMB_TRANS2_REQ_OFFSET_DATACOUNT 55
73 #define SMB_TRANS2_REQ_OFFSET_DATAOFFSET 57
74 #define SMB_TRANS2_REQ_OFFSET_SETUPCOUNT 59
75 #define SMB_TRANS2_REQ_OFFSET_RESERVED3 60
76 #define SMB_TRANS2_REQ_OFFSET_SETUP0 61
78 // Transaction2 Response Field offsets
79 #define SMB_TRANS2_RSP_OFFSET_TOTALPARAMCOUNT 33
80 #define SMB_TRANS2_RSP_OFFSET_TOTALDATACOUNT 35
81 #define SMB_TRANS2_RSP_OFFSET_RESERVED 37
82 #define SMB_TRANS2_RSP_OFFSET_PARAMCOUNT 39
83 #define SMB_TRANS2_RSP_OFFSET_PARAMOFFSET 41
84 #define SMB_TRANS2_RSP_OFFSET_PARAMDISPLACEMENT 43
85 #define SMB_TRANS2_RSP_OFFSET_DATACOUNT 45
86 #define SMB_TRANS2_RSP_OFFSET_DATAOFFSET 47
87 #define SMB_TRANS2_RSP_OFFSET_DATADISPLACEMENT 49
88 #define SMB_TRANS2_RSP_OFFSET_SETUPCOUNT 51
89 #define SMB_TRANS2_RSP_OFFSET_RESERVED2 52
90 #define SMB_TRANS2_RSP_OFFSET_BYTECOUNT 53
91 #define SMB_TRANS2_RSP_OFFSET_PAD 55
93 // SMB File Attributes Encoding (16-bit)
94 #define ATTR_READONLY 0x01
95 #define ATTR_HIDDEN 0x02
96 #define ATTR_SYSTEM 0x04
97 #define ATTR_VOLUME 0x08
98 #define ATTR_DIRECTORY 0x10
99 #define ATTR_ARCHIVE 0x20
101 // SMB Extended File Attributes Encoding (32-bit)
102 #define EXT_ATTR_READONLY 0x001
103 #define EXT_ATTR_HIDDEN 0x002
104 #define EXT_ATTR_SYSTEM 0x004
105 #define EXT_ATTR_DIRECTORY 0x010
106 #define EXT_ATTR_ARCHIVE 0x020
107 #define EXT_ATTR_NORMAL 0x080
108 #define EXT_ATTR_TEMPORARY 0x100
109 #define EXT_ATTR_COMPRESSED 0x800
111 // SMB Information Level
112 #define SMB_INFO_STANDARD 0x001
113 #define SMB_INFO_QUERY_EA_SIZE 0x002
114 #define SMB_INFO_QUERY_EAS_FROM_LIST 0x003
115 #define SMB_FIND_FILE_DIRECTORY_INFO 0x101
116 #define SMB_FIND_FILE_FULL_DIRECTORY_INFO 0x102
117 #define SMB_FIND_FILE_NAMES_INFO 0x103
118 #define SMB_FIND_FILE_BOTH_DIRECTORY_INFO 0x104
119 #define SMB_FIND_FILE_UNIX 0x202
122 #define CLOSE_SEARCH_AFTER_REQUEST 0x01
123 #define CLOSE_SEARCH_IF_EOS 0x02
124 #define RESUME_SEARCH 0x04
125 #define CONTINUE_SEARCH 0x08
126 #define BACKUP_INTENT_SEARCH 0x10
128 // SMB Server Capabilities
129 #define SERVER_CAP_EXTENDED_SECURITY 0x80000000
130 #define SERVER_CAP_COMPRESSED_DATA 0x40000000
131 #define SERVER_CAP_BULK_TRANSFER 0x20000000
132 #define SERVER_CAP_UNIX 0x00800000
133 #define SERVER_CAP_LARGE_WRITEX 0x00008000
134 #define SERVER_CAP_LARGE_READX 0x00004000
135 #define SERVER_CAP_INFOLEVEL_PASSTHROUGH 0x00002000
136 #define SERVER_CAP_DFS 0x00001000
137 #define SERVER_CAP_NT_FIND 0x00000200
138 #define SERVER_CAP_LOCK_AND_READ 0x00000100
139 #define SERVER_CAP_LEVEL_II_OPLOCKS 0x00000080
140 #define SERVER_CAP_STATUS32 0x00000040
141 #define SERVER_CAP_RPC_REMOTE_APIS 0x00000020
142 #define SERVER_CAP_NT_SMBS 0x00000010
143 #define SERVER_CAP_LARGE_FILES 0x00000008
144 #define SERVER_CAP_UNICODE 0x00000004
145 #define SERVER_CAP_MPX_MODE 0x00000002
146 #define SERVER_CAP_RAW_MODE 0x00000001
148 // SMB Client Capabilities
149 #define CLIENT_CAP_EXTENDED_SECURITY 0x80000000
150 #define CLIENT_CAP_LARGE_READX 0x00004000
151 #define CLIENT_CAP_NT_FIND 0x00000200
152 #define CLIENT_CAP_LEVEL_II_OPLOCKS 0x00000080
153 #define CLIENT_CAP_STATUS32 0x00000040
154 #define CLIENT_CAP_NT_SMBS 0x00000010
155 #define CLIENT_CAP_LARGE_FILES 0x00000008
156 #define CLIENT_CAP_UNICODE 0x00000004
159 #define NEGOCIATE_SECURITY_SIGNATURES_REQUIRED 0x08
160 #define NEGOCIATE_SECURITY_SIGNATURES_ENABLED 0x04
161 #define NEGOCIATE_SECURITY_CHALLENGE_RESPONSE 0x02
162 #define NEGOCIATE_SECURITY_USER_LEVEL 0x01
165 #define SMB_COM_CREATE_DIRECTORY 0x00
166 #define SMB_COM_DELETE_DIRECTORY 0x01
167 #define SMB_COM_OPEN 0x02
168 #define SMB_COM_CREATE 0x03
169 #define SMB_COM_CLOSE 0x04
170 #define SMB_COM_FLUSH 0x05
171 #define SMB_COM_DELETE 0x06
172 #define SMB_COM_RENAME 0x07
173 #define SMB_COM_QUERY_INFORMATION 0x08
174 #define SMB_COM_SET_INFORMATION 0x09
175 #define SMB_COM_READ 0x0a
176 #define SMB_COM_WRITE 0x0b
177 #define SMB_COM_LOCK_BYTE_RANGE 0x0c
178 #define SMB_COM_UNLOCK_BYTE_RANGE 0x0d
179 #define SMB_COM_CREATE_TEMPORARY 0x0e
180 #define SMB_COM_CREATE_NEW 0x0f
181 #define SMB_COM_CHECK_DIRECTORY 0x10
182 #define SMB_COM_PROCESS_EXIT 0x11
183 #define SMB_COM_SEEK 0x12
184 #define SMB_COM_LOCK_AND_READ 0x13
185 #define SMB_COM_WRITE_AND_UNLOCK 0x14
186 #define SMB_COM_READ_RAW 0x1a
187 #define SMB_COM_READ_MPX 0x1b
188 #define SMB_COM_READ_MPX_SECONDARY 0x1c
189 #define SMB_COM_WRITE_RAW 0x1d
190 #define SMB_COM_WRITE_MPX 0x1e
191 #define SMB_COM_WRITE_MPX_SECONDARY 0x1f
192 #define SMB_COM_WRITE_COMPLETE 0x20
193 #define SMB_COM_QUERY_SERVER 0x21
194 #define SMB_COM_SET_INFORMATION2 0x22
195 #define SMB_COM_QUERY_INFORMATION2 0x23
196 #define SMB_COM_LOCKING_ANDX 0x24
197 #define SMB_COM_TRANSACTION 0x25
198 #define SMB_COM_TRANSACTION_SECONDARY 0x26
199 #define SMB_COM_IOCTL 0x27
200 #define SMB_COM_IOCTL_SECONDARY 0x28
201 #define SMB_COM_COPY 0x29
202 #define SMB_COM_MOVE 0x2a
203 #define SMB_COM_ECHO 0x2b
204 #define SMB_COM_WRITE_AND_CLOSE 0x2c
205 #define SMB_COM_OPEN_ANDX 0x2d
206 #define SMB_COM_READ_ANDX 0x2e
207 #define SMB_COM_WRITE_ANDX 0x2f
208 #define SMB_COM_NEW_FILE_SIZE 0x30
209 #define SMB_COM_CLOSE_AND_TREE_DISC 0x31
210 #define SMB_COM_TRANSACTION2 0x32
211 #define SMB_COM_TRANSACTION2_SECONDARY 0x33
212 #define SMB_COM_FIND_CLOSE2 0x34
213 #define SMB_COM_FIND_NOTIFY_CLOSE 0x35
214 #define SMB_COM_TREE_CONNECT 0x70
215 #define SMB_COM_TREE_DISCONNECT 0x71
216 #define SMB_COM_NEGOCIATE 0x72
217 #define SMB_COM_SESSION_SETUP_ANDX 0x73
218 #define SMB_COM_LOGOFF_ANDX 0x74
219 #define SMB_COM_TREE_CONNECT_ANDX 0x75
220 #define SMB_COM_QUERY_INFORMATION_DISK 0x80
221 #define SMB_COM_SEARCH 0x81
222 #define SMB_COM_FIND 0x82
223 #define SMB_COM_FIND_UNIQUE 0x83
224 #define SMB_COM_FIND_CLOSE 0x84
225 #define SMB_COM_NT_TRANSACT 0xa0
226 #define SMB_COM_NT_TRANSACT_SECONDARY 0xa1
227 #define SMB_COM_NT_CREATE_ANDX 0xa2
228 #define SMB_COM_NT_CANCEL 0xa4
229 #define SMB_COM_NT_RENAME 0xa5
230 #define SMB_COM_OPEN_PRINT_FILE 0xc0
231 #define SMB_COM_WRITE_PRINT_FILE 0xc1
232 #define SMB_COM_CLOSE_PRINT_FILE 0xc2
233 #define SMB_COM_GET_PRINT_QUEUE 0xc3
234 #define SMB_COM_READ_BULK 0xd8
235 #define SMB_COM_WRITE_BULK 0xd9
236 #define SMB_COM_WRITE_BULK_DATA 0xda
237 #define SMB_COM_NONE 0xff
239 // Setup[0] Transaction2 Subcommands
240 #define TRANS2_OPEN2 0x00
241 #define TRANS2_FIND_FIRST2 0x01
242 #define TRANS2_FIND_NEXT2 0x02
243 #define TRANS2_QUERY_FS_INFORMATION 0x03
244 #define TRANS2_SET_FS_INFORMATION 0x04
245 #define TRANS2_QUERY_PATH_INFORMATION 0X05
246 #define TRANS2_SET_PATH_INFORMATION 0x06
247 #define TRANS2_QUERY_FILE_INFORMATION 0x07
248 #define TRANS2_SET_FILE_INFORMATION 0x08
249 #define TRANS2_FSCTL 0x09
250 #define TRANS2_IOCTL2 0x0a
251 #define TRANS2_FIND_NOTIFY_FIRST 0x0b
252 #define TRANS2_FIND_NOTIFY_NEXT 0x0c
253 #define TRANS2_CREATE_DIRECTORY 0x0d
254 #define TRANS2_SESSION_SETUP 0x0e
255 #define TRANS2_GET_DFS_REFERRAL 0x10
256 #define TRANS2_REPORT_DFS_INCONSISTENCY 0x11
259 #define DOS_ECLASS_SUCCESS 0x00
262 #define STATUS_SUCCESS 0x00000000
263 #define STATUS_NO_MEDIA_IN_DEVICE 0xc0000013
264 #define STATUS_ACCESS_DENIED 0xc0000022
265 #define STATUS_OBJECT_NAME_NOT_FOUND 0xc0000034
266 #define STATUS_LOGON_FAILURE 0xc000006d
268 typedef struct _smb_time
{
273 // function prototypes
274 int rawTCP_SetSessionHeader(u32 size
); // Write Session Service header
275 int rawTCP_GetSessionHeader(void); // Read Session Service header
277 int smb_NegociateProtocol(char *SMBServerIP
, int SMBServerPort
, char *Username
, char *Password
); // process a Negociate Procotol message
278 int smb_SessionSetupTreeConnect(char *share_name
);
279 int smb_SessionSetupAndX(void); // process a Session Setup message, for NT LM 0.12 dialect, Non Extended Security negociated
280 int smb_TreeConnectAndX(char *ShareName
);
281 int smb_OpenAndX(char *filename
, u16
*FID
, int Write
); // process a Open AndX message
282 int smb_ReadFile(u16 FID
, u32 offsetlow
, u32 offsethigh
, void *readbuf
, u16 nbytes
);
283 int smb_WriteFile(u16 FID
, u32 offsetlow
, u32 offsethigh
, void *writebuf
, u16 nbytes
);
284 int smb_ReadCD(unsigned int lsn
, unsigned int nsectors
, void *buf
, int part_num
);
285 int smb_Disconnect(void);
287 #define MAX_SMB_SECTORS 2