4 * This contains all the functions needed to actually login.
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);
17 * FIXME: Reimplement the TIS stuff.
19 #ifdef TIS_TELNET_PROXY
20 #include "tis_telnet_proxy.h"
23 faim_export int aim_sendconnack(struct aim_session_t *sess,
24 struct aim_conn_t *conn)
28 struct command_tx_struct *newpacket;
30 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0001, conn, 4)))
35 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
36 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
39 return aim_tx_enqueue(sess, newpacket);
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).
50 faim_export int aim_request_login(struct aim_session_t *sess,
51 struct aim_conn_t *conn,
55 struct command_tx_struct *newpacket;
57 if (!sess || !conn || !sn)
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.
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,
75 if ((sn[0] >= '0') && (sn[0] <= '9')) {
76 struct command_rx_struct *newrx;
79 if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct))))
81 memset(newrx, 0x00, sizeof(struct command_rx_struct));
83 newrx->hdrtype = AIM_FRAMETYPE_OSCAR;
84 newrx->hdr.oscar.type = 0x02;
85 newrx->hdr.oscar.seqnum = 0;
86 newrx->commandlen = 10+2+1;
88 if (!(newrx->data = malloc(newrx->commandlen))) {
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);
99 newrx->next = sess->queue_incoming;
100 sess->queue_incoming = newrx;
104 sess->flags &= ~AIM_SESS_FLAGS_SNACLOGIN;
109 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
111 aim_sendconnack(sess, conn);
113 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+2+2+strlen(sn))))
118 curbyte = aim_putsnac(newpacket->data, 0x0017, 0x0006, 0x0000, 0x00010000);
119 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
121 newpacket->commandlen = curbyte;
124 return aim_tx_enqueue(sess, newpacket);
128 * send_login(int socket, char *sn, char *password)
130 * This is the initial login request packet.
132 * The password is encoded before transmition, as per
133 * encode_password(). See that function for their
134 * stupid method of doing it.
137 * clientstring = "AOL Instant Messenger (SM), version 4.3.2188/WIN32"
143 * unknown = 0x00000086
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,
155 struct command_tx_struct *newpacket;
157 if (!clientinfo || !sn || !password)
160 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
165 newpacket->hdr.oscar.type = (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)?0x02:0x01;
167 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)
168 curbyte = aim_putsnac(newpacket->data, 0x0017, 0x0002, 0x0000, 0x00010000);
170 curbyte = aimutil_put16(newpacket->data, 0x0000);
171 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
174 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
176 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN) {
177 md5_byte_t digest[16];
179 aim_encode_password_md5(password, key, digest);
180 curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0025, 16, (char *)digest);
182 char *password_encoded;
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);
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);
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);
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);
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);
214 if (strlen(clientinfo->country))
215 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000e, strlen(clientinfo->country), clientinfo->country);
217 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000e, 2, "us");
219 if (strlen(clientinfo->lang))
220 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000f, strlen(clientinfo->lang), clientinfo->lang);
222 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000f, 2, "en");
224 newpacket->commandlen = curbyte;
227 return aim_tx_enqueue(sess, newpacket);
230 static int aim_encode_password_md5(const char *password, const char *key, md5_byte_t *digest)
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);
244 * aim_encode_password - Encode a password using old XOR method
245 * @password: incoming password
246 * @encoded: buffer to put encoded password
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/.
254 * The encoding_table seems to be a fixed set of values. We'll
255 * hope it doesn't change over time!
257 * This is only used for the XOR method, not the better MD5 method.
260 static int aim_encode_password(const char *password, unsigned char *encoded)
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
278 for (i = 0; i < strlen(password); i++)
279 encoded[i] = (password[i] ^ encoding_table[i]);
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.
289 * The client should check the value passed as errorcode. If
290 * its nonzero, there was an error.
293 faim_internal int aim_authparse(struct aim_session_t *sess,
294 struct command_rx_struct *command)
296 struct aim_tlvlist_t *tlvlist;
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;
308 * Read block of TLVs. All further data is derived
309 * from what is parsed here.
311 * For SNAC login, there's a 17/3 SNAC header in front.
314 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)
315 tlvlist = aim_readtlvchain(command->data+10, command->commandlen-10);
317 tlvlist = aim_readtlvchain(command->data, command->commandlen);
320 * No matter what, we should have a screen name.
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));
329 * Check for an error code. If so, we should also
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);
338 * BOS server address.
340 if (aim_gettlv(tlvlist, 0x0005, 1))
341 bosip = aim_gettlv_str(tlvlist, 0x0005, 1);
344 * Authorization cookie.
346 if (aim_gettlv(tlvlist, 0x0006, 1)) {
347 struct aim_tlv_t *tmptlv;
349 tmptlv = aim_gettlv(tlvlist, 0x0006, 1);
351 if ((cookie = malloc(tmptlv->length)))
352 memcpy(cookie, tmptlv->value, tmptlv->length);
356 * The email address attached to this account
357 * Not available for ICQ logins.
359 if (aim_gettlv(tlvlist, 0x0011, 1))
360 email = aim_gettlv_str(tlvlist, 0x0011, 1);
363 * The registration status. (Not real sure what it means.)
364 * Not available for ICQ logins.
367 * 2 = Limited disclosure
368 * 3 = Full disclosure
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.
374 if (aim_gettlv(tlvlist, 0x0013, 1))
375 regstatus = aim_gettlv16(tlvlist, 0x0013, 1);
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 */
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 */
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);
416 if (latestreleaseurl)
417 free(latestreleaseurl);
422 if (latestreleaseinfo)
423 free(latestreleaseinfo);
425 free(latestbetainfo);
427 aim_freetlvchain(&tlvlist);
433 * Middle handler for 0017/0007 SNACs. Contains the auth key prefixed
434 * by only its length in a two byte word.
436 * Calls the client, which should then use the value to call aim_send_login.
439 faim_internal int aim_authkeyparse(struct aim_session_t *sess, struct command_rx_struct *command)
444 rxcallback_t userfunc;
446 keylen = aimutil_get16(command->data+10);
447 if (!(key = malloc(keylen+1)))
449 memcpy(key, command->data+12, keylen);
452 if ((userfunc = aim_callhandler(command->conn, 0x0017, 0x0007)))
453 ret = userfunc(sess, command, (char *)key);
461 * Generate an authorization response.
463 * You probably don't want this unless you're writing an AIM server.
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,
473 struct command_tx_struct *tx;
474 struct aim_tlvlist_t *tlvlist = NULL;
476 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0004, conn, 1152)))
482 aim_addtlvtochain_str(&tlvlist, 0x0001, sn, strlen(sn));
484 aim_addtlvtochain_str(&tlvlist, 0x0001, sess->sn, strlen(sess->sn));
487 aim_addtlvtochain16(&tlvlist, 0x0008, errorcode);
488 aim_addtlvtochain_str(&tlvlist, 0x0004, errorurl, strlen(errorurl));
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);
496 tx->commandlen = aim_writetlvchain(tx->data, tx->commandlen, &tlvlist);
499 return aim_tx_enqueue(sess, tx);
503 * Generate a random cookie. (Non-client use only)
505 faim_export int aim_gencookie(unsigned char *buf)
511 for (i=0; i < AIM_COOKIELEN; i++)
512 buf[i] = 1+(int) (256.0*rand()/(RAND_MAX+0.0));
518 * Send Server Ready. (Non-client)
520 faim_export int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn)
522 struct command_tx_struct *tx;
525 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+0x22)))
530 i += aim_putsnac(tx->data, 0x0001, 0x0003, 0x0000, sess->snac_nextid++);
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);
547 return aim_tx_enqueue(sess, tx);
552 * Send service redirect. (Non-Client)
554 faim_export unsigned long aim_sendredirect(struct aim_session_t *sess,
555 struct aim_conn_t *conn,
556 unsigned short servid,
560 struct command_tx_struct *tx;
561 struct aim_tlvlist_t *tlvlist = NULL;
564 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
569 i += aim_putsnac(tx->data+i, 0x0001, 0x0005, 0x0000, 0x00000000);
571 aim_addtlvtochain16(&tlvlist, 0x000d, servid);
572 aim_addtlvtochain_str(&tlvlist, 0x0005, ip, strlen(ip));
573 aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
575 tx->commandlen = aim_writetlvchain(tx->data+i, tx->commandlen-i, &tlvlist)+i;
576 aim_freetlvchain(&tlvlist);
579 return aim_tx_enqueue(sess, tx);