File: sync-roundcubemail-totp.php

package info (click to toggle)
postfixadmin 4.0.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,888 kB
  • sloc: php: 12,256; perl: 1,156; sh: 717; python: 142; xml: 63; sql: 3; makefile: 2
file content (34 lines) | stat: -rw-r--r-- 1,147 bytes parent folder | download
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
#!/bin/env php
<?php

// Get positional arguments
$USERNAME = $argv[1];
$DOMAIN = $argv[2];

// Get TOTP shared secret from stdin
$SHARED_SECRET = trim(fgets(STDIN));

// Include database configuration
include_once "/etc/postfixadmin/rcm-totp-sync.php";

// connect to Roundcubemail database and update user preferences with TOTP secret
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli($CONFIG["host"], $CONFIG["user"], $CONFIG["password"], $CONFIG["database"]);

$stmt = $mysqli->prepare("SELECT preferences FROM users WHERE username=?");
$stmt->bind_param("s", $USERNAME);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows == 1) {
    echo "Updating TOTP secret for $USERNAME\n";
    $row = $result->fetch_assoc();
    $preferences = unserialize($row['preferences']);
    $preferences['twofactor_gauthenticator']['secret'] = $SHARED_SECRET;
    $stmt_update = $mysqli->prepare("UPDATE users SET preferences=?");
    $stmt_update->bind_param("s", serialize($preferences));
    $stmt_update->execute();
} else {
    echo "Could not find user $USERNAME in Roundcubemail.\n";
}
$mysqli->close();