[5997488] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Jabber module - SASL authentication * |
---|
| 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2006-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[5997488] | 7 | * * |
---|
| 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. * |
---|
| 12 | * * |
---|
| 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. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU General Public License along * |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
| 21 | * * |
---|
| 22 | \***************************************************************************/ |
---|
| 23 | |
---|
[af97b23] | 24 | #include <ctype.h> |
---|
| 25 | |
---|
[5997488] | 26 | #include "jabber.h" |
---|
| 27 | #include "base64.h" |
---|
[57b4525] | 28 | #include "oauth2.h" |
---|
[e1c926f] | 29 | #include "oauth.h" |
---|
[5997488] | 30 | |
---|
[18c6d36] | 31 | const struct oauth2_service oauth2_service_google = |
---|
| 32 | { |
---|
| 33 | "https://accounts.google.com/o/oauth2/auth", |
---|
| 34 | "https://accounts.google.com/o/oauth2/token", |
---|
| 35 | "urn:ietf:wg:oauth:2.0:oob", |
---|
| 36 | "https://www.googleapis.com/auth/googletalk", |
---|
| 37 | "783993391592.apps.googleusercontent.com", |
---|
| 38 | "6C-Zgf7Tr7gEQTPlBhMUgo7R", |
---|
| 39 | }; |
---|
| 40 | const struct oauth2_service oauth2_service_facebook = |
---|
| 41 | { |
---|
| 42 | "https://www.facebook.com/dialog/oauth", |
---|
| 43 | "https://graph.facebook.com/oauth/access_token", |
---|
[8eb2e84] | 44 | "https://www.bitlbee.org/main.php/Facebook/oauth2.html", |
---|
[18c6d36] | 45 | "offline_access,xmpp_login", |
---|
| 46 | "126828914005625", |
---|
| 47 | "4b100f0f244d620bf3f15f8b217d4c32", |
---|
| 48 | }; |
---|
| 49 | |
---|
[5ebff60] | 50 | xt_status sasl_pkt_mechanisms(struct xt_node *node, gpointer data) |
---|
[5997488] | 51 | { |
---|
[0da65d5] | 52 | struct im_connection *ic = data; |
---|
| 53 | struct jabber_data *jd = ic->proto_data; |
---|
[5997488] | 54 | struct xt_node *c, *reply; |
---|
| 55 | char *s; |
---|
[73dd021] | 56 | int sup_plain = 0, sup_digest = 0, sup_gtalk = 0, sup_fb = 0, sup_anonymous = 0; |
---|
| 57 | int want_oauth = FALSE, want_hipchat = FALSE, want_anonymous = FALSE; |
---|
[4be0e34] | 58 | GString *mechs; |
---|
[5ebff60] | 59 | |
---|
| 60 | if (!sasl_supported(ic)) { |
---|
[8d74291] | 61 | /* Should abort this now, since we should already be doing |
---|
| 62 | IQ authentication. Strange things happen when you try |
---|
| 63 | to do both... */ |
---|
[5ebff60] | 64 | imcb_log(ic, |
---|
| 65 | "XMPP 1.0 non-compliant server seems to support SASL, please report this as a BitlBee bug!"); |
---|
[8d74291] | 66 | return XT_HANDLED; |
---|
| 67 | } |
---|
[5ebff60] | 68 | |
---|
| 69 | s = xt_find_attr(node, "xmlns"); |
---|
| 70 | if (!s || strcmp(s, XMLNS_SASL) != 0) { |
---|
| 71 | imcb_log(ic, "Stream error while authenticating"); |
---|
| 72 | imc_logout(ic, FALSE); |
---|
[5997488] | 73 | return XT_ABORT; |
---|
| 74 | } |
---|
[5ebff60] | 75 | |
---|
[73dd021] | 76 | want_anonymous = set_getbool(&ic->acc->set, "anonymous"); |
---|
[5ebff60] | 77 | want_oauth = set_getbool(&ic->acc->set, "oauth"); |
---|
[40cfbc5] | 78 | want_hipchat = (jd->flags & JFLAG_HIPCHAT); |
---|
[5ebff60] | 79 | |
---|
| 80 | mechs = g_string_new(""); |
---|
[5997488] | 81 | c = node->children; |
---|
[5ebff60] | 82 | while ((c = xt_find_node(c, "mechanism"))) { |
---|
| 83 | if (c->text && g_strcasecmp(c->text, "PLAIN") == 0) { |
---|
[5997488] | 84 | sup_plain = 1; |
---|
[5ebff60] | 85 | } else if (c->text && g_strcasecmp(c->text, "DIGEST-MD5") == 0) { |
---|
[5997488] | 86 | sup_digest = 1; |
---|
[73dd021] | 87 | } else if (c->text && g_strcasecmp(c->text, "ANONYMOUS") == 0) { |
---|
| 88 | sup_anonymous = 1; |
---|
[5ebff60] | 89 | } else if (c->text && g_strcasecmp(c->text, "X-OAUTH2") == 0) { |
---|
[18c6d36] | 90 | sup_gtalk = 1; |
---|
[5ebff60] | 91 | } else if (c->text && g_strcasecmp(c->text, "X-FACEBOOK-PLATFORM") == 0) { |
---|
[e1c926f] | 92 | sup_fb = 1; |
---|
[5ebff60] | 93 | } |
---|
| 94 | |
---|
| 95 | if (c->text) { |
---|
| 96 | g_string_append_printf(mechs, " %s", c->text); |
---|
| 97 | } |
---|
| 98 | |
---|
[5997488] | 99 | c = c->next; |
---|
| 100 | } |
---|
[5ebff60] | 101 | |
---|
| 102 | if (!want_oauth && !sup_plain && !sup_digest) { |
---|
| 103 | if (!sup_gtalk && !sup_fb) { |
---|
| 104 | imcb_error(ic, "This server requires OAuth " |
---|
| 105 | "(supported schemes:%s)", mechs->str); |
---|
| 106 | } else { |
---|
| 107 | imcb_error(ic, "BitlBee does not support any of the offered SASL " |
---|
| 108 | "authentication schemes:%s", mechs->str); |
---|
| 109 | } |
---|
| 110 | imc_logout(ic, FALSE); |
---|
| 111 | g_string_free(mechs, TRUE); |
---|
[5997488] | 112 | return XT_ABORT; |
---|
| 113 | } |
---|
[5ebff60] | 114 | g_string_free(mechs, TRUE); |
---|
| 115 | |
---|
| 116 | reply = xt_new_node("auth", NULL, NULL); |
---|
[40cfbc5] | 117 | if (!want_hipchat) { |
---|
| 118 | xt_add_attr(reply, "xmlns", XMLNS_SASL); |
---|
| 119 | } else { |
---|
| 120 | xt_add_attr(reply, "xmlns", XMLNS_HIPCHAT); |
---|
| 121 | } |
---|
[5ebff60] | 122 | |
---|
| 123 | if (sup_gtalk && want_oauth) { |
---|
[4a5d885] | 124 | int len; |
---|
[5ebff60] | 125 | |
---|
[4a5d885] | 126 | /* X-OAUTH2 is, not *the* standard OAuth2 SASL/XMPP implementation. |
---|
| 127 | It's currently used by GTalk and vaguely documented on |
---|
| 128 | http://code.google.com/apis/cloudprint/docs/rawxmpp.html . */ |
---|
[5ebff60] | 129 | xt_add_attr(reply, "mechanism", "X-OAUTH2"); |
---|
| 130 | |
---|
| 131 | len = strlen(jd->username) + strlen(jd->oauth2_access_token) + 2; |
---|
| 132 | s = g_malloc(len + 1); |
---|
[4a5d885] | 133 | s[0] = 0; |
---|
[5ebff60] | 134 | strcpy(s + 1, jd->username); |
---|
| 135 | strcpy(s + 2 + strlen(jd->username), jd->oauth2_access_token); |
---|
| 136 | reply->text = base64_encode((unsigned char *) s, len); |
---|
| 137 | reply->text_len = strlen(reply->text); |
---|
| 138 | g_free(s); |
---|
| 139 | } else if (sup_fb && want_oauth) { |
---|
| 140 | xt_add_attr(reply, "mechanism", "X-FACEBOOK-PLATFORM"); |
---|
[e1c926f] | 141 | jd->flags |= JFLAG_SASL_FB; |
---|
[5ebff60] | 142 | } else if (want_oauth) { |
---|
| 143 | imcb_error(ic, "OAuth requested, but not supported by server"); |
---|
| 144 | imc_logout(ic, FALSE); |
---|
| 145 | xt_free_node(reply); |
---|
[18c6d36] | 146 | return XT_ABORT; |
---|
[73dd021] | 147 | } else if (want_anonymous && sup_anonymous) { |
---|
| 148 | xt_add_attr(reply, "mechanism", "ANONYMOUS"); |
---|
| 149 | |
---|
| 150 | /* Well, that was easy. */ |
---|
| 151 | } else if (want_anonymous) { |
---|
| 152 | imcb_error(ic, "Anonymous login requested, but not supported by server"); |
---|
| 153 | imc_logout(ic, FALSE); |
---|
| 154 | xt_free_node(reply); |
---|
| 155 | return XT_ABORT; |
---|
[5ebff60] | 156 | } else if (sup_digest) { |
---|
| 157 | xt_add_attr(reply, "mechanism", "DIGEST-MD5"); |
---|
| 158 | |
---|
[fe7a554] | 159 | /* The rest will be done later, when we receive a <challenge/>. */ |
---|
[5ebff60] | 160 | } else if (sup_plain) { |
---|
[5997488] | 161 | int len; |
---|
[40cfbc5] | 162 | GString *gs; |
---|
| 163 | char *username; |
---|
[5ebff60] | 164 | |
---|
[40cfbc5] | 165 | if (!want_hipchat) { |
---|
| 166 | xt_add_attr(reply, "mechanism", "PLAIN"); |
---|
| 167 | username = jd->username; |
---|
| 168 | } else { |
---|
| 169 | username = jd->me; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* set an arbitrary initial size to avoid reallocations */ |
---|
| 173 | gs = g_string_sized_new(128); |
---|
[5ebff60] | 174 | |
---|
[5997488] | 175 | /* With SASL PLAIN in XMPP, the text should be b64(\0user\0pass) */ |
---|
[40cfbc5] | 176 | g_string_append_c(gs, '\0'); |
---|
| 177 | g_string_append(gs, username); |
---|
| 178 | g_string_append_c(gs, '\0'); |
---|
| 179 | g_string_append(gs, ic->acc->pass); |
---|
| 180 | if (want_hipchat) { |
---|
| 181 | /* Hipchat's variation adds \0resource at the end */ |
---|
| 182 | g_string_append_c(gs, '\0'); |
---|
| 183 | g_string_append(gs, set_getstr(&ic->acc->set, "resource")); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | len = gs->len; |
---|
| 187 | s = g_string_free(gs, FALSE); |
---|
| 188 | |
---|
[5ebff60] | 189 | reply->text = base64_encode((unsigned char *) s, len); |
---|
| 190 | reply->text_len = strlen(reply->text); |
---|
| 191 | g_free(s); |
---|
[5997488] | 192 | } |
---|
[5ebff60] | 193 | |
---|
| 194 | if (reply && !jabber_write_packet(ic, reply)) { |
---|
| 195 | xt_free_node(reply); |
---|
[5997488] | 196 | return XT_ABORT; |
---|
| 197 | } |
---|
[5ebff60] | 198 | xt_free_node(reply); |
---|
| 199 | |
---|
[5997488] | 200 | /* To prevent classic authentication from happening. */ |
---|
| 201 | jd->flags |= JFLAG_STREAM_STARTED; |
---|
[5ebff60] | 202 | |
---|
[5997488] | 203 | return XT_HANDLED; |
---|
| 204 | } |
---|
| 205 | |
---|
[af97b23] | 206 | /* Non-static function, but not mentioned in jabber.h because it's for internal |
---|
| 207 | use, just that the unittest should be able to reach it... */ |
---|
[5ebff60] | 208 | char *sasl_get_part(char *data, char *field) |
---|
[d8e0484] | 209 | { |
---|
| 210 | int i, len; |
---|
[5ebff60] | 211 | |
---|
| 212 | len = strlen(field); |
---|
| 213 | |
---|
| 214 | while (g_ascii_isspace(*data) || *data == ',') { |
---|
| 215 | data++; |
---|
[d8e0484] | 216 | } |
---|
[5ebff60] | 217 | |
---|
| 218 | if (g_strncasecmp(data, field, len) == 0 && data[len] == '=') { |
---|
| 219 | i = strlen(field) + 1; |
---|
| 220 | } else { |
---|
| 221 | for (i = 0; data[i]; i++) { |
---|
[d8e0484] | 222 | /* If we have a ", skip until it's closed again. */ |
---|
[5ebff60] | 223 | if (data[i] == '"') { |
---|
| 224 | i++; |
---|
| 225 | while (data[i] != '"' || data[i - 1] == '\\') { |
---|
| 226 | i++; |
---|
| 227 | } |
---|
[d8e0484] | 228 | } |
---|
[5ebff60] | 229 | |
---|
[af97b23] | 230 | /* If we got a comma, we got a new field. Check it, |
---|
| 231 | find the next key after it. */ |
---|
[5ebff60] | 232 | if (data[i] == ',') { |
---|
| 233 | while (g_ascii_isspace(data[i]) || data[i] == ',') { |
---|
| 234 | i++; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | if (g_strncasecmp(data + i, field, len) == 0 && |
---|
| 238 | data[i + len] == '=') { |
---|
[af97b23] | 239 | i += len + 1; |
---|
| 240 | break; |
---|
| 241 | } |
---|
[d8e0484] | 242 | } |
---|
| 243 | } |
---|
| 244 | } |
---|
[5ebff60] | 245 | |
---|
| 246 | if (data[i] == '"') { |
---|
[d8e0484] | 247 | int j; |
---|
| 248 | char *ret; |
---|
[5ebff60] | 249 | |
---|
| 250 | i++; |
---|
[d8e0484] | 251 | len = 0; |
---|
[5ebff60] | 252 | while (data[i + len] != '"' || data[i + len - 1] == '\\') { |
---|
| 253 | len++; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | ret = g_strndup(data + i, len); |
---|
| 257 | for (i = j = 0; ret[i]; i++) { |
---|
| 258 | if (ret[i] == '\\') { |
---|
[d8e0484] | 259 | ret[j++] = ret[++i]; |
---|
[5ebff60] | 260 | } else { |
---|
[d8e0484] | 261 | ret[j++] = ret[i]; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | ret[j] = 0; |
---|
[5ebff60] | 265 | |
---|
[d8e0484] | 266 | return ret; |
---|
[5ebff60] | 267 | } else if (data[i]) { |
---|
[d8e0484] | 268 | len = 0; |
---|
[5ebff60] | 269 | while (data[i + len] && data[i + len] != ',') { |
---|
| 270 | len++; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | return g_strndup(data + i, len); |
---|
| 274 | } else { |
---|
[d8e0484] | 275 | return NULL; |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | |
---|
[5ebff60] | 279 | xt_status sasl_pkt_challenge(struct xt_node *node, gpointer data) |
---|
[5997488] | 280 | { |
---|
[0da65d5] | 281 | struct im_connection *ic = data; |
---|
| 282 | struct jabber_data *jd = ic->proto_data; |
---|
[f138bd2] | 283 | struct xt_node *reply_pkt = NULL; |
---|
[3b6eadc] | 284 | char *nonce = NULL, *realm = NULL, *cnonce = NULL; |
---|
| 285 | unsigned char cnonce_bin[30]; |
---|
[d8e0484] | 286 | char *digest_uri = NULL; |
---|
| 287 | char *dec = NULL; |
---|
[f138bd2] | 288 | char *s = NULL, *reply = NULL; |
---|
[d8e0484] | 289 | xt_status ret = XT_ABORT; |
---|
[5ebff60] | 290 | |
---|
| 291 | if (node->text_len == 0) { |
---|
[d8e0484] | 292 | goto error; |
---|
[5ebff60] | 293 | } |
---|
| 294 | |
---|
| 295 | dec = frombase64(node->text); |
---|
| 296 | |
---|
| 297 | if (jd->flags & JFLAG_SASL_FB) { |
---|
[644b808] | 298 | /* New-style Facebook OAauth2 support. Instead of sending a refresh |
---|
| 299 | token, they just send an access token that should never expire. */ |
---|
[64b6635] | 300 | GSList *p_in = NULL, *p_out = NULL; |
---|
| 301 | char time[33]; |
---|
[5ebff60] | 302 | |
---|
| 303 | oauth_params_parse(&p_in, dec); |
---|
| 304 | oauth_params_add(&p_out, "nonce", oauth_params_get(&p_in, "nonce")); |
---|
| 305 | oauth_params_add(&p_out, "method", oauth_params_get(&p_in, "method")); |
---|
| 306 | oauth_params_free(&p_in); |
---|
| 307 | |
---|
| 308 | g_snprintf(time, sizeof(time), "%lld", (long long) (gettime() * 1000)); |
---|
| 309 | oauth_params_add(&p_out, "call_id", time); |
---|
| 310 | oauth_params_add(&p_out, "api_key", oauth2_service_facebook.consumer_key); |
---|
| 311 | oauth_params_add(&p_out, "v", "1.0"); |
---|
| 312 | oauth_params_add(&p_out, "format", "XML"); |
---|
| 313 | oauth_params_add(&p_out, "access_token", jd->oauth2_access_token); |
---|
| 314 | |
---|
| 315 | reply = oauth_params_string(p_out); |
---|
| 316 | oauth_params_free(&p_out); |
---|
| 317 | } else if (!(s = sasl_get_part(dec, "rspauth"))) { |
---|
[d8e0484] | 318 | /* See RFC 2831 for for information. */ |
---|
| 319 | md5_state_t A1, A2, H; |
---|
| 320 | md5_byte_t A1r[16], A2r[16], Hr[16]; |
---|
| 321 | char A1h[33], A2h[33], Hh[33]; |
---|
| 322 | int i; |
---|
[5ebff60] | 323 | |
---|
| 324 | nonce = sasl_get_part(dec, "nonce"); |
---|
| 325 | realm = sasl_get_part(dec, "realm"); |
---|
| 326 | |
---|
| 327 | if (!nonce) { |
---|
[d8e0484] | 328 | goto error; |
---|
[5ebff60] | 329 | } |
---|
| 330 | |
---|
[d9282b4] | 331 | /* Jabber.Org considers the realm part optional and doesn't |
---|
| 332 | specify one. Oh well, actually they're right, but still, |
---|
| 333 | don't know if this is right... */ |
---|
[5ebff60] | 334 | if (!realm) { |
---|
| 335 | realm = g_strdup(jd->server); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | random_bytes(cnonce_bin, sizeof(cnonce_bin)); |
---|
| 339 | cnonce = base64_encode(cnonce_bin, sizeof(cnonce_bin)); |
---|
| 340 | digest_uri = g_strdup_printf("%s/%s", "xmpp", jd->server); |
---|
| 341 | |
---|
[d8e0484] | 342 | /* Generate the MD5 hash of username:realm:password, |
---|
| 343 | I decided to call it H. */ |
---|
[5ebff60] | 344 | md5_init(&H); |
---|
| 345 | s = g_strdup_printf("%s:%s:%s", jd->username, realm, ic->acc->pass); |
---|
| 346 | md5_append(&H, (unsigned char *) s, strlen(s)); |
---|
| 347 | g_free(s); |
---|
| 348 | md5_finish(&H, Hr); |
---|
| 349 | |
---|
[d8e0484] | 350 | /* Now generate the hex. MD5 hash of H:nonce:cnonce, called A1. */ |
---|
[5ebff60] | 351 | md5_init(&A1); |
---|
| 352 | s = g_strdup_printf(":%s:%s", nonce, cnonce); |
---|
| 353 | md5_append(&A1, Hr, 16); |
---|
| 354 | md5_append(&A1, (unsigned char *) s, strlen(s)); |
---|
| 355 | g_free(s); |
---|
| 356 | md5_finish(&A1, A1r); |
---|
| 357 | for (i = 0; i < 16; i++) { |
---|
| 358 | sprintf(A1h + i * 2, "%02x", A1r[i]); |
---|
| 359 | } |
---|
| 360 | |
---|
[d8e0484] | 361 | /* A2... */ |
---|
[5ebff60] | 362 | md5_init(&A2); |
---|
| 363 | s = g_strdup_printf("%s:%s", "AUTHENTICATE", digest_uri); |
---|
| 364 | md5_append(&A2, (unsigned char *) s, strlen(s)); |
---|
| 365 | g_free(s); |
---|
| 366 | md5_finish(&A2, A2r); |
---|
| 367 | for (i = 0; i < 16; i++) { |
---|
| 368 | sprintf(A2h + i * 2, "%02x", A2r[i]); |
---|
| 369 | } |
---|
| 370 | |
---|
[d8e0484] | 371 | /* Final result: A1:nonce:00000001:cnonce:auth:A2. Let's reuse H for it. */ |
---|
[5ebff60] | 372 | md5_init(&H); |
---|
| 373 | s = g_strdup_printf("%s:%s:%s:%s:%s:%s", A1h, nonce, "00000001", cnonce, "auth", A2h); |
---|
| 374 | md5_append(&H, (unsigned char *) s, strlen(s)); |
---|
| 375 | g_free(s); |
---|
| 376 | md5_finish(&H, Hr); |
---|
| 377 | for (i = 0; i < 16; i++) { |
---|
| 378 | sprintf(Hh + i * 2, "%02x", Hr[i]); |
---|
| 379 | } |
---|
| 380 | |
---|
[d8e0484] | 381 | /* Now build the SASL response string: */ |
---|
[5ebff60] | 382 | reply = g_strdup_printf("username=\"%s\",realm=\"%s\",nonce=\"%s\",cnonce=\"%s\"," |
---|
| 383 | "nc=%08x,qop=auth,digest-uri=\"%s\",response=%s,charset=%s", |
---|
| 384 | jd->username, realm, nonce, cnonce, 1, digest_uri, Hh, "utf-8"); |
---|
| 385 | } else { |
---|
[d8e0484] | 386 | /* We found rspauth, but don't really care... */ |
---|
[5ebff60] | 387 | g_free(s); |
---|
[d8e0484] | 388 | } |
---|
[5ebff60] | 389 | |
---|
| 390 | s = reply ? tobase64(reply) : NULL; |
---|
| 391 | reply_pkt = xt_new_node("response", s, NULL); |
---|
| 392 | xt_add_attr(reply_pkt, "xmlns", XMLNS_SASL); |
---|
| 393 | |
---|
| 394 | if (!jabber_write_packet(ic, reply_pkt)) { |
---|
[d8e0484] | 395 | goto silent_error; |
---|
[5ebff60] | 396 | } |
---|
| 397 | |
---|
[d8e0484] | 398 | ret = XT_HANDLED; |
---|
| 399 | goto silent_error; |
---|
| 400 | |
---|
| 401 | error: |
---|
[5ebff60] | 402 | imcb_error(ic, "Incorrect SASL challenge received"); |
---|
| 403 | imc_logout(ic, FALSE); |
---|
[d8e0484] | 404 | |
---|
| 405 | silent_error: |
---|
[5ebff60] | 406 | g_free(digest_uri); |
---|
| 407 | g_free(cnonce); |
---|
| 408 | g_free(nonce); |
---|
| 409 | g_free(reply); |
---|
| 410 | g_free(realm); |
---|
| 411 | g_free(dec); |
---|
| 412 | g_free(s); |
---|
| 413 | xt_free_node(reply_pkt); |
---|
| 414 | |
---|
[d8e0484] | 415 | return ret; |
---|
[5997488] | 416 | } |
---|
| 417 | |
---|
[5ebff60] | 418 | xt_status sasl_pkt_result(struct xt_node *node, gpointer data) |
---|
[5997488] | 419 | { |
---|
[0da65d5] | 420 | struct im_connection *ic = data; |
---|
| 421 | struct jabber_data *jd = ic->proto_data; |
---|
[5997488] | 422 | char *s; |
---|
[5ebff60] | 423 | |
---|
| 424 | s = xt_find_attr(node, "xmlns"); |
---|
| 425 | if (!s || strcmp(s, XMLNS_SASL) != 0) { |
---|
| 426 | imcb_log(ic, "Stream error while authenticating"); |
---|
| 427 | imc_logout(ic, FALSE); |
---|
[5997488] | 428 | return XT_ABORT; |
---|
| 429 | } |
---|
[5ebff60] | 430 | |
---|
| 431 | if (strcmp(node->name, "success") == 0) { |
---|
| 432 | imcb_log(ic, "Authentication finished"); |
---|
[5997488] | 433 | jd->flags |= JFLAG_AUTHENTICATED | JFLAG_STREAM_RESTART; |
---|
[40cfbc5] | 434 | |
---|
| 435 | if (jd->flags & JFLAG_HIPCHAT) { |
---|
| 436 | return hipchat_handle_success(ic, node); |
---|
| 437 | } |
---|
[5ebff60] | 438 | } else if (strcmp(node->name, "failure") == 0) { |
---|
| 439 | imcb_error(ic, "Authentication failure"); |
---|
| 440 | imc_logout(ic, FALSE); |
---|
[5997488] | 441 | return XT_ABORT; |
---|
| 442 | } |
---|
[5ebff60] | 443 | |
---|
[5997488] | 444 | return XT_HANDLED; |
---|
| 445 | } |
---|
[8d74291] | 446 | |
---|
| 447 | /* This one is needed to judge if we'll do authentication using IQ or SASL. |
---|
| 448 | It's done by checking if the <stream:stream> from the server has a |
---|
| 449 | version attribute. I don't know if this is the right way though... */ |
---|
[5ebff60] | 450 | gboolean sasl_supported(struct im_connection *ic) |
---|
[8d74291] | 451 | { |
---|
[0da65d5] | 452 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 453 | |
---|
| 454 | return (jd->xt && jd->xt->root && xt_find_attr(jd->xt->root, "version")) != 0; |
---|
[8d74291] | 455 | } |
---|
[4a5d885] | 456 | |
---|
[5ebff60] | 457 | void sasl_oauth2_init(struct im_connection *ic) |
---|
[4a5d885] | 458 | { |
---|
[18c6d36] | 459 | struct jabber_data *jd = ic->proto_data; |
---|
[4a5d885] | 460 | char *msg, *url; |
---|
[5ebff60] | 461 | |
---|
| 462 | imcb_log(ic, "Starting OAuth authentication"); |
---|
| 463 | |
---|
[4a5d885] | 464 | /* Temporary contact, just used to receive the OAuth response. */ |
---|
[5ebff60] | 465 | imcb_add_buddy(ic, JABBER_OAUTH_HANDLE, NULL); |
---|
| 466 | url = oauth2_url(jd->oauth2_service); |
---|
| 467 | msg = g_strdup_printf("Open this URL in your browser to authenticate: %s", url); |
---|
| 468 | imcb_buddy_msg(ic, JABBER_OAUTH_HANDLE, msg, 0, 0); |
---|
| 469 | imcb_buddy_msg(ic, JABBER_OAUTH_HANDLE, "Respond to this message with the returned " |
---|
| 470 | "authorization token.", 0, 0); |
---|
| 471 | |
---|
| 472 | g_free(msg); |
---|
| 473 | g_free(url); |
---|
[4a5d885] | 474 | } |
---|
| 475 | |
---|
[5ebff60] | 476 | static gboolean sasl_oauth2_remove_contact(gpointer data, gint fd, b_input_condition cond) |
---|
[4a5d885] | 477 | { |
---|
| 478 | struct im_connection *ic = data; |
---|
[5ebff60] | 479 | |
---|
| 480 | if (g_slist_find(jabber_connections, ic)) { |
---|
| 481 | imcb_remove_buddy(ic, JABBER_OAUTH_HANDLE, NULL); |
---|
| 482 | } |
---|
[4a5d885] | 483 | return FALSE; |
---|
| 484 | } |
---|
| 485 | |
---|
[5ebff60] | 486 | static void sasl_oauth2_got_token(gpointer data, const char *access_token, const char *refresh_token, |
---|
| 487 | const char *error); |
---|
[4a5d885] | 488 | |
---|
[5ebff60] | 489 | int sasl_oauth2_get_refresh_token(struct im_connection *ic, const char *msg) |
---|
[4a5d885] | 490 | { |
---|
[18c6d36] | 491 | struct jabber_data *jd = ic->proto_data; |
---|
[4a5d885] | 492 | char *code; |
---|
| 493 | int ret; |
---|
[5ebff60] | 494 | |
---|
| 495 | imcb_log(ic, "Requesting OAuth access token"); |
---|
| 496 | |
---|
[4a5d885] | 497 | /* Don't do it here because the caller may get confused if the contact |
---|
| 498 | we're currently sending a message to is deleted. */ |
---|
[5ebff60] | 499 | b_timeout_add(1, sasl_oauth2_remove_contact, ic); |
---|
| 500 | |
---|
| 501 | code = g_strdup(msg); |
---|
| 502 | g_strstrip(code); |
---|
| 503 | ret = oauth2_access_token(jd->oauth2_service, OAUTH2_AUTH_CODE, |
---|
| 504 | code, sasl_oauth2_got_token, ic); |
---|
| 505 | |
---|
| 506 | g_free(code); |
---|
[4a5d885] | 507 | return ret; |
---|
| 508 | } |
---|
| 509 | |
---|
[5ebff60] | 510 | int sasl_oauth2_refresh(struct im_connection *ic, const char *refresh_token) |
---|
[4a5d885] | 511 | { |
---|
[18c6d36] | 512 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 513 | |
---|
| 514 | return oauth2_access_token(jd->oauth2_service, OAUTH2_AUTH_REFRESH, |
---|
| 515 | refresh_token, sasl_oauth2_got_token, ic); |
---|
[4a5d885] | 516 | } |
---|
| 517 | |
---|
[5ebff60] | 518 | static void sasl_oauth2_got_token(gpointer data, const char *access_token, const char *refresh_token, const char *error) |
---|
[4a5d885] | 519 | { |
---|
| 520 | struct im_connection *ic = data; |
---|
| 521 | struct jabber_data *jd; |
---|
[36533bf] | 522 | GSList *auth = NULL; |
---|
[5ebff60] | 523 | |
---|
| 524 | if (g_slist_find(jabber_connections, ic) == NULL) { |
---|
[4a5d885] | 525 | return; |
---|
[5ebff60] | 526 | } |
---|
| 527 | |
---|
[4a5d885] | 528 | jd = ic->proto_data; |
---|
[5ebff60] | 529 | |
---|
| 530 | if (access_token == NULL) { |
---|
| 531 | imcb_error(ic, "OAuth failure (%s)", error); |
---|
| 532 | imc_logout(ic, TRUE); |
---|
[e1c926f] | 533 | return; |
---|
[4a5d885] | 534 | } |
---|
[5ebff60] | 535 | |
---|
| 536 | oauth_params_parse(&auth, ic->acc->pass); |
---|
| 537 | if (refresh_token) { |
---|
| 538 | oauth_params_set(&auth, "refresh_token", refresh_token); |
---|
| 539 | } |
---|
| 540 | if (access_token) { |
---|
| 541 | oauth_params_set(&auth, "access_token", access_token); |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | g_free(ic->acc->pass); |
---|
| 545 | ic->acc->pass = oauth_params_string(auth); |
---|
| 546 | oauth_params_free(&auth); |
---|
| 547 | |
---|
| 548 | g_free(jd->oauth2_access_token); |
---|
| 549 | jd->oauth2_access_token = g_strdup(access_token); |
---|
| 550 | |
---|
| 551 | jabber_connect(ic); |
---|
[4a5d885] | 552 | } |
---|