Added initial unfs3 sources for version 0.9.22+dfsg-1maemo2
[unfs3] / unfs3 / contrib / nfsotpclient / nfsotpclient.py
1 #!/usr/bin/env python
2 # -*-mode: python; coding: iso-8859-1 -*-
3 #
4 # Copyright (c) 2002-2005 Cendio AB. All rights reserved.
5
6 import sys
7 import rpc
8 import mountclient
9 import socket
10 import os
11 import md5
12
13
14 class PartialMOUNTClient:
15     def __init__(self):
16         pass
17
18     def addpackers(self):
19         self.packer = mountclient.mountpacker.MOUNTPacker(self)
20         self.unpacker = mountclient.mountpacker.MOUNTUnpacker(self, '')
21
22     def mnt(self, dirpath):
23         res = mountclient.mounttypes.mountres3(self)
24         self.make_call(mountclient.mountconstants.MOUNTPROC_MNT,
25                        dirpath, self.packer.pack_string, res.unpack)
26         return res
27     
28
29 class TCPMOUNTClient(PartialMOUNTClient, rpc.RawTCPClient):
30     def __init__(self, host, port):
31         rpc.RawTCPClient.__init__(self, host,
32                                   mountclient.mountconstants.MOUNT_PROGRAM,
33                                   mountclient.mountconstants.MOUNT_V3,
34                                   port)
35         PartialMOUNTClient.__init__(self)
36
37
38 class NFSOTPClient:
39     def __init__(self, host, port):
40         self.mountcl = TCPMOUNTClient(host, port)
41
42     def getotp(self, password):
43         res = self.mountcl.mnt("@getnonce")
44
45         if res.fhs_status != mountclient.mountconstants.MNT3_OK:
46             print >>sys.stderr, "Failed to get nonce:", mountclient.mountconstants.mountstat3_id[res.fhs_status]
47             sys.exit(1)
48         
49         fhandle = res.mountinfo.fhandle
50         digest = md5.new(fhandle + password).hexdigest()
51         return digest
52
53
54 def usage():
55     print >>sys.stderr, "Usage: nfsotpclient.py host[:port]"
56     sys.exit(1)
57
58
59 if __name__ == "__main__":
60     if len(sys.argv) != 2:
61         usage()
62
63     fields = sys.argv[1].split(":")
64     host = fields[0]
65     del fields[0]
66     if fields:
67         port = int(fields[0])
68     else:
69         # No port specified, fetch from portmapper
70         # FIXME
71         print >>sys.stderr, "Portmapper support not yet implemented"
72         sys.exit(1)
73
74     cl = NFSOTPClient(host, port)
75     import getpass
76     password = getpass.getpass()
77     
78     print cl.getotp(password)