From d9366a26d6ef1a82eeccd7d7523db25d91e4efc1 Mon Sep 17 00:00:00 2001 From: swirl Date: Thu, 1 Jul 2021 19:30:30 -0400 Subject: initial register command test --- src/command/cmd_defs.c | 27 ++++++++++++ src/command/cmd_funcs.c | 65 +++++++++++++++++++++++++++++ src/command/cmd_funcs.h | 2 + src/xmpp/connection.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++ src/xmpp/connection.h | 3 ++ src/xmpp/iq.c | 32 +++++++++++++++ src/xmpp/stanza.c | 35 ++++++++++++++++ src/xmpp/stanza.h | 2 + src/xmpp/xmpp.h | 2 + 9 files changed, 274 insertions(+) diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index 423de94e..8a08e236 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -2642,6 +2642,32 @@ static struct cmd_t command_defs[] = { CMD_NOEXAMPLES }, + { "/register", + parse_args, 2, 6, NULL, + CMD_NOSUBFUNCS + CMD_MAINFUNC(cmd_register) + CMD_TAGS( + CMD_TAG_CONNECTION) + CMD_SYN( + "/register [port ] [tls force|allow|trust|legacy|disable]") + CMD_DESC( + "Register an account on a server.") + CMD_ARGS( + { "", "Server to register account on." }, + { "", "Username to register with." }, + { "port ", "The port to use if different to the default (5222, or 5223 for SSL)." }, + { "tls force", "Force TLS connection, and fail if one cannot be established. This is the default behavior." }, + { "tls allow", "Use TLS for the connection if it is available." }, + { "tls trust", "Force TLS connection and trust server's certificate." }, + { "tls legacy", "Use legacy TLS for the connection. This forces TLS just after the TCP connection is established. Use when a server doesn't support STARTTLS." }, + { "tls disable", "Disable TLS for the connection." }) + CMD_EXAMPLES( + "/register valhalla.edda odin", + "/register vanaheimr.edda freyr port 5678", + "/register 127.0.0.1 me tls disable", + "/register my.xmppserv.er someuser port 5443 tls force") + }, + // NEXT-COMMAND (search helper) }; @@ -3012,3 +3038,4 @@ command_mangen(void) g_free(header); g_list_free(cmds); } + diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 96b541b8..a00c6a46 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9541,3 +9541,68 @@ cmd_silence(ProfWin* window, const char* const command, gchar** args) return TRUE; } + +gboolean +cmd_register(ProfWin* window, const char* const command, gchar** args) +{ + gchar* opt_keys[] = { "port", "tls", NULL }; + gboolean parsed; + + GHashTable* options = parse_options(&args[2], opt_keys, &parsed); + if (!parsed) { + cons_bad_cmd_usage(command); + cons_show(""); + options_destroy(options); + return TRUE; + } + + char* tls_policy = g_hash_table_lookup(options, "tls"); + if (tls_policy && (g_strcmp0(tls_policy, "force") != 0) && (g_strcmp0(tls_policy, "allow") != 0) && (g_strcmp0(tls_policy, "trust") != 0) && (g_strcmp0(tls_policy, "disable") != 0) && (g_strcmp0(tls_policy, "legacy") != 0)) { + cons_bad_cmd_usage(command); + cons_show(""); + options_destroy(options); + return TRUE; + } + + int port = 0; + if (g_hash_table_contains(options, "port")) { + char* port_str = g_hash_table_lookup(options, "port"); + char* err_msg = NULL; + gboolean res = strtoi_range(port_str, &port, 1, 65535, &err_msg); + if (!res) { + cons_show(err_msg); + cons_show(""); + free(err_msg); + port = 0; + options_destroy(options); + return TRUE; + } + } + + char* host = args[0]; + + jabber_conn_status_t conn_status = connection_connect_raw(host, port, tls_policy, "default"); + + if (conn_status == JABBER_DISCONNECTED) { + cons_show_error("Connection attempt to server %s port %d failed.", host, port); + log_info("Connection attempt to server %s port %d failed.", host, port); + return TRUE; + } + + char* username = args[1]; + char* passwd = ui_ask_password(false); + char* confirm_passwd = ui_ask_password(true); + + if (g_strcmp0(passwd, confirm_passwd) == 0) { + iq_register_new_account(username, passwd); + } else { + cons_show("The two passwords do not match."); + } + + free(username); + free(passwd); + free(confirm_passwd); + + return TRUE; +} + diff --git a/src/command/cmd_funcs.h b/src/command/cmd_funcs.h index 54cc6e78..dee71371 100644 --- a/src/command/cmd_funcs.h +++ b/src/command/cmd_funcs.h @@ -247,5 +247,7 @@ gboolean cmd_executable_editor(ProfWin* window, const char* const command, gchar gboolean cmd_mam(ProfWin* window, const char* const command, gchar** args); gboolean cmd_editor(ProfWin* window, const char* const command, gchar** args); gboolean cmd_silence(ProfWin* window, const char* const command, gchar** args); +gboolean cmd_register(ProfWin* window, const char* const command, gchar** args); #endif + diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index e25b8f6f..43833205 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -245,6 +245,111 @@ connection_connect(const char* const jid, const char* const passwd, const char* return conn.conn_status; } +jabber_conn_status_t +connection_connect_raw(const char* const altdomain, int port, + const char* const tls_policy, const char* const auth_policy) +{ + 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; + } + + if (auth_policy && (g_strcmp0(auth_policy, "legacy") == 0)) { + flags |= XMPP_CONN_FLAG_LEGACY_AUTH; + } + + 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); + LOG_FLAG_IF_SET(XMPP_CONN_FLAG_LEGACY_AUTH); +#undef LOG_FLAG_IF_SET + } + +#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, + _connection_handler, + conn.xmpp_ctx); +#else + int connect_status = xmpp_connect_raw( + conn.xmpp_conn, + altdomain, + port, + _connection_handler, + conn.xmpp_ctx); +#endif + + if (connect_status == 0) { + conn.conn_status = JABBER_CONNECTING; + } else { + conn.conn_status = JABBER_DISCONNECTED; + } + + return conn.conn_status; +} + void connection_disconnect(void) { @@ -755,3 +860,4 @@ connection_get_profanity_identifier(void) { return prof_identifier; } + diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h index cab579f7..cf24fc56 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_connect_raw(const char* const altdomain, int port, + const char* const tls_policy, const char* const auth_policy); 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 42571b9c..a35144ff 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -150,6 +150,7 @@ static int _command_list_result_handler(xmpp_stanza_t* const stanza, void* const static int _command_exec_response_handler(xmpp_stanza_t* const stanza, void* const userdata); static int _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata); static int _register_change_password_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata); +static int _register_new_account_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata); static void _iq_free_room_data(ProfRoomInfoData* roominfo); static void _iq_free_affiliation_set(ProfPrivilegeSet* affiliation_set); @@ -2654,6 +2655,36 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata) return 0; } +void +iq_register_new_account(const char* const user, const char* const password) +{ + //char* id = connection_create_stanza_id(); + xmpp_ctx_t* const ctx = connection_get_ctx(); + xmpp_stanza_t* iq = stanza_register_new_account(ctx, user, password); + + const char* id = xmpp_stanza_get_id(iq); + iq_id_handler_add(id, _register_new_account_result_id_handler, NULL, NULL); + + iq_send_stanza(iq); + xmpp_stanza_release(iq); +} + +static int +_register_new_account_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata) +{ + const char* type = xmpp_stanza_get_type(stanza); + if (g_strcmp0(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); + free(error_message); + } else { + cons_show("Registration successful."); + log_debug("Registration successful."); + } + return 0; +} + void iq_register_change_password(const char* const user, const char* const password) { @@ -2807,3 +2838,4 @@ iq_muc_register_nick(const char* const roomjid) xmpp_stanza_release(iq); xmpp_stanza_release(query); } + diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 604d4003..b1958799 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2751,6 +2751,40 @@ stanza_change_password(xmpp_ctx_t* ctx, const char* const user, const char* cons return iq; } +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); + + return iq; +} + xmpp_stanza_t* stanza_request_voice(xmpp_ctx_t* ctx, const char* const room) { @@ -2889,3 +2923,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 52008b1c..366305ff 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -408,8 +408,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 61c7a642..686a0dc8 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -266,6 +266,7 @@ void iq_command_list(const char* const target); void iq_command_exec(const char* const target, const char* const command); void iq_mam_request(ProfChatWin* win); void iq_register_change_password(const char* const user, const char* const password); +void iq_register_new_account(const char* const user, const char* const password); void iq_muc_register_nick(const char* const roomjid); EntityCapabilities* caps_lookup(const char* const jid); @@ -313,3 +314,4 @@ Autocomplete form_get_value_ac(DataForm* form, const char* const tag); void form_reset_autocompleters(DataForm* form); #endif + -- cgit 1.4.1-2-gfad0 From 5ea1ccbb468bd6c1195cb85fe29ac664e74436e7 Mon Sep 17 00:00:00 2001 From: swirl Date: Thu, 1 Jul 2021 21:29:09 -0400 Subject: /register: parameter updates add auth param, switch host and username params, rename host param to server --- src/command/cmd_defs.c | 18 ++++++++++-------- src/command/cmd_funcs.c | 20 ++++++++++++++------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index 8a08e236..80d98827 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -2649,23 +2649,25 @@ static struct cmd_t command_defs[] = { CMD_TAGS( CMD_TAG_CONNECTION) CMD_SYN( - "/register [port ] [tls force|allow|trust|legacy|disable]") + "/register [port ] [tls force|allow|trust|legacy|disable] [auth default|legacy]") CMD_DESC( "Register an account on a server.") CMD_ARGS( - { "", "Server to register account on." }, { "", "Username to register with." }, + { "", "Server to register account on." }, { "port ", "The port to use if different to the default (5222, or 5223 for SSL)." }, { "tls force", "Force TLS connection, and fail if one cannot be established. This is the default behavior." }, { "tls allow", "Use TLS for the connection if it is available." }, - { "tls trust", "Force TLS connection and trust server's certificate." }, + { "tls trust", "Force TLS connection and trust the server's certificate." }, { "tls legacy", "Use legacy TLS for the connection. This forces TLS just after the TCP connection is established. Use when a server doesn't support STARTTLS." }, - { "tls disable", "Disable TLS for the connection." }) + { "tls disable", "Disable TLS for the connection." }, + { "auth default", "Default authentication process." }, + { "auth legacy", "Allow legacy authentication." }) CMD_EXAMPLES( - "/register valhalla.edda odin", - "/register vanaheimr.edda freyr port 5678", - "/register 127.0.0.1 me tls disable", - "/register my.xmppserv.er someuser port 5443 tls force") + "/register odin valhalla.edda ", + "/register freyr vanaheimr.edda port 5678", + "/register me 127.0.0.1 tls disable", + "/register someuser my.xmppserv.er port 5443 tls force") }, // NEXT-COMMAND (search helper) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index a00c6a46..68eec91b 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9545,7 +9545,7 @@ cmd_silence(ProfWin* window, const char* const command, gchar** args) gboolean cmd_register(ProfWin* window, const char* const command, gchar** args) { - gchar* opt_keys[] = { "port", "tls", NULL }; + gchar* opt_keys[] = { "port", "tls", "auth", NULL }; gboolean parsed; GHashTable* options = parse_options(&args[2], opt_keys, &parsed); @@ -9579,17 +9579,25 @@ cmd_register(ProfWin* window, const char* const command, gchar** args) } } - char* host = args[0]; + char* auth_policy = g_hash_table_lookup(options, "auth"); + if (auth_policy && (g_strcmp0(auth_policy, "default") != 0) && (g_strcmp0(auth_policy, "legacy") != 0)) { + cons_bad_cmd_usage(command); + cons_show(""); + options_destroy(options); + return TRUE; + } + + char* server = args[1]; - jabber_conn_status_t conn_status = connection_connect_raw(host, port, tls_policy, "default"); + jabber_conn_status_t conn_status = connection_connect_raw(server, port, tls_policy, auth_policy); if (conn_status == JABBER_DISCONNECTED) { - cons_show_error("Connection attempt to server %s port %d failed.", host, port); - log_info("Connection attempt to server %s port %d failed.", host, port); + cons_show_error("Connection attempt to server %s port %d failed.", server, port); + log_info("Connection attempt to server %s port %d failed.", server, port); return TRUE; } - char* username = args[1]; + char* username = args[0]; char* passwd = ui_ask_password(false); char* confirm_passwd = ui_ask_password(true); -- cgit 1.4.1-2-gfad0 From 2cc354b6aef2f54b3f4dacc81f257c0625d5c734 Mon Sep 17 00:00:00 2001 From: swirl Date: Sun, 4 Jul 2021 19:02:38 -0400 Subject: fixed some bugs, added some more - Added JABBER_RAW_CONNECT[ING/ED] connection states - Added cl_ev_connect_raw and session_connect_raw to conform to normal connection functions - Fixed SIGABRT during registration - Added a check in cmd_register to ensure it's actually connected before registering--but this will always fail atm --- src/command/cmd_funcs.c | 29 ++++++++++++++++++----------- src/event/client_events.c | 8 ++++++++ src/event/client_events.h | 2 ++ src/profanity.c | 1 + src/xmpp/connection.c | 17 ++++++++++++++++- src/xmpp/iq.c | 5 +++-- src/xmpp/session.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/xmpp/stanza.c | 3 +++ src/xmpp/xmpp.h | 6 +++++- 9 files changed, 96 insertions(+), 15 deletions(-) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 68eec91b..07acd266 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9589,7 +9589,7 @@ cmd_register(ProfWin* window, const char* const command, gchar** args) char* server = args[1]; - jabber_conn_status_t conn_status = connection_connect_raw(server, port, tls_policy, auth_policy); + jabber_conn_status_t conn_status = cl_ev_connect_raw(server, port, tls_policy, auth_policy); if (conn_status == JABBER_DISCONNECTED) { cons_show_error("Connection attempt to server %s port %d failed.", server, port); @@ -9598,19 +9598,26 @@ cmd_register(ProfWin* window, const char* const command, gchar** args) } char* username = args[0]; - char* passwd = ui_ask_password(false); - char* confirm_passwd = ui_ask_password(true); + if (connection_get_status() != JABBER_RAW_CONNECTED) { // FIXME: this is ALWAYS the case, as the connection doesn't finish by this time. + cons_show_error("Raw connection attempt failed or not yet completed."); + log_info("Raw connection attempt failed or not yet completed."); + } //else { + char* passwd = ui_ask_password(false); + char* confirm_passwd = ui_ask_password(true); - if (g_strcmp0(passwd, confirm_passwd) == 0) { - iq_register_new_account(username, passwd); - } else { - cons_show("The two passwords do not match."); - } + if (g_strcmp0(passwd, confirm_passwd) == 0) { + log_info("Attempting to register account %s on server %s.", username, server); + iq_register_new_account(username, passwd); + } else { + cons_show("The two passwords do not match."); + } + free(passwd); + free(confirm_passwd); + //} - free(username); - free(passwd); - free(confirm_passwd); + options_destroy(options); + log_info("we are leaving the registration process"); return TRUE; } diff --git a/src/event/client_events.c b/src/event/client_events.c index bf519feb..14b5b8c4 100644 --- a/src/event/client_events.c +++ b/src/event/client_events.c @@ -81,6 +81,13 @@ cl_ev_connect_account(ProfAccount* account) return session_connect_with_account(account); } +jabber_conn_status_t +cl_ev_connect_raw(const char* const altdomain, const int port, const char* const tls_policy, const char* const auth_policy) +{ + cons_show("Raw connecting to %s", altdomain); + return session_connect_raw(altdomain, port, tls_policy, auth_policy); +} + void cl_ev_disconnect(void) { @@ -262,3 +269,4 @@ cl_ev_send_priv_msg(ProfPrivateWin* privwin, const char* const msg, const char* jid_destroy(jidp); } } + diff --git a/src/event/client_events.h b/src/event/client_events.h index fed2bb37..8fbc7a05 100644 --- a/src/event/client_events.h +++ b/src/event/client_events.h @@ -40,6 +40,7 @@ jabber_conn_status_t cl_ev_connect_jid(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 cl_ev_connect_account(ProfAccount* account); +jabber_conn_status_t cl_ev_connect_raw(const char* const altdomain, const int port, const char* const tls_policy, const char* const auth_policy); void cl_ev_disconnect(void); @@ -52,3 +53,4 @@ void cl_ev_send_muc_msg(ProfMucWin* mucwin, const char* const msg, const char* c void cl_ev_send_priv_msg(ProfPrivateWin* privwin, const char* const msg, const char* const oob_url); #endif + diff --git a/src/profanity.c b/src/profanity.c index eb829bad..a74f26d2 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -268,3 +268,4 @@ _shutdown(void) ui_close(); prefs_close(); } + diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 43833205..49da1dc3 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -342,7 +342,7 @@ connection_connect_raw(const char* const altdomain, int port, #endif if (connect_status == 0) { - conn.conn_status = JABBER_CONNECTING; + conn.conn_status = JABBER_RAW_CONNECTING; } else { conn.conn_status = JABBER_DISCONNECTED; } @@ -686,6 +686,21 @@ _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)); + + break; + // disconnected case XMPP_CONN_DISCONNECT: log_debug("Connection handler: XMPP_CONN_DISCONNECT"); diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index a35144ff..2490fd9c 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -2658,14 +2658,15 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata) void iq_register_new_account(const char* const user, const char* const password) { - //char* id = connection_create_stanza_id(); xmpp_ctx_t* const ctx = connection_get_ctx(); xmpp_stanza_t* iq = stanza_register_new_account(ctx, user, password); const char* id = xmpp_stanza_get_id(iq); - iq_id_handler_add(id, _register_new_account_result_id_handler, NULL, NULL); + iq_id_handler_add(id, _register_new_account_result_id_handler, NULL, NULL); // FIXME: function doesn't seem to ever run? + log_debug("HI hi sending registration stanza"); iq_send_stanza(iq); + log_debug("registration stanza has been sent"); xmpp_stanza_release(iq); } diff --git a/src/xmpp/session.c b/src/xmpp/session.c index e81d55ae..ccd36865 100644 --- a/src/xmpp/session.c +++ b/src/xmpp/session.c @@ -204,6 +204,43 @@ session_connect_with_details(const char* const jid, const char* const passwd, co saved_details.auth_policy); } +jabber_conn_status_t +session_connect_raw(const char* const altdomain, const int port, const char* const tls_policy, + const char* const auth_policy) +{ + assert(altdomain != NULL); + + _session_free_saved_account(); + _session_free_saved_details(); + + // save details for reconnect + saved_details.altdomain = strdup(altdomain); + if (port != 0) { + saved_details.port = port; + } else { + saved_details.port = 0; + } + if (tls_policy) { + saved_details.tls_policy = strdup(tls_policy); + } else { + saved_details.tls_policy = NULL; + } + if (auth_policy) { + saved_details.auth_policy = strdup(auth_policy); + } else { + saved_details.auth_policy = NULL; + } + + // raw connect + log_info("Raw connecting to server: %s", altdomain); + + return connection_connect_raw( + saved_details.altdomain, + saved_details.port, + saved_details.tls_policy, + saved_details.auth_policy); +} + void session_autoping_fail(void) { @@ -261,6 +298,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 +582,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 b1958799..4849f3e1 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2782,6 +2782,9 @@ stanza_register_new_account(xmpp_ctx_t* ctx, const char* const user, const char* 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; } diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 686a0dc8..0bdf6934 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,8 @@ 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); + +jabber_conn_status_t session_connect_raw(const char* const altdomain, const int port, const char* const tls_policy, const char* const auth_policy); void session_disconnect(void); void session_shutdown(void); void session_process_events(void); -- cgit 1.4.1-2-gfad0 From 4d3f26154e2bcb9b975eb5236982cf8f19d9bed1 Mon Sep 17 00:00:00 2001 From: swirl Date: Mon, 5 Jul 2021 17:44:00 -0400 Subject: hacky fix(?) for register very bad and spaghetti will fix later --- src/command/cmd_funcs.c | 33 +++-- src/xmpp/connection.c | 347 +++++++++++++++++++++++++++++++++++++++++++++++- src/xmpp/connection.h | 3 + src/xmpp/stanza.c | 3 +- 4 files changed, 366 insertions(+), 20 deletions(-) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 07acd266..ed0a502f 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9587,32 +9587,29 @@ cmd_register(ProfWin* window, const char* const command, gchar** args) return TRUE; } + char* username = args[0]; char* server = args[1]; - jabber_conn_status_t conn_status = cl_ev_connect_raw(server, port, tls_policy, auth_policy); + char* passwd = ui_ask_password(false); + char* confirm_passwd = ui_ask_password(true); - if (conn_status == JABBER_DISCONNECTED) { + if (g_strcmp0(passwd, confirm_passwd) == 0) { + log_info("Attempting to register account %s on server %s.", username, server); + connection_register((server), port, tls_policy, auth_policy, username, passwd); + //iq_register_new_account(username, passwd); + } else { + cons_show("The two passwords do not match."); + } + //jabber_conn_status_t conn_status = cl_ev_connect_raw(server, port, tls_policy, auth_policy); + + if (connection_get_status() == JABBER_DISCONNECTED) { cons_show_error("Connection attempt to server %s port %d failed.", server, port); log_info("Connection attempt to server %s port %d failed.", server, port); return TRUE; } - char* username = args[0]; - if (connection_get_status() != JABBER_RAW_CONNECTED) { // FIXME: this is ALWAYS the case, as the connection doesn't finish by this time. - cons_show_error("Raw connection attempt failed or not yet completed."); - log_info("Raw connection attempt failed or not yet completed."); - } //else { - char* passwd = ui_ask_password(false); - char* confirm_passwd = ui_ask_password(true); - - if (g_strcmp0(passwd, confirm_passwd) == 0) { - log_info("Attempting to register account %s on server %s.", username, server); - iq_register_new_account(username, passwd); - } else { - cons_show("The two passwords do not match."); - } - free(passwd); - free(confirm_passwd); + free(passwd); + free(confirm_passwd); //} options_destroy(options); diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 49da1dc3..241070e0 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; @@ -350,6 +356,344 @@ connection_connect_raw(const char* const altdomain, int port, 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: + connection_disconnect(); + + 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); + connection_disconnect(); + goto quit; + } + + if (strcmp(type, "result") != 0) { + log_debug("Expected type 'result', but got %s.", type); + connection_disconnect(); + 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."); + connection_disconnect(); + 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); + connection_disconnect(); + + 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 */ + connection_disconnect(); + } + } + return 0; +} + +static int _register_handle_missing_features(xmpp_conn_t *xmpp_conn, void *userdata) +{ + (void)userdata; + + log_debug("Timeout"); + connection_disconnect(); + + 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 && strcmp(xmpp_stanza_get_ns(child), XMPP_NS_REGISTER) == 0) { + log_debug("Server does not support in-band registration."); + cons_show_error("Server does not support in-band registration, aborting."); + connection_disconnect(); + 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 auth_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, strdup(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; + } + + if (auth_policy && (g_strcmp0(auth_policy, "legacy") == 0)) { + flags |= XMPP_CONN_FLAG_LEGACY_AUTH; + } + + 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); + LOG_FLAG_IF_SET(XMPP_CONN_FLAG_LEGACY_AUTH); +#undef LOG_FLAG_IF_SET + } + + prof_reg_t *reg; + + reg = malloc(sizeof(*reg)); + if (reg != NULL) { + memset(reg, 0, sizeof(*reg)); + } + + 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, + strdup(altdomain), + port, + _connection_certfail_cb, + _register_handler, + reg); +#else + int connect_status = xmpp_connect_raw( + conn.xmpp_conn, + strdup(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) { @@ -620,7 +964,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, @@ -699,6 +1042,8 @@ _connection_handler(xmpp_conn_t* const xmpp_conn, const xmpp_conn_event_t status 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 diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h index cf24fc56..0c8669c6 100644 --- a/src/xmpp/connection.h +++ b/src/xmpp/connection.h @@ -48,6 +48,9 @@ jabber_conn_status_t connection_connect(const char* const fulljid, const char* c const char* const tls_policy, const char* const auth_policy); jabber_conn_status_t connection_connect_raw(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 auth_policy, + const char* const username, const char* const password); void connection_disconnect(void); void connection_set_disconnected(void); diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 4849f3e1..22835d1f 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2755,7 +2755,8 @@ 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); + //char* id = "reg2"; + xmpp_stanza_t* iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, strdup(id)); free(id); xmpp_stanza_t* register_new_account = xmpp_stanza_new(ctx); -- cgit 1.4.1-2-gfad0 From 8a6f1b52d76213d213c6e43f2b1d2fa40c3d09ea Mon Sep 17 00:00:00 2001 From: swirl Date: Sun, 18 Jul 2021 17:25:07 -0400 Subject: progress on fixing registration doesnt segfault on register, but aborts when you type after registering need to find a way to disconnect later --- src/command/cmd_funcs.c | 5 +---- src/xmpp/connection.c | 9 +++++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index ed0a502f..b7c6afad 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9595,12 +9595,10 @@ cmd_register(ProfWin* window, const char* const command, gchar** args) if (g_strcmp0(passwd, confirm_passwd) == 0) { log_info("Attempting to register account %s on server %s.", username, server); - connection_register((server), port, tls_policy, auth_policy, username, passwd); - //iq_register_new_account(username, passwd); + connection_register(server, port, tls_policy, auth_policy, username, passwd); } else { cons_show("The two passwords do not match."); } - //jabber_conn_status_t conn_status = cl_ev_connect_raw(server, port, tls_policy, auth_policy); if (connection_get_status() == JABBER_DISCONNECTED) { cons_show_error("Connection attempt to server %s port %d failed.", server, port); @@ -9610,7 +9608,6 @@ cmd_register(ProfWin* window, const char* const command, gchar** args) free(passwd); free(confirm_passwd); - //} options_destroy(options); diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 241070e0..da381898 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -380,7 +380,7 @@ static int iq_reg2_cb(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userd goto quit; quit: - connection_disconnect(); + //connection_disconnect(); return 0; } @@ -506,7 +506,6 @@ _register_handle_features(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *u 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); @@ -671,7 +670,8 @@ connection_register(const char* const altdomain, int port, int connect_status = xmpp_connect_raw( conn.xmpp_conn, - strdup(altdomain), + //strdup(altdomain), + altdomain, port, _connection_certfail_cb, _register_handler, @@ -679,7 +679,8 @@ connection_register(const char* const altdomain, int port, #else int connect_status = xmpp_connect_raw( conn.xmpp_conn, - strdup(altdomain), + //strdup(altdomain), + altdomain, port, _register_handler, reg); -- cgit 1.4.1-2-gfad0 From f53b4353fdba469297ee97ff4947f830bfd79f53 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 14 Jul 2021 16:23:05 +0200 Subject: REGISTRATION FIXED --- src/xmpp/connection.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index da381898..2d2df7c2 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -380,7 +380,7 @@ static int iq_reg2_cb(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userd goto quit; quit: - //connection_disconnect(); + xmpp_disconnect(xmpp_conn); return 0; } @@ -397,13 +397,13 @@ static int iq_reg_cb(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userda char* error_message = stanza_get_error_message(stanza); cons_show_error("Server error: %s", error_message); log_debug("Registration error: %s", error_message); - connection_disconnect(); + xmpp_disconnect(xmpp_conn); goto quit; } if (strcmp(type, "result") != 0) { log_debug("Expected type 'result', but got %s.", type); - connection_disconnect(); + xmpp_disconnect(xmpp_conn); goto quit; } @@ -413,7 +413,7 @@ static int iq_reg_cb(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userda if (registered != NULL) { cons_show_error("Already registered."); log_debug("Already registered."); - connection_disconnect(); + xmpp_disconnect(xmpp_conn); goto quit; } xmpp_stanza_t* iq = stanza_register_new_account(conn.xmpp_ctx, reg->username, reg->password); @@ -433,7 +433,7 @@ _register_handle_error(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *user char* error_message = stanza_get_error_message(stanza); cons_show_error("Server error: %s", error_message); log_debug("Registration error: %s", error_message); - connection_disconnect(); + xmpp_disconnect(xmpp_conn); return 0; } @@ -454,7 +454,7 @@ static int _register_handle_proceedtls_default(xmpp_conn_t *xmpp_conn, } else { log_debug("TLS failed."); /* failed tls spoils the connection, so disconnect */ - connection_disconnect(); + xmpp_disconnect(xmpp_conn); } } return 0; @@ -465,7 +465,7 @@ static int _register_handle_missing_features(xmpp_conn_t *xmpp_conn, void *userd (void)userdata; log_debug("Timeout"); - connection_disconnect(); + xmpp_disconnect(xmpp_conn); return 0; } @@ -500,7 +500,7 @@ _register_handle_features(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *u if (child && strcmp(xmpp_stanza_get_ns(child), XMPP_NS_REGISTER) == 0) { log_debug("Server does not support in-band registration."); cons_show_error("Server does not support in-band registration, aborting."); - connection_disconnect(); + xmpp_disconnect(xmpp_conn); return 0; } -- cgit 1.4.1-2-gfad0 From 87d2c95c870d697822c7d25f4c734e259a893d85 Mon Sep 17 00:00:00 2001 From: swirl Date: Tue, 17 Aug 2021 14:09:32 -0400 Subject: registration: remove auth param and excess functions --- src/command/cmd_defs.c | 6 +-- src/command/cmd_funcs.c | 10 +--- src/event/client_events.c | 7 --- src/event/client_events.h | 1 - src/xmpp/connection.c | 115 +--------------------------------------------- src/xmpp/connection.h | 5 +- src/xmpp/iq.c | 32 ------------- src/xmpp/session.c | 37 --------------- src/xmpp/xmpp.h | 2 - 9 files changed, 5 insertions(+), 210 deletions(-) diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index 80d98827..09523bd7 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -2649,7 +2649,7 @@ static struct cmd_t command_defs[] = { CMD_TAGS( CMD_TAG_CONNECTION) CMD_SYN( - "/register [port ] [tls force|allow|trust|legacy|disable] [auth default|legacy]") + "/register [port ] [tls force|allow|trust|legacy|disable]") CMD_DESC( "Register an account on a server.") CMD_ARGS( @@ -2660,9 +2660,7 @@ static struct cmd_t command_defs[] = { { "tls allow", "Use TLS for the connection if it is available." }, { "tls trust", "Force TLS connection and trust the server's certificate." }, { "tls legacy", "Use legacy TLS for the connection. This forces TLS just after the TCP connection is established. Use when a server doesn't support STARTTLS." }, - { "tls disable", "Disable TLS for the connection." }, - { "auth default", "Default authentication process." }, - { "auth legacy", "Allow legacy authentication." }) + { "tls disable", "Disable TLS for the connection." }) CMD_EXAMPLES( "/register odin valhalla.edda ", "/register freyr vanaheimr.edda port 5678", diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index b7c6afad..8b4dc3fa 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9579,14 +9579,6 @@ cmd_register(ProfWin* window, const char* const command, gchar** args) } } - char* auth_policy = g_hash_table_lookup(options, "auth"); - if (auth_policy && (g_strcmp0(auth_policy, "default") != 0) && (g_strcmp0(auth_policy, "legacy") != 0)) { - cons_bad_cmd_usage(command); - cons_show(""); - options_destroy(options); - return TRUE; - } - char* username = args[0]; char* server = args[1]; @@ -9595,7 +9587,7 @@ cmd_register(ProfWin* window, const char* const command, gchar** args) if (g_strcmp0(passwd, confirm_passwd) == 0) { log_info("Attempting to register account %s on server %s.", username, server); - connection_register(server, port, tls_policy, auth_policy, username, passwd); + connection_register(server, port, tls_policy, username, passwd); } else { cons_show("The two passwords do not match."); } diff --git a/src/event/client_events.c b/src/event/client_events.c index 14b5b8c4..a25ce106 100644 --- a/src/event/client_events.c +++ b/src/event/client_events.c @@ -81,13 +81,6 @@ cl_ev_connect_account(ProfAccount* account) return session_connect_with_account(account); } -jabber_conn_status_t -cl_ev_connect_raw(const char* const altdomain, const int port, const char* const tls_policy, const char* const auth_policy) -{ - cons_show("Raw connecting to %s", altdomain); - return session_connect_raw(altdomain, port, tls_policy, auth_policy); -} - void cl_ev_disconnect(void) { diff --git a/src/event/client_events.h b/src/event/client_events.h index 8fbc7a05..de180d21 100644 --- a/src/event/client_events.h +++ b/src/event/client_events.h @@ -40,7 +40,6 @@ jabber_conn_status_t cl_ev_connect_jid(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 cl_ev_connect_account(ProfAccount* account); -jabber_conn_status_t cl_ev_connect_raw(const char* const altdomain, const int port, const char* const tls_policy, const char* const auth_policy); void cl_ev_disconnect(void); diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 2d2df7c2..038fbb51 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -251,111 +251,6 @@ connection_connect(const char* const jid, const char* const passwd, const char* return conn.conn_status; } -jabber_conn_status_t -connection_connect_raw(const char* const altdomain, int port, - const char* const tls_policy, const char* const auth_policy) -{ - 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; - } - - if (auth_policy && (g_strcmp0(auth_policy, "legacy") == 0)) { - flags |= XMPP_CONN_FLAG_LEGACY_AUTH; - } - - 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); - LOG_FLAG_IF_SET(XMPP_CONN_FLAG_LEGACY_AUTH); -#undef LOG_FLAG_IF_SET - } - -#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, - _connection_handler, - conn.xmpp_ctx); -#else - int connect_status = xmpp_connect_raw( - conn.xmpp_conn, - altdomain, - port, - _connection_handler, - conn.xmpp_ctx); -#endif - - if (connect_status == 0) { - conn.conn_status = JABBER_RAW_CONNECTING; - } else { - conn.conn_status = JABBER_DISCONNECTED; - } - - return conn.conn_status; -} - static int iq_reg2_cb(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *userdata) { const char *type; @@ -578,8 +473,7 @@ _register_handler(xmpp_conn_t *xmpp_conn, } jabber_conn_status_t -connection_register(const char* const altdomain, int port, - const char* const tls_policy, const char* const auth_policy, +connection_register(const char* const altdomain, int port, const char* const tls_policy, const char* const username, const char* const password) { long flags; @@ -630,10 +524,6 @@ connection_register(const char* const altdomain, int port, flags |= XMPP_CONN_FLAG_LEGACY_SSL; } - if (auth_policy && (g_strcmp0(auth_policy, "legacy") == 0)) { - flags |= XMPP_CONN_FLAG_LEGACY_AUTH; - } - xmpp_conn_set_flags(conn.xmpp_conn, flags); /* Print debug logs that can help when users share the logs */ @@ -647,7 +537,6 @@ connection_register(const char* const altdomain, int port, 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); - LOG_FLAG_IF_SET(XMPP_CONN_FLAG_LEGACY_AUTH); #undef LOG_FLAG_IF_SET } @@ -670,7 +559,6 @@ connection_register(const char* const altdomain, int port, int connect_status = xmpp_connect_raw( conn.xmpp_conn, - //strdup(altdomain), altdomain, port, _connection_certfail_cb, @@ -679,7 +567,6 @@ connection_register(const char* const altdomain, int port, #else int connect_status = xmpp_connect_raw( conn.xmpp_conn, - //strdup(altdomain), altdomain, port, _register_handler, diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h index 0c8669c6..0f0c2c10 100644 --- a/src/xmpp/connection.h +++ b/src/xmpp/connection.h @@ -46,10 +46,7 @@ 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_connect_raw(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 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); diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 2490fd9c..2118b6ba 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -150,7 +150,6 @@ static int _command_list_result_handler(xmpp_stanza_t* const stanza, void* const static int _command_exec_response_handler(xmpp_stanza_t* const stanza, void* const userdata); static int _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata); static int _register_change_password_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata); -static int _register_new_account_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata); static void _iq_free_room_data(ProfRoomInfoData* roominfo); static void _iq_free_affiliation_set(ProfPrivilegeSet* affiliation_set); @@ -2655,37 +2654,6 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata) return 0; } -void -iq_register_new_account(const char* const user, const char* const password) -{ - xmpp_ctx_t* const ctx = connection_get_ctx(); - xmpp_stanza_t* iq = stanza_register_new_account(ctx, user, password); - - const char* id = xmpp_stanza_get_id(iq); - iq_id_handler_add(id, _register_new_account_result_id_handler, NULL, NULL); // FIXME: function doesn't seem to ever run? - - log_debug("HI hi sending registration stanza"); - iq_send_stanza(iq); - log_debug("registration stanza has been sent"); - xmpp_stanza_release(iq); -} - -static int -_register_new_account_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata) -{ - const char* type = xmpp_stanza_get_type(stanza); - if (g_strcmp0(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); - free(error_message); - } else { - cons_show("Registration successful."); - log_debug("Registration successful."); - } - return 0; -} - void iq_register_change_password(const char* const user, const char* const password) { diff --git a/src/xmpp/session.c b/src/xmpp/session.c index ccd36865..de255116 100644 --- a/src/xmpp/session.c +++ b/src/xmpp/session.c @@ -204,43 +204,6 @@ session_connect_with_details(const char* const jid, const char* const passwd, co saved_details.auth_policy); } -jabber_conn_status_t -session_connect_raw(const char* const altdomain, const int port, const char* const tls_policy, - const char* const auth_policy) -{ - assert(altdomain != NULL); - - _session_free_saved_account(); - _session_free_saved_details(); - - // save details for reconnect - saved_details.altdomain = strdup(altdomain); - if (port != 0) { - saved_details.port = port; - } else { - saved_details.port = 0; - } - if (tls_policy) { - saved_details.tls_policy = strdup(tls_policy); - } else { - saved_details.tls_policy = NULL; - } - if (auth_policy) { - saved_details.auth_policy = strdup(auth_policy); - } else { - saved_details.auth_policy = NULL; - } - - // raw connect - log_info("Raw connecting to server: %s", altdomain); - - return connection_connect_raw( - saved_details.altdomain, - saved_details.port, - saved_details.tls_policy, - saved_details.auth_policy); -} - void session_autoping_fail(void) { diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 0bdf6934..b961afe9 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -186,7 +186,6 @@ jabber_conn_status_t session_connect_with_details(const char* const jid, const c 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); -jabber_conn_status_t session_connect_raw(const char* const altdomain, const int port, const char* const tls_policy, const char* const auth_policy); void session_disconnect(void); void session_shutdown(void); void session_process_events(void); @@ -270,7 +269,6 @@ void iq_command_list(const char* const target); void iq_command_exec(const char* const target, const char* const command); void iq_mam_request(ProfChatWin* win); void iq_register_change_password(const char* const user, const char* const password); -void iq_register_new_account(const char* const user, const char* const password); void iq_muc_register_nick(const char* const roomjid); EntityCapabilities* caps_lookup(const char* const jid); -- cgit 1.4.1-2-gfad0 From feaa770444a1063f9a272e90d84b711b09669548 Mon Sep 17 00:00:00 2001 From: swirl Date: Sun, 10 Oct 2021 15:43:14 -0400 Subject: fixed crashes when server doesn't support registration --- src/xmpp/connection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 038fbb51..e478df1a 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -392,7 +392,7 @@ _register_handle_features(xmpp_conn_t *xmpp_conn, xmpp_stanza_t *stanza, void *u /* check whether server supports in-band registration */ child = xmpp_stanza_get_child_by_name(stanza, "register"); - if (child && strcmp(xmpp_stanza_get_ns(child), XMPP_NS_REGISTER) == 0) { + 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); -- cgit 1.4.1-2-gfad0 From 0653200965122c46be4f31996d2396b568ee8dcf Mon Sep 17 00:00:00 2001 From: swirl Date: Wed, 13 Oct 2021 15:02:05 -0400 Subject: registration: memory leaks, error checking --- src/xmpp/connection.c | 9 +++++---- src/xmpp/stanza.c | 3 +-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index e478df1a..295c8bec 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -509,7 +509,7 @@ connection_register(const char* const altdomain, int port, const char* const tls log_warning("Failed to get libstrophe conn during connect"); return JABBER_DISCONNECTED; } - xmpp_conn_set_jid(conn.xmpp_conn, strdup(altdomain)); + xmpp_conn_set_jid(conn.xmpp_conn, altdomain); flags = xmpp_conn_get_flags(conn.xmpp_conn); @@ -542,9 +542,10 @@ connection_register(const char* const altdomain, int port, const char* const tls prof_reg_t *reg; - reg = malloc(sizeof(*reg)); - if (reg != NULL) { - memset(reg, 0, sizeof(*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); diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 22835d1f..4849f3e1 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2755,8 +2755,7 @@ 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(); - //char* id = "reg2"; - xmpp_stanza_t* iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, strdup(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); -- cgit 1.4.1-2-gfad0