kividb 1.2.1

A fast RedisĀ®-compatible in-memory store written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
use ahash::AHashMap;
use parking_lot::RwLock;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

/// Count of connected clients whose authenticated user has a non-wildcard
/// key pattern. When this is zero the key check in execute_command is a
/// single Relaxed load and a branch — effectively free.
pub static RESTRICTED_KEY_CLIENTS: std::sync::atomic::AtomicUsize =
    std::sync::atomic::AtomicUsize::new(0);

/// Count of connected clients whose authenticated user's `commands` rule
/// string is not the trivial "+@all" (i.e. some command may be denied), or
/// who may not use every pub/sub channel.
/// Same free-when-zero trick as `RESTRICTED_KEY_CLIENTS`.
pub static RESTRICTED_CMD_CLIENTS: std::sync::atomic::AtomicUsize =
    std::sync::atomic::AtomicUsize::new(0);

/// Bumped on every change to the ACL. A connection compares it with the value
/// it last saw before running a command, and re-reads its user's rules when
/// they differ; see [`resync_connection`].
///
/// The two counters above are worked out when a connection authenticates. On
/// their own that meant a rule changed *afterwards* never reached a connection
/// that was already open: revoke a command or a key pattern from a user, and
/// every client already logged in as that user kept the access — indefinitely,
/// on the fast path, where the counters said nobody needed checking. The same
/// gap let a transaction queued before a revocation run after it.
pub static ACL_EPOCH: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Record that the ACL changed. Call after any mutation of the user table.
pub fn acl_changed() {
    ACL_EPOCH.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}

/// Bring a connection's restriction flags (and the global counters they
/// feed) into line with the ACL as it now stands.
///
/// Returns `false` if the connection's user no longer exists; the connection
/// should then be closed, as Redis does when a user is deleted.
pub fn resync_connection(
    store: &AclStore,
    user: Option<&str>,
    epoch_seen: &mut u64,
    key_restricted: &mut bool,
    cmd_restricted: &mut bool,
) -> bool {
    *epoch_seen = ACL_EPOCH.load(std::sync::atomic::Ordering::Relaxed);
    let Some(name) = user else {
        // Not authenticated: nothing to keep in step.
        return true;
    };
    match store.read().get(name) {
        Some(user) => {
            track_restriction_counters(user, key_restricted, cmd_restricted);
            true
        }
        None => false,
    }
}

// ── ACL user ──────────────────────────────────────────────────────────────────

/// One key pattern of a user: `~pattern` (read and write), `%R~pattern`,
/// `%W~pattern` or `%RW~pattern`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KeyPattern {
    pub pattern: String,
    pub read: bool,
    pub write: bool,
}

impl KeyPattern {
    /// `~pattern`: both reading and writing.
    pub fn read_write(pattern: &str) -> Self {
        Self {
            pattern: pattern.to_string(),
            read: true,
            write: true,
        }
    }

    fn is_all(&self) -> bool {
        self.pattern == "*" && self.read && self.write
    }
}

impl std::fmt::Display for KeyPattern {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match (self.read, self.write) {
            (true, false) => write!(f, "%R~{}", self.pattern),
            (false, true) => write!(f, "%W~{}", self.pattern),
            _ => write!(f, "~{}", self.pattern),
        }
    }
}

/// A selector: a second, independent set of permissions on a user
/// (`(~cache:* +get)`). A command is allowed if the user's own rules allow it
/// *or* any one selector allows it — command, keys and channels together.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Selector {
    pub commands: String,
    pub keys: Vec<KeyPattern>,
    pub channels: Vec<String>,
}

impl Default for Selector {
    fn default() -> Self {
        Self {
            commands: "-@all".into(),
            keys: Vec::new(),
            channels: Vec::new(),
        }
    }
}

impl std::fmt::Display for Selector {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "({})",
            describe_permissions(&self.keys, &self.channels, &self.commands)
        )
    }
}

#[derive(Clone, Debug)]
pub struct AclUser {
    pub name: String,
    /// true = user is active (on), false = disabled (off)
    pub enabled: bool,
    /// true = no password required (nopass flag)
    pub nopass: bool,
    /// Hashed passwords (SHA-256 hex). Empty, without `nopass`, means the user
    /// cannot authenticate at all.
    pub passwords: Vec<String>,
    /// Command permissions: "+@all" / "-@all" / "+cmd" / "-cmd", applied left
    /// to right.
    pub commands: String,
    /// Key patterns. A key is allowed if it matches any of them; the pattern
    /// `*` is "all keys", and no patterns is "no keys".
    pub keys: Vec<KeyPattern>,
    /// Pub/sub channel patterns, held without their `&`. `*` is every
    /// channel, none is no channel.
    pub channels: Vec<String>,
    /// Further permission sets; see [`Selector`].
    pub selectors: Vec<Selector>,
}

impl AclUser {
    /// A user as `ACL SETUSER` first creates it, and as a line of an ACL file
    /// starts out: off, no password, no keys, no channels, no commands.
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            enabled: false,
            nopass: false,
            passwords: vec![],
            commands: "-@all".into(),
            keys: Vec::new(),
            channels: Vec::new(),
            selectors: Vec::new(),
        }
    }

    /// The default user — full permissions; `nopass` unless `requirepass` is
    /// set.
    pub fn default_user(requirepass: &str) -> Self {
        let (nopass, passwords) = if requirepass.is_empty() {
            (true, vec![])
        } else {
            // Store SHA-256 of the password
            (false, vec![sha256_hex(requirepass)])
        };
        Self {
            name: "default".into(),
            enabled: true,
            nopass,
            passwords,
            commands: "+@all".into(),
            keys: vec![KeyPattern::read_write("*")],
            channels: vec!["*".into()],
            selectors: Vec::new(),
        }
    }

    /// May use every key (`~*` / `allkeys`).
    pub fn all_keys(&self) -> bool {
        all_keys(&self.keys)
    }

    /// May read every key (`~*`, `allkeys` or `%R~*`).
    pub fn reads_all_keys(&self) -> bool {
        self.keys.iter().any(|k| k.pattern == "*" && k.read)
    }

    /// May use every pub/sub channel (`&*` / `allchannels`).
    pub fn all_channels(&self) -> bool {
        all_channels(&self.channels)
    }

    /// Whether `password` authenticates this user.
    /// Whether `password`, as the client sent it (any bytes, as in Redis),
    /// logs in as this user.
    pub fn accepts(&self, password: impl AsRef<[u8]>) -> bool {
        self.enabled && (self.nopass || self.passwords.contains(&sha256_hex(password)))
    }

    /// The key patterns as `ACL GETUSER` prints them: `~app:* %R~cache:*`.
    pub fn keys_string(&self) -> String {
        join(self.keys.iter())
    }

    /// The channel patterns as `ACL GETUSER` prints them: `&news.*`.
    pub fn channels_string(&self) -> String {
        join(self.channels.iter().map(|c| format!("&{c}")))
    }

    /// Format as an ACL file / `ACL LIST` line. What this writes,
    /// [`parse_acl_file`] reads back to the same user.
    pub fn to_acl_string(&self) -> String {
        let mut out = format!(
            "user {} {}",
            self.name,
            if self.enabled { "on" } else { "off" }
        );
        if self.nopass {
            out.push_str(" nopass");
        }
        // A user with neither `nopass` nor a password cannot log in. It used
        // to be written as `nopass` — which, read back, let anyone in.
        for hash in &self.passwords {
            out.push_str(" #");
            out.push_str(hash);
        }
        out.push(' ');
        out.push_str(&describe_permissions(
            &self.keys,
            &self.channels,
            &self.commands,
        ));
        for selector in &self.selectors {
            out.push(' ');
            out.push_str(&selector.to_string());
        }
        out
    }
}

