source: protocols/oscar/auth.c @ 6b13103

Last change on this file since 6b13103 was 6b13103, checked in by dequis <dx@…>, at 2015-01-16T19:50:23Z

Replace isdigit/isalpha/.../tolower/toupper with glib variants

This fixes warnings about passing signed chars to them (apparently they
are implemented as macros that do array lookups without checks in some
platforms, yay)

Specifically:

functions=isalnum|isalpha|isdigit|isspace|isxdigit|tolower|toupper
sed -ir "s/$functions/g_ascii_&/g" /*.c

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