diff --git a/meson.build b/meson.build index 04d3d6f..539ea22 100644 --- a/meson.build +++ b/meson.build @@ -13,5 +13,13 @@ gtk = dependency('gtk+-3.0', version: gtk_required) json_glib = dependency('json-glib-1.0', version: json_glib_required) libsodium = dependency('libsodium', version: libsodium_required) +confinc = include_directories('.') + +cdata = configuration_data() +cdata.set_quoted('GETTEXT_PACKAGE', 'ssb-gtk') + +config_h = configure_file(output: 'config.h', + configuration: cdata) + subdir('data') subdir('ssb-gtk') diff --git a/ssb-gtk/meson.build b/ssb-gtk/meson.build index 9ccce07..d266933 100644 --- a/ssb-gtk/meson.build +++ b/ssb-gtk/meson.build @@ -3,9 +3,11 @@ sources = [ 'ssb-app.c', 'ssb-window.c', 'ssb-profile.c', + 'ssb-packet-stream.c', 'ssb-scuttler.c', ] executable('ssb-gtk', sources, ssb_resources, dependencies: [glib, gtk, json_glib, libsodium], + include_directories: [confinc], install: true) diff --git a/ssb-gtk/ssb-app.c b/ssb-gtk/ssb-app.c index c73c875..b8e1fdc 100644 --- a/ssb-gtk/ssb-app.c +++ b/ssb-gtk/ssb-app.c @@ -1,6 +1,6 @@ #include "ssb-app.h" #include "ssb-window.h" -#include "sbot.h" +#include "ssb-scuttler.h" struct _SsbApp { GtkApplication parent_instance; @@ -94,7 +94,8 @@ connect_finished(GObject *source, GAsyncResult *res, gpointer user_data) SsbScuttler *scuttler = SSB_SCUTTLER(source); SsbApp *app = user_data; GError *err = NULL; - gboolean connect_successful = ssb_scuttler_connect_finish(scuttler, res, &err); + gboolean + connect_successful = ssb_scuttler_connect_finish(scuttler, res, &err); ssb_scuttler_whoami_async(scuttler, NULL, whoami_finished, app); } diff --git a/ssb-gtk/ssb-packet-stream.c b/ssb-gtk/ssb-packet-stream.c new file mode 100644 index 0000000..e11bff8 --- /dev/null +++ b/ssb-gtk/ssb-packet-stream.c @@ -0,0 +1,198 @@ +#include "config.h" + +#include + +#include "ssb-packet-stream.h" + +#define DEFAULT_SHS_CAP_KEY "\xd4\xa1\xcb\x88\xa6\x6f\x02\xf8\xdb\x63\x5c\xe2\x64\x41\xcc\x5d" \ + "\xac\x1b\x08\x42\x0c\xea\xac\x23\x08\x39\xb7\x55\x84\x5a\x9f\xfb" + +static void ssb_packet_stream_initable_interface_init(GInitableIface *iface); + +static GInitableIface *ssb_packet_stream_parent_initable_iface; + +typedef struct { + GIOStream *base_io_stream; + GInputStream *input_stream; + GOutputStream *output_stream; +} SsbPacketStreamPrivate; + +G_DEFINE_TYPE_WITH_CODE(SsbPacketStream, ssb_packet_stream, G_TYPE_IO_STREAM, + G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, ssb_packet_stream_initable_interface_init) + G_ADD_PRIVATE(SsbPacketStream)); + +enum { + PROP_0, + PROP_BASE_IO_STREAM, + PROP_COUNT +}; + +static GParamSpec *properties[PROP_COUNT]; + +static GInputStream * +ssb_packet_stream_get_input_stream(GIOStream *iostream) +{ + SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private( + SSB_PACKET_STREAM(iostream)); + + return priv->input_stream; +} + +static GOutputStream * +ssb_packet_stream_get_output_stream(GIOStream *iostream) +{ + SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private( + SSB_PACKET_STREAM(iostream)); + + return priv->output_stream; +} + +static gboolean +ssb_packet_stream_close_fn(GIOStream *iostream, GCancellable *cancellable, GError **error) +{ + // TODO: How to do this? + return TRUE; +} + +static void +close_thread(GTask *task, gpointer object, gpointer task_data, GCancellable *cancellable) +{ + GIOStream *iostream = object; + GError *err = NULL; + + if (!ssb_packet_stream_close(st)) +} + +static void +ssb_packet_stream_close_async(GIOStream *iostream, + GIOPriority io_priority, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GTask *task = g_task_new(stream, cancellable, callback, user_data); + g_task_set_source_tag(task, ssb_packet_stream_close_async); + g_task_set_priority(task, io_priority); + g_task_run_in_thread(task, close_thread); + g_object_unref(task); +} + +static gboolean +ssb_packet_stream_close_finish(GIOStream *stream, GAsyncResult *result, GError **error) +{ + g_return_val_if_fail(g_task_is_valid(result, stream), FALSE); + + return g_task_propagate_boolean(G_TASK(result), error); +} + +static void +ssb_packet_stream_get_property(GObject *gobject, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private( + SSB_PACKET_STREAM(gobject)); + + switch (prop_id) { + case PROP_BASE_IO_STREAM: + g_value_set_object(value, priv->base_io_stream); + + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec); + } +} + +static void +ssb_packet_stream_set_property(GObject *gobject, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private( + SSB_PACKET_STREAM(gobject)); + + switch (prop_id) { + case PROP_BASE_IO_STREAM: + g_clear_object(&(priv->base_io_stream)); + priv->base_io_stream = g_value_dup_object(value); + + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec); + } +} + +static void +ssb_packet_stream_finalize(GObject *gobject) +{ + SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private( + SSB_PACKET_STREAM(gobject)); + + g_clear_object(&(priv->base_io_stream)); +} + +static void +ssb_packet_stream_init(SsbPacketStream *stream) +{ + SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private(stream); + + priv->base_io_stream = NULL; +} + +static void +ssb_packet_stream_class_init(SsbPacketStreamClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + GIOStreamClass *iostream_class = G_IO_STREAM_CLASS(klass); + + gobject_class->get_property = ssb_packet_stream_get_property; + gobject_class->set_property = ssb_packet_stream_set_property; + gobject_class->finalize = ssb_packet_stream_finalize; + + iostream_class->get_input_stream = ssb_packet_stream_get_input_stream; + iostream_class->get_output_stream = ssb_packet_stream_get_output_stream; + iostream_class->close_fn = ssb_packet_stream_close_fn; + iostream_class->close_async = ssb_packet_stream_close_async; + iostream_class->close_finish = ssb_packet_stream_close_finish; + + properties[PROP_BASE_IO_STREAM] = g_param_spec_object( + "base-io-stream", P_("Base IOStream"), P_("The GIOStream that the connection wraps"), + G_TYPE_IO_STREAM, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties(gobject_class, PROP_COUNT, properties); +} + +static gboolean +ssb_packet_stream_initable_init(GInitable *initable, GCancellable *cancellable, GError **error) +{ + if (!ssb_packet_stream_parent_initable_iface->init(initable, cancellable, error)) { + return FALSE; + } + + return TRUE; +} + +static void +ssb_packet_stream_initable_interface_init(GInitableIface *iface) +{ + ssb_packet_stream_parent_initable_iface = g_type_interface_peek_parent(iface); + + iface->init = ssb_packet_stream_initable_init; +} + +GIOStream * +ssb_packet_stream_new(GIOStream *base_io_stream, GError **error) +{ + GObject *conn; + + conn = g_initable_new(SSB_TYPE_PACKET_STREAM, NULL, error, + "base-io-stream", base_io_stream, + NULL); + + return G_IO_STREAM(conn); +} diff --git a/ssb-gtk/ssb-packet-stream.h b/ssb-gtk/ssb-packet-stream.h new file mode 100644 index 0000000..4d9303c --- /dev/null +++ b/ssb-gtk/ssb-packet-stream.h @@ -0,0 +1,21 @@ +#ifndef __SSB_PACKET_STREAM_H__ +# define __SSB_PACKET_STREAM_H__ + +# include +# include + +G_BEGIN_DECLS + +# define SSB_TYPE_PACKET_STREAM (ssb_packet_stream_get_type()) +G_DECLARE_DERIVABLE_TYPE(SsbPacketStream, ssb_packet_stream, SSB, PACKET_STREAM, GIOStream) + +struct _SsbPacketStreamClass { + GIOStreamClass parent_class; +}; + +GIOStream *ssb_packet_stream_new(GIOStream *io_stream, GError **error); + +G_END_DECLS + + +#endif /* __SSB_PACKET_STREAM_H__ */ diff --git a/ssb-gtk/ssb-scuttler.c b/ssb-gtk/ssb-scuttler.c index d77171c..14a22f5 100644 --- a/ssb-gtk/ssb-scuttler.c +++ b/ssb-gtk/ssb-scuttler.c @@ -1,1245 +1,15 @@ -/* - * sbotc.c - * Copyright (c) 2017 Secure Scuttlebutt Consortium - * - * Usage of the works is permitted provided that this instrument is - * retained with the works, so that any entity that uses the works is - * notified of this instrument. - * - * DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY. - */ - #include "ssb-scuttler.h" -#include -#include - -#define BOXS_MAXLEN 4096 - -#define write_buf(fd, buf) \ - write_all(fd, buf, sizeof(buf)-1) - -typedef struct { - uint16_t len; - uint8_t mac[16]; -} BoxsHeader; - struct _SsbScuttler { - GObject parent_instance; - - gboolean ip4_only; - gboolean ip6_only; - GSocketClient *socket_client; - GSocketConnection *connection; - - int s; - unsigned char encrypt_key[32]; - unsigned char decrypt_key[32]; - unsigned char nonce1[24]; - unsigned char nonce2[24]; - unsigned char rx_nonce[24]; - unsigned char rx_buf[BOXS_MAXLEN]; - size_t rx_buf_pos; - size_t rx_buf_len; - gboolean noauth; - gboolean wrote_goodbye; -}; - -typedef enum { - pkt_type_buffer = 0, - pkt_type_string = 1, - pkt_type_json = 2, -} SsbPacketType; - -typedef enum { - pkt_flags_buffer = 0, - pkt_flags_string = 1, - pkt_flags_json = 2, - pkt_flags_end = 4, - pkt_flags_stream = 8, -} SsbPacketFlags; - -typedef struct __attribute__((packed)){ - uint32_t len; - int32_t req; -} SsbPacketHeader; - -enum muxrpc_type { - muxrpc_type_async, - muxrpc_type_source, - muxrpc_type_sink, - muxrpc_type_duplex, -}; - -enum stream_state { - stream_state_open, - stream_state_ended_ok, - stream_state_ended_error, -}; - -enum ip_family { - ip_family_ipv4, - ip_family_ipv6, - ip_family_any, + GInitiallyUnowned parent_instance; }; G_DEFINE_TYPE(SsbScuttler, ssb_scuttler, G_TYPE_INITIALLY_UNOWNED); -static unsigned char zeros[24] = {0}; +static void +ssb_scuttler_init(SsbScuttler *scuttler) +{} -static const unsigned char ssb_cap[] = { - 0xd4, 0xa1, 0xcb, 0x88, 0xa6, 0x6f, 0x02, 0xf8, - 0xdb, 0x63, 0x5c, 0xe2, 0x64, 0x41, 0xcc, 0x5d, - 0xac, 0x1b, 0x08, 0x42, 0x0c, 0xea, 0xac, 0x23, - 0x08, 0x39, 0xb7, 0x55, 0x84, 0x5a, 0x9f, 0xfb -}; - -static gboolean -tcp_connect(SsbScuttler *scuttler, const char *host, guint16 port, GError **error) { - GList *addresses; - GResolver *resolver; - GError *err = NULL; - GSocketConnection *connection = NULL; - - resolver = g_resolver_get_default(); - addresses = g_resolver_lookup_by_name(resolver, host, NULL, &err); - g_object_unref(resolver); - - if (addresses == NULL) { - g_propagate_error(error, err); - - return FALSE; - } - - for (GList *elem = addresses; elem; elem = elem->next) { - GInetAddress *address = elem->data; - GSocketAddress *connectable; - - if (((g_inet_address_get_family(address) == G_SOCKET_FAMILY_IPV4) && scuttler->ip6_only) || - ((g_inet_address_get_family(address) == G_SOCKET_FAMILY_IPV6) && scuttler->ip4_only)) { - continue; - } - - connectable = g_inet_socket_address_new(address, port); - - if ((connection = g_socket_client_connect(scuttler->socket_client, G_SOCKET_CONNECTABLE(connectable), NULL, &err)) != NULL) { - break; - } - - g_clear_object(&connectable); - } - - if (connection == NULL) { - g_propagate_error(error, err); - - return FALSE; - } - - g_clear_object(&(scuttler->connection)); - scuttler->connection = connection; - - return TRUE; -} - -static gboolean -unix_connect(SsbScuttler *scuttler, const gchar *path, GError **error) -{ - GSocketAddress *connectable = g_unix_socket_address_new(path); - GError *err = NULL; - GSocketConnection *connection; - - if ((connection = g_socket_client_connect(scuttler->socket_client, G_SOCKET_CONNECTABLE(connectable), NULL, &err)) == NULL) { - g_propagate_error(error, err); - - return FALSE; - } - - g_clear_object(&(scuttler->connection)); - scuttler->connection = connection; - - return TRUE; -} - -static gboolean -get_socket_path(gchar **socket_path, const gchar *app_dir) -{ - *socket_path = g_strdup_printf("%s/socket", app_dir); - GFile *socket_file = g_file_new_for_path(*socket_path); - GFileType socket_type = g_file_query_file_type(socket_file, G_FILE_QUERY_INFO_NONE, NULL); - - g_object_unref(socket_file); - - return (socket_type == G_FILE_TYPE_SPECIAL); -} - -static int -read_all(SsbScuttler *scuttler, void *buf, size_t count, GError **error) -{ - gssize nbytes; - GInputStream *stream = g_io_stream_get_input_stream(G_IO_STREAM(scuttler->connection)); - GError *err = NULL; - - while (count > 0) { - nbytes = g_input_stream_read(stream, buf, count, NULL, &err); - - if (nbytes == 0) { - if (err) { - g_propagate_error(error, err); - } else { - g_set_error(error, G_IO_ERROR, G_IO_ERROR_BROKEN_PIPE, "Unexpected end of stream"); - } - - return FALSE; - } - - if (nbytes < 0) { - g_propagate_error(error, err); - - return FALSE; - } - - buf += nbytes; - count -= nbytes; - } - - return TRUE; -} - -static gboolean -read_some(SsbScuttler *scuttler, unsigned char *buf, size_t *lenp, GError **error) -{ - ssize_t nbytes; - GInputStream *stream = g_io_stream_get_input_stream(G_IO_STREAM(scuttler->connection)); - GError *err = NULL; - - do { - nbytes = g_input_stream_read(stream, buf, *lenp, NULL, &err); - } while (nbytes < 0); - - if (nbytes == 0) { - g_set_error(error, G_IO_ERROR, G_IO_ERROR_BROKEN_PIPE, "Unexpected end of stream"); - - return FALSE; - } - - if (nbytes < 0) { - g_propagate_error(error, err); - - return FALSE; - } - - if (lenp) { - *lenp = nbytes; - } - - return TRUE; -} - -static gboolean -write_all(SsbScuttler *scuttler, const void *buf, size_t count, GError **error) -{ - gssize nbytes; - GOutputStream *stream = g_io_stream_get_output_stream(G_IO_STREAM(scuttler->connection)); - GError *err = NULL; - - while (count > 0) { - nbytes = g_output_stream_write(stream, buf, count, NULL, &err); - - if (nbytes < 0 && errno == EINTR) continue; - if (nbytes < 0) return -1; - buf += nbytes; - count -= nbytes; - } - return 0; -} - -static void shs_connect(int sfd, int infd, int outfd, - const unsigned char pubkey[32], - const unsigned char seckey[64], - const unsigned char appkey[32], - const unsigned char server_pubkey[32], - SsbScuttler *bs) { - int rc; - unsigned char local_app_mac[32], remote_app_mac[32]; - - unsigned char kx_pk[32], kx_sk[32]; - rc = crypto_box_keypair(kx_pk, kx_sk); - if (rc < 0) errx(1, "failed to generate auth keypair"); - - rc = crypto_auth(local_app_mac, kx_pk, 32, appkey); - if (rc < 0) err(1, "failed to generate app mac"); - - // send challenge - unsigned char buf[64]; - memcpy(buf, local_app_mac, 32); - memcpy(buf+32, kx_pk, 32); - rc = write_all(outfd, buf, sizeof(buf)); - if (rc < 0) err(1, "failed to send challenge"); - - // recv challenge - unsigned char remote_kx_pk[32]; - rc = read_all(scuttler, buf, sizeof(buf)); - if (rc < 0) err(1, "challenge not accepted"); - memcpy(remote_app_mac, buf, 32); - memcpy(remote_kx_pk, buf+32, 32); - rc = crypto_auth_verify(buf, remote_kx_pk, 32, appkey); - if (rc < 0) errx(1, "wrong protocol (version?)"); - - // send auth - - unsigned char secret[32]; - rc = crypto_scalarmult(secret, kx_sk, remote_kx_pk); - if (rc < 0) errx(1, "failed to derive shared secret"); - - unsigned char remote_pk_curve[32]; - rc = crypto_sign_ed25519_pk_to_curve25519(remote_pk_curve, server_pubkey); - if (rc < 0) errx(1, "failed to curvify remote public key"); - - unsigned char a_bob[32]; - rc = crypto_scalarmult(a_bob, kx_sk, remote_pk_curve); - if (rc < 0) errx(1, "failed to derive a_bob"); - - unsigned char secret2a[96]; - memcpy(secret2a, appkey, 32); - memcpy(secret2a+32, secret, 32); - memcpy(secret2a+64, a_bob, 32); - - unsigned char secret2[32]; - rc = crypto_hash_sha256(secret2, secret2a, sizeof(secret2a)); - if (rc < 0) errx(1, "failed to hash secret2"); - - unsigned char shash[32]; - rc = crypto_hash_sha256(shash, secret, sizeof(secret)); - if (rc < 0) errx(1, "failed to hash secret"); - - unsigned char signed1[96]; - memcpy(signed1, appkey, 32); - memcpy(signed1+32, server_pubkey, 32); - memcpy(signed1+64, shash, 32); - - unsigned char sig[64]; - rc = crypto_sign_detached(sig, NULL, signed1, sizeof(signed1), seckey); - if (rc < 0) errx(1, "failed to sign inner hello"); - - unsigned char hello[96]; - memcpy(hello, sig, 64); - memcpy(hello+64, pubkey, 32); - - unsigned char boxed_auth[112]; - rc = crypto_secretbox_easy(boxed_auth, hello, sizeof(hello), zeros, secret2); - if (rc < 0) errx(1, "failed to box hello"); - - rc = write_all(outfd, boxed_auth, sizeof(boxed_auth)); - if (rc < 0) errx(1, "failed to send auth"); - - // verify accept - - unsigned char boxed_okay[80]; - rc = read_all(scuttler, boxed_okay, sizeof(boxed_okay)); - if (rc < 0) err(1, "hello not accepted"); - - unsigned char local_sk_curve[32]; - rc = crypto_sign_ed25519_sk_to_curve25519(local_sk_curve, seckey); - if (rc < 0) errx(1, "failed to curvify local secret key"); - - unsigned char b_alice[32]; - rc = crypto_scalarmult(b_alice, local_sk_curve, remote_kx_pk); - if (rc < 0) errx(1, "failed to derive b_alice"); - - unsigned char secret3a[128]; - memcpy(secret3a, appkey, 32); - memcpy(secret3a+32, secret, 32); - memcpy(secret3a+64, a_bob, 32); - memcpy(secret3a+96, b_alice, 32); - - unsigned char secret3[32]; - rc = crypto_hash_sha256(secret3, secret3a, sizeof(secret3a)); - if (rc < 0) errx(1, "failed to hash secret3"); - - rc = crypto_secretbox_open_easy(sig, boxed_okay, sizeof(boxed_okay), zeros, secret3); - if (rc < 0) errx(1, "failed to unbox the okay"); - - unsigned char signed2[160]; - memcpy(signed2, appkey, 32); - memcpy(signed2+32, hello, 96); - memcpy(signed2+128, shash, 32); - - rc = crypto_sign_verify_detached(sig, signed2, sizeof(signed2), server_pubkey); - if (rc < 0) errx(1, "server not authenticated"); - - rc = crypto_hash_sha256(secret, secret3, 32); - if (rc < 0) errx(1, "failed to hash secret3"); - - unsigned char enc_key_hashed[64]; - memcpy(enc_key_hashed, secret, 32); - memcpy(enc_key_hashed+32, server_pubkey, 32); - rc = crypto_hash_sha256(bs->encrypt_key, enc_key_hashed, 64); - if (rc < 0) errx(1, "failed to hash the encrypt key"); - - unsigned char dec_key_hashed[64]; - memcpy(dec_key_hashed, secret, 32); - memcpy(dec_key_hashed+32, pubkey, 32); - rc = crypto_hash_sha256(bs->decrypt_key, dec_key_hashed, 64); - if (rc < 0) errx(1, "failed to hash the decrypt key"); - - memcpy(bs->nonce1, remote_app_mac, 24); - memcpy(bs->nonce2, remote_app_mac, 24); - memcpy(bs->rx_nonce, local_app_mac, 24); - - bs->rx_buf_pos = 0; - bs->rx_buf_len = 0; - bs->s = sfd; - bs->noauth = false; - bs->wrote_goodbye = false; -} - -static int pubkey_decode(const char *key_str, unsigned char key[32]) { - if (!key_str) { errno = EPROTO; return -1; } - if (!*key_str) { errno = EPROTO; return -1; } - if (*key_str == '@') key_str++; - size_t len = strlen(key_str); - if (len == 52 && strcmp(key_str+44, ".ed25519") == 0) {} - else if (len != 44) { errno = EMSGSIZE; return -1; } - return base64_decode(key_str, 44, key, 32); -} - -static jsmntok_t *json_lookup(const char *buf, jsmntok_t *tok, const char *prop, size_t prop_len) { - jsmntok_t *end = tok + tok->size + 1; - if (tok->type != JSMN_OBJECT) { errno = EPROTO; return NULL; } - tok++; - while (tok < end) { - if (tok + 1 >= end) { errno = EPROTO; return NULL; } - if (tok->type == JSMN_STRING - && tok->end - tok->start == (int)prop_len - && memcmp(buf + tok->start, prop, prop_len) == 0) - return tok + 1; - tok += tok->size + 1; - end += tok->size; - } - return NULL; -} - -static ssize_t json_get_value(const char *buf, const char *path, const char **value) { - static const int num_tokens = 1024; - jsmntok_t tokens[num_tokens], *tok = tokens; - jsmn_parser parser; - - jsmn_init(&parser); - switch (jsmn_parse(&parser, buf, tokens, num_tokens)) { - case JSMN_ERROR_NOMEM: errno = ENOMEM; return -1; - case JSMN_ERROR_INVAL: errno = EINVAL; return -1; - case JSMN_ERROR_PART: errno = EMSGSIZE; return -1; - case JSMN_SUCCESS: break; - default: errno = EPROTO; return -1; - } - - while (*path) { - const char *end = strchr(path, '.'); - size_t part_len = end ? (size_t)end - (size_t)path : strlen(path); - tok = json_lookup(buf, tok, path, part_len); - if (!tok) { errno = ENOMSG; return -1; } - path += part_len; - if (*path == '.') path++; - } - - *value = buf + tok->start; - return tok->end - tok->start; -} - -static void get_app_dir(char *dir, size_t len) { - const char *path, *home, *appname; - int rc; - path = getenv("ssb_path"); - if (path) { - if (strlen(path) > len) errx(1, "ssb_path too long"); - strncpy(dir, path, len); - return; - } - home = getenv("HOME"); - if (!home) home = "."; - appname = getenv("ssb_appname"); - if (!appname) appname = "ssb"; - rc = snprintf(dir, len, "%s/.%s", home, appname); - if (rc < 0) err(1, "failed to get app dir"); - if ((size_t)rc >= len) errx(1, "path to app dir too long"); -} - -static ssize_t read_file(char *buf, size_t len, const char *fmt, ...) { - va_list ap; - int rc; - struct stat st; - int fd; - - va_start(ap, fmt); - rc = vsnprintf(buf, len, fmt, ap); - va_end(ap); - if (rc < 0) return -1; - if ((size_t)rc >= len) { errno = ENAMETOOLONG; return -1; } - - rc = stat(buf, &st); - if (rc < 0) return -1; - if (st.st_size > (off_t)(len-1)) { errno = EMSGSIZE; return -1; } - - fd = open(buf, O_RDONLY); - if (fd < 0) return -1; - - rc = read_all(scuttler, buf, st.st_size); - if (rc < 0) return -1; - buf[st.st_size] = '\0'; - - close(fd); - return st.st_size; -} - - -static void read_private_key(const char *dir, unsigned char pk[64]) { - ssize_t len; - char buf[8192]; - const char *pk_b64; - int rc; - ssize_t key_len; - char *line; - - len = read_file(buf, sizeof(buf), "%s/secret", dir); - if (len < 0) err(1, "failed to read secret file"); - - // strip comments - for (line = buf; *line; ) { - if (*line == '#') while (*line && *line != '\n') *line++ = ' '; - else while (*line && *line++ != '\n'); - } - - key_len = json_get_value(buf, "private", &pk_b64); - if (key_len < 0) err(1, "unable to read private key"); - - if (key_len > 8 && memcmp(pk_b64 + key_len - 8, ".ed25519", 8) == 0) - key_len -= 8; - rc = base64_decode(pk_b64, key_len, pk, 64); - if (rc < 0) err(1, "unable to decode private key"); -} - -static void increment_nonce(uint8_t nonce[24]) { - int i; - for (i = 23; i >= 0 && nonce[i] == 0xff; i--) nonce[i] = 0; - if (i >= 0) nonce[i]++; -} - -static void bs_write_end_box(SsbScuttler *bs) { - unsigned char boxed[34]; - int rc = crypto_secretbox_easy(boxed, zeros, 18, bs->nonce1, bs->encrypt_key); - if (rc < 0) errx(1, "failed to box packet end header"); - increment_nonce(bs->nonce1); - increment_nonce(bs->nonce2); - rc = write_all(bs->s, boxed, 34); - if (rc < 0) err(1, "failed to write boxed end header"); -} - -static void bs_write_packet(SsbScuttler *bs, const unsigned char *buf, uint16_t len) { - size_t boxed_len = len + 34; - unsigned char boxed[boxed_len]; - increment_nonce(bs->nonce2); - int rc = crypto_secretbox_easy(boxed + 18, buf, len, bs->nonce2, bs->encrypt_key); - if (rc < 0) errx(1, "failed to box packet data"); - BoxsHeader header; - header.len = htons(len); - memcpy(header.mac, boxed + 18, 16); - rc = crypto_secretbox_easy(boxed, (unsigned char *)&header, 18, bs->nonce1, bs->encrypt_key); - if (rc < 0) errx(1, "failed to box packet header"); - increment_nonce(bs->nonce1); - increment_nonce(bs->nonce1); - increment_nonce(bs->nonce2); - rc = write_all(bs->s, boxed, boxed_len); - if (rc < 0) err(1, "failed to write boxed packet"); -} - -static void bs_end(SsbScuttler *bs) { - if (!bs->noauth) { - bs_write_end_box(bs); - } - shutdown(bs->s, SHUT_WR); -} - -static int bs_read_packet(struct boxs *bs, void *buf, size_t *lenp) { - int rc; - if (bs->noauth) { - rc = read_some(bs->s, buf, lenp); - if (rc < 0 && errno == EPIPE) return -1; - if (rc < 0) err(1, "failed to read packet data"); - return 0; - } - unsigned char boxed_header[34]; - BoxsHeader header; - rc = read_all(scuttler, boxed_header, 34); - if (rc < 0 && errno == EPIPE) errx(1, "unexpected end of parent stream"); - if (rc < 0) err(1, "failed to read boxed packet header"); - rc = crypto_secretbox_open_easy((unsigned char *)&header, boxed_header, 34, bs->rx_nonce, bs->decrypt_key); - if (rc < 0) errx(1, "failed to unbox packet header"); - increment_nonce(bs->rx_nonce); - if (header.len == 0 && !memcmp(header.mac, zeros, 16)) { errno = EPIPE; return -1; } - size_t len = ntohs(header.len); - if (len > BOXS_MAXLEN) errx(1, "received boxed packet too large"); - unsigned char boxed_data[len + 16]; - rc = read_all(scuttler, boxed_data + 16, len); - if (rc < 0) err(1, "failed to read boxed packet data"); - memcpy(boxed_data, header.mac, 16); - rc = crypto_secretbox_open_easy(buf, boxed_data, len+16, bs->rx_nonce, bs->decrypt_key); - if (rc < 0) errx(1, "failed to unbox packet data"); - increment_nonce(bs->rx_nonce); - *lenp = len; - return 0; -} - -static int bs_read(struct boxs *bs, char *buf, size_t len) { - if (bs->noauth) { - int rc = read_all(scuttler, buf, len); - if (rc < 0) err(1, "failed to read packet data"); - return 0; - } - size_t remaining; - while (len > 0) { - remaining = bs->rx_buf_len > len ? len : bs->rx_buf_len; - if (buf) memcpy(buf, bs->rx_buf + bs->rx_buf_pos, remaining); - bs->rx_buf_len -= remaining; - bs->rx_buf_pos += remaining; - len -= remaining; - buf += remaining; - if (len == 0) return 0; - if (bs_read_packet(bs, bs->rx_buf, &bs->rx_buf_len) < 0) return -1; - bs->rx_buf_pos = 0; - } - return 0; -} - -static enum stream_state bs_read_out_1(struct boxs *bs, int fd) { - size_t buf[4096]; - size_t len = sizeof(buf); - int rc; - rc = bs_read_packet(bs, buf, &len); - if (rc < 0 && errno == EPIPE) return stream_state_ended_ok; - if (rc < 0) return stream_state_ended_error; - rc = write_all(fd, buf, len); - if (rc < 0) return stream_state_ended_error; - return stream_state_open; -} - -static int bs_read_out(struct boxs *bs, int fd, size_t len) { - size_t chunk; - char buf[4096]; - int rc; - while (len > 0) { - chunk = len > sizeof(buf) ? sizeof(buf) : len; - rc = bs_read(bs, buf, chunk); - if (rc < 0) return -1; - rc = write_all(fd, buf, chunk); - if (rc < 0) return -1; - len -= chunk; - } - return 0; -} - -static int bs_read_error(struct boxs *bs, int errfd, SsbPacketFlags flags, size_t len, gboolean no_newline) { - // suppress printing "true" indicating end without error - if (flags & pkt_flags_json && len == 4) { - char buf[4]; - if (bs_read(bs, buf, 4) < 0) return -1; - if (strncmp(buf, "true", 0) == 0) { - return 0; - } - if (write_all(errfd, buf, 4) < 0) return -1; - } else { - if (bs_read_out(bs, errfd, len) < 0) return -1; - } - if (flags & (pkt_flags_json | pkt_flags_string) && !no_newline) { - if (write_buf(errfd, "\n") < 0) return -1; - } - return 1; -} - -static void bs_write(struct boxs *bs, const unsigned char *buf, size_t len) { - if (bs->noauth) { - int rc = write_all(bs->s, buf, len); - if (rc < 0) err(1, "failed to write packet"); - return; - } - while (len > 0) { - size_t l = len > BOXS_MAXLEN ? BOXS_MAXLEN : len; - bs_write_packet(bs, buf, l); - len -= l; - buf += l; - } -} - -static enum stream_state bs_write_in_1(struct boxs *bs, int fd) { - unsigned char buf[4096]; - ssize_t sz = read(fd, buf, sizeof(buf)); - if (sz < 0) err(1, "read"); - if (sz == 0) { - bs_end(bs); - return stream_state_ended_ok; - } - bs_write(bs, buf, sz); - return stream_state_open; -} - -static void ps_write(struct boxs *bs, - const char *data, - size_t len, - SsbPacketType type, - int req_id, - gboolean stream, - gboolean end) { - size_t out_len = 9 + len; - unsigned char out_buf[out_len]; - SsbPacketHeader header = {htonl(len), htonl(req_id)}; - out_buf[0] = (stream << 3) | (end << 2) | (type & 3); - memcpy(out_buf+1, &header, 8); - memcpy(out_buf+9, data, len); - bs_write(bs, out_buf, out_len); -} - -static void ps_goodbye(struct boxs *bs) { - if (bs->wrote_goodbye) return; - bs->wrote_goodbye = true; - bs_write(bs, zeros, 9); -} - -static int ps_read_header(struct boxs *bs, size_t *len, int *req_id, SsbPacketFlags *flags) { - char buf[9]; - SsbPacketHeader header; - if (bs_read(bs, buf, sizeof(buf)) < 0) return -1; - memcpy(&header, buf+1, 8); - if (len) *len = ntohl(header.len); - if (req_id) *req_id = ntohl(header.req); - if (flags) *flags = buf[0]; - return 0; -} - -static void muxrpc_call(struct boxs *bs, - const char *method, - const char *argument, - enum muxrpc_type type, - const char *typestr, - int req_id) { - char req[33792]; // 32768 max message value size + 1024 extra - ssize_t reqlen; - gboolean is_request = type == muxrpc_type_async; - - if (is_request) { - reqlen = snprintf(req, sizeof(req), - "{\"name\":%s,\"args\":%s}", - method, argument); - } else { - reqlen = snprintf(req, sizeof(req), - "{\"name\":%s,\"args\":%s,\"type\":\"%s\"}", - method, argument, typestr); - } - if (reqlen < 0) err(1, "failed to construct request"); - if ((size_t)reqlen >= sizeof(req)) errx(1, "request too large"); - - ps_write(bs, req, reqlen, pkt_type_json, req_id, !is_request, false); -} - -static int bs_passthrough(struct boxs *bs, int infd, int outfd) { - int rc; - fd_set rd; - int sfd = bs->s; - int maxfd = infd > sfd ? infd : sfd; - enum stream_state in = stream_state_open; - enum stream_state out = stream_state_open; - - while (out == stream_state_open - || (in == stream_state_open && out != stream_state_ended_error)) { - FD_ZERO(&rd); - if (in == stream_state_open) FD_SET(infd, &rd); - if (out == stream_state_open) FD_SET(sfd, &rd); - rc = select(maxfd + 1, &rd, 0, 0, NULL); - if (rc < 0) err(1, "select"); - if (FD_ISSET(infd, &rd)) in = bs_write_in_1(bs, infd); - if (FD_ISSET(sfd, &rd)) out = bs_read_out_1(bs, outfd); - } - - return in == stream_state_ended_ok && out == stream_state_ended_ok ? 0 : - in == stream_state_ended_error || out == stream_state_ended_error ? 2 : 1; -} - -static void ps_reject(struct boxs *bs, size_t len, int32_t req, SsbPacketFlags flags) { - // ignore the packet. if this is a request, the substream on the other end - // will just have to wait until the rpc connection closes. - (void)req; - (void)flags; - write_buf(STDERR_FILENO, "ignoring packet: "); - int rc = bs_read_out(bs, STDERR_FILENO, len); - if (rc < 0) err(1, "bs_read_out"); - write_buf(STDERR_FILENO, "\n"); -} - -static enum stream_state muxrpc_read_source_1(struct boxs *bs, int outfd, int req_id, gboolean no_newline) { - SsbPacketFlags flags; - size_t len; - int32_t req; - int rc = ps_read_header(bs, &len, &req, &flags); - if (rc < 0) err(1, "ps_read_header"); - if (req == 0 && len == 0) { - if (bs->wrote_goodbye) return stream_state_ended_ok; - warnx("unexpected end of parent stream"); - return stream_state_ended_error; - } - if (req != -req_id) { - ps_reject(bs, len, req, flags); - return stream_state_open; - } - if (flags & pkt_flags_end) { - rc = bs_read_error(bs, STDERR_FILENO, flags, len, no_newline); - if (rc < 0) err(1, "bs_read_error"); - if (rc == 1) return stream_state_ended_error; - return stream_state_ended_ok; - } - rc = bs_read_out(bs, outfd, len); - if (rc < 0) err(1, "bs_read_out"); - if (flags & (pkt_flags_json | pkt_flags_string) && !no_newline) { - rc = write_buf(outfd, "\n"); - if (rc < 0) err(1, "write_buf"); - } - return stream_state_open; -} - -static int muxrpc_read_source(struct boxs *bs, int outfd, int req_id, gboolean no_newline) { - enum stream_state state; - while ((state = muxrpc_read_source_1(bs, outfd, req_id, no_newline)) == stream_state_open); - return state == stream_state_ended_ok ? 0 : - state == stream_state_ended_error ? 2 : 1; -} - -static int muxrpc_read_async(struct boxs *bs, int outfd, int req_id, gboolean no_newline) { - SsbPacketFlags flags; - size_t len; - int32_t req; - int rc; - - while (1) { - rc = ps_read_header(bs, &len, &req, &flags); - if (rc < 0) err(1, "ps_read_header"); - if (req == -req_id) break; - if (req == 0 && len == 0) errx(1, "unexpected end of parent stream"); - ps_reject(bs, len, req, flags); - } - if (flags & pkt_flags_end) { - rc = bs_read_error(bs, STDERR_FILENO, flags, len, no_newline); - if (rc < 0) err(1, "bs_read_error"); - if (rc == 1) return 2; - return 1; - } - rc = bs_read_out(bs, outfd, len); - if (rc < 0) err(1, "bs_read_out"); - if (flags & (pkt_flags_json | pkt_flags_string) && !no_newline) { - rc = write_buf(outfd, "\n"); - if (rc < 0) err(1, "write_buf"); - } - return 0; -} - -static enum stream_state muxrpc_write_sink_1(struct boxs *bs, int infd, - SsbPacketType ptype, int req_id) { - char buf[4096]; - ssize_t sz = read(infd, buf, sizeof(buf)); - if (sz < 0) err(1, "read"); - if (sz == 0) { - ps_write(bs, "true", 4, pkt_type_json, req_id, true, true); - return stream_state_ended_ok; - } - ps_write(bs, buf, sz, ptype, req_id, true, false); - return stream_state_open; -} - -static enum stream_state muxrpc_write_sink_1_hashed(struct boxs *bs, int infd, - crypto_hash_sha256_state *hash_state, int req_id) { - int rc; - unsigned char buf[4096]; - ssize_t sz = read(infd, buf, sizeof(buf)); - if (sz < 0) err(1, "read"); - if (sz == 0) { - ps_write(bs, "true", 4, pkt_type_json, req_id, true, true); - return stream_state_ended_ok; - } - rc = crypto_hash_sha256_update(hash_state, buf, sz); - if (rc < 0) errx(1, "hash update failed"); - ps_write(bs, (char *)buf, sz, pkt_type_buffer, req_id, true, false); - return stream_state_open; -} - -static int muxrpc_write_sink(struct boxs *bs, int infd, SsbPacketType ptype, int req_id, gboolean no_newline) { - int rc; - fd_set rd; - int sfd = bs->s; - int maxfd = infd > sfd ? infd : sfd; - enum stream_state in = stream_state_open; - enum stream_state out = stream_state_open; - - while (out == stream_state_open) { - FD_ZERO(&rd); - if (in == stream_state_open) FD_SET(infd, &rd); - if (out == stream_state_open) FD_SET(sfd, &rd); - rc = select(maxfd + 1, &rd, 0, 0, NULL); - if (rc < 0) err(1, "select"); - if (FD_ISSET(infd, &rd)) in = muxrpc_write_sink_1(bs, infd, ptype, req_id); - if (FD_ISSET(sfd, &rd)) out = muxrpc_read_source_1(bs, -1, req_id, no_newline); - } - - return in == stream_state_ended_ok && out == stream_state_ended_ok ? 0 : - in == stream_state_ended_error || out == stream_state_ended_error ? 2 : 1; -} - -static int muxrpc_write_blob_add(struct boxs *bs, int infd, int outfd, int req_id, gboolean no_newline) { - int rc; - fd_set rd; - int sfd = bs->s; - int maxfd = infd > sfd ? infd : sfd; - enum stream_state in = stream_state_open; - enum stream_state out = stream_state_open; - crypto_hash_sha256_state hash_state; - unsigned char hash[32]; - char id[54] = "&"; - - rc = crypto_hash_sha256_init(&hash_state); - if (rc < 0) { errno = EINVAL; return -1; } - - while (out == stream_state_open) { - FD_ZERO(&rd); - if (in == stream_state_open) FD_SET(infd, &rd); - if (out == stream_state_open) FD_SET(sfd, &rd); - rc = select(maxfd + 1, &rd, 0, 0, NULL); - if (rc < 0) err(1, "select"); - if (FD_ISSET(infd, &rd)) in = muxrpc_write_sink_1_hashed(bs, infd, &hash_state, req_id); - if (FD_ISSET(sfd, &rd)) out = muxrpc_read_source_1(bs, -1, req_id, no_newline); - } - - rc = crypto_hash_sha256_final(&hash_state, hash); - if (rc < 0) errx(1, "hash finalize failed"); - - rc = base64_encode(hash, 32, id+1, sizeof(id)-1); - if (rc < 0) err(1, "encoding hash failed"); - strcpy(id + 45, ".sha256\n"); - rc = write_all(outfd, id, sizeof(id)-1); - if (rc < 0) err(1, "writing hash failed"); - - return in == stream_state_ended_ok && out == stream_state_ended_ok ? 0 : - in == stream_state_ended_error || out == stream_state_ended_error ? 2 : 1; -} - -static int muxrpc_duplex(struct boxs *bs, int infd, int outfd, SsbPacketType in_ptype, int req_id, gboolean no_newline) { - int rc; - fd_set rd; - int sfd = bs->s; - int maxfd = infd > sfd ? infd : sfd; - enum stream_state in = stream_state_open; - enum stream_state out = stream_state_open; - - while (out == stream_state_open - || (in == stream_state_open && out != stream_state_ended_error)) { - FD_ZERO(&rd); - if (in == stream_state_open) FD_SET(infd, &rd); - if (out == stream_state_open) FD_SET(sfd, &rd); - rc = select(maxfd + 1, &rd, 0, 0, NULL); - if (rc < 0) err(1, "select"); - if (FD_ISSET(infd, &rd)) in = muxrpc_write_sink_1(bs, infd, in_ptype, req_id); - if (FD_ISSET(sfd, &rd)) out = muxrpc_read_source_1(bs, outfd, req_id, no_newline); - } - - return in == stream_state_ended_ok && out == stream_state_ended_ok ? 0 : - in == stream_state_ended_error || out == stream_state_ended_error ? 2 : 1; -} - -static int method_to_json(char *out, size_t outlen, const char *str) { - // blobs.get => ["blobs", "get"] - size_t i = 0; - char c; - if (i+2 > outlen) return -1; - out[i++] = '['; - out[i++] = '"'; - while ((c = *str++)) { - if (c == '.') { - if (i+3 > outlen) return -1; - out[i++] = '"'; - out[i++] = ','; - out[i++] = '"'; - } else if (c == '"') { - if (i+2 > outlen) return -1; - out[i++] = '\\'; - out[i++] = '"'; - } else { - if (i+1 > outlen) return -1; - out[i++] = c; - } - } - if (i+3 > outlen) return -1; - out[i++] = '"'; - out[i++] = ']'; - out[i++] = '\0'; - return i; -} - -static int args_to_json_length(int argc, char *argv[]) { - int i = 0; - int len = 3; // "[]\0" - for (i = 0; i < argc; i++) - len += strlen(argv[i])+1; - return len; -} - -static int args_to_json(char *out, size_t outlen, unsigned int argc, char *argv[]) { - size_t i = 0; - size_t j; - if (i+1 > outlen) return -1; - out[i++] = '['; - for (j = 0; j < argc; j++) { - size_t len = strlen(argv[j]); - if (j > 0) out[i++] = ','; - if (i+len > outlen) return -1; - strncpy(out+i, argv[j], len); - i += len; - } - if (i+2 > outlen) return -1; - out[i++] = ']'; - out[i++] = '\0'; - return i; -} - -int main(int argc, char *argv[]) { - int i, s, infd, outfd, rc; - const char *key = NULL; - const char *keypair_seed_str = NULL; - const char *host = NULL; - const char *port = "8008"; - const char *typestr = NULL, *methodstr = NULL; - const char *shs_cap_key_str = NULL; - const char *socket_path = NULL; - size_t argument_len; - unsigned char private_key[64]; - unsigned char public_key[32]; - unsigned char remote_key[32]; - unsigned char shs_cap_key[32]; - enum muxrpc_type type; - SsbPacketType ptype = pkt_type_buffer; - char method[256]; - char app_dir[_POSIX_PATH_MAX]; - ssize_t len; - gboolean test = false; - gboolean noauth = false; - gboolean no_newline = false; - gboolean host_arg = false; - gboolean port_arg = false; - gboolean key_arg = false; - gboolean shs_cap_key_str_arg = false; - gboolean ipv4_arg = false; - gboolean ipv6_arg = false; - gboolean passthrough = false; - enum ip_family ip_family; - - get_app_dir(app_dir, sizeof(app_dir)); - - char config_buf[8192]; - len = read_file(config_buf, sizeof(config_buf), "%s/config", app_dir); - if (len > 0) { - ssize_t key_len = json_get_value(config_buf, "key", &key); - ssize_t host_len = json_get_value(config_buf, "host", &host); - ssize_t port_len = json_get_value(config_buf, "port", &port); - ssize_t shs_cap_len = json_get_value(config_buf, "caps.shs", &shs_cap_key_str); - if (key_len >= 0) ((char *)key)[key_len] = '\0'; - if (host_len >= 0) ((char *)host)[host_len] = '\0'; - if (port_len >= 0) ((char *)port)[port_len] = '\0'; - if (shs_cap_len >= 0) ((char *)shs_cap_key_str)[shs_cap_len] = '\0'; - } else if (len < 0 && errno != ENOENT) { - err(1, "failed to read config"); - } - - for (i = 1; i < argc && (argv[i][0] == '-'); i++) { - switch (argv[i][1]) { - case 'c': shs_cap_key_str = argv[++i]; shs_cap_key_str_arg = true; break; - case 'j': ptype = pkt_type_json; break; - case 'T': test = true; break; - case 's': host = argv[++i]; host_arg = true; break; - case 'k': key = argv[++i]; key_arg = true; break; - case 'K': keypair_seed_str = argv[++i]; break; - case 'p': port = argv[++i]; port_arg = true; break; - case 'u': socket_path = argv[++i]; break; - case 't': typestr = argv[++i]; break; - case 'n': noauth = true; break; - case '4': ipv4_arg = true; break; - case '6': ipv6_arg = true; break; - case 'a': passthrough = true; break; - case 'l': no_newline = true; break; - default: usage(); - } - } - if (i < argc) methodstr = argv[i++]; - else if (!test && !passthrough) usage(); - - if (ipv4_arg && ipv6_arg) errx(1, "options -4 and -6 conflict"); - ip_family = - ipv4_arg ? ip_family_ipv4 : - ipv6_arg ? ip_family_ipv6 : - ip_family_any; - - if (shs_cap_key_str) { - rc = pubkey_decode(shs_cap_key_str, shs_cap_key); - if (rc < 0) err(1, "unable to decode cap key '%s'", shs_cap_key_str); - } else { - memcpy(shs_cap_key, ssb_cap, 32); - } - - argument_len = test ? 0 : args_to_json_length(argc-i, argv+i); - char argument[argument_len]; - - if (passthrough) { - if (methodstr) errx(1, "-a option conflicts with method"); - if (typestr) errx(1, "-a option conflicts with -t option"); - if (argc-i > 0) errx(1, "-a option conflicts with method arguments"); - if (test) errx(1, "-a option conflicts with -T test"); - - } else if (!test) { - rc = args_to_json(argument, sizeof(argument), argc-i, argv+i); - if (rc < 0) errx(1, "unable to collect arguments"); - - char manifest_buf[8192]; - if (!typestr) { - len = read_file(manifest_buf, sizeof(manifest_buf), - "%s/manifest.json", app_dir); - if (len < 0) err(1, "failed to read manifest file"); - - ssize_t type_len = json_get_value(manifest_buf, methodstr, &typestr); - if (!typestr && errno == ENOMSG) errx(1, - "unable to find method '%s' in manifest", methodstr); - if (!typestr) err(1, "unable to read manifest %s/%s", manifest_buf, methodstr); - ((char *)typestr)[type_len] = '\0'; - } - if (strcmp(typestr, "sync") == 0) type = muxrpc_type_async; - else if (strcmp(typestr, "async") == 0) type = muxrpc_type_async; - else if (strcmp(typestr, "sink") == 0) type = muxrpc_type_sink; - else if (strcmp(typestr, "source") == 0) type = muxrpc_type_source; - else if (strcmp(typestr, "duplex") == 0) type = muxrpc_type_duplex; - else errx(1, "type must be one of "); - - rc = method_to_json(method, sizeof(method), methodstr); - if (rc < 0) errx(0, "unable to convert method name"); - } - - if (keypair_seed_str) { - unsigned char seed[crypto_sign_SEEDBYTES]; - unsigned char ed25519_skpk[crypto_sign_ed25519_SECRETKEYBYTES]; - - rc = pubkey_decode(keypair_seed_str, ed25519_skpk); - if (rc < 0) err(1, "unable to decode private key"); - rc = crypto_sign_ed25519_sk_to_seed(seed, ed25519_skpk); - if (rc < 0) err(1, "unable to convert private key to seed"); - rc = crypto_sign_seed_keypair(public_key, private_key, seed); - if (rc < 0) err(1, "unable to generate keypair from seed"); - } else { - read_private_key(app_dir, private_key); - memcpy(public_key, private_key+32, 32); - } - - if (key) { - rc = pubkey_decode(key, remote_key); - if (rc < 0) err(1, "unable to decode remote key '%s'", key); - } else { - memcpy(remote_key, public_key, 32); - } - - gboolean implied_tcp = host_arg || port_arg || ipv4_arg || ipv6_arg; - gboolean implied_auth = key_arg || keypair_seed_str || shs_cap_key_str_arg || test; - - if (test) { - infd = STDIN_FILENO; - outfd = STDOUT_FILENO; - s = -1; - - } else if (socket_path) { - if (implied_tcp) errx(1, "-u option conflicts with -s and -p options"); - if (!unix_connect(scuttler, socket_path, &err)) { - g_error("Error during connect: %s", err->message); - - return 1; - } - } else if (!implied_tcp && !implied_auth) { - gchar *socket_path_buf; - - if (!get_socket_path(socket_path_buf, app_dir)) { - if (noauth) { - g_error("Error getting socket path"); - - return 1; - } - - goto do_tcp_connect; - } - - if (!unix_connect(scuttler, socket_path_buf, &err)) { - if (noauth) { - g_error("Error during connect: %s", err->message); - - return 1; - } - - goto do_tcp_connect; - } - - noauth = true; - } else { -do_tcp_connect: - if (!tcp_connect(host, port, &err)) { - g_error("Could not connect: %s", err->message); - - return 1; - } - } - - struct boxs bs; - if (noauth) { - bs.s = s; - bs.noauth = true; - if (implied_auth) errx(1, "-n option conflicts with -k, -K, -c and -T options."); - } else { - shs_connect(s, infd, outfd, public_key, private_key, shs_cap_key, remote_key, &bs); - } - - if (test) { - rc = write_all(outfd, bs.encrypt_key, sizeof(bs.encrypt_key)); - rc |= write_all(outfd, bs.nonce1, sizeof(bs.nonce1)); - rc |= write_all(outfd, bs.decrypt_key, sizeof(bs.decrypt_key)); - rc |= write_all(outfd, bs.rx_nonce, sizeof(bs.rx_nonce)); - if (rc < 0) err(1, "failed to write handshake result"); - return 0; - } - - if (passthrough) { - rc = bs_passthrough(&bs, STDIN_FILENO, STDOUT_FILENO); - close(s); - return rc; - } - - muxrpc_call(&bs, method, argument, type, typestr, 1); - - switch (type) { - case muxrpc_type_async: - rc = muxrpc_read_async(&bs, STDOUT_FILENO, 1, no_newline); - break; - case muxrpc_type_source: - rc = muxrpc_read_source(&bs, STDOUT_FILENO, 1, no_newline); - break; - case muxrpc_type_sink: - if (!strcmp(methodstr, "blobs.add")) { - rc = muxrpc_write_blob_add(&bs, STDIN_FILENO, STDOUT_FILENO, 1, no_newline); - } else { - rc = muxrpc_write_sink(&bs, STDIN_FILENO, ptype, 1, no_newline); - } - break; - case muxrpc_type_duplex: - rc = muxrpc_duplex(&bs, STDIN_FILENO, STDOUT_FILENO, ptype, 1, no_newline); - break; - } - - ps_goodbye(&bs); - bs_end(&bs); - close(s); - return rc; -} +static void +ssb_scuttler_class_init(SsbScuttlerClass *klass) +{} diff --git a/ssb-gtk/ssb-scuttler.h b/ssb-gtk/ssb-scuttler.h index 0aa7532..b4e0e86 100644 --- a/ssb-gtk/ssb-scuttler.h +++ b/ssb-gtk/ssb-scuttler.h @@ -1,5 +1,5 @@ -#ifndef __SBOT_H__ -# define __SBOT_H__ +#ifndef __SSB_SCUTTLER_H__ +# define __SSB_SCUTTLER_H__ # include # include @@ -16,10 +16,10 @@ typedef enum { SSB_SCUTTLER_ERROR_SEND, SSB_SCUTTLER_ERROR_NOTCONNECTED, SSB_SCUTTLER_ERROR_READ, - SSB_SCUTTLER_ERROR_CRYPT, - SSB_SCUTTLER_ERROR_DECRYPT, + SSB_SCUTTLER_ERROR_CRYPTO, SSB_SCUTTLER_ERROR_RESPONSE_TOO_LARGE, SSB_SCUTTLER_ERROR_STREAM_END, + SSB_SCUTTLER_ERROR_CONFIG_ERROR, } SsbScuttlerError; G_BEGIN_DECLS @@ -45,4 +45,4 @@ void ssb_scuttler_whoami_async(SsbScuttler *scuttler, G_END_DECLS -#endif /* __SBOT_H__ */ +#endif /* __SSB_SCUTTLER_H__ */