00001 #include <df/smberr.hpp>
00002
00003 class SMBSession;
00004
00005 #define SMB_MAX_X 4 //max # of supported andX per packet
00006 #define SMB_MAX_MTU 1456 //1500(EthernetMTU) - 20(IP) - 20(TCP) - 4 (NetBios packet)
00007
00008 struct SMBCommand {
00009 uint8 cmd;
00010 uint8 errclass;
00011 uint16 errcode;
00012 uint32 ntstatus;
00013 uint16 tid;
00014 uint16 uid;
00015 uint16 pid;
00016 uint16 mid;
00017
00018 uint8 wordcnt;
00019 uint16 *words;
00020 uint16 bytecnt;
00021 uint8 *bytes;
00022
00023 uint8 Xcmd;
00024 int Xcnt;
00025 uint8 Xwordcnt[SMB_MAX_X];
00026 uint16 *Xwords;
00027 uint16 Xbytecnt[SMB_MAX_X];
00028 uint8 *Xbytes;
00029 };
00030
00031 class SMBCallback {
00032 public:
00033 virtual void SMBProcessCommand(SMBSession *, SMBCommand *)=0;
00034 };
00035
00036 class SMBServer : public NetworkServer {
00037 friend class SMBSession;
00038 private:
00039 SMBCallback *callback;
00040 LogFile *lf;
00041 public:
00042 SMBServer();
00043 ~SMBServer();
00044 void SetCallback(SMBCallback *cb);
00045 int16 StartServer(uint16 port);
00046 void SetLogFile(LogFile *lf);
00047 };
00048
00049 class SMBSession : public NetworkSession {
00050 private:
00051 PTR data, cmd;
00052 uint32 cmdlen, readlen;
00053 #ifdef DEBUG
00054 void LogPacket(PTR, int, BOOL);
00055 #endif
00056 void Log(char *);
00057 ExclusiveThread lock;
00058 public:
00059 SMBSession();
00060 ~SMBSession();
00061 BOOL WriteCommand(SMBCommand *);
00062 BOOL NextX(SMBCommand *);
00063 virtual void Process();
00064 virtual void AsyncRead();
00065 virtual void AsyncClose();
00066 BOOL SSLConnect(OpenSSL *);
00067
00068 PTR UserData;
00069 static void Time2timelh(Time &, uint16 &, uint16 &);
00070 };
00071
00072 #pragma pack(push, 1)
00073 struct smb_header {
00074
00075 uint8 sign[4];
00076 uint8 cmd;
00077 uint32 ntstatus;
00078 uint8 flgs;
00079 uint16 flgs2;
00080 uint16 pid_hi;
00081 uint64 sign64;
00082 uint16 reserved;
00083 uint16 tid;
00084 uint16 pid;
00085 uint16 uid;
00086 uint16 mid;
00087
00088
00089
00090
00091 };
00092 #pragma pack(pop)
00093
00094 #define SMB_FLG_RESPONSE 0x80 //else REQUEST
00095 #define SMB_FLG_NOTIFY 0x40 //OBSOLETE : notify client only on open
00096 #define SMB_FLG_OPLOCKS 0x20 //OBSOLETE : req/grant oplocks
00097 #define SMB_FLG_CANON 0x10 //pathnames canonicalized (always set)
00098 #define SMB_FLG_CASELESS 0x08 //pathnames are not case sensitive (do not use)
00099 #define SMB_FLG_RESERVED 0x04 //not used
00100 #define SMB_FLG_RBP 0x02 //OBSOLETE : receive buffer posted
00101 #define SMB_FLG_LOCKREAD 0x01 //OBSOLETE : Lock/Read & Write/Unlock supported?
00102
00103 #define SMB_FLG2_UNICODE 0x8000 //strings are unicode (not supported)
00104 #define SMB_FLG2_NTCODES 0x4000 //error codes are NT codes (always set)
00105 #define SMB_FLG2_EXECREAD 0x2000 //execute permission implies read permission
00106 #define SMB_FLG2_DFS 0x1000 //to server : resolve pathnames with DFS
00107 #define SMB_FLG2_EXTSEC 0x0800 //extended security negotiation
00108 #define SMB_FLG2_LFNREQ 0x0040 //filename in message is LFN
00109 #define SMB_FLG2_SECSIGN 0x0004 //Message Auth Code (to prevent hacks)
00110 #define SMB_FLG2_EXTATTR 0x0002 //to server : Extended Attr support (OS/2)
00111 #define SMB_FLG2_LFNSUP 0x0001 //to server : LFN supported
00112
00113 #define SMB_CMD_MKDIR 0x00
00114 #define SMB_CMD_RMDIR 0x01
00115 #define SMB_CMD_OPEN 0x02
00116 #define SMB_CMD_CREATE_OPEN 0x03
00117 #define SMB_CMD_CLOSE 0x04
00118 #define SMB_CMD_FLUSH 0x05
00119 #define SMB_CMD_DELETE 0x06
00120 #define SMB_CMD_RENAME 0x07
00121 #define SMB_CMD_GETATTR 0x08
00122 #define SMB_CMD_SETATTR 0x09
00123 #define SMB_CMD_READ 0x0a
00124 #define SMB_CMD_WRITE 0x0b
00125 #define SMB_CMD_LOCK 0x0c
00126 #define SMB_CMD_UNLOCK 0x0d
00127 #define SMB_CMD_CREATE_TEMP 0x0e
00128 #define SMB_CMD_CREATE_ONLY 0x0f
00129 #define SMB_CMD_CHECK_PATH 0x10
00130 #define SMB_CMD_EXIT_PROCESS 0x11
00131 #define SMB_CMD_SEEK 0x12
00132
00133 #define SMB_CMD_SET_INFO 0x22
00134 #define SMB_CMD_GET_INFO 0x23
00135 #define SMB_CMD_TRANS 0x25
00136 #define SMB_CMD_OPEN_X 0x2d
00137
00138 #define SMB_CMD_TRANS2 0x32
00139
00140 #define SMB_CMD_TREE_CONNECT 0x70
00141 #define SMB_CMD_TREE_DISCONNECT 0x71
00142 #define SMB_CMD_NEGOTIATE_PROTOCOL 0x72
00143 #define SMB_CMD_NEGOTIATE_LOGON_X 0x73
00144 #define SMB_CMD_TREE_CONNECT_X 0x75
00145
00146
00147 #define SMB_CMD_GETATTR_DISK 0x80
00148 #define SMB_CMD_FIND 0x81
00149
00150 #define SMB_CMD_PRINT_OPEN 0xc0 //open spool file
00151 #define SMB_CMD_PRINT_WRITE 0xc1
00152 #define SMB_CMD_PRINT_CLOSE 0xc2
00153 #define SMB_CMD_PRINT_GETQUEUE 0xc3
00154
00155 #define SMB_CMD_SEND_SINGLE 0xd0
00156 #define SMB_CMD_SEND_BROADCAST 0xd1
00157 #define SMB_CMD_FORWARD_USERNAME 0xd2
00158 #define SMB_CMD_CANCEL_FORWARD 0xd3
00159 #define SMB_CMD_GETMACHINENAME 0xd4
00160 #define SMB_CMD_START_MULTI 0xd5
00161 #define SMB_CMD_END_MULTI 0xd6
00162 #define SMB_CMD_TEXT_MULTI 0xd7 //data of multi block message
00163
00164 #define SMB_CMD_DF_API 0xfe //advanced DF API
00165 #define SMB_CMD_DISCONNECT 0xff //connection closed (issued by server, not client)
00166
00167 #define SMB_TRANS_OPEN 0x00
00168 #define SMB_TRANS_FINDFIRST 0x01
00169 #define SMB_TRANS_FINDNEXT 0x02
00170 #define SMB_TRANS_GET_FSINFO 0x03
00171 #define SMB_TRANS_SET_FSINFO 0x04
00172 #define SMB_TRANS_GET_PATHINFO 0x05
00173 #define SMB_TRANS_SET_PATHINFO 0x06
00174 #define SMB_TRANS_GET_FILEINFO 0x07
00175 #define SMB_TRANS_SET_FILEINFO 0x08
00176 #define SMB_TRANS_FIND_NOTIFY_FIRST 0x0b
00177 #define SMB_TRANS_FIND_NOTIFY_NEXT 0x0c
00178 #define SMB_TRANS_MKDIR 0x0d
00179
00180 #define SMB_LVL_INFO_STANDARD 0x001
00181 #define SMB_LVL_EA_SIZE 0x002
00182 #define SMB_LVL_EA_LIST 0x003
00183 #define SMB_LVL_FILE_DIRECTORY_INFO 0x101
00184 #define SMB_LVL_FILE_FULL_DIRECTORY_INFO 0x102
00185 #define SMB_LVL_FILE_NAMES_INFO 0x103
00186 #define SMB_LVL_FILE_BOTH_DIRECTORY_INFO 0x104
00187 #define SMB_LVL_ID_FULL_DIRECTORY_INFO 0x105
00188 #define SMB_LVL_ID_BOTH_DIRECTORY_INFO 0x106
00189
00190
00191 #define SMB_FLG_CLOSE 0x01
00192 #define SMB_FLG_CLOSE_AT_END 0x02
00193 #define SMB_FLG_RESUME_KEYS 0x04
00194
00195 struct smb_protocol_reply {
00196 uint16 DialectIndex;
00197 uint8 SecurityMode;
00198 uint16 MaxMIDs;
00199 uint16 MaxVCs;
00200 uint32 MaxBufferSize;
00201 uint32 MaxRawBufSize;
00202 uint32 SessionKey;
00203 uint32 Capabilities;
00204 uint32 TimeLow;
00205 uint32 TimeHigh;
00206 int16 TimeZone;
00207 uint8 KeyLength;
00208 };
00209
00210 #define SMB_FLG_SEC_MAC_REQUIRED 0x08
00211 #define SMB_FLG_SEC_MAC_ENABLED 0x04
00212 #define SMB_FLG_SEC_CHALLENGE 0x02 //required (else plaintext)
00213 #define SMB_FLG_SEC_USER_LEVEL 0x01 //use Share level Mode
00214
00215
00216 #define SMB_FLG_CAPS_EXT_SEC 0x80000000
00217 #define SMB_FLG_CAPS_DFS 0x00001000 //server CAPS only
00218 #define SMB_FLG_CAPS_NTCODES 0x00000040
00219 #define SMB_FLG_CAPS_64BIT 0x00000008
00220 #define SMB_FLG_CAPS_UNICODE 0x00000004
00221
00222 union smb_protocol_reply_data {
00223 struct {
00224 uint8 GUID[16];
00225
00226 } ext_sec;
00227 struct {
00228
00229
00230 } non_ext_sec;
00231 };
00232
00233 struct smb_session_setup_andx {
00234 uint8 x_cmd;
00235 uint8 reserved;
00236 uint16 x_offset;
00237 uint16 MaxBufferSize;
00238 uint16 MaxMIDs;
00239 uint16 MaxVCs;
00240 uint32 SessionKey;
00241 uint16 CIPasswordLength;
00242 uint16 CSPasswordLength;
00243 uint32 reserved2;
00244 uint32 caps;
00245 };
00246
00247 struct smb_session_setup_andx_ext_sec {
00248 uint8 x_cmd;
00249 uint8 reserved;
00250 uint16 x_offset;
00251 uint16 MaxBufferSize;
00252 uint16 MaxMIDs;
00253 uint16 MaxVCs;
00254 uint32 SessionKey;
00255 uint16 SecurityBlobLength;
00256 uint32 reserved2;
00257 uint32 caps;
00258 };
00259
00260