forked from winfsp/sshfs-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshfs-win.c
252 lines (220 loc) · 6.37 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
/**
* sshfs-win.c
*
* Copyright 2015-2019 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 <fcntl.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static char *sshfs = "/bin/sshfs.exe";
static char *sshfs_environ[] = { "PATH=/bin", 0 };
#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[.r|k]\\[LOCUSER=]REMUSER@HOST[!PORT][\\PATH]\n"
" sshfs: remote home; sshfs.r: remote root\n"
" sshfs.k: remote home 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 int use_pass(const char *cls)
{
char regpath[256];
int regfd;
uint32_t value = -1;
snprintf(regpath, sizeof regpath,
"/proc/registry32/HKEY_LOCAL_MACHINE/Software/WinFsp/Services/%s/Credentials", cls);
regfd = open(regpath, O_RDONLY);
if (-1 != regfd)
{
read(regfd, &value, sizeof value);
close(regfd);
}
return 1 == value;
}
static int do_cmd(int argc, char *argv[])
{
if (200 < argc)
usage();
char *sshfs_argv[256] =
{
sshfs, 0,
};
concat_argv(sshfs_argv, argv + 1);
execve(sshfs, sshfs_argv, sshfs_environ);
return 1;
}
static int do_svc(int argc, char *argv[])
{
#define SSHFS_ARGS \
"-f", \
"-orellinks", \
"-ofstypename=SSHFS", \
"-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 (\\sshfs\, \\sshfs.r\ or \\sshfs.k\) */
p = argv[1];
while ('/' == *p)
p++;
cls = p;
while (*p && '/' != *p)
p++;
if (*p)
*p++ = '\0';
while ('/' == *p)
p++;
root = 0 == strcmp("sshfs.r", cls) ? "/" : "";
/* 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
{
/* translate backslash to '+' */
locuser = argv[3];
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 (use_pass(cls))
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);
execve(sshfs, sshfs_argv, sshfs_environ);
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;
}