OSDN Git Service

cleanup
[openpts/openpts.git] / src / reason.c
1 /*
2  * This file is part of the OpenPTS project.
3  *
4  * The Initial Developer of the Original Code is International
5  * Business Machines Corporation. Portions created by IBM
6  * Corporation are Copyright (C) 2010 International Business
7  * Machines Corporation. All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the Common Public License as published by
11  * IBM Corporation; either version 1 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * Common Public License for more details.
18  *
19  * You should have received a copy of the Common Public License
20  * along with this program; if not, a copy can be viewed at
21  * http://www.opensource.org/licenses/cpl1.0.php.
22  */
23
24 /**
25  * \file src/reason.c
26  * \brief properties
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-11-26
29  * cleanup 2012-01-05 SM
30  *
31  * Reason (Remidiation) of validation fail
32  *
33  * Fail at FSM check
34  * Fail at Policy check
35  *
36  */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>  /* va_ */
42
43 #include <openpts.h>
44
45 /**
46  * Free Reason
47  */
48 void freeReason(OPENPTS_REASON *reason) {
49     /* check */
50     if (reason == NULL) {
51         LOG(LOG_ERR, "null input");
52         return;
53     }
54
55     /* free */
56     xfree(reason->message);
57     xfree(reason);
58
59     return;
60 }
61
62 /**
63  * Free Reason Chain
64  */
65 int freeReasonChain(OPENPTS_REASON *reason) {
66     /* check */
67     if (reason == NULL) {
68         LOG(LOG_ERR, "null input");
69         return PTS_FATAL;
70     }
71
72     /* chain */
73     if (reason->next != NULL) {
74         freeReasonChain(reason->next);
75     }
76
77     freeReason(reason);
78
79     return PTS_SUCCESS;
80 }
81
82 /**
83  * add reason
84  */
85 int addReason_old(OPENPTS_CONTEXT *ctx, int pcr, char *message) {
86     OPENPTS_REASON *start;
87     OPENPTS_REASON *end;
88     OPENPTS_REASON *reason;
89     int len;
90
91     /* check */
92     if (ctx == NULL) {
93         LOG(LOG_ERR, "null input");
94         return PTS_FATAL;
95     }
96
97     len   = strlen(message);
98     start = ctx->reason_start;
99     end   = ctx->reason_end;
100
101     reason = (OPENPTS_REASON *) xmalloc(sizeof(OPENPTS_REASON));
102     if (reason == NULL) {
103         LOG(LOG_ERR, "no memory");
104         return PTS_FATAL;
105     }
106     memset(reason, 0, sizeof(OPENPTS_REASON));
107
108     if (start == NULL) {
109         /* 1st prop */
110         /* update the link */
111         ctx->reason_start = reason;
112         ctx->reason_end = reason;
113         reason->next = NULL;
114         ctx->reason_count = 0;
115     } else {
116         /* update the link */
117         end->next     = reason;
118         ctx->reason_end = reason;
119         reason->next = NULL;
120     }
121     reason->pcr = pcr;
122     reason->message = xmalloc(len +1);
123     if (reason->message == NULL) {
124         LOG(LOG_ERR, "no memory");
125         xfree(reason);
126         return PTS_FATAL;
127     }
128     memcpy(reason->message, message, len);
129     reason->message[len] = 0;
130     ctx->reason_count++;
131
132     return PTS_SUCCESS;
133 }
134
135 /**
136  * addReason with format
137  */
138 #define MAX_REASON_SIZE 2048
139 int addReason(OPENPTS_CONTEXT *ctx, int pcr, const char *format, ...) {
140     int rc;
141     char buf[MAX_REASON_SIZE +1];  // TODO size
142     va_list list;
143
144     /* check */
145     if (ctx == NULL) {
146         LOG(LOG_ERR, "null input");
147         return PTS_FATAL;
148     }
149
150     va_start(list, format);
151     vsnprintf(buf, MAX_REASON_SIZE, format, list);
152
153     rc = addReason_old(ctx, pcr, (char *)buf);
154
155     return rc;
156 }
157
158 /**
159  * PCR Usage HINT for each platform.
160  * TODO supply them by Conf.
161  */
162 #ifdef AIX_TARGET
163 char *reason_pcr_hints[] = {
164     "IBM Partition Firmware Images",
165     "Basic Partition Configuration (e.g. CPUs, memory)",
166     "Third-party Adapter Firmware",
167     "Partition Device Tree",
168     "OS Boot Image",
169     "OS Boot Info (e.g. boot device, or firmware prompt)",
170     NULL, /* PCR6 Unused */
171     NULL, /* PCR7 Unused */
172     NULL, /* PCR8 Unused */
173     NULL, /* PCR9 Unused */
174     "Trusted Execution Database"
175 };
176 #else  // TPM v1.2, PC Linux, TODO add other type of platform?
177 char *reason_pcr_hints[] = {
178     "CRTM, BIOS and Platform Extensions",
179     "Platform Configuration",
180     "Option ROM Code",
181     "Option ROM Configuration and Data",
182     "IPL Code (usually the MBR)",
183     "IPL Code Configuration and Data (for use by the IPL code)",
184     "State Transition and Wake Events",
185     "Host Platform Manufacturer Control",   // v1.1"Reserved for future usage. Do not use.",
186     "OS Kernels (GRUB-IMA)",
187     NULL, /* PCR9 Unused */
188     "Applications (LINUX-IMA)", /* PCR10 */
189     "OpenPTS", /* PCR11 */
190     NULL, /* PCR12 Unused */
191     NULL, /* PCR13 Unused */
192     NULL, /* PCR14 Unused */
193     NULL, /* PCR15 Unused */
194     "Debug", /* PCR16 */
195     "Associated with the D-CRTM (Locality 4)", /* PCR17 */
196     "Host Platform defined (locality 3)", /* PCR18 */
197     "Trusted Operating System (locality 2)", /* PCR19 */
198     "Used by Trusted Operating System (locality 1)", /* PCR20 */
199     "Used by Trusted Operating System", /* PCR21 */
200     "Used by Trusted Operating System", /* PCR22 */
201     "Application Support", /* PCR23 */
202 };
203 #endif
204
205 /**
206  * print Reason
207  *
208  */
209 void printReason(OPENPTS_CONTEXT *ctx, int print_pcr_hints) {
210     OPENPTS_REASON *reason;
211     unsigned int i = 0, pcrmask = 0;
212
213     /* check */
214     if (ctx == NULL) {
215         LOG(LOG_ERR, "null input");
216         return;
217     }
218     reason = ctx->reason_start;
219
220     while (reason != NULL) {
221     if (reason->pcr >= 0)
222         pcrmask |= 1 << reason->pcr;
223         OUTPUT("%5d %s\n", i, reason->message);
224         reason = reason->next;
225         i++;
226     }
227     if (print_pcr_hints) {
228         for (i = 0; i < sizeof(reason_pcr_hints) / sizeof(char *); i++) {
229             if (!(pcrmask & (1 << i)) || reason_pcr_hints[i] == NULL) continue;
230             OUTPUT("PCR%02d corresponds to: %s\n", i, reason_pcr_hints[i]);
231         }
232     }
233 }