blob: 9cc50342e4abeae5c7056a6e3da1f99a533ea476 [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:
17# - POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3 and
18# POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the
19# respective tests were adapted
20my @excluded = qw(
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +010021POLARSSL_DEPRECATED_REMOVED
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020022POLARSSL_HAVE_INT8
23POLARSSL_HAVE_INT16
24POLARSSL_HAVE_SSE2
25POLARSSL_PLATFORM_NO_STD_FUNCTIONS
26POLARSSL_ECP_DP_M221_ENABLED
27POLARSSL_ECP_DP_M383_ENABLED
28POLARSSL_ECP_DP_M511_ENABLED
29POLARSSL_NO_DEFAULT_ENTROPY_SOURCES
30POLARSSL_NO_PLATFORM_ENTROPY
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +010031POLARSSL_REMOVE_ARC4_CIPHERSUITES
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020032POLARSSL_SSL_HW_RECORD_ACCEL
Manuel Pégourié-Gonnard86b29082014-11-06 02:28:34 +010033POLARSSL_SSL_DISABLE_RENEGOTIATION
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020034POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3
35POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
36POLARSSL_ZLIB_SUPPORT
37POLARSSL_PKCS11_C
38_ALT\s*$
39);
40
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041my $config_file = "include/mbedtls/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020042
43# get -f option
44if (@ARGV >= 2 && $ARGV[0] eq "-f") {
45 shift; # -f
46 $config_file = shift;
47
48 -f $config_file or die "No such file: $config_file\n";
49} else {
50 if (! -f $config_file) {
51 chdir '..' or die;
52 -d $config_file
53 or die "Without -f, must be run from root or scripts\n"
54 }
55}
56
57# get action
58die $usage unless @ARGV;
59my $action = shift;
60
61my ($name, $value);
62if ($action eq "full") {
63 # nothing to do
64} elsif ($action eq "unset") {
65 die $usage unless @ARGV;
66 $name = shift;
67} elsif ($action eq "set") {
68 die $usage unless @ARGV;
69 $name = shift;
70 $value = shift if @ARGV;
71} else {
72 die $usage;
73}
74die $usage if @ARGV;
75
76open my $config_read, '<', $config_file or die "read $config_file: $!\n";
77my @config_lines = <$config_read>;
78close $config_read;
79
80my $exclude_re = join '|', @excluded;
81
82open my $config_write, '>', $config_file or die "write $config_file: $!\n";
83
84my $done;
85for my $line (@config_lines) {
86 if ($action eq "full") {
87 if ($line =~ /name SECTION: Module configuration options/) {
88 $done = 1;
89 }
90
91 if (!$done && $line =~ m!^//\s?#define! && $line !~ /$exclude_re/) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +010092 $line =~ s!^//\s?!!;
93 }
94 if (!$done && $line =~ m!^\s?#define! && $line =~ /$exclude_re/) {
95 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020096 }
97 } elsif ($action eq "unset") {
98 if (!$done && $line =~ /^\s*#define\s*$name/) {
99 $line = '//' . $line;
100 $done = 1;
101 }
102 } elsif (!$done && $action eq "set") {
103 if ($line =~ m!^(?://)?\s*#define\s*$name!) {
104 $line = "#define $name";
105 $line .= " $value" if defined $value && $value ne "";
106 $line .= "\n";
107 $done = 1;
108 }
109 }
110
111 print $config_write $line;
112}
113
114close $config_write;
115
116warn "configuration section not found" if ($action eq "full" && !$done);
117warn "$name not found" if ($action ne "full" && !$done);
118
119__END__