964cc716c26bc7fc628411c496a5f32e7a215345
[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 some charon symbols to dlopen() its plugins */
24 void *charon, *eap_type_names, *auth_class_names, *protocol_id_names,
25 *action_names, *ipsec_mode_names, *ike_sa_state_names, *child_sa_state_names,
26 *policy_dir_names, *ipcomp_transform_names, *debug_names, *controller_cb_empty;
27
28 int main(int argc, char* argv[])
29 {
30 int i;
31 integrity_checker_t *integrity;
32
33 /* avoid confusing leak reports in build process */
34 setenv("LEAK_DETECTIVE_DISABLE", "1", 0);
35 library_init(NULL);
36 atexit(library_deinit);
37
38 integrity = integrity_checker_create(NULL);
39
40 printf("/**\n");
41 printf(" * checksums of files and loaded code segments.\n");
42 printf(" * created by %s\n", argv[0]);
43 printf(" */\n");
44 printf("\n");
45 printf("#include <library.h>\n");
46 printf("\n");
47 printf("integrity_checksum_t checksums[] = {\n");
48 fprintf(stderr, "integrity test data:\n");
49 fprintf(stderr, "module name, file size / checksum segment size / checksum\n");
50 for (i = 1; i < argc; i++)
51 {
52 char *name, *path, *sname = NULL;
53 void *handle, *symbol;
54 u_int32_t fsum, ssum;
55 size_t fsize = 0;
56 size_t ssize = 0;
57
58 path = argv[i];
59
60 if ((name = strstr(path, "libstrongswan-")))
61 {
62 name = strdup(name + strlen("libstrongswan-"));
63 name[strlen(name) - 3] = '"';
64 name[strlen(name) - 2] = ',';
65 name[strlen(name) - 1] = '\0';
66 if (asprintf(&sname, "%.*s_plugin_create", strlen(name) - 2,
67 name) < 0)
68 {
69 fprintf(stderr, "failed to format plugin constructor "
70 "for '%s', ignored", path);
71 free(name);
72 continue;
73 }
74 translate(sname, "-", "_");
75 }
76 else if (strstr(path, "libstrongswan.so"))
77 {
78 name = strdup("libstrongswan\",");
79 sname = strdup("library_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