1 | /* passport.c |
---|
2 | * |
---|
3 | * Functions to login to microsoft passport service for Messenger |
---|
4 | * Copyright (C) 2004 Wouter Paesen <wouter@blue-gate.be> |
---|
5 | * Copyright (C) 2004 Wilmer van der Gaast <wilmer@gaast.net> |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License version 2 |
---|
9 | * as published by the Free Software Foundation |
---|
10 | * |
---|
11 | * This program is distributed in the hope that is will be useful, |
---|
12 | * bit WITHOU ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | #include "http_client.h" |
---|
23 | #include "passport.h" |
---|
24 | #include "msn.h" |
---|
25 | #include "bitlbee.h" |
---|
26 | #include <ctype.h> |
---|
27 | #include <errno.h> |
---|
28 | |
---|
29 | #define MSN_BUF_LEN 8192 |
---|
30 | |
---|
31 | static char *prd_cached = NULL; |
---|
32 | |
---|
33 | static int passport_get_id_real( gpointer func, gpointer data, char *header ); |
---|
34 | static void passport_get_id_ready( struct http_request *req ); |
---|
35 | |
---|
36 | static int passport_retrieve_dalogin( gpointer data, gpointer func, char *header ); |
---|
37 | static void passport_retrieve_dalogin_ready( struct http_request *req ); |
---|
38 | |
---|
39 | static char *passport_create_header( char *cookie, char *email, char *pwd ); |
---|
40 | static void destroy_reply( struct passport_reply *rep ); |
---|
41 | |
---|
42 | int passport_get_id( gpointer func, gpointer data, char *username, char *password, char *cookie ) |
---|
43 | { |
---|
44 | char *header = passport_create_header( cookie, username, password ); |
---|
45 | |
---|
46 | if( prd_cached == NULL ) |
---|
47 | return passport_retrieve_dalogin( func, data, header ); |
---|
48 | else |
---|
49 | return passport_get_id_real( func, data, header ); |
---|
50 | } |
---|
51 | |
---|
52 | static int passport_get_id_real( gpointer func, gpointer data, char *header ) |
---|
53 | { |
---|
54 | struct passport_reply *rep; |
---|
55 | char *server, *dummy, *reqs; |
---|
56 | struct http_request *req; |
---|
57 | |
---|
58 | rep = g_new0( struct passport_reply, 1 ); |
---|
59 | rep->data = data; |
---|
60 | rep->func = func; |
---|
61 | |
---|
62 | server = g_strdup( prd_cached ); |
---|
63 | dummy = strchr( server, '/' ); |
---|
64 | |
---|
65 | if( dummy == NULL ) |
---|
66 | { |
---|
67 | destroy_reply( rep ); |
---|
68 | return( 0 ); |
---|
69 | } |
---|
70 | |
---|
71 | reqs = g_strdup_printf( "GET %s HTTP/1.0\r\n%s\r\n\r\n", dummy, header ); |
---|
72 | |
---|
73 | *dummy = 0; |
---|
74 | req = http_dorequest( server, 443, 1, reqs, passport_get_id_ready, rep ); |
---|
75 | |
---|
76 | g_free( server ); |
---|
77 | g_free( reqs ); |
---|
78 | |
---|
79 | if( req == NULL ) |
---|
80 | destroy_reply( rep ); |
---|
81 | |
---|
82 | return( req != NULL ); |
---|
83 | } |
---|
84 | |
---|
85 | static void passport_get_id_ready( struct http_request *req ) |
---|
86 | { |
---|
87 | struct passport_reply *rep = req->data; |
---|
88 | |
---|
89 | if( !g_slist_find( msn_connections, rep->data ) ) |
---|
90 | { |
---|
91 | destroy_reply( rep ); |
---|
92 | return; |
---|
93 | } |
---|
94 | |
---|
95 | if( req->finished && req->reply_headers && req->status_code == 200 ) |
---|
96 | { |
---|
97 | char *dummy; |
---|
98 | |
---|
99 | if( ( dummy = strstr( req->reply_headers, "from-PP='" ) ) ) |
---|
100 | { |
---|
101 | char *responseend; |
---|
102 | |
---|
103 | dummy += strlen( "from-PP='" ); |
---|
104 | responseend = strchr( dummy, '\'' ); |
---|
105 | if( responseend ) |
---|
106 | *responseend = 0; |
---|
107 | |
---|
108 | rep->result = g_strdup( dummy ); |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | rep->error_string = g_strdup( "Could not parse Passport server response" ); |
---|
113 | } |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | rep->error_string = g_strdup_printf( "HTTP error: %s", |
---|
118 | req->status_string ? req->status_string : "Unknown error" ); |
---|
119 | } |
---|
120 | |
---|
121 | rep->func( rep ); |
---|
122 | destroy_reply( rep ); |
---|
123 | } |
---|
124 | |
---|
125 | static char *passport_create_header( char *cookie, char *email, char *pwd ) |
---|
126 | { |
---|
127 | char *buffer = g_new0( char, 2048 ); |
---|
128 | char *currenttoken; |
---|
129 | char *email_enc, *pwd_enc; |
---|
130 | |
---|
131 | email_enc = g_new0( char, strlen( email ) * 3 + 1 ); |
---|
132 | strcpy( email_enc, email ); |
---|
133 | http_encode( email_enc ); |
---|
134 | |
---|
135 | pwd_enc = g_new0( char, strlen( pwd ) * 3 + 1 ); |
---|
136 | strcpy( pwd_enc, pwd ); |
---|
137 | http_encode( pwd_enc ); |
---|
138 | |
---|
139 | currenttoken = strstr( cookie, "lc=" ); |
---|
140 | if( currenttoken == NULL ) |
---|
141 | return( NULL ); |
---|
142 | |
---|
143 | g_snprintf( buffer, 2048, |
---|
144 | "Authorization: Passport1.4 OrgVerb=GET," |
---|
145 | "OrgURL=http%%3A%%2F%%2Fmessenger%%2Emsn%%2Ecom," |
---|
146 | "sign-in=%s,pwd=%s,%s", email_enc, pwd_enc, |
---|
147 | currenttoken ); |
---|
148 | |
---|
149 | g_free( email_enc ); |
---|
150 | g_free( pwd_enc ); |
---|
151 | |
---|
152 | return( buffer ); |
---|
153 | } |
---|
154 | |
---|
155 | static int passport_retrieve_dalogin( gpointer func, gpointer data, char *header ) |
---|
156 | { |
---|
157 | struct passport_reply *rep = g_new0( struct passport_reply, 1 ); |
---|
158 | struct http_request *req; |
---|
159 | |
---|
160 | rep->data = data; |
---|
161 | rep->func = func; |
---|
162 | rep->header = header; |
---|
163 | |
---|
164 | req = http_dorequest_url( "https://nexus.passport.com/rdr/pprdr.asp", passport_retrieve_dalogin_ready, rep ); |
---|
165 | |
---|
166 | if( !req ) |
---|
167 | destroy_reply( rep ); |
---|
168 | |
---|
169 | return( req != NULL ); |
---|
170 | } |
---|
171 | |
---|
172 | static void passport_retrieve_dalogin_ready( struct http_request *req ) |
---|
173 | { |
---|
174 | struct passport_reply *rep = req->data; |
---|
175 | char *dalogin; |
---|
176 | char *urlend; |
---|
177 | |
---|
178 | if( !g_slist_find( msn_connections, rep->data ) ) |
---|
179 | { |
---|
180 | destroy_reply( rep ); |
---|
181 | return; |
---|
182 | } |
---|
183 | |
---|
184 | if( !req->finished || !req->reply_headers || req->status_code != 200 ) |
---|
185 | { |
---|
186 | rep->error_string = g_strdup_printf( "HTTP error while fetching DALogin: %s", |
---|
187 | req->status_string ? req->status_string : "Unknown error" ); |
---|
188 | goto failure; |
---|
189 | } |
---|
190 | |
---|
191 | dalogin = strstr( req->reply_headers, "DALogin=" ); |
---|
192 | |
---|
193 | if( !dalogin ) |
---|
194 | { |
---|
195 | rep->error_string = g_strdup( "Parse error while fetching DALogin" ); |
---|
196 | goto failure; |
---|
197 | } |
---|
198 | |
---|
199 | dalogin += strlen( "DALogin=" ); |
---|
200 | urlend = strchr( dalogin, ',' ); |
---|
201 | if( urlend ) |
---|
202 | *urlend = 0; |
---|
203 | |
---|
204 | /* strip the http(s):// part from the url */ |
---|
205 | urlend = strstr( urlend, "://" ); |
---|
206 | if( urlend ) |
---|
207 | dalogin = urlend + strlen( "://" ); |
---|
208 | |
---|
209 | if( prd_cached == NULL ) |
---|
210 | prd_cached = g_strdup( dalogin ); |
---|
211 | |
---|
212 | if( passport_get_id_real( rep->func, rep->data, rep->header ) ) |
---|
213 | { |
---|
214 | destroy_reply( rep ); |
---|
215 | return; |
---|
216 | } |
---|
217 | |
---|
218 | failure: |
---|
219 | rep->func( rep ); |
---|
220 | destroy_reply( rep ); |
---|
221 | } |
---|
222 | |
---|
223 | static void destroy_reply( struct passport_reply *rep ) |
---|
224 | { |
---|
225 | g_free( rep->result ); |
---|
226 | g_free( rep->header ); |
---|
227 | g_free( rep->error_string ); |
---|
228 | g_free( rep ); |
---|
229 | } |
---|