fn join<T: std::fmt::Display>(items: impl Iterator<Item = T>) -> String {
    items
        .map(|item| item.to_string())
        .collect::<Vec<_>>()
        .join(" ")
}

fn all_keys(keys: &[KeyPattern]) -> bool {
    keys.iter().any(KeyPattern::is_all)
}

fn all_channels(channels: &[String]) -> bool {
    channels.iter().any(|c| c == "*")
}

/// `~a ~b resetchannels &c +@all` — keys, channels, commands, in the order
/// Redis prints them and in a form that can be applied to an empty user.
fn describe_permissions(keys: &[KeyPattern], channels: &[String], commands: &str) -> String {
    let keys = if keys.is_empty() {
        "resetkeys".to_string()
    } else {
        join(keys.iter())
    };
    let channels = if channels.is_empty() {
        "resetchannels".to_string()
    } else {
        join(channels.iter().map(|c| format!("&{c}")))
    };
    format!("{keys} {channels} {commands}")
}

// ── SHA-256 helper ─────────────────────────────────────────────────────────────

pub fn sha256_hex(input: impl AsRef<[u8]>) -> String {
    use sha2::{Digest, Sha256};
    let hash = Sha256::digest(input.as_ref());
    hash.iter().map(|b| format!("{:02x}", b)).collect()
}

// ── ACL store ─────────────────────────────────────────────────────────────────

pub type AclStore = Arc<RwLock<AHashMap<String, AclUser>>>;

pub fn new_acl_store(requirepass: &str) -> AclStore {
    let mut map = AHashMap::new();
    map.insert("default".into(), AclUser::default_user(requirepass));
    Arc::new(RwLock::new(map))
}

/// Whether a connection that has not sent `AUTH` is the `default` user.
///
/// It is when — and only when — `default` is enabled and needs no password.
/// This is read from the user table, where `--requirepass`, an ACL file,
/// `ACL SETUSER default …` and `CONFIG SET requirepass` all end up. It used
/// to be read from `--requirepass` alone, so a password given to `default`
/// any other way was not asked for: every connection started out logged in.
pub fn default_user_is_open(store: &AclStore) -> bool {
    store
        .read()
        .get("default")
        .is_some_and(|user| user.enabled && user.nopass)
}

/// Redis's reply to `AUTH <password>` when the default user needs none.
pub const AUTH_WITHOUT_PASSWORD: &str = "-ERR AUTH <password> called without any password \
    configured for the default user. Are you sure your configuration is correct?\r\n";

/// The user `username` authenticates as with `password`, if it does.
pub fn authenticate(
    store: &AclStore,
    username: &str,
    password: impl AsRef<[u8]>,
) -> Option<AclUser> {
    let password = password.as_ref();
    let user = store
        .read()
        .get(username)
        .filter(|user| user.accepts(password))
        .cloned();
    if user.is_none() {
        log_verbose!(
            "Authentication failed for user '{username}': unknown user, wrong password, or \
             user disabled. ACL LOG has the client's address"
        );
    }
    user
}

// ── ACL log ───────────────────────────────────────────────────────────────────

#[derive(Clone, Debug)]
pub struct AclLogEntry {
    pub id: u64,
    pub reason: String,              // "auth" or "command" or "key" or "channel"
    pub context: String,             // "toplevel" or "multi" or "lua"
    pub object: String,              // the offending command/key/channel name
    pub username: String,            // which user triggered it
    pub age_seconds: f64,            // seconds since the event (computed at query time)
    pub client_info: String,         // addr:port
    pub timestamp_created: u64,      // Unix ms when created
    pub timestamp_last_updated: u64, // Unix ms of the last event merged into it
    pub count: u64,                  // how many times this event was seen
}

pub struct AclLog {
    entries: RwLock<std::collections::VecDeque<AclLogEntry>>,
    next_id: std::sync::atomic::AtomicU64,
    /// `acllog-max-len`: 0 keeps nothing.
    max_len: std::sync::atomic::AtomicUsize,
}

impl AclLog {
    pub fn new() -> Self {
        Self {
            entries: RwLock::new(std::collections::VecDeque::new()),
            // As Redis's: the first entry is 0.
            next_id: std::sync::atomic::AtomicU64::new(0),
            max_len: std::sync::atomic::AtomicUsize::new(128),
        }
    }

    /// Add `entry`, as Redis's `addACLLogEntry` does: if one of the ten
    /// newest entries has the same reason, context, object and user and was
    /// last updated within 60 seconds, that entry is counted once more,
    /// takes this one's client and time, and becomes the newest; otherwise
    /// `entry` is added, with an id of its own.
    pub fn push(&self, mut entry: AclLogEntry) {
        const GROUPING_MS: u64 = 60_000;
        let max_len = self.max_len();
        let mut log = self.entries.write();
        if max_len == 0 {
            log.clear();
            return;
        }
        let newest = log.len();
        let matched = (0..newest).rev().take(10).find(|&i| {
            let e = &log[i];
            e.reason == entry.reason
                && e.context == entry.context
                && e.timestamp_last_updated
                    .abs_diff(entry.timestamp_last_updated)
                    <= GROUPING_MS
                && e.object == entry.object
                && e.username == entry.username
        });
        if let Some(i) = matched {
            let mut e = log.remove(i).expect("an index found in the log");
            e.count += 1;
            e.client_info = entry.client_info;
            e.timestamp_last_updated = entry.timestamp_last_updated;
            log.push_back(e);
            return;
        }
        entry.id = self.next_id();
        log.push_back(entry);
        while log.len() > max_len {
            log.pop_front();
        }
    }

    /// Record a refusal: a command, key or channel the user's rules refuse,
    /// or a failed AUTH. The context is the one the connection is in (see
    /// [`log_context`]): `toplevel`, `multi` or `lua`.
    pub fn record(&self, reason: &str, object: &str, username: &str, client_info: &str) {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;
        self.push(AclLogEntry {
            id: 0,
            reason: reason.into(),
            context: LOG_CONTEXT.with(|context| context.get()).into(),
            object: object.into(),
            username: username.into(),
            age_seconds: 0.0,
            client_info: client_info.into(),
            timestamp_created: now,
            timestamp_last_updated: now,
            count: 1,
        });
    }

    pub fn get(&self, count: usize) -> Vec<AclLogEntry> {
        let log = self.entries.read();
        let now_ms = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;
        log.iter()
            .rev()
            .take(count)
            .map(|e| {
                let mut e = e.clone();
                e.age_seconds = (now_ms.saturating_sub(e.timestamp_last_updated)) as f64 / 1000.0;
                e
            })
            .collect()
    }

    /// `acllog-max-len`.
    pub fn max_len(&self) -> usize {
        self.max_len.load(std::sync::atomic::Ordering::Relaxed)
    }

