diff options
author | Michael Vetter <jubalh@iodoru.org> | 2021-10-13 21:19:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-13 21:19:22 +0200 |
commit | e0e45b7b24aad0932c2778ba5f93a87778cb9c24 (patch) | |
tree | db71f24a3498f76b8fd2fab37e2d07636cdffd23 /src/xmpp | |
parent | f21595597f4fa872b77d7f76ae04916f4aae732e (diff) | |
parent | 0653200965122c46be4f31996d2396b568ee8dcf (diff) | |
download | profani-tty-e0e45b7b24aad0932c2778ba5f93a87778cb9c24.tar.gz |
Merge pull request #1574 from binex-dsk/master
Add in-band account registration Fix https://github.com/profanity-im/profanity/issues/199
Diffstat (limited to 'src/xmpp')
-rw-r--r-- | src/xmpp/connection.c | 357 | ||||
-rw-r--r-- | src/xmpp/connection.h | 3 | ||||
-rw-r--r-- | src/xmpp/iq.c | 1 | ||||
-rw-r--r-- | src/xmpp/session.c | 3 | ||||
-rw-r--r-- | src/xmpp/stanza.c | 38 | ||||
-rw-r--r-- | src/xmpp/stanza.h | 2 | ||||
-rw-r--r-- | src/xmpp/xmpp.h | 6 |
7 files changed, 408 insertions, 2 deletions
diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 30ef0a9e..4fecad3b 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -58,6 +58,7 @@ #include "event/server_events.h" #include "xmpp/connection.h" #include "xmpp/session.h" +#include "xmpp/stanza.h" #include "xmpp/iq.h" #include "ui/ui.h" @@ -77,6 +78,11 @@ typedef struct prof_conn_t GHashTable* requested_features; } ProfConnection; +typedef struct { + const char* username; + const char* password; +} prof_reg_t; + static ProfConnection conn; static gchar* profanity_instance_id = NULL; static gchar* prof_identifier = NULL; @@ -245,6 +251,338 @@ connection_connect(const char* const jid, const char* const passwd, const char* return conn.conn_status; } +static int iq_reg2_cb(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userdata) +{ + const char *type; + + (void)userdata; + + type = xmpp_stanza_get_type(stanza); + if (!type || strcmp(type, "error") == 0) { + char* error_message = stanza_get_error_message(stanza); + cons_show_error("Server error: %s", error_message); + log_debug("Registration error: %s", error_message); + goto quit; + } + + if (strcmp(type, "result") != 0) { + log_debug("Expected type 'result', but got %s.", type); + goto quit; + } + + cons_show("Registration successful."); + log_info("Registration successful."); + goto quit; + +quit: + xmpp_disconnect(xmpp_conn); + + return 0; +} + +static int iq_reg_cb(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userdata) +{ + prof_reg_t *reg = (prof_reg_t *)userdata; + xmpp_stanza_t *registered = NULL; + xmpp_stanza_t *query; + const char *type; + + type = xmpp_stanza_get_type(stanza); + if (!type || strcmp(type, "error") == 0) { + char* error_message = stanza_get_error_message(stanza); + cons_show_error("Server error: %s", error_message); + log_debug("Registration error: %s", error_message); + xmpp_disconnect(xmpp_conn); + goto quit; + } + + if (strcmp(type, "result") != 0) { + log_debug("Expected type 'result', but got %s.", type); + xmpp_disconnect(xmpp_conn); + goto quit; + } + + query = xmpp_stanza_get_child_by_name(stanza, "query"); + if (query) + registered = xmpp_stanza_get_child_by_name(query, "registered"); + if (registered != NULL) { + cons_show_error("Already registered."); + log_debug("Already registered."); + xmpp_disconnect(xmpp_conn); + goto quit; + } + xmpp_stanza_t* iq = stanza_register_new_account(conn.xmpp_ctx, reg->username, reg->password); + xmpp_id_handler_add(xmpp_conn, iq_reg2_cb, xmpp_stanza_get_id(iq), reg); + xmpp_send(xmpp_conn, iq); + +quit: + return 0; +} + +static int +_register_handle_error(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userdata) +{ + (void)stanza; + (void)userdata; + + char* error_message = stanza_get_error_message(stanza); + cons_show_error("Server error: %s", error_message); + log_debug("Registration error: %s", error_message); + xmpp_disconnect(xmpp_conn); + + return 0; +} + +static int _register_handle_proceedtls_default(xmpp_conn_t *xmpp_conn, + xmpp_stanza_t *stanza, + void *userdata) +{ + const char *name = xmpp_stanza_get_name(stanza); + + (void)userdata; + + if (strcmp(name, "proceed") == 0) { + log_debug("Proceeding with TLS."); + if (xmpp_conn_tls_start(xmpp_conn) == 0) { + xmpp_handler_delete(xmpp_conn, _register_handle_error); + xmpp_conn_open_stream_default(xmpp_conn); + } else { + log_debug("TLS failed."); + /* failed tls spoils the connection, so disconnect */ + xmpp_disconnect(xmpp_conn); + } + } + return 0; +} + +static int _register_handle_missing_features(xmpp_conn_t *xmpp_conn, void *userdata) +{ + (void)userdata; + + log_debug("Timeout"); + xmpp_disconnect(xmpp_conn); + + return 0; +} + +static int +_register_handle_features(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userdata) +{ + prof_reg_t *reg = (prof_reg_t *)userdata; + xmpp_ctx_t *ctx = conn.xmpp_ctx; + xmpp_stanza_t *child; + xmpp_stanza_t *iq; + char *domain; + + xmpp_timed_handler_delete(xmpp_conn, _register_handle_missing_features); + + /* secure connection if possible */ + child = xmpp_stanza_get_child_by_name(stanza, "starttls"); + if (child && (strcmp(xmpp_stanza_get_ns(child), XMPP_NS_TLS) == 0)) { + log_debug("Server supports TLS. Attempting to establish..."); + child = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(child, "starttls"); + xmpp_stanza_set_ns(child, XMPP_NS_TLS); + xmpp_handler_add(xmpp_conn, _register_handle_proceedtls_default, XMPP_NS_TLS, NULL, + NULL, NULL); + xmpp_send(xmpp_conn, child); + xmpp_stanza_release(child); + return 0; + } + + /* check whether server supports in-band registration */ + child = xmpp_stanza_get_child_by_name(stanza, "register"); + if (!child) { + log_debug("Server does not support in-band registration."); + cons_show_error("Server does not support in-band registration, aborting."); + xmpp_disconnect(xmpp_conn); + return 0; + } + + log_debug("Server supports in-band registration. Attempting registration."); + + domain = strdup(conn.domain); + iq = xmpp_iq_new(ctx, "get", "reg1"); + xmpp_stanza_set_to(iq, domain); + child = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(child, "query"); + xmpp_stanza_set_ns(child, STANZA_NS_REGISTER); + xmpp_stanza_add_child(iq, child); + + xmpp_handler_add(xmpp_conn, iq_reg_cb, STANZA_NS_REGISTER, "iq", NULL, reg); + xmpp_send(xmpp_conn, iq); + + xmpp_free(ctx, domain); + xmpp_stanza_release(child); + xmpp_stanza_release(iq); + + return 0; +} + +static void +_register_handler(xmpp_conn_t *xmpp_conn, + xmpp_conn_event_t status, + int error, + xmpp_stream_error_t *stream_error, + void *userdata) +{ + conn.conn_last_event = status; + + prof_reg_t *reg = (prof_reg_t *)userdata; + int secured; + + (void)error; + (void)stream_error; + + switch (status) { + + case XMPP_CONN_RAW_CONNECT: + log_debug("Raw connection established."); + xmpp_conn_open_stream_default(xmpp_conn); + conn.conn_status = JABBER_RAW_CONNECTED; + break; + + case XMPP_CONN_CONNECT: + log_debug("Connected."); + secured = xmpp_conn_is_secured(xmpp_conn); + conn.conn_status = JABBER_CONNECTED; + log_debug("Connection is %s.\n", + secured ? "secured" : "NOT secured"); + + Jid* my_jid = jid_create(xmpp_conn_get_jid(xmpp_conn)); + conn.domain = strdup(my_jid->domainpart); + jid_destroy(my_jid); + + xmpp_handler_add(xmpp_conn, _register_handle_error, XMPP_NS_STREAMS, "error", NULL, + NULL); + xmpp_handler_add(xmpp_conn, _register_handle_features, XMPP_NS_STREAMS, "features", + NULL, reg); + xmpp_timed_handler_add(xmpp_conn, _register_handle_missing_features, 5000, + NULL); + break; + + case XMPP_CONN_DISCONNECT: + log_debug("Disconnected"); + conn.conn_status = JABBER_DISCONNECTED; + break; + + default: + break; + + } +} + +jabber_conn_status_t +connection_register(const char* const altdomain, int port, const char* const tls_policy, + const char* const username, const char* const password) +{ + long flags; + + Jid* jidp = jid_create(altdomain); + if (jidp == NULL) { + log_error("Malformed JID not able to connect: %s", altdomain); + conn.conn_status = JABBER_DISCONNECTED; + return conn.conn_status; + } + + _compute_identifier(jidp->barejid); + jid_destroy(jidp); + + if (conn.xmpp_log) { + free(conn.xmpp_log); + } + conn.xmpp_log = _xmpp_get_file_logger(); + + if (conn.xmpp_conn) { + xmpp_conn_release(conn.xmpp_conn); + } + if (conn.xmpp_ctx) { + xmpp_ctx_free(conn.xmpp_ctx); + } + conn.xmpp_ctx = xmpp_ctx_new(NULL, conn.xmpp_log); + if (conn.xmpp_ctx == NULL) { + log_warning("Failed to get libstrophe ctx during connect"); + return JABBER_DISCONNECTED; + } + conn.xmpp_conn = xmpp_conn_new(conn.xmpp_ctx); + if (conn.xmpp_conn == NULL) { + log_warning("Failed to get libstrophe conn during connect"); + return JABBER_DISCONNECTED; + } + xmpp_conn_set_jid(conn.xmpp_conn, altdomain); + + flags = xmpp_conn_get_flags(conn.xmpp_conn); + + if (!tls_policy || (g_strcmp0(tls_policy, "force") == 0)) { + flags |= XMPP_CONN_FLAG_MANDATORY_TLS; + } else if (g_strcmp0(tls_policy, "trust") == 0) { + flags |= XMPP_CONN_FLAG_MANDATORY_TLS; + flags |= XMPP_CONN_FLAG_TRUST_TLS; + } else if (g_strcmp0(tls_policy, "disable") == 0) { + flags |= XMPP_CONN_FLAG_DISABLE_TLS; + } else if (g_strcmp0(tls_policy, "legacy") == 0) { + flags |= XMPP_CONN_FLAG_LEGACY_SSL; + } + + xmpp_conn_set_flags(conn.xmpp_conn, flags); + + /* Print debug logs that can help when users share the logs */ + if (flags != 0) { + log_debug("Connecting with flags (0x%lx):", flags); +#define LOG_FLAG_IF_SET(name) \ + if (flags & name) { \ + log_debug(" " #name); \ + } + LOG_FLAG_IF_SET(XMPP_CONN_FLAG_MANDATORY_TLS); + LOG_FLAG_IF_SET(XMPP_CONN_FLAG_TRUST_TLS); + LOG_FLAG_IF_SET(XMPP_CONN_FLAG_DISABLE_TLS); + LOG_FLAG_IF_SET(XMPP_CONN_FLAG_LEGACY_SSL); +#undef LOG_FLAG_IF_SET + } + + prof_reg_t *reg; + + reg = calloc(1, sizeof(*reg)); + if (reg == NULL) { + log_warning("Failed to allocate registration data struct during connect"); + return JABBER_DISCONNECTED; + } + + reg->username = strdup(username); + reg->password = strdup(password); + +#ifdef HAVE_LIBMESODE + char* cert_path = prefs_get_tls_certpath(); + if (cert_path) { + xmpp_conn_tlscert_path(conn.xmpp_conn, cert_path); + free(cert_path); + } + + int connect_status = xmpp_connect_raw( + conn.xmpp_conn, + altdomain, + port, + _connection_certfail_cb, + _register_handler, + reg); +#else + int connect_status = xmpp_connect_raw( + conn.xmpp_conn, + altdomain, + port, + _register_handler, + reg); +#endif + + if (connect_status == 0) { + conn.conn_status = JABBER_RAW_CONNECTING; + } else { + conn.conn_status = JABBER_DISCONNECTED; + } + + return conn.conn_status; +} + void connection_disconnect(void) { @@ -515,7 +853,6 @@ char* connection_create_stanza_id(void) { char* rndid = get_random_string(CON_RAND_ID_LEN); - assert(rndid != NULL); gchar* hmac = g_compute_hmac_for_string(G_CHECKSUM_SHA1, @@ -581,6 +918,23 @@ _connection_handler(xmpp_conn_t* const xmpp_conn, const xmpp_conn_event_t status break; + // raw connection success + case XMPP_CONN_RAW_CONNECT: + log_debug("Connection handler: XMPP_CONN_RAW_CONNECT"); + conn.conn_status = JABBER_RAW_CONNECTED; + + Jid* my_raw_jid = jid_create(xmpp_conn_get_jid(conn.xmpp_conn)); + log_debug("jid: %s", xmpp_conn_get_jid(conn.xmpp_conn)); + conn.domain = strdup(my_raw_jid->domainpart); + jid_destroy(my_raw_jid); + + conn.features_by_jid = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_hash_table_destroy); + g_hash_table_insert(conn.features_by_jid, strdup(conn.domain), g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL)); + + xmpp_conn_open_stream_default(xmpp_conn); + + break; + // disconnected case XMPP_CONN_DISCONNECT: log_debug("Connection handler: XMPP_CONN_DISCONNECT"); @@ -755,3 +1109,4 @@ connection_get_profanity_identifier(void) { return prof_identifier; } + diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h index cab579f7..0f0c2c10 100644 --- a/src/xmpp/connection.h +++ b/src/xmpp/connection.h @@ -46,6 +46,8 @@ void connection_check_events(void); jabber_conn_status_t connection_connect(const char* const fulljid, const char* const passwd, const char* const altdomain, int port, const char* const tls_policy, const char* const auth_policy); +jabber_conn_status_t connection_register(const char* const altdomain, int port, const char* const tls_policy, + const char* const username, const char* const password); void connection_disconnect(void); void connection_set_disconnected(void); @@ -68,3 +70,4 @@ void connection_remove_available_resource(const char* const resource); char* connection_create_stanza_id(void); #endif + diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index dd3c2423..8f7c8290 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -2804,3 +2804,4 @@ iq_muc_register_nick(const char* const roomjid) xmpp_stanza_release(iq); xmpp_stanza_release(query); } + diff --git a/src/xmpp/session.c b/src/xmpp/session.c index e81d55ae..de255116 100644 --- a/src/xmpp/session.c +++ b/src/xmpp/session.c @@ -261,6 +261,8 @@ session_process_events(void) switch (conn_status) { case JABBER_CONNECTED: case JABBER_CONNECTING: + case JABBER_RAW_CONNECTED: + case JABBER_RAW_CONNECTING: case JABBER_DISCONNECTING: connection_check_events(); break; @@ -543,3 +545,4 @@ _session_free_saved_details(void) FREE_SET_NULL(saved_details.tls_policy); FREE_SET_NULL(saved_details.auth_policy); } + diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 81a41bed..bb9083f0 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2750,6 +2750,43 @@ stanza_change_password(xmpp_ctx_t* ctx, const char* const user, const char* cons } xmpp_stanza_t* +stanza_register_new_account(xmpp_ctx_t* ctx, const char* const user, const char* const password) +{ + char* id = connection_create_stanza_id(); + xmpp_stanza_t* iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, id); + free(id); + + xmpp_stanza_t* register_new_account = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(register_new_account, STANZA_NAME_QUERY); + xmpp_stanza_set_ns(register_new_account, STANZA_NS_REGISTER); + + xmpp_stanza_t* username_st = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(username_st, STANZA_NAME_USERNAME); + xmpp_stanza_t* username_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(username_text, user); + xmpp_stanza_add_child(username_st, username_text); + xmpp_stanza_release(username_text); + + xmpp_stanza_t* password_st = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(password_st, STANZA_NAME_PASSWORD); + xmpp_stanza_t* password_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(password_text, password); + xmpp_stanza_add_child(password_st, password_text); + xmpp_stanza_release(password_text); + + xmpp_stanza_add_child(register_new_account, username_st); + xmpp_stanza_release(username_st); + + xmpp_stanza_add_child(register_new_account, password_st); + xmpp_stanza_release(password_st); + + xmpp_stanza_add_child(iq, register_new_account); + xmpp_stanza_release(register_new_account); + + return iq; +} + +xmpp_stanza_t* stanza_request_voice(xmpp_ctx_t* ctx, const char* const room) { char* id = connection_create_stanza_id(); @@ -2887,3 +2924,4 @@ stanza_get_service_contact_addresses(xmpp_ctx_t* ctx, xmpp_stanza_t* stanza) return addresses; } + diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index c58395bb..f19b4f4c 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -409,8 +409,10 @@ void stanza_free_caps(XMPPCaps* caps); xmpp_stanza_t* stanza_create_avatar_retrieve_data_request(xmpp_ctx_t* ctx, const char* stanza_id, const char* const item_id, const char* const jid); xmpp_stanza_t* stanza_create_mam_iq(xmpp_ctx_t* ctx, const char* const jid, const char* const startdate, const char* const lastid); xmpp_stanza_t* stanza_change_password(xmpp_ctx_t* ctx, const char* const user, const char* const password); +xmpp_stanza_t* stanza_register_new_account(xmpp_ctx_t* ctx, const char* const user, const char* const password); xmpp_stanza_t* stanza_request_voice(xmpp_ctx_t* ctx, const char* const room); xmpp_stanza_t* stanza_create_approve_voice(xmpp_ctx_t* ctx, const char* const id, const char* const jid, const char* const node, DataForm* form); xmpp_stanza_t* stanza_create_muc_register_nick(xmpp_ctx_t* ctx, const char* const id, const char* const jid, const char* const node, DataForm* form); #endif + diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 4c49eb8b..f4a7a6e5 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -76,7 +76,9 @@ typedef enum { JABBER_CONNECTING, JABBER_CONNECTED, JABBER_DISCONNECTING, - JABBER_DISCONNECTED + JABBER_DISCONNECTED, + JABBER_RAW_CONNECTING, + JABBER_RAW_CONNECTED } jabber_conn_status_t; typedef enum { @@ -183,6 +185,7 @@ void session_init(void); jabber_conn_status_t session_connect_with_details(const char* const jid, const char* const passwd, const char* const altdomain, const int port, const char* const tls_policy, const char* const auth_policy); jabber_conn_status_t session_connect_with_account(const ProfAccount* const account); + void session_disconnect(void); void session_shutdown(void); void session_process_events(void); @@ -313,3 +316,4 @@ Autocomplete form_get_value_ac(DataForm* form, const char* const tag); void form_reset_autocompleters(DataForm* form); #endif + |