Debian Codegraph @

src/main/org/h2/engine/User.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.engine;
7
8 import java.util.ArrayList;
9 import java.util.Arrays;
10
11 import org.h2.api.ErrorCode;
12 import org.h2.message.DbException;
13 import org.h2.message.Trace;
14 import org.h2.schema.Schema;
15 import org.h2.security.SHA256;
16 import org.h2.table.DualTable;
17 import org.h2.table.MetaTable;
18 import org.h2.table.RangeTable;
19 import org.h2.table.Table;
20 import org.h2.table.TableType;
21 import org.h2.util.MathUtils;
22 import org.h2.util.StringUtils;
23 import org.h2.util.Utils;
24
25 /**
26 * Represents a user object.
27 */
28 public final class User extends RightOwner {
29
30 private final boolean systemUser;
31 private byte[] salt;
32 private byte[] passwordHash;
33 private boolean admin;
34
35 public User(Database database, int id, String userName, boolean systemUser) {
36 super(database, id, userName, Trace.USER);
37 this.systemUser = systemUser;
38 }
39
40 public void setAdmin(boolean admin) {
41 this.admin = admin;
42 }
43
44 public boolean isAdmin() {
45 return admin;
46 }
47
48 /**
49 * Set the salt and hash of the password for this user.
50 *
51 * @param salt the salt
52 * @param hash the password hash
53 */
54 public void setSaltAndHash(byte[] salt, byte[] hash) {
55 this.salt = salt;
56 this.passwordHash = hash;
57 }
58
59 /**
60 * Set the user name password hash. A random salt is generated as well.
61 * The parameter is filled with zeros after use.
62 *
63 * @param userPasswordHash the user name password hash
64 */
65 public void setUserPasswordHash(byte[] userPasswordHash) {
66 if (userPasswordHash != null) {
67 if (userPasswordHash.length == 0) {
68 salt = passwordHash = userPasswordHash;
69 } else {
70 salt = new byte[Constants.SALT_LEN];
71 MathUtils.randomBytes(salt);
72 passwordHash = SHA256.getHashWithSalt(userPasswordHash, salt);
73 }
74 }
75 }
76
77 @Override
78 public String getCreateSQL() {
79 return getCreateSQL(true);
80 }
81
82 /**
83 * Get the CREATE SQL statement for this object.
84 *
85 * @param password true if the password (actually the salt and hash) should
86 * be returned
87 * @return the SQL statement
88 */
89 public String getCreateSQL(boolean password) {
90 StringBuilder buff = new StringBuilder("CREATE USER IF NOT EXISTS ");
91 getSQL(buff, DEFAULT_SQL_FLAGS);
92 if (comment != null) {
93 buff.append(" COMMENT ");
94 StringUtils.quoteStringSQL(buff, comment);
95 }
96 if (password) {
97 buff.append(" SALT '");
98 StringUtils.convertBytesToHex(buff, salt).
99 append("' HASH '");
100 StringUtils.convertBytesToHex(buff, passwordHash).
101 append('\'');
102 } else {
103 buff.append(" PASSWORD ''");
104 }
105 if (admin) {
106 buff.append(" ADMIN");
107 }
108 return buff.toString();
109 }
110
111 /**
112 * Check the password of this user.
113 *
114 * @param userPasswordHash the password data (the user password hash)
115 * @return true if the user password hash is correct
116 */
117 boolean validateUserPasswordHash(byte[] userPasswordHash) {
118 if (userPasswordHash.length == 0 && passwordHash.length == 0) {
119 return true;
120 }
121 if (userPasswordHash.length == 0) {
122 userPasswordHash = SHA256.getKeyPasswordHash(getName(), new char[0]);
123 }
124 byte[] hash = SHA256.getHashWithSalt(userPasswordHash, salt);
125 return Utils.compareSecure(hash, passwordHash);
126 }
127
128 /**
129 * Checks if this user has admin rights. An exception is thrown if user
130 * doesn't have them.
131 *
132 * @throws DbException if this user is not an admin
133 */
134 public void checkAdmin() {
135 if (!admin) {
136 throw DbException.get(ErrorCode.ADMIN_RIGHTS_REQUIRED);
137 }
138 }
139
140 /**
141 * Checks if this user has schema admin rights for every schema. An
142 * exception is thrown if user doesn't have them.
143 *
144 * @throws DbException if this user is not a schema admin
145 */
146 public void checkSchemaAdmin() {
147 if (!hasSchemaRight(null)) {
148 throw DbException.get(ErrorCode.ADMIN_RIGHTS_REQUIRED);
149 }
150 }
151
152 /**
153 * Checks if this user has schema owner rights for the specified schema. An
154 * exception is thrown if user doesn't have them.
155 *
156 * @param schema the schema
157 * @throws DbException if this user is not a schema owner
158 */
159 public void checkSchemaOwner(Schema schema) {
160 if (!hasSchemaRight(schema)) {
161 throw DbException.get(ErrorCode.NOT_ENOUGH_RIGHTS_FOR_1, schema.getTraceSQL());
162 }
163 }
164
165 /**
166 * See if this user has owner rights for the specified schema
167 *
168 * @param schema the schema
169 * @return true if the user has the rights
170 */
171 private boolean hasSchemaRight(Schema schema) {
172 if (admin) {
173 return true;
174 }
175 Role publicRole = database.getPublicRole();
176 if (publicRole.isSchemaRightGrantedRecursive(schema)) {
177 return true;
178 }
179 return isSchemaRightGrantedRecursive(schema);
180 }
181
182 /**
183 * Checks that this user has the given rights for the specified table.
184 *
185 * @param table the table
186 * @param rightMask the rights required
187 * @throws DbException if this user does not have the required rights
188 */
189 public void checkTableRight(Table table, int rightMask) {
190 if (!hasTableRight(table, rightMask)) {
191 throw DbException.get(ErrorCode.NOT_ENOUGH_RIGHTS_FOR_1, table.getTraceSQL());
192 }
193 }
194
195 /**
196 * See if this user has the given rights for this database object.
197 *
198 * @param table the database object, or null for schema-only check
199 * @param rightMask the rights required
200 * @return true if the user has the rights
201 */
202 public boolean hasTableRight(Table table, int rightMask) {
203 if (rightMask != Right.SELECT && !systemUser) {
204 table.checkWritingAllowed();
205 }
206 if (admin) {
207 return true;
208 }
209 Role publicRole = database.getPublicRole();
210 if (publicRole.isTableRightGrantedRecursive(table, rightMask)) {
211 return true;
212 }
213 if (table instanceof MetaTable || table instanceof DualTable || table instanceof RangeTable) {
214 // everybody has access to the metadata information
215 return true;
216 }
217 TableType tableType = table.getTableType();
218 if (tableType == null) {
219 // derived or function table
220 return true;
221 }
222 if (table.isTemporary() && !table.isGlobalTemporary()) {
223 // the owner has all rights on local temporary tables
224 return true;
225 }
226 return isTableRightGrantedRecursive(table, rightMask);
227 }
228
229 @Override
230 public int getType() {
231 return DbObject.USER;
232 }
233
234 @Override
235 public ArrayList<DbObject> getChildren() {
236 ArrayList<DbObject> children = new ArrayList<>();
237 for (Right right : database.getAllRights()) {
238 if (right.getGrantee() == this) {
239 children.add(right);
240 }
241 }
242 for (Schema schema : database.getAllSchemas()) {
243 if (schema.getOwner() == this) {
244 children.add(schema);
245 }
246 }
247 return children;
248 }
249
250 @Override
251 public void removeChildrenAndResources(SessionLocal session) {
252 for (Right right : database.getAllRights()) {
253 if (right.getGrantee() == this) {
254 database.removeDatabaseObject(session, right);
255 }
256 }
257 database.removeMeta(session, getId());
258 salt = null;
259 Arrays.fill(passwordHash, (byte) 0);
260 passwordHash = null;
261 invalidate();
262 }
263
264 }