    /// `CONFIG SET acllog-max-len`: taken at the next entry, as in Redis.
    pub fn set_max_len(&self, max_len: usize) {
        self.max_len
            .store(max_len, std::sync::atomic::Ordering::Relaxed);
    }

    pub fn reset(&self) {
        self.entries.write().clear();
    }

    pub fn next_id(&self) -> u64 {
        self.next_id
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
    }
}

impl Default for AclLog {
    fn default() -> Self {
        Self::new()
    }
}

pub type SharedAclLog = Arc<AclLog>;

thread_local! {
    static LOG_CONTEXT: std::cell::Cell<&'static str> = const { std::cell::Cell::new("toplevel") };
}

/// Until it is dropped, what this thread writes to the ACL log is in
/// `context` (`multi` inside a transaction, `lua` inside a script), as
/// Redis's entries are. Set around code that does not await.
pub fn log_context(context: &'static str) -> LogContext {
    LogContext(
        LOG_CONTEXT.with(|current| current.replace(context)),
        std::marker::PhantomData,
    )
}

/// See [`log_context`]: the context before, put back on drop. Not `Send`,
/// so that a task cannot hold it across an await on the tokio loop.
pub struct LogContext(&'static str, std::marker::PhantomData<*const ()>);

impl Drop for LogContext {
    fn drop(&mut self) {
        LOG_CONTEXT.with(|current| current.set(self.0));
    }
}

/// The client in an ACL log entry, as `CLIENT INFO` describes it (Redis
/// logs `catClientInfoString`); its address if it is not registered.
pub fn log_client_info(
    registry: &crate::client_registry::ClientRegistry,
    client_id: u64,
    address: &str,
) -> String {
    registry.get(client_id).map_or_else(
        || address.to_string(),
        |info| info.to_list_line().trim_end().to_string(),
    )
}

pub fn new_acl_log() -> SharedAclLog {
    Arc::new(AclLog::new())
}

// ── ACL file ──────────────────────────────────────────────────────────────────

use std::io::Write;

/// Read the users an ACL file defines.
///
/// Each `user <name> <rule>…` line defines its user from nothing: a line does
/// not add to a user of the same name that already exists in memory. `default`
/// is the one user that exists without a line; if the file has no line for it,
/// it is the default user `requirepass` describes.
///
/// The file is taken whole or not at all: any line that cannot be understood
/// is an error, reported with its line number, and no users are returned.
pub fn parse_acl_file(
    contents: &str,
    requirepass: &str,
) -> Result<AHashMap<String, AclUser>, String> {
    let mut users: AHashMap<String, AclUser> = AHashMap::new();
    let mut errors: Vec<String> = Vec::new();

    for (index, line) in contents.lines().enumerate() {
        let number = index + 1;
        let line = line.trim();
        // Skip comments and blank lines
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let mut tokens = line.split_whitespace();
        if tokens.next() != Some("user") {
            errors.push(format!(
                "line {number}: a line should start with the 'user' keyword"
            ));
            continue;
        }
        let Some(name) = tokens.next() else {
            errors.push(format!("line {number}: 'user' must be followed by a name"));
            continue;
        };
        if users.contains_key(name) {
            errors.push(format!("line {number}: duplicate user '{name}'"));
            continue;
        }
        let rules = match merge_selector_args(tokens) {
            Ok(rules) => rules,
            Err(e) => {
                errors.push(format!("line {number}: {e}"));
                continue;
            }
        };
        let mut user = AclUser::new(name);
        let mut ok = true;
        for rule in &rules {
            if let Err(e) = apply_rule(&mut user, rule) {
                errors.push(format!("line {number}: error in rule '{rule}': {e}"));
                ok = false;
                break;
            }
        }
        if ok {
            users.insert(name.to_string(), user);
        }
    }

    if !errors.is_empty() {
        return Err(errors.join("; "));
    }
    users
        .entry("default".to_string())
        .or_insert_with(|| AclUser::default_user(requirepass));
    Ok(users)
}

/// Replace the user table with the users the ACL file at `path` defines.
///
/// All or nothing: if the file cannot be read, or any line of it is wrong,
/// the users in memory are left exactly as they were. Otherwise the file is
/// what the users now are — a password that is no longer in it no longer
/// works, a user that is no longer in it no longer exists. (This used to
/// apply each line on top of the user already in memory, so a changed
/// password was *added* and a removed user stayed.)
pub fn load_acl_file(path: &str, store: &AclStore, requirepass: &str) -> Result<(), String> {
    let contents = std::fs::read_to_string(path).map_err(|e| format!("opening '{path}': {e}"))?;
    let users = parse_acl_file(&contents, requirepass).map_err(|e| format!("{path}: {e}"))?;
    *store.write() = users;
    acl_changed();
    Ok(())
}

/// Save all users to the ACL file atomically (write to .tmp then rename).
pub fn save_acl_file(path: &str, store: &AclStore) -> std::io::Result<()> {
    let tmp = format!("{path}.tmp");
    let mut file = std::fs::File::create(&tmp)?;

    let map = store.read();
    // Write default user first, then others alphabetically
    let mut names: Vec<&String> = map.keys().collect();
    names.sort_by_key(|n| (n.as_str() != "default", n.as_str()));

    for name in names {
        if let Some(user) = map.get(name) {
            writeln!(file, "{}", user.to_acl_string())?;
        }
    }
    file.flush()?;
    drop(file);

    // Atomic rename — safe on POSIX, best-effort on Windows
    std::fs::rename(&tmp, path)?;
    Ok(())
}

// ── Rules ─────────────────────────────────────────────────────────────────────

/// Join the pieces of a selector back together. A selector may arrive as one
/// argument, `(~k +get)`, or — typed without quotes, or read from a file
/// split on spaces — as several: `(~k`, `+get)`.
pub fn merge_selector_args<'a>(args: impl Iterator<Item = &'a str>) -> Result<Vec<String>, String> {
    let mut out: Vec<String> = Vec::new();
    let mut open: Option<String> = None;
    for arg in args {
        match open.as_mut() {
            Some(selector) => {
                selector.push(' ');
                selector.push_str(arg);
                if arg.ends_with(')') {
                    out.push(open.take().unwrap_or_default());
                }
            }
            None if arg.starts_with('(') && !arg.ends_with(')') => open = Some(arg.to_string()),
            None => out.push(arg.to_string()),
        }
    }
    match open {
        Some(_) => Err("Unmatched parenthesis in acl selector starting at '('.".to_string()),
        None => Ok(out),
    }
}

const SYNTAX_ERROR: &str = "Syntax error";
const PATTERN_AFTER_ALL: &str = "Adding a pattern after the * pattern (or the 'allkeys' flag) is \
     not valid and does not have any effect. Try 'resetkeys' to start with an empty list of \
     patterns";
const CHANNEL_AFTER_ALL: &str = "Adding a pattern after the * pattern (or the 'allchannels' flag) \
     is not valid and does not have any effect. Try 'resetchannels' to start with an empty list \
     of channels";

