blob: 2685b4ec34a3570156cf934308c80a9cee20df44 [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é-Gonnardb7527152015-06-03 09:59:06 +010038# Things that should be enabled in "full" even if they match @excluded
39my @non_excluded = qw(
40PLATFORM_[A-Z0-9]+_ALT
41);
42
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043my $config_file = "include/mbedtls/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020044
45# get -f option
46if (@ARGV >= 2 && $ARGV[0] eq "-f") {
47 shift; # -f
48 $config_file = shift;
49
50 -f $config_file or die "No such file: $config_file\n";
51} else {
52 if (! -f $config_file) {
53 chdir '..' or die;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020054 -f $config_file
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020055 or die "Without -f, must be run from root or scripts\n"
56 }
57}
58
59# get action
60die $usage unless @ARGV;
61my $action = shift;
62
63my ($name, $value);
64if ($action eq "full") {
65 # nothing to do
66} elsif ($action eq "unset") {
67 die $usage unless @ARGV;
68 $name = shift;
69} elsif ($action eq "set") {
70 die $usage unless @ARGV;
71 $name = shift;
72 $value = shift if @ARGV;
73} else {
74 die $usage;
75}
76die $usage if @ARGV;
77
78open my $config_read, '<', $config_file or die "read $config_file: $!\n";
79my @config_lines = <$config_read>;
80close $config_read;
81
82my $exclude_re = join '|', @excluded;
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010083my $no_exclude_re = join '|', @non_excluded;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020084
85open my $config_write, '>', $config_file or die "write $config_file: $!\n";
86
87my $done;
88for my $line (@config_lines) {
89 if ($action eq "full") {
90 if ($line =~ /name SECTION: Module configuration options/) {
91 $done = 1;
92 }
93
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010094 if (!$done && $line =~ m!^//\s?#define! &&
95 ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +010096 $line =~ s!^//\s?!!;
97 }
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +010098 if (!$done && $line =~ m!^\s?#define! &&
99 ! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100100 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200101 }
102 } elsif ($action eq "unset") {
103 if (!$done && $line =~ /^\s*#define\s*$name/) {
104 $line = '//' . $line;
105 $done = 1;
106 }
107 } elsif (!$done && $action eq "set") {
108 if ($line =~ m!^(?://)?\s*#define\s*$name!) {
109 $line = "#define $name";
110 $line .= " $value" if defined $value && $value ne "";
111 $line .= "\n";
112 $done = 1;
113 }
114 }
115
116 print $config_write $line;
117}
118
119close $config_write;
120
121warn "configuration section not found" if ($action eq "full" && !$done);
122warn "$name not found" if ($action ne "full" && !$done);
123
124__END__