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{\"%-25s%7u, 0x%08x, %6u, 0x%08x},\n",
68 name
, fsize
, fsum
, ssize
, ssum
);
69 fprintf(stderr
, "\"%-25s%7u / 0x%08x %6u / 0x%08x\n",
70 name
, fsize
, fsum
, ssize
, ssum
);
74 * Build checksums for a set of plugins
76 static void build_plugin_checksums(char *plugins
)
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/libstrongswan-%s.so",
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\",", (int)(pos
- binary
),
111 if (streq(name
, "libstrongswan\","))
113 snprintf(sname
, sizeof(sname
), "%s", "library_init");
117 snprintf(sname
, sizeof(sname
), "%.*s_init", (int)(pos
- binary
),
120 build_checksum(path
, name
, sname
);
124 snprintf(name
, sizeof(name
), "%s\",", binary
);
125 build_checksum(path
, name
, NULL
);
130 int main(int argc
, char* argv
[])
134 /* forces link against libhydra/libcharon, imports symbols needed to
139 /* avoid confusing leak reports in build process */
140 setenv("LEAK_DETECTIVE_DISABLE", "1", 0);
141 /* don't use a strongswan.conf, forces integrity check to disabled */
143 atexit(library_deinit
);
145 integrity
= integrity_checker_create(NULL
);
148 printf(" * checksums of files and loaded code segments.\n");
149 printf(" * created by %s\n", argv
[0]);
152 printf("#include <library.h>\n");
154 printf("integrity_checksum_t checksums[] = {\n");
155 fprintf(stderr
, "integrity test data:\n");
156 fprintf(stderr
, "module name, file size / checksum "
157 "segment size / checksum\n");
158 for (i
= 1; i
< argc
; i
++)
160 build_binary_checksum(argv
[i
]);
163 build_plugin_checksums(S_PLUGINS
);
166 build_plugin_checksums(H_PLUGINS
);
169 build_plugin_checksums(P_PLUGINS
);
172 build_plugin_checksums(C_PLUGINS
);
177 printf("int checksum_count = countof(checksums);\n");
179 integrity
->destroy(integrity
);