]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CertificateProfile.java
upd: convert to PostgreSQL
[gigi.git] / src / org / cacert / gigi / dbObjects / CertificateProfile.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Properties;
12
13 import org.cacert.gigi.database.DatabaseConnection;
14 import org.cacert.gigi.database.GigiPreparedStatement;
15 import org.cacert.gigi.database.GigiResultSet;
16
17 public class CertificateProfile implements IdCachable {
18
19     private final int id;
20
21     private final String keyName;
22
23     private final String visibleName;
24
25     private static final Map<String, CertificateProfile> byName;
26
27     private static final Map<Integer, CertificateProfile> byId;
28
29     private final Map<String, PropertyTemplate> pt;
30
31     private final List<String> req;
32
33     private CertificateProfile(int id, String keyName, String visibleName, String requires, String include) {
34         this.id = id;
35         this.keyName = keyName;
36         this.visibleName = visibleName;
37         req = parseConditions(requires);
38         pt = parsePropertyTemplates(include);
39     }
40
41     public static class PropertyTemplate implements Comparable<PropertyTemplate> {
42
43         private boolean required = true;
44
45         private boolean multiple = false;
46
47         private String base;
48
49         public PropertyTemplate(String inc) {
50             if (inc.endsWith("?") || inc.endsWith("*") || inc.endsWith("+")) {
51                 char sfx = inc.charAt(inc.length() - 1);
52                 if (sfx == '?') {
53                     required = false;
54                 } else if (sfx == '*') {
55                     multiple = true;
56                     required = false;
57                 } else {
58                     multiple = true;
59                 }
60                 inc = inc.substring(0, inc.length() - 1);
61             }
62             this.base = inc;
63         }
64
65         public String getBase() {
66             return base;
67         }
68
69         public boolean isMultiple() {
70             return multiple;
71         }
72
73         public boolean isRequired() {
74             return required;
75         }
76
77         @Override
78         public int hashCode() {
79             final int prime = 31;
80             int result = 1;
81             result = prime * result + ((base == null) ? 0 : base.hashCode());
82             result = prime * result + (multiple ? 1231 : 1237);
83             result = prime * result + (required ? 1231 : 1237);
84             return result;
85         }
86
87         @Override
88         public boolean equals(Object obj) {
89             if (this == obj) {
90                 return true;
91             }
92             if (obj == null) {
93                 return false;
94             }
95             if (getClass() != obj.getClass()) {
96                 return false;
97             }
98             PropertyTemplate other = (PropertyTemplate) obj;
99             if (base == null) {
100                 if (other.base != null) {
101                     return false;
102                 }
103             } else if ( !base.equals(other.base)) {
104                 return false;
105             }
106             if (multiple != other.multiple) {
107                 return false;
108             }
109             if (required != other.required) {
110                 return false;
111             }
112             return true;
113         }
114
115         @Override
116         public String toString() {
117             return base + (multiple ? (required ? "+" : "*") : (required ? "" : "?"));
118         }
119
120         @Override
121         public int compareTo(PropertyTemplate o) {
122             return toString().compareTo(o.toString());
123         }
124
125     }
126
127     private CertificateProfile(File f) throws IOException {
128         Properties p = new Properties();
129         p.load(new FileInputStream(f));
130         String[] parts = f.getName().split("\\.")[0].split("-", 2);
131         id = Integer.parseInt(parts[0]);
132         keyName = parts[1];
133         visibleName = "";
134         pt = parsePropertyTemplates(p.getProperty("include"));
135         req = parseConditions(p.getProperty("requires", ""));
136     }
137
138     private List<String> parseConditions(String property) {
139         String[] split2 = property.split(",");
140         if (split2.length == 1 && split2[0].equals("")) {
141             split2 = new String[0];
142         }
143         return Collections.unmodifiableList(Arrays.asList(split2));
144     }
145
146     private Map<String, PropertyTemplate> parsePropertyTemplates(String property) {
147         String[] split = property.split(",");
148         HashMap<String, PropertyTemplate> map = new HashMap<>(split.length);
149         for (int i = 0; i < split.length; i++) {
150
151             PropertyTemplate value = new PropertyTemplate(split[i]);
152
153             map.put(value.getBase(), value);
154         }
155         return Collections.unmodifiableMap(map);
156     }
157
158     public int getId() {
159         return id;
160     }
161
162     public String getKeyName() {
163         return keyName;
164     }
165
166     public String getVisibleName() {
167         return visibleName;
168     }
169
170     public Map<String, PropertyTemplate> getTemplates() {
171         return pt;
172     }
173
174     public List<String> getReqireds() {
175         return req;
176     }
177
178     static {
179         final HashMap<String, CertificateProfile> myName = new HashMap<String, CertificateProfile>();
180         final HashMap<Integer, CertificateProfile> myId = new HashMap<Integer, CertificateProfile>();
181
182         for (File f : new File("config/profiles").listFiles()) {
183             Properties p = new Properties();
184             try {
185                 p.load(new FileInputStream(f));
186             } catch (IOException e) {
187                 throw new Error("Unable to load profile from " + f.getName(), e);
188             }
189
190             String[] parts = f.getName().split("\\.")[0].split("-", 2);
191             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `keyname`, `include`, `requires`, `name` FROM `profiles` WHERE `id`=?");
192             ps.setInt(1, Integer.parseInt(parts[0]));
193             GigiResultSet rs = ps.executeQuery();
194
195             if (rs.next()) {
196                 if ( !rs.getString("keyname").equals(parts[1])) {
197                     throw new Error("Config error. Certificate Profile mismatch");
198                 }
199                 if ( !rs.getString("include").equals(p.getProperty("include"))) {
200                     throw new Error("Config error. Certificate Profile mismatch");
201                 }
202                 if ( !rs.getString("requires").equals(p.getProperty("requires", ""))) {
203                     throw new Error("Config error. Certificate Profile mismatch");
204                 }
205             } else {
206                 GigiPreparedStatement insert = DatabaseConnection.getInstance().prepare("INSERT INTO `profiles` SET `keyname`=?, `include`=?, `requires`=?, `name`=?, `id`=?");
207                 insert.setString(1, parts[1]);
208                 insert.setString(2, p.getProperty("include"));
209                 insert.setString(3, p.getProperty("requires", ""));
210                 insert.setString(4, p.getProperty("name"));
211                 insert.setInt(5, Integer.parseInt(parts[0]));
212                 insert.execute();
213             }
214         }
215
216         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `id`, `keyname`, `name`, `requires`, `include` FROM `profiles`");
217         GigiResultSet rs = ps.executeQuery();
218         while (rs.next()) {
219             CertificateProfile cp = new CertificateProfile(rs.getInt("id"), rs.getString("keyName"), rs.getString("name"), rs.getString("requires"), rs.getString("include"));
220             myId.put(cp.getId(), cp);
221             myName.put(cp.getKeyName(), cp);
222         }
223
224         byName = Collections.unmodifiableMap(myName);
225         byId = Collections.unmodifiableMap(myId);
226     }
227
228     public static CertificateProfile getById(int id) {
229         return byId.get(id);
230     }
231
232     public static CertificateProfile getByName(String name) {
233         return byName.get(name);
234     }
235
236     public static CertificateProfile[] getAll() {
237         return byId.values().toArray(new CertificateProfile[byId.size()]);
238     }
239
240     public boolean canBeIssuedBy(User u) {
241         for (String s : req) {
242             if (s.equals("points>=50")) {
243                 if (u.getAssurancePoints() < 50) {
244                     return false;
245                 }
246             } else if (s.equals("points>=100")) {
247                 if (u.getAssurancePoints() < 100) {
248                     return false;
249                 }
250             } else if (s.equals("codesign")) {
251                 if (u.isInGroup(Group.CODESIGNING)) {
252                     return false;
253                 }
254             } else {
255                 return false;
256             }
257
258         }
259         return true;
260     }
261
262 }