mount_webdav authentication

| | Comments (0)
Yesterday I was talking to some guys who had set up a WebDAV server, but noted that under Mac OS X, the keychain was not remembering the authentication for it. Sucks. So I played around with mount/mount_webdav and got it to work.

But man, is it ugly. mount_webdav requires you to write out a file with the username and password in it, then pass the file descriptor to the program as an argument. So I encode the username and password, then make a temp file and unlink it, prepare the file with select() and fcntl(), then print to it and call mount with the appropriate arguments.

And then, it seems Mac OS X's open command won't open the new volume most of the time. I also tried AppleScript, with open location, and that similarly failed. Bug.

#!/usr/bin/perl -w
use strict;
use File::Temp 'tempfile';
use Fcntl 'F_SETFD';
 
my $server = 'dav.example.com';
my $path   = '/Volumes/dav';
my $user   = 'login';
my $pass   = 'password';
 
# encode username and password
(my $ulen = sprintf "%4s", chr length $user) =~ s/ /\0/g;
(my $plen = sprintf "%4s", chr length $pass) =~ s/ /\0/g;
my $data = sprintf "%s%s%s%s", $ulen, $user, $plen, $pass;
 
# create mount point and temp file, and unlink file so no one can see it
mkdir $path;
my $fh = tempfile();
unlink $fh;
 
# autoflush
select( (select($fh), $|++)[0] );
 
# get ready to pass fd
my $fd = fileno($fh);
fcntl($fh, F_SETFD, 0) or die "Can't clear close-on-exec flag on temp fh: $!\n";
 
# finally, write username and password and mount
print $fh $data;
system('mount', '-t', 'webdav', '-o', "-a$fd", $server, $path);
 
# open (first usually fails, don't know why; but you can just open the
# icon in the Volumes folder, which is just as good as opening the icon
# on the Desktop
system('open', $path);
system('open', '/Volumes/');
 
# clean up
close $fh;
 
__END__

Now Playing: What It Takes - Aerosmith (Pump)

use.perl.org

Leave a comment

<pudge/*> (pronounced "PudgeGlob") is thousands of posts over many years by Pudge.

"It is the common fate of the indolent to see their rights become a prey to the active. The condition upon which God hath given liberty to man is eternal vigilance; which condition if he break, servitude is at once the consequence of his crime and the punishment of his guilt."

About this Entry

This page contains a single entry by pudge published on November 6, 2002 7:07 AM.

Public Drunkenness was the previous entry in this site.

About Time is the next entry in this site.

Find recent content on the main index or look in the archives to find all content.