Question

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. (http://www.phpclasses.org/package/1546-PHP-Parse-and-recreate-the-Samba-smb-conf-file.html)

Could you recommend a way to do it?

Was it helpful?

Solution

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.

OTHER TIPS

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

Zend_Config_Writer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top