summaryrefslogtreecommitdiffstats
path: root/pgp.gumx.cc/cgi-bin/pgp-hkp.sh
blob: c4e74d7ea605b29f1aa146985c01bfec3db4cf02 (plain) (blame)
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
137
138
#!/bin/bash
# HKP keyserver CGI
# Implements: GET /pks/lookup?op=get|index|vindex and POST /pks/add
# Keys live in /var/pgp/keys/<FINGERPRINT_UPPERCASE>.asc

KEYS_DIR=/var/pgp/keys

FAVICON="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1IDUiPjxyZWN0IHg9IjEiIHk9IjAiIHdpZHRoPSIxIiBoZWlnaHQ9IjEiLz48cmVjdCB4PSIyIiB5PSIxIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIi8+PHJlY3QgeD0iMCIgeT0iMiIgd2lkdGg9IjEiIGhlaWdodD0iMSIvPjxyZWN0IHg9IjEiIHk9IjIiIHdpZHRoPSIxIiBoZWlnaHQ9IjEiLz48cmVjdCB4PSIyIiB5PSIyIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIi8+PC9zdmc+"

CSS='@font-face { font-family: "Kawkab Mono"; src: url(/fonts/KawkabMono-Regular.woff2); font-weight: normal; }
@font-face { font-family: "Kawkab Mono"; src: url(/fonts/KawkabMono-Bold.woff2); font-weight: bold; }
* { unicode-bidi: plaintext; box-sizing: border-box; }
html { color: black; background-color: white; }
body { font-family: "Kawkab Mono"; font-size: 16px; line-height: 1.4; margin: 0; padding: 4rem 0; min-height: 100%; overflow-wrap: break-word; }
main, header, footer { max-width: 800px; margin-inline: auto; padding: 0 2rem; }
h1, header, footer { text-align: center; }
main { text-align: justify; }
p, h2, h3, h4 { margin: 1em 0 0 0; }
table { margin: 1em auto; border-collapse: collapse; }
th, td { border: 1px solid; padding: 0.3em 0.8em; }
code { font-size: 85%; }
hr { border: none; border-top: thin solid; margin: 1.25rem 0; }
header { margin-bottom: 1em; }
footer { margin-top: 3em; }
a { color: inherit; }
@media (max-width: 600px) { body { font-size: 0.9em; } h1 { font-size: 1.8em; } }
@media (max-width: 400px) { body { font-size: 0.8em; } h1 { font-size: 1.6em; } }
@media (prefers-color-scheme: dark) { html { filter: invert(1); } }'

FOOTER='<footer>
<hr>
<a href="https://twt.gumx.cc">twt</a> /
<a href="https://git.gumx.cc">git</a> /
<a href="https://mail.gumx.cc">mail</a> /
<a href="https://irc.gumx.cc">irc</a> /
<a href="https://files.gumx.cc">files</a> /
<a href="https://vpn.gumx.cc">vpn</a> /
<a href="https://pgp.gumx.cc">pgp</a> /
<a href="https://demo.gumx.cc">demo</a> /
<a href="https://wk.fo">wk.fo</a>
</footer>'

qs_get() {
    echo "$QUERY_STRING" \
        | tr '&' '\n' \
        | grep -E "^$1=" \
        | head -1 \
        | cut -d= -f2- \
        | sed 's/+/ /g; s/%20/ /g'
}

op=$(qs_get op)
search=$(qs_get search | sed 's/^0x//i; s/^0X//i')

case "$REQUEST_METHOD" in
    GET)
        case "$op" in
            get)
                printf "Content-Type: text/plain\r\n\r\n"
                FP="${search^^}"
                keyfile="$KEYS_DIR/$FP.asc"
                if [[ -f "$keyfile" ]]; then
                    cat "$keyfile"
                else
                    printf "Error: No key found for %s\n" "$search"
                fi
                ;;
            index)
                printf "Content-Type: text/plain\r\n\r\n"
                total=$(find "$KEYS_DIR" -name '*.asc' 2>/dev/null | wc -l)
                echo "info:1:$total"
                for f in "$KEYS_DIR"/*.asc; do
                    [[ -f "$f" ]] || continue
                    gpg --with-colons --import-options show-only \
                        --import < "$f" 2>/dev/null \
                        | awk -F: '/^pub/{print "pub:" $5 ":" $6 ":::" $10 "::"}'
                done
                ;;
            vindex)
                printf "Content-Type: text/html\r\n\r\n"
                printf '<!DOCTYPE html>\n<html lang="en">\n<head>\n'
                printf '<meta charset="utf-8">\n'
                printf '<link rel="icon" type="image/svg+xml" href="%s">\n' "$FAVICON"
                printf '<meta name="viewport" content="width=device-width,initial-scale=1">\n'
                printf '<title>pgp.gumx.cc / keys</title>\n'
                printf '<style>\n%s\n</style>\n' "$CSS"
                printf '</head>\n<body>\n'
                printf '<header>\n<h1><a href="https://gumx.cc">gumx</a> / <a href="https://pgp.gumx.cc">pgp</a> / keys</h1>\n</header>\n'
                printf '<main>\n<table>\n<tr><th>fingerprint</th><th>uid</th></tr>\n'
                found=0
                for f in "$KEYS_DIR"/*.asc; do
                    [[ -f "$f" ]] || continue
                    found=1
                    FP=$(basename "$f" .asc)
                    KEY_UID=$(gpg --with-colons --import-options show-only --import < "$f" 2>/dev/null \
                              | awk -F: '/^uid/{print $10; exit}')
                    printf '<tr><td><a href="/pks/lookup?op=get&amp;search=0x%s"><code>%s</code></a></td><td>%s</td></tr>\n' \
                        "$FP" "$FP" "$KEY_UID"
                done
                [[ $found -eq 0 ]] && printf '<tr><td colspan="2">no keys</td></tr>\n'
                printf '</table>\n</main>\n'
                printf '%s\n' "$FOOTER"
                printf '</body>\n</html>\n'
                ;;
            *)
                printf "Content-Type: text/plain\r\n\r\n"
                echo "Error: unknown op"
                ;;
        esac
        ;;

    POST)
        printf "Content-Type: text/plain\r\n\r\n"
        read -r -d '' -n "${CONTENT_LENGTH:-4096}" SUBMITTED_KEY

        TMPDIR_GPG=$(mktemp -d)
        FINGERPRINT=$(echo "$SUBMITTED_KEY" \
            | gpg --homedir "$TMPDIR_GPG" \
                  --with-colons --import-options show-only \
                  --import 2>/dev/null \
            | awk -F: '/^fpr/{print $10; exit}')
        rm -rf "$TMPDIR_GPG"

        if [[ -z "$FINGERPRINT" ]]; then
            echo "Error: Could not parse submitted key"
            exit 0
        fi

        KEYFILE="$KEYS_DIR/${FINGERPRINT^^}.asc"
        if [[ -f "$KEYFILE" ]]; then
            echo "$SUBMITTED_KEY" > "$KEYFILE"
            echo "Key updated successfully"
        else
            echo "Error: Fingerprint not registered."
            echo "Contact ahmed@gumx.cc to arrange a key signing meeting."
        fi
        ;;
esac