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
|
#! /bin/sh
#
# parse a yaml file and turn it into a bip config
#
set -e
export HOME=$SNAP_APP_DATA_PATH
# function to turn yaml into variables
parse_yaml()
{
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_-]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
# get the piped input and store it in a tmpfile
while IFS= read -r LINE; do
if [ ! "$(id -u)" = "0" ]; then
echo -n "permission denied (try sudo)"
exit 1
fi
printf "%s\n" "$LINE" >>$SNAP_APP_DATA_PATH/.tmp.yaml
done
# if we have permissions, write the different configs
if [ "$(id -u)" = "0" ]; then
touch $SNAP_APP_DATA_PATH/.tmp.yaml
[ -e $SNAP_APP_DATA_PATH/config.yaml ] || \
cp $SNAP_APP_PATH/default.yaml $SNAP_APP_DATA_PATH/config.yaml
# put a header in place
sed '/^user {.*/,/^};/c\' $SNAP_APP_PATH/etc/bip.conf \
>$SNAP_APP_DATA_PATH/bip.conf.head 2>&1
# get all the vars from yaml
eval $(parse_yaml $SNAP_APP_DATA_PATH/config.yaml)
eval $(parse_yaml $SNAP_APP_DATA_PATH/.tmp.yaml)
if [ ! -e "$SNAP_APP_DATA_PATH/bip.pem" ] || \
grep -q sslcert $SNAP_APP_DATA_PATH/.tmp.yaml; then
usr/bin/openssl req -new -newkey rsa:4096 -nodes -x509 \
-subj "/C=$config_ircproxy_sslcert_country/ST=\
$config_ircproxy_sslcert_state/L=\
$config_ircproxy_sslcert_locality/O=\
$config_ircproxy_sslcert_org/CN=\
$config_ircproxy_sslcert_domain" \
-keyout $SNAP_APP_DATA_PATH/bip.pem \
-out $SNAP_APP_DATA_PATH/bip.pem >/dev/null 2>&1
chmod 600 $SNAP_APP_DATA_PATH/bip.pem
fi
# put a new config.yaml in place
cat << EOF >$SNAP_APP_DATA_PATH/config.yaml
config:
ircproxy:
port: $config_ircproxy_port
networks: $config_ircproxy_networks
clientssl: $config_ircproxy_clientssl
sslcert:
country: $config_ircproxy_sslcert_country
state: $config_ircproxy_sslcert_state
locality: $config_ircproxy_sslcert_locality
org: $config_ircproxy_sslcert_org
domain: $config_ircproxy_sslcert_domain
user:
nick: $config_ircproxy_user_nick
user: $config_ircproxy_user_user
realname: $config_ircproxy_user_realname
password: $config_ircproxy_user_password
connections: $config_ircproxy_user_connections
EOF
# create the actual bip configuration
sed "s:^client_side_ssl.*:client_side_ssl = $config_ircproxy_clientssl;:" \
$SNAP_APP_DATA_PATH/bip.conf.head >$SNAP_APP_DATA_PATH/bip.conf
sed "s:^port.*:port = $config_ircproxy_port;:" \
$SNAP_APP_DATA_PATH/bip.conf >$SNAP_APP_DATA_PATH/bip.conf.tmp
mv $SNAP_APP_DATA_PATH/bip.conf.tmp $SNAP_APP_DATA_PATH/bip.conf
if [ ! "$config_ircproxy_networks" = "[]" ]; then
echo $config_ircproxy_networks| \
sed "s/\][ ]*,[ ]*\[/\\n/g;s/\(\]\)//g;s/\(\[\)//g;s/\x27//g;s/,/\t/g"| \
while read -r line; do
name=$(echo $line|cut -d' ' -f1)
host=$(echo $line|cut -d' ' -f2)
port=$(echo $line|cut -d' ' -f3)
echo "network {" >>$SNAP_APP_DATA_PATH/bip.conf
echo " name = \"$name\";" >>$SNAP_APP_DATA_PATH/bip.conf
echo " server { host = \"$host\"; port = $port; };" >>$SNAP_APP_DATA_PATH/bip.conf
echo "};" >>$SNAP_APP_DATA_PATH/bip.conf
done
fi
cat << EOF >>$SNAP_APP_DATA_PATH/bip.conf
user {
name = "$config_ircproxy_user_nick";
password = "$config_ircproxy_user_password";
ssl_check_mode = "none";
default_nick = "$config_ircproxy_user_nick";
default_user = "$config_ircproxy_user_user";
default_realname = "$config_ircproxy_user_realname";
EOF
if [ ! "$config_ircproxy_user_connections" = "[]" ]; then
echo $config_ircproxy_user_connections| \
sed "s/\][ ]*,[ ]*\[/\\n/g;s/\(\]\)//g;s/\(\[\)//g;s/\x27//g;s/,/\t/g"| \
while read -r line; do
network=$(echo $line|cut -d' ' -f1)
channels=$(echo $line|sed "s/^$network //;s/ /,/g")
echo " connection {" >>$SNAP_APP_DATA_PATH/bip.conf
echo " name = \"$network\";" >>$SNAP_APP_DATA_PATH/bip.conf
echo " network = \"$network\";" >>$SNAP_APP_DATA_PATH/bip.conf
echo " channel { name = \"$channels\";};" >>$SNAP_APP_DATA_PATH/bip.conf
echo " };" >>$SNAP_APP_DATA_PATH/bip.conf
done
fi
echo "};" >>$SNAP_APP_DATA_PATH/bip.conf
chmod 0640 $SNAP_APP_DATA_PATH/bip.conf
# flush the tmpfile
>$SNAP_APP_DATA_PATH/.tmp.yaml
fi
cat $SNAP_APP_DATA_PATH/config.yaml|sed 's/password: .*/password: \*\*\* hidden \*\*\*/'
|