-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfsclient.py
406 lines (346 loc) · 11.9 KB
/
nfsclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from enum import Enum
import rpc
from mountclient import MountPacker, MountUnpacker
from rpc import TCPClient
NFS_PROGRAM = 100003
NFS_VERSION = 3
class NfsStat3(Enum):
NFS3_OK = 0
NFS3ERR_PERM = 1
NFS3ERR_NOENT = 2
NFS3ERR_IO = 5
NFS3ERR_NXIO = 6
NFS3ERR_ACCES = 13
NFS3ERR_EXIST = 17
NFS3ERR_XDEV = 18
NFS3ERR_NODEV = 19
NFS3ERR_NOTDIR = 20
NFS3ERR_ISDIR = 21
NFS3ERR_INVAL = 22
NFS3ERR_FBIG = 27
NFS3ERR_NOSPC = 28
NFS3ERR_ROFS = 30
NFS3ERR_MLINK = 31
NFS3ERR_NAMETOOLONG = 63
NFS3ERR_NOTEMPTY = 66
NFS3ERR_DQUOT = 69
NFS3ERR_STALE = 70
NFS3ERR_REMOTE = 71
NFS3ERR_BADHANDLE = 10001
class CreateMode(Enum):
UNCHECKED = 0
GUARDED = 1
EXCLUSIVE = 2
class UnexpectedNfsStatus(Exception):
pass
class NFSPacker(MountPacker):
def pack_sattrargs(self, sa):
file, attributes = sa
self.pack_fhandle(file)
self.pack_sattr(attributes)
def pack_sattr(self, sa):
mode, uid, gid, size, atime, mtime = sa
self.pack_uint(1)
self.pack_uint(sa["mode"])
self.pack_uint(sa["uid"])
self.pack_uint(sa["gid"])
self.pack_uint(1)
self.pack_uhyper(sa["size"])
if atime == 0:
self.pack_uint(0)
else:
self.pack_uint(1)
self.pack_timeval(sa["atime"])
if mtime == 0:
self.pack_uint(0)
else:
self.pack_uint(1)
self.pack_timeval(sa["mtime"])
def pack_diropargs(self, what):
self.pack_fhandle(what["dir"])
self.pack_string(what["Name"].encode())
def pack_readdirargs(self, ra):
"""
struct READDIRPLUS3args {
nfs_fh3 dir;
cookie3 cookie;
cookieverf3 cookieverf;
count3 dircount;
count3 maxcount;
};
"""
self.pack_fhandle(ra["dir"])
self.pack_uhyper(ra["cookie"])
self._handle_verifier(ra["Verifier"])
self.pack_uint(ra["count"])
def _handle_verifier(self, cookie_version):
if cookie_version == 0:
self.pack_uhyper(cookie_version)
else:
self.pack_fopaque(8, cookie_version)
def pack_readdirplus(self, ra):
"""
struct READDIRPLUS3args {
nfs_fh3 dir;
cookie3 cookie;
cookieverf3 cookieverf;
count3 dircount;
count3 maxcount;
};
"""
self.pack_readdirargs(ra)
self.pack_uint(ra["maxcount"])
def pack_write_args(self, wa):
self.pack_fhandle(wa["file"])
self.pack_uhyper(wa["offset"])
self.pack_uint(wa["count"])
self.pack_uint(wa["Stable"])
self.pack_data(wa["Data"].encode())
def pack_create_args(self, ca):
self.pack_diropargs(ca['where'])
self.pack_enum(ca["Create Mode"])
self.pack_sattr(ca["obj_attributes"])
def pack_commitargs(self, ca):
fh, offset, count = ca
self.pack_fhandle(fh)
self.pack_uhyper(offset)
self.pack_uint(count)
def pack_fs_info_args(self, fh):
self.pack_fhandle(fh)
def pack_data(self, data):
data_length = len(data)
self.pack_uint(data_length)
self.pack_fopaque(data_length, data)
def pack_timeval(self, tv):
secs, usecs = tv
self.pack_uint(secs)
self.pack_uint(usecs)
class NFSUnpacker(MountUnpacker):
def unpack_readdirres(self):
status = verify_nfs_status(self.unpack_enum(), [NfsStat3.NFS3_OK])
self.unpack_obj_attributes()
_ = self.unpack_uhyper()
entries = self.unpack_list(self.unpack_entry)
eof = self.unpack_bool()
rest = (entries, eof)
return status, rest
def unpack_readdirplus(self):
status = verify_nfs_status(self.unpack_enum(), [NfsStat3.NFS3_OK])
attr = self.unpack_obj_attributes()
self.unpack_uhyper() # Verifier
entries = self.unpack_list(self.unpack_entry_plus)
eof = self.unpack_bool()
rest = (entries, eof)
return status, attr, rest
def unpack_obj_attributes(self):
attributes_follow = self.unpack_bool()
if attributes_follow == 1:
fattr3 = self.unpack_fattr3()
return fattr3
def unpack_dir_or_file_wcc(self):
before_follows = self.unpack_bool()
if before_follows:
self.unpack_wcc_attr()
after_follows = self.unpack_bool()
if after_follows:
self.unpack_fattr3()
def unpack_wcc_attributes(self):
op_attr = self.unpack_uint()
if op_attr == 1:
wcc_attrs = self.unpack_wcc_attr()
return wcc_attrs
def unpack_fh_attributes(self):
fh = self.unpack_fhandle()
return fh
def unpack_fsinfo_res(self):
"""
struct FSINFO3resok {
post_op_attr obj_attributes;
uint32 rtmax;
uint32 rtpref;
uint32 rtmult;
uint32 wtmax;
uint32 wtpref;
uint32 wtmult;
uint32 dtpref;
size3 maxfilesize;
nfstime3 time_delta;
uint32 properties;
:return:
"""
verify_nfs_status(self.unpack_enum(), [NfsStat3.NFS3_OK])
attr = self.unpack_obj_attributes()
rtmax = self.unpack_uint()
rtpref = self.unpack_uint()
rtmult= self.unpack_uint()
wtmax = self.unpack_uint()
wtpref = self.unpack_uint()
wtmult = self.unpack_uint()
dtpref = self.unpack_uint()
time_delta = self.unpack_uhyper()
properties = self.unpack_uint()
max_file_size = self.unpack_uhyper()
return attr, rtmax, rtpref, rtmult, wtmax, wtpref, wtmult, dtpref, time_delta, \
properties, max_file_size
def unpack_entry(self):
file_id = self.unpack_uhyper()
name = self.unpack_string()
cookie = self.unpack_uhyper()
return file_id, name, cookie
def unpack_entry_plus(self):
fh = None
fileid, name, cookie = self.unpack_entry()
entry_attr = self.unpack_obj_attributes()
handle_follow = self.unpack_bool()
if handle_follow:
fh = self.unpack_fh_attributes()
return fileid, name, cookie, entry_attr, fh
def unpack_write_res(self):
status = verify_nfs_status(self.unpack_enum(), [NfsStat3.NFS3_OK])
self.unpack_dir_or_file_wcc()
# count
self.unpack_uint()
# committed
self.unpack_uint()
# committed:
self.unpack_uhyper()
return status
def unpack_create_res(self):
fh = None
status = verify_nfs_status(self.unpack_enum(), [NfsStat3.NFS3_OK])
handle_follow = self.unpack_bool()
if handle_follow:
fh = self.unpack_fh_attributes()
self.unpack_obj_attributes()
self.unpack_dir_or_file_wcc()
return status, fh
def unpack_dirop_res(self):
file_handle = None
status = verify_nfs_status(self.unpack_enum(), [NfsStat3.NFS3_OK, NfsStat3.NFS3ERR_NOENT])
if status == NfsStat3.NFS3_OK:
file_handle = self.unpack_fhandle()
self.unpack_obj_attributes()
self.unpack_obj_attributes()
elif status == NfsStat3.NFS3ERR_NOENT:
self.unpack_obj_attributes()
return status, file_handle
def unpack_attribute_status(self):
status = verify_nfs_status(self.unpack_enum(), [NfsStat3.NFS3_OK])
attributes = self.unpack_fattr3()
return status, attributes
def unpack_fattr3(self):
"""
struct fattr3 {
ftype3 type;
mode3 mode;
uint32 nlink;
uid3 uid;
gid3 gid;
size3 size;
size3 used;
specdata3 rdev;
uint64 fsid;
fileid3 fileid;
nfstime3 atime;
nfstime3 mtime;
nfstime3 ctime;
};
"""
_type = self.unpack_enum()
mode = self.unpack_uint()
nlink = self.unpack_uint()
uid = self.unpack_uint()
gid = self.unpack_uint()
size = self.unpack_uhyper()
used = self.unpack_uhyper()
rdev = self.unpack_uhyper()
fsid = self.unpack_uhyper()
fileid = self.unpack_uhyper()
atime = self.unpack_timeval()
mtime = self.unpack_timeval()
ctime = self.unpack_timeval()
return _type, mode, nlink, uid, gid, size, used, rdev, fsid, fileid, atime, mtime, ctime
def unpack_wcc_attr(self):
"""
struct wcc_attr {
size3 size;
nfstime3 mtime;
nfstime3 ctime;
};
"""
size = self.unpack_uhyper()
mtime = self.unpack_timeval()
ctime = self.unpack_timeval()
return size, mtime, ctime
def unpack_timeval(self):
secs = self.unpack_uint()
usecs = self.unpack_uint()
return (secs, usecs)
class NFSClient(TCPClient):
def __init__(self, host):
TCPClient.__init__(self, host, NFS_PROGRAM, NFS_VERSION)
def addpackers(self):
self.packer = NFSPacker()
self.unpacker = NFSUnpacker('')
def mkcred(self):
if self.cred is None:
self.cred = rpc.AuthFlavor.AUTH_UNIX.value, rpc.make_auth_unix_default()
return self.cred
def getattr(self, fh):
return self.make_call(1, fh,
self.packer.pack_fhandle,
self.unpacker.unpack_attribute_status)
def setattr(self, sa):
return self.make_call(2, sa,
self.packer.pack_sattrargs,
self.unpacker.unpack_attribute_status)
def lookup(self, da):
return self.make_call(3, da,
self.packer.pack_diropargs,
self.unpacker.unpack_dirop_res)
def read_dir(self, ra):
return self.make_call(16, ra,
self.packer.pack_readdirargs,
self.unpacker.unpack_readdirres)
def read_dir_plus(self, ra):
return self.make_call(17, ra,
self.packer.pack_readdirplus,
self.unpacker.unpack_readdirplus)
def write(self, wa):
return self.make_call(7, wa,
self.packer.pack_write_args,
self.unpacker.unpack_write_res)
def create(self, ca):
return self.make_call(8, ca,
self.packer.pack_create_args,
self.unpacker.unpack_create_res)
def fsinfo(self, fh):
return self.make_call(19, fh,
self.packer.pack_fs_info_args,
self.unpacker.unpack_fsinfo_res)
def listdir_wrapper(self, dir_handle):
list_dir = []
ra = (dir_handle, 0, 0, 2000, 2000)
while 1:
status, dir_params, rest = self.read_dir_plus(ra)
if status != NfsStat3.NFS3_OK:
print ("Server returned {}".format(status))
break
entries, eof = rest
last_cookie = None
for file_id, dir_or_file_name, dir_params, cookie, fh in entries:
list_dir.append((file_id, dir_or_file_name))
last_cookie = cookie
if eof or last_cookie is None:
break
ra = (ra[0], last_cookie, ra[1], ra[2])
return list_dir
def verify_nfs_status(status, allowed_statuses):
"""
Check if the current matches the given allowed status. Raise an exception otherwise
:type status: int
:type allowed_statuses: list
"""
if NfsStat3(status) not in allowed_statuses:
raise UnexpectedNfsStatus(f"{NfsStat3(status).name} ({NfsStat3(status).value})")
return NfsStat3(status)