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.

531 lines
11 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. tftpd.h
  5. Abstract:
  6. This contains the constants and types for the tftp daemon.
  7. Author:
  8. Full Name (email name) DD-MMM-YYYY
  9. Sam Patton (sampa) 08-apr-1992
  10. Environment:
  11. Streams
  12. Revision History:
  13. dd-mmm-yyy <email>
  14. MohsinA, 02-Dec-96, 29-May-97.
  15. See discussion in R.Steven's books:
  16. - "Unix Network Programming", Prentice Hall, 1990.
  17. - TCP/IP Illustrated, Vol 1. Addison Wesley.
  18. --*/
  19. #include <nt.h>
  20. #include <ntrtl.h>
  21. #include <nturtl.h>
  22. #include <windef.h>
  23. #include <winbase.h>
  24. #include <winsock2.h>
  25. #if defined(REMOTE_BOOT_SECURITY)
  26. #include <ntseapi.h>
  27. #endif //defined(REMOTE_BOOT_SECURITY)
  28. #include <assert.h>
  29. #include <ctype.h>
  30. #include <direct.h>
  31. #include <fcntl.h>
  32. #include <io.h>
  33. #include <malloc.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <sys/stat.h>
  38. #include <time.h>
  39. #include <lmerr.h>
  40. #include <lmcons.h>
  41. #include <netlib.h>
  42. #include <iphlpapi.h>
  43. #include <iprtrmib.h>
  44. #if defined(REMOTE_BOOT_SECURITY)
  45. #include <security.h>
  46. #include <ntlmsp.h>
  47. #include <spseal.h>
  48. #endif //defined(REMOTE_BOOT_SECURITY)
  49. //
  50. // max size of a tftp datagram is 2 byte opcode, 2 byte data count, and a
  51. // negotiated number of bytes of data
  52. //
  53. #define MAX_TFTP_DATAGRAM 65468
  54. #define MAX_OACK_PACKET_LENGTH 1460
  55. #define TFTPD_INITIAL_TIMEOUT 1 // seconds
  56. #define TFTPD_MAX_TIMEOUT 10 // seconds
  57. #define REAPER_INTERVAL_SEC 60
  58. #define MAX_TFTP_DATA (MAX_TFTP_DATAGRAM - 4)
  59. // Number of reaper hits before context deleted
  60. #define DEAD_CONTEXT_COUNT 4
  61. #define TFTPD_RRQ 1
  62. #define TFTPD_WRQ 2
  63. #define TFTPD_DATA 3
  64. #define TFTPD_ACK 4
  65. #define TFTPD_ERROR 5
  66. #define TFTPD_OACK 6
  67. #define TFTPD_LOGIN 16
  68. #define TFTPD_KEY 17
  69. #define MAX_TFTPD_RETRIES 10
  70. #define TFTPD_ERROR_UNDEFINED 0
  71. #define TFTPD_ERROR_FILE_NOT_FOUND 1
  72. #define TFTPD_ERROR_ACCESS_VIOLATION 2
  73. #define TFTPD_ERROR_DISK_FULL 3
  74. #define TFTPD_ERROR_ILLEGAL_OPERATION 4
  75. #define TFTPD_ERROR_UNKNOWN_TRANSFER_ID 5
  76. #define TFTPD_ERROR_FILE_EXISTS 6
  77. #define TFTPD_ERROR_NO_SUCH_USER 7
  78. #define TFTPD_ERROR_OPTION_NEGOT_FAILED 8
  79. #define NUM_TFTP_ERROR_CODES 9
  80. #define REG_NEW_SOCKET 0x1
  81. #define REG_CONTINUE_SOCKET 0x2
  82. //
  83. // Types
  84. //
  85. typedef struct _TFTP_GLOBALS {
  86. CRITICAL_SECTION Lock; //protects r/w access to all fields
  87. LIST_ENTRY WorkList; //list of outstanding work contexts
  88. HANDLE TimerQueueHandle;
  89. } TFTP_GLOBALS, *PTFTP_GLOBALS;
  90. typedef struct _TFTP_REQUEST {
  91. LIST_ENTRY RequestLinkage;
  92. SOCKET TftpdPort;
  93. struct sockaddr_in ForeignAddress;
  94. IPAddr MyAddr;
  95. DWORD BlockSize;
  96. DWORD FileSize;
  97. int Timeout;
  98. DWORD DataSize; //actual size of data of incoming packet
  99. HANDLE RcvEvent; //event for WSARecvFrom
  100. #if defined(REMOTE_BOOT_SECURITY)
  101. ULONG SecurityHandle;
  102. char Sign[NTLMSSP_MESSAGE_SIGNATURE_SIZE];
  103. #endif //defined(REMOTE_BOOT_SECURITY)
  104. char Packet1[MAX_TFTP_DATAGRAM + 1];
  105. char Packet2[MAX_TFTP_DATAGRAM];
  106. char Packet3[MAX_TFTP_DATAGRAM];
  107. } TFTP_REQUEST, *PTFTP_REQUEST;
  108. // ========================================================================
  109. #if defined(REMOTE_BOOT_SECURITY)
  110. typedef struct _TFTPD_SECURITY {
  111. struct sockaddr_in ForeignAddress; // remote IP address
  112. USHORT Validation; // used to check consistency of handles -- 0 means it is not in use
  113. BOOLEAN LoginComplete; // have we successfully logged them in
  114. BOOLEAN CredentialsHandleValid;
  115. BOOLEAN ServerContextHandleValid;
  116. BOOLEAN GeneratedKey;
  117. SECURITY_STATUS LoginStatus;
  118. CtxtHandle ServerContextHandle;
  119. CredHandle CredentialsHandle;
  120. ULONG ContextAttributes;
  121. ULONG Key; // the key if he requests one
  122. UCHAR SignedKey[4]; // the signed version of the key
  123. UCHAR Sign[NTLMSSP_MESSAGE_SIGNATURE_SIZE]; // the actual sign
  124. UCHAR LastFileRead[64]; // the last 64 bytes of the name of the last file read
  125. UCHAR LastFileSign[NTLMSSP_MESSAGE_SIGNATURE_SIZE]; // the sign of the filename in the request
  126. USHORT LastFileReadPort; // the port it was read on
  127. } TFTPD_SECURITY, *PTFTPD_SECURITY;
  128. #endif //defined(REMOTE_BOOT_SECURITY)
  129. typedef enum _CONTEXT_TYPE_ENUM {NO_CONTEXT,READ_CONTEXT,WRITE_CONTEXT,LOGIN_CONTEXT,KEY_CONTEXT} CONTEXT_TYPE_ENUM;
  130. typedef struct _TFTP_CONTEXT_HEADER {
  131. CRITICAL_SECTION Lock;
  132. LIST_ENTRY ContextLinkage;
  133. SOCKET Sock;
  134. CONTEXT_TYPE_ENUM ContextType;
  135. struct sockaddr_in ForeignAddress;
  136. char *Packet;
  137. HANDLE SocketEvent;
  138. HANDLE WaitEvent;
  139. DWORD DueTime; //timeout interval in msecs
  140. HANDLE TimerHandle;
  141. DWORD IdleCount; //to test if connection died
  142. DWORD RetransmissionCount;
  143. DWORD RefCount;
  144. WORD SendFail;
  145. BOOL Closing;
  146. } TFTP_CONTEXT_HEADER, *PTFTP_CONTEXT_HEADER;
  147. typedef struct _TFTP_READ_WRITE_CONTEXT_HEADER {
  148. TFTP_CONTEXT_HEADER ;
  149. WORD BlockNumber;
  150. DWORD BlockSize;
  151. DWORD BytesRead;
  152. DWORD packetLength;
  153. DWORD oackLength;
  154. BOOL FixedTimer; // true if timeout option received
  155. int fd;
  156. } TFTP_READ_WRITE_CONTEXT_HEADER, *PTFTP_READ_WRITE_CONTEXT_HEADER;
  157. //N.B. The first field of ALL following contexts must be TFTP_CONTEXT_HEADER or
  158. // TFTP_READ_WRITE_CONTEXT_HEADER
  159. typedef struct _TFTP_READ_CONTEXT {
  160. // Fields in ALL Contexts
  161. TFTP_READ_WRITE_CONTEXT_HEADER ;
  162. // Begin Context Specific Data
  163. BOOL Done;
  164. #if defined(REMOTE_BOOT_SECURITY)
  165. DWORD EncryptedBytesSent;
  166. char *EncryptFileBuffer;
  167. TFTPD_SECURITY Security;
  168. int EncryptBytesSent;
  169. SecBufferDesc SignMessage;
  170. SecBuffer SigBuffers[2];
  171. #endif //defined(REMOTE_BOOT_SECURITY)
  172. } TFTP_READ_CONTEXT, *PTFTP_READ_CONTEXT;
  173. typedef struct _TFTP_WRITE_CONTEXT {
  174. // Fields in All contexts
  175. TFTP_READ_WRITE_CONTEXT_HEADER ;
  176. // Begin Context Specific Data
  177. int FileMode;
  178. #if defined(REMOTE_BOOT_SECURITY)
  179. TFTPD_SECURITY Security;
  180. #endif //defined(REMOTE_BOOT_SECURITY)
  181. } TFTP_WRITE_CONTEXT, *PTFTP_WRITE_CONTEXT;
  182. typedef struct _TFTP_LOGIN_CONTEXT {
  183. // Fields in all contexts
  184. TFTP_CONTEXT_HEADER ;
  185. // Begin Context Specific Data
  186. } TFTP_LOGIN_CONTEXT, *PTFTP_LOGIN_CONTEXT;
  187. typedef struct _TFTP_KEY_CONTEXT {
  188. // Fields in all contexts
  189. TFTP_CONTEXT_HEADER ;
  190. // Begin Context Specific Data
  191. } TFTP_KEY_CONTEXT, *PTFTP_KEY_CONTEXT;
  192. // ========================================================================
  193. struct TFTPD_STAT {
  194. time_t started_at; // updated in main.
  195. unsigned int req_read; // updated in master.
  196. unsigned int req_write; // updated in master.
  197. #if defined(REMOTE_BOOT_SECURITY)
  198. unsigned int req_login; // updated in master.
  199. unsigned int req_key; // updated in master.
  200. #endif defined(REMOTE_BOOT_SECURITY)
  201. unsigned int req_error; // updated in master.
  202. unsigned int req_asc;
  203. unsigned int req_bin;
  204. unsigned int bytes_sent;
  205. unsigned int bytes_recv;
  206. unsigned int errors;
  207. };
  208. typedef struct {
  209. LIST_ENTRY Linkage;
  210. SOCKET Sock;
  211. IPAddr IPAddress;
  212. HANDLE WaitHandle;
  213. HANDLE WaitEvent;
  214. BOOL Referenced;
  215. DWORD Flags;
  216. } SocketEntry, *PSocketEntry;
  217. // ========================================================================
  218. //
  219. // Prototypes
  220. //
  221. void
  222. TftpdReleaseContextLock(
  223. PTFTP_CONTEXT_HEADER Context
  224. );
  225. void
  226. TftpdErrorPacket(
  227. struct sockaddr * PeerAddress,
  228. char * RequestPacket,
  229. SOCKET LocalSocket,
  230. unsigned short ErrorCode,
  231. char * ErrorMessage OPTIONAL
  232. );
  233. DWORD
  234. TftpdHandleRead(
  235. PVOID
  236. );
  237. DWORD
  238. TftpdHandleWrite(
  239. PVOID
  240. );
  241. #if defined(REMOTE_BOOT_SECURITY)
  242. DWORD
  243. TftpdHandleLogin(
  244. PVOID
  245. );
  246. DWORD
  247. TftpdHandleKey(
  248. PVOID
  249. );
  250. #endif //defined(REMOTE_BOOT_SECURITY)
  251. void
  252. s_inet_ntoa(
  253. unsigned long,
  254. char *
  255. );
  256. int
  257. TftpdDoRead(
  258. int,
  259. char *,
  260. int,
  261. int);
  262. int
  263. TftpdDoWrite(
  264. int,
  265. char *,
  266. int,
  267. int,
  268. char *);
  269. unsigned long
  270. s_inet_addr(
  271. char *
  272. );
  273. VOID
  274. TftpdControlHandler(
  275. DWORD);
  276. VOID
  277. TftpdServiceExit(
  278. IN ULONG);
  279. DWORD
  280. TftpdStart(
  281. IN DWORD,
  282. IN LPTSTR *);
  283. DWORD
  284. TftpdInitializeThreadPool();
  285. DWORD
  286. TftpdThreadPool(LPVOID);
  287. VOID
  288. TftpdInitializeReceiveHeap();
  289. DWORD
  290. TftpdResumeRead(
  291. PTFTP_READ_CONTEXT Context,
  292. PTFTP_REQUEST Request
  293. );
  294. DWORD
  295. TftpdResumeWrite(
  296. PTFTP_WRITE_CONTEXT Context,
  297. PTFTP_REQUEST Request
  298. );
  299. DWORD
  300. TftpdResumeLogin(
  301. PTFTP_LOGIN_CONTEXT Context,
  302. PTFTP_REQUEST Request
  303. );
  304. DWORD
  305. TftpdResumeKey(
  306. PTFTP_KEY_CONTEXT Context,
  307. PTFTP_REQUEST Request
  308. );
  309. DWORD
  310. TftpdNewReceive(
  311. PVOID Argument,
  312. BYTE Flags
  313. );
  314. DWORD
  315. TftpdContinueReceive(
  316. PVOID Argument,
  317. BYTE Flags
  318. );
  319. VOID TftpdCleanHeap();
  320. VOID DeleteSocketEntry(SocketEntry *SE);
  321. DWORD GetIpTable(PMIB_IPADDRTABLE *AddrTable);
  322. SocketEntry* AddSocket(IPAddr Addr);
  323. DWORD CleanSocketList();
  324. DWORD LookupSocketEntryBySock(SOCKET Sock, SocketEntry **SE);
  325. VOID NTAPI InterfaceChange(PVOID Context, BOOLEAN Flag);
  326. HANDLE RegisterSocket(SOCKET Sock, HANDLE Event, DWORD Flag);
  327. VOID
  328. TftpdReaper(PVOID ReaperContext,
  329. BOOLEAN Flag);
  330. VOID
  331. TftpdFreeContext(PTFTP_CONTEXT_HEADER Context);
  332. VOID
  333. TftpdRemoveContextFromList(PTFTP_CONTEXT_HEADER Context);
  334. VOID
  335. TftpdResumeProcessing(PVOID Argument);
  336. //
  337. // Macros
  338. //
  339. //
  340. // BOOLEAN
  341. // CHECK_ACK(
  342. // char * Buffer,
  343. // unsigned short AckType,
  344. // unsigned short BlockNumber);
  345. //
  346. // This returns TRUE if this Buffer is an ack for this block number.
  347. //
  348. #define CHECK_ACK(BUFFER, ACK_TYPE, BLOCK_NUMBER) \
  349. ((*((unsigned short *) (BUFFER)) == ntohs((short)ACK_TYPE)) \
  350. && ((((unsigned short *) (BUFFER))[1]) == ntohs((short)(BLOCK_NUMBER))))
  351. extern struct TFTPD_STAT tftpd_stat;
  352. extern FILE* LogFile;
  353. extern char LogFileName[];
  354. extern BOOL LoggingEvent;
  355. #define LEN_DbgPrint 1000
  356. int TftpdPrintLog( char * format, ... );
  357. void TftpdLogEvent( WORD logtype, char message[] );
  358. void BeginLogFile( void );
  359. // #define DbgPrint printf
  360. // #define DbgPrint TftpdPrintLog
  361. #define DbgPrint
  362. //
  363. // In dir.c
  364. //
  365. int ReadRegistryValues( void );
  366. int Set_StartDirectory(void);
  367. int match( const char * p, const char * s );
  368. #define TFTPD_DEFAULT_DIR "\\tftpdroot\\"
  369. #define TFTPD_LOGFILE "tftpd.log"
  370. #define TFTPD_REGKEY "System\\CurrentControlSet\\Services\\tftpd\\parameters"
  371. #define TFTPD_REGKEY_DIR "directory"
  372. #define TFTPD_REGKEY_CLIENTS "clients"
  373. #define TFTPD_REGKEY_MASTERS "masters"
  374. #define TFTPD_REGKEY_READABLE "readable"
  375. #define TFTPD_REGKEY_WRITEABLE "writable"
  376. //
  377. // Start directory setup.
  378. //
  379. extern char StartDirectory[];
  380. extern int StartDirectoryLen;
  381. //
  382. // Client and file read/write validation.
  383. //
  384. extern char ValidClients [];
  385. extern char ValidMasters [];
  386. extern char ValidReadFiles [];
  387. extern char ValidWriteFiles[];
  388. //
  389. //
  390. //