Leaked source code of windows server 2003
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.

279 lines
6.9 KiB

  1. #include "uddi.xp.h"
  2. //
  3. // Add your new Extended Stored Procedure from a Visual Studio Data Project,
  4. // or using the SQL Server Enterprise Manager, or by executing the following
  5. // SQL command:
  6. // sp_addextendedproc 'xp_reset_key', 'uddi.xp.dll'
  7. //
  8. // You may drop the extended stored procedure by using the SQL command:
  9. // sp_dropextendedproc 'xp_reset_key'
  10. //
  11. // You may release the DLL from the Server (to delete or replace the file), by
  12. // using the SQL command:
  13. // DBCC xp_reset_key(FREE)
  14. //
  15. // sp_addextendedproc 'xp_reset_key', 'uddi.xp.dll'
  16. // sp_dropextendedproc 'xp_reset_key'
  17. // exec xp_reset_key
  18. //
  19. // DBCC xp_reset_key(FREE)
  20. //
  21. RETCODE xp_reset_key( SRV_PROC *srvproc )
  22. {
  23. DBSMALLINT i = 0;
  24. DBCHAR spName[ MAXNAME ];
  25. DBCHAR spText[ MAXTEXT ];
  26. CHAR bReadBuffer[ 255 ];
  27. DWORD cbReadBuffer = 0;
  28. DBINT cnt = 0;
  29. DBINT rows = 0;
  30. BOOL fSuccess = FALSE;
  31. STARTUPINFOA si;
  32. PROCESS_INFORMATION pi;
  33. SECURITY_ATTRIBUTES saPipe;
  34. HANDLE hReadPipe = NULL;
  35. HANDLE hWritePipe = NULL;
  36. DWORD dwExitCode = 0;
  37. BOOL fSendRowNotFailed = TRUE;
  38. //
  39. // Name of this procedure
  40. //
  41. _snprintf( spName, MAXNAME, "xp_reset_key" );
  42. spName[ MAXNAME - 1 ] = 0x00;
  43. //
  44. // Send a text message
  45. //
  46. _snprintf( spText, MAXTEXT, "UDDI Services Extended Stored Procedure: %s\n", spName );
  47. spText[ MAXTEXT - 1 ] = 0x00;
  48. srv_sendmsg(
  49. srvproc,
  50. SRV_MSG_INFO,
  51. 0,
  52. (DBTINYINT)0,
  53. (DBTINYINT)0,
  54. NULL,
  55. 0,
  56. 0,
  57. spText,
  58. SRV_NULLTERM );
  59. string strResetKeyFile = GetUddiInstallDirectory();
  60. if( 0 == strResetKeyFile.length() )
  61. {
  62. ReportError( srvproc, "GetUddiInstallDirectory" );
  63. return FAIL;
  64. }
  65. strResetKeyFile += "\\resetkey.exe";
  66. _snprintf( spText, MAXTEXT, "Resetkey.exe Installed at location: %s\n", strResetKeyFile.c_str() );
  67. spText[ MAXTEXT - 1 ] = 0x00;
  68. srv_sendmsg(
  69. srvproc,
  70. SRV_MSG_INFO,
  71. 0,
  72. (DBTINYINT)0,
  73. (DBTINYINT)0,
  74. NULL,
  75. 0,
  76. 0,
  77. spText,
  78. SRV_NULLTERM );
  79. //
  80. // Create child process to execute the command string. Use an
  81. // anonymous pipe to read the output from the command and send
  82. // any results to the client.
  83. // In order for the child process to be able to write
  84. // to the anonymous pipe, the handle must be marked as
  85. // inheritable by child processes by setting the
  86. // SECURITY_ATTRIBUTES.bInheritHandle flag to TRUE.
  87. //
  88. saPipe.nLength = sizeof( SECURITY_ATTRIBUTES );
  89. saPipe.lpSecurityDescriptor = NULL;
  90. saPipe.bInheritHandle = TRUE;
  91. fSuccess = CreatePipe(
  92. &hReadPipe, // read handle
  93. &hWritePipe, // write handle
  94. &saPipe, // security descriptor
  95. 0 ); // use default pipe buffer size
  96. if( !fSuccess )
  97. {
  98. ReportError( srvproc, "CreatePipe", GetLastError() );
  99. return FAIL;
  100. }
  101. //
  102. // Now we must set standard out and standard error to the
  103. // write end of the pipe. Once standard out and standard
  104. // error are set to the pipe handle, we must close the pipe
  105. // handle so that when the child process dies, the write end
  106. // of the pipe will close, setting an EOF condition on the pipe.
  107. //
  108. memset( &si, 0, sizeof(si) );
  109. si.cb = sizeof(si);
  110. si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  111. si.wShowWindow = SW_HIDE;
  112. si.hStdOutput = hWritePipe;
  113. si.hStdError = hWritePipe;
  114. //
  115. // Set the fInheritHandles parameter to TRUE so that open
  116. // file handles will be inheritied. We can close the child
  117. // process and thread handles as we won't be needing them.
  118. // The child process will not die until these handles are
  119. // closed.
  120. //
  121. char params[ 6 ];
  122. params[ 0 ] = 0x00;
  123. strncat( params, " /now", 6 );
  124. params[ 5 ] = 0x00;
  125. fSuccess = CreateProcessA(
  126. strResetKeyFile.c_str(), // filename
  127. params, // command line for child
  128. NULL, // process security descriptor
  129. NULL, // thread security descriptor
  130. TRUE, // inherit handles?
  131. 0, // creation flags
  132. NULL, // inherited environment address
  133. NULL, // startup dir; NULL = start in current
  134. &si, // pointer to startup info (input)
  135. &pi ); // pointer to process info (output)
  136. if (!fSuccess)
  137. {
  138. ReportError( srvproc, "CreateProcess", GetLastError() );
  139. return FAIL;
  140. }
  141. //
  142. // We need to close our instance of the inherited pipe write
  143. // handle now that it's been inherited so that it will actually
  144. // close when the child process ends. This will put an EOF
  145. // condition on the pipe which we can then detect.
  146. //
  147. fSuccess = CloseHandle( hWritePipe );
  148. if( !fSuccess )
  149. {
  150. ReportError( srvproc, "CloseHandle", GetLastError() );
  151. CloseHandle( pi.hThread );
  152. CloseHandle( pi.hProcess );
  153. return FAIL;
  154. }
  155. string strOutput = "";
  156. //
  157. // Now read from the pipe until EOF condition reached.
  158. //
  159. do
  160. {
  161. cnt = 0;
  162. while( ( cnt < ( sizeof( bReadBuffer ) / sizeof( bReadBuffer[0] ) ) ) &&
  163. ( 0 != (fSuccess = ReadFile(
  164. hReadPipe, // read handle
  165. &bReadBuffer[cnt], // buffer for incoming data
  166. 1, // number of bytes to read
  167. &cbReadBuffer, // number of bytes actually read
  168. NULL ) ) ) )
  169. {
  170. if( !fSuccess )
  171. {
  172. if( ERROR_BROKEN_PIPE == GetLastError() )
  173. {
  174. break;
  175. }
  176. //
  177. // Child has died
  178. //
  179. ReportError( srvproc, "CloseHandle", GetLastError() );
  180. CloseHandle( pi.hThread );
  181. CloseHandle( pi.hProcess );
  182. return FAIL;
  183. }
  184. if( '\n' == bReadBuffer[ cnt ] )
  185. {
  186. break;
  187. }
  188. else
  189. {
  190. cnt++;
  191. }
  192. }
  193. if( fSuccess && cbReadBuffer )
  194. {
  195. if( !cnt )
  196. {
  197. bReadBuffer[ 0 ] = ' ';
  198. cnt = 1;
  199. }
  200. //
  201. // Remove carriage return if it exists
  202. //
  203. // if( 0x0D == bReadBuffer[ cnt-1 ] )
  204. // {
  205. // cnt--;
  206. // }
  207. if( cnt >= 0 )
  208. {
  209. bReadBuffer[ cnt ] = 0x00;
  210. //
  211. // Send program output back as information
  212. //
  213. strOutput.append( bReadBuffer, cnt );
  214. srv_sendmsg(
  215. srvproc,
  216. SRV_MSG_INFO,
  217. 0,
  218. (DBTINYINT)0,
  219. (DBTINYINT)0,
  220. NULL,
  221. 0,
  222. 0,
  223. bReadBuffer,
  224. cnt );
  225. }
  226. }
  227. }
  228. while( fSuccess && cbReadBuffer );
  229. OutputDebugStringA( strOutput.c_str() );
  230. //
  231. // Close the trace file, pipe handles
  232. //
  233. CloseHandle( hReadPipe );
  234. if( !GetExitCodeProcess( pi.hProcess, &dwExitCode ) || dwExitCode != 0 )
  235. {
  236. ReportError( srvproc, "GetExitCodeProcess", dwExitCode );
  237. return FAIL;
  238. }
  239. CloseHandle( pi.hThread );
  240. CloseHandle( pi.hProcess );
  241. return XP_NOERROR ;
  242. }