Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / protocols / jabber / auth_plain.c
blob1df6b77837187b52232f6db1b6f36532eb3382a3
1 /*
2 * purple - Jabber Protocol Plugin
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "internal.h"
25 #include "account.h"
26 #include "debug.h"
27 #include "request.h"
28 #include "util.h"
29 #include "xmlnode.h"
31 #include "jabber.h"
32 #include "auth.h"
34 static PurpleXmlNode *finish_plaintext_authentication(JabberStream *js)
36 PurpleXmlNode *auth;
37 GString *response;
38 gchar *enc_out;
40 auth = purple_xmlnode_new("auth");
41 purple_xmlnode_set_namespace(auth, NS_XMPP_SASL);
43 purple_xmlnode_set_attrib(auth, "xmlns:ga", "http://www.google.com/talk/protocol/auth");
44 purple_xmlnode_set_attrib(auth, "ga:client-uses-full-bind-result", "true");
46 response = g_string_new("");
47 response = g_string_append_c(response, '\0');
48 response = g_string_append(response, js->user->node);
49 response = g_string_append_c(response, '\0');
50 response = g_string_append(response,
51 purple_connection_get_password(js->gc));
53 enc_out = purple_base64_encode((guchar *)response->str, response->len);
55 purple_xmlnode_set_attrib(auth, "mechanism", "PLAIN");
56 purple_xmlnode_insert_data(auth, enc_out, -1);
57 g_free(enc_out);
58 g_string_free(response, TRUE);
60 return auth;
63 static void allow_plaintext_auth(PurpleAccount *account)
65 PurpleConnection *gc = purple_account_get_connection(account);
66 JabberStream *js = purple_connection_get_protocol_data(gc);
67 PurpleXmlNode *response;
69 purple_account_set_bool(account, "auth_plain_in_clear", TRUE);
71 response = finish_plaintext_authentication(js);
72 jabber_send(js, response);
73 purple_xmlnode_free(response);
76 static void disallow_plaintext_auth(PurpleAccount *account)
78 purple_connection_error(purple_account_get_connection(account),
79 PURPLE_CONNECTION_ERROR_ENCRYPTION_ERROR,
80 _("Server requires plaintext authentication over an unencrypted stream"));
83 static JabberSaslState
84 jabber_plain_start(JabberStream *js, PurpleXmlNode *packet, PurpleXmlNode **response, char **error)
86 PurpleAccount *account = purple_connection_get_account(js->gc);
87 char *msg;
89 if (jabber_stream_is_ssl(js) || purple_account_get_bool(account, "auth_plain_in_clear", FALSE)) {
90 *response = finish_plaintext_authentication(js);
91 return JABBER_SASL_STATE_OK;
94 msg = g_strdup_printf(_("%s requires plaintext authentication over an unencrypted connection. Allow this and continue authentication?"),
95 purple_account_get_username(account));
96 purple_request_yes_no(js->gc, _("Plaintext Authentication"),
97 _("Plaintext Authentication"),
98 msg,
100 purple_request_cpar_from_account(account),
101 account, allow_plaintext_auth, disallow_plaintext_auth);
102 g_free(msg);
103 return JABBER_SASL_STATE_CONTINUE;
106 static JabberSaslMech plain_mech = {
107 0, /* priority */
108 "PLAIN", /* name */
109 jabber_plain_start,
110 NULL, /* handle_challenge */
111 NULL, /* handle_success */
112 NULL, /* handle_failure */
113 NULL /* dispose */
116 JabberSaslMech *jabber_auth_get_plain_mech(void)
118 return &plain_mech;