Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Interactive shell for working with SIM / UICC / USIM / ISIM cards |
| 4 | # |
Harald Welte | 8fab463 | 2023-11-03 01:03:28 +0100 | [diff] [blame] | 5 | # (C) 2021-2023 by Harald Welte <laforge@osmocom.org> |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation, either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Harald Welte | e126872 | 2023-06-24 07:57:08 +0200 | [diff] [blame] | 20 | from typing import List, Optional |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 21 | |
| 22 | import json |
Philipp Maier | 5d698e5 | 2021-09-16 13:18:01 +0200 | [diff] [blame] | 23 | import traceback |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 24 | |
| 25 | import cmd2 |
Harald Welte | 93aac3a | 2023-04-27 15:18:13 +0200 | [diff] [blame] | 26 | from packaging import version |
Harald Welte | 961b803 | 2023-04-27 17:30:22 +0200 | [diff] [blame] | 27 | from cmd2 import style |
| 28 | # cmd2 >= 2.3.0 has deprecated the bg/fg in favor of Bg/Fg :( |
| 29 | if version.parse(cmd2.__version__) < version.parse("2.3.0"): |
Oliver Smith | e47ea5f | 2023-05-23 12:54:15 +0200 | [diff] [blame] | 30 | from cmd2 import fg, bg # pylint: disable=no-name-in-module |
Harald Welte | 961b803 | 2023-04-27 17:30:22 +0200 | [diff] [blame] | 31 | RED = fg.red |
| 32 | LIGHT_RED = fg.bright_red |
| 33 | LIGHT_GREEN = fg.bright_green |
| 34 | else: |
| 35 | from cmd2 import Fg, Bg # pylint: disable=no-name-in-module |
| 36 | RED = Fg.RED |
| 37 | LIGHT_RED = Fg.LIGHT_RED |
| 38 | LIGHT_GREEN = Fg.LIGHT_GREEN |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 39 | from cmd2 import CommandSet, with_default_category, with_argparser |
| 40 | import argparse |
| 41 | |
| 42 | import os |
| 43 | import sys |
Harald Welte | 6ad9a24 | 2023-07-11 18:55:29 +0200 | [diff] [blame] | 44 | import inspect |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 45 | from pathlib import Path |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 46 | from io import StringIO |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 47 | |
Harald Welte | cab26c7 | 2022-08-06 16:12:30 +0200 | [diff] [blame] | 48 | from pprint import pprint as pp |
| 49 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 50 | from pySim.exceptions import * |
Harald Welte | cab26c7 | 2022-08-06 16:12:30 +0200 | [diff] [blame] | 51 | from pySim.transport import init_reader, ApduTracer, argparse_add_reader_args, ProactiveHandler |
Philipp Maier | e345e11 | 2023-06-09 15:19:56 +0200 | [diff] [blame] | 52 | from pySim.utils import h2b, b2h, i2h, swap_nibbles, rpad, JsonEncoder, bertlv_parse_one, sw_match |
Philipp Maier | a42ee6f | 2023-08-16 10:44:57 +0200 | [diff] [blame] | 53 | from pySim.utils import sanitize_pin_adm, tabulate_str_list, boxed_heading_str, Hexstr, dec_iccid |
Harald Welte | 1c849f8 | 2023-11-01 23:48:28 +0100 | [diff] [blame] | 54 | from pySim.utils import is_hexstr_or_decimal, is_hexstr, is_decimal |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 55 | from pySim.card_handler import CardHandler, CardHandlerAuto |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 56 | |
Philipp Maier | 5482737 | 2023-10-24 16:18:30 +0200 | [diff] [blame] | 57 | from pySim.filesystem import CardMF, CardDF, CardADF |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 58 | from pySim.ts_102_222 import Ts102222Commands |
Harald Welte | 2a33ad2 | 2021-10-20 10:14:18 +0200 | [diff] [blame] | 59 | from pySim.gsm_r import DF_EIRENE |
Harald Welte | cab26c7 | 2022-08-06 16:12:30 +0200 | [diff] [blame] | 60 | from pySim.cat import ProactiveCommand |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 61 | |
Harald Welte | 4442b3d | 2021-04-03 09:00:16 +0200 | [diff] [blame] | 62 | from pySim.card_key_provider import CardKeyProviderCsv, card_key_provider_register, card_key_provider_get_field |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 63 | |
Harald Welte | 8fab463 | 2023-11-03 01:03:28 +0100 | [diff] [blame] | 64 | from pySim.app import init_card |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 65 | |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 66 | |
Harald Welte | e126872 | 2023-06-24 07:57:08 +0200 | [diff] [blame] | 67 | class Cmd2Compat(cmd2.Cmd): |
| 68 | """Backwards-compatibility wrapper around cmd2.Cmd to support older and newer |
| 69 | releases. See https://github.com/python-cmd2/cmd2/blob/master/CHANGELOG.md""" |
| 70 | def run_editor(self, file_path: Optional[str] = None) -> None: |
| 71 | if version.parse(cmd2.__version__) < version.parse("2.0.0"): |
Harald Welte | 0ec0150 | 2023-06-24 10:00:12 +0200 | [diff] [blame] | 72 | return self._run_editor(file_path) # pylint: disable=no-member |
Harald Welte | e126872 | 2023-06-24 07:57:08 +0200 | [diff] [blame] | 73 | else: |
Harald Welte | 0ec0150 | 2023-06-24 10:00:12 +0200 | [diff] [blame] | 74 | return super().run_editor(file_path) # pylint: disable=no-member |
| 75 | |
| 76 | class Settable2Compat(cmd2.Settable): |
| 77 | """Backwards-compatibility wrapper around cmd2.Settable to support older and newer |
| 78 | releases. See https://github.com/python-cmd2/cmd2/blob/master/CHANGELOG.md""" |
| 79 | def __init__(self, name, val_type, description, settable_object, **kwargs): |
| 80 | if version.parse(cmd2.__version__) < version.parse("2.0.0"): |
| 81 | super().__init__(name, val_type, description, **kwargs) # pylint: disable=no-value-for-parameter |
| 82 | else: |
| 83 | super().__init__(name, val_type, description, settable_object, **kwargs) # pylint: disable=too-many-function-args |
Harald Welte | e126872 | 2023-06-24 07:57:08 +0200 | [diff] [blame] | 84 | |
| 85 | class PysimApp(Cmd2Compat): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 86 | CUSTOM_CATEGORY = 'pySim Commands' |
Harald Welte | 0ba3fd9 | 2023-11-01 19:18:24 +0100 | [diff] [blame] | 87 | BANNER = """Welcome to pySim-shell! |
| 88 | (C) 2021-2023 by Harald Welte, sysmocom - s.f.m.c. GmbH and contributors |
| 89 | Online manual available at https://downloads.osmocom.org/docs/pysim/master/html/shell.html """ |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 90 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 91 | def __init__(self, card, rs, sl, ch, script=None): |
Harald Welte | c85d406 | 2023-04-27 17:10:17 +0200 | [diff] [blame] | 92 | if version.parse(cmd2.__version__) < version.parse("2.0.0"): |
| 93 | kwargs = {'use_ipython': True} |
| 94 | else: |
| 95 | kwargs = {'include_ipy': True} |
| 96 | |
Oliver Smith | e47ea5f | 2023-05-23 12:54:15 +0200 | [diff] [blame] | 97 | # pylint: disable=unexpected-keyword-arg |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 98 | super().__init__(persistent_history_file='~/.pysim_shell_history', allow_cli_args=False, |
Harald Welte | c85d406 | 2023-04-27 17:10:17 +0200 | [diff] [blame] | 99 | auto_load_commands=False, startup_script=script, **kwargs) |
Harald Welte | 0ba3fd9 | 2023-11-01 19:18:24 +0100 | [diff] [blame] | 100 | self.intro = style(self.BANNER, fg=RED) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 101 | self.default_category = 'pySim-shell built-in commands' |
| 102 | self.card = None |
| 103 | self.rs = None |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 104 | self.lchan = None |
| 105 | self.py_locals = {'card': self.card, 'rs': self.rs, 'lchan': self.lchan} |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 106 | self.sl = sl |
| 107 | self.ch = ch |
Harald Welte | 1748b93 | 2021-04-06 21:12:25 +0200 | [diff] [blame] | 108 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 109 | self.numeric_path = False |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 110 | self.conserve_write = True |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 111 | self.json_pretty_print = True |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 112 | self.apdu_trace = False |
Philipp Maier | 5d698e5 | 2021-09-16 13:18:01 +0200 | [diff] [blame] | 113 | |
Harald Welte | 0ec0150 | 2023-06-24 10:00:12 +0200 | [diff] [blame] | 114 | self.add_settable(Settable2Compat('numeric_path', bool, 'Print File IDs instead of names', self, |
| 115 | onchange_cb=self._onchange_numeric_path)) |
| 116 | self.add_settable(Settable2Compat('conserve_write', bool, 'Read and compare before write', self, |
| 117 | onchange_cb=self._onchange_conserve_write)) |
| 118 | self.add_settable(Settable2Compat('json_pretty_print', bool, 'Pretty-Print JSON output', self)) |
| 119 | self.add_settable(Settable2Compat('apdu_trace', bool, 'Trace and display APDUs exchanged with card', self, |
| 120 | onchange_cb=self._onchange_apdu_trace)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 121 | self.equip(card, rs) |
Philipp Maier | 5d698e5 | 2021-09-16 13:18:01 +0200 | [diff] [blame] | 122 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 123 | def equip(self, card, rs): |
| 124 | """ |
| 125 | Equip pySim-shell with the supplied card and runtime state, add (or remove) all required settables and |
| 126 | and commands to enable card operations. |
| 127 | """ |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 128 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 129 | rc = False |
Philipp Maier | 5d698e5 | 2021-09-16 13:18:01 +0200 | [diff] [blame] | 130 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 131 | # Unequip everything from pySim-shell that would not work in unequipped state |
| 132 | if self.rs: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 133 | lchan = self.rs.lchan[0] |
| 134 | lchan.unregister_cmds(self) |
iw0 | f818acd | 2023-06-19 10:27:04 +0200 | [diff] [blame] | 135 | if self.rs.profile: |
| 136 | for cmd_set in self.rs.profile.shell_cmdsets: |
| 137 | self.unregister_command_set(cmd_set) |
| 138 | |
Harald Welte | 892526f | 2023-06-06 17:09:50 +0200 | [diff] [blame] | 139 | for cmds in [Iso7816Commands, Ts102222Commands, PySimCommands]: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 140 | cmd_set = self.find_commandsets(cmds) |
| 141 | if cmd_set: |
| 142 | self.unregister_command_set(cmd_set[0]) |
Philipp Maier | 5d698e5 | 2021-09-16 13:18:01 +0200 | [diff] [blame] | 143 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 144 | self.card = card |
| 145 | self.rs = rs |
Philipp Maier | 5d698e5 | 2021-09-16 13:18:01 +0200 | [diff] [blame] | 146 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 147 | # When a card object and a runtime state is present, (re)equip pySim-shell with everything that is |
| 148 | # needed to operate on cards. |
| 149 | if self.card and self.rs: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 150 | self.lchan = self.rs.lchan[0] |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 151 | self._onchange_conserve_write( |
| 152 | 'conserve_write', False, self.conserve_write) |
| 153 | self._onchange_apdu_trace('apdu_trace', False, self.apdu_trace) |
Harald Welte | 659781c | 2023-06-06 17:00:51 +0200 | [diff] [blame] | 154 | if self.rs.profile: |
| 155 | for cmd_set in self.rs.profile.shell_cmdsets: |
| 156 | self.register_command_set(cmd_set) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 157 | self.register_command_set(Iso7816Commands()) |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 158 | self.register_command_set(Ts102222Commands()) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 159 | self.register_command_set(PySimCommands()) |
Philipp Maier | a42ee6f | 2023-08-16 10:44:57 +0200 | [diff] [blame] | 160 | |
Philipp Maier | 7c0cd0a | 2023-10-16 15:02:07 +0200 | [diff] [blame] | 161 | try: |
| 162 | self.lchan.select('MF/EF.ICCID', self) |
| 163 | self.iccid = dec_iccid(self.lchan.read_binary()[0]) |
| 164 | except: |
| 165 | self.iccid = None |
Philipp Maier | a42ee6f | 2023-08-16 10:44:57 +0200 | [diff] [blame] | 166 | |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 167 | self.lchan.select('MF', self) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 168 | rc = True |
| 169 | else: |
| 170 | self.poutput("pySim-shell not equipped!") |
Philipp Maier | 5d698e5 | 2021-09-16 13:18:01 +0200 | [diff] [blame] | 171 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 172 | self.update_prompt() |
| 173 | return rc |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 174 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 175 | def poutput_json(self, data, force_no_pretty=False): |
| 176 | """like cmd2.poutput() but for a JSON serializable dict.""" |
| 177 | if force_no_pretty or self.json_pretty_print == False: |
| 178 | output = json.dumps(data, cls=JsonEncoder) |
| 179 | else: |
| 180 | output = json.dumps(data, cls=JsonEncoder, indent=4) |
| 181 | self.poutput(output) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 182 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 183 | def _onchange_numeric_path(self, param_name, old, new): |
| 184 | self.update_prompt() |
Philipp Maier | 38c74f6 | 2021-03-17 17:19:52 +0100 | [diff] [blame] | 185 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 186 | def _onchange_conserve_write(self, param_name, old, new): |
| 187 | if self.rs: |
| 188 | self.rs.conserve_write = new |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 189 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 190 | def _onchange_apdu_trace(self, param_name, old, new): |
| 191 | if self.card: |
| 192 | if new == True: |
| 193 | self.card._scc._tp.apdu_tracer = self.Cmd2ApduTracer(self) |
| 194 | else: |
| 195 | self.card._scc._tp.apdu_tracer = None |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 196 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 197 | class Cmd2ApduTracer(ApduTracer): |
| 198 | def __init__(self, cmd2_app): |
Philipp Maier | 91c971b | 2023-10-09 10:48:46 +0200 | [diff] [blame] | 199 | self.cmd2 = cmd2_app |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 200 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 201 | def trace_response(self, cmd, sw, resp): |
| 202 | self.cmd2.poutput("-> %s %s" % (cmd[:10], cmd[10:])) |
| 203 | self.cmd2.poutput("<- %s: %s" % (sw, resp)) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 204 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 205 | def update_prompt(self): |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 206 | if self.lchan: |
Harald Welte | b2e4b4a | 2022-07-19 23:48:45 +0200 | [diff] [blame] | 207 | path_str = self.lchan.selected_file.fully_qualified_path_str(not self.numeric_path) |
Harald Welte | 237ddb5 | 2023-10-22 10:36:58 +0200 | [diff] [blame] | 208 | self.prompt = 'pySIM-shell (%02u:%s)> ' % (self.lchan.lchan_nr, path_str) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 209 | else: |
Philipp Maier | 7226c09 | 2022-06-01 17:58:38 +0200 | [diff] [blame] | 210 | if self.card: |
| 211 | self.prompt = 'pySIM-shell (no card profile)> ' |
| 212 | else: |
| 213 | self.prompt = 'pySIM-shell (no card)> ' |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 214 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 215 | @cmd2.with_category(CUSTOM_CATEGORY) |
| 216 | def do_intro(self, _): |
| 217 | """Display the intro banner""" |
| 218 | self.poutput(self.intro) |
Philipp Maier | 9764de2 | 2021-11-03 10:44:39 +0100 | [diff] [blame] | 219 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 220 | def do_eof(self, _: argparse.Namespace) -> bool: |
| 221 | self.poutput("") |
| 222 | return self.do_quit('') |
Philipp Maier | 5d698e5 | 2021-09-16 13:18:01 +0200 | [diff] [blame] | 223 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 224 | @cmd2.with_category(CUSTOM_CATEGORY) |
| 225 | def do_equip(self, opts): |
| 226 | """Equip pySim-shell with card""" |
Philipp Maier | e6cba76 | 2023-08-11 11:23:17 +0200 | [diff] [blame] | 227 | if self.rs and self.rs.profile: |
Harald Welte | 659781c | 2023-06-06 17:00:51 +0200 | [diff] [blame] | 228 | for cmd_set in self.rs.profile.shell_cmdsets: |
| 229 | self.unregister_command_set(cmd_set) |
Philipp Maier | 91c971b | 2023-10-09 10:48:46 +0200 | [diff] [blame] | 230 | rs, card = init_card(self.sl) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 231 | self.equip(card, rs) |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 232 | |
Philipp Maier | 7226c09 | 2022-06-01 17:58:38 +0200 | [diff] [blame] | 233 | apdu_cmd_parser = argparse.ArgumentParser() |
Harald Welte | 4e59d89 | 2023-11-01 23:40:07 +0100 | [diff] [blame] | 234 | apdu_cmd_parser.add_argument('APDU', type=is_hexstr, help='APDU as hex string') |
Philipp Maier | e7d1b67 | 2022-06-01 18:05:34 +0200 | [diff] [blame] | 235 | apdu_cmd_parser.add_argument('--expect-sw', help='expect a specified status word', type=str, default=None) |
Philipp Maier | 7226c09 | 2022-06-01 17:58:38 +0200 | [diff] [blame] | 236 | |
| 237 | @cmd2.with_argparser(apdu_cmd_parser) |
| 238 | def do_apdu(self, opts): |
| 239 | """Send a raw APDU to the card, and print SW + Response. |
Philipp Maier | 1c207a2 | 2023-11-29 13:04:09 +0100 | [diff] [blame] | 240 | CAUTION: this command bypasses the logical channel handling of pySim-shell and card state changes are not |
| 241 | tracked. Dpending on the raw APDU sent, pySim-shell may not continue to work as expected if you e.g. select |
| 242 | a different file.""" |
| 243 | |
| 244 | # When sending raw APDUs we access the scc object through _scc member of the card object. It should also be |
| 245 | # noted that the apdu command plays an exceptional role since it is the only card accessing command that |
| 246 | # can be executed without the presence of a runtime state (self.rs) object. However, this also means that |
| 247 | # self.lchan is also not present (see method equip). |
| 248 | data, sw = self.card._scc._tp.send_apdu(opts.APDU) |
Philipp Maier | 7226c09 | 2022-06-01 17:58:38 +0200 | [diff] [blame] | 249 | if data: |
| 250 | self.poutput("SW: %s, RESP: %s" % (sw, data)) |
| 251 | else: |
| 252 | self.poutput("SW: %s" % sw) |
Philipp Maier | e7d1b67 | 2022-06-01 18:05:34 +0200 | [diff] [blame] | 253 | if opts.expect_sw: |
| 254 | if not sw_match(sw, opts.expect_sw): |
| 255 | raise SwMatchError(sw, opts.expect_sw) |
Philipp Maier | 7226c09 | 2022-06-01 17:58:38 +0200 | [diff] [blame] | 256 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 257 | class InterceptStderr(list): |
| 258 | def __init__(self): |
| 259 | self._stderr_backup = sys.stderr |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 260 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 261 | def __enter__(self): |
| 262 | self._stringio_stderr = StringIO() |
| 263 | sys.stderr = self._stringio_stderr |
| 264 | return self |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 265 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 266 | def __exit__(self, *args): |
| 267 | self.stderr = self._stringio_stderr.getvalue().strip() |
| 268 | del self._stringio_stderr |
| 269 | sys.stderr = self._stderr_backup |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 270 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 271 | def _show_failure_sign(self): |
Harald Welte | 961b803 | 2023-04-27 17:30:22 +0200 | [diff] [blame] | 272 | self.poutput(style(" +-------------+", fg=LIGHT_RED)) |
| 273 | self.poutput(style(" + ## ## +", fg=LIGHT_RED)) |
| 274 | self.poutput(style(" + ## ## +", fg=LIGHT_RED)) |
| 275 | self.poutput(style(" + ### +", fg=LIGHT_RED)) |
| 276 | self.poutput(style(" + ## ## +", fg=LIGHT_RED)) |
| 277 | self.poutput(style(" + ## ## +", fg=LIGHT_RED)) |
| 278 | self.poutput(style(" +-------------+", fg=LIGHT_RED)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 279 | self.poutput("") |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 280 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 281 | def _show_success_sign(self): |
Harald Welte | 961b803 | 2023-04-27 17:30:22 +0200 | [diff] [blame] | 282 | self.poutput(style(" +-------------+", fg=LIGHT_GREEN)) |
| 283 | self.poutput(style(" + ## +", fg=LIGHT_GREEN)) |
| 284 | self.poutput(style(" + ## +", fg=LIGHT_GREEN)) |
| 285 | self.poutput(style(" + # ## +", fg=LIGHT_GREEN)) |
| 286 | self.poutput(style(" + ## # +", fg=LIGHT_GREEN)) |
| 287 | self.poutput(style(" + ## +", fg=LIGHT_GREEN)) |
| 288 | self.poutput(style(" +-------------+", fg=LIGHT_GREEN)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 289 | self.poutput("") |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 290 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 291 | def _process_card(self, first, script_path): |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 292 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 293 | # Early phase of card initialzation (this part may fail with an exception) |
| 294 | try: |
| 295 | rs, card = init_card(self.sl) |
| 296 | rc = self.equip(card, rs) |
| 297 | except: |
| 298 | self.poutput("") |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 299 | self.poutput("Card initialization (%s) failed with an exception:" % str(self.sl)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 300 | self.poutput("---------------------8<---------------------") |
| 301 | traceback.print_exc() |
| 302 | self.poutput("---------------------8<---------------------") |
| 303 | self.poutput("") |
| 304 | return -1 |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 305 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 306 | # Actual card processing step. This part should never fail with an exception since the cmd2 |
| 307 | # do_run_script method will catch any exception that might occur during script execution. |
| 308 | if rc: |
| 309 | self.poutput("") |
| 310 | self.poutput("Transcript stdout:") |
| 311 | self.poutput("---------------------8<---------------------") |
| 312 | with self.InterceptStderr() as logged: |
| 313 | self.do_run_script(script_path) |
| 314 | self.poutput("---------------------8<---------------------") |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 315 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 316 | self.poutput("") |
| 317 | self.poutput("Transcript stderr:") |
| 318 | if logged.stderr: |
| 319 | self.poutput("---------------------8<---------------------") |
| 320 | self.poutput(logged.stderr) |
| 321 | self.poutput("---------------------8<---------------------") |
| 322 | else: |
| 323 | self.poutput("(none)") |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 324 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 325 | # Check for exceptions |
| 326 | self.poutput("") |
| 327 | if "EXCEPTION of type" not in logged.stderr: |
| 328 | return 0 |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 329 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 330 | return -1 |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 331 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 332 | bulk_script_parser = argparse.ArgumentParser() |
| 333 | bulk_script_parser.add_argument( |
| 334 | 'script_path', help="path to the script file") |
| 335 | bulk_script_parser.add_argument('--halt_on_error', help='stop card handling if an exeption occurs', |
| 336 | action='store_true') |
| 337 | bulk_script_parser.add_argument('--tries', type=int, default=2, |
| 338 | help='how many tries before trying the next card') |
| 339 | bulk_script_parser.add_argument('--on_stop_action', type=str, default=None, |
| 340 | help='commandline to execute when card handling has stopped') |
| 341 | bulk_script_parser.add_argument('--pre_card_action', type=str, default=None, |
| 342 | help='commandline to execute before actually talking to the card') |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 343 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 344 | @cmd2.with_argparser(bulk_script_parser) |
| 345 | @cmd2.with_category(CUSTOM_CATEGORY) |
| 346 | def do_bulk_script(self, opts): |
| 347 | """Run script on multiple cards (bulk provisioning)""" |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 348 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 349 | # Make sure that the script file exists and that it is readable. |
| 350 | if not os.access(opts.script_path, os.R_OK): |
| 351 | self.poutput("Invalid script file!") |
| 352 | return |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 353 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 354 | success_count = 0 |
| 355 | fail_count = 0 |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 356 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 357 | first = True |
| 358 | while 1: |
| 359 | # TODO: Count consecutive failures, if more than N consecutive failures occur, then stop. |
| 360 | # The ratinale is: There may be a problem with the device, we do want to prevent that |
| 361 | # all remaining cards are fired to the error bin. This is only relevant for situations |
| 362 | # with large stacks, probably we do not need this feature right now. |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 363 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 364 | try: |
| 365 | # In case of failure, try multiple times. |
| 366 | for i in range(opts.tries): |
| 367 | # fetch card into reader bay |
Philipp Maier | 91c971b | 2023-10-09 10:48:46 +0200 | [diff] [blame] | 368 | self.ch.get(first) |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 369 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 370 | # if necessary execute an action before we start processing the card |
| 371 | if(opts.pre_card_action): |
| 372 | os.system(opts.pre_card_action) |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 373 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 374 | # process the card |
| 375 | rc = self._process_card(first, opts.script_path) |
| 376 | if rc == 0: |
| 377 | success_count = success_count + 1 |
| 378 | self._show_success_sign() |
| 379 | self.poutput("Statistics: success :%i, failure: %i" % ( |
| 380 | success_count, fail_count)) |
| 381 | break |
| 382 | else: |
| 383 | fail_count = fail_count + 1 |
| 384 | self._show_failure_sign() |
| 385 | self.poutput("Statistics: success :%i, failure: %i" % ( |
| 386 | success_count, fail_count)) |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 387 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 388 | # Depending on success or failure, the card goes either in the "error" bin or in the |
| 389 | # "done" bin. |
| 390 | if rc < 0: |
Philipp Maier | 91c971b | 2023-10-09 10:48:46 +0200 | [diff] [blame] | 391 | self.ch.error() |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 392 | else: |
Philipp Maier | 91c971b | 2023-10-09 10:48:46 +0200 | [diff] [blame] | 393 | self.ch.done() |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 394 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 395 | # In most cases it is possible to proceed with the next card, but the |
| 396 | # user may decide to halt immediately when an error occurs |
| 397 | if opts.halt_on_error and rc < 0: |
| 398 | return |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 399 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 400 | except (KeyboardInterrupt): |
| 401 | self.poutput("") |
| 402 | self.poutput("Terminated by user!") |
| 403 | return |
| 404 | except (SystemExit): |
| 405 | # When all cards are processed the card handler device will throw a SystemExit |
| 406 | # exception. Also Errors that are not recoverable (cards stuck etc.) will end up here. |
| 407 | # The user has the option to execute some action to make aware that the card handler |
| 408 | # needs service. |
| 409 | if(opts.on_stop_action): |
| 410 | os.system(opts.on_stop_action) |
| 411 | return |
| 412 | except: |
| 413 | self.poutput("") |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 414 | self.poutput("Card handling (%s) failed with an exception:" % str(self.sl)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 415 | self.poutput("---------------------8<---------------------") |
| 416 | traceback.print_exc() |
| 417 | self.poutput("---------------------8<---------------------") |
| 418 | self.poutput("") |
| 419 | fail_count = fail_count + 1 |
| 420 | self._show_failure_sign() |
| 421 | self.poutput("Statistics: success :%i, failure: %i" % |
| 422 | (success_count, fail_count)) |
Philipp Maier | b52feed | 2021-09-22 16:43:13 +0200 | [diff] [blame] | 423 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 424 | first = False |
| 425 | |
| 426 | echo_parser = argparse.ArgumentParser() |
Harald Welte | 977c592 | 2023-11-01 23:42:55 +0100 | [diff] [blame] | 427 | echo_parser.add_argument('string', help="string to echo on the shell", nargs='+') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 428 | |
| 429 | @cmd2.with_argparser(echo_parser) |
| 430 | @cmd2.with_category(CUSTOM_CATEGORY) |
| 431 | def do_echo(self, opts): |
| 432 | """Echo (print) a string on the console""" |
Harald Welte | 977c592 | 2023-11-01 23:42:55 +0100 | [diff] [blame] | 433 | self.poutput(' '.join(opts.string)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 434 | |
Harald Welte | fc31548 | 2022-07-23 12:49:14 +0200 | [diff] [blame] | 435 | @cmd2.with_category(CUSTOM_CATEGORY) |
| 436 | def do_version(self, opts): |
| 437 | """Print the pySim software version.""" |
| 438 | import pkg_resources |
| 439 | self.poutput(pkg_resources.get_distribution('pySim')) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 440 | |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 441 | @with_default_category('pySim Commands') |
| 442 | class PySimCommands(CommandSet): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 443 | def __init__(self): |
| 444 | super().__init__() |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 445 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 446 | dir_parser = argparse.ArgumentParser() |
| 447 | dir_parser.add_argument( |
| 448 | '--fids', help='Show file identifiers', action='store_true') |
| 449 | dir_parser.add_argument( |
| 450 | '--names', help='Show file names', action='store_true') |
| 451 | dir_parser.add_argument( |
| 452 | '--apps', help='Show applications', action='store_true') |
| 453 | dir_parser.add_argument( |
| 454 | '--all', help='Show all selectable identifiers and names', action='store_true') |
Philipp Maier | 5d3e259 | 2021-02-22 17:22:16 +0100 | [diff] [blame] | 455 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 456 | @cmd2.with_argparser(dir_parser) |
| 457 | def do_dir(self, opts): |
| 458 | """Show a listing of files available in currently selected DF or MF""" |
| 459 | if opts.all: |
| 460 | flags = [] |
| 461 | elif opts.fids or opts.names or opts.apps: |
| 462 | flags = ['PARENT', 'SELF'] |
| 463 | if opts.fids: |
| 464 | flags += ['FIDS', 'AIDS'] |
| 465 | if opts.names: |
| 466 | flags += ['FNAMES', 'ANAMES'] |
| 467 | if opts.apps: |
| 468 | flags += ['ANAMES', 'AIDS'] |
| 469 | else: |
| 470 | flags = ['PARENT', 'SELF', 'FNAMES', 'ANAMES'] |
| 471 | selectables = list( |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 472 | self._cmd.lchan.selected_file.get_selectable_names(flags=flags)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 473 | directory_str = tabulate_str_list( |
| 474 | selectables, width=79, hspace=2, lspace=1, align_left=True) |
Harald Welte | b2e4b4a | 2022-07-19 23:48:45 +0200 | [diff] [blame] | 475 | path = self._cmd.lchan.selected_file.fully_qualified_path_str(True) |
| 476 | self._cmd.poutput(path) |
| 477 | path = self._cmd.lchan.selected_file.fully_qualified_path_str(False) |
| 478 | self._cmd.poutput(path) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 479 | self._cmd.poutput(directory_str) |
| 480 | self._cmd.poutput("%d files" % len(selectables)) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 481 | |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 482 | def walk(self, indent=0, action_ef=None, action_df=None, context=None, **kwargs): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 483 | """Recursively walk through the file system, starting at the currently selected DF""" |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 484 | |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 485 | if isinstance(self._cmd.lchan.selected_file, CardDF): |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 486 | if action_df: |
Philipp Maier | 91c971b | 2023-10-09 10:48:46 +0200 | [diff] [blame] | 487 | action_df(context, **kwargs) |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 488 | |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 489 | files = self._cmd.lchan.selected_file.get_selectables( |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 490 | flags=['FNAMES', 'ANAMES']) |
| 491 | for f in files: |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 492 | # special case: When no action is performed, just output a directory |
| 493 | if not action_ef and not action_df: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 494 | output_str = " " * indent + str(f) + (" " * 250) |
| 495 | output_str = output_str[0:25] |
| 496 | if isinstance(files[f], CardADF): |
| 497 | output_str += " " + str(files[f].aid) |
| 498 | else: |
| 499 | output_str += " " + str(files[f].fid) |
| 500 | output_str += " " + str(files[f].desc) |
| 501 | self._cmd.poutput(output_str) |
Philipp Maier | f408a40 | 2021-04-09 21:16:12 +0200 | [diff] [blame] | 502 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 503 | if isinstance(files[f], CardDF): |
| 504 | skip_df = False |
| 505 | try: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 506 | fcp_dec = self._cmd.lchan.select(f, self._cmd) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 507 | except Exception as e: |
| 508 | skip_df = True |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 509 | df = self._cmd.lchan.selected_file |
Harald Welte | b2e4b4a | 2022-07-19 23:48:45 +0200 | [diff] [blame] | 510 | df_path = df.fully_qualified_path_str(True) |
| 511 | df_skip_reason_str = df_path + \ |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 512 | "/" + str(f) + ", " + str(e) |
| 513 | if context: |
| 514 | context['DF_SKIP'] += 1 |
| 515 | context['DF_SKIP_REASON'].append(df_skip_reason_str) |
Philipp Maier | f408a40 | 2021-04-09 21:16:12 +0200 | [diff] [blame] | 516 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 517 | # If the DF was skipped, we never have entered the directory |
| 518 | # below, so we must not move up. |
| 519 | if skip_df == False: |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 520 | self.walk(indent + 1, action_ef, action_df, context, **kwargs) |
Philipp Maier | 5482737 | 2023-10-24 16:18:30 +0200 | [diff] [blame] | 521 | |
| 522 | parent = self._cmd.lchan.selected_file.parent |
| 523 | df = self._cmd.lchan.selected_file |
| 524 | adf = self._cmd.lchan.selected_adf |
| 525 | if isinstance(parent, CardMF) and (adf and adf.has_fs == False): |
| 526 | # Not every application that may be present on a GlobalPlatform card will support the SELECT |
| 527 | # command as we know it from ETSI TS 102 221, section 11.1.1. In fact the only subset of |
| 528 | # SELECT we may rely on is the OPEN SELECT command as specified in GlobalPlatform Card |
| 529 | # Specification, section 11.9. Unfortunately the OPEN SELECT command only supports the |
| 530 | # "select by name" method, which means we can only select an application and not a file. |
| 531 | # The consequence of this is that we may get trapped in an application that does not have |
| 532 | # ISIM/USIM like file system support and the only way to leave that application is to select |
| 533 | # an ISIM/USIM application in order to get the file system access back. |
| 534 | # |
| 535 | # To automate this escape-route while traversing the file system we will check whether |
| 536 | # the parent file is the MF. When this is the case and the selected ADF has no file system |
| 537 | # support, we will select an arbitrary ADF that has file system support first and from there |
| 538 | # we will then select the MF. |
| 539 | for selectable in parent.get_selectables().items(): |
| 540 | if isinstance(selectable[1], CardADF) and selectable[1].has_fs == True: |
| 541 | self._cmd.lchan.select(selectable[1].name, self._cmd) |
| 542 | break |
| 543 | self._cmd.lchan.select(df.get_mf().name, self._cmd) |
| 544 | else: |
| 545 | # Normal DF/ADF selection |
| 546 | fcp_dec = self._cmd.lchan.select("..", self._cmd) |
Philipp Maier | f408a40 | 2021-04-09 21:16:12 +0200 | [diff] [blame] | 547 | |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 548 | elif action_ef: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 549 | df_before_action = self._cmd.lchan.selected_file |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 550 | action_ef(f, context, **kwargs) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 551 | # When walking through the file system tree the action must not |
| 552 | # always restore the currently selected file to the file that |
| 553 | # was selected before executing the action() callback. |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 554 | if df_before_action != self._cmd.lchan.selected_file: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 555 | raise RuntimeError("inconsistent walk, %s is currently selected but expecting %s to be selected" |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 556 | % (str(self._cmd.lchan.selected_file), str(df_before_action))) |
Philipp Maier | ff9dae2 | 2021-02-25 17:03:21 +0100 | [diff] [blame] | 557 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 558 | def do_tree(self, opts): |
| 559 | """Display a filesystem-tree with all selectable files""" |
| 560 | self.walk() |
Philipp Maier | ff9dae2 | 2021-02-25 17:03:21 +0100 | [diff] [blame] | 561 | |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 562 | def export_ef(self, filename, context, as_json): |
| 563 | """ Select and export a single elementary file (EF) """ |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 564 | context['COUNT'] += 1 |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 565 | df = self._cmd.lchan.selected_file |
Philipp Maier | ac34dcc | 2021-04-01 17:19:05 +0200 | [diff] [blame] | 566 | |
Philipp Maier | ea81f75 | 2022-05-19 10:13:30 +0200 | [diff] [blame] | 567 | # The currently selected file (not the file we are going to export) |
| 568 | # must always be an ADF or DF. From this starting point we select |
| 569 | # the EF we want to export. To maintain consistency we will then |
| 570 | # select the current DF again (see comment below). |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 571 | if not isinstance(df, CardDF): |
| 572 | raise RuntimeError( |
| 573 | "currently selected file %s is not a DF or ADF" % str(df)) |
Philipp Maier | ac34dcc | 2021-04-01 17:19:05 +0200 | [diff] [blame] | 574 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 575 | df_path_list = df.fully_qualified_path(True) |
Harald Welte | b2e4b4a | 2022-07-19 23:48:45 +0200 | [diff] [blame] | 576 | df_path = df.fully_qualified_path_str(True) |
| 577 | df_path_fid = df.fully_qualified_path_str(False) |
Philipp Maier | 24f7bd3 | 2021-02-25 17:06:18 +0100 | [diff] [blame] | 578 | |
Harald Welte | b2e4b4a | 2022-07-19 23:48:45 +0200 | [diff] [blame] | 579 | file_str = df_path + "/" + str(filename) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 580 | self._cmd.poutput(boxed_heading_str(file_str)) |
Philipp Maier | 24f7bd3 | 2021-02-25 17:06:18 +0100 | [diff] [blame] | 581 | |
Harald Welte | b2e4b4a | 2022-07-19 23:48:45 +0200 | [diff] [blame] | 582 | self._cmd.poutput("# directory: %s (%s)" % (df_path, df_path_fid)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 583 | try: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 584 | fcp_dec = self._cmd.lchan.select(filename, self._cmd) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 585 | self._cmd.poutput("# file: %s (%s)" % ( |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 586 | self._cmd.lchan.selected_file.name, self._cmd.lchan.selected_file.fid)) |
Philipp Maier | 24f7bd3 | 2021-02-25 17:06:18 +0100 | [diff] [blame] | 587 | |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 588 | structure = self._cmd.lchan.selected_file_structure() |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 589 | self._cmd.poutput("# structure: %s" % str(structure)) |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 590 | self._cmd.poutput("# RAW FCP Template: %s" % str(self._cmd.lchan.selected_file_fcp_hex)) |
| 591 | self._cmd.poutput("# Decoded FCP Template: %s" % str(self._cmd.lchan.selected_file_fcp)) |
Philipp Maier | 24f7bd3 | 2021-02-25 17:06:18 +0100 | [diff] [blame] | 592 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 593 | for f in df_path_list: |
| 594 | self._cmd.poutput("select " + str(f)) |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 595 | self._cmd.poutput("select " + self._cmd.lchan.selected_file.name) |
Philipp Maier | 24f7bd3 | 2021-02-25 17:06:18 +0100 | [diff] [blame] | 596 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 597 | if structure == 'transparent': |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 598 | if as_json: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 599 | result = self._cmd.lchan.read_binary_dec() |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 600 | self._cmd.poutput("update_binary_decoded '%s'" % json.dumps(result[0], cls=JsonEncoder)) |
| 601 | else: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 602 | result = self._cmd.lchan.read_binary() |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 603 | self._cmd.poutput("update_binary " + str(result[0])) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 604 | elif structure == 'cyclic' or structure == 'linear_fixed': |
| 605 | # Use number of records specified in select response |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 606 | num_of_rec = self._cmd.lchan.selected_file_num_of_rec() |
Harald Welte | 747a978 | 2022-02-13 17:52:28 +0100 | [diff] [blame] | 607 | if num_of_rec: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 608 | for r in range(1, num_of_rec + 1): |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 609 | if as_json: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 610 | result = self._cmd.lchan.read_record_dec(r) |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 611 | self._cmd.poutput("update_record_decoded %d '%s'" % (r, json.dumps(result[0], cls=JsonEncoder))) |
| 612 | else: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 613 | result = self._cmd.lchan.read_record(r) |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 614 | self._cmd.poutput("update_record %d %s" % (r, str(result[0]))) |
| 615 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 616 | # When the select response does not return the number of records, read until we hit the |
| 617 | # first record that cannot be read. |
| 618 | else: |
| 619 | r = 1 |
| 620 | while True: |
| 621 | try: |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 622 | if as_json: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 623 | result = self._cmd.lchan.read_record_dec(r) |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 624 | self._cmd.poutput("update_record_decoded %d '%s'" % (r, json.dumps(result[0], cls=JsonEncoder))) |
| 625 | else: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 626 | result = self._cmd.lchan.read_record(r) |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 627 | self._cmd.poutput("update_record %d %s" % (r, str(result[0]))) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 628 | except SwMatchError as e: |
| 629 | # We are past the last valid record - stop |
| 630 | if e.sw_actual == "9402": |
| 631 | break |
| 632 | # Some other problem occurred |
| 633 | else: |
| 634 | raise e |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 635 | r = r + 1 |
| 636 | elif structure == 'ber_tlv': |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 637 | tags = self._cmd.lchan.retrieve_tags() |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 638 | for t in tags: |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 639 | result = self._cmd.lchan.retrieve_data(t) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 640 | (tag, l, val, remainer) = bertlv_parse_one(h2b(result[0])) |
| 641 | self._cmd.poutput("set_data 0x%02x %s" % (t, b2h(val))) |
| 642 | else: |
| 643 | raise RuntimeError( |
| 644 | 'Unsupported structure "%s" of file "%s"' % (structure, filename)) |
| 645 | except Exception as e: |
Harald Welte | b2e4b4a | 2022-07-19 23:48:45 +0200 | [diff] [blame] | 646 | bad_file_str = df_path + "/" + str(filename) + ", " + str(e) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 647 | self._cmd.poutput("# bad file: %s" % bad_file_str) |
| 648 | context['ERR'] += 1 |
| 649 | context['BAD'].append(bad_file_str) |
Philipp Maier | 24f7bd3 | 2021-02-25 17:06:18 +0100 | [diff] [blame] | 650 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 651 | # When reading the file is done, make sure the parent file is |
| 652 | # selected again. This will be the usual case, however we need |
| 653 | # to check before since we must not select the same DF twice |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 654 | if df != self._cmd.lchan.selected_file: |
| 655 | self._cmd.lchan.select(df.fid or df.aid, self._cmd) |
Philipp Maier | ac34dcc | 2021-04-01 17:19:05 +0200 | [diff] [blame] | 656 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 657 | self._cmd.poutput("#") |
Philipp Maier | 24f7bd3 | 2021-02-25 17:06:18 +0100 | [diff] [blame] | 658 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 659 | export_parser = argparse.ArgumentParser() |
| 660 | export_parser.add_argument( |
| 661 | '--filename', type=str, default=None, help='only export specific file') |
Harald Welte | 08b11ab | 2022-02-10 18:56:41 +0100 | [diff] [blame] | 662 | export_parser.add_argument( |
| 663 | '--json', action='store_true', help='export as JSON (less reliable)') |
Philipp Maier | 24f7bd3 | 2021-02-25 17:06:18 +0100 | [diff] [blame] | 664 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 665 | @cmd2.with_argparser(export_parser) |
| 666 | def do_export(self, opts): |
| 667 | """Export files to script that can be imported back later""" |
| 668 | context = {'ERR': 0, 'COUNT': 0, 'BAD': [], |
| 669 | 'DF_SKIP': 0, 'DF_SKIP_REASON': []} |
Philipp Maier | 9a4091d | 2022-05-19 10:20:30 +0200 | [diff] [blame] | 670 | kwargs_export = {'as_json': opts.json} |
Philipp Maier | f16ac6a | 2022-05-31 14:08:47 +0200 | [diff] [blame] | 671 | exception_str_add = "" |
| 672 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 673 | if opts.filename: |
Philipp Maier | 7b138b0 | 2022-05-31 13:42:56 +0200 | [diff] [blame] | 674 | self.export_ef(opts.filename, context, **kwargs_export) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 675 | else: |
Philipp Maier | f16ac6a | 2022-05-31 14:08:47 +0200 | [diff] [blame] | 676 | try: |
| 677 | self.walk(0, self.export_ef, None, context, **kwargs_export) |
| 678 | except Exception as e: |
| 679 | print("# Stopping early here due to exception: " + str(e)) |
| 680 | print("#") |
| 681 | exception_str_add = ", also had to stop early due to exception:" + str(e) |
Philipp Maier | 80ce71f | 2021-04-19 21:24:23 +0200 | [diff] [blame] | 682 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 683 | self._cmd.poutput(boxed_heading_str("Export summary")) |
Philipp Maier | 80ce71f | 2021-04-19 21:24:23 +0200 | [diff] [blame] | 684 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 685 | self._cmd.poutput("# total files visited: %u" % context['COUNT']) |
| 686 | self._cmd.poutput("# bad files: %u" % context['ERR']) |
| 687 | for b in context['BAD']: |
| 688 | self._cmd.poutput("# " + b) |
Philipp Maier | f408a40 | 2021-04-09 21:16:12 +0200 | [diff] [blame] | 689 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 690 | self._cmd.poutput("# skipped dedicated files(s): %u" % |
| 691 | context['DF_SKIP']) |
| 692 | for b in context['DF_SKIP_REASON']: |
| 693 | self._cmd.poutput("# " + b) |
Philipp Maier | f408a40 | 2021-04-09 21:16:12 +0200 | [diff] [blame] | 694 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 695 | if context['ERR'] and context['DF_SKIP']: |
Philipp Maier | f16ac6a | 2022-05-31 14:08:47 +0200 | [diff] [blame] | 696 | raise RuntimeError("unable to export %i elementary file(s) and %i dedicated file(s)%s" % ( |
| 697 | context['ERR'], context['DF_SKIP'], exception_str_add)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 698 | elif context['ERR']: |
| 699 | raise RuntimeError( |
Philipp Maier | f16ac6a | 2022-05-31 14:08:47 +0200 | [diff] [blame] | 700 | "unable to export %i elementary file(s)%s" % (context['ERR'], exception_str_add)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 701 | elif context['DF_SKIP']: |
| 702 | raise RuntimeError( |
Philipp Maier | f16ac6a | 2022-05-31 14:08:47 +0200 | [diff] [blame] | 703 | "unable to export %i dedicated files(s)%s" % (context['ERR'], exception_str_add)) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 704 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 705 | def do_reset(self, opts): |
| 706 | """Reset the Card.""" |
Philipp Maier | e345e11 | 2023-06-09 15:19:56 +0200 | [diff] [blame] | 707 | atr = self._cmd.card.reset() |
| 708 | self._cmd.poutput('Card ATR: %s' % i2h(atr)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 709 | self._cmd.update_prompt() |
Harald Welte | daf2b39 | 2021-05-03 23:17:29 +0200 | [diff] [blame] | 710 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 711 | def do_desc(self, opts): |
| 712 | """Display human readable file description for the currently selected file""" |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 713 | desc = self._cmd.lchan.selected_file.desc |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 714 | if desc: |
| 715 | self._cmd.poutput(desc) |
| 716 | else: |
| 717 | self._cmd.poutput("no description available") |
Philipp Maier | a8c9ea9 | 2021-09-16 12:51:46 +0200 | [diff] [blame] | 718 | |
Harald Welte | 469db93 | 2023-11-01 23:17:06 +0100 | [diff] [blame] | 719 | verify_adm_parser = argparse.ArgumentParser() |
Harald Welte | f9ea63e | 2023-11-01 23:35:31 +0100 | [diff] [blame] | 720 | verify_adm_parser.add_argument('ADM1', nargs='?', type=is_hexstr_or_decimal, |
Harald Welte | 469db93 | 2023-11-01 23:17:06 +0100 | [diff] [blame] | 721 | help='ADM1 pin value. If none given, CSV file will be queried') |
| 722 | |
| 723 | @cmd2.with_argparser(verify_adm_parser) |
| 724 | def do_verify_adm(self, opts): |
| 725 | """Verify the ADM (Administrator) PIN specified as argument. This is typically needed in order |
| 726 | to get write/update permissions to most of the files on SIM cards. |
| 727 | |
| 728 | Currently only ADM1 is supported.""" |
| 729 | if opts.ADM1: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 730 | # use specified ADM-PIN |
Harald Welte | 469db93 | 2023-11-01 23:17:06 +0100 | [diff] [blame] | 731 | pin_adm = sanitize_pin_adm(opts.ADM1) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 732 | else: |
| 733 | # try to find an ADM-PIN if none is specified |
| 734 | result = card_key_provider_get_field( |
| 735 | 'ADM1', key='ICCID', value=self._cmd.iccid) |
| 736 | pin_adm = sanitize_pin_adm(result) |
| 737 | if pin_adm: |
| 738 | self._cmd.poutput( |
| 739 | "found ADM-PIN '%s' for ICCID '%s'" % (result, self._cmd.iccid)) |
| 740 | else: |
| 741 | raise ValueError( |
| 742 | "cannot find ADM-PIN for ICCID '%s'" % (self._cmd.iccid)) |
Philipp Maier | a8c9ea9 | 2021-09-16 12:51:46 +0200 | [diff] [blame] | 743 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 744 | if pin_adm: |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 745 | self._cmd.lchan.scc.verify_chv(self._cmd.card._adm_chv_num, h2b(pin_adm)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 746 | else: |
| 747 | raise ValueError("error: cannot authenticate, no adm-pin!") |
| 748 | |
Philipp Maier | 7b9e244 | 2023-03-22 15:19:54 +0100 | [diff] [blame] | 749 | def do_cardinfo(self, opts): |
| 750 | """Display information about the currently inserted card""" |
| 751 | self._cmd.poutput("Card info:") |
| 752 | self._cmd.poutput(" Name: %s" % self._cmd.card.name) |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 753 | self._cmd.poutput(" ATR: %s" % b2h(self._cmd.lchan.scc.get_atr())) |
Philipp Maier | 7b9e244 | 2023-03-22 15:19:54 +0100 | [diff] [blame] | 754 | self._cmd.poutput(" ICCID: %s" % self._cmd.iccid) |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 755 | self._cmd.poutput(" Class-Byte: %s" % self._cmd.lchan.scc.cla_byte) |
| 756 | self._cmd.poutput(" Select-Ctrl: %s" % self._cmd.lchan.scc.sel_ctrl) |
Philipp Maier | 7b9e244 | 2023-03-22 15:19:54 +0100 | [diff] [blame] | 757 | self._cmd.poutput(" AIDs:") |
| 758 | for a in self._cmd.rs.mf.applications: |
| 759 | self._cmd.poutput(" %s" % a) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 760 | |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 761 | @with_default_category('ISO7816 Commands') |
| 762 | class Iso7816Commands(CommandSet): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 763 | def __init__(self): |
| 764 | super().__init__() |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 765 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 766 | def do_select(self, opts): |
| 767 | """SELECT a File (ADF/DF/EF)""" |
| 768 | if len(opts.arg_list) == 0: |
Harald Welte | b2e4b4a | 2022-07-19 23:48:45 +0200 | [diff] [blame] | 769 | path = self._cmd.lchan.selected_file.fully_qualified_path_str(True) |
| 770 | path_fid = self._cmd.lchan.selected_file.fully_qualified_path_str(False) |
| 771 | self._cmd.poutput("currently selected file: %s (%s)" % (path, path_fid)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 772 | return |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 773 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 774 | path = opts.arg_list[0] |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 775 | fcp_dec = self._cmd.lchan.select(path, self._cmd) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 776 | self._cmd.update_prompt() |
| 777 | self._cmd.poutput_json(fcp_dec) |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 778 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 779 | def complete_select(self, text, line, begidx, endidx) -> List[str]: |
| 780 | """Command Line tab completion for SELECT""" |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 781 | index_dict = {1: self._cmd.lchan.selected_file.get_selectable_names()} |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 782 | return self._cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict) |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 783 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 784 | def get_code(self, code): |
| 785 | """Use code either directly or try to get it from external data source""" |
| 786 | auto = ('PIN1', 'PIN2', 'PUK1', 'PUK2') |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 787 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 788 | if str(code).upper() not in auto: |
| 789 | return sanitize_pin_adm(code) |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 790 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 791 | result = card_key_provider_get_field( |
| 792 | str(code), key='ICCID', value=self._cmd.iccid) |
| 793 | result = sanitize_pin_adm(result) |
| 794 | if result: |
| 795 | self._cmd.poutput("found %s '%s' for ICCID '%s'" % |
| 796 | (code.upper(), result, self._cmd.iccid)) |
| 797 | else: |
| 798 | self._cmd.poutput("cannot find %s for ICCID '%s'" % |
| 799 | (code.upper(), self._cmd.iccid)) |
| 800 | return result |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 801 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 802 | verify_chv_parser = argparse.ArgumentParser() |
| 803 | verify_chv_parser.add_argument( |
| 804 | '--pin-nr', type=int, default=1, help='PIN Number, 1=PIN1, 2=PIN2 or custom value (decimal)') |
| 805 | verify_chv_parser.add_argument( |
Harald Welte | 1c849f8 | 2023-11-01 23:48:28 +0100 | [diff] [blame] | 806 | 'pin_code', type=is_decimal, help='PIN code digits, \"PIN1\" or \"PIN2\" to get PIN code from external data source') |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 807 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 808 | @cmd2.with_argparser(verify_chv_parser) |
| 809 | def do_verify_chv(self, opts): |
Harald Welte | 12af793 | 2022-02-15 16:39:08 +0100 | [diff] [blame] | 810 | """Verify (authenticate) using specified CHV (PIN) code, which is how the specifications |
| 811 | call it if you authenticate yourself using the specified PIN. There usually is at least PIN1 and |
| 812 | PIN2.""" |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 813 | pin = self.get_code(opts.pin_code) |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 814 | (data, sw) = self._cmd.lchan.scc.verify_chv(opts.pin_nr, h2b(pin)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 815 | self._cmd.poutput("CHV verification successful") |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 816 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 817 | unblock_chv_parser = argparse.ArgumentParser() |
| 818 | unblock_chv_parser.add_argument( |
| 819 | '--pin-nr', type=int, default=1, help='PUK Number, 1=PIN1, 2=PIN2 or custom value (decimal)') |
| 820 | unblock_chv_parser.add_argument( |
Harald Welte | 1c849f8 | 2023-11-01 23:48:28 +0100 | [diff] [blame] | 821 | 'puk_code', type=is_decimal, help='PUK code digits \"PUK1\" or \"PUK2\" to get PUK code from external data source') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 822 | unblock_chv_parser.add_argument( |
Harald Welte | 1c849f8 | 2023-11-01 23:48:28 +0100 | [diff] [blame] | 823 | 'new_pin_code', type=is_decimal, help='PIN code digits \"PIN1\" or \"PIN2\" to get PIN code from external data source') |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 824 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 825 | @cmd2.with_argparser(unblock_chv_parser) |
| 826 | def do_unblock_chv(self, opts): |
| 827 | """Unblock PIN code using specified PUK code""" |
| 828 | new_pin = self.get_code(opts.new_pin_code) |
| 829 | puk = self.get_code(opts.puk_code) |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 830 | (data, sw) = self._cmd.lchan.scc.unblock_chv( |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 831 | opts.pin_nr, h2b(puk), h2b(new_pin)) |
| 832 | self._cmd.poutput("CHV unblock successful") |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 833 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 834 | change_chv_parser = argparse.ArgumentParser() |
| 835 | change_chv_parser.add_argument( |
| 836 | '--pin-nr', type=int, default=1, help='PUK Number, 1=PIN1, 2=PIN2 or custom value (decimal)') |
| 837 | change_chv_parser.add_argument( |
Harald Welte | 1c849f8 | 2023-11-01 23:48:28 +0100 | [diff] [blame] | 838 | 'pin_code', type=is_decimal, help='PIN code digits \"PIN1\" or \"PIN2\" to get PIN code from external data source') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 839 | change_chv_parser.add_argument( |
Harald Welte | 1c849f8 | 2023-11-01 23:48:28 +0100 | [diff] [blame] | 840 | 'new_pin_code', type=is_decimal, help='PIN code digits \"PIN1\" or \"PIN2\" to get PIN code from external data source') |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 841 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 842 | @cmd2.with_argparser(change_chv_parser) |
| 843 | def do_change_chv(self, opts): |
| 844 | """Change PIN code to a new PIN code""" |
| 845 | new_pin = self.get_code(opts.new_pin_code) |
| 846 | pin = self.get_code(opts.pin_code) |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 847 | (data, sw) = self._cmd.lchan.scc.change_chv( |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 848 | opts.pin_nr, h2b(pin), h2b(new_pin)) |
| 849 | self._cmd.poutput("CHV change successful") |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 850 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 851 | disable_chv_parser = argparse.ArgumentParser() |
| 852 | disable_chv_parser.add_argument( |
| 853 | '--pin-nr', type=int, default=1, help='PIN Number, 1=PIN1, 2=PIN2 or custom value (decimal)') |
| 854 | disable_chv_parser.add_argument( |
Harald Welte | 1c849f8 | 2023-11-01 23:48:28 +0100 | [diff] [blame] | 855 | 'pin_code', type=is_decimal, help='PIN code digits, \"PIN1\" or \"PIN2\" to get PIN code from external data source') |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 856 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 857 | @cmd2.with_argparser(disable_chv_parser) |
| 858 | def do_disable_chv(self, opts): |
| 859 | """Disable PIN code using specified PIN code""" |
| 860 | pin = self.get_code(opts.pin_code) |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 861 | (data, sw) = self._cmd.lchan.scc.disable_chv(opts.pin_nr, h2b(pin)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 862 | self._cmd.poutput("CHV disable successful") |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 863 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 864 | enable_chv_parser = argparse.ArgumentParser() |
| 865 | enable_chv_parser.add_argument( |
| 866 | '--pin-nr', type=int, default=1, help='PIN Number, 1=PIN1, 2=PIN2 or custom value (decimal)') |
| 867 | enable_chv_parser.add_argument( |
Harald Welte | 1c849f8 | 2023-11-01 23:48:28 +0100 | [diff] [blame] | 868 | 'pin_code', type=is_decimal, help='PIN code digits, \"PIN1\" or \"PIN2\" to get PIN code from external data source') |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 869 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 870 | @cmd2.with_argparser(enable_chv_parser) |
| 871 | def do_enable_chv(self, opts): |
| 872 | """Enable PIN code using specified PIN code""" |
| 873 | pin = self.get_code(opts.pin_code) |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 874 | (data, sw) = self._cmd.lchan.scc.enable_chv(opts.pin_nr, h2b(pin)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 875 | self._cmd.poutput("CHV enable successful") |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 876 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 877 | def do_deactivate_file(self, opts): |
Harald Welte | 799c354 | 2022-02-15 15:56:28 +0100 | [diff] [blame] | 878 | """Deactivate the currently selected EF""" |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 879 | (data, sw) = self._cmd.lchan.scc.deactivate_file() |
Harald Welte | a463161 | 2021-04-10 18:17:55 +0200 | [diff] [blame] | 880 | |
Harald Welte | 799c354 | 2022-02-15 15:56:28 +0100 | [diff] [blame] | 881 | activate_file_parser = argparse.ArgumentParser() |
| 882 | activate_file_parser.add_argument('NAME', type=str, help='File name or FID of file to activate') |
| 883 | @cmd2.with_argparser(activate_file_parser) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 884 | def do_activate_file(self, opts): |
Harald Welte | cd18ed0 | 2024-01-15 09:21:03 +0100 | [diff] [blame^] | 885 | """Activate the specified EF by sending an ACTIVATE FILE apdu command (used to be called REHABILITATE |
| 886 | in TS 11.11 for classic SIM). |
| 887 | |
| 888 | This command is used to (re-)activate a file that is currently in deactivated (sometimes also called |
| 889 | "invalidated") state. You need to call this from the DF above the to-be-activated EF and specify the name or |
| 890 | FID of the file to activate. |
| 891 | |
| 892 | Note that for *deactivation* the to-be-deactivated EF must be selected, but for *activation*, the DF |
| 893 | above the to-be-activated EF must be selected!""" |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 894 | (data, sw) = self._cmd.lchan.activate_file(opts.NAME) |
Harald Welte | 485692b | 2021-05-25 22:21:44 +0200 | [diff] [blame] | 895 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 896 | def complete_activate_file(self, text, line, begidx, endidx) -> List[str]: |
| 897 | """Command Line tab completion for ACTIVATE FILE""" |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 898 | index_dict = {1: self._cmd.lchan.selected_file.get_selectable_names()} |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 899 | return self._cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict) |
Harald Welte | 31d2cf0 | 2021-04-03 10:47:29 +0200 | [diff] [blame] | 900 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 901 | open_chan_parser = argparse.ArgumentParser() |
| 902 | open_chan_parser.add_argument( |
| 903 | 'chan_nr', type=int, default=0, help='Channel Number') |
Harald Welte | 703f933 | 2021-04-10 18:39:32 +0200 | [diff] [blame] | 904 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 905 | @cmd2.with_argparser(open_chan_parser) |
| 906 | def do_open_channel(self, opts): |
| 907 | """Open a logical channel.""" |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 908 | (data, sw) = self._cmd.lchan.scc.manage_channel( |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 909 | mode='open', lchan_nr=opts.chan_nr) |
Harald Welte | bdf5957 | 2023-10-21 20:06:19 +0200 | [diff] [blame] | 910 | # this is executed only in successful case, as unsuccessful raises exception |
| 911 | self._cmd.lchan.add_lchan(opts.chan_nr) |
Harald Welte | 703f933 | 2021-04-10 18:39:32 +0200 | [diff] [blame] | 912 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 913 | close_chan_parser = argparse.ArgumentParser() |
| 914 | close_chan_parser.add_argument( |
| 915 | 'chan_nr', type=int, default=0, help='Channel Number') |
Harald Welte | 703f933 | 2021-04-10 18:39:32 +0200 | [diff] [blame] | 916 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 917 | @cmd2.with_argparser(close_chan_parser) |
| 918 | def do_close_channel(self, opts): |
| 919 | """Close a logical channel.""" |
Harald Welte | 4625512 | 2023-10-21 23:40:42 +0200 | [diff] [blame] | 920 | (data, sw) = self._cmd.lchan.scc.manage_channel( |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 921 | mode='close', lchan_nr=opts.chan_nr) |
Harald Welte | bdf5957 | 2023-10-21 20:06:19 +0200 | [diff] [blame] | 922 | # this is executed only in successful case, as unsuccessful raises exception |
| 923 | self._cmd.rs.del_lchan(opts.chan_nr) |
Harald Welte | 703f933 | 2021-04-10 18:39:32 +0200 | [diff] [blame] | 924 | |
Harald Welte | 2065099 | 2023-10-21 23:47:02 +0200 | [diff] [blame] | 925 | switch_chan_parser = argparse.ArgumentParser() |
| 926 | switch_chan_parser.add_argument( |
| 927 | 'chan_nr', type=int, default=0, help='Channel Number') |
| 928 | |
| 929 | @cmd2.with_argparser(switch_chan_parser) |
| 930 | def do_switch_channel(self, opts): |
| 931 | """Switch currently active logical channel.""" |
| 932 | self._cmd.lchan._select_pre(self._cmd) |
| 933 | self._cmd.lchan = self._cmd.rs.lchan[opts.chan_nr] |
| 934 | self._cmd.lchan._select_post(self._cmd) |
| 935 | self._cmd.update_prompt() |
| 936 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 937 | def do_status(self, opts): |
| 938 | """Perform the STATUS command.""" |
Harald Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 939 | fcp_dec = self._cmd.lchan.status() |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 940 | self._cmd.poutput_json(fcp_dec) |
Harald Welte | 34b05d3 | 2021-05-25 22:03:13 +0200 | [diff] [blame] | 941 | |
Harald Welte | ec95053 | 2021-10-20 13:09:00 +0200 | [diff] [blame] | 942 | |
Harald Welte | cab26c7 | 2022-08-06 16:12:30 +0200 | [diff] [blame] | 943 | class Proact(ProactiveHandler): |
| 944 | def receive_fetch(self, pcmd: ProactiveCommand): |
| 945 | # print its parsed representation |
| 946 | print(pcmd.decoded) |
| 947 | # TODO: implement the basics, such as SMS Sending, ... |
| 948 | |
| 949 | |
Harald Welte | 703f933 | 2021-04-10 18:39:32 +0200 | [diff] [blame] | 950 | |
Harald Welte | f422eb1 | 2023-06-09 11:15:09 +0200 | [diff] [blame] | 951 | option_parser = argparse.ArgumentParser(description='interactive SIM card shell', |
Harald Welte | f2e761c | 2021-04-11 11:56:44 +0200 | [diff] [blame] | 952 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 953 | argparse_add_reader_args(option_parser) |
Harald Welte | c8ff026 | 2021-04-11 12:06:13 +0200 | [diff] [blame] | 954 | |
| 955 | global_group = option_parser.add_argument_group('General Options') |
| 956 | global_group.add_argument('--script', metavar='PATH', default=None, |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 957 | help='script with pySim-shell commands to be executed automatically at start-up') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 958 | global_group.add_argument('--csv', metavar='FILE', |
| 959 | default=None, help='Read card data from CSV file') |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 960 | global_group.add_argument("--card_handler", dest="card_handler_config", metavar="FILE", |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 961 | help="Use automatic card handling machine") |
Harald Welte | c8ff026 | 2021-04-11 12:06:13 +0200 | [diff] [blame] | 962 | |
| 963 | adm_group = global_group.add_mutually_exclusive_group() |
| 964 | adm_group.add_argument('-a', '--pin-adm', metavar='PIN_ADM1', dest='pin_adm', default=None, |
| 965 | help='ADM PIN used for provisioning (overwrites default)') |
| 966 | adm_group.add_argument('-A', '--pin-adm-hex', metavar='PIN_ADM1_HEX', dest='pin_adm_hex', default=None, |
| 967 | help='ADM PIN used for provisioning, as hex string (16 characters long)') |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 968 | |
Harald Welte | 38306df | 2023-07-11 21:17:55 +0200 | [diff] [blame] | 969 | option_parser.add_argument("command", nargs='?', |
| 970 | help="A pySim-shell command that would optionally be executed at startup") |
| 971 | option_parser.add_argument('command_args', nargs=argparse.REMAINDER, |
| 972 | help="Optional Arguments for command") |
| 973 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 974 | |
| 975 | if __name__ == '__main__': |
| 976 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 977 | # Parse options |
| 978 | opts = option_parser.parse_args() |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 979 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 980 | # If a script file is specified, be sure that it actually exists |
| 981 | if opts.script: |
| 982 | if not os.access(opts.script, os.R_OK): |
| 983 | print("Invalid script file!") |
| 984 | sys.exit(2) |
Philipp Maier | 13e258d | 2021-04-08 17:48:49 +0200 | [diff] [blame] | 985 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 986 | # Register csv-file as card data provider, either from specified CSV |
| 987 | # or from CSV file in home directory |
| 988 | csv_default = str(Path.home()) + "/.osmocom/pysim/card_data.csv" |
| 989 | if opts.csv: |
| 990 | card_key_provider_register(CardKeyProviderCsv(opts.csv)) |
| 991 | if os.path.isfile(csv_default): |
| 992 | card_key_provider_register(CardKeyProviderCsv(csv_default)) |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 993 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 994 | # Init card reader driver |
Harald Welte | cab26c7 | 2022-08-06 16:12:30 +0200 | [diff] [blame] | 995 | sl = init_reader(opts, proactive_handler = Proact()) |
Philipp Maier | ea95c39 | 2021-09-16 13:10:19 +0200 | [diff] [blame] | 996 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 997 | # Create a card handler (for bulk provisioning) |
| 998 | if opts.card_handler_config: |
| 999 | ch = CardHandlerAuto(None, opts.card_handler_config) |
| 1000 | else: |
| 1001 | ch = CardHandler(sl) |
Philipp Maier | 7666764 | 2021-09-22 16:53:22 +0200 | [diff] [blame] | 1002 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 1003 | # Detect and initialize the card in the reader. This may fail when there |
| 1004 | # is no card in the reader or the card is unresponsive. PysimApp is |
| 1005 | # able to tolerate and recover from that. |
| 1006 | try: |
| 1007 | rs, card = init_card(sl) |
| 1008 | app = PysimApp(card, rs, sl, ch, opts.script) |
| 1009 | except: |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 1010 | print("Card initialization (%s) failed with an exception:" % str(sl)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 1011 | print("---------------------8<---------------------") |
| 1012 | traceback.print_exc() |
| 1013 | print("---------------------8<---------------------") |
| 1014 | print("(you may still try to recover from this manually by using the 'equip' command.)") |
| 1015 | print( |
| 1016 | " it should also be noted that some readers may behave strangely when no card") |
| 1017 | print(" is inserted.)") |
| 1018 | print("") |
Philipp Maier | eb3b0dd | 2023-11-23 11:46:39 +0100 | [diff] [blame] | 1019 | if opts.script: |
| 1020 | print("will not execute startup script due to card initialization errors!") |
| 1021 | app = PysimApp(None, None, sl, ch) |
Philipp Maier | ea95c39 | 2021-09-16 13:10:19 +0200 | [diff] [blame] | 1022 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 1023 | # If the user supplies an ADM PIN at via commandline args authenticate |
| 1024 | # immediately so that the user does not have to use the shell commands |
| 1025 | pin_adm = sanitize_pin_adm(opts.pin_adm, opts.pin_adm_hex) |
| 1026 | if pin_adm: |
| 1027 | if not card: |
| 1028 | print("Card error, cannot do ADM verification with supplied ADM pin now.") |
| 1029 | try: |
Philipp Maier | 4840d4d | 2023-09-06 14:54:14 +0200 | [diff] [blame] | 1030 | card._scc.verify_chv(card._adm_chv_num, h2b(pin_adm)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 1031 | except Exception as e: |
| 1032 | print(e) |
Philipp Maier | 228c98e | 2021-03-10 20:14:06 +0100 | [diff] [blame] | 1033 | |
Harald Welte | 38306df | 2023-07-11 21:17:55 +0200 | [diff] [blame] | 1034 | if opts.command: |
| 1035 | app.onecmd_plus_hooks('{} {}'.format(opts.command, ' '.join(opts.command_args))) |
| 1036 | else: |
| 1037 | app.cmdloop() |