OSDN Git Service

Re-organize console out and logging out
[openpts/openpts.git] / src / ctx.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/ctx.c
26  * \brief PTS context
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-04-01
29  * cleanup 2011-07-06 SM
30  *
31  * OpenPTS main context
32  *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39
40 #include <openssl/sha.h>
41
42 #include <openpts.h>
43
44 /**
45  * New OpenPTS context (New)
46  */
47 OPENPTS_CONTEXT  * newPtsContext(OPENPTS_CONFIG *conf) {
48     OPENPTS_CONTEXT *ctx = NULL;
49
50     DEBUG_CAL("newPtsContext - start\n");
51
52     ctx = (OPENPTS_CONTEXT *) xmalloc(sizeof(OPENPTS_CONTEXT));
53     if (ctx == NULL) {
54         return NULL;
55     }
56     memset(ctx, 0, sizeof(OPENPTS_CONTEXT));
57
58     /* config - use given config */
59     ctx->conf = conf;
60
61     /* TPM emu - reset */
62     resetTpm(&ctx->tpm, ctx->drtm);
63
64     /* IF-M nonce */
65     ctx->nonce = newNonceContext();
66     if (ctx->nonce == NULL) {
67         goto error;
68     }
69
70     DEBUG_CAL("newPtsContext - done\n");
71
72     return ctx;
73
74   error:
75     xfree(ctx);
76     return NULL;
77 }
78
79
80 /**
81  * free OpenPTS context, but keep conf (shared) 
82  * 
83  * TODO(munetoh) check memory leak
84  */
85 int freePtsContext(OPENPTS_CONTEXT *ctx) {
86     int i;
87     DEBUG_CAL("freePtsContext - start\n");
88
89     if (ctx == NULL) {
90         DEBUG("freePtsContext - NULL\n");
91         return -1;
92     }
93
94     /* TPM emu - reset */
95     // just free with CTX
96
97     /* PCRs - free, malloc at ifm.c */
98     if (ctx->pcrs != NULL) {
99         if (ctx->pcrs->pcr_select_byte != NULL) {
100             xfree(ctx->pcrs->pcr_select_byte);
101         }
102         xfree(ctx->pcrs);
103     }
104
105     /* Quote - free, malloc at ifm.c, ir.c */
106     if (ctx->validation_data != NULL) {
107         if (ctx->validation_data->rgbExternalData != NULL) {
108             xfree(ctx->validation_data->rgbExternalData);
109         }
110         if (ctx->validation_data->rgbData != NULL) {
111             xfree(ctx->validation_data->rgbData);
112         }
113         if (ctx->validation_data->rgbValidationData != NULL) {
114             xfree(ctx->validation_data->rgbValidationData);
115         }
116         xfree(ctx->validation_data);
117     }
118
119     /* UUIDs */
120     if (ctx->uuid != NULL) {
121         xfree(ctx->uuid);
122     }
123     if (ctx->str_uuid != NULL) {
124         xfree(ctx->str_uuid);
125     }
126
127     /* IML - reset & free */
128     if (ctx->ss_table != NULL) {
129         freeSnapshotTable(ctx->ss_table);
130     }
131
132     /* Properties - free */
133     freePropertyChain(ctx->prop_start);
134
135     /* Policy - free */
136     if (ctx->policy_start != NULL) {
137         freePolicyChain(ctx->policy_start);
138     }
139
140     /* Reason - free */
141     if (ctx->reason_start != NULL) {
142         freeReasonChain(ctx->reason_start);
143     }
144
145     /* RM - free malloc at rm.c  */
146     if (ctx->rm_ctx != NULL) {
147         freeRmContext(ctx->rm_ctx);
148     }
149
150     /* IR - free, malloc at ir.c */
151     if (ctx->ir_ctx != NULL) {
152         freeIrContext(ctx->ir_ctx);
153     }
154
155     /* Runtime Validation - free */
156
157     /* IF-M - free */
158     if (ctx->read_msg != NULL) {
159         xfree(ctx->read_msg);
160     }
161
162     if (ctx->nonce != NULL) {
163         freeNonceContext(ctx->nonce);
164     }
165
166     if (ctx->target_conf_filename != NULL) {
167         xfree(ctx->target_conf_filename);
168     }
169
170     for (i = 0; i < MAX_RM_NUM; i++) {
171         if (ctx->compIDs[i].SimpleName != NULL) xfree(ctx->compIDs[i].SimpleName);
172         if (ctx->compIDs[i].ModelName != NULL) xfree(ctx->compIDs[i].ModelName);
173         if (ctx->compIDs[i].ModelNumber != NULL) xfree(ctx->compIDs[i].ModelNumber);
174         if (ctx->compIDs[i].ModelSerialNumber != NULL) xfree(ctx->compIDs[i].ModelSerialNumber);
175         if (ctx->compIDs[i].ModelSystemClass != NULL) xfree(ctx->compIDs[i].ModelSystemClass);
176         if (ctx->compIDs[i].VersionMajor != NULL) xfree(ctx->compIDs[i].VersionMajor);
177         if (ctx->compIDs[i].VersionMinor != NULL) xfree(ctx->compIDs[i].VersionMinor);
178         if (ctx->compIDs[i].VersionBuild != NULL) xfree(ctx->compIDs[i].VersionBuild);
179         if (ctx->compIDs[i].VersionString != NULL) xfree(ctx->compIDs[i].VersionString);
180         if (ctx->compIDs[i].MfgDate != NULL) xfree(ctx->compIDs[i].MfgDate);
181         if (ctx->compIDs[i].PatchLevel != NULL) xfree(ctx->compIDs[i].PatchLevel);
182         if (ctx->compIDs[i].DiscretePatches != NULL) xfree(ctx->compIDs[i].DiscretePatches);
183         if (ctx->compIDs[i].VendorID_Name != NULL) xfree(ctx->compIDs[i].VendorID_Name);
184         if (ctx->compIDs[i].VendorID_Value != NULL) xfree(ctx->compIDs[i].VendorID_Value);
185     }
186
187     /* free */
188     xfree(ctx);
189
190     DEBUG_CAL("freePtsContext - done\n");
191
192     return PTS_SUCCESS;
193 }
194
195
196 /**
197  * get Hash Alg string by ID
198  *
199  * TODO table?
200  */
201 char * getAlgString(int type) {
202     if (type == ALGTYPE_SHA1) {
203         return "sha1";
204     } else if (type == ALGTYPE_MD5) {
205         return "md5";
206     } else {
207         LOG(LOG_ERR, "unknown type %d\n", type);
208         return NULL;
209     }
210 }
211
212 /**
213  * properties
214  *  rm.model.0.pcr.1=bios_pcr1.uml
215  *  rm.model.0.pcr.4=grub_pcr4.uml
216  *
217  * snapshots
218  *   level 0 = Platform(BIOS)
219  *   level 1 = Runtime(IPL,OS,IMA)
220  *   level 2 = apps (TBD)
221  *
222  * Return
223  *    0 OK
224  *   -1 ERROR
225  *    PTS_SUCCESS
226  *    PTS_OS_ERROR
227  *    PTS_INTERNAL_ERROR
228  *
229  */
230 int readFsmFromPropFile(OPENPTS_CONTEXT *ctx, char * filename) {
231     int rc = PTS_SUCCESS;
232     OPENPTS_CONFIG *conf;
233     FILE *fp;
234
235     char buf[FSM_BUF_SIZE];
236     char buf2[FSM_BUF_SIZE];
237     char *eqp = NULL;
238     int pcr_index;
239     int level;
240     char *model_filename = NULL;
241     int len;
242
243     OPENPTS_FSM_CONTEXT *fsm = NULL;
244     OPENPTS_SNAPSHOT *ss = NULL;
245
246     conf = ctx->conf;
247
248     /* new snapshot table */
249     if (ctx->ss_table == NULL) {
250         ctx->ss_table = newSnapshotTable();
251     }
252
253
254     /* Open prop file */
255     if ((fp = fopen(filename, "r")) == NULL) {
256         OUTPUT(NLS(MS_OPENPTS, OPENPTS_CONFIG_MISSING, "Cannot open config file '%s'\n"), filename);
257         return PTS_OS_ERROR;
258     }
259
260     /* parse */
261     while (fgets(buf, FSM_BUF_SIZE, fp) != NULL) {  // read line
262         len = strlen(buf);
263
264         /* check for line length */
265         if (len == FSM_BUF_SIZE) {
266             LOG(LOG_ERR, "Line too long in %s\n", filename);
267             OUTPUT(NLS(MS_OPENPTS, OPENPTS_CONFIG_BAD_CONFIG_FILE, "Bad configuration file\n"));
268             rc = PTS_FATAL;
269             goto error;
270         }
271
272         /* ignore comment, null line */
273         if (buf[0] == '#') {
274             // comment
275         } else if ((eqp = strstr(buf, "=")) != NULL) {
276             /* this is property line */
277
278             /* remove CR */
279             if (buf[len-1] == '\n') buf[len-1] = '\0';
280
281             model_filename = NULL;
282
283 #if 1
284             // Using config file <= version 0.2.3
285             if (strstr(buf, "platform.model.") != NULL) {
286                 LOG(LOG_ERR, "ptsc.conf has old format <=v0.2.3 %s\n", filename);
287                 LOG(LOG_ERR, "change platform.model to rm.model.0\n");
288                 OUTPUT(NLS(MS_OPENPTS, OPENPTS_COLLECTOR_BAD_CONFIG_FILE, "Bad configuration file\n"));
289                 rc = PTS_FATAL;
290                 goto error;
291             }
292
293             if (strstr(buf, "runtime.model.") != NULL) {
294                 LOG(LOG_ERR, "ptsc.conf has old format <=v0.2.3 %s\n", filename);
295                 LOG(LOG_ERR, "change runtime.model to rm.model.1\n");
296                 OUTPUT(NLS(MS_OPENPTS, OPENPTS_COLLECTOR_BAD_CONFIG_FILE, "Bad configuration file\n"));
297                 rc = PTS_FATAL;
298                 goto error;
299             }
300 #endif
301
302             //           1111111
303             // 01234567890123456
304             // rm.model.0.pcr.7
305             if (!strncmp(buf, "rm.model.", 9)) {
306                 level = (int) strtol(&buf[9], NULL, 10);
307                 pcr_index = (int) strtol(&buf[15], NULL, 10);
308                 model_filename = eqp + 1;
309
310                 setModelFile(conf, pcr_index, level, model_filename);
311
312                 /* new FSM */
313                 fsm = newFsmContext();
314                 fsm->level = level;
315                 fsm->pcr_index = pcr_index;
316
317                 /* read Model */
318                 snprintf(
319                     buf2, sizeof(buf2),
320                     "%s/%s",
321                     conf->model_dir, model_filename);
322                 rc = readUmlModel(fsm, buf2);
323                 // TODO(munetoh) cehck rc
324                 if (rc != PTS_SUCCESS) {
325                     LOG(LOG_ERR, "addFsmByPropFile -  [%s] / [%s] -> [%s] fail rc=%d, pwd = %s\n",
326                         conf->model_dir, model_filename, buf2, rc,
327                         getenv("PWD"));
328                     goto error;  // return -1;
329                 }
330
331                 /* setup the NEW snapshots, BIOS, GRUB */
332                 ss = getNewSnapshotFromTable(ctx->ss_table, pcr_index, level);
333                 if (ss == NULL) {
334                     LOG(LOG_ERR, "FSM has been assigned at lvl=%d pcr=%d  %s. check the config file\n",
335                         level, pcr_index, buf);
336                     rc = PTS_FATAL;
337                     goto error;
338                 }
339
340                 ss->fsm_behavior = fsm;
341
342                 // TODO set by getNewSnapshotFromTable
343                 // s s->level = level;
344                 // ss->pcrIndex = pcr_index;
345
346                 // 2011-02-07 SM added
347                 if (ctx->pcrs != NULL && OPENPTS_PCR_INDEX != pcr_index) {
348                     ctx->pcrs->pcr_select[pcr_index] = 1;
349                 }
350
351                 DEBUG_FSM("platform(level%d) pcr[%d] [%s] ss=%p\n",
352                     level,
353                     pcr_index,
354                     conf->model_filename[level][pcr_index],
355                     ss);
356             }
357         } else {
358             /* accept only blank lines */
359             char *ptr;
360
361             ptr = buf;
362             while (*ptr != '\0') {
363                 if (!isspace(*ptr)) {
364                     LOG(LOG_ERR, "Syntax error in %s\n", filename);
365                     OUTPUT(NLS(MS_OPENPTS, OPENPTS_COLLECTOR_BAD_CONFIG_FILE, "Bad configuration file\n"));
366                     rc =  PTS_FATAL;
367                     goto error;
368                 }
369                 ptr++;
370             }
371         }
372     }
373
374   error:
375     fclose(fp);
376
377     return rc;
378 }
379