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
|
/*
* Copyright (c) 2015 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/SASL/WindowsServicePrincipalName.h>
#include <vector>
#include <Ntdsapi.h>
#include <Swiften/Base/ByteArray.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Base/String.h>
namespace Swift {
WindowsServicePrincipalName::WindowsServicePrincipalName(const std::string& serviceName) : serviceClass_(L"xmpp"), instancePort_(0) {
serviceName_ = convertStringToWString(serviceName);
}
WindowsServicePrincipalName::~WindowsServicePrincipalName() {
}
void WindowsServicePrincipalName::setServiceClass(const std::string& serviceClass) {
serviceClass_ = convertStringToWString(serviceClass);
}
void WindowsServicePrincipalName::setInstanceName(const std::string& instanceName) {
instanceName_ = convertStringToWString(instanceName);
}
void WindowsServicePrincipalName::setReferrer(const std::string& referrer) {
referrer_ = convertStringToWString(referrer);
}
std::string WindowsServicePrincipalName::toString() {
DWORD length = 512;
DWORD status = ERROR_BUFFER_OVERFLOW;
bool firstCall = true;
std::string str;
while (status == ERROR_BUFFER_OVERFLOW) {
std::vector<wchar_t> value(length);
/* length after this call will contain the required length if current length is not enough - so the next call should succeed */
status = dsMakeSpn(&length, vecptr(value));
if (status == ERROR_SUCCESS) {
str = convertWStringToString(std::wstring(vecptr(value), length-1 /* trailing 0 character */));
break;
}
if ((firstCall == false) || (status != ERROR_BUFFER_OVERFLOW)) {
std::stringstream errorString;
boost::system::error_code errorCode(status, boost::system::system_category());
errorString << "Error creating Service Principal Name: status: 0x" << std::hex << status << ": " << errorCode.message();
/* Any other error will be a programming error */
throw std::runtime_error(errorString.str());
}
firstCall = false;
}
SWIFT_LOG(debug) << "SPN: " << str << std::endl;
return str;
}
DWORD WindowsServicePrincipalName::dsMakeSpn(DWORD* length, wchar_t* value) {
DWORD status;
#ifdef UNICODE
SWIFT_LOG(debug) << "UNICODE is defined" << std::endl;
#else
SWIFT_LOG(debug) << "UNICODE is not defined" << std::endl;
#endif
SWIFT_LOG(debug) << "serviceClass_: " << convertWStringToString(serviceClass_.c_str()) << std::endl;
SWIFT_LOG(debug) << "serviceName_: " << convertWStringToString(serviceName_.c_str()) << std::endl;
SWIFT_LOG(debug) << "instanceName_: " << convertWStringToString(instanceName_.c_str()) << std::endl;
SWIFT_LOG(debug) << "referrer_: " << convertWStringToString(referrer_.c_str()) << std::endl;
SWIFT_LOG(debug) << "instancePort_: " << instancePort_ << std::endl;
SWIFT_LOG(debug) << "length: " << *length << std::endl;
/* Call the Unicode function because that is recommended:
https://msdn.microsoft.com/en-us/library/windows/desktop/ff381407%28v=vs.85%29.aspx */
status = DsMakeSpnW(
serviceClass_.c_str(),
serviceName_.c_str(),
instanceName_.empty() ? NULL : instanceName_.c_str(),
instancePort_,
referrer_.empty() ? NULL : referrer_.c_str(),
length,
value);
if (status != ERROR_SUCCESS) {
boost::system::error_code errorCode(status, boost::system::system_category());
SWIFT_LOG(debug) << std::hex << "status: 0x" << status << ": " << errorCode.message() << std::endl;
}
return status;
}
}
|