From d9366a26d6ef1a82eeccd7d7523db25d91e4efc1 Mon Sep 17 00:00:00 2001 From: swirl Date: Thu, 1 Jul 2021 19:30:30 -0400 Subject: [PATCH] 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 +