|
| 1 | +/* |
| 2 | + * RDPShare: Windows Desktop Sharing remote control interface |
| 3 | + * |
| 4 | + * Copyright 2016 Simul Piscator |
| 5 | + * |
| 6 | + * RDPShare is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * RDPShare is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with RDPShare. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +#include "RDPFile.h" |
| 20 | +#include "Utils.h" |
| 21 | +#include <vector> |
| 22 | +#include <sstream> |
| 23 | +#include <ctime> |
| 24 | + |
| 25 | +static void ParseConnectionString( |
| 26 | + const std::string& s, |
| 27 | + std::string& RASpecificParams, |
| 28 | + std::string& RASessionID, |
| 29 | + std::vector<std::pair<std::string,std::string>>& Addresses |
| 30 | +) |
| 31 | +{ |
| 32 | + size_t begin = s.find("KH=\""), end = begin; |
| 33 | + if (begin != s.npos) |
| 34 | + { |
| 35 | + begin = s.find('"', begin) + 1; |
| 36 | + end = s.find('"', begin); |
| 37 | + RASpecificParams = s.substr(begin, end - begin); |
| 38 | + } |
| 39 | + begin = s.find("ID=\""), end = begin; |
| 40 | + if (begin != s.npos) |
| 41 | + { |
| 42 | + begin = s.find('"', begin) + 1; |
| 43 | + end = s.find('"', begin); |
| 44 | + RASessionID = s.substr(begin, end - begin); |
| 45 | + } |
| 46 | + begin = 0, end = 0; |
| 47 | + while ((begin = s.find("<L P=\"", end)) != s.npos) |
| 48 | + { |
| 49 | + begin = s.find('"', begin) + 1; |
| 50 | + end = s.find('"', begin); |
| 51 | + std::string port = s.substr(begin, end - begin); |
| 52 | + begin = s.find("N=\"", end); |
| 53 | + if (begin != s.npos) |
| 54 | + { |
| 55 | + begin = s.find('"', begin) + 1; |
| 56 | + end = s.find('"', begin); |
| 57 | + Addresses.push_back( std::make_pair(s.substr(begin, end - begin),port) ); |
| 58 | + } |
| 59 | + } |
| 60 | + if (RASpecificParams.empty() || RASessionID.empty() || Addresses.empty()) |
| 61 | + throw std::string("Failed to parse connection string"); |
| 62 | +} |
| 63 | + |
| 64 | +bool |
| 65 | +ParseConnectionString(const std::string& inString, RemoteAssistanceInfo& outInfo) |
| 66 | +{ |
| 67 | + std::string RASpecificParams; |
| 68 | + std::vector<std::pair<std::string, std::string>> Addresses; |
| 69 | + ParseConnectionString(inString, RASpecificParams, outInfo.SessionID, Addresses); |
| 70 | + outInfo.IP = Addresses.back().first; |
| 71 | + outInfo.Port = Addresses.back().second; |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +#if 0 |
| 76 | +std::string |
| 77 | +CreateRdpFile(const std::string& s, int width, int height, bool freerdpworkaround) |
| 78 | +{ |
| 79 | + std::string RASessionID, RASpecificParams; |
| 80 | + std::vector<std::pair<std::string,std::string>> Addresses; |
| 81 | + ParseConnectionString(s, RASpecificParams, RASessionID, Addresses); |
| 82 | + |
| 83 | + std::ostringstream rdpfile; |
| 84 | + if (freerdpworkaround) |
| 85 | + rdpfile |
| 86 | + // including port into full address triggers a bug in FreeRDP 1.1, so always use server port field for port |
| 87 | + << "full address:s:" << Addresses.back().first << "\n" |
| 88 | + << "server port:i:" << Addresses.back().second << "\n"; |
| 89 | + else // ms clients seem to ignore server port field if full address is given |
| 90 | + rdpfile |
| 91 | + << "full address:s:" << Addresses.back().first << ":" << Addresses.back().second << "\n"; |
| 92 | + rdpfile |
| 93 | + << "alternate shell:s:\n" |
| 94 | + << "shell working directory:s:" << RASessionID << "\n" |
| 95 | + << "authentication level:i:0\n" |
| 96 | + << "audiomode:i:2\n" |
| 97 | + << "prompt for credentials on client:i:0\n"; |
| 98 | + if (freerdpworkaround) |
| 99 | + rdpfile |
| 100 | + << "smart sizing:i:0\n"; // 1 doesn't work properly with FreeRDP 1.1 |
| 101 | + else |
| 102 | + rdpfile |
| 103 | + << "smart sizing:i:1\n"; |
| 104 | + rdpfile |
| 105 | + << "screen mode id:i:1\n" // window |
| 106 | + << "username:s:rdpshare\n" // we don't care |
| 107 | + << "desktopwidth:i:" << width << "\n" |
| 108 | + << "desktopheight:i:" << height << "\n"; |
| 109 | + ; |
| 110 | + return rdpfile.str(); |
| 111 | +} |
| 112 | + |
| 113 | +std::string RdpFileToUri(const std::string& s) |
| 114 | +{ |
| 115 | + std::string uri = "rdp://"; |
| 116 | + std::istringstream iss(s); |
| 117 | + std::string line; |
| 118 | + while(getline(iss, line)) |
| 119 | + { |
| 120 | + size_t pos1 = line.find(':'), pos2 = line.find(':', pos1 + 1); |
| 121 | + uri += UrlEscape_(line.substr(0,pos1)); |
| 122 | + uri += "="; |
| 123 | + uri += line.substr(pos1 + 1, pos2 - pos1); |
| 124 | + uri += UrlEscape_(line.substr(pos2 + 1),".:="); |
| 125 | + uri += "&"; |
| 126 | + } |
| 127 | + uri.pop_back(); |
| 128 | + return uri; |
| 129 | +} |
| 130 | + |
| 131 | +std::string RdpFileToCmdline(const std::string& s) |
| 132 | +{ |
| 133 | + std::string cmd; |
| 134 | + std::istringstream iss(s); |
| 135 | + std::string line; |
| 136 | + while (getline(iss, line)) |
| 137 | + { |
| 138 | + size_t pos1 = line.find(':'), pos2 = line.find(':', pos1 + 1); |
| 139 | + std::string var = line.substr(0, pos1), val = line.substr(pos2 + 1); |
| 140 | + if (var == "full address") |
| 141 | + cmd += "\"/v:" + val + "\" "; |
| 142 | + else if( var == "shell working directory") |
| 143 | + cmd += "\"/shell-dir:" + val + "\" "; |
| 144 | + } |
| 145 | + return cmd; |
| 146 | +} |
| 147 | + |
| 148 | +std::string CreateTicket(const std::string& s) |
| 149 | +{ |
| 150 | + std::string RASessionID, RASpecificParams, AddressList; |
| 151 | + std::vector<std::pair<std::string, std::string>> Addresses; |
| 152 | + ParseConnectionString(s, RASpecificParams, RASessionID, Addresses); |
| 153 | + for (const auto& address : Addresses) |
| 154 | + AddressList += ";" + address.first + ":" + address.second; |
| 155 | + |
| 156 | + std::ostringstream ticket; |
| 157 | + ticket << "<?xml version=\"1.0\"?>\r\n" |
| 158 | + << "<UPLOADINFO TYPE=\"Escalated\"> " |
| 159 | + << "<UPLOADDATA USERNAME=\"rdpshare\" " |
| 160 | + << "RCTICKET=\"65538,1," |
| 161 | + << AddressList.substr(1) << ",*," << RASessionID << ",*,*," << RASpecificParams << "\" " |
| 162 | + << "RCTICKETENCRYPTED=\"0\" " |
| 163 | + << "DtStart=\"" << ::time(nullptr) << "\" " |
| 164 | + << "DtLength=\"" << INT32_MAX << "\" " |
| 165 | + << "L=\"0\"" |
| 166 | + << "/>" |
| 167 | + << "</UPLOADINFO>\r\n"; |
| 168 | + return ticket.str(); |
| 169 | +} |
| 170 | + |
| 171 | +#endif |
0 commit comments