[b7d3cc34] | 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 | |
---|
[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 | |
---|
| 29 | #define MSN_BUF_LEN 8192 |
---|
| 30 | |
---|
| 31 | static char *prd_cached = NULL; |
---|
| 32 | |
---|
[ad8b8a3] | 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 | |
---|
[b7d3cc34] | 36 | static int passport_retrieve_dalogin( gpointer data, gpointer func, char *header ); |
---|
[ad8b8a3] | 37 | static void passport_retrieve_dalogin_ready( struct http_request *req ); |
---|
[b7d3cc34] | 38 | |
---|
[ad8b8a3] | 39 | static char *passport_create_header( char *cookie, char *email, char *pwd ); |
---|
| 40 | static void destroy_reply( struct passport_reply *rep ); |
---|
[b7d3cc34] | 41 | |
---|
[ad8b8a3] | 42 | int passport_get_id( gpointer func, gpointer data, char *username, char *password, char *cookie ) |
---|
[b7d3cc34] | 43 | { |
---|
| 44 | char *header = passport_create_header( cookie, username, password ); |
---|
| 45 | |
---|
[ad8b8a3] | 46 | if( prd_cached == NULL ) |
---|
| 47 | return passport_retrieve_dalogin( func, data, header ); |
---|
[b7d3cc34] | 48 | else |
---|
[ad8b8a3] | 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 ) |
---|
[b7d3cc34] | 66 | { |
---|
[ad8b8a3] | 67 | destroy_reply( rep ); |
---|
| 68 | return( 0 ); |
---|
[b7d3cc34] | 69 | } |
---|
[ad8b8a3] | 70 | |
---|
| 71 | reqs = g_malloc( strlen( header ) + strlen( dummy ) + 128 ); |
---|
| 72 | sprintf( reqs, "GET %s HTTP/1.0\r\n%s\r\n\r\n", dummy, header ); |
---|
| 73 | |
---|
| 74 | *dummy = 0; |
---|
| 75 | req = http_dorequest( server, 443, 1, reqs, passport_get_id_ready, rep ); |
---|
| 76 | |
---|
| 77 | g_free( server ); |
---|
| 78 | g_free( reqs ); |
---|
| 79 | |
---|
| 80 | if( req == NULL ) |
---|
| 81 | destroy_reply( rep ); |
---|
| 82 | |
---|
| 83 | return( req != NULL ); |
---|
[b7d3cc34] | 84 | } |
---|
| 85 | |
---|
[ad8b8a3] | 86 | static void passport_get_id_ready( struct http_request *req ) |
---|
| 87 | { |
---|
| 88 | struct passport_reply *rep = req->data; |
---|
| 89 | |
---|
[43f205b] | 90 | if( !g_slist_find( msn_connections, rep->data ) || !req->finished || !req->reply_headers ) |
---|
[ad8b8a3] | 91 | { |
---|
| 92 | destroy_reply( rep ); |
---|
| 93 | return; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | if( req->status_code == 200 ) |
---|
| 97 | { |
---|
| 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 | } |
---|
| 112 | |
---|
| 113 | rep->func( rep ); |
---|
| 114 | destroy_reply( rep ); |
---|
| 115 | } |
---|
[b7d3cc34] | 116 | |
---|
[ad8b8a3] | 117 | static char *passport_create_header( char *cookie, char *email, char *pwd ) |
---|
[b7d3cc34] | 118 | { |
---|
| 119 | char *buffer = g_new0( char, 2048 ); |
---|
| 120 | char *currenttoken; |
---|
| 121 | char *email_enc, *pwd_enc; |
---|
| 122 | |
---|
| 123 | email_enc = g_new0( char, strlen( email ) * 3 + 1 ); |
---|
| 124 | strcpy( email_enc, email ); |
---|
| 125 | http_encode( email_enc ); |
---|
| 126 | |
---|
| 127 | pwd_enc = g_new0( char, strlen( pwd ) * 3 + 1 ); |
---|
| 128 | strcpy( pwd_enc, pwd ); |
---|
| 129 | http_encode( pwd_enc ); |
---|
| 130 | |
---|
[ad8b8a3] | 131 | currenttoken = strstr( cookie, "lc=" ); |
---|
[b7d3cc34] | 132 | if( currenttoken == NULL ) |
---|
| 133 | return( NULL ); |
---|
| 134 | |
---|
| 135 | g_snprintf( buffer, 2048, |
---|
| 136 | "Authorization: Passport1.4 OrgVerb=GET," |
---|
| 137 | "OrgURL=http%%3A%%2F%%2Fmessenger%%2Emsn%%2Ecom," |
---|
| 138 | "sign-in=%s,pwd=%s,%s", email_enc, pwd_enc, |
---|
| 139 | currenttoken ); |
---|
| 140 | |
---|
| 141 | g_free( email_enc ); |
---|
| 142 | g_free( pwd_enc ); |
---|
| 143 | |
---|
| 144 | return( buffer ); |
---|
| 145 | } |
---|
| 146 | |
---|
[ad8b8a3] | 147 | #define PPR_REQUEST "GET /rdr/pprdr.asp HTTP/1.0\r\n\r\n" |
---|
| 148 | static int passport_retrieve_dalogin( gpointer func, gpointer data, char *header ) |
---|
[b7d3cc34] | 149 | { |
---|
| 150 | struct passport_reply *rep = g_new0( struct passport_reply, 1 ); |
---|
[ad8b8a3] | 151 | struct http_request *req; |
---|
[b7d3cc34] | 152 | |
---|
| 153 | rep->data = data; |
---|
| 154 | rep->func = func; |
---|
| 155 | rep->header = header; |
---|
| 156 | |
---|
[ad8b8a3] | 157 | req = http_dorequest( "nexus.passport.com", 443, 1, PPR_REQUEST, passport_retrieve_dalogin_ready, rep ); |
---|
[b7d3cc34] | 158 | |
---|
[ad8b8a3] | 159 | if( !req ) |
---|
[b7d3cc34] | 160 | destroy_reply( rep ); |
---|
| 161 | |
---|
[ad8b8a3] | 162 | return( req != NULL ); |
---|
[b7d3cc34] | 163 | } |
---|
| 164 | |
---|
[ad8b8a3] | 165 | static void passport_retrieve_dalogin_ready( struct http_request *req ) |
---|
[b7d3cc34] | 166 | { |
---|
[ad8b8a3] | 167 | struct passport_reply *rep = req->data; |
---|
| 168 | char *dalogin; |
---|
| 169 | char *urlend; |
---|
[b7d3cc34] | 170 | |
---|
[43f205b] | 171 | if( !g_slist_find( msn_connections, rep->data ) || !req->finished || !req->reply_headers ) |
---|
[b7d3cc34] | 172 | { |
---|
| 173 | destroy_reply( rep ); |
---|
| 174 | return; |
---|
| 175 | } |
---|
| 176 | |
---|
[ad8b8a3] | 177 | dalogin = strstr( req->reply_headers, "DALogin=" ); |
---|
[b7d3cc34] | 178 | |
---|
[ad8b8a3] | 179 | if( !dalogin ) |
---|
[b7d3cc34] | 180 | goto failure; |
---|
| 181 | |
---|
[ad8b8a3] | 182 | dalogin += strlen( "DALogin=" ); |
---|
| 183 | urlend = strchr( dalogin, ',' ); |
---|
| 184 | if( urlend ) |
---|
| 185 | *urlend = 0; |
---|
[b7d3cc34] | 186 | |
---|
[ad8b8a3] | 187 | /* strip the http(s):// part from the url */ |
---|
| 188 | urlend = strstr( urlend, "://" ); |
---|
| 189 | if( urlend ) |
---|
| 190 | dalogin = urlend + strlen( "://" ); |
---|
[b7d3cc34] | 191 | |
---|
[ad8b8a3] | 192 | if( prd_cached == NULL ) |
---|
| 193 | prd_cached = g_strdup( dalogin ); |
---|
[b7d3cc34] | 194 | |
---|
[ad8b8a3] | 195 | if( passport_get_id_real( rep->func, rep->data, rep->header ) ) |
---|
[b7d3cc34] | 196 | { |
---|
| 197 | destroy_reply( rep ); |
---|
| 198 | return; |
---|
| 199 | } |
---|
| 200 | |
---|
[ad8b8a3] | 201 | failure: |
---|
[b7d3cc34] | 202 | rep->func( rep ); |
---|
| 203 | destroy_reply( rep ); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | static void destroy_reply( struct passport_reply *rep ) |
---|
| 207 | { |
---|
[ad8b8a3] | 208 | g_free( rep->result ); |
---|
| 209 | g_free( rep->header ); |
---|
| 210 | g_free( rep ); |
---|
[b7d3cc34] | 211 | } |
---|