summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/smpppdcs/libsmpppdclient/smpppdunsettled.cpp
blob: 3d1f8a79af4f609b7ac0e0b17b14e2b539465523 (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
/*
	smpppdunsettled.cpp
 
	Copyright (c) 2006      by Heiko Schaefer        <heiko@rangun.de>
 
	Kopete    (c) 2002-2006 by the Kopete developers <kopete-devel@kde.org>
 
	*************************************************************************
	*                                                                       *
	* This program is free software; you can redistribute it and/or modify  *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; version 2 of the License.               *
	*                                                                       *
	*************************************************************************
*/

#include <cstdlib>
#include <openssl/md5.h>

#include <tqregexp.h>

#include <kdebug.h>
#include <kstreamsocket.h>

#include "smpppdready.h"
#include "smpppdunsettled.h"

using namespace SMPPPD;

Unsettled * Unsettled::m_instance = NULL;

Unsettled::Unsettled() {}

Unsettled::~Unsettled() {}

Unsettled * Unsettled::instance() {
    if(!m_instance) {
        m_instance = new Unsettled();
    }

    return m_instance;
}

bool Unsettled::connect(Client * client, const TQString& server, uint port) {
	if(!socket(client) ||
		socket(client)->state() != KNetwork::KStreamSocket::Connected ||
		socket(client)->state() != KNetwork::KStreamSocket::Connecting) {

        TQString resolvedServer = server;

		changeState(client, Ready::instance());
        disconnect(client);

        // since a lookup on a non-existant host can take a lot of time we
        // try to get the IP of server before and we do the lookup ourself
        KNetwork::KResolver resolver(server);
        resolver.start();
        if(resolver.wait(500)) {
            KNetwork::KResolverResults results = resolver.results();
            if(!results.empty()) {
                TQString ip = results[0].address().asInet().ipAddress().toString();
                kdDebug(14312) << k_funcinfo << "Found IP-Address for " << server << ": " << ip << endl;
                resolvedServer = ip;
            } else {
                kdWarning(14312) << k_funcinfo << "No IP-Address found for " << server << endl;
                return false;
            }
        } else {
            kdWarning(14312) << k_funcinfo << "Looking up hostname timed out, consider to use IP or correct host" << endl;
            return false;
        }

		setSocket(client, new KNetwork::KStreamSocket(resolvedServer, TQString::number(port)));
		socket(client)->setBlocking(TRUE);

		if(!socket(client)->connect()) {
			kdDebug(14312) << k_funcinfo << "Socket Error: " << KNetwork::KStreamSocket::errorString(socket(client)->error()) << endl;
        } else {
            kdDebug(14312) << k_funcinfo << "Successfully connected to smpppd \"" << server << ":" << port << "\"" << endl;

            static TQString verRex = "^SuSE Meta pppd \\(smpppd\\), Version (.*)$";
            static TQString clgRex = "^challenge = (.*)$";

            TQRegExp ver(verRex);
            TQRegExp clg(clgRex);

            TQString response = read(client)[0];

            if(response != TQString::null &&
                    ver.exactMatch(response)) {
				setServerID(client, response);
				setServerVersion(client, ver.cap(1));
                changeState(client, Ready::instance());
                return true;
            } else if(response != TQString::null &&
                      clg.exactMatch(response)) {
				if(password(client) != TQString::null) {
                    // we are challenged, ok, respond
					write(client, TQString("response = %1\n").arg(make_response(clg.cap(1).stripWhiteSpace(), password(client))).latin1());
                    response = read(client)[0];
                    if(ver.exactMatch(response)) {
						setServerID(client, response);
						setServerVersion(client, ver.cap(1));
                        return true;
                    } else {
                        kdWarning(14312) << k_funcinfo << "SMPPPD responded: " << response << endl;
						changeState(client, Ready::instance());
                        disconnect(client);
                    }
                } else {
                    kdWarning(14312) << k_funcinfo << "SMPPPD requested a challenge, but no password was supplied!" << endl;
					changeState(client, Ready::instance());
                    disconnect(client);
                }
            }
        }
    }

    return false;
}

TQString Unsettled::make_response(const TQString& chex, const TQString& password) const {

	int size = chex.length ();
	if (size & 1)
		return "error";
	size >>= 1;

    // convert challenge from hex to bin
	TQString cbin;
	for (int i = 0; i < size; i++) {
		TQString tmp = chex.mid (2 * i, 2);
		cbin.append ((char) strtol (tmp.ascii (), 0, 16));
	}

    // calculate response
	unsigned char rbin[MD5_DIGEST_LENGTH];
	MD5state_st md5;
	MD5_Init (&md5);
	MD5_Update (&md5, cbin.ascii (), size);
	MD5_Update (&md5, password.ascii(), password.length ());
	MD5_Final (rbin, &md5);

    // convert response from bin to hex
	TQString rhex;
	for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
		char buffer[3];
		snprintf (buffer, 3, "%02x", rbin[i]);
		rhex.append (buffer);
	}

	return rhex;
}