#!/usr/bin/env python """ auto.maton: script for use with Linux's autofs, mounted on /net Copyright 2006 Adam Sampson The aim is to allow this to be mounted on /net, and to support various kinds of network mounts for users within a group. At the moment it'll only do CIFS. """ USER = "anime" GROUP = "anime" FMODE = 0644 DMODE = 0755 import os, sys, re, socket, pwd, grp def die(*s): sys.stderr.write("auto.maton: fatal: " + "".join(map(str, s)) + "\n") sys.exit(1) def hostlookup(host): try: return socket.gethostbyname(host) except: return None def nmblookup(host): f = os.popen("nmblookup " + host + " 2>/dev/null") addr = None for l in f.readlines(): m = re.match(r'^([0-9.]+)\s', l) if m is not None: addr = m.group(1) f.close() return addr def smblist(host, args): exports = [] f = os.popen("smbclient -gNL " + host + " 2>/dev/null") for l in f.readlines(): if not l.startswith("Disk|"): continue fs = l.split("|") if fs[1].endswith("$"): continue exports.append((fs[1], "://" + host + "/" + fs[1])) f.close() opts = "-fstype=cifs,guest,uid=%(uid)d,gid=%(gid)d,file_mode=%(fmode)04o,dir_mode=%(dmode)04o,ip=%(addr)s" % args return (opts, exports) def main(args): if len(args) != 1: die("Usage: auto.maton key") key = args[0] if not re.match(r'^[a-zA-Z._-]+$', key): die("Bad key: ", repr(key)) host = key addr = None for f in (hostlookup, nmblookup): addr = f(host) if addr is not None: break if addr is None: die("Cannot look up host ", host) args = { "addr": addr, "uid": pwd.getpwnam(USER)[2], "gid": grp.getgrnam(GROUP)[2], "fmode": FMODE, "dmode": DMODE, } (opts, exports) = smblist(host, args) if len(exports) == 0: # It would be nicer to show an empty directory, but we have to # mount *something*... sys.exit(1) if len(exports) == 1: # automount doesn't parse multimount lines properly when # there's only one mount, so add a dummy item. exports.append((".dummy", exports[0][1])) sys.stdout.write(opts) for (name, path) in exports: sys.stdout.write(" \\\n\t/" + name + " " + path) sys.stdout.write("\n") sys.exit(0) if __name__ == "__main__": main(sys.argv[1:])