]> andersk Git - libfaim.git/blob - aim_login.c
10e1f792fd3a11fed166937bcbaf0e1d410ddde8
[libfaim.git] / aim_login.c
1 /*
2  *  aim_login.c
3  *
4  *  This contains all the functions needed to actually login.
5  *
6  */
7
8 #define FAIM_INTERNAL
9 #include <faim/aim.h>
10
11 #include "md5.h"
12
13 static int aim_encode_password_md5(const char *password, const char *key, md5_byte_t *digest);
14 static int aim_encode_password(const char *password, unsigned char *encoded);
15
16 /*
17  * FIXME: Reimplement the TIS stuff.
18  */
19 #ifdef TIS_TELNET_PROXY
20 #include "tis_telnet_proxy.h"
21 #endif
22
23 faim_export int aim_sendconnack(struct aim_session_t *sess,
24                                 struct aim_conn_t *conn)
25 {
26   int curbyte=0;
27   
28   struct command_tx_struct *newpacket;
29
30   if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0001, conn, 4)))
31     return -1;
32
33   newpacket->lock = 1;
34   
35   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
36   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
37
38   newpacket->lock = 0;
39   return aim_tx_enqueue(sess, newpacket);
40 }
41
42 /*
43  * In AIM 3.5 protocol, the first stage of login is to request
44  * login from the Authorizer, passing it the screen name
45  * for verification.  If the name is invalid, a 0017/0003 
46  * is spit back, with the standard error contents.  If valid,
47  * a 0017/0007 comes back, which is the signal to send
48  * it the main login command (0017/0002).  
49  */
50 faim_export int aim_request_login(struct aim_session_t *sess,
51                                   struct aim_conn_t *conn, 
52                                   char *sn)
53 {
54   int curbyte;
55   struct command_tx_struct *newpacket;
56
57   if (!sess || !conn || !sn)
58     return -1;
59
60   /*
61    * For ICQ, we enable the ancient horrible login and stuff
62    * a key packet into the queue to make it look like we got
63    * a reply back. This is so the client doesn't know we're
64    * really not doing MD5 login.
65    *
66    * This may sound stupid, but I'm not in the best of moods and 
67    * I don't plan to keep support for this crap around much longer.
68    * Its all AOL's fault anyway, really. I hate AOL.  Really.  They
69    * always seem to be able to piss me off by doing the dumbest little
70    * things.  Like disabling MD5 logins for ICQ UINs, or adding purposefully
71    * wrong TLV lengths, or adding superfluous information to host strings,
72    * or... I'll stop.
73    *
74    */
75   if ((sn[0] >= '0') && (sn[0] <= '9')) {
76     struct command_rx_struct *newrx;
77     int i;
78
79     if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct))))
80       return -1;
81     memset(newrx, 0x00, sizeof(struct command_rx_struct));
82     newrx->lock = 1; 
83     newrx->hdrtype = AIM_FRAMETYPE_OSCAR;
84     newrx->hdr.oscar.type = 0x02;
85     newrx->hdr.oscar.seqnum = 0;
86     newrx->commandlen = 10+2+1;
87     newrx->nofree = 0; 
88     if (!(newrx->data = malloc(newrx->commandlen))) {
89       free(newrx);
90       return -1;
91     }
92
93     i = aim_putsnac(newrx->data, 0x0017, 0x0007, 0x0000, 0x0000);
94     i += aimutil_put16(newrx->data+i, 0x01);
95     i += aimutil_putstr(newrx->data+i, "0", 1);
96
97     newrx->conn = conn;
98
99     newrx->next = sess->queue_incoming;
100     sess->queue_incoming = newrx;
101
102     newrx->lock = 0;
103
104     sess->flags &= ~AIM_SESS_FLAGS_SNACLOGIN;
105
106     return 0;
107   } 
108
109   sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
110
111   aim_sendconnack(sess, conn);
112
113   if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+2+2+strlen(sn))))
114     return -1;
115
116   newpacket->lock = 1;
117   
118   curbyte  = aim_putsnac(newpacket->data, 0x0017, 0x0006, 0x0000, 0x00010000);
119   curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
120
121   newpacket->commandlen = curbyte;
122   newpacket->lock = 0;
123
124   return aim_tx_enqueue(sess, newpacket);
125 }
126
127 /*
128  * send_login(int socket, char *sn, char *password)
129  *  
130  * This is the initial login request packet.
131  *
132  * The password is encoded before transmition, as per
133  * encode_password().  See that function for their
134  * stupid method of doing it.
135  *
136  * Latest WinAIM:
137  *   clientstring = "AOL Instant Messenger (SM), version 4.3.2188/WIN32"
138  *   major2 = 0x0109
139  *   major = 0x0400
140  *   minor = 0x0003
141  *   minor2 = 0x0000
142  *   build = 0x088c
143  *   unknown = 0x00000086
144  *   lang = "en"
145  *   country = "us"
146  *   unknown4a = 0x01
147  */
148 faim_export int aim_send_login (struct aim_session_t *sess,
149                                 struct aim_conn_t *conn, 
150                                 char *sn, char *password, 
151                                 struct client_info_s *clientinfo,
152                                 char *key)
153 {
154   int curbyte=0;
155   struct command_tx_struct *newpacket;
156
157   if (!clientinfo || !sn || !password)
158     return -1;
159
160   if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
161     return -1;
162
163   newpacket->lock = 1;
164
165   newpacket->hdr.oscar.type = (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)?0x02:0x01;
166   
167   if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)
168     curbyte = aim_putsnac(newpacket->data, 0x0017, 0x0002, 0x0000, 0x00010000);
169   else {
170     curbyte  = aimutil_put16(newpacket->data, 0x0000);
171     curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
172   }
173
174   curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
175   
176   if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN) {
177     md5_byte_t digest[16];
178
179     aim_encode_password_md5(password, key, digest);
180     curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0025, 16, (char *)digest);
181   } else { 
182     char *password_encoded;
183
184     password_encoded = (char *) malloc(strlen(password));
185     aim_encode_password(password, password_encoded);
186     curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0002, strlen(password), password_encoded);
187     free(password_encoded);
188   }
189
190   /* XXX is clientstring required by oscar? */
191   if (strlen(clientinfo->clientstring))
192     curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0003, strlen(clientinfo->clientstring), clientinfo->clientstring);
193
194   if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN) {
195     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0016, (unsigned short)clientinfo->major2);
196     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0017, (unsigned short)clientinfo->major);
197     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0018, (unsigned short)clientinfo->minor);
198     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0019, (unsigned short)clientinfo->minor2);
199     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x001a, (unsigned short)clientinfo->build);
200   
201     curbyte += aim_puttlv_32(newpacket->data+curbyte, 0x0014, clientinfo->unknown);
202     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0009, 0x0015);
203     curbyte += aim_puttlv_8(newpacket->data+curbyte, 0x004a, 0x00);
204   } else {
205     /* Use very specific version numbers, to further indicate the hack. */
206     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0016, 0x010a);
207     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0017, 0x0004);
208     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0018, 0x003c);
209     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0019, 0x0001);
210     curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x001a, 0x0cce);
211     curbyte += aim_puttlv_32(newpacket->data+curbyte, 0x0014, 0x00000055);
212   }
213
214   if (strlen(clientinfo->country))
215     curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000e, strlen(clientinfo->country), clientinfo->country);
216   else
217     curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000e, 2, "us");
218
219   if (strlen(clientinfo->lang))
220     curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000f, strlen(clientinfo->lang), clientinfo->lang);
221   else
222     curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000f, 2, "en");
223   
224   newpacket->commandlen = curbyte;
225
226   newpacket->lock = 0;
227   return aim_tx_enqueue(sess, newpacket);
228 }
229
230 static int aim_encode_password_md5(const char *password, const char *key, md5_byte_t *digest)
231 {
232   md5_state_t state;
233
234   md5_init(&state);     
235   md5_append(&state, (const md5_byte_t *)key, strlen(key));
236   md5_append(&state, (const md5_byte_t *)password, strlen(password));
237   md5_append(&state, (const md5_byte_t *)AIM_MD5_STRING, strlen(AIM_MD5_STRING));
238   md5_finish(&state, (md5_byte_t *)digest);
239
240   return 0;
241 }
242
243 /**
244  * aim_encode_password - Encode a password using old XOR method
245  * @password: incoming password
246  * @encoded: buffer to put encoded password
247  *
248  * This takes a const pointer to a (null terminated) string
249  * containing the unencoded password.  It also gets passed
250  * an already allocated buffer to store the encoded password.
251  * This buffer should be the exact length of the password without
252  * the null.  The encoded password buffer /is not %NULL terminated/.
253  *
254  * The encoding_table seems to be a fixed set of values.  We'll
255  * hope it doesn't change over time!  
256  *
257  * This is only used for the XOR method, not the better MD5 method.
258  *
259  */
260 static int aim_encode_password(const char *password, unsigned char *encoded)
261 {
262   u_char encoding_table[] = {
263 #if 0 /* old v1 table */
264     0xf3, 0xb3, 0x6c, 0x99,
265     0x95, 0x3f, 0xac, 0xb6,
266     0xc5, 0xfa, 0x6b, 0x63,
267     0x69, 0x6c, 0xc3, 0x9f
268 #else /* v2.1 table, also works for ICQ */
269     0xf3, 0x26, 0x81, 0xc4,
270     0x39, 0x86, 0xdb, 0x92,
271     0x71, 0xa3, 0xb9, 0xe6,
272     0x53, 0x7a, 0x95, 0x7c
273 #endif
274   };
275
276   int i;
277   
278   for (i = 0; i < strlen(password); i++)
279       encoded[i] = (password[i] ^ encoding_table[i]);
280
281   return 0;
282 }
283
284 /*
285  * This is sent back as a general response to the login command.
286  * It can be either an error or a success, depending on the
287  * precense of certain TLVs.  
288  *
289  * The client should check the value passed as errorcode. If
290  * its nonzero, there was an error.
291  *
292  */
293 faim_internal int aim_authparse(struct aim_session_t *sess, 
294                                 struct command_rx_struct *command)
295 {
296   struct aim_tlvlist_t *tlvlist;
297   int ret = 1;
298   rxcallback_t userfunc = NULL;
299   char *sn = NULL, *bosip = NULL, *errurl = NULL, *email = NULL;
300   unsigned char *cookie = NULL;
301   int errorcode = 0, regstatus = 0;
302   int latestbuild = 0, latestbetabuild = 0;
303   char *latestrelease = NULL, *latestbeta = NULL;
304   char *latestreleaseurl = NULL, *latestbetaurl = NULL;
305   char *latestreleaseinfo = NULL, *latestbetainfo = NULL;
306
307   /*
308    * Read block of TLVs.  All further data is derived
309    * from what is parsed here.
310    *
311    * For SNAC login, there's a 17/3 SNAC header in front.
312    *
313    */
314   if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)
315     tlvlist = aim_readtlvchain(command->data+10, command->commandlen-10);
316   else
317     tlvlist = aim_readtlvchain(command->data, command->commandlen);
318
319   /*
320    * No matter what, we should have a screen name.
321    */
322   memset(sess->sn, 0, sizeof(sess->sn));
323   if (aim_gettlv(tlvlist, 0x0001, 1)) {
324     sn = aim_gettlv_str(tlvlist, 0x0001, 1);
325     strncpy(sess->sn, sn, sizeof(sess->sn));
326   }
327
328   /*
329    * Check for an error code.  If so, we should also
330    * have an error url.
331    */
332   if (aim_gettlv(tlvlist, 0x0008, 1)) 
333     errorcode = aim_gettlv16(tlvlist, 0x0008, 1);
334   if (aim_gettlv(tlvlist, 0x0004, 1))
335     errurl = aim_gettlv_str(tlvlist, 0x0004, 1);
336
337   /*
338    * BOS server address.
339    */
340   if (aim_gettlv(tlvlist, 0x0005, 1))
341     bosip = aim_gettlv_str(tlvlist, 0x0005, 1);
342
343   /*
344    * Authorization cookie.
345    */
346   if (aim_gettlv(tlvlist, 0x0006, 1)) {
347     struct aim_tlv_t *tmptlv;
348
349     tmptlv = aim_gettlv(tlvlist, 0x0006, 1);
350
351     if ((cookie = malloc(tmptlv->length)))
352       memcpy(cookie, tmptlv->value, tmptlv->length);
353   }
354
355   /*
356    * The email address attached to this account
357    *   Not available for ICQ logins.
358    */
359   if (aim_gettlv(tlvlist, 0x0011, 1))
360     email = aim_gettlv_str(tlvlist, 0x0011, 1);
361
362   /*
363    * The registration status.  (Not real sure what it means.)
364    *   Not available for ICQ logins.
365    *
366    *   1 = No disclosure
367    *   2 = Limited disclosure
368    *   3 = Full disclosure
369    *
370    * This has to do with whether your email address is available
371    * to other users or not.  AFAIK, this feature is no longer used.
372    *
373    */
374   if (aim_gettlv(tlvlist, 0x0013, 1))
375     regstatus = aim_gettlv16(tlvlist, 0x0013, 1);
376
377   if (aim_gettlv(tlvlist, 0x0040, 1))
378     latestbetabuild = aim_gettlv32(tlvlist, 0x0040, 1);
379   if (aim_gettlv(tlvlist, 0x0041, 1))
380     latestbetaurl = aim_gettlv_str(tlvlist, 0x0041, 1);
381   if (aim_gettlv(tlvlist, 0x0042, 1))
382     latestbetainfo = aim_gettlv_str(tlvlist, 0x0042, 1);
383   if (aim_gettlv(tlvlist, 0x0043, 1))
384     latestbeta = aim_gettlv_str(tlvlist, 0x0043, 1);
385   if (aim_gettlv(tlvlist, 0x0048, 1))
386     ; /* no idea what this is */
387
388   if (aim_gettlv(tlvlist, 0x0044, 1))
389     latestbuild = aim_gettlv32(tlvlist, 0x0044, 1);
390   if (aim_gettlv(tlvlist, 0x0045, 1))
391     latestreleaseurl = aim_gettlv_str(tlvlist, 0x0045, 1);
392   if (aim_gettlv(tlvlist, 0x0046, 1))
393     latestreleaseinfo = aim_gettlv_str(tlvlist, 0x0046, 1);
394   if (aim_gettlv(tlvlist, 0x0047, 1))
395     latestrelease = aim_gettlv_str(tlvlist, 0x0047, 1);
396   if (aim_gettlv(tlvlist, 0x0049, 1))
397     ; /* no idea what this is */
398
399
400   if ((userfunc = aim_callhandler(command->conn, 0x0017, 0x0003)))
401     ret = userfunc(sess, command, sn, errorcode, errurl, regstatus, email, bosip, cookie, latestrelease, latestbuild, latestreleaseurl, latestreleaseinfo, latestbeta, latestbetabuild, latestbetaurl, latestbetainfo);
402
403
404   if (sn)
405     free(sn);
406   if (bosip)
407     free(bosip);
408   if (errurl)
409     free(errurl);
410   if (email)
411     free(email);
412   if (cookie)
413     free(cookie);
414   if (latestrelease)
415     free(latestrelease);
416   if (latestreleaseurl)
417     free(latestreleaseurl);
418   if (latestbeta)
419     free(latestbeta);
420   if (latestbetaurl)
421     free(latestbetaurl);
422   if (latestreleaseinfo)
423     free(latestreleaseinfo);
424   if (latestbetainfo)
425     free(latestbetainfo);
426
427   aim_freetlvchain(&tlvlist);
428
429   return ret;
430 }
431
432 /*
433  * Middle handler for 0017/0007 SNACs.  Contains the auth key prefixed
434  * by only its length in a two byte word.
435  *
436  * Calls the client, which should then use the value to call aim_send_login.
437  *
438  */
439 faim_internal int aim_authkeyparse(struct aim_session_t *sess, struct command_rx_struct *command)
440 {
441   unsigned char *key;
442   int keylen;
443   int ret = 1;
444   rxcallback_t userfunc;
445
446   keylen = aimutil_get16(command->data+10);
447   if (!(key = malloc(keylen+1)))
448     return ret;
449   memcpy(key, command->data+12, keylen);
450   key[keylen] = '\0';
451   
452   if ((userfunc = aim_callhandler(command->conn, 0x0017, 0x0007)))
453     ret = userfunc(sess, command, (char *)key);
454
455   free(key);  
456
457   return ret;
458 }
459
460 /*
461  * Generate an authorization response.  
462  *
463  * You probably don't want this unless you're writing an AIM server.
464  *
465  */
466 faim_export unsigned long aim_sendauthresp(struct aim_session_t *sess, 
467                                            struct aim_conn_t *conn, 
468                                            char *sn, int errorcode,
469                                            char *errorurl, char *bosip, 
470                                            char *cookie, char *email, 
471                                            int regstatus)
472 {       
473   struct command_tx_struct *tx;
474   struct aim_tlvlist_t *tlvlist = NULL;
475
476   if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0004, conn, 1152)))
477     return -1;
478   
479   tx->lock = 1;
480
481   if (sn)
482     aim_addtlvtochain_str(&tlvlist, 0x0001, sn, strlen(sn));
483   else
484     aim_addtlvtochain_str(&tlvlist, 0x0001, sess->sn, strlen(sess->sn));
485
486   if (errorcode) {
487     aim_addtlvtochain16(&tlvlist, 0x0008, errorcode);
488     aim_addtlvtochain_str(&tlvlist, 0x0004, errorurl, strlen(errorurl));
489   } else {
490     aim_addtlvtochain_str(&tlvlist, 0x0005, bosip, strlen(bosip));
491     aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
492     aim_addtlvtochain_str(&tlvlist, 0x0011, email, strlen(email));
493     aim_addtlvtochain16(&tlvlist, 0x0013, (unsigned short)regstatus);
494   }
495
496   tx->commandlen = aim_writetlvchain(tx->data, tx->commandlen, &tlvlist);
497   tx->lock = 0;
498
499   return aim_tx_enqueue(sess, tx);
500 }
501
502 /*
503  * Generate a random cookie.  (Non-client use only)
504  */
505 faim_export int aim_gencookie(unsigned char *buf)
506 {
507   int i;
508
509   srand(time(NULL));
510
511   for (i=0; i < AIM_COOKIELEN; i++)
512     buf[i] = 1+(int) (256.0*rand()/(RAND_MAX+0.0));
513
514   return i;
515 }
516
517 /*
518  * Send Server Ready.  (Non-client)
519  */
520 faim_export int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn)
521 {
522   struct command_tx_struct *tx;
523   int i = 0;
524
525   if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+0x22)))
526     return -1;
527
528   tx->lock = 1;
529
530   i += aim_putsnac(tx->data, 0x0001, 0x0003, 0x0000, sess->snac_nextid++);
531   
532   i += aimutil_put16(tx->data+i, 0x0001);  
533   i += aimutil_put16(tx->data+i, 0x0002);
534   i += aimutil_put16(tx->data+i, 0x0003);
535   i += aimutil_put16(tx->data+i, 0x0004);
536   i += aimutil_put16(tx->data+i, 0x0006);
537   i += aimutil_put16(tx->data+i, 0x0008);
538   i += aimutil_put16(tx->data+i, 0x0009);
539   i += aimutil_put16(tx->data+i, 0x000a);
540   i += aimutil_put16(tx->data+i, 0x000b);
541   i += aimutil_put16(tx->data+i, 0x000c);
542   i += aimutil_put16(tx->data+i, 0x0013);
543   i += aimutil_put16(tx->data+i, 0x0015);
544
545   tx->commandlen = i;
546   tx->lock = 0;
547   return aim_tx_enqueue(sess, tx);
548 }
549
550
551 /* 
552  * Send service redirect.  (Non-Client)
553  */
554 faim_export unsigned long aim_sendredirect(struct aim_session_t *sess, 
555                                            struct aim_conn_t *conn, 
556                                            unsigned short servid, 
557                                            char *ip,
558                                            char *cookie)
559 {       
560   struct command_tx_struct *tx;
561   struct aim_tlvlist_t *tlvlist = NULL;
562   int i = 0;
563
564   if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
565     return -1;
566
567   tx->lock = 1;
568
569   i += aim_putsnac(tx->data+i, 0x0001, 0x0005, 0x0000, 0x00000000);
570   
571   aim_addtlvtochain16(&tlvlist, 0x000d, servid);
572   aim_addtlvtochain_str(&tlvlist, 0x0005, ip, strlen(ip));
573   aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
574
575   tx->commandlen = aim_writetlvchain(tx->data+i, tx->commandlen-i, &tlvlist)+i;
576   aim_freetlvchain(&tlvlist);
577
578   tx->lock = 0;
579   return aim_tx_enqueue(sess, tx);
580 }
This page took 0.076009 seconds and 3 git commands to generate.