|
|
|
@ -105,18 +105,33 @@ class SearchPassService(dbus.service.Object):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def get_bw_result_set(self, terms):
|
|
|
|
|
if terms[0].startswith(":"):
|
|
|
|
|
field = terms[0][1:]
|
|
|
|
|
terms = terms[1:]
|
|
|
|
|
else:
|
|
|
|
|
field = None
|
|
|
|
|
name = "".join(terms)
|
|
|
|
|
|
|
|
|
|
password_list = subprocess.check_output(
|
|
|
|
|
[self.password_executable, "list"], universal_newlines=True
|
|
|
|
|
).split("\n")[:-1]
|
|
|
|
|
|
|
|
|
|
results = [
|
|
|
|
|
results = list(filter(lambda p: p.startswith(name), password_list))[:5]
|
|
|
|
|
|
|
|
|
|
remaining = 5 - len(results)
|
|
|
|
|
|
|
|
|
|
if remaining > 0:
|
|
|
|
|
fuzzy_results = [
|
|
|
|
|
e[0]
|
|
|
|
|
for e in process.extract(
|
|
|
|
|
name, password_list, limit=5, scorer=fuzz.partial_ratio
|
|
|
|
|
name, password_list, limit=remaining, scorer=fuzz.token_sort_ratio
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
results.extend(fuzzy_results)
|
|
|
|
|
|
|
|
|
|
if field is not None:
|
|
|
|
|
results = [f":{field} {r}" for r in results]
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
def get_pass_result_set(self, terms):
|
|
|
|
@ -142,51 +157,76 @@ class SearchPassService(dbus.service.Object):
|
|
|
|
|
path = path_join(dir_path, filename)[:-4]
|
|
|
|
|
password_list.append(path)
|
|
|
|
|
|
|
|
|
|
results = [
|
|
|
|
|
results = list(filter(lambda p: p.startswith(name), password_list))[:5]
|
|
|
|
|
|
|
|
|
|
remaining = 5 - len(results)
|
|
|
|
|
|
|
|
|
|
if remaining > 0:
|
|
|
|
|
containing_results = list(filter(lambda p: name in p, password_list))[
|
|
|
|
|
:remaining
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
results.extend(containing_results)
|
|
|
|
|
|
|
|
|
|
remaining = 5 - len(results)
|
|
|
|
|
|
|
|
|
|
if remaining > 0:
|
|
|
|
|
fuzzy_results = [
|
|
|
|
|
e[0]
|
|
|
|
|
for e in process.extract(
|
|
|
|
|
name, password_list, limit=5, scorer=fuzz.partial_ratio
|
|
|
|
|
name, password_list, limit=remaining, scorer=fuzz.token_sort_ratio
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
results.extend(fuzzy_results)
|
|
|
|
|
|
|
|
|
|
if field == "otp":
|
|
|
|
|
results = [f"otp {r}" for r in results]
|
|
|
|
|
elif field is not None:
|
|
|
|
|
results = [f":{field} {r}" for r in results]
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
def get_value_from_output(self, output, field=None):
|
|
|
|
|
if field is not None:
|
|
|
|
|
match = re.search(
|
|
|
|
|
rf"^{field}:\s*(?P<value>.+?)$", output, flags=re.I | re.M
|
|
|
|
|
)
|
|
|
|
|
if match:
|
|
|
|
|
value = match.group("value")
|
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"The field {field} was not found in the password entry."
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
value = output.split("\n", 1)[0]
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def send_password_to_gpaste(self, base_args, name, field=None):
|
|
|
|
|
gpaste = self.session_bus.get_object(
|
|
|
|
|
"org.gnome.GPaste.Daemon", "/org/gnome/GPaste"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
output = subprocess.check_output(base_args + [name], universal_newlines=True)
|
|
|
|
|
if field is not None:
|
|
|
|
|
match = re.search(
|
|
|
|
|
rf"^{field}:\s*(?P<value>.+?)$", output, flags=re.I | re.M
|
|
|
|
|
)
|
|
|
|
|
if match:
|
|
|
|
|
password = match.group("value")
|
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError(f"The field {field} was not found in the pass file.")
|
|
|
|
|
else:
|
|
|
|
|
password = output.split("\n", 1)[0]
|
|
|
|
|
value = self.get_value_from_output(output, field)
|
|
|
|
|
try:
|
|
|
|
|
gpaste.AddPassword(name, password, dbus_interface="org.gnome.GPaste1")
|
|
|
|
|
gpaste.AddPassword(name, value, dbus_interface="org.gnome.GPaste1")
|
|
|
|
|
except dbus.DBusException:
|
|
|
|
|
gpaste.AddPassword(name, password, dbus_interface="org.gnome.GPaste2")
|
|
|
|
|
gpaste.AddPassword(name, value, dbus_interface="org.gnome.GPaste2")
|
|
|
|
|
|
|
|
|
|
def send_password_to_native_clipboard(self, base_args, name, field=None):
|
|
|
|
|
if field is not None:
|
|
|
|
|
raise RuntimeError("This feature requires GPaste.")
|
|
|
|
|
|
|
|
|
|
if self.password_mode == "bw":
|
|
|
|
|
p1 = subprocess.Popen(base_args + [name], stdout=subprocess.PIPE)
|
|
|
|
|
p2 = subprocess.run(self.clipboard_executable, stdin=p1.stdout)
|
|
|
|
|
if p1.returncode or p2.returncode:
|
|
|
|
|
output = subprocess.check_output(
|
|
|
|
|
base_args + [name], universal_newlines=True
|
|
|
|
|
)
|
|
|
|
|
value = self.get_value_from_output(output, field)
|
|
|
|
|
p1 = subprocess.run(self.clipboard_executable, input=value, text=True)
|
|
|
|
|
if p1.returncode:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"Error while running rbw: got return codes: {p1.returncode} {p2.returncode}."
|
|
|
|
|
f"Error while running copying to clipboard: got return code: {p1.returncode}."
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
if field is not None:
|
|
|
|
|
raise RuntimeError("This feature requires GPaste.")
|
|
|
|
|
result = subprocess.run(base_args + ["-c", name])
|
|
|
|
|
if result.returncode:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
@ -194,17 +234,21 @@ class SearchPassService(dbus.service.Object):
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def send_password_to_clipboard(self, name):
|
|
|
|
|
field = None
|
|
|
|
|
if self.password_mode == "bw":
|
|
|
|
|
base_args = [self.password_executable, "get"]
|
|
|
|
|
elif name.startswith("otp "):
|
|
|
|
|
base_args = [self.password_executable, "otp", "code"]
|
|
|
|
|
name = name[4:]
|
|
|
|
|
else:
|
|
|
|
|
base_args = [self.password_executable, "show"]
|
|
|
|
|
if name.startswith(":"):
|
|
|
|
|
field, name = name.split(" ", 1)
|
|
|
|
|
field = field[1:]
|
|
|
|
|
else:
|
|
|
|
|
field = None
|
|
|
|
|
|
|
|
|
|
if self.password_mode == "bw":
|
|
|
|
|
base_args = [self.password_executable, "get", "--full"]
|
|
|
|
|
elif name.startswith("otp "):
|
|
|
|
|
base_args = [self.password_executable, "otp", "code"]
|
|
|
|
|
name = name[4:]
|
|
|
|
|
field = None
|
|
|
|
|
else:
|
|
|
|
|
base_args = [self.password_executable, "show"]
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
self.send_password_to_gpaste(base_args, name, field)
|
|
|
|
|