epm-repo-addkey 4.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/bin/sh
#
# Copyright (C) 2023  Etersoft
# Copyright (C) 2023  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

load_helper epm-sh-altlinux


23 24 25
# allowed files too
__epm_get_file_from_url()
{
26
    local url="$1"
27
    local tmpfile=$(mktemp)
28 29
    remove_on_exit "$tmpfile"
    eget -O "$tmpfile" "$url" >/dev/null
30 31 32
    echo "$tmpfile"
}

33 34
__epm_addkey_altlinux()
{
35
    local name
36
    local url="$1"
37 38 39 40 41 42 43 44 45
    shift
    if is_url "$url" ; then
        name="$(basename "$url" .gpg)"
    else
        name="$url"
        url="$1"
        shift
    fi

46 47 48 49 50 51 52 53 54 55
    local fingerprint
    if is_url "$url" ; then
        fingerprint="$1"
        shift
    else
        fingerprint="$url"
        url=""
    fi

    local comment="$1"
56
    # compat
57
    [ -n "$2" ] && name="$2"
58 59 60 61 62 63 64 65 66 67

    [ -s /etc/apt/vendors.list.d/$name.list ] && return

# TODO: get this info from the gpg key
    cat << EOF | sudorun tee /etc/apt/vendors.list.d/$name.list
simple-key "$name" {
        FingerPrint "$fingerprint";
        Name "$comment";
}
EOF
68
    if [ -n "$url" ] ; then
69 70
        local tmpfile=$(__epm_get_file_from_url $url) || fatal
        sudocmd gpg --no-default-keyring --keyring /usr/lib/alt-gpgkeys/pubring.gpg --import $tmpfile
71
    fi
72 73 74
}


75 76
__epm_addkey_alpine()
{
77
    local name
78
    local url="$1"
79 80 81 82 83 84 85 86
    shift
    if is_url "$url" ; then
        name="$(basename "$url" .rsa)"
    else
        name="$url"
        url="$1"
        shift
    fi
87 88 89 90 91 92 93 94 95

    local target="/etc/apk/keys/$name.rsa"

    [ -s $target ] && return

    local tmpfile=$(__epm_get_file_from_url $url) || fatal
    sudocmd cp $tmpfile $target
}

96 97 98

__epm_addkey_dnf()
{
99
    local name
100
    local url="$1"
101 102 103 104 105 106 107 108 109 110 111 112
    shift
    if is_url "$url" ; then
        name="$(basename "$url" .gpg)"
    else
        name="$url"
        url="$1"
        shift
    fi
    local gpgkeyurl="$1"
    local nametext="$2"
    # compat
    [ -n "$3" ] && name="$3"
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    # TODO: missed name, nametext, gpgkeyurl (disable gpgcheck=1)

    local target="/etc/yum.repos.d/$name.repo"
    [ -s $target ] && return

    local tmpfile=$(mktemp)
    remove_on_exit $tmpfile
    cat >$tmpfile <<EOF
[$name]
name=$nametext
baseurl=$url
gpgcheck=1
enabled=1
gpgkey=$gpgkeyurl
EOF
    chmod 644 $tmpfile
    sudocmd cp $tmpfile $target
131 132 133 134 135
}


__epm_addkey_deb()
{
136
    local name
137
    local url="$1"
138 139 140 141 142 143 144 145 146 147 148 149
    shift
    if is_url "$url" ; then
        name="$(basename "$url" .gpg)"
    else
        name="$url"
        url="$1"
        shift
    fi
    local fingerprint="$1"
    local comment="$2"
    # compat
    [ -n "$3" ] && name="$3"
150

151
    # FIXME: check by GPG PUBKEY
152 153 154
    [ -s /etc/apt/trusted.gpg.d/$name.gpg ] && return

    if [ -z "$fingerprint" ] ; then
155
        local tmpfile=$(__epm_get_file_from_url $url) || fatal
156 157 158 159
        if cat $tmpfile | head -n3 | grep -- "-----BEGIN PGP PUBLIC KEY BLOCK-----" ; then
            # This is a GnuPG extension to OpenPGP
            cat $tmpfile | gpg --dearmor >$tmpfile
        fi
160
        sudocmd apt-key add $tmpfile
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#
#        if [ ! -f /etc/apt/trusted.gpg.d/$name.gpg ]; then
#                epm tool eget -q -O /etc/apt/trusted.gpg.d/$name.gpg https://example.com/$name.gpg > /dev/null
#                chmod 0644 /etc/apt/trusted.gpg.d/$name.gpg
#        fi

        return
    fi
    sudocmd apt-key adv --keyserver "$url" --recv "$fingerprint"
}


epm_addkey()
{

176
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ -z "$1" ] ; then
177
    echo "Usage: $ epm repo addkey [name] [url] [fingerprint/gpgkey] [comment/name]"
178 179 180
    return
fi

181 182 183
# initialize here
remove_on_exit

184 185 186 187 188
case $BASEDISTRNAME in
    "alt")
        __epm_addkey_altlinux "$@"
        return
        ;;
189 190 191 192
    "alpine")
        __epm_addkey_alpine "$@"
        return
        ;;
193 194 195 196 197 198
esac

case $PMTYPE in
    apt-dpkg)
        __epm_addkey_deb "$@"
        ;;
199 200 201
    dnf-*|yum-*)
        __epm_addkey_dnf "$@"
        ;;
202 203 204 205
esac

}