LCOV - code coverage report
Current view: top level - backends - databasehelpers.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7822d31adece Lines: 56 64 87.5 %
Date: 2019-05-23 11:15:29 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 <cstdlib>
      27                 :            : #include <fstream>
      28                 :            : #include <string>
      29                 :            : 
      30                 :            : #include "fileutils.h"
      31                 :            : #include "safesysstat.h"
      32                 :            : #include "safeunistd.h"
      33                 :            : #include "str.h"
      34                 :            : #include "xapian/error.h"
      35                 :            : 
      36                 :            : /** Probe if a path is a single-file database.
      37                 :            :  *
      38                 :            :  *  @param sb      struct statbuf from calling stat() on path
      39                 :            :  *  @param path    path to probe
      40                 :            :  *  @param fd_ptr  used to return fd open of path (unless BACKEND_UNKNOWN is
      41                 :            :  *                 returned)
      42                 :            :  *
      43                 :            :  *  @return  A BACKEND_* constant from backends.h:
      44                 :            :  *           * BACKEND_UNKNOWN : unknown (could be a stub file)
      45                 :            :  *           * BACKEND_GLASS : glass single file
      46                 :            :  *           * BACKEND_HONEY : honey single file
      47                 :            :  */
      48                 :            : int
      49                 :            : test_if_single_file_db(const struct stat& sb,
      50                 :            :                        const std::string& path,
      51                 :            :                        int* fd_ptr);
      52                 :            : 
      53                 :            : /** Open, read and process a stub database file.
      54                 :            :  *
      55                 :            :  *  Implemented as a template with separate actions for each database type.
      56                 :            :  */
      57                 :            : template<typename A1,
      58                 :            :          typename A2,
      59                 :            :          typename A3,
      60                 :            :          typename A4,
      61                 :            :          typename A5,
      62                 :            :          typename A6>
      63                 :            : void
      64                 :        575 : read_stub_file(const std::string& file,
      65                 :            :                A1 action_auto,
      66                 :            :                A2 action_glass,
      67                 :            :                A3 action_honey,
      68                 :            :                A4 action_remote_prog,
      69                 :            :                A5 action_remote_tcp,
      70                 :            :                A6 action_inmemory)
      71                 :            : {
      72                 :            :     // A stub database is a text file with one or more lines of this format:
      73                 :            :     // <dbtype> <serialised db object>
      74                 :            :     //
      75                 :            :     // Lines which start with a "#" character are ignored.
      76                 :            :     //
      77                 :            :     // Any paths specified in stub database files which are relative will be
      78                 :            :     // considered to be relative to the directory containing the stub database.
      79 [ +  - ][ +  - ]:        575 :     std::ifstream stub(file.c_str());
      80 [ +  - ][ +  + ]:        575 :     if (!stub) {
         [ +  - ][ +  + ]
      81 [ +  - ][ +  - ]:          2 :         std::string msg = "Couldn't open stub database file: ";
      82   [ +  -  +  - ]:          2 :         msg += file;
      83 [ +  - ][ +  - ]:          2 :         throw Xapian::DatabaseNotFoundError(msg, errno);
      84                 :            :     }
      85 [ +  - ][ +  - ]:       1145 :     std::string line;
      86                 :        573 :     unsigned int line_no = 0;
      87 [ +  - ][ +  - ]:       1669 :     while (std::getline(stub, line)) {
         [ +  + ][ +  - ]
         [ +  - ][ +  + ]
      88                 :       1097 :         ++line_no;
      89 [ +  - ][ +  - ]:       1097 :         if (line.empty() || line[0] == '#')
         [ +  + ][ +  + ]
         [ +  - ][ +  - ]
         [ +  + ][ +  + ]
      90                 :         62 :             continue;
      91                 :       1035 :         std::string::size_type space = line.find(' ');
      92 [ +  + ][ +  + ]:       1035 :         if (space == std::string::npos) space = line.size();
      93                 :            : 
      94 [ +  - ][ +  - ]:       1035 :         std::string type(line, 0, space);
      95 [ +  - ][ +  - ]:       1035 :         line.erase(0, space + 1);
      96                 :            : 
      97 [ +  - ][ +  + ]:       1035 :         if (type == "auto") {
         [ +  - ][ +  + ]
      98 [ +  - ][ +  - ]:        107 :             resolve_relative_path(line, file);
      99 [ +  + ][ +  - ]:        107 :             action_auto(line);
     100                 :        105 :             continue;
     101                 :            :         }
     102                 :            : 
     103 [ +  - ][ +  + ]:        928 :         if (type == "glass") {
         [ +  - ][ +  + ]
     104                 :            : #ifdef XAPIAN_HAS_GLASS_BACKEND
     105 [ +  - ][ +  - ]:        861 :             resolve_relative_path(line, file);
     106 [ +  + ][ +  - ]:        861 :             action_glass(line);
     107                 :        859 :             continue;
     108                 :            : #else
     109                 :            :             (void)action_glass;
     110                 :            :             throw Xapian::FeatureUnavailableError("Glass backend disabled");
     111                 :            : #endif
     112                 :            :         }
     113                 :            : 
     114 [ +  - ][ -  + ]:         67 :         if (type == "honey") {
         [ +  - ][ -  + ]
     115                 :            : #ifdef XAPIAN_HAS_HONEY_BACKEND
     116 [ #  # ][ #  # ]:          0 :             resolve_relative_path(line, file);
     117 [ #  # ][ #  # ]:          0 :             action_honey(line);
     118                 :          0 :             continue;
     119                 :            : #else
     120                 :            :             (void)action_honey;
     121                 :            :             throw Xapian::FeatureUnavailableError("Honey backend disabled");
     122                 :            : #endif
     123                 :            :         }
     124                 :            : 
     125 [ +  - ][ +  + ]:         67 :         if (type == "remote" && !line.empty()) {
         [ +  + ][ +  + ]
         [ +  - ][ +  + ]
         [ +  + ][ +  + ]
     126                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     127 [ +  - ][ +  + ]:         36 :             if (line[0] == ':') {
         [ +  - ][ +  + ]
     128                 :            :                 // prog
     129                 :            :                 // FIXME: timeouts
     130                 :            :                 // Is it a security risk?
     131                 :         12 :                 space = line.find(' ');
     132   [ +  -  +  - ]:         12 :                 std::string args;
     133 [ +  - ][ +  - ]:         12 :                 if (space != std::string::npos) {
     134 [ +  - ][ +  - ]:         12 :                     args.assign(line, space + 1, std::string::npos);
     135 [ +  - ][ +  - ]:         12 :                     line.assign(line, 1, space - 1);
     136                 :            :                 } else {
     137 [ #  # ][ #  # ]:          0 :                     line.erase(0, 1);
     138                 :            :                 }
     139 [ -  + ][ +  - ]:         12 :                 action_remote_prog(line, args);
     140                 :         12 :                 continue;
     141                 :            :             }
     142                 :         24 :             std::string::size_type colon = line.rfind(':');
     143   [ +  +  +  + ]:         24 :             if (colon != std::string::npos) {
     144                 :            :                 // tcp
     145                 :            :                 // FIXME: timeouts
     146                 :            :                 // Avoid misparsing an IPv6 address without a port number.  The
     147                 :            :                 // port number is required, so just leave that case to the
     148                 :            :                 // error handling further below.
     149 [ +  - ][ +  - ]:         16 :                 if (!(line[0] == '[' && line.back() == ']')) {
         [ +  - ][ +  + ]
         [ +  + ][ +  - ]
         [ +  - ][ +  - ]
         [ +  + ][ +  + ]
     150                 :          8 :                     unsigned int port = std::atoi(line.c_str() + colon + 1);
     151   [ +  -  +  - ]:          8 :                     line.erase(colon);
     152 [ +  - ][ +  - ]:          8 :                     if (line[0] == '[' && line.back() == ']') {
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     153 [ +  - ][ +  - ]:          8 :                         line.erase(line.size() - 1, 1);
     154 [ +  - ][ +  - ]:          8 :                         line.erase(0, 1);
     155                 :            :                     }
     156 [ -  + ][ -  + ]:          8 :                     action_remote_tcp(line, port);
     157                 :         16 :                     continue;
     158                 :            :                 }
     159                 :            :             }
     160                 :            : #else
     161                 :            :             (void)action_remote_prog;
     162                 :            :             (void)action_remote_tcp;
     163                 :            :             throw Xapian::FeatureUnavailableError("Remote backend disabled");
     164                 :            : #endif
     165                 :            :         }
     166                 :            : 
     167 [ +  - ][ +  + ]:         47 :         if (type == "inmemory" && line.empty()) {
         [ +  - ][ +  + ]
         [ +  - ][ +  + ]
         [ +  - ][ +  + ]
     168                 :            : #ifdef XAPIAN_HAS_INMEMORY_BACKEND
     169 [ +  + ][ +  - ]:          5 :             action_inmemory();
     170                 :          4 :             continue;
     171                 :            : #else
     172                 :            :             (void)action_inmemory;
     173                 :            :             throw Xapian::FeatureUnavailableError("Inmemory backend disabled");
     174                 :            : #endif
     175                 :            :         }
     176                 :            : 
     177 [ +  - ][ -  + ]:         42 :         if (type == "chert") {
         [ +  - ][ -  + ]
     178                 :          0 :             auto msg = "Chert backend no longer supported";
     179 [ #  # ][ #  # ]:          0 :             throw Xapian::FeatureUnavailableError(msg);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     180                 :            :         }
     181                 :            : 
     182 [ +  - ][ -  + ]:         42 :         if (type == "flint") {
         [ +  - ][ -  + ]
     183                 :          0 :             auto msg = "Flint backend no longer supported";
     184 [ #  # ][ #  # ]:          0 :             throw Xapian::FeatureUnavailableError(msg);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     185                 :            :         }
     186                 :            : 
     187                 :            :         // Don't include the line itself - that might help an attacker
     188                 :            :         // by revealing part of a sensitive file's contents if they can
     189                 :            :         // arrange for it to be read as a stub database via infelicities in
     190                 :            :         // an application which uses Xapian.  The line number is enough
     191                 :            :         // information to identify the problem line.
     192 [ +  - ][ +  - ]:         84 :         std::string msg = file;
     193 [ +  - ][ +  - ]:         42 :         msg += ':';
     194 [ +  - ][ +  - ]:         42 :         msg += str(line_no);
         [ +  - ][ +  - ]
     195   [ +  -  +  - ]:         42 :         msg += ": Bad line";
     196 [ +  - ][ +  - ]:         42 :         throw Xapian::DatabaseOpeningError(msg);
         [ +  - ][ +  - ]
     197                 :        574 :     }
     198                 :        514 : }
     199                 :            : 
     200                 :            : #endif

Generated by: LCOV version 1.11