-
Notifications
You must be signed in to change notification settings - Fork 5
/
bminfo
427 lines (371 loc) · 9.53 KB
/
bminfo
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/bash
#--clear - udalit vse poluchennye faily iz rabochego kataloga
#--getinfo - poluchit osnovnuju informaciju, API_BASE i konfig iz API_BASE
#--getconfigs - osnovnaya informacija, configs.json i konfig eip
#--providerkey - kluch provider'a
#--userkey - kluch user'a
#--copy - skopirovat v $OUTDIR
#--check - izmenit v $OUTDIR esli chto-to pomenyalos
PARAMS=""
ACT_CLEAR=""
ACT_GETINFO=""
ACT_GETCONFIG=""
ACT_PROVIDERKEY=""
ACT_USERKEY=""
ACT_COPY=""
ACT_CHECK=""
ACTSEL=0
OUTDIR="/home/user/bitmask/providers"
OUTDIR_CA="keys/ca"
OUTDIR_USER="keys/client"
WORKDIR="./data"
PROVIDER=""
API_URL=""
API_VER=""
API_BASE=""
JSONFILE="provider.json";
CONFIG_JSON="configs.json"
EIP_JSON=""
CACRTFILE="cacert.pem"
USERCERT="openvpn.pem"
HEADERDUMP="/tmp/headerdump.txt"
DL_TEMP="/tmp/httpfile"
HTTPSTATUS=""
declare -a FILE_LIST #massiv polu4ennyh failov dlya kopirovanija
existparam()
{
echo "$PARAMS"|grep -q -i -e $1
if [ "$?" -eq 0 ];then
return 1
else
return 0
fi
}
print_help()
{
echo "Use $1 <-p> <provider> [KEYS]"
echo "or"
echo "$1 --clear"
echo "Keys:"
echo "--getinfo - get provider.json"
echo "--clear - delete files in workdir and temp files"
echo "--getconfigs - get provider.json, configs.json and eip config"
echo "--providerkey - get provider certificate (cacert.pem)"
echo "--userkey - get user certificate (openvpn.pem)"
echo "--copy - copy certificates and json's in '\$OUTDIR/<provider>'"
echo " \$OUTDIR=$OUTDIR"
echo "--check - copy if info or certificates changed"
}
get_file() #polu4aet fail po http
{
#$1 - adres $2 - kone4ny put'
#$3 - --insecure/-k esli ignorirovat rugan' na samopodpisannye sertifikaty
curl -o "$DL_TEMP" "$1" -D $HEADERDUMP $3 >/dev/null 2>/dev/null
EXITCODE=$?
if [ $EXITCODE -ne 0 ]; then #proverka na oshybki curl
echo "CURL error $EXITCODE"
return 1
else
HTTPSTATUS=`cat $HEADERDUMP|head -1|awk '{print $2}'` #oshybki HTTP
if [ "$HTTPSTATUS" == "200" ];then
echo "Get $1 OK"
else
echo "HTTP error $HTTPSTATUS"
return 1
fi
fi
cp "$DL_TEMP" "$2"
if [ $EXITCODE -ne 0 ]; then
return 1
fi
}
clear_data()
{
echo
echo "Clear data..."
echo
CURDIR=`pwd`
cd "$WORKDIR"
echo "Remove temp files..."
rm "$DL_TEMP"
rm "$HEADERDUMP"
echo "Remove JSON files..."
rm *.json
echo "Remove certificates..."
rm "$CACRTFILE"
rm "$USERCERT"
cd "$CURDIR"
echo "Cleared"
}
getinfo()
{
#Polu4aem provider.json iz kornya saita
echo "Get $JSONFILE..."
get_file "https://$PROVIDER/$JSONFILE" "$WORKDIR/$JSONFILE"
if [ $? -ne 0 ]; then
echo "Get $JSONFILE ERROR"
exit 2
fi
#vytaskivaem parametry api iz json
API_URL=`cat $WORKDIR"/"$JSONFILE|jq '.api_uri'|tr -d '\"'`
API_VER=`cat $WORKDIR"/"$JSONFILE|jq '.api_version'|tr -d '\"'`
API_BASE="$API_URL/$API_VER/"
echo
echo "==============="
echo "API URL: $API_URL"
echo "API Version: $API_VER"
echo "API Base: $API_BASE"
echo "==============="
echo
#Pytaemsya polu4it' provider.json iz BASE_API/provider.json,
#hotya jego oby4no net
#echo "Get $API_BASE$JSONFILE..."
#get_file $API_BASE$JSONFILE "$WORKDIR/provider.api.json" -k
#if [ $? -ne 0 ];then
#echo "$API_BASE$JSONFILE get error."
#echo "Use $PROVIDER/$JSONFILE"
#else
#echo "$API_BASE$JSONFILE get OK."
#echo "Use provider.api.json"
#JSONFILE="provider.api.json"
#fi
}
getconfigs()
{
echo
echo "Get configs..."
echo
#ska4ivaem konfig s konfigami
get_file "$API_BASE$CONFIG_JSON" "$WORKDIR/$CONFIG_JSON" -k
if [ $? -ne 0 ]; then
return 1
fi
#vitaskivaem adress configa EIP
EIPADDR=`cat $WORKDIR"/"$CONFIG_JSON|jq '.services.eip'|tr -d '\"'`
echo "EIP config: $EIPADDR"
if [ -z "$EIPADDR" ]; then
echo "No EIP config."
return 1
fi
#vitaskivaem imya EIP-konfiga
EIP_JSON=`echo "$EIPADDR"|awk -F "/" '{print $NF}'`
echo "EIP config file: $EIP_JSON"
#ska4ivaem konfig eip
get_file "$API_URL$EIPADDR" "$WORKDIR/$EIP_JSON" -k
if [ $? -ne 0 ]; then
return 1
fi
}
getprovcert()
{
echo
echo "Get provider certificate..."
echo
#polu4aem URL sertificata i ego fingerprint iz configa
CACRTURL=`cat $WORKDIR"/"$JSONFILE|jq '.ca_cert_uri'|tr -d '\"'`
FGPRINT=`cat $WORKDIR"/"$JSONFILE|jq '.ca_cert_fingerprint'|tr -d '\"'|awk '{print $2}'`
echo "Provider certificate URL:" $CACRTURL
echo "Provider certificate fingerprint:" $FGPRINT
#получаем сертификат
get_file "$CACRTURL" "$WORKDIR/$CACRTFILE" -k
if [ $? -ne 0 ]; then
return 1
fi
#polu4aem fingerprint faila sertificata
CRTFGPRINT=`openssl x509 -in $WORKDIR"/"$CACRTFILE -noout -fingerprint -sha256 -inform pem|awk -F = '{print tolower($2)}'|tr -d ':'`
echo "File fingerprint:" $CRTFGPRINT
#proverka fingerprint
if [ "$FGPRINT" == "$CRTFGPRINT" ];then
echo "Fingerprint OK"
else
echo "WARNING: Fingerprint in JSON != CRT Fingerprint!"
fi
}
getusercert()
{
# v 1 parametre mozhno ustanovit' --insecure dlya curl
echo
echo "Get user certificate..."
echo
#otpravlyaem POST zapros na polu4enie sertificata
#sohranyaem polu4ennoe vo vremennyi fail i dampim
#zagolovki otveta
curl -X POST "$API_BASE/cert" -D "$HEADERDUMP" $1 >"$DL_TEMP" 2>/dev/null
EXITCODE=$?
if [ $EXITCODE -ne 0 ];then
echo "curl error $EXITCODE"
return 1
else
HTTPSTATUS=`cat $HEADERDUMP|head -1|awk '{print $2}'` #oshibki HTTP
if [ "$HTTPSTATUS" == "200" ];then
echo "Get VPN certificate OK"
else
echo "Error $HTTPSTATUS"
return 1
fi
#kopiruem polu4ennoe v rabo4ii katalog
cp "$DL_TEMP" "$WORKDIR/$USERCERT"
if [ $? -eq 0 ];then
echo "User certificate $USERCERT"
else
return 1;
fi
fi
}
copy_files()
{
echo
echo "Copy files..."
echo
BAKDATE=`date -u +%Y%m%d-%H%M%S`
#proveryaem polu4ali li faily
FCTR=${#FILE_LIST[@]}
if [ "$FCTR" -eq 0 ]; then
echo "No files to copy."
return 1
fi
#sozdaem katalogi esli ih net
echo "Creating (if not exist) output dirs:"
mkdir -p "$OUTDIR"
echo "$OUTDIR"
mkdir -p "$OUTDIR/$PROVIDER"
echo "$OUTDIR/$PROVIDER"
mkdir -p "$OUTDIR/$PROVIDER/$OUTDIR_CA"
echo "$OUTDIR/$PROVIDER/$OUTDIR_CA"
mkdir -p "$OUTDIR/$PROVIDER/$OUTDIR_USER"
echo "$OUTDIR/$PROVIDER/$OUTDIR_USER"
#kopiruem faily
for FNAME in ${FILE_LIST[@]}
do
echo -n "Copy $FNAME..."
#imena dlya ishodnogo
IN_FILE="$WORKDIR/$FNAME"
#i kone4nogo faila
if [ "$FNAME" == "$CACRTFILE" ];then #sertificat provider'a
OUT_FILE="$OUTDIR/$PROVIDER/$OUTDIR_CA/$FNAME"
else
if [ "$FNAME" == "$USERCERT" ];then #sertificat polzovatelya
OUT_FILE="$OUTDIR/$PROVIDER/$OUTDIR_USER/$FNAME"
else #ina4e eto json - v katalog provider'a
OUT_FILE="$OUTDIR/$PROVIDER/$FNAME"
fi
fi
if [ -f "$OUT_FILE" ];then #kone4nyi fail jest'
echo -n "exist..."
if [ "$ACT_CHECK" -eq 1 ];then #nado proverit'
echo -n "check..."
diff "$IN_FILE" "$OUT_FILE"
if [ $? -eq 0 ];then #razli4iya net - kod vozvrata diff == 0
echo "skip." #nado propustit' fail
continue
fi
fi
#nado zabekapit' kone4nyi fail
cp "$OUT_FILE" "$OUT_FILE.$BAKDATE.bak"
echo -n "file backuped..."
fi
#kopiruem fail
cp "$IN_FILE" "$OUT_FILE"
echo "OK."
done
}
if [ -z "$1" ];then
print_help `basename $0`
exit 0
fi
if [ "$1" == "--help" ];then
print_help `basename $0`
exit 0
fi
if [ "$1" == "-h" ];then
print_help `basename $0`
exit 0
fi
if [ "$1" == "--clear" ];then
clear_data
exit 0
fi
if [ "$1" == "-p" ];then
if [ -z "$2" ];then
print_help `basename $0`
echo
echo "ERROR: Provider not set"
exit 1
else
PROVIDER="$2"
fi
else
print_help `basename $0`
echo
echo "ERROR: Set provider first"
exit 1
fi
#ustanavlivaem rabo4ii katalog
WORKDIR="$WORKDIR/$PROVIDER"
#sozdaem ego
mkdir -p "$WORKDIR"
PARAMS=$*
existparam "--clear"
ACT_CLEAR=$?
existparam "--getinfo"
ACT_GETINFO=$?
existparam "--getconfigs"
ACT_GETCONFIG=$?
existparam "--providerkey"
ACT_PROVIDERKEY=$?
existparam "--userkey"
ACT_USERKEY=$?
existparam "--copy"
ACT_COPY=$?
existparam "--check"
ACT_CHECK=$?
if [ "$ACT_CLEAR" -eq 1 ];then
clear_data
ACTSEL=1
fi
if [ "$ACT_GETINFO" -eq 1 ];then #get provider.json
getinfo
FILE_LIST+=($JSONFILE)
ACTSEL=1
fi
if [ "$ACT_GETCONFIG" -eq 1 ];then #polu4enie konfigov
if [ -z "$API_BASE" ]; then #API_BASE ne ustanovlen
getinfo #polu4aem osnovnuju informaciju (ne polu4im, vyletim)
FILE_LIST+=($JSONFILE)
fi
getconfigs
FILE_LIST+=($CONFIG_JSON)
FILE_LIST+=($EIP_JSON)
ACTSEL=1
fi
if [ "$ACT_PROVIDERKEY" -eq 1 ];then #polu4enie sertificata provider'a
if [ -z "$API_BASE" ]; then #API_BASE ne ustanovlen
getinfo ##polu4aem osnovnuju informaciju (ne polu4im, vyletim)
FILE_LIST+=($JSONFILE)
fi
getprovcert
FILE_LIST+=($CACRTFILE)
ACTSEL=1
fi
if [ "$ACT_USERKEY" -eq 1 ];then #polu4enie sertificata polzovatelya
if [ -z "$API_BASE" ]; then #API_BASE ne ustanovlen
getinfo #polu4aem osnovnuju informaciju (ne polu4im, vyletim)
FILE_LIST+=($JSONFILE)
fi
getusercert "--insecure"
FILE_LIST+=($USERCERT)
ACTSEL=1
fi
if [ "$ACT_COPY" -eq 1 ];then #kopirovanie failov v vihodnoj katalog
copy_files
ACTSEL=1
fi
if [ "$ACTSEL" -ne 1 ];then #neizvestnyj klu4
print_help `basename $0`
echo "ERROR: No know keys"
exit 1
fi
#debug
#echo
#echo "Debug"
#echo ${FILE_LIST[@]}