Use exit 4 when sending a single recipient message fails due to untrusted identity key

Fixes #88
This commit is contained in:
AsamK 2021-01-16 15:38:38 +01:00
parent e1134d832a
commit 06404667a1
6 changed files with 57 additions and 25 deletions

View file

@ -404,6 +404,7 @@ signal-cli -u USERNAME trust -a NUMBER
* *1*: Error is probably caused and fixable by the user * *1*: Error is probably caused and fixable by the user
* *2*: Some unexpected error * *2*: Some unexpected error
* *3*: Server or IO error * *3*: Server or IO error
* *4*: Sending failed due to untrusted key
== Files == Files

View file

@ -15,21 +15,21 @@ public interface Signal extends DBusInterface {
long sendMessage( long sendMessage(
String message, List<String> attachments, String recipient String message, List<String> attachments, String recipient
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber; ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity;
long sendMessage( long sendMessage(
String message, List<String> attachments, List<String> recipients String message, List<String> attachments, List<String> recipients
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UnregisteredUser, Error.UntrustedIdentity; ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity;
long sendNoteToSelfMessage( long sendNoteToSelfMessage(
String message, List<String> attachments String message, List<String> attachments
) throws Error.AttachmentInvalid, Error.Failure, Error.UnregisteredUser, Error.UntrustedIdentity; ) throws Error.AttachmentInvalid, Error.Failure;
void sendEndSessionMessage(List<String> recipients) throws Error.Failure, Error.InvalidNumber, Error.UnregisteredUser, Error.UntrustedIdentity; void sendEndSessionMessage(List<String> recipients) throws Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity;
long sendGroupMessage( long sendGroupMessage(
String message, List<String> attachments, byte[] groupId String message, List<String> attachments, byte[] groupId
) throws Error.GroupNotFound, Error.Failure, Error.AttachmentInvalid, Error.UnregisteredUser, Error.UntrustedIdentity; ) throws Error.GroupNotFound, Error.Failure, Error.AttachmentInvalid;
String getContactName(String number) throws Error.InvalidNumber; String getContactName(String number) throws Error.InvalidNumber;
@ -47,7 +47,7 @@ public interface Signal extends DBusInterface {
byte[] updateGroup( byte[] updateGroup(
byte[] groupId, String name, List<String> members, String avatar byte[] groupId, String name, List<String> members, String avatar
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound, Error.UnregisteredUser, Error.UntrustedIdentity; ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound;
boolean isRegistered(); boolean isRegistered();
@ -198,13 +198,6 @@ public interface Signal extends DBusInterface {
} }
} }
class UnregisteredUser extends DBusExecutionException {
public UnregisteredUser(final String message) {
super(message);
}
}
class UntrustedIdentity extends DBusExecutionException { class UntrustedIdentity extends DBusExecutionException {
public UntrustedIdentity(final String message) { public UntrustedIdentity(final String message) {

View file

@ -66,6 +66,9 @@ public class SendCommand implements DbusCommand {
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; return 1;
} catch (Signal.Error.UntrustedIdentity e) {
System.err.println("Failed to send message: " + e.getMessage());
return 4;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send message: " + e.getMessage()); System.err.println("Failed to send message: " + e.getMessage());
return 2; return 2;
@ -118,6 +121,9 @@ public class SendCommand implements DbusCommand {
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; return 1;
} catch (Signal.Error.UntrustedIdentity e) {
System.err.println("Failed to send message: " + e.getMessage());
return 4;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send note to self message: " + e.getMessage()); System.err.println("Failed to send note to self message: " + e.getMessage());
return 2; return 2;
@ -134,6 +140,9 @@ public class SendCommand implements DbusCommand {
} catch (UnknownObject e) { } catch (UnknownObject e) {
System.err.println("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage()); System.err.println("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
return 1; return 1;
} catch (Signal.Error.UntrustedIdentity e) {
System.err.println("Failed to send message: " + e.getMessage());
return 4;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send message: " + e.getMessage()); System.err.println("Failed to send message: " + e.getMessage());
return 2; return 2;

View file

@ -45,9 +45,30 @@ public class DbusSignalImpl implements Signal {
return sendMessage(message, attachments, recipients); return sendMessage(message, attachments, recipients);
} }
private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException {
String error = ErrorUtils.getErrorMessageFromSendMessageResult(result);
if (error == null) {
return;
}
final String message = timestamp + "\nFailed to send message:\n" + error + '\n';
if (result.getIdentityFailure() != null) {
throw new Error.UntrustedIdentity(message);
} else {
throw new Error.Failure(message);
}
}
private static void checkSendMessageResults( private static void checkSendMessageResults(
long timestamp, List<SendMessageResult> results long timestamp, List<SendMessageResult> results
) throws DBusExecutionException { ) throws DBusExecutionException {
if (results.size() == 1) {
checkSendMessageResult(timestamp, results.get(0));
return;
}
List<String> errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); List<String> errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results);
if (errors.size() == 0) { if (errors.size() == 0) {
return; return;
@ -81,10 +102,10 @@ public class DbusSignalImpl implements Signal {
@Override @Override
public long sendNoteToSelfMessage( public long sendNoteToSelfMessage(
final String message, final List<String> attachments final String message, final List<String> attachments
) throws Error.AttachmentInvalid, Error.Failure, Error.UnregisteredUser, Error.UntrustedIdentity { ) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
try { try {
final Pair<Long, List<SendMessageResult>> results = m.sendSelfMessage(message, attachments); final Pair<Long, SendMessageResult> results = m.sendSelfMessage(message, attachments);
checkSendMessageResults(results.first(), results.second()); checkSendMessageResult(results.first(), results.second());
return results.first(); return results.first();
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
throw new Error.AttachmentInvalid(e.getMessage()); throw new Error.AttachmentInvalid(e.getMessage());

View file

@ -953,7 +953,7 @@ public class Manager implements Closeable {
return sendMessage(messageBuilder, getSignalServiceAddresses(recipients)); return sendMessage(messageBuilder, getSignalServiceAddresses(recipients));
} }
public Pair<Long, List<SendMessageResult>> sendSelfMessage( public Pair<Long, SendMessageResult> sendSelfMessage(
String messageText, List<String> attachments String messageText, List<String> attachments
) throws IOException, AttachmentInvalidException { ) throws IOException, AttachmentInvalidException {
final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder() final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
@ -1278,7 +1278,7 @@ public class Manager implements Closeable {
} }
} }
private Pair<Long, List<SendMessageResult>> sendSelfMessage( private Pair<Long, SendMessageResult> sendSelfMessage(
SignalServiceDataMessage.Builder messageBuilder SignalServiceDataMessage.Builder messageBuilder
) throws IOException { ) throws IOException {
final long timestamp = System.currentTimeMillis(); final long timestamp = System.currentTimeMillis();
@ -1294,7 +1294,7 @@ public class Manager implements Closeable {
SignalServiceDataMessage message = messageBuilder.build(); SignalServiceDataMessage message = messageBuilder.build();
final SendMessageResult result = sendSelfMessage(message); final SendMessageResult result = sendSelfMessage(message);
return new Pair<>(timestamp, List.of(result)); return new Pair<>(timestamp, result);
} finally { } finally {
account.save(); account.save();
} }

View file

@ -33,18 +33,26 @@ public class ErrorUtils {
public static List<String> getErrorMessagesFromSendMessageResults(List<SendMessageResult> results) { public static List<String> getErrorMessagesFromSendMessageResults(List<SendMessageResult> results) {
List<String> errors = new ArrayList<>(); List<String> errors = new ArrayList<>();
for (SendMessageResult result : results) { for (SendMessageResult result : results) {
if (result.isNetworkFailure()) { String error = getErrorMessageFromSendMessageResult(result);
errors.add(String.format("Network failure for \"%s\"", result.getAddress().getLegacyIdentifier())); if (error != null) {
} else if (result.isUnregisteredFailure()) { errors.add(error);
errors.add(String.format("Unregistered user \"%s\"", result.getAddress().getLegacyIdentifier()));
} else if (result.getIdentityFailure() != null) {
errors.add(String.format("Untrusted Identity for \"%s\"", result.getAddress().getLegacyIdentifier()));
} }
} }
return errors; return errors;
} }
public static String getErrorMessageFromSendMessageResult(SendMessageResult result) {
if (result.isNetworkFailure()) {
return String.format("Network failure for \"%s\"", result.getAddress().getLegacyIdentifier());
} else if (result.isUnregisteredFailure()) {
return String.format("Unregistered user \"%s\"", result.getAddress().getLegacyIdentifier());
} else if (result.getIdentityFailure() != null) {
return String.format("Untrusted Identity for \"%s\"", result.getAddress().getLegacyIdentifier());
}
return null;
}
private static int handleSendMessageResultErrors(List<String> errors) { private static int handleSendMessageResultErrors(List<String> errors) {
if (errors.size() == 0) { if (errors.size() == 0) {
return 0; return 0;