libdap++  Updated for version 3.8.2
RCReader.cc
Go to the documentation of this file.
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: Jose Garcia <jgarcia@ucar.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 2001,2002
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jose Jose Garcia <jgarcia@ucar.edu>
31 
37 // #define DODS_DEBUG
38 #include "config.h"
39 
40 #include <cstring>
41 #include <cstdlib>
42 
43 #include <unistd.h> // for stat
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 
47 #ifdef WIN32
48 #define FALSE 0
49 // Win32 does not define F_OK. 08/21/02 jhrg
50 #define F_OK 0
51 #define DIR_SEP_STRING "\\"
52 #define DIR_SEP_CHAR '\\'
53 #include <direct.h>
54 #else
55 #define DIR_SEP_STRING "/"
56 #define DIR_SEP_CHAR '/'
57 #endif
58 
59 #include <pthread.h>
60 
61 #include <fstream>
62 
63 #include "debug.h"
64 #include "RCReader.h"
65 #include "Error.h"
66 
67 using namespace std;
68 
69 namespace libdap {
70 
71 RCReader* RCReader::_instance = 0;
72 
73 // This variable (instance_control) is used to ensure that in a MT
74 // environment _instance is correctly initialized. See the get_instance
75 // method. 08/07/02 jhrg
76 static pthread_once_t instance_control = PTHREAD_ONCE_INIT;
77 
82 bool
83 RCReader::write_rc_file(const string &pathname)
84 {
85  DBG(cerr << "Writing the RC file to " << pathname << endl);
86  ofstream fpo(pathname.c_str());
87 
88  // If the file couldn't be created. Nothing needs to be done here,
89  // the program will simply use the defaults.
90 
91  if (fpo) {
92  // This means we just created the file. We will now save
93  // the defaults in it for future use.
94  fpo << "# OPeNDAP client configuration file. See the OPeNDAP" << endl;
95  fpo << "# users guide for information." << endl;
96  fpo << "USE_CACHE=" << _dods_use_cache << endl;
97  fpo << "# Cache and object size are given in megabytes (20 ==> 20Mb)."
98  << endl;
99  fpo << "MAX_CACHE_SIZE=" << _dods_cache_max << endl;
100  fpo << "MAX_CACHED_OBJ=" << _dods_cached_obj << endl;
101  fpo << "IGNORE_EXPIRES=" << _dods_ign_expires << endl;
102  fpo << "CACHE_ROOT=" << d_cache_root << endl;
103  fpo << "DEFAULT_EXPIRES=" << _dods_default_expires << endl;
104  fpo << "ALWAYS_VALIDATE=" << _dods_always_validate << endl;
105  fpo << "# Request servers compress responses if possible?" << endl;
106  fpo << "# 1 (yes) or 0 (false)." << endl;
107  fpo << "DEFLATE=" << _dods_deflate << endl;
108 
109  fpo << "# Should SSL certificates and hosts be validated? SSL" << endl;
110  fpo << "# will only work with signed certificates." << endl;
111  fpo << "VALIDATE_SSL=" << d_validate_ssl << endl;
112 
113  fpo << "# Proxy configuration (optional parts in []s)." << endl;
114  fpo << "# You may also use the 'http_proxy' environment variable"
115  << endl;
116  fpo << "# but a value in this file will override that env variable."
117  << endl;
118  fpo << "# PROXY_SERVER=[http://][username:password@]host[:port]"
119  << endl;
120  if (!d_dods_proxy_server_host.empty()) {
121  fpo << "PROXY_SERVER=" << d_dods_proxy_server_protocol << "://"
122  << (d_dods_proxy_server_userpw.empty()
123  ? ""
124  : d_dods_proxy_server_userpw + "@")
125  + d_dods_proxy_server_host
126  + ":" + long_to_string(d_dods_proxy_server_port) << endl;
127  }
128 
129  fpo << "# NO_PROXY_FOR=<host|domain>" << endl;
130  if (!d_dods_no_proxy_for_host.empty()) {
131  fpo << "NO_PROXY_FOR=" << d_dods_no_proxy_for_host << endl;
132  }
133 
134  fpo << "# AIS_DATABASE=<file or url>" << endl;
135 
136  fpo << "# COOKIE_JAR=.dods_cookies" << endl;
137  fpo << "# The cookie jar is a file that holds cookies sent from"
138  << endl;
139  fpo << "# servers such as single signon systems. Uncomment this"
140  << endl;
141  fpo << "# option and provide a file name to activate this feature."
142  << endl;
143  fpo << "# If the value is a filename, it will be created in this"
144  << endl;
145  fpo << "# directory; a full pathname can be used to force a specific"
146  << endl;
147  fpo << "# location." << endl;
148 
149  fpo.close();
150  return true;
151  }
152 
153  return false;
154 }
155 
156 bool
157 RCReader::read_rc_file(const string &pathname)
158 {
159  DBG(cerr << "Reading the RC file from " << pathname << endl);
160 
161  ifstream fpi(pathname.c_str());
162  if (fpi) {
163  // The file exists and we may now begin to parse it.
164  // Defaults are already stored in the variables, if the correct
165  // tokens are found in the file then those defaults will be
166  // overwritten.
167  char *value;
168  char *tempstr = new char[1024];;
169  int tokenlength;
170  while (true) {
171  fpi.getline(tempstr, 1023);
172  if (!fpi.good())
173  break;
174 
175  value = strchr(tempstr, '=');
176  if (!value)
177  continue;
178  tokenlength = value - tempstr;
179  value++;
180 
181  if ((strncmp(tempstr, "USE_CACHE", 9) == 0)
182  && tokenlength == 9) {
183  _dods_use_cache = atoi(value) ? true : false;
184  }
185  else if ((strncmp(tempstr, "MAX_CACHE_SIZE", 14) == 0)
186  && tokenlength == 14) {
187  _dods_cache_max = atoi(value);
188  }
189  else if ((strncmp(tempstr, "MAX_CACHED_OBJ", 14) == 0)
190  && tokenlength == 14) {
191  _dods_cached_obj = atoi(value);
192  }
193  else if ((strncmp(tempstr, "IGNORE_EXPIRES", 14) == 0)
194  && tokenlength == 14) {
195  _dods_ign_expires = atoi(value);
196  }
197  else if ((strncmp(tempstr, "DEFLATE", 7) == 0)
198  && tokenlength == 7) {
199  _dods_deflate = atoi(value) ? true : false;
200  }
201  else if ((strncmp(tempstr, "CACHE_ROOT", 10) == 0)
202  && tokenlength == 10) {
203  d_cache_root = value;
204  if (d_cache_root[d_cache_root.length() - 1] != DIR_SEP_CHAR)
205  d_cache_root += string(DIR_SEP_STRING);
206  }
207  else if ((strncmp(tempstr, "DEFAULT_EXPIRES", 15) == 0)
208  && tokenlength == 15) {
209  _dods_default_expires = atoi(value);
210  }
211  else if ((strncmp(tempstr, "ALWAYS_VALIDATE", 15) == 0)
212  && tokenlength == 15) {
213  _dods_always_validate = atoi(value);
214  }
215  else if ((strncmp(tempstr, "VALIDATE_SSL", 12) == 0)
216  && tokenlength == 12) {
217  d_validate_ssl = atoi(value);
218  }
219  else if (strncmp(tempstr, "AIS_DATABASE", 12) == 0
220  && tokenlength == 12) {
221  d_ais_database = value;
222  }
223  else if (strncmp(tempstr, "COOKIE_JAR", 10) == 0
224  && tokenlength == 10) {
225  // if the value of COOKIE_JAR starts with a slash, use it as
226  // is. However, if it does not start with a slash, prefix it
227  // with the directory that contains the .dodsrc file.
228  if (value[0] == '/') {
229  d_cookie_jar = value;
230  }
231  else {
232  d_cookie_jar = d_rc_file_path.substr(0, d_rc_file_path.find(".dodsrc")) + string(value);
233  }
234  DBG(cerr << "set cookie jar to: " << d_cookie_jar << endl);
235  }
236  else if ((strncmp(tempstr, "PROXY_SERVER", 12) == 0)
237  && tokenlength == 12) {
238  // Setup a proxy server for all requests.
239  // The original syntax was <protocol>,<machine> where the
240  // machine could also contain the user/pass and port info.
241  // Support that but also support machine prefixed by
242  // 'http://' with and without the '<protocol>,' prefix. jhrg
243  // 4/21/08 (see bug 1095).
244  string proxy = value;
245  string::size_type comma = proxy.find(',');
246 
247  // Since the <protocol> is now optional, the comma might be
248  // here. If it is, check that the protocol given is http.
249  if (comma != string::npos) {
250  d_dods_proxy_server_protocol = proxy.substr(0, comma);
251  downcase(d_dods_proxy_server_protocol);
252  if (d_dods_proxy_server_protocol != "http")
253  throw Error("The only supported protocol for a proxy server is \"HTTP\". Correct your \".dodsrc\" file.");
254  proxy = proxy.substr(comma + 1);
255  }
256  else {
257  d_dods_proxy_server_protocol = "http";
258  }
259 
260  // Look for a 'protocol://' prefix; skip if found
261  string::size_type protocol = proxy.find("://");
262  if (protocol != string::npos) {
263  proxy = proxy.substr(protocol + 3);
264  }
265 
266  // Break apart into userpw, host and port.
267  string::size_type at_sign = proxy.find('@');
268  if (at_sign != string::npos) { // has userpw
269  d_dods_proxy_server_userpw = proxy.substr(0, at_sign);
270  proxy = proxy.substr(at_sign + 1);
271  }
272  else
273  d_dods_proxy_server_userpw = "";
274 
275  // Get host and look for a port number
276  string::size_type colon = proxy.find(':');
277  if (colon != string::npos) {
278  d_dods_proxy_server_host = proxy.substr(0, colon);
279  d_dods_proxy_server_port
280  = strtol(proxy.substr(colon + 1).c_str(), 0, 0);
281  }
282  else {
283  d_dods_proxy_server_host = proxy;
284  d_dods_proxy_server_port = 80;
285  }
286  }
287  else if ((strncmp(tempstr, "NO_PROXY_FOR", 12) == 0)
288  && tokenlength == 12) {
289  // Setup a proxy server for all requests.
290  string no_proxy = value;
291  string::size_type comma = no_proxy.find(',');
292 
293  // Since the protocol is required, the comma *must* be
294  // present. We could throw an Error on the malformed line...
295  if (comma == string::npos) {
296  d_dods_no_proxy_for_protocol = "http";
297  d_dods_no_proxy_for_host = no_proxy;
298  d_dods_no_proxy_for = true;
299  }
300  else {
301  d_dods_no_proxy_for_protocol = no_proxy.substr(0, comma);
302  d_dods_no_proxy_for_host = no_proxy.substr(comma + 1);
303  d_dods_no_proxy_for = true;
304  }
305  }
306  }
307 
308  delete [] tempstr; tempstr = 0;
309 
310  fpi.close(); // Close the .dodsrc file. 12/14/99 jhrg
311 
312  return true;
313  } // End of cache file parsing.
314 
315  return false;
316 }
317 
318 // Helper for check_env_var(). This is its main logic, separated out for the
319 // cases under WIN32 where we don't use an environment variable. 09/19/03
320 // jhrg
321 string
322 RCReader::check_string(string env_var)
323 {
324  DBG(cerr << "Entering check_string... (" << env_var << ")" << endl);
325  struct stat stat_info;
326 
327  if (stat(env_var.c_str(), &stat_info) != 0) {
328  DBG(cerr << "stat returned non-zero" << endl);
329  return ""; // ENV VAR not a file or dir, bail
330  }
331 
332  if (S_ISREG(stat_info.st_mode)) {
333  DBG(cerr << "S_ISREG: " << S_ISREG(stat_info.st_mode) << endl);
334  return env_var; // ENV VAR is a file, use it
335  }
336 
337  // ENV VAR is a directory, does it contain .dodsrc? Can we create
338  // .dodsrc if it's not there?
339  if (S_ISDIR(stat_info.st_mode)) {
340  DBG(cerr << "S_ISDIR: " << S_ISDIR(stat_info.st_mode) << endl);
341  if (*env_var.rbegin() != DIR_SEP_CHAR) // Add trailing / if missing
342  env_var += DIR_SEP_STRING;
343  // Trick: set d_cache_root here in case we're going to create the
344  // .dodsrc later on. If the .dodsrc file exists, its value will
345  // overwrite this value, if not write_rc_file() will use the correct
346  // value. 09/19/03 jhrg
347  d_cache_root = env_var + string(".dods_cache") + DIR_SEP_STRING;
348  env_var += ".dodsrc";
349  if (stat(env_var.c_str(), &stat_info) == 0 &&
350  S_ISREG(stat_info.st_mode)) {
351  DBG(cerr << "Found .dodsrc in \"" << env_var << "\"" << endl);
352  return env_var; // Found .dodsrc in ENV VAR
353  }
354 
355  // Didn't find .dodsrc in ENV VAR and ENV VAR is a directory; try to
356  // create it. Note write_rc_file uses d_cache_root (set above) when
357  // it creates the RC file's contents.
358  if (write_rc_file(env_var)) {
359  DBG(cerr << "Wrote .dodsrc in \"" << env_var << "\"" << endl);
360  return env_var;
361  }
362  }
363 
364  // If we're here, then we've neither found nor created the RC file.
365  DBG(cerr << "could neither find nor create a .dodsrc file" << endl);
366  return "";
367 }
368 
378 string
379 RCReader::check_env_var(const string &variable_name)
380 {
381  char *ev = getenv(variable_name.c_str());
382  if (!ev || strlen(ev) == 0)
383  return "";
384 
385  return check_string(ev);
386 }
387 
388 RCReader::RCReader() throw(Error)
389 {
390  d_rc_file_path = "";
391  d_cache_root = "";
392 
393  // ** Set default values **
394  // Users must explicitly turn caching on.
395  _dods_use_cache = false;
396  _dods_cache_max = 20;
397  _dods_cached_obj = 5;
398  _dods_ign_expires = 0;
399  _dods_default_expires = 86400;
400  _dods_always_validate = 0;
401 
402  _dods_deflate = 0;
403  d_validate_ssl = 1;
404 
405  //flags for PROXY_SERVER=<protocol>,<host url>
406  // New syntax PROXY_SERVER=[http://][user:pw@]host[:port]
407  d_dods_proxy_server_protocol = "";
408  d_dods_proxy_server_host = "";
409  d_dods_proxy_server_port = 0;
410  d_dods_proxy_server_userpw = "";
411 
412  _dods_proxy_server_host_url = ""; // deprecated
413 
414  // PROXY_FOR is deprecated.
415  // flags for PROXY_FOR=<regex>,<proxy host url>,<flags>
416  _dods_proxy_for = false; // true if proxy_for is used.
417  _dods_proxy_for_regexp = "";
418  _dods_proxy_for_proxy_host_url = "";
419  _dods_proxy_for_regexp_flags = 0;
420 
421  //flags for NO_PROXY_FOR=<protocol>,<host>,<port>
422  // New syntax NO_PROXY_FOR=<host|domain>
423  d_dods_no_proxy_for = false;
424  d_dods_no_proxy_for_protocol = ""; // deprecated
425  d_dods_no_proxy_for_host = "";
426  // default to port 0 if not specified. This means all ports. Using 80
427  // will fail when the URL does not contain the port number. That's
428  // probably a bug in libwww. 10/23/2000 jhrg
429  _dods_no_proxy_for_port = 0; // deprecated
430 
431  d_cookie_jar = "";
432 
433 #ifdef WIN32
434  string homedir = string("C:") + string(DIR_SEP_STRING) + string("Dods");
435  d_rc_file_path = check_string(homedir);
436  if (d_rc_file_path.empty()) {
437  homedir = string("C:") + string(DIR_SEP_STRING) + string("opendap");
438  d_rc_file_path = check_string(homedir);
439  }
440  // Normally, I'd prefer this for WinNT-based systems.
441  if (d_rc_file_path.empty())
442  d_rc_file_path = check_env_var("APPDATA");
443  if (d_rc_file_path.empty())
444  d_rc_file_path = check_env_var("TEMP");
445  if (d_rc_file_path.empty())
446  d_rc_file_path = check_env_var("TMP");
447 #else
448  d_rc_file_path = check_env_var("DODS_CONF");
449  if (d_rc_file_path.empty())
450  d_rc_file_path = check_env_var("HOME");
451 #endif
452  DBG(cerr << "Looking for .dodsrc in: " << d_rc_file_path << endl);
453 
454  if (!d_rc_file_path.empty())
455  read_rc_file(d_rc_file_path);
456 }
457 
458 RCReader::~RCReader()
459 {}
460 
462 void
463 RCReader::delete_instance()
464 {
465  if (RCReader::_instance) {
466  delete RCReader::_instance;
467  RCReader::_instance = 0;
468  }
469 }
470 
472 void
473 RCReader::initialize_instance()
474 {
475  DBGN(cerr << "RCReader::initialize_instance() ... ");
476 
477  RCReader::_instance = new RCReader;
478  atexit(RCReader::delete_instance);
479 
480  DBG(cerr << "exiting." << endl);
481 }
482 
483 RCReader*
484 RCReader::instance()
485 {
486  DBG(cerr << "Entring RCReader::instance" << endl);
487  // The instance_control variable is defined at the top of this file.
488  // 08/07/02 jhrg
489  pthread_once(&instance_control, initialize_instance);
490 
491  DBG(cerr << "Instance value: " << hex << _instance << dec << endl);
492 
493  return _instance;
494 }
495 
496 } // namespace libdap
#define DIR_SEP_CHAR
Definition: RCReader.cc:56
void downcase(string &s)
Definition: util.cc:367
#define DBGN(x)
Definition: debug.h:59
#define DBG(x)
Definition: debug.h:58
#define DIR_SEP_STRING
Definition: RCReader.cc:55
string long_to_string(long val, int base)
Definition: util.cc:440