aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lets-ca.sh
blob: 36abbca5ee0371397438ccf700ebf70ce12eb06f (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
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
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
#############

# This script creates a key and csr, and uses acme-tiny to have the csr signed
# with lets encrypt.

# by Dennis Eriksen, dennis@eriksen.im, 2015-12-16

#############




################################################################################

# TODO

################################################################################

# Handling of getopts. If you trigger -b after -r, -b doesn't count. Also, you
# don't want to be able to trigger -s and -r, etc.

# There should probably be a file indicating which domains each cert is valid 
# for.. Like $domain/SAN, with an entry for each domain.


################################################################################

# Config 

################################################################################

readonly CERTDIR="/etc/letsencrypt/certs"
readonly CERTDEST="/etc/ssl/letsencrypt"
readonly OPENSSLCONF="/etc/letsencrypt/openssl.cnf"
readonly ACCOUNTKEY="/etc/letsencrypt/account.key"
readonly -a ARGS=("$@")

readonly LETSCAUSER="letsencrypt"
readonly LETSCAGROUP="letsencrypt"

readonly CERTKEYOWNER="root"
readonly CERTGROUP="ssl-cert"



readonly INTERMEDIATE="/etc/ssl/lets-encrypt-x3-cross-signed.pem"


readonly ACMETINY="/usr/local/sbin/acme_tiny.py"

readonly CHALLENGEDIR="/var/www/letsencrypt-challenges"

readonly NGINXCONFSUFFIX=""

BATCHMODE=FALSE

QUIET=FALSE

################################################################################

# Usage

################################################################################

usage() {
  echo "Yes. This is Usage. Hello."
}



################################################################################

# say

################################################################################

echo() {
  if [[ ! "$QUIET" == TRUE ]]; then
    builtin echo $1
  fi
}



################################################################################

# cmdline

################################################################################

cmdline() {

  local bflag=FALSE
  local dflag=FALSE
  local qflag=FALSE
  local rflag=FALSE
  local sflag=FALSE

  while getopts "xqhbr:s:d:" OPTION
  do
    case ${OPTION} in
      b)
        bflag=TRUE
        BATCHMODE=TRUE
        ;;
      h)
        usage
        exit 0
        ;;
      x)
        readonly DEBUG='-x'
        echo "-x was triggered"
        echo "\${ARGS{[@]} = ${ARGS[@]}"
        set -x
        ;;
      s)
        sflag=TRUE
        sign ${OPTARG}
        ;;
      d)
        dflag=TRUE
        deploy ${OPTARG}
        ;;
      q)
        QUIET=TRUE
        ;;
      r)
        rflag=TRUE
        req ${OPTARG} 
        ;;
      \?)
        usage && exit 1
        ;;
      :)
        usage && exit 1
        ;;
    esac
  done
  shift $((OPTIND-1))

  if [[ "$bflag" == TRUE ]] && [[ ! "$rflag" == TRUE ]]; then
    echo "-b is only used with -r."
    echo ""
    usage && exit 1
  fi

  return 0
}


################################################################################

# deploy

################################################################################

