~ubuntu-branches/ubuntu/trusty/elida/trusty

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash

# Copyright 2007-2009 Anibal Monsalve Salazar <anibal@debian.org>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 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 General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

set -u
set -e

. /etc/elida/elida.conf
. $FUNCTIONS

mail_in()
{
    local line
    local tmpline
    local header
    local value

    from=
    message_id=
    subject=
    reply_to=

    while IFS= read -r tmpline
    do
        if [[ "$tmpline" =~ ^[[:space:]] ]]
        then
            line="$line$tmpline"
        else
            if [ -n "$line" ]
            then
                IFS=' ' read -r header value <<< "$line"
                case "$header" in
                    [Ff][Rr][Oo][Mm]: )
                        from=${value//\"/\\\"} #"
                        ;;
                    [Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]: )
                        message_id=${value//\"/\\\"} #"
                        ;;
                    [Ss][Uu][Bb][Jj][Ee][Cc][Tt]: )
                        subject=${value//\"/\\\"} #"
                        ;;
                    [Rr][Ee][Pp][Ll][Yy]-[Tt][Oo]: )
                        reply_to=${value//\"/\\\"} #"
                        ;;
                esac
            fi
            line=$tmpline
            if [ -z "$line" ]
            then
                break
            fi
        fi
    done

    return 0
}

$LOGGER -p syslog.info $BASENAME: starting

set +e
makedirs
set -e
mail_in

regex="^.*tp://.+\.dsc$"
if [[ "$subject" =~ $regex ]]
then
    dsc=${subject##*/}
    url=${subject%/*}
    pkg=${dsc%_*}
    pdv=${dsc%\.dsc}
    log=$ELIDAHOME/source/$pkg/${pdv}_$ARCH.source.log
    sig=$(mktemp $TMP-XXXXXX)
    md5=$(mktemp $TMP-XXXXXX)
    dif=$(mktemp $TMP-XXXXXX)
    tmp=$(mktemp $TMP-XXXXXX)
    set +e
    makedir $ELIDAHOME/source/$pkg
    set -e
    if [ -e $log.bz2 ]
    then
        bunzip2 -f $log.bz2
    fi
    if [ -e $log ]
    then
        sed -e '/^Format: 1.0$/,$ d' $log > $tmp
        rm -f $log
        mv $tmp $log
    else
        touch $log
    fi
    chmod 664 $log
    date -R >> $log
    echo >> $log
    set +e
    wget -nH -N -P $ELIDAHOME/source/$pkg/ $subject >> $log 2>&1
    set -e
    if [ ! -f $ELIDAHOME/source/$pkg/$dsc ]
    then
        $LOGGER -p syslog.crit $BASENAME: $dsc download error
        (echo -e "Subject: Re: $subject\nTo: $from\nFrom: $FROM\nBcc: $BCC\nIn-Reply-To: $message_id\nUser-Agent: elida/$ELIDAVERSION\n"; cat $log) | $SENDMAIL -t
        exit 0
    fi
    chmod 664 $ELIDAHOME/source/$pkg/$dsc
    set +e
    su - $USERNAME -c "gpg --homedir $GPGHOMEDIR --no-permission-warning" < $ELIDAHOME/source/$pkg/$dsc > $tmp 2> $sig
    rc=$(grep -c "^gpg: Good signature from " $sig)
    set -e
    if [ $rc -ne 1 ]
    then
        cat $tmp $sig >> $log
        echo >> $log
        $LOGGER -p syslog.crit $BASENAME: gpg error
        (echo -e "Subject: Re: $subject\nTo: $from\nFrom: $FROM\nBcc: $BCC\nIn-Reply-To: $message_id\nUser-Agent: elida/$ELIDAVERSION\n"; cat $tmp $sig) | $SENDMAIL -t
        exit 0
    fi

    files=$(grep -E "^ [0-9a-f]{32} [0-9]+ .*\.(diff|tar)\.gz$" $tmp | sed "s/.* //")
    for file in $files
    do
        if [ $file != ${dsc%-*}.orig.tar.gz ]
        then
            set +e
            wget -nH -N -P $ELIDAHOME/source/$pkg/ $url/$file >> $log 2>&1
            set -e
        else
            if [ ! -e $ELIDAHOME/source/$pkg/$file ]
            then
                if [[ $pkg =~ ^lib ]]
                then
                    sdir=${pkg:0:4}
                else
                    sdir=${pkg:0:1}
                fi
                for mirror in $MIRRORS
                do
                    if [ ! -e $ELIDAHOME/source/$pkg/$file ]
                    then
                        set +e
                        wget -nH -N -P $ELIDAHOME/source/$pkg/ $mirror/pool/main/$sdir/$pkg/$file >> $log 2>&1
                        set -e
                    fi
                done
                if [ ! -e $ELIDAHOME/source/$pkg/$file ]
                then
                    set +e
                    wget -nH -N -P $ELIDAHOME/source/$pkg/ $url/$file >> $log 2>&1
                    set -e
                fi
            fi
        fi
        chmod 664 $ELIDAHOME/source/$pkg/$file
    done
    cat $tmp $sig >> $log
    echo -e '\nMD5s:' >> $log
    (cd $ELIDAHOME/source/$pkg; md5sum $dsc $files) >> $log
    echo >> $log
    sed -e '1,/^Files:/ d' $tmp | cut -d\  -f2,4 | sed -e 's/ /  /' > $md5
    cd $ELIDAHOME/source/$pkg
    set +e
    md5sum --check $md5 >> $log 2>&1
    rc=$?
    set -e
    if [ ! $rc ]
    then
        $LOGGER -p syslog.crit $BASENAME: md5sum error rc=$rc
        exit $rc
    fi

    cat > $ELIDAHOME/pending/$pdv << FIN
from="$from"
message_id="$message_id"
subject="$subject"
reply_to="$reply_to"
FIN
    mv $ELIDAHOME/pending/$pdv $ELIDAHOME/ready/$pdv

    echo >> $log
    set +e
    apt-get --download-only source $pkg >> $log
    set -e

    if [ -e $pdv.diff.gz ]
    then
        echo >> $log
        echo "diffstat $pdv.diff.gz" >> $log
        diffstat $pdv.diff.gz >> $log
    fi

    diffs=$(ls -1rt *.diff.gz | tail -2)
    rc=$(wc -w <<< $diffs)
    if [ $rc -eq 2 ]
    then
        set +e
        interdiff -zp1 $diffs > $dif 2>&1
        set -e
        echo >> $log
        echo interdiff -zp1 $diffs >> $log
        diffstat < $dif >> $log
        echo >> $log
        cat $dif >> $log
    fi
    bzip2 -f -9 $log
    log="$log.bz2"

    rm -f $tmp
    mpack -s "Re: $subject" -o $tmp $log
    (echo -e "To: $from\nFrom: $FROM\nBcc: $BCC\nIn-Reply-To: $message_id\nUser-Agent: elida/$ELIDAVERSION"; cat $tmp) | $SENDMAIL -t
fi

exit 0