4 * This contains all the functions needed to actually login.
12 static int aim_encode_password_md5(const char *password, const char *key, md5_byte_t *digest);
13 static int aim_encode_password(const char *password, unsigned char *encoded);
16 * FIXME: Reimplement the TIS stuff.
18 #ifdef TIS_TELNET_PROXY
19 #include "tis_telnet_proxy.h"
22 faim_export int aim_sendconnack(struct aim_session_t *sess,
23 struct aim_conn_t *conn)
27 struct command_tx_struct *newpacket;
29 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0001, conn, 4)))
34 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
35 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
38 return aim_tx_enqueue(sess, newpacket);
42 * In AIM 3.5 protocol, the first stage of login is to request
43 * login from the Authorizer, passing it the screen name
44 * for verification. If the name is invalid, a 0017/0003
45 * is spit back, with the standard error contents. If valid,
46 * a 0017/0007 comes back, which is the signal to send
47 * it the main login command (0017/0002).
49 faim_export int aim_request_login(struct aim_session_t *sess,
50 struct aim_conn_t *conn,
54 struct command_tx_struct *newpacket;
56 if (!sess || !conn || !sn)
60 * For ICQ, we enable the ancient horrible login and stuff
61 * a key packet into the queue to make it look like we got
62 * a reply back. This is so the client doesn't know we're
63 * really not doing MD5 login.
65 * This may sound stupid, but I'm not in the best of moods and
66 * I don't plan to keep support for this crap around much longer.
67 * Its all AOL's fault anyway, really. I hate AOL. Really. They
68 * always seem to be able to piss me off by doing the dumbest little
69 * things. Like disabling MD5 logins for ICQ UINs, or adding purposefully
70 * wrong TLV lengths, or adding superfluous information to host strings,
74 if ((sn[0] >= '0') && (sn[0] <= '9')) {
75 struct command_rx_struct *newrx;
78 if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct))))
80 memset(newrx, 0x00, sizeof(struct command_rx_struct));
82 newrx->hdrtype = AIM_FRAMETYPE_OSCAR;
83 newrx->hdr.oscar.type = 0x02;
84 newrx->hdr.oscar.seqnum = 0;
85 newrx->commandlen = 10+2+1;
87 if (!(newrx->data = malloc(newrx->commandlen))) {
92 i = aim_putsnac(newrx->data, 0x0017, 0x0007, 0x0000, 0x0000);
93 i += aimutil_put16(newrx->data+i, 0x01);
94 i += aimutil_putstr(newrx->data+i, "0", 1);
98 newrx->next = sess->queue_incoming;
99 sess->queue_incoming = newrx;
103 sess->flags &= ~AIM_SESS_FLAGS_SNACLOGIN;
108 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
110 aim_sendconnack(sess, conn);
112 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+2+2+strlen(sn))))
117 curbyte = aim_putsnac(newpacket->data, 0x0017, 0x0006, 0x0000, 0x00010000);
118 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
120 newpacket->commandlen = curbyte;
123 return aim_tx_enqueue(sess, newpacket);
127 * send_login(int socket, char *sn, char *password)
129 * This is the initial login request packet.
131 * The password is encoded before transmition, as per
132 * encode_password(). See that function for their
133 * stupid method of doing it.
136 * clientstring = "AOL Instant Messenger (SM), version 4.3.2188/WIN32"
142 * unknown = 0x00000086
147 faim_export int aim_send_login (struct aim_session_t *sess,
148 struct aim_conn_t *conn,
149 char *sn, char *password,
150 struct client_info_s *clientinfo,
154 struct command_tx_struct *newpacket;
156 if (!clientinfo || !sn || !password)
159 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
164 newpacket->hdr.oscar.type = (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)?0x02:0x01;
166 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)
167 curbyte = aim_putsnac(newpacket->data, 0x0017, 0x0002, 0x0000, 0x00010000);
169 curbyte = aimutil_put16(newpacket->data, 0x0000);
170 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
173 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
175 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN) {
176 md5_byte_t digest[16];
178 aim_encode_password_md5(password, key, digest);
179 curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0025, 16, (char *)digest);
181 char *password_encoded;
183 password_encoded = (char *) malloc(strlen(password));
184 aim_encode_password(password, password_encoded);
185 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0002, strlen(password), password_encoded);
186 free(password_encoded);
189 /* XXX is clientstring required by oscar? */
190 if (strlen(clientinfo->clientstring))
191 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0003, strlen(clientinfo->clientstring), clientinfo->clientstring);
193 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN) {
194 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0016, (unsigned short)clientinfo->major2);
195 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0017, (unsigned short)clientinfo->major);
196 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0018, (unsigned short)clientinfo->minor);
197 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0019, (unsigned short)clientinfo->minor2);
198 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x001a, (unsigned short)clientinfo->build);
200 curbyte += aim_puttlv_32(newpacket->data+curbyte, 0x0014, clientinfo->unknown);
201 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0009, 0x0015);
202 curbyte += aim_puttlv_8(newpacket->data+curbyte, 0x004a, 0x01);
204 /* Use very specific version numbers, to further indicate the hack. */
205 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0016, 0x010a);
206 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0017, 0x0004);
207 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0018, 0x003c);
208 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0019, 0x0001);
209 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x001a, 0x0cce);
210 curbyte += aim_puttlv_32(newpacket->data+curbyte, 0x0014, 0x00000055);
213 if (strlen(clientinfo->country))
214 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000e, strlen(clientinfo->country), clientinfo->country);
216 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000e, 2, "us");
218 if (strlen(clientinfo->lang))
219 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000f, strlen(clientinfo->lang), clientinfo->lang);
221 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000f, 2, "en");
223 newpacket->commandlen = curbyte;
226 return aim_tx_enqueue(sess, newpacket);
229 static int aim_encode_password_md5(const char *password, const char *key, md5_byte_t *digest)
234 md5_append(&state, (const md5_byte_t *)key, strlen(key));
235 md5_append(&state, (const md5_byte_t *)password, strlen(password));
236 md5_append(&state, (const md5_byte_t *)AIM_MD5_STRING, strlen(AIM_MD5_STRING));
237 md5_finish(&state, (md5_byte_t *)digest);
243 * aim_encode_password - Encode a password using old XOR method
244 * @password: incoming password
245 * @encoded: buffer to put encoded password
247 * This takes a const pointer to a (null terminated) string
248 * containing the unencoded password. It also gets passed
249 * an already allocated buffer to store the encoded password.
250 * This buffer should be the exact length of the password without
251 * the null. The encoded password buffer /is not %NULL terminated/.
253 * The encoding_table seems to be a fixed set of values. We'll
254 * hope it doesn't change over time!
256 * This is only used for the XOR method, not the better MD5 method.
259 static int aim_encode_password(const char *password, unsigned char *encoded)
261 u_char encoding_table[] = {
262 #if 0 /* old v1 table */
263 0xf3, 0xb3, 0x6c, 0x99,
264 0x95, 0x3f, 0xac, 0xb6,
265 0xc5, 0xfa, 0x6b, 0x63,
266 0x69, 0x6c, 0xc3, 0x9f
267 #else /* v2.1 table, also works for ICQ */
268 0xf3, 0x26, 0x81, 0xc4,
269 0x39, 0x86, 0xdb, 0x92,
270 0x71, 0xa3, 0xb9, 0xe6,
271 0x53, 0x7a, 0x95, 0x7c
277 for (i = 0; i < strlen(password); i++)
278 encoded[i] = (password[i] ^ encoding_table[i]);
284 * This is sent back as a general response to the login command.
285 * It can be either an error or a success, depending on the
286 * precense of certain TLVs.
288 * The client should check the value passed as errorcode. If
289 * its nonzero, there was an error.
292 faim_internal int aim_authparse(struct aim_session_t *sess,
293 struct command_rx_struct *command)
295 struct aim_tlvlist_t *tlvlist;
297 rxcallback_t userfunc = NULL;
298 char *sn = NULL, *bosip = NULL, *errurl = NULL, *email = NULL;
299 unsigned char *cookie = NULL;
300 int errorcode = 0, regstatus = 0;
301 int latestbuild = 0, latestbetabuild = 0;
302 char *latestrelease = NULL, *latestbeta = NULL;
303 char *latestreleaseurl = NULL, *latestbetaurl = NULL;
304 char *latestreleaseinfo = NULL, *latestbetainfo = NULL;
307 * Read block of TLVs. All further data is derived
308 * from what is parsed here.
310 * For SNAC login, there's a 17/3 SNAC header in front.
313 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)
314 tlvlist = aim_readtlvchain(command->data+10, command->commandlen-10);
316 tlvlist = aim_readtlvchain(command->data, command->commandlen);
319 * No matter what, we should have a screen name.
321 memset(sess->sn, 0, sizeof(sess->sn));
322 if (aim_gettlv(tlvlist, 0x0001, 1)) {
323 sn = aim_gettlv_str(tlvlist, 0x0001, 1);
324 strncpy(sess->sn, sn, sizeof(sess->sn));
328 * Check for an error code. If so, we should also
331 if (aim_gettlv(tlvlist, 0x0008, 1))
332 errorcode = aim_gettlv16(tlvlist, 0x0008, 1);
333 if (aim_gettlv(tlvlist, 0x0004, 1))
334 errurl = aim_gettlv_str(tlvlist, 0x0004, 1);
337 * BOS server address.
339 if (aim_gettlv(tlvlist, 0x0005, 1))
340 bosip = aim_gettlv_str(tlvlist, 0x0005, 1);
343 * Authorization cookie.
345 if (aim_gettlv(tlvlist, 0x0006, 1)) {
346 struct aim_tlv_t *tmptlv;
348 tmptlv = aim_gettlv(tlvlist, 0x0006, 1);
350 if ((cookie = malloc(tmptlv->length)))
351 memcpy(cookie, tmptlv->value, tmptlv->length);
355 * The email address attached to this account
356 * Not available for ICQ logins.
358 if (aim_gettlv(tlvlist, 0x0011, 1))
359 email = aim_gettlv_str(tlvlist, 0x0011, 1);
362 * The registration status. (Not real sure what it means.)
363 * Not available for ICQ logins.
365 if (aim_gettlv(tlvlist, 0x0013, 1))
366 regstatus = aim_gettlv16(tlvlist, 0x0013, 1);
368 if (aim_gettlv(tlvlist, 0x0040, 1))
369 latestbetabuild = aim_gettlv32(tlvlist, 0x0040, 1);
370 if (aim_gettlv(tlvlist, 0x0041, 1))
371 latestbetaurl = aim_gettlv_str(tlvlist, 0x0041, 1);
372 if (aim_gettlv(tlvlist, 0x0042, 1))
373 latestbetainfo = aim_gettlv_str(tlvlist, 0x0042, 1);
374 if (aim_gettlv(tlvlist, 0x0043, 1))
375 latestbeta = aim_gettlv_str(tlvlist, 0x0043, 1);
376 if (aim_gettlv(tlvlist, 0x0048, 1))
377 ; /* no idea what this is */
379 if (aim_gettlv(tlvlist, 0x0044, 1))
380 latestbuild = aim_gettlv32(tlvlist, 0x0044, 1);
381 if (aim_gettlv(tlvlist, 0x0045, 1))
382 latestreleaseurl = aim_gettlv_str(tlvlist, 0x0045, 1);
383 if (aim_gettlv(tlvlist, 0x0046, 1))
384 latestreleaseinfo = aim_gettlv_str(tlvlist, 0x0046, 1);
385 if (aim_gettlv(tlvlist, 0x0047, 1))
386 latestrelease = aim_gettlv_str(tlvlist, 0x0047, 1);
387 if (aim_gettlv(tlvlist, 0x0049, 1))
388 ; /* no idea what this is */
391 if ((userfunc = aim_callhandler(command->conn, 0x0017, 0x0003)))
392 ret = userfunc(sess, command, sn, errorcode, errurl, regstatus, email, bosip, cookie, latestrelease, latestbuild, latestreleaseurl, latestreleaseinfo, latestbeta, latestbetabuild, latestbetaurl, latestbetainfo);
407 if (latestreleaseurl)
408 free(latestreleaseurl);
413 if (latestreleaseinfo)
414 free(latestreleaseinfo);
416 free(latestbetainfo);
418 aim_freetlvchain(&tlvlist);
424 * Middle handler for 0017/0007 SNACs. Contains the auth key prefixed
425 * by only its length in a two byte word.
427 * Calls the client, which should then use the value to call aim_send_login.
430 faim_internal int aim_authkeyparse(struct aim_session_t *sess, struct command_rx_struct *command)
435 rxcallback_t userfunc;
437 keylen = aimutil_get16(command->data+10);
438 if (!(key = malloc(keylen+1)))
440 memcpy(key, command->data+12, keylen);
443 if ((userfunc = aim_callhandler(command->conn, 0x0017, 0x0007)))
444 ret = userfunc(sess, command, (char *)key);
452 * Generate an authorization response.
454 * You probably don't want this unless you're writing an AIM server.
457 faim_export unsigned long aim_sendauthresp(struct aim_session_t *sess,
458 struct aim_conn_t *conn,
459 char *sn, int errorcode,
460 char *errorurl, char *bosip,
461 char *cookie, char *email,
464 struct command_tx_struct *tx;
465 struct aim_tlvlist_t *tlvlist = NULL;
467 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0004, conn, 1152)))
473 aim_addtlvtochain_str(&tlvlist, 0x0001, sn, strlen(sn));
475 aim_addtlvtochain_str(&tlvlist, 0x0001, sess->sn, strlen(sess->sn));
478 aim_addtlvtochain16(&tlvlist, 0x0008, errorcode);
479 aim_addtlvtochain_str(&tlvlist, 0x0004, errorurl, strlen(errorurl));
481 aim_addtlvtochain_str(&tlvlist, 0x0005, bosip, strlen(bosip));
482 aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
483 aim_addtlvtochain_str(&tlvlist, 0x0011, email, strlen(email));
484 aim_addtlvtochain16(&tlvlist, 0x0013, (unsigned short)regstatus);
487 tx->commandlen = aim_writetlvchain(tx->data, tx->commandlen, &tlvlist);
490 return aim_tx_enqueue(sess, tx);
494 * Generate a random cookie. (Non-client use only)
496 faim_export int aim_gencookie(unsigned char *buf)
502 for (i=0; i < AIM_COOKIELEN; i++)
503 buf[i] = 1+(int) (256.0*rand()/(RAND_MAX+0.0));
509 * Send Server Ready. (Non-client)
511 faim_export int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn)
513 struct command_tx_struct *tx;
516 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+0x22)))
521 i += aim_putsnac(tx->data, 0x0001, 0x0003, 0x0000, sess->snac_nextid++);
523 i += aimutil_put16(tx->data+i, 0x0001);
524 i += aimutil_put16(tx->data+i, 0x0002);
525 i += aimutil_put16(tx->data+i, 0x0003);
526 i += aimutil_put16(tx->data+i, 0x0004);
527 i += aimutil_put16(tx->data+i, 0x0006);
528 i += aimutil_put16(tx->data+i, 0x0008);
529 i += aimutil_put16(tx->data+i, 0x0009);
530 i += aimutil_put16(tx->data+i, 0x000a);
531 i += aimutil_put16(tx->data+i, 0x000b);
532 i += aimutil_put16(tx->data+i, 0x000c);
533 i += aimutil_put16(tx->data+i, 0x0013);
534 i += aimutil_put16(tx->data+i, 0x0015);
538 return aim_tx_enqueue(sess, tx);
543 * Send service redirect. (Non-Client)
545 faim_export unsigned long aim_sendredirect(struct aim_session_t *sess,
546 struct aim_conn_t *conn,
547 unsigned short servid,
551 struct command_tx_struct *tx;
552 struct aim_tlvlist_t *tlvlist = NULL;
555 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
560 i += aim_putsnac(tx->data+i, 0x0001, 0x0005, 0x0000, 0x00000000);
562 aim_addtlvtochain16(&tlvlist, 0x000d, servid);
563 aim_addtlvtochain_str(&tlvlist, 0x0005, ip, strlen(ip));
564 aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
566 tx->commandlen = aim_writetlvchain(tx->data+i, tx->commandlen-i, &tlvlist)+i;
567 aim_freetlvchain(&tlvlist);
570 return aim_tx_enqueue(sess, tx);