blob: 60a8ead2a6fcbc5267d7a352207c8b9d9988391b [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2function init_db_schema() {
3 try {
4 global $pdo;
5
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01006 $db_version = "31102021_0620";
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01007
8 $stmt = $pdo->query("SHOW TABLES LIKE 'versions'");
9 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
10 if ($num_results != 0) {
11 $stmt = $pdo->query("SELECT `version` FROM `versions` WHERE `application` = 'db_schema'");
12 if ($stmt->fetch(PDO::FETCH_ASSOC)['version'] == $db_version) {
13 return true;
14 }
15 if (!preg_match('/y|yes/i', getenv('MASTER'))) {
16 $_SESSION['return'][] = array(
17 'type' => 'warning',
18 'log' => array(__FUNCTION__),
19 'msg' => 'Database not initialized: not running db_init on slave.'
20 );
21 return true;
22 }
23 }
24
25 $views = array(
26 "grouped_mail_aliases" => "CREATE VIEW grouped_mail_aliases (username, aliases) AS
27 SELECT goto, IFNULL(GROUP_CONCAT(address ORDER BY address SEPARATOR ' '), '') AS address FROM alias
28 WHERE address!=goto
29 AND active = '1'
30 AND sogo_visible = '1'
31 AND address NOT LIKE '@%'
32 GROUP BY goto;",
33 // START
34 // Unused at the moment - we cannot allow to show a foreign mailbox as sender address in SOGo, as SOGo does not like this
35 // We need to create delegation in SOGo AND set a sender_acl in mailcow to allow to send as user X
36 "grouped_sender_acl" => "CREATE VIEW grouped_sender_acl (username, send_as_acl) AS
37 SELECT logged_in_as, IFNULL(GROUP_CONCAT(send_as SEPARATOR ' '), '') AS send_as_acl FROM sender_acl
38 WHERE send_as NOT LIKE '@%'
39 GROUP BY logged_in_as;",
40 // END
41 "grouped_sender_acl_external" => "CREATE VIEW grouped_sender_acl_external (username, send_as_acl) AS
42 SELECT logged_in_as, IFNULL(GROUP_CONCAT(send_as SEPARATOR ' '), '') AS send_as_acl FROM sender_acl
43 WHERE send_as NOT LIKE '@%' AND external = '1'
44 GROUP BY logged_in_as;",
45 "grouped_domain_alias_address" => "CREATE VIEW grouped_domain_alias_address (username, ad_alias) AS
46 SELECT username, IFNULL(GROUP_CONCAT(local_part, '@', alias_domain SEPARATOR ' '), '') AS ad_alias FROM mailbox
47 LEFT OUTER JOIN alias_domain ON target_domain=domain
48 GROUP BY username;",
49 "sieve_before" => "CREATE VIEW sieve_before (id, username, script_name, script_data) AS
50 SELECT md5(script_data), username, script_name, script_data FROM sieve_filters
51 WHERE filter_type = 'prefilter';",
52 "sieve_after" => "CREATE VIEW sieve_after (id, username, script_name, script_data) AS
53 SELECT md5(script_data), username, script_name, script_data FROM sieve_filters
54 WHERE filter_type = 'postfilter';"
55 );
56
57 $tables = array(
58 "versions" => array(
59 "cols" => array(
60 "application" => "VARCHAR(255) NOT NULL",
61 "version" => "VARCHAR(100) NOT NULL",
62 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
63 ),
64 "keys" => array(
65 "primary" => array(
66 "" => array("application")
67 )
68 ),
69 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
70 ),
71 "admin" => array(
72 "cols" => array(
73 "username" => "VARCHAR(255) NOT NULL",
74 "password" => "VARCHAR(255) NOT NULL",
75 "superadmin" => "TINYINT(1) NOT NULL DEFAULT '0'",
76 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
77 "modified" => "DATETIME ON UPDATE NOW(0)",
78 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
79 ),
80 "keys" => array(
81 "primary" => array(
82 "" => array("username")
83 )
84 ),
85 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
86 ),
87 "fido2" => array(
88 "cols" => array(
89 "username" => "VARCHAR(255) NOT NULL",
90 "friendlyName" => "VARCHAR(255)",
91 "rpId" => "VARCHAR(255) NOT NULL",
92 "credentialPublicKey" => "TEXT NOT NULL",
93 "certificateChain" => "TEXT",
94 // Can be null for format "none"
95 "certificate" => "TEXT",
96 "certificateIssuer" => "VARCHAR(255)",
97 "certificateSubject" => "VARCHAR(255)",
98 "signatureCounter" => "INT",
99 "AAGUID" => "BLOB",
100 "credentialId" => "BLOB NOT NULL",
101 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
102 "modified" => "DATETIME ON UPDATE NOW(0)",
103 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
104 ),
105 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
106 ),
107 "_sogo_static_view" => array(
108 "cols" => array(
109 "c_uid" => "VARCHAR(255) NOT NULL",
110 "domain" => "VARCHAR(255) NOT NULL",
111 "c_name" => "VARCHAR(255) NOT NULL",
112 "c_password" => "VARCHAR(255) NOT NULL DEFAULT ''",
113 "c_cn" => "VARCHAR(255)",
114 "mail" => "VARCHAR(255) NOT NULL",
115 // TODO -> use TEXT and check if SOGo login breaks on empty aliases
116 "aliases" => "TEXT NOT NULL",
117 "ad_aliases" => "VARCHAR(6144) NOT NULL DEFAULT ''",
118 "ext_acl" => "VARCHAR(6144) NOT NULL DEFAULT ''",
119 "kind" => "VARCHAR(100) NOT NULL DEFAULT ''",
120 "multiple_bookings" => "INT NOT NULL DEFAULT -1"
121 ),
122 "keys" => array(
123 "primary" => array(
124 "" => array("c_uid")
125 ),
126 "key" => array(
127 "domain" => array("domain")
128 )
129 ),
130 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
131 ),
132 "relayhosts" => array(
133 "cols" => array(
134 "id" => "INT NOT NULL AUTO_INCREMENT",
135 "hostname" => "VARCHAR(255) NOT NULL",
136 "username" => "VARCHAR(255) NOT NULL",
137 "password" => "VARCHAR(255) NOT NULL",
138 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
139 ),
140 "keys" => array(
141 "primary" => array(
142 "" => array("id")
143 ),
144 "key" => array(
145 "hostname" => array("hostname")
146 )
147 ),
148 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
149 ),
150 "transports" => array(
151 "cols" => array(
152 "id" => "INT NOT NULL AUTO_INCREMENT",
153 "destination" => "VARCHAR(255) NOT NULL",
154 "nexthop" => "VARCHAR(255) NOT NULL",
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200155 "username" => "VARCHAR(255) NOT NULL DEFAULT ''",
156 "password" => "VARCHAR(255) NOT NULL DEFAULT ''",
157 "is_mx_based" => "TINYINT(1) NOT NULL DEFAULT '0'",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100158 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
159 ),
160 "keys" => array(
161 "primary" => array(
162 "" => array("id")
163 ),
164 "key" => array(
165 "destination" => array("destination"),
166 "nexthop" => array("nexthop"),
167 )
168 ),
169 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
170 ),
171 "alias" => array(
172 "cols" => array(
173 "id" => "INT NOT NULL AUTO_INCREMENT",
174 "address" => "VARCHAR(255) NOT NULL",
175 "goto" => "TEXT NOT NULL",
176 "domain" => "VARCHAR(255) NOT NULL",
177 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
178 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
179 "private_comment" => "TEXT",
180 "public_comment" => "TEXT",
181 "sogo_visible" => "TINYINT(1) NOT NULL DEFAULT '1'",
182 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
183 ),
184 "keys" => array(
185 "primary" => array(
186 "" => array("id")
187 ),
188 "unique" => array(
189 "address" => array("address")
190 ),
191 "key" => array(
192 "domain" => array("domain")
193 )
194 ),
195 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
196 ),
197 "api" => array(
198 "cols" => array(
199 "api_key" => "VARCHAR(255) NOT NULL",
200 "allow_from" => "VARCHAR(512) NOT NULL",
201 "skip_ip_check" => "TINYINT(1) NOT NULL DEFAULT '0'",
202 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
203 "modified" => "DATETIME ON UPDATE NOW(0)",
204 "access" => "ENUM('ro', 'rw') NOT NULL DEFAULT 'rw'",
205 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
206 ),
207 "keys" => array(
208 "primary" => array(
209 "" => array("api_key")
210 ),
211 ),
212 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
213 ),
214 "sender_acl" => array(
215 "cols" => array(
216 "id" => "INT NOT NULL AUTO_INCREMENT",
217 "logged_in_as" => "VARCHAR(255) NOT NULL",
218 "send_as" => "VARCHAR(255) NOT NULL",
219 "external" => "TINYINT(1) NOT NULL DEFAULT '0'"
220 ),
221 "keys" => array(
222 "primary" => array(
223 "" => array("id")
224 )
225 ),
226 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
227 ),
228 "domain" => array(
229 // Todo: Move some attributes to json
230 "cols" => array(
231 "domain" => "VARCHAR(255) NOT NULL",
232 "description" => "VARCHAR(255)",
233 "aliases" => "INT(10) NOT NULL DEFAULT '0'",
234 "mailboxes" => "INT(10) NOT NULL DEFAULT '0'",
235 "defquota" => "BIGINT(20) NOT NULL DEFAULT '3072'",
236 "maxquota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
237 "quota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
238 "relayhost" => "VARCHAR(255) NOT NULL DEFAULT '0'",
239 "backupmx" => "TINYINT(1) NOT NULL DEFAULT '0'",
240 "gal" => "TINYINT(1) NOT NULL DEFAULT '1'",
241 "relay_all_recipients" => "TINYINT(1) NOT NULL DEFAULT '0'",
242 "relay_unknown_only" => "TINYINT(1) NOT NULL DEFAULT '0'",
243 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
244 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
245 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
246 ),
247 "keys" => array(
248 "primary" => array(
249 "" => array("domain")
250 )
251 ),
252 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
253 ),
254 "tls_policy_override" => array(
255 "cols" => array(
256 "id" => "INT NOT NULL AUTO_INCREMENT",
257 "dest" => "VARCHAR(255) NOT NULL",
258 "policy" => "ENUM('none', 'may', 'encrypt', 'dane', 'dane-only', 'fingerprint', 'verify', 'secure') NOT NULL",
259 "parameters" => "VARCHAR(255) DEFAULT ''",
260 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
261 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
262 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
263 ),
264 "keys" => array(
265 "primary" => array(
266 "" => array("id")
267 ),
268 "unique" => array(
269 "dest" => array("dest")
270 ),
271 ),
272 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
273 ),
274 "quarantine" => array(
275 "cols" => array(
276 "id" => "INT NOT NULL AUTO_INCREMENT",
277 "qid" => "VARCHAR(30) NOT NULL",
278 "subject" => "VARCHAR(500)",
279 "score" => "FLOAT(8,2)",
280 "ip" => "VARCHAR(50)",
281 "action" => "CHAR(20) NOT NULL DEFAULT 'unknown'",
282 "symbols" => "JSON",
283 "fuzzy_hashes" => "JSON",
284 "sender" => "VARCHAR(255) NOT NULL DEFAULT 'unknown'",
285 "rcpt" => "VARCHAR(255)",
286 "msg" => "LONGTEXT",
287 "domain" => "VARCHAR(255)",
288 "notified" => "TINYINT(1) NOT NULL DEFAULT '0'",
289 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
290 "user" => "VARCHAR(255) NOT NULL DEFAULT 'unknown'",
291 ),
292 "keys" => array(
293 "primary" => array(
294 "" => array("id")
295 )
296 ),
297 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
298 ),
299 "mailbox" => array(
300 "cols" => array(
301 "username" => "VARCHAR(255) NOT NULL",
302 "password" => "VARCHAR(255) NOT NULL",
303 "name" => "VARCHAR(255)",
304 "description" => "VARCHAR(255)",
305 // mailbox_path_prefix is followed by domain/local_part/
306 "mailbox_path_prefix" => "VARCHAR(150) DEFAULT '/var/vmail/'",
307 "quota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
308 "local_part" => "VARCHAR(255) NOT NULL",
309 "domain" => "VARCHAR(255) NOT NULL",
310 "attributes" => "JSON",
311 "kind" => "VARCHAR(100) NOT NULL DEFAULT ''",
312 "multiple_bookings" => "INT NOT NULL DEFAULT -1",
313 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
314 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
315 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
316 ),
317 "keys" => array(
318 "primary" => array(
319 "" => array("username")
320 ),
321 "key" => array(
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200322 "domain" => array("domain"),
323 "kind" => array("kind")
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100324 )
325 ),
326 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
327 ),
328 "sieve_filters" => array(
329 "cols" => array(
330 "id" => "INT NOT NULL AUTO_INCREMENT",
331 "username" => "VARCHAR(255) NOT NULL",
332 "script_desc" => "VARCHAR(255) NOT NULL",
333 "script_name" => "ENUM('active','inactive')",
334 "script_data" => "TEXT NOT NULL",
335 "filter_type" => "ENUM('postfilter','prefilter')",
336 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
337 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP"
338 ),
339 "keys" => array(
340 "primary" => array(
341 "" => array("id")
342 ),
343 "key" => array(
344 "username" => array("username"),
345 "script_desc" => array("script_desc")
346 ),
347 "fkey" => array(
348 "fk_username_sieve_global_before" => array(
349 "col" => "username",
350 "ref" => "mailbox.username",
351 "delete" => "CASCADE",
352 "update" => "NO ACTION"
353 )
354 )
355 ),
356 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
357 ),
358 "app_passwd" => array(
359 "cols" => array(
360 "id" => "INT NOT NULL AUTO_INCREMENT",
361 "name" => "VARCHAR(255) NOT NULL",
362 "mailbox" => "VARCHAR(255) NOT NULL",
363 "domain" => "VARCHAR(255) NOT NULL",
364 "password" => "VARCHAR(255) NOT NULL",
365 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
366 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +0100367 "imap_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
368 "smtp_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
369 "dav_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
370 "eas_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
371 "pop3_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
372 "sieve_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100373 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
374 ),
375 "keys" => array(
376 "primary" => array(
377 "" => array("id")
378 ),
379 "key" => array(
380 "mailbox" => array("mailbox"),
381 "password" => array("password"),
382 "domain" => array("domain"),
383 ),
384 "fkey" => array(
385 "fk_username_app_passwd" => array(
386 "col" => "mailbox",
387 "ref" => "mailbox.username",
388 "delete" => "CASCADE",
389 "update" => "NO ACTION"
390 )
391 )
392 ),
393 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
394 ),
395 "user_acl" => array(
396 "cols" => array(
397 "username" => "VARCHAR(255) NOT NULL",
398 "spam_alias" => "TINYINT(1) NOT NULL DEFAULT '1'",
399 "tls_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
400 "spam_score" => "TINYINT(1) NOT NULL DEFAULT '1'",
401 "spam_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
402 "delimiter_action" => "TINYINT(1) NOT NULL DEFAULT '1'",
403 "syncjobs" => "TINYINT(1) NOT NULL DEFAULT '1'",
404 "eas_reset" => "TINYINT(1) NOT NULL DEFAULT '1'",
405 "sogo_profile_reset" => "TINYINT(1) NOT NULL DEFAULT '0'",
406 "pushover" => "TINYINT(1) NOT NULL DEFAULT '1'",
407 // quarantine is for quarantine actions, todo: rename
408 "quarantine" => "TINYINT(1) NOT NULL DEFAULT '1'",
409 "quarantine_attachments" => "TINYINT(1) NOT NULL DEFAULT '1'",
410 "quarantine_notification" => "TINYINT(1) NOT NULL DEFAULT '1'",
411 "quarantine_category" => "TINYINT(1) NOT NULL DEFAULT '1'",
412 "app_passwds" => "TINYINT(1) NOT NULL DEFAULT '1'",
413 ),
414 "keys" => array(
415 "primary" => array(
416 "" => array("username")
417 ),
418 "fkey" => array(
419 "fk_username" => array(
420 "col" => "username",
421 "ref" => "mailbox.username",
422 "delete" => "CASCADE",
423 "update" => "NO ACTION"
424 )
425 )
426 ),
427 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
428 ),
429 "alias_domain" => array(
430 "cols" => array(
431 "alias_domain" => "VARCHAR(255) NOT NULL",
432 "target_domain" => "VARCHAR(255) NOT NULL",
433 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
434 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
435 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
436 ),
437 "keys" => array(
438 "primary" => array(
439 "" => array("alias_domain")
440 ),
441 "key" => array(
442 "active" => array("active"),
443 "target_domain" => array("target_domain")
444 )
445 ),
446 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
447 ),
448 "spamalias" => array(
449 "cols" => array(
450 "address" => "VARCHAR(255) NOT NULL",
451 "goto" => "TEXT NOT NULL",
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200452 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
453 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
454 "validity" => "INT(11)"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100455 ),
456 "keys" => array(
457 "primary" => array(
458 "" => array("address")
459 ),
460 ),
461 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
462 ),
463 "filterconf" => array(
464 "cols" => array(
465 "object" => "VARCHAR(255) NOT NULL DEFAULT ''",
466 "option" => "VARCHAR(50) NOT NULL DEFAULT ''",
467 "value" => "VARCHAR(100) NOT NULL DEFAULT ''",
468 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
469 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
470 "prefid" => "INT(11) NOT NULL AUTO_INCREMENT"
471 ),
472 "keys" => array(
473 "primary" => array(
474 "" => array("prefid")
475 ),
476 "key" => array(
477 "object" => array("object")
478 )
479 ),
480 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
481 ),
482 "settingsmap" => array(
483 "cols" => array(
484 "id" => "INT NOT NULL AUTO_INCREMENT",
485 "desc" => "VARCHAR(255) NOT NULL",
486 "content" => "LONGTEXT NOT NULL",
487 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
488 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
489 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
490 ),
491 "keys" => array(
492 "primary" => array(
493 "" => array("id")
494 )
495 ),
496 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
497 ),
498 "logs" => array(
499 "cols" => array(
500 "id" => "INT NOT NULL AUTO_INCREMENT",
501 "task" => "CHAR(32) NOT NULL DEFAULT '000000'",
502 "type" => "VARCHAR(32) DEFAULT ''",
503 "msg" => "TEXT",
504 "call" => "TEXT",
505 "user" => "VARCHAR(64) NOT NULL",
506 "role" => "VARCHAR(32) NOT NULL",
507 "remote" => "VARCHAR(39) NOT NULL",
508 "time" => "INT(11) NOT NULL"
509 ),
510 "keys" => array(
511 "primary" => array(
512 "" => array("id")
513 )
514 ),
515 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
516 ),
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200517 "sasl_log" => array(
518 "cols" => array(
519 "service" => "VARCHAR(32) NOT NULL DEFAULT ''",
520 "app_password" => "INT",
521 "username" => "VARCHAR(255) NOT NULL",
522 "real_rip" => "VARCHAR(64) NOT NULL",
523 "datetime" => "DATETIME(0) NOT NULL DEFAULT NOW(0)"
524 ),
525 "keys" => array(
526 "primary" => array(
527 "" => array("service", "real_rip", "username")
528 ),
529 "key" => array(
530 "username" => array("username"),
531 "service" => array("service"),
532 "datetime" => array("datetime"),
533 "real_rip" => array("real_rip")
534 )
535 ),
536 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
537 ),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100538 "quota2" => array(
539 "cols" => array(
540 "username" => "VARCHAR(255) NOT NULL",
541 "bytes" => "BIGINT(20) NOT NULL DEFAULT '0'",
542 "messages" => "BIGINT(20) NOT NULL DEFAULT '0'"
543 ),
544 "keys" => array(
545 "primary" => array(
546 "" => array("username")
547 )
548 ),
549 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
550 ),
551 "quota2replica" => array(
552 "cols" => array(
553 "username" => "VARCHAR(255) NOT NULL",
554 "bytes" => "BIGINT(20) NOT NULL DEFAULT '0'",
555 "messages" => "BIGINT(20) NOT NULL DEFAULT '0'"
556 ),
557 "keys" => array(
558 "primary" => array(
559 "" => array("username")
560 )
561 ),
562 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
563 ),
564 "domain_admins" => array(
565 "cols" => array(
566 "id" => "INT NOT NULL AUTO_INCREMENT",
567 "username" => "VARCHAR(255) NOT NULL",
568 "domain" => "VARCHAR(255) NOT NULL",
569 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
570 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
571 ),
572 "keys" => array(
573 "primary" => array(
574 "" => array("id")
575 ),
576 "key" => array(
577 "username" => array("username")
578 )
579 ),
580 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
581 ),
582 "da_acl" => array(
583 "cols" => array(
584 "username" => "VARCHAR(255) NOT NULL",
585 "syncjobs" => "TINYINT(1) NOT NULL DEFAULT '1'",
586 "quarantine" => "TINYINT(1) NOT NULL DEFAULT '1'",
587 "login_as" => "TINYINT(1) NOT NULL DEFAULT '1'",
588 "sogo_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
589 "app_passwds" => "TINYINT(1) NOT NULL DEFAULT '1'",
590 "bcc_maps" => "TINYINT(1) NOT NULL DEFAULT '1'",
591 "pushover" => "TINYINT(1) NOT NULL DEFAULT '0'",
592 "filters" => "TINYINT(1) NOT NULL DEFAULT '1'",
593 "ratelimit" => "TINYINT(1) NOT NULL DEFAULT '1'",
594 "spam_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
595 "extend_sender_acl" => "TINYINT(1) NOT NULL DEFAULT '0'",
596 "unlimited_quota" => "TINYINT(1) NOT NULL DEFAULT '0'",
597 "protocol_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
598 "smtp_ip_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
599 "alias_domains" => "TINYINT(1) NOT NULL DEFAULT '0'",
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200600 "mailbox_relayhost" => "TINYINT(1) NOT NULL DEFAULT '1'",
601 "domain_relayhost" => "TINYINT(1) NOT NULL DEFAULT '1'",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100602 "domain_desc" => "TINYINT(1) NOT NULL DEFAULT '0'"
603 ),
604 "keys" => array(
605 "primary" => array(
606 "" => array("username")
607 )
608 ),
609 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
610 ),
611 "imapsync" => array(
612 "cols" => array(
613 "id" => "INT NOT NULL AUTO_INCREMENT",
614 "user2" => "VARCHAR(255) NOT NULL",
615 "host1" => "VARCHAR(255) NOT NULL",
616 "authmech1" => "ENUM('PLAIN','LOGIN','CRAM-MD5') DEFAULT 'PLAIN'",
617 "regextrans2" => "VARCHAR(255) DEFAULT ''",
618 "authmd51" => "TINYINT(1) NOT NULL DEFAULT 0",
619 "domain2" => "VARCHAR(255) NOT NULL DEFAULT ''",
620 "subfolder2" => "VARCHAR(255) NOT NULL DEFAULT ''",
621 "user1" => "VARCHAR(255) NOT NULL",
622 "password1" => "VARCHAR(255) NOT NULL",
623 "exclude" => "VARCHAR(500) NOT NULL DEFAULT ''",
624 "maxage" => "SMALLINT NOT NULL DEFAULT '0'",
625 "mins_interval" => "SMALLINT UNSIGNED NOT NULL DEFAULT '0'",
626 "maxbytespersecond" => "VARCHAR(50) NOT NULL DEFAULT '0'",
627 "port1" => "SMALLINT UNSIGNED NOT NULL",
628 "enc1" => "ENUM('TLS','SSL','PLAIN') DEFAULT 'TLS'",
629 "delete2duplicates" => "TINYINT(1) NOT NULL DEFAULT '1'",
630 "delete1" => "TINYINT(1) NOT NULL DEFAULT '0'",
631 "delete2" => "TINYINT(1) NOT NULL DEFAULT '0'",
632 "automap" => "TINYINT(1) NOT NULL DEFAULT '0'",
633 "skipcrossduplicates" => "TINYINT(1) NOT NULL DEFAULT '0'",
634 "custom_params" => "VARCHAR(512) NOT NULL DEFAULT ''",
635 "timeout1" => "SMALLINT NOT NULL DEFAULT '600'",
636 "timeout2" => "SMALLINT NOT NULL DEFAULT '600'",
637 "subscribeall" => "TINYINT(1) NOT NULL DEFAULT '1'",
638 "is_running" => "TINYINT(1) NOT NULL DEFAULT '0'",
639 "returned_text" => "LONGTEXT",
640 "last_run" => "TIMESTAMP NULL DEFAULT NULL",
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +0100641 "success" => "TINYINT(1) UNSIGNED DEFAULT NULL",
642 "exit_status" => "VARCHAR(50) DEFAULT NULL",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100643 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
644 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
645 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
646 ),
647 "keys" => array(
648 "primary" => array(
649 "" => array("id")
650 )
651 ),
652 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
653 ),
654 "bcc_maps" => array(
655 "cols" => array(
656 "id" => "INT NOT NULL AUTO_INCREMENT",
657 "local_dest" => "VARCHAR(255) NOT NULL",
658 "bcc_dest" => "VARCHAR(255) NOT NULL",
659 "domain" => "VARCHAR(255) NOT NULL",
660 "type" => "ENUM('sender','rcpt')",
661 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
662 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
663 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
664 ),
665 "keys" => array(
666 "primary" => array(
667 "" => array("id")
668 ),
669 "key" => array(
670 "local_dest" => array("local_dest"),
671 )
672 ),
673 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
674 ),
675 "recipient_maps" => array(
676 "cols" => array(
677 "id" => "INT NOT NULL AUTO_INCREMENT",
678 "old_dest" => "VARCHAR(255) NOT NULL",
679 "new_dest" => "VARCHAR(255) NOT NULL",
680 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
681 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
682 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
683 ),
684 "keys" => array(
685 "primary" => array(
686 "" => array("id")
687 ),
688 "key" => array(
689 "local_dest" => array("old_dest"),
690 )
691 ),
692 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
693 ),
694 "tfa" => array(
695 "cols" => array(
696 "id" => "INT NOT NULL AUTO_INCREMENT",
697 "key_id" => "VARCHAR(255) NOT NULL",
698 "username" => "VARCHAR(255) NOT NULL",
699 "authmech" => "ENUM('yubi_otp', 'u2f', 'hotp', 'totp')",
700 "secret" => "VARCHAR(255) DEFAULT NULL",
701 "keyHandle" => "VARCHAR(255) DEFAULT NULL",
702 "publicKey" => "VARCHAR(255) DEFAULT NULL",
703 "counter" => "INT NOT NULL DEFAULT '0'",
704 "certificate" => "TEXT",
705 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
706 ),
707 "keys" => array(
708 "primary" => array(
709 "" => array("id")
710 )
711 ),
712 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
713 ),
714 "forwarding_hosts" => array(
715 "cols" => array(
716 "host" => "VARCHAR(255) NOT NULL",
717 "source" => "VARCHAR(255) NOT NULL",
718 "filter_spam" => "TINYINT(1) NOT NULL DEFAULT '0'"
719 ),
720 "keys" => array(
721 "primary" => array(
722 "" => array("host")
723 ),
724 ),
725 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
726 ),
727 "sogo_acl" => array(
728 "cols" => array(
729 "id" => "INT NOT NULL AUTO_INCREMENT",
730 "c_folder_id" => "INT NOT NULL",
731 "c_object" => "VARCHAR(255) NOT NULL",
732 "c_uid" => "VARCHAR(255) NOT NULL",
733 "c_role" => "VARCHAR(80) NOT NULL"
734 ),
735 "keys" => array(
736 "primary" => array(
737 "" => array("id")
738 ),
739 "key" => array(
740 "sogo_acl_c_folder_id_idx" => array("c_folder_id"),
741 "sogo_acl_c_uid_idx" => array("c_uid")
742 )
743 ),
744 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
745 ),
746 "sogo_alarms_folder" => array(
747 "cols" => array(
748 "id" => "INT NOT NULL AUTO_INCREMENT",
749 "c_path" => "VARCHAR(255) NOT NULL",
750 "c_name" => "VARCHAR(255) NOT NULL",
751 "c_uid" => "VARCHAR(255) NOT NULL",
752 "c_recurrence_id" => "INT(11) DEFAULT NULL",
753 "c_alarm_number" => "INT(11) NOT NULL",
754 "c_alarm_date" => "INT(11) NOT NULL"
755 ),
756 "keys" => array(
757 "primary" => array(
758 "" => array("id")
759 )
760 ),
761 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
762 ),
763 "sogo_cache_folder" => array(
764 "cols" => array(
765 "c_uid" => "VARCHAR(255) NOT NULL",
766 "c_path" => "VARCHAR(255) NOT NULL",
767 "c_parent_path" => "VARCHAR(255) DEFAULT NULL",
768 "c_type" => "TINYINT(3) unsigned NOT NULL",
769 "c_creationdate" => "INT(11) NOT NULL",
770 "c_lastmodified" => "INT(11) NOT NULL",
771 "c_version" => "INT(11) NOT NULL DEFAULT '0'",
772 "c_deleted" => "TINYINT(4) NOT NULL DEFAULT '0'",
773 "c_content" => "LONGTEXT"
774 ),
775 "keys" => array(
776 "primary" => array(
777 "" => array("c_uid", "c_path")
778 ),
779 ),
780 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
781 ),
782 "sogo_folder_info" => array(
783 "cols" => array(
784 "c_folder_id" => "BIGINT(20) unsigned NOT NULL AUTO_INCREMENT",
785 "c_path" => "VARCHAR(255) NOT NULL",
786 "c_path1" => "VARCHAR(255) NOT NULL",
787 "c_path2" => "VARCHAR(255) DEFAULT NULL",
788 "c_path3" => "VARCHAR(255) DEFAULT NULL",
789 "c_path4" => "VARCHAR(255) DEFAULT NULL",
790 "c_foldername" => "VARCHAR(255) NOT NULL",
791 "c_location" => "VARCHAR(2048) DEFAULT NULL",
792 "c_quick_location" => "VARCHAR(2048) DEFAULT NULL",
793 "c_acl_location" => "VARCHAR(2048) DEFAULT NULL",
794 "c_folder_type" => "VARCHAR(255) NOT NULL"
795 ),
796 "keys" => array(
797 "primary" => array(
798 "" => array("c_path")
799 ),
800 "unique" => array(
801 "c_folder_id" => array("c_folder_id")
802 )
803 ),
804 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
805 ),
806 "sogo_quick_appointment" => array(
807 "cols" => array(
808 "c_folder_id" => "INT NOT NULL",
809 "c_name" => "VARCHAR(255) NOT NULL",
810 "c_uid" => "VARCHAR(1000) NOT NULL",
811 "c_startdate" => "INT",
812 "c_enddate" => "INT",
813 "c_cycleenddate" => "INT",
814 "c_title" => "VARCHAR(1000) NOT NULL",
815 "c_participants" => "TEXT",
816 "c_isallday" => "INT",
817 "c_iscycle" => "INT",
818 "c_cycleinfo" => "TEXT",
819 "c_classification" => "INT NOT NULL",
820 "c_isopaque" => "INT NOT NULL",
821 "c_status" => "INT NOT NULL",
822 "c_priority" => "INT",
823 "c_location" => "VARCHAR(255)",
824 "c_orgmail" => "VARCHAR(255)",
825 "c_partmails" => "TEXT",
826 "c_partstates" => "TEXT",
827 "c_category" => "VARCHAR(255)",
828 "c_sequence" => "INT",
829 "c_component" => "VARCHAR(10) NOT NULL",
830 "c_nextalarm" => "INT",
831 "c_description" => "TEXT"
832 ),
833 "keys" => array(
834 "primary" => array(
835 "" => array("c_folder_id", "c_name")
836 )
837 ),
838 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
839 ),
840 "sogo_quick_contact" => array(
841 "cols" => array(
842 "c_folder_id" => "INT NOT NULL",
843 "c_name" => "VARCHAR(255) NOT NULL",
844 "c_givenname" => "VARCHAR(255)",
845 "c_cn" => "VARCHAR(255)",
846 "c_sn" => "VARCHAR(255)",
847 "c_screenname" => "VARCHAR(255)",
848 "c_l" => "VARCHAR(255)",
849 "c_mail" => "TEXT",
850 "c_o" => "VARCHAR(500)",
851 "c_ou" => "VARCHAR(255)",
852 "c_telephonenumber" => "VARCHAR(255)",
853 "c_categories" => "VARCHAR(255)",
854 "c_component" => "VARCHAR(10) NOT NULL",
855 "c_hascertificate" => "INT4 DEFAULT 0"
856 ),
857 "keys" => array(
858 "primary" => array(
859 "" => array("c_folder_id", "c_name")
860 )
861 ),
862 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
863 ),
864 "sogo_sessions_folder" => array(
865 "cols" => array(
866 "c_id" => "VARCHAR(255) NOT NULL",
867 "c_value" => "VARCHAR(255) NOT NULL",
868 "c_creationdate" => "INT(11) NOT NULL",
869 "c_lastseen" => "INT(11) NOT NULL"
870 ),
871 "keys" => array(
872 "primary" => array(
873 "" => array("c_id")
874 )
875 ),
876 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
877 ),
878 "sogo_store" => array(
879 "cols" => array(
880 "c_folder_id" => "INT NOT NULL",
881 "c_name" => "VARCHAR(255) NOT NULL",
882 "c_content" => "MEDIUMTEXT NOT NULL",
883 "c_creationdate" => "INT NOT NULL",
884 "c_lastmodified" => "INT NOT NULL",
885 "c_version" => "INT NOT NULL",
886 "c_deleted" => "INT"
887 ),
888 "keys" => array(
889 "primary" => array(
890 "" => array("c_folder_id", "c_name")
891 )
892 ),
893 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
894 ),
895 "pushover" => array(
896 "cols" => array(
897 "username" => "VARCHAR(255) NOT NULL",
898 "key" => "VARCHAR(255) NOT NULL",
899 "token" => "VARCHAR(255) NOT NULL",
900 "attributes" => "JSON",
901 "title" => "TEXT",
902 "text" => "TEXT",
903 "senders" => "TEXT",
904 "senders_regex" => "TEXT",
905 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
906 ),
907 "keys" => array(
908 "primary" => array(
909 "" => array("username")
910 )
911 ),
912 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
913 ),
914 "sogo_user_profile" => array(
915 "cols" => array(
916 "c_uid" => "VARCHAR(255) NOT NULL",
917 "c_defaults" => "LONGTEXT",
918 "c_settings" => "LONGTEXT"
919 ),
920 "keys" => array(
921 "primary" => array(
922 "" => array("c_uid")
923 )
924 ),
925 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
926 ),
927 "oauth_clients" => array(
928 "cols" => array(
929 "id" => "INT NOT NULL AUTO_INCREMENT",
930 "client_id" => "VARCHAR(80) NOT NULL",
931 "client_secret" => "VARCHAR(80)",
932 "redirect_uri" => "VARCHAR(2000)",
933 "grant_types" => "VARCHAR(80)",
934 "scope" => "VARCHAR(4000)",
935 "user_id" => "VARCHAR(80)"
936 ),
937 "keys" => array(
938 "primary" => array(
939 "" => array("client_id")
940 ),
941 "unique" => array(
942 "id" => array("id")
943 )
944 ),
945 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
946 ),
947 "oauth_access_tokens" => array(
948 "cols" => array(
949 "access_token" => "VARCHAR(40) NOT NULL",
950 "client_id" => "VARCHAR(80) NOT NULL",
951 "user_id" => "VARCHAR(80)",
952 "expires" => "TIMESTAMP NOT NULL",
953 "scope" => "VARCHAR(4000)"
954 ),
955 "keys" => array(
956 "primary" => array(
957 "" => array("access_token")
958 )
959 ),
960 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
961 ),
962 "oauth_authorization_codes" => array(
963 "cols" => array(
964 "authorization_code" => "VARCHAR(40) NOT NULL",
965 "client_id" => "VARCHAR(80) NOT NULL",
966 "user_id" => "VARCHAR(80)",
967 "redirect_uri" => "VARCHAR(2000)",
968 "expires" => "TIMESTAMP NOT NULL",
969 "scope" => "VARCHAR(4000)",
970 "id_token" => "VARCHAR(1000)"
971 ),
972 "keys" => array(
973 "primary" => array(
974 "" => array("authorization_code")
975 )
976 ),
977 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
978 ),
979 "oauth_refresh_tokens" => array(
980 "cols" => array(
981 "refresh_token" => "VARCHAR(40) NOT NULL",
982 "client_id" => "VARCHAR(80) NOT NULL",
983 "user_id" => "VARCHAR(80)",
984 "expires" => "TIMESTAMP NOT NULL",
985 "scope" => "VARCHAR(4000)"
986 ),
987 "keys" => array(
988 "primary" => array(
989 "" => array("refresh_token")
990 )
991 ),
992 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
993 )
994 );
995
996 foreach ($tables as $table => $properties) {
997 // Migrate to quarantine
998 if ($table == 'quarantine') {
999 $stmt = $pdo->query("SHOW TABLES LIKE 'quarantaine'");
1000 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1001 if ($num_results != 0) {
1002 $stmt = $pdo->query("SHOW TABLES LIKE 'quarantine'");
1003 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1004 if ($num_results == 0) {
1005 $pdo->query("RENAME TABLE `quarantaine` TO `quarantine`");
1006 }
1007 }
1008 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001009
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001010 // Migrate tls_enforce_* options
1011 if ($table == 'mailbox') {
1012 $stmt = $pdo->query("SHOW TABLES LIKE 'mailbox'");
1013 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1014 if ($num_results != 0) {
1015 $stmt = $pdo->query("SHOW COLUMNS FROM `mailbox` LIKE '%tls_enforce%'");
1016 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1017 if ($num_results != 0) {
1018 $stmt = $pdo->query("SELECT `username`, `tls_enforce_in`, `tls_enforce_out` FROM `mailbox`");
1019 $tls_options_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
1020 while ($row = array_shift($tls_options_rows)) {
1021 $tls_options[$row['username']] = array('tls_enforce_in' => $row['tls_enforce_in'], 'tls_enforce_out' => $row['tls_enforce_out']);
1022 }
1023 }
1024 }
1025 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001026
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001027 $stmt = $pdo->query("SHOW TABLES LIKE '" . $table . "'");
1028 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1029 if ($num_results != 0) {
1030 $stmt = $pdo->prepare("SELECT CONCAT('ALTER TABLE ', `table_schema`, '.', `table_name`, ' DROP FOREIGN KEY ', `constraint_name`, ';') AS `FKEY_DROP` FROM `information_schema`.`table_constraints`
1031 WHERE `constraint_type` = 'FOREIGN KEY' AND `table_name` = :table;");
1032 $stmt->execute(array(':table' => $table));
1033 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
1034 while ($row = array_shift($rows)) {
1035 $pdo->query($row['FKEY_DROP']);
1036 }
1037 foreach($properties['cols'] as $column => $type) {
1038 $stmt = $pdo->query("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'");
1039 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1040 if ($num_results == 0) {
1041 if (strpos($type, 'AUTO_INCREMENT') !== false) {
1042 $type = $type . ' PRIMARY KEY ';
1043 // Adding an AUTO_INCREMENT key, need to drop primary keys first, if exists
1044 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1045 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1046 if ($num_results != 0) {
1047 $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY");
1048 }
1049 }
1050 $pdo->query("ALTER TABLE `" . $table . "` ADD `" . $column . "` " . $type);
1051 }
1052 else {
1053 $pdo->query("ALTER TABLE `" . $table . "` MODIFY COLUMN `" . $column . "` " . $type);
1054 }
1055 }
1056 foreach($properties['keys'] as $key_type => $key_content) {
1057 if (strtolower($key_type) == 'primary') {
1058 foreach ($key_content as $key_values) {
1059 $fields = "`" . implode("`, `", $key_values) . "`";
1060 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1061 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1062 $is_drop = ($num_results != 0) ? "DROP PRIMARY KEY, " : "";
1063 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD PRIMARY KEY (" . $fields . ")");
1064 }
1065 }
1066 if (strtolower($key_type) == 'key') {
1067 foreach ($key_content as $key_name => $key_values) {
1068 $fields = "`" . implode("`, `", $key_values) . "`";
1069 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1070 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1071 $is_drop = ($num_results != 0) ? "DROP INDEX `" . $key_name . "`, " : "";
1072 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD KEY `" . $key_name . "` (" . $fields . ")");
1073 }
1074 }
1075 if (strtolower($key_type) == 'unique') {
1076 foreach ($key_content as $key_name => $key_values) {
1077 $fields = "`" . implode("`, `", $key_values) . "`";
1078 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1079 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1080 $is_drop = ($num_results != 0) ? "DROP INDEX `" . $key_name . "`, " : "";
1081 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD UNIQUE KEY `" . $key_name . "` (" . $fields . ")");
1082 }
1083 }
1084 if (strtolower($key_type) == 'fkey') {
1085 foreach ($key_content as $key_name => $key_values) {
1086 $fields = "`" . implode("`, `", $key_values) . "`";
1087 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1088 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1089 if ($num_results != 0) {
1090 $pdo->query("ALTER TABLE `" . $table . "` DROP INDEX `" . $key_name . "`");
1091 }
1092 @list($table_ref, $field_ref) = explode('.', $key_values['ref']);
1093 $pdo->query("ALTER TABLE `" . $table . "` ADD FOREIGN KEY `" . $key_name . "` (" . $key_values['col'] . ") REFERENCES `" . $table_ref . "` (`" . $field_ref . "`)
1094 ON DELETE " . $key_values['delete'] . " ON UPDATE " . $key_values['update']);
1095 }
1096 }
1097 }
1098 // Drop all vanished columns
1099 $stmt = $pdo->query("SHOW COLUMNS FROM `" . $table . "`");
1100 $cols_in_table = $stmt->fetchAll(PDO::FETCH_ASSOC);
1101 while ($row = array_shift($cols_in_table)) {
1102 if (!array_key_exists($row['Field'], $properties['cols'])) {
1103 $pdo->query("ALTER TABLE `" . $table . "` DROP COLUMN `" . $row['Field'] . "`;");
1104 }
1105 }
1106
1107 // Step 1: Get all non-primary keys, that currently exist and those that should exist
1108 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE `Key_name` != 'PRIMARY'");
1109 $keys_in_table = $stmt->fetchAll(PDO::FETCH_ASSOC);
1110 $keys_to_exist = array();
1111 if (isset($properties['keys']['unique']) && is_array($properties['keys']['unique'])) {
1112 foreach ($properties['keys']['unique'] as $key_name => $key_values) {
1113 $keys_to_exist[] = $key_name;
1114 }
1115 }
1116 if (isset($properties['keys']['key']) && is_array($properties['keys']['key'])) {
1117 foreach ($properties['keys']['key'] as $key_name => $key_values) {
1118 $keys_to_exist[] = $key_name;
1119 }
1120 }
1121 // Index for foreign key must exist
1122 if (isset($properties['keys']['fkey']) && is_array($properties['keys']['fkey'])) {
1123 foreach ($properties['keys']['fkey'] as $key_name => $key_values) {
1124 $keys_to_exist[] = $key_name;
1125 }
1126 }
1127 // Step 2: Drop all vanished indexes
1128 while ($row = array_shift($keys_in_table)) {
1129 if (!in_array($row['Key_name'], $keys_to_exist)) {
1130 $pdo->query("ALTER TABLE `" . $table . "` DROP INDEX `" . $row['Key_name'] . "`");
1131 }
1132 }
1133 // Step 3: Drop all vanished primary keys
1134 if (!isset($properties['keys']['primary'])) {
1135 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1136 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1137 if ($num_results != 0) {
1138 $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY");
1139 }
1140 }
1141 }
1142 else {
1143 // Create table if it is missing
1144 $sql = "CREATE TABLE IF NOT EXISTS `" . $table . "` (";
1145 foreach($properties['cols'] as $column => $type) {
1146 $sql .= "`" . $column . "` " . $type . ",";
1147 }
1148 foreach($properties['keys'] as $key_type => $key_content) {
1149 if (strtolower($key_type) == 'primary') {
1150 foreach ($key_content as $key_values) {
1151 $fields = "`" . implode("`, `", $key_values) . "`";
1152 $sql .= "PRIMARY KEY (" . $fields . ")" . ",";
1153 }
1154 }
1155 elseif (strtolower($key_type) == 'key') {
1156 foreach ($key_content as $key_name => $key_values) {
1157 $fields = "`" . implode("`, `", $key_values) . "`";
1158 $sql .= "KEY `" . $key_name . "` (" . $fields . ")" . ",";
1159 }
1160 }
1161 elseif (strtolower($key_type) == 'unique') {
1162 foreach ($key_content as $key_name => $key_values) {
1163 $fields = "`" . implode("`, `", $key_values) . "`";
1164 $sql .= "UNIQUE KEY `" . $key_name . "` (" . $fields . ")" . ",";
1165 }
1166 }
1167 elseif (strtolower($key_type) == 'fkey') {
1168 foreach ($key_content as $key_name => $key_values) {
1169 @list($table_ref, $field_ref) = explode('.', $key_values['ref']);
1170 $sql .= "FOREIGN KEY `" . $key_name . "` (" . $key_values['col'] . ") REFERENCES `" . $table_ref . "` (`" . $field_ref . "`)
1171 ON DELETE " . $key_values['delete'] . " ON UPDATE " . $key_values['update'] . ",";
1172 }
1173 }
1174 }
1175 $sql = rtrim($sql, ",");
1176 $sql .= ") " . $properties['attr'];
1177 $pdo->query($sql);
1178 }
1179 // Reset table attributes
1180 $pdo->query("ALTER TABLE `" . $table . "` " . $properties['attr'] . ";");
1181
1182 }
1183
1184 // Recreate SQL views
1185 foreach ($views as $view => $create) {
1186 $pdo->query("DROP VIEW IF EXISTS `" . $view . "`;");
1187 $pdo->query($create);
1188 }
1189
1190 // Mitigate imapsync pipemess issue
1191 $pdo->query("UPDATE `imapsync` SET `custom_params` = '' WHERE `custom_params` LIKE '%pipemess%';");
1192
1193 // Inject admin if not exists
1194 $stmt = $pdo->query("SELECT NULL FROM `admin`");
1195 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1196 if ($num_results == 0) {
1197 $pdo->query("INSERT INTO `admin` (`username`, `password`, `superadmin`, `created`, `modified`, `active`)
1198 VALUES ('admin', '{SSHA256}K8eVJ6YsZbQCfuJvSUbaQRLr0HPLz5rC9IAp0PAFl0tmNDBkMDc0NDAyOTAxN2Rk', 1, NOW(), NOW(), 1)");
1199 $pdo->query("INSERT INTO `domain_admins` (`username`, `domain`, `created`, `active`)
1200 SELECT `username`, 'ALL', NOW(), 1 FROM `admin`
1201 WHERE superadmin='1' AND `username` NOT IN (SELECT `username` FROM `domain_admins`);");
1202 $pdo->query("DELETE FROM `admin` WHERE `username` NOT IN (SELECT `username` FROM `domain_admins`);");
1203 }
1204 // Insert new DB schema version
1205 $pdo->query("REPLACE INTO `versions` (`application`, `version`) VALUES ('db_schema', '" . $db_version . "');");
1206
1207 // Fix dangling domain admins
1208 $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
1209 $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
1210
1211 // Migrate attributes
1212 // pushover
1213 $pdo->query("UPDATE `pushover` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
1214 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.evaluate_x_prio', \"0\") WHERE JSON_VALUE(`attributes`, '$.evaluate_x_prio') IS NULL;");
1215 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.only_x_prio', \"0\") WHERE JSON_VALUE(`attributes`, '$.only_x_prio') IS NULL;");
1216 // mailbox
1217 $pdo->query("UPDATE `mailbox` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001218 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.passwd_update', \"0\") WHERE JSON_VALUE(`attributes`, '$.passwd_update') IS NULL;");
1219 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.relayhost', \"0\") WHERE JSON_VALUE(`attributes`, '$.relayhost') IS NULL;");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001220 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.force_pw_update', \"0\") WHERE JSON_VALUE(`attributes`, '$.force_pw_update') IS NULL;");
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001221 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.sieve_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.sieve_access') IS NULL;");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001222 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.sogo_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.sogo_access') IS NULL;");
1223 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.imap_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.imap_access') IS NULL;");
1224 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.pop3_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.pop3_access') IS NULL;");
1225 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.smtp_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.smtp_access') IS NULL;");
1226 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.mailbox_format', \"maildir:\") WHERE JSON_VALUE(`attributes`, '$.mailbox_format') IS NULL;");
1227 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', \"never\") WHERE JSON_VALUE(`attributes`, '$.quarantine_notification') IS NULL;");
1228 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', \"reject\") WHERE JSON_VALUE(`attributes`, '$.quarantine_category') IS NULL;");
1229 foreach($tls_options as $tls_user => $tls_options) {
1230 $stmt = $pdo->prepare("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_enforce_in),
1231 `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_enforce_out)
1232 WHERE `username` = :username");
1233 $stmt->execute(array(':tls_enforce_in' => $tls_options['tls_enforce_in'], ':tls_enforce_out' => $tls_options['tls_enforce_out'], ':username' => $tls_user));
1234 }
1235 // Set tls_enforce_* if still missing (due to deleted attrs for example)
1236 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', \"1\") WHERE JSON_VALUE(`attributes`, '$.tls_enforce_out') IS NULL;");
1237 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', \"1\") WHERE JSON_VALUE(`attributes`, '$.tls_enforce_in') IS NULL;");
1238 // Fix ACL
1239 $pdo->query("INSERT INTO `user_acl` (`username`) SELECT `username` FROM `mailbox` WHERE `kind` = '' AND NOT EXISTS (SELECT `username` FROM `user_acl`);");
1240 $pdo->query("INSERT INTO `da_acl` (`username`) SELECT DISTINCT `username` FROM `domain_admins` WHERE `username` != 'admin' AND NOT EXISTS (SELECT `username` FROM `da_acl`);");
1241 // Fix domain_admins
1242 $pdo->query("DELETE FROM `domain_admins` WHERE `domain` = 'ALL';");
1243
1244 if (php_sapi_name() == "cli") {
1245 echo "DB initialization completed" . PHP_EOL;
1246 } else {
1247 $_SESSION['return'][] = array(
1248 'type' => 'success',
1249 'log' => array(__FUNCTION__),
1250 'msg' => 'db_init_complete'
1251 );
1252 }
1253 }
1254 catch (PDOException $e) {
1255 if (php_sapi_name() == "cli") {
1256 echo "DB initialization failed: " . print_r($e, true) . PHP_EOL;
1257 } else {
1258 $_SESSION['return'][] = array(
1259 'type' => 'danger',
1260 'log' => array(__FUNCTION__),
1261 'msg' => array('mysql_error', $e)
1262 );
1263 }
1264 }
1265}
1266if (php_sapi_name() == "cli") {
1267 include '/web/inc/vars.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001268 include '/web/inc/functions.docker.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001269 // $now = new DateTime();
1270 // $mins = $now->getOffset() / 60;
1271 // $sgn = ($mins < 0 ? -1 : 1);
1272 // $mins = abs($mins);
1273 // $hrs = floor($mins / 60);
1274 // $mins -= $hrs * 60;
1275 // $offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
1276 $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
1277 $opt = [
1278 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
1279 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
1280 PDO::ATTR_EMULATE_PREPARES => false,
1281 //PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '" . $offset . "', group_concat_max_len = 3423543543;",
1282 ];
1283 $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
1284 $stmt = $pdo->query("SELECT COUNT('OK') AS OK_C FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sogo_view' OR TABLE_NAME = '_sogo_static_view';");
1285 $res = $stmt->fetch(PDO::FETCH_ASSOC);
1286 if (intval($res['OK_C']) === 2) {
1287 // Be more precise when replacing into _sogo_static_view, col orders may change
1288 try {
1289 $stmt = $pdo->query("REPLACE INTO _sogo_static_view (`c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings`)
1290 SELECT `c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings` from sogo_view");
1291 $stmt = $pdo->query("DELETE FROM _sogo_static_view WHERE `c_uid` NOT IN (SELECT `username` FROM `mailbox` WHERE `active` = '1');");
1292 echo "Fixed _sogo_static_view" . PHP_EOL;
1293 }
1294 catch ( Exception $e ) {
1295 // Dunno
1296 }
1297 }
1298 try {
1299 $m = new Memcached();
1300 $m->addServer('memcached', 11211);
1301 $m->flush();
1302 echo "Cleaned up memcached". PHP_EOL;
1303 }
1304 catch ( Exception $e ) {
1305 // Dunno
1306 }
1307 init_db_schema();
1308}