|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (C) 2024 VyOS maintainers and contributors |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License version 2 or later as |
| 7 | +# published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +# |
| 17 | +# T6318: WiFi country-code should be set system-wide instead of per-device |
| 18 | + |
| 19 | +from sys import argv |
| 20 | +from sys import exit |
| 21 | +from vyos.configtree import ConfigTree |
| 22 | + |
| 23 | +if len(argv) < 2: |
| 24 | + print("Must specify file name!") |
| 25 | + exit(1) |
| 26 | + |
| 27 | +file_name = argv[1] |
| 28 | +with open(file_name, 'r') as f: |
| 29 | + config_file = f.read() |
| 30 | + |
| 31 | +base = ['interfaces', 'wireless'] |
| 32 | + |
| 33 | +config = ConfigTree(config_file) |
| 34 | +if not config.exists(base): |
| 35 | + # Nothing to do |
| 36 | + exit(0) |
| 37 | + |
| 38 | +installed = False |
| 39 | +for interface in config.list_nodes(base): |
| 40 | + cc_path = base + [interface, 'country-code'] |
| 41 | + if config.exists(cc_path): |
| 42 | + tmp = config.return_value(cc_path) |
| 43 | + config.delete(cc_path) |
| 44 | + |
| 45 | + # There can be only ONE wireless country-code per device, everything |
| 46 | + # else makes no sense as a WIFI router can not operate in two |
| 47 | + # different countries |
| 48 | + if not installed: |
| 49 | + config.set(['system', 'wireless', 'country-code'], value=tmp) |
| 50 | + installed = True |
| 51 | + |
| 52 | +try: |
| 53 | + with open(file_name, 'w') as f: |
| 54 | + f.write(config.to_string()) |
| 55 | +except OSError as e: |
| 56 | + print(f'Failed to save the modified config: {e}') |
| 57 | + exit(1) |
0 commit comments