]> andersk Git - libfaim.git/blame - aim_login.c
- Wed Nov 29 17:31:23 UTC 2000
[libfaim.git] / aim_login.c
CommitLineData
9de3ca7e 1/*
2 * aim_login.c
3 *
4 * This contains all the functions needed to actually login.
5 *
6 */
7
a25832e6 8#include <faim/aim.h>
9de3ca7e 9
b5bc2a8c 10#include "md5.h"
11
12static int aim_encode_password_md5(const char *password, const char *key, md5_byte_t *digest);
5daacaa3 13static int aim_encode_password(const char *password, unsigned char *encoded);
9de3ca7e 14
15/*
16 * FIXME: Reimplement the TIS stuff.
17 */
18#ifdef TIS_TELNET_PROXY
19#include "tis_telnet_proxy.h"
20#endif
21
78b3fb13 22faim_export int aim_sendconnack(struct aim_session_t *sess,
23 struct aim_conn_t *conn)
01b59e1e 24{
25 int curbyte=0;
26
5b79dc93 27 struct command_tx_struct *newpacket;
01b59e1e 28
b69540e3 29 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0001, conn, 4)))
01b59e1e 30 return -1;
31
5b79dc93 32 newpacket->lock = 1;
01b59e1e 33
5b79dc93 34 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
35 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
01b59e1e 36
5b79dc93 37 newpacket->lock = 0;
38 return aim_tx_enqueue(sess, newpacket);
01b59e1e 39}
40
01b59e1e 41/*
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).
48 */
78b3fb13 49faim_export int aim_request_login(struct aim_session_t *sess,
50 struct aim_conn_t *conn,
51 char *sn)
01b59e1e 52{
5daacaa3 53 int curbyte;
5b79dc93 54 struct command_tx_struct *newpacket;
01b59e1e 55
5daacaa3 56 if (!sess || !conn || !sn)
57 return -1;
58
59 /*
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.
64 *
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,
71 * or... I'll stop.
72 *
73 */
74 if ((sn[0] >= '0') && (sn[0] <= '9')) {
75 struct command_rx_struct *newrx;
76 int i;
77
78 if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct))))
79 return -1;
80 memset(newrx, 0x00, sizeof(struct command_rx_struct));
81 newrx->lock = 1;
82 newrx->hdrtype = AIM_FRAMETYPE_OSCAR;
83 newrx->hdr.oscar.type = 0x02;
84 newrx->hdr.oscar.seqnum = 0;
85 newrx->commandlen = 10+2+1;
86 newrx->nofree = 0;
87 if (!(newrx->data = malloc(newrx->commandlen))) {
88 free(newrx);
89 return -1;
90 }
91
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);
95
96 newrx->conn = conn;
97
98 newrx->next = sess->queue_incoming;
99 sess->queue_incoming = newrx;
100
101 newrx->lock = 0;
102
22517493 103 sess->flags &= ~AIM_SESS_FLAGS_SNACLOGIN;
104
5daacaa3 105 return 0;
106 }
107
22517493 108 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
5daacaa3 109
110 aim_sendconnack(sess, conn);
111
b69540e3 112 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+2+2+strlen(sn))))
5b79dc93 113 return -1;
01b59e1e 114
5b79dc93 115 newpacket->lock = 1;
01b59e1e 116
5daacaa3 117 curbyte = aim_putsnac(newpacket->data, 0x0017, 0x0006, 0x0000, 0x00010000);
5b79dc93 118 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
01b59e1e 119
5daacaa3 120 newpacket->commandlen = curbyte;
5b79dc93 121 newpacket->lock = 0;
5daacaa3 122
5b79dc93 123 return aim_tx_enqueue(sess, newpacket);
01b59e1e 124}
01b59e1e 125
9de3ca7e 126/*
24286d93 127 * send_login(int socket, char *sn, char *password)
9de3ca7e 128 *
129 * This is the initial login request packet.
130 *
131 * The password is encoded before transmition, as per
132 * encode_password(). See that function for their
133 * stupid method of doing it.
134 *
9de3ca7e 135 */
78b3fb13 136faim_export int aim_send_login (struct aim_session_t *sess,
137 struct aim_conn_t *conn,
138 char *sn, char *password,
139 struct client_info_s *clientinfo,
140 char *key)
9de3ca7e 141{
9de3ca7e 142 int curbyte=0;
5b79dc93 143 struct command_tx_struct *newpacket;
9de3ca7e 144
01b59e1e 145 if (!clientinfo || !sn || !password)
146 return -1;
147
b69540e3 148 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
5b79dc93 149 return -1;
9de3ca7e 150
5b79dc93 151 newpacket->lock = 1;
01b59e1e 152
22517493 153 newpacket->hdr.oscar.type = (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)?0x02:0x01;
b5bc2a8c 154
22517493 155 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)
5daacaa3 156 curbyte = aim_putsnac(newpacket->data, 0x0017, 0x0002, 0x0000, 0x00010000);
157 else {
158 curbyte = aimutil_put16(newpacket->data, 0x0000);
159 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
160 }
b5bc2a8c 161
5daacaa3 162 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
9de3ca7e 163
22517493 164 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN) {
5daacaa3 165 md5_byte_t digest[16];
166
167 aim_encode_password_md5(password, key, digest);
168 curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0025, 16, (char *)digest);
169 } else {
170 char *password_encoded;
171
172 password_encoded = (char *) malloc(strlen(password));
173 aim_encode_password(password, password_encoded);
174 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0002, strlen(password), password_encoded);
175 free(password_encoded);
176 }
177
13ebc4c4 178 /* XXX is clientstring required by oscar? */
179 if (strlen(clientinfo->clientstring))
180 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0003, strlen(clientinfo->clientstring), clientinfo->clientstring);
181
22517493 182 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN) {
5daacaa3 183 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0016, (unsigned short)clientinfo->major2);
184 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0017, (unsigned short)clientinfo->major);
185 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0018, (unsigned short)clientinfo->minor);
186 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0019, (unsigned short)clientinfo->minor2);
187 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x001a, (unsigned short)clientinfo->build);
b5bc2a8c 188
5daacaa3 189 curbyte += aim_puttlv_32(newpacket->data+curbyte, 0x0014, clientinfo->unknown);
190 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0009, 0x0015);
191 } else {
192 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0016, 0x010a);
193 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0017, 0x0004);
194 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0018, 0x003c);
195 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0019, 0x0001);
196 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x001a, 0x0cce);
197 curbyte += aim_puttlv_32(newpacket->data+curbyte, 0x0014, 0x00000055);
198 }
9de3ca7e 199
13ebc4c4 200 if (strlen(clientinfo->country))
201 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000e, strlen(clientinfo->country), clientinfo->country);
202 else
203 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000e, 2, "us");
204
205 if (strlen(clientinfo->lang))
206 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000f, strlen(clientinfo->lang), clientinfo->lang);
207 else
208 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000f, 2, "en");
209
210 newpacket->commandlen = curbyte;
9de3ca7e 211
5b79dc93 212 newpacket->lock = 0;
213 return aim_tx_enqueue(sess, newpacket);
9de3ca7e 214}
9de3ca7e 215
b5bc2a8c 216static int aim_encode_password_md5(const char *password, const char *key, md5_byte_t *digest)
217{
218 md5_state_t state;
219
220 md5_init(&state);
221 md5_append(&state, (const md5_byte_t *)key, strlen(key));
222 md5_append(&state, (const md5_byte_t *)password, strlen(password));
223 md5_append(&state, (const md5_byte_t *)AIM_MD5_STRING, strlen(AIM_MD5_STRING));
224 md5_finish(&state, (md5_byte_t *)digest);
225
226 return 0;
227}
228
5daacaa3 229/**
230 * aim_encode_password - Encode a password using old XOR method
231 * @password: incoming password
232 * @encoded: buffer to put encoded password
9de3ca7e 233 *
234 * This takes a const pointer to a (null terminated) string
235 * containing the unencoded password. It also gets passed
236 * an already allocated buffer to store the encoded password.
237 * This buffer should be the exact length of the password without
5daacaa3 238 * the null. The encoded password buffer /is not %NULL terminated/.
9de3ca7e 239 *
240 * The encoding_table seems to be a fixed set of values. We'll
241 * hope it doesn't change over time!
242 *
5daacaa3 243 * This is only used for the XOR method, not the better MD5 method.
b5bc2a8c 244 *
9de3ca7e 245 */
b5bc2a8c 246static int aim_encode_password(const char *password, unsigned char *encoded)
9de3ca7e 247{
248 u_char encoding_table[] = {
b69540e3 249#if 0 /* old v1 table */
9de3ca7e 250 0xf3, 0xb3, 0x6c, 0x99,
251 0x95, 0x3f, 0xac, 0xb6,
252 0xc5, 0xfa, 0x6b, 0x63,
253 0x69, 0x6c, 0xc3, 0x9f
b69540e3 254#else /* v2.1 table, also works for ICQ */
5e02cf44 255 0xf3, 0x26, 0x81, 0xc4,
b69540e3 256 0x39, 0x86, 0xdb, 0x92,
257 0x71, 0xa3, 0xb9, 0xe6,
258 0x53, 0x7a, 0x95, 0x7c
259#endif
5e02cf44 260 };
261
262 int i;
b69540e3 263
5e02cf44 264 for (i = 0; i < strlen(password); i++)
265 encoded[i] = (password[i] ^ encoding_table[i]);
266
267 return 0;
268}
269
01b59e1e 270/*
271 * This is sent back as a general response to the login command.
272 * It can be either an error or a success, depending on the
273 * precense of certain TLVs.
274 *
275 * The client should check the value of logininfo->errorcode. If
276 * its nonzero, there was an error.
277 *
278 */
78b3fb13 279faim_internal int aim_authparse(struct aim_session_t *sess,
280 struct command_rx_struct *command)
01b59e1e 281{
282 struct aim_tlvlist_t *tlvlist;
283 int ret = 1;
284 char *sn;
285 rxcallback_t userfunc = NULL;
286
287 memset(&sess->logininfo, 0x00, sizeof(sess->logininfo));
288
289 /*
290 * Read block of TLVs. All further data is derived
291 * from what is parsed here.
b5bc2a8c 292 *
293 * For SNAC login, there's a 17/3 SNAC header in front.
294 *
01b59e1e 295 */
22517493 296 if (sess->flags & AIM_SESS_FLAGS_SNACLOGIN)
5daacaa3 297 tlvlist = aim_readtlvchain(command->data+10, command->commandlen-10);
298 else
299 tlvlist = aim_readtlvchain(command->data, command->commandlen);
b5bc2a8c 300
01b59e1e 301 /*
302 * No matter what, we should have a screen name.
303 */
304 sn = aim_gettlv_str(tlvlist, 0x0001, 1);
b13c9e13 305 strncpy(sess->logininfo.screen_name, sn, strlen(sn));
306 free(sn);
307
01b59e1e 308 /*
309 * Check for an error code. If so, we should also
310 * have an error url.
311 */
5e02cf44 312 if (aim_gettlv(tlvlist, 0x0008, 1)) {
313 struct aim_tlv_t *errtlv;
314 errtlv = aim_gettlv(tlvlist, 0x0008, 1);
315 sess->logininfo.errorcode = aimutil_get16(errtlv->value);
316 sess->logininfo.errorurl = aim_gettlv_str(tlvlist, 0x0004, 1);
317 }
01b59e1e 318 /*
319 * If we have both an IP number (0x0005) and a cookie (0x0006),
320 * then the login was successful.
321 */
b69540e3 322 else if (aim_gettlv(tlvlist, 0x0005, 1) && aim_gettlv(tlvlist, 0x0006, 1)
323 /*aim_gettlv(tlvlist, 0x0006, 1)->length*/) {
5e02cf44 324 struct aim_tlv_t *tmptlv;
325
326 /*
327 * IP address of BOS server.
328 */
329 sess->logininfo.BOSIP = aim_gettlv_str(tlvlist, 0x0005, 1);
330
331 /*
332 * Authorization Cookie
333 */
334 tmptlv = aim_gettlv(tlvlist, 0x0006, 1);
335 memcpy(sess->logininfo.cookie, tmptlv->value, AIM_COOKIELEN);
336
337 /*
338 * The email address attached to this account
339 * Not available for ICQ logins.
340 */
341 if (aim_gettlv(tlvlist, 0x0011, 1))
01b59e1e 342 sess->logininfo.email = aim_gettlv_str(tlvlist, 0x0011, 1);
5e02cf44 343
344 /*
345 * The registration status. (Not real sure what it means.)
346 * Not available for ICQ logins.
347 */
348 if ((tmptlv = aim_gettlv(tlvlist, 0x0013, 1)))
01b59e1e 349 sess->logininfo.regstatus = aimutil_get16(tmptlv->value);
350
5e02cf44 351 }
01b59e1e 352
01b59e1e 353 userfunc = aim_callhandler(command->conn, 0x0017, 0x0003);
b5bc2a8c 354
01b59e1e 355 if (userfunc)
356 ret = userfunc(sess, command);
357
358 aim_freetlvchain(&tlvlist);
359
dad2e696 360 if (sess->logininfo.BOSIP) {
91c55d47 361 free(sess->logininfo.BOSIP);
dad2e696 362 sess->logininfo.BOSIP = NULL;
363 }
364 if (sess->logininfo.email) {
91c55d47 365 free(sess->logininfo.email);
dad2e696 366 sess->logininfo.email = NULL;
367 }
368 if (sess->logininfo.errorurl) {
91c55d47 369 free(sess->logininfo.errorurl);
dad2e696 370 sess->logininfo.errorurl = NULL;
371 }
01b59e1e 372
373 return ret;
374}
e6b05d80 375
b5bc2a8c 376/*
377 * Middle handler for 0017/0007 SNACs. Contains the auth key prefixed
378 * by only its length in a two byte word.
379 *
380 * Calls the client, which should then use the value to call aim_send_login.
381 *
382 */
78b3fb13 383faim_internal int aim_authkeyparse(struct aim_session_t *sess, struct command_rx_struct *command)
b5bc2a8c 384{
385 unsigned char *key;
386 int keylen;
387 int ret = 1;
388 rxcallback_t userfunc;
389
390 keylen = aimutil_get16(command->data+10);
98c88242 391 if (!(key = malloc(keylen+1)))
392 return ret;
b5bc2a8c 393 memcpy(key, command->data+12, keylen);
394 key[keylen] = '\0';
395
396 if ((userfunc = aim_callhandler(command->conn, 0x0017, 0x0007)))
78b3fb13 397 ret = userfunc(sess, command, (char *)key);
b5bc2a8c 398
399 free(key);
400
401 return ret;
402}
403
e6b05d80 404/*
405 * Generate an authorization response.
406 *
407 * You probably don't want this unless you're writing an AIM server.
408 *
409 */
78b3fb13 410faim_export unsigned long aim_sendauthresp(struct aim_session_t *sess,
411 struct aim_conn_t *conn,
412 char *sn, char *bosip,
413 char *cookie, char *email,
414 int regstatus)
e6b05d80 415{
5b79dc93 416 struct command_tx_struct *tx;
e6b05d80 417 struct aim_tlvlist_t *tlvlist = NULL;
418
b69540e3 419 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0004, conn, 1152)))
5b79dc93 420 return -1;
e6b05d80 421
5b79dc93 422 tx->lock = 1;
e6b05d80 423
424 if (sn)
425 aim_addtlvtochain_str(&tlvlist, 0x0001, sn, strlen(sn));
426 else
427 aim_addtlvtochain_str(&tlvlist, 0x0001, sess->logininfo.screen_name, strlen(sess->logininfo.screen_name));
428
429 if (sess->logininfo.errorcode) {
430 aim_addtlvtochain16(&tlvlist, 0x0008, sess->logininfo.errorcode);
431 aim_addtlvtochain_str(&tlvlist, 0x0004, sess->logininfo.errorurl, strlen(sess->logininfo.errorurl));
432 } else {
433 aim_addtlvtochain_str(&tlvlist, 0x0005, bosip, strlen(bosip));
434 aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
435 aim_addtlvtochain_str(&tlvlist, 0x0011, email, strlen(email));
5ac21963 436 aim_addtlvtochain16(&tlvlist, 0x0013, (unsigned short)regstatus);
e6b05d80 437 }
438
5b79dc93 439 tx->commandlen = aim_writetlvchain(tx->data, tx->commandlen, &tlvlist);
440 tx->lock = 0;
441 return aim_tx_enqueue(sess, tx);
e6b05d80 442}
443
444/*
445 * Generate a random cookie. (Non-client use only)
446 */
78b3fb13 447faim_export int aim_gencookie(unsigned char *buf)
e6b05d80 448{
449 int i;
450
451 srand(time(NULL));
452
453 for (i=0; i < AIM_COOKIELEN; i++)
454 buf[i] = 1+(int) (256.0*rand()/(RAND_MAX+0.0));
455
456 return i;
457}
458
459/*
460 * Send Server Ready. (Non-client)
461 */
78b3fb13 462faim_export int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn)
e6b05d80 463{
5b79dc93 464 struct command_tx_struct *tx;
e6b05d80 465 int i = 0;
466
b69540e3 467 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+0x22)))
5b79dc93 468 return -1;
e6b05d80 469
5b79dc93 470 tx->lock = 1;
e6b05d80 471
67c0bb2f 472 i += aim_putsnac(tx->data, 0x0001, 0x0003, 0x0000, sess->snac_nextid++);
5b79dc93 473
474 i += aimutil_put16(tx->data+i, 0x0001);
475 i += aimutil_put16(tx->data+i, 0x0002);
476 i += aimutil_put16(tx->data+i, 0x0003);
477 i += aimutil_put16(tx->data+i, 0x0004);
478 i += aimutil_put16(tx->data+i, 0x0006);
479 i += aimutil_put16(tx->data+i, 0x0008);
480 i += aimutil_put16(tx->data+i, 0x0009);
481 i += aimutil_put16(tx->data+i, 0x000a);
482 i += aimutil_put16(tx->data+i, 0x000b);
483 i += aimutil_put16(tx->data+i, 0x000c);
67c0bb2f 484 i += aimutil_put16(tx->data+i, 0x0013);
485 i += aimutil_put16(tx->data+i, 0x0015);
5b79dc93 486
67c0bb2f 487 tx->commandlen = i;
5b79dc93 488 tx->lock = 0;
5b79dc93 489 return aim_tx_enqueue(sess, tx);
e6b05d80 490}
491
492
493/*
494 * Send service redirect. (Non-Client)
495 */
78b3fb13 496faim_export unsigned long aim_sendredirect(struct aim_session_t *sess,
497 struct aim_conn_t *conn,
498 unsigned short servid,
499 char *ip,
500 char *cookie)
e6b05d80 501{
5b79dc93 502 struct command_tx_struct *tx;
e6b05d80 503 struct aim_tlvlist_t *tlvlist = NULL;
504 int i = 0;
505
b69540e3 506 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
5b79dc93 507 return -1;
508
509 tx->lock = 1;
e6b05d80 510
13ebc4c4 511 i += aim_putsnac(tx->data+i, 0x0001, 0x0005, 0x0000, 0x00000000);
e6b05d80 512
513 aim_addtlvtochain16(&tlvlist, 0x000d, servid);
514 aim_addtlvtochain_str(&tlvlist, 0x0005, ip, strlen(ip));
515 aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
516
5b79dc93 517 tx->commandlen = aim_writetlvchain(tx->data+i, tx->commandlen-i, &tlvlist)+i;
e6b05d80 518 aim_freetlvchain(&tlvlist);
519
5b79dc93 520 tx->lock = 0;
521 return aim_tx_enqueue(sess, tx);
e6b05d80 522}
This page took 1.433238 seconds and 5 git commands to generate.