Daniel Willmann | de07b95 | 2020-10-19 10:32:34 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 2 | |
| 3 | # |
| 4 | # Utility to deal with sim cards and program the 'magic' ones easily |
| 5 | # |
| 6 | # |
| 7 | # Part of the sim link code of inspired by pySimReader-Serial-src-v2 |
| 8 | # |
| 9 | # |
| 10 | # Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com> |
| 11 | # Copyright (C) 2010 Harald Welte <laforge@gnumonks.org> |
| 12 | # |
| 13 | # This program is free software: you can redistribute it and/or modify |
| 14 | # it under the terms of the GNU General Public License as published by |
| 15 | # the Free Software Foundation, either version 2 of the License, or |
| 16 | # (at your option) any later version. |
| 17 | # |
| 18 | # This program is distributed in the hope that it will be useful, |
| 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | # GNU General Public License for more details. |
| 22 | # |
| 23 | # You should have received a copy of the GNU General Public License |
| 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | # |
| 26 | |
| 27 | import hashlib |
| 28 | from optparse import OptionParser |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 29 | import os |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 30 | import random |
| 31 | import re |
| 32 | import sys |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 33 | import traceback |
Denis 'GNUtoo' Carikli | 79f5b60 | 2020-02-15 04:02:57 +0700 | [diff] [blame] | 34 | import json |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 35 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 36 | from pySim.commands import SimCardCommands |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 37 | from pySim.transport import init_reader |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 38 | from pySim.cards import _cards_classes, card_detect |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 39 | from pySim.utils import h2b, swap_nibbles, rpad, derive_milenage_opc, calculate_luhn, dec_iccid |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 40 | from pySim.ts_51_011 import EF, EF_AD |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 41 | from pySim.card_handler import * |
Philipp Maier | 7592eee | 2019-09-12 13:03:23 +0200 | [diff] [blame] | 42 | from pySim.utils import * |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 43 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 44 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 45 | def parse_options(): |
| 46 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 47 | parser = OptionParser(usage="usage: %prog [options]") |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 48 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 49 | parser.add_option("-d", "--device", dest="device", metavar="DEV", |
| 50 | help="Serial Device for SIM access [default: %default]", |
| 51 | default="/dev/ttyUSB0", |
| 52 | ) |
| 53 | parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD", |
| 54 | help="Baudrate used for SIM access [default: %default]", |
| 55 | default=9600, |
| 56 | ) |
| 57 | parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC", |
| 58 | help="Which PC/SC reader number for SIM access", |
| 59 | default=None, |
| 60 | ) |
| 61 | parser.add_option("--modem-device", dest="modem_dev", metavar="DEV", |
| 62 | help="Serial port of modem for Generic SIM Access (3GPP TS 27.007)", |
| 63 | default=None, |
| 64 | ) |
| 65 | parser.add_option("--modem-baud", dest="modem_baud", type="int", metavar="BAUD", |
| 66 | help="Baudrate used for modem's port [default: %default]", |
| 67 | default=115200, |
| 68 | ) |
| 69 | parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH", |
| 70 | help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)", |
| 71 | default=None, |
| 72 | ) |
| 73 | parser.add_option("-t", "--type", dest="type", |
| 74 | help="Card type (user -t list to view) [default: %default]", |
| 75 | default="auto", |
| 76 | ) |
| 77 | parser.add_option("-T", "--probe", dest="probe", |
| 78 | help="Determine card type", |
| 79 | default=False, action="store_true" |
| 80 | ) |
| 81 | parser.add_option("-a", "--pin-adm", dest="pin_adm", |
| 82 | help="ADM PIN used for provisioning (overwrites default)", |
| 83 | ) |
| 84 | parser.add_option("-A", "--pin-adm-hex", dest="pin_adm_hex", |
| 85 | help="ADM PIN used for provisioning, as hex string (16 characters long", |
| 86 | ) |
| 87 | parser.add_option("-e", "--erase", dest="erase", action='store_true', |
| 88 | help="Erase beforehand [default: %default]", |
| 89 | default=False, |
| 90 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 91 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 92 | parser.add_option("-S", "--source", dest="source", |
| 93 | help="Data Source[default: %default]", |
| 94 | default="cmdline", |
| 95 | ) |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 96 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 97 | # if mode is "cmdline" |
| 98 | parser.add_option("-n", "--name", dest="name", |
| 99 | help="Operator name [default: %default]", |
| 100 | default="Magic", |
| 101 | ) |
| 102 | parser.add_option("-c", "--country", dest="country", type="int", metavar="CC", |
| 103 | help="Country code [default: %default]", |
| 104 | default=1, |
| 105 | ) |
| 106 | parser.add_option("-x", "--mcc", dest="mcc", type="string", |
| 107 | help="Mobile Country Code [default: %default]", |
| 108 | default="901", |
| 109 | ) |
| 110 | parser.add_option("-y", "--mnc", dest="mnc", type="string", |
| 111 | help="Mobile Network Code [default: %default]", |
| 112 | default="55", |
| 113 | ) |
| 114 | parser.add_option("--mnclen", dest="mnclen", type="choice", |
| 115 | help="Length of Mobile Network Code [default: %default]", |
| 116 | default=2, |
| 117 | choices=[2, 3], |
| 118 | ) |
| 119 | parser.add_option("-m", "--smsc", dest="smsc", |
| 120 | help="SMSC number (Start with + for international no.) [default: '00 + country code + 5555']", |
| 121 | ) |
| 122 | parser.add_option("-M", "--smsp", dest="smsp", |
| 123 | help="Raw SMSP content in hex [default: auto from SMSC]", |
| 124 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 125 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 126 | parser.add_option("-s", "--iccid", dest="iccid", metavar="ID", |
| 127 | help="Integrated Circuit Card ID", |
| 128 | ) |
| 129 | parser.add_option("-i", "--imsi", dest="imsi", |
| 130 | help="International Mobile Subscriber Identity", |
| 131 | ) |
| 132 | parser.add_option("--msisdn", dest="msisdn", |
| 133 | help="Mobile Subscriber Integrated Services Digital Number", |
| 134 | ) |
| 135 | parser.add_option("-k", "--ki", dest="ki", |
| 136 | help="Ki (default is to randomize)", |
| 137 | ) |
| 138 | parser.add_option("-o", "--opc", dest="opc", |
| 139 | help="OPC (default is to randomize)", |
| 140 | ) |
| 141 | parser.add_option("--op", dest="op", |
| 142 | help="Set OP to derive OPC from OP and KI", |
| 143 | ) |
| 144 | parser.add_option("--acc", dest="acc", |
| 145 | help="Set ACC bits (Access Control Code). not all card types are supported", |
| 146 | ) |
| 147 | parser.add_option("--opmode", dest="opmode", type="choice", |
| 148 | help="Set UE Operation Mode in EF.AD (Administrative Data)", |
| 149 | default=None, |
| 150 | choices=['{:02X}'.format(int(m)) for m in EF_AD.OP_MODE], |
| 151 | ) |
| 152 | parser.add_option("--epdgid", dest="epdgid", |
| 153 | help="Set Home Evolved Packet Data Gateway (ePDG) Identifier. (Only FQDN format supported)", |
| 154 | ) |
| 155 | parser.add_option("--epdgSelection", dest="epdgSelection", |
| 156 | help="Set PLMN for ePDG Selection Information. (Only Operator Identifier FQDN format supported)", |
| 157 | ) |
| 158 | parser.add_option("--pcscf", dest="pcscf", |
| 159 | help="Set Proxy Call Session Control Function (P-CSCF) Address. (Only FQDN format supported)", |
| 160 | ) |
| 161 | parser.add_option("--ims-hdomain", dest="ims_hdomain", |
| 162 | help="Set IMS Home Network Domain Name in FQDN format", |
| 163 | ) |
| 164 | parser.add_option("--impi", dest="impi", |
| 165 | help="Set IMS private user identity", |
| 166 | ) |
| 167 | parser.add_option("--impu", dest="impu", |
| 168 | help="Set IMS public user identity", |
| 169 | ) |
| 170 | parser.add_option("--read-imsi", dest="read_imsi", action="store_true", |
| 171 | help="Read the IMSI from the CARD", default=False |
| 172 | ) |
| 173 | parser.add_option("--read-iccid", dest="read_iccid", action="store_true", |
| 174 | help="Read the ICCID from the CARD", default=False |
| 175 | ) |
| 176 | parser.add_option("-z", "--secret", dest="secret", metavar="STR", |
| 177 | help="Secret used for ICCID/IMSI autogen", |
| 178 | ) |
| 179 | parser.add_option("-j", "--num", dest="num", type=int, |
| 180 | help="Card # used for ICCID/IMSI autogen", |
| 181 | ) |
| 182 | parser.add_option("--batch", dest="batch_mode", |
| 183 | help="Enable batch mode [default: %default]", |
| 184 | default=False, action='store_true', |
| 185 | ) |
| 186 | parser.add_option("--batch-state", dest="batch_state", metavar="FILE", |
| 187 | help="Optional batch state file", |
| 188 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 189 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 190 | # if mode is "csv" |
| 191 | parser.add_option("--read-csv", dest="read_csv", metavar="FILE", |
| 192 | help="Read parameters from CSV file rather than command line") |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 193 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 194 | parser.add_option("--write-csv", dest="write_csv", metavar="FILE", |
| 195 | help="Append generated parameters in CSV file", |
| 196 | ) |
| 197 | parser.add_option("--write-hlr", dest="write_hlr", metavar="FILE", |
| 198 | help="Append generated parameters to OpenBSC HLR sqlite3", |
| 199 | ) |
| 200 | parser.add_option("--dry-run", dest="dry_run", |
| 201 | help="Perform a 'dry run', don't actually program the card", |
| 202 | default=False, action="store_true") |
| 203 | parser.add_option("--card_handler", dest="card_handler_config", metavar="FILE", |
| 204 | help="Use automatic card handling machine") |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 205 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 206 | (options, args) = parser.parse_args() |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 207 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 208 | if options.type == 'list': |
| 209 | for kls in _cards_classes: |
| 210 | print(kls.name) |
| 211 | sys.exit(0) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 212 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 213 | if options.probe: |
| 214 | return options |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 215 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 216 | if options.source == 'csv': |
| 217 | if (options.imsi is None) and (options.batch_mode is False) and (options.read_imsi is False) and (options.read_iccid is False): |
| 218 | parser.error( |
| 219 | "CSV mode needs either an IMSI, --read-imsi, --read-iccid or batch mode") |
| 220 | if options.read_csv is None: |
| 221 | parser.error("CSV mode requires a CSV input file") |
| 222 | elif options.source == 'cmdline': |
| 223 | if ((options.imsi is None) or (options.iccid is None)) and (options.num is None): |
| 224 | parser.error( |
| 225 | "If either IMSI or ICCID isn't specified, num is required") |
| 226 | else: |
| 227 | parser.error("Only `cmdline' and `csv' sources supported") |
Philipp Maier | ac9dde6 | 2018-07-04 11:05:14 +0200 | [diff] [blame] | 228 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 229 | if (options.read_csv is not None) and (options.source != 'csv'): |
| 230 | parser.error("You cannot specify a CSV input file in source != csv") |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 231 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 232 | if (options.batch_mode) and (options.num is None): |
| 233 | options.num = 0 |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 234 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 235 | if (options.batch_mode): |
| 236 | if (options.imsi is not None) or (options.iccid is not None): |
| 237 | parser.error( |
| 238 | "Can't give ICCID/IMSI for batch mode, need to use automatic parameters ! see --num and --secret for more information") |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 239 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 240 | if args: |
| 241 | parser.error("Extraneous arguments") |
Sylvain Munaut | 98d2b85 | 2010-12-23 20:27:25 +0100 | [diff] [blame] | 242 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 243 | return options |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 244 | |
| 245 | |
| 246 | def _digits(secret, usage, len, num): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 247 | seed = secret + usage + '%d' % num |
| 248 | s = hashlib.sha1(seed.encode()) |
| 249 | d = ''.join(['%02d' % x for x in s.digest()]) |
| 250 | return d[0:len] |
| 251 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 252 | |
| 253 | def _mcc_mnc_digits(mcc, mnc): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 254 | return '%s%s' % (mcc, mnc) |
| 255 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 256 | |
| 257 | def _cc_digits(cc): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 258 | return ('%03d' if cc > 100 else '%02d') % cc |
| 259 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 260 | |
| 261 | def _isnum(s, l=-1): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 262 | return s.isdigit() and ((l == -1) or (len(s) == l)) |
| 263 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 264 | |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 265 | def _ishex(s, l=-1): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 266 | hc = '0123456789abcdef' |
| 267 | return all([x in hc for x in s.lower()]) and ((l == -1) or (len(s) == l)) |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 268 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 269 | |
Sylvain Munaut | 9f120e0 | 2010-12-23 20:28:24 +0100 | [diff] [blame] | 270 | def _dbi_binary_quote(s): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 271 | # Count usage of each char |
| 272 | cnt = {} |
| 273 | for c in s: |
| 274 | cnt[c] = cnt.get(c, 0) + 1 |
Sylvain Munaut | 9f120e0 | 2010-12-23 20:28:24 +0100 | [diff] [blame] | 275 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 276 | # Find best offset |
| 277 | e = 0 |
| 278 | m = len(s) |
| 279 | for i in range(1, 256): |
| 280 | if i == 39: |
| 281 | continue |
| 282 | sum_ = cnt.get(i, 0) + cnt.get((i+1) & 0xff, 0) + \ |
| 283 | cnt.get((i+39) & 0xff, 0) |
| 284 | if sum_ < m: |
| 285 | m = sum_ |
| 286 | e = i |
| 287 | if m == 0: # No overhead ? use this ! |
| 288 | break |
Sylvain Munaut | 1a91443 | 2011-12-08 20:08:26 +0100 | [diff] [blame] | 289 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 290 | # Generate output |
| 291 | out = [] |
| 292 | out.append(chr(e)) # Offset |
| 293 | for c in s: |
| 294 | x = (256 + ord(c) - e) % 256 |
| 295 | if x in (0, 1, 39): |
| 296 | out.append('\x01') |
| 297 | out.append(chr(x+1)) |
| 298 | else: |
| 299 | out.append(chr(x)) |
Sylvain Munaut | 9f120e0 | 2010-12-23 20:28:24 +0100 | [diff] [blame] | 300 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 301 | return ''.join(out) |
| 302 | |
Sylvain Munaut | 9f120e0 | 2010-12-23 20:28:24 +0100 | [diff] [blame] | 303 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 304 | def gen_parameters(opts): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 305 | """Generates Name, ICCID, MCC, MNC, IMSI, SMSP, Ki, PIN-ADM from the |
| 306 | options given by the user""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 307 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 308 | # MCC/MNC |
| 309 | mcc = opts.mcc |
| 310 | mnc = opts.mnc |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 311 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 312 | if not mcc.isdigit() or not mnc.isdigit(): |
| 313 | raise ValueError('mcc & mnc must only contain decimal digits') |
| 314 | if len(mcc) < 1 or len(mcc) > 3: |
| 315 | raise ValueError('mcc must be between 1 .. 3 digits') |
| 316 | if len(mnc) < 1 or len(mnc) > 3: |
| 317 | raise ValueError('mnc must be between 1 .. 3 digits') |
Harald Welte | 7f1d3c4 | 2020-05-12 21:12:44 +0200 | [diff] [blame] | 318 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 319 | # MCC always has 3 digits |
| 320 | mcc = lpad(mcc, 3, "0") |
| 321 | # MNC must be at least 2 digits |
| 322 | mnc = lpad(mnc, 2, "0") |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 323 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 324 | # Digitize country code (2 or 3 digits) |
| 325 | cc_digits = _cc_digits(opts.country) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 326 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 327 | # Digitize MCC/MNC (5 or 6 digits) |
| 328 | plmn_digits = _mcc_mnc_digits(mcc, mnc) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 329 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 330 | if opts.name is not None: |
| 331 | if len(opts.name) > 16: |
| 332 | raise ValueError('Service Provider Name must max 16 characters!') |
Supreeth Herle | 840a9e2 | 2020-01-21 13:32:46 +0100 | [diff] [blame] | 333 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 334 | if opts.msisdn is not None: |
| 335 | msisdn = opts.msisdn |
| 336 | if msisdn[0] == '+': |
| 337 | msisdn = msisdn[1:] |
| 338 | if not msisdn.isdigit(): |
| 339 | raise ValueError('MSISDN must be digits only! ' |
| 340 | 'Start with \'+\' for international numbers.') |
| 341 | if len(msisdn) > 10 * 2: |
| 342 | # TODO: Support MSISDN of length > 20 (10 Bytes) |
| 343 | raise ValueError( |
| 344 | 'MSISDNs longer than 20 digits are not (yet) supported.') |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 345 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 346 | # ICCID (19 digits, E.118), though some phase1 vendors use 20 :( |
| 347 | if opts.iccid is not None: |
| 348 | iccid = opts.iccid |
| 349 | if not _isnum(iccid, 19) and not _isnum(iccid, 20): |
| 350 | raise ValueError('ICCID must be 19 or 20 digits !') |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 351 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 352 | else: |
| 353 | if opts.num is None: |
| 354 | raise ValueError('Neither ICCID nor card number specified !') |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 355 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 356 | iccid = ( |
| 357 | '89' + # Common prefix (telecom) |
| 358 | cc_digits + # Country Code on 2/3 digits |
| 359 | plmn_digits # MCC/MNC on 5/6 digits |
| 360 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 361 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 362 | ml = 18 - len(iccid) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 363 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 364 | if opts.secret is None: |
| 365 | # The raw number |
| 366 | iccid += ('%%0%dd' % ml) % opts.num |
| 367 | else: |
| 368 | # Randomized digits |
| 369 | iccid += _digits(opts.secret, 'ccid', ml, opts.num) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 370 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 371 | # Add checksum digit |
| 372 | iccid += ('%1d' % calculate_luhn(iccid)) |
Harald Welte | 2c0ff3a | 2011-12-07 12:34:13 +0100 | [diff] [blame] | 373 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 374 | # IMSI (15 digits usually) |
| 375 | if opts.imsi is not None: |
| 376 | imsi = opts.imsi |
| 377 | if not _isnum(imsi): |
| 378 | raise ValueError('IMSI must be digits only !') |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 379 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 380 | else: |
| 381 | if opts.num is None: |
| 382 | raise ValueError('Neither IMSI nor card number specified !') |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 383 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 384 | ml = 15 - len(plmn_digits) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 385 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 386 | if opts.secret is None: |
| 387 | # The raw number |
| 388 | msin = ('%%0%dd' % ml) % opts.num |
| 389 | else: |
| 390 | # Randomized digits |
| 391 | msin = _digits(opts.secret, 'imsi', ml, opts.num) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 392 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 393 | imsi = ( |
| 394 | plmn_digits + # MCC/MNC on 5/6 digits |
| 395 | msin # MSIN |
| 396 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 397 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 398 | # SMSP |
| 399 | if opts.smsp is not None: |
| 400 | smsp = opts.smsp |
| 401 | if not _ishex(smsp): |
| 402 | raise ValueError('SMSP must be hex digits only !') |
| 403 | if len(smsp) < 28*2: |
| 404 | raise ValueError('SMSP must be at least 28 bytes') |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 405 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 406 | else: |
| 407 | ton = "81" |
| 408 | if opts.smsc is not None: |
| 409 | smsc = opts.smsc |
| 410 | if smsc[0] == '+': |
| 411 | ton = "91" |
| 412 | smsc = smsc[1:] |
| 413 | if not _isnum(smsc): |
| 414 | raise ValueError('SMSC must be digits only!\n \ |
Daniel Willmann | 4fa8f1c | 2018-10-02 18:10:21 +0200 | [diff] [blame] | 415 | Start with \'+\' for international numbers') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 416 | else: |
| 417 | smsc = '00%d' % opts.country + '5555' # Hack ... |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 418 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 419 | smsc = '%02d' % ((len(smsc) + 3)//2,) + ton + \ |
| 420 | swap_nibbles(rpad(smsc, 20)) |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 421 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 422 | smsp = ( |
| 423 | 'e1' + # Parameters indicator |
| 424 | 'ff' * 12 + # TP-Destination address |
| 425 | smsc + # TP-Service Centre Address |
| 426 | '00' + # TP-Protocol identifier |
| 427 | '00' + # TP-Data coding scheme |
| 428 | '00' # TP-Validity period |
| 429 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 430 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 431 | # ACC |
| 432 | if opts.acc is not None: |
| 433 | acc = opts.acc |
| 434 | if not _ishex(acc): |
| 435 | raise ValueError('ACC must be hex digits only !') |
| 436 | if len(acc) != 2*2: |
| 437 | raise ValueError('ACC must be exactly 2 bytes') |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 438 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 439 | else: |
| 440 | acc = None |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 441 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 442 | # Ki (random) |
| 443 | if opts.ki is not None: |
| 444 | ki = opts.ki |
| 445 | if not re.match('^[0-9a-fA-F]{32}$', ki): |
| 446 | raise ValueError('Ki needs to be 128 bits, in hex format') |
| 447 | else: |
| 448 | ki = ''.join(['%02x' % random.randrange(0, 256) for i in range(16)]) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 449 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 450 | # OPC (random) |
| 451 | if opts.opc is not None: |
| 452 | opc = opts.opc |
| 453 | if not re.match('^[0-9a-fA-F]{32}$', opc): |
| 454 | raise ValueError('OPC needs to be 128 bits, in hex format') |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 455 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 456 | elif opts.op is not None: |
| 457 | opc = derive_milenage_opc(ki, opts.op) |
| 458 | else: |
| 459 | opc = ''.join(['%02x' % random.randrange(0, 256) for i in range(16)]) |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 460 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 461 | pin_adm = sanitize_pin_adm(opts.pin_adm, opts.pin_adm_hex) |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 462 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 463 | # ePDG Selection Information |
| 464 | if opts.epdgSelection: |
| 465 | if len(opts.epdgSelection) < 5 or len(opts.epdgSelection) > 6: |
| 466 | raise ValueError('ePDG Selection Information is not valid') |
| 467 | epdg_mcc = opts.epdgSelection[:3] |
| 468 | epdg_mnc = opts.epdgSelection[3:] |
| 469 | if not epdg_mcc.isdigit() or not epdg_mnc.isdigit(): |
| 470 | raise ValueError( |
| 471 | 'PLMN for ePDG Selection must only contain decimal digits') |
Supreeth Herle | f964df4 | 2020-03-24 13:15:37 +0100 | [diff] [blame] | 472 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 473 | # Return that |
| 474 | return { |
| 475 | 'name': opts.name, |
| 476 | 'iccid': iccid, |
| 477 | 'mcc': mcc, |
| 478 | 'mnc': mnc, |
| 479 | 'imsi': imsi, |
| 480 | 'smsp': smsp, |
| 481 | 'ki': ki, |
| 482 | 'opc': opc, |
| 483 | 'acc': acc, |
| 484 | 'pin_adm': pin_adm, |
| 485 | 'msisdn': opts.msisdn, |
| 486 | 'epdgid': opts.epdgid, |
| 487 | 'epdgSelection': opts.epdgSelection, |
| 488 | 'pcscf': opts.pcscf, |
| 489 | 'ims_hdomain': opts.ims_hdomain, |
| 490 | 'impi': opts.impi, |
| 491 | 'impu': opts.impu, |
| 492 | 'opmode': opts.opmode, |
| 493 | } |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 494 | |
| 495 | |
| 496 | def print_parameters(params): |
| 497 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 498 | s = ["Generated card parameters :"] |
| 499 | if 'name' in params: |
| 500 | s.append(" > Name : %(name)s") |
| 501 | if 'smsp' in params: |
| 502 | s.append(" > SMSP : %(smsp)s") |
| 503 | s.append(" > ICCID : %(iccid)s") |
| 504 | s.append(" > MCC/MNC : %(mcc)s/%(mnc)s") |
| 505 | s.append(" > IMSI : %(imsi)s") |
| 506 | s.append(" > Ki : %(ki)s") |
| 507 | s.append(" > OPC : %(opc)s") |
| 508 | if 'acc' in params: |
| 509 | s.append(" > ACC : %(acc)s") |
| 510 | s.append(" > ADM1(hex): %(pin_adm)s") |
| 511 | if 'opmode' in params: |
| 512 | s.append(" > OPMODE : %(opmode)s") |
| 513 | print("\n".join(s) % params) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 514 | |
| 515 | |
Harald Welte | 130524b | 2012-08-13 15:53:43 +0200 | [diff] [blame] | 516 | def write_params_csv(opts, params): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 517 | # csv |
| 518 | if opts.write_csv: |
| 519 | import csv |
| 520 | row = ['name', 'iccid', 'mcc', 'mnc', 'imsi', 'smsp', 'ki', 'opc'] |
| 521 | f = open(opts.write_csv, 'a') |
| 522 | cw = csv.writer(f) |
| 523 | cw.writerow([params[x] for x in row]) |
| 524 | f.close() |
| 525 | |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 526 | |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 527 | def _read_params_csv(opts, iccid=None, imsi=None): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 528 | import csv |
| 529 | f = open(opts.read_csv, 'r') |
| 530 | cr = csv.DictReader(f) |
Philipp Maier | 120a000 | 2019-09-12 13:11:45 +0200 | [diff] [blame] | 531 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 532 | # Lower-case fieldnames |
| 533 | cr.fieldnames = [field.lower() for field in cr.fieldnames] |
Philipp Maier | 120a000 | 2019-09-12 13:11:45 +0200 | [diff] [blame] | 534 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 535 | i = 0 |
| 536 | if not 'iccid' in cr.fieldnames: |
| 537 | raise Exception("CSV file in wrong format!") |
| 538 | for row in cr: |
| 539 | if opts.num is not None and opts.read_iccid is False and opts.read_imsi is False: |
| 540 | if opts.num == i: |
| 541 | f.close() |
| 542 | return row |
| 543 | i += 1 |
| 544 | if row['iccid'] == iccid: |
| 545 | f.close() |
| 546 | return row |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 547 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 548 | if row['imsi'] == imsi: |
| 549 | f.close() |
| 550 | return row |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 551 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 552 | f.close() |
| 553 | return None |
| 554 | |
Harald Welte | c26b829 | 2012-08-15 15:25:51 +0200 | [diff] [blame] | 555 | |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 556 | def read_params_csv(opts, imsi=None, iccid=None): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 557 | row = _read_params_csv(opts, iccid=iccid, imsi=imsi) |
| 558 | if row is not None: |
| 559 | row['mcc'] = row.get('mcc', mcc_from_imsi(row.get('imsi'))) |
| 560 | row['mnc'] = row.get('mnc', mnc_from_imsi(row.get('imsi'))) |
Philipp Maier | 7592eee | 2019-09-12 13:03:23 +0200 | [diff] [blame] | 561 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 562 | pin_adm = None |
| 563 | # We need to escape the pin_adm we get from the csv |
| 564 | if 'pin_adm' in row: |
| 565 | pin_adm = ''.join(['%02x' % (ord(x)) for x in row['pin_adm']]) |
| 566 | # Stay compatible to the odoo csv format |
| 567 | elif 'adm1' in row: |
| 568 | pin_adm = ''.join(['%02x' % (ord(x)) for x in row['adm1']]) |
| 569 | if pin_adm: |
| 570 | row['pin_adm'] = rpad(pin_adm, 16) |
Philipp Maier | e053da5 | 2019-09-05 13:08:36 +0200 | [diff] [blame] | 571 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 572 | # If the CSV-File defines a pin_adm_hex field use this field to |
| 573 | # generate pin_adm from that. |
| 574 | pin_adm_hex = row.get('pin_adm_hex') |
| 575 | if pin_adm_hex: |
| 576 | if len(pin_adm_hex) == 16: |
| 577 | row['pin_adm'] = pin_adm_hex |
| 578 | # Ensure that it's hex-encoded |
| 579 | try: |
| 580 | try_encode = h2b(pin_adm) |
| 581 | except ValueError: |
| 582 | raise ValueError( |
| 583 | "pin_adm_hex needs to be hex encoded using this option") |
| 584 | else: |
| 585 | raise ValueError( |
| 586 | "pin_adm_hex needs to be exactly 16 digits (hex encoded)") |
Philipp Maier | e053da5 | 2019-09-05 13:08:36 +0200 | [diff] [blame] | 587 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 588 | return row |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 589 | |
Harald Welte | c26b829 | 2012-08-15 15:25:51 +0200 | [diff] [blame] | 590 | |
Harald Welte | 130524b | 2012-08-13 15:53:43 +0200 | [diff] [blame] | 591 | def write_params_hlr(opts, params): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 592 | # SQLite3 OpenBSC HLR |
| 593 | if opts.write_hlr: |
| 594 | import sqlite3 |
| 595 | conn = sqlite3.connect(opts.write_hlr) |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 596 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 597 | c = conn.execute( |
| 598 | 'INSERT INTO Subscriber ' + |
| 599 | '(imsi, name, extension, authorized, created, updated) ' + |
| 600 | 'VALUES ' + |
| 601 | '(?,?,?,1,datetime(\'now\'),datetime(\'now\'));', |
| 602 | [ |
| 603 | params['imsi'], |
| 604 | params['name'], |
| 605 | '9' + params['iccid'][-5:-1] |
| 606 | ], |
| 607 | ) |
| 608 | sub_id = c.lastrowid |
| 609 | c.close() |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 610 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 611 | c = conn.execute( |
| 612 | 'INSERT INTO AuthKeys ' + |
| 613 | '(subscriber_id, algorithm_id, a3a8_ki)' + |
| 614 | 'VALUES ' + |
| 615 | '(?,?,?)', |
| 616 | [sub_id, 2, sqlite3.Binary( |
| 617 | _dbi_binary_quote(h2b(params['ki'])))], |
| 618 | ) |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 619 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 620 | conn.commit() |
| 621 | conn.close() |
| 622 | |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 623 | |
Harald Welte | 130524b | 2012-08-13 15:53:43 +0200 | [diff] [blame] | 624 | def write_parameters(opts, params): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 625 | write_params_csv(opts, params) |
| 626 | write_params_hlr(opts, params) |
Harald Welte | 130524b | 2012-08-13 15:53:43 +0200 | [diff] [blame] | 627 | |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 628 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 629 | BATCH_STATE = ['name', 'country', 'mcc', 'mnc', 'smsp', 'secret', 'num'] |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 630 | BATCH_INCOMPATIBLE = ['iccid', 'imsi', 'ki'] |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 631 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 632 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 633 | def init_batch(opts): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 634 | # Need to do something ? |
| 635 | if not opts.batch_mode: |
| 636 | return |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 637 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 638 | for k in BATCH_INCOMPATIBLE: |
| 639 | if getattr(opts, k): |
| 640 | print("Incompatible option with batch_state: %s" % (k,)) |
| 641 | sys.exit(-1) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 642 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 643 | # Don't load state if there is none ... |
| 644 | if not opts.batch_state: |
| 645 | return |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 646 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 647 | if not os.path.isfile(opts.batch_state): |
| 648 | print("No state file yet") |
| 649 | return |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 650 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 651 | # Get stored data |
| 652 | fh = open(opts.batch_state) |
| 653 | d = json.loads(fh.read()) |
| 654 | fh.close() |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 655 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 656 | for k, v in d.iteritems(): |
| 657 | setattr(opts, k, v) |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 658 | |
| 659 | |
| 660 | def save_batch(opts): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 661 | # Need to do something ? |
| 662 | if not opts.batch_mode or not opts.batch_state: |
| 663 | return |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 664 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 665 | d = json.dumps(dict([(k, getattr(opts, k)) for k in BATCH_STATE])) |
| 666 | fh = open(opts.batch_state, 'w') |
| 667 | fh.write(d) |
| 668 | fh.close() |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 669 | |
| 670 | |
Philipp Maier | 82511e5 | 2021-09-17 13:38:36 +0200 | [diff] [blame] | 671 | def process_card(opts, first, ch): |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 672 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 673 | if opts.dry_run is False: |
| 674 | # Connect transport |
| 675 | ch.get(first) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 676 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 677 | if opts.dry_run is False: |
| 678 | # Get card |
| 679 | card = card_detect(opts.type, scc) |
| 680 | if card is None: |
| 681 | print("No card detected!") |
| 682 | return -1 |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 683 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 684 | # Probe only |
| 685 | if opts.probe: |
| 686 | return 0 |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 687 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 688 | # Erase if requested |
| 689 | if opts.erase: |
| 690 | print("Formatting ...") |
| 691 | card.erase() |
| 692 | card.reset() |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 693 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 694 | # Generate parameters |
| 695 | if opts.source == 'cmdline': |
| 696 | cp = gen_parameters(opts) |
| 697 | elif opts.source == 'csv': |
| 698 | imsi = None |
| 699 | iccid = None |
| 700 | if opts.read_iccid: |
| 701 | if opts.dry_run: |
| 702 | # Connect transport |
| 703 | ch.get(False) |
| 704 | (res, _) = scc.read_binary(['3f00', '2fe2'], length=10) |
| 705 | iccid = dec_iccid(res) |
| 706 | elif opts.read_imsi: |
| 707 | if opts.dry_run: |
| 708 | # Connect transport |
| 709 | ch.get(False) |
| 710 | (res, _) = scc.read_binary(EF['IMSI']) |
| 711 | imsi = swap_nibbles(res)[3:] |
| 712 | else: |
| 713 | imsi = opts.imsi |
| 714 | cp = read_params_csv(opts, imsi=imsi, iccid=iccid) |
| 715 | if cp is None: |
| 716 | print("Error reading parameters from CSV file!\n") |
| 717 | return 2 |
| 718 | print_parameters(cp) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 719 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 720 | if opts.dry_run is False: |
| 721 | # Program the card |
| 722 | print("Programming ...") |
| 723 | card.program(cp) |
| 724 | else: |
| 725 | print("Dry Run: NOT PROGRAMMING!") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 726 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 727 | # Write parameters permanently |
| 728 | write_parameters(opts, cp) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 729 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 730 | # Batch mode state update and save |
| 731 | if opts.num is not None: |
| 732 | opts.num += 1 |
| 733 | save_batch(opts) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 734 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 735 | ch.done() |
| 736 | return 0 |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 737 | |
| 738 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 739 | if __name__ == '__main__': |
| 740 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 741 | # Parse options |
| 742 | opts = parse_options() |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 743 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 744 | # Init card reader driver |
| 745 | sl = init_reader(opts) |
| 746 | if sl is None: |
| 747 | exit(1) |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 748 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 749 | # Create command layer |
| 750 | scc = SimCardCommands(transport=sl) |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 751 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 752 | # If we use a CSV file as data input, check if the CSV file exists. |
| 753 | if opts.source == 'csv': |
| 754 | print("Using CSV file as data input: " + str(opts.read_csv)) |
| 755 | if not os.path.isfile(opts.read_csv): |
| 756 | print("CSV file not found!") |
| 757 | sys.exit(1) |
Philipp Maier | 196b08c | 2019-09-12 11:49:44 +0200 | [diff] [blame] | 758 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 759 | # Batch mode init |
| 760 | init_batch(opts) |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 761 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 762 | if opts.card_handler_config: |
| 763 | ch = CardHandlerAuto(sl, opts.card_handler_config) |
| 764 | else: |
| 765 | ch = CardHandler(sl) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 766 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 767 | # Iterate |
| 768 | first = True |
| 769 | card = None |
Sylvain Munaut | 1a91443 | 2011-12-08 20:08:26 +0100 | [diff] [blame] | 770 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 771 | while 1: |
| 772 | try: |
| 773 | rc = process_card(opts, first, ch) |
| 774 | except (KeyboardInterrupt): |
| 775 | print("") |
| 776 | print("Terminated by user!") |
| 777 | sys.exit(0) |
| 778 | except (SystemExit): |
| 779 | raise |
| 780 | except: |
| 781 | print("") |
| 782 | print("Card programming failed with an exception:") |
| 783 | print("---------------------8<---------------------") |
| 784 | traceback.print_exc() |
| 785 | print("---------------------8<---------------------") |
| 786 | print("") |
| 787 | rc = -1 |
Harald Welte | e9e5ecb | 2012-08-15 15:26:30 +0200 | [diff] [blame] | 788 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 789 | # Something did not work as well as expected, however, lets |
| 790 | # make sure the card is pulled from the reader. |
| 791 | if rc != 0: |
| 792 | ch.error() |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 793 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 794 | # If we are not in batch mode we are done in any case, so lets |
| 795 | # exit here. |
| 796 | if not opts.batch_mode: |
| 797 | sys.exit(rc) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 798 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 799 | first = False |