forked from frankosterfeld/qtkeychain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keychain_haiku.cpp
187 lines (155 loc) · 5.13 KB
/
keychain_haiku.cpp
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
/******************************************************************************
* Copyright (C) 2018 François Revol <revol@free.fr> *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. For licensing and distribution *
* details, check the accompanying file 'COPYING'. *
*****************************************************************************/
#include "keychain_p.h"
#include <KeyStore.h>
#include <Application.h>
#include <AppFileInfo.h>
#include <File.h>
#include <QDebug>
#include <QCoreApplication>
#include <QString>
using namespace QKeychain;
class AutoApp {
public:
AutoApp();
~AutoApp();
BApplication *app;
};
AutoApp::AutoApp()
: app(NULL)
{
if (be_app != NULL)
return;
// no BApplication object, probably using QCoreApplication
// but we need one around
QString appSignature;
char signature[B_MIME_TYPE_LENGTH];
signature[0] = '\0';
QString appPath = QCoreApplication::applicationFilePath();
BFile appFile(appPath.toUtf8(), B_READ_ONLY);
if (appFile.InitCheck() == B_OK) {
BAppFileInfo info(&appFile);
if (info.InitCheck() == B_OK) {
if (info.GetSignature(signature) != B_OK)
signature[0] = '\0';
}
}
if (signature[0] != '\0')
appSignature = QLatin1String(signature);
else
appSignature = QLatin1String("application/x-vnd.qtkeychain-") +
QCoreApplication::applicationName().remove("_x86");
app = new BApplication(appSignature.toUtf8().constData());
}
AutoApp::~AutoApp()
{
delete app;
}
static QString strForStatus( status_t os ) {
const char * const buf = strerror(os) ;
return QObject::tr( "error 0x%1: %2" )
.arg( os, 8, 16 ).arg( QString::fromUtf8( buf, strlen( buf ) ) );
}
void ReadPasswordJobPrivate::scheduledStart()
{
AutoApp aa;
QString errorString;
Error error = NoError;
BKeyStore keyStore;
BPasswordKey password;
status_t result = keyStore.GetKey(B_KEY_TYPE_PASSWORD,
q->service().toUtf8().constData(),
q->key().toUtf8().constData(),
false, password);
data = QByteArray(reinterpret_cast<const char*>(password.Data()), password.DataLength());
switch ( result ) {
case B_OK:
q->emitFinished();
return;
case B_ENTRY_NOT_FOUND:
errorString = tr("Password not found");
error = EntryNotFound;
break;
default:
errorString = strForStatus( result );
error = OtherError;
break;
}
q->emitFinishedWithError( error, errorString );
}
void WritePasswordJobPrivate::scheduledStart()
{
AutoApp aa;
QString errorString;
Error error = NoError;
BKeyStore keyStore;
BPasswordKey password(data.constData(),
B_KEY_PURPOSE_GENERIC,
q->service().toUtf8().constData(),
q->key().toUtf8().constData());
status_t result = B_OK;
// re-add as binary if it's not text
if (mode == Binary)
result = password.SetData(reinterpret_cast<const uint8*>(data.constData()), data.size());
if (result == B_OK)
result = keyStore.AddKey(password);
if (result == B_NAME_IN_USE) {
BPasswordKey old_password;
result = keyStore.GetKey(B_KEY_TYPE_PASSWORD,
q->service().toUtf8().constData(),
q->key().toUtf8().constData(),
false, old_password);
if (result == B_OK)
result = keyStore.RemoveKey(old_password);
if (result == B_OK)
result = keyStore.AddKey(password);
}
switch ( result ) {
case B_OK:
q->emitFinished();
return;
case B_ENTRY_NOT_FOUND:
errorString = tr("Password not found");
error = EntryNotFound;
break;
default:
errorString = strForStatus( result );
error = OtherError;
break;
}
q->emitFinishedWithError( error, errorString );
}
void DeletePasswordJobPrivate::scheduledStart()
{
AutoApp aa;
QString errorString;
Error error = NoError;
BKeyStore keyStore;
BPasswordKey password;
status_t result = keyStore.GetKey(B_KEY_TYPE_PASSWORD,
q->service().toUtf8().constData(),
q->key().toUtf8().constData(),
false, password);
if (result == B_OK)
result = keyStore.RemoveKey(password);
switch ( result ) {
case B_OK:
q->emitFinished();
return;
case B_ENTRY_NOT_FOUND:
errorString = tr("Password not found");
error = EntryNotFound;
break;
default:
errorString = strForStatus( result );
error = CouldNotDeleteEntry;
break;
}
q->emitFinishedWithError( error, errorString );
}