Merge branch 'vendor/NCURSES'
[dragonfly.git] / usr.sbin / puffs / mount_psshfs / fs.c
1 /*      $NetBSD: fs.c,v 1.24 2011/06/22 04:03:23 mrg Exp $      */
2
3 /*
4  * Copyright (c) 2006-2009  Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <err.h>
29 #include <errno.h>
30 #include <puffs.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #include "psshfs.h"
37 #include "sftp_proto.h"
38
39 #define DO_IO(fname, a1, a2, a3, a4, rv)                                \
40 do {                                                                    \
41         puffs_framebuf_seekset(a2, 0);                                  \
42         *(a4) = 0;                                                      \
43         rv = fname(a1, a2, a3, a4);                                     \
44         if (rv) {                                                       \
45                 return rv ? rv : EPROTO;                                \
46         }                                                               \
47 } while (/*CONSTCOND*/0)
48
49 #define reterr(str, rv)                                                 \
50 do {                                                                    \
51         fprintf str;                                                    \
52         return rv;                                                      \
53 } while (/*CONSTCOND*/0)
54
55 /* openssh extensions */
56 static const struct extunit {
57         const char *ext;
58         const char *val;
59         int extflag;
60 } exttable[] = {
61 {
62         "posix-rename@openssh.com",
63         "1",
64         SFTP_EXT_POSIX_RENAME,
65 },{
66         "statvfs@openssh.com",
67         "2",
68         SFTP_EXT_STATVFS,
69 },{
70         "fstatvfs@openssh.com",
71         "2",
72         SFTP_EXT_FSTATVFS,
73 },{
74         NULL,
75         NULL,
76         0
77 }};
78
79 int
80 psshfs_handshake(struct puffs_usermount *pu, int fd)
81 {
82         struct psshfs_ctx *pctx = puffs_getspecific(pu);
83         struct puffs_framebuf *pb;
84         struct puffs_pathobj *po_root;
85         struct puffs_node *pn_root;
86         struct vattr va, *rva;
87         const struct extunit *extu;
88         char *rootpath;
89         char *ext, *val;
90         uint32_t count;
91         int rv, done;
92
93         pb = psbuf_makeout();
94         psbuf_put_1(pb, SSH_FXP_INIT);
95         psbuf_put_4(pb, SFTP_PROTOVERSION);
96         DO_IO(psbuf_write, pu, pb, fd, &done, rv);
97
98         puffs_framebuf_recycle(pb);
99         DO_IO(psbuf_read, pu, pb, fd, &done, rv);
100         if (psbuf_get_type(pb) != SSH_FXP_VERSION)
101                 reterr((stderr, "invalid server response: %d",
102                     psbuf_get_type(pb)), EPROTO);
103         pctx->protover = psbuf_get_reqid(pb);
104
105         /*
106          * Check out which extensions are available.  Currently
107          * we are only interested in the openssh statvfs extension.
108          */
109         for (;;) {
110                 if (psbuf_get_str(pb, &ext, NULL) != 0)
111                         break;
112                 if (psbuf_get_str(pb, &val, NULL) != 0)
113                         break;
114
115                 for (extu = exttable; extu->ext; extu++)
116                         if (strcmp(ext, extu->ext) == 0
117                             && strcmp(val, extu->val) == 0)
118                                 pctx->extensions |= extu->extflag;
119         }
120
121         /* scope out our rootpath */
122         psbuf_recycleout(pb);
123         psbuf_put_1(pb, SSH_FXP_REALPATH);
124         psbuf_put_4(pb, NEXTREQ(pctx));
125         psbuf_put_str(pb, pctx->mountpath);
126         DO_IO(psbuf_write, pu, pb, fd, &done, rv);
127
128         puffs_framebuf_recycle(pb);
129         DO_IO(psbuf_read, pu, pb, fd, &done, rv);
130         if (psbuf_get_type(pb) != SSH_FXP_NAME)
131                 reterr((stderr, "invalid server realpath response for \"%s\"",
132                     pctx->mountpath), EPROTO);
133         if (psbuf_get_4(pb, &count) == -1)
134                 reterr((stderr, "invalid realpath response: count"), EPROTO);
135         if (psbuf_get_str(pb, &rootpath, NULL) == -1)
136                 reterr((stderr, "invalid realpath response: rootpath"), EPROTO);
137
138         /* stat the rootdir so that we know it's a dir */
139         psbuf_recycleout(pb);
140         psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
141         DO_IO(psbuf_write, pu, pb, fd, &done, rv);
142
143         puffs_framebuf_recycle(pb);
144         DO_IO(psbuf_read, pu, pb, fd, &done, rv);
145
146         rv = psbuf_expect_attrs(pb, &va);
147         if (rv)
148                 reterr((stderr, "couldn't stat rootpath"), rv);
149         puffs_framebuf_destroy(pb);
150
151         if (puffs_mode2vt(va.va_mode) != VDIR)
152                 reterr((stderr, "remote path (%s) not a directory", rootpath),
153                     ENOTDIR);
154
155         pn_root = puffs_getroot(pu);
156         rva = &pn_root->pn_va;
157         puffs_setvattr(rva, &va);
158
159         po_root = puffs_getrootpathobj(pu);
160         if (po_root == NULL)
161                 err(1, "getrootpathobj");
162         po_root->po_path = rootpath;
163         po_root->po_len = strlen(rootpath);
164
165         return 0;
166 }
167
168 int
169 psshfs_fs_statvfs(struct puffs_usermount *pu, struct statvfs *sbp)
170 {
171         PSSHFSAUTOVAR(pu);
172         uint64_t tmpval;
173         uint8_t type;
174
175         memset(sbp, 0, sizeof(*sbp));
176         sbp->f_bsize = sbp->f_frsize = 512;
177
178         if ((pctx->extensions & SFTP_EXT_STATVFS) == 0)
179                 goto out;
180
181         psbuf_req_str(pb, SSH_FXP_EXTENDED, reqid, "statvfs@openssh.com");
182         psbuf_put_str(pb, pctx->mountpath);
183         GETRESPONSE(pb, pctx->sshfd);
184
185         type = psbuf_get_type(pb);
186         if (type != SSH_FXP_EXTENDED_REPLY) {
187                 /* use the default */
188                 goto out;
189         }
190
191         psbuf_get_8(pb, &tmpval);
192         sbp->f_bsize = tmpval;
193         psbuf_get_8(pb, &tmpval);
194         sbp->f_frsize = tmpval;
195         psbuf_get_8(pb, &sbp->f_blocks);
196         psbuf_get_8(pb, &sbp->f_bfree);
197         psbuf_get_8(pb, &sbp->f_bavail);
198         psbuf_get_8(pb, &sbp->f_files);
199         psbuf_get_8(pb, &sbp->f_ffree);
200         psbuf_get_8(pb, &sbp->f_favail);
201
202         psbuf_get_8(pb, &tmpval); /* fsid */
203         psbuf_get_8(pb, &tmpval); /* flag */
204         psbuf_get_8(pb, &tmpval);
205         sbp->f_namemax = tmpval;
206
207 #ifdef XXXDF
208         sbp->f_bresvd = sbp->f_bfree - sbp->f_bavail;
209         sbp->f_fresvd = sbp->f_ffree - sbp->f_favail;
210 #endif
211
212  out:
213         PSSHFSRETURN(rv);
214 }
215
216 int
217 psshfs_fs_unmount(struct puffs_usermount *pu, int flags)
218 {
219         struct psshfs_ctx *pctx = puffs_getspecific(pu);
220
221         kill(pctx->sshpid, SIGTERM);
222         close(pctx->sshfd);
223         if (pctx->numconnections == 2) {
224                 kill(pctx->sshpid_data, SIGTERM);
225                 close(pctx->sshfd_data);
226         }
227
228         return 0;
229 }
230
231 int
232 psshfs_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie,
233         void *fid, size_t *fidsize)
234 {
235         struct psshfs_ctx *pctx = puffs_getspecific(pu);
236         struct puffs_node *pn = cookie;
237         struct psshfs_node *psn = pn->pn_data;
238         struct psshfs_fid *pf = fid;
239
240         pf->mounttime = pctx->mounttime;
241         pf->node = pn;
242
243         psn->stat |= PSN_HASFH;
244
245         return 0;
246 }
247
248 int
249 psshfs_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
250         struct puffs_newinfo *pni)
251 {
252         struct psshfs_ctx *pctx = puffs_getspecific(pu);
253         struct psshfs_fid *pf = fid;
254         struct puffs_node *pn = pf->node;
255         struct psshfs_node *psn;
256         int rv;
257
258         if (pf->mounttime != pctx->mounttime)
259                 return EINVAL;
260         if (pn == NULL)
261                 return EINVAL;
262         psn = pn->pn_data;
263         if ((psn->stat & PSN_HASFH) == 0)
264                 return EINVAL;
265
266         /* update node attributes */
267         rv = getnodeattr(pu, pn, NULL);
268         if (rv)
269                 return EINVAL;
270
271         puffs_newinfo_setcookie(pni, pn);
272         puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
273         puffs_newinfo_setsize(pni, pn->pn_va.va_size);
274
275         return 0;
276 }