// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* // ** Copyright UCAR (c) 1990 - 2016 // ** University Corporation for Atmospheric Research (UCAR) // ** National Center for Atmospheric Research (NCAR) // ** Boulder, Colorado, USA // ** BSD licence applies - redistribution and use in source and binary // ** forms, with or without modification, are permitted provided that // ** the following conditions are met: // ** 1) If the software is modified to produce derivative works, // ** such modified software should be clearly marked, so as not // ** to confuse it with the version available from UCAR. // ** 2) Redistributions of source code must retain the above copyright // ** notice, this list of conditions and the following disclaimer. // ** 3) Redistributions in binary form must reproduce the above copyright // ** notice, this list of conditions and the following disclaimer in the // ** documentation and/or other materials provided with the distribution. // ** 4) Neither the name of UCAR nor the names of its contributors, // ** if any, may be used to endorse or promote products derived from // ** this software without specific prior written permission. // ** DISCLAIMER: THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS // ** OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED // ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ // RCS info // $Author: dixon $ // $Locker: $ // $Date: 2016/03/03 18:03:31 $ // $Id: DsAccessFile.cc,v 1.3 2016/03/03 18:03:31 dixon Exp $ // $Revision: 1.3 $ // $State: Exp $ /**-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**/ /********************************************************************* * DsAccessFile.cc: Class for manipulating the _dsaccess files that * control access to DsServer directories. * * RAP, NCAR, Boulder CO * * November 2000 * * Nancy Rehak * *********************************************************************/ #include #include #include using namespace std; /* * Initialize constants */ const string DsAccessFile::CLASS_NAME = "DsAccessFile"; const string DsAccessFile::fileName = "_dsaccess"; const int DsAccessFile::MAX_TOKENS = 10; const int DsAccessFile::MAX_TOKEN_LEN = 100; const string DsAccessFile::LIMIT_DIRECTIVE = " // // Where: access_type is replaces with the value of the // access_type string received by this method. if (line[0] != '<') continue; if (STRparse(line, tokens, BUFSIZ, MAX_TOKENS, MAX_TOKEN_LEN) != 2) continue; if (string(tokens[0]) != LIMIT_DIRECTIVE) continue; string access_token = access_type + string(">"); if (string(tokens[1]) != access_token) continue; // If we get here, we found the "Limit" directive limit_found = true; break; } /* endwhile - fgets(...) */ // If the appropriate limit directive wasn't found, universal // access is granted. if (!limit_found) return new DsAccess(DsAccess::DENY_ALLOW, true, _debugFlag); // Create the access structure to return if everything is // okay. DsAccess *access = new DsAccess(DsAccess::DENY_ALLOW, true, _debugFlag); // Process all of the directives until we reach the limit // end directive. If there is no limit end directive, return // an error. bool limit_end_found = false; while (fgets(line, BUFSIZ, access_file) != 0) { // Parse the line into tokens int num_tokens = STRparse(line, tokens, BUFSIZ, MAX_TOKENS, MAX_TOKEN_LEN); // Skip empty lines if (num_tokens == 0) continue; // Check for the end limit token if (num_tokens == 1 && string(tokens[0]) == LIMIT_END_DIRECTIVE) { limit_end_found = true; break; } // Process the limit directives string directive = tokens[0]; if (directive == ORDER_DIRECTIVE) { bool error = false; if (num_tokens == 2) { string ad_token = string(tokens[1]); if (ad_token == DENY_ALLOW_TOKEN) access->setOrder(DsAccess::DENY_ALLOW); else if (ad_token == ALLOW_DENY_TOKEN) access->setOrder(DsAccess::ALLOW_DENY); else error = true; } else { error = true; } if (error) { cerr << "ERROR: " << method_name << endl; cerr << "Error parsing order directive line: " << line << endl; delete access; return 0; } } else if (directive == DENY_DIRECTIVE || directive == ALLOW_DIRECTIVE) { bool error = false; if (num_tokens == 3) { if (string(tokens[1]) == FROM_TOKEN) { if (string(tokens[2]) == ALL_TOKEN) { if (directive == DENY_DIRECTIVE) access->setDenyAllFlag(true); else access->setAllowAllFlag(true); } else if (strchr(tokens[2], '.') == 0) { if (directive == DENY_DIRECTIVE) access->addUserToDenyList(tokens[2]); else access->addUserToAllowList(tokens[2]); } else { IPAddress address; if (!address.setFromString(tokens[2])) { cerr << "ERROR: " << method_name << endl; cerr << "Error parsing IP address in line: " << line << endl; delete access; return 0; } if (directive == DENY_DIRECTIVE) access->addIpToDenyList(address); else access->addIpToAllowList(address); } } else { error = true; } } else { error = true; } if (error) { cerr << "ERROR: " << method_name << endl; cerr << "Error parsing allow/deny directive line: " << line << endl; delete access; return 0; } } else { cerr << "WARNING: " << method_name << endl; cerr << "Unrecognized directive on line: " << line << endl; cerr << "Skipping line" << endl; continue; } } // Check for errors if (!limit_end_found) { delete access; return 0; } return access; }