ISPConfig mass email users creation

Preparing for migration from old email system (±15 years old FreeBSD...) to latest ISPConfig. One of steps - email users creation. ISPConfig have minimalist documentation and few examples.

Yeh, old system store plain text passwords :)

p1.txt
email1@domainname.com plain_text_password1
email2@domainname.com plain_text_password2
email3@domainname.com plain_text_password3
...
Idea is simple, I created one user with all needed settings, and then used him as default for others.

rest_make_users.php

$remote_user = 'admin-api-user';
$remote_pass = 'admin-api-password';
$remote_url = 'https://127.0.0.1:8080/remote/json.php';

function restCall($method, $data) {
    global $remote_url;
    if(!is_array($data)) return false;
    $json = json_encode($data);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_POST, 1);
    if($data) curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $remote_url . '?' . $method);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

$result = restCall('login', array('username' => $remote_user, 'password' => $remote_pass, 'client_login' => false));
if($result) {
    $data = json_decode($result, true);
    if(!$data) die("ERROR!\n");

    $session_id = $data['response'];
    $client_id = 1;

    $result = restCall('mail_user_get', array('session_id' => $session_id, 'primary_id' => '1'));
    if($result) {
//        print_r(json_decode($result, true));
    }
    else{
        print "Could not get client_get result\n";
        print_r(json_decode($result, true));
    }


    $fn = fopen("p1.txt","r");
    while(! feof($fn)) {
        $r = fgets($fn);
        $m_mail = explode(" ", $r)[0];
        $m_pass = trim(explode(" ", $r)[1]);
        str_replace(array("\r", "\n"), '', $m_pass);

        $params = json_decode($result, true)['response'];
        $params['email'] = $m_mail;
        $params['login'] = $params['email'];
        $params['password'] = $m_pass;
        $params['maildir'] = '/var/vmail/' . explode("@", $params['email'])[1] . '/' . explode("@", $params['email'])[0];

        unset($params['mailuser_id']);
        unset($params['sys_userid']);
        unset($params['sys_groupid']);
        unset($params['sys_perm_user']);
        unset($params['sys_perm_group']);
        unset($params['sys_perm_other']);

//        print_r($params);

        $result1 = restCall('mail_user_add', array('session_id' => $session_id, 'client_id' => $client_id, 'params' => $params));
        if($result1) {
            print_r(json_decode($result1, true));
        }
        else {
            print "Could not get mail_user_add result\n";
            print_r(json_decode($result1, true));
        }

//        sleep(1);

        echo "
"; } fclose($fn); $result = restCall('logout', array('session_id' => $session_id)); if($result) { // var_dump(json_decode($result, true)); } else print "Could not get logout result\n"; }

Comments

Popular posts from this blog

FreeRadius and Google Workspace LDAP

pssh (parallel-ssh) problems on Debian 10 with Python 3.7