xmlrpc_authcookie.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. static char env_buffer[1024];
  37. const char* block;
  38. /* Check asserts. */
  39. XMLRPC_ASSERT_ENV_OK(env);
  40. XMLRPC_ASSERT_PTR_OK(username);
  41. XMLRPC_ASSERT_PTR_OK(password);
  42. /* Clear out memory. */
  43. unencoded = (char *) malloc ( sizeof ( char * ) );
  44. /* Create unencoded string/hash. */
  45. sprintf(unencoded, "%s:%s", username, password);
  46. /* Create encoded string. */
  47. token = xmlrpc_base64_encode_without_newlines(env, (unsigned char*)unencoded,
  48. strlen(unencoded));
  49. XMLRPC_FAIL_IF_FAULT(env);
  50. /* Set HTTP_COOKIE_AUTH to the character representation of the
  51. ** encoded string. */
  52. block = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, token);
  53. sprintf(env_buffer, "HTTP_COOKIE_AUTH=%s", block);
  54. putenv(env_buffer);
  55. cleanup:
  56. if (token) xmlrpc_mem_block_free(token);
  57. }
  58. char *xmlrpc_authcookie ( void ) {
  59. return getenv("HTTP_COOKIE_AUTH");
  60. }