1 /*****************************************************************************
2  * Author:   Valient Gough <vgough@pobox.com>
3  *
4  *****************************************************************************
5  * Copyright (c) 2004, Valient Gough
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _FileUtils_incl_
22 #define _FileUtils_incl_
23 
24 #include <memory>
25 #include <string>
26 #include <sys/types.h>
27 
28 #include "CipherKey.h"
29 #include "FSConfig.h"
30 #include "Interface.h"
31 #include "encfs.h"
32 
33 namespace encfs {
34 
35 // true if the path points to an existing node (of any type)
36 bool fileExists(const char *fileName);
37 // true if path is a directory
38 bool isDirectory(const char *fileName);
39 // true if starts with '/'
40 bool isAbsolutePath(const char *fileName);
41 // pointer to just after the last '/'
42 const char *lastPathElement(const char *name);
43 
44 std::string parentDirectory(const std::string &path);
45 
46 // ask the user for permission to create the directory.  If they say ok, then
47 // do it and return true.
48 bool userAllowMkdir(const char *dirPath, mode_t mode);
49 bool userAllowMkdir(int promptno, const char *dirPath, mode_t mode);
50 
51 class Cipher;
52 class DirNode;
53 
54 struct EncFS_Root {
55   std::shared_ptr<Cipher> cipher;
56   CipherKey volumeKey;
57   std::shared_ptr<DirNode> root;
58 
59   EncFS_Root();
60   ~EncFS_Root();
61 };
62 
63 using RootPtr = std::shared_ptr<EncFS_Root>;
64 
65 enum ConfigMode { Config_Prompt, Config_Standard, Config_Paranoia };
66 
67 /**
68  * EncFS_Opts stores internal settings
69  *
70  * See struct EncFS_Args (main.cpp) for the parsed command line arguments
71  */
72 struct EncFS_Opts {
73   std::string rootDir;
74   std::string mountPoint;  // where to make filesystem visible
75   std::string unmountPoint;// same as mountPoint, but as given by the user
76   std::string cygDrive;    // Cygwin mount drive
77   bool createIfNotFound;   // create filesystem if not found
78   bool idleTracking;       // turn on idle monitoring of filesystem
79   bool mountOnDemand;      // mounting on-demand
80   bool delayMount;         // delay initial mount
81   bool unmount;            // unmount
82 
83   bool checkKey;     // check crypto key decoding
84   bool forceDecode;  // force decode on MAC block failures
85 
86   std::string passwordProgram;  // path to password program (or empty)
87   bool useStdin;  // read password from stdin rather then prompting
88   bool annotate;  // print annotation line prompt to stderr.
89 
90   bool ownerCreate;  // set owner of new files to caller
91 
92   bool reverseEncryption;  // Reverse encryption
93 
94   bool noCache; /* Disable block cache (in EncFS) and stat cache (in kernel).
95                  * This is needed if the backing files may be modified
96                  * behind the back of EncFS (for example, in reverse mode).
97                  * See main.cpp for a longer explaination. */
98 
99   bool readOnly;  // Mount read-only
100 
101   bool insecure; // Allow to use plain data / to disable data encoding
102 
103   bool requireMac;  // Throw an error if MAC is disabled
104 
105   ConfigMode configMode;
106   std::string config;  // path to configuration file (or empty)
107 
108   EncFS_Opts() {
109     createIfNotFound = true;
110     idleTracking = false;
111     mountOnDemand = false;
112     delayMount = false;
113     unmount = false;
114     checkKey = true;
115     forceDecode = false;
116     useStdin = false;
117     annotate = false;
118     ownerCreate = false;
119     reverseEncryption = false;
120     configMode = Config_Prompt;
121     noCache = false;
122     readOnly = false;
123     insecure = false;
124     requireMac = false;
125   }
126 };
127 
128 /*
129     Read existing config file.  Looks for any supported configuration version.
130 */
131 ConfigType readConfig(const std::string &rootDir, EncFSConfig *config, const std::string &cmdConfig);
132 
133 /*
134     Save the configuration.  Saves back as the same configuration type as was
135     read from.
136 */
137 bool saveConfig(ConfigType type, const std::string &rootdir,
138                 const EncFSConfig *config, const std::string &cmdConfig);
139 
140 class EncFS_Context;
141 
142 RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr<EncFS_Opts> &opts);
143 
144 void unmountFS(const char *mountPoint);
145 
146 RootPtr createV6Config(EncFS_Context *ctx,
147                        const std::shared_ptr<EncFS_Opts> &opts);
148 
149 void showFSInfo(const EncFSConfig *config);
150 
151 bool readV4Config(const char *configFile, EncFSConfig *config,
152                   struct ConfigInfo *);
153 bool writeV4Config(const char *configFile, const EncFSConfig *config);
154 
155 bool readV5Config(const char *configFile, EncFSConfig *config,
156                   struct ConfigInfo *);
157 bool writeV5Config(const char *configFile, const EncFSConfig *config);
158 
159 bool readV6Config(const char *configFile, EncFSConfig *config,
160                   struct ConfigInfo *);
161 bool writeV6Config(const char *configFile, const EncFSConfig *config);
162 
163 }  // namespace encfs
164 
165 #endif
166