-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathdav.go
287 lines (255 loc) · 9.75 KB
/
dav.go
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
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package ocdav
import (
"context"
"net/http"
"path"
"strings"
gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/pkg/rhttp/router"
"google.golang.org/grpc/metadata"
)
type tokenStatInfoKey struct{}
// DavHandler routes to the different sub handlers
type DavHandler struct {
AvatarsHandler *AvatarsHandler
FilesHandler *WebDavHandler
FilesHomeHandler *WebDavHandler
MetaHandler *MetaHandler
TrashbinHandler *TrashbinHandler
SpacesHandler *SpacesHandler
PublicFolderHandler *WebDavHandler
PublicFileHandler *PublicFileHandler
}
func (h *DavHandler) init(c *Config) error {
h.AvatarsHandler = new(AvatarsHandler)
if err := h.AvatarsHandler.init(c); err != nil {
return err
}
h.FilesHandler = new(WebDavHandler)
if err := h.FilesHandler.init(c.FilesNamespace, false); err != nil {
return err
}
h.FilesHomeHandler = new(WebDavHandler)
if err := h.FilesHomeHandler.init(c.WebdavNamespace, true); err != nil {
return err
}
h.MetaHandler = new(MetaHandler)
if err := h.MetaHandler.init(c); err != nil {
return err
}
h.TrashbinHandler = new(TrashbinHandler)
h.SpacesHandler = new(SpacesHandler)
if err := h.SpacesHandler.init(c); err != nil {
return err
}
h.PublicFolderHandler = new(WebDavHandler)
if err := h.PublicFolderHandler.init("public", true); err != nil { // jail public file requests to /public/ prefix
return err
}
h.PublicFileHandler = new(PublicFileHandler)
if err := h.PublicFileHandler.init("public"); err != nil { // jail public file requests to /public/ prefix
return err
}
return h.TrashbinHandler.init(c)
}
func isOwner(userIDorName string, user *userv1beta1.User) bool {
return userIDorName != "" && (userIDorName == user.Id.OpaqueId || strings.EqualFold(userIDorName, user.Username))
}
// Handler handles requests
func (h *DavHandler) Handler(s *svc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := appctx.GetLogger(ctx)
// if there is no file in the request url we assume the request url is: "/remote.php/dav/files"
// https://github.com/owncloud/core/blob/18475dac812064b21dabcc50f25ef3ffe55691a5/tests/acceptance/features/apiWebdavOperations/propfind.feature
if r.URL.Path == "/files" {
log.Debug().Str("path", r.URL.Path).Msg("method not allowed")
contextUser, ok := ctxpkg.ContextGetUser(ctx)
if ok {
r.URL.Path = path.Join(r.URL.Path, contextUser.Username)
}
if r.Header.Get("Depth") == "" {
w.WriteHeader(http.StatusMethodNotAllowed)
b, err := Marshal(exception{
code: SabredavMethodNotAllowed,
message: "Listing members of this collection is disabled",
})
if err != nil {
log.Error().Msgf("error marshaling xml response: %s", b)
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = w.Write(b)
if err != nil {
log.Error().Msgf("error writing xml response: %s", b)
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
}
var head string
head, r.URL.Path = router.ShiftPath(r.URL.Path)
switch head {
case "avatars":
h.AvatarsHandler.Handler(s).ServeHTTP(w, r)
case "files":
var requestUserID string
var oldPath = r.URL.Path
// detect and check current user in URL
requestUserID, r.URL.Path = router.ShiftPath(r.URL.Path)
// note: some requests like OPTIONS don't forward the user
contextUser, ok := ctxpkg.ContextGetUser(ctx)
if ok && isOwner(requestUserID, contextUser) {
// use home storage handler when user was detected
base := path.Join(ctx.Value(ctxKeyBaseURI).(string), "files", requestUserID)
ctx := context.WithValue(ctx, ctxKeyBaseURI, base)
r = r.WithContext(ctx)
h.FilesHomeHandler.Handler(s).ServeHTTP(w, r)
} else {
r.URL.Path = oldPath
base := path.Join(ctx.Value(ctxKeyBaseURI).(string), "files")
ctx := context.WithValue(ctx, ctxKeyBaseURI, base)
r = r.WithContext(ctx)
h.FilesHandler.Handler(s).ServeHTTP(w, r)
}
case "meta":
base := path.Join(ctx.Value(ctxKeyBaseURI).(string), "meta")
ctx = context.WithValue(ctx, ctxKeyBaseURI, base)
r = r.WithContext(ctx)
h.MetaHandler.Handler(s).ServeHTTP(w, r)
case "trash-bin":
base := path.Join(ctx.Value(ctxKeyBaseURI).(string), "trash-bin")
ctx := context.WithValue(ctx, ctxKeyBaseURI, base)
r = r.WithContext(ctx)
h.TrashbinHandler.Handler(s).ServeHTTP(w, r)
case "spaces":
base := path.Join(ctx.Value(ctxKeyBaseURI).(string), "spaces")
ctx := context.WithValue(ctx, ctxKeyBaseURI, base)
r = r.WithContext(ctx)
h.SpacesHandler.Handler(s).ServeHTTP(w, r)
case "public-files":
base := path.Join(ctx.Value(ctxKeyBaseURI).(string), "public-files")
ctx = context.WithValue(ctx, ctxKeyBaseURI, base)
c, err := pool.GetGatewayServiceClient(s.c.GatewaySvc)
if err != nil {
w.WriteHeader(http.StatusNotFound)
}
var res *gatewayv1beta1.AuthenticateResponse
token, _ := router.ShiftPath(r.URL.Path)
if _, pass, ok := r.BasicAuth(); ok {
res, err = handleBasicAuth(r.Context(), c, token, pass)
} else {
q := r.URL.Query()
sig := q.Get("signature")
expiration := q.Get("expiration")
// We restrict the pre-signed urls to downloads.
if sig != "" && expiration != "" && r.Method != http.MethodGet {
w.WriteHeader(http.StatusUnauthorized)
return
}
res, err = handleSignatureAuth(r.Context(), c, token, sig, expiration)
}
switch {
case err != nil:
w.WriteHeader(http.StatusInternalServerError)
return
case res.Status.Code == rpcv1beta1.Code_CODE_PERMISSION_DENIED:
fallthrough
case res.Status.Code == rpcv1beta1.Code_CODE_UNAUTHENTICATED:
w.WriteHeader(http.StatusUnauthorized)
return
case res.Status.Code == rpcv1beta1.Code_CODE_NOT_FOUND:
w.WriteHeader(http.StatusNotFound)
return
case res.Status.Code != rpcv1beta1.Code_CODE_OK:
w.WriteHeader(http.StatusInternalServerError)
return
}
ctx = ctxpkg.ContextSetToken(ctx, res.Token)
ctx = ctxpkg.ContextSetUser(ctx, res.User)
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, res.Token)
r = r.WithContext(ctx)
// the public share manager knew the token, but does the referenced target still exist?
sRes, err := getTokenStatInfo(ctx, c, token)
switch {
case err != nil:
log.Error().Err(err).Msg("error sending grpc stat request")
w.WriteHeader(http.StatusInternalServerError)
return
case sRes.Status.Code == rpc.Code_CODE_PERMISSION_DENIED:
fallthrough
case sRes.Status.Code == rpc.Code_CODE_NOT_FOUND:
log.Debug().Str("token", token).Interface("status", res.Status).Msg("resource not found")
w.WriteHeader(http.StatusNotFound) // log the difference
return
case sRes.Status.Code == rpc.Code_CODE_UNAUTHENTICATED:
log.Debug().Str("token", token).Interface("status", res.Status).Msg("unauthorized")
w.WriteHeader(http.StatusUnauthorized)
return
case sRes.Status.Code != rpc.Code_CODE_OK:
log.Error().Str("token", token).Interface("status", res.Status).Msg("grpc stat request failed")
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Debug().Interface("statInfo", sRes.Info).Msg("Stat info from public link token path")
if sRes.Info.Type != provider.ResourceType_RESOURCE_TYPE_CONTAINER {
ctx := context.WithValue(ctx, tokenStatInfoKey{}, sRes.Info)
r = r.WithContext(ctx)
h.PublicFileHandler.Handler(s).ServeHTTP(w, r)
} else {
h.PublicFolderHandler.Handler(s).ServeHTTP(w, r)
}
default:
w.WriteHeader(http.StatusNotFound)
b, err := Marshal(exception{
code: SabredavNotFound,
message: "File not found in root",
})
HandleWebdavError(log, w, b, err)
}
})
}
func getTokenStatInfo(ctx context.Context, client gatewayv1beta1.GatewayAPIClient, token string) (*provider.StatResponse, error) {
return client.Stat(ctx, &provider.StatRequest{Ref: &provider.Reference{Path: path.Join("/public", token)}})
}
func handleBasicAuth(ctx context.Context, c gatewayv1beta1.GatewayAPIClient, token, pw string) (*gatewayv1beta1.AuthenticateResponse, error) {
authenticateRequest := gatewayv1beta1.AuthenticateRequest{
Type: "publicshares",
ClientId: token,
ClientSecret: "password|" + pw,
}
return c.Authenticate(ctx, &authenticateRequest)
}
func handleSignatureAuth(ctx context.Context, c gatewayv1beta1.GatewayAPIClient, token, sig, expiration string) (*gatewayv1beta1.AuthenticateResponse, error) {
authenticateRequest := gatewayv1beta1.AuthenticateRequest{
Type: "publicshares",
ClientId: token,
ClientSecret: "signature|" + sig + "|" + expiration,
}
return c.Authenticate(ctx, &authenticateRequest)
}