xmlrpc_authcookie.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright (C) 2002 by [email protected]. 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. #include "xmlrpc_config.h"
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "xmlrpc.h"
  30. /* set cookie function */
  31. void xmlrpc_authcookie_set ( xmlrpc_env *env,
  32. const char *username,
  33. const char *password ) {
  34. char *unencoded;
  35. xmlrpc_mem_block *token;
  36. /* Check asserts. */
  37. XMLRPC_ASSERT_ENV_OK(env);
  38. XMLRPC_ASSERT_PTR_OK(username);
  39. XMLRPC_ASSERT_PTR_OK(password);
  40. /* Clear out memory. */
  41. unencoded = (char *) malloc ( sizeof ( char * ) );
  42. token = NULL;
  43. /* Create unencoded string/hash. */
  44. sprintf(unencoded, "%s:%s", username, password);
  45. /* Create encoded string. */
  46. token = xmlrpc_base64_encode_without_newlines(env, (unsigned char*)unencoded,
  47. strlen(unencoded));
  48. XMLRPC_FAIL_IF_FAULT(env);
  49. /* Set HTTP_COOKIE_AUTH to the character representation of the
  50. ** encoded string. */
  51. setenv("HTTP_COOKIE_AUTH", XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, token),
  52. 1);
  53. cleanup:
  54. if (token) xmlrpc_mem_block_free(token);
  55. }
  56. char *xmlrpc_authcookie ( void ) {
  57. return getenv("HTTP_COOKIE_AUTH");
  58. }