blob: 7d6c4ea059ac9b960d27914f632397009153816a [file] [log] [blame]
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +02001#!/usr/bin/perl
2
3# Tune the configuration file
4
5use warnings;
6use strict;
7
8my $usage = <<EOU;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +02009$0 [-f <file>] unset <name>
10$0 [-f <file>] set <name> [<value>]
11EOU
Manuel Pégourié-Gonnard052ae252014-11-14 13:09:41 +010012# for our eyes only:
13# $0 [-f <file>] full
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020014
15# Things that shouldn't be enabled with "full".
16# Notes:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017# - MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 and
18# MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020019# respective tests were adapted
20my @excluded = qw(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021MBEDTLS_DEPRECATED_REMOVED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022MBEDTLS_HAVE_SSE2
23MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
24MBEDTLS_ECP_DP_M221_ENABLED
25MBEDTLS_ECP_DP_M383_ENABLED
26MBEDTLS_ECP_DP_M511_ENABLED
27MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
28MBEDTLS_NO_PLATFORM_ENTROPY
29MBEDTLS_REMOVE_ARC4_CIPHERSUITES
30MBEDTLS_SSL_HW_RECORD_ACCEL
31MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
32MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
33MBEDTLS_ZLIB_SUPPORT
34MBEDTLS_PKCS11_C
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020035_ALT\s*$
36);
37
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038my $config_file = "include/mbedtls/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020039
40# get -f option
41if (@ARGV >= 2 && $ARGV[0] eq "-f") {
42 shift; # -f
43 $config_file = shift;
44
45 -f $config_file or die "No such file: $config_file\n";
46} else {
47 if (! -f $config_file) {
48 chdir '..' or die;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020049 -f $config_file
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020050 or die "Without -f, must be run from root or scripts\n"
51 }
52}
53
54# get action
55die $usage unless @ARGV;
56my $action = shift;
57
58my ($name, $value);
59if ($action eq "full") {
60 # nothing to do
61} elsif ($action eq "unset") {
62 die $usage unless @ARGV;
63 $name = shift;
64} elsif ($action eq "set") {
65 die $usage unless @ARGV;
66 $name = shift;
67 $value = shift if @ARGV;
68} else {
69 die $usage;
70}
71die $usage if @ARGV;
72
73open my $config_read, '<', $config_file or die "read $config_file: $!\n";
74my @config_lines = <$config_read>;
75close $config_read;
76
77my $exclude_re = join '|', @excluded;
78
79open my $config_write, '>', $config_file or die "write $config_file: $!\n";
80
81my $done;
82for my $line (@config_lines) {
83 if ($action eq "full") {
84 if ($line =~ /name SECTION: Module configuration options/) {
85 $done = 1;
86 }
87
88 if (!$done && $line =~ m!^//\s?#define! && $line !~ /$exclude_re/) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +010089 $line =~ s!^//\s?!!;
90 }
91 if (!$done && $line =~ m!^\s?#define! && $line =~ /$exclude_re/) {
92 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020093 }
94 } elsif ($action eq "unset") {
95 if (!$done && $line =~ /^\s*#define\s*$name/) {
96 $line = '//' . $line;
97 $done = 1;
98 }
99 } elsif (!$done && $action eq "set") {
100 if ($line =~ m!^(?://)?\s*#define\s*$name!) {
101 $line = "#define $name";
102 $line .= " $value" if defined $value && $value ne "";
103 $line .= "\n";
104 $done = 1;
105 }
106 }
107
108 print $config_write $line;
109}
110
111close $config_write;
112
113warn "configuration section not found" if ($action eq "full" && !$done);
114warn "$name not found" if ($action ne "full" && !$done);
115
116__END__