source: libfaim/auth.c @ 8c46404

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