xref: /haiku/src/bin/network/mount_nfs/mount_nfs.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 #include <string.h>
2 #include <netdb.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <Application.h>
7 #include <Roster.h>
8 #include <unistd.h>
9 #include <signal.h>
10 #include <sys/socket.h>
11 #include <arpa/inet.h>
12 #ifdef __HAIKU__
13 #include <fs_volume.h>
14 int mount(const char *filesystem, const char *where, const char *device, ulong flags, void *parameters, size_t len)
15 {
16 	(void) len;
17 	return fs_mount_volume(where, device, filesystem, flags, (const char *)parameters);
18 }
19 #endif
20 
21 #define BUFSZ 1024
22 
23 struct mount_nfs_params
24 {
25 	unsigned int serverIP;
26 	char *server;
27 	char *_export;
28 	uid_t uid;
29 	gid_t gid;
30 	char *hostname;
31 };
32 
33 void usage (const char *exename);
34 
35 void usage (const char *exename)
36 {
37 	printf ("usage: %s server:export mountpoint uid gid\n",exename);
38 }
39 
40 int main (int argc, char **argv)
41 {
42 	char buf[BUFSZ];
43 	signal(SIGINT, SIG_IGN);
44 	signal(SIGHUP, SIG_IGN);
45 
46 	//BApplication theApp ("application/x-vnd.barecode-mount_nfs");
47 
48 	if (argc!=5)
49 	{
50 		usage(argv[0]);
51 		return 1;
52 	}
53 
54 	struct stat st;
55 	if (stat(argv[2],&st)<B_NO_ERROR)
56 	{
57 		printf ("mountpoint does not exist\n");
58 		return 1;
59 	}
60 
61 	char *colon=strchr(argv[1],':');
62 
63 	if (colon==NULL)
64 	{
65 		usage(argv[0]);
66 		return 1;
67 	}
68 
69 	int serverLength=colon-argv[1];
70 	char *server=new char[serverLength+1];
71 	memcpy (server,argv[1],serverLength);
72 	server[serverLength]=0;
73 
74 	hostent *ent;
75 
76 	ent=gethostbyname (server);
77 
78 	if (ent==NULL)
79 	{
80 		printf ("could not get server ip\n");
81 		delete[] server;
82 		return 1;
83 	}
84 
85 	unsigned int serverIP=ntohl(*((unsigned int *)ent->h_addr));
86 
87 	mount_nfs_params params;
88 
89 	params.serverIP=serverIP;
90 	params.server=server;
91 	params._export=colon+1;
92 
93 	sscanf (argv[3],"%d",&params.uid);
94 	sscanf (argv[4],"%d",&params.gid);
95 
96 	char hostname[256];
97 	gethostname (hostname,256);
98 
99 	params.hostname=hostname;
100 
101 	sprintf(buf, "nfs:%s:%s,uid=%u,gid=%u,hostname=%s",
102 		inet_ntoa(*((struct in_addr *)ent->h_addr)),
103 		params._export,
104 		params.uid,
105 		params.gid,
106 		params.hostname);
107 
108 	int result=mount ("nfs",argv[2],NULL,0,buf,strlen(buf));
109 	//int result=mount ("nfs",argv[2],NULL,0,&params,sizeof(params));
110 
111 	delete[] server;
112 
113 	if (result<B_NO_ERROR)
114 	{
115 		printf ("mount failed (%s)\n",strerror(result));
116 		return 1;
117 	}
118 
119 	return 0;
120 }
121