forked from kofemann/ms-nfs41-client
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmount.c
More file actions
373 lines (337 loc) · 11.3 KB
/
mount.c
File metadata and controls
373 lines (337 loc) · 11.3 KB
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
/* NFSv4.1 client for Windows
* Copyright © 2012 The Regents of the University of Michigan
*
* Olga Kornievskaia <aglo@umich.edu>
* Casey Bodley <cbodley@umich.edu>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* without any warranty; without even the implied warranty of merchantability
* or fitness for a particular purpose. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA
*/
#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <Winnetwk.h> /* for WNet*Connection */
#include <stdlib.h>
#include <stdio.h>
#include "nfs41_driver.h" /* NFS41_PROVIDER_NAME_A */
#include "options.h"
DWORD EnumMounts(
IN LPNETRESOURCE pContainer);
static DWORD DoMount(
IN LPTSTR pLocalName,
IN LPTSTR pRemoteName,
IN BOOL bPersistent,
IN PMOUNT_OPTION_LIST pOptions);
static DWORD DoUnmount(
IN LPTSTR pLocalName,
IN BOOL bForce);
static void RecursivePrintEaInformation(
IN PFILE_FULL_EA_INFORMATION EA);
static BOOL ParseDriveLetter(
IN LPTSTR pArg,
OUT PTCH pDriveLetter);
void PrintErrorMessage(
IN DWORD dwError);
static VOID PrintUsage(LPTSTR pProcess)
{
_tprintf(TEXT("Usage: %s [options] <drive letter|*> <hostname>:<path>\n")
TEXT("Options:\n")
TEXT("\t-h\thelp\n")
TEXT("\t-d\tunmount\n")
TEXT("\t-f\tforce unmount if the drive is in use\n")
TEXT("\t-p\tmake the mount persist over reboots\n")
TEXT("\t-o <comma-separated mount options>\n")
TEXT("Mount options:\n")
TEXT("\tro\tmount as read-only\n")
TEXT("\trsize=#\tread buffer size in bytes\n")
TEXT("\twsize=#\twrite buffer size in bytes\n")
TEXT("\tsec=krb5:krb5i:krb5p\tspecify gss security flavor\n")
TEXT("\twritethru\tturns off rdbss caching for writes\n")
TEXT("\tnocache\tturns off rdbss caching\n")
TEXT("\ttimeout=#\tspecify upcall timeout value in seconds (default 120s)\n"),
pProcess, pProcess, pProcess);
}
DWORD __cdecl _tmain(DWORD argc, LPTSTR argv[])
{
DWORD i, result = NO_ERROR;
TCHAR szLocalName[] = TEXT("C:\0");
LPTSTR pLocalName = NULL;
LPTSTR pRemoteName = NULL;
BOOL bUnmount = FALSE;
BOOL bForceUnmount = FALSE;
BOOL bPersistent = FALSE;
MOUNT_OPTION_LIST Options;
if (argc == 1) {
/* list open nfs shares */
result = EnumMounts(NULL);
if (result)
PrintErrorMessage(GetLastError());
goto out;
}
result = InitializeMountOptions(&Options, MAX_OPTION_BUFFER_SIZE);
if (result) {
PrintErrorMessage(GetLastError());
goto out;
}
/* parse command line */
for (i = 1; i < argc; i++)
{
if (argv[i][0] == TEXT('-'))
{
if (_tcscmp(argv[i], TEXT("-h")) == 0) /* help */
{
PrintUsage(argv[0]);
goto out;
}
else if (_tcscmp(argv[i], TEXT("-d")) == 0) /* unmount */
{
bUnmount = TRUE;
}
else if (_tcscmp(argv[i], TEXT("-f")) == 0) /* force unmount */
{
bForceUnmount = TRUE;
}
else if (_tcscmp(argv[i], TEXT("-p")) == 0) /* persistent */
{
bPersistent = TRUE;
}
else if (_tcscmp(argv[i], TEXT("-o")) == 0) /* mount option */
{
++i;
if (i >= argc)
{
result = ERROR_BAD_ARGUMENTS;
_ftprintf(stderr, TEXT("Mount options missing ")
TEXT("after '-o'.\n\n"));
PrintUsage(argv[0]);
goto out_free;
}
if (!ParseMountOptions(argv[i], &Options))
{
result = ERROR_BAD_ARGUMENTS;
goto out_free;
}
}
else
_ftprintf(stderr, TEXT("Unrecognized option ")
TEXT("'%s', disregarding.\n"), argv[i]);
}
else if (pLocalName == NULL) /* drive letter */
{
pLocalName = argv[i];
}
else if (pRemoteName == NULL) /* remote path */
{
pRemoteName = argv[i];
}
else
_ftprintf(stderr, TEXT("Unrecognized argument ")
TEXT("'%s', disregarding.\n"), argv[i]);
}
/* validate local drive letter */
if (pLocalName == NULL)
{
result = ERROR_BAD_ARGUMENTS;
_ftprintf(stderr, TEXT("Missing argument for drive letter.\n\n"));
PrintUsage(argv[0]);
goto out_free;
}
if (FALSE == ParseDriveLetter(pLocalName, szLocalName))
{
result = ERROR_BAD_ARGUMENTS;
_ftprintf(stderr, TEXT("Invalid drive letter '%s'. ")
TEXT("Expected 'C' or 'C:'.\n\n"), pLocalName);
PrintUsage(argv[0]);
goto out_free;
}
if (bUnmount == TRUE) /* unmount */
{
result = DoUnmount(szLocalName, bForceUnmount);
if (result)
PrintErrorMessage(result);
}
else /* mount */
{
if (pRemoteName == NULL)
{
result = ERROR_BAD_NET_NAME;
_ftprintf(stderr, TEXT("Missing argument for remote path.\n\n"));
PrintUsage(argv[0]);
goto out_free;
}
result = DoMount(szLocalName, pRemoteName, bPersistent, &Options);
if (result)
PrintErrorMessage(result);
}
out_free:
FreeMountOptions(&Options);
out:
return result;
}
static void ConvertUnixSlashes(
IN OUT LPTSTR pRemoteName)
{
LPTSTR pos = pRemoteName;
for (pos = pRemoteName; *pos; pos++)
if (*pos == TEXT('/'))
*pos = TEXT('\\');
}
static DWORD ParseRemoteName(
IN LPTSTR pRemoteName,
IN OUT PMOUNT_OPTION_LIST pOptions,
OUT LPTSTR pConnectionName,
IN size_t cchConnectionLen)
{
DWORD result = NO_ERROR;
LPTSTR pEnd;
ConvertUnixSlashes(pRemoteName);
/* fail if the server name doesn't end with :\ */
pEnd = _tcsrchr(pRemoteName, TEXT(':'));
if (pEnd == NULL || pEnd[1] != TEXT('\\')) {
_ftprintf(stderr, TEXT("Failed to parse the remote path. ")
TEXT("Expected 'hostname:\\path'.\n"));
result = ERROR_BAD_ARGUMENTS;
goto out;
}
*pEnd = TEXT('\0');
++pEnd;
if (!InsertOption(TEXT("srvname"), pRemoteName, pOptions) ||
!InsertOption(TEXT("mntpt"), *pEnd ? pEnd : TEXT("\\"), pOptions)) {
result = ERROR_BAD_ARGUMENTS;
goto out;
}
result = StringCchCopy(pConnectionName, cchConnectionLen, TEXT("\\\\"));
if (FAILED(result))
goto out;
result = StringCbCat(pConnectionName, cchConnectionLen, pRemoteName);
if (FAILED(result))
goto out;
if (*pEnd)
result = StringCchCat(pConnectionName, cchConnectionLen, pEnd);
out:
return result;
}
static DWORD DoMount(
IN LPTSTR pLocalName,
IN LPTSTR pRemoteName,
IN BOOL bPersistent,
IN PMOUNT_OPTION_LIST pOptions)
{
DWORD result = NO_ERROR;
TCHAR szExisting[MAX_PATH];
TCHAR szRemoteName[MAX_PATH];
DWORD dwLength;
*szRemoteName = TEXT('\0');
result = ParseRemoteName(pRemoteName, pOptions, szRemoteName, MAX_PATH);
if (result)
goto out;
/* fail if the connection already exists */
result = WNetGetConnection(pLocalName, (LPTSTR)szExisting, &dwLength);
if (result == NO_ERROR)
{
result = ERROR_ALREADY_ASSIGNED;
_ftprintf(stderr, TEXT("Mount failed, drive %s is ")
TEXT("already assigned to '%s'.\n"),
pLocalName, szExisting);
}
else
{
NETRESOURCE NetResource;
TCHAR szConnection[MAX_PATH];
DWORD ConnectSize = MAX_PATH, ConnectResult, Flags = 0;
ZeroMemory(&NetResource, sizeof(NETRESOURCE));
NetResource.dwType = RESOURCETYPE_DISK;
/* drive letter is chosen automatically if lpLocalName == NULL */
NetResource.lpLocalName = *pLocalName == TEXT('*') ? NULL : pLocalName;
NetResource.lpRemoteName = szRemoteName;
/* ignore other network providers */
NetResource.lpProvider = TEXT(NFS41_PROVIDER_NAME_A);
/* pass mount options via lpComment */
if (pOptions->Buffer->Length)
{
if (pOptions->Current)
pOptions->Current->NextEntryOffset = 0;
NetResource.lpComment = (LPTSTR)pOptions->Buffer;
}
if (bPersistent)
Flags |= CONNECT_UPDATE_PROFILE;
result = WNetUseConnection(NULL,
&NetResource, NULL, NULL, Flags,
szConnection, &ConnectSize, &ConnectResult);
if (result == NO_ERROR)
_tprintf(TEXT("Successfully mounted %s to drive %s\n"),
pRemoteName, szConnection);
else
_ftprintf(stderr, TEXT("WNetUseConnection(%s, %s) ")
TEXT("failed with error code %u.\n"),
pLocalName, szRemoteName, result);
}
out:
return result;
}
static DWORD DoUnmount(
IN LPTSTR pLocalName,
IN BOOL bForce)
{
DWORD result;
/* disconnect the specified local drive */
result = WNetCancelConnection2(pLocalName, CONNECT_UPDATE_PROFILE, bForce);
/* TODO: verify that this connection uses the nfs41 provider -cbodley */
switch (result)
{
case NO_ERROR:
_tprintf(TEXT("Drive %s unmounted successfully.\n"), pLocalName);
break;
case ERROR_NOT_CONNECTED:
_ftprintf(stderr, TEXT("Drive %s is not currently ")
TEXT("connected.\n"), pLocalName);
break;
default:
_ftprintf(stderr, TEXT("WNetCancelConnection2(%s) failed ")
TEXT("with error code %u.\n"), pLocalName, result);
break;
}
return result;
}
static BOOL ParseDriveLetter(
IN LPTSTR pArg,
OUT PTCH pDriveLetter)
{
/* accept 'C' or 'C:' */
switch (_tcslen(pArg))
{
case 2:
if (pArg[1] != TEXT(':'))
return FALSE;
/* break intentionally missing */
case 1:
if (_istlower(*pArg))
*pArg = (TCHAR)_totupper(*pArg);
else if (!_istupper(*pArg) && *pArg != TEXT('*'))
return FALSE;
*pDriveLetter = *pArg;
return TRUE;
}
return FALSE;
}
void PrintErrorMessage(
IN DWORD dwError)
{
LPTSTR lpMsgBuf = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL);
_fputts(lpMsgBuf, stderr);
LocalFree(lpMsgBuf);
}