summaryrefslogtreecommitdiff
path: root/AuthPlugin.cpp
blob: fcea81cbcbf05281bc5a0f9d1e86d24de3d3a3b6 (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
/** \file AuthPlugin.cpp
\brief Define the authentication plugin
\author alpha_one_x86
\version 0.3
\date 2010
\copyright GNU Public License 3 (GPL3), see the file COPYING */

#include <QDir>

#include "AuthPlugin.h"

AuthPlugin::AuthPlugin()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	//load the overall instance
	stopIt=false;
	//set sem to 1
	sem.release();
	moveToThread(this);
}

AuthPlugin::~AuthPlugin()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	stop();
}

QStringList AuthPlugin::getFileList(const QString &path)
{
	QStringList list;
	QDir dir(path);
	if(dir.exists())
	{
		foreach(QString dirName, dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot))
			list<<getFileList(path+dirName+QDir::separator());
		foreach(QString fileName, dir.entryList(QDir::Files|QDir::NoDotAndDotDot))
			list<<path+fileName;
	}
	return list;
}

void AuthPlugin::run()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	sem.acquire();
	stopIt=false;
	foreach(QString basePath,readPath)
	{
		foreach(QString dirSub,searchPathPlugin)
		{
			QString pluginComposed=basePath+dirSub+QDir::separator();
			QDir dir(pluginComposed);
			if(dir.exists())
			{
				foreach(QString dirName, dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot))
				{
					QString pluginBasePath	= pluginComposed+dirName+QDir::separator();
					if(QFile::exists(pluginBasePath+"sign.key"))
					{
						QCryptographicHash folderHash(QCryptographicHash::Sha1);
						QStringList filesList=getFileList(pluginBasePath);
						if(stopIt)
							return;
						QFile keyDescriptor(pluginBasePath+"sign.key");
						if(keyDescriptor.open(QIODevice::ReadOnly))
						{
							if(stopIt)
								return;
							bool errorFound=false;
							foreach(QString fileToAddToCheckSum,filesList)
								if(fileToAddToCheckSum!=(pluginBasePath+"sign.key"))
								{
									QFile fileDescriptor(fileToAddToCheckSum);
									if(fileDescriptor.open(QIODevice::ReadOnly))
									{
										if(stopIt)
											return;
										folderHash.addData(fileDescriptor.readAll());
										fileDescriptor.close();
										if(stopIt)
											return;
									}
									else
									{
										ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"Error at open this file: "+fileToAddToCheckSum+", error string: "+fileDescriptor.errorString());
										errorFound=true;
										break;
									}
								}
							if(!errorFound)
							{
								ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"folder: \""+pluginBasePath+"\", hash: "+QString(folderHash.result().toHex()));
								QByteArray key=keyDescriptor.readAll();
								if(key==folderHash.result())
									emit authentifiedPath(pluginBasePath);
								else
									ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"This plugin have wrong authentification");
							}
							keyDescriptor.close();
						}
						else
							ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"file: \""+pluginBasePath+"sign.key\", unable to open the key file: "+keyDescriptor.errorString());
					}
				}
			}
		}
	}
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"stop()");
	sem.release();
}

void AuthPlugin::loadSearchPath(const QStringList &readPath,const QStringList &searchPathPlugin)
{
	this->readPath=readPath;
	this->searchPathPlugin=searchPathPlugin;
}

void AuthPlugin::stop()
{
	stopIt=true;
	sem.acquire();
	sem.release();
}