deploy() {
  local domain=""

  # Domains are provided separated by spaces.
  IFS=' ' read -r -a domain <<< $1

  if [[ ! ${#domain[@]} == 1 ]]; then
    echo "-d only takes one domain."
    exit 1
  elif [[ ! -d "$CERTDIR/${domain[0]}" ]]; then
    echo "\"${domain[0]}\" does not exist in $CERTDIR"
    exit 1
  elif [[ ! -f "$CERTDIR/${domain[0]}/${domain[0]}.key" ]]; then
    echo "${domain[0]}.key seems to be missing."
    exit 1
  elif [[ ! -f "$CERTDIR/${domain[0]}/${domain[0]}.crt" ]]; then
    echo "${domain[0]}.crt seems to be missing."
    echo "Are you sure you've signed the CSR?"
    exit 1
  fi

  if [[ ! -d "$CERTDEST" ]]; then
    mkdir -p $CERTDEST
    chgrp $CERTGROUP $CERTDEST
  fi


  # Lets's copy!
  echo "Copying ${domain[0]}.crt and ${domain[0]}.key to $CERTDEST"
  cp --preserve=all $CERTDIR/${domain[0]}/${domain[0]}.{crt,key} $CERTDEST

  # If the certificate is meant for nginx, we want to combine the cert and the
  # intermediary certificate.
  if [[ -f "/etc/nginx/sites-enabled/${domain[0]}$NGINXCONFSUFFIX" ]] || \
     [[ -f "$CERTDEST/${domain[0]}.pem" ]]; then
    echo "Detected nginx-config for ${domain[0]}, or existing pem-file."
    echo "Moving crt to pem, and appending intermediate certificate."
    mv $CERTDEST/${domain[0]}.crt $CERTDEST/${domain[0]}.pem
    cat $INTERMEDIATE >> $CERTDEST/${domain[0]}.pem
  fi

  echo ""
  echo "Deployment finished. You should probably reload your configuration."


  exit 0


}


################################################################################

# req

################################################################################

req() {
  local domain=""
  local i=0
  local batch=""

  # Domains are provided separated by spaces.
  IFS=' ' read -r -a domain <<< $1

  # Is batchmode triggered?
  [[ "$BATCHMODE" == TRUE ]] && batch="-batch"

  if [[ -z "$CERTDIR" ]] || [[ -z "${domain[0]}" ]]; then
    echo "\$CERTDIR and/or \${domain[0]} is empty. Something is wrong."
    exit 1
  fi

  if [[ ! -d $CERTDIR ]]; then
    mkdir -p $CERTDIR
    chmod 750 $CERTDIR
    chown $LETSCAUSER:$CERTGROUP $CERTDIR
  fi

  if [[ -d "$CERTDIR/${domain[0]}" ]]; then
    echo "The directory $CERTDIR/${domain[0]} already exists."
    echo "If you continue, it will be deleted."
    echo ""
    read -p "Press Y to continue, or any other button to abort." -n 1 -r

      if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
        echo "Okay then. Live long and prosper."
        exit 1
      else
        echo ""
        rm -r "$CERTDIR/${domain[0]}" && mkdir -p "$CERTDIR/${domain[0]}"
      fi

  else
    mkdir -p "$CERTDIR/${domain[0]}"
  fi

  # if there is only one domain
  if [[ ${#domain[@]} == 1 ]]; then
    echo "Only one domain - ${domain[0]}"
    openssl req -new -sha256 -nodes -out $CERTDIR/${domain[0]}/${domain[0]}.csr\
      -keyout $CERTDIR/${domain[0]}/${domain[0]}.key -config <(cat $OPENSSLCONF\
      | sed -r "s/REPLACE/${domain[0]}/") $batch

  # if there is more than one domain
  else
    echo "Several domains: "

    SANtext="[SAN]\nsubjectAltName="

    for SAN in "${domain[@]}"; do

      # Echo both to stdout and to SAN-file in cert-dir.
      echo "$SAN" | tee -a "$CERTDIR/${domain[0]}/SAN"

      if [[ ! "$SAN" == "${domain[0]}" ]]; then
        ((i+=1)) && [[ $i > 1 ]] && SANtext+=","
        SANtext+="DNS:$SAN"
      fi
    done
    echo "Common Name will be ${domain[0]}"
    echo $SANtext

    openssl req -new -sha256 -nodes -out $CERTDIR/${domain[0]}/${domain[0]}.csr\
      -keyout $CERTDIR/${domain[0]}/${domain[0]}.key -reqexts SAN -config <(cat\
      $OPENSSLCONF <(printf "$SANtext") | sed -r "s/REPLACE/${domain[0]}/") \
      $batch

  fi

  chown -R $LETSCAUSER:$CERTGROUP $CERTDIR/${domain[0]}
  chmod 750 $CERTDIR/${domain[0]}

  chown $CERTKEYOWNER:$CERTGROUP $CERTDIR/${domain[0]}/${domain[0]}.key
  chmod 440 $CERTDIR/${domain[0]}/${domain[0]}.{csr,key}

  exit 0

}



################################################################################

# sign

################################################################################

sign() {
  local domain=""

  # Domains are provided separated by spaces.
  IFS=' ' read -r -a domain <<< $1

  if [[ ! ${#domain[@]} == 1 ]]; then
    echo "-s only takes one domain."
    exit 1
  elif [[ ! -d "$CERTDIR/${domain[0]}" ]]; then
    echo "\"${domain[0]}\" does not exist in $CERTDIR"
    exit 1
  elif [[ ! -f "$CERTDIR/${domain[0]}/${domain[0]}.key" ]] || \
       [[ ! -f "$CERTDIR/${domain[0]}/${domain[0]}.csr" ]]; then
    echo "The CSR or KEY for ${domain[0]} seems to be missing."
    echo "Are you sure you've created the request?"
    exit 1
  fi

  if [[ ! "$QUIET" == TRUE ]]; then
    echo "Have you remembered to point the address"
    echo ""
    echo "${domain[0]}/.well-known/acme-challenge/"
    echo ""
    echo "to your challenges-directory? - $CHALLENGEDIR"
    echo "See https://github.com/diafygi/acme-tiny#step-3-make-your-website-host"\
         "-challenge-files for more info."
    echo ""
    echo "If the directory isn't available at the above address, "\
         "the signing will fail."
    echo ""

    read -p "Press Y to continue, or any other button to abort." -n 1 -r
    if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
      echo "Okay then. Live long and prosper."
      exit 1
    fi
  fi

  sudo -u $LETSCAUSER -H python $ACMETINY --account-key $ACCOUNTKEY \
    --csr $CERTDIR/${domain[0]}/${domain[0]}.csr --acme-dir $CHALLENGEDIR \
    > $CERTDIR/${domain[0]}/${domain[0]}.crt

  chown $LETSCAUSER:$CERTGROUP $CERTDIR/${domain[0]}/${domain[0]}.crt 


  exit 0

}



################################################################################

# main

################################################################################

main() {

cmdline ${ARGS[@]}

}



main
exit 0