OSDN Git Service

cleanup
[openpts/openpts.git] / src / iml2aide.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/iml2aide.c
26  * \brief convert IML to AIDE DB
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-08-24
29  * cleanup 2012-01-05 SM
30  *
31  * create AIDE DB from IML (via securityfs)
32  *
33  *  ./src/iml2aide -c tests/data/Fedora12/ptscd.conf -i /sys/kernel/security/ima/binary_runtime_measurements -o tests/data/Fedora12/aide.db.gz
34  *  zcat tests/data/Fedora12/aide.db.gz | less
35  *
36  * Create AIDE DB from IML (via TSS)
37  *
38  *  time ./src/iml2aide -c tests/data/Fedora12/ptscd.conf -o tests/data/Fedora12/aide.db.gz
39  *  IML          : 5673 events (TSS)
40  *  AIDE DB      : 5520 entries (tests/data/Fedora12/aide.db.gz) 
41  *
42  *  real        0m0.872s
43  *  user        0m0.037s
44  *  sys         0m0.011s
45  *
46  * Create AIDE DB from IML (via TSS) and refer actual AIDE DB and get the fullpath name if existed.
47  *
48  *  time src/iml2aide -c tests/data/Fedora12/ptscd.conf -r /var/lib/aide/aide.db.new.gz -o tests/data/Fedora12/aide.db.gz
49  *  AIDE DB(ref) : 241826 entries (/var/lib/aide/aide.db.new.gz)
50  *  IML          : 5681 events (TSS)
51  *  AIDE DB      : 3986 entries (tests/data/Fedora12/aide.db.gz) 
52  *
53  *  real        1m27.252s  << YES VERY SLOW :-(
54  *  user        1m26.112s
55  *  sys         0m0.167s
56  *
57  *
58  * Create AIDE DB from IML (via TSS) and refer actual AIDE DB and get the fullpath name if existed. also generate ignore list
59  *
60  *  AIDE DB(ref) : 241826 entries (< /var/lib/aide/aide.db.new.gz)
61  *  IML          : 5949 events (< TSS)
62  *  AIDE DB      : 4153 entries (> tests/data/Fedora12/aide.db.gz) 
63  *  Ignore list  : 224 entries (> tests/data/Fedora12/aide.ignore.list) 
64  * 
65  *  real        1m35.009s
66  *  user        1m33.900s
67  *  sys         0m0.148s
68  *
69  *
70  *
71  */
72
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <netdb.h>
77 #include <errno.h>
78
79 #include <sys/types.h>
80 #include <sys/socket.h>
81 #include <netinet/in.h>
82 #include <unistd.h>
83
84 #include <sys/stat.h>
85 #include <fcntl.h>
86
87 #include <openpts.h>
88
89 /**
90  * print FSM status (location)
91  */
92 void printFsmInfo2(OPENPTS_CONTEXT *ctx) {
93     int i;
94     OPENPTS_SNAPSHOT *ss;
95     int level0_num = 0;
96     int level1_num = 0;
97
98     OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_EVENT,
99         "Number of events\n"
100         "PCR Level0 Level1\n"));
101     OUTPUT("--------------------------\n");
102
103     for (i = 0; i < MAX_PCRNUM; i++) {
104         OUTPUT("%2d ", i);
105         ss = getSnapshotFromTable(ctx->ss_table, i, 0);
106         if (ss == NULL) {
107             OUTPUT(" ----- - - ");
108         } else {
109             OUTPUT(" %p ", ss);
110             if (ss->fsm_behavior != NULL) OUTPUT(" O ");
111             else                          OUTPUT(" X ");
112
113             if (ss->fsm_binary   != NULL) OUTPUT(" O ");
114             else                          OUTPUT(" X ");
115
116             /* level 1 */
117             ss = getSnapshotFromTable(ctx->ss_table, i, 1);
118             if (ss != NULL) {
119                 OUTPUT("  ");
120                 OUTPUT(" %p ", ss);
121                 if (ss->fsm_behavior != NULL) OUTPUT(" O ");
122                 else                          OUTPUT(" X ");
123
124                 if (ss->fsm_binary   != NULL) OUTPUT(" O ");
125                 else                          OUTPUT(" X ");
126             }
127         }
128
129         OUTPUT("\n");
130     }
131     OUTPUT("---------------------------\n");
132     OUTPUT("level 0 total = %d\n", level0_num);
133     OUTPUT("level 1 total = %d\n", level1_num);
134     OUTPUT("---------------------------\n");
135 }
136
137 /**
138  * usage
139  */
140 void usage(void) {
141     OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_USAGE,
142         "OpenPTS command\n\n"
143         "Usage: iml2aide [options]\n\n"
144         "Options:\n"
145         "  -c filename           Set config file\n"
146         "  -i filename           Set IMA IML file. default, get IML via TSS\n"
147         "  -r filename           Set AIDE DB file as reference of fullpathname\n"
148         "  -o filename           Set output file (AIDE DB format, gziped)\n"
149         "  -w filename           Set output file (Ignore name list, plain text format)\n"
150         "  -h                    Show this help message\n"
151         "\n"));
152 }
153
154 /**
155  * main
156  */
157 int main(int argc, char *argv[]) {
158     int rc = -1;
159     int ima_type = BINARY_IML_TYPE_IMA;
160     int c;
161     char *ima_filename = NULL;
162     char *aide_filename = NULL;
163     char *config_filename = NULL;
164     char *aideref_filename = NULL;
165     char *ignorelist_filename = NULL;
166     OPENPTS_CONFIG *conf = NULL;
167     OPENPTS_CONTEXT *ctx = NULL;
168
169
170     /* args */
171     while ((c = getopt(argc, argv, "do:i:c:r:w:h")) != EOF) {
172         switch (c) {
173         case 'd':
174             setDebugFlags(DEBUG_FLAG);
175             break;
176         case 'i':
177             ima_filename = optarg;
178             break;
179         case 'o':
180             aide_filename = optarg;
181             break;
182         case 'r':
183             aideref_filename = optarg;
184             break;
185         case 'c':
186             config_filename = optarg;
187             break;
188         case 'w':
189             ignorelist_filename = optarg;
190             break;
191         case 'h':
192             /* fall through */
193         default:
194             usage();
195             return -1;
196         }
197     }
198     argc -= optind;
199     argv += optind;
200
201     /* check */
202     if (aide_filename == NULL) {
203         OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_SET_OUTPUT, "Set output file (AIDE DB file)\n\n"));
204         usage();
205         return -1;
206     }
207     if (config_filename == NULL) {
208         OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_CONFIG, "Set config file\n\n"));
209         usage();
210         return -1;
211     }
212
213
214     /* ctx */
215     conf = newPtsConfig();
216     if (conf == NULL) {
217         LOG(LOG_ERR, "Internal Error\n");
218         return -1;
219     }
220
221     ctx = newPtsContext(conf);
222     if (ctx == NULL) {
223         LOG(LOG_ERR, "Internal Error\n");
224         return -1;
225     }
226
227     /* conf */
228     rc = readPtsConfig(ctx->conf, config_filename);
229
230     /* FSM */
231     rc = readFsmFromPropFile(ctx, config_filename);
232     if (rc != PTS_SUCCESS) {
233         LOG(LOG_ERR, "read FSM failed\n");
234         printFsmInfo2(ctx);
235     }
236
237     /* set dummy prop */
238     setEventProperty(ctx, "linux.kernel.digest", "valid", NULL);
239     setEventProperty(ctx, "linux.initrd.digest", "valid", NULL);
240     setEventProperty(ctx, "linux.kernel.cmdline.ima_tcb", "1", NULL);
241
242
243     /* AIDE reference DB, pre load   */
244     if (aideref_filename != NULL) {
245         ctx->aide_ctx = newAideContext();
246
247         rc = loadAideDatabaseFile(ctx->aide_ctx, aideref_filename);  // ir.c
248         if (rc < 0) {
249             LOG(LOG_ERR, "Internal Error, load AIDE DB() was failed\n");
250             return -1;
251         }
252         OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_DATABASE,
253             "AIDE Database(ref): %d entries (< %s)\n"), rc, aideref_filename);
254
255         /* set flags */
256         ctx->conf->ima_validation_mode = OPENPTS_VALIDATION_MODE_AIDE;
257         ctx->conf->aide_database_filename = NULL;
258     }
259
260
261
262     /* load IML */
263     if (ima_filename == NULL) {
264         /* IML -> TSS -> Struct */
265         rc = getIml(ctx, 0);
266         OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_EVENTS,
267             "IML: %d events (< TSS)\n"), rc);
268     } else {
269         int count;
270         /* IML(file) -> Struct */
271         rc = readImaImlFile(
272                 ctx,
273                 ima_filename,
274                 ima_type, 0, &count);
275
276         if (rc != PTS_SUCCESS) {
277             LOG(LOG_ERR, "Internal Error, raild atr ead IMA's IML\n");
278             return -1;
279         }
280         OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_EVENTS_2,
281             "IML: %d events (< %s)\n"), rc, ima_filename);
282     }
283     if (rc < 0) {
284         LOG(LOG_ERR, "Internal Error\n");
285         return -1;
286     }
287
288     /* Conv to Aide */
289     if (aideref_filename == NULL) {
290         /* just IML -> AIDE.DB*/
291         rc = convertImlToAideDbFile(ctx, aide_filename);
292     } else {
293         /* IML&AIDE.DB -> AIDE.DB */
294         rc = writeReducedAidbDatabase(ctx->aide_ctx, aide_filename);
295     }
296     if (rc < 0) {
297         LOG(LOG_ERR, "Internal Error\n");
298         return -1;
299     }
300
301     OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_DATABASE_2,
302         "AIDE Database      : %d entries (> %s) \n"), rc, aide_filename);
303
304     if (ignorelist_filename != NULL) {
305         rc = writeAideIgnoreList(ctx, ignorelist_filename);
306         OUTPUT(NLS(MS_OPENPTS, OPENPTS_IML2AIDE_IGN_LIST,
307             "Ignore list  : %d entries (> %s) \n"), rc, ignorelist_filename);
308     }
309
310     /* free */
311     freePtsContext(ctx);
312     freePtsConfig(conf);
313
314     return rc;
315 }