fs.c revision 1.4 1 /* $NetBSD: fs.c,v 1.4 2007/04/12 15:09:02 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2006 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 * 3. The name of the company nor the name of the author may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: fs.c,v 1.4 2007/04/12 15:09:02 pooka Exp $");
34 #endif /* !lint */
35
36 #include <err.h>
37 #include <puffs.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 #include "psshfs.h"
44 #include "sftp_proto.h"
45
46 int
47 psshfs_domount(struct puffs_usermount *pu)
48 {
49 struct statvfs sb;
50 struct psshfs_ctx *pctx = puffs_getspecific(pu);
51 struct psshfs_node *root = &pctx->psn_root;
52 struct puffs_pathobj *po_root;
53 struct puffs_node *pn_root;
54 struct vattr va;
55 struct vattr *rva;
56 struct psbuf *pb;
57 char *rootpath;
58 uint32_t count;
59 int rv;
60
61 pb = psbuf_make(PSB_OUT);
62 psbuf_put_1(pb, SSH_FXP_INIT);
63 psbuf_put_4(pb, SFTP_PROTOVERSION);
64
65 while ((rv = psbuf_write(pctx, pb)) != 1)
66 if (rv == -1)
67 err(1, "write handshake");
68
69 psbuf_destroy(pb);
70 pb = psbuf_make(PSB_IN);
71
72 while ((rv = psbuf_read(pctx, pb)) != 1)
73 if (rv == -1)
74 err(1, "read handshake response");
75
76 if (pb->type != SSH_FXP_VERSION)
77 errx(1, "invalid server response");
78 pctx->protover = pb->reqid;
79
80 /* might contain some other stuff, but we're not interested */
81
82 /* scope out our rootpath */
83 psbuf_recycle(pb, PSB_OUT);
84 psbuf_put_1(pb, SSH_FXP_REALPATH);
85 psbuf_put_4(pb, NEXTREQ(pctx));
86 psbuf_put_str(pb, pctx->mountpath);
87 while ((rv = psbuf_write(pctx, pb)) != 1)
88 if (rv == -1)
89 err(1, "realpath query");
90
91 psbuf_recycle(pb, PSB_IN);
92
93 while ((rv = psbuf_read(pctx, pb)) != 1)
94 if (rv == -1)
95 err(1, "read realpath query response");
96 if (pb->type != SSH_FXP_NAME)
97 errx(1, "invalid server realpath response for \"%s\"",
98 pctx->mountpath);
99
100 if (!psbuf_get_4(pb, &count))
101 errx(1, "invalid realpath response: count");
102 if (!psbuf_get_str(pb, &rootpath, NULL))
103 errx(1, "invalid realpath response: rootpath");
104
105 /* stat the rootdir so that we know it's a dir */
106 psbuf_recycle(pb, PSB_OUT);
107 psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
108 while ((rv == psbuf_write(pctx, pb)) != 1)
109 if (rv == -1)
110 errx(1, "lstat");
111
112 psbuf_recycle(pb, PSB_IN);
113
114 while ((rv = psbuf_read(pctx, pb)) != 1)
115 if (rv == -1)
116 errx(1, "read lstat response");
117
118 rv = psbuf_expect_attrs(pb, &va);
119 if (rv)
120 errx(1, "couldn't stat rootpath");
121 psbuf_destroy(pb);
122
123 if (puffs_mode2vt(va.va_mode) != VDIR)
124 errx(1, "remote path (%s) not a directory", rootpath);
125
126 pctx->nextino = 2;
127
128 memset(root, 0, sizeof(struct psshfs_node));
129 pn_root = puffs_pn_new(pu, root);
130 if (pn_root == NULL)
131 return errno;
132 puffs_setroot(pu, pn_root);
133
134 po_root = puffs_getrootpathobj(pu);
135 if (po_root == NULL)
136 err(1, "getrootpathobj");
137 po_root->po_path = rootpath;
138 po_root->po_len = strlen(rootpath);
139
140 rva = &pn_root->pn_va;
141 puffs_setvattr(rva, &va);
142 rva->va_fileid = pctx->nextino++;
143 rva->va_nlink = 0156; /* XXX */
144
145 puffs_zerostatvfs(&sb);
146 if (puffs_start(pu, pn_root, &sb) != 0)
147 return errno;
148
149 return 0;
150 }
151
152 int
153 psshfs_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
154 {
155 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
156 struct psshfs_ctx *pctx = puffs_getspecific(pu);
157
158 kill(pctx->sshpid, SIGTERM);
159 close(pctx->sshfd);
160 return 0;
161 }
162