summaryrefslogtreecommitdiffstats
path: root/bin/dnssec-sign.sh
blob: 3182cebc2bc4680f638a82b413a238d36e918a60 (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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/bash



function prep {
    ###
    # User variables, you may edit these variables
    ###
    SOURCEDIR="/home/casper/park-admin/playbooks-ansible/roles/dnsserver/files"

    if [ ! -z "$WORKDIR" ]
    then
        echo -e "$OK working directory is $WORKDIR"
    else
        WORKDIR="roles/dnsserver/files/dnssec"  # you may edit this
    fi

    if [ ! -z "$TARGETDIR" ]
    then
        echo -e "$OK target directory is $TARGETDIR"
    else
        TARGETDIR="/home/casper/park-admin/playbooks-ansible/roles/dnsserver/files/signatures"  # you may edit this
    fi
    ###
    # Stop editing, it is ready
    ###

    # sortie formatée des logs
    OK="\e[0m[  \e[92mOK\e[0m  ]"
    ERROR="\e[0m[  \e[91mERROR\e[0m  ]"
    INFO="\e[0m[  \e[93mINFO\e[0m  ]"

    if [ -z "$DOMAIN" ]
    then
        echo -e "$ERROR there is no domain to sign"
        exit 25
    fi


    if [ ! -z "$SUB" ]
    then
        SUBDOMAIN=""
        for i in $SUB
        do
            SUBDOMAIN="$SUBDOMAIN ${i}.$DOMAIN"
        done
    else
        SUBDOMAIN=""
        echo -e "$OK there is no subdomain to sign"
    fi


    if [ -e $WORKDIR ]
    then
        echo -e "$OK testing root directory"
    else
        echo -e "$ERROR root directory is not reacheable, use -d option"
        exit 1
    fi


    if [ -e $TARGETDIR ]
    then
        echo -e "$OK testing target directory"
    else
        echo -e "$ERROR target directory is not reacheable, use -t option"
        exit 1
    fi


    # vérifier si la commande est installée
    if ( rpm -q bind-dnssec-utils >/dev/null )
    then
        echo -e "$OK all dependancies are installed"
    else
        echo -e "$ERROR dependancies are missing"
        exit 1
    fi
}



function genkey {
    # GENKEY
    if [[ $GENKEYLIMIT == 1 ]]
    then
        DIRLIST="$SUBDOMAIN"
    else
        DIRLIST="$DOMAIN $SUBDOMAIN"
    fi

    # générer les clés
    pushd $WORKDIR/$DOMAIN/ >/dev/null
    VERSION=$(date +%Y%m%d%H%M%S)

    for i in $DIRLIST
    do

        pushd $i >/dev/null

        mkdir $VERSION
        pushd $VERSION >/dev/null

        # générer une clé Zone Signing Key (ZSK)
        echo -e "$INFO creating new Zone Signing Key ZSK..."
        dnssec-keygen -a ED25519 -n ZONE $i

        # générer une clé Key Signing Key (KSK)
        echo -e "$INFO creating new Key Signing Key KSK..."
        dnssec-keygen -f KSK -a ED25519 -n ZONE $i
        popd >/dev/null
        popd >/dev/null

    done
    popd >/dev/null
}



function sign {
    # SIGN
    # préparer les fichiers pour signature :
    # ajout des DNSKEY records dans toutes les zones
    pushd $WORKDIR/$DOMAIN/ >/dev/null
    pushd $DOMAIN >/dev/null
    if ( ls |grep 20 >/dev/null )
    then
        VERSION=$(ls |grep 20 |tail -n 1)
        echo -e "$OK dns keys has been found for signature"
    else
        echo -e "$ERROR dns keys has not been found for signature"
        exit 2
    fi
    popd >/dev/null

    for i in $DOMAIN $SUBDOMAIN
    do

        ZONEFILE="${i}.zone"
        TMPZONEFILE="${i}.zone.edited"
        pushd $i >/dev/null

        cp -f $ZONEFILE $TMPZONEFILE

        SERIAL=$(/usr/sbin/named-checkzone $i $ZONEFILE | grep -E -ho '[0-9]{10}')
        NEWSERIAL=$((SERIAL + 1))
        echo -e "$OK new serial is: $NEWSERIAL"
        VERSION=$(ls |grep 20 |tail -n 1)

        # mise à jour du serial
        echo -e "$INFO updating serial..."
        sed -i 's/'$SERIAL'/'$NEWSERIAL'/' $TMPZONEFILE
        cp -f $TMPZONEFILE $SOURCEDIR/$ZONEFILE

        # modification de la zone DNS
        for key in `ls $VERSION/K${i}*.key`
        do
            echo -e "$INFO adding DNSKEY records..."
            echo "\$INCLUDE $key" >> $TMPZONEFILE
        done


        popd >/dev/null

    done


    # signer les sous-domaines
    for i in $SUBDOMAIN
    do

        ZONEFILE="${i}.zone"
        TMPZONEFILE="${i}.zone.edited"
        pushd $i >/dev/null

        VERSION=$(ls |grep 20 |tail -n 1)
        echo -e "$INFO making signature of DNS records..."
        if ( dnssec-signzone -d $VERSION -K $VERSION -N INCREMENT -e +2851200 -o $i -t $TMPZONEFILE )
        then
            echo -e "$OK signature done for $i"
        else
            echo -e "$ERROR cannot make signature for $i"
            exit 3
        fi

        if ( mv ${TMPZONEFILE}.signed $TARGETDIR/${ZONEFILE}.signed )
        then
            echo -e "$OK zone files has moved to the current directory"
        else
            echo -e "$ERROR zone files has not moved to the current directory"
        fi
        popd >/dev/null

    done


    # préparer les fichiers pour signature :
    # ajout des DS records dans la zone principale
    for i in $DOMAIN
    do

        ZONEFILE="${i}.zone"
        TMPZONEFILE="${i}.zone.edited"
        pushd $i >/dev/null

        for j in $SUBDOMAIN
        do

            pushd ../$j/ >/dev/null
            VERSION=$(ls |grep 20)
            for m in $VERSION
            do
                DSSET=$(ls $m/dsset-*)

                echo -e "$INFO adding DS records..."
                echo "\$INCLUDE ../$j/$DSSET" >> ../$i/$TMPZONEFILE
            done
            popd >/dev/null
        done
        popd >/dev/null

    done


    # signer les domaines principaux
    for i in $DOMAIN
    do

        ZONEFILE="${i}.zone"
        TMPZONEFILE="${i}.zone.edited"
        pushd $i >/dev/null

        VERSION=$(ls |grep 20 |tail -n 1)
        echo -e "$INFO making signature of DNS records..."
        if ( dnssec-signzone -d $VERSION -K $VERSION -N INCREMENT -e +2851200 -o $i -t $TMPZONEFILE )
        then
            echo -e "$OK signature done for $i"
        else
            echo -e "$ERROR cannot make signature for $i"
            exit 3
        fi        

        if ( mv ${TMPZONEFILE}.signed $TARGETDIR/${ZONEFILE}.signed )
        then
            echo -e "$OK zone files has moved to the current directory"
        else
            echo -e "$ERROR zone files has not moved to the current directory"
        fi

        # print DS records for the Registrar
        KEYTAG1=$(head -1 $VERSION/dsset-* |awk '{ print $4 }')
        ALGO1=$(head -1 $VERSION/dsset-* |awk '{ print $5 }')
        TDIGEST1=$(head -1 $VERSION/dsset-* |awk '{ print $6 }')
        DIGEST1=$(head -1 $VERSION/dsset-* |awk '{ print $7 }')

        KEYTAG2=$(tail -1 $VERSION/dsset-* |awk '{ print $4 }')
        ALGO2=$(tail -1 $VERSION/dsset-* |awk '{ print $5 }')
        TDIGEST2=$(tail -1 $VERSION/dsset-* |awk '{ print $6 }')
        DIGEST2=$(tail -1 $VERSION/dsset-* |awk '{ print $7 $8 }')

        echo -e "$OK Registrar informations for $i"
        echo -e "$OK DS record #1"
        echo -e "$OK DS record KEY TAG: $KEYTAG1"
        echo -e "$OK DS record ALGO: $ALGO1"
        echo -e "$OK DS record DIGEST TYPE: $TDIGEST1"
        echo -e "$OK DS record DIGEST: $DIGEST1"

        echo -e "$OK DS record #2"
        echo -e "$OK DS record KEY TAG: $KEYTAG2"
        echo -e "$OK DS record ALGO: $ALGO2"
        echo -e "$OK DS record DIGEST TYPE: $TDIGEST2"
        echo -e "$OK DS record DIGEST: $DIGEST2"
        popd >/dev/null

    done
    popd >/dev/null
}



function help {
    echo "Usage:"
    echo "    $(basename $0)"
    echo "    $(basename $0) -g|--genkey [-l|--limit] [-d <working directory>] [-t <target directory>] <main domain> [subdomain list...]"
    echo "    $(basename $0) -s|--sign-zone [-d <working directory>] [-t <target directory>] <main domain> [subdomain list...]"
    echo ""
    echo "Options:"
    echo "    -g:    Generate new keys in a versonned subdirectory"
    echo "    -l:    Generate new keys for subdomains only"
    echo "    -s:    Sign zone files using existing keys in a versonned subdirectory"
    echo "    -d directory:"
    echo "           Working directory for generating new keys, key versionning, storage,"
    echo "           and look for original zone files"
    echo "    -t directory:"
    echo "           Target directory to push signed zone files only"
    echo ""
    echo "Arguments:"
    echo "    main domain: example.net or super-domain.org"
    echo "    subdomain list: intranet proxy home cdn mirror etc..."
    exit 25
}


###
# User command-line options
###

# If no argument provided by command-line, use default
if [[ $# -eq 0 ]]
then
    help
fi

while [[ $# -gt 0 ]]
do
    case "$1" in
        -d)
            case $2 in
                ""|-*)
                    echo -e "$ERROR you must specify path directory"
                    exit 25 ;;
                *)
                    WORKDIR="$2"
                    shift 2 ;;
            esac
            ;;
        -t)
            case $2 in
                ""|-*)
                    echo -e "$ERROR you must specify path directory"
                    exit 25 ;;
                *)
                    TARGETDIR="$2"
                    shift 2 ;;
            esac
            ;;
        -g|--genkey)
            GENKEY=1
            shift
            ;;
        -l|--limit)
            GENKEYLIMIT=1
            shift
            ;;
        -s|--sign-zone)
            SIGN=1
            shift
            ;;
        *)
            DOMAIN="$1"
            shift
            while [[ $# -gt 0 ]]
            do
                case $1 in
                    -*)
                        echo -e "$ERROR parser: option is not expected here"
                        exit 25 ;;
                    "")
                        SUB=""
                        shift ;;
                    *)
                        SUB="$SUB $1"
                        shift ;;
                esac
            done
            ;;
    esac
done
###
# End
###


# main function
if [[ $GENKEY == 1 ]] && [[ $SIGN == 1 ]]
then
    prep
    genkey
    sign
elif [[ $GENKEY == 1 ]]
then
    prep
    genkey
elif [[ $SIGN == 1 ]]
then
    prep
    sign
else
    help
fi