Link libcharon to checksum_builder in order to get rid of the fake symbols.
[strongswan.git] / src / checksum / checksum_builder.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil, Switzerland
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <dlfcn.h>
20
21 #include <library.h>
22
23 int main(int argc, char* argv[])
24 {
25 int i;
26 integrity_checker_t *integrity;
27
28 /* avoid confusing leak reports in build process */
29 setenv("LEAK_DETECTIVE_DISABLE", "1", 0);
30 library_init(NULL);
31 atexit(library_deinit);
32
33 integrity = integrity_checker_create(NULL);
34
35 printf("/**\n");
36 printf(" * checksums of files and loaded code segments.\n");
37 printf(" * created by %s\n", argv[0]);
38 printf(" */\n");
39 printf("\n");
40 printf("#include <library.h>\n");
41 printf("\n");
42 printf("integrity_checksum_t checksums[] = {\n");
43 fprintf(stderr, "integrity test data:\n");
44 fprintf(stderr, "module name, file size / checksum segment size / checksum\n");
45 for (i = 1; i < argc; i++)
46 {
47 char *name, *path, *sname = NULL;
48 void *handle, *symbol;
49 u_int32_t fsum, ssum;
50 size_t fsize = 0;
51 size_t ssize = 0;
52
53 path = argv[i];
54
55 if ((name = strstr(path, "libstrongswan-")))
56 {
57 name = strdup(name + strlen("libstrongswan-"));
58 name[strlen(name) - 3] = '"';
59 name[strlen(name) - 2] = ',';
60 name[strlen(name) - 1] = '\0';
61 if (asprintf(&sname, "%.*s_plugin_create", strlen(name) - 2,
62 name) < 0)
63 {
64 fprintf(stderr, "failed to format plugin constructor "
65 "for '%s', ignored", path);
66 free(name);
67 continue;
68 }
69 translate(sname, "-", "_");
70 }
71 else if (strstr(path, "libstrongswan.so"))
72 {
73 name = strdup("libstrongswan\",");
74 sname = strdup("library_init");
75 }
76 else if (strstr(path, "libcharon.so"))
77 {
78 name = strdup("libcharon\",");
79 sname = strdup("libcharon_init");
80 }
81 else if (strstr(path, "pool"))
82 {
83 name = strdup("pool\",");
84 }
85 else if (strstr(path, "charon"))
86 {
87 name = strdup("charon\",");
88 }
89 else if (strstr(path, "pluto"))
90 {
91 name = strdup("pluto\",");
92 }
93 else if (strstr(path, "openac"))
94 {
95 name = strdup("openac\",");
96 }
97 else if (strstr(path, "scepclient"))
98 {
99 name = strdup("scepclient\",");
100 }
101 else if (strstr(path, "pki"))
102 {
103 name = strdup("pki\",");
104 }
105 else
106 {
107 fprintf(stderr, "don't know how to handle '%s', ignored", path);
108 continue;
109 }
110
111 fsum = integrity->build_file(integrity, path, &fsize);
112 ssum = 0;
113 if (sname)
114 {
115 handle = dlopen(path, RTLD_LAZY);
116 if (handle)
117 {
118 symbol = dlsym(handle, sname);
119 if (symbol)
120 {
121 ssum = integrity->build_segment(integrity, symbol, &ssize);
122 }
123 else
124 {
125 fprintf(stderr, "symbol lookup failed: %s\n", dlerror());
126 }
127 dlclose(handle);
128 }
129 else
130 {
131 fprintf(stderr, "dlopen failed: %s\n", dlerror());
132 }
133 }
134 printf("\t{\"%-20s%7u, 0x%08x, %6u, 0x%08x},\n",
135 name, fsize, fsum, ssize, ssum);
136 fprintf(stderr, "\"%-20s%7u / 0x%08x %6u / 0x%08x\n",
137 name, fsize, fsum, ssize, ssum);
138 free(sname);
139 free(name);
140 }
141 printf("};\n");
142 printf("\n");
143 printf("int checksum_count = countof(checksums);\n");
144 printf("\n");
145 integrity->destroy(integrity);
146
147 exit(0);
148 }
149