You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.cxx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2004-2005 Cendio AB.
  3. * Copyright 2017 Peter Astrand <astrand@cendio.se> for Cendio AB
  4. * Copyright 2011-2022 Pierre Ossman for Cendio AB
  5. *
  6. * This 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 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This software 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 this software; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. * USA.
  20. */
  21. // -=- Configuration.cxx
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <assert.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <os/Mutex.h>
  30. #include <rfb/util.h>
  31. #include <rfb/Configuration.h>
  32. #include <rfb/LogWriter.h>
  33. #include <rfb/Exception.h>
  34. #define LOCK_CONFIG os::AutoMutex a(mutex)
  35. #include <rdr/HexOutStream.h>
  36. #include <rdr/HexInStream.h>
  37. using namespace rfb;
  38. static LogWriter vlog("Config");
  39. // -=- The Global/server/viewer Configuration objects
  40. Configuration* Configuration::global_ = 0;
  41. Configuration* Configuration::server_ = 0;
  42. Configuration* Configuration::viewer_ = 0;
  43. Configuration* Configuration::global() {
  44. if (!global_)
  45. global_ = new Configuration("Global");
  46. return global_;
  47. }
  48. Configuration* Configuration::server() {
  49. if (!server_)
  50. server_ = new Configuration("Server");
  51. return server_;
  52. }
  53. Configuration* Configuration::viewer() {
  54. if (!viewer_)
  55. viewer_ = new Configuration("Viewer");
  56. return viewer_;
  57. }
  58. // -=- Configuration implementation
  59. bool Configuration::set(const char* n, const char* v, bool immutable) {
  60. return set(n, strlen(n), v, immutable);
  61. }
  62. bool Configuration::set(const char* name, int len,
  63. const char* val, bool immutable)
  64. {
  65. VoidParameter* current = head;
  66. while (current) {
  67. if ((int)strlen(current->getName()) == len &&
  68. strncasecmp(current->getName(), name, len) == 0)
  69. {
  70. bool b = current->setParam(val);
  71. if (b && immutable)
  72. current->setImmutable();
  73. return b;
  74. }
  75. current = current->_next;
  76. }
  77. return _next ? _next->set(name, len, val, immutable) : false;
  78. }
  79. bool Configuration::set(const char* config, bool immutable) {
  80. bool hyphen = false;
  81. if (config[0] == '-') {
  82. hyphen = true;
  83. config++;
  84. if (config[0] == '-') config++; // allow gnu-style --<option>
  85. }
  86. const char* equal = strchr(config, '=');
  87. if (equal) {
  88. return set(config, equal-config, equal+1, immutable);
  89. } else if (hyphen) {
  90. VoidParameter* current = head;
  91. while (current) {
  92. if (strcasecmp(current->getName(), config) == 0) {
  93. bool b = current->setParam();
  94. if (b && immutable)
  95. current->setImmutable();
  96. return b;
  97. }
  98. current = current->_next;
  99. }
  100. }
  101. return _next ? _next->set(config, immutable) : false;
  102. }
  103. VoidParameter* Configuration::get(const char* param)
  104. {
  105. VoidParameter* current = head;
  106. while (current) {
  107. if (strcasecmp(current->getName(), param) == 0)
  108. return current;
  109. current = current->_next;
  110. }
  111. return _next ? _next->get(param) : 0;
  112. }
  113. void Configuration::list(int width, int nameWidth) {
  114. VoidParameter* current = head;
  115. fprintf(stderr, "%s Parameters:\n", name.c_str());
  116. while (current) {
  117. std::string def_str = current->getDefaultStr();
  118. const char* desc = current->getDescription();
  119. fprintf(stderr," %-*s -", nameWidth, current->getName());
  120. int column = strlen(current->getName());
  121. if (column < nameWidth) column = nameWidth;
  122. column += 4;
  123. while (true) {
  124. const char* s = strchr(desc, ' ');
  125. int wordLen;
  126. if (s) wordLen = s-desc;
  127. else wordLen = strlen(desc);
  128. if (column + wordLen + 1 > width) {
  129. fprintf(stderr,"\n%*s",nameWidth+4,"");
  130. column = nameWidth+4;
  131. }
  132. fprintf(stderr," %.*s",wordLen,desc);
  133. column += wordLen + 1;
  134. desc += wordLen + 1;
  135. if (!s) break;
  136. }
  137. if (!def_str.empty()) {
  138. if (column + (int)def_str.size() + 11 > width)
  139. fprintf(stderr,"\n%*s",nameWidth+4,"");
  140. fprintf(stderr," (default=%s)\n",def_str.c_str());
  141. } else {
  142. fprintf(stderr,"\n");
  143. }
  144. current = current->_next;
  145. }
  146. if (_next)
  147. _next->list(width, nameWidth);
  148. }
  149. bool Configuration::remove(const char* param) {
  150. VoidParameter *current = head;
  151. VoidParameter **prevnext = &head;
  152. while (current) {
  153. if (strcasecmp(current->getName(), param) == 0) {
  154. *prevnext = current->_next;
  155. return true;
  156. }
  157. prevnext = &current->_next;
  158. current = current->_next;
  159. }
  160. return false;
  161. }
  162. // -=- VoidParameter
  163. VoidParameter::VoidParameter(const char* name_, const char* desc_,
  164. ConfigurationObject co)
  165. : immutable(false), name(name_), description(desc_)
  166. {
  167. Configuration *conf = NULL;
  168. switch (co) {
  169. case ConfGlobal: conf = Configuration::global();
  170. break;
  171. case ConfServer: conf = Configuration::server();
  172. break;
  173. case ConfViewer: conf = Configuration::viewer();
  174. break;
  175. }
  176. _next = conf->head;
  177. conf->head = this;
  178. mutex = new os::Mutex();
  179. }
  180. VoidParameter::~VoidParameter() {
  181. delete mutex;
  182. }
  183. const char*
  184. VoidParameter::getName() const {
  185. return name;
  186. }
  187. const char*
  188. VoidParameter::getDescription() const {
  189. return description;
  190. }
  191. bool VoidParameter::setParam() {
  192. return false;
  193. }
  194. bool VoidParameter::isBool() const {
  195. return false;
  196. }
  197. void
  198. VoidParameter::setImmutable() {
  199. vlog.debug("set immutable %s", getName());
  200. immutable = true;
  201. }
  202. // -=- AliasParameter
  203. AliasParameter::AliasParameter(const char* name_, const char* desc_,
  204. VoidParameter* param_, ConfigurationObject co)
  205. : VoidParameter(name_, desc_, co), param(param_) {
  206. }
  207. bool
  208. AliasParameter::setParam(const char* v) {
  209. return param->setParam(v);
  210. }
  211. bool AliasParameter::setParam() {
  212. return param->setParam();
  213. }
  214. std::string AliasParameter::getDefaultStr() const {
  215. return "";
  216. }
  217. std::string AliasParameter::getValueStr() const {
  218. return param->getValueStr();
  219. }
  220. bool AliasParameter::isBool() const {
  221. return param->isBool();
  222. }
  223. void
  224. AliasParameter::setImmutable() {
  225. vlog.debug("set immutable %s (Alias)", getName());
  226. param->setImmutable();
  227. }
  228. // -=- BoolParameter
  229. BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v,
  230. ConfigurationObject co)
  231. : VoidParameter(name_, desc_, co), value(v), def_value(v) {
  232. }
  233. bool
  234. BoolParameter::setParam(const char* v) {
  235. if (immutable) return true;
  236. if (*v == 0 || strcasecmp(v, "1") == 0 || strcasecmp(v, "on") == 0
  237. || strcasecmp(v, "true") == 0 || strcasecmp(v, "yes") == 0)
  238. setParam(true);
  239. else if (strcasecmp(v, "0") == 0 || strcasecmp(v, "off") == 0
  240. || strcasecmp(v, "false") == 0 || strcasecmp(v, "no") == 0)
  241. setParam(false);
  242. else {
  243. vlog.error("Bool parameter %s: invalid value '%s'", getName(), v);
  244. return false;
  245. }
  246. return true;
  247. }
  248. bool BoolParameter::setParam() {
  249. setParam(true);
  250. return true;
  251. }
  252. void BoolParameter::setParam(bool b) {
  253. if (immutable) return;
  254. value = b;
  255. vlog.debug("set %s(Bool) to %d", getName(), value);
  256. }
  257. std::string BoolParameter::getDefaultStr() const {
  258. return def_value ? "1" : "0";
  259. }
  260. std::string BoolParameter::getValueStr() const {
  261. return value ? "1" : "0";
  262. }
  263. bool BoolParameter::isBool() const {
  264. return true;
  265. }
  266. BoolParameter::operator bool() const {
  267. return value;
  268. }
  269. // -=- IntParameter
  270. IntParameter::IntParameter(const char* name_, const char* desc_, int v,
  271. int minValue_, int maxValue_, ConfigurationObject co)
  272. : VoidParameter(name_, desc_, co), value(v), def_value(v),
  273. minValue(minValue_), maxValue(maxValue_)
  274. {
  275. }
  276. bool
  277. IntParameter::setParam(const char* v) {
  278. if (immutable) return true;
  279. return setParam(strtol(v, NULL, 0));
  280. }
  281. bool
  282. IntParameter::setParam(int v) {
  283. if (immutable) return true;
  284. vlog.debug("set %s(Int) to %d", getName(), v);
  285. if (v < minValue || v > maxValue)
  286. return false;
  287. value = v;
  288. return true;
  289. }
  290. std::string IntParameter::getDefaultStr() const {
  291. char result[16];
  292. sprintf(result, "%d", def_value);
  293. return result;
  294. }
  295. std::string IntParameter::getValueStr() const {
  296. char result[16];
  297. sprintf(result, "%d", value);
  298. return result;
  299. }
  300. IntParameter::operator int() const {
  301. return value;
  302. }
  303. // -=- StringParameter
  304. StringParameter::StringParameter(const char* name_, const char* desc_,
  305. const char* v, ConfigurationObject co)
  306. : VoidParameter(name_, desc_, co), value(v), def_value(v)
  307. {
  308. if (!v) {
  309. vlog.error("Default value <null> for %s not allowed",name_);
  310. throw rfb::Exception("Default value <null> not allowed");
  311. }
  312. }
  313. StringParameter::~StringParameter() {
  314. }
  315. bool StringParameter::setParam(const char* v) {
  316. LOCK_CONFIG;
  317. if (immutable) return true;
  318. if (!v)
  319. throw rfb::Exception("setParam(<null>) not allowed");
  320. vlog.debug("set %s(String) to %s", getName(), v);
  321. value = v;
  322. return true;
  323. }
  324. std::string StringParameter::getDefaultStr() const {
  325. return def_value;
  326. }
  327. std::string StringParameter::getValueStr() const {
  328. LOCK_CONFIG;
  329. return value;
  330. }
  331. StringParameter::operator const char *() const {
  332. return value.c_str();
  333. }
  334. // -=- BinaryParameter
  335. BinaryParameter::BinaryParameter(const char* name_, const char* desc_,
  336. const uint8_t* v, size_t l, ConfigurationObject co)
  337. : VoidParameter(name_, desc_, co), value(0), length(0), def_value(0), def_length(0) {
  338. if (l) {
  339. assert(v);
  340. value = new uint8_t[l];
  341. length = l;
  342. memcpy(value, v, l);
  343. def_value = new uint8_t[l];
  344. def_length = l;
  345. memcpy(def_value, v, l);
  346. }
  347. }
  348. BinaryParameter::~BinaryParameter() {
  349. delete [] value;
  350. delete [] def_value;
  351. }
  352. bool BinaryParameter::setParam(const char* v) {
  353. if (immutable) return true;
  354. std::vector<uint8_t> newValue = hexToBin(v, strlen(v));
  355. if (newValue.empty() && strlen(v) > 0)
  356. return false;
  357. setParam(newValue.data(), newValue.size());
  358. return true;
  359. }
  360. void BinaryParameter::setParam(const uint8_t* v, size_t len) {
  361. LOCK_CONFIG;
  362. if (immutable) return;
  363. vlog.debug("set %s(Binary)", getName());
  364. delete [] value;
  365. value = NULL;
  366. length = 0;
  367. if (len) {
  368. assert(v);
  369. value = new uint8_t[len];
  370. length = len;
  371. memcpy(value, v, len);
  372. }
  373. }
  374. std::string BinaryParameter::getDefaultStr() const {
  375. return binToHex(def_value, def_length);
  376. }
  377. std::string BinaryParameter::getValueStr() const {
  378. LOCK_CONFIG;
  379. return binToHex(value, length);
  380. }
  381. std::vector<uint8_t> BinaryParameter::getData() const {
  382. LOCK_CONFIG;
  383. std::vector<uint8_t> out(length);
  384. memcpy(out.data(), value, length);
  385. return out;
  386. }