command line samba/windows logins

The question came up on the FreeBSD Questions mailing list: how do you set up commandline/non-interactive logins or mounts of Windows shares, aka samba shares?

I had this working at my last job where I was running OS X in a Windows environment, so I could do things like web traffic reports, backups and the like unattended, and the key was the .nsmbrc file, a cousin of the .netrc file. Sadly, online documentation for the .nsmbrc file seems to be scanty, but there is an example file distributed with the samba source that’s actually more useful. Look for a dot.nsmbrc file with find(1) or locate(2).

This is an actual file, redacted to remove my network-specific stuff. There are three types of entries. The default stuff describes the network/workgroup, then you can specify servers/shares, and then user login details. Also, this all works for samba 3 clients against 2.2.8a servers, but not cleanly vice versa: I’m in the process of standardizing on 3.0 and will test again to see if it works.

NB: the only keyword is “default.” The other tokens are site-specific, so SERVER, USER, and SHARE are for you to replace with what you need at your site.

And of course there are a lot more options available: these are the barest minimum that I need to make things work.

[default]
workgroup=[your workgroup]

[SERVER]
addr=192.168.2.3

[SERVER:USERNAME]
password=[encrypted password]

[SERVER2]
addr=192.168.2.1

[SERVER2:USERNAME:SHARE1]
password=[encrypted password]

[SERVER2:USERNAME:SHARE2]
password=[encrypted password]

Note that you can login to different servers as different user names as well as specify different shares.

To encrypt passwords, use smbutil: smbutil crypt and either enter the password there or at the prompt. The encryption is trivial, but as we say, a lock keeps honest people honest. Between that and the 0600 permissions on the file, you’re reasonably secure.

Test it thusly:
white:~ paul$ smbutil view //paul@red
kextload: extension /System/Library/Extensions/smbfs.kext is already loaded
Share Type Comment