/// The rules that keys, channels and commands share between a user and a
/// selector. `Ok(false)` means the rule is not one of them.
fn apply_permission_rule(
    commands: &mut String,
    keys: &mut Vec<KeyPattern>,
    channels: &mut Vec<String>,
    rule: &str,
) -> Result<bool, String> {
    let add_key = |keys: &mut Vec<KeyPattern>, pattern: KeyPattern| -> Result<(), String> {
        if pattern.pattern.is_empty() {
            return Err(SYNTAX_ERROR.to_string());
        }
        if pattern.is_all() {
            // `~*` replaces whatever was there.
            keys.clear();
            keys.push(pattern);
            return Ok(());
        }
        if all_keys(keys) {
            return Err(PATTERN_AFTER_ALL.to_string());
        }
        if !keys.contains(&pattern) {
            keys.push(pattern);
        }
        Ok(())
    };

    match rule {
        "allkeys" | "~*" => add_key(keys, KeyPattern::read_write("*"))?,
        "resetkeys" => keys.clear(),
        "allchannels" | "&*" => {
            channels.clear();
            channels.push("*".to_string());
        }
        "resetchannels" => channels.clear(),
        "allcommands" | "+@all" => *commands = "+@all".into(),
        "nocommands" | "-@all" => *commands = "-@all".into(),
        // Sugar for a read-only user: block every data-mutating command
        // (see the `write` ACL category / `replication::is_write_command`).
        // Does not itself restrict admin/server commands (CONFIG, SHUTDOWN,
        // ...) — combine with explicit `-cmd` rules for full lockdown.
        "readonly" => push_command_rule(commands, "-@write"),
        r if r.starts_with('~') => add_key(keys, KeyPattern::read_write(&r[1..]))?,
        r if r.starts_with('%') => {
            let Some((flags, pattern)) = r[1..].split_once('~') else {
                return Err(SYNTAX_ERROR.to_string());
            };
            let (mut read, mut write) = (false, false);
            for flag in flags.chars() {
                match flag {
                    'R' | 'r' => read = true,
                    'W' | 'w' => write = true,
                    _ => return Err(SYNTAX_ERROR.to_string()),
                }
            }
            if !read && !write {
                return Err(SYNTAX_ERROR.to_string());
            }
            add_key(
                keys,
                KeyPattern {
                    pattern: pattern.to_string(),
                    read,
                    write,
                },
            )?;
        }
        r if r.starts_with('&') => {
            let pattern = &r[1..];
            if pattern.is_empty() {
                return Err(SYNTAX_ERROR.to_string());
            }
            if all_channels(channels) {
                return Err(CHANNEL_AFTER_ALL.to_string());
            }
            if !channels.iter().any(|c| c == pattern) {
                channels.push(pattern.to_string());
            }
        }
        r if r.starts_with('+') || r.starts_with('-') => {
            check_command_rule(r)?;
            push_command_rule(commands, r);
        }
        _ => return Ok(false),
    }
    Ok(true)
}

/// Append a `+…` / `-…` rule, unless it could not change anything.
fn push_command_rule(commands: &mut String, rule: &str) {
    if !(commands == "+@all" && rule.starts_with('+')
        || commands == "-@all" && rule.starts_with('-'))
    {
        commands.push(' ');
        commands.push_str(rule);
    }
}

/// A `+…` / `-…` rule must name a category, a command or a subcommand that
/// exists. One that does not used to be stored and never match — so a typo in
/// `-@dangerous` granted what it was written to deny.
fn check_command_rule(rule: &str) -> Result<(), String> {
    const UNKNOWN: &str = "Unknown command or category name in ACL";
    let name = &rule[1..];
    if let Some(category) = name.strip_prefix('@') {
        let category = category.to_ascii_lowercase();
        return if category == "all"
            || crate::commands::server::acl_cat_commands_opt(&category).is_some()
        {
            Ok(())
        } else {
            Err(UNKNOWN.to_string())
        };
    }
    let mut parts = name.split('|');
    let command = parts.next().unwrap_or_default().to_ascii_uppercase();
    if command.is_empty() || !crate::commands::server::is_registered_command(&command) {
        return Err(UNKNOWN.to_string());
    }
    match (parts.next(), parts.next()) {
        (None, _) => Ok(()),
        (Some(sub), None) if !sub.is_empty() => {
            if CONTAINER_COMMANDS.contains(&command.as_str()) {
                Ok(())
            } else {
                // `+select|0`: allowing a command for one first argument only.
                Err("Allowing first-arg of a subcommand is not supported".to_string())
            }
        }
        _ => Err(SYNTAX_ERROR.to_string()),
    }
}

fn check_password_hash(hash: &str) -> Result<(), String> {
    if hash.len() == 64
        && hash
            .bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
    {
        Ok(())
    } else {
        Err(
            "The password hash must be exactly 64 characters and contain only lowercase \
             hexadecimal characters"
                .to_string(),
        )
    }
}

/// Parse the inside of `(…)` into a selector.
fn parse_selector(rule: &str) -> Result<Selector, String> {
    let inner = rule
        .strip_prefix('(')
        .and_then(|r| r.strip_suffix(')'))
        .ok_or_else(|| SYNTAX_ERROR.to_string())?;
    let mut selector = Selector::default();
    for part in inner.split_whitespace() {
        let known = apply_permission_rule(
            &mut selector.commands,
            &mut selector.keys,
            &mut selector.channels,
            part,
        )?;
        if !known {
            return Err(SYNTAX_ERROR.to_string());
        }
    }
    Ok(selector)
}

/// Apply a single rule token to a user — shared between SETUSER and the ACL
/// file, so both read rules the same way.
///
/// `Err` is the reason the rule was refused, in Redis's words. A rule that is
/// not understood is an error: it used to be skipped, which turned a
/// misspelt restriction into no restriction.
pub fn apply_rule(user: &mut AclUser, rule: &str) -> Result<(), String> {
    match rule {
        "on" => user.enabled = true,
        "off" => user.enabled = false,
        "nopass" => {
            user.nopass = true;
            user.passwords.clear();
        }
        "resetpass" => {
            user.nopass = false;
            user.passwords.clear();
        }
        "reset" => {
            let name = std::mem::take(&mut user.name);
            *user = AclUser::new(&name);
        }
        "clearselectors" => user.selectors.clear(),
        // Accepted for compatibility with Redis-written files: payloads are
        // always checked.
        "sanitize-payload" | "skip-sanitize-payload" => {}
        r if r.starts_with('(') => user.selectors.push(parse_selector(r)?),
        r if r.starts_with('>') => {
            let hash = sha256_hex(&r[1..]);
            if !user.passwords.contains(&hash) {
                user.passwords.push(hash);
            }
            user.nopass = false;
        }
        r if r.starts_with('<') => {
            let hash = sha256_hex(&r[1..]);
            remove_password(user, &hash)?;
        }
        r if r.starts_with('#') => {
            let hash = &r[1..];
            check_password_hash(hash)?;
            if !user.passwords.iter().any(|h| h == hash) {
                user.passwords.push(hash.to_string());
            }
            user.nopass = false;
        }
        r if r.starts_with('!') => {
            let hash = &r[1..];
            check_password_hash(hash)?;
            remove_password(user, hash)?;
        }
        r => {
            let known =
                apply_permission_rule(&mut user.commands, &mut user.keys, &mut user.channels, r)?;
            if !known {
                return Err(SYNTAX_ERROR.to_string());
            }
        }
    }
    Ok(())
}

