blob: f6b0cd7923f334fb55cf5ed3dec5e7355d3f033e [file] [log] [blame]
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001<?php
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02002function init_db_schema()
3{
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01004 try {
5 global $pdo;
6
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02007 $db_version = "18082026_1200";
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01008
9 $stmt = $pdo->query("SHOW TABLES LIKE 'versions'");
10 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
11 if ($num_results != 0) {
12 $stmt = $pdo->query("SELECT `version` FROM `versions` WHERE `application` = 'db_schema'");
13 if ($stmt->fetch(PDO::FETCH_ASSOC)['version'] == $db_version) {
14 return true;
15 }
16 if (!preg_match('/y|yes/i', getenv('MASTER'))) {
17 $_SESSION['return'][] = array(
18 'type' => 'warning',
19 'log' => array(__FUNCTION__),
20 'msg' => 'Database not initialized: not running db_init on slave.'
21 );
22 return true;
23 }
24 }
25
26 $views = array(
27 "grouped_mail_aliases" => "CREATE VIEW grouped_mail_aliases (username, aliases) AS
28 SELECT goto, IFNULL(GROUP_CONCAT(address ORDER BY address SEPARATOR ' '), '') AS address FROM alias
29 WHERE address!=goto
30 AND active = '1'
31 AND sogo_visible = '1'
32 AND address NOT LIKE '@%'
33 GROUP BY goto;",
34 // START
35 // Unused at the moment - we cannot allow to show a foreign mailbox as sender address in SOGo, as SOGo does not like this
36 // We need to create delegation in SOGo AND set a sender_acl in mailcow to allow to send as user X
37 "grouped_sender_acl" => "CREATE VIEW grouped_sender_acl (username, send_as_acl) AS
38 SELECT logged_in_as, IFNULL(GROUP_CONCAT(send_as SEPARATOR ' '), '') AS send_as_acl FROM sender_acl
39 WHERE send_as NOT LIKE '@%'
40 GROUP BY logged_in_as;",
41 // END
42 "grouped_sender_acl_external" => "CREATE VIEW grouped_sender_acl_external (username, send_as_acl) AS
43 SELECT logged_in_as, IFNULL(GROUP_CONCAT(send_as SEPARATOR ' '), '') AS send_as_acl FROM sender_acl
44 WHERE send_as NOT LIKE '@%' AND external = '1'
45 GROUP BY logged_in_as;",
46 "grouped_domain_alias_address" => "CREATE VIEW grouped_domain_alias_address (username, ad_alias) AS
47 SELECT username, IFNULL(GROUP_CONCAT(local_part, '@', alias_domain SEPARATOR ' '), '') AS ad_alias FROM mailbox
48 LEFT OUTER JOIN alias_domain ON target_domain=domain
49 GROUP BY username;",
50 "sieve_before" => "CREATE VIEW sieve_before (id, username, script_name, script_data) AS
51 SELECT md5(script_data), username, script_name, script_data FROM sieve_filters
52 WHERE filter_type = 'prefilter';",
53 "sieve_after" => "CREATE VIEW sieve_after (id, username, script_name, script_data) AS
54 SELECT md5(script_data), username, script_name, script_data FROM sieve_filters
55 WHERE filter_type = 'postfilter';"
56 );
57
58 $tables = array(
59 "versions" => array(
60 "cols" => array(
61 "application" => "VARCHAR(255) NOT NULL",
62 "version" => "VARCHAR(100) NOT NULL",
63 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
64 ),
65 "keys" => array(
66 "primary" => array(
67 "" => array("application")
68 )
69 ),
70 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
71 ),
72 "admin" => array(
73 "cols" => array(
74 "username" => "VARCHAR(255) NOT NULL",
75 "password" => "VARCHAR(255) NOT NULL",
76 "superadmin" => "TINYINT(1) NOT NULL DEFAULT '0'",
77 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
78 "modified" => "DATETIME ON UPDATE NOW(0)",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020079 "active" => "TINYINT(1) NOT NULL DEFAULT '1'",
80 "attributes" => "JSON"
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010081 ),
82 "keys" => array(
83 "primary" => array(
84 "" => array("username")
85 )
86 ),
87 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
88 ),
89 "fido2" => array(
90 "cols" => array(
91 "username" => "VARCHAR(255) NOT NULL",
92 "friendlyName" => "VARCHAR(255)",
93 "rpId" => "VARCHAR(255) NOT NULL",
94 "credentialPublicKey" => "TEXT NOT NULL",
95 "certificateChain" => "TEXT",
96 // Can be null for format "none"
97 "certificate" => "TEXT",
98 "certificateIssuer" => "VARCHAR(255)",
99 "certificateSubject" => "VARCHAR(255)",
100 "signatureCounter" => "INT",
101 "AAGUID" => "BLOB",
102 "credentialId" => "BLOB NOT NULL",
103 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
104 "modified" => "DATETIME ON UPDATE NOW(0)",
105 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
106 ),
107 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
108 ),
109 "_sogo_static_view" => array(
110 "cols" => array(
111 "c_uid" => "VARCHAR(255) NOT NULL",
112 "domain" => "VARCHAR(255) NOT NULL",
113 "c_name" => "VARCHAR(255) NOT NULL",
114 "c_password" => "VARCHAR(255) NOT NULL DEFAULT ''",
115 "c_cn" => "VARCHAR(255)",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200116 "c_l" => "VARCHAR(255)",
117 "c_o" => "VARCHAR(255)",
118 "c_ou" => "VARCHAR(255)",
119 "c_telephonenumber" => "VARCHAR(255)",
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100120 "mail" => "VARCHAR(255) NOT NULL",
121 // TODO -> use TEXT and check if SOGo login breaks on empty aliases
122 "aliases" => "TEXT NOT NULL",
123 "ad_aliases" => "VARCHAR(6144) NOT NULL DEFAULT ''",
124 "ext_acl" => "VARCHAR(6144) NOT NULL DEFAULT ''",
125 "kind" => "VARCHAR(100) NOT NULL DEFAULT ''",
126 "multiple_bookings" => "INT NOT NULL DEFAULT -1"
127 ),
128 "keys" => array(
129 "primary" => array(
130 "" => array("c_uid")
131 ),
132 "key" => array(
133 "domain" => array("domain")
134 )
135 ),
136 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
137 ),
138 "relayhosts" => array(
139 "cols" => array(
140 "id" => "INT NOT NULL AUTO_INCREMENT",
141 "hostname" => "VARCHAR(255) NOT NULL",
142 "username" => "VARCHAR(255) NOT NULL",
143 "password" => "VARCHAR(255) NOT NULL",
144 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
145 ),
146 "keys" => array(
147 "primary" => array(
148 "" => array("id")
149 ),
150 "key" => array(
151 "hostname" => array("hostname")
152 )
153 ),
154 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
155 ),
156 "transports" => array(
157 "cols" => array(
158 "id" => "INT NOT NULL AUTO_INCREMENT",
159 "destination" => "VARCHAR(255) NOT NULL",
160 "nexthop" => "VARCHAR(255) NOT NULL",
161 "username" => "VARCHAR(255) NOT NULL DEFAULT ''",
162 "password" => "VARCHAR(255) NOT NULL DEFAULT ''",
163 "is_mx_based" => "TINYINT(1) NOT NULL DEFAULT '0'",
164 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
165 ),
166 "keys" => array(
167 "primary" => array(
168 "" => array("id")
169 ),
170 "key" => array(
171 "destination" => array("destination"),
172 "nexthop" => array("nexthop"),
173 )
174 ),
175 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
176 ),
177 "alias" => array(
178 "cols" => array(
179 "id" => "INT NOT NULL AUTO_INCREMENT",
180 "address" => "VARCHAR(255) NOT NULL",
181 "goto" => "TEXT NOT NULL",
182 "domain" => "VARCHAR(255) NOT NULL",
183 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
184 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
185 "private_comment" => "TEXT",
186 "public_comment" => "TEXT",
187 "sogo_visible" => "TINYINT(1) NOT NULL DEFAULT '1'",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200188 "internal" => "TINYINT(1) NOT NULL DEFAULT '0'",
189 "sender_allowed" => "TINYINT(1) NOT NULL DEFAULT '1'",
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100190 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
191 ),
192 "keys" => array(
193 "primary" => array(
194 "" => array("id")
195 ),
196 "unique" => array(
197 "address" => array("address")
198 ),
199 "key" => array(
200 "domain" => array("domain")
201 )
202 ),
203 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
204 ),
205 "api" => array(
206 "cols" => array(
207 "api_key" => "VARCHAR(255) NOT NULL",
208 "allow_from" => "VARCHAR(512) NOT NULL",
209 "skip_ip_check" => "TINYINT(1) NOT NULL DEFAULT '0'",
210 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
211 "modified" => "DATETIME ON UPDATE NOW(0)",
212 "access" => "ENUM('ro', 'rw') NOT NULL DEFAULT 'rw'",
213 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
214 ),
215 "keys" => array(
216 "primary" => array(
217 "" => array("api_key")
218 ),
219 ),
220 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
221 ),
222 "sender_acl" => array(
223 "cols" => array(
224 "id" => "INT NOT NULL AUTO_INCREMENT",
225 "logged_in_as" => "VARCHAR(255) NOT NULL",
226 "send_as" => "VARCHAR(255) NOT NULL",
227 "external" => "TINYINT(1) NOT NULL DEFAULT '0'"
228 ),
229 "keys" => array(
230 "primary" => array(
231 "" => array("id")
232 )
233 ),
234 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
235 ),
236 "templates" => array(
237 "cols" => array(
238 "id" => "INT NOT NULL AUTO_INCREMENT",
239 "template" => "VARCHAR(255) NOT NULL",
240 "type" => "VARCHAR(255) NOT NULL",
241 "attributes" => "JSON",
242 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
243 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP"
244 ),
245 "keys" => array(
246 "primary" => array(
247 "" => array("id")
248 )
249 ),
250 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
251 ),
252 "domain" => array(
253 // Todo: Move some attributes to json
254 "cols" => array(
255 "domain" => "VARCHAR(255) NOT NULL",
256 "description" => "VARCHAR(255)",
257 "aliases" => "INT(10) NOT NULL DEFAULT '0'",
258 "mailboxes" => "INT(10) NOT NULL DEFAULT '0'",
259 "defquota" => "BIGINT(20) NOT NULL DEFAULT '3072'",
260 "maxquota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
261 "quota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
262 "relayhost" => "VARCHAR(255) NOT NULL DEFAULT '0'",
263 "backupmx" => "TINYINT(1) NOT NULL DEFAULT '0'",
264 "gal" => "TINYINT(1) NOT NULL DEFAULT '1'",
265 "relay_all_recipients" => "TINYINT(1) NOT NULL DEFAULT '0'",
266 "relay_unknown_only" => "TINYINT(1) NOT NULL DEFAULT '0'",
267 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
268 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
269 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
270 ),
271 "keys" => array(
272 "primary" => array(
273 "" => array("domain")
274 )
275 ),
276 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
277 ),
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100278 "domain_wide_footer" => array(
279 "cols" => array(
280 "domain" => "VARCHAR(255) NOT NULL",
281 "html" => "LONGTEXT",
282 "plain" => "LONGTEXT",
283 "mbox_exclude" => "JSON NOT NULL DEFAULT ('[]')",
284 "alias_domain_exclude" => "JSON NOT NULL DEFAULT ('[]')",
285 "skip_replies" => "TINYINT(1) NOT NULL DEFAULT '0'"
286 ),
287 "keys" => array(
288 "primary" => array(
289 "" => array("domain")
290 )
291 ),
292 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
293 ),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100294 "tags_domain" => array(
295 "cols" => array(
296 "tag_name" => "VARCHAR(255) NOT NULL",
297 "domain" => "VARCHAR(255) NOT NULL"
298 ),
299 "keys" => array(
300 "fkey" => array(
301 "fk_tags_domain" => array(
302 "col" => "domain",
303 "ref" => "domain.domain",
304 "delete" => "CASCADE",
305 "update" => "NO ACTION"
306 )
307 ),
308 "unique" => array(
309 "tag_name" => array("tag_name", "domain")
310 )
311 ),
312 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
313 ),
314 "tls_policy_override" => array(
315 "cols" => array(
316 "id" => "INT NOT NULL AUTO_INCREMENT",
317 "dest" => "VARCHAR(255) NOT NULL",
318 "policy" => "ENUM('none', 'may', 'encrypt', 'dane', 'dane-only', 'fingerprint', 'verify', 'secure') NOT NULL",
319 "parameters" => "VARCHAR(255) DEFAULT ''",
320 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
321 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
322 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
323 ),
324 "keys" => array(
325 "primary" => array(
326 "" => array("id")
327 ),
328 "unique" => array(
329 "dest" => array("dest")
330 ),
331 ),
332 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
333 ),
334 "quarantine" => array(
335 "cols" => array(
336 "id" => "INT NOT NULL AUTO_INCREMENT",
337 "qid" => "VARCHAR(30) NOT NULL",
338 "subject" => "VARCHAR(500)",
339 "score" => "FLOAT(8,2)",
340 "ip" => "VARCHAR(50)",
341 "action" => "CHAR(20) NOT NULL DEFAULT 'unknown'",
342 "symbols" => "JSON",
343 "fuzzy_hashes" => "JSON",
344 "sender" => "VARCHAR(255) NOT NULL DEFAULT 'unknown'",
345 "rcpt" => "VARCHAR(255)",
346 "msg" => "LONGTEXT",
347 "domain" => "VARCHAR(255)",
348 "notified" => "TINYINT(1) NOT NULL DEFAULT '0'",
349 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
350 "user" => "VARCHAR(255) NOT NULL DEFAULT 'unknown'",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200351 "qhash" => "VARCHAR(64)",
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100352 ),
353 "keys" => array(
354 "primary" => array(
355 "" => array("id")
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200356 ),
357 "key" => array(
358 "qhash" => array("qhash")
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100359 )
360 ),
361 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
362 ),
363 "mailbox" => array(
364 "cols" => array(
365 "username" => "VARCHAR(255) NOT NULL",
366 "password" => "VARCHAR(255) NOT NULL",
367 "name" => "VARCHAR(255)",
368 "description" => "VARCHAR(255)",
369 // mailbox_path_prefix is followed by domain/local_part/
370 "mailbox_path_prefix" => "VARCHAR(150) DEFAULT '/var/vmail/'",
371 "quota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
372 "local_part" => "VARCHAR(255) NOT NULL",
373 "domain" => "VARCHAR(255) NOT NULL",
374 "attributes" => "JSON",
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100375 "custom_attributes" => "JSON NOT NULL DEFAULT ('{}')",
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100376 "kind" => "VARCHAR(100) NOT NULL DEFAULT ''",
377 "multiple_bookings" => "INT NOT NULL DEFAULT -1",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200378 "authsource" => "ENUM('mailcow', 'keycloak', 'generic-oidc', 'ldap') DEFAULT 'mailcow'",
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100379 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
380 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
381 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
382 ),
383 "keys" => array(
384 "primary" => array(
385 "" => array("username")
386 ),
387 "key" => array(
388 "domain" => array("domain"),
389 "kind" => array("kind")
390 )
391 ),
392 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
393 ),
394 "tags_mailbox" => array(
395 "cols" => array(
396 "tag_name" => "VARCHAR(255) NOT NULL",
397 "username" => "VARCHAR(255) NOT NULL"
398 ),
399 "keys" => array(
400 "fkey" => array(
401 "fk_tags_mailbox" => array(
402 "col" => "username",
403 "ref" => "mailbox.username",
404 "delete" => "CASCADE",
405 "update" => "NO ACTION"
406 )
407 ),
408 "unique" => array(
409 "tag_name" => array("tag_name", "username")
410 )
411 ),
412 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
413 ),
414 "sieve_filters" => array(
415 "cols" => array(
416 "id" => "INT NOT NULL AUTO_INCREMENT",
417 "username" => "VARCHAR(255) NOT NULL",
418 "script_desc" => "VARCHAR(255) NOT NULL",
419 "script_name" => "ENUM('active','inactive')",
420 "script_data" => "TEXT NOT NULL",
421 "filter_type" => "ENUM('postfilter','prefilter')",
422 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
423 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP"
424 ),
425 "keys" => array(
426 "primary" => array(
427 "" => array("id")
428 ),
429 "key" => array(
430 "username" => array("username"),
431 "script_desc" => array("script_desc")
432 ),
433 "fkey" => array(
434 "fk_username_sieve_global_before" => array(
435 "col" => "username",
436 "ref" => "mailbox.username",
437 "delete" => "CASCADE",
438 "update" => "NO ACTION"
439 )
440 )
441 ),
442 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
443 ),
444 "app_passwd" => array(
445 "cols" => array(
446 "id" => "INT NOT NULL AUTO_INCREMENT",
447 "name" => "VARCHAR(255) NOT NULL",
448 "mailbox" => "VARCHAR(255) NOT NULL",
449 "domain" => "VARCHAR(255) NOT NULL",
450 "password" => "VARCHAR(255) NOT NULL",
451 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
452 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
453 "imap_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
454 "smtp_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
455 "dav_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
456 "eas_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
457 "pop3_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
458 "sieve_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
459 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
460 ),
461 "keys" => array(
462 "primary" => array(
463 "" => array("id")
464 ),
465 "key" => array(
466 "mailbox" => array("mailbox"),
467 "password" => array("password"),
468 "domain" => array("domain"),
469 ),
470 "fkey" => array(
471 "fk_username_app_passwd" => array(
472 "col" => "mailbox",
473 "ref" => "mailbox.username",
474 "delete" => "CASCADE",
475 "update" => "NO ACTION"
476 )
477 )
478 ),
479 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
480 ),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200481 "mta_sts" => array(
482 "cols" => array(
483 "id" => "BIGINT NOT NULL",
484 "domain" => "VARCHAR(255) NOT NULL",
485 "version" => "VARCHAR(255) NOT NULL",
486 "mode" => "VARCHAR(255) NOT NULL",
487 "mx" => "VARCHAR(255) NOT NULL",
488 "max_age" => "VARCHAR(255) NOT NULL",
489 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
490 ),
491 "keys" => array(
492 "primary" => array(
493 "" => array("domain")
494 )
495 ),
496 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
497 ),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100498 "user_acl" => array(
499 "cols" => array(
500 "username" => "VARCHAR(255) NOT NULL",
501 "spam_alias" => "TINYINT(1) NOT NULL DEFAULT '1'",
502 "tls_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
503 "spam_score" => "TINYINT(1) NOT NULL DEFAULT '1'",
504 "spam_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
505 "delimiter_action" => "TINYINT(1) NOT NULL DEFAULT '1'",
506 "syncjobs" => "TINYINT(1) NOT NULL DEFAULT '0'",
507 "eas_reset" => "TINYINT(1) NOT NULL DEFAULT '1'",
508 "sogo_profile_reset" => "TINYINT(1) NOT NULL DEFAULT '0'",
509 "pushover" => "TINYINT(1) NOT NULL DEFAULT '1'",
510 // quarantine is for quarantine actions, todo: rename
511 "quarantine" => "TINYINT(1) NOT NULL DEFAULT '1'",
512 "quarantine_attachments" => "TINYINT(1) NOT NULL DEFAULT '1'",
513 "quarantine_notification" => "TINYINT(1) NOT NULL DEFAULT '1'",
514 "quarantine_category" => "TINYINT(1) NOT NULL DEFAULT '1'",
515 "app_passwds" => "TINYINT(1) NOT NULL DEFAULT '1'",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200516 "pw_reset" => "TINYINT(1) NOT NULL DEFAULT '1'",
517 ),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100518 "keys" => array(
519 "primary" => array(
520 "" => array("username")
521 ),
522 "fkey" => array(
523 "fk_username" => array(
524 "col" => "username",
525 "ref" => "mailbox.username",
526 "delete" => "CASCADE",
527 "update" => "NO ACTION"
528 )
529 )
530 ),
531 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
532 ),
533 "alias_domain" => array(
534 "cols" => array(
535 "alias_domain" => "VARCHAR(255) NOT NULL",
536 "target_domain" => "VARCHAR(255) NOT NULL",
537 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
538 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
539 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
540 ),
541 "keys" => array(
542 "primary" => array(
543 "" => array("alias_domain")
544 ),
545 "key" => array(
546 "active" => array("active"),
547 "target_domain" => array("target_domain")
548 )
549 ),
550 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
551 ),
552 "spamalias" => array(
553 "cols" => array(
554 "address" => "VARCHAR(255) NOT NULL",
555 "goto" => "TEXT NOT NULL",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200556 "description" => "TEXT NOT NULL",
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100557 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
558 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200559 "validity" => "INT(11)",
560 "permanent" => "TINYINT(1) NOT NULL DEFAULT '0'"
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100561 ),
562 "keys" => array(
563 "primary" => array(
564 "" => array("address")
565 ),
566 ),
567 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
568 ),
569 "filterconf" => array(
570 "cols" => array(
571 "object" => "VARCHAR(255) NOT NULL DEFAULT ''",
572 "option" => "VARCHAR(50) NOT NULL DEFAULT ''",
573 "value" => "VARCHAR(100) NOT NULL DEFAULT ''",
574 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
575 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
576 "prefid" => "INT(11) NOT NULL AUTO_INCREMENT"
577 ),
578 "keys" => array(
579 "primary" => array(
580 "" => array("prefid")
581 ),
582 "key" => array(
583 "object" => array("object")
584 )
585 ),
586 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
587 ),
588 "settingsmap" => array(
589 "cols" => array(
590 "id" => "INT NOT NULL AUTO_INCREMENT",
591 "desc" => "VARCHAR(255) NOT NULL",
592 "content" => "LONGTEXT NOT NULL",
593 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
594 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
595 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
596 ),
597 "keys" => array(
598 "primary" => array(
599 "" => array("id")
600 )
601 ),
602 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
603 ),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200604 "identity_provider" => array(
605 "cols" => array(
606 "key" => "VARCHAR(255) NOT NULL",
607 "value" => "TEXT NOT NULL",
608 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
609 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP"
610 ),
611 "keys" => array(
612 "primary" => array(
613 "" => array("key")
614 )
615 ),
616 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
617 ),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100618 "logs" => array(
619 "cols" => array(
620 "id" => "INT NOT NULL AUTO_INCREMENT",
621 "task" => "CHAR(32) NOT NULL DEFAULT '000000'",
622 "type" => "VARCHAR(32) DEFAULT ''",
623 "msg" => "TEXT",
624 "call" => "TEXT",
625 "user" => "VARCHAR(64) NOT NULL",
626 "role" => "VARCHAR(32) NOT NULL",
627 "remote" => "VARCHAR(39) NOT NULL",
628 "time" => "INT(11) NOT NULL"
629 ),
630 "keys" => array(
631 "primary" => array(
632 "" => array("id")
633 )
634 ),
635 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
636 ),
637 "sasl_log" => array(
638 "cols" => array(
639 "service" => "VARCHAR(32) NOT NULL DEFAULT ''",
640 "app_password" => "INT",
641 "username" => "VARCHAR(255) NOT NULL",
642 "real_rip" => "VARCHAR(64) NOT NULL",
643 "datetime" => "DATETIME(0) NOT NULL DEFAULT NOW(0)"
644 ),
645 "keys" => array(
646 "primary" => array(
647 "" => array("service", "real_rip", "username")
648 ),
649 "key" => array(
650 "username" => array("username"),
651 "service" => array("service"),
652 "datetime" => array("datetime"),
653 "real_rip" => array("real_rip")
654 )
655 ),
656 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
657 ),
658 "quota2" => array(
659 "cols" => array(
660 "username" => "VARCHAR(255) NOT NULL",
661 "bytes" => "BIGINT(20) NOT NULL DEFAULT '0'",
662 "messages" => "BIGINT(20) NOT NULL DEFAULT '0'"
663 ),
664 "keys" => array(
665 "primary" => array(
666 "" => array("username")
667 )
668 ),
669 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
670 ),
671 "quota2replica" => array(
672 "cols" => array(
673 "username" => "VARCHAR(255) NOT NULL",
674 "bytes" => "BIGINT(20) NOT NULL DEFAULT '0'",
675 "messages" => "BIGINT(20) NOT NULL DEFAULT '0'"
676 ),
677 "keys" => array(
678 "primary" => array(
679 "" => array("username")
680 )
681 ),
682 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
683 ),
684 "domain_admins" => array(
685 "cols" => array(
686 "id" => "INT NOT NULL AUTO_INCREMENT",
687 "username" => "VARCHAR(255) NOT NULL",
688 "domain" => "VARCHAR(255) NOT NULL",
689 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
690 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
691 ),
692 "keys" => array(
693 "primary" => array(
694 "" => array("id")
695 ),
696 "key" => array(
697 "username" => array("username")
698 )
699 ),
700 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
701 ),
702 "da_acl" => array(
703 "cols" => array(
704 "username" => "VARCHAR(255) NOT NULL",
705 "syncjobs" => "TINYINT(1) NOT NULL DEFAULT '1'",
706 "quarantine" => "TINYINT(1) NOT NULL DEFAULT '1'",
707 "login_as" => "TINYINT(1) NOT NULL DEFAULT '1'",
708 "sogo_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
709 "app_passwds" => "TINYINT(1) NOT NULL DEFAULT '1'",
710 "bcc_maps" => "TINYINT(1) NOT NULL DEFAULT '1'",
711 "pushover" => "TINYINT(1) NOT NULL DEFAULT '0'",
712 "filters" => "TINYINT(1) NOT NULL DEFAULT '1'",
713 "ratelimit" => "TINYINT(1) NOT NULL DEFAULT '1'",
714 "spam_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
715 "extend_sender_acl" => "TINYINT(1) NOT NULL DEFAULT '0'",
716 "unlimited_quota" => "TINYINT(1) NOT NULL DEFAULT '0'",
717 "protocol_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
718 "smtp_ip_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
719 "alias_domains" => "TINYINT(1) NOT NULL DEFAULT '0'",
720 "mailbox_relayhost" => "TINYINT(1) NOT NULL DEFAULT '1'",
721 "domain_relayhost" => "TINYINT(1) NOT NULL DEFAULT '1'",
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200722 "domain_desc" => "TINYINT(1) NOT NULL DEFAULT '0'",
723 "alias_external_goto" => "TINYINT(1) NOT NULL DEFAULT '1'"
724 ),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100725 "keys" => array(
726 "primary" => array(
727 "" => array("username")
728 )
729 ),
730 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
731 ),
732 "da_sso" => array(
733 "cols" => array(
734 "username" => "VARCHAR(255) NOT NULL",
735 "token" => "VARCHAR(255) NOT NULL",
736 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
737 ),
738 "keys" => array(
739 "primary" => array(
740 "" => array("token", "created")
741 ),
742 ),
743 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
744 ),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200745 "reset_password" => array(
746 "cols" => array(
747 "username" => "VARCHAR(255) NOT NULL",
748 "token" => "VARCHAR(255) NOT NULL",
749 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
750 ),
751 "keys" => array(
752 "primary" => array(
753 "" => array("token", "created")
754 ),
755 ),
756 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
757 ),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100758 "imapsync" => array(
759 "cols" => array(
760 "id" => "INT NOT NULL AUTO_INCREMENT",
761 "user2" => "VARCHAR(255) NOT NULL",
762 "host1" => "VARCHAR(255) NOT NULL",
763 "authmech1" => "ENUM('PLAIN','LOGIN','CRAM-MD5') DEFAULT 'PLAIN'",
764 "regextrans2" => "VARCHAR(255) DEFAULT ''",
765 "authmd51" => "TINYINT(1) NOT NULL DEFAULT 0",
766 "domain2" => "VARCHAR(255) NOT NULL DEFAULT ''",
767 "subfolder2" => "VARCHAR(255) NOT NULL DEFAULT ''",
768 "user1" => "VARCHAR(255) NOT NULL",
769 "password1" => "VARCHAR(255) NOT NULL",
770 "exclude" => "VARCHAR(500) NOT NULL DEFAULT ''",
771 "maxage" => "SMALLINT NOT NULL DEFAULT '0'",
772 "mins_interval" => "SMALLINT UNSIGNED NOT NULL DEFAULT '0'",
773 "maxbytespersecond" => "VARCHAR(50) NOT NULL DEFAULT '0'",
774 "port1" => "SMALLINT UNSIGNED NOT NULL",
775 "enc1" => "ENUM('TLS','SSL','PLAIN') DEFAULT 'TLS'",
776 "delete2duplicates" => "TINYINT(1) NOT NULL DEFAULT '1'",
777 "delete1" => "TINYINT(1) NOT NULL DEFAULT '0'",
778 "delete2" => "TINYINT(1) NOT NULL DEFAULT '0'",
779 "automap" => "TINYINT(1) NOT NULL DEFAULT '0'",
780 "skipcrossduplicates" => "TINYINT(1) NOT NULL DEFAULT '0'",
781 "custom_params" => "VARCHAR(512) NOT NULL DEFAULT ''",
782 "timeout1" => "SMALLINT NOT NULL DEFAULT '600'",
783 "timeout2" => "SMALLINT NOT NULL DEFAULT '600'",
784 "subscribeall" => "TINYINT(1) NOT NULL DEFAULT '1'",
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100785 "dry" => "TINYINT(1) NOT NULL DEFAULT '0'",
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100786 "is_running" => "TINYINT(1) NOT NULL DEFAULT '0'",
787 "returned_text" => "LONGTEXT",
788 "last_run" => "TIMESTAMP NULL DEFAULT NULL",
789 "success" => "TINYINT(1) UNSIGNED DEFAULT NULL",
790 "exit_status" => "VARCHAR(50) DEFAULT NULL",
791 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
792 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
793 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
794 ),
795 "keys" => array(
796 "primary" => array(
797 "" => array("id")
798 )
799 ),
800 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
801 ),
802 "bcc_maps" => array(
803 "cols" => array(
804 "id" => "INT NOT NULL AUTO_INCREMENT",
805 "local_dest" => "VARCHAR(255) NOT NULL",
806 "bcc_dest" => "VARCHAR(255) NOT NULL",
807 "domain" => "VARCHAR(255) NOT NULL",
808 "type" => "ENUM('sender','rcpt')",
809 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
810 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
811 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
812 ),
813 "keys" => array(
814 "primary" => array(
815 "" => array("id")
816 ),
817 "key" => array(
818 "local_dest" => array("local_dest"),
819 )
820 ),
821 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
822 ),
823 "recipient_maps" => array(
824 "cols" => array(
825 "id" => "INT NOT NULL AUTO_INCREMENT",
826 "old_dest" => "VARCHAR(255) NOT NULL",
827 "new_dest" => "VARCHAR(255) NOT NULL",
828 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
829 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
830 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
831 ),
832 "keys" => array(
833 "primary" => array(
834 "" => array("id")
835 ),
836 "key" => array(
837 "local_dest" => array("old_dest"),
838 )
839 ),
840 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
841 ),
842 "tfa" => array(
843 "cols" => array(
844 "id" => "INT NOT NULL AUTO_INCREMENT",
845 "key_id" => "VARCHAR(255) NOT NULL",
846 "username" => "VARCHAR(255) NOT NULL",
847 "authmech" => "ENUM('yubi_otp', 'u2f', 'hotp', 'totp', 'webauthn')",
848 "secret" => "VARCHAR(255) DEFAULT NULL",
849 "keyHandle" => "VARCHAR(1023) DEFAULT NULL",
850 "publicKey" => "VARCHAR(4096) DEFAULT NULL",
851 "counter" => "INT NOT NULL DEFAULT '0'",
852 "certificate" => "TEXT",
853 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
854 ),
855 "keys" => array(
856 "primary" => array(
857 "" => array("id")
858 )
859 ),
860 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
861 ),
862 "forwarding_hosts" => array(
863 "cols" => array(
864 "host" => "VARCHAR(255) NOT NULL",
865 "source" => "VARCHAR(255) NOT NULL",
866 "filter_spam" => "TINYINT(1) NOT NULL DEFAULT '0'"
867 ),
868 "keys" => array(
869 "primary" => array(
870 "" => array("host")
871 ),
872 ),
873 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
874 ),
875 "sogo_acl" => array(
876 "cols" => array(
877 "id" => "INT NOT NULL AUTO_INCREMENT",
878 "c_folder_id" => "INT NOT NULL",
879 "c_object" => "VARCHAR(255) NOT NULL",
880 "c_uid" => "VARCHAR(255) NOT NULL",
881 "c_role" => "VARCHAR(80) NOT NULL"
882 ),
883 "keys" => array(
884 "primary" => array(
885 "" => array("id")
886 ),
887 "key" => array(
888 "sogo_acl_c_folder_id_idx" => array("c_folder_id"),
889 "sogo_acl_c_uid_idx" => array("c_uid")
890 )
891 ),
892 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
893 ),
894 "sogo_alarms_folder" => array(
895 "cols" => array(
896 "id" => "INT NOT NULL AUTO_INCREMENT",
897 "c_path" => "VARCHAR(255) NOT NULL",
898 "c_name" => "VARCHAR(255) NOT NULL",
899 "c_uid" => "VARCHAR(255) NOT NULL",
900 "c_recurrence_id" => "INT(11) DEFAULT NULL",
901 "c_alarm_number" => "INT(11) NOT NULL",
902 "c_alarm_date" => "INT(11) NOT NULL"
903 ),
904 "keys" => array(
905 "primary" => array(
906 "" => array("id")
907 )
908 ),
909 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
910 ),
911 "sogo_cache_folder" => array(
912 "cols" => array(
913 "c_uid" => "VARCHAR(255) NOT NULL",
914 "c_path" => "VARCHAR(255) NOT NULL",
915 "c_parent_path" => "VARCHAR(255) DEFAULT NULL",
916 "c_type" => "TINYINT(3) unsigned NOT NULL",
917 "c_creationdate" => "INT(11) NOT NULL",
918 "c_lastmodified" => "INT(11) NOT NULL",
919 "c_version" => "INT(11) NOT NULL DEFAULT '0'",
920 "c_deleted" => "TINYINT(4) NOT NULL DEFAULT '0'",
921 "c_content" => "LONGTEXT"
922 ),
923 "keys" => array(
924 "primary" => array(
925 "" => array("c_uid", "c_path")
926 ),
927 ),
928 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
929 ),
930 "sogo_folder_info" => array(
931 "cols" => array(
932 "c_folder_id" => "BIGINT(20) unsigned NOT NULL AUTO_INCREMENT",
933 "c_path" => "VARCHAR(255) NOT NULL",
934 "c_path1" => "VARCHAR(255) NOT NULL",
935 "c_path2" => "VARCHAR(255) DEFAULT NULL",
936 "c_path3" => "VARCHAR(255) DEFAULT NULL",
937 "c_path4" => "VARCHAR(255) DEFAULT NULL",
938 "c_foldername" => "VARCHAR(255) NOT NULL",
939 "c_location" => "VARCHAR(2048) DEFAULT NULL",
940 "c_quick_location" => "VARCHAR(2048) DEFAULT NULL",
941 "c_acl_location" => "VARCHAR(2048) DEFAULT NULL",
942 "c_folder_type" => "VARCHAR(255) NOT NULL"
943 ),
944 "keys" => array(
945 "primary" => array(
946 "" => array("c_path")
947 ),
948 "unique" => array(
949 "c_folder_id" => array("c_folder_id")
950 )
951 ),
952 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
953 ),
954 "sogo_quick_appointment" => array(
955 "cols" => array(
956 "c_folder_id" => "INT NOT NULL",
957 "c_name" => "VARCHAR(255) NOT NULL",
958 "c_uid" => "VARCHAR(1000) NOT NULL",
959 "c_startdate" => "INT",
960 "c_enddate" => "INT",
961 "c_cycleenddate" => "INT",
962 "c_title" => "VARCHAR(1000) NOT NULL",
963 "c_participants" => "TEXT",
964 "c_isallday" => "INT",
965 "c_iscycle" => "INT",
966 "c_cycleinfo" => "TEXT",
967 "c_classification" => "INT NOT NULL",
968 "c_isopaque" => "INT NOT NULL",
969 "c_status" => "INT NOT NULL",
970 "c_priority" => "INT",
971 "c_location" => "VARCHAR(255)",
972 "c_orgmail" => "VARCHAR(255)",
973 "c_partmails" => "TEXT",
974 "c_partstates" => "TEXT",
975 "c_category" => "VARCHAR(255)",
976 "c_sequence" => "INT",
977 "c_component" => "VARCHAR(10) NOT NULL",
978 "c_nextalarm" => "INT",
979 "c_description" => "TEXT"
980 ),
981 "keys" => array(
982 "primary" => array(
983 "" => array("c_folder_id", "c_name")
984 )
985 ),
986 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
987 ),
988 "sogo_quick_contact" => array(
989 "cols" => array(
990 "c_folder_id" => "INT NOT NULL",
991 "c_name" => "VARCHAR(255) NOT NULL",
992 "c_givenname" => "VARCHAR(255)",
993 "c_cn" => "VARCHAR(255)",
994 "c_sn" => "VARCHAR(255)",
995 "c_screenname" => "VARCHAR(255)",
996 "c_l" => "VARCHAR(255)",
997 "c_mail" => "TEXT",
998 "c_o" => "VARCHAR(500)",
999 "c_ou" => "VARCHAR(255)",
1000 "c_telephonenumber" => "VARCHAR(255)",
1001 "c_categories" => "VARCHAR(255)",
1002 "c_component" => "VARCHAR(10) NOT NULL",
1003 "c_hascertificate" => "INT4 DEFAULT 0"
1004 ),
1005 "keys" => array(
1006 "primary" => array(
1007 "" => array("c_folder_id", "c_name")
1008 )
1009 ),
1010 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1011 ),
1012 "sogo_sessions_folder" => array(
1013 "cols" => array(
1014 "c_id" => "VARCHAR(255) NOT NULL",
1015 "c_value" => "VARCHAR(4096) NOT NULL",
1016 "c_creationdate" => "INT(11) NOT NULL",
1017 "c_lastseen" => "INT(11) NOT NULL"
1018 ),
1019 "keys" => array(
1020 "primary" => array(
1021 "" => array("c_id")
1022 )
1023 ),
1024 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1025 ),
1026 "sogo_store" => array(
1027 "cols" => array(
1028 "c_folder_id" => "INT NOT NULL",
1029 "c_name" => "VARCHAR(255) NOT NULL",
1030 "c_content" => "MEDIUMTEXT NOT NULL",
1031 "c_creationdate" => "INT NOT NULL",
1032 "c_lastmodified" => "INT NOT NULL",
1033 "c_version" => "INT NOT NULL",
1034 "c_deleted" => "INT"
1035 ),
1036 "keys" => array(
1037 "primary" => array(
1038 "" => array("c_folder_id", "c_name")
1039 )
1040 ),
1041 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1042 ),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001043 "sogo_admin" => array(
1044 "cols" => array(
1045 "c_key" => "VARCHAR(255) NOT NULL DEFAULT ''",
1046 "c_content" => "mediumtext NOT NULL",
1047 ),
1048 "keys" => array(
1049 "primary" => array(
1050 "" => array("c_key")
1051 )
1052 ),
1053 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1054 ),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001055 "pushover" => array(
1056 "cols" => array(
1057 "username" => "VARCHAR(255) NOT NULL",
1058 "key" => "VARCHAR(255) NOT NULL",
1059 "token" => "VARCHAR(255) NOT NULL",
1060 "attributes" => "JSON",
1061 "title" => "TEXT",
1062 "text" => "TEXT",
1063 "senders" => "TEXT",
1064 "senders_regex" => "TEXT",
1065 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
1066 ),
1067 "keys" => array(
1068 "primary" => array(
1069 "" => array("username")
1070 )
1071 ),
1072 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1073 ),
1074 "sogo_user_profile" => array(
1075 "cols" => array(
1076 "c_uid" => "VARCHAR(255) NOT NULL",
1077 "c_defaults" => "LONGTEXT",
1078 "c_settings" => "LONGTEXT"
1079 ),
1080 "keys" => array(
1081 "primary" => array(
1082 "" => array("c_uid")
1083 )
1084 ),
1085 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1086 ),
1087 "oauth_clients" => array(
1088 "cols" => array(
1089 "id" => "INT NOT NULL AUTO_INCREMENT",
1090 "client_id" => "VARCHAR(80) NOT NULL",
1091 "client_secret" => "VARCHAR(80)",
1092 "redirect_uri" => "VARCHAR(2000)",
1093 "grant_types" => "VARCHAR(80)",
1094 "scope" => "VARCHAR(4000)",
1095 "user_id" => "VARCHAR(80)"
1096 ),
1097 "keys" => array(
1098 "primary" => array(
1099 "" => array("client_id")
1100 ),
1101 "unique" => array(
1102 "id" => array("id")
1103 )
1104 ),
1105 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1106 ),
1107 "oauth_access_tokens" => array(
1108 "cols" => array(
1109 "access_token" => "VARCHAR(40) NOT NULL",
1110 "client_id" => "VARCHAR(80) NOT NULL",
1111 "user_id" => "VARCHAR(80)",
1112 "expires" => "TIMESTAMP NOT NULL",
1113 "scope" => "VARCHAR(4000)"
1114 ),
1115 "keys" => array(
1116 "primary" => array(
1117 "" => array("access_token")
1118 )
1119 ),
1120 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1121 ),
1122 "oauth_authorization_codes" => array(
1123 "cols" => array(
1124 "authorization_code" => "VARCHAR(40) NOT NULL",
1125 "client_id" => "VARCHAR(80) NOT NULL",
1126 "user_id" => "VARCHAR(80)",
1127 "redirect_uri" => "VARCHAR(2000)",
1128 "expires" => "TIMESTAMP NOT NULL",
1129 "scope" => "VARCHAR(4000)",
1130 "id_token" => "VARCHAR(1000)"
1131 ),
1132 "keys" => array(
1133 "primary" => array(
1134 "" => array("authorization_code")
1135 )
1136 ),
1137 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1138 ),
1139 "oauth_refresh_tokens" => array(
1140 "cols" => array(
1141 "refresh_token" => "VARCHAR(40) NOT NULL",
1142 "client_id" => "VARCHAR(80) NOT NULL",
1143 "user_id" => "VARCHAR(80)",
1144 "expires" => "TIMESTAMP NOT NULL",
1145 "scope" => "VARCHAR(4000)"
1146 ),
1147 "keys" => array(
1148 "primary" => array(
1149 "" => array("refresh_token")
1150 )
1151 ),
1152 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
1153 )
1154 );
1155
1156 foreach ($tables as $table => $properties) {
1157 // Migrate to quarantine
1158 if ($table == 'quarantine') {
1159 $stmt = $pdo->query("SHOW TABLES LIKE 'quarantaine'");
1160 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1161 if ($num_results != 0) {
1162 $stmt = $pdo->query("SHOW TABLES LIKE 'quarantine'");
1163 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1164 if ($num_results == 0) {
1165 $pdo->query("RENAME TABLE `quarantaine` TO `quarantine`");
1166 }
1167 }
1168 }
1169
1170 // Migrate tls_enforce_* options
1171 if ($table == 'mailbox') {
1172 $stmt = $pdo->query("SHOW TABLES LIKE 'mailbox'");
1173 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1174 if ($num_results != 0) {
1175 $stmt = $pdo->query("SHOW COLUMNS FROM `mailbox` LIKE '%tls_enforce%'");
1176 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1177 if ($num_results != 0) {
1178 $stmt = $pdo->query("SELECT `username`, `tls_enforce_in`, `tls_enforce_out` FROM `mailbox`");
1179 $tls_options_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
1180 while ($row = array_shift($tls_options_rows)) {
1181 $tls_options[$row['username']] = array('tls_enforce_in' => $row['tls_enforce_in'], 'tls_enforce_out' => $row['tls_enforce_out']);
1182 }
1183 }
1184 }
1185 }
1186
1187 $stmt = $pdo->query("SHOW TABLES LIKE '" . $table . "'");
1188 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1189 if ($num_results != 0) {
1190 $stmt = $pdo->prepare("SELECT CONCAT('ALTER TABLE `', `table_schema`, '`.', `table_name`, ' DROP FOREIGN KEY ', `constraint_name`, ';') AS `FKEY_DROP` FROM `information_schema`.`table_constraints`
1191 WHERE `constraint_type` = 'FOREIGN KEY' AND `table_name` = :table;");
1192 $stmt->execute(array(':table' => $table));
1193 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
1194 while ($row = array_shift($rows)) {
1195 $pdo->query($row['FKEY_DROP']);
1196 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001197 foreach ($properties['cols'] as $column => $type) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001198 $stmt = $pdo->query("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'");
1199 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1200 if ($num_results == 0) {
1201 if (strpos($type, 'AUTO_INCREMENT') !== false) {
1202 $type = $type . ' PRIMARY KEY ';
1203 // Adding an AUTO_INCREMENT key, need to drop primary keys first, if exists
1204 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1205 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1206 if ($num_results != 0) {
1207 $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY");
1208 }
1209 }
1210 $pdo->query("ALTER TABLE `" . $table . "` ADD `" . $column . "` " . $type);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001211 } else {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001212 $pdo->query("ALTER TABLE `" . $table . "` MODIFY COLUMN `" . $column . "` " . $type);
1213 }
1214 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001215 foreach ($properties['keys'] as $key_type => $key_content) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001216 if (strtolower($key_type) == 'primary') {
1217 foreach ($key_content as $key_values) {
1218 $fields = "`" . implode("`, `", $key_values) . "`";
1219 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1220 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1221 $is_drop = ($num_results != 0) ? "DROP PRIMARY KEY, " : "";
1222 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD PRIMARY KEY (" . $fields . ")");
1223 }
1224 }
1225 if (strtolower($key_type) == 'key') {
1226 foreach ($key_content as $key_name => $key_values) {
1227 $fields = "`" . implode("`, `", $key_values) . "`";
1228 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1229 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1230 $is_drop = ($num_results != 0) ? "DROP INDEX `" . $key_name . "`, " : "";
1231 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD KEY `" . $key_name . "` (" . $fields . ")");
1232 }
1233 }
1234 if (strtolower($key_type) == 'unique') {
1235 foreach ($key_content as $key_name => $key_values) {
1236 $fields = "`" . implode("`, `", $key_values) . "`";
1237 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1238 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1239 $is_drop = ($num_results != 0) ? "DROP INDEX `" . $key_name . "`, " : "";
1240 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD UNIQUE KEY `" . $key_name . "` (" . $fields . ")");
1241 }
1242 }
1243 if (strtolower($key_type) == 'fkey') {
1244 foreach ($key_content as $key_name => $key_values) {
1245 $fields = "`" . implode("`, `", $key_values) . "`";
1246 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1247 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1248 if ($num_results != 0) {
1249 $pdo->query("ALTER TABLE `" . $table . "` DROP INDEX `" . $key_name . "`");
1250 }
1251 @list($table_ref, $field_ref) = explode('.', $key_values['ref']);
1252 $pdo->query("ALTER TABLE `" . $table . "` ADD FOREIGN KEY `" . $key_name . "` (" . $key_values['col'] . ") REFERENCES `" . $table_ref . "` (`" . $field_ref . "`)
1253 ON DELETE " . $key_values['delete'] . " ON UPDATE " . $key_values['update']);
1254 }
1255 }
1256 }
1257 // Drop all vanished columns
1258 $stmt = $pdo->query("SHOW COLUMNS FROM `" . $table . "`");
1259 $cols_in_table = $stmt->fetchAll(PDO::FETCH_ASSOC);
1260 while ($row = array_shift($cols_in_table)) {
1261 if (!array_key_exists($row['Field'], $properties['cols'])) {
1262 $pdo->query("ALTER TABLE `" . $table . "` DROP COLUMN `" . $row['Field'] . "`;");
1263 }
1264 }
1265
1266 // Step 1: Get all non-primary keys, that currently exist and those that should exist
1267 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE `Key_name` != 'PRIMARY'");
1268 $keys_in_table = $stmt->fetchAll(PDO::FETCH_ASSOC);
1269 $keys_to_exist = array();
1270 if (isset($properties['keys']['unique']) && is_array($properties['keys']['unique'])) {
1271 foreach ($properties['keys']['unique'] as $key_name => $key_values) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001272 $keys_to_exist[] = $key_name;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001273 }
1274 }
1275 if (isset($properties['keys']['key']) && is_array($properties['keys']['key'])) {
1276 foreach ($properties['keys']['key'] as $key_name => $key_values) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001277 $keys_to_exist[] = $key_name;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001278 }
1279 }
1280 // Index for foreign key must exist
1281 if (isset($properties['keys']['fkey']) && is_array($properties['keys']['fkey'])) {
1282 foreach ($properties['keys']['fkey'] as $key_name => $key_values) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001283 $keys_to_exist[] = $key_name;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001284 }
1285 }
1286 // Step 2: Drop all vanished indexes
1287 while ($row = array_shift($keys_in_table)) {
1288 if (!in_array($row['Key_name'], $keys_to_exist)) {
1289 $pdo->query("ALTER TABLE `" . $table . "` DROP INDEX `" . $row['Key_name'] . "`");
1290 }
1291 }
1292 // Step 3: Drop all vanished primary keys
1293 if (!isset($properties['keys']['primary'])) {
1294 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1295 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1296 if ($num_results != 0) {
1297 $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY");
1298 }
1299 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001300 } else {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001301 // Create table if it is missing
1302 $sql = "CREATE TABLE IF NOT EXISTS `" . $table . "` (";
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001303 foreach ($properties['cols'] as $column => $type) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001304 $sql .= "`" . $column . "` " . $type . ",";
1305 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001306 foreach ($properties['keys'] as $key_type => $key_content) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001307 if (strtolower($key_type) == 'primary') {
1308 foreach ($key_content as $key_values) {
1309 $fields = "`" . implode("`, `", $key_values) . "`";
1310 $sql .= "PRIMARY KEY (" . $fields . ")" . ",";
1311 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001312 } elseif (strtolower($key_type) == 'key') {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001313 foreach ($key_content as $key_name => $key_values) {
1314 $fields = "`" . implode("`, `", $key_values) . "`";
1315 $sql .= "KEY `" . $key_name . "` (" . $fields . ")" . ",";
1316 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001317 } elseif (strtolower($key_type) == 'unique') {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001318 foreach ($key_content as $key_name => $key_values) {
1319 $fields = "`" . implode("`, `", $key_values) . "`";
1320 $sql .= "UNIQUE KEY `" . $key_name . "` (" . $fields . ")" . ",";
1321 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001322 } elseif (strtolower($key_type) == 'fkey') {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001323 foreach ($key_content as $key_name => $key_values) {
1324 @list($table_ref, $field_ref) = explode('.', $key_values['ref']);
1325 $sql .= "FOREIGN KEY `" . $key_name . "` (" . $key_values['col'] . ") REFERENCES `" . $table_ref . "` (`" . $field_ref . "`)
1326 ON DELETE " . $key_values['delete'] . " ON UPDATE " . $key_values['update'] . ",";
1327 }
1328 }
1329 }
1330 $sql = rtrim($sql, ",");
1331 $sql .= ") " . $properties['attr'];
1332 $pdo->query($sql);
1333 }
1334 // Reset table attributes
1335 $pdo->query("ALTER TABLE `" . $table . "` " . $properties['attr'] . ";");
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001336 }
1337
1338 // Recreate SQL views
1339 foreach ($views as $view => $create) {
1340 $pdo->query("DROP VIEW IF EXISTS `" . $view . "`;");
1341 $pdo->query($create);
1342 }
1343
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001344 // Clear old app_passwd log entries
1345 $pdo->exec("DELETE FROM logs
1346 WHERE role != 'unauthenticated'
1347 AND JSON_EXTRACT(`call`, '$[0]') = 'app_passwd'
1348 AND JSON_EXTRACT(`call`, '$[1]') = 'edit'
1349 AND (JSON_CONTAINS_PATH(`call`, 'one', '$[2].password')
1350 OR JSON_CONTAINS_PATH(`call`, 'one', '$[2].password2'));");
1351
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001352 // Mitigate imapsync argument injection issue
1353 $pdo->query("UPDATE `imapsync` SET `custom_params` = ''
1354 WHERE `custom_params` LIKE '%pipemess%'
1355 OR custom_params LIKE '%skipmess%'
1356 OR custom_params LIKE '%delete2foldersonly%'
1357 OR custom_params LIKE '%delete2foldersbutnot%'
1358 OR custom_params LIKE '%regexflag%'
1359 OR custom_params LIKE '%pipemess%'
1360 OR custom_params LIKE '%regextrans2%'
1361 OR custom_params LIKE '%maxlinelengthcmd%';");
1362
1363 // Migrate webauthn tfa
1364 $stmt = $pdo->query("ALTER TABLE `tfa` MODIFY COLUMN `authmech` ENUM('yubi_otp', 'u2f', 'hotp', 'totp', 'webauthn')");
1365
1366 // Inject admin if not exists
1367 $stmt = $pdo->query("SELECT NULL FROM `admin`");
1368 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1369 if ($num_results == 0) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001370 $pdo->query("INSERT INTO `admin` (`username`, `password`, `superadmin`, `created`, `modified`, `active`)
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001371 VALUES ('admin', '{SSHA256}K8eVJ6YsZbQCfuJvSUbaQRLr0HPLz5rC9IAp0PAFl0tmNDBkMDc0NDAyOTAxN2Rk', 1, NOW(), NOW(), 1)");
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001372 $pdo->query("INSERT INTO `domain_admins` (`username`, `domain`, `created`, `active`)
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001373 SELECT `username`, 'ALL', NOW(), 1 FROM `admin`
1374 WHERE superadmin='1' AND `username` NOT IN (SELECT `username` FROM `domain_admins`);");
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001375 $pdo->query("DELETE FROM `admin` WHERE `username` NOT IN (SELECT `username` FROM `domain_admins`);");
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001376 }
1377 // Insert new DB schema version
1378 $pdo->query("REPLACE INTO `versions` (`application`, `version`) VALUES ('db_schema', '" . $db_version . "');");
1379
1380 // Fix dangling domain admins
1381 $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
1382 $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
1383
1384 // Migrate attributes
1385 // pushover
1386 $pdo->query("UPDATE `pushover` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
1387 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.evaluate_x_prio', \"0\") WHERE JSON_VALUE(`attributes`, '$.evaluate_x_prio') IS NULL;");
1388 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.only_x_prio', \"0\") WHERE JSON_VALUE(`attributes`, '$.only_x_prio') IS NULL;");
1389 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.sound', \"pushover\") WHERE JSON_VALUE(`attributes`, '$.sound') IS NULL;");
1390 // mailbox
1391 $pdo->query("UPDATE `mailbox` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
1392 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.passwd_update', \"0\") WHERE JSON_VALUE(`attributes`, '$.passwd_update') IS NULL;");
1393 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.relayhost', \"0\") WHERE JSON_VALUE(`attributes`, '$.relayhost') IS NULL;");
1394 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.force_pw_update', \"0\") WHERE JSON_VALUE(`attributes`, '$.force_pw_update') IS NULL;");
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001395 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.force_tfa', \"0\") WHERE JSON_VALUE(`attributes`, '$.force_tfa') IS NULL;");
1396 // admin attributes
1397 $pdo->query("UPDATE `admin` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
1398 $pdo->query("UPDATE `admin` SET `attributes` = JSON_SET(`attributes`, '$.force_tfa', \"0\") WHERE JSON_VALUE(`attributes`, '$.force_tfa') IS NULL;");
1399 $pdo->query("UPDATE `admin` SET `attributes` = JSON_SET(`attributes`, '$.force_pw_update', \"0\") WHERE JSON_VALUE(`attributes`, '$.force_pw_update') IS NULL;");
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001400 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.sieve_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.sieve_access') IS NULL;");
1401 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.sogo_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.sogo_access') IS NULL;");
1402 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.imap_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.imap_access') IS NULL;");
1403 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.pop3_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.pop3_access') IS NULL;");
1404 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.smtp_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.smtp_access') IS NULL;");
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001405 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.eas_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.eas_access') IS NULL;");
1406 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.dav_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.dav_access') IS NULL;");
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001407 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.mailbox_format', \"maildir:\") WHERE JSON_VALUE(`attributes`, '$.mailbox_format') IS NULL;");
1408 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', \"never\") WHERE JSON_VALUE(`attributes`, '$.quarantine_notification') IS NULL;");
1409 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', \"reject\") WHERE JSON_VALUE(`attributes`, '$.quarantine_category') IS NULL;");
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001410 foreach ($tls_options as $tls_user => $tls_options) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001411 $stmt = $pdo->prepare("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_enforce_in),
1412 `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_enforce_out)
1413 WHERE `username` = :username");
1414 $stmt->execute(array(':tls_enforce_in' => $tls_options['tls_enforce_in'], ':tls_enforce_out' => $tls_options['tls_enforce_out'], ':username' => $tls_user));
1415 }
1416 // Set tls_enforce_* if still missing (due to deleted attrs for example)
1417 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', \"1\") WHERE JSON_VALUE(`attributes`, '$.tls_enforce_out') IS NULL;");
1418 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', \"1\") WHERE JSON_VALUE(`attributes`, '$.tls_enforce_in') IS NULL;");
1419 // Fix ACL
1420 $pdo->query("INSERT INTO `user_acl` (`username`) SELECT `username` FROM `mailbox` WHERE `kind` = '' AND NOT EXISTS (SELECT `username` FROM `user_acl`);");
1421 $pdo->query("INSERT INTO `da_acl` (`username`) SELECT DISTINCT `username` FROM `domain_admins` WHERE `username` != 'admin' AND NOT EXISTS (SELECT `username` FROM `da_acl`);");
1422 // Fix domain_admins
1423 $pdo->query("DELETE FROM `domain_admins` WHERE `domain` = 'ALL';");
1424
1425 // add default templates
1426 $default_domain_template = array(
1427 "template" => "Default",
1428 "type" => "domain",
1429 "attributes" => array(
1430 "tags" => array(),
1431 "max_num_aliases_for_domain" => 400,
1432 "max_num_mboxes_for_domain" => 10,
1433 "def_quota_for_mbox" => 3072 * 1048576,
1434 "max_quota_for_mbox" => 10240 * 1048576,
1435 "max_quota_for_domain" => 10240 * 1048576,
1436 "rl_frame" => "s",
1437 "rl_value" => "",
1438 "active" => 1,
1439 "gal" => 1,
1440 "backupmx" => 0,
1441 "relay_all_recipients" => 0,
1442 "relay_unknown_only" => 0,
1443 "dkim_selector" => "dkim",
1444 "key_size" => 2048,
1445 "max_quota_for_domain" => 10240 * 1048576,
1446 )
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001447 );
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001448 $default_mailbox_template = array(
1449 "template" => "Default",
1450 "type" => "mailbox",
1451 "attributes" => array(
1452 "tags" => array(),
1453 "quota" => 0,
1454 "quarantine_notification" => strval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['quarantine_notification']),
1455 "quarantine_category" => strval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['quarantine_category']),
1456 "rl_frame" => "s",
1457 "rl_value" => "",
1458 "force_pw_update" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['force_pw_update']),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001459 "force_tfa" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['force_tfa']),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001460 "sogo_access" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['sogo_access']),
1461 "active" => 1,
1462 "tls_enforce_in" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['tls_enforce_in']),
1463 "tls_enforce_out" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['tls_enforce_out']),
1464 "imap_access" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['imap_access']),
1465 "pop3_access" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['pop3_access']),
1466 "smtp_access" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['smtp_access']),
1467 "sieve_access" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['sieve_access']),
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001468 "eas_access" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['eas_access']),
1469 "dav_access" => intval($GLOBALS['MAILBOX_DEFAULT_ATTRIBUTES']['dav_access']),
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001470 "acl_spam_alias" => 1,
1471 "acl_tls_policy" => 1,
1472 "acl_spam_score" => 1,
1473 "acl_spam_policy" => 1,
1474 "acl_delimiter_action" => 1,
1475 "acl_syncjobs" => 0,
1476 "acl_eas_reset" => 1,
1477 "acl_sogo_profile_reset" => 0,
1478 "acl_pushover" => 1,
1479 "acl_quarantine" => 1,
1480 "acl_quarantine_attachments" => 1,
1481 "acl_quarantine_notification" => 1,
1482 "acl_quarantine_category" => 1,
1483 "acl_app_passwds" => 1,
1484 )
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001485 );
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001486 $stmt = $pdo->prepare("SELECT id FROM `templates` WHERE `type` = :type AND `template` = :template");
1487 $stmt->execute(array(
1488 ":type" => "domain",
1489 ":template" => $default_domain_template["template"]
1490 ));
1491 $row = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001492 if (empty($row)) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001493 $stmt = $pdo->prepare("INSERT INTO `templates` (`type`, `template`, `attributes`)
1494 VALUES (:type, :template, :attributes)");
1495 $stmt->execute(array(
1496 ":type" => "domain",
1497 ":template" => $default_domain_template["template"],
1498 ":attributes" => json_encode($default_domain_template["attributes"])
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001499 ));
1500 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001501 $stmt = $pdo->prepare("SELECT id FROM `templates` WHERE `type` = :type AND `template` = :template");
1502 $stmt->execute(array(
1503 ":type" => "mailbox",
1504 ":template" => $default_mailbox_template["template"]
1505 ));
1506 $row = $stmt->fetch(PDO::FETCH_ASSOC);
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001507 if (empty($row)) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001508 $stmt = $pdo->prepare("INSERT INTO `templates` (`type`, `template`, `attributes`)
1509 VALUES (:type, :template, :attributes)");
1510 $stmt->execute(array(
1511 ":type" => "mailbox",
1512 ":template" => $default_mailbox_template["template"],
1513 ":attributes" => json_encode($default_mailbox_template["attributes"])
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001514 ));
1515 }
1516
1517 // remove old sogo views and triggers
1518 $pdo->query("DROP TRIGGER IF EXISTS sogo_update_password");
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001519
1520 if (php_sapi_name() == "cli") {
1521 echo "DB initialization completed" . PHP_EOL;
1522 } else {
1523 $_SESSION['return'][] = array(
1524 'type' => 'success',
1525 'log' => array(__FUNCTION__),
1526 'msg' => 'db_init_complete'
1527 );
1528 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001529
1530 // fill quarantine.qhash
1531 $pdo->query("UPDATE `quarantine` SET `qhash` = SHA2(CONCAT(`id`, `qid`), 256) WHERE ISNULL(`qhash`)");
1532
1533 } catch (PDOException $e) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001534 if (php_sapi_name() == "cli") {
1535 echo "DB initialization failed: " . print_r($e, true) . PHP_EOL;
1536 } else {
1537 $_SESSION['return'][] = array(
1538 'type' => 'danger',
1539 'log' => array(__FUNCTION__),
1540 'msg' => array('mysql_error', $e)
1541 );
1542 }
1543 }
1544}
1545if (php_sapi_name() == "cli") {
1546 include '/web/inc/vars.inc.php';
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001547 include '/web/inc/functions.inc.php';
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001548 include '/web/inc/functions.docker.inc.php';
1549 // $now = new DateTime();
1550 // $mins = $now->getOffset() / 60;
1551 // $sgn = ($mins < 0 ? -1 : 1);
1552 // $mins = abs($mins);
1553 // $hrs = floor($mins / 60);
1554 // $mins -= $hrs * 60;
1555 // $offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
1556 $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
1557 $opt = [
1558 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
1559 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
1560 PDO::ATTR_EMULATE_PREPARES => false,
1561 //PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '" . $offset . "', group_concat_max_len = 3423543543;",
1562 ];
1563 $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
1564 $stmt = $pdo->query("SELECT COUNT('OK') AS OK_C FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sogo_view' OR TABLE_NAME = '_sogo_static_view';");
1565 $res = $stmt->fetch(PDO::FETCH_ASSOC);
1566 if (intval($res['OK_C']) === 2) {
1567 // Be more precise when replacing into _sogo_static_view, col orders may change
1568 try {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001569 update_sogo_static_view();
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001570 echo "Fixed _sogo_static_view" . PHP_EOL;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001571 } catch (Exception $e) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001572 // Dunno
1573 }
1574 }
1575 try {
1576 $m = new Memcached();
1577 $m->addServer('memcached', 11211);
1578 $m->flush();
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02001579 echo "Cleaned up memcached" . PHP_EOL;
1580 } catch (Exception $e) {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +01001581 // Dunno
1582 }
1583 init_db_schema();
1584}