1/*
2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28#ifndef DatabaseAuthorizer_h
29#define DatabaseAuthorizer_h
30
31#include <wtf/Forward.h>
32#include <wtf/HashSet.h>
33#include <wtf/ThreadSafeRefCounted.h>
34#include <wtf/text/StringHash.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39extern const int SQLAuthAllow;
40extern const int SQLAuthIgnore;
41extern const int SQLAuthDeny;
42
43class DatabaseAuthorizer : public ThreadSafeRefCounted<DatabaseAuthorizer> {
44public:
45
46    enum Permissions {
47        ReadWriteMask = 0,
48        ReadOnlyMask = 1 << 1,
49        NoAccessMask = 1 << 2
50    };
51
52    static PassRefPtr<DatabaseAuthorizer> create(const String& databaseInfoTableName);
53
54    int createTable(const String& tableName);
55    int createTempTable(const String& tableName);
56    int dropTable(const String& tableName);
57    int dropTempTable(const String& tableName);
58    int allowAlterTable(const String& databaseName, const String& tableName);
59
60    int createIndex(const String& indexName, const String& tableName);
61    int createTempIndex(const String& indexName, const String& tableName);
62    int dropIndex(const String& indexName, const String& tableName);
63    int dropTempIndex(const String& indexName, const String& tableName);
64
65    int createTrigger(const String& triggerName, const String& tableName);
66    int createTempTrigger(const String& triggerName, const String& tableName);
67    int dropTrigger(const String& triggerName, const String& tableName);
68    int dropTempTrigger(const String& triggerName, const String& tableName);
69
70    int createView(const String& viewName);
71    int createTempView(const String& viewName);
72    int dropView(const String& viewName);
73    int dropTempView(const String& viewName);
74
75    int createVTable(const String& tableName, const String& moduleName);
76    int dropVTable(const String& tableName, const String& moduleName);
77
78    int allowDelete(const String& tableName);
79    int allowInsert(const String& tableName);
80    int allowUpdate(const String& tableName, const String& columnName);
81    int allowTransaction();
82
83    int allowSelect() { return SQLAuthAllow; }
84    int allowRead(const String& tableName, const String& columnName);
85
86    int allowReindex(const String& indexName);
87    int allowAnalyze(const String& tableName);
88    int allowFunction(const String& functionName);
89    int allowPragma(const String& pragmaName, const String& firstArgument);
90
91    int allowAttach(const String& filename);
92    int allowDetach(const String& databaseName);
93
94    void disable();
95    void enable();
96    void setReadOnly();
97    void setPermissions(int permissions);
98
99    void reset();
100    void resetDeletes();
101
102    bool lastActionWasInsert() const { return m_lastActionWasInsert; }
103    bool lastActionChangedDatabase() const { return m_lastActionChangedDatabase; }
104    bool hadDeletes() const { return m_hadDeletes; }
105
106private:
107    explicit DatabaseAuthorizer(const String& databaseInfoTableName);
108    void addWhitelistedFunctions();
109    int denyBasedOnTableName(const String&) const;
110    int updateDeletesBasedOnTableName(const String&);
111    bool allowWrite();
112
113    int m_permissions;
114    bool m_securityEnabled : 1;
115    bool m_lastActionWasInsert : 1;
116    bool m_lastActionChangedDatabase : 1;
117    bool m_hadDeletes : 1;
118
119    const String m_databaseInfoTableName;
120
121    HashSet<String, CaseFoldingHash> m_whitelistedFunctions;
122};
123
124} // namespace WebCore
125
126#endif // DatabaseAuthorizer_h
127