This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
setup
executable file
·650 lines (529 loc) · 26.4 KB
/
setup
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#!/usr/bin/env python2.7
'''
/*
* The Shadow Simulator
* Copyright (c) 2010-2011, Rob Jansen
* See LICENSE for licensing information
*/
'''
import sys, os, argparse, subprocess, multiprocessing, shlex, shutil, urllib2, tarfile, gzip, stat, time
from datetime import datetime
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG)
BUILD_PREFIX="./build"
INSTALL_PREFIX=os.path.expanduser("~/.shadow")
TOR_DEFAULT_VERSION="0.3.5.7"
TOR_URL="https://archive.torproject.org/tor-package-archive/tor-{0}.tar.gz".format(TOR_DEFAULT_VERSION)
TOR_URL_SIG="https://archive.torproject.org/tor-package-archive/tor-{0}.tar.gz.asc".format(TOR_DEFAULT_VERSION)
OPENSSL_DEFAULT_VERSION="1.1.0h"
OPENSSL_URL="https://www.openssl.org/source/old/{0}/openssl-{1}.tar.gz".format(OPENSSL_DEFAULT_VERSION[:-1], OPENSSL_DEFAULT_VERSION)
OPENSSL_URL_SIG="https://www.openssl.org/source/old/{0}/openssl-{1}.tar.gz.asc".format(OPENSSL_DEFAULT_VERSION[:-1], OPENSSL_DEFAULT_VERSION)
LIBEVENT_DEFAULT_VERSION="2.1.11-stable"
LIBEVENT_URL="https://github.com/libevent/libevent/releases/download/release-{0}/libevent-{0}.tar.gz".format(LIBEVENT_DEFAULT_VERSION)
LIBEVENT_URL_SIG="https://github.com/libevent/libevent/releases/download/release-{0}/libevent-{0}.tar.gz.asc".format(LIBEVENT_DEFAULT_VERSION)
def main():
parser_main = argparse.ArgumentParser(
description='Utility to help setup the Shadow simulator plug-in that runs Tor',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# setup our commands
subparsers_main = parser_main.add_subparsers(
help='run a subcommand (for help use <subcommand> --help)')
# configure dependencies subcommand
parser_dep = subparsers_main.add_parser('dependencies',
help="configure and build custom plug-in dependencies",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser_dep.set_defaults(func=dependencies,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# add dependencies options
parser_dep.add_argument('-p', '--prefix',
help="configure PATH as dependency root installation directory",
metavar="PATH",
action="store", dest="prefix",
default=INSTALL_PREFIX)
parser_dep.add_argument('-y', '--assume-yes',
help="don't ask for confirmation while downloading dependencies",
action="store_true", dest="preapproved",
default=False)
# configure build subcommand
parser_build = subparsers_main.add_parser('build',
help='configure and build plug-in',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser_build.set_defaults(func=build,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# add building options
parser_build.add_argument('-y', '--assume-yes',
help="don't ask for confirmation while downloading dependencies",
action="store_true", dest="preapproved",
default=False)
parser_build.add_argument('-p', '--prefix',
help="configure PATH as Shadow root installation directory",
metavar="PATH",
action="store", dest="prefix",
default=INSTALL_PREFIX)
parser_build.add_argument('-i', '--include',
help="append PATH to the list of paths searched for headers. useful if dependencies are installed to non-standard locations, or when compiling custom libraries.",
metavar="PATH",
action="append", dest="extra_includes",
default=[INSTALL_PREFIX+ "/include", os.path.expanduser("~/.local/include"), os.path.expanduser("~/.local/share/llvm/cmake")])
parser_build.add_argument('-l', '--library',
help="append PATH to the list of paths searched for libraries. useful if dependencies are installed to non-standard locations, or when compiling custom libraries.",
metavar="PATH",
action="append", dest="extra_libraries",
default=[INSTALL_PREFIX+ "/lib", os.path.expanduser("~/.local/lib")])
parser_build.add_argument('-D', '--define',
help="append DEF to the list of define flags. useful when compiling a custom Tor with --tor-prefix",
metavar="DEF",
action="append", dest="extra_defines",
default=[])
parser_build.add_argument('-c', '--clean',
help="force a full rebuild by removing build cache",
action="store_true", dest="do_force_rebuild",
default=False)
parser_build.add_argument('-g', '--debug',
help="build in extra memory checks and debugging symbols when running Shadow",
action="store_true", dest="do_debug",
default=False)
parser_build.add_argument('-v', '--verbose',
help="print verbose output from the compiler",
action="store_true", dest="do_verbose",
default=False)
parser_build.add_argument('-j', '--jobs',
help="number of jobs to run simultaneously during the build",
metavar="N", type=int,
action="store", dest="njobs",
default=1)#multiprocessing.cpu_count())
parser_build.add_argument('-o', '--profile',
help="build in gprof profiling information when running Shadow",
action="store_true", dest="do_profile",
default=False)
parser_build.add_argument('-t', '--test',
help="build tests",
action="store_true", dest="do_test",
default=False)
parser_build.add_argument('--tor-tracing',
help="add necessary build options to compile Tor tracing feature for Shadow",
action="store_true", dest="do_tor_tracing",
default=False)
parser_build.add_argument('--tor-prefix',
help="PATH to a custom base Tor directory to build (overrides '--tor-version')",
metavar="PATH",
action="store", dest="tor_prefix",
default=None)
parser_build.add_argument('--tor-version',
help="specify which VERSION of Tor to download and build (overridden by '--tor-prefix')",
metavar="VERSION",
action="store", dest="tor_version",
default=TOR_DEFAULT_VERSION)
parser_build.add_argument('--tor-lib',
help="append LIB to the list of libraries that get linked with shadow-plugin-tor. Useful when compiling and linking custom libraries.",
metavar="LIB",
action="append", dest="tor_libraries",
default=[])
parser_build.add_argument('--tor-config-option',
help="append option to Tor's configure script",
metavar="OPT",
action="append", dest="tor_config_opts",
default=[])
parser_build.add_argument('--tor-config-cflag',
help="append cflag to Tor's configure script",
metavar="CFLAG",
action="append", dest="tor_config_cflags",
default=[])
parser_build.add_argument('--libevent-prefix',
help="use non-standard PATH when linking Tor to libevent.",
metavar="PATH",
action="store", dest="libevent_prefix",
default=INSTALL_PREFIX)
parser_build.add_argument('--static-libevent',
help="tell Tor to link against the static version of libevent",
action="store_true", dest="static_libevent",
default=True)
parser_build.add_argument('--openssl-prefix',
help="use non-standard PATH when linking Tor to openssl.",
metavar="PATH",
action="store", dest="openssl_prefix",
default=INSTALL_PREFIX)
parser_build.add_argument('--static-openssl',
help="tell Tor to link against the static version of openssl",
action="store_true", dest="static_openssl",
default=True)
parser_build.add_argument('--export-libraries',
help="export Shadow's plug-in service libraries and headers",
action="store_true", dest="export_libraries",
default=False)
parser_build.add_argument('--enable-evp-cipher-encryption',
help="do not intercept EVP_Cipher, so that we allow EVP_Cipher encryption to be performed as normal during simulation",
action="store_true", dest="enable_evpcipher",
default=False)
# configure install subcommand
parser_install = subparsers_main.add_parser('install', help='install Shadow',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser_install.set_defaults(func=install,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# get arguments, accessible with args.value
args = parser_main.parse_args()
# run chosen command
r = args.func(args)
logging.info("returning code '{0}'".format(r))
sys.exit(r)
def dependencies(args):
if args.prefix is not None: args.prefix = getfullpath(args.prefix)
filepath=getfullpath(__file__)
rootdir=filepath[:filepath.rfind("/")]
builddir=getfullpath(BUILD_PREFIX)
keyring=getfullpath(rootdir + "/tools/deps_keyring.gpg")
## we will start in build directory
if not os.path.exists(builddir): os.makedirs(builddir)
os.chdir(builddir)
openssl_tarball = get("./", OPENSSL_URL, OPENSSL_URL_SIG, keyring, args.preapproved)
if openssl_tarball is None: return -1
libevent_tarball = get("./", LIBEVENT_URL, LIBEVENT_URL_SIG, keyring, args.preapproved)
if libevent_tarball is None: return -1
## build openssl first
dir = extract(openssl_tarball)
if dir is None: return -1
os.chdir(dir)
## for debugging and profiling (you may want to enable -g and -pg independently)
#configure = "./config --prefix={0} no-shared threads -fPIC -g -pg -DPURIFY -Bsymbolic".format(args.prefix)
is64 = sys.maxsize > 2**32
ecopt = "enable-ec_nistp_64_gcc_128" if is64 else ""
configure = "./config --prefix={0} shared threads {1} -fPIC".format(args.prefix, ecopt)
logging.info("now attempting to build openssl with '{0}'".format(configure))
if automake(configure, "make", "make install_sw") != 0:
logging.error("problems building {0}".format(openssl_tarball))
return -1
## now build libevent
os.chdir(builddir)
dir = extract(libevent_tarball)
if dir is None: return -1
os.chdir(dir)
#configure = "./configure --prefix={0} --enable-shared=no CFLAGS=\"-fPIC -I{0} -g -pg\" LDFLAGS=\"-L{0}\" CPPFLAGS=\"-DUSE_DEBUG\"".format(args.prefix)
configure = "./configure --prefix={0} --enable-shared CFLAGS=\"-fPIC -I{0}/include/\" LDFLAGS=\"-L{0}/lib/\"".format(args.prefix)
logging.info("now attempting to build libevent with '{0}'".format(configure))
if automake(configure, "make", "make install") != 0:
logging.error("problems building {0}".format(openssl_tarball))
return -1
logging.info("successfully installed dependencies to '{0}'".format(args.prefix))
return 0
def automake(configure, make, install):
retcode = subprocess.call(shlex.split(configure))
if retcode != 0: return retcode
retcode = subprocess.call(shlex.split(make))
if retcode != 0: return retcode
retcode = subprocess.call(shlex.split(install))
if retcode != 0: return retcode
return 0
def build(args):
# get absolute paths
if args.prefix is not None: args.prefix = getfullpath(args.prefix)
if args.tor_prefix is not None: args.tor_prefix = getfullpath(args.tor_prefix)
if args.libevent_prefix is not None: args.libevent_prefix = getfullpath(args.libevent_prefix)
if args.openssl_prefix is not None: args.openssl_prefix = getfullpath(args.openssl_prefix)
args.tor_url = TOR_URL.replace(TOR_DEFAULT_VERSION, args.tor_version)
args.tor_sig_url = TOR_URL_SIG.replace(TOR_DEFAULT_VERSION, args.tor_version)
filepath=getfullpath(__file__)
rootdir=filepath[:filepath.rfind("/")]
builddir=getfullpath(BUILD_PREFIX)
installdir=getfullpath(args.prefix)
args.keyring = getfullpath(rootdir + "/tools/deps_keyring.gpg")
# clear cmake cache
if args.do_force_rebuild and os.path.exists(builddir+"/main"): shutil.rmtree(builddir+"/main")
if args.tor_prefix != None and os.path.exists(builddir+"/main/src/tor"): shutil.rmtree(builddir+"/main/src/tor")
if args.tor_version != TOR_DEFAULT_VERSION and os.path.exists(builddir+"/tor"): shutil.rmtree(builddir+"/tor")
# create directories
if not os.path.exists(builddir+"/main"): os.makedirs(builddir+"/main")
if not os.path.exists(installdir): os.makedirs(installdir)
# build up args string for the cmake command
cmake_cmd = "cmake " + rootdir + " -DCMAKE_INSTALL_PREFIX=" + installdir
# other cmake options
if args.do_verbose: os.putenv("VERBOSE", "1")
if args.do_test: cmake_cmd += " -DSHADOW_TEST=ON"
if args.do_profile: cmake_cmd += " -DSHADOW_PROFILE=ON"
if args.export_libraries: cmake_cmd += " -DSHADOW_EXPORT=ON"
if args.enable_evpcipher: cmake_cmd += " -DSHADOW_ENABLE_EVPCIPHER=ON"
if args.do_tor_tracing: cmake_cmd += " -DTOR_TRACING=ON"
# we will run from build directory
calledDirectory = os.getcwd()
# run build tasks
os.chdir(builddir)
# check if we need to setup Tor
args.tordir = getfullpath(builddir+"/tor")
if setup_tor(args) != 0: return -1
#cmake_cmd += " -DTORPATH=" + args.tordir
torversion = get_tor_version(args)
if torversion == None: return -1
logging.info("detected tor version {0}".format(torversion))
vparts = torversion.split(".")
a, b, c, d = int(vparts[0]), int(vparts[1]), int(vparts[2]), int(vparts[3].split("-")[0])
cmake_cmd += " -DTOR_VERSION_A={0}".format(a)
cmake_cmd += " -DTOR_VERSION_B={0}".format(b)
cmake_cmd += " -DTOR_VERSION_C={0}".format(c)
cmake_cmd += " -DTOR_VERSION_D={0}".format(d)
# now we will be using cmake to build shadow and the plug-ins
os.chdir(builddir+"/main")
# hack to make passing args to CMAKE work... doesnt seem to like the first arg
args.extra_includes.insert(0, "./")
args.extra_libraries.insert(0, "./")
# add extra library and include directories as absolution paths
make_paths_absolute(args.extra_includes)
make_paths_absolute(args.extra_libraries)
# add '-D' to defines
extra_defines = [ '-D' + d.strip() for d in args.extra_defines]
# make sure we can access them from cmake
cmake_cmd += " -DCMAKE_EXTRA_INCLUDES=" + ';'.join(args.extra_includes)
cmake_cmd += " -DCMAKE_EXTRA_LIBRARIES=" + ';'.join(args.extra_libraries)
cmake_cmd += " -DCMAKE_EXTRA_DEFINES=" + ';'.join(extra_defines)
cmake_cmd += " -DCMAKE_TOR_LIBRARIES=" + ';'.join(args.tor_libraries)
# call cmake to configure the make process, wait for completion
logging.info("running \'{0}\' from \'{1}\'".format(cmake_cmd, os.getcwd()))
retcode = subprocess.call(cmake_cmd.strip().split())
logging.info("cmake returned " + str(retcode))
if retcode == 0:
# call make, wait for it to finish
make = "make -j{0}".format(args.njobs)
logging.info("calling " + make)
retcode = subprocess.call(shlex.split(make))
logging.info("make returned " + str(retcode))
if retcode == 0: logging.info("now run \'./setup install\'")
else: log("ERROR! Non-zero return code from make.")
else: log("ERROR! Non-zero return code from cmake.")
# go back to where we came from
os.chdir(calledDirectory)
return retcode
def install(args):
builddir=getfullpath(BUILD_PREFIX+"/main")
if not os.path.exists(builddir):
log("ERROR: please build before installing!")
return
# go to build dir and install from makefile
calledDirectory = os.getcwd()
os.chdir(builddir)
# call make install, wait for it to finish
makeCommand = "make install"
logging.info("calling \'"+makeCommand+"\'")
retcode = subprocess.call(makeCommand.strip().split())
logging.info("make install returned " + str(retcode))
if retcode == 0: logging.info("Successfully installed shadow-plugin-tor!")
# go back to where we came from
os.chdir(calledDirectory)
return retcode
def setup_tor(args):
logging.info("checking Tor source dependencies...")
if which("autoreconf") is None or which("aclocal") is None or which("autoheader") is None or which("autoconf") is None or which("automake") is None:
logging.error("missing dependencies - please install 'automake' and 'autoconf', or make sure they are in your PATH")
return -1
# if custom Tor prefix is given, always blow away Tor's build directory
# and clean the potentially dirty custom directory
if args.tor_prefix is not None:
if os.path.exists(args.tordir): shutil.rmtree(args.tordir)
shutil.copytree(args.tor_prefix, args.tordir)
distcleancmd = "make distclean"
logging.info("running \'{0}\' in \'{1}\'".format(distcleancmd, args.tordir))
subprocess.call(shlex.split(distcleancmd.strip()), cwd=args.tordir)
# otherwise blow it away if there were problems building it so we try again
elif os.path.exists(args.tordir) and not os.path.exists(args.tordir+"/src/or/tor"):
shutil.rmtree(args.tordir)
if not os.path.exists(args.tordir):
# we have no Tor build directory, check requested url
target_tor = getfullpath(os.path.basename(args.tor_url))
# only download if we dont have the requested URL
if not os.path.exists(target_tor):
if get("./", args.tor_url, args.tor_sig_url, args.keyring, args.preapproved) == None:
# if download(args.tor_url, target_tor) != 0:
logging.error("problem getting \'{0}\'".format(args.tor_url))
return -1
# we are either extracting a cached tarball, or one we just downloaded
d = extract(target_tor)
if d is not None:
shutil.copytree(d, args.tordir)
shutil.rmtree(d)
else:
logging.error("\'{0}\' is not a tarfile".format(target_tor))
return -1
retcode = 0
# Copy patch and static symbol converter scripts to the build directory
#shutil.copy("../tools/patch.sh", args.tordir)
# if we already configured successfully, dont patch or re-configure
if os.path.exists(args.tordir):
os.chdir(args.tordir)
if not os.path.exists(args.tordir+"/orconfig.h"):
opts = " ".join(args.tor_config_opts)
if args.do_tor_tracing: opts += " --enable-shadow-tracing"
cflags = "-fPIC -fno-inline"
if args.extra_includes is not None:
for i in args.extra_includes: cflags += " -I" + i.strip()
if args.do_debug: cflags += " -ggdb"
if args.tor_config_cflags: cflags += " " + " ".join(args.tor_config_cflags)
if args.extra_defines:
for d in args.extra_defines: cflags += " -D" + d.strip()
ldflags = ""
if args.extra_libraries is not None:
for l in args.extra_libraries: ldflags += " -L" + l.strip()
libs = ""
if len(args.tor_libraries) > 0:
for l in args.tor_libraries: libs += " -l" + l.strip()
configure = "./configure --disable-asciidoc --disable-gcc-hardening --disable-linker-hardening --disable-unittests {0} CFLAGS=\"{1}\" LDFLAGS=\"{2}\" LIBS=\"{3}\"".format(opts, cflags, ldflags, libs)
if args.libevent_prefix is not None: configure += " --with-libevent-dir=" + getfullpath(args.libevent_prefix)
if args.openssl_prefix is not None: configure += " --with-openssl-dir=" + getfullpath(args.openssl_prefix)
if args.static_openssl: configure += " --enable-static-openssl"
if args.static_libevent: configure += " --enable-static-libevent"
# generate configure script
if os.path.exists(args.tordir+"/configure"):
reconf = which('autoconf')
logging.info("running \'{0}\'".format(reconf))
retcode = subprocess.call(shlex.split(reconf))
if retcode !=0: return retcode
else:
autogen = os.path.abspath(os.path.expanduser(args.tordir+"/autogen.sh"))
if not os.path.exists(autogen):
log("ERROR: neither 'configure' nor 'autogen.sh' exist. unable to configure Tor")
return -2
logging.info("running \'{0}\'".format(autogen))
retcode = subprocess.call(shlex.split(autogen))
if retcode !=0: return retcode
# need to patch AFTER generating configure to avoid overwriting the patched configure
# patch static variables/functions
#patch = "./patch.sh"
#logging.info("running \'{0}\'".format(patch))
#retcode = subprocess.call(shlex.split(patch))
#if retcode !=0: return retcode
# configure
logging.info("running \'{0}\'".format(configure))
retcode = subprocess.call(shlex.split(configure))
if retcode !=0: return retcode
make = "make -j{0}".format(args.njobs)
logging.info("running \'{0}\'".format(make))
retcode = subprocess.call(shlex.split(make))
return retcode
def get_tor_version(args):
# get tor version info
orconf = getfullpath(args.tordir+"/orconfig.h")
if not os.path.exists(orconf):
log("ERROR: file \'{0}\' does not exist".format(orconf))
return None
search = "#define VERSION "
with open(orconf, 'rb') as f:
for line in f:
if line.find(search) > -1:
return line[line.index("\"")+1:line.rindex("\"")]
log("ERROR: cant find version information in config file \'{0}\'".format(orconf))
return None
def getfullpath(path):
return os.path.abspath(os.path.expanduser(path))
def make_paths_absolute(list):
for i in xrange(len(list)): list[i] = getfullpath(list[i])
def extract(tarball):
if tarfile.is_tarfile(tarball):
tar = tarfile.open(tarball, "r:gz")
n = tar.next().name
while n.find('/') < 0: n = tar.next().name
d = getfullpath(os.getcwd() + "/" + n[:n.index('/')])
#tar.extractall()
subprocess.call(shlex.split("tar xaf {0}".format(tarball)))
tar.close()
return d
else:
logging.error("\'{0}\' is not a tarfile".format(tarball))
return None
def get(targetdir, fileurl, sigurl, keyring, preapproved=False):
targetfile = getfullpath(targetdir + "/" + os.path.basename(fileurl))
targetsig = getfullpath(targetdir + "/" + os.path.basename(sigurl))
if not os.path.exists(targetfile) and (download(fileurl, targetfile, preapproved) < 0): return None
if preapproved: return targetfile
if not query_yes_no("Do you want to check the signature of {0}?".format(os.path.basename(fileurl))): return targetfile
if (not os.path.exists(targetsig)) and (download(sigurl, targetsig) < 0): return None
question = "Do you want to use the included keyring instead of the running user's keyring?"
answer = query_yes_no(question)
retcode = -2
gpg_binaries = ["gpg2", "gpg"]
for gpg_binary in gpg_binaries:
try:
if answer:
gpg = gpg_binary + " --keyring {0} --verify {1}".format(keyring, targetsig)
logging.info("running \'{0}\'".format(gpg))
retcode = subprocess.call(shlex.split(gpg))
else:
gpg = gpg_binary + " --verify {0}".format(targetsig)
logging.info("running \'{0}\'".format(gpg))
retcode = subprocess.call(shlex.split(gpg))
except (OSError):
logging.info(gpg_binary + " binary is not present")
continue
break
success = False
if retcode == 0:
logging.info("Signature is good")
success = True
else:
if retcode == -2:
logging.warning("No GnuPG binary found")
else:
logging.warning("Signature is bad")
if query_yes_no("Do you want to continue without verifying the signature?"): success = True
if success and query_yes_no("Everything looks good. OK to proceed?"): return targetfile
else: return None
def download(url, target_path, preapproved=False):
if preapproved or query_yes_no("May we download \'{0}\'?".format(url)):
logging.info("attempting to download " + url)
try:
u = urllib2.urlopen(url)
localfile = open(target_path, 'w')
localfile.write(u.read())
localfile.close()
logging.info("successfully downloaded \'{0}\' using urllib2".format(url))
return 0
except urllib2.URLError as e:
logging.error("failed to download \'{0}\' using urllib2".format(url))
curlpath = which("curl")
if curlpath is not None:
retcode = subprocess.call(shlex.split("{0} -f -L -O {1}".format(curlpath, url)))
if retcode == 0:
logging.info("successfully downloaded \'{0}\' using curl".format(url))
return 0
raise e
return -1
else:
logging.error("user denied download permission.")
return -1
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is one of "yes" or "no".
"""
valid = {"yes":True, "y":True, "ye":True, "no":False, "n":False}
if default == None: prompt = " [y/n] "
elif default == "yes": prompt = " [Y/n] "
elif default == "no": prompt = " [y/N] "
else: raise ValueError("invalid default answer: '%s'" % default)
color_start_code = "\033[1m" + "\033[93m" # <- bold+yellow
color_end_code = "\033[0m"
while True:
sys.stdout.write(color_start_code + question + color_end_code + prompt)
choice = raw_input().lower()
if default is not None and choice == '': return valid[default]
elif choice in valid: return valid[choice]
else: sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
## helper - test if program is in path
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def log(msg):
color_start_code = "\033[94m" # red: \033[91m"
color_end_code = "\033[0m"
prefix = "[" + str(datetime.now()) + "] Shadow Setup: "
print >> sys.stderr, color_start_code + prefix + msg + color_end_code
if __name__ == '__main__':
main()