2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil, Switzerland
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
24 #include <utils/enumerator.h>
26 /* we need to fake the pluto symbol to dlopen() the xauth plugin */
32 integrity_checker_t
*integrity
;
35 * Create the checksum of a binary, using name and a symbol name
37 static void build_checksum(char *path
, char *name
, char *sname
)
39 void *handle
, *symbol
;
44 fsum
= integrity
->build_file(integrity
, path
, &fsize
);
48 handle
= dlopen(path
, RTLD_LAZY
);
51 symbol
= dlsym(handle
, sname
);
54 ssum
= integrity
->build_segment(integrity
, symbol
, &ssize
);
58 fprintf(stderr
, "symbol lookup failed: %s\n", dlerror());
64 fprintf(stderr
, "dlopen failed: %s\n", dlerror());
67 printf("\t{\"%-20s%7u, 0x%08x, %6u, 0x%08x},\n",
68 name
, fsize
, fsum
, ssize
, ssum
);
69 fprintf(stderr
, "\"%-20s%7u / 0x%08x %6u / 0x%08x\n",
70 name
, fsize
, fsum
, ssize
, ssum
);
74 * Build checksums for a set of plugins in a given path prefix
76 static void build_plugin_checksums(char *plugins
, char *prefix
)
78 enumerator_t
*enumerator
;
79 char *plugin
, path
[256], under
[128], sname
[128], name
[128];
81 enumerator
= enumerator_create_token(plugins
, " ", " ");
82 while (enumerator
->enumerate(enumerator
, &plugin
))
84 snprintf(under
, sizeof(under
), "%s", plugin
);
85 translate(under
, "-", "_");
86 snprintf(path
, sizeof(path
), "%s/%s/.libs/libstrongswan-%s.so",
87 prefix
, under
, plugin
);
88 snprintf(sname
, sizeof(sname
), "%s_plugin_create", under
);
89 snprintf(name
, sizeof(name
), "%s\",", plugin
);
90 build_checksum(path
, name
, sname
);
92 enumerator
->destroy(enumerator
);
96 * Build checksums for a binary/library found at path
98 static void build_binary_checksum(char *path
)
100 char *binary
, *pos
, name
[128], sname
[128];
102 binary
= strrchr(path
, '/');
106 pos
= strrchr(binary
, '.');
107 if (pos
&& streq(pos
, ".so"))
109 snprintf(name
, sizeof(name
), "%.*s\",", pos
- binary
, binary
);
110 if (streq(name
, "libstrongswan\","))
112 snprintf(sname
, sizeof(sname
), "%s", "library_init");
116 snprintf(sname
, sizeof(sname
), "%.*s_init", pos
- binary
, binary
);
118 build_checksum(path
, name
, sname
);
122 snprintf(name
, sizeof(name
), "%s\",", binary
);
123 build_checksum(path
, name
, NULL
);
128 int main(int argc
, char* argv
[])
132 /* forces link against libhydra/libcharon */
136 /* avoid confusing leak reports in build process */
137 setenv("LEAK_DETECTIVE_DISABLE", "1", 0);
138 /* don't use a strongswan.conf, forces integrity check to disabled */
140 atexit(library_deinit
);
142 integrity
= integrity_checker_create(NULL
);
145 printf(" * checksums of files and loaded code segments.\n");
146 printf(" * created by %s\n", argv
[0]);
149 printf("#include <library.h>\n");
151 printf("integrity_checksum_t checksums[] = {\n");
152 fprintf(stderr
, "integrity test data:\n");
153 fprintf(stderr
, "module name, file size / checksum segment size / checksum\n");
154 for (i
= 1; i
< argc
; i
++)
156 build_binary_checksum(argv
[i
]);
159 build_plugin_checksums(S_PLUGINS
, S_PATH
);
162 build_plugin_checksums(H_PLUGINS
, H_PATH
);
165 build_plugin_checksums(P_PLUGINS
, P_PATH
);
168 build_plugin_checksums(C_PLUGINS
, C_PATH
);
173 printf("int checksum_count = countof(checksums);\n");
175 integrity
->destroy(integrity
);