[0f4c2734] | 1 | /* passport.c |
---|
[b7d3cc34] | 2 | * |
---|
[0f4c2734] | 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> |
---|
[b7d3cc34] | 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 | |
---|
[ad8b8a3] | 22 | #include "http_client.h" |
---|
[b7d3cc34] | 23 | #include "passport.h" |
---|
| 24 | #include "msn.h" |
---|
| 25 | #include "bitlbee.h" |
---|
| 26 | #include <ctype.h> |
---|
| 27 | #include <errno.h> |
---|
| 28 | |
---|
[0f4c2734] | 29 | #define MSN_BUF_LEN 8192 |
---|
[b7d3cc34] | 30 | |
---|
[0f4c2734] | 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 ) |
---|
[b7d3cc34] | 43 | { |
---|
[0f4c2734] | 44 | char *header = passport_create_header( cookie, username, password ); |
---|
[e6648bf] | 45 | |
---|
[0f4c2734] | 46 | if( prd_cached == NULL ) |
---|
| 47 | return passport_retrieve_dalogin( func, data, header ); |
---|
| 48 | else |
---|
| 49 | return passport_get_id_real( func, data, header ); |
---|
[ad8b8a3] | 50 | } |
---|
| 51 | |
---|
[0f4c2734] | 52 | static int passport_get_id_real( gpointer func, gpointer data, char *header ) |
---|
[ad8b8a3] | 53 | { |
---|
[0f4c2734] | 54 | struct passport_reply *rep; |
---|
| 55 | char *server, *dummy, *reqs; |
---|
[ad8b8a3] | 56 | struct http_request *req; |
---|
| 57 | |
---|
[0f4c2734] | 58 | rep = g_new0( struct passport_reply, 1 ); |
---|
| 59 | rep->data = data; |
---|
| 60 | rep->func = func; |
---|
| 61 | rep->header = header; |
---|
| 62 | |
---|
| 63 | server = g_strdup( prd_cached ); |
---|
| 64 | dummy = strchr( server, '/' ); |
---|
| 65 | |
---|
| 66 | if( dummy == NULL ) |
---|
| 67 | { |
---|
| 68 | destroy_reply( rep ); |
---|
| 69 | return( 0 ); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | reqs = g_strdup_printf( "GET %s HTTP/1.0\r\n%s\r\n\r\n", dummy, header ); |
---|
[ad8b8a3] | 73 | |
---|
[0f4c2734] | 74 | *dummy = 0; |
---|
| 75 | req = http_dorequest( server, 443, 1, reqs, passport_get_id_ready, rep ); |
---|
[ad8b8a3] | 76 | |
---|
[0f4c2734] | 77 | g_free( server ); |
---|
| 78 | g_free( reqs ); |
---|
[ad8b8a3] | 79 | |
---|
[0f4c2734] | 80 | if( req == NULL ) |
---|
| 81 | destroy_reply( rep ); |
---|
[ad8b8a3] | 82 | |
---|
[0f4c2734] | 83 | return( req != NULL ); |
---|
[b7d3cc34] | 84 | } |
---|
| 85 | |
---|
[0f4c2734] | 86 | static void passport_get_id_ready( struct http_request *req ) |
---|
[ad8b8a3] | 87 | { |
---|
[0f4c2734] | 88 | struct passport_reply *rep = req->data; |
---|
[ad8b8a3] | 89 | |
---|
[0f4c2734] | 90 | if( !g_slist_find( msn_connections, rep->data ) ) |
---|
[a2582c8] | 91 | { |
---|
[0f4c2734] | 92 | destroy_reply( rep ); |
---|
| 93 | return; |
---|
[ad8b8a3] | 94 | } |
---|
| 95 | |
---|
[0f4c2734] | 96 | if( req->finished && req->reply_headers && req->status_code == 200 ) |
---|
[e6648bf] | 97 | { |
---|
[0f4c2734] | 98 | char *dummy; |
---|
| 99 | |
---|
| 100 | if( ( dummy = strstr( req->reply_headers, "from-PP='" ) ) ) |
---|
| 101 | { |
---|
| 102 | char *responseend; |
---|
| 103 | |
---|
| 104 | dummy += strlen( "from-PP='" ); |
---|
| 105 | responseend = strchr( dummy, '\'' ); |
---|
| 106 | if( responseend ) |
---|
| 107 | *responseend = 0; |
---|
| 108 | |
---|
| 109 | rep->result = g_strdup( dummy ); |
---|
| 110 | } |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | rep->error_string = g_strdup( "Could not parse Passport server response" ); |
---|
| 114 | } |
---|
[e6648bf] | 115 | } |
---|
| 116 | else |
---|
| 117 | { |
---|
[0f4c2734] | 118 | rep->error_string = g_strdup_printf( "HTTP error: %s", |
---|
| 119 | req->status_string ? req->status_string : "Unknown error" ); |
---|
[e6648bf] | 120 | } |
---|
[0f4c2734] | 121 | |
---|
| 122 | rep->func( rep ); |
---|
| 123 | destroy_reply( rep ); |
---|
[b7d3cc34] | 124 | } |
---|
| 125 | |
---|
[0f4c2734] | 126 | static char *passport_create_header( char *cookie, char *email, char *pwd ) |
---|
[b7d3cc34] | 127 | { |
---|
[0f4c2734] | 128 | char *buffer; |
---|
| 129 | char *currenttoken; |
---|
| 130 | char *email_enc, *pwd_enc; |
---|
| 131 | |
---|
| 132 | currenttoken = strstr( cookie, "lc=" ); |
---|
| 133 | if( currenttoken == NULL ) |
---|
| 134 | return NULL; |
---|
| 135 | |
---|
| 136 | email_enc = g_new0( char, strlen( email ) * 3 + 1 ); |
---|
| 137 | strcpy( email_enc, email ); |
---|
| 138 | http_encode( email_enc ); |
---|
| 139 | |
---|
| 140 | pwd_enc = g_new0( char, strlen( pwd ) * 3 + 1 ); |
---|
| 141 | g_snprintf( pwd_enc, 17, "%s", pwd ); /* Passwords >16 chars never succeed. (Bug #360) */ |
---|
| 142 | strcpy( pwd_enc, pwd ); |
---|
| 143 | http_encode( pwd_enc ); |
---|
[b7d3cc34] | 144 | |
---|
[0f4c2734] | 145 | buffer = g_strdup_printf( "Authorization: Passport1.4 OrgVerb=GET," |
---|
| 146 | "OrgURL=http%%3A%%2F%%2Fmessenger%%2Emsn%%2Ecom," |
---|
| 147 | "sign-in=%s,pwd=%s,%s", email_enc, pwd_enc, |
---|
| 148 | currenttoken ); |
---|
[b7d3cc34] | 149 | |
---|
[0f4c2734] | 150 | g_free( email_enc ); |
---|
| 151 | g_free( pwd_enc ); |
---|
| 152 | |
---|
| 153 | return buffer; |
---|
[b7d3cc34] | 154 | } |
---|
| 155 | |
---|
[0f4c2734] | 156 | static int passport_retrieve_dalogin( gpointer func, gpointer data, char *header ) |
---|
[b7d3cc34] | 157 | { |
---|
[0f4c2734] | 158 | struct passport_reply *rep = g_new0( struct passport_reply, 1 ); |
---|
| 159 | struct http_request *req; |
---|
[b7d3cc34] | 160 | |
---|
[0f4c2734] | 161 | rep->data = data; |
---|
| 162 | rep->func = func; |
---|
| 163 | rep->header = header; |
---|
[a2582c8] | 164 | |
---|
[0f4c2734] | 165 | req = http_dorequest_url( "https://nexus.passport.com/rdr/pprdr.asp", passport_retrieve_dalogin_ready, rep ); |
---|
| 166 | |
---|
| 167 | if( !req ) |
---|
| 168 | destroy_reply( rep ); |
---|
| 169 | |
---|
| 170 | return( req != NULL ); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | static void passport_retrieve_dalogin_ready( struct http_request *req ) |
---|
| 174 | { |
---|
| 175 | struct passport_reply *rep = req->data; |
---|
| 176 | char *dalogin; |
---|
| 177 | char *urlend; |
---|
| 178 | |
---|
| 179 | if( !g_slist_find( msn_connections, rep->data ) ) |
---|
| 180 | { |
---|
| 181 | destroy_reply( rep ); |
---|
| 182 | return; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | if( !req->finished || !req->reply_headers || req->status_code != 200 ) |
---|
| 186 | { |
---|
| 187 | rep->error_string = g_strdup_printf( "HTTP error while fetching DALogin: %s", |
---|
| 188 | req->status_string ? req->status_string : "Unknown error" ); |
---|
| 189 | goto failure; |
---|
| 190 | } |
---|
[b7d3cc34] | 191 | |
---|
[0f4c2734] | 192 | dalogin = strstr( req->reply_headers, "DALogin=" ); |
---|
| 193 | |
---|
| 194 | if( !dalogin ) |
---|
| 195 | { |
---|
| 196 | rep->error_string = g_strdup( "Parse error while fetching DALogin" ); |
---|
| 197 | goto failure; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | dalogin += strlen( "DALogin=" ); |
---|
| 201 | urlend = strchr( dalogin, ',' ); |
---|
| 202 | if( urlend ) |
---|
| 203 | *urlend = 0; |
---|
| 204 | |
---|
| 205 | /* strip the http(s):// part from the url */ |
---|
| 206 | urlend = strstr( urlend, "://" ); |
---|
| 207 | if( urlend ) |
---|
| 208 | dalogin = urlend + strlen( "://" ); |
---|
| 209 | |
---|
| 210 | if( prd_cached == NULL ) |
---|
| 211 | prd_cached = g_strdup( dalogin ); |
---|
| 212 | |
---|
| 213 | if( passport_get_id_real( rep->func, rep->data, rep->header ) ) |
---|
| 214 | { |
---|
| 215 | rep->header = NULL; |
---|
| 216 | destroy_reply( rep ); |
---|
| 217 | return; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | failure: |
---|
| 221 | rep->func( rep ); |
---|
| 222 | destroy_reply( rep ); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | static void destroy_reply( struct passport_reply *rep ) |
---|
| 226 | { |
---|
| 227 | g_free( rep->result ); |
---|
| 228 | g_free( rep->header ); |
---|
| 229 | g_free( rep->error_string ); |
---|
| 230 | g_free( rep ); |
---|
[b7d3cc34] | 231 | } |
---|