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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/*
Kopete Yahoo Protocol
Handles logging into to the Yahoo service
Copyright (c) 2004 Duncan Mac-Vicar P. <[email protected]>
Copyright (c) 2005-2006 André Duffeck <[email protected]>
Copyright 2009 Matt Rogers <[email protected]>
Kopete (c) 2002-2009 by the Kopete developers <[email protected]>
*************************************************************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
*************************************************************************
*/
#include "logintask.h"
#include "transfer.h"
#include "ymsgtransfer.h"
#include "yahootypes.h"
#include "client.h"
#include <tqstring.h>
#include <kdebug.h>
#include <stdlib.h>
#include <kio/job.h>
#include <kmdcodec.h>
extern "C"
{
#include "libyahoo.h"
}
LoginTask::LoginTask(Task* parent) : Task(parent)
{
kdDebug(YAHOO_RAW_DEBUG) ;
mState = InitialState;
}
LoginTask::~LoginTask()
{
}
bool LoginTask::take(Transfer* transfer)
{
/*
Yahoo login task has various stages
1 .- Initial State
1.1 .- OnGo is called
1.2 .- SendVerify() - send a service verify ack
2.- SentVerify
2.1 - take(), get a useless transfer, sendAuth is called
3.- SentAuth
2.2 - take(), get a transfer with login and challenge string
sendAuthResp is called.
2.3 - Need to decode and send a transfer back
4.- SentAuthResp
*/
if ( !forMe( transfer ) )
return false;
YMSGTransfer *t = static_cast<YMSGTransfer *>(transfer);
if ( t->service() == Yahoo::ServicePing) {
emit buddyListReady();
return true;
}
switch (mState)
{
case (InitialState):
client()->notifyError( "Error in login procedure.", "take called while in initial state", Client::Debug );
return false;
break;
case (SentVerify):
sendAuth( t );
return true;
break;
case (SentAuth):
sendAuthResp( t );
return true;
break;
case (SentAuthResp):
parseCookies( t );
handleAuthResp( t );
// Throw transfer to the next task as it contains further data
return false;
break;
default:
return false;
break;
}
}
bool LoginTask::forMe(const Transfer* transfer) const
{
const YMSGTransfer *t = 0L;
t = dynamic_cast<const YMSGTransfer*>(transfer);
if (!t)
return false;
if ( t->service() == Yahoo::ServicePing)
return true;
switch (mState)
{
case (InitialState):
//there shouldn't be a incoming transfer for this task at this state
return false;
break;
case (SentVerify):
if ( t->service() == Yahoo::ServiceVerify )
return true;
break;
case (SentAuth):
if ( t->service() == Yahoo::ServiceAuth )
return true;
break;
case (SentAuthResp ):
if ( t->service() == Yahoo::ServiceList ||
t->service() == Yahoo::ServiceAuthResp )
return true;
default:
return false;
break;
}
return false;
}
void LoginTask::onGo()
{
kdDebug(YAHOO_RAW_DEBUG) ;
/* initial state, we have to send a ServiceVerify */
if (mState == InitialState)
sendVerify();
else
client()->notifyError( "Error in login procedure.", "onGo called while not in initial state", Client::Debug );
}
void LoginTask::reset()
{
mState = InitialState;
}
void LoginTask::sendVerify()
{
/* send a ServiceVerify */
kdDebug(YAHOO_RAW_DEBUG) ;
YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceVerify);
send( t );
mState = SentVerify;
}
void LoginTask::sendAuth(YMSGTransfer* transfer)
{
kdDebug(YAHOO_RAW_DEBUG) ;
// transfer is the verify ack transfer, no useful data in it.
Q_UNUSED(transfer);
/* got ServiceVerify ACK, send a ServiceAuth with username */
kdDebug(YAHOO_RAW_DEBUG) ;
YMSGTransfer *t = new YMSGTransfer( Yahoo::ServiceAuth );
t->setParam( 1 , client()->userId().local8Bit() );
send(t);
mState = SentAuth;
}
void LoginTask::sendAuthResp(YMSGTransfer* t)
{
kdDebug(YAHOO_RAW_DEBUG) ;
TQString sn = t->firstParam( 1 );
TQString seed = t->firstParam( 94 );
m_challengeString = seed;
TQString version_s = t->firstParam( 13 );
m_sessionID = t->id();
int version = version_s.toInt();
switch (version)
{
case 0:
case 1:
case 2:
kdDebug(YAHOO_RAW_DEBUG) << "Using version 16 authorization" << endl;
sendAuthSixteenStage1(sn, seed);
break;
default:
kdDebug(YAHOO_RAW_DEBUG) << "Unknown authentication method used!"
<< "Attempting current authentication anyways" << endl;
sendAuthSixteenStage1(sn, seed);
break;
}
mState = SentAuthResp;
emit haveSessionID( m_sessionID );
}
void LoginTask::sendAuthSixteenStage1(const TQString& sn, const TQString& seed)
{
const TQString YahooTokenUrl = "https://login.yahoo.com/config/pwtoken_get?src=ymsgr&ts=&login=%1&passwd=%2&chal=%3";
kdDebug(YAHOO_RAW_DEBUG) << "seed:" << seed << endl;
m_stage1Data= TQString();
/* construct a URL from the seed and request tokens */
TQByteArray encodedUrl;
TQString fullUrl = YahooTokenUrl.arg(sn, client()->password(), seed);
KURL tokenUrl(fullUrl);
KIO::Job* job = KIO::get(tokenUrl, true, false);
connect(job, TQT_SIGNAL(data(KIO::Job*, const TQByteArray&)),
this, TQT_SLOT(handleAuthSixteenStage1Data(KIO::Job*, const TQByteArray&)));
connect(job, TQT_SIGNAL(result(KIO::Job*)),
this, TQT_SLOT(handleAuthSixteenStage1Result(KIO::Job*)));
}
void LoginTask::handleAuthSixteenStage1Data(KIO::Job* job, const TQByteArray& data)
{
kdDebug(YAHOO_RAW_DEBUG) << "data:" << data << endl;
m_stage1Data.append(data);
}
void LoginTask::handleAuthSixteenStage1Result(KIO::Job* job)
{
int responseNumber = -1;
TQString token;
int error = job->error();
kdDebug(YAHOO_RAW_DEBUG) << "error:" << error << endl;
if (error == 0)
{
TQStringList responses = TQStringList::split("\r\n", m_stage1Data);
responseNumber = responses[0].toInt();
if (responses.count() >= 3)
{
token = responses[1];
token.remove("ymsgr=");
kdDebug(YAHOO_RAW_DEBUG) << "response is:" << responseNumber << endl;
kdDebug(YAHOO_RAW_DEBUG) << "token is:" << token << endl;
}
if (responseNumber != 0)
{
switch(responseNumber)
{
case -1:
/* error in the received stream */
emit loginResponse(Yahoo::LoginSock, TQString());
kdDebug(YAHOO_RAW_DEBUG) << "unknown error logging in" << endl;
break;
case 1212:
/* password incorrect */
emit loginResponse(Yahoo::LoginPasswd, TQString());
kdDebug(YAHOO_RAW_DEBUG) << "password incorrect" << endl;
break;
case 1213:
/* security lock */
emit loginResponse(Yahoo::LoginLock, TQString());
break;
case 1235:
/* username does not exist */
emit loginResponse(Yahoo::LoginUname, TQString());
kdDebug(YAHOO_RAW_DEBUG) << "user does not exist" << endl;
break;
case 1214:
case 1236:
emit loginResponse(Yahoo::LoginVerify, TQString());
break;
case 100: /* username or password missing */
/*FIXME handle this */
break;
default:
/* FIXME unknown error. handle it! */
break;
}
}
else
{
/* start stage 2 here */
sendAuthSixteenStage2(token);
}
}
}
void LoginTask::sendAuthSixteenStage2(const TQString& token)
{
const TQString YahooLoginUrl = "https://login.yahoo.com/config/pwtoken_login?src=ymsgr&ts=&token=%1";
kdDebug(YAHOO_RAW_DEBUG) << "token:" << token << endl;
m_stage2Data = TQString();
TQString fullUrl = YahooLoginUrl.arg(token);
KURL loginUrl(fullUrl);
KIO::Job* job = KIO::get(loginUrl, true, false);
connect(job, TQT_SIGNAL(data(KIO::Job*, const TQByteArray&)),
this, TQT_SLOT(handleAuthSixteenStage2Data(KIO::Job*, const TQByteArray&)));
connect(job, TQT_SIGNAL(result(KIO::Job*)),
this, TQT_SLOT(handleAuthSixteenStage2Result(KIO::Job*)));
}
void LoginTask::handleAuthSixteenStage2Data(KIO::Job*, const TQByteArray& data)
{
kdDebug(YAHOO_RAW_DEBUG) << "data:" << data << endl;
m_stage2Data.append(data);
}
void LoginTask::handleAuthSixteenStage2Result(KIO::Job* job)
{
TQString crumb;
int responseNumber = -1;
int error = job->error();
kdDebug(YAHOO_RAW_DEBUG) << "error:" << error << endl;
if (error == 0)
{
TQStringList responses = TQStringList::split("\r\n", m_stage2Data);
kdDebug(YAHOO_RAW_DEBUG) << responses << endl;
responseNumber = responses[0].toInt();
if (responseNumber == 0)
{
crumb = responses[1];
crumb.remove("crumb=");
m_yCookie = responses[2].remove(0,2); /* remove Y= */
m_tCookie = responses[3].remove(0,2); /* remove T= */
}
if (responseNumber != 0)
{
switch(responseNumber)
{
case -1:
emit loginResponse(Yahoo::LoginSock, TQString());
break;
case 100:
emit loginResponse(Yahoo::LoginSock, TQString());
break;
default: /* try to login anyways */
break;
}
}
else
{
TQString cryptString = crumb;
cryptString.append(m_challengeString);
sendAuthSixteenStage3(cryptString);
}
}
}
void LoginTask::sendAuthSixteenStage3(const TQString& cryptString)
{
kdDebug(YAHOO_RAW_DEBUG) << " with crypt string" << cryptString << endl;
//TQByteArray cryptStringHash = TQCryptographicHash::hash( cryptString.toAscii(),
// TQCryptographicHash::Md5 );
//cryptStringHash = cryptStringHash.toBase64();
TQString cryptStringHash = KMD5( cryptString.ascii() ).base64Digest();
cryptStringHash = cryptStringHash.replace('+', '.');
cryptStringHash = cryptStringHash.replace('/', '_');
cryptStringHash = cryptStringHash.replace('=', '-');
YMSGTransfer *t = new YMSGTransfer(Yahoo::ServiceAuthResp, m_stateOnConnect);
t->setId( m_sessionID );
t->setParam( 1, client()->userId().local8Bit());
t->setParam( 0 , client()->userId().local8Bit());
t->setParam( 277, m_yCookie.local8Bit() );
t->setParam( 278, m_tCookie.local8Bit() );
t->setParam( 307, cryptStringHash.local8Bit() );
t->setParam( 244, 2097087 );
t->setParam( 2 , client()->userId().local8Bit());
t->setParam( 2, 1 ); // Both parameter 2s wind up in the packet
t->setParam( 135, YMSG_PROGRAM_VERSION_STRING );
send(t);
}
void LoginTask::sendAuthResp_pre_0x0b(const TQString &/*sn*/, const TQString &/*seed*/)
{
kdDebug(YAHOO_RAW_DEBUG) ;
}
void LoginTask::handleAuthResp(YMSGTransfer *t)
{
kdDebug(YAHOO_RAW_DEBUG) ;
switch( t->service() )
{
case( Yahoo::ServiceList ):
kdDebug(YAHOO_RAW_DEBUG) << "Emitting Signal" << endl;
emit loginResponse( Yahoo::LoginOk, TQString() );
break;
case( Yahoo::ServiceAuthResp ):
kdDebug(YAHOO_RAW_DEBUG) << "Emitting Signal" << endl;
emit loginResponse( t->firstParam( 66 ).toInt(), t->firstParam( 20 ) );
break;
default:
break;
}
mState = InitialState;
}
void LoginTask::setStateOnConnect( Yahoo::Status status )
{
m_stateOnConnect = status;
}
void LoginTask::parseCookies( YMSGTransfer *t )
{
kdDebug(YAHOO_RAW_DEBUG) ;
for( int i = 0; i < t->paramCount( 59 ); ++i)
{
TQString cookie;
cookie = t->nthParam( 59, i );
if( cookie.startsWith( "Y" ) )
{
m_yCookie = getcookie( cookie.latin1() );
m_loginCookie = getlcookie( cookie.latin1() );
}
else if( cookie.startsWith( "T" ) )
{
m_tCookie = getcookie( cookie.latin1() );
}
else if( cookie.startsWith( "C" ) )
{
m_cCookie = getcookie( cookie.latin1() );
}
}
if( !m_yCookie.isEmpty() && !m_tCookie.isEmpty())
emit haveCookies();
}
void LoginTask::setVerificationWord( const TQString &word )
{
m_verificationWord = word;
}
const TQString& LoginTask::yCookie()
{
return m_yCookie;
}
const TQString& LoginTask::tCookie()
{
return m_tCookie;
}
const TQString& LoginTask::cCookie()
{
return m_cCookie;
}
const TQString& LoginTask::loginCookie()
{
return m_loginCookie;
}
#include "logintask.moc"
|