source: protocols/oscar/auth.c @ 5447c59

Last change on this file since 5447c59 was e88fe7da, checked in by Veres Lajos <vlajos@…>, at 2015-08-07T21:53:25Z

typofix - https://github.com/vlajos/misspell_fixer

  • Property mode set to 100644
File size: 15.1 KB
RevLine 
[b7d3cc34]1/*
2 * Deals with the authorizer (group 0x0017=23, and old-style non-SNAC login).
3 *
4 */
5
[5ebff60]6#include <aim.h>
[b7d3cc34]7
8#include "md5.h"
9
10static int aim_encode_password(const char *password, unsigned char *encoded);
11
[5ebff60]12/*
[b7d3cc34]13 * This just pushes the passed cookie onto the passed connection, without
14 * the SNAC header or any of that.
15 *
16 * Very commonly used, as every connection except auth will require this to
17 * be the first thing you send.
18 *
19 */
20int aim_sendcookie(aim_session_t *sess, aim_conn_t *conn, const guint8 *chipsahoy)
21{
22        aim_frame_t *fr;
23        aim_tlvlist_t *tl = NULL;
24
[5ebff60]25        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x0001, 4 + 2 + 2 + AIM_COOKIELEN))) {
[b7d3cc34]26                return -ENOMEM;
[5ebff60]27        }
[b7d3cc34]28
29        aimbs_put32(&fr->data, 0x00000001);
[5ebff60]30        aim_addtlvtochain_raw(&tl, 0x0006, AIM_COOKIELEN, chipsahoy);
[b7d3cc34]31        aim_writetlvchain(&fr->data, &tl);
32        aim_freetlvchain(&tl);
33
34        aim_tx_enqueue(sess, fr);
35
36        return 0;
37}
38
39/*
40 * Normally the FLAP version is sent as the first few bytes of the cookie,
41 * meaning you generally never call this.
42 *
[e88fe7da]43 * But there are times when something might want it separate. Specifically,
[b7d3cc34]44 * libfaim sends this internally when doing SNAC login.
45 *
46 */
47int aim_sendflapver(aim_session_t *sess, aim_conn_t *conn)
48{
49        aim_frame_t *fr;
50
[5ebff60]51        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x01, 4))) {
[b7d3cc34]52                return -ENOMEM;
[5ebff60]53        }
[b7d3cc34]54
55        aimbs_put32(&fr->data, 0x00000001);
56
57        aim_tx_enqueue(sess, fr);
58
59        return 0;
60}
61
62/*
63 * This is a bit confusing.
64 *
65 * Normal SNAC login goes like this:
66 *   - connect
67 *   - server sends flap version
68 *   - client sends flap version
69 *   - client sends screen name (17/6)
70 *   - server sends hash key (17/7)
71 *   - client sends auth request (17/2 -- aim_send_login)
72 *   - server yells
73 *
74 * XOR login (for ICQ) goes like this:
75 *   - connect
76 *   - server sends flap version
77 *   - client sends auth request which contains flap version (aim_send_login)
78 *   - server yells
79 *
80 * For the client API, we make them implement the most complicated version,
81 * and for the simpler version, we fake it and make it look like the more
82 * complicated process.
83 *
84 * This is done by giving the client a faked key, just so we can convince
85 * them to call aim_send_login right away, which will detect the session
86 * flag that says this is XOR login and ignore the key, sending an ICQ
87 * login request instead of the normal SNAC one.
88 *
89 * As soon as AOL makes ICQ log in the same way as AIM, this is /gone/.
90 *
91 * XXX This may cause problems if the client relies on callbacks only
92 * being called from the context of aim_rxdispatch()...
93 *
94 */
95static int goddamnicq(aim_session_t *sess, aim_conn_t *conn, const char *sn)
96{
97        aim_frame_t fr;
98        aim_rxcallback_t userfunc;
[5ebff60]99
[b7d3cc34]100        sess->flags &= ~AIM_SESS_FLAGS_SNACLOGIN;
101        sess->flags |= AIM_SESS_FLAGS_XORLOGIN;
102
103        fr.conn = conn;
[5ebff60]104
105        if ((userfunc = aim_callhandler(sess, conn, 0x0017, 0x0007))) {
[b7d3cc34]106                userfunc(sess, &fr, "");
[5ebff60]107        }
[b7d3cc34]108
109        return 0;
110}
111
112/*
[5ebff60]113 * In AIM 3.5 protocol, the first stage of login is to request login from the
114 * Authorizer, passing it the screen name for verification.  If the name is
115 * invalid, a 0017/0003 is spit back, with the standard error contents.  If
116 * valid, a 0017/0007 comes back, which is the signal to send it the main
117 * login command (0017/0002).
[b7d3cc34]118 *
119 */
120int aim_request_login(aim_session_t *sess, aim_conn_t *conn, const char *sn)
121{
122        aim_frame_t *fr;
123        aim_snacid_t snacid;
124        aim_tlvlist_t *tl = NULL;
[3dc6d86]125        struct im_connection *ic = sess->aux_data;
[5ebff60]126
127        if (!sess || !conn || !sn) {
[b7d3cc34]128                return -EINVAL;
[5ebff60]129        }
[b7d3cc34]130
[5ebff60]131        if (g_ascii_isdigit(sn[0]) && set_getbool(&ic->acc->set, "old_icq_auth")) {
[b7d3cc34]132                return goddamnicq(sess, conn, sn);
[5ebff60]133        }
[b7d3cc34]134
135        sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
136
137        aim_sendflapver(sess, conn);
138
[5ebff60]139        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10 + 2 + 2 + strlen(sn)))) {
[b7d3cc34]140                return -ENOMEM;
[5ebff60]141        }
[b7d3cc34]142
143        snacid = aim_cachesnac(sess, 0x0017, 0x0006, 0x0000, NULL, 0);
144        aim_putsnac(&fr->data, 0x0017, 0x0006, 0x0000, snacid);
145
[5ebff60]146        aim_addtlvtochain_raw(&tl, 0x0001, strlen(sn), (guint8 *) sn);
[b7d3cc34]147        aim_writetlvchain(&fr->data, &tl);
148        aim_freetlvchain(&tl);
149
150        aim_tx_enqueue(sess, fr);
151
152        return 0;
153}
154
155/*
156 * Part two of the ICQ hack.  Note the ignoring of the key and clientinfo.
157 */
158static int goddamnicq2(aim_session_t *sess, aim_conn_t *conn, const char *sn, const char *password)
159{
[5ebff60]160        static const char clientstr[] = { "ICQ Inc. - Product of ICQ (TM) 2001b.5.17.1.3642.85" };
161        static const char lang[] = { "en" };
162        static const char country[] = { "us" };
[b7d3cc34]163        aim_frame_t *fr;
164        aim_tlvlist_t *tl = NULL;
165        guint8 *password_encoded;
166
[5ebff60]167        if (!(password_encoded = (guint8 *) g_malloc(strlen(password)))) {
[b7d3cc34]168                return -ENOMEM;
[5ebff60]169        }
[b7d3cc34]170
171        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x01, 1152))) {
172                g_free(password_encoded);
173                return -ENOMEM;
174        }
175
176        aim_encode_password(password, password_encoded);
177
178        aimbs_put32(&fr->data, 0x00000001);
[5ebff60]179        aim_addtlvtochain_raw(&tl, 0x0001, strlen(sn), (guint8 *) sn);
[b7d3cc34]180        aim_addtlvtochain_raw(&tl, 0x0002, strlen(password), password_encoded);
[5ebff60]181        aim_addtlvtochain_raw(&tl, 0x0003, strlen(clientstr), (guint8 *) clientstr);
[b7d3cc34]182        aim_addtlvtochain16(&tl, 0x0016, 0x010a); /* cliend ID */
183        aim_addtlvtochain16(&tl, 0x0017, 0x0005); /* major version */
184        aim_addtlvtochain16(&tl, 0x0018, 0x0011); /* minor version */
185        aim_addtlvtochain16(&tl, 0x0019, 0x0001); /* point version */
186        aim_addtlvtochain16(&tl, 0x001a, 0x0e3a); /* build */
187        aim_addtlvtochain32(&tl, 0x0014, 0x00000055); /* distribution chan */
[5ebff60]188        aim_addtlvtochain_raw(&tl, 0x000f, strlen(lang), (guint8 *) lang);
189        aim_addtlvtochain_raw(&tl, 0x000e, strlen(country), (guint8 *) country);
[b7d3cc34]190
191        aim_writetlvchain(&fr->data, &tl);
192
193        g_free(password_encoded);
194        aim_freetlvchain(&tl);
195
196        aim_tx_enqueue(sess, fr);
197
198        return 0;
199}
200
201/*
202 * send_login(int socket, char *sn, char *password)
[5ebff60]203 *
[b7d3cc34]204 * This is the initial login request packet.
205 *
206 * NOTE!! If you want/need to make use of the aim_sendmemblock() function,
207 * then the client information you send here must exactly match the
208 * executable that you're pulling the data from.
209 *
210 * WinAIM 4.8.2540
211 *   clientstring = "AOL Instant Messenger (SM), version 4.8.2540/WIN32"
212 *   clientid = 0x0109
213 *   major = 0x0004
214 *   minor = 0x0008
215 *   point = 0x0000
216 *   build = 0x09ec
217 *   t(0x0014) = 0x000000af
218 *   t(0x004a) = 0x01
219 *
220 * WinAIM 4.3.2188:
221 *   clientstring = "AOL Instant Messenger (SM), version 4.3.2188/WIN32"
222 *   clientid = 0x0109
223 *   major = 0x0400
224 *   minor = 0x0003
225 *   point = 0x0000
226 *   build = 0x088c
227 *   unknown = 0x00000086
228 *   lang = "en"
229 *   country = "us"
230 *   unknown4a = 0x01
231 *
232 * Latest WinAIM that libfaim can emulate without server-side buddylists:
233 *   clientstring = "AOL Instant Messenger (SM), version 4.1.2010/WIN32"
234 *   clientid = 0x0004
235 *   major  = 0x0004
236 *   minor  = 0x0001
237 *   point = 0x0000
238 *   build  = 0x07da
239 *   unknown= 0x0000004b
240 *
241 * WinAIM 3.5.1670:
242 *   clientstring = "AOL Instant Messenger (SM), version 3.5.1670/WIN32"
243 *   clientid = 0x0004
244 *   major =  0x0003
245 *   minor =  0x0005
246 *   point = 0x0000
247 *   build =  0x0686
248 *   unknown =0x0000002a
249 *
250 * Java AIM 1.1.19:
251 *   clientstring = "AOL Instant Messenger (TM) version 1.1.19 for Java built 03/24/98, freeMem 215871 totalMem 1048567, i686, Linus, #2 SMP Sun Feb 11 03:41:17 UTC 2001 2.4.1-ac9, IBM Corporation, 1.1.8, 45.3, Tue Mar 27 12:09:17 PST 2001"
252 *   clientid = 0x0001
253 *   major  = 0x0001
254 *   minor  = 0x0001
255 *   point = (not sent)
256 *   build  = 0x0013
257 *   unknown= (not sent)
[5ebff60]258 *
[b7d3cc34]259 * AIM for Linux 1.1.112:
260 *   clientstring = "AOL Instant Messenger (SM)"
261 *   clientid = 0x1d09
262 *   major  = 0x0001
263 *   minor  = 0x0001
264 *   point = 0x0001
265 *   build  = 0x0070
266 *   unknown= 0x0000008b
267 *   serverstore = 0x01
268 *
269 */
[5ebff60]270int aim_send_login(aim_session_t *sess, aim_conn_t *conn, const char *sn, const char *password,
271                   struct client_info_s *ci, const char *key)
[b7d3cc34]272{
273        aim_frame_t *fr;
274        aim_tlvlist_t *tl = NULL;
275        guint8 digest[16];
276        aim_snacid_t snacid;
277
[5ebff60]278        if (!ci || !sn || !password) {
[b7d3cc34]279                return -EINVAL;
[5ebff60]280        }
[b7d3cc34]281
282        /*
283         * What the XORLOGIN flag _really_ means is that its an ICQ login,
284         * which is really stupid and painful, so its not done here.
285         *
286         */
[5ebff60]287        if (sess->flags & AIM_SESS_FLAGS_XORLOGIN) {
[b7d3cc34]288                return goddamnicq2(sess, conn, sn, password);
[5ebff60]289        }
[b7d3cc34]290
291
[5ebff60]292        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152))) {
[b7d3cc34]293                return -ENOMEM;
[5ebff60]294        }
[b7d3cc34]295
296        snacid = aim_cachesnac(sess, 0x0017, 0x0002, 0x0000, NULL, 0);
297        aim_putsnac(&fr->data, 0x0017, 0x0002, 0x0000, snacid);
298
[5ebff60]299        aim_addtlvtochain_raw(&tl, 0x0001, strlen(sn), (guint8 *) sn);
[b7d3cc34]300
301        aim_encode_password_md5(password, key, digest);
302        aim_addtlvtochain_raw(&tl, 0x0025, 16, digest);
303
304        /*
305         * Newer versions of winaim have an empty type x004c TLV here.
306         */
307
[5ebff60]308        if (ci->clientstring) {
309                aim_addtlvtochain_raw(&tl, 0x0003, strlen(ci->clientstring), (guint8 *) ci->clientstring);
310        }
311        aim_addtlvtochain16(&tl, 0x0016, (guint16) ci->clientid);
312        aim_addtlvtochain16(&tl, 0x0017, (guint16) ci->major);
313        aim_addtlvtochain16(&tl, 0x0018, (guint16) ci->minor);
314        aim_addtlvtochain16(&tl, 0x0019, (guint16) ci->point);
315        aim_addtlvtochain16(&tl, 0x001a, (guint16) ci->build);
316        aim_addtlvtochain_raw(&tl, 0x000e, strlen(ci->country), (guint8 *) ci->country);
317        aim_addtlvtochain_raw(&tl, 0x000f, strlen(ci->lang), (guint8 *) ci->lang);
[b7d3cc34]318
319        /*
320         * If set, old-fashioned buddy lists will not work. You will need
321         * to use SSI.
322         */
323        aim_addtlvtochain8(&tl, 0x004a, 0x01);
324
325        aim_writetlvchain(&fr->data, &tl);
326
327        aim_freetlvchain(&tl);
[5ebff60]328
[b7d3cc34]329        aim_tx_enqueue(sess, fr);
330
331        return 0;
332}
333
334int aim_encode_password_md5(const char *password, const char *key, guint8 *digest)
335{
336        md5_state_t state;
337
[5ebff60]338        md5_init(&state);
339        md5_append(&state, (const md5_byte_t *) key, strlen(key));
340        md5_append(&state, (const md5_byte_t *) password, strlen(password));
341        md5_append(&state, (const md5_byte_t *) AIM_MD5_STRING, strlen(AIM_MD5_STRING));
342        md5_finish(&state, (md5_byte_t *) digest);
[b7d3cc34]343
344        return 0;
345}
346
347/**
348 * aim_encode_password - Encode a password using old XOR method
349 * @password: incoming password
350 * @encoded: buffer to put encoded password
351 *
352 * This takes a const pointer to a (null terminated) string
353 * containing the unencoded password.  It also gets passed
354 * an already allocated buffer to store the encoded password.
355 * This buffer should be the exact length of the password without
356 * the null.  The encoded password buffer /is not %NULL terminated/.
357 *
358 * The encoding_table seems to be a fixed set of values.  We'll
[5ebff60]359 * hope it doesn't change over time!
[b7d3cc34]360 *
361 * This is only used for the XOR method, not the better MD5 method.
362 *
363 */
364static int aim_encode_password(const char *password, guint8 *encoded)
365{
366        guint8 encoding_table[] = {
[5ebff60]367                /* v2.1 table, also works for ICQ */
[b7d3cc34]368                0xf3, 0x26, 0x81, 0xc4,
369                0x39, 0x86, 0xdb, 0x92,
370                0x71, 0xa3, 0xb9, 0xe6,
371                0x53, 0x7a, 0x95, 0x7c
372        };
373        int i;
374
[5ebff60]375        for (i = 0; i < strlen(password); i++) {
[b7d3cc34]376                encoded[i] = (password[i] ^ encoding_table[i]);
[5ebff60]377        }
[b7d3cc34]378
379        return 0;
380}
381
382/*
383 * This is sent back as a general response to the login command.
384 * It can be either an error or a success, depending on the
[5ebff60]385 * precense of certain TLVs.
[b7d3cc34]386 *
387 * The client should check the value passed as errorcode. If
388 * its nonzero, there was an error.
389 *
390 */
391static int parse(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
392{
393        aim_tlvlist_t *tlvlist;
394        aim_rxcallback_t userfunc;
395        struct aim_authresp_info info;
396        int ret = 0;
397
398        memset(&info, 0, sizeof(info));
399
400        /*
401         * Read block of TLVs.  All further data is derived
402         * from what is parsed here.
403         */
404        tlvlist = aim_readtlvchain(bs);
405
406        /*
407         * No matter what, we should have a screen name.
408         */
409        memset(sess->sn, 0, sizeof(sess->sn));
410        if (aim_gettlv(tlvlist, 0x0001, 1)) {
411                info.sn = aim_gettlv_str(tlvlist, 0x0001, 1);
412                strncpy(sess->sn, info.sn, sizeof(sess->sn));
413        }
414
415        /*
416         * Check for an error code.  If so, we should also
417         * have an error url.
418         */
[5ebff60]419        if (aim_gettlv(tlvlist, 0x0008, 1)) {
[b7d3cc34]420                info.errorcode = aim_gettlv16(tlvlist, 0x0008, 1);
[5ebff60]421        }
422        if (aim_gettlv(tlvlist, 0x0004, 1)) {
[b7d3cc34]423                info.errorurl = aim_gettlv_str(tlvlist, 0x0004, 1);
[5ebff60]424        }
[b7d3cc34]425
426        /*
427         * BOS server address.
428         */
[5ebff60]429        if (aim_gettlv(tlvlist, 0x0005, 1)) {
[b7d3cc34]430                info.bosip = aim_gettlv_str(tlvlist, 0x0005, 1);
[5ebff60]431        }
[b7d3cc34]432
433        /*
434         * Authorization cookie.
435         */
436        if (aim_gettlv(tlvlist, 0x0006, 1)) {
437                aim_tlv_t *tmptlv;
438
439                tmptlv = aim_gettlv(tlvlist, 0x0006, 1);
440
441                info.cookie = tmptlv->value;
442        }
443
444        /*
445         * The email address attached to this account
446         *   Not available for ICQ logins.
447         */
[5ebff60]448        if (aim_gettlv(tlvlist, 0x0011, 1)) {
[b7d3cc34]449                info.email = aim_gettlv_str(tlvlist, 0x0011, 1);
[5ebff60]450        }
[b7d3cc34]451
452        /*
453         * The registration status.  (Not real sure what it means.)
454         *   Not available for ICQ logins.
455         *
456         *   1 = No disclosure
457         *   2 = Limited disclosure
458         *   3 = Full disclosure
459         *
460         * This has to do with whether your email address is available
461         * to other users or not.  AFAIK, this feature is no longer used.
462         *
463         */
[5ebff60]464        if (aim_gettlv(tlvlist, 0x0013, 1)) {
[b7d3cc34]465                info.regstatus = aim_gettlv16(tlvlist, 0x0013, 1);
[5ebff60]466        }
[b7d3cc34]467
[5ebff60]468        if (aim_gettlv(tlvlist, 0x0040, 1)) {
[b7d3cc34]469                info.latestbeta.build = aim_gettlv32(tlvlist, 0x0040, 1);
[5ebff60]470        }
471        if (aim_gettlv(tlvlist, 0x0041, 1)) {
[b7d3cc34]472                info.latestbeta.url = aim_gettlv_str(tlvlist, 0x0041, 1);
[5ebff60]473        }
474        if (aim_gettlv(tlvlist, 0x0042, 1)) {
[b7d3cc34]475                info.latestbeta.info = aim_gettlv_str(tlvlist, 0x0042, 1);
[5ebff60]476        }
477        if (aim_gettlv(tlvlist, 0x0043, 1)) {
[b7d3cc34]478                info.latestbeta.name = aim_gettlv_str(tlvlist, 0x0043, 1);
[5ebff60]479        }
480        if (aim_gettlv(tlvlist, 0x0048, 1)) {
[b7d3cc34]481                ; /* no idea what this is */
482
[5ebff60]483        }
484        if (aim_gettlv(tlvlist, 0x0044, 1)) {
[b7d3cc34]485                info.latestrelease.build = aim_gettlv32(tlvlist, 0x0044, 1);
[5ebff60]486        }
487        if (aim_gettlv(tlvlist, 0x0045, 1)) {
[b7d3cc34]488                info.latestrelease.url = aim_gettlv_str(tlvlist, 0x0045, 1);
[5ebff60]489        }
490        if (aim_gettlv(tlvlist, 0x0046, 1)) {
[b7d3cc34]491                info.latestrelease.info = aim_gettlv_str(tlvlist, 0x0046, 1);
[5ebff60]492        }
493        if (aim_gettlv(tlvlist, 0x0047, 1)) {
[b7d3cc34]494                info.latestrelease.name = aim_gettlv_str(tlvlist, 0x0047, 1);
[5ebff60]495        }
496        if (aim_gettlv(tlvlist, 0x0049, 1)) {
[b7d3cc34]497                ; /* no idea what this is */
498
499
[5ebff60]500        }
501        if ((userfunc = aim_callhandler(sess, rx->conn, snac ? snac->family : 0x0017, snac ? snac->subtype : 0x0003))) {
[b7d3cc34]502                ret = userfunc(sess, rx, &info);
[5ebff60]503        }
[b7d3cc34]504
505        g_free(info.sn);
506        g_free(info.bosip);
507        g_free(info.errorurl);
508        g_free(info.email);
509        g_free(info.latestrelease.name);
510        g_free(info.latestrelease.url);
511        g_free(info.latestrelease.info);
512        g_free(info.latestbeta.name);
513        g_free(info.latestbeta.url);
514        g_free(info.latestbeta.info);
515
516        aim_freetlvchain(&tlvlist);
517
518        return ret;
519}
520
521/*
522 * Middle handler for 0017/0007 SNACs.  Contains the auth key prefixed
523 * by only its length in a two byte word.
524 *
525 * Calls the client, which should then use the value to call aim_send_login.
526 *
527 */
528static int keyparse(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
529{
530        int keylen, ret = 1;
531        aim_rxcallback_t userfunc;
532        char *keystr;
533
534        keylen = aimbs_get16(bs);
535        keystr = aimbs_getstr(bs, keylen);
536
[5ebff60]537        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) {
[b7d3cc34]538                ret = userfunc(sess, rx, keystr);
[5ebff60]539        }
[b7d3cc34]540
[5ebff60]541        g_free(keystr);
[b7d3cc34]542
543        return ret;
544}
545
546static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
547{
548
[5ebff60]549        if (snac->subtype == 0x0003) {
[b7d3cc34]550                return parse(sess, mod, rx, snac, bs);
[5ebff60]551        } else if (snac->subtype == 0x0007) {
[b7d3cc34]552                return keyparse(sess, mod, rx, snac, bs);
[5ebff60]553        }
[b7d3cc34]554
555        return 0;
556}
557
558int auth_modfirst(aim_session_t *sess, aim_module_t *mod)
559{
560
561        mod->family = 0x0017;
562        mod->version = 0x0000;
563        mod->flags = 0;
564        strncpy(mod->name, "auth", sizeof(mod->name));
565        mod->snachandler = snachandler;
566
567        return 0;
568}
569
Note: See TracBrowser for help on using the repository browser.