1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include "KMSAgentSoapUtilities.h"
27 #include "KMSAgentStringUtilities.h"
28 #include "ApplianceParameters.h"
29 
30 #include "stdsoap2.h"
31 
32 /**
33  * Get the peer's network address
34  */
GetPeerNetworkAddress(char * const o_psPeerNetworkAddress,struct soap * i_pSoap)35 void GetPeerNetworkAddress (char* const o_psPeerNetworkAddress,
36                             struct soap* i_pSoap)
37 {
38     FATAL_ASSERT(o_psPeerNetworkAddress);
39 
40     if (strlen(i_pSoap->host) > 0)
41     {
42         // IPv4 addresses can appear as ::ffff:a.b.c.d, strip off the prefix
43         if (strncmp(i_pSoap->host, "::ffff:", 7) == 0)
44         {
45             strncpy(o_psPeerNetworkAddress, &i_pSoap->host[7], g_iMAX_PEER_NETWORK_ADDRESS_LENGTH);
46             o_psPeerNetworkAddress[g_iMAX_PEER_NETWORK_ADDRESS_LENGTH-1] = '\0';
47             return;
48         }
49         strncpy(o_psPeerNetworkAddress, i_pSoap->host, g_iMAX_PEER_NETWORK_ADDRESS_LENGTH);
50         o_psPeerNetworkAddress[g_iMAX_PEER_NETWORK_ADDRESS_LENGTH-1] = '\0';
51         return;
52     }
53 
54     // i_pSoap->ip == 0 could not represent a valid
55     // Peer Network Address (IPv4), check i_pSoap->session_host
56     // for an IPv6 address
57     if (i_pSoap->ip == 0)
58     {
59 #ifndef METAWARE
60         if (strlen(i_pSoap->session_host) > 0)
61         {
62             // IPv4 addresses can appear as ::ffff:a.b.c.d, strip off the
63             // prefix
64 
65             if (strncmp(i_pSoap->session_host, "::ffff:", 7) == 0)
66             {
67                 strncpy(o_psPeerNetworkAddress, &i_pSoap->session_host[7], g_iMAX_PEER_NETWORK_ADDRESS_LENGTH);
68                 o_psPeerNetworkAddress[g_iMAX_PEER_NETWORK_ADDRESS_LENGTH-1] = '\0';
69                 return;
70             }
71 
72             strncpy(o_psPeerNetworkAddress, i_pSoap->session_host, g_iMAX_PEER_NETWORK_ADDRESS_LENGTH);
73             o_psPeerNetworkAddress[g_iMAX_PEER_NETWORK_ADDRESS_LENGTH-1] = '\0';
74             return;
75         }
76 #endif
77         strcpy(o_psPeerNetworkAddress, "");
78         return;
79     }
80 
81     K_snprintf(o_psPeerNetworkAddress,
82             g_iMAX_PEER_NETWORK_ADDRESS_LENGTH,
83             "%d.%d.%d.%d",
84             (int) (i_pSoap->ip >> 24)&0xFF,
85             (int) (i_pSoap->ip >> 16)&0xFF,
86             (int) (i_pSoap->ip >> 8)&0xFF,
87             (int) (i_pSoap->ip)&0xFF);
88 
89     return;
90 }
91 
92 /**
93  * Get the soap fault code and print it
94  */
GetSoapFault(char * o_psFaultMessage,struct soap * i_pstSoap)95 void GetSoapFault(char* o_psFaultMessage,
96                   struct soap *i_pstSoap)
97 {
98    FATAL_ASSERT( i_pstSoap );
99 
100    strncpy (o_psFaultMessage, " SoapFaultCode=",g_iMAX_SOAP_FAULT_MESSAGE_LENGTH);
101    o_psFaultMessage[g_iMAX_SOAP_FAULT_MESSAGE_LENGTH-1] = '\0';
102    strncat (o_psFaultMessage, GET_SOAP_FAULTCODE(i_pstSoap),
103            g_iMAX_SOAP_FAULT_MESSAGE_LENGTH-strlen(o_psFaultMessage));
104    strncat (o_psFaultMessage, " SoapFaultString=",
105            g_iMAX_SOAP_FAULT_MESSAGE_LENGTH-strlen(o_psFaultMessage));
106    strncat (o_psFaultMessage, GET_SOAP_FAULTSTRING(i_pstSoap),
107            g_iMAX_SOAP_FAULT_MESSAGE_LENGTH-strlen(o_psFaultMessage));
108    strncat (o_psFaultMessage, " SoapFaultDetail=",
109            g_iMAX_SOAP_FAULT_MESSAGE_LENGTH-strlen(o_psFaultMessage));
110    strncat (o_psFaultMessage, GET_SOAP_FAULTDETAIL(i_pstSoap),
111            g_iMAX_SOAP_FAULT_MESSAGE_LENGTH-strlen(o_psFaultMessage));
112 
113    return;
114 }
115 
PutBinaryIntoSoapBinary(struct soap * i_pSoap,const unsigned char * i_pBinary,int i_iBinarySize,unsigned char * & o_pSoapBinary,int & o_iSoapBinarySize)116 bool PutBinaryIntoSoapBinary(
117         struct soap* i_pSoap,
118         const unsigned char* i_pBinary,
119         int i_iBinarySize,
120         unsigned char*& o_pSoapBinary,
121         int& o_iSoapBinarySize )
122 {
123     FATAL_ASSERT( i_pSoap );
124 
125     o_pSoapBinary = 0;
126     o_iSoapBinarySize = 0;
127 
128     if ( i_iBinarySize > 0 )
129     {
130         o_pSoapBinary =
131                     (unsigned char*)soap_malloc(
132                         i_pSoap,
133                         sizeof(unsigned char) * i_iBinarySize);
134 
135         if ( !o_pSoapBinary )
136         {
137             // No log for out of memory condition
138 
139             return false;
140         }
141 
142         o_iSoapBinarySize = i_iBinarySize;
143 
144         memcpy(o_pSoapBinary, i_pBinary, i_iBinarySize);
145     }
146 
147     return true;
148 }
149