2 * Copyright (C) 2010 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "tnc_imc_plugin.h"
17 #include "tnc_imc_manager.h"
20 #include <sys/types.h>
27 #include <utils/lexparser.h>
30 typedef struct private_tnc_imc_plugin_t private_tnc_imc_plugin_t
;
33 * Private data of a tnc_imc_plugin_t object.
35 struct private_tnc_imc_plugin_t
{
40 tnc_imc_plugin_t
public;
43 * TNC IMC manager controlling Integrity Measurement Collectors
49 * load IMCs from a configuration file
51 static bool load_imcs(private_tnc_imc_plugin_t
*this, char *filename
)
58 DBG1(DBG_TNC
, "loading IMCs from '%s'", filename
);
59 fd
= open(filename
, O_RDONLY
);
62 DBG1(DBG_TNC
, "opening configuration file '%s' failed: %s", filename
,
66 if (fstat(fd
, &sb
) == -1)
68 DBG1(DBG_LIB
, "getting file size of '%s' failed: %s", filename
,
73 addr
= mmap(NULL
, sb
.st_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
74 if (addr
== MAP_FAILED
)
76 DBG1(DBG_LIB
, "mapping '%s' failed: %s", filename
, strerror(errno
));
80 src
= chunk_create(addr
, sb
.st_size
);
82 while (fetchline(&src
, &line
))
90 /* skip comments or empty lines */
91 if (*line
.ptr
== '#' || !eat_whitespace(&line
))
96 /* determine keyword */
97 if (!extract_token(&token
, ' ', &line
))
99 DBG1(DBG_TNC
, "line %d: keyword must be followed by a space",
104 /* only interested in IMCs */
105 if (!match("IMC", &token
))
110 /* advance to the IMC name and extract it */
111 if (!extract_token(&token
, '"', &line
) ||
112 !extract_token(&token
, '"', &line
))
114 DBG1(DBG_TNC
, "line %d: IMC name must be set in double quotes",
119 /* copy the IMC name */
120 name
= malloc(token
.len
+ 1);
121 memcpy(name
, token
.ptr
, token
.len
);
122 name
[token
.len
] = '\0';
124 /* advance to the IMC path and extract it */
125 if (!eat_whitespace(&line
))
127 DBG1(DBG_TNC
, "line %d: IMC path is missing", line_nr
);
131 if (!extract_token(&token
, ' ', &line
))
136 /* copy the IMC path */
137 path
= malloc(token
.len
+ 1);
138 memcpy(path
, token
.ptr
, token
.len
);
139 path
[token
.len
] = '\0';
141 /* load and register IMC instance */
142 imc
= tnc_imc_create(name
, path
);
149 if (!this->imcs
->add(this->imcs
, imc
))
151 if (imc
->terminate
&&
152 imc
->terminate(imc
->get_id(imc
)) != TNC_RESULT_SUCCESS
)
154 DBG1(DBG_TNC
, "IMC \"%s\" not terminated successfully",
160 DBG1(DBG_TNC
, "IMC %u \"%s\" loaded from '%s'", imc
->get_id(imc
),
163 munmap(addr
, sb
.st_size
);
168 METHOD(plugin_t
, get_name
, char*,
169 private_tnc_imc_plugin_t
*this)
174 METHOD(plugin_t
, get_features
, int,
175 private_tnc_imc_plugin_t
*this, plugin_feature_t
*features
[])
177 static plugin_feature_t f
[] = {
178 PLUGIN_PROVIDE(CUSTOM
, "imc-manager"),
184 METHOD(plugin_t
, destroy
, void,
185 private_tnc_imc_plugin_t
*this)
187 lib
->set(lib
, "imc-manager", NULL
);
188 this->imcs
->destroy(this->imcs
);
195 plugin_t
*tnc_imc_plugin_create(void)
197 private_tnc_imc_plugin_t
*this;
203 .get_name
= _get_name
,
204 .get_features
= _get_features
,
208 .imcs
= tnc_imc_manager_create(),
211 lib
->set(lib
, "imc-manager", this->imcs
);
213 /* Load IMCs and abort if not all instances initalize successfully */
214 tnc_config
= lib
->settings
->get_str(lib
->settings
,
215 "charon.plugins.tnc-imc.tnc_config", "/etc/tnc_config");
216 if (!load_imcs(this, tnc_config
))
222 return &this->public.plugin
;