aboutsummaryrefslogtreecommitdiff
path: root/install.sh
blob: 00d1089a3f29f0899df1fefd392b195c8641fe96 (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
# !/bin/sh

set -o errexit
#set -o pipefail
#set -o nounset
#set -o xtrace

BOLD='\033[01m'
UNDL='\033[04m'
GREEN='\033[32m'
RED='\033[31m'
STYLE_END='\033[0m'
command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
self_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
self_fullpath="$self_dir/$0"
emailre=".\+@.\+\\..\+"

### VARIABLES TO EDIT ###
HAS_MAILSERVER=0 # 0 is the default, it'll use an external smtp server (your gmail
# account /ISP subscriber mail address for example). Change to 1 if you have a local mail
# server.
YOUR_EMAIL=0
GPG_XLONG='0x0000000000000000' # running 'gpg -k --keyid-format 0xlong yourmail@example.com' will
# help you there.
SMTP=0
SMTP_P=465
MAIL_PW=0
SCRIPT="$self_dir/gpigeon.cgi"
GPG_DATA_DIR='/usr/share/www-data'
SCRIPT_USER='www-data'
SCRIPT_GROUP="$SCRIPT_USER"
ROOT_DIR='/var/www/gpigeon'
CGI_DIR="$ROOT_DIR/cgi-bin"
LINKS_DIR="$CGI_DIR/l"
APP_PW=0
### END VARIABLES TO EDIT ###

self_abort() {
    printf "\n${BOLD}${RED}Aborting...${STYLE_END}\n"
    exit 1
}

list_setupvars() {
    printf "\nThis is what has been configured so far:"
    printf "\nGpigeon root directory: %s" "$ROOT_DIR"
    printf "\nCGI script directory: %s" "$CGI_DIR"
    printf "\nGpigeon ownership: %s:%s" "$SCRIPT_USER" "$SCRIPT_GROUP"
    printf "\nGpigeon links folder: %s" "$LINKS_DIR"
    printf "\nGpigeon GPG homedir: %s" "$GPG_DATA_DIR"
    printf "\nGPG public key id: %s" "$GPG_XLONG"
    printf "\nLocal mailserver method: "   
    if [ $HAS_MAILSERVER -eq 0 ]; then
        printf "${RED}no${STYLE_END}\nMail address: %s\nMail password: %s\nExternal SMTP server and port: %s:%s\n" "$YOUR_EMAIL" "$MAIL_PW" "$SMTP" "$SMTP_P"
    else
        printf "${GREEN}yes${STYLE_END}\n"
    fi
    printf "App password: %s\n" "$APP_PW"
    printf "\n"

    printf "\nPress any key to continue (CTRL+C to abort)..."
    read
}

__check_setupvars() {
    if ! $GPG -k "$GPG_HEX" 2>/dev/null >/dev/null; then
        printf "No GPG key pair are related to your email. Create one and launch
    this script again."
        self_abort
    fi

    if ! id $SCRIPT_USER; then
        printf "\nThe user ${BOLD}$SCRIPT_USER${STYLE_END} doesn't exist. Edit
    ${UNDL}$self_fullpath${STYLE_END} and search
    for the ${BOLD}SCRIPT_USER${STYLE_END} variable.\n\n"
        self_abort
    fi

    if ! getent group $SCRIPT_GROUP; then
        echo "The ${BOLD}$SCRIPT_GROUP${END_STYLE} group doesn't exist. Edit $self_fullpath then modify
    the SCRIPT_GROUP variable value."
        self_abort
    fi

    if [ "$APP_PW" -eq "0" ] || [ -z $APP_PW ] ; then
        echo "Please edit $0 with a text editor ($EDITOR I guess?) and change the
        APP_PW variable."
        self_abort
    else
        PW_LENGTH=$(echo $APP_PW | wc -L)
        if [ $PW_LENGTH -le 8 ]; then
            echo "Your password is too short, make it lengthier than 8 characters."
            self_abort
        fi
    fi

    # prevent obscure errors with q{$APP_PW} in perl script
    APP_PW_SANE="$(echo $APP_PW | sed s/{/\\\\{/g | sed s/}/\\\\}/g)"

    # password checksum'd so no plaintext
    HASHED_PASSWORD=$(printf "%s" "$APP_PW" | sha256sum)

    if ! echo "$YOUR_EMAIL" | grep "$emailre" >/dev/null; then
        printf "\nYour email address is not a valid one. Edit $self_fullpath and
        modify the value of the YOUR_EMAIL variable."
    fi
}

setup_gpigeon() {
    apt install perl gcc make cpanminus libnet-ssleay-perl || self_abort
    cpanm Digest::SHA Email::Valid String::Random HTML::Entities CGI CGI::Carp Net::SMTP Net::SMTPS GPG || ( printf "\nInstallation of dependencies failed\n" && self_abort )

    cp $self_dir/gpigeon-template.cgi $SCRIPT
    sed "s/password_hash_goes_here/$HASHED_PASSWORD/g" -i $SCRIPT
    sed "s/your_mail_address_goes_here/$YOUR_EMAIL/g" -i $SCRIPT
    sed "s/your_mail_address_password_goes_here/$YOUR_EMAIL_PW/g" -i $SCRIPT
    sed "s/smtp_domain_goes_here/$SMTP/g" -i $SCRIPT
    sed "s/smtp_port_goes_here/$SMTP_P/g" -i $SCRIPT
    sed "s/gpgid_goes_here/$gpgidlong/g" -i $SCRIPT

    printf "\nCreating static files directory at $ROOT_DIR"
    mkdir -p "$ROOT_DIR" || self_abort
    printf "\nCopying static files to $ROOT_DIR ..."
    cp -r $self_dir/{merci/,index.html,gpigeon.css,favicon.ico} $ROOT_DIR || self_abort

    printf "\n\nCreating script and links directory at $CGI_DIR ..."
    mkdir -p {"$CGI_DIR","$LINKS_DIR"} || self_abort

    printf "\nCopying personalized gpigeon.cgi script to $CGI_DIR ..."
    cp $SCRIPT $CGI_DIR/ || self_abort

    printf "\nSetting ownership as $SCRIPT_USER:$SCRIPT_GROUP for directory $CGI_DIR ..."
    chown $SCRIPT_GROUP:$SCRIPT_USER $CGI_DIR || self_abort

    printf "\nSetting ownership as $SCRIPT_USER:$SCRIPT_GROUP for static directory $ROOT_DIR ..."
    chown $SCRIPT_GROUP:$SCRIPT_USER $ROOT_DIR || self_abort

    printf "\nSetting up the GPG directory for the script ..."
    if [[ -z $GPG_DATA_DIR ]]; then
        mkdir -p /usr/share/www-data/.gnupg
        cp -r ~/.gnupg /usr/share/www-data/
        chown $SCRIPT_USER:$SCRIPT_GROUP /usr/share/www-data/.gnupg
        chmod 600 /usr/share/www-data/.gnupg
    else
        mkdir -p $GPG_DATA_DIR
        cp -r ~/.gnupg /usr/share/www-data/
        chown $SCRIPT_USER:$SCRIPT_GROUP $GPG_DATA_DIR
        chmod 600 $GPG_DATA_DIR
    fi
    printf "${BOLD}${GREEN}Congrats, we are done!${END_STYLE} You should now manually configure your web server to execute the CGI scripts in the $CGI_DIR folder. Manuals and
    official websites for these softwares should help you.\n\n"
    exit 0
}

_usage_(){
    printf "\n  -c    checks variables"
    printf "\n  -l    lists variables"
    printf "\n  -y    checks variables and attempts to install gpigeon"
    printf "\n  -s    install gpigeon"
    printf "\n  -h    print this help"
    printf "\n\n"
}

while getopts "clysh" o; do
   case "${o}" in
       c) __check_setupvars && exit 0;;
       l) list_setupvars && exit 0;;
       y) __check_vars && setup_gpigeon;;
       s) setup_gpigeon;;
       h) _usage_;;
       #i) interactive_setup;;
       *) __check_vars && list_setupvars && setup_gpigeon
   esac
done