Instantly share code, notes, and snippets.

Embed
What would you like to do?
February 20 2018 - Connect Mac OS X to a Samba File Server Remotely via PHP

Connect Mac OS X to a Samba File Server Remotely via PHP

Ever had to connect a Mac web server to a Samba file share remotely via a PHP/HMTL form? No?! Really!? Are you sure?? Check again, I'll wait.

What's that you say!? The answer is still no!? Oh. Well.. OK.

Well anyways, I had to do this the other day and I'm going to write it down here so that I don't forget how I did it.

My work uses a little Mac web server to serve up a GitList installation (a listing of all our repos). Problem is, the Git repositories live on our LAN (Samba) file server. The Mac needs to talk to the LAN so that it can read the Git repositories and display them on the page via GitList. The Mac web server doesn't have any credentials of its own to supply to the Samba share, so, with this scipt, we ask the user for their credentials and then use those to connect to the Samba share.

Sounds like fun (kinda). I created the connect-samba.php HTML/PHP form/script and hosted it on the Mac. When someone views the page, they'll be asked to login with thier credentials. Then the Mac will make the connection to the Samba server and mount the share to /Volumes. When disconnect-samba.php is run (or visited in the browser) the share will be unmounted.

We are passing user credentials via $_POST here so obviously an SSL'd connection to the web server and Samba share is recommended.

This was one of the first experiences I had calling an external process with PHP via exec().

<?php
$domain = 'myorg.internal';
$server_name = 'FileServer';
$share_name = 'XXXXX'; // example: S11110
if (!isset($_POST['user']) || !isset($_POST['password'])): ?>
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title>Connect this computer to Samba Share via PHP</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<style>body { padding: 1rem; }</style>
</head>
<body>
<p>Connect to <?php echo $share_name; ?></p>
<form action="connect-samba.php" method="POST">
User:<br>
<input type="text" name="user"><br>
Password:<br>
<input type="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
<p style="margin-top: 1rem;"><a href="disconnect-samba.php">Disconnect the share?</a></p>
</body>
</html>
<?php else:
try {
exec('open "smb://'.$domain.';'.$_POST['user'].':'.$_POST['password'].'@'.$server_name.'/'.$share_name.'"');
} catch (Exception $e) {
header('HTTP/1.0 500 Internal Server Error');
exit("Cannot open Samba share. Error: $e");
}
?>
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $share_name; ?> mounted.</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<style>body { padding: 1rem; }</style>
</head>
<body>
<p><?php echo $share_name; ?> mounted at: /Volumes/<?php echo $share_name; ?></p>
</body>
</html>
<?php endif; ?>
<?php
$share_name = 'XXXXX'; // example: S11110
try {
exec('umount /Volumes/'.$share_name.' 2>&1', $retArr, $retVal);
if (isset($retArr[0])) {
if (strpos($retArr[0], 'not currently mounted') !== false) {
echo $share_name.' was not connected/mounted. You need to <a href="connect-samba.php">connect the server</a> first before you can disconnect it.<br>';
}
} else {
echo $share_name.' unmounted. <a href="/">Go home?</a>';
}
} catch (Exception $e) {
header('HTTP/1.0 500 Internal Server Error');
exit ("Problem unmounting $share_name <br> Error: $e");
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment