summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMili Verma <mili.verma@isode.com>2015-06-25 12:13:14 (GMT)
committerKevin Smith <kevin.smith@isode.com>2015-06-29 17:02:01 (GMT)
commit394642c69bc232429621e209b787d3496967b37b (patch)
tree12fda8350b5694b4e6dc93899debc5a8c46e0adf /Swiften/SASL/WindowsServicePrincipalName.cpp
parentac45f360be049242a89ae80258e4ea8350f909ba (diff)
downloadswift-394642c69bc232429621e209b787d3496967b37b.zip
swift-394642c69bc232429621e209b787d3496967b37b.tar.bz2
Add WindowsServicePrincipalName class
Test-information: Tested on Windows using WIP GSSAPI code. Unit tests pass. Change-Id: If872863d6a8b5a164f8ebec4f88e9939b4e73c62
Diffstat (limited to 'Swiften/SASL/WindowsServicePrincipalName.cpp')
-rw-r--r--Swiften/SASL/WindowsServicePrincipalName.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/Swiften/SASL/WindowsServicePrincipalName.cpp b/Swiften/SASL/WindowsServicePrincipalName.cpp
new file mode 100644
index 0000000..187e9ac
--- /dev/null
+++ b/Swiften/SASL/WindowsServicePrincipalName.cpp
@@ -0,0 +1,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;
+}
+
+}