Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

611 lines
19 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. srvutil.c
  5. Abstract:
  6. Implements misc. smb stuff
  7. Author:
  8. Ahmed Mohamed (ahmedm) 1-Feb-2000
  9. Revision History:
  10. --*/
  11. #include "srv.h"
  12. #if TRANS2_MAX_FUNCTION > 0xFF
  13. #pragma error "TRANS2_MAX_FUNCTION > 0xFF"
  14. #endif
  15. typedef BOOL (*SMB_DISPATCH_FUNC)(Packet_t *);
  16. typedef BOOL (*TRANS2_DISPATCH_FUNC)(Packet_t *, Trans2_t *);
  17. static SMB_DISPATCH_FUNC SrvDispatchTable[0x100] = {0};
  18. static TRANS2_DISPATCH_FUNC Trans2DispatchTable[0x100] = {0};
  19. VOID
  20. SrvInitDispTable();
  21. void
  22. SrvUtilInit(SrvCtx_t *ctx)
  23. {
  24. extern void Trans2Init();
  25. SrvInitDispTable();
  26. Trans2Init();
  27. }
  28. void
  29. SrvUtilExit(SrvCtx_t *ctx)
  30. {
  31. // nothing to do
  32. return;
  33. }
  34. BOOL
  35. IsSmb(
  36. LPVOID pBuffer,
  37. DWORD nLength
  38. )
  39. {
  40. PNT_SMB_HEADER pSmb = (PNT_SMB_HEADER) pBuffer;
  41. return (pSmb && !(nLength < sizeof(NT_SMB_HEADER)) &&
  42. (*(PULONG)pSmb->Protocol == SMB_HEADER_PROTOCOL));
  43. }
  44. BOOL
  45. SrvDispatch(
  46. Packet_t * msg
  47. )
  48. {
  49. USHORT command = msg->in.command;
  50. if (command > 0xFF) {
  51. SrvLogError(("(command > 0xFF)\n"));
  52. return FALSE;
  53. }
  54. SrvLog(("- parameter block - <0x%02x = %s>\n",
  55. command,
  56. SrvUnparseCommand(command)));
  57. if (SrvDispatchTable[command])
  58. return SrvDispatchTable[command](msg);
  59. else {
  60. return SrvComUnknown(msg);
  61. }
  62. }
  63. // returns whether or not to conitnue
  64. BOOL
  65. Trans2Dispatch(
  66. Packet_t * msg,
  67. Trans2_t * trans
  68. )
  69. {
  70. USHORT command = msg->in.command;
  71. if (command > 0xFF) {
  72. SrvLogError(("DOSERROR: (function > 0xFF)\n"));
  73. SET_DOSERROR(msg, SERVER, NO_SUPPORT);
  74. return FALSE;
  75. }
  76. if (command > TRANS2_MAX_FUNCTION) {
  77. SrvLogError(("DOSERROR: (function > maximum)\n"));
  78. SET_DOSERROR(msg, SERVER, NO_SUPPORT);
  79. return FALSE;
  80. }
  81. SrvLog(("- parameter block - <0x%02x = %s>\n",
  82. command,
  83. SrvUnparseTrans2(command)));
  84. if (Trans2DispatchTable[command])
  85. return Trans2DispatchTable[command](msg, trans);
  86. else {
  87. return Trans2Unknown(msg, trans);
  88. }
  89. }
  90. VOID
  91. InitSmbHeader(
  92. Packet_t * msg
  93. )
  94. {
  95. ZeroMemory(msg->out.smb, sizeof(NT_SMB_HEADER));
  96. CopyMemory(msg->out.smb->Protocol, msg->in.smb->Protocol,
  97. sizeof(msg->out.smb->Protocol));
  98. msg->out.smb->Command = msg->in.smb->Command;
  99. msg->out.smb->Flags = 0x80;
  100. msg->out.smb->Flags2 = 1;
  101. msg->out.smb->Pid = msg->in.smb->Pid;
  102. msg->out.smb->Tid = msg->in.smb->Tid;
  103. msg->out.smb->Mid = msg->in.smb->Mid;
  104. msg->out.smb->Uid = msg->in.smb->Uid;
  105. }
  106. #define XLAT_STRING(code) static const char STRING_##code[] = #code
  107. #define XLAT_CASE(code) case code: return STRING_##code
  108. #define XLAT_STRING_DEFAULT XLAT_STRING(Unknown)
  109. #define XLAT_CASE_DEFAULT default: return STRING_Unknown
  110. LPCSTR
  111. SrvUnparseCommand(
  112. USHORT command
  113. )
  114. {
  115. XLAT_STRING_DEFAULT;
  116. XLAT_STRING(SMB_COM_CREATE_DIRECTORY);
  117. XLAT_STRING(SMB_COM_DELETE_DIRECTORY);
  118. XLAT_STRING(SMB_COM_OPEN);
  119. XLAT_STRING(SMB_COM_CREATE);
  120. XLAT_STRING(SMB_COM_CLOSE);
  121. XLAT_STRING(SMB_COM_FLUSH);
  122. XLAT_STRING(SMB_COM_DELETE);
  123. XLAT_STRING(SMB_COM_RENAME);
  124. XLAT_STRING(SMB_COM_QUERY_INFORMATION);
  125. XLAT_STRING(SMB_COM_SET_INFORMATION);
  126. XLAT_STRING(SMB_COM_READ);
  127. XLAT_STRING(SMB_COM_WRITE);
  128. XLAT_STRING(SMB_COM_LOCK_BYTE_RANGE);
  129. XLAT_STRING(SMB_COM_UNLOCK_BYTE_RANGE);
  130. XLAT_STRING(SMB_COM_CREATE_TEMPORARY);
  131. XLAT_STRING(SMB_COM_CREATE_NEW);
  132. XLAT_STRING(SMB_COM_CHECK_DIRECTORY);
  133. XLAT_STRING(SMB_COM_PROCESS_EXIT);
  134. XLAT_STRING(SMB_COM_SEEK);
  135. XLAT_STRING(SMB_COM_LOCK_AND_READ);
  136. XLAT_STRING(SMB_COM_WRITE_AND_UNLOCK);
  137. XLAT_STRING(SMB_COM_READ_RAW);
  138. XLAT_STRING(SMB_COM_READ_MPX);
  139. XLAT_STRING(SMB_COM_READ_MPX_SECONDARY); // server to redir only
  140. XLAT_STRING(SMB_COM_WRITE_RAW);
  141. XLAT_STRING(SMB_COM_WRITE_MPX);
  142. XLAT_STRING(SMB_COM_WRITE_MPX_SECONDARY);
  143. XLAT_STRING(SMB_COM_WRITE_COMPLETE); // server to redir only
  144. XLAT_STRING(SMB_COM_QUERY_INFORMATION_SRV);
  145. XLAT_STRING(SMB_COM_SET_INFORMATION2);
  146. XLAT_STRING(SMB_COM_QUERY_INFORMATION2);
  147. XLAT_STRING(SMB_COM_LOCKING_ANDX);
  148. XLAT_STRING(SMB_COM_TRANSACTION);
  149. XLAT_STRING(SMB_COM_TRANSACTION_SECONDARY);
  150. XLAT_STRING(SMB_COM_IOCTL);
  151. XLAT_STRING(SMB_COM_IOCTL_SECONDARY);
  152. XLAT_STRING(SMB_COM_COPY);
  153. XLAT_STRING(SMB_COM_MOVE);
  154. XLAT_STRING(SMB_COM_ECHO);
  155. XLAT_STRING(SMB_COM_WRITE_AND_CLOSE);
  156. XLAT_STRING(SMB_COM_OPEN_ANDX);
  157. XLAT_STRING(SMB_COM_READ_ANDX);
  158. XLAT_STRING(SMB_COM_WRITE_ANDX);
  159. XLAT_STRING(SMB_COM_CLOSE_AND_TREE_DISC);
  160. XLAT_STRING(SMB_COM_TRANSACTION2);
  161. XLAT_STRING(SMB_COM_TRANSACTION2_SECONDARY);
  162. XLAT_STRING(SMB_COM_FIND_CLOSE2);
  163. XLAT_STRING(SMB_COM_FIND_NOTIFY_CLOSE);
  164. XLAT_STRING(SMB_COM_TREE_CONNECT);
  165. XLAT_STRING(SMB_COM_TREE_DISCONNECT);
  166. XLAT_STRING(SMB_COM_NEGOTIATE);
  167. XLAT_STRING(SMB_COM_SESSION_SETUP_ANDX);
  168. XLAT_STRING(SMB_COM_LOGOFF_ANDX);
  169. XLAT_STRING(SMB_COM_TREE_CONNECT_ANDX);
  170. XLAT_STRING(SMB_COM_QUERY_INFORMATION_DISK);
  171. XLAT_STRING(SMB_COM_SEARCH);
  172. XLAT_STRING(SMB_COM_FIND);
  173. XLAT_STRING(SMB_COM_FIND_UNIQUE);
  174. XLAT_STRING(SMB_COM_FIND_CLOSE);
  175. XLAT_STRING(SMB_COM_NT_TRANSACT);
  176. XLAT_STRING(SMB_COM_NT_TRANSACT_SECONDARY);
  177. XLAT_STRING(SMB_COM_NT_CREATE_ANDX);
  178. XLAT_STRING(SMB_COM_NT_CANCEL);
  179. XLAT_STRING(SMB_COM_NT_RENAME);
  180. XLAT_STRING(SMB_COM_OPEN_PRINT_FILE);
  181. XLAT_STRING(SMB_COM_WRITE_PRINT_FILE);
  182. XLAT_STRING(SMB_COM_CLOSE_PRINT_FILE);
  183. XLAT_STRING(SMB_COM_GET_PRINT_QUEUE);
  184. XLAT_STRING(SMB_COM_SEND_MESSAGE);
  185. XLAT_STRING(SMB_COM_SEND_BROADCAST_MESSAGE);
  186. XLAT_STRING(SMB_COM_FORWARD_USER_NAME);
  187. XLAT_STRING(SMB_COM_CANCEL_FORWARD);
  188. XLAT_STRING(SMB_COM_GET_MACHINE_NAME);
  189. XLAT_STRING(SMB_COM_SEND_START_MB_MESSAGE);
  190. XLAT_STRING(SMB_COM_SEND_END_MB_MESSAGE);
  191. XLAT_STRING(SMB_COM_SEND_TEXT_MB_MESSAGE);
  192. switch (command)
  193. {
  194. XLAT_CASE(SMB_COM_CREATE_DIRECTORY);
  195. XLAT_CASE(SMB_COM_DELETE_DIRECTORY);
  196. XLAT_CASE(SMB_COM_OPEN);
  197. XLAT_CASE(SMB_COM_CREATE);
  198. XLAT_CASE(SMB_COM_CLOSE);
  199. XLAT_CASE(SMB_COM_FLUSH);
  200. XLAT_CASE(SMB_COM_DELETE);
  201. XLAT_CASE(SMB_COM_RENAME);
  202. XLAT_CASE(SMB_COM_QUERY_INFORMATION);
  203. XLAT_CASE(SMB_COM_SET_INFORMATION);
  204. XLAT_CASE(SMB_COM_READ);
  205. XLAT_CASE(SMB_COM_WRITE);
  206. XLAT_CASE(SMB_COM_LOCK_BYTE_RANGE);
  207. XLAT_CASE(SMB_COM_UNLOCK_BYTE_RANGE);
  208. XLAT_CASE(SMB_COM_CREATE_TEMPORARY);
  209. XLAT_CASE(SMB_COM_CREATE_NEW);
  210. XLAT_CASE(SMB_COM_CHECK_DIRECTORY);
  211. XLAT_CASE(SMB_COM_PROCESS_EXIT);
  212. XLAT_CASE(SMB_COM_SEEK);
  213. XLAT_CASE(SMB_COM_LOCK_AND_READ);
  214. XLAT_CASE(SMB_COM_WRITE_AND_UNLOCK);
  215. XLAT_CASE(SMB_COM_READ_RAW);
  216. XLAT_CASE(SMB_COM_READ_MPX);
  217. XLAT_CASE(SMB_COM_READ_MPX_SECONDARY); // server to redir only
  218. XLAT_CASE(SMB_COM_WRITE_RAW);
  219. XLAT_CASE(SMB_COM_WRITE_MPX);
  220. XLAT_CASE(SMB_COM_WRITE_MPX_SECONDARY);
  221. XLAT_CASE(SMB_COM_WRITE_COMPLETE); // server to redir only
  222. XLAT_CASE(SMB_COM_QUERY_INFORMATION_SRV);
  223. XLAT_CASE(SMB_COM_SET_INFORMATION2);
  224. XLAT_CASE(SMB_COM_QUERY_INFORMATION2);
  225. XLAT_CASE(SMB_COM_LOCKING_ANDX);
  226. XLAT_CASE(SMB_COM_TRANSACTION);
  227. XLAT_CASE(SMB_COM_TRANSACTION_SECONDARY);
  228. XLAT_CASE(SMB_COM_IOCTL);
  229. XLAT_CASE(SMB_COM_IOCTL_SECONDARY);
  230. XLAT_CASE(SMB_COM_COPY);
  231. XLAT_CASE(SMB_COM_MOVE);
  232. XLAT_CASE(SMB_COM_ECHO);
  233. XLAT_CASE(SMB_COM_WRITE_AND_CLOSE);
  234. XLAT_CASE(SMB_COM_OPEN_ANDX);
  235. XLAT_CASE(SMB_COM_READ_ANDX);
  236. XLAT_CASE(SMB_COM_WRITE_ANDX);
  237. XLAT_CASE(SMB_COM_CLOSE_AND_TREE_DISC);
  238. XLAT_CASE(SMB_COM_TRANSACTION2);
  239. XLAT_CASE(SMB_COM_TRANSACTION2_SECONDARY);
  240. XLAT_CASE(SMB_COM_FIND_CLOSE2);
  241. XLAT_CASE(SMB_COM_FIND_NOTIFY_CLOSE);
  242. XLAT_CASE(SMB_COM_TREE_CONNECT);
  243. XLAT_CASE(SMB_COM_TREE_DISCONNECT);
  244. XLAT_CASE(SMB_COM_NEGOTIATE);
  245. XLAT_CASE(SMB_COM_SESSION_SETUP_ANDX);
  246. XLAT_CASE(SMB_COM_LOGOFF_ANDX);
  247. XLAT_CASE(SMB_COM_TREE_CONNECT_ANDX);
  248. XLAT_CASE(SMB_COM_QUERY_INFORMATION_DISK);
  249. XLAT_CASE(SMB_COM_SEARCH);
  250. XLAT_CASE(SMB_COM_FIND);
  251. XLAT_CASE(SMB_COM_FIND_UNIQUE);
  252. XLAT_CASE(SMB_COM_FIND_CLOSE);
  253. XLAT_CASE(SMB_COM_NT_TRANSACT);
  254. XLAT_CASE(SMB_COM_NT_TRANSACT_SECONDARY);
  255. XLAT_CASE(SMB_COM_NT_CREATE_ANDX);
  256. XLAT_CASE(SMB_COM_NT_CANCEL);
  257. XLAT_CASE(SMB_COM_NT_RENAME);
  258. XLAT_CASE(SMB_COM_OPEN_PRINT_FILE);
  259. XLAT_CASE(SMB_COM_WRITE_PRINT_FILE);
  260. XLAT_CASE(SMB_COM_CLOSE_PRINT_FILE);
  261. XLAT_CASE(SMB_COM_GET_PRINT_QUEUE);
  262. XLAT_CASE(SMB_COM_SEND_MESSAGE);
  263. XLAT_CASE(SMB_COM_SEND_BROADCAST_MESSAGE);
  264. XLAT_CASE(SMB_COM_FORWARD_USER_NAME);
  265. XLAT_CASE(SMB_COM_CANCEL_FORWARD);
  266. XLAT_CASE(SMB_COM_GET_MACHINE_NAME);
  267. XLAT_CASE(SMB_COM_SEND_START_MB_MESSAGE);
  268. XLAT_CASE(SMB_COM_SEND_END_MB_MESSAGE);
  269. XLAT_CASE(SMB_COM_SEND_TEXT_MB_MESSAGE);
  270. XLAT_CASE_DEFAULT;
  271. }
  272. }
  273. LPCSTR
  274. SrvUnparseTrans2(
  275. USHORT code
  276. )
  277. {
  278. XLAT_STRING_DEFAULT;
  279. XLAT_STRING(TRANS2_OPEN2);
  280. XLAT_STRING(TRANS2_FIND_FIRST2);
  281. XLAT_STRING(TRANS2_FIND_NEXT2);
  282. XLAT_STRING(TRANS2_QUERY_FS_INFORMATION);
  283. XLAT_STRING(TRANS2_SET_FS_INFORMATION);
  284. XLAT_STRING(TRANS2_QUERY_PATH_INFORMATION);
  285. XLAT_STRING(TRANS2_SET_PATH_INFORMATION);
  286. XLAT_STRING(TRANS2_QUERY_FILE_INFORMATION);
  287. XLAT_STRING(TRANS2_SET_FILE_INFORMATION);
  288. XLAT_STRING(TRANS2_FSCTL);
  289. XLAT_STRING(TRANS2_IOCTL2);
  290. XLAT_STRING(TRANS2_FIND_NOTIFY_FIRST);
  291. XLAT_STRING(TRANS2_FIND_NOTIFY_NEXT);
  292. XLAT_STRING(TRANS2_CREATE_DIRECTORY);
  293. XLAT_STRING(TRANS2_SESSION_SETUP);
  294. XLAT_STRING(TRANS2_QUERY_FS_INFORMATION_FID);
  295. XLAT_STRING(TRANS2_GET_DFS_REFERRAL);
  296. XLAT_STRING(TRANS2_REPORT_DFS_INCONSISTENCY);
  297. switch (code)
  298. {
  299. XLAT_CASE(TRANS2_OPEN2);
  300. XLAT_CASE(TRANS2_FIND_FIRST2);
  301. XLAT_CASE(TRANS2_FIND_NEXT2);
  302. XLAT_CASE(TRANS2_QUERY_FS_INFORMATION);
  303. XLAT_CASE(TRANS2_SET_FS_INFORMATION);
  304. XLAT_CASE(TRANS2_QUERY_PATH_INFORMATION);
  305. XLAT_CASE(TRANS2_SET_PATH_INFORMATION);
  306. XLAT_CASE(TRANS2_QUERY_FILE_INFORMATION);
  307. XLAT_CASE(TRANS2_SET_FILE_INFORMATION);
  308. XLAT_CASE(TRANS2_FSCTL);
  309. XLAT_CASE(TRANS2_IOCTL2);
  310. XLAT_CASE(TRANS2_FIND_NOTIFY_FIRST);
  311. XLAT_CASE(TRANS2_FIND_NOTIFY_NEXT);
  312. XLAT_CASE(TRANS2_CREATE_DIRECTORY);
  313. XLAT_CASE(TRANS2_SESSION_SETUP);
  314. XLAT_CASE(TRANS2_QUERY_FS_INFORMATION_FID);
  315. XLAT_CASE(TRANS2_GET_DFS_REFERRAL);
  316. XLAT_CASE(TRANS2_REPORT_DFS_INCONSISTENCY);
  317. XLAT_CASE_DEFAULT;
  318. }
  319. }
  320. VOID
  321. SrvInitDispTable(
  322. )
  323. {
  324. ZeroMemory((PVOID) SrvDispatchTable, sizeof(SrvDispatchTable));
  325. SrvDispatchTable[SMB_COM_NEGOTIATE] = SrvComNegotiate;
  326. SrvDispatchTable[SMB_COM_TRANSACTION] = SrvComTrans;
  327. SrvDispatchTable[SMB_COM_TRANSACTION2] = SrvComTrans2;
  328. SrvDispatchTable[SMB_COM_SESSION_SETUP_ANDX] = SrvComSessionSetupAndx;
  329. SrvDispatchTable[SMB_COM_TREE_CONNECT_ANDX] = SrvComTreeConnectAndx;
  330. SrvDispatchTable[SMB_COM_NO_ANDX_COMMAND] = SrvComNoAndx;
  331. SrvDispatchTable[SMB_COM_QUERY_INFORMATION] = SrvComQueryInformation;
  332. SrvDispatchTable[SMB_COM_SET_INFORMATION] = SrvComSetInformation;
  333. SrvDispatchTable[SMB_COM_CHECK_DIRECTORY] = SrvComCheckDirectory;
  334. SrvDispatchTable[SMB_COM_FIND_CLOSE2] = SrvComFindClose2;
  335. SrvDispatchTable[SMB_COM_DELETE] = SrvComDelete;
  336. SrvDispatchTable[SMB_COM_RENAME] = SrvComRename;
  337. SrvDispatchTable[SMB_COM_CREATE_DIRECTORY] = SrvComCreateDirectory;
  338. SrvDispatchTable[SMB_COM_DELETE_DIRECTORY] = SrvComDeleteDirectory;
  339. SrvDispatchTable[SMB_COM_OPEN_ANDX] = SrvComOpenAndx;
  340. SrvDispatchTable[SMB_COM_OPEN] = SrvComOpen;
  341. SrvDispatchTable[SMB_COM_WRITE] = SrvComWrite;
  342. SrvDispatchTable[SMB_COM_CLOSE] = SrvComClose;
  343. SrvDispatchTable[SMB_COM_READ_ANDX] = SrvComReadAndx;
  344. SrvDispatchTable[SMB_COM_QUERY_INFORMATION2] = SrvComQueryInformation2;
  345. SrvDispatchTable[SMB_COM_SET_INFORMATION2] = SrvComSetInformation2;
  346. SrvDispatchTable[SMB_COM_LOCKING_ANDX] = SrvComLockingAndx;
  347. SrvDispatchTable[SMB_COM_SEEK] = SrvComSeek;
  348. SrvDispatchTable[SMB_COM_FLUSH] = SrvComFlush;
  349. SrvDispatchTable[SMB_COM_LOGOFF_ANDX] = SrvComLogoffAndx;
  350. SrvDispatchTable[SMB_COM_TREE_DISCONNECT] = SrvComTreeDisconnect;
  351. SrvDispatchTable[SMB_COM_FIND_NOTIFY_CLOSE] = SrvComFindNotifyClose;
  352. SrvDispatchTable[SMB_COM_SEARCH] = SrvComSearch;
  353. SrvDispatchTable[SMB_COM_IOCTL] = SrvComIoctl;
  354. SrvDispatchTable[SMB_COM_ECHO] = SrvComEcho;
  355. ZeroMemory((PVOID) Trans2DispatchTable, sizeof(Trans2DispatchTable));
  356. Trans2DispatchTable[TRANS2_QUERY_FS_INFORMATION] = Trans2QueryFsInfo;
  357. Trans2DispatchTable[TRANS2_FIND_FIRST2] = Trans2FindFirst2;
  358. Trans2DispatchTable[TRANS2_FIND_NEXT2] = Trans2FindNext2;
  359. Trans2DispatchTable[TRANS2_QUERY_PATH_INFORMATION] = Trans2QueryPathInfo;
  360. Trans2DispatchTable[TRANS2_SET_PATH_INFORMATION] = Trans2SetPathInfo;
  361. Trans2DispatchTable[TRANS2_QUERY_FILE_INFORMATION] = Trans2QueryFileInfo;
  362. Trans2DispatchTable[TRANS2_SET_FILE_INFORMATION] = Trans2SetFileInfo;
  363. Trans2DispatchTable[TRANS2_GET_DFS_REFERRAL] = Trans2GetDfsReferral;
  364. }
  365. USHORT
  366. attribs_to_smb_attribs(
  367. UINT32 attribs
  368. )
  369. {
  370. USHORT smb_attribs = 0;
  371. if (attribs & ATTR_READONLY) smb_attribs |= SMB_FILE_ATTRIBUTE_READONLY;
  372. if (attribs & ATTR_HIDDEN) smb_attribs |= SMB_FILE_ATTRIBUTE_HIDDEN;
  373. if (attribs & ATTR_SYSTEM) smb_attribs |= SMB_FILE_ATTRIBUTE_SYSTEM;
  374. if (attribs & ATTR_ARCHIVE) smb_attribs |= SMB_FILE_ATTRIBUTE_ARCHIVE;
  375. if (attribs & ATTR_DIRECTORY) smb_attribs |= SMB_FILE_ATTRIBUTE_DIRECTORY;
  376. return smb_attribs;
  377. }
  378. UINT32
  379. smb_attribs_to_attribs(
  380. USHORT smb_attribs
  381. )
  382. {
  383. UINT32 attribs = 0;
  384. if (smb_attribs & SMB_FILE_ATTRIBUTE_READONLY) attribs |= ATTR_READONLY;
  385. if (smb_attribs & SMB_FILE_ATTRIBUTE_HIDDEN) attribs |= ATTR_HIDDEN;
  386. if (smb_attribs & SMB_FILE_ATTRIBUTE_SYSTEM) attribs |= ATTR_SYSTEM;
  387. if (smb_attribs & SMB_FILE_ATTRIBUTE_ARCHIVE) attribs |= ATTR_ARCHIVE;
  388. if (smb_attribs & SMB_FILE_ATTRIBUTE_DIRECTORY) attribs |= ATTR_DIRECTORY;
  389. return attribs;
  390. }
  391. UINT32
  392. smb_access_to_flags(
  393. USHORT access
  394. )
  395. {
  396. UINT32 flags = 0;
  397. switch (access & SMB_DA_SHARE_MASK) {
  398. case SMB_DA_SHARE_COMPATIBILITY:
  399. case SMB_DA_SHARE_DENY_NONE:
  400. flags |= SHARE_READ | SHARE_WRITE;
  401. break;
  402. case SMB_DA_SHARE_DENY_WRITE:
  403. flags |= SHARE_READ;
  404. break;
  405. case SMB_DA_SHARE_DENY_READ:
  406. flags |= SHARE_WRITE;
  407. break;
  408. case SMB_DA_SHARE_EXCLUSIVE:
  409. default:
  410. break;
  411. }
  412. switch (access & SMB_DA_ACCESS_MASK) {
  413. case SMB_DA_ACCESS_READ:
  414. case SMB_DA_ACCESS_EXECUTE:
  415. flags |= ACCESS_READ;
  416. break;
  417. case SMB_DA_ACCESS_WRITE:
  418. flags |= ACCESS_WRITE;
  419. break;
  420. case SMB_DA_ACCESS_READ_WRITE:
  421. flags |= ACCESS_READ | ACCESS_WRITE;
  422. break;
  423. }
  424. if (access & SMB_DO_NOT_CACHE) {
  425. flags |= CACHE_NO_BUFFERING;
  426. }
  427. if (access & SMB_DA_WRITE_THROUGH) {
  428. flags |= CACHE_WRITE_THROUGH;
  429. }
  430. return flags;
  431. }
  432. UINT32
  433. smb_openfunc_to_flags(
  434. USHORT openfunc
  435. )
  436. {
  437. switch (openfunc & (SMB_OFUN_OPEN_MASK | SMB_OFUN_CREATE_MASK)) {
  438. case (SMB_OFUN_CREATE_FAIL | SMB_OFUN_OPEN_FAIL):
  439. return 0;
  440. case (SMB_OFUN_CREATE_FAIL | SMB_OFUN_OPEN_OPEN):
  441. return DISP_OPEN_EXISTING;
  442. case (SMB_OFUN_CREATE_FAIL | SMB_OFUN_OPEN_TRUNCATE):
  443. return DISP_TRUNCATE_EXISTING;
  444. case (SMB_OFUN_CREATE_CREATE | SMB_OFUN_OPEN_FAIL):
  445. return DISP_CREATE_NEW;
  446. case (SMB_OFUN_CREATE_CREATE | SMB_OFUN_OPEN_OPEN):
  447. return DISP_OPEN_ALWAYS;
  448. case (SMB_OFUN_CREATE_CREATE | SMB_OFUN_OPEN_TRUNCATE):
  449. return DISP_CREATE_ALWAYS;
  450. default:
  451. return 0;
  452. }
  453. }
  454. void
  455. local_time64(
  456. TIME64 *time // system time to be converted to local time
  457. )
  458. {
  459. TIME64 local = *time;
  460. FileTimeToLocalFileTime((LPFILETIME)time, (LPFILETIME)&local);
  461. *time = local;
  462. }
  463. void
  464. sys_time64(
  465. TIME64 *time // local time to be converted to system time
  466. )
  467. {
  468. TIME64 sys = *time;
  469. LocalFileTimeToFileTime((LPFILETIME)time, (LPFILETIME)&sys);
  470. *time = sys;
  471. }
  472. void
  473. smb_datetime_to_time64(
  474. const USHORT smbdate,
  475. const USHORT smbtime,
  476. TIME64 *time
  477. )
  478. {
  479. DosDateTimeToFileTime(smbdate, smbtime, (LPFILETIME)time);
  480. sys_time64(time);
  481. }
  482. typedef struct {
  483. SMB_TIME time;
  484. SMB_DATE date;
  485. }SMB_TIMEDATE;
  486. TIME64 // system time
  487. smb_timedate_to_time64(
  488. ULONG smb_timedate // local time
  489. )
  490. {
  491. TIME64 time = 0;
  492. DosDateTimeToFileTime(((SMB_TIMEDATE*)&smb_timedate)->date.Ushort,
  493. ((SMB_TIMEDATE*)&smb_timedate)->time.Ushort,
  494. (LPFILETIME)&time);
  495. sys_time64(&time);
  496. return time;
  497. }
  498. void
  499. _time64_to_smb_datetime(
  500. TIME64 *time,
  501. USHORT *smbdate,
  502. USHORT *smbtime
  503. )
  504. {
  505. local_time64(time);
  506. *smbdate = *smbtime = 0;
  507. FileTimeToDosDateTime((LPFILETIME)time, smbdate, smbtime);
  508. }
  509. ULONG // local time
  510. time64_to_smb_timedate(
  511. TIME64 *time // system time
  512. )
  513. {
  514. ULONG smb_timedate = 0;
  515. local_time64(time);
  516. FileTimeToDosDateTime((LPFILETIME)&time,
  517. &(((SMB_TIMEDATE*)&smb_timedate)->date.Ushort),
  518. &(((SMB_TIMEDATE*)&smb_timedate)->date.Ushort));
  519. return smb_timedate;
  520. }
  521. #define dword_in_range(in, low, high) ((low <= in) && (in <= high))
  522. void set_DOSERROR(Packet_t * msg, USHORT ec, USHORT err)
  523. {
  524. msg->out.smb->Status.DosError.ErrorClass = (unsigned char) ec;
  525. msg->out.smb->Status.DosError.Error = err;
  526. }
  527. void SET_WIN32ERROR(
  528. Packet_t * msg,
  529. DWORD error
  530. )
  531. {
  532. if (!error) return;
  533. if (dword_in_range(error, 1, 18) || (error == 32) || (error == 33) ||
  534. (error == 80) || dword_in_range(error, 230, 234) || (error == 145)) {
  535. set_DOSERROR(msg,
  536. SMB_ERR_CLASS_DOS,
  537. (USHORT)error);
  538. } else if (dword_in_range(error, 19, 31) ||
  539. (error == 34) || (error == 36) || (error == 39)) {
  540. set_DOSERROR(msg,
  541. SMB_ERR_CLASS_HARDWARE,
  542. (USHORT)error);
  543. } else if (error == 112) {
  544. SET_DOSERROR(msg, HARDWARE, DISK_FULL);
  545. } else if (error == 67) {
  546. SET_DOSERROR(msg, SERVER, BAD_NET_NAME);
  547. } else if (error == 123) {
  548. set_DOSERROR(msg,
  549. SMB_ERR_CLASS_SERVER,
  550. (USHORT)error);
  551. } else if (error == 183) {
  552. SET_DOSERROR(msg, DOS, FILE_EXISTS);
  553. } else {
  554. SET_DOSERROR(msg, SERVER, ERROR);
  555. }
  556. }