diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2019-02-24 21:05:27 +0100 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2019-02-24 21:05:27 +0100 |
commit | 8081a5520a441b43a8a7a73f3a90c7aacfaa8e10 (patch) | |
tree | c7f49bd33ed19d53afc0ee9df8b2c82c200c5910 /tasks/edit_find_grep_compile_convert | |
parent | 9963b74f777edf985540eac71b1ca095f88b8bca (diff) |
Move everything one level up
Diffstat (limited to 'tasks/edit_find_grep_compile_convert')
-rw-r--r-- | tasks/edit_find_grep_compile_convert/task.py | 313 |
1 files changed, 313 insertions, 0 deletions
diff --git a/tasks/edit_find_grep_compile_convert/task.py b/tasks/edit_find_grep_compile_convert/task.py new file mode 100644 index 0000000..deaac4f --- /dev/null +++ b/tasks/edit_find_grep_compile_convert/task.py @@ -0,0 +1,313 @@ +# TODO: +# - check if everything is filled in (computers, params, preparation) +# - improve scoring +# - test +# - switch to a real SSH/SFTP client to properly handle filenames + +instructions = { + 'si': '''\ +<p> +Ustvari dva navidezna računalnika: <em>SimpleArbiter</em> <em>SmallStudent</em>. + +<p> +Poskrbi, da bo <em>SmallStudent</em> s <em>SimpleArbiter</em> dostopen na naslovu <code>{{testip}}</code>. + +<p> +Na <em>SmallStudent</em> ustvari uporabnika <code>{{testuser}}</code> z geslom <code>{{passwd}}</code>. + +<p> +Na <em>SmallStudent</em> je nekje v domačem imeniku uporabnika <code>bilbo</code> skrita datoteka, ki vsebuje niz <code>{{magicstr}}</code>. Skopiraj jo v domači imenik uporabnika <code>{{testuser}}</code> in jo poimenuj <code>{{dstfile}}</code>. Poskrbi, da bo lastnik <code>{{testuser}}</code>, skupina pa naj bo enaka kot pri izvorni datoteki. Brati naj jo ima pravico samo lastnik, pisati lastnik in skupina, poganjati nihče. + +<p> +V <code>{{dstfile}}</code> zamenjaj vse vrstice oblike <code>poXYZlz</code>, kjer je <code>XYZ</code> poljubno zaporedje znakov, tako, da bo namesto <code>XYZ</code> niz <code>kaka</code>. + +<p> +Napiši program v poljubnem programskem jeziku, ki kot argument sprejme število B med 0 in 7. Program naj prebere znak s standardnega vhoda Če je B-ti bit v znaku nastavljen na 1, naj izpiše <code>ta</code>. Če je B-ti bit nastavljen na 0, naj program izpiše <code>ti</code>. Program poimenuj <code>{{progname}}</code> in ga spravi v domači imenik uporabnika <code>{{testuser}}</code>. +''', + 'en': '''\ +<p> +Create two virtual machines: <em>SimpleArbiter</em> and <em>SmallStudent</em>. + +<p> +Make sure that <em>SmallStudent</em> is accessible from <em>SimpleArbiter</em> on IP <code>{{testip}}</code>. + +<p> +Create a user <code>{{testuser}}</code> with the password <code>{{passwd}}</code> on <em>SmallStudent</em>. + +<p> +There is a file containing <code>{{magicstr}}</code> hidden somewhere in the home directory of user <code>bilbo</code>. Copy it into <code>{{testuser}}</code>’s home directory and name it <code>{{dstfile}}</code>. Change the owner to <code>{{testuser}}</code> and ensure the group is the same as for the original file. Make sure only the owner has the right to read it, only the owner and group members have the right to write to it and nobody has the right to execute it. + +<p> +In <code>{{dstfile}}</code>, replace all lines of the form <code>poXYZlz</code> where <code>XYZ</code> are arbitrary characters so that <code>XYZ</code> is replaced by <code>kaka</code>. + +<p> +Write a program in any programming language. The program should accept a single argument B, which is a number between 0 and 7. It should read a character from standard input and output <code>ta</code> if B-th bit in this character is set to 1, and <code>ti</code>. If B-th bit is set to 0. Name the program <code>{{progname}}</code> and place it in the home directory of <code>{{testuser}}</code>. +''', +} + +# instructions = {'si': 'Potrpite.', 'en': 'Have patience.'} + +computers = { + 'SimpleArbiter': { + 'disks': [ + { + 'name': 'simpleArbiterDhcpGW', + }, + ], + 'network_interfaces': [ + { + 'network': 'net1', + }, + { + 'network': 'net2', + }, + ], + 'flavor': 'm1.tiny', + 'config_drive': False, + }, + 'SmallStudent': { + 'disks': [ + { + 'name': 'student-entrance2', + }, + ], + 'network_interfaces': [ + { + 'network': 'net2', + }, + ], + 'flavor': 'm1.tiny', + 'config_drive': False, + } +} + +networks = { + 'net1': { + 'public': True, + }, + 'net2': { + 'public': False, + } +} + +params_meta = { + 'testip': { + 'descriptions': { + 'si': 'IP SmallStudent', + 'en': 'IP SmallStudent', + }, + 'w': False, + 'public': True, + 'type': 'IP', + 'generated': True, + }, + 'testuser': { + 'descriptions': { + 'si': 'Uporabnik na SmallStudent', + 'en': 'Username on SmallStudent', + }, + 'w': False, + 'public': True, + 'type': 'username', + 'generated': True, + }, + 'passwd': { + 'descriptions': { + 'si': 'Geslo na SmallStudent', + 'en': 'Password on SmallStudent', + }, + 'w': False, + 'public': True, + 'type': None, + 'generated': True, + + }, + 'magicstr':{ + 'descriptions': { + 'si': 'Niz v iskani datoteki', + 'en': 'String in the file you need to find', + }, + 'w': False, + 'public': True, + 'type': None, + 'generated': True, + + }, + 'dstfile':{ + 'descriptions': { + 'si': 'Ciljno ime datoteke', + 'en': 'Destination filename', + }, + 'w': False, + 'public': True, + 'type': 'filename', + 'generated': True, + }, + 'progname':{ + 'descriptions': { + 'si': 'Ime programa', + 'en': 'Program filename', + }, + 'w': False, + 'public': True, + 'type': 'filename', + 'generated': True, + }, + 'pubseed':{ + 'descriptions': { + 'si': 'Nekaj nepredvidenega', + 'en': 'A random seed', + }, + 'w': False, + 'public': True, + 'type': None, + 'generated': True, + }, + 'rndseed':{ + 'descriptions': { + 'si': 'random seed za skrito datoteko', + 'en': 'random seed for hiding the file', + }, + 'w': False, + 'public': False, + 'type': None, + 'generated': True, + }, +} + +def task(testip, testuser, passwd, magicstr, dstfile, progname, pubseed): + import random + + r = random.Random(pubseed) + tests = [ + ('dst_ls', 'ls -l ~/{}'.format(dstfile)), + ('dst_file_contents', 'cat ~/{}'.format(dstfile)), + ('home_ls', 'ls ~/'.format(dstfile)), + ] + + N_TITA = 40 + for i in range(N_TITA): + b = r.randint(0, 7) + x = oct(r.randint(37, 127)).replace('o', '') + tests += [('tita-{:02}'.format(i), 'echo -e "\\{}" | ~/{} {}'.format(x, progname, b))] + + results = kpov_util.ssh_test(testip, testuser, passwd, tests) + results['tita_return'] = ''.join(results['tita-{:02}'.format(i)] for i in range(N_TITA)) + + return results + +def gen_params(user_id, params_meta): + import random + params = dict() + r = random.Random(user_id) + params['testip'] = kpov_util.IPv4_addr_gen(r, + network = '10.94.80.0/19', n_generated=1)[0] + params['testuser'] = kpov_util.default_generators['username'](r) + params['passwd'] = kpov_util.alnum_gen(r, 8) + params['magicstr'] = "".join([r.choice("qwerztlpoQWERTPOL") for i in range(10)]) + params['dstfile'] = kpov_util.default_generators['filename'](r) + params['progname'] = kpov_util.default_generators['filename'](r) + while params['dstfile'] == params['progname']: + params['progname'] = kpov_util.default_generators['filename'](r) + params['pubseed'] = kpov_util.alnum_gen(r, 8) + params['rndseed'] = kpov_util.alnum_gen(r, 8) + return params + + +def task_check(results, params): + import os + import re + N_TITA = 40 + hints = [] + score = 0 + r = random.Random(params['rndseed']) + + if results['ssh'] is not True: + hints += ['ssh failed: ' + results['ssh']] + return score, hints + + expected_contents = params['magicstr'] + for i in range(1000): + start = "".join([r.choice(["po", "p0", "no", "ko", "fo", "qo"]) for i in range(20)]) + mid = "".join([r.choice("uiasdfghjkyxcvbnm1234567890ASDFGHJKYZXCVBNM") for i in range(60)]) + end = r.choice(["lz", "1z", "Iz", "iz", "l2", "I2", "12"]) + if start[:2] == "po" and end == "lz": + start = "po" + mid = "kaka" + x = start + mid + end + expected_contents += x + "\r\n" + if results["dst_file_contents"] == expected_contents: + score += 3 + else: + for i, (a, b) in enumerate(zip(expected_contents, results['dst_file_contents'])): + if a != b: + break + hints += ['wrong file {} at position {}'.format(params['dstfile'], i)] + + expected_ls = "-rw--w---- 1 {testuser} bilbo .*{dstfile}.*\r\n".format(**params) + if re.match(expected_ls, results["dst_ls"]): + score += 3 + else: + hints += ["missing file or wrong user/permissions\n" + results["dst_ls"]] + if results["home_ls"].find(params['progname']) > -1: + score += 2 + else: + hints += ["missing program"] + + expected_tita = "" + r = random.Random(params['pubseed']) + for i in range(N_TITA): + b = r.randint(0, 7) + x_i = r.randint(37, 127) + if x_i & (1 << b): + expected_tita += "ta" + else: + expected_tita += "ti" + if results["tita_return"] == expected_tita: + score += 2 + else: + hints += ['program output incorrect:\nwanted:\t{}\ngot:\t{}'.format(expected_tita, results["tita_return"])] + return score, hints + + +def prepare_disks(templates, task_params, global_params): + import random + import os + + # first create the file contents to make it easyer to check. + hidden_contents = task_params['magicstr'] + r = random.Random(task_params['rndseed']) + for i in range(1000): + x = "".join([r.choice(["po", "p0", "no", "ko", "fo", "qo"]) for i in range(20)]) + x += "".join([r.choice("uiasdfghjkyxcvbnm1234567890ASDFGHJKYZXCVBNM") for i in range(60)]) + x += r.choice(["lz", "1z", "Iz", "iz", "l2", "I2", "12"]) + hidden_contents += x + "\n" + + # create hidden file + dir_list = ['Qlipper', 'Thunar', 'blender', 'autostart', 'kazam', 'mc', 'netsurf', 'pulse', 'qupzilla', 'radare2', 'teamviewer', 'texstudio', 'vlc'] + ending_list = ['rc', '.conf', ''] + start_list = ['net', 'dev', 'doc', 'lib', 'time', 'conf'] + r.shuffle(dir_list) + file_letters = ["mod", "co"] + + d = templates['student-entrance2'] + for potential_dir in dir_list: + try: + potential_dir = os.path.join('/home/bilbo/.config', potential_dir) + d.mkdir(potential_dir) + d.chown(1001, 1001, potential_dir) + except: + pass + for i in range(r.randint(2, 20)): + rndstr2 = r.choice(start_list) + \ + r.choice(file_letters) + r.choice(ending_list) + hidden_file_name = os.path.join(potential_dir, + rndstr2) + d.write(hidden_file_name, hidden_contents) + d.chown(1001, 1001, hidden_file_name) + file_letters = ["stamp", "", "dev", "re"] + hidden_contents = "".join([r.choice("asdfghjkyxcvbnm1234567890 \n") for j in range(10000)]) + file_letters = file_letters + ["mod", "co"] + # TODO create some additional files + + write_default_config(templates['simpleArbiterDhcpGW'], global_params) |