OSDN Git Service

Re-organize console out and logging out
[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 2011-01-22 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;  // PTS_SUCCESS;
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     // DEBUG("addReason - [%s]\n", message);
92
93     /* check */
94     if (ctx == NULL) {
95         LOG(LOG_ERR, "null input");
96         return PTS_FATAL;
97     }
98
99     len = strlen(message);
100
101     start = ctx->reason_start;
102     end   = ctx->reason_end;
103
104     reason = (OPENPTS_REASON *) xmalloc(sizeof(OPENPTS_REASON));
105     if (reason == NULL) {
106         LOG(LOG_ERR, "no memory");
107         return PTS_FATAL;
108     }
109     memset(reason, 0, sizeof(OPENPTS_REASON));
110
111     if (start == NULL) {
112         /* 1st prop */
113         /* update the link */
114         ctx->reason_start = reason;
115         ctx->reason_end = reason;
116         reason->next = NULL;
117         ctx->reason_count = 0;
118     } else {
119         /* update the link */
120         end->next     = reason;
121         ctx->reason_end = reason;
122         reason->next = NULL;
123     }
124     reason->pcr = pcr;
125     reason->message = xmalloc(len +1);
126     if (reason->message == NULL) {
127         LOG(LOG_ERR, "no memory");
128         xfree(reason);
129         return PTS_FATAL;
130     }
131     memcpy(reason->message, message, len);
132     reason->message[len] = 0;
133     ctx->reason_count++;
134
135     // DEBUG("addReason - done %d [%s]\n", ctx->reason_count, reason->message);
136
137     return PTS_SUCCESS;
138 }
139
140 /**
141  * addReason with format
142  */
143 #define MAX_REASON_SIZE 2048
144 int addReason(OPENPTS_CONTEXT *ctx, int pcr, const char *format, ...) {
145     char buf[MAX_REASON_SIZE +1];  // TODO size
146     int rc;
147     va_list list;
148     va_start(list, format);
149
150     /* check */
151     if (ctx == NULL) {
152         LOG(LOG_ERR, "null input");
153         return PTS_FATAL;
154     }
155
156     vsnprintf(buf, MAX_REASON_SIZE, format, list);
157
158     rc = addReason_old(ctx, pcr, (char *)buf);
159
160     return rc;
161 }
162
163 /**
164  * PCR Usage HINT for each platform.
165  * TODO supply them by Conf.
166  */
167 #ifdef AIX_TARGET
168 char *reason_pcr_hints[] = {
169     "IBM Partition Firmware Images",
170     "Basic Partition Configuration (e.g. CPUs, memory)",
171     "Third-party Adapter Firmware",
172     "Partition Device Tree",
173     "OS Boot Image",
174     "OS Boot Info (e.g. boot device, or firmware prompt)",
175     NULL, /* PCR6 Unused */
176     NULL, /* PCR7 Unused */
177     NULL, /* PCR8 Unused */
178     NULL, /* PCR9 Unused */
179     "Trusted Execution Database"
180 };
181 #else // TPM v1.2, PC Linux, TODO add other type of platform?
182 char *reason_pcr_hints[] = {
183     "CRTM, BIOS and Platform Extensions",
184     "Platform Configuration",
185     "Option ROM Code",
186     "Option ROM Configuration and Data",
187     "IPL Code (usually the MBR)",
188     "IPL Code Configuration and Data (for use by the IPL code)",
189     "State Transition and Wake Events",
190     "Host Platform Manufacturer Control",   // v1.1"Reserved for future usage. Do not use.", 
191     "OS Kernels (GRUB-IMA)",
192     NULL, /* PCR9 Unused */
193     "Applications (LINUX-IMA)", /* PCR10 */
194     "OpenPTS", /* PCR11 */
195     NULL, /* PCR12 Unused */
196     NULL, /* PCR13 Unused */
197     NULL, /* PCR14 Unused */
198     NULL, /* PCR15 Unused */
199     "Debug", /* PCR16 */
200     "Associated with the D-CRTM (Locality 4)", /* PCR17 */
201     "Host Platform defined (locality 3)", /* PCR18 */
202     "Trusted Operating System (locality 2)", /* PCR19 */
203     "Used by Trusted Operating System (locality 1)", /* PCR20 */
204     "Used by Trusted Operating System", /* PCR21 */
205     "Used by Trusted Operating System", /* PCR22 */
206     "Application Support", /* PCR23 */
207 };
208 #endif
209
210 /**
211  * print Reason
212  *
213  */
214 void printReason(OPENPTS_CONTEXT *ctx, int print_pcr_hints) {
215     OPENPTS_REASON *reason;
216     unsigned int i = 0, pcrmask = 0;
217
218     /* check */
219     if (ctx == NULL) {
220         LOG(LOG_ERR, "null input");
221         return;
222     }
223     reason = ctx->reason_start;
224
225     while (reason != NULL) {
226     if (reason->pcr >= 0)
227         pcrmask |= 1 << reason->pcr;
228         OUTPUT("%5d %s\n", i, reason->message);
229         reason = reason->next;
230         i++;
231     }
232     if (print_pcr_hints) {
233         for (i = 0; i < sizeof(reason_pcr_hints) / sizeof(char *); i++) {
234             if (!(pcrmask & (1 << i)) || reason_pcr_hints[i] == NULL) continue;
235             OUTPUT("PCR%02d corresponds to: %s\n", i, reason_pcr_hints[i]);
236         }
237     }
238 }
239
240
241 // TODO add freeReason()