ENH: make this work for older versions of OSX
[cmake.git] / Utilities / cmxmlrpc / xmlrpc_authcookie.c
blobcee41478be96c1aeacccde4ef485ee9f82771776
1 /* Copyright (C) 2002 by jeff@ourexchange.net. 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. */
26 #include "xmlrpc_config.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "xmlrpc.h"
34 /* set cookie function */
35 void xmlrpc_authcookie_set ( xmlrpc_env *env,
36 const char *username,
37 const char *password ) {
38 char *unencoded;
39 xmlrpc_mem_block *token;
40 static char env_buffer[1024];
41 const char* block;
43 /* Check asserts. */
44 XMLRPC_ASSERT_ENV_OK(env);
45 XMLRPC_ASSERT_PTR_OK(username);
46 XMLRPC_ASSERT_PTR_OK(password);
48 /* Clear out memory. */
49 unencoded = (char *) malloc ( sizeof ( char * ) );
51 /* Create unencoded string/hash. */
52 sprintf(unencoded, "%s:%s", username, password);
54 /* Create encoded string. */
55 token = xmlrpc_base64_encode_without_newlines(env, (unsigned char*)unencoded,
56 strlen(unencoded));
57 XMLRPC_FAIL_IF_FAULT(env);
59 /* Set HTTP_COOKIE_AUTH to the character representation of the
60 ** encoded string. */
61 block = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, token);
62 sprintf(env_buffer, "HTTP_COOKIE_AUTH=%s", block);
63 putenv(env_buffer);
65 cleanup:
66 if (token) xmlrpc_mem_block_free(token);
69 char *xmlrpc_authcookie ( void ) {
70 return getenv("HTTP_COOKIE_AUTH");