fn remove_password(user: &mut AclUser, hash: &str) -> Result<(), String> {
    let before = user.passwords.len();
    user.passwords.retain(|h| h != hash);
    if user.passwords.len() == before {
        return Err(
            "The password you are trying to remove from the user does not exist".to_string(),
        );
    }
    Ok(())
}

// ── Permission checks ─────────────────────────────────────────────────────────

/// Commands whose subcommands can be granted and denied one at a time, as
/// `+config|get` or by a category that lists `CLUSTER|RESET`.
const CONTAINER_COMMANDS: &[&str] = &[
    "ACL", "BACKUP", "CLIENT", "CLUSTER", "COMMAND", "CONFIG", "FUNCTION", "HIMPORT", "HOTKEYS",
    "LATENCY", "MEMORY", "MODULE", "OBJECT", "PUBSUB", "SCRIPT", "SLOWLOG", "XGROUP", "XINFO",
];

/// Whether `name` (upper case) is a command with subcommands.
///
/// Asked of every command that runs, for its statistics: a `match` the
/// compiler turns into a switch on the length, not a walk down the list
/// comparing strings. `CONTAINER_COMMANDS` is the same list, and a test
/// keeps the two the same.
#[inline]
pub fn has_subcommands(name: &str) -> bool {
    matches!(
        name,
        "ACL"
            | "BACKUP"
            | "CLIENT"
            | "CLUSTER"
            | "COMMAND"
            | "CONFIG"
            | "FUNCTION"
            | "HIMPORT"
            | "HOTKEYS"
            | "LATENCY"
            | "MEMORY"
            | "MODULE"
            | "OBJECT"
            | "PUBSUB"
            | "SCRIPT"
            | "SLOWLOG"
            | "XGROUP"
            | "XINFO"
    )
}

#[cfg(test)]
mod has_subcommands_tests {
    use super::{CONTAINER_COMMANDS, has_subcommands};

    #[test]
    fn the_match_and_the_list_agree() {
        for name in CONTAINER_COMMANDS {
            assert!(has_subcommands(name), "{name} is in the list");
        }
        for name in ["GET", "SET", "ACLX", "XINF", "", "CONFI"] {
            assert_eq!(has_subcommands(name), CONTAINER_COMMANDS.contains(&name));
        }
    }
}

/// The name to check a command's permission under: `CLUSTER|RESET` for a
/// command with subcommands, the plain name for the rest. `name` is the
/// upper-cased command name and `first_arg` the argument that follows it.
pub fn acl_command_name<'a>(name: &'a str, first_arg: Option<&[u8]>) -> std::borrow::Cow<'a, str> {
    match first_arg {
        Some(sub) if CONTAINER_COMMANDS.contains(&name) => {
            let sub = String::from_utf8_lossy(sub).to_ascii_uppercase();
            std::borrow::Cow::Owned(format!("{name}|{sub}"))
        }
        _ => std::borrow::Cow::Borrowed(name),
    }
}

/// Whether a list of command rules permits `cmd_name`. The rules are applied
/// left to right, starting from "nothing allowed":
///   "+@all"       → grant all
///   "-@all"       → deny all
///   "+@string"    → grant all string commands
///   "-@string"    → deny all string commands
///   "+get"        → grant GET specifically
///   "-set"        → deny SET specifically
fn rules_allow(commands: &str, cmd_name: &str, acl_cat: fn(&str) -> Vec<&'static str>) -> bool {
    let cmd_lower = cmd_name.to_lowercase();
    // The command itself, for a `command|subcommand` name.
    let base = cmd_lower.split('|').next().unwrap_or(&cmd_lower);
    let names = |c: &str| c.eq_ignore_ascii_case(&cmd_lower) || c.eq_ignore_ascii_case(base);
    // Start from deny-all, apply rules left to right
    let mut allowed = false;

    for rule in commands.split_whitespace() {
        if rule == "+@all" || rule == "allcommands" {
            allowed = true;
        } else if rule == "-@all" || rule == "nocommands" {
            allowed = false;
        } else if let Some(cat) = rule.strip_prefix("+@") {
            if acl_cat(&cat.to_ascii_lowercase()).iter().any(|c| names(c)) {
                allowed = true;
            }
        } else if let Some(cat) = rule.strip_prefix("-@") {
            if acl_cat(&cat.to_ascii_lowercase()).iter().any(|c| names(c)) {
                allowed = false;
            }
        } else if let Some(c) = rule.strip_prefix('+') {
            if names(c) {
                allowed = true;
            }
        } else if let Some(c) = rule.strip_prefix('-')
            && names(c)
        {
            allowed = false;
        }
    }

    allowed
}

/// Whether one of `patterns` lets `key` be used in the way asked.
fn key_matches(patterns: &[KeyPattern], key: &[u8], read: bool, write: bool) -> bool {
    patterns.iter().any(|p| {
        (!read || p.read)
            && (!write || p.write)
            && (p.pattern == "*"
                || crate::commands::helpers::glob_match_bytes(p.pattern.as_bytes(), key))
    })
}

/// Whether a user may read `key`, through its own patterns or a selector's.
/// For commands that go looking for keys themselves (`TS.QUERYLABELS`).
pub fn may_read_key(user: &AclUser, key: &[u8]) -> bool {
    user.enabled
        && (key_matches(&user.keys, key, true, false)
            || user
                .selectors
                .iter()
                .any(|s| key_matches(&s.keys, key, true, false)))
}

/// Why a command was refused.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Denied {
    /// The command itself (named as it should be reported: `config|set`).
    Command(String),
    Key(String),
    Channel(String),
}

impl Denied {
    /// The `reason` an ACL log entry gives for it.
    pub fn reason(&self) -> &'static str {
        match self {
            Denied::Command(_) => "command",
            Denied::Key(_) => "key",
            Denied::Channel(_) => "channel",
        }
    }

    /// The command, key or channel that was refused.
    pub fn object(&self) -> &str {
        match self {
            Denied::Command(s) | Denied::Key(s) | Denied::Channel(s) => s,
        }
    }

    /// The error a client is sent, without the leading `-`.
    pub fn message(&self, username: &str) -> String {
        match self {
            Denied::Command(command) => {
                format!("NOPERM User {username} has no permissions to run the '{command}' command")
            }
            Denied::Key(key) => {
                format!("NOPERM User {username} has no permissions to access the '{key}' key")
            }
            Denied::Channel(channel) => format!(
                "NOPERM User {username} has no permissions to access the '{channel}' channel"
            ),
        }
    }
}

/// Write commands that put data into a key without giving any of it back:
/// a `%W~` pattern is enough for them. Every other write command also reads
/// what it writes (`INCR` answers with the value, `LPOP` with the element),
/// and needs `%R~` as well.
const WRITE_ONLY_COMMANDS: &[&str] = &[
    "APPEND",
    "DEL",
    "EXPIRE",
    "EXPIREAT",
    "GEOADD",
    "HDEL",
    "HMSET",
    "HSET",
    "HSETNX",
    "JSON.DEL",
    "JSON.SET",
    "LINSERT",
    "LPUSH",
    "LPUSHX",
    "LREM",
    "LSET",
    "LTRIM",
    "MSET",
    "MSETEX",
    "MSETNX",
    "PERSIST",
    "PEXPIRE",
    "PEXPIREAT",
    "PFADD",
    "PSETEX",
    "RESTORE",
    "RPUSH",
    "RPUSHX",
    "SADD",
    "SETEX",
    "SETNX",
    "SETRANGE",
    "SREM",
    "TS.ADD",
    "TS.CREATE",
    "TS.MADD",
    "UNLINK",
    "XACK",
    "XADD",
    "XDEL",
    "XTRIM",
    "ZADD",
    "ZREM",
    "ZREMRANGEBYLEX",
    "ZREMRANGEBYRANK",
    "ZREMRANGEBYSCORE",
];

