aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/9import.c
blob: c74ffb50d0bc4dc7cc132f7ee493611bfa5ba2ec (plain)
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
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <thread.h>

enum {
	Encnone,
	Encssl,
	Enctls,
};

static char *encprotos[] = {
	[Encnone] =	"clear",
	[Encssl] =	"ssl",
	[Enctls] = 	"tls",
			nil,
};

char		*keyspec = "";
char		*filterp;
char		*ealgs = "rc4_256 sha1";
int		encproto = Encnone;
AuthInfo 	*ai;
int		debug;
int		doauth = 1;
int		timedout;

int	connectez(char*, char*);
void	sysfatal(char*, ...);
void	usage(void);
int	filter(int, char *, char *);

int
catcher(void *v, char *msg)
{
	timedout = 1;
	if(strcmp(msg, "alarm") == 0)
		return 1;
	return 0;
}

static int
lookup(char *s, char *l[])
{
	int i;

	for (i = 0; l[i] != 0; i++)
		if (strcmp(l[i], s) == 0)
			return i;
	return -1;
}

static char*
srvname(char *addr)
{
	int i;

	for(i=0; i<strlen(addr); i++){
		if(addr[i] == '!')
			addr[i] = ':';
	}
	return addr;
}

void
threadmain(int argc, char **argv)
{
	char *mntpt, *srvpost, srvfile[64];
	int fd;

	quotefmtinstall();
	srvpost = nil;
	ARGBEGIN{
	case 'A':
		doauth = 0;
		break;
	case 'd':
		debug++;
		break;
	case 'E':
		if ((encproto = lookup(EARGF(usage()), encprotos)) < 0)
			usage();
		break;
	case 'e':
		ealgs = EARGF(usage());
		if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
			ealgs = nil;
		break;
	case 'k':
		keyspec = EARGF(usage());
		break;
	case 'p':
		filterp = unsharp("#9/bin/aan");
		break;
	case 's':
		srvpost = EARGF(usage());
		break;
	default:
		usage();
	}ARGEND;

	mntpt = 0;		/* to shut up compiler */
	switch(argc) {
	case 2:
		mntpt = argv[1];
		break;
	case 3:
		mntpt = argv[2];
		break;
	default:
		usage();
	}

	if(encproto != Encnone)
		sysfatal("%s: tls and ssl have not yet been implemented", argv[0]);

	threadnotify(catcher, 1);
	alarm(60*1000);

	fd = connectez(argv[0], argv[1]);

	fprint(fd, "impo %s %s\n", filterp? "aan": "nofilter",
		encprotos[encproto]);

	if (filterp)
		fd = filter(fd, filterp, argv[0]);

	if(srvpost == nil)
		 srvpost = srvname(argv[0]);
	sprint(srvfile, "%s", srvpost);

	if(post9pservice(fd, srvfile, mntpt) < 0)
		sysfatal("can't post %s: %r", argv[1]);
	alarm(0);

	threadexitsall(0);
}

/* the name "connect" is special */
int
connectez(char *system, char *tree)
{
	char buf[ERRMAX], *na;
	int fd, n;
	char *authp;

	na = netmkaddr(system, "tcp", "exportfs");
	threadsetname("dial %s", na);
	if((fd = dial(na, nil, nil, nil)) < 0)
		sysfatal("can't dial %s: %r", system);

	if(doauth){
		authp = "p9any";
		threadsetname("auth_proxy auth_getkey proto=%q role=client %s",
			authp, keyspec);
		ai = auth_proxy(fd, auth_getkey, "proto=%q role=client %s",
			authp, keyspec);
		if(ai == nil)
			sysfatal("%r: %s", system);
	}

	threadsetname("writing tree name %s", tree);
	n = write(fd, tree, strlen(tree));
	if(n < 0)
		sysfatal("can't write tree: %r");

	strcpy(buf, "can't read tree");

	threadsetname("awaiting OK for %s", tree);
	n = read(fd, buf, sizeof buf - 1);
	if(n!=2 || buf[0]!='O' || buf[1]!='K'){
		if (timedout)
			sysfatal("timed out connecting to %s", na);
		buf[sizeof buf - 1] = '\0';
		sysfatal("bad remote tree: %s", buf);
	}

	return fd;
}

void
usage(void)
{
	fprint(2, "usage: 9import [-A] [-E clear|ssl|tls] "
"[-e 'crypt auth'|clear] [-k keypattern] [-p] [-s srv] host remotefs [mountpoint]\n");
	threadexitsall("usage");
}

/* Network on fd1, mount driver on fd0 */
int
filter(int fd, char *cmd, char *host)
{
	int p[2], len, argc;
	char newport[256], buf[256], *s;
	char *argv[16], *file, *pbuf;

	if ((len = read(fd, newport, sizeof newport - 1)) < 0)
		sysfatal("filter: cannot write port; %r");
	newport[len] = '\0';

	if ((s = strchr(newport, '!')) == nil)
		sysfatal("filter: illegally formatted port %s", newport);

	strecpy(buf, buf+sizeof buf, netmkaddr(host, "tcp", "0"));
	pbuf = strrchr(buf, '!');
	strecpy(pbuf, buf+sizeof buf, s);

	if(debug)
		fprint(2, "filter: remote port %s\n", newport);

	argc = tokenize(cmd, argv, nelem(argv)-2);
	if (argc == 0)
		sysfatal("filter: empty command");
	argv[argc++] = "-c";
	argv[argc++] = buf;
	argv[argc] = nil;
	file = argv[0];
	if (s = strrchr(argv[0], '/'))
		argv[0] = s+1;

	if(pipe(p) < 0)
		sysfatal("pipe: %r");

	switch(rfork(RFNOWAIT|RFPROC|RFFDG)) {
	case -1:
		sysfatal("rfork record module: %r");
	case 0:
		dup(p[0], 1);
		dup(p[0], 0);
		close(p[0]);
		close(p[1]);
		exec(file, argv);
		sysfatal("exec record module: %r");
	default:
		close(fd);
		close(p[0]);
	}
	return p[1];
}