xmlrpc_sample_add_server_w32httpsys.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (C) 2005 by Steven A. Bone, sbone@pobox.com. All rights reserved.
  2. **
  3. ** Redistribution and use in source and binary forms, with or without
  4. ** modification, are permitted provided that the following conditions
  5. ** are met:
  6. ** 1. Redistributions of source code must retain the above copyright
  7. ** notice, this list of conditions and the following disclaimer.
  8. ** 2. Redistributions in binary form must reproduce the above copyright
  9. ** notice, this list of conditions and the following disclaimer in the
  10. ** documentation and/or other materials provided with the distribution.
  11. ** 3. The name of the author may not be used to endorse or promote products
  12. ** derived from this software without specific prior written permission.
  13. **
  14. ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. ** SUCH DAMAGE. */
  25. /* COMPILATION NOTE:
  26. Note that the Platform SDK headers and
  27. link libraries for Windows XP SP2 or newer are required to compile
  28. xmlrpc-c for this module. If you are not using this server, it is
  29. safe to exclude the xmlrpc_server_w32httpsys.c file from the xmlrpc
  30. project and these dependencies will not be required. You can get the
  31. latest platform SDK at
  32. http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
  33. Be sure after installation to choose the program to "register the PSDK
  34. directories with Visual Studio" so the newer headers are found.
  35. */
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include <xmlrpc-c/base.h>
  39. #include <xmlrpc-c/server.h>
  40. #include <xmlrpc-c/server_w32httpsys.h>
  41. /* SECURITY NOTE: Using HTTP Authorization
  42. The current implementation of HTTP Authorization in the win32httpsys
  43. server only uses basic Authorization. This means the userid and password
  44. is sent in clear-text over the network (Technically, it is Base64 encoded,
  45. but this is essentially clear text). This method is not secure, as it
  46. can be captured and decoded. The use of HTTP Basic Authorization with SSL
  47. is considered much more secure. See the note below for configuring SSL
  48. support.
  49. */
  50. /*
  51. HOWTO: Configure SSL for the XMLRPC-C Server
  52. To use SSL you need an SSL certificate. For testing purposes,
  53. it is possible to create a self-signed SSL certificate. To do so,
  54. you must download the IIS 6.0 Resource Kit tools. The current
  55. URL to get the download link is http://support.microsoft.com/kb/840671
  56. We will be using the SelfSSL version 1.0 from this toolkit for
  57. this example. The other tool you will need is httpcfg.exe, which
  58. can be compiled from the sources in the Windows XP SP2 (or newer) Platform SDK,
  59. or downloaded as part of the Windows XP SP2 Support Tools at the following URL:
  60. http://www.microsoft.com/downloads/details.aspx?FamilyID=49ae8576-9bb9-4126-9761-ba8011fabf38&displaylang=en
  61. The last assumption is that this procedure is being done on the machine that is
  62. hosting the XMLRPC-C server application.
  63. 1) Make sure that IIS is installed, and you are running at least Windows XP SP2
  64. or Windows Server 2003. WARNING: This process will replace any existing IIS SSL
  65. certificates currently installed.
  66. 2) In a command prompt, navigate to the directory of the
  67. IIS Support Tools where the selfssl program exists (usually
  68. C:\Program Files\IIS Resources\SelfSSL). Assuming (as we are for this example)
  69. that we are going to run on port 8443, use the following command line (see the
  70. documentation for all the flags):
  71. selfssl /T /V:365 /P:8443
  72. 3) In the Control Panel, Administrative tools, run the Internet Information Services
  73. program. Drill down to the Default Web Site. Right-click it and choose Properties.
  74. On the "Web Site" tab, you will notice that the SSL port is now set to 8443. Change
  75. it back to 443. On the Directory Security tab, pick "View Certificate". In the
  76. "Details" tab, select the "Thumbprint" line. The edit box below the listbox will
  77. display a series of hex numbers. Copy these to the clipboard and paste into notepad.
  78. OK yourself out of the IIS program.
  79. 4) Remove all the spaces in the hex string so you are left with a string with no spaces.
  80. This is your SSL Thumbprint hash which you will need in the next step.
  81. 5) At your command prompt, navigate to the support tools directory (or the location
  82. where you built httpcfg.exe) - usually C:\Program Files\Support Tools. Run the following
  83. command line, replacing both the brackets and text with your thumbprint hash from step 4 above:
  84. httpcfg.exe set ssl -i 0.0.0.0:8443 -h <replace with thumbprint hash> -g "{2bb50d9c-7f6a-4d6f-873d-5aee7fb43290}" -c "MY" -t "" -n ""
  85. 6) You can check the setup by performing a "httpcfg.exe query ssl" if you wish.
  86. 7) Modify the example server code below to use SSL. Set the xmlrpc_server_httpsys_parms.useSSL
  87. to '1' and the xmlrpc_server_httpsys_parms.portNum to be '8443'. You can test the server by using
  88. IE to browse to the URL https://127.0.0.1:8443/rpc2. An error 405 (Resource not allowed) is the
  89. expected result if everything is working properly.
  90. NOTE: Testing clients with a 'test' or not real SSL certificate involves changing some of the default
  91. code in the client samples, as by default the transports will fail if there are any issues with the
  92. certificate. The WinInet transport as of 1.2 has a transport-specific setting to allow
  93. invalid SSL certificates. See the libxmlrpc_client.html documentation for more details.
  94. NOTE: Failure to follow all the steps listed above correctly will result in no application
  95. errors, event log messages, or HTTP.SYS log messages indicating failure or the cause. If
  96. anyone can provide information on debugging SSL certificate issues in HTTP.SYS, please
  97. submit to us!
  98. */
  99. static xmlrpc_value *
  100. sample_add(xmlrpc_env * const env,
  101. xmlrpc_value * const param_array,
  102. void * const user_data ) {
  103. xmlrpc_int32 x, y, z;
  104. /* Parse our argument array. */
  105. xmlrpc_decompose_value(env, param_array, "(ii)", &x, &y);
  106. if (env->fault_occurred)
  107. return NULL;
  108. /* Add our two numbers. */
  109. z = x + y;
  110. /* Return our result. */
  111. return xmlrpc_build_value(env, "i", z);
  112. }
  113. static void handleAuthorization(
  114. xmlrpc_env * envP,
  115. char * userid,
  116. char * password)
  117. {
  118. if (strcmp(userid,"jrandom")==0 && strcmp(password,"secret")==0)
  119. return;
  120. xmlrpc_env_set_fault( envP, XMLRPC_REQUEST_REFUSED_ERROR,
  121. "Username and/or password do not match.");
  122. }
  123. int __cdecl wmain( int argc, wchar_t * argv[])
  124. {
  125. xmlrpc_server_httpsys_parms serverparm;
  126. xmlrpc_registry * registryP;
  127. xmlrpc_env env;
  128. xmlrpc_env_init(&env);
  129. registryP = xmlrpc_registry_new(&env);
  130. xmlrpc_registry_add_method(
  131. &env, registryP, NULL, "sample.add", &sample_add, NULL);
  132. wprintf(L"Starting XML-RPC server...\n");
  133. //Sets the port number we are listening on
  134. serverparm.portNum=8080;
  135. //if this is set, we will use the authorization function
  136. //serverparm.authfn=NULL;
  137. serverparm.authfn=&handleAuthorization;
  138. //set the logging level and log file
  139. serverparm.logLevel=2;
  140. serverparm.logFile="C:\\httpsysserverlog.txt";
  141. //set the use of SSL
  142. serverparm.useSSL=0;
  143. serverparm.registryP = registryP;
  144. xmlrpc_server_httpsys(&env, &serverparm, XMLRPC_HSSIZE(authfn));
  145. wprintf(L"Stopping XML-RPC server...\n");
  146. xmlrpc_registry_free(registryP);
  147. xmlrpc_env_clean(&env);
  148. return 0;
  149. }