/// Commands whose first key is written and whose other keys are only read.
const DESTINATION_FIRST: &[&str] = &[
    "BITOP",
    "GEOSEARCHSTORE",
    "SDIFFSTORE",
    "SINTERSTORE",
    "SUNIONSTORE",
    "ZDIFFSTORE",
    "ZINTERSTORE",
    "ZRANGESTORE",
    "ZUNIONSTORE",
];

/// Commands that take from their first key and put into their second.
const SOURCE_THEN_DESTINATION: &[&str] = &[
    "BLMOVE",
    "BLMOVEM",
    "BRPOPLPUSH",
    "LMOVE",
    "LMOVEM",
    "RENAME",
    "RENAMENX",
    "RPOPLPUSH",
    "SMOVE",
];

/// What a command does to its `nth` key: (reads it, writes it).
fn key_access(name: &str, cmd: &[&[u8]], nth: usize) -> (bool, bool) {
    // Commands that write only with some arguments are not in
    // `is_write_command`, so they would be taken for pure reads below and the
    // key they write would pass a read-only (`%R~`) permission. GEORADIUS /
    // GEORADIUSBYMEMBER read their source key and write the destination of a
    // STORE / STOREDIST; MIGRATE reads a key and removes it from this node.
    match name {
        "GEORADIUS" | "GEORADIUSBYMEMBER" => {
            return if nth == 0 {
                (true, false)
            } else {
                (false, true)
            };
        }
        "MIGRATE" => return (true, true),
        _ => {}
    }
    if !crate::replication::is_write_command(name) {
        return (true, false);
    }
    if name == "SET" {
        // `SET key value GET` answers with the old value.
        let returns_old = cmd.iter().skip(3).any(|a| a.eq_ignore_ascii_case(b"GET"));
        return (returns_old, true);
    }
    // `SORT key ... STORE destination`, like `COPY`, reads the key and only
    // writes the destination (Redis: `RO access`, `OW update`).
    if name == "COPY" || name == "SORT" {
        return if nth == 0 {
            (true, false)
        } else {
            (false, true)
        };
    }
    if WRITE_ONLY_COMMANDS.contains(&name) {
        return (false, true);
    }
    if DESTINATION_FIRST.contains(&name) {
        return if nth == 0 {
            (false, true)
        } else {
            (true, false)
        };
    }
    if SOURCE_THEN_DESTINATION.contains(&name) && nth > 0 {
        return (false, true);
    }
    (true, true)
}

/// Commands that name channels, and how: (first channel argument, the
/// arguments are patterns).
fn channel_arguments(name: &str) -> Option<(std::ops::RangeFrom<usize>, bool, bool)> {
    // (arguments, only the first is a channel, they are patterns)
    match name {
        "PUBLISH" | "SPUBLISH" => Some((1.., true, false)),
        "SUBSCRIBE" | "SSUBSCRIBE" => Some((1.., false, false)),
        "PSUBSCRIBE" => Some((1.., false, true)),
        _ => None,
    }
}

/// One set of permissions — the user's own, or a selector's — against one
/// command.
fn permissions_allow(
    commands: &str,
    keys: &[KeyPattern],
    channels: &[String],
    name: &str,
    cmd: &[&[u8]],
) -> Result<(), Denied> {
    let acl_name = acl_command_name(name, cmd.get(1).copied());
    if !rules_allow(
        commands,
        &acl_name,
        crate::commands::server::acl_cat_commands_pub,
    ) {
        return Err(Denied::Command(acl_name.to_lowercase()));
    }

    if let Some((args, first_only, patterns)) = channel_arguments(name) {
        if !all_channels(channels) {
            let take = if first_only { 1 } else { usize::MAX };
            for arg in cmd.iter().skip(args.start).take(take) {
                // As the bytes they are: two channels that are not text are
                // not one U+FFFD.
                let channel: &[u8] = arg;
                let allowed = if patterns {
                    // A pattern subscription is allowed only for a pattern
                    // the user was given, letter for letter.
                    channels.iter().any(|c| c.as_bytes() == channel)
                } else {
                    channels
                        .iter()
                        .any(|c| crate::commands::helpers::glob_match_bytes(c.as_bytes(), channel))
                };
                if !allowed {
                    return Err(Denied::Channel(
                        String::from_utf8_lossy(channel).into_owned(),
                    ));
                }
            }
        }
        // The channel of SPUBLISH / SSUBSCRIBE is listed as a key for cluster
        // routing; it is a channel, and was checked as one.
        return Ok(());
    }

    if !all_keys(keys) {
        for (nth, pos) in crate::keyspec::key_positions(name, cmd).iter().enumerate() {
            let key = cmd[pos];
            let (read, write) = key_access(name, cmd, nth);
            if !key_matches(keys, key, read, write) {
                return Err(Denied::Key(String::from_utf8_lossy(key).into_owned()));
            }
        }
    }
    Ok(())
}

/// Whether `user` may run this command: the command, every key it names, and
/// every channel it names, under the user's own rules or under any one of its
/// selectors.
///
/// `name` is the upper-cased command name and `cmd` the whole command, name
/// included. The reason returned is the one the user's own rules give.
pub fn check_permission<A: AsRef<[u8]>>(
    user: &AclUser,
    name: &str,
    cmd: &[A],
) -> Result<(), Denied> {
    // `AUTH` and `HELLO` are how a connection becomes somebody: they are
    // not subject to what the user it is now may run, as in Redis (its
    // `CMD_NO_AUTH` commands). Checked, `HELLO 3 AUTH user pass` was refused
    // for a default user of `-@all +auth` before the `AUTH` in it was ever
    // looked at, and the clients that open with it could not connect.
    if matches!(name, "AUTH" | "HELLO") {
        return Ok(());
    }
    let cmd: Vec<&[u8]> = cmd.iter().map(AsRef::as_ref).collect();
    if !user.enabled {
        return Err(Denied::Command(
            acl_command_name(name, cmd.get(1).copied()).to_lowercase(),
        ));
    }
    let own = permissions_allow(&user.commands, &user.keys, &user.channels, name, &cmd);
    if own.is_ok() {
        return own;
    }
    for selector in &user.selectors {
        if permissions_allow(
            &selector.commands,
            &selector.keys,
            &selector.channels,
            name,
            &cmd,
        )
        .is_ok()
        {
            return Ok(());
        }
    }
    own
}

