← history for iron/libexec/iron/iron-service~
4d45db04iron/libexec/iron/iron-service~133 lines⬡ raw↓ download
  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
#! /bin/bash 
# Usage: iron service <service-name> <setup|create|delete>
# Summary: manage service/server certificate creation
# Help: This command groups commands used to setup config create delete a CA

APPNAME="iron"
CURRDIR=$(pwd)

RCDIR=$HOME/.$APPNAME
test -d $RCDIR || mkdir -p $RCDIR
CURRENT_TSTAMP=$(date '+%Y%m%d%H%M')

function service_create()
{
        service=$1
        extfile="${RCDIR}/${service}/conf/v3.ext"

	test -d $CERTPATH   || mkdir -p $CERTPATH
	openssl genrsa -out   $CERTPATH/server.key 2048 
	openssl req    -batch -new -key $CERTPATH/server.key -out $CERTPATH/server.csr -config $SERVER_CONFIG_FILE
	#OLD::openssl x509   -req   -days 365 -in $CERTPATH/server.csr -signkey $CERTPATH/server.key -out $CERTPATH/server.crt
# TODO: add v3 extension
# https://stackoverflow.com/questions/18233835/creating-an-x509-v3-user-certificate-by-signing-csr
# https://medium.com/@tbusser/creating-a-browser-trusted-self-signed-ssl-certificate-2709ce43fd15
	openssl x509   -req   -days 365 -CA $CAPATH/ca.crt -CAkey $CAPATH/ca.key -CAcreateserial -in $CERTPATH/server.csr \
		-signkey $CERTPATH/server.key -out $CERTPATH/server.crt -extfile ${extfile}
	#openssl ca -cert $CAPATH/ca.crt  -keyfile $CAPATH/ca.key -in $CERTPATH/server.csr -out $CERTPATH/server.crt -config /home/paolo/.iron/dev.lulli.net/conf/openssl-server.conf

}

function service_renew()
{
        service=$1
        extfile="${RCDIR}/${service}/conf/v3.ext"
	
	test -d $CERTPATH   || mkdir -p $CERTPATH
	openssl req    -batch -new -key $CERTPATH/server.key -out $CERTPATH/server.csr -config $SERVER_CONFIG_FILE
	#OLD::openssl x509   -req   -days 365 -in $CERTPATH/server.csr -signkey $CERTPATH/server.key -out $CERTPATH/server.crt
	openssl x509   -req   -days 365 -CA $CAPATH/ca.crt -CAkey $CAPATH/ca.key -CAcreateserial -in $CERTPATH/server.csr \
		-signkey $CERTPATH/server.key -out $CERTPATH/server.crt -extfile ${extfile}
	#openssl ca -cert $CAPATH/ca.crt  -keyfile $CAPATH/ca.key -in $CERTPATH/server.csr -out $CERTPATH/server.crt -config /home/paolo/.iron/dev.lulli.net/conf/openssl-server.conf

}

function service_home_setup_delete()
{
	DOMAIN=$1
	echo "About to DELETE cert path: [$RCDIR/${DOMAIN}] are you sure? y/n"
	read confirmation
	if [ "$confirmation" = "y" ]; then
		(rm -fr $RCDIR/${DOMAIN} ; rm -fr $RCFILE) && echo "CA DELETED"
	else
		echo "SKIPPING"
		exit -1
	fi
}

function service_home_setup_write()
{
	DOMAIN=$1
	test -d $RCDIR/${DOMAIN} && ( echo "Dir $RCDIR/${DOMAIN} exist, please delete before" && exit -1)
	test -d $RCDIR/${DOMAIN} || mkdir -p $RCDIR/${DOMAIN}
	test -d $RCDIR/${DOMAIN}/conf || mkdir -p $RCDIR/${DOMAIN}/conf
	echo "CERTPATH=${RCDIR}/${DOMAIN}/certs" >> ${RCFILE}
	echo "CAPATH=${RCDIR}/CA"  >> ${RCFILE}
	echo "SERVER_CONFIG_FILE=${RCDIR}/${DOMAIN}/conf/openssl-server.conf"  >> ${RCFILE}

	cat<<__EOF__ >$RCDIR/${DOMAIN}/conf/openssl-server.conf
RANDFILE               = $ENV::HOME/.rnd

[ req ]
default_bits           = 1024
default_keyfile        = keyfile.pem
distinguished_name     = req_distinguished_name
attributes             = req_attributes
prompt                 = no
#output_password        = bliblablu

[ req_distinguished_name ]
C                      = IT
ST                     = Italia
L                      = Roma
O                      = ${DOMAIN}
OU                     = ${DOMAIN}
CN                     = ${DOMAIN}
emailAddress           = info@${DOMAIN}

[ req_attributes ]
#challengePassword      = blablabla
__EOF__

	cat<<__EOF__ >$RCDIR/${DOMAIN}/conf/v3.ext
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = ${DOMAIN}
DNS.2 = www.${DOMAIN}
__EOF__
}

if [ "$#" = 2 ];then
	operation=$1
	service=$2
	RCFILE="$HOME/"."$APPNAME/service-${service}"".env"

	if [ "$operation" = "create" ]; then
		echo "BEFORE"
		test -f ${RCFILE} || ( service_home_setup_write $service; echo "Edit values in ${RCFILE}"; exit -1) 
		echo "AFTER"
		source ${RCFILE}
		service_create $service 
	fi 
	if [ "$operation" = "renew" ]; then
		echo "BEFORE"
		test -f ${RCFILE} || ( service_home_setup_write $service; echo "Edit values in ${RCFILE}"; exit -1) 
		echo "AFTER"
		source ${RCFILE}
		service_renew $service 
	fi 
	if [ "$operation" = "setup" ]; then
		test -f ${RCFILE} || ( service_home_setup_write $service; echo "Edit values in ${RCFILE}"; exit 0) 
	fi 
	if [ "$operation" = "delete" ]; then
		source ${RCFILE}
		service_home_setup_delete  $service
	fi 
else
	echo "Usage: \$iron service <setup|create|renew|delete> <service>"
	exit -1
fi