Segment check added for libhydra.
[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, "libhydra.so"))
77 {
78 name = strdup("libhydra\",");
79 sname = strdup("libhydra_init");
80 }
81 else if (strstr(path, "libcharon.so"))
82 {
83 name = strdup("libcharon\",");
84 sname = strdup("libcharon_init");
85 }
86 else if (strstr(path, "pool"))
87 {
88 name = strdup("pool\",");
89 }
90 else if (strstr(path, "charon"))
91 {
92 name = strdup("charon\",");
93 }
94 else if (strstr(path, "pluto"))
95 {
96 name = strdup("pluto\",");
97 }
98 else if (strstr(path, "openac"))
99 {
100 name = strdup("openac\",");
101 }
102 else if (strstr(path, "scepclient"))
103 {
104 name = strdup("scepclient\",");
105 }
106 else if (strstr(path, "pki"))
107 {
108 name = strdup("pki\",");
109 }
110 else
111 {
112 fprintf(stderr, "don't know how to handle '%s', ignored", path);
113 continue;
114 }
115
116 fsum = integrity->build_file(integrity, path, &fsize);
117 ssum = 0;
118 if (sname)
119 {
120 handle = dlopen(path, RTLD_LAZY);
121 if (handle)
122 {
123 symbol = dlsym(handle, sname);
124 if (symbol)
125 {
126 ssum = integrity->build_segment(integrity, symbol, &ssize);
127 }
128 else
129 {
130 fprintf(stderr, "symbol lookup failed: %s\n", dlerror());
131 }
132 dlclose(handle);
133 }
134 else
135 {
136 fprintf(stderr, "dlopen failed: %s\n", dlerror());
137 }
138 }
139 printf("\t{\"%-20s%7u, 0x%08x, %6u, 0x%08x},\n",
140 name, fsize, fsum, ssize, ssum);
141 fprintf(stderr, "\"%-20s%7u / 0x%08x %6u / 0x%08x\n",
142 name, fsize, fsum, ssize, ssum);
143 free(sname);
144 free(name);
145 }
146 printf("};\n");
147 printf("\n");
148 printf("int checksum_count = countof(checksums);\n");
149 printf("\n");
150 integrity->destroy(integrity);
151
152 exit(0);
153 }
154