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.

216 lines
6.4 KiB

  1. #include "tftpd.h"
  2. char StartDirectory[500] = "";
  3. int StartDirectoryLen = 0;
  4. // shell patterns.
  5. char ValidClients[ 50] = "*"; // eg. 157.*.55?.0*
  6. char ValidMasters[ 50] = "*"; // eg. 157.*.55?.0*
  7. char ValidReadFiles[ 50] = "*"; // eg. r*.txt
  8. char ValidWriteFiles[50] = "*"; // eg. w*.txt
  9. // ========================================================================
  10. // Read constants from TFTPD_REGKEY in registry.
  11. // returns number of keys read.
  12. //
  13. int
  14. ReadRegistryValues( void )
  15. {
  16. int dwErrcode;
  17. int ok = 0;
  18. HKEY tkey;
  19. DWORD dwType, dwValueSize;
  20. dwErrcode = RegOpenKeyEx( HKEY_LOCAL_MACHINE, TFTPD_REGKEY,
  21. 0, KEY_ALL_ACCESS, &tkey );
  22. if ( dwErrcode != ERROR_SUCCESS )
  23. {
  24. DbgPrint("RegOpenKeyEx %s failed, err=%d\n", TFTPD_REGKEY,
  25. GetLastError() );
  26. return 0;
  27. }
  28. // =====================================================================
  29. if( StartDirectory[0] == '\0' ){
  30. dwValueSize = sizeof( StartDirectory );
  31. dwErrcode = RegQueryValueEx( tkey,
  32. TFTPD_REGKEY_DIR,
  33. NULL,
  34. &dwType,
  35. (LPBYTE)&StartDirectory[0],
  36. &dwValueSize );
  37. if( dwErrcode != ERROR_SUCCESS ){
  38. DbgPrint("RegQueryValueEx %s failed, err=%d\n", TFTPD_REGKEY_DIR,
  39. GetLastError() );
  40. }else if( dwType == REG_SZ ){
  41. DbgPrint("ReadRegistryValues: %s=%s\n",
  42. TFTPD_REGKEY_DIR, StartDirectory );
  43. ok++;
  44. }
  45. }
  46. // =====================================================================
  47. dwValueSize = sizeof( ValidClients );
  48. dwErrcode = RegQueryValueEx( tkey,
  49. TFTPD_REGKEY_CLIENTS,
  50. NULL,
  51. &dwType,
  52. (LPBYTE)&ValidClients[0],
  53. &dwValueSize );
  54. if( dwErrcode != ERROR_SUCCESS ){
  55. DbgPrint("RegQueryValueEx %s failed, err=%d\n",
  56. TFTPD_REGKEY_CLIENTS,
  57. GetLastError() );
  58. }else if( dwType == REG_SZ ){
  59. DbgPrint("ReadRegistryValues: %s=%s\n",
  60. TFTPD_REGKEY_CLIENTS, ValidClients );
  61. ok++;
  62. }
  63. // =====================================================================
  64. dwValueSize = sizeof( ValidMasters );
  65. dwErrcode = RegQueryValueEx( tkey,
  66. TFTPD_REGKEY_MASTERS,
  67. NULL,
  68. &dwType,
  69. (LPBYTE)&ValidMasters[0],
  70. &dwValueSize );
  71. if( dwErrcode != ERROR_SUCCESS ){
  72. DbgPrint("RegQueryValueEx %s failed, err=%d\n",
  73. TFTPD_REGKEY_MASTERS,
  74. GetLastError() );
  75. }else if( dwType == REG_SZ ){
  76. DbgPrint("ReadRegistryValues: %s=%s\n",
  77. TFTPD_REGKEY_MASTERS, ValidMasters );
  78. ok++;
  79. }
  80. // =====================================================================
  81. dwValueSize = sizeof( ValidReadFiles );
  82. dwErrcode = RegQueryValueEx( tkey,
  83. TFTPD_REGKEY_READABLE,
  84. NULL,
  85. &dwType,
  86. (LPBYTE)&ValidReadFiles[0],
  87. &dwValueSize );
  88. if( dwErrcode != ERROR_SUCCESS ){
  89. DbgPrint("RegQueryValueEx %s failed, err=%d\n",
  90. TFTPD_REGKEY_READABLE,
  91. GetLastError() );
  92. }else if( dwType == REG_SZ ){
  93. DbgPrint("ReadRegistryValues: %s=%s\n",
  94. TFTPD_REGKEY_READABLE, ValidReadFiles );
  95. ok++;
  96. }
  97. // =====================================================================
  98. dwValueSize = sizeof( ValidWriteFiles );
  99. dwErrcode = RegQueryValueEx( tkey,
  100. TFTPD_REGKEY_WRITEABLE,
  101. NULL,
  102. &dwType,
  103. (LPBYTE)&ValidWriteFiles[0],
  104. &dwValueSize );
  105. if( dwErrcode != ERROR_SUCCESS ){
  106. DbgPrint("RegQueryValueEx %s failed, err=%d\n",
  107. TFTPD_REGKEY_WRITEABLE,
  108. GetLastError() );
  109. }else if( dwType == REG_SZ ){
  110. DbgPrint("ReadRegistryValues: %s=%s\n",
  111. TFTPD_REGKEY_WRITEABLE, ValidWriteFiles );
  112. ok++;
  113. }
  114. // =====================================================================
  115. RegCloseKey( tkey );
  116. return ok;
  117. }
  118. // ========================================================================
  119. //
  120. //
  121. int
  122. Set_StartDirectory( void )
  123. {
  124. char ExpandedDir[500];
  125. DWORD ExpandedDirLen=0;
  126. if( StartDirectory[0] == '\0' ){
  127. strncpy( StartDirectory, TFTPD_DEFAULT_DIR,
  128. sizeof( StartDirectory ));
  129. }
  130. ExpandedDirLen=ExpandEnvironmentStrings( StartDirectory,ExpandedDir,500);
  131. if (ExpandedDirLen == 0) {
  132. return ERROR_INVALID_PARAMETER;
  133. }
  134. memcpy(StartDirectory,ExpandedDir,ExpandedDirLen);
  135. //
  136. // Set the (one time) length and trailing slash.
  137. //
  138. StartDirectoryLen = strlen( StartDirectory );
  139. if( StartDirectory[ StartDirectoryLen-1 ] == '/' )
  140. StartDirectory[ StartDirectoryLen-1 ] = '\\';
  141. if( StartDirectory[ StartDirectoryLen-1 ] != '\\' &&
  142. StartDirectoryLen < sizeof( StartDirectory )
  143. ){
  144. strcat( StartDirectory, "\\" );
  145. }
  146. StartDirectoryLen = strlen( StartDirectory );
  147. assert( 0 < StartDirectoryLen );
  148. assert( StartDirectoryLen < sizeof( StartDirectory ) );
  149. assert( StartDirectory[ StartDirectoryLen - 1 ] == '\\' );
  150. assert( StartDirectory[ StartDirectoryLen ] == '\0' );
  151. return 1;
  152. }
  153. // ========================================================================
  154. // From C FAQ.
  155. // * matches one or more chars, eg. match( "a*b", "a..b" ).
  156. // ? matches exactly one char, eg. match( "a?b", "a.b" ).
  157. int match( const char * p, const char * s )
  158. {
  159. switch( *p ){
  160. case '\0' : return ! *s ;
  161. case '*' : return match( p+1, s ) || *s && match( p, s+1 );
  162. case '?' : return *s && match( p+1, s+1 );
  163. default : return *p == *s && match( p+1, s+1 );
  164. }
  165. }
  166. // ========================================================================