LCOV - code coverage report
Current view: top level - backends - databasehelpers.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 56 64 87.5 %
Date: 2019-06-30 05:20:33 Functions: 3 3 100.0 %
Branches: 173 312 55.4 %

           Branch data     Line data    Source code
       1                 :            : /** @file databasehelpers.h
       2                 :            :  * @brief Helper functions for database handling
       3                 :            :  */
       4                 :            : /* Copyright 2002-2019 Olly Betts
       5                 :            :  * Copyright 2008 Lemur Consulting Ltd
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU General Public License as
       9                 :            :  * published by the Free Software Foundation; either version 2 of the
      10                 :            :  * License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      20                 :            :  * USA
      21                 :            :  */
      22                 :            : 
      23                 :            : #ifndef XAPIAN_INCLUDED_DATABASEHELPERS_H
      24                 :            : #define XAPIAN_INCLUDED_DATABASEHELPERS_H
      25                 :            : 
      26                 :            : #include <cerrno>
      27                 :            : #include <cstdlib>
      28                 :            : #include <fstream>
      29                 :            : #include <string>
      30                 :            : 
      31                 :            : #include "fileutils.h"
      32                 :            : #include "safesysstat.h"
      33                 :            : #include "safeunistd.h"
      34                 :            : #include "str.h"
      35                 :            : #include "xapian/error.h"
      36                 :            : 
      37                 :            : /** Probe if a path is a single-file database.
      38                 :            :  *
      39                 :            :  *  @param sb      struct statbuf from calling stat() on path
      40                 :            :  *  @param path    path to probe
      41                 :            :  *  @param fd_ptr  used to return fd open of path (unless BACKEND_UNKNOWN is
      42                 :            :  *                 returned)
      43                 :            :  *
      44                 :            :  *  @return  A BACKEND_* constant from backends.h:
      45                 :            :  *           * BACKEND_UNKNOWN : unknown (could be a stub file)
      46                 :            :  *           * BACKEND_GLASS : glass single file
      47                 :            :  *           * BACKEND_HONEY : honey single file
      48                 :            :  */
      49                 :            : int
      50                 :            : test_if_single_file_db(const struct stat& sb,
      51                 :            :                        const std::string& path,
      52                 :            :                        int* fd_ptr);
      53                 :            : 
      54                 :            : /** Open, read and process a stub database file.
      55                 :            :  *
      56                 :            :  *  Implemented as a template with separate actions for each database type.
      57                 :            :  */
      58                 :            : template<typename A1,
      59                 :            :          typename A2,
      60                 :            :          typename A3,
      61                 :            :          typename A4,
      62                 :            :          typename A5,
      63                 :            :          typename A6>
      64                 :            : void
      65                 :        576 : read_stub_file(const std::string& file,
      66                 :            :                A1 action_auto,
      67                 :            :                A2 action_glass,
      68                 :            :                A3 action_honey,
      69                 :            :                A4 action_remote_prog,
      70                 :            :                A5 action_remote_tcp,
      71                 :            :                A6 action_inmemory)
      72                 :            : {
      73                 :            :     // A stub database is a text file with one or more lines of this format:
      74                 :            :     // <dbtype> <serialised db object>
      75                 :            :     //
      76                 :            :     // Lines which start with a "#" character are ignored.
      77                 :            :     //
      78                 :            :     // Any paths specified in stub database files which are relative will be
      79                 :            :     // considered to be relative to the directory containing the stub database.
      80 [ +  - ][ +  - ]:        576 :     std::ifstream stub(file.c_str());
      81 [ +  - ][ +  + ]:        576 :     if (!stub) {
         [ +  - ][ +  + ]
      82 [ +  - ][ +  - ]:          2 :         std::string msg = "Couldn't open stub database file: ";
      83   [ +  -  +  - ]:          2 :         msg += file;
      84 [ +  - ][ +  - ]:          2 :         throw Xapian::DatabaseNotFoundError(msg, errno);
      85                 :            :     }
      86 [ +  - ][ +  - ]:       1148 :     std::string line;
      87                 :        574 :     unsigned int line_no = 0;
      88 [ +  - ][ +  - ]:       1673 :     while (std::getline(stub, line)) {
         [ +  + ][ +  - ]
         [ +  - ][ +  + ]
      89                 :       1099 :         ++line_no;
      90 [ +  - ][ +  - ]:       1099 :         if (line.empty() || line[0] == '#')
         [ +  + ][ +  + ]
         [ +  - ][ +  - ]
         [ +  + ][ +  + ]
      91                 :         62 :             continue;
      92                 :       1037 :         std::string::size_type space = line.find(' ');
      93 [ +  + ][ +  + ]:       1037 :         if (space == std::string::npos) space = line.size();
      94                 :            : 
      95 [ +  - ][ +  - ]:       1037 :         std::string type(line, 0, space);
      96 [ +  - ][ +  - ]:       1037 :         line.erase(0, space + 1);
      97                 :            : 
      98 [ +  - ][ +  + ]:       1037 :         if (type == "auto") {
         [ +  - ][ +  + ]
      99 [ +  - ][ +  - ]:        107 :             resolve_relative_path(line, file);
     100 [ +  + ][ +  - ]:        107 :             action_auto(line);
     101                 :        105 :             continue;
     102                 :            :         }
     103                 :            : 
     104 [ +  - ][ +  + ]:        930 :         if (type == "glass") {
         [ +  - ][ +  + ]
     105                 :            : #ifdef XAPIAN_HAS_GLASS_BACKEND
     106 [ +  - ][ +  - ]:        863 :             resolve_relative_path(line, file);
     107 [ +  + ][ +  - ]:        863 :             action_glass(line);
     108                 :        862 :             continue;
     109                 :            : #else
     110                 :            :             (void)action_glass;
     111                 :            :             throw Xapian::FeatureUnavailableError("Glass backend disabled");
     112                 :            : #endif
     113                 :            :         }
     114                 :            : 
     115 [ +  - ][ -  + ]:         67 :         if (type == "honey") {
         [ +  - ][ -  + ]
     116                 :            : #ifdef XAPIAN_HAS_HONEY_BACKEND
     117 [ #  # ][ #  # ]:          0 :             resolve_relative_path(line, file);
     118 [ #  # ][ #  # ]:          0 :             action_honey(line);
     119                 :          0 :             continue;
     120                 :            : #else
     121                 :            :             (void)action_honey;
     122                 :            :             throw Xapian::FeatureUnavailableError("Honey backend disabled");
     123                 :            : #endif
     124                 :            :         }
     125                 :            : 
     126 [ +  - ][ +  + ]:         67 :         if (type == "remote" && !line.empty()) {
         [ +  + ][ +  + ]
         [ +  - ][ +  + ]
         [ +  + ][ +  + ]
     127                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     128 [ +  - ][ +  + ]:         36 :             if (line[0] == ':') {
         [ +  - ][ +  + ]
     129                 :            :                 // prog
     130                 :            :                 // FIXME: timeouts
     131                 :            :                 // Is it a security risk?
     132                 :         12 :                 space = line.find(' ');
     133   [ +  -  +  - ]:         12 :                 std::string args;
     134 [ +  - ][ +  - ]:         12 :                 if (space != std::string::npos) {
     135 [ +  - ][ +  - ]:         12 :                     args.assign(line, space + 1, std::string::npos);
     136 [ +  - ][ +  - ]:         12 :                     line.assign(line, 1, space - 1);
     137                 :            :                 } else {
     138 [ #  # ][ #  # ]:          0 :                     line.erase(0, 1);
     139                 :            :                 }
     140 [ -  + ][ +  - ]:         12 :                 action_remote_prog(line, args);
     141                 :         12 :                 continue;
     142                 :            :             }
     143                 :         24 :             std::string::size_type colon = line.rfind(':');
     144   [ +  +  +  + ]:         24 :             if (colon != std::string::npos) {
     145                 :            :                 // tcp
     146                 :            :                 // FIXME: timeouts
     147                 :            :                 // Avoid misparsing an IPv6 address without a port number.  The
     148                 :            :                 // port number is required, so just leave that case to the
     149                 :            :                 // error handling further below.
     150 [ +  - ][ +  - ]:         16 :                 if (!(line[0] == '[' && line.back() == ']')) {
         [ +  - ][ +  + ]
         [ +  + ][ +  - ]
         [ +  - ][ +  - ]
         [ +  + ][ +  + ]
     151                 :          8 :                     unsigned int port = std::atoi(line.c_str() + colon + 1);
     152   [ +  -  +  - ]:          8 :                     line.erase(colon);
     153 [ +  - ][ +  - ]:          8 :                     if (line[0] == '[' && line.back() == ']') {
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     154 [ +  - ][ +  - ]:          8 :                         line.erase(line.size() - 1, 1);
     155 [ +  - ][ +  - ]:          8 :                         line.erase(0, 1);
     156                 :            :                     }
     157 [ -  + ][ -  + ]:          8 :                     action_remote_tcp(line, port);
     158                 :         16 :                     continue;
     159                 :            :                 }
     160                 :            :             }
     161                 :            : #else
     162                 :            :             (void)action_remote_prog;
     163                 :            :             (void)action_remote_tcp;
     164                 :            :             throw Xapian::FeatureUnavailableError("Remote backend disabled");
     165                 :            : #endif
     166                 :            :         }
     167                 :            : 
     168 [ +  - ][ +  + ]:         47 :         if (type == "inmemory" && line.empty()) {
         [ +  - ][ +  + ]
         [ +  - ][ +  + ]
         [ +  - ][ +  + ]
     169                 :            : #ifdef XAPIAN_HAS_INMEMORY_BACKEND
     170 [ +  + ][ +  - ]:          5 :             action_inmemory();
     171                 :          4 :             continue;
     172                 :            : #else
     173                 :            :             (void)action_inmemory;
     174                 :            :             throw Xapian::FeatureUnavailableError("Inmemory backend disabled");
     175                 :            : #endif
     176                 :            :         }
     177                 :            : 
     178 [ +  - ][ -  + ]:         42 :         if (type == "chert") {
         [ +  - ][ -  + ]
     179                 :          0 :             auto msg = "Chert backend no longer supported";
     180 [ #  # ][ #  # ]:          0 :             throw Xapian::FeatureUnavailableError(msg);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     181                 :            :         }
     182                 :            : 
     183 [ +  - ][ -  + ]:         42 :         if (type == "flint") {
         [ +  - ][ -  + ]
     184                 :          0 :             auto msg = "Flint backend no longer supported";
     185 [ #  # ][ #  # ]:          0 :             throw Xapian::FeatureUnavailableError(msg);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     186                 :            :         }
     187                 :            : 
     188                 :            :         // Don't include the line itself - that might help an attacker
     189                 :            :         // by revealing part of a sensitive file's contents if they can
     190                 :            :         // arrange for it to be read as a stub database via infelicities in
     191                 :            :         // an application which uses Xapian.  The line number is enough
     192                 :            :         // information to identify the problem line.
     193 [ +  - ][ +  - ]:         84 :         std::string msg = file;
     194 [ +  - ][ +  - ]:         42 :         msg += ':';
     195 [ +  - ][ +  - ]:         42 :         msg += str(line_no);
         [ +  - ][ +  - ]
     196   [ +  -  +  - ]:         42 :         msg += ": Bad line";
     197 [ +  - ][ +  - ]:         42 :         throw Xapian::DatabaseOpeningError(msg);
         [ +  - ][ +  - ]
     198                 :        576 :     }
     199                 :        516 : }
     200                 :            : 
     201                 :            : #endif

Generated by: LCOV version 1.11