forked from winfsp/sshfs-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshfs-win.c
337 lines (298 loc) · 8.65 KB
/
sshfs-win.c
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
/**
* sshfs-win.c
*
* Copyright 2015-2021 Bill Zissimopoulos
*/
/*
* This file is part of SSHFS-Win.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <assert.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static char *sshfs = "/usr/bin/sshfs.exe";
#if 0
#define execve pr_execv
static void pr_execv(const char *path, char *argv[], ...)
{
fprintf(stderr, "%s\n", path);
for (; 0 != *argv; argv++)
fprintf(stderr, " %s\n", *argv);
}
#endif
static void usage(void)
{
fprintf(stderr,
"usage: sshfs-win cmd SSHFS_COMMAND_LINE\n"
" SSHFS_COMMAND_LINE command line to pass to sshfs\n"
"\n"
"usage: sshfs-win svc PREFIX X: [LOCUSER] [SSHFS_OPTIONS]\n"
" PREFIX Windows UNC prefix (note single backslash)\n"
" \\sshfs[.SUFFIX]\\[LOCUSER=]REMUSER@HOST[!PORT][\\PATH]\n"
" sshfs: remote user home dir\n"
" sshfs.r: remote root dir\n"
" sshfs.k: remote user home dir with key authentication\n"
" sshfs.kr: remote root dir with key authentication\n"
" LOCUSER local user (DOMAIN+USERNAME)\n"
" REMUSER remote user\n"
" HOST remote host\n"
" PORT remote port\n"
" PATH remote path (relative to remote home or root)\n"
" X: mount drive\n"
" SSHFS_OPTIONS additional options to pass to SSHFS\n");
exit(2);
}
static void concat_argv(char *dst[], char *src[])
{
for (; 0 != *dst; dst++)
;
for (; 0 != (*dst = *src); dst++, src++)
;
}
static void opt_escape(const char *opt, char *escaped, size_t size)
{
const char *p = opt;
char *q = escaped, *endq = escaped + size;
for (; *p && endq > q + 1; p++, q++)
{
if (',' == *p || '\\' == *p)
*q++ = '\\';
*q = *p;
}
*q = '\0';
}
static uint32_t reg_get(const char *cls, const char *name)
{
char regpath[256];
int regfd;
uint32_t value = -1;
snprintf(regpath, sizeof regpath,
"/proc/registry32/HKEY_LOCAL_MACHINE/Software/WinFsp/Services/%s/%s", cls, name);
regfd = open(regpath, O_RDONLY);
if (-1 != regfd)
{
if (sizeof value != read(regfd, &value, sizeof value))
value = -1;
close(regfd);
}
return value;
}
static int fixenv_and_execv(const char *path, char **argv)
{
static char *default_environ[] = { "PATH=/bin", 0 };
extern char **environ;
char **oldenv, **oldp;
char **newenv, **newp;
size_t len, siz;
int res;
oldenv = environ;
siz = 0;
for (oldp = oldenv; *oldp; oldp++)
{
if (0 == strncmp(*oldp, "PATH=", 5))
{
len = strlen(*oldp + 5);
siz += len + sizeof "PATH=/usr/bin:";
}
else
{
len = strlen(*oldp);
siz += len + 1;
}
}
oldp++;
newenv = malloc((oldp - oldenv) * sizeof(char *) + siz);
if (0 != newenv)
{
siz = (oldp - oldenv) * sizeof(char *);
for (oldp = oldenv, newp = newenv; *oldp; oldp++, newp++)
{
*newp = (char *)newenv + siz;
if (0 == strncmp(*oldp, "PATH=", 5))
{
len = strlen(*oldp + 5);
siz += len + sizeof "PATH=/usr/bin:";
memcpy(*newp, "PATH=/usr/bin:", sizeof "PATH=/usr/bin:" - 1);
memcpy(*newp + sizeof "PATH=/usr/bin:" - 1, *oldp + 5, len + 1);
}
else
{
len = strlen(*oldp);
siz += len + 1;
memcpy(*newp, *oldp, len + 1);
}
}
*newp = 0;
}
else
{
newenv = default_environ;
newp = newenv + 1;
}
#if 1
res = execve(path, argv, newenv);
#else
char **p;
for (p = newenv; *p; p++)
printf("%s\n", *p);
assert(newp == p);
#endif
if (newenv != default_environ)
free(newenv);
return res;
}
static int do_cmd(int argc, char *argv[])
{
if (200 < argc)
usage();
char *sshfs_argv[256] =
{
sshfs, 0,
};
concat_argv(sshfs_argv, argv + 1);
fixenv_and_execv(sshfs, sshfs_argv);
return 1;
}
static int do_svc(int argc, char *argv[])
{
#define SSHFS_ARGS \
"-f", \
"-orellinks", \
"-ofstypename=SSHFS", \
"-o ssh_command=/usr/bin/ssh.exe", \
"-oUserKnownHostsFile=/dev/null", \
"-oStrictHostKeyChecking=no"
if (3 > argc || 200 < argc)
usage();
struct passwd *passwd = 0;
char idmap[64], authmeth[64 + 1024], volpfx[256], portopt[256], remote[256];
char escaped[1024];
char *cls, *locuser, *locuser_nodom, *userhost, *port, *root, *path, *p;
snprintf(volpfx, sizeof volpfx, "--VolumePrefix=%s", argv[1]);
/* translate backslash to forward slash */
for (p = argv[1]; *p; p++)
if ('\\' == *p)
*p = '/';
/* skip class name */
p = argv[1];
while ('/' == *p)
p++;
cls = p;
while (*p && '/' != *p)
p++;
if (*p)
*p++ = '\0';
while ('/' == *p)
p++;
root = 1 == reg_get(cls, "sshfs.rootdir") ? "/" : "";
/* parse instance name (syntax: [locuser=]user@host!port) */
locuser = locuser_nodom = 0;
userhost = p;
port = "22";
while (*p && '/' != *p)
{
if ('=' == *p)
{
*p = '\0';
locuser = userhost;
userhost = p + 1;
}
else if ('!' == *p)
{
*p = '\0';
port = p + 1;
}
p++;
}
if (*p)
*p++ = '\0';
path = p;
opt_escape(port, escaped, sizeof escaped);
snprintf(portopt, sizeof portopt, "-oPort=%s", escaped);
snprintf(remote, sizeof remote, "%s:%s%s", userhost, root, path);
/* get local user name */
if (0 == locuser)
{
if (3 >= argc)
{
p = userhost;
while (*p && '@' != *p)
p++;
if (*p)
{
*p = '\0';
locuser = userhost;
}
}
else
{
locuser = argv[3];
/* the Cygwin runtime does not remove double quotes around args with non-ASCII chars */
if (locuser[0] == '"')
{
size_t len = strlen(locuser);
if (len >= 2 && locuser[len - 1] == '"')
{
locuser[len - 1] = '\0'; /* remove the trailing quote... */
++locuser; /* ...and the heading one */
}
}
/* translate backslash to '+' */
for (p = locuser; *p; p++)
if ('\\' == *p)
{
*p = '+';
locuser_nodom = p + 1;
}
}
}
snprintf(idmap, sizeof idmap, "-ouid=-1,gid=-1");
if (0 != locuser)
{
/* get uid/gid from local user name */
passwd = getpwnam(locuser);
if (0 == passwd && 0 != locuser_nodom)
passwd = getpwnam(locuser_nodom);
if (0 != passwd)
snprintf(idmap, sizeof idmap, "-ouid=%d,gid=%d", passwd->pw_uid, passwd->pw_gid);
}
if (1 == reg_get(cls, "Credentials"))
snprintf(authmeth, sizeof authmeth,
"-opassword_stdin,password_stdout");
else if (0 != passwd)
{
opt_escape(passwd->pw_dir, escaped, sizeof escaped);
snprintf(authmeth, sizeof authmeth,
"-oPreferredAuthentications=publickey,IdentityFile=\"%s/.ssh/id_rsa\"", escaped);
}
else
snprintf(authmeth, sizeof authmeth,
"-oPreferredAuthentications=publickey");
char *sshfs_argv[256] =
{
sshfs, SSHFS_ARGS, idmap, authmeth, volpfx, portopt, remote, argv[2], 0,
};
if (4 <= argc)
concat_argv(sshfs_argv, argv + 4);
fixenv_and_execv(sshfs, sshfs_argv);
return 1;
#undef SSHFS_ARGS
}
int main(int argc, char *argv[])
{
const char *mode = argv[1];
if (0 != mode && 0 == strcmp("cmd", mode))
return do_cmd(argc - 1, argv + 1);
if (0 != mode && 0 == strcmp("svc", mode))
return do_svc(argc - 1, argv + 1);
usage();
return 1;
}