/
winnfsd.cpp
270 lines (244 loc) · 6.29 KB
/
winnfsd.cpp
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
#include "RPCServer.h"
#include "PortmapProg.h"
#include "NFSProg.h"
#include "MountProg.h"
#include "ServerSocket.h"
#include "DatagramSocket.h"
#include <stdio.h>
#define SOCKET_NUM 3
enum
{
PORTMAP_PORT = 111,
MOUNT_PORT = 1058,
NFS_PORT = 2049
};
enum
{
PROG_PORTMAP = 100000,
PROG_NFS = 100003,
PROG_MOUNT = 100005
};
static unsigned int g_nUID, g_nGID;
static bool g_bLogOn;
static CRPCServer g_RPCServer;
static CPortmapProg g_PortmapProg;
static CNFSProg g_NFSProg;
static CMountProg g_MountProg;
static map<string, string> g_WinPathsOfName;
static void printUsage(char *pExe)
{
printf("\n");
printf("Usage: %s [-id <uid> <gid>] [-log on | off] export_path>exportdir|export_path>exportdir\n", pExe);
printf("For example:\n");
printf("On Windows> %s d:\\one>/one|d:\\two>/two\n", pExe);
printf("On Linux> mount -t nfs 192.168.12.34:/d/work mount\n");
}
static void printLine(void)
{
printf("=====================================================\n");
}
static void printAbout(void)
{
printLine();
printf("WinNFSd v2.0\n");
printf("Network File System server for Windows\n");
printf("Copyright (C) 2005 Ming-Yang Kao\n");
printLine();
}
static void printHelp(void)
{
printLine();
printf("Commands:\n");
printf("about: display messages about this program\n");
printf("help: display help\n");
printf("log on/off: display log messages or not\n");
printf("list: list mounted clients\n");
printf("quit: quit this program\n");
printLine();
}
static void printCount(void)
{
int nNum;
nNum = g_MountProg.GetMountNumber();
if (nNum == 0)
printf("There is no client mounted.\n");
else if (nNum == 1)
printf("There is 1 client mounted.\n");
else
printf("There are %d clients mounted.\n", nNum);
}
static void printList(void)
{
int i, nNum;
printLine();
nNum = g_MountProg.GetMountNumber();
for (i = 0; i < nNum; i++)
printf("%s\n", g_MountProg.GetClientAddr(i));
printCount();
printLine();
}
static void printConfirmQuit(void)
{
printf("\n");
printCount();
printf("Are you sure to quit? (y/N): ");
}
static void inputCommand(void)
{
char command[20];
printf("Type 'help' to see help\n\n");
while (true)
{
fgets(command, 20, stdin);
if (command[strlen(command) - 1] == '\n')
command[strlen(command) - 1] = '\0';
if (stricmp(command, "about") == 0)
printAbout();
else if (stricmp(command, "help") == 0)
printHelp();
else if (stricmp(command, "log on") == 0)
g_RPCServer.SetLogOn(true);
else if (stricmp(command, "log off") == 0)
g_RPCServer.SetLogOn(false);
else if (stricmp(command, "list") == 0)
printList();
else if (stricmp(command, "quit") == 0)
{
if (g_MountProg.GetMountNumber() == 0)
break;
else
{
printConfirmQuit();
fgets(command, 20, stdin);
if (command[0] == 'y' || command[0] == 'Y')
break;
}
}
else if (stricmp(command, "reset") == 0)
g_RPCServer.Set(PROG_NFS, NULL);
else if (strcmp(command, "") != 0)
{
printf("Unknown command: '%s'\n", command);
printf("Type 'help' to see help\n");
}
}
}
static void start(char *path)
{
int i;
CDatagramSocket DatagramSockets[SOCKET_NUM];
CServerSocket ServerSockets[SOCKET_NUM];
bool bSuccess;
hostent *localHost;
g_PortmapProg.Set(PROG_MOUNT, MOUNT_PORT); //map port for mount
g_PortmapProg.Set(PROG_NFS, NFS_PORT); //map port for nfs
g_NFSProg.SetUserID(g_nUID, g_nGID); //set uid and gid of files
//g_MountProg.Export(path, "a"); //export path for mount
char winPath[MAXPATHLEN] = {0};
char name[MAXPATHLEN] = {0};
char *begin = path;
while (*path != '\0' && (path - begin) < MAXPATHLEN )
{
int j, k;
for (j = 0; *path != '\0' && j < MAXPATHLEN; path++, j++)
{
if (*path == '=')
{
path++;
break;
}
winPath[j] = *path;
}
winPath[j] = '\0';
for (k = 0; *path != '\0' && k < MAXPATHLEN; path++, k++)
{
if (*path == ';')
{
name[k] = '\0';
path++;
break;
}
name[k] = *path;
}
name[k] = '\0';
if (name[0] && winPath[0])
g_MountProg.Export(winPath, name);
}
g_RPCServer.Set(PROG_PORTMAP, &g_PortmapProg); //program for portmap
g_RPCServer.Set(PROG_NFS, &g_NFSProg); //program for nfs
g_RPCServer.Set(PROG_MOUNT, &g_MountProg); //program for mount
g_RPCServer.SetLogOn(g_bLogOn);
for (i = 0; i < SOCKET_NUM; i++)
{
DatagramSockets[i].SetListener(&g_RPCServer);
ServerSockets[i].SetListener(&g_RPCServer);
}
bSuccess = false;
if (ServerSockets[0].Open(PORTMAP_PORT, 3) && DatagramSockets[0].Open(PORTMAP_PORT)) //start portmap daemon
{
printf("Portmap daemon started\n");
if (ServerSockets[1].Open(NFS_PORT, 10) && DatagramSockets[1].Open(NFS_PORT)) //start nfs daemon
{
printf("NFS daemon started\n");
if (ServerSockets[2].Open(MOUNT_PORT, 3) && DatagramSockets[2].Open(MOUNT_PORT)) //start mount daemon
{
printf("Mount daemon started\n");
bSuccess = true; //all daemon started
}
else
printf("Mount daemon starts failed.\n");
}
else
printf("NFS daemon starts failed.\n");
}
else
printf("Portmap daemon starts failed.\n");
if (bSuccess)
{
localHost = gethostbyname("");
printf("Local IP = %s\n", inet_ntoa (*(struct in_addr *)*localHost->h_addr_list)); //local address
inputCommand(); //wait for commands
}
for (i = 0; i < SOCKET_NUM; i++)
{
DatagramSockets[i].Close();
ServerSockets[i].Close();
}
}
int main(int argc, char *argv[])
{
int i;
char *pPath;
WSADATA wsaData;
printAbout();
if (argc < 2)
{
pPath = strrchr(argv[0], '\\');
pPath = pPath == NULL? argv[0] : pPath + 1;
printUsage(pPath);
return 1;
}
pPath = argv[argc - 1]; //path is the last parameter
if (*pPath == '"')
++pPath; //remove head "
if (*(pPath + strlen(pPath) - 1) == '"')
*(pPath + strlen(pPath) - 1) = '\0'; //remove tail "
g_nUID = g_nGID = 0;
g_bLogOn = true;
for (i = 1; i < argc - 1; i++) //parse parameters
{
if (stricmp(argv[i], "-id") == 0)
{
g_nUID = atoi(argv[++i]);
g_nGID = atoi(argv[++i]);
}
else if (stricmp(argv[i], "-log") == 0)
{
g_bLogOn = stricmp(argv[++i], "off") != 0;
}
}
WSAStartup(0x0101, &wsaData);
start(pPath);
WSACleanup();
return 0;
}