/// Updates the `RESTRICTED_KEY_CLIENTS`/`RESTRICTED_CMD_CLIENTS` counters and
/// the paired connection-local flags after a connection authenticates or
/// re-authenticates as `user`. Both server loops call this from every place
/// that can change `authenticated_user` (plain `AUTH` and `HELLO ... AUTH`),
/// so the counters — and therefore enforcement in `execute_command` — never
/// fall out of sync with who's actually connected.
pub fn track_restriction_counters(
    user: &AclUser,
    key_restricted: &mut bool,
    cmd_restricted: &mut bool,
) {
    use std::sync::atomic::Ordering::Relaxed;

    // A user with no key patterns at all may use no keys: that is the most
    // restricted a user can be, and it used to be counted as unrestricted.
    let new_key_restricted = !user.all_keys();
    if *key_restricted && !new_key_restricted {
        RESTRICTED_KEY_CLIENTS.fetch_sub(1, Relaxed);
    } else if !*key_restricted && new_key_restricted {
        RESTRICTED_KEY_CLIENTS.fetch_add(1, Relaxed);
    }
    *key_restricted = new_key_restricted;

    let new_cmd_restricted = user.commands != "+@all" || !user.all_channels() || !user.enabled;
    if *cmd_restricted && !new_cmd_restricted {
        RESTRICTED_CMD_CLIENTS.fetch_sub(1, Relaxed);
    } else if !*cmd_restricted && new_cmd_restricted {
        RESTRICTED_CMD_CLIENTS.fetch_add(1, Relaxed);
    }
    *cmd_restricted = new_cmd_restricted;
}

/// A connection stops being any user at all (`RESET` on a server whose
/// default user needs a password): give back whatever it held in the
/// restricted-client counters.
pub fn clear_restriction_counters(key_restricted: &mut bool, cmd_restricted: &mut bool) {
    use std::sync::atomic::Ordering::Relaxed;
    if std::mem::take(key_restricted) {
        RESTRICTED_KEY_CLIENTS.fetch_sub(1, Relaxed);
    }
    if std::mem::take(cmd_restricted) {
        RESTRICTED_CMD_CLIENTS.fetch_sub(1, Relaxed);
    }
}

#[cfg(test)]
mod tests {
    /// An entry as `AclLog::push` is given one, at `ms`.
    fn event(object: &str, client: &str, ms: u64) -> super::AclLogEntry {
        super::AclLogEntry {
            id: 0,
            reason: "command".into(),
            context: "toplevel".into(),
            object: object.into(),
            username: "u".into(),
            age_seconds: 0.0,
            client_info: client.into(),
            timestamp_created: ms,
            timestamp_last_updated: ms,
            count: 1,
        }
    }

    /// As Redis's `addACLLogEntry`: an event like one of the ten newest
    /// entries, within 60 s of its last update, is counted in it.
    #[test]
    fn the_acl_log_merges_as_redis() {
        let log = super::AclLog::new();
        log.push(event("a", "c1", 1_000));
        log.push(event("b", "c1", 2_000));
        // Like the older one: counted in it, which becomes the newest, keeps
        // its id and creation, and takes the new client and time.
        log.push(event("a", "c2", 3_000));
        let entries = log.get(usize::MAX);
        assert_eq!(entries.len(), 2);
        let a = &entries[0];
        assert_eq!((a.object.as_str(), a.count, a.id), ("a", 2, 0));
        assert_eq!(a.timestamp_created, 1_000);
        assert_eq!(a.timestamp_last_updated, 3_000);
        assert_eq!(a.client_info, "c2");
        assert_eq!((entries[1].object.as_str(), entries[1].id), ("b", 1));

        // More than 60 s after its last update: a new entry.
        log.push(event("a", "c3", 3_000 + 60_001));
        let entries = log.get(usize::MAX);
        assert_eq!(entries.len(), 3);
        assert_eq!((entries[0].count, entries[0].id), (1, 2));
        // Exactly 60 s: the same one.
        log.push(event("a", "c4", 3_000 + 60_001 + 60_000));
        assert_eq!(log.get(1)[0].count, 2);

        // Only the ten newest are looked at.
        let log = super::AclLog::new();
        log.push(event("old", "c", 0));
        for i in 0..10 {
            log.push(event(&format!("o{i}"), "c", 0));
        }
        log.push(event("old", "c", 0));
        let entries = log.get(usize::MAX);
        assert_eq!(entries.len(), 12);
        assert!(entries.iter().all(|e| e.count == 1));
        // Nine back, it is found.
        log.push(event("o2", "c", 0));
        let entries = log.get(usize::MAX);
        assert_eq!(entries.len(), 12);
        assert_eq!((entries[0].object.as_str(), entries[0].count), ("o2", 2));
    }

    /// `acllog-max-len`: the oldest go; 0 keeps nothing.
    #[test]
    fn the_acl_log_keeps_acllog_max_len_entries() {
        let log = super::AclLog::new();
        log.set_max_len(2);
        for object in ["a", "b", "c"] {
            log.push(event(object, "c", 0));
        }
        let objects: Vec<String> = log.get(usize::MAX).into_iter().map(|e| e.object).collect();
        assert_eq!(objects, ["c", "b"]);
        log.set_max_len(0);
        log.push(event("d", "c", 0));
        assert!(log.get(usize::MAX).is_empty());
    }

    use super::*;

    fn user(rules: &[&str]) -> AclUser {
        let mut user = AclUser::new("u");
        for rule in rules {
            apply_rule(&mut user, rule).unwrap_or_else(|e| panic!("{rule}: {e}"));
        }
        user
    }

    fn check(user: &AclUser, cmd: &[&str]) -> Result<(), Denied> {
        check_permission(user, &cmd[0].to_ascii_uppercase(), cmd)
    }

    #[test]
    fn a_user_holds_every_key_pattern_it_is_given() {
        let u = user(&["on", "nopass", "~app:*", "~session:*", "+@all"]);
        assert_eq!(u.keys_string(), "~app:* ~session:*");
        assert!(check(&u, &["SET", "app:x", "1"]).is_ok());
        assert!(check(&u, &["SET", "session:x", "1"]).is_ok());
        assert_eq!(
            check(&u, &["SET", "other:x", "1"]),
            Err(Denied::Key("other:x".into()))
        );
        // Every key of a multi-key command.
        assert!(check(&u, &["MGET", "app:a", "session:b"]).is_ok());
        assert_eq!(
            check(&u, &["MGET", "app:a", "other"]),
            Err(Denied::Key("other".into()))
        );
    }

    #[test]
    fn no_key_patterns_means_no_keys() {
        let u = user(&["on", "nopass", "+@all"]);
        assert_eq!(check(&u, &["GET", "k"]), Err(Denied::Key("k".into())));
        // Commands without keys are not affected.
        assert!(check(&u, &["PING"]).is_ok());
        let (mut key_restricted, mut cmd_restricted) = (false, false);
        track_restriction_counters(&u, &mut key_restricted, &mut cmd_restricted);
        assert!(key_restricted);
        // Undo, so the process-wide counter is left as it was found.
        let open = AclUser::default_user("");
        track_restriction_counters(&open, &mut key_restricted, &mut cmd_restricted);
    }

