Implement checkInJail()
[armadillo.git] / src / paths.go
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
4 //
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
8 //
9
10 package paths
11
12 import (
13 "container/vector"
14 "os"
15 "path"
16 "strings"
17 )
18
19 var JailRoot string;
20
21 func canonicalizePath(raw_path string) string {
22 raw_path = path.Join(JailRoot, raw_path)
23 return path.Clean(raw_path)
24 }
25
26 func checkInJail(the_path string) bool {
27 if len(the_path) < len(JailRoot) {
28 return false
29 }
30 if the_path[0:len(JailRoot)] != JailRoot {
31 return false
32 }
33 if strings.Index(the_path, "../") != -1 {
34 return false
35 }
36 return true
37 }
38
39 func List(the_path string) (files vector.StringVector, err os.Error) {
40 full_path := canonicalizePath(the_path)
41 if !checkInJail(full_path) {
42 return nil, os.NewError("path outside of jail")
43 }
44
45 fd, file_error := os.Open(full_path, os.O_RDONLY, 0)
46 if file_error != nil {
47 return nil, file_error
48 }
49
50 fileinfos, read_err := fd.Readdir(-1)
51 if read_err != nil {
52 return nil, read_err
53 }
54
55 for _, info := range fileinfos {
56 name := info.Name
57 if info.IsDirectory() {
58 name += "/"
59 }
60 files.Push(name)
61 }
62 return files, nil
63 }