Import upstream 0.7.1
[manu/suphp.git] / src / Application.hpp
1 /*
2     suPHP - (c)2002-2008 Sebastian Marsching <sebastian@marsching.com>
3
4     This file is part of suPHP.
5
6     suPHP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     suPHP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with suPHP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifndef SUPHP_APPLICATION_H
22
23 namespace suPHP {
24     class Application;
25 };
26
27 enum TargetMode {
28     TARGETMODE_PHP,
29     TARGETMODE_SELFEXECUTE
30 };
31
32 #define SUPHP_APPLICATION_H
33
34 #include <string>
35 #include <iostream>
36
37 #include "Environment.hpp"
38 #include "CommandLine.hpp"
39 #include "SystemException.hpp"
40 #include "SoftException.hpp"
41 #include "SecurityException.hpp"
42 #include "UserInfo.hpp"
43 #include "GroupInfo.hpp"
44
45 namespace suPHP {
46     /**
47      * Main application class.
48      * Contains the main() function.
49      */
50     class Application {
51     private:
52         /**
53          * Print message containing version information
54          */
55         void printAboutMessage();
56         
57         /**
58          * Checks wheter process has root privileges
59          * and calling user is webserver user
60          */
61         void checkProcessPermissions(Configuration& config) 
62             throw (SecurityException, LookupException);
63         
64         /**
65          * Checks scriptfile (first stage).
66          * Includes check for VHost docroot, symbollink and permissions.
67          */
68         void checkScriptFileStage1(const std::string& scriptFilename, 
69                              const Configuration& config, 
70                              const Environment& environment) const
71             throw (SystemException, SoftException);
72         
73         /**
74          * Checks scriptfile.
75          * Includes check for paths which might be user specific
76          */
77         void checkScriptFileStage2(const std::string& scriptFilename, 
78                              const Configuration& config, 
79                              const Environment& environment,
80                              const UserInfo& targetUser,
81                              const GroupInfo& targetGroup) const
82             throw (SystemException, SoftException);
83         
84         /**
85          * Determines target user and group that is to be used for script execution.
86          * Uses preprocessor macros to distinguish between modes
87          */
88         void checkProcessPermissions(const std::string& scriptFilename,
89                                       const Configuration& config,
90                                       const Environment& environment,
91                                       UserInfo& targetUser,
92                                       GroupInfo& targetGroup) const
93             throw (SystemException, SoftException, SecurityException);
94         
95         /**
96          * Changes process permission (user and group).
97          * Uses preprocessor macros to distinguish between modes
98          */
99         void changeProcessPermissions(const Configuration& config,
100                                       const UserInfo& targetUser,
101                                       const GroupInfo& targetGroup) const
102             throw (SystemException, SoftException, SecurityException);
103
104         /**
105          * Prepares the environment before invoking the script
106          */
107         Environment prepareEnvironment(const Environment& sourceEnv,
108                                        const Configuration& config,
109                                        TargetMode mode)
110             throw (KeyNotFoundException);
111
112         /**
113          * Returns interpreter for script being executed
114          */
115         std::string getInterpreter(const Environment& env,
116                                    const Configuration& config)
117             throw (SecurityException);
118         
119         /**
120          * Returns mode interpreter is using
121          */
122         TargetMode getTargetMode(const std::string& interpreter)
123             throw (SecurityException);
124
125         /**
126          * Runs script
127          */
128         void executeScript(const std::string& scriptFilename,
129                            const std::string& interpreter,
130                            TargetMode mode,
131                            const Environment& env,
132                            const Configuration& config) const
133             throw (SoftException);
134         
135         /**
136          * Checks ownership and permissions for parent directories
137          */
138         void checkParentDirectories(const File& file,
139                                     const UserInfo& owner,
140                                     const Configuration& config) const
141             throw (SoftException);
142
143
144     public:
145         /**
146          * Constructer
147          */
148         Application();
149         
150         /**
151          * Function called by the main() function
152          */
153         int run(CommandLine& cmdline, Environment& env);
154     };
155 };
156
157 int main(int argc, char **argv);
158
159 #endif // SUPHP_APPLICATION_H