4 * This contains all the functions needed to actually login.
12 * FIXME: Reimplement the TIS stuff.
14 #ifdef TIS_TELNET_PROXY
15 #include "tis_telnet_proxy.h"
18 int aim_sendconnack(struct aim_session_t *sess,
19 struct aim_conn_t *conn)
23 struct command_tx_struct *newpacket;
25 if (!(newpacket = aim_tx_new(0x0001, conn, 4)))
30 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
31 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
34 return aim_tx_enqueue(sess, newpacket);
39 * In AIM 3.5 protocol, the first stage of login is to request
40 * login from the Authorizer, passing it the screen name
41 * for verification. If the name is invalid, a 0017/0003
42 * is spit back, with the standard error contents. If valid,
43 * a 0017/0007 comes back, which is the signal to send
44 * it the main login command (0017/0002).
46 int aim_request_login(struct aim_session_t *sess,
47 struct aim_conn_t *conn,
52 struct command_tx_struct *newpacket;
54 if (!(newpacket = aim_tx_new(0x0002, conn, 10+2+2+strlen(sn))))
59 curbyte += aim_putsnac(newpacket->data+curbyte, 0x0017, 0x0006, 0x0000, 0x00010000);
60 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
63 return aim_tx_enqueue(sess, newpacket);
65 #endif /* SNACLOGIN */
68 * send_login(int socket, char *sn, char *password)
70 * This is the initial login request packet.
72 * The password is encoded before transmition, as per
73 * encode_password(). See that function for their
74 * stupid method of doing it.
77 int aim_send_login (struct aim_session_t *sess,
78 struct aim_conn_t *conn,
79 char *sn, char *password, struct client_info_s *clientinfo)
81 u_char *password_encoded = NULL; /* to store encoded password */
84 struct command_tx_struct *newpacket;
86 if (!clientinfo || !sn || !password)
89 if (!(newpacket = aim_tx_new(0x0002, conn, 1152)))
93 newpacket->commandlen = 10;
94 newpacket->commandlen += 2 + 2 + strlen(sn);
95 newpacket->commandlen += 2 + 2 + strlen(password);
96 newpacket->commandlen += 2 + 2 + strlen(clientinfo->clientstring);
97 newpacket->commandlen += 56;
101 curbyte = aim_putsnac(newpacket->data+curbyte, 0x0017, 0x0002, 0x0000, 0x00010000);
102 curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(sn), sn);
103 password_encoded = (u_char *) malloc(strlen(password));
104 aim_encode_password(password, password_encoded);
105 curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0002, strlen(password), password_encoded);
106 curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0003,
107 strlen(clientinfo->clientstring),
108 clientinfo->clientstring);
109 /* XXX: should use clientinfo provided version info */
110 curbyte+= aim_puttlv_16(newpacket->data+curbyte, 0x0016, 0x0004);
111 curbyte+= aim_puttlv_16(newpacket->data+curbyte, 0x0017, 0x0003);
112 curbyte+= aim_puttlv_16(newpacket->data+curbyte, 0x0018, 0x0005);
113 curbyte+= aim_puttlv_16(newpacket->data+curbyte, 0x0019, 0x0000);
114 curbyte+= aim_puttlv_16(newpacket->data+curbyte, 0x001a, 0x0686);
115 curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0001, 0x0002, clientinfo->country);
116 curbyte+= aim_puttlv_str(newpacket->data+curbyte, 0x0001, 0x0002, clientinfo->lang);
117 curbyte+= aim_puttlv_32(newpacket->data+curbyte, 0x0014, 0x0000002a);
118 curbyte+= aim_puttlv_16(newpacket->data+curbyte, 0x0009, 0x0015);
121 newpacket->commandlen = 4 + 4+strlen(sn) + 4+strlen(password) + 6;
124 if (strlen(clientinfo->clientstring))
125 newpacket->commandlen += 4+strlen(clientinfo->clientstring);
126 newpacket->commandlen += 6+6+6+6;
127 if (strlen(clientinfo->country))
128 newpacket->commandlen += 4+strlen(clientinfo->country);
129 if (strlen(clientinfo->lang))
130 newpacket->commandlen += 4+strlen(clientinfo->lang);
132 newpacket->commandlen += 6;
135 newpacket->type = 0x01;
137 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
138 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
139 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
140 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(sn));
141 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
143 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
144 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(password));
145 password_encoded = (char *) malloc(strlen(password));
146 aim_encode_password(password, password_encoded);
147 curbyte += aimutil_putstr(newpacket->data+curbyte, password_encoded, strlen(password));
148 free(password_encoded);
150 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0016, 0x0004);
153 if (strlen(clientinfo->clientstring)) {
154 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
155 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(clientinfo->clientstring));
156 curbyte += aimutil_putstr(newpacket->data+curbyte, clientinfo->clientstring, strlen(clientinfo->clientstring));
158 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0017, clientinfo->major /*0x0001*/);
159 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0018, clientinfo->minor /*0x0001*/);
160 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0019, 0x0000);
161 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x001a, clientinfo->build /*0x0013*/);
162 if (strlen(clientinfo->country)) {
163 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000e);
164 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(clientinfo->country));
165 curbyte += aimutil_putstr(newpacket->data+curbyte, clientinfo->country, strlen(clientinfo->country));
167 if (strlen(clientinfo->lang)) {
168 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
169 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(clientinfo->lang));
170 curbyte += aimutil_putstr(newpacket->data+curbyte, clientinfo->lang, strlen(clientinfo->lang));
174 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0009, 0x0015);
178 return aim_tx_enqueue(sess, newpacket);
182 * int encode_password(
183 * const char *password,
187 * This takes a const pointer to a (null terminated) string
188 * containing the unencoded password. It also gets passed
189 * an already allocated buffer to store the encoded password.
190 * This buffer should be the exact length of the password without
191 * the null. The encoded password buffer IS NOT NULL TERMINATED.
193 * The encoding_table seems to be a fixed set of values. We'll
194 * hope it doesn't change over time!
197 int aim_encode_password(const char *password, u_char *encoded)
199 u_char encoding_table[] = {
200 0xf3, 0xb3, 0x6c, 0x99,
201 0x95, 0x3f, 0xac, 0xb6,
202 0xc5, 0xfa, 0x6b, 0x63,
203 0x69, 0x6c, 0xc3, 0x9f
208 for (i = 0; i < strlen(password); i++)
209 encoded[i] = (password[i] ^ encoding_table[i]);
215 * This is sent back as a general response to the login command.
216 * It can be either an error or a success, depending on the
217 * precense of certain TLVs.
219 * The client should check the value of logininfo->errorcode. If
220 * its nonzero, there was an error.
223 int aim_authparse(struct aim_session_t *sess,
224 struct command_rx_struct *command)
226 struct aim_tlvlist_t *tlvlist;
229 rxcallback_t userfunc = NULL;
231 memset(&sess->logininfo, 0x00, sizeof(sess->logininfo));
234 * Read block of TLVs. All further data is derived
235 * from what is parsed here.
238 tlvlist = aim_readtlvchain(command->data+10, command->commandlen-10);
240 tlvlist = aim_readtlvchain(command->data, command->commandlen);
243 * No matter what, we should have a screen name.
245 sn = aim_gettlv_str(tlvlist, 0x0001, 1);
246 memcpy(sess->logininfo.screen_name, sn, strlen(sn));
247 sn[(strlen(sn))] = '\0';
250 * Check for an error code. If so, we should also
253 if (aim_gettlv(tlvlist, 0x0008, 1))
255 struct aim_tlv_t *errtlv;
256 errtlv = aim_gettlv(tlvlist, 0x0008, 1);
257 sess->logininfo.errorcode = aimutil_get16(errtlv->value);
258 sess->logininfo.errorurl = aim_gettlv_str(tlvlist, 0x0004, 1);
261 * If we have both an IP number (0x0005) and a cookie (0x0006),
262 * then the login was successful.
264 else if (aim_gettlv(tlvlist, 0x0005, 1) && aim_gettlv(tlvlist, 0x0006, 1))
266 struct aim_tlv_t *tmptlv;
269 * IP address of BOS server.
271 sess->logininfo.BOSIP = aim_gettlv_str(tlvlist, 0x0005, 1);
274 * Authorization Cookie
276 tmptlv = aim_gettlv(tlvlist, 0x0006, 1);
277 memcpy(sess->logininfo.cookie, tmptlv->value, AIM_COOKIELEN);
280 * The email address attached to this account
282 sess->logininfo.email = aim_gettlv_str(tlvlist, 0x0011, 1);
285 * The registration status. (Not real sure what it means.)
287 tmptlv = aim_gettlv(tlvlist, 0x0013, 1);
288 sess->logininfo.regstatus = aimutil_get16(tmptlv->value);
293 userfunc = aim_callhandler(command->conn, 0x0017, 0x0003);
295 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_AUTHSUCCESS);
298 ret = userfunc(sess, command);
300 aim_freetlvchain(&tlvlist);
302 /* These have been clobbered by the freetlvchain */
303 sess->logininfo.BOSIP = NULL;
304 sess->logininfo.email = NULL;
305 sess->logininfo.errorurl = NULL;
311 * Generate an authorization response.
313 * You probably don't want this unless you're writing an AIM server.
316 unsigned long aim_sendauthresp(struct aim_session_t *sess,
317 struct aim_conn_t *conn,
318 char *sn, char *bosip,
319 char *cookie, char *email,
322 struct command_tx_struct *tx;
323 struct aim_tlvlist_t *tlvlist = NULL;
325 if (!(tx = aim_tx_new(0x0004, conn, 1152)))
331 aim_addtlvtochain_str(&tlvlist, 0x0001, sn, strlen(sn));
333 aim_addtlvtochain_str(&tlvlist, 0x0001, sess->logininfo.screen_name, strlen(sess->logininfo.screen_name));
335 if (sess->logininfo.errorcode) {
336 aim_addtlvtochain16(&tlvlist, 0x0008, sess->logininfo.errorcode);
337 aim_addtlvtochain_str(&tlvlist, 0x0004, sess->logininfo.errorurl, strlen(sess->logininfo.errorurl));
339 aim_addtlvtochain_str(&tlvlist, 0x0005, bosip, strlen(bosip));
340 aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
341 aim_addtlvtochain_str(&tlvlist, 0x0011, email, strlen(email));
342 aim_addtlvtochain16(&tlvlist, 0x0013, regstatus);
345 tx->commandlen = aim_writetlvchain(tx->data, tx->commandlen, &tlvlist);
347 return aim_tx_enqueue(sess, tx);
351 * Generate a random cookie. (Non-client use only)
353 int aim_gencookie(unsigned char *buf)
359 for (i=0; i < AIM_COOKIELEN; i++)
360 buf[i] = 1+(int) (256.0*rand()/(RAND_MAX+0.0));
366 * Send Server Ready. (Non-client)
368 int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn)
370 struct command_tx_struct *tx;
373 if (!(tx = aim_tx_new(0x0002, conn, 10+0x22)))
378 i += aim_putsnac(tx->data, 0x0001, 0x0003, 0x0000, sess->snac_nextid++);
380 i += aimutil_put16(tx->data+i, 0x0001);
381 i += aimutil_put16(tx->data+i, 0x0002);
382 i += aimutil_put16(tx->data+i, 0x0003);
383 i += aimutil_put16(tx->data+i, 0x0004);
384 i += aimutil_put16(tx->data+i, 0x0006);
385 i += aimutil_put16(tx->data+i, 0x0008);
386 i += aimutil_put16(tx->data+i, 0x0009);
387 i += aimutil_put16(tx->data+i, 0x000a);
388 i += aimutil_put16(tx->data+i, 0x000b);
389 i += aimutil_put16(tx->data+i, 0x000c);
390 i += aimutil_put16(tx->data+i, 0x0013);
391 i += aimutil_put16(tx->data+i, 0x0015);
395 return aim_tx_enqueue(sess, tx);
400 * Send service redirect. (Non-Client)
402 unsigned long aim_sendredirect(struct aim_session_t *sess,
403 struct aim_conn_t *conn,
404 unsigned short servid,
408 struct command_tx_struct *tx;
409 struct aim_tlvlist_t *tlvlist = NULL;
412 if (!(tx = aim_tx_new(0x0002, conn, 1152)))
417 i += aimutil_put16(tx->data+i, 0x0001);
418 i += aimutil_put16(tx->data+i, 0x0005);
419 i += aimutil_put16(tx->data+i, 0x0000);
420 i += aimutil_put16(tx->data+i, 0x0000);
421 i += aimutil_put16(tx->data+i, 0x0000);
423 aim_addtlvtochain16(&tlvlist, 0x000d, servid);
424 aim_addtlvtochain_str(&tlvlist, 0x0005, ip, strlen(ip));
425 aim_addtlvtochain_str(&tlvlist, 0x0006, cookie, AIM_COOKIELEN);
427 tx->commandlen = aim_writetlvchain(tx->data+i, tx->commandlen-i, &tlvlist)+i;
428 aim_freetlvchain(&tlvlist);
431 return aim_tx_enqueue(sess, tx);