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
|
#!/bin/bash
# NextCloudPi function library
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# More at ownyourbits.com
#
export NCPCFG=${NCPCFG:-/usr/local/etc/ncp.cfg}
export CFGDIR=/usr/local/etc/ncp-config.d
export BINDIR=/usr/local/bin/ncp
command -v jq &>/dev/null || {
apt-get update
apt-get install -y --no-install-recommends jq
}
[[ -f "$NCPCFG" ]] && {
NCVER=$( jq -r .nextcloud_version < "$NCPCFG")
PHPVER=$( jq -r .php_version < "$NCPCFG")
RELEASE=$(jq -r .release < "$NCPCFG")
}
function configure_app()
{
local ncp_app="$1"
local cfg_file="$CFGDIR/$ncp_app.cfg"
local backtitle="NextCloudPi installer configuration"
local ret=1
# checks
type dialog &>/dev/null || { echo "please, install dialog for interactive configuration"; return 1; }
[[ -f "$cfg_file" ]] || return 0;
local cfg="$( cat "$cfg_file" )"
local len="$(jq '.params | length' <<<"$cfg")"
[[ $len -eq 0 ]] && return
# read cfg parameters
local parameters=()
for (( i = 0 ; i < len ; i++ )); do
local var="$(jq -r ".params[$i].id" <<<"$cfg")"
local val="$(jq -r ".params[$i].value" <<<"$cfg")"
local vars+=("$var")
local vals+=("$val")
local idx=$((i+1))
parameters+=("$var" "$idx" 1 "$val" "$idx" 15 60 120)
done
# dialog
local DIALOG_OK=0
local DIALOG_CANCEL=1
local DIALOG_ERROR=254
local DIALOG_ESC=255
local res=0
while test $res != 1 && test $res != 250; do
local value
value="$( dialog --ok-label "Start" \
--no-lines --backtitle "$backtitle" \
--form "Enter configuration for $ncp_app" \
20 70 0 "${parameters[@]}" \
3>&1 1>&2 2>&3 )"
res=$?
case $res in
$DIALOG_CANCEL)
break
;;
$DIALOG_OK)
while read val; do local ret_vals+=("$val"); done <<<"$value"
for (( i = 0 ; i < len ; i++ )); do
# check for invalid characters
grep -q '[\\&#;'"'"'`|*?~<>^"()[{}$&[:space:]]' <<< "${ret_vals[$i]}" && { echo "Invalid characters in field ${vars[$i]}"; return 1; }
cfg="$(jq ".params[$i].value = \"${ret_vals[$i]}\"" <<<"$cfg")"
done
ret=0
break
;;
$DIALOG_ERROR)
echo "ERROR!$value"
break
;;
$DIALOG_ESC)
echo "ESC pressed."
break
;;
*)
echo "Return code was $res"
break
;;
esac
done
echo "$cfg" > "$cfg_file"
printf '\033[2J' && tput cup 0 0 # clear screen, don't clear scroll, cursor on top
return $ret
}
function run_app()
{
local ncp_app=$1
local script="$(find "$BINDIR" -name $ncp_app.sh | head -1)"
[[ -f "$script" ]] || { echo "file $script not found"; return 1; }
run_app_unsafe "$script"
}
function find_app_param_num()
{
local script="${1?}"
local param_id="${2?}"
local ncp_app="$(basename "$script" .sh)"
local cfg_file="$CFGDIR/$ncp_app.cfg"
[[ -f "$cfg_file" ]] && {
local cfg="$( cat "$cfg_file" )"
local len="$(jq '.params | length' <<<"$cfg")"
for (( i = 0 ; i < len ; i++ )); do
local p_id="$(jq -r ".params[$i].id" <<<"$cfg")"
if [[ "${param_id}" == "${p_id}" ]]
then
echo "$i"
return 0
fi
done
}
return 1
}
find_app_param()
{
local script="${1?}"
local param_id="${2?}"
local ncp_app="$(basename "$script" .sh)"
local cfg_file="$CFGDIR/$ncp_app.cfg"
local p_num="$(find_app_param_num "$script" "$param_id")" || return 1
jq -r ".params[$p_num].value" < "$cfg_file"
}
# receives a script file, no security checks
function run_app_unsafe()
{
local script=$1
local ncp_app="$(basename "$script" .sh)"
local cfg_file="$CFGDIR/$ncp_app.cfg"
local log=/var/log/ncp.log
[[ -f "$script" ]] || { echo "file $script not found"; return 1; }
touch $log
chmod 640 $log
chown root:www-data $log
echo "Running $ncp_app"
echo "[ $ncp_app ] ($(date))" >> $log
# read script
unset configure
source "$script"
# read cfg parameters
[[ -f "$cfg_file" ]] && {
local cfg="$( cat "$cfg_file" )"
local len="$(jq '.params | length' <<<"$cfg")"
for (( i = 0 ; i < len ; i++ )); do
local var="$(jq -r ".params[$i].id" <<<"$cfg")"
local val="$(jq -r ".params[$i].value" <<<"$cfg")"
eval "$var=$val"
done
}
# run
configure 2>&1 | tee -a $log
local ret="${PIPESTATUS[0]}"
echo "" >> $log
[[ -f "$cfg_file" ]] && clear_password_fields "$cfg_file"
return "$ret"
}
function is_active_app()
{
local ncp_app=$1
local bin_dir=${2:-.}
local script="$bin_dir/$ncp_app.sh"
local cfg_file="$CFGDIR/$ncp_app.cfg"
[[ -f "$script" ]] || local script="$(find "$BINDIR" -name $ncp_app.sh | head -1)"
[[ -f "$script" ]] || { echo "file $script not found"; return 1; }
# function
unset is_active
source "$script"
[[ $( type -t is_active ) == function ]] && { is_active; return $?; }
# config
[[ -f "$cfg_file" ]] || return 1
local cfg="$( cat "$cfg_file" )"
[[ "$(jq -r ".params[0].id" <<<"$cfg")" == "ACTIVE" ]] && \
[[ "$(jq -r ".params[0].value" <<<"$cfg")" == "yes" ]] && \
return 0
}
# show an info box for a script if the INFO variable is set in the script
function info_app()
{
local ncp_app=$1
local cfg_file="$CFGDIR/$ncp_app.cfg"
local cfg="$( cat "$cfg_file" 2>/dev/null )"
local info=$( jq -r .info <<<"$cfg" )
local infotitle=$( jq -r .infotitle <<<"$cfg" )
[[ "$info" == "" ]] || [[ "$info" == "null" ]] && return 0
[[ "$infotitle" == "" ]] || [[ "$infotitle" == "null" ]] && infotitle="Info"
whiptail --yesno \
--backtitle "NextCloudPi configuration" \
--title "$infotitle" \
--yes-button "I understand" \
--no-button "Go back" \
"$info" 20 90
}
function install_app()
{
local ncp_app=$1
# $1 can be either an installed app name or an app script
if [[ -f "$ncp_app" ]]; then
local script="$ncp_app"
local ncp_app="$(basename "$script" .sh)"
else
local script="$(find "$BINDIR" -name $ncp_app.sh | head -1)"
fi
# do it
unset install
source "$script"
echo "Installing $ncp_app"
(install)
}
function cleanup_script()
{
local script=$1
unset cleanup
source "$script"
if [[ $( type -t cleanup ) == function ]]; then
cleanup
return $?
fi
return 0
}
function persistent_cfg()
{
local SRC="$1"
local DST="${2:-/data/etc/$( basename "$SRC" )}"
test -e /changelog.md && return # trick to disable in dev docker
mkdir -p "$( dirname "$DST" )"
test -e "$DST" || {
echo "Making $SRC persistent ..."
mv "$SRC" "$DST"
}
rm -rf "$SRC"
ln -s "$DST" "$SRC"
}
function is_more_recent_than()
{
local version_A="$1"
local version_B="$2"
local major_a=$( cut -d. -f1 <<<"$version_A" )
local minor_a=$( cut -d. -f2 <<<"$version_A" )
local patch_a=$( cut -d. -f3 <<<"$version_A" )
local major_b=$( cut -d. -f1 <<<"$version_B" )
local minor_b=$( cut -d. -f2 <<<"$version_B" )
local patch_b=$( cut -d. -f3 <<<"$version_B" )
# Compare version A with version B
# Return true if A is more recent than B
if [ "$major_b" -gt "$major_a" ]; then
return 1
elif [ "$major_b" -eq "$major_a" ] && [ "$minor_b" -gt "$minor_a" ]; then
return 1
elif [ "$major_b" -eq "$major_a" ] && [ "$minor_b" -eq "$minor_a" ] && [ "$patch_b" -ge "$patch_a" ]; then
return 1
fi
return 0
}
function check_distro()
{
local cfg="${1:-$NCPCFG}"
local supported=$(jq -r .release "$cfg")
grep -q "$supported" <(lsb_release -sc) && return 0
return 1
}
function clear_password_fields()
{
local cfg_file="$1"
local cfg="$(cat "$cfg_file")"
local len="$(jq '.params | length' <<<"$cfg")"
for (( i = 0 ; i < len ; i++ )); do
local type="$(jq -r ".params[$i].type" <<<"$cfg")"
local val="$( jq -r ".params[$i].value" <<<"$cfg")"
[[ "$type" == "password" ]] && val=""
cfg="$(jq -r ".params[$i].value=\"$val\"" <<<"$cfg")"
done
echo "$cfg" > "$cfg_file"
}
function apt_install()
{
apt-get update
apt-get install -y --no-install-recommends -o Dpkg::Options::=--force-confdef -o Dpkg::Options::="--force-confold" "$@"
}
function notify_admin()
{
local header="$1"
local msg="$2"
local admin=$(mysql -u root nextcloud -Nse "select uid from oc_group_user where gid='admin' limit 1;")
[[ "${admin}" == "" ]] && { echo "admin user not found" >&2; return 0; }
ncc notification:generate "${admin}" "${header}" -l "${msg}" || true
}
# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
|