2019-12-19 10:34:19 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-03-08 01:08:06 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
This module defines user ID-related CLI commands
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
import getpass
|
|
|
|
|
|
|
|
import unpaddedbase32
|
|
|
|
import niceware
|
|
|
|
|
|
|
|
import logger
|
|
|
|
import onionrexceptions
|
|
|
|
from onionrutils import stringvalidators, bytesconverter
|
|
|
|
import config
|
|
|
|
import keymanager
|
|
|
|
import onionrcrypto
|
2022-02-07 01:18:53 +00:00
|
|
|
import onionrvalues
|
2019-12-19 10:34:19 +00:00
|
|
|
"""
|
2022-03-18 00:56:31 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-19 10:34:19 +00:00
|
|
|
"""
|
2019-03-08 01:08:06 +00:00
|
|
|
|
2019-09-09 08:23:09 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
DETERMINISTIC_REQUIREMENT = onionrvalues.PASSWORD_LENGTH
|
2019-09-09 08:23:09 +00:00
|
|
|
|
|
|
|
|
2019-08-05 23:09:04 +00:00
|
|
|
def add_ID():
|
2019-12-19 10:34:19 +00:00
|
|
|
"""Command to create a new user ID key pair."""
|
2019-07-27 20:29:15 +00:00
|
|
|
key_manager = keymanager.KeyManager()
|
2020-09-14 11:54:14 +00:00
|
|
|
pw = ""
|
2019-03-08 01:08:06 +00:00
|
|
|
try:
|
2019-12-19 10:34:19 +00:00
|
|
|
sys.argv[2] # pylint: disable=W0104
|
|
|
|
if not sys.argv[2].lower() == 'true':
|
|
|
|
raise ValueError
|
|
|
|
except (IndexError, ValueError):
|
2019-07-27 20:29:15 +00:00
|
|
|
newID = key_manager.addKey()[0]
|
2019-03-08 01:08:06 +00:00
|
|
|
else:
|
2020-09-14 11:54:14 +00:00
|
|
|
pw = "-".join(niceware.generate_passphrase(32))
|
|
|
|
newID, privKey = onionrcrypto.generate_deterministic(pw)
|
2019-06-26 19:54:13 +00:00
|
|
|
try:
|
2019-12-19 10:34:19 +00:00
|
|
|
key_manager.addKey(pubKey=newID,
|
|
|
|
privKey=privKey)
|
2019-06-26 19:54:13 +00:00
|
|
|
except ValueError:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.error(
|
|
|
|
'That ID is already available, you can change to it ' +
|
|
|
|
'with the change-id command.', terminal=True)
|
2019-06-26 19:54:13 +00:00
|
|
|
return
|
2020-09-14 11:54:14 +00:00
|
|
|
if pw:
|
|
|
|
print("Phrase to restore ID:", pw)
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info('Added ID: %s' %
|
2020-09-14 11:54:14 +00:00
|
|
|
(bytesconverter.bytes_to_str(newID.replace('=', '')),), terminal=True)
|
2019-12-19 10:34:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
add_ID.onionr_help = "If the first argument is true, " # type: ignore
|
|
|
|
add_ID.onionr_help += "Onionr will show a deterministic " # type: ignore
|
|
|
|
add_ID.onionr_help += "generation prompt. Otherwise it will " # type: ignore
|
|
|
|
add_ID.onionr_help += "generate & save a new random key pair." # type: ignore
|
2019-03-08 01:08:06 +00:00
|
|
|
|
2019-09-21 05:06:49 +00:00
|
|
|
|
2019-08-05 23:09:04 +00:00
|
|
|
def change_ID():
|
2019-12-19 10:34:19 +00:00
|
|
|
"""Command to change active ID from argv or stdin."""
|
2019-07-27 20:29:15 +00:00
|
|
|
key_manager = keymanager.KeyManager()
|
2019-03-08 01:08:06 +00:00
|
|
|
try:
|
|
|
|
key = sys.argv[2]
|
2019-06-19 06:57:13 +00:00
|
|
|
key = unpaddedbase32.repad(key.encode()).decode()
|
2019-03-08 01:08:06 +00:00
|
|
|
except IndexError:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.warn('Specify pubkey to use', terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
else:
|
2019-06-25 08:21:36 +00:00
|
|
|
if stringvalidators.validate_pub_key(key):
|
2019-09-09 08:52:40 +00:00
|
|
|
key_list = key_manager.getPubkeyList()
|
|
|
|
if key in key_list or key.replace('=', '') in key_list:
|
2019-08-05 23:09:04 +00:00
|
|
|
config.set('general.public_key', key)
|
|
|
|
config.save()
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.info('Set active key to: %s' % (key,), terminal=True)
|
|
|
|
logger.info('Restart Onionr if it is running.', terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
else:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.warn('That key does not exist', terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
else:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.warn('Invalid key %s' % (key,), terminal=True)
|
2019-09-09 08:23:09 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
|
|
|
|
change_ID.onionr_help = "<pubkey>: Switches Onionr to " # type: ignore
|
|
|
|
change_ID.onionr_help += "use a different user ID key. " # type: ignore
|
|
|
|
change_ID.onionr_help += "You should immediately restart " # type: ignore
|
|
|
|
change_ID.onionr_help += "Onionr if it is running." # type: ignore
|