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