]> andersk Git - libfaim.git/blob - aim_login.c
Added capability block sending and chat invitation parsing.
[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 #include <faim/aim.h>
9
10
11 /*
12  * FIXME: Reimplement the TIS stuff.
13  */
14 #ifdef TIS_TELNET_PROXY
15 #include "tis_telnet_proxy.h"
16 #endif
17
18 int aim_sendconnack(struct aim_session_t *sess,
19                       struct aim_conn_t *conn)
20 {
21   int curbyte=0;
22   
23   struct command_tx_struct newpacket;
24
25   if (conn)
26     newpacket.conn = conn;
27   else
28     return -1;
29
30   newpacket.commandlen = 2+2;
31   newpacket.data = (u_char *) calloc (1,  newpacket.commandlen );
32   newpacket.lock = 1;
33   newpacket.type = 0x01;
34   
35   curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
36   curbyte += aimutil_put16(newpacket.data+curbyte, 0x0001);
37
38   newpacket.lock = 0;
39   aim_tx_enqueue(sess, &newpacket);
40
41   return 0;
42 }
43
44 #ifdef SNACLOGIN
45 /*
46  * In AIM 3.5 protocol, the first stage of login is to request
47  * login from the Authorizer, passing it the screen name
48  * for verification.  If the name is invalid, a 0017/0003 
49  * is spit back, with the standard error contents.  If valid,
50  * a 0017/0007 comes back, which is the signal to send
51  * it the main login command (0017/0002).  
52  */
53 int aim_request_login(struct aim_session_t *sess,
54                       struct aim_conn_t *conn, 
55                       char *sn)
56 {
57   int curbyte=0;
58   
59   struct command_tx_struct newpacket;
60
61   if (conn)
62     newpacket.conn = conn;
63   else
64     newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_AUTH);
65
66   newpacket.commandlen = 10+2+2+strlen(sn);
67   newpacket.data = (u_char *) calloc (1,  newpacket.commandlen );
68   newpacket.lock = 1;
69   newpacket.type = 0x02;
70   
71   curbyte += aim_putsnac(newpacket.data+curbyte, 0x0017, 0x0006, 0x0000, 0x00010000);
72   curbyte += aim_puttlv_str(newpacket.data+curbyte, 0x0001, strlen(sn), sn);
73
74   newpacket.lock = 0;
75   aim_tx_enqueue(sess, &newpacket);
76
77   return 0;
78 }
79 #endif /* SNACLOGIN */
80
81 /*
82  * send_login(int socket, char *sn, char *password)
83  *  
84  * This is the initial login request packet.
85  *
86  * The password is encoded before transmition, as per
87  * encode_password().  See that function for their
88  * stupid method of doing it.
89  *
90  */
91 int aim_send_login (struct aim_session_t *sess,
92                     struct aim_conn_t *conn, 
93                     char *sn, char *password, struct client_info_s *clientinfo)
94 {
95   u_char *password_encoded = NULL;  /* to store encoded password */
96   int curbyte=0;
97
98   struct command_tx_struct newpacket;
99
100   if (!clientinfo || !sn || !password)
101     return -1;
102
103   if (conn)
104     newpacket.conn = conn;
105   else
106     newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_AUTH);
107
108 #ifdef SNACLOGIN 
109   newpacket.commandlen = 10;
110   newpacket.commandlen += 2 + 2 + strlen(sn);
111   newpacket.commandlen += 2 + 2 + strlen(password);
112   newpacket.commandlen += 2 + 2 + strlen(clientinfo->clientstring);
113   newpacket.commandlen += 56;
114   
115   newpacket.data = (u_char *) calloc (1,  newpacket.commandlen );
116   newpacket.lock = 1;
117   newpacket.type = 0x02;
118
119   curbyte = aim_putsnac(newpacket.data+curbyte, 0x0017, 0x0002, 0x0000, 0x00010000);
120   curbyte+= aim_puttlv_str(newpacket.data+curbyte, 0x0001, strlen(sn), sn);
121   password_encoded = (u_char *) malloc(strlen(password));
122   aim_encode_password(password, password_encoded);
123   curbyte+= aim_puttlv_str(newpacket.data+curbyte, 0x0002, strlen(password), password_encoded);
124   curbyte+= aim_puttlv_str(newpacket.data+curbyte, 0x0003, 
125                            strlen(clientinfo->clientstring), 
126                            clientinfo->clientstring);
127   /* XXX: should use clientinfo provided version info */
128   curbyte+= aim_puttlv_16(newpacket.data+curbyte, 0x0016, 0x0004);
129   curbyte+= aim_puttlv_16(newpacket.data+curbyte, 0x0017, 0x0003);
130   curbyte+= aim_puttlv_16(newpacket.data+curbyte, 0x0018, 0x0005);
131   curbyte+= aim_puttlv_16(newpacket.data+curbyte, 0x0019, 0x0000);
132   curbyte+= aim_puttlv_16(newpacket.data+curbyte, 0x001a, 0x0686);
133   curbyte+= aim_puttlv_str(newpacket.data+curbyte, 0x0001, 0x0002, clientinfo->country);
134   curbyte+= aim_puttlv_str(newpacket.data+curbyte, 0x0001, 0x0002, clientinfo->lang);
135   curbyte+= aim_puttlv_32(newpacket.data+curbyte, 0x0014, 0x0000002a);
136   curbyte+= aim_puttlv_16(newpacket.data+curbyte, 0x0009, 0x0015);
137 #else
138   
139   newpacket.commandlen = 4 + 4+strlen(sn) + 4+strlen(password) + 6;
140  
141   if (clientinfo)
142     {
143       if (strlen(clientinfo->clientstring))
144         newpacket.commandlen += 4+strlen(clientinfo->clientstring);
145       newpacket.commandlen += 6+6+6+6;
146       if (strlen(clientinfo->country))
147         newpacket.commandlen += 4+strlen(clientinfo->country);
148       if (strlen(clientinfo->lang))
149         newpacket.commandlen += 4+strlen(clientinfo->lang);
150     }
151   newpacket.commandlen += 6;
152
153   newpacket.data = (char *) calloc (1,  newpacket.commandlen );
154   newpacket.lock = 1;
155   newpacket.type = 0x01;
156
157   curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
158   curbyte += aimutil_put16(newpacket.data+curbyte, 0x0001);
159   curbyte += aimutil_put16(newpacket.data+curbyte, 0x0001);
160   curbyte += aimutil_put16(newpacket.data+curbyte, strlen(sn));
161   curbyte += aimutil_putstr(newpacket.data+curbyte, sn, strlen(sn));
162
163   curbyte += aimutil_put16(newpacket.data+curbyte, 0x0002);
164   curbyte += aimutil_put16(newpacket.data+curbyte, strlen(password));
165   password_encoded = (char *) malloc(strlen(password));
166   aim_encode_password(password, password_encoded);
167   curbyte += aimutil_putstr(newpacket.data+curbyte, password_encoded, strlen(password));
168   free(password_encoded);
169   
170   curbyte += aim_puttlv_16(newpacket.data+curbyte, 0x0016, 0x0004);
171   
172   if (clientinfo)
173     {
174       if (strlen(clientinfo->clientstring))
175         {
176           curbyte += aimutil_put16(newpacket.data+curbyte, 0x0003);
177           curbyte += aimutil_put16(newpacket.data+curbyte, strlen(clientinfo->clientstring));
178           curbyte += aimutil_putstr(newpacket.data+curbyte, clientinfo->clientstring, strlen(clientinfo->clientstring));
179         }
180       curbyte += aim_puttlv_16(newpacket.data+curbyte, 0x0017, clientinfo->major /*0x0001*/);
181       curbyte += aim_puttlv_16(newpacket.data+curbyte, 0x0018, clientinfo->minor /*0x0001*/);
182       curbyte += aim_puttlv_16(newpacket.data+curbyte, 0x0019, 0x0000);
183       curbyte += aim_puttlv_16(newpacket.data+curbyte, 0x001a, clientinfo->build /*0x0013*/);
184       if (strlen(clientinfo->country))
185         {
186           curbyte += aimutil_put16(newpacket.data+curbyte, 0x000e);
187           curbyte += aimutil_put16(newpacket.data+curbyte, strlen(clientinfo->country));
188           curbyte += aimutil_putstr(newpacket.data+curbyte, clientinfo->country, strlen(clientinfo->country));
189         }
190       if (strlen(clientinfo->lang))
191         {
192           curbyte += aimutil_put16(newpacket.data+curbyte, 0x000f);
193           curbyte += aimutil_put16(newpacket.data+curbyte, strlen(clientinfo->lang));
194           curbyte += aimutil_putstr(newpacket.data+curbyte, clientinfo->lang, strlen(clientinfo->lang));
195         }
196     }
197
198   curbyte += aim_puttlv_16(newpacket.data+curbyte, 0x0009, 0x0015);
199 #endif
200
201   newpacket.lock = 0;
202   aim_tx_enqueue(sess, &newpacket);
203
204   return 0;
205 }
206
207 /*
208  *  int encode_password(
209  *                      const char *password,
210  *                      char *encoded
211  *                      ); 
212  *
213  * This takes a const pointer to a (null terminated) string
214  * containing the unencoded password.  It also gets passed
215  * an already allocated buffer to store the encoded password.
216  * This buffer should be the exact length of the password without
217  * the null.  The encoded password buffer IS NOT NULL TERMINATED.
218  *
219  * The encoding_table seems to be a fixed set of values.  We'll
220  * hope it doesn't change over time!  
221  *
222  */
223 int aim_encode_password(const char *password, u_char *encoded)
224 {
225   u_char encoding_table[] = {
226     0xf3, 0xb3, 0x6c, 0x99,
227     0x95, 0x3f, 0xac, 0xb6,
228     0xc5, 0xfa, 0x6b, 0x63,
229     0x69, 0x6c, 0xc3, 0x9f
230   };
231
232   int i;
233   
234   for (i = 0; i < strlen(password); i++)
235       encoded[i] = (password[i] ^ encoding_table[i]);
236
237   return 0;
238 }
239
240 /*
241  * This is sent back as a general response to the login command.
242  * It can be either an error or a success, depending on the
243  * precense of certain TLVs.  
244  *
245  * The client should check the value of logininfo->errorcode. If
246  * its nonzero, there was an error.
247  *
248  */
249 int aim_authparse(struct aim_session_t *sess, 
250                   struct command_rx_struct *command)
251 {
252   struct aim_tlvlist_t *tlvlist;
253   int ret = 1;
254   char *sn;
255   rxcallback_t userfunc = NULL;
256
257   memset(&sess->logininfo, 0x00, sizeof(sess->logininfo));
258
259   /*
260    * Read block of TLVs.  All further data is derived
261    * from what is parsed here.
262    */
263 #ifdef SNACLOGIN
264   tlvlist = aim_readtlvchain(command->data+10, command->commandlen-10);
265 #else
266   tlvlist = aim_readtlvchain(command->data, command->commandlen);
267 #endif
268   /*
269    * No matter what, we should have a screen name.
270    */
271   sn = aim_gettlv_str(tlvlist, 0x0001, 1);
272   memcpy(sess->logininfo.screen_name, sn, strlen(sn));
273   sn[(strlen(sn))] = '\0';
274   
275   /*
276    * Check for an error code.  If so, we should also
277    * have an error url.
278    */
279   if (aim_gettlv(tlvlist, 0x0008, 1))
280     {
281       struct aim_tlv_t *errtlv;
282       errtlv = aim_gettlv(tlvlist, 0x0008, 1);
283       sess->logininfo.errorcode = aimutil_get16(errtlv->value);
284       sess->logininfo.errorurl = aim_gettlv_str(tlvlist, 0x0004, 1);
285     }
286   /* 
287    * If we have both an IP number (0x0005) and a cookie (0x0006),
288    * then the login was successful.
289    */
290   else if (aim_gettlv(tlvlist, 0x0005, 1) && aim_gettlv(tlvlist, 0x0006, 1))
291     {
292       struct aim_tlv_t *tmptlv;
293
294       /*
295        * IP address of BOS server.
296        */
297       sess->logininfo.BOSIP = aim_gettlv_str(tlvlist, 0x0005, 1);
298
299       /*
300        * Authorization Cookie
301        */
302       tmptlv = aim_gettlv(tlvlist, 0x0006, 1);
303       memcpy(sess->logininfo.cookie, tmptlv->value, AIM_COOKIELEN);
304
305       /*
306        * The email address attached to this account
307        */
308       sess->logininfo.email = aim_gettlv_str(tlvlist, 0x0011, 1);
309
310       /*
311        * The registration status.  (Not real sure what it means.)
312        */
313       tmptlv = aim_gettlv(tlvlist, 0x0013, 1);
314       sess->logininfo.regstatus = aimutil_get16(tmptlv->value);
315       
316     }
317
318 #ifdef SNACLOGIN
319   userfunc = aim_callhandler(command->conn, 0x0017, 0x0003);
320 #else
321   userfunc = aim_callhandler(command->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_AUTHSUCCESS);
322 #endif
323   if (userfunc)
324     ret = userfunc(sess, command);
325
326   aim_freetlvchain(&tlvlist);
327
328   /* These have been clobbered by the freetlvchain */
329   sess->logininfo.BOSIP = NULL;
330   sess->logininfo.email = NULL;
331   sess->logininfo.errorurl = NULL;
332
333   return ret;
334 }
This page took 0.094353 seconds and 5 git commands to generate.