epm-repo-addkey 4.39 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 28
    local tmpfile
    tmpfile=$(mktemp) || fatal
29
    remove_on_exit $tmpfile
30
    eget -O "$tmpfile" "$url" >/dev/null
31 32 33
    echo "$tmpfile"
}

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

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

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

    [ -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
69
    if [ -n "$url" ] ; then
70 71
        local tmpfile=$(__epm_get_file_from_url $url) || fatal
        sudocmd gpg --no-default-keyring --keyring /usr/lib/alt-gpgkeys/pubring.gpg --import $tmpfile
72
    fi
73 74 75
}


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

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

    [ -s $target ] && return

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

97 98 99

__epm_addkey_dnf()
{
100
    local name
101
    local url="$1"
102 103 104 105 106 107 108 109 110 111 112 113
    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"
114 115 116 117 118 119

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

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

120 121
    local tmpfile
    tmpfile=$(mktemp) || fatal
122 123 124 125 126 127 128 129 130 131 132
    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
133 134 135 136 137
}


__epm_addkey_deb()
{
138
    local name
139
    local url="$1"
140 141 142 143 144 145 146 147 148 149 150 151
    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"
152

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

    if [ -z "$fingerprint" ] ; then
157
        local tmpfile=$(__epm_get_file_from_url $url) || fatal
158 159
        if cat $tmpfile | head -n3 | grep -- "-----BEGIN PGP PUBLIC KEY BLOCK-----" ; then
            # This is a GnuPG extension to OpenPGP
160
            cat $tmpfile | a= gpg --dearmor >$tmpfile
161
        fi
162
        sudocmd apt-key add $tmpfile
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
#
#        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()
{

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

183 184 185
# initialize here
remove_on_exit

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

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

}