summaryrefslogtreecommitdiff
path: root/lib/sel-hashes.awk
blob: da6cfd71297225be6707970a806dc63964b02e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Expand a list of selected hashes to a list of enabled hashes, using
# the information in hashes.conf.
#
#   Copyright 2018 Zack Weinberg
#
#   This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public License
#   as published by the Free Software Foundation; either version 2.1 of
#   the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, see
#   <https://www.gnu.org/licenses/>.

BEGIN {
    enable_all       = 0
    enable_strong    = 0
    enable_glibc     = 0
    enable_alt       = 0
    enable_fedora    = 0
    enable_freebsd   = 0
    enable_netbsd    = 0
    enable_openbsd   = 0
    enable_osx       = 0
    enable_owl       = 0
    enable_solaris   = 0
    enable_suse      = 0
    error = 0
    split(SELECTED_HASHES, selected_hashes_list, ",")
    for (i in selected_hashes_list) {
        h = selected_hashes_list[i]
        if (h == "all") {
            enable_all = 1
        } else if (h == "strong") {
            enable_strong = 1
        } else if (h == "glibc") {
            enable_glibc = 1
        } else if (h == "alt") {
            enable_alt = 1
        } else if (h == "fedora") {
            enable_fedora = 1
        } else if (h == "freebsd") {
            enable_freebsd = 1
        } else if (h == "netbsd") {
            enable_netbsd = 1
        } else if (h == "openbsd") {
            enable_openbsd = 1
        } else if (h == "osx") {
            enable_osx = 1
        } else if (h == "owl") {
            enable_owl = 1
        } else if (h == "solaris") {
            enable_solaris = 1
        } else if (h == "suse") {
            enable_suse = 1
        } else {
            enable_some = 1
            selected_hashes[h] = 1
        }
    }
    if (enable_all && (enable_strong  || enable_glibc   || enable_some    || \
                       enable_alt     || enable_fedora  || enable_freebsd || \
                       enable_netbsd  || enable_openbsd || enable_osx     || \
                       enable_owl     || enable_solaris || enable_suse)) {
        error = 1
        exit 1
    }
}

/^#/ {
    next
}

{
    if (enable_all || $1 in selected_hashes) {
        enabled_hashes[$1] = 1
    } else {
        enabled_hashes[$1] = 0

        split($4, flags, ",")
        for (i in flags) {
            flag = flags[i]
            if (flag == "STRONG" && enable_strong) {
                enabled_hashes[$1] = 1
            } else if (flag == "GLIBC" && enable_glibc) {
                enabled_hashes[$1] = 1
            } else if (flag == "ALT" && enable_alt) {
                enabled_hashes[$1] = 1
            } else if (flag == "FEDORA" && enable_fedora) {
                enabled_hashes[$1] = 1
            } else if (flag == "FREEBSD" && enable_freebsd) {
                enabled_hashes[$1] = 1
            } else if (flag == "NETBSD" && enable_netbsd) {
                enabled_hashes[$1] = 1
            } else if (flag == "OPENBSD" && enable_openbsd) {
                enabled_hashes[$1] = 1
            } else if (flag == "OSX" && enable_osx) {
                enabled_hashes[$1] = 1
            } else if (flag == "OWL" && enable_owl) {
                enabled_hashes[$1] = 1
            } else if (flag == "SOLARIS" && enable_solaris) {
                enabled_hashes[$1] = 1
            } else if (flag == "SUSE" && enable_suse) {
                enabled_hashes[$1] = 1
            }
        }
    }
}


END {
    if (error) {
        exit 1
    }

    # Check for individual selected hashes that didn't appear in
    # hashes.conf.
    for (h in selected_hashes) {
        if (!(h in enabled_hashes)) {
            exit 1
        }
    }

    enabled_hash_list = ","
    for (i in enabled_hashes) {
        if (enabled_hashes[i]) {
            enabled_hash_list = enabled_hash_list i ","
        }
    }
    print enabled_hash_list
}