removed trailing spaces ([[:space:]]+$)
[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 #include <stdlib.h>
17 #include <stdio.h>
18 #include <dlfcn.h>
19
20 #include <library.h>
21
22 /* we need to fake some charon symbols to dlopen() its plugins */
23 void *charon, *eap_type_names, *auth_class_names, *protocol_id_names,
24 *action_names, *ipsec_mode_names, *ike_sa_state_names, *child_sa_state_names,
25 *policy_dir_names, *ipcomp_transform_names, *debug_names, *controller_cb_empty;
26
27 int main(int argc, char* argv[])
28 {
29 int i;
30 integrity_checker_t *integrity;
31
32 /* avoid confusing leak reports in build process */
33 setenv("LEAK_DETECTIVE_DISABLE", "1", 0);
34 library_init(NULL);
35 atexit(library_deinit);
36
37 integrity = integrity_checker_create(NULL);
38
39 printf("/**\n");
40 printf(" * checksums of files and loaded code segments.\n");
41 printf(" * created by %s\n", argv[0]);
42 printf(" */\n");
43 printf("\n");
44 printf("#include <library.h>\n");
45 printf("\n");
46 printf("integrity_checksum_t checksums[] = {\n");
47 fprintf(stderr, "integrity test data:\n");
48 fprintf(stderr, "module name, file size / checksum segment size / checksum\n");
49 for (i = 1; i < argc; i++)
50 {
51 char *name, *path, *sname = NULL;
52 void *handle, *symbol;
53 u_int32_t fsum, ssum;
54 size_t fsize = 0;
55 size_t ssize = 0;
56
57 path = argv[i];
58
59 if ((name = strstr(path, "libstrongswan-")))
60 {
61 name = strdup(name + strlen("libstrongswan-"));
62 name[strlen(name) - 3] = '"';
63 name[strlen(name) - 2] = ',';
64 name[strlen(name) - 1] = '\0';
65 sname = "plugin_create";
66 }
67 else if (strstr(path, "libstrongswan.so"))
68 {
69 name = strdup("libstrongswan\",");
70 sname = "library_init";
71 }
72 else if (strstr(path, "pool"))
73 {
74 name = strdup("pool\",");
75 }
76 else if (strstr(path, "charon"))
77 {
78 name = strdup("charon\",");
79 }
80 else if (strstr(path, "pluto"))
81 {
82 name = strdup("pluto\",");
83 }
84 else if (strstr(path, "openac"))
85 {
86 name = strdup("openac\",");
87 }
88 else if (strstr(path, "scepclient"))
89 {
90 name = strdup("scepclient\",");
91 }
92 else if (strstr(path, "pki"))
93 {
94 name = strdup("pki\",");
95 }
96 else
97 {
98 fprintf(stderr, "don't know how to handle '%s', ignored", path);
99 continue;
100 }
101
102 fsum = integrity->build_file(integrity, path, &fsize);
103 ssum = 0;
104 if (sname)
105 {
106 handle = dlopen(path, RTLD_LAZY);
107 if (handle)
108 {
109 symbol = dlsym(handle, sname);
110 if (symbol)
111 {
112 ssum = integrity->build_segment(integrity, symbol, &ssize);
113 }
114 else
115 {
116 fprintf(stderr, "symbol lookup failed: %s\n", dlerror());
117 }
118 dlclose(handle);
119 }
120 else
121 {
122 fprintf(stderr, "dlopen failed: %s\n", dlerror());
123 }
124 }
125 printf("\t{\"%-20s%7u, 0x%08x, %6u, 0x%08x},\n",
126 name, fsize, fsum, ssize, ssum);
127 fprintf(stderr, "\"%-20s%7u / 0x%08x %6u / 0x%08x\n",
128 name, fsize, fsum, ssize, ssum);
129 free(name);
130 }
131 printf("};\n");
132 printf("\n");
133 printf("int checksum_count = countof(checksums);\n");
134 printf("\n");
135 integrity->destroy(integrity);
136
137 exit(0);
138 }
139