ia64/xen-unstable

view tools/vtpm_manager/migration/vtpm_migratord_handler.c @ 17838:e5c9c8e6e726

tools: replace sprintf with snprintf where applicable

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 12 15:41:15 2008 +0100 (2008-06-12)
parents 5014fd2b5c5a
children
line source
1 // ===================================================================
2 //
3 // Copyright (c) 2005, Intel Corp.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of Intel Corporation nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 // OF THE POSSIBILITY OF SUCH DAMAGE.
32 // ===================================================================
34 #include <stdlib.h>
35 #include <string.h>
37 #include "tcg.h"
38 #include "bsg.h"
39 #include "log.h"
40 #include "vtpm_migrator.h"
41 #include "vtpm_manager.h"
43 #define VTPM_SH_CMD_HDR "bash -c \"cd /etc/xen/scripts; source /etc/xen/scripts/vtpm-common.sh;"
44 #define VTPM_SH_CMD_FTR "\""
45 #define VTPM_SH_GETINST "vtpmdb_get_free_instancenum"
46 #define VTPM_SH_ADD "vtpm_add_and_activate"
47 #define VTPM_SH_RESUME "vtpm_resume"
49 // This must be updated to the longest command name. Currently GETINST
50 #define VTPM_SH_CMD_SIZE (strlen(VTPM_SH_CMD_HDR) + strlen(VTPM_SH_CMD_FTR) + 1 + strlen(VTPM_SH_GETINST) + 2)
52 void handle_vtpm_mig_step2(buffer_t *in_param_buf, buffer_t *result_buf)
53 {
54 TPM_TAG tag = VTPM_TAG_RSP;
55 buffer_t out_param_buf= NULL_BUF, mig_key_buf=NULL_BUF;
56 TPM_RESULT status=TPM_SUCCESS, cmd_status;
57 UINT32 out_param_size;
59 if ( (!in_param_buf) || (!result_buf) ) {
60 status = TPM_BAD_PARAMETER;
61 goto abort_egress;
62 }
64 // ================= Call manager and get mig key ===============
65 TPMTRYRETURN( vtpm_manager_open() );
66 TPMTRYRETURN( vtpm_manager_command(VTPM_ORD_GET_MIG_KEY,
67 &out_param_buf, // Empty
68 &cmd_status,
69 &mig_key_buf) );
71 vtpm_manager_close();
73 TPMTRYRETURN(cmd_status);
75 // ==================== return the mig key =====================
76 out_param_size = VTPM_COMMAND_HEADER_SIZE + buffer_len(&mig_key_buf);
78 TPMTRYRETURN( buffer_init(result_buf,
79 out_param_size,
80 NULL) );
82 BSG_PackList( result_buf->bytes, 3,
83 BSG_TPM_TAG, &tag,
84 BSG_TYPE_UINT32, &out_param_size,
85 BSG_TPM_RESULT, &status);
87 memcpy(result_buf->bytes + VTPM_COMMAND_HEADER_SIZE,
88 mig_key_buf.bytes, buffer_len(&mig_key_buf));
90 goto egress;
92 abort_egress:
93 buffer_free(result_buf);
94 build_error_msg(result_buf, status);
96 egress:
97 return;
98 }
100 void handle_vtpm_mig_step3(buffer_t *in_param_buf, buffer_t *result_buf)
101 {
102 TPM_TAG tag = VTPM_TAG_RSP;
103 buffer_t out_param_buf= NULL_BUF, mig_key_buf=NULL_BUF, empty_buf=NULL_BUF;
104 TPM_RESULT status=TPM_SUCCESS, cmd_status;
105 UINT32 out_param_size, instance;
106 char *shell_cmd_str=NULL;
107 size_t shell_cmd_strlen;
108 FILE *shell_f=NULL;
110 if ( (!in_param_buf) || (!result_buf) ) {
111 status = TPM_BAD_PARAMETER;
112 goto abort_egress;
113 }
115 // ================= Read Parameters ===============
116 struct pack_buf_t name_data32, state_data32;
118 BSG_UnpackList(in_param_buf->bytes, 2,
119 BSG_TPM_SIZE32_DATA, &name_data32,
120 BSG_TPM_SIZE32_DATA, &state_data32);
122 // Before using this string, protect us from a non-null term array.
123 if (name_data32.data[name_data32.size -1] != 0x00) {
124 name_data32.data[name_data32.size -1] = 0x00;
125 }
127 // ====== Call hotplug-script and get an instance ======
128 shell_cmd_strlen = VTPM_SH_CMD_SIZE + name_data32.size + 10;
129 shell_cmd_str = (char *) malloc(shell_cmd_strlen); // 10 is just padding for the UINT32
131 snprintf(shell_cmd_str, shell_cmd_strlen,
132 VTPM_SH_CMD_HDR VTPM_SH_GETINST VTPM_SH_CMD_FTR);
134 shell_f = popen(shell_cmd_str, "r");
135 fscanf(shell_f, "%d", &instance);
136 pclose(shell_f);
138 // ====== Call hotplug-script and add instance ======
139 snprintf(shell_cmd_str, shell_cmd_strlen,
140 VTPM_SH_CMD_HDR VTPM_SH_ADD " %s %d" VTPM_SH_CMD_FTR,
141 name_data32.data, instance);
142 system(shell_cmd_str);
144 // ========= Call vtpm_manager and load VTPM =======
145 TPMTRYRETURN( buffer_init( &out_param_buf,
146 2*sizeof(UINT32) + state_data32.size,
147 NULL) );
149 BSG_PackList(out_param_buf.bytes, 2,
150 BSG_TYPE_UINT32, &instance,
151 BSG_TPM_SIZE32_DATA, &state_data32);
153 TPMTRYRETURN( vtpm_manager_open() );
154 TPMTRYRETURN( vtpm_manager_command(VTPM_ORD_MIGRATE_IN,
155 &out_param_buf,
156 &cmd_status,
157 &empty_buf) );
159 vtpm_manager_close();
161 TPMTRYRETURN(cmd_status);
163 // ====== Call hotplug-script and resume instance ======
164 snprintf(shell_cmd_str, shell_cmd_strlen,
165 VTPM_SH_CMD_HDR VTPM_SH_RESUME " %d" VTPM_SH_CMD_FTR, instance);
166 system(shell_cmd_str);
168 goto egress;
169 abort_egress:
170 egress:
171 free(shell_cmd_str);
173 // In this case no params come back, so reuse build_error_msg even for succes.
174 build_error_msg(result_buf, status);
175 return;
176 }