    #[test]
    fn read_and_write_patterns() {
        let u = user(&["on", "nopass", "%R~ro:*", "%W~wo:*", "%RW~rw:*", "+@all"]);
        assert!(check(&u, &["GET", "ro:a"]).is_ok());
        assert!(check(&u, &["SET", "ro:a", "1"]).is_err());
        assert!(check(&u, &["SET", "wo:a", "1"]).is_ok());
        assert!(check(&u, &["GET", "wo:a"]).is_err());
        // SET … GET reads the old value.
        assert!(check(&u, &["SET", "wo:a", "1", "GET"]).is_err());
        assert!(check(&u, &["SET", "rw:a", "1", "GET"]).is_ok());
        // INCR answers with the value: it needs both.
        assert!(check(&u, &["INCR", "wo:n"]).is_err());
        assert!(check(&u, &["INCR", "rw:n"]).is_ok());
        // COPY reads its source and writes its destination.
        assert!(check(&u, &["COPY", "ro:a", "wo:b"]).is_ok());
        assert!(check(&u, &["COPY", "wo:a", "ro:b"]).is_err());
        assert!(check(&u, &["SUNIONSTORE", "wo:dst", "ro:a", "ro:b"]).is_ok());
        assert_eq!(u.keys_string(), "%R~ro:* %W~wo:* ~rw:*");
    }

    #[test]
    fn channels_are_enforced() {
        let u = user(&["on", "nopass", "~*", "&news.*", "+@all"]);
        assert!(check(&u, &["PUBLISH", "news.uk", "hello"]).is_ok());
        assert_eq!(
            check(&u, &["PUBLISH", "secret", "hello"]),
            Err(Denied::Channel("secret".into()))
        );
        assert!(check(&u, &["SUBSCRIBE", "news.uk", "news.fr"]).is_ok());
        assert!(check(&u, &["SUBSCRIBE", "news.uk", "other"]).is_err());
        // A pattern subscription must be one of the user's patterns exactly.
        assert!(check(&u, &["PSUBSCRIBE", "news.*"]).is_ok());
        assert!(check(&u, &["PSUBSCRIBE", "news.u*"]).is_err());
        assert!(check(&u, &["PSUBSCRIBE", "*"]).is_err());
        // A new user has no channels.
        let none = user(&["on", "nopass", "~*", "+@all"]);
        assert!(check(&none, &["PUBLISH", "c", "m"]).is_err());
        let all = user(&["on", "nopass", "~*", "&*", "+@all"]);
        assert!(check(&all, &["PSUBSCRIBE", "*"]).is_ok());
    }

    #[test]
    fn selectors_are_alternatives() {
        let u = user(&["on", "nopass", "~app:*", "+@all", "(~cache:* +get)"]);
        assert!(check(&u, &["SET", "app:x", "1"]).is_ok());
        assert!(check(&u, &["GET", "cache:x"]).is_ok());
        // The selector allows GET only, and only on cache:*.
        assert_eq!(
            check(&u, &["SET", "cache:x", "1"]),
            Err(Denied::Key("cache:x".into()))
        );
        // Not a mixture: one set of permissions has to cover the whole command.
        assert!(check(&u, &["MGET", "app:x", "cache:x"]).is_err());
        assert_eq!(
            u.to_acl_string(),
            "user u on nopass ~app:* resetchannels +@all (~cache:* resetchannels -@all +get)"
        );
        let mut u = u;
        apply_rule(&mut u, "clearselectors").unwrap();
        assert!(check(&u, &["GET", "cache:x"]).is_err());
    }

    #[test]
    fn a_selector_split_over_several_arguments_is_put_back_together() {
        let merged =
            merge_selector_args(["on", "(~k*", "+get", "+set)", "~a"].into_iter()).unwrap();
        assert_eq!(merged, ["on", "(~k* +get +set)", "~a"]);
        assert!(merge_selector_args(["(~k*", "+get"].into_iter()).is_err());
    }

    #[test]
    fn rules_that_mean_nothing_are_refused() {
        let mut u = AclUser::new("u");
        for bad in [
            "bogus",
            "+@nosuchcategory",
            "-@nosuchcategory",
            "+nosuchcommand",
            "+get|x",
            "#abc",
            "%X~k",
            "~",
            "&",
            "(bogus)",
            "<never-set",
        ] {
            assert!(apply_rule(&mut u, bad).is_err(), "{bad} was accepted");
        }
        apply_rule(&mut u, "~*").unwrap();
        assert!(apply_rule(&mut u, "~more").is_err());
        for good in [
            "+get",
            "-@dangerous",
            "+config|get",
            "+@all",
            "resetkeys",
            "~a",
            "~b",
        ] {
            apply_rule(&mut u, good).unwrap_or_else(|e| panic!("{good}: {e}"));
        }
    }

    #[test]
    fn a_user_without_a_password_is_not_written_as_nopass() {
        let u = user(&["on", "~*", "+@all"]);
        assert_eq!(u.to_acl_string(), "user u on ~* resetchannels +@all");
        assert!(!u.accepts(""));
        assert!(!u.accepts("anything"));
    }

    #[test]
    fn an_acl_file_defines_its_users_from_nothing() {
        let hash = sha256_hex("pw");
        let file = format!(
            "# users\n\nuser default on #{hash} ~* &* +@all\nuser app on #{hash} ~app:* ~session:* &* +@all -flushall\n"
        );
        let users = parse_acl_file(&file, "").unwrap();
        assert_eq!(users.len(), 2);
        assert!(!users["default"].nopass);
        assert!(users["default"].accepts("pw"));
        assert_eq!(users["app"].keys.len(), 2);
        // What is written reads back the same.
        for user in users.values() {
            let again = parse_acl_file(&user.to_acl_string(), "").unwrap();
            assert_eq!(again[&user.name].to_acl_string(), user.to_acl_string());
        }
    }

    #[test]
    fn a_file_without_a_default_line_leaves_default_as_requirepass_made_it() {
        let users = parse_acl_file("user app on nopass ~* +@all\n", "").unwrap();
        assert!(users["default"].nopass);
        let users = parse_acl_file("user app on nopass ~* +@all\n", "secret").unwrap();
        assert!(!users["default"].nopass);
        assert!(users["default"].accepts("secret"));
    }

    #[test]
    fn a_bad_acl_file_is_refused_whole_and_says_where() {
        for (file, needle) in [
            ("user a on nopass\nnonsense line\n", "line 2"),
            ("user a on +@nosuchcategory\n", "line 1"),
            ("user a on\nuser a off\n", "duplicate user 'a'"),
            ("user\n", "line 1"),
            ("user a (~k +get\n", "parenthesis"),
        ] {
            let err = parse_acl_file(file, "").unwrap_err();
            assert!(err.contains(needle), "{file:?}: {err}");
        }
    }

    #[test]
    fn the_default_user_is_open_only_without_a_password() {
        let store = new_acl_store("");
        assert!(default_user_is_open(&store));
        apply_rule(store.write().get_mut("default").unwrap(), ">secret").unwrap();
        assert!(!default_user_is_open(&store));
        assert!(authenticate(&store, "default", "secret").is_some());
        assert!(authenticate(&store, "default", "wrong").is_none());
        assert!(authenticate(&store, "nobody", "secret").is_none());
        apply_rule(store.write().get_mut("default").unwrap(), "nopass").unwrap();
        apply_rule(store.write().get_mut("default").unwrap(), "off").unwrap();
        assert!(!default_user_is_open(&store));
    }
}