aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/msn/passport.c
blob: 347034325e229f6d374b4ebd5718e6098d55b7d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* passport.c
 *
 * Functions to login to microsoft passport service for Messenger
 * Copyright (C) 2004 Wouter Paesen <wouter@blue-gate.be>
 * Copyright (C) 2004 Wilmer van der Gaast <wilmer@gaast.net>
 *
 * This program is free software; you can redistribute it and/or modify             
 * it under the terms of the GNU General Public License version 2                   
 * as published by the Free Software Foundation                                     
 *                                                                                   
 * This program is distributed in the hope that is will be useful,                  
 * bit WITHOU ANY WARRANTY; without even the implied warranty of                   
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    
 * GNU General Public License for more details.                                     
 *                                                                                   
 * You should have received a copy of the GNU General Public License                
 * along with this program; if not, write to the Free Software                      
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA          
 *
 */

#include "http_client.h"
#include "passport.h"
#include "msn.h"
#include "bitlbee.h"
#include <ctype.h>
#include <errno.h>

#define MSN_BUF_LEN 8192

static char *prd_cached = NULL;

static int passport_get_id_real( gpointer func, gpointer data, char *header );
static void passport_get_id_ready( struct http_request *req );

static int passport_retrieve_dalogin( gpointer data, gpointer func, char *header );
static void passport_retrieve_dalogin_ready( struct http_request *req );

static char *passport_create_header( char *cookie, char *email, char *pwd );
static void destroy_reply( struct passport_reply *rep );

int passport_get_id( gpointer func, gpointer data, char *username, char *password, char *cookie )
{
	char *header = passport_create_header( cookie, username, password );
	
	if( prd_cached == NULL )
		return passport_retrieve_dalogin( func, data, header );
	else
		return passport_get_id_real( func, data, header );
}

static int passport_get_id_real( gpointer func, gpointer data, char *header )
{
	struct passport_reply *rep;
	char *server, *dummy, *reqs;
	struct http_request *req;
	
	rep = g_new0( struct passport_reply, 1 );
	rep->data = data;
	rep->func = func;
	
	server = g_strdup( prd_cached );
	dummy = strchr( server, '/' );
	
	if( dummy == NULL )
	{
		destroy_reply( rep );
		return( 0 );
	}
	
	reqs = g_malloc( strlen( header ) + strlen( dummy ) + 128 );
	sprintf( reqs, "GET %s HTTP/1.0\r\n%s\r\n\r\n", dummy, header );
	
	*dummy = 0;
	req = http_dorequest( server, 443, 1, reqs, passport_get_id_ready, rep );
	
	g_free( server );
	g_free( reqs );
	
	if( req == NULL )
		destroy_reply( rep );
	
	return( req != NULL );
}

static void passport_get_id_ready( struct http_request *req )
{
	struct passport_reply *rep = req->data;
	
	if( !g_slist_find( msn_connections, rep->data ) || !req->finished || !req->reply_headers )
	{
		destroy_reply( rep );
		return;
	}
	
	if( req->status_code == 200 )
	{
		char *dummy;
		
		if( ( dummy = strstr( req->reply_headers, "from-PP='" ) ) )
		{
			char *responseend;
			
			dummy += strlen( "from-PP='" );
			responseend = strchr( dummy, '\'' );
			if( responseend )
				*responseend = 0;
			
			rep->result = g_strdup( dummy );
		}
	}
	
	rep->func( rep );
	destroy_reply( rep );
}

static char *passport_create_header( char *cookie, char *email, char *pwd )
{
	char *buffer = g_new0( char, 2048 );
	char *currenttoken;
	char *email_enc, *pwd_enc;
	
	email_enc = g_new0( char, strlen( email ) * 3 + 1 );
	strcpy( email_enc, email );
	http_encode( email_enc );
	
	pwd_enc = g_new0( char, strlen( pwd ) * 3 + 1 );
	strcpy( pwd_enc, pwd );
	http_encode( pwd_enc );
	
	currenttoken = strstr( cookie, "lc=" );
	if( currenttoken == NULL )
		return( NULL );
	
	g_snprintf( buffer, 2048,
	            "Authorization: Passport1.4 OrgVerb=GET,"
	            "OrgURL=http%%3A%%2F%%2Fmessenger%%2Emsn%%2Ecom,"
	            "sign-in=%s,pwd=%s,%s", email_enc, pwd_enc,
	            currenttoken );
	
	g_free( email_enc );
	g_free( pwd_enc );
	
	return( buffer );
}

#define PPR_REQUEST "GET /rdr/pprdr.asp HTTP/1.0\r\n\r\n"
static int passport_retrieve_dalogin( gpointer func, gpointer data, char *header )
{
	struct passport_reply *rep = g_new0( struct passport_reply, 1 );
	struct http_request *req;
	
	rep->data = data;
	rep->func = func;
	rep->header = header;
	
	req = http_dorequest( "nexus.passport.com", 443, 1, PPR_REQUEST, passport_retrieve_dalogin_ready, rep );
	
	if( !req )
		destroy_reply( rep );
	
	return( req != NULL );
}

static void passport_retrieve_dalogin_ready( struct http_request *req )
{
	struct passport_reply *rep = req->data;
	char *dalogin;
	char *urlend;
	
	if( !g_slist_find( msn_connections, rep->data ) || !req->finished || !req->reply_headers )
	{
		destroy_reply( rep );
		return;
	}
	
	dalogin = strstr( req->reply_headers, "DALogin=" );	
	
	if( !dalogin )
		goto failure;
	
	dalogin += strlen( "DALogin=" );
	urlend = strchr( dalogin, ',' );
	if( urlend )
		*urlend = 0;
	
	/* strip the http(s):// part from the url */
	urlend = strstr( urlend, "://" );
	if( urlend )
		dalogin = urlend + strlen( "://" );
	
	if( prd_cached == NULL )
		prd_cached = g_strdup( dalogin );
	
	if( passport_get_id_real( rep->func, rep->data, rep->header ) )
	{
		destroy_reply( rep );
		return;
	}
	
failure:	
	rep->func( rep );
	destroy_reply( rep );
}

static void destroy_reply( struct passport_reply *rep )
{
	g_free( rep->result );
	g_free( rep->header );
	g_free( rep );
}