Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # Tune the configuration file |
| 4 | |
| 5 | use warnings; |
| 6 | use strict; |
| 7 | |
Simon Butcher | 07cc549 | 2017-02-02 14:26:15 +0000 | [diff] [blame] | 8 | my $config_file = "include/mbedtls/config.h"; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 9 | my $usage = <<EOU; |
Brian J Murray | e7f8dc3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 10 | $0 [-f <file>] [set <symbol> <value> | unset <symbol> | full | realfull] |
| 11 | |
| 12 | Commands |
| 13 | set <symbol> [<value>] - Uncomments or adds a #define for the <symbol> to |
| 14 | the configuration file, and optionally making it |
| 15 | of <value>. |
| 16 | If the symbol isn't present in the file an error |
| 17 | is returned. |
| 18 | unset <symbol> - Comments out the #define for the given symbol if |
| 19 | present in the configuration file. |
| 20 | full - Uncomments all #define's in the configuration file |
| 21 | excluding some reserved symbols, until the |
| 22 | 'Module configuration options' section |
| 23 | realfull - Uncomments all #define's with no exclusions |
| 24 | |
| 25 | Options |
| 26 | -f <filename> - The file or file path for the configuration file |
| 27 | to edit. When omitted, the following default is |
| 28 | used: |
| 29 | $config_file |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 30 | EOU |
Manuel Pégourié-Gonnard | 052ae25 | 2014-11-14 13:09:41 +0100 | [diff] [blame] | 31 | # for our eyes only: |
Manuel Pégourié-Gonnard | 2134d25 | 2016-01-04 12:57:32 +0100 | [diff] [blame] | 32 | # $0 [-f <file>] full|realfull |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 33 | |
| 34 | # Things that shouldn't be enabled with "full". |
| 35 | # Notes: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | # - MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 and |
| 37 | # MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 38 | # respective tests were adapted |
| 39 | my @excluded = qw( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | MBEDTLS_DEPRECATED_REMOVED |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | MBEDTLS_HAVE_SSE2 |
| 42 | MBEDTLS_PLATFORM_NO_STD_FUNCTIONS |
| 43 | MBEDTLS_ECP_DP_M221_ENABLED |
| 44 | MBEDTLS_ECP_DP_M383_ENABLED |
| 45 | MBEDTLS_ECP_DP_M511_ENABLED |
| 46 | MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES |
| 47 | MBEDTLS_NO_PLATFORM_ENTROPY |
| 48 | MBEDTLS_REMOVE_ARC4_CIPHERSUITES |
| 49 | MBEDTLS_SSL_HW_RECORD_ACCEL |
| 50 | MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 |
| 51 | MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION |
| 52 | MBEDTLS_ZLIB_SUPPORT |
| 53 | MBEDTLS_PKCS11_C |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 54 | _ALT\s*$ |
| 55 | ); |
| 56 | |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 57 | # Things that should be enabled in "full" even if they match @excluded |
| 58 | my @non_excluded = qw( |
| 59 | PLATFORM_[A-Z0-9]+_ALT |
| 60 | ); |
| 61 | |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 62 | # get -f option |
| 63 | if (@ARGV >= 2 && $ARGV[0] eq "-f") { |
| 64 | shift; # -f |
| 65 | $config_file = shift; |
| 66 | |
| 67 | -f $config_file or die "No such file: $config_file\n"; |
| 68 | } else { |
| 69 | if (! -f $config_file) { |
| 70 | chdir '..' or die; |
Manuel Pégourié-Gonnard | b20a70f | 2015-04-08 14:56:51 +0200 | [diff] [blame] | 71 | -f $config_file |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 72 | or die "Without -f, must be run from root or scripts\n" |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | # get action |
| 77 | die $usage unless @ARGV; |
| 78 | my $action = shift; |
| 79 | |
| 80 | my ($name, $value); |
Manuel Pégourié-Gonnard | 2134d25 | 2016-01-04 12:57:32 +0100 | [diff] [blame] | 81 | if ($action eq "full" || $action eq "realfull") { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 82 | # nothing to do |
| 83 | } elsif ($action eq "unset") { |
| 84 | die $usage unless @ARGV; |
| 85 | $name = shift; |
| 86 | } elsif ($action eq "set") { |
| 87 | die $usage unless @ARGV; |
| 88 | $name = shift; |
| 89 | $value = shift if @ARGV; |
| 90 | } else { |
| 91 | die $usage; |
| 92 | } |
| 93 | die $usage if @ARGV; |
| 94 | |
| 95 | open my $config_read, '<', $config_file or die "read $config_file: $!\n"; |
| 96 | my @config_lines = <$config_read>; |
| 97 | close $config_read; |
| 98 | |
Manuel Pégourié-Gonnard | 2134d25 | 2016-01-04 12:57:32 +0100 | [diff] [blame] | 99 | my ($exclude_re, $no_exclude_re); |
| 100 | if ($action eq "realfull") { |
| 101 | $exclude_re = qr/^$/; |
| 102 | $no_exclude_re = qr/./; |
| 103 | } else { |
| 104 | $exclude_re = join '|', @excluded; |
| 105 | $no_exclude_re = join '|', @non_excluded; |
| 106 | } |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 107 | |
| 108 | open my $config_write, '>', $config_file or die "write $config_file: $!\n"; |
| 109 | |
| 110 | my $done; |
| 111 | for my $line (@config_lines) { |
Manuel Pégourié-Gonnard | 2134d25 | 2016-01-04 12:57:32 +0100 | [diff] [blame] | 112 | if ($action eq "full" || $action eq "realfull") { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 113 | if ($line =~ /name SECTION: Module configuration options/) { |
| 114 | $done = 1; |
| 115 | } |
| 116 | |
Manuel Pégourié-Gonnard | b752715 | 2015-06-03 09:59:06 +0100 | [diff] [blame] | 117 | if (!$done && $line =~ m!^//\s?#define! && |
| 118 | ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) { |
Manuel Pégourié-Gonnard | ea0920f | 2015-03-24 09:50:15 +0100 | [diff] [blame] | 119 | $line =~ s!^//\s?!!; |
| 120 | } |
Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 121 | if (!$done && $line =~ m!^\s?#define! && |
| 122 | ! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) { |
Manuel Pégourié-Gonnard | ea0920f | 2015-03-24 09:50:15 +0100 | [diff] [blame] | 123 | $line =~ s!^!//!; |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 124 | } |
| 125 | } elsif ($action eq "unset") { |
Manuel Pégourié-Gonnard | 7f9049b | 2015-06-23 17:42:51 +0200 | [diff] [blame] | 126 | if (!$done && $line =~ /^\s*#define\s*$name\b/) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 127 | $line = '//' . $line; |
| 128 | $done = 1; |
| 129 | } |
| 130 | } elsif (!$done && $action eq "set") { |
Manuel Pégourié-Gonnard | 7f9049b | 2015-06-23 17:42:51 +0200 | [diff] [blame] | 131 | if ($line =~ m!^(?://)?\s*#define\s*$name\b!) { |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 132 | $line = "#define $name"; |
| 133 | $line .= " $value" if defined $value && $value ne ""; |
| 134 | $line .= "\n"; |
| 135 | $done = 1; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | print $config_write $line; |
| 140 | } |
| 141 | |
| 142 | close $config_write; |
| 143 | |
Manuel Pégourié-Gonnard | a14cbb6 | 2015-06-03 10:49:38 +0100 | [diff] [blame] | 144 | die "configuration section not found" if ($action eq "full" && !$done); |
| 145 | die "$name not found" if ($action ne "full" && !$done); |
Manuel Pégourié-Gonnard | ab3d862 | 2014-07-12 03:19:18 +0200 | [diff] [blame] | 146 | |
| 147 | __END__ |