开发者

Modify smb.conf from PHP?

开发者 https://www.devze.com 2023-03-01 13:25 出处:网络
I need to add/remove SAMBA shares through PHP or via SSH (ssh link from PHP). So far the only solution I\'ve found is a class for primitively handling the file, which I haven\'t got very much faith i

I need to add/remove SAMBA shares through PHP or via SSH (ssh link from PHP).

So far the only solution I've found is a class for primitively handling the file, which I haven't got very much faith in in terms of reliability. (h开发者_JS百科ttp://www.phpclasses.org/package/1546-PHP-Parse-and-recreate-the-Samba-smb-conf-file.html)

Could you recommend a way to do it?


Can't find something useful, so I would recommend a likewise simplistic approach. Instead of finding a full-fledged INI parser, it's advisable to work in append-mostly mode. A simple regex would be sufficient to replace existing [share] sections without harming the rest of the smb.conf file.

And you can use the testparm utility to probe for correctness before overwriting the real file.

define("SMB_CONF", "/etc/samba/smb.conf");

function add_share($section, $options) {

    // read old data
    #$old = parse_ini_file(SMB_CONF);
    $conf = file_get_contents(SMB_CONF);

    // merge new settings
    #if (isset($old[$section])) {
    #    $options = array_merge($old[$section], $options);
    #}

    // remove old share section, if it exists
    preg_replace("/^\[$section\]\s*\n(^(?!\[).*\n)+/m", "", $conf);

    // write out new ini file
    $conf .= "\n\n[$section]\n";
    foreach ($options as $key=>$value) {
        $conf .= "$key = $value\n";
    }
    $tmp = tempnam("/tmp/", "SMB");
    file_put_contents($tmp, $conf);

    // copy if it is syntactically correct
    if (strstr(`testparm -s $tmp 2>&1`, "OK")) {
        rename($tmp, SMB_CONF);
    }
}

Updated Okay, parse_ini_file does not work in either case. It trips over the smb.conf style. So you can only completely replace existing entries, not update them.


Zend Framework's Zend_Config setup might be useful as it's able to read/write ini files.

Zend_Config_Writer

0

精彩评论

暂无评论...
验证码